001/*
002 * (C) Copyright 2006-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 *     Nuxeo - initial API and implementation
018 *
019 */
020
021package org.nuxeo.template.listeners;
022
023import static org.nuxeo.ecm.core.api.event.DocumentEventTypes.ABOUT_TO_CREATE;
024import static org.nuxeo.ecm.core.api.event.DocumentEventTypes.BEFORE_DOC_UPDATE;
025
026import java.util.ArrayList;
027import java.util.List;
028
029import org.apache.commons.logging.Log;
030import org.apache.commons.logging.LogFactory;
031import org.nuxeo.ecm.core.api.Blob;
032import org.nuxeo.ecm.core.api.DocumentModel;
033import org.nuxeo.ecm.core.api.DocumentRef;
034import org.nuxeo.ecm.core.api.IdRef;
035import org.nuxeo.ecm.core.api.blobholder.BlobHolder;
036import org.nuxeo.ecm.core.event.Event;
037import org.nuxeo.ecm.core.event.EventContext;
038import org.nuxeo.ecm.core.event.EventListener;
039import org.nuxeo.ecm.core.event.impl.DocumentEventContext;
040import org.nuxeo.runtime.api.Framework;
041import org.nuxeo.template.api.TemplateInput;
042import org.nuxeo.template.api.TemplateProcessorService;
043import org.nuxeo.template.api.adapters.TemplateBasedDocument;
044import org.nuxeo.template.api.adapters.TemplateSourceDocument;
045
046/**
047 * Listener to manage initialization :
048 * <ul>
049 * <li>of the TemplateSourceDocument : init the parameters</li>
050 * <li>of the other DocumentModels if they need to be automatically associated to a template</li>
051 * </ul>
052 *
053 * @author Tiry (tdelprat@nuxeo.com)
054 */
055public class TemplateInitListener implements EventListener {
056
057    private static final Log log = LogFactory.getLog(TemplateInitListener.class);
058
059    @Override
060    public void handleEvent(Event event) {
061
062        EventContext ctx = event.getContext();
063
064        if (ABOUT_TO_CREATE.equals(event.getName()) || BEFORE_DOC_UPDATE.equals(event.getName())) {
065            if (ctx instanceof DocumentEventContext) {
066                DocumentEventContext docCtx = (DocumentEventContext) ctx;
067
068                DocumentModel targetDoc = docCtx.getSourceDocument();
069
070                if (targetDoc.isVersion()) {
071                    return;
072                }
073
074                TemplateSourceDocument templateDoc = targetDoc.getAdapter(TemplateSourceDocument.class);
075                if (templateDoc != null) {
076                    // init types bindings
077                    templateDoc.initTypesBindings();
078
079                    // init template source
080                    List<TemplateInput> params = templateDoc.getParams();
081                    if (params == null || params.size() == 0 || isBlobDirty(targetDoc)) {
082                        templateDoc.initTemplate(false);
083                    }
084                } else {
085                    TemplateBasedDocument tmplBased = targetDoc.getAdapter(TemplateBasedDocument.class);
086                    if (tmplBased == null) {
087                        // if not templateBased see if we must add the facet
088                        // because of the type binding
089                        // or template selection as main file
090                        TemplateProcessorService tps = Framework.getService(TemplateProcessorService.class);
091
092                        String targetTemplateUid = (String) targetDoc.getContextData("templateId");
093                        if ("none".equals(targetTemplateUid)) {
094                            targetTemplateUid = null;
095                        }
096                        List<String> templatesUids = new ArrayList<String>();
097
098                        if (targetTemplateUid != null) {
099                            templatesUids.add(targetTemplateUid);
100                        }
101
102                        List<String> tuids = tps.getTypeMapping().get(targetDoc.getType());
103                        if (tuids != null) {
104                            for (String tuid : tuids) {
105                                // let's be paranoid
106                                if (!templatesUids.contains(tuid)) {
107                                    templatesUids.add(tuid);
108                                }
109                            }
110                        }
111
112                        // do the association
113                        if (templatesUids.size() > 0) {
114                            for (String tuid : templatesUids) {
115                                DocumentRef templateRef = new IdRef(tuid);
116                                // check if source template is visible
117                                if (docCtx.getCoreSession().exists(templateRef)) {
118                                    DocumentModel sourceTemplateDoc = docCtx.getCoreSession().getDocument(templateRef);
119                                    if (!sourceTemplateDoc.isTrashed()) {
120                                        tps.makeTemplateBasedDocument(targetDoc, sourceTemplateDoc, false);
121                                    }
122                                }
123                            }
124                        }
125                    }
126                }
127            }
128        }
129    }
130
131    protected boolean isBlobDirty(DocumentModel targetDoc) {
132        BlobHolder bh = targetDoc.getAdapter(BlobHolder.class);
133        Blob mainBlob = bh.getBlob();
134        if (mainBlob != null && mainBlob.getDigest() == null) {
135            // Blobs that have not changed should be SQL Blobs and have a digest
136            return true;
137        } else {
138            // newly uploaded Blob should be FileBlob, and anyway Digest can not
139            // have been computed so far
140            return false;
141        }
142    }
143}