001/*
002 * (C) Copyright 2006-20012 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Nuxeo - initial API and implementation
016 *
017 */
018
019package org.nuxeo.template.adapters;
020
021import java.io.Serializable;
022
023import org.apache.commons.logging.Log;
024import org.apache.commons.logging.LogFactory;
025import org.nuxeo.ecm.core.api.CoreSession;
026import org.nuxeo.ecm.core.api.DocumentModel;
027import org.nuxeo.template.api.adapters.TemplateBasedDocument;
028import org.nuxeo.template.api.adapters.TemplateSourceDocument;
029
030/**
031 * Base class for shared code bewteen the {@link TemplateBasedDocument} and the {@link TemplateSourceDocument}.
032 * 
033 * @author Tiry (tdelprat@nuxeo.com)
034 */
035public abstract class AbstractTemplateDocument implements Serializable {
036
037    private static final long serialVersionUID = 1L;
038
039    protected static Log log = LogFactory.getLog(AbstractTemplateDocument.class);
040
041    protected DocumentModel adaptedDoc;
042
043    protected CoreSession getSession() {
044        if (adaptedDoc == null) {
045            return null;
046        }
047        return adaptedDoc.getCoreSession();
048    }
049
050    public DocumentModel getAdaptedDoc() {
051        return adaptedDoc;
052    }
053
054    protected void doSave() {
055        adaptedDoc = getSession().saveDocument(adaptedDoc);
056    }
057
058    public DocumentModel save() {
059        doSave();
060        return adaptedDoc;
061    }
062}