001/*
002 * (C) Copyright 2006-2009 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Nuxeo
018 */
019
020package org.nuxeo.ecm.platform.publisher.api;
021
022import java.io.Serializable;
023import java.util.HashMap;
024import java.util.List;
025import java.util.Map;
026
027import org.nuxeo.ecm.core.api.CoreSession;
028import org.nuxeo.ecm.core.api.DocumentModel;
029import org.nuxeo.ecm.core.api.VersioningOption;
030import org.nuxeo.ecm.core.api.event.CoreEventConstants;
031import org.nuxeo.ecm.core.api.event.DocumentEventCategories;
032import org.nuxeo.ecm.core.event.Event;
033import org.nuxeo.ecm.core.event.EventProducer;
034import org.nuxeo.ecm.core.event.impl.DocumentEventContext;
035import org.nuxeo.ecm.platform.publisher.rules.ValidatorsRule;
036import org.nuxeo.runtime.api.Framework;
037
038public abstract class AbstractBasePublishedDocumentFactory implements PublishedDocumentFactory {
039
040    public static final String ENABLE_SNAPSHOT = "enableSnapshot";
041
042    public static final String TARGET_PUBLISHED_DOCUMENT_STATE = "targetPublishedDocumentState";
043
044    protected CoreSession coreSession;
045
046    protected Map<String, String> parameters;
047
048    protected PublicationTree publicationTree;
049
050    protected ValidatorsRule validatorsRule;
051
052    protected EventProducer eventProducer;
053
054    public void init(CoreSession coreSession, ValidatorsRule validatorsRule, Map<String, String> parameters)
055            {
056        this.coreSession = coreSession;
057        this.parameters = parameters;
058        this.validatorsRule = validatorsRule;
059        if (this.parameters == null) {
060            this.parameters = new HashMap<String, String>();
061        }
062    }
063
064    public void init(CoreSession coreSession, Map<String, String> parameters) {
065        init(coreSession, null, parameters);
066    }
067
068    public String getName() {
069        return this.getClass().getSimpleName();
070    }
071
072    protected String getParameter(String name) {
073        return parameters.get(name);
074    }
075
076    protected boolean isSnapshotingEnabled() {
077        String snap = getParameter(ENABLE_SNAPSHOT);
078        if (snap == null) {
079            return false;
080        } else {
081            return snap.equalsIgnoreCase("true");
082        }
083    }
084
085    protected String getTargetPublishedDocumentState() {
086        return getParameter(TARGET_PUBLISHED_DOCUMENT_STATE);
087    }
088
089    public PublishedDocument publishDocument(DocumentModel doc, PublicationNode targetNode) {
090        return publishDocument(doc, targetNode, null);
091    }
092
093    protected boolean needToVersionDocument(DocumentModel doc) {
094        if (!doc.isVersion() && doc.isVersionable()) {
095            return true;
096        }
097        return false;
098    }
099
100    public DocumentModel snapshotDocumentBeforePublish(DocumentModel doc) {
101
102        if (isSnapshotingEnabled() && needToVersionDocument(doc)) {
103            if (doc.isCheckedOut()) {
104                doc.checkIn(VersioningOption.MINOR, null);
105            }
106            coreSession.save();
107            List<DocumentModel> versions = coreSession.getVersions(doc.getRef());
108            return versions.get(versions.size() - 1);
109        } else {
110            return doc;
111        }
112    }
113
114    public String[] getValidatorsFor(DocumentModel dm) {
115        return validatorsRule.computesValidatorsFor(dm);
116    }
117
118    public ValidatorsRule getValidatorsRule() {
119        return validatorsRule;
120    }
121
122    public void validatorPublishDocument(PublishedDocument publishedDocument, String comment) {
123    }
124
125    public void validatorRejectPublication(PublishedDocument publishedDocument, String comment) {
126    }
127
128    public boolean canManagePublishing(PublishedDocument publishedDocument) {
129        return false;
130    }
131
132    public boolean hasValidationTask(PublishedDocument publishedDocument) {
133        return false;
134    }
135
136    /*
137     * -------- Event firing --------
138     */
139
140    protected void notifyEvent(PublishingEvent event, DocumentModel doc, CoreSession coreSession) {
141        notifyEvent(event.name(), null, null, null, doc, coreSession);
142    }
143
144    protected void notifyEvent(String eventId, Map<String, Serializable> properties, String comment, String category,
145            DocumentModel dm, CoreSession coreSession) {
146        // Default category
147        if (category == null) {
148            category = DocumentEventCategories.EVENT_DOCUMENT_CATEGORY;
149        }
150        if (properties == null) {
151            properties = new HashMap<String, Serializable>();
152        }
153        properties.put(CoreEventConstants.REPOSITORY_NAME, dm.getRepositoryName());
154        properties.put(CoreEventConstants.SESSION_ID, coreSession.getSessionId());
155        properties.put(CoreEventConstants.DOC_LIFE_CYCLE, dm.getCurrentLifeCycleState());
156
157        DocumentEventContext ctx = new DocumentEventContext(coreSession, coreSession.getPrincipal(), dm);
158        ctx.setProperties(properties);
159        ctx.setComment(comment);
160        ctx.setCategory(category);
161
162        Event event = ctx.newEvent(eventId);
163        getEventProducer().fireEvent(event);
164    }
165
166    protected EventProducer getEventProducer() {
167        if (eventProducer == null) {
168            eventProducer = Framework.getService(EventProducer.class);
169        }
170        return eventProducer;
171    }
172
173}