001/*
002 * (C) Copyright 2010 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Anahide Tchertchian
016 */
017package org.nuxeo.ecm.platform.forms.layout.demo.jsf;
018
019import static org.jboss.seam.ScopeType.EVENT;
020import static org.jboss.seam.ScopeType.SESSION;
021
022import java.io.Serializable;
023import java.util.ArrayList;
024import java.util.Calendar;
025import java.util.HashMap;
026import java.util.List;
027import java.util.Map;
028
029import javax.faces.context.FacesContext;
030
031import org.jboss.seam.annotations.Create;
032import org.jboss.seam.annotations.Destroy;
033import org.jboss.seam.annotations.Factory;
034import org.jboss.seam.annotations.In;
035import org.jboss.seam.annotations.Name;
036import org.jboss.seam.annotations.Scope;
037import org.jboss.seam.annotations.intercept.BypassInterceptors;
038import org.nuxeo.ecm.core.api.Blob;
039import org.nuxeo.ecm.core.api.Blobs;
040import org.nuxeo.ecm.core.api.CoreInstance;
041import org.nuxeo.ecm.core.api.CoreSession;
042import org.nuxeo.ecm.core.api.DocumentModel;
043import org.nuxeo.ecm.core.api.NuxeoException;
044import org.nuxeo.ecm.platform.actions.Action;
045import org.nuxeo.ecm.platform.actions.ActionContext;
046import org.nuxeo.ecm.platform.actions.ejb.ActionManager;
047import org.nuxeo.ecm.platform.actions.jsf.JSFActionContext;
048import org.nuxeo.ecm.platform.forms.layout.demo.service.DemoWidgetType;
049import org.nuxeo.ecm.platform.forms.layout.demo.service.LayoutDemoManager;
050import org.nuxeo.ecm.platform.forms.layout.service.WebLayoutManager;
051import org.nuxeo.ecm.platform.query.api.Aggregate;
052import org.nuxeo.ecm.platform.query.api.AggregateDefinition;
053import org.nuxeo.ecm.platform.query.api.Bucket;
054import org.nuxeo.ecm.platform.query.api.PageSelection;
055import org.nuxeo.ecm.platform.query.api.PageSelections;
056import org.nuxeo.ecm.platform.query.core.AggregateBase;
057import org.nuxeo.ecm.platform.query.core.AggregateDescriptor;
058import org.nuxeo.ecm.platform.query.core.BucketTerm;
059
060/**
061 * Seam component providing information contextual to a session on the application.
062 *
063 * @author Anahide Tchertchian
064 */
065@Name("layoutDemoContext")
066@Scope(SESSION)
067public class LayoutDemoContext implements Serializable {
068
069    private static final long serialVersionUID = 1L;
070
071    public static final String TEST_REPO_NAME = "layoutDemo";
072
073    public static final String DEMO_DOCUMENT_TYPE = "LayoutDemoDocument";
074
075    /**
076     * @since 7.2
077     */
078    public static final String VALIDATION_DOCUMENT_TYPE = "LayoutValidationDocument";
079
080    protected CoreSession demoCoreSession;
081
082    @In(create = true)
083    protected LayoutDemoManager layoutDemoManager;
084
085    @In(create = true)
086    protected WebLayoutManager webLayoutManager;
087
088    @In(create = true)
089    protected transient ActionManager actionManager;
090
091    protected DocumentModel bareDemoDocument;
092
093    protected DocumentModel validationDocument;
094
095    protected DocumentModel previewDocument;
096
097    protected PageSelections<DocumentModel> demoDocuments;
098
099    protected List<Action> layoutDemoCustomActions;
100
101    protected Map<String, Aggregate<Bucket>> layoutDemoAggregates;
102
103    @Create
104    public void openCoreSession() {
105        demoCoreSession = CoreInstance.openCoreSessionSystem("layoutDemo");
106    }
107
108    @Destroy
109    @BypassInterceptors
110    public void closeCoreSession() {
111        if (demoCoreSession != null) {
112            demoCoreSession.close();
113        }
114    }
115
116    @Factory(value = "standardWidgetTypes", scope = SESSION)
117    public List<DemoWidgetType> getStandardWidgetTypes() {
118        return layoutDemoManager.getWidgetTypes("standard");
119    }
120
121    @Factory(value = "listingWidgetTypes", scope = SESSION)
122    public List<DemoWidgetType> getListingWidgetTypes() {
123        return layoutDemoManager.getWidgetTypes("listing");
124    }
125
126    @Factory(value = "customWidgetTypes", scope = SESSION)
127    public List<DemoWidgetType> getCustomWidgetTypes() {
128        return layoutDemoManager.getWidgetTypes("custom");
129    }
130
131    /**
132     * @since 6.0
133     */
134    @Factory(value = "aggregateWidgetTypes", scope = SESSION)
135    public List<DemoWidgetType> getAggregateWidgetTypes() {
136        return layoutDemoManager.getWidgetTypes("aggregate");
137    }
138
139    @Factory(value = "actionWidgetTypes", scope = SESSION)
140    public List<DemoWidgetType> getActionWidgetTypes() {
141        return layoutDemoManager.getWidgetTypes("action");
142    }
143
144    protected DocumentModel generateBareDemoDocument() {
145        return demoCoreSession.createDocumentModel(DEMO_DOCUMENT_TYPE);
146    }
147
148    /**
149     * @since 7.2
150     */
151    protected DocumentModel generateValidationDocument() {
152        DocumentModel doc = demoCoreSession.createDocumentModel(VALIDATION_DOCUMENT_TYPE);
153        doc.setPropertyValue("dc:title", "My title");
154        return doc;
155    }
156
157    @Factory(value = "layoutBareDemoDocument", scope = EVENT)
158    public DocumentModel getBareDemoDocument() {
159        if (bareDemoDocument == null) {
160            bareDemoDocument = generateBareDemoDocument();
161            bareDemoDocument.setPropertyValue("dc:title", "My title");
162            bareDemoDocument.setPropertyValue("dc:description", "My description");
163            bareDemoDocument.setPropertyValue("lds:select_coverage_field", "europe/France");
164        }
165        return bareDemoDocument;
166    }
167
168    /**
169     * @since 7.2
170     */
171    @Factory(value = "layoutValidationDocument", scope = EVENT)
172    public DocumentModel getValidationDocument() {
173        if (validationDocument == null) {
174            validationDocument = generateValidationDocument();
175        }
176        return validationDocument;
177    }
178
179    /**
180     * @since 7.2
181     */
182    public void resetValidationDocument() {
183        validationDocument = null;
184    }
185
186    @Factory(value = "layoutPreviewDocument", scope = EVENT)
187    public DocumentModel getPreviewDocument() {
188        if (previewDocument == null) {
189            previewDocument = generateBareDemoDocument();
190            previewDocument.setPathInfo("/", "preview");
191            fillPreviewDocumentProperties(previewDocument, 0);
192            previewDocument = demoCoreSession.createDocument(previewDocument);
193        }
194        return previewDocument;
195    }
196
197    @Factory(value = "layoutDemoDocuments", scope = EVENT)
198    public PageSelections<DocumentModel> getDemoDocuments() {
199        if (demoDocuments == null) {
200            List<PageSelection<DocumentModel>> docs = new ArrayList<PageSelection<DocumentModel>>();
201            DocumentModel demoDocument1 = getListingDemoDocument(1);
202            docs.add(new PageSelection<DocumentModel>(demoDocument1, false));
203            DocumentModel demoDocument2 = getListingDemoDocument(2);
204            docs.add(new PageSelection<DocumentModel>(demoDocument2, false));
205            demoDocuments = new PageSelections<DocumentModel>(docs);
206        }
207        return demoDocuments;
208    }
209
210    protected DocumentModel fillPreviewDocumentProperties(DocumentModel doc, int index) {
211        // fill all fields used in preview
212        if (index <= 1) {
213            doc.setPropertyValue("lds:textField", "Some sample text");
214            doc.setPropertyValue("lds:anotherTextField", "");
215            doc.setPropertyValue("lds:textareaField", "Some sample text with\nseveral lines.");
216            doc.setPropertyValue("lds:htmlField", "Some sample text<br/> with html <b>tags</b>.");
217            doc.setPropertyValue("lds:secretField", "Some secret text");
218            doc.setPropertyValue("lds:selectVocabularyField", "cartman");
219            doc.setPropertyValue("lds:selectMultiVocabularyField", new String[] { "cartman", "marsh" });
220            doc.setPropertyValue("lds:selectVocabularyLocalizedField", "europe");
221            doc.setPropertyValue("lds:selectMultiVocabularyLocalizedField", new String[] { "europe" });
222            doc.setPropertyValue("lds:selectVocabularyL10NField", "europe");
223            doc.setPropertyValue("lds:selectMultiVocabularyL10NField", new String[] { "europe", "africa" });
224            doc.setPropertyValue("lds:select_coverage_field", "africa/Botswana");
225            doc.setPropertyValue("lds:select_subjects_multi_fields", new String[] { "art/art history", "art/culture",
226                    "sciences/logic" });
227            doc.setPropertyValue("lds:select_user_field", "Administrator");
228            doc.setPropertyValue("lds:select_users_multi_fields", new String[] { "Administrator" });
229            doc.setPropertyValue("lds:select_doc_field", "");
230            doc.setPropertyValue("lds:select_docs_multi_fields", new String[] {});
231            doc.setPropertyValue("lds:dateField", Calendar.getInstance());
232            doc.setPropertyValue("lds:intField", new Integer(666));
233            doc.setPropertyValue("lds:booleanField", Boolean.FALSE);
234            Blob blob = Blobs.createBlob("Hello!\nThis is a sample text.");
235            blob.setFilename("hello.txt");
236            doc.setPropertyValue("lds:fileField", (Serializable) blob);
237
238            // complex props
239            ArrayList<Map<String, Serializable>> cl = new ArrayList<Map<String, Serializable>>();
240            HashMap<String, Serializable> clItem = new HashMap<String, Serializable>();
241            clItem.put("stringComplexItem", "Some sample text");
242            clItem.put("dateComplexItem", Calendar.getInstance());
243            clItem.put("intComplexItem", new Integer(33));
244            clItem.put("booleanComplexItem", Boolean.FALSE);
245            clItem.put("stringComplexItem2", "Hello, ");
246            clItem.put("stringComplexItem3", "is it me you're looking for?");
247
248            HashMap<String, Serializable> clItem2 = new HashMap<String, Serializable>();
249            clItem2.put("stringComplexItem", "Some other sample text");
250            clItem2.put("dateComplexItem", Calendar.getInstance());
251            clItem2.put("intComplexItem", new Integer(-2));
252            clItem2.put("booleanComplexItem", Boolean.TRUE);
253
254            cl.add(clItem);
255            cl.add(clItem2);
256
257            doc.setPropertyValue("lds:complexList", cl);
258            doc.setPropertyValue("lds:complexField", clItem);
259
260        } else {
261            doc.setPropertyValue("lds:textField", "Some other sample text");
262            doc.setPropertyValue("lds:anotherTextField", "");
263            doc.setPropertyValue("lds:textareaField", "Some other sample text with\nseveral lines.");
264            doc.setPropertyValue("lds:htmlField", "Some other sample text<br/> with html <b>tags</b>.");
265            doc.setPropertyValue("lds:secretField", "Some other secret text");
266            doc.setPropertyValue("lds:selectVocabularyField", "marsh");
267            doc.setPropertyValue("lds:selectMultiVocabularyField", new String[] { "cartman" });
268            doc.setPropertyValue("select_coverage_field", "africa/Botswana");
269            doc.setPropertyValue("lds:select_subjects_multi_fields", new String[] { "art/art history", "art/culture",
270                    "sciences/logic" });
271            doc.setPropertyValue("lds:dateField", Calendar.getInstance());
272            doc.setPropertyValue("lds:intField", new Integer(667));
273            doc.setPropertyValue("lds:booleanField", Boolean.TRUE);
274            Blob blob = Blobs.createBlob("Hello!\nThis is another sample text.");
275            blob.setFilename("hello-again.txt");
276            doc.setPropertyValue("lds:fileField", (Serializable) blob);
277        }
278        return doc;
279    }
280
281    protected DocumentModel fillListingDocumentProperties(DocumentModel doc, int index) {
282        if (index <= 1) {
283            doc.prefetchCurrentLifecycleState("project");
284        } else {
285            doc.prefetchCurrentLifecycleState("deleted");
286        }
287        // fill demo docs for listings
288        doc.setPropertyValue("dc:title", "Demo document " + index);
289        Calendar created = Calendar.getInstance();
290        created.set(Calendar.YEAR, 2000 + index);
291        created.set(Calendar.MONTH, 2 + index);
292        created.set(Calendar.DATE, 2 + index);
293        doc.setPropertyValue("dc:created", created);
294        Calendar modified = Calendar.getInstance();
295        modified.set(Calendar.YEAR, 2011);
296        modified.set(Calendar.MONTH, 3);
297        modified.set(Calendar.DATE, 16);
298        if (index <= 1) {
299            doc.setPropertyValue("dc:modified", modified);
300            doc.setPropertyValue("dc:creator", "Administrator");
301            doc.setPropertyValue("dc:lastContributor", "Administrator");
302        } else {
303            doc.setPropertyValue("dc:modified", created);
304            doc.setPropertyValue("dc:creator", "Administrator");
305            doc.setPropertyValue("dc:lastContributor", "Administrator");
306        }
307        doc.setPropertyValue("uid:major_version", new Integer(1));
308        doc.setPropertyValue("uid:minor_version", new Integer(index));
309        if (index <= 1) {
310            doc.setPropertyValue("common:icon", "/icons/pdf.png");
311        }
312        return doc;
313    }
314
315    protected DocumentModel getListingDemoDocument(int index) {
316        DocumentModel doc = generateBareDemoDocument();
317        doc.setPathInfo("/", "demoDoc_" + index);
318        fillListingDocumentProperties(doc, index);
319        fillPreviewDocumentProperties(doc, index);
320        doc = demoCoreSession.createDocument(doc);
321        // set lock after creation
322        if (index <= 1) {
323            doc.setLock();
324        }
325        return doc;
326    }
327
328    @Factory(value = "layoutDemoCustomActions", scope = EVENT)
329    public List<Action> getLayoutDemoCustomActions() {
330        if (layoutDemoCustomActions == null) {
331            layoutDemoCustomActions = new ArrayList<Action>();
332            FacesContext faces = FacesContext.getCurrentInstance();
333            if (faces == null) {
334                throw new NuxeoException("Faces context is null");
335            }
336            ActionContext ctx = new JSFActionContext(faces);
337            List<Action> actions = actionManager.getActions("LAYOUT_DEMO_ACTIONS", ctx);
338            if (actions != null) {
339                layoutDemoCustomActions.addAll(actions);
340            }
341        }
342        return layoutDemoCustomActions;
343    }
344
345    /**
346     * @since 6.0
347     */
348    @Factory(value = "layoutDemoAggregates", scope = EVENT)
349    public Map<String, Aggregate<Bucket>> getLayoutDemoAggregates() {
350        if (layoutDemoAggregates == null) {
351            layoutDemoAggregates = new HashMap<>();
352
353            AggregateDefinition mockDef = new AggregateDescriptor();
354            mockDef.setId("mock");
355
356            List<Bucket> buckets = new ArrayList<>();
357            buckets.add(new BucketTerm("eric", 10));
358            buckets.add(new BucketTerm("stan", 5));
359            buckets.add(new BucketTerm("kyle", 2));
360            Aggregate<Bucket> stringTerms = new AggregateBase<Bucket>(mockDef, null);
361            stringTerms.setBuckets(buckets);
362            layoutDemoAggregates.put("string_terms", stringTerms);
363
364            buckets = new ArrayList<>();
365            buckets.add(new BucketTerm("cartman", 10));
366            buckets.add(new BucketTerm("marsh", 5));
367            buckets.add(new BucketTerm("broflovski", 2));
368            Aggregate<Bucket> dirTerms = new AggregateBase<Bucket>(mockDef, null);
369            dirTerms.setBuckets(buckets);
370            layoutDemoAggregates.put("dir_terms", dirTerms);
371
372            buckets = new ArrayList<>();
373            buckets.add(new BucketTerm("oceania", 10));
374            buckets.add(new BucketTerm("antarctica", 5));
375            buckets.add(new BucketTerm("europe", 2));
376            Aggregate<Bucket> dirTermsL10n = new AggregateBase<Bucket>(mockDef, null);
377            dirTermsL10n.setBuckets(buckets);
378            layoutDemoAggregates.put("dir_terms_translated", dirTermsL10n);
379
380            buckets = new ArrayList<>();
381            buckets.add(new BucketTerm("oceania/Australia", 10));
382            buckets.add(new BucketTerm("antarctica", 5));
383            buckets.add(new BucketTerm("europe/France", 2));
384            Aggregate<Bucket> dirTermsL10nHier = new AggregateBase<Bucket>(mockDef, null);
385            dirTermsL10nHier.setBuckets(buckets);
386            layoutDemoAggregates.put("dir_terms_l10n", dirTermsL10nHier);
387        }
388        return layoutDemoAggregates;
389    }
390}