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        }
164        return bareDemoDocument;
165    }
166
167    /**
168     * @since 7.2
169     */
170    @Factory(value = "layoutValidationDocument", scope = EVENT)
171    public DocumentModel getValidationDocument() {
172        if (validationDocument == null) {
173            validationDocument = generateValidationDocument();
174        }
175        return validationDocument;
176    }
177
178    /**
179     * @since 7.2
180     */
181    public void resetValidationDocument() {
182        validationDocument = null;
183    }
184
185    @Factory(value = "layoutPreviewDocument", scope = EVENT)
186    public DocumentModel getPreviewDocument() {
187        if (previewDocument == null) {
188            previewDocument = generateBareDemoDocument();
189            previewDocument.setPathInfo("/", "preview");
190            fillPreviewDocumentProperties(previewDocument, 0);
191            previewDocument = demoCoreSession.createDocument(previewDocument);
192        }
193        return previewDocument;
194    }
195
196    @Factory(value = "layoutDemoDocuments", scope = EVENT)
197    public PageSelections<DocumentModel> getDemoDocuments() {
198        if (demoDocuments == null) {
199            List<PageSelection<DocumentModel>> docs = new ArrayList<PageSelection<DocumentModel>>();
200            DocumentModel demoDocument1 = getListingDemoDocument(1);
201            docs.add(new PageSelection<DocumentModel>(demoDocument1, false));
202            DocumentModel demoDocument2 = getListingDemoDocument(2);
203            docs.add(new PageSelection<DocumentModel>(demoDocument2, false));
204            demoDocuments = new PageSelections<DocumentModel>(docs);
205        }
206        return demoDocuments;
207    }
208
209    protected DocumentModel fillPreviewDocumentProperties(DocumentModel doc, int index) {
210        // fill all fields used in preview
211        if (index <= 1) {
212            doc.setPropertyValue("lds:textField", "Some sample text");
213            doc.setPropertyValue("lds:anotherTextField", "");
214            doc.setPropertyValue("lds:textareaField", "Some sample text with\nseveral lines.");
215            doc.setPropertyValue("lds:htmlField", "Some sample text<br/> with html <b>tags</b>.");
216            doc.setPropertyValue("lds:secretField", "Some secret text");
217            doc.setPropertyValue("lds:selectVocabularyField", "cartman");
218            doc.setPropertyValue("lds:selectMultiVocabularyField", new String[] { "cartman", "marsh" });
219            doc.setPropertyValue("lds:selectVocabularyLocalizedField", "europe");
220            doc.setPropertyValue("lds:selectMultiVocabularyLocalizedField", new String[] { "europe" });
221            doc.setPropertyValue("lds:selectVocabularyL10NField", "europe");
222            doc.setPropertyValue("lds:selectMultiVocabularyL10NField", new String[] { "europe", "africa" });
223            doc.setPropertyValue("lds:select_coverage_field", "africa/Botswana");
224            doc.setPropertyValue("lds:select_subjects_multi_fields", new String[] { "art/art history", "art/culture",
225                    "sciences/logic" });
226            doc.setPropertyValue("lds:select_user_field", "Administrator");
227            doc.setPropertyValue("lds:select_users_multi_fields", new String[] { "Administrator" });
228            doc.setPropertyValue("lds:select_doc_field", "");
229            doc.setPropertyValue("lds:select_docs_multi_fields", new String[] {});
230            doc.setPropertyValue("lds:dateField", Calendar.getInstance());
231            doc.setPropertyValue("lds:intField", new Integer(666));
232            doc.setPropertyValue("lds:booleanField", Boolean.FALSE);
233            Blob blob = Blobs.createBlob("Hello!\nThis is a sample text.");
234            blob.setFilename("hello.txt");
235            doc.setPropertyValue("lds:fileField", (Serializable) blob);
236
237            // complex props
238            ArrayList<Map<String, Serializable>> cl = new ArrayList<Map<String, Serializable>>();
239            HashMap<String, Serializable> clItem = new HashMap<String, Serializable>();
240            clItem.put("stringComplexItem", "Some sample text");
241            clItem.put("dateComplexItem", Calendar.getInstance());
242            clItem.put("intComplexItem", new Integer(33));
243            clItem.put("booleanComplexItem", Boolean.FALSE);
244            clItem.put("stringComplexItem2", "Hello, ");
245            clItem.put("stringComplexItem3", "is it me you're looking for?");
246
247            HashMap<String, Serializable> clItem2 = new HashMap<String, Serializable>();
248            clItem2.put("stringComplexItem", "Some other sample text");
249            clItem2.put("dateComplexItem", Calendar.getInstance());
250            clItem2.put("intComplexItem", new Integer(-2));
251            clItem2.put("booleanComplexItem", Boolean.TRUE);
252
253            cl.add(clItem);
254            cl.add(clItem2);
255
256            doc.setPropertyValue("lds:complexList", cl);
257            doc.setPropertyValue("lds:complexField", clItem);
258
259        } else {
260            doc.setPropertyValue("lds:textField", "Some other sample text");
261            doc.setPropertyValue("lds:anotherTextField", "");
262            doc.setPropertyValue("lds:textareaField", "Some other sample text with\nseveral lines.");
263            doc.setPropertyValue("lds:htmlField", "Some other sample text<br/> with html <b>tags</b>.");
264            doc.setPropertyValue("lds:secretField", "Some other secret text");
265            doc.setPropertyValue("lds:selectVocabularyField", "marsh");
266            doc.setPropertyValue("lds:selectMultiVocabularyField", new String[] { "cartman" });
267            doc.setPropertyValue("select_coverage_field", "africa/Botswana");
268            doc.setPropertyValue("lds:select_subjects_multi_fields", new String[] { "art/art history", "art/culture",
269                    "sciences/logic" });
270            doc.setPropertyValue("lds:dateField", Calendar.getInstance());
271            doc.setPropertyValue("lds:intField", new Integer(667));
272            doc.setPropertyValue("lds:booleanField", Boolean.TRUE);
273            Blob blob = Blobs.createBlob("Hello!\nThis is another sample text.");
274            blob.setFilename("hello-again.txt");
275            doc.setPropertyValue("lds:fileField", (Serializable) blob);
276        }
277        return doc;
278    }
279
280    protected DocumentModel fillListingDocumentProperties(DocumentModel doc, int index) {
281        if (index <= 1) {
282            doc.prefetchCurrentLifecycleState("project");
283        } else {
284            doc.prefetchCurrentLifecycleState("deleted");
285        }
286        // fill demo docs for listings
287        doc.setPropertyValue("dc:title", "Demo document " + index);
288        Calendar created = Calendar.getInstance();
289        created.set(Calendar.YEAR, 2000 + index);
290        created.set(Calendar.MONTH, 2 + index);
291        created.set(Calendar.DATE, 2 + index);
292        doc.setPropertyValue("dc:created", created);
293        Calendar modified = Calendar.getInstance();
294        modified.set(Calendar.YEAR, 2011);
295        modified.set(Calendar.MONTH, 3);
296        modified.set(Calendar.DATE, 16);
297        if (index <= 1) {
298            doc.setPropertyValue("dc:modified", modified);
299            doc.setPropertyValue("dc:creator", "Administrator");
300            doc.setPropertyValue("dc:lastContributor", "Administrator");
301        } else {
302            doc.setPropertyValue("dc:modified", created);
303            doc.setPropertyValue("dc:creator", "Administrator");
304            doc.setPropertyValue("dc:lastContributor", "Administrator");
305        }
306        doc.setPropertyValue("uid:major_version", new Integer(1));
307        doc.setPropertyValue("uid:minor_version", new Integer(index));
308        if (index <= 1) {
309            doc.setPropertyValue("common:icon", "/icons/pdf.png");
310        }
311        return doc;
312    }
313
314    protected DocumentModel getListingDemoDocument(int index) {
315        DocumentModel doc = generateBareDemoDocument();
316        doc.setPathInfo("/", "demoDoc_" + index);
317        fillListingDocumentProperties(doc, index);
318        fillPreviewDocumentProperties(doc, index);
319        doc = demoCoreSession.createDocument(doc);
320        // set lock after creation
321        if (index <= 1) {
322            doc.setLock();
323        }
324        return doc;
325    }
326
327    @Factory(value = "layoutDemoCustomActions", scope = EVENT)
328    public List<Action> getLayoutDemoCustomActions() {
329        if (layoutDemoCustomActions == null) {
330            layoutDemoCustomActions = new ArrayList<Action>();
331            FacesContext faces = FacesContext.getCurrentInstance();
332            if (faces == null) {
333                throw new NuxeoException("Faces context is null");
334            }
335            ActionContext ctx = new JSFActionContext(faces);
336            List<Action> actions = actionManager.getActions("LAYOUT_DEMO_ACTIONS", ctx);
337            if (actions != null) {
338                layoutDemoCustomActions.addAll(actions);
339            }
340        }
341        return layoutDemoCustomActions;
342    }
343
344    /**
345     * @since 6.0
346     */
347    @Factory(value = "layoutDemoAggregates", scope = EVENT)
348    public Map<String, Aggregate<Bucket>> getLayoutDemoAggregates() {
349        if (layoutDemoAggregates == null) {
350            layoutDemoAggregates = new HashMap<>();
351
352            AggregateDefinition mockDef = new AggregateDescriptor();
353            mockDef.setId("mock");
354
355            List<Bucket> buckets = new ArrayList<>();
356            buckets.add(new BucketTerm("eric", 10));
357            buckets.add(new BucketTerm("stan", 5));
358            buckets.add(new BucketTerm("kyle", 2));
359            Aggregate<Bucket> stringTerms = new AggregateBase<Bucket>(mockDef, null);
360            stringTerms.setBuckets(buckets);
361            layoutDemoAggregates.put("string_terms", stringTerms);
362
363            buckets = new ArrayList<>();
364            buckets.add(new BucketTerm("cartman", 10));
365            buckets.add(new BucketTerm("marsh", 5));
366            buckets.add(new BucketTerm("broflovski", 2));
367            Aggregate<Bucket> dirTerms = new AggregateBase<Bucket>(mockDef, null);
368            dirTerms.setBuckets(buckets);
369            layoutDemoAggregates.put("dir_terms", dirTerms);
370
371            buckets = new ArrayList<>();
372            buckets.add(new BucketTerm("oceania", 10));
373            buckets.add(new BucketTerm("antarctica", 5));
374            buckets.add(new BucketTerm("europe", 2));
375            Aggregate<Bucket> dirTermsL10n = new AggregateBase<Bucket>(mockDef, null);
376            dirTermsL10n.setBuckets(buckets);
377            layoutDemoAggregates.put("dir_terms_translated", dirTermsL10n);
378
379            buckets = new ArrayList<>();
380            buckets.add(new BucketTerm("oceania/Australia", 10));
381            buckets.add(new BucketTerm("antarctica", 5));
382            buckets.add(new BucketTerm("europe/France", 2));
383            Aggregate<Bucket> dirTermsL10nHier = new AggregateBase<Bucket>(mockDef, null);
384            dirTermsL10nHier.setBuckets(buckets);
385            layoutDemoAggregates.put("dir_terms_l10n", dirTermsL10nHier);
386        }
387        return layoutDemoAggregates;
388    }
389}