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.web;
020
021import java.io.Serializable;
022import java.util.List;
023
024import org.apache.commons.logging.Log;
025import org.apache.commons.logging.LogFactory;
026import org.jboss.seam.annotations.In;
027import org.nuxeo.ecm.core.api.CoreSession;
028import org.nuxeo.ecm.core.api.DocumentModel;
029import org.nuxeo.ecm.core.api.DocumentNotFoundException;
030import org.nuxeo.ecm.core.api.IdRef;
031import org.nuxeo.ecm.core.api.security.SecurityConstants;
032import org.nuxeo.ecm.platform.rendition.service.RenditionDefinition;
033import org.nuxeo.ecm.platform.rendition.service.RenditionService;
034import org.nuxeo.ecm.platform.ui.web.api.NavigationContext;
035import org.nuxeo.runtime.api.Framework;
036import org.nuxeo.template.api.TemplateProcessorService;
037import org.nuxeo.template.api.adapters.TemplateBasedDocument;
038import org.nuxeo.template.api.adapters.TemplateSourceDocument;
039import org.nuxeo.template.rendition.TemplateBasedRenditionProvider;
040
041public class BaseTemplateAction implements Serializable {
042
043    private static final long serialVersionUID = 1L;
044
045    protected static final Log log = LogFactory.getLog(BaseTemplateAction.class);
046
047    @In(create = true)
048    protected transient NavigationContext navigationContext;
049
050    @In(create = true, required = false)
051    protected transient CoreSession documentManager;
052
053    public boolean canAddTemplateInputs() {
054        DocumentModel currentDocument = navigationContext.getCurrentDocument();
055        if (!documentManager.hasPermission(currentDocument.getRef(), SecurityConstants.WRITE)) {
056            return false;
057        }
058        TemplateSourceDocument template = currentDocument.getAdapter(TemplateSourceDocument.class);
059        return template != null ? true : false;
060    }
061
062    public boolean canUpdateTemplateInputs(String templateName) {
063        DocumentModel currentDocument = navigationContext.getCurrentDocument();
064        if (!documentManager.hasPermission(currentDocument.getRef(), SecurityConstants.WRITE)) {
065            return false;
066        }
067        TemplateSourceDocument template = currentDocument.getAdapter(TemplateSourceDocument.class);
068        if (template != null) {
069            return true;
070        }
071        TemplateBasedDocument templateBased = currentDocument.getAdapter(TemplateBasedDocument.class);
072        if (templateBased != null) {
073            return templateBased.hasEditableParams(templateName);
074        }
075        return false;
076    }
077
078    public boolean canResetParameters() {
079        DocumentModel currentDocument = navigationContext.getCurrentDocument();
080        if (!documentManager.hasPermission(currentDocument.getRef(), SecurityConstants.WRITE)) {
081            return false;
082        }
083        TemplateBasedDocument templateBased = currentDocument.getAdapter(TemplateBasedDocument.class);
084        if (templateBased != null) {
085            return true;
086        }
087        return false;
088    }
089
090    public TemplateSourceDocument getCurrentDocumentAsTemplateSourceDocument() {
091        return navigationContext.getCurrentDocument().getAdapter(TemplateSourceDocument.class);
092    }
093
094    public DocumentModel resolveTemplateById(String uuid) {
095        try {
096            return documentManager.getDocument(new IdRef(uuid));
097        } catch (DocumentNotFoundException e) {
098            return null;
099        }
100    }
101
102    public List<RenditionDefinition> getRenditions() {
103        RenditionService rs = Framework.getLocalService(RenditionService.class);
104        return rs.getDeclaredRenditionDefinitionsForProviderType(TemplateBasedRenditionProvider.class.getSimpleName());
105    }
106
107    public List<TemplateSourceDocument> getAvailableOfficeTemplates(String targetType) {
108        TemplateProcessorService tps = Framework.getLocalService(TemplateProcessorService.class);
109        return tps.getAvailableOfficeTemplates(documentManager, targetType);
110    }
111
112}