1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 package org.imixs.workflow.jee.faces;
29
30 import java.text.MessageFormat;
31 import java.util.ArrayList;
32 import java.util.Collection;
33 import java.util.List;
34 import java.util.Locale;
35 import java.util.ResourceBundle;
36
37 import javax.ejb.EJB;
38 import javax.faces.application.FacesMessage;
39 import javax.faces.component.UIComponent;
40 import javax.faces.component.UIData;
41 import javax.faces.component.UIParameter;
42 import javax.faces.context.ExternalContext;
43 import javax.faces.context.FacesContext;
44 import javax.faces.event.ActionEvent;
45
46 import org.imixs.workflow.ItemCollection;
47 import org.imixs.workflow.jee.ejb.WorkflowService;
48
49
50
51
52
53
54
55
56
57
58 public abstract class AbstractWorkflowController {
59 protected ItemCollection workitemItemCollection = null;
60
61 private String type;
62
63
64 private List<ItemCollection> activityList;
65 private List<ItemCollection> startProcessList = null;
66 private String modelVersion = null;
67
68
69 private List<ItemCollection> workitems = null;
70 private int maxSearchResult = 10;
71 private int row = 0;
72 private boolean endOfList = false;
73 private int queryType = 0;
74 private String searchQuery = null;
75 final int QUERY_WORKLIST_BY_OWNER = 0;
76 final int QUERY_WORKLIST_BY_CREATOR = 1;
77 final int QUERY_WORKLIST_BY_AUTHOR = 2;
78 final int QUERY_WORKLIST_ALL = 3;
79 final int QUERY_WORKLIST_ARCHIVE = 4;
80 final int QUERY_SEARCH = 5;
81 final int QUERY_WORKLIST_BY_WRITEACCESS = 6;
82 private int sortOrder = WorkflowService.SORT_ORDER_CREATED_DESC;
83
84
85
86
87 @EJB
88 private org.imixs.workflow.jee.ejb.EntityService entityService;
89
90 @EJB
91 private org.imixs.workflow.jee.ejb.ModelService modelService;
92
93 @EJB
94 private org.imixs.workflow.jee.ejb.WorkflowService workflowService;
95
96 public AbstractWorkflowController() {
97 super();
98 setType("workitem");
99 workitemItemCollection = new ItemCollection();
100
101 }
102
103
104
105
106
107
108 public org.imixs.workflow.jee.ejb.EntityService getEntityService() {
109 return entityService;
110 }
111
112
113
114
115
116
117 public org.imixs.workflow.jee.ejb.ModelService getModelService() {
118 return modelService;
119 }
120
121
122
123
124
125
126 public org.imixs.workflow.jee.ejb.WorkflowService getWorkflowService() {
127 return workflowService;
128 }
129
130
131
132
133
134
135 public String getID() {
136 if (workitemItemCollection == null)
137 return null;
138 else
139 return workitemItemCollection.getItemValueString("$uniqueid");
140 }
141
142
143
144
145
146 public String getType() {
147 return type;
148 }
149
150
151
152
153
154
155
156
157
158 public void setType(String type) {
159 this.type = type;
160 }
161
162
163
164
165
166
167 public int getSortOrder() {
168 return sortOrder;
169 }
170
171 public void setSortOrder(int sortOrder) {
172 this.sortOrder = sortOrder;
173 }
174
175
176
177
178
179
180
181 public String getModelVersion() {
182 if (modelVersion == null) {
183 try {
184 modelVersion = modelService.getLatestVersion();
185 } catch (Exception e) {
186 e.printStackTrace();
187 return null;
188 }
189 }
190 return modelVersion;
191 }
192
193 public void setModelVersion(String modelVersion) {
194 this.modelVersion = modelVersion;
195 }
196
197
198
199
200
201
202 public int getMaxSearchResult() {
203 return maxSearchResult;
204 }
205
206
207
208
209
210
211 public void setMaxSearchResult(int searchCount) {
212 this.maxSearchResult = searchCount;
213 }
214
215
216
217
218
219
220
221
222
223 public String getSearchQuery() {
224 return searchQuery;
225 }
226
227
228
229
230
231
232 public void setSearchQuery(String query) {
233 this.searchQuery = query;
234 }
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256 public void setWorkitem(ItemCollection aworkitem) {
257 if (aworkitem != null)
258 workitemItemCollection = aworkitem;
259 else
260 workitemItemCollection = new ItemCollection();
261
262 }
263
264 public ItemCollection getWorkitem() {
265 return workitemItemCollection;
266 }
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293 public void doCreateWorkitem(ActionEvent event) throws Exception {
294
295 List children = event.getComponent().getChildren();
296 String processEntityIdentifier = "";
297
298 for (int i = 0; i < children.size(); i++) {
299 if (children.get(i) instanceof UIParameter) {
300 UIParameter currentParam = (UIParameter) children.get(i);
301 if (currentParam.getName().equals("id")
302 && currentParam.getValue() != null) {
303 processEntityIdentifier = currentParam.getValue()
304 .toString();
305 break;
306 }
307 }
308 }
309
310 if (processEntityIdentifier != null
311 && !"".equals(processEntityIdentifier)) {
312
313 String sProcessModelVersion = null;
314 String sProcessID = processEntityIdentifier;
315
316
317 if (processEntityIdentifier.indexOf('|') > -1) {
318
319 sProcessModelVersion = processEntityIdentifier.substring(0,
320 processEntityIdentifier.indexOf('|'));
321 sProcessID = processEntityIdentifier
322 .substring(processEntityIdentifier.indexOf('|') + 1);
323
324
325 this.setModelVersion(sProcessModelVersion);
326 }
327
328
329 workitemItemCollection = createWorkitem(sProcessModelVersion,
330 Integer.parseInt(sProcessID));
331 } else {
332
333 workitemItemCollection = new ItemCollection();
334 FacesContext context = FacesContext.getCurrentInstance();
335 ExternalContext externalContext = context.getExternalContext();
336 String sUser = externalContext.getRemoteUser();
337 workitemItemCollection.replaceItemValue("namCreator", sUser);
338 workitemItemCollection.replaceItemValue("type", getType());
339
340 }
341
342
343 this.setWorkitem(workitemItemCollection);
344 }
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360 public ItemCollection createWorkitem(String sModelVersion, int ProcessID)
361 throws Exception {
362 ItemCollection itemColProcessEntity = null;
363 ItemCollection aWorkitem;
364
365 try {
366 if (sModelVersion == null)
367 sModelVersion = this.getModelVersion();
368 else
369
370 setModelVersion(sModelVersion);
371
372 itemColProcessEntity = modelService.getProcessEntityByVersion(
373 ProcessID, sModelVersion);
374
375
376 if (itemColProcessEntity == null)
377 throw new NullPointerException();
378 } catch (Exception eproc) {
379 throw new Exception(
380 "unable to find ProcessEntity in model version "
381 + sModelVersion + " for ID=" + ProcessID);
382 }
383
384 String sEditorID = itemColProcessEntity
385 .getItemValueString("txteditorid");
386 int processID = itemColProcessEntity
387 .getItemValueInteger("numProcessID");
388 String sWorkflowGroup = itemColProcessEntity
389 .getItemValueString("txtworkflowgroup");
390
391
392 aWorkitem = new ItemCollection();
393 aWorkitem.replaceItemValue("$ProcessID", new Integer(processID));
394 FacesContext context = FacesContext.getCurrentInstance();
395 ExternalContext externalContext = context.getExternalContext();
396 String sUser = externalContext.getRemoteUser();
397
398 aWorkitem.replaceItemValue("namCreator", sUser);
399 aWorkitem.replaceItemValue("$WriteAccess", sUser);
400
401
402 aWorkitem.replaceItemValue("$modelversion", this.getModelVersion());
403 aWorkitem.replaceItemValue("txtworkflowgroup", sWorkflowGroup);
404 aWorkitem.replaceItemValue("txteditorid", sEditorID);
405
406 aWorkitem.replaceItemValue("type", getType());
407
408
409
410 return aWorkitem;
411
412
413
414 }
415
416
417
418
419
420
421
422
423
424
425
426 public void doProcessWorkitem(ActionEvent event) throws Exception {
427
428 List children = event.getComponent().getChildren();
429 int activityID = -1;
430
431 for (int i = 0; i < children.size(); i++) {
432 if (children.get(i) instanceof UIParameter) {
433 UIParameter currentParam = (UIParameter) children.get(i);
434 if (currentParam.getName().equals("id")
435 && currentParam.getValue() != null) {
436
437 activityID = Integer.parseInt(currentParam.getValue()
438 .toString());
439 break;
440 }
441 }
442 }
443
444
445 workitemItemCollection.replaceItemValue("type", getType());
446
447 workitemItemCollection.replaceItemValue("$activityid", activityID);
448
449
450 workitemItemCollection = workflowService
451 .processWorkItem(workitemItemCollection);
452
453
454
455 this.setWorkitem(workitemItemCollection);
456
457 doReset(event);
458 }
459
460
461
462
463
464
465 public void doEdit(ActionEvent event) {
466 ItemCollection currentSelection = null;
467
468 UIComponent component = event.getComponent();
469 for (UIComponent parent = component.getParent(); parent != null; parent = parent
470 .getParent()) {
471 if (!(parent instanceof UIData))
472 continue;
473
474
475 currentSelection = (ItemCollection) ((UIData) parent).getRowData();
476 setWorkitem(currentSelection);
477 break;
478
479 }
480 }
481
482
483
484
485
486
487
488
489 public void doDelete(ActionEvent event) throws Exception {
490 ItemCollection currentSelection = null;
491
492 UIComponent component = event.getComponent();
493 for (UIComponent parent = component.getParent(); parent != null; parent = parent
494 .getParent()) {
495 if (!(parent instanceof UIData))
496 continue;
497
498
499 currentSelection = (ItemCollection) ((UIData) parent).getRowData();
500 break;
501 }
502
503
504 if (currentSelection == null)
505 currentSelection = this.workitemItemCollection;
506 if (currentSelection != null) {
507 deleteChilds(currentSelection);
508 entityService.remove(currentSelection);
509 this.setWorkitem(null);
510 doReset(event);
511 }
512
513 }
514
515
516
517
518
519
520 private void deleteChilds(ItemCollection parent) {
521 try {
522 String id = parent.getItemValueString("$uniqueid");
523
524 String sQuery = null;
525 sQuery = "SELECT";
526 sQuery += " wi FROM Entity as wi JOIN wi.textItems as t "
527 + "WHERE ";
528 sQuery += " t.itemName = '$uniqueidref' and t.itemValue = '" + id
529 + "'";
530
531 Collection<ItemCollection> col = entityService.findAllEntities(
532 sQuery, 0, -1);
533
534 for (ItemCollection aworkitem : col) {
535
536 deleteChilds(aworkitem);
537
538 entityService.remove(aworkitem);
539 }
540 } catch (Exception e) {
541 e.printStackTrace();
542 }
543
544 }
545
546
547
548
549
550
551 public void doReset(ActionEvent event) {
552 workitems = null;
553 row = 0;
554 }
555
556
557
558
559
560 public void doRefresh(ActionEvent event) {
561 workitems = null;
562 }
563
564 public void doSwitchToWorklistByAuthor(ActionEvent event) {
565 queryType = this.QUERY_WORKLIST_BY_AUTHOR;
566 doReset(event);
567 }
568
569 public void doSwitchToWorklistByWriteAccess(ActionEvent event) {
570 queryType = this.QUERY_WORKLIST_BY_WRITEACCESS;
571 doReset(event);
572 }
573
574 public void doSwitchToWorklistByOwner(ActionEvent event) {
575 queryType = this.QUERY_WORKLIST_BY_OWNER;
576 doReset(event);
577 }
578
579 public void doSwitchToWorklistByCreator(ActionEvent event) {
580 queryType = this.QUERY_WORKLIST_BY_CREATOR;
581 doReset(event);
582 }
583
584 public void doSwitchToWorklistAll(ActionEvent event) {
585 queryType = this.QUERY_WORKLIST_ALL;
586 doReset(event);
587 }
588
589 public void doSwitchToSearchlist(ActionEvent event) {
590 queryType = this.QUERY_SEARCH;
591 doReset(event);
592 }
593
594 public void doLoadNext(ActionEvent event) {
595 row = row + maxSearchResult;
596 workitems = null;
597 }
598
599 public void doLoadPrev(ActionEvent event) {
600 row = row - maxSearchResult;
601 if (row < 0)
602 row = 0;
603 workitems = null;
604 }
605
606 public List<ItemCollection> getWorkitems() {
607 if (workitems == null)
608 loadWorkItemList();
609 return workitems;
610 }
611
612 public void setWorkitems(List<ItemCollection> workitems) {
613 this.workitems = workitems;
614 }
615
616
617
618
619
620 public int getRow() {
621 return row;
622 }
623
624 public boolean isEndOfList() {
625 return endOfList;
626 }
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641 private void loadWorkItemList() {
642 workitems = new ArrayList<ItemCollection>();
643 Collection<ItemCollection> col = null;
644 try {
645 long lTime = System.currentTimeMillis();
646
647 switch (queryType) {
648 case QUERY_WORKLIST_BY_OWNER:
649 col = findWorkitemsByOwner(row, maxSearchResult);
650 break;
651 case QUERY_WORKLIST_BY_CREATOR:
652 col = findWorkitemsByCreator(row, maxSearchResult);
653 break;
654
655 case QUERY_WORKLIST_BY_AUTHOR:
656 col = findWorkitemsByAuthor(row, maxSearchResult);
657 break;
658
659 case QUERY_WORKLIST_BY_WRITEACCESS:
660 col = findWorkitemsByWriteAccess(row, maxSearchResult);
661 break;
662
663 case QUERY_SEARCH:
664 col = findWorkitemsByQuery(getSearchQuery(), row,
665 maxSearchResult);
666 break;
667
668
669 default:
670 col = findAllWorkitems(row, maxSearchResult);
671 }
672
673 lTime = System.currentTimeMillis() - lTime;
674 System.out.println(" loadWorkItemList (" + lTime + " ms)");
675
676 endOfList = col.size() < maxSearchResult;
677 for (ItemCollection aworkitem : col) {
678 workitems.add(aworkitem);
679 }
680 } catch (Exception ee) {
681 ee.printStackTrace();
682 }
683
684
685 setWorkitem(null);
686 }
687
688 private List<ItemCollection> findWorkitemsByQuery(String query, int row,
689 int count) {
690 ArrayList<ItemCollection> workitemList = new ArrayList<ItemCollection>();
691
692 if (query == null || "".equals(query))
693 return workitemList;
694 Collection<ItemCollection> col = entityService.findAllEntities(query,
695 row, count);
696 workitemList.addAll(col);
697 return workitemList;
698 }
699
700
701
702
703
704
705
706
707
708 private List<ItemCollection> findWorkitemsByOwner(int row, int count)
709 throws Exception {
710 ArrayList<ItemCollection> workList = new ArrayList<ItemCollection>();
711 Collection<ItemCollection> col = workflowService.getWorkListByOwner(
712 null, row, count, type, getSortOrder());
713 workList.addAll(col);
714
715 return workList;
716
717 }
718
719
720
721
722
723
724
725
726
727 private List<ItemCollection> findWorkitemsByAuthor(int row, int count)
728 throws Exception {
729 ArrayList<ItemCollection> workList = new ArrayList<ItemCollection>();
730 Collection<ItemCollection> col = workflowService.getWorkList(null, row,
731 count, type, getSortOrder());
732 workList.addAll(col);
733
734 return workList;
735
736 }
737
738
739
740
741
742
743
744
745
746
747 private List<ItemCollection> findWorkitemsByWriteAccess(int row, int count)
748 throws Exception {
749 ArrayList<ItemCollection> workList = new ArrayList<ItemCollection>();
750 Collection<ItemCollection> col = workflowService
751 .getWorkListByWriteAccess(row, count, type, getSortOrder());
752 workList.addAll(col);
753 return workList;
754 }
755
756
757
758
759
760
761
762
763
764 private List<ItemCollection> findWorkitemsByCreator(int row, int count)
765 throws Exception {
766 ArrayList<ItemCollection> workList = new ArrayList<ItemCollection>();
767 Collection<ItemCollection> col = workflowService.getWorkListByCreator(
768 null, row, count, type, getSortOrder());
769 workList.addAll(col);
770 return workList;
771 }
772
773
774
775
776
777
778
779
780
781 private List<ItemCollection> findAllWorkitems(int row, int count) {
782 ArrayList<ItemCollection> teamList = new ArrayList<ItemCollection>();
783
784 String sQuery = "SELECT wi FROM Entity AS wi " + " WHERE wi.type= '"
785 + getType() + "' ORDER BY wi.modified desc";
786
787 Collection<ItemCollection> col = entityService.findAllEntities(sQuery,
788 row, count);
789
790 teamList.addAll(col);
791
792 return teamList;
793 }
794
795
796
797
798
799
800
801
802
803 public String getAction() {
804 if (workitemItemCollection == null)
805 return "show_workitem";
806
807 String sResult = workitemItemCollection.getItemValueString("action");
808 if ("".equals(sResult))
809 return "show_workitem";
810 else
811 return sResult;
812 }
813
814
815
816
817
818
819
820
821 public List<ItemCollection> getActivities() {
822 activityList = new ArrayList<ItemCollection>();
823
824 if (workitemItemCollection == null)
825 return activityList;
826
827 int processId = workitemItemCollection
828 .getItemValueInteger("$processid");
829 String sversion = workitemItemCollection
830 .getItemValueString("$modelversion");
831
832
833 List<ItemCollection> col = null;
834 if (sversion != null && !"".equals(sversion))
835 col = this.getModelService().getPublicActivitiesByVersion(
836 processId, sversion);
837
838
839 if (col == null || col.size() == 0)
840
841 col = this.getModelService().getPublicActivitiesByVersion(
842 processId, getModelVersion());
843
844 for (ItemCollection aworkitem : col) {
845 activityList.add(aworkitem);
846 }
847 return activityList;
848 }
849
850
851
852
853
854
855
856
857
858 public List<ItemCollection> getStartProcessList() {
859 if (startProcessList != null)
860 return startProcessList;
861
862
863 startProcessList = new ArrayList<ItemCollection>();
864
865
866 List<ItemCollection> col;
867 col = modelService.getAllStartProcessEntitiesByVersion(this
868 .getModelVersion());
869
870 for (ItemCollection aworkitem : col) {
871 startProcessList.add(aworkitem);
872 }
873 return startProcessList;
874 }
875
876
877
878
879
880
881 public boolean isNewWorkitem() {
882 try {
883 return (!workitemItemCollection.hasItem("numlastactivityid"));
884 } catch (Exception e) {
885 return true;
886 }
887 }
888
889
890
891
892
893
894
895
896
897
898
899
900 public void addMessage(String ressourceBundleName, String messageKey,
901 Object param) {
902 FacesContext context = FacesContext.getCurrentInstance();
903 Locale locale = context.getViewRoot().getLocale();
904
905 ResourceBundle rb = ResourceBundle.getBundle(ressourceBundleName,
906 locale);
907 String msgPattern = rb.getString(messageKey);
908 String msg = msgPattern;
909 if (param != null) {
910 Object[] params = { param };
911 msg = MessageFormat.format(msgPattern, params);
912 }
913 FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_ERROR,
914 msg, msg);
915 context.addMessage(null, facesMsg);
916 }
917
918 }