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 @Override 055 public void init(CoreSession coreSession, ValidatorsRule validatorsRule, Map<String, String> parameters) 056 { 057 this.coreSession = coreSession; 058 this.parameters = parameters; 059 this.validatorsRule = validatorsRule; 060 if (this.parameters == null) { 061 this.parameters = new HashMap<>(); 062 } 063 } 064 065 @Override 066 public void init(CoreSession coreSession, Map<String, String> parameters) { 067 init(coreSession, null, parameters); 068 } 069 070 @Override 071 public String getName() { 072 return this.getClass().getSimpleName(); 073 } 074 075 protected String getParameter(String name) { 076 return parameters.get(name); 077 } 078 079 protected boolean isSnapshotingEnabled() { 080 String snap = getParameter(ENABLE_SNAPSHOT); 081 if (snap == null) { 082 return false; 083 } else { 084 return snap.equalsIgnoreCase("true"); 085 } 086 } 087 088 protected String getTargetPublishedDocumentState() { 089 return getParameter(TARGET_PUBLISHED_DOCUMENT_STATE); 090 } 091 092 @Override 093 public PublishedDocument publishDocument(DocumentModel doc, PublicationNode targetNode) { 094 return publishDocument(doc, targetNode, null); 095 } 096 097 protected boolean needToVersionDocument(DocumentModel doc) { 098 if (!doc.isVersion() && doc.isVersionable()) { 099 return true; 100 } 101 return false; 102 } 103 104 @Override 105 public DocumentModel snapshotDocumentBeforePublish(DocumentModel doc) { 106 107 if (isSnapshotingEnabled() && needToVersionDocument(doc)) { 108 if (doc.isCheckedOut()) { 109 doc.checkIn(VersioningOption.MINOR, null); 110 } 111 coreSession.save(); 112 List<DocumentModel> versions = coreSession.getVersions(doc.getRef()); 113 return versions.get(versions.size() - 1); 114 } else { 115 return doc; 116 } 117 } 118 119 @Override 120 public String[] getValidatorsFor(DocumentModel dm) { 121 return validatorsRule.computesValidatorsFor(dm); 122 } 123 124 @Override 125 public ValidatorsRule getValidatorsRule() { 126 return validatorsRule; 127 } 128 129 @Override 130 public void validatorPublishDocument(PublishedDocument publishedDocument, String comment) { 131 } 132 133 @Override 134 public void validatorRejectPublication(PublishedDocument publishedDocument, String comment) { 135 } 136 137 @Override 138 public boolean canManagePublishing(PublishedDocument publishedDocument) { 139 return false; 140 } 141 142 @Override 143 public boolean hasValidationTask(PublishedDocument publishedDocument) { 144 return false; 145 } 146 147 /* 148 * -------- Event firing -------- 149 */ 150 151 protected void notifyEvent(PublishingEvent event, DocumentModel doc, CoreSession coreSession) { 152 notifyEvent(event.name(), null, null, null, doc, coreSession); 153 } 154 155 protected void notifyEvent(String eventId, Map<String, Serializable> properties, String comment, String category, 156 DocumentModel dm, CoreSession coreSession) { 157 // Default category 158 if (category == null) { 159 category = DocumentEventCategories.EVENT_DOCUMENT_CATEGORY; 160 } 161 if (properties == null) { 162 properties = new HashMap<>(); 163 } 164 properties.put(CoreEventConstants.REPOSITORY_NAME, dm.getRepositoryName()); 165 properties.put(CoreEventConstants.DOC_LIFE_CYCLE, dm.getCurrentLifeCycleState()); 166 167 DocumentEventContext ctx = new DocumentEventContext(coreSession, coreSession.getPrincipal(), dm); 168 ctx.setProperties(properties); 169 ctx.setComment(comment); 170 ctx.setCategory(category); 171 172 Event event = ctx.newEvent(eventId); 173 getEventProducer().fireEvent(event); 174 } 175 176 protected EventProducer getEventProducer() { 177 if (eventProducer == null) { 178 eventProducer = Framework.getService(EventProducer.class); 179 } 180 return eventProducer; 181 } 182 183}