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