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