001/* 002 * (C) Copyright 2012 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 * Thierry Delprat 018 */ 019package org.nuxeo.template.listeners; 020 021import static org.nuxeo.ecm.core.api.event.DocumentEventTypes.DOCUMENT_CREATED; 022import static org.nuxeo.ecm.core.api.event.DocumentEventTypes.DOCUMENT_UPDATED; 023 024import org.apache.commons.logging.Log; 025import org.apache.commons.logging.LogFactory; 026import org.nuxeo.ecm.core.api.DocumentModel; 027import org.nuxeo.ecm.core.event.Event; 028import org.nuxeo.ecm.core.event.EventBundle; 029import org.nuxeo.ecm.core.event.EventContext; 030import org.nuxeo.ecm.core.event.PostCommitFilteringEventListener; 031import org.nuxeo.ecm.core.event.impl.DocumentEventContext; 032import org.nuxeo.ecm.core.event.impl.ShallowDocumentModel; 033import org.nuxeo.runtime.api.Framework; 034import org.nuxeo.template.adapters.TemplateAdapterFactory; 035import org.nuxeo.template.api.TemplateProcessorService; 036import org.nuxeo.template.api.adapters.TemplateSourceDocument; 037 038public class TemplateTypeBindingListener implements PostCommitFilteringEventListener { 039 040 protected static Log log = LogFactory.getLog(TemplateTypeBindingListener.class); 041 042 @Override 043 public boolean acceptEvent(Event event) { 044 EventContext context = event.getContext(); 045 if (!(context instanceof DocumentEventContext)) { 046 return false; 047 } 048 DocumentModel doc = ((DocumentEventContext) context).getSourceDocument(); 049 if (doc == null || doc.isVersion()) { 050 return false; 051 } 052 // we cannot directly adapt the ShallowDocumentModel, 053 // so check the adapter factory manually 054 return TemplateAdapterFactory.isAdaptable(doc, TemplateSourceDocument.class); 055 } 056 057 @Override 058 public void handleEvent(EventBundle eventBundle) { 059 if (eventBundle.containsEventName(DOCUMENT_CREATED) || eventBundle.containsEventName(DOCUMENT_UPDATED)) { 060 061 TemplateProcessorService tps = Framework.getLocalService(TemplateProcessorService.class); 062 063 for (Event event : eventBundle) { 064 if (DOCUMENT_CREATED.equals(event.getName()) || DOCUMENT_UPDATED.equals(event.getName())) { 065 EventContext ctx = event.getContext(); 066 if (ctx instanceof DocumentEventContext) { 067 DocumentEventContext docCtx = (DocumentEventContext) ctx; 068 DocumentModel targetDoc = docCtx.getSourceDocument(); 069 070 if (targetDoc.isVersion()) { 071 continue; 072 } 073 if (targetDoc instanceof ShallowDocumentModel) { 074 log.warn("Skip unconnected document with type " + targetDoc.getType() + " and path " 075 + targetDoc.getPathAsString()); 076 continue; 077 } 078 TemplateSourceDocument tmpl = targetDoc.getAdapter(TemplateSourceDocument.class); 079 if (tmpl != null) { 080 tps.registerTypeMapping(targetDoc); 081 // be sure to trigger invalidations in unit tests 082 targetDoc.getCoreSession().save(); 083 } 084 } 085 } 086 } 087 } 088 } 089 090}