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