001/*
002 * (C) Copyright 2006-2009 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.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 *     Nuxeo
016 */
017
018package org.nuxeo.ecm.platform.publisher.listeners;
019
020import org.nuxeo.ecm.core.api.DocumentModel;
021import org.nuxeo.ecm.core.api.LifeCycleConstants;
022import org.nuxeo.ecm.core.api.event.CoreEventConstants;
023import org.nuxeo.ecm.core.api.event.DocumentEventTypes;
024import org.nuxeo.ecm.core.event.Event;
025import org.nuxeo.ecm.core.event.EventContext;
026import org.nuxeo.ecm.core.event.EventListener;
027import org.nuxeo.ecm.core.event.impl.DocumentEventContext;
028import org.nuxeo.ecm.platform.publisher.api.PublisherService;
029import org.nuxeo.ecm.platform.publisher.impl.service.PublisherServiceImpl;
030import org.nuxeo.runtime.api.Framework;
031
032/**
033 * Handle Domain creation, deletion and lifecycle changes. Register new {@code PublicationTreeConfigDescriptor}
034 * according to the new Domain, if at least one descriptor is pending. Unregister
035 * {@code PublicationTreeConfigDescriptor} associated to the Domain when it is removed or if its lifecycle has changed
036 * (ie. to delete state).
037 *
038 * @author <a href="mailto:troger@nuxeo.com">Thomas Roger</a>
039 */
040public class DomainEventsListener implements EventListener {
041
042    public static final String DISABLE_DOMAIN_LISTENER = "disableDomainListener";
043
044    public void handleEvent(Event event) {
045        EventContext ctx = event.getContext();
046        Boolean disableListener = (Boolean) ctx.getProperty(DISABLE_DOMAIN_LISTENER);
047        if (Boolean.TRUE.equals(disableListener)) {
048            return;
049        }
050        if (ctx instanceof DocumentEventContext) {
051            DocumentEventContext docCtx = (DocumentEventContext) ctx;
052            DocumentModel doc = docCtx.getSourceDocument();
053            if (doc != null && "Domain".equals(doc.getType())) {
054                String eventName = event.getName();
055                if (DocumentEventTypes.DOCUMENT_CREATED.equals(eventName)) {
056                    registerNewPublicationTrees(doc);
057                } else if (DocumentEventTypes.DOCUMENT_UPDATED.equals(eventName)) {
058                    // re-register in case of title update for instance
059                    unregisterPublicationTrees(doc);
060                    registerNewPublicationTrees(doc);
061                } else if (DocumentEventTypes.DOCUMENT_REMOVED.equals(eventName)) {
062                    unregisterPublicationTrees(doc);
063                } else if (LifeCycleConstants.TRANSITION_EVENT.equals(eventName)) {
064                    handleDomainLifeCycleChanged(docCtx, doc);
065                } else if (DocumentEventTypes.DOCUMENT_MOVED.equals(eventName)) {
066                    handleDomainMoved(docCtx, doc);
067                }
068            }
069        }
070    }
071
072    protected void registerNewPublicationTrees(DocumentModel doc) {
073        PublisherServiceImpl service = (PublisherServiceImpl) Framework.getService(PublisherService.class);
074        service.registerTreeConfigFor(doc);
075    }
076
077    protected void unregisterPublicationTrees(DocumentModel doc) {
078        PublisherServiceImpl service = (PublisherServiceImpl) Framework.getService(PublisherService.class);
079        service.unRegisterTreeConfigFor(doc);
080    }
081
082    protected void handleDomainLifeCycleChanged(DocumentEventContext docCtx, DocumentModel doc) {
083        String from = (String) docCtx.getProperty(LifeCycleConstants.TRANSTION_EVENT_OPTION_FROM);
084        String to = (String) docCtx.getProperty(LifeCycleConstants.TRANSTION_EVENT_OPTION_TO);
085
086        if (LifeCycleConstants.DELETED_STATE.equals(to)) {
087            handleDomainGoesToDeletedState(doc);
088        } else if (LifeCycleConstants.DELETED_STATE.equals(from)) {
089            handleDomainGoesFromDeletedState(doc);
090        }
091    }
092
093    protected void handleDomainGoesToDeletedState(DocumentModel doc) {
094        unregisterPublicationTrees(doc);
095    }
096
097    protected void handleDomainGoesFromDeletedState(DocumentModel doc) {
098        registerNewPublicationTrees(doc);
099    }
100
101    /**
102     * @since 7.3
103     */
104    protected void handleDomainMoved(DocumentEventContext docCtx, DocumentModel doc) {
105        String originalName = (String) docCtx.getProperty(CoreEventConstants.ORIGINAL_NAME);
106        if (originalName != null) {
107            PublisherServiceImpl service = (PublisherServiceImpl) Framework.getService(PublisherService.class);
108            service.unRegisterTreeConfigFor(originalName);
109            service.registerTreeConfigFor(doc);
110        }
111    }
112
113}