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 @Override 048 public void handleEvent(Event event) { 049 EventContext ctx = event.getContext(); 050 Boolean disableListener = (Boolean) ctx.getProperty(DISABLE_DOMAIN_LISTENER); 051 if (Boolean.TRUE.equals(disableListener)) { 052 return; 053 } 054 if (ctx instanceof DocumentEventContext) { 055 DocumentEventContext docCtx = (DocumentEventContext) ctx; 056 DocumentModel doc = docCtx.getSourceDocument(); 057 if (doc != null && "Domain".equals(doc.getType())) { 058 String eventName = event.getName(); 059 if (DocumentEventTypes.DOCUMENT_CREATED.equals(eventName)) { 060 registerNewPublicationTrees(doc); 061 } else if (DocumentEventTypes.DOCUMENT_UPDATED.equals(eventName) && !doc.isTrashed()) { 062 // re-register in case of title update for instance 063 unregisterPublicationTrees(doc); 064 registerNewPublicationTrees(doc); 065 } else if (DocumentEventTypes.DOCUMENT_REMOVED.equals(eventName)) { 066 unregisterPublicationTrees(doc); 067 } else if (LifeCycleConstants.TRANSITION_EVENT.equals(eventName)) { 068 handleDomainLifeCycleChanged(docCtx, doc); 069 } else if (DocumentEventTypes.DOCUMENT_MOVED.equals(eventName)) { 070 handleDomainMoved(docCtx, doc); 071 } else if (TrashService.DOCUMENT_TRASHED.equals(eventName)) { 072 handleDomainGoesToTrashedState(doc); 073 } else if (TrashService.DOCUMENT_UNTRASHED.equals(eventName)) { 074 handleDomainGoesFromTrashedState(doc); 075 } 076 } 077 } 078 } 079 080 protected void registerNewPublicationTrees(DocumentModel doc) { 081 PublisherServiceImpl service = (PublisherServiceImpl) Framework.getService(PublisherService.class); 082 service.registerTreeConfigFor(doc); 083 } 084 085 protected void unregisterPublicationTrees(DocumentModel doc) { 086 PublisherServiceImpl service = (PublisherServiceImpl) Framework.getService(PublisherService.class); 087 service.unRegisterTreeConfigFor(doc); 088 } 089 090 protected void handleDomainLifeCycleChanged(DocumentEventContext docCtx, DocumentModel doc) { 091 String from = (String) docCtx.getProperty(LifeCycleConstants.TRANSTION_EVENT_OPTION_FROM); 092 String to = (String) docCtx.getProperty(LifeCycleConstants.TRANSTION_EVENT_OPTION_TO); 093 094 if (LifeCycleConstants.DELETED_STATE.equals(to)) { 095 handleDomainGoesToTrashedState(doc); 096 } else if (LifeCycleConstants.DELETED_STATE.equals(from)) { 097 handleDomainGoesFromTrashedState(doc); 098 } 099 } 100 101 protected void handleDomainGoesToTrashedState(DocumentModel doc) { 102 unregisterPublicationTrees(doc); 103 } 104 105 protected void handleDomainGoesFromTrashedState(DocumentModel doc) { 106 registerNewPublicationTrees(doc); 107 } 108 109 /** 110 * @since 7.3 111 */ 112 protected void handleDomainMoved(DocumentEventContext docCtx, DocumentModel doc) { 113 String originalName = (String) docCtx.getProperty(CoreEventConstants.ORIGINAL_NAME); 114 if (originalName != null) { 115 PublisherServiceImpl service = (PublisherServiceImpl) Framework.getService(PublisherService.class); 116 service.unRegisterTreeConfigFor(originalName); 117 service.registerTreeConfigFor(doc); 118 } 119 } 120 121}