001/*
002 * (C) Copyright 2012-2016 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;
023import java.util.Map;
024
025import org.apache.commons.logging.Log;
026import org.apache.commons.logging.LogFactory;
027import org.jboss.seam.annotations.In;
028import org.jboss.seam.faces.FacesMessages;
029import org.jboss.seam.international.StatusMessage.Severity;
030import org.nuxeo.ecm.core.api.CoreSession;
031import org.nuxeo.ecm.core.api.DocumentModel;
032import org.nuxeo.ecm.core.api.DocumentNotFoundException;
033import org.nuxeo.ecm.core.api.IdRef;
034import org.nuxeo.ecm.core.api.security.SecurityConstants;
035import org.nuxeo.ecm.platform.rendition.service.RenditionDefinition;
036import org.nuxeo.ecm.platform.rendition.service.RenditionService;
037import org.nuxeo.ecm.platform.ui.web.api.NavigationContext;
038import org.nuxeo.runtime.api.Framework;
039import org.nuxeo.template.api.TemplateInput;
040import org.nuxeo.template.api.TemplateProcessorService;
041import org.nuxeo.template.api.adapters.TemplateBasedDocument;
042import org.nuxeo.template.api.adapters.TemplateSourceDocument;
043import org.nuxeo.template.rendition.TemplateBasedRenditionProvider;
044
045public class BaseTemplateAction implements Serializable {
046
047    private static final long serialVersionUID = 1L;
048
049    protected static final Log log = LogFactory.getLog(BaseTemplateAction.class);
050
051    @In(create = true)
052    protected transient NavigationContext navigationContext;
053
054    @In(create = true, required = false)
055    protected transient CoreSession documentManager;
056
057    @In(create = true, required = false)
058    protected FacesMessages facesMessages;
059
060    @In(create = true)
061    protected Map<String, String> messages;
062
063    protected List<TemplateInput> templateEditableInputs;
064
065    protected TemplateInput newInput;
066
067    public boolean canAddTemplateInputs() {
068        DocumentModel currentDocument = navigationContext.getCurrentDocument();
069        if (!documentManager.hasPermission(currentDocument.getRef(), SecurityConstants.WRITE)) {
070            return false;
071        }
072        TemplateSourceDocument template = currentDocument.getAdapter(TemplateSourceDocument.class);
073        return template != null ? true : false;
074    }
075
076    public boolean canUpdateTemplateInputs(String templateName) {
077        DocumentModel currentDocument = navigationContext.getCurrentDocument();
078        if (!documentManager.hasPermission(currentDocument.getRef(), SecurityConstants.WRITE)) {
079            return false;
080        }
081        TemplateSourceDocument template = currentDocument.getAdapter(TemplateSourceDocument.class);
082        if (template != null) {
083            return true;
084        }
085        TemplateBasedDocument templateBased = currentDocument.getAdapter(TemplateBasedDocument.class);
086        if (templateBased != null) {
087            return templateBased.hasEditableParams(templateName);
088        }
089        return false;
090    }
091
092    public boolean canResetParameters() {
093        DocumentModel currentDocument = navigationContext.getCurrentDocument();
094        if (!documentManager.hasPermission(currentDocument.getRef(), SecurityConstants.WRITE)) {
095            return false;
096        }
097        TemplateBasedDocument templateBased = currentDocument.getAdapter(TemplateBasedDocument.class);
098        if (templateBased != null) {
099            return true;
100        }
101        return false;
102    }
103
104    public TemplateSourceDocument getCurrentDocumentAsTemplateSourceDocument() {
105        return navigationContext.getCurrentDocument().getAdapter(TemplateSourceDocument.class);
106    }
107
108    public DocumentModel resolveTemplateById(String uuid) {
109        try {
110            return documentManager.getDocument(new IdRef(uuid));
111        } catch (DocumentNotFoundException e) {
112            return null;
113        }
114    }
115
116    public List<RenditionDefinition> getRenditions() {
117        RenditionService rs = Framework.getLocalService(RenditionService.class);
118        return rs.getDeclaredRenditionDefinitionsForProviderType(TemplateBasedRenditionProvider.class.getSimpleName());
119    }
120
121    public List<TemplateSourceDocument> getAvailableOfficeTemplates(String targetType) {
122        TemplateProcessorService tps = Framework.getLocalService(TemplateProcessorService.class);
123        return tps.getAvailableOfficeTemplates(documentManager, targetType);
124    }
125
126    public String addTemplateInput() {
127        DocumentModel currentDocument = navigationContext.getCurrentDocument();
128
129        TemplateSourceDocument template = currentDocument.getAdapter(TemplateSourceDocument.class);
130        if (template != null) {
131            if (template.hasInput(newInput.getName())) {
132                facesMessages.add(Severity.WARN, messages.get("label.template.parameter.already.exist"),
133                        newInput.getName());
134                return null;
135            }
136            template.addInput(newInput);
137            newInput = null;
138            templateEditableInputs = null;
139        } else {
140            return null;
141        }
142        return navigationContext.navigateToDocument(currentDocument);
143    }
144
145}