001/* 002 * (C) Copyright 2006-2011 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 * bstefanescu 018 */ 019package org.nuxeo.ecm.platform.rendition.operation; 020 021import java.io.Serializable; 022import java.util.HashMap; 023import java.util.Map; 024 025import org.apache.commons.lang3.StringUtils; 026import org.nuxeo.ecm.automation.core.Constants; 027import org.nuxeo.ecm.automation.core.annotations.Context; 028import org.nuxeo.ecm.automation.core.annotations.Operation; 029import org.nuxeo.ecm.automation.core.annotations.OperationMethod; 030import org.nuxeo.ecm.automation.core.annotations.Param; 031import org.nuxeo.ecm.automation.core.collectors.DocumentModelCollector; 032import org.nuxeo.ecm.automation.core.operations.document.PublishDocument; 033import org.nuxeo.ecm.core.api.CoreSession; 034import org.nuxeo.ecm.core.api.DocumentModel; 035import org.nuxeo.ecm.core.api.DocumentModelList; 036import org.nuxeo.ecm.core.api.event.CoreEventConstants; 037import org.nuxeo.ecm.core.api.event.DocumentEventCategories; 038import org.nuxeo.ecm.core.api.event.DocumentEventTypes; 039import org.nuxeo.ecm.core.api.impl.DocumentModelListImpl; 040import org.nuxeo.ecm.core.event.Event; 041import org.nuxeo.ecm.core.event.EventProducer; 042import org.nuxeo.ecm.core.event.impl.DocumentEventContext; 043import org.nuxeo.ecm.platform.rendition.service.RenditionService; 044import org.nuxeo.runtime.api.Framework; 045 046/** 047 * Publish the rendition of a document. If rendition is not given and default rendition option is false, it falls back 048 * on {@link PublishDocument} operation. 049 * 050 * @since 10.3 051 */ 052@Operation(id = PublishRendition.ID, category = Constants.CAT_DOCUMENT, label = "Publish Document's Rendition", description = "input document's chosen rendition into the target section. If rendition is not given and default rendition option is false, it falls back on default publishing. Existing proxy is overrided if the override attribute is set. Return the created proxy.", aliases = { 053 "Document.PublishRendition" }) 054public class PublishRendition { 055 056 public static final String ID = PublishDocument.ID; 057 058 @Context 059 protected CoreSession session; 060 061 @Param(name = "renditionName", required = false) 062 protected String renditionName; 063 064 @Param(name = "defaultRendition", required = false) 065 protected boolean defaultRendition; 066 067 @Param(name = "target") 068 protected DocumentModel target; 069 070 @Param(name = "override", required = false, values = "true") 071 protected boolean override = true; 072 073 @OperationMethod(collector = DocumentModelCollector.class) 074 public DocumentModel run(DocumentModel doc) { 075 DocumentModel proxy; 076 if (!defaultRendition && StringUtils.isEmpty(renditionName)) { 077 proxy = session.publishDocument(doc, target, override); 078 } else { 079 RenditionService rs = Framework.getService(RenditionService.class); 080 proxy = rs.publishRendition(doc, target, renditionName, override); 081 } 082 notifyPublishedEvent(doc); 083 notifyPublishedEvent(proxy); 084 return proxy; 085 } 086 087 @OperationMethod 088 public DocumentModelList run(DocumentModelList docs) { 089 DocumentModelList result = new DocumentModelListImpl(); 090 for (DocumentModel doc : docs) { 091 result.add(run(doc)); 092 } 093 return result; 094 } 095 096 /** 097 * @since 10.3 098 */ 099 protected void notifyPublishedEvent(DocumentModel proxy) { 100 Map<String, Serializable> properties = new HashMap<>(); 101 properties.put(CoreEventConstants.REPOSITORY_NAME, proxy.getRepositoryName()); 102 properties.put(CoreEventConstants.SESSION_ID, session.getSessionId()); 103 properties.put(CoreEventConstants.DOC_LIFE_CYCLE, proxy.getCurrentLifeCycleState()); 104 105 DocumentEventContext ctx = new DocumentEventContext(session, session.getPrincipal(), proxy); 106 ctx.setProperties(properties); 107 ctx.setCategory(DocumentEventCategories.EVENT_DOCUMENT_CATEGORY); 108 109 Event event = ctx.newEvent(DocumentEventTypes.DOCUMENT_PUBLISHED); 110 Framework.getService(EventProducer.class).fireEvent(event); 111 } 112 113}