001/*
002 * (C) Copyright 2006-2009 Nuxeo SA (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
016 */
017
018package org.nuxeo.ecm.platform.publisher.api;
019
020import java.io.Serializable;
021import java.util.HashMap;
022import java.util.List;
023import java.util.Map;
024
025import org.nuxeo.ecm.core.api.CoreSession;
026import org.nuxeo.ecm.core.api.DocumentModel;
027import org.nuxeo.ecm.core.api.VersioningOption;
028import org.nuxeo.ecm.core.api.event.CoreEventConstants;
029import org.nuxeo.ecm.core.api.event.DocumentEventCategories;
030import org.nuxeo.ecm.core.event.Event;
031import org.nuxeo.ecm.core.event.EventProducer;
032import org.nuxeo.ecm.core.event.impl.DocumentEventContext;
033import org.nuxeo.ecm.platform.publisher.rules.ValidatorsRule;
034import org.nuxeo.runtime.api.Framework;
035
036public abstract class AbstractBasePublishedDocumentFactory implements PublishedDocumentFactory {
037
038    public static final String ENABLE_SNAPSHOT = "enableSnapshot";
039
040    public static final String TARGET_PUBLISHED_DOCUMENT_STATE = "targetPublishedDocumentState";
041
042    protected CoreSession coreSession;
043
044    protected Map<String, String> parameters;
045
046    protected PublicationTree publicationTree;
047
048    protected ValidatorsRule validatorsRule;
049
050    protected EventProducer eventProducer;
051
052    public void init(CoreSession coreSession, ValidatorsRule validatorsRule, Map<String, String> parameters)
053            {
054        this.coreSession = coreSession;
055        this.parameters = parameters;
056        this.validatorsRule = validatorsRule;
057        if (this.parameters == null) {
058            this.parameters = new HashMap<String, String>();
059        }
060    }
061
062    public void init(CoreSession coreSession, Map<String, String> parameters) {
063        init(coreSession, null, parameters);
064    }
065
066    public String getName() {
067        return this.getClass().getSimpleName();
068    }
069
070    protected String getParameter(String name) {
071        return parameters.get(name);
072    }
073
074    protected boolean isSnapshotingEnabled() {
075        String snap = getParameter(ENABLE_SNAPSHOT);
076        if (snap == null) {
077            return false;
078        } else {
079            return snap.equalsIgnoreCase("true");
080        }
081    }
082
083    protected String getTargetPublishedDocumentState() {
084        return getParameter(TARGET_PUBLISHED_DOCUMENT_STATE);
085    }
086
087    public PublishedDocument publishDocument(DocumentModel doc, PublicationNode targetNode) {
088        return publishDocument(doc, targetNode, null);
089    }
090
091    protected boolean needToVersionDocument(DocumentModel doc) {
092        if (!doc.isVersion() && doc.isVersionable()) {
093            return true;
094        }
095        return false;
096    }
097
098    public DocumentModel snapshotDocumentBeforePublish(DocumentModel doc) {
099
100        if (isSnapshotingEnabled() && needToVersionDocument(doc)) {
101            if (doc.isCheckedOut()) {
102                doc.checkIn(VersioningOption.MINOR, null);
103            }
104            coreSession.save();
105            List<DocumentModel> versions = coreSession.getVersions(doc.getRef());
106            return versions.get(versions.size() - 1);
107        } else {
108            return doc;
109        }
110    }
111
112    public String[] getValidatorsFor(DocumentModel dm) {
113        return validatorsRule.computesValidatorsFor(dm);
114    }
115
116    public ValidatorsRule getValidatorsRule() {
117        return validatorsRule;
118    }
119
120    public void validatorPublishDocument(PublishedDocument publishedDocument, String comment) {
121    }
122
123    public void validatorRejectPublication(PublishedDocument publishedDocument, String comment) {
124    }
125
126    public boolean canManagePublishing(PublishedDocument publishedDocument) {
127        return false;
128    }
129
130    public boolean hasValidationTask(PublishedDocument publishedDocument) {
131        return false;
132    }
133
134    /*
135     * -------- Event firing --------
136     */
137
138    protected void notifyEvent(PublishingEvent event, DocumentModel doc, CoreSession coreSession) {
139        notifyEvent(event.name(), null, null, null, doc, coreSession);
140    }
141
142    protected void notifyEvent(String eventId, Map<String, Serializable> properties, String comment, String category,
143            DocumentModel dm, CoreSession coreSession) {
144        // Default category
145        if (category == null) {
146            category = DocumentEventCategories.EVENT_DOCUMENT_CATEGORY;
147        }
148        if (properties == null) {
149            properties = new HashMap<String, Serializable>();
150        }
151        properties.put(CoreEventConstants.REPOSITORY_NAME, dm.getRepositoryName());
152        properties.put(CoreEventConstants.SESSION_ID, coreSession.getSessionId());
153        properties.put(CoreEventConstants.DOC_LIFE_CYCLE, dm.getCurrentLifeCycleState());
154
155        DocumentEventContext ctx = new DocumentEventContext(coreSession, coreSession.getPrincipal(), dm);
156        ctx.setProperties(properties);
157        ctx.setComment(comment);
158        ctx.setCategory(category);
159
160        Event event = ctx.newEvent(eventId);
161        getEventProducer().fireEvent(event);
162    }
163
164    protected EventProducer getEventProducer() {
165        if (eventProducer == null) {
166            eventProducer = Framework.getService(EventProducer.class);
167        }
168        return eventProducer;
169    }
170
171}