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.automation.core.operations.document;
020
021import java.io.Serializable;
022import java.util.HashMap;
023import java.util.Map;
024
025import org.nuxeo.ecm.automation.core.Constants;
026import org.nuxeo.ecm.automation.core.annotations.Context;
027import org.nuxeo.ecm.automation.core.annotations.Operation;
028import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
029import org.nuxeo.ecm.automation.core.annotations.Param;
030import org.nuxeo.ecm.automation.core.collectors.DocumentModelCollector;
031import org.nuxeo.ecm.core.api.CoreSession;
032import org.nuxeo.ecm.core.api.DocumentModel;
033import org.nuxeo.ecm.core.api.event.CoreEventConstants;
034import org.nuxeo.ecm.core.api.event.DocumentEventCategories;
035import org.nuxeo.ecm.core.api.event.DocumentEventTypes;
036import org.nuxeo.ecm.core.event.Event;
037import org.nuxeo.ecm.core.event.EventProducer;
038import org.nuxeo.ecm.core.event.impl.DocumentEventContext;
039import org.nuxeo.runtime.api.Framework;
040
041/**
042 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
043 */
044@Operation(id = PublishDocument.ID, category = Constants.CAT_DOCUMENT, label = "Publish Document", description = "Publish the input document into the target section. Existing proxy is overrided if the override attribute is set. Return the created proxy.", aliases = { "Document.Publish" })
045public class PublishDocument {
046
047    public static final String ID = "Document.PublishToSection";
048
049    @Context
050    protected CoreSession session;
051
052    @Param(name = "target")
053    protected DocumentModel target;
054
055    @Param(name = "override", required = false, values = "true")
056    protected boolean override = true;
057
058    @OperationMethod(collector = DocumentModelCollector.class)
059    public DocumentModel run(DocumentModel doc) {
060        DocumentModel proxy = session.publishDocument(doc, target, override);
061        notifyPublishedEvent(doc);
062        notifyPublishedEvent(proxy);
063        return proxy;
064    }
065
066    /**
067     * @since 10.3
068     */
069    protected void notifyPublishedEvent(DocumentModel doc) {
070        Map<String, Serializable> properties = new HashMap<>();
071        properties.put(CoreEventConstants.REPOSITORY_NAME, doc.getRepositoryName());
072        properties.put(CoreEventConstants.DOC_LIFE_CYCLE, doc.getCurrentLifeCycleState());
073
074        DocumentEventContext ctx = new DocumentEventContext(session, session.getPrincipal(), doc);
075        ctx.setProperties(properties);
076        ctx.setCategory(DocumentEventCategories.EVENT_DOCUMENT_CATEGORY);
077
078        Event event = ctx.newEvent(DocumentEventTypes.DOCUMENT_PUBLISHED);
079        Framework.getService(EventProducer.class).fireEvent(event);
080    }
081
082}