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