001/*
002 * (C) Copyright 2014 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl-2.1.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     <a href="mailto:grenard@nuxeo.com">Guillaume</a>
016 */
017package org.nuxeo.ecm.collections.core.listener;
018
019import org.apache.commons.logging.Log;
020import org.apache.commons.logging.LogFactory;
021import org.nuxeo.ecm.collections.api.CollectionConstants;
022import org.nuxeo.ecm.collections.api.CollectionManager;
023import org.nuxeo.ecm.collections.core.adapter.CollectionMember;
024import org.nuxeo.ecm.core.api.DocumentModel;
025import org.nuxeo.ecm.core.api.DocumentRef;
026import org.nuxeo.ecm.core.api.event.DocumentEventTypes;
027import org.nuxeo.ecm.core.event.Event;
028import org.nuxeo.ecm.core.event.EventContext;
029import org.nuxeo.ecm.core.event.EventListener;
030import org.nuxeo.ecm.core.event.impl.DocumentEventContext;
031import org.nuxeo.runtime.api.Framework;
032
033/**
034 * Event handler to duplicate the collection members of a duplicated collection. The handler is synchronous because it
035 * is important to capture the collection member ids of the duplicated collection at the exact moment of duplication. We
036 * don't want to duplicate a collection member that was indeed added to the duplicated collection after the duplication.
037 * The handler will then launch asynchronous tasks to duplicate the collection members.
038 *
039 * @since 5.9.3
040 */
041public class DuplicatedCollectionListener implements EventListener {
042
043    private static final Log log = LogFactory.getLog(DuplicatedCollectionListener.class);
044
045    @Override
046    public void handleEvent(Event event) {
047        EventContext ctx = event.getContext();
048        if (!(ctx instanceof DocumentEventContext)) {
049            return;
050        }
051
052        final String eventId = event.getName();
053
054        final DocumentEventContext docCxt = (DocumentEventContext) event.getContext();
055
056        DocumentModel doc = null;
057        if (eventId.equals(DocumentEventTypes.DOCUMENT_CREATED_BY_COPY)) {
058            doc = docCxt.getSourceDocument();
059        } else if (eventId.equals(DocumentEventTypes.DOCUMENT_CHECKEDIN)) {
060            DocumentRef checkedInVersionRef = (DocumentRef) ctx.getProperties().get("checkedInVersionRef");
061            doc = ctx.getCoreSession().getDocument(checkedInVersionRef);
062            if (!doc.isVersion()) {
063                return;
064            }
065        } else {
066            return;
067        }
068
069        final CollectionManager collectionManager = Framework.getLocalService(CollectionManager.class);
070
071        if (collectionManager.isCollection(doc)) {
072
073            if (eventId.equals(DocumentEventTypes.DOCUMENT_CREATED_BY_COPY)) {
074                log.trace(String.format("Collection %s copied", doc.getId()));
075            } else if (eventId.equals(DocumentEventTypes.DOCUMENT_CHECKEDIN)) {
076                log.trace(String.format("Collection %s checked in", doc.getId()));
077            }
078
079            collectionManager.processCopiedCollection(doc);
080
081        } else if (collectionManager.isCollected(doc)) {
082            doc.getAdapter(CollectionMember.class).setCollectionIds(null);
083            ctx.getCoreSession().saveDocument(doc);
084            doc.removeFacet(CollectionConstants.COLLECTABLE_FACET);
085        }
086    }
087
088}