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