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 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 List<TemplateInput> templateEditableInputs;
061
062    protected TemplateInput newInput;
063
064    protected boolean showParamEditor = false;
065
066    protected boolean showUsageListing = false;
067
068    protected boolean showVersions = false;
069
070    protected boolean checkedInVersion = false;
071
072    public String createTemplate() {
073        DocumentModel changeableDocument = navigationContext.getChangeableDocument();
074        TemplateSourceDocument sourceTemplate = changeableDocument.getAdapter(TemplateSourceDocument.class);
075        if (sourceTemplate != null && sourceTemplate.getTemplateBlob() != null) {
076            try {
077                sourceTemplate.initTemplate(false);
078                if (sourceTemplate.hasEditableParams()) {
079                    templateInputs = sourceTemplate.getParams();
080                    return "editTemplateRelatedData";
081                }
082            } catch (PropertyException e) {
083                log.error("Error during parameter automatic initialization", e);
084            }
085        }
086        return documentActions.saveDocument(changeableDocument);
087    }
088
089    public List<TemplateInput> getTemplateInputs() {
090        return templateInputs;
091    }
092
093    public void setTemplateInputs(List<TemplateInput> templateInputs) {
094        this.templateInputs = templateInputs;
095    }
096
097    public String saveDocument() {
098        DocumentModel changeableDocument = navigationContext.getChangeableDocument();
099
100        for (TemplateInput ti : templateInputs) {
101            log.info(ti.toString());
102        }
103        TemplateSourceDocument source = changeableDocument.getAdapter(TemplateSourceDocument.class);
104        if (source != null) {
105            source.saveParams(templateInputs, false);
106        }
107
108        return documentActions.saveDocument(changeableDocument);
109    }
110
111    @Observer(value = { EventNames.DOCUMENT_SELECTION_CHANGED, EventNames.NEW_DOCUMENT_CREATED,
112            EventNames.DOCUMENT_CHANGED }, create = false)
113    @BypassInterceptors
114    public void reset() {
115        templateInputs = null;
116        templateEditableInputs = null;
117        showParamEditor = false;
118    }
119
120    public List<TemplateInput> getTemplateEditableInputs() {
121        if (templateEditableInputs == null) {
122            DocumentModel currentDocument = navigationContext.getCurrentDocument();
123
124            TemplateSourceDocument template = currentDocument.getAdapter(TemplateSourceDocument.class);
125            if (template != null) {
126                templateEditableInputs = template.getParams();
127            }
128        }
129        return templateEditableInputs;
130    }
131
132    public void setTemplateEditableInputs(List<TemplateInput> templateEditableInputs) {
133        this.templateEditableInputs = templateEditableInputs;
134    }
135
136    public String saveTemplateInputs() {
137
138        DocumentModel currentDocument = navigationContext.getCurrentDocument();
139
140        TemplateSourceDocument template = currentDocument.getAdapter(TemplateSourceDocument.class);
141        if (template != null) {
142            currentDocument = template.saveParams(templateEditableInputs, true);
143        }
144        return navigationContext.navigateToDocument(currentDocument);
145    }
146
147    public void cancelTemplateInputsEdit() {
148        reset();
149    }
150
151    public TemplateInput getNewInput() {
152        if (newInput == null) {
153            newInput = new TemplateInput("newField");
154        }
155        return newInput;
156    }
157
158    public void setNewInput(TemplateInput newInput) {
159        this.newInput = newInput;
160    }
161
162    public String addTemplateInput() {
163        DocumentModel currentDocument = navigationContext.getCurrentDocument();
164
165        showParamEditor = true;
166        TemplateSourceDocument template = currentDocument.getAdapter(TemplateSourceDocument.class);
167        if (template != null) {
168            template.addInput(newInput);
169            newInput = null;
170            templateEditableInputs = null;
171        } else {
172            return null;
173        }
174
175        return navigationContext.navigateToDocument(currentDocument);
176    }
177
178    public String removeTemplateInput(String name) {
179        DocumentModel currentDocument = navigationContext.getCurrentDocument();
180
181        showParamEditor = true;
182
183        TemplateSourceDocument template = currentDocument.getAdapter(TemplateSourceDocument.class);
184        if (template != null) {
185
186            Iterator<TemplateInput> it = templateEditableInputs.listIterator();
187            while (it.hasNext()) {
188                TemplateInput input = it.next();
189                if (input.getName().equals(name)) {
190                    it.remove();
191                    break;
192                }
193            }
194
195            currentDocument = template.saveParams(templateEditableInputs, true);
196            newInput = null;
197            templateEditableInputs = null;
198            return navigationContext.navigateToDocument(currentDocument);
199        } else {
200            return null;
201        }
202    }
203
204    public Collection<Type> getAllTypes() {
205        return typeManager.getTypes();
206    }
207
208    public Collection<Type> getForcableTypes() {
209
210        Collection<Type> types = typeManager.getTypes();
211
212        Iterator<Type> it = types.iterator();
213        while (it.hasNext()) {
214            Type type = it.next();
215            if (type.getId().equals("TemplateBasedFile")) {
216                it.remove();
217                break;
218            }
219        }
220        return types;
221    }
222
223    public Collection<TemplateProcessorDescriptor> getRegistredTemplateProcessors() {
224        return Framework.getLocalService(TemplateProcessorService.class).getRegisteredTemplateProcessors();
225    }
226
227    public Collection<OutputFormatDescriptor> getOutputFormatDescriptors() {
228        return Framework.getLocalService(TemplateProcessorService.class).getOutputFormats();
229    }
230
231    public List<String> getTemplateAndVersionsUUIDs() {
232        DocumentModel currentDocument = navigationContext.getCurrentDocument();
233
234        TemplateSourceDocument template = currentDocument.getAdapter(TemplateSourceDocument.class);
235        if (template != null) {
236            List<String> uuids = new ArrayList<String>();
237            uuids.add(currentDocument.getId());
238
239            if (showVersions) {
240                for (DocumentModel version : documentManager.getVersions(currentDocument.getRef())) {
241                    uuids.add(version.getId());
242                }
243            }
244            return uuids;
245        }
246
247        return new ArrayList<String>();
248    }
249
250    public boolean isShowParamEditor() {
251        return showParamEditor;
252    }
253
254    public boolean isShowUsageListing() {
255        return showUsageListing;
256    }
257
258    public void setShowUsageListing(boolean showUsageListing) {
259        this.showUsageListing = showUsageListing;
260    }
261
262    public boolean isShowVersions() {
263        return showVersions;
264    }
265
266    public void setShowVersions(boolean showVersions) {
267        this.showVersions = showVersions;
268    }
269
270    public boolean isCheckedInVersion() {
271        return checkedInVersion;
272    }
273
274    public void setCheckedInVersion(boolean checkedInVersion) {
275        this.checkedInVersion = checkedInVersion;
276    }
277
278}