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.jsf;
020
021import static org.jboss.seam.ScopeType.CONVERSATION;
022
023import java.util.ArrayList;
024import java.util.Collection;
025import java.util.Iterator;
026import java.util.List;
027
028import org.jboss.seam.annotations.In;
029import org.jboss.seam.annotations.Name;
030import org.jboss.seam.annotations.Observer;
031import org.jboss.seam.annotations.Scope;
032import org.jboss.seam.annotations.intercept.BypassInterceptors;
033import org.nuxeo.ecm.core.api.DocumentModel;
034import org.nuxeo.ecm.core.api.PropertyException;
035import org.nuxeo.ecm.platform.types.Type;
036import org.nuxeo.ecm.platform.types.TypeManager;
037import org.nuxeo.ecm.webapp.contentbrowser.DocumentActions;
038import org.nuxeo.ecm.webapp.helpers.EventNames;
039import org.nuxeo.runtime.api.Framework;
040import org.nuxeo.template.api.TemplateInput;
041import org.nuxeo.template.api.TemplateProcessorService;
042import org.nuxeo.template.api.adapters.TemplateSourceDocument;
043import org.nuxeo.template.api.descriptor.OutputFormatDescriptor;
044import org.nuxeo.template.api.descriptor.TemplateProcessorDescriptor;
045
046@Name("templateActions")
047@Scope(CONVERSATION)
048public class TemplatesActionBean extends BaseTemplateAction {
049
050    private static final long serialVersionUID = 1L;
051
052    @In(create = true)
053    protected transient DocumentActions documentActions;
054
055    @In(create = true)
056    protected transient TypeManager typeManager;
057
058    protected List<TemplateInput> templateInputs;
059
060    protected boolean showParamEditor = false;
061
062    protected boolean showUsageListing = false;
063
064    protected boolean showVersions = false;
065
066    protected boolean checkedInVersion = false;
067
068    public String createTemplate() {
069        DocumentModel changeableDocument = navigationContext.getChangeableDocument();
070        TemplateSourceDocument sourceTemplate = changeableDocument.getAdapter(TemplateSourceDocument.class);
071        if (sourceTemplate != null && sourceTemplate.getTemplateBlob() != null) {
072            try {
073                sourceTemplate.initTemplate(false);
074                if (sourceTemplate.hasEditableParams()) {
075                    templateInputs = sourceTemplate.getParams();
076                    return "editTemplateRelatedData";
077                }
078            } catch (PropertyException e) {
079                log.error("Error during parameter automatic initialization", e);
080            }
081        }
082        return documentActions.saveDocument(changeableDocument);
083    }
084
085    public List<TemplateInput> getTemplateInputs() {
086        return templateInputs;
087    }
088
089    public void setTemplateInputs(List<TemplateInput> templateInputs) {
090        this.templateInputs = templateInputs;
091    }
092
093    public String saveDocument() {
094        DocumentModel changeableDocument = navigationContext.getChangeableDocument();
095
096        for (TemplateInput ti : templateInputs) {
097            log.info(ti.toString());
098        }
099        TemplateSourceDocument source = changeableDocument.getAdapter(TemplateSourceDocument.class);
100        if (source != null) {
101            source.saveParams(templateInputs, false);
102        }
103
104        return documentActions.saveDocument(changeableDocument);
105    }
106
107    @Observer(value = { EventNames.DOCUMENT_SELECTION_CHANGED, EventNames.NEW_DOCUMENT_CREATED,
108            EventNames.DOCUMENT_CHANGED }, create = false)
109    @BypassInterceptors
110    public void reset() {
111        templateInputs = null;
112        templateEditableInputs = null;
113        showParamEditor = false;
114    }
115
116    public List<TemplateInput> getTemplateEditableInputs() {
117        if (templateEditableInputs == null) {
118            DocumentModel currentDocument = navigationContext.getCurrentDocument();
119
120            TemplateSourceDocument template = currentDocument.getAdapter(TemplateSourceDocument.class);
121            if (template != null) {
122                templateEditableInputs = template.getParams();
123            }
124        }
125        return templateEditableInputs;
126    }
127
128    public void setTemplateEditableInputs(List<TemplateInput> templateEditableInputs) {
129        this.templateEditableInputs = templateEditableInputs;
130    }
131
132    public String saveTemplateInputs() {
133
134        DocumentModel currentDocument = navigationContext.getCurrentDocument();
135
136        TemplateSourceDocument template = currentDocument.getAdapter(TemplateSourceDocument.class);
137        if (template != null) {
138            currentDocument = template.saveParams(templateEditableInputs, true);
139        }
140        navigationContext.invalidateCurrentDocument();
141        return navigationContext.navigateToDocument(currentDocument);
142    }
143
144    public void cancelTemplateInputsEdit() {
145        reset();
146    }
147
148    public TemplateInput getNewInput() {
149        if (newInput == null) {
150            newInput = new TemplateInput("newField");
151        }
152        return newInput;
153    }
154
155    public void setNewInput(TemplateInput newInput) {
156        this.newInput = newInput;
157    }
158
159    @Override
160    public String addTemplateInput() {
161        showParamEditor = true;
162        return super.addTemplateInput();
163    }
164
165    public String removeTemplateInput(String name) {
166        DocumentModel currentDocument = navigationContext.getCurrentDocument();
167
168        showParamEditor = true;
169
170        TemplateSourceDocument template = currentDocument.getAdapter(TemplateSourceDocument.class);
171        if (template != null) {
172
173            Iterator<TemplateInput> it = templateEditableInputs.listIterator();
174            while (it.hasNext()) {
175                TemplateInput input = it.next();
176                if (input.getName().equals(name)) {
177                    it.remove();
178                    break;
179                }
180            }
181
182            currentDocument = template.saveParams(templateEditableInputs, true);
183            newInput = null;
184            templateEditableInputs = null;
185            navigationContext.invalidateCurrentDocument();
186            return navigationContext.navigateToDocument(currentDocument);
187        } else {
188            return null;
189        }
190    }
191
192    public Collection<Type> getAllTypes() {
193        return typeManager.getTypes();
194    }
195
196    public Collection<Type> getForcableTypes() {
197
198        Collection<Type> types = typeManager.getTypes();
199
200        Iterator<Type> it = types.iterator();
201        while (it.hasNext()) {
202            Type type = it.next();
203            if (type.getId().equals("TemplateBasedFile")) {
204                it.remove();
205                break;
206            }
207        }
208        return types;
209    }
210
211    public Collection<TemplateProcessorDescriptor> getRegistredTemplateProcessors() {
212        return Framework.getLocalService(TemplateProcessorService.class).getRegisteredTemplateProcessors();
213    }
214
215    public Collection<OutputFormatDescriptor> getOutputFormatDescriptors() {
216        return Framework.getLocalService(TemplateProcessorService.class).getOutputFormats();
217    }
218
219    public List<String> getTemplateAndVersionsUUIDs() {
220        DocumentModel currentDocument = navigationContext.getCurrentDocument();
221
222        TemplateSourceDocument template = currentDocument.getAdapter(TemplateSourceDocument.class);
223        if (template != null) {
224            List<String> uuids = new ArrayList<>();
225            uuids.add("\"" + currentDocument.getId() + "\"");
226
227            if (showVersions) {
228                for (DocumentModel version : documentManager.getVersions(currentDocument.getRef())) {
229                    uuids.add("\"" + version.getId() + "\"");
230                }
231            }
232            return uuids;
233        }
234
235        return new ArrayList<>();
236    }
237
238    public boolean isShowParamEditor() {
239        return showParamEditor;
240    }
241
242    public boolean isShowUsageListing() {
243        return showUsageListing;
244    }
245
246    public void setShowUsageListing(boolean showUsageListing) {
247        this.showUsageListing = showUsageListing;
248    }
249
250    public boolean isShowVersions() {
251        return showVersions;
252    }
253
254    public void setShowVersions(boolean showVersions) {
255        this.showVersions = showVersions;
256    }
257
258    public boolean isCheckedInVersion() {
259        return checkedInVersion;
260    }
261
262    public void setCheckedInVersion(boolean checkedInVersion) {
263        this.checkedInVersion = checkedInVersion;
264    }
265
266}