001/*
002 * (C) Copyright 2014 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 *     <a href="mailto:grenard@nuxeo.com">Guillaume</a>
018 */
019package org.nuxeo.ecm.collections.core.listener;
020
021import static org.nuxeo.ecm.core.api.CoreSession.ALLOW_VERSION_WRITE;
022
023import org.apache.commons.logging.Log;
024import org.apache.commons.logging.LogFactory;
025import org.nuxeo.ecm.collections.api.CollectionConstants;
026import org.nuxeo.ecm.collections.api.CollectionManager;
027import org.nuxeo.ecm.collections.core.adapter.CollectionMember;
028import org.nuxeo.ecm.core.api.DocumentModel;
029import org.nuxeo.ecm.core.api.DocumentRef;
030import org.nuxeo.ecm.core.api.event.DocumentEventTypes;
031import org.nuxeo.ecm.core.event.Event;
032import org.nuxeo.ecm.core.event.EventContext;
033import org.nuxeo.ecm.core.event.EventListener;
034import org.nuxeo.ecm.core.event.impl.DocumentEventContext;
035import org.nuxeo.runtime.api.Framework;
036
037/**
038 * Event handler to duplicate the collection members of a duplicated collection. The handler is synchronous because it
039 * is important to capture the collection member ids of the duplicated collection at the exact moment of duplication. We
040 * don't want to duplicate a collection member that was indeed added to the duplicated collection after the duplication.
041 * The handler will then launch asynchronous tasks to duplicate the collection members.
042 *
043 * @since 5.9.3
044 */
045public class DuplicatedCollectionListener implements EventListener {
046
047    private static final Log log = LogFactory.getLog(DuplicatedCollectionListener.class);
048
049    @Override
050    public void handleEvent(Event event) {
051        EventContext ctx = event.getContext();
052        if (!(ctx instanceof DocumentEventContext)) {
053            return;
054        }
055
056        final String eventId = event.getName();
057
058        final DocumentEventContext docCxt = (DocumentEventContext) event.getContext();
059
060        DocumentModel doc = null;
061        if (eventId.equals(DocumentEventTypes.DOCUMENT_CREATED_BY_COPY)) {
062            doc = docCxt.getSourceDocument();
063        } else if (eventId.equals(DocumentEventTypes.DOCUMENT_CHECKEDIN)) {
064            DocumentRef checkedInVersionRef = (DocumentRef) ctx.getProperties().get("checkedInVersionRef");
065            doc = ctx.getCoreSession().getDocument(checkedInVersionRef);
066            if (!doc.isVersion()) {
067                return;
068            }
069        } else {
070            return;
071        }
072
073        final CollectionManager collectionManager = Framework.getLocalService(CollectionManager.class);
074
075        if (collectionManager.isCollection(doc)) {
076
077            if (eventId.equals(DocumentEventTypes.DOCUMENT_CREATED_BY_COPY)) {
078                log.trace(String.format("Collection %s copied", doc.getId()));
079            } else if (eventId.equals(DocumentEventTypes.DOCUMENT_CHECKEDIN)) {
080                log.trace(String.format("Collection %s checked in", doc.getId()));
081            }
082
083            collectionManager.processCopiedCollection(doc);
084
085        } else if (collectionManager.isCollected(doc)) {
086            doc.getAdapter(CollectionMember.class).setCollectionIds(null);
087            if (doc.isVersion()) {
088                doc.putContextData(ALLOW_VERSION_WRITE, Boolean.TRUE);
089            }
090            ctx.getCoreSession().saveDocument(doc);
091            doc.removeFacet(CollectionConstants.COLLECTABLE_FACET);
092        }
093    }
094
095}