1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.kns.web.struts.form;
17
18 import java.io.Serializable;
19 import java.util.ArrayList;
20 import java.util.HashMap;
21 import java.util.List;
22 import java.util.Map;
23 import java.util.Properties;
24
25 import javax.servlet.http.HttpServletRequest;
26
27 import org.apache.commons.lang.StringUtils;
28 import org.apache.struts.action.ActionErrors;
29 import org.apache.struts.action.ActionMapping;
30 import org.apache.struts.upload.FormFile;
31 import org.kuali.rice.core.api.CoreApiServiceLocator;
32 import org.kuali.rice.core.api.util.RiceKeyConstants;
33 import org.kuali.rice.core.web.format.NoOpStringFormatter;
34 import org.kuali.rice.core.web.format.TimestampAMPMFormatter;
35 import org.kuali.rice.coreservice.framework.CoreFrameworkServiceLocator;
36 import org.kuali.rice.kew.api.KewApiServiceLocator;
37 import org.kuali.rice.kew.api.WorkflowDocument;
38 import org.kuali.rice.kew.api.WorkflowDocumentFactory;
39 import org.kuali.rice.kew.api.action.ActionRequest;
40 import org.kuali.rice.kew.api.action.ActionRequestType;
41 import org.kuali.rice.kew.api.doctype.DocumentType;
42 import org.kuali.rice.kew.api.document.DocumentStatus;
43 import org.kuali.rice.kew.api.document.node.RouteNodeInstance;
44 import org.kuali.rice.kew.api.exception.WorkflowException;
45 import org.kuali.rice.kim.api.KimConstants;
46 import org.kuali.rice.kim.api.identity.Person;
47 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
48 import org.kuali.rice.kns.datadictionary.HeaderNavigation;
49 import org.kuali.rice.kns.datadictionary.KNSDocumentEntry;
50 import org.kuali.rice.kns.service.KNSServiceLocator;
51 import org.kuali.rice.kns.service.SessionDocumentService;
52 import org.kuali.rice.kns.util.WebUtils;
53 import org.kuali.rice.kns.web.derivedvaluesetter.DerivedValuesSetter;
54 import org.kuali.rice.kns.web.ui.HeaderField;
55 import org.kuali.rice.krad.bo.AdHocRoutePerson;
56 import org.kuali.rice.krad.bo.AdHocRouteWorkgroup;
57 import org.kuali.rice.krad.bo.Note;
58 import org.kuali.rice.krad.datadictionary.DataDictionary;
59 import org.kuali.rice.krad.document.Document;
60 import org.kuali.rice.krad.service.KRADServiceLocator;
61 import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
62 import org.kuali.rice.krad.service.ModuleService;
63 import org.kuali.rice.krad.util.GlobalVariables;
64 import org.kuali.rice.krad.util.KRADConstants;
65 import org.kuali.rice.krad.util.MessageMap;
66 import org.kuali.rice.krad.util.ObjectUtils;
67 import org.kuali.rice.krad.util.UrlFactory;
68 import org.springframework.util.AutoPopulatingList;
69
70
71
72
73
74 public abstract class KualiDocumentFormBase extends KualiForm implements Serializable {
75 private static final long serialVersionUID = 916061016201941821L;
76
77 private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(KualiDocumentFormBase.class);
78
79 private Document document;
80 private String annotation = "";
81 private String command;
82
83 private String docId;
84 private String docTypeName;
85
86 private List<String> additionalScriptFiles;
87
88 private AdHocRoutePerson newAdHocRoutePerson;
89 private AdHocRouteWorkgroup newAdHocRouteWorkgroup;
90
91 private Note newNote;
92
93
94 private List boNotes;
95
96 protected FormFile attachmentFile = new BlankFormFile();
97
98 protected Map editingMode;
99 protected Map documentActions;
100 protected boolean suppressAllButtons;
101
102 protected Map adHocActionRequestCodes;
103 private boolean returnToActionList;
104
105
106 private String formKey;
107 private String docNum;
108
109 private List<ActionRequest> actionRequests;
110 private List<String> selectedActionRequests;
111 private String superUserAnnotation;
112
113
114
115
116
117 private MessageMap errorMapFromPreviousRequest;
118
119
120
121
122 @Override
123 public void addRequiredNonEditableProperties(){
124 super.addRequiredNonEditableProperties();
125 registerRequiredNonEditableProperty(KRADConstants.DOCUMENT_TYPE_NAME);
126 registerRequiredNonEditableProperty(KRADConstants.FORM_KEY);
127 registerRequiredNonEditableProperty(KRADConstants.NEW_NOTE_NOTE_TYPE_CODE);
128 }
129
130
131
132
133 public String getDocNum() {
134 return this.docNum;
135 }
136
137
138
139
140
141 public void setDocNum(String docNum) {
142 this.docNum = docNum;
143 }
144
145
146
147
148 @SuppressWarnings("unchecked")
149 public KualiDocumentFormBase() {
150 super();
151
152 instantiateDocument();
153 newNote = new Note();
154 this.editingMode = new HashMap();
155
156 this.additionalScriptFiles = new AutoPopulatingList<String>(String.class);
157
158
159 newAdHocRoutePerson = new AdHocRoutePerson();
160
161
162 newAdHocRouteWorkgroup = new AdHocRouteWorkgroup();
163
164
165 setFormatterType("document.documentHeader.note.finDocNotePostedDttmStamp", TimestampAMPMFormatter.class);
166 setFormatterType("document.documentHeader.note.attachment.finDocNotePostedDttmStamp", TimestampAMPMFormatter.class);
167
168
169 setFormatterType("document.documentHeader.boNote.notePostedTimestamp",TimestampAMPMFormatter.class);
170 setFormatterType("document.documentBusinessObject.boNote.notePostedTimestamp",TimestampAMPMFormatter.class);
171
172 setFormatterType("editingMode", NoOpStringFormatter.class);
173 setFormatterType("editableAccounts", NoOpStringFormatter.class);
174
175 setDocumentActions(new HashMap());
176 suppressAllButtons = false;
177
178 initializeHeaderNavigationTabs();
179 }
180
181
182
183
184 @Override
185 public void populate(HttpServletRequest request) {
186 super.populate(request);
187
188 WorkflowDocument workflowDocument = null;
189
190 if (hasDocumentId()) {
191
192
193 try {
194 SessionDocumentService sessionDocumentService = KNSServiceLocator.getSessionDocumentService();
195 workflowDocument = sessionDocumentService.getDocumentFromSession( GlobalVariables.getUserSession(), getDocument().getDocumentNumber());
196 if ( workflowDocument == null)
197 {
198
199
200 Person person = GlobalVariables.getUserSession().getPerson();
201 if (ObjectUtils.isNull(person)) {
202 person = KimApiServiceLocator.getPersonService().getPersonByPrincipalName(KRADConstants.SYSTEM_USER);
203 }
204 workflowDocument = KRADServiceLocatorWeb.getWorkflowDocumentService().loadWorkflowDocument(getDocument().getDocumentNumber(), person);
205 sessionDocumentService.addDocumentToUserSession(GlobalVariables.getUserSession(), workflowDocument);
206 if (workflowDocument == null)
207 {
208 throw new WorkflowException("Unable to retrieve workflow document # " + getDocument().getDocumentNumber() + " from workflow document service createWorkflowDocument");
209 }
210 else
211 {
212 LOG.debug("Retrieved workflow Document ID: " + workflowDocument.getDocumentId());
213 }
214 }
215
216 getDocument().getDocumentHeader().setWorkflowDocument(workflowDocument);
217 } catch (WorkflowException e) {
218 LOG.warn("Error while instantiating workflowDoc", e);
219 throw new RuntimeException("error populating documentHeader.workflowDocument", e);
220 }
221 }
222 if (workflowDocument != null) {
223
224 populateHeaderFields(workflowDocument);
225 }
226 }
227
228 protected String getPersonInquiryUrlLink(Person user, String linkBody) {
229 StringBuffer urlBuffer = new StringBuffer();
230
231 if(user != null && StringUtils.isNotEmpty(linkBody) ) {
232 ModuleService moduleService = KRADServiceLocatorWeb.getKualiModuleService().getResponsibleModuleService(Person.class);
233 Map<String, String[]> parameters = new HashMap<String, String[]>();
234 parameters.put(KimConstants.AttributeConstants.PRINCIPAL_ID, new String[] { user.getPrincipalId() });
235 String inquiryUrl = moduleService.getExternalizableBusinessObjectInquiryUrl(Person.class, parameters);
236 if(!StringUtils.equals(KimConstants.EntityTypes.SYSTEM, user.getEntityTypeCode())){
237 urlBuffer.append("<a href='");
238 urlBuffer.append(inquiryUrl);
239 urlBuffer.append("' ");
240 urlBuffer.append("target='_blank'");
241 urlBuffer.append("title='Person Inquiry'>");
242 urlBuffer.append(linkBody);
243 urlBuffer.append("</a>");
244 } else{
245 urlBuffer.append(linkBody);
246 }
247 }
248
249 return urlBuffer.toString();
250 }
251
252 protected String getDocumentHandlerUrl(String documentId) {
253 Properties parameters = new Properties();
254 parameters.put(KRADConstants.PARAMETER_DOC_ID, documentId);
255 parameters.put(KRADConstants.PARAMETER_COMMAND, KRADConstants.METHOD_DISPLAY_DOC_SEARCH_VIEW);
256 return UrlFactory.parameterizeUrl(
257 KRADServiceLocator.getKualiConfigurationService().getPropertyValueAsString(
258 KRADConstants.WORKFLOW_URL_KEY) + "/" + KRADConstants.DOC_HANDLER_ACTION, parameters);
259 }
260
261 protected String buildHtmlLink(String url, String linkBody) {
262 StringBuffer urlBuffer = new StringBuffer();
263
264 if(StringUtils.isNotEmpty(url) && StringUtils.isNotEmpty(linkBody) ) {
265 urlBuffer.append("<a href='").append(url).append("'>").append(linkBody).append("</a>");
266 }
267
268 return urlBuffer.toString();
269 }
270
271
272
273
274
275
276
277 public void populateHeaderFields(WorkflowDocument workflowDocument) {
278 getDocInfo().clear();
279 getDocInfo().addAll(getStandardHeaderFields(workflowDocument));
280 }
281
282
283
284
285
286
287
288
289
290 protected List<HeaderField> getStandardHeaderFields(WorkflowDocument workflowDocument) {
291 List<HeaderField> headerFields = new ArrayList<HeaderField>();
292 setNumColumns(2);
293
294 HeaderField docTemplateNumber = null;
295 if ((ObjectUtils.isNotNull(getDocument())) && (ObjectUtils.isNotNull(getDocument().getDocumentHeader())) && (StringUtils.isNotBlank(getDocument().getDocumentHeader().getDocumentTemplateNumber()))) {
296 String templateDocumentNumber = getDocument().getDocumentHeader().getDocumentTemplateNumber();
297 docTemplateNumber = new HeaderField(KRADConstants.DocumentFormHeaderFieldIds.DOCUMENT_TEMPLATE_NUMBER, "DataDictionary.DocumentHeader.attributes.documentTemplateNumber",
298 templateDocumentNumber, buildHtmlLink(getDocumentHandlerUrl(templateDocumentNumber), templateDocumentNumber));
299 }
300
301 HeaderField docNumber = new HeaderField("DataDictionary.DocumentHeader.attributes.documentNumber", workflowDocument != null? getDocument().getDocumentNumber() : null);
302 docNumber.setId(KRADConstants.DocumentFormHeaderFieldIds.DOCUMENT_NUMBER);
303 HeaderField docStatus = new HeaderField("DataDictionary.AttributeReferenceDummy.attributes.workflowDocumentStatus", workflowDocument != null? workflowDocument.getStatus().getLabel() : null);
304 docStatus.setId(KRADConstants.DocumentFormHeaderFieldIds.DOCUMENT_WORKFLOW_STATUS);
305 String initiatorNetworkId = null;
306 Person user = null;
307 if (workflowDocument != null) {
308 if (getInitiator() == null) {
309 LOG.warn("User Not Found while attempting to build inquiry link for document header fields");
310 } else {
311 user = getInitiator();
312 initiatorNetworkId = getInitiator().getPrincipalName();
313 }
314 }
315 String inquiryUrl = getPersonInquiryUrlLink(user, workflowDocument != null? initiatorNetworkId:null);
316
317 HeaderField docInitiator = new HeaderField(KRADConstants.DocumentFormHeaderFieldIds.DOCUMENT_INITIATOR, "DataDictionary.AttributeReferenceDummy.attributes.initiatorNetworkId",
318 workflowDocument != null? initiatorNetworkId : null, workflowDocument != null? inquiryUrl : null);
319
320 String createDateStr = null;
321 if(workflowDocument != null && workflowDocument.getDateCreated() != null) {
322 createDateStr = CoreApiServiceLocator.getDateTimeService().toString(workflowDocument.getDateCreated().toDate(), "hh:mm a MM/dd/yyyy");
323 }
324
325 HeaderField docCreateDate = new HeaderField("DataDictionary.AttributeReferenceDummy.attributes.createDate", createDateStr);
326 docCreateDate.setId(KRADConstants.DocumentFormHeaderFieldIds.DOCUMENT_CREATE_DATE);
327 if (ObjectUtils.isNotNull(docTemplateNumber)) {
328 setNumColumns(3);
329 }
330
331 headerFields.add(docNumber);
332 headerFields.add(docStatus);
333 if (ObjectUtils.isNotNull(docTemplateNumber)) {
334 headerFields.add(docTemplateNumber);
335 }
336 headerFields.add(docInitiator);
337 headerFields.add(docCreateDate);
338 if (ObjectUtils.isNotNull(docTemplateNumber)) {
339
340 headerFields.add(HeaderField.EMPTY_FIELD);
341 }
342 return headerFields;
343 }
344
345
346
347
348
349 @Override
350 public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
351
352 setAnnotation(StringUtils.stripToNull(getAnnotation()));
353 int diff = StringUtils.defaultString(getAnnotation()).length() - KRADConstants.DOCUMENT_ANNOTATION_MAX_LENGTH;
354 if (diff > 0) {
355 GlobalVariables.getMessageMap().putError("annotation", RiceKeyConstants.ERROR_DOCUMENT_ANNOTATION_MAX_LENGTH_EXCEEDED, new String[] { Integer.toString(KRADConstants.DOCUMENT_ANNOTATION_MAX_LENGTH), Integer.toString(diff) });
356 }
357 return super.validate(mapping, request);
358 }
359
360
361
362
363 final public boolean isFormDocumentInitialized() {
364 boolean initialized = false;
365
366 if (document != null) {
367 if (document.getDocumentHeader() != null) {
368 initialized = document.getDocumentHeader().hasWorkflowDocument();
369 }
370 }
371
372 return initialized;
373 }
374
375
376
377
378
379
380 @SuppressWarnings("unchecked")
381 public Map getEditingMode() {
382 return editingMode;
383 }
384
385
386
387
388 @SuppressWarnings("unchecked")
389 public void setEditingMode(Map editingMode) {
390 this.editingMode = editingMode;
391 }
392
393
394
395
396 @SuppressWarnings("unchecked")
397 public Map getDocumentActions() {
398 return this.documentActions;
399 }
400
401
402
403
404 @SuppressWarnings("unchecked")
405 public void setDocumentActions(Map documentActions) {
406 this.documentActions = documentActions;
407 }
408
409
410
411
412
413
414 @SuppressWarnings("unchecked")
415 public void setAdHocActionRequestCodes(Map adHocActionRequestCodes) {
416 this.adHocActionRequestCodes = adHocActionRequestCodes;
417 }
418
419
420
421
422 @SuppressWarnings("unchecked")
423 public Map getAdHocActionRequestCodes() {
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440 return adHocActionRequestCodes;
441 }
442
443
444
445
446
447 public List<AdHocRoutePerson> getAdHocRoutePersons() {
448 return document.getAdHocRoutePersons();
449 }
450
451
452
453
454
455 public FormFile getAttachmentFile() {
456 return attachmentFile;
457 }
458
459
460
461
462 public void setAttachmentFile(FormFile attachmentFile) {
463 this.attachmentFile = attachmentFile;
464 }
465
466
467
468
469
470
471
472 public void setAdHocRoutePersons(List<AdHocRoutePerson> adHocRouteRecipients) {
473 document.setAdHocRoutePersons(adHocRouteRecipients);
474 }
475
476
477
478
479
480
481 public List<AdHocRouteWorkgroup> getAdHocRouteWorkgroups() {
482 return document.getAdHocRouteWorkgroups();
483 }
484
485
486
487
488
489
490 public void setAdHocRouteWorkgroups(List<AdHocRouteWorkgroup> adHocRouteWorkgroups) {
491 document.setAdHocRouteWorkgroups(adHocRouteWorkgroups);
492 }
493
494
495
496
497
498
499
500 public AdHocRoutePerson getAdHocRoutePerson(int index) {
501 while (getAdHocRoutePersons().size() <= index) {
502 getAdHocRoutePersons().add(new AdHocRoutePerson());
503 }
504 return getAdHocRoutePersons().get(index);
505 }
506
507
508
509
510
511
512
513 public AdHocRouteWorkgroup getAdHocRouteWorkgroup(int index) {
514 while (getAdHocRouteWorkgroups().size() <= index) {
515 getAdHocRouteWorkgroups().add(new AdHocRouteWorkgroup());
516 }
517 return getAdHocRouteWorkgroups().get(index);
518 }
519
520
521
522
523 public AdHocRoutePerson getNewAdHocRoutePerson() {
524 return newAdHocRoutePerson;
525 }
526
527
528
529
530
531
532 public void setNewAdHocRoutePerson(AdHocRoutePerson newAdHocRoutePerson) {
533 this.newAdHocRoutePerson = newAdHocRoutePerson;
534 }
535
536
537
538
539 public AdHocRouteWorkgroup getNewAdHocRouteWorkgroup() {
540 return newAdHocRouteWorkgroup;
541 }
542
543
544
545
546
547
548 public void setNewAdHocRouteWorkgroup(AdHocRouteWorkgroup newAdHocRouteWorkgroup) {
549 this.newAdHocRouteWorkgroup = newAdHocRouteWorkgroup;
550 }
551
552
553
554
555 public Document getDocument() {
556 return document;
557 }
558
559
560
561
562 public void setDocument(Document document) {
563 this.document = document;
564 if(document != null && StringUtils.isNotEmpty(document.getDocumentNumber())) {
565 populateHeaderFields(document.getDocumentHeader().getWorkflowDocument());
566 }
567 }
568
569
570
571
572 public WorkflowDocument getWorkflowDocument() {
573 return getDocument().getDocumentHeader().getWorkflowDocument();
574 }
575
576
577
578
579
580 public boolean isHasWorkflowDocument() {
581 if ( getDocument() == null || getDocument().getDocumentHeader() == null ) {
582 return false;
583 }
584 return getDocument().getDocumentHeader().hasWorkflowDocument();
585 }
586
587
588
589
590 public boolean isUserDocumentInitiator() {
591 if (getWorkflowDocument() != null) {
592 return getWorkflowDocument().getInitiatorPrincipalId().equalsIgnoreCase(
593 GlobalVariables.getUserSession().getPrincipalId());
594 }
595 return false;
596 }
597
598 public Person getInitiator() {
599 String initiatorPrincipalId = getWorkflowDocument().getInitiatorPrincipalId();
600 return KimApiServiceLocator.getPersonService().getPerson(initiatorPrincipalId);
601 }
602
603
604
605
606 public boolean isDocumentEnRoute() {
607 return getWorkflowDocument().isEnroute();
608 }
609
610
611
612
613 public void setAnnotation(String annotation) {
614 this.annotation = annotation;
615 }
616
617
618
619
620 public String getAnnotation() {
621 return annotation;
622 }
623
624
625
626
627 public String getCommand() {
628 return command;
629 }
630
631
632
633
634
635
636 public void setCommand(String command) {
637 this.command = command;
638 }
639
640
641
642
643 public String getDocId() {
644 return docId;
645 }
646
647
648
649
650
651
652 public void setDocId(String docId) {
653 this.docId = docId;
654 }
655
656
657
658
659
660
661 public String getDocTypeName() {
662 return docTypeName;
663 }
664
665
666
667
668
669
670 public void setDocTypeName(String docTypeName) {
671 this.docTypeName = docTypeName;
672 }
673
674
675
676
677
678
679 public String getInitiatorNetworkId() {
680 return this.getWorkflowDocument().getInitiatorPrincipalId();
681 }
682
683
684
685
686
687
688 public final boolean isSuppressAllButtons() {
689 return suppressAllButtons;
690 }
691
692
693
694
695
696
697 public final void setSuppressAllButtons(boolean suppressAllButtons) {
698 this.suppressAllButtons = suppressAllButtons;
699 }
700
701
702
703
704
705 public boolean hasDocumentId() {
706 boolean hasDocId = false;
707
708 Document d = getDocument();
709 if (d != null) {
710 String docHeaderId = d.getDocumentNumber();
711
712 hasDocId = StringUtils.isNotBlank(docHeaderId);
713 }
714
715 return hasDocId;
716 }
717
718
719
720
721
722
723
724 public void setReturnToActionList(boolean returnToActionList) {
725 this.returnToActionList = returnToActionList;
726 }
727
728 public boolean isReturnToActionList() {
729 return returnToActionList;
730 }
731
732 public List<String> getAdditionalScriptFiles() {
733 return additionalScriptFiles;
734 }
735
736 public void setAdditionalScriptFiles(List<String> additionalScriptFiles) {
737 this.additionalScriptFiles = additionalScriptFiles;
738 }
739
740 public void setAdditionalScriptFile( int index, String scriptFile ) {
741 additionalScriptFiles.set( index, scriptFile );
742 }
743
744 public String getAdditionalScriptFile( int index ) {
745 return additionalScriptFiles.get( index );
746 }
747
748 public Note getNewNote() {
749 return newNote;
750 }
751
752 public void setNewNote(Note newNote) {
753 this.newNote = newNote;
754 }
755
756
757
758
759
760 @SuppressWarnings("unchecked")
761 public List getBoNotes() {
762 return boNotes;
763 }
764
765
766
767
768
769 @SuppressWarnings("unchecked")
770 public void setBoNotes(List boNotes) {
771 this.boNotes = boNotes;
772 }
773
774 public String getFormKey() {
775 return this.formKey;
776 }
777
778 public void setFormKey(String formKey) {
779 this.formKey = formKey;
780 }
781
782
783
784
785
786
787 @Override
788 public void reset(ActionMapping mapping, HttpServletRequest request) {
789 super.reset(mapping, request);
790 this.setMethodToCall(null);
791 this.setRefreshCaller(null);
792 this.setAnchor(null);
793 this.setCurrentTabIndex(0);
794 this.setSelectedActionRequests(new ArrayList<String>());
795 }
796
797
798
799
800
801
802
803 @Override
804 protected void customInitMaxUploadSizes() {
805 super.customInitMaxUploadSizes();
806 String attachmentSize = CoreFrameworkServiceLocator.getParameterService().getParameterValueAsString(KRADConstants.KNS_NAMESPACE, KRADConstants.DetailTypes.DOCUMENT_DETAIL_TYPE, KRADConstants.ATTACHMENT_MAX_FILE_SIZE_PARM_NM);
807 if (StringUtils.isNotBlank(attachmentSize)) {
808 addMaxUploadSize(attachmentSize);
809 }
810 }
811
812
813
814
815
816
817
818
819
820 @Override
821 public boolean shouldPropertyBePopulatedInForm(String requestParameterName, HttpServletRequest request) {
822 for ( String prefix : KRADConstants.ALWAYS_VALID_PARAMETER_PREFIXES ) {
823 if (requestParameterName.startsWith(prefix)) {
824 return true;
825 }
826 }
827
828 if (StringUtils.equalsIgnoreCase(getMethodToCall(), KRADConstants.DOC_HANDLER_METHOD)) {
829 return true;
830 }
831 if (WebUtils.isDocumentSession(getDocument(), this)) {
832 return isPropertyEditable(requestParameterName) || isPropertyNonEditableButRequired(requestParameterName);
833 }
834 return true;
835 }
836
837
838
839
840
841
842 @Override
843 public boolean shouldMethodToCallParameterBeUsed(
844 String methodToCallParameterName,
845 String methodToCallParameterValue, HttpServletRequest request) {
846 if (StringUtils.equals(methodToCallParameterName, KRADConstants.DISPATCH_REQUEST_PARAMETER) &&
847 StringUtils.equals(methodToCallParameterValue, KRADConstants.DOC_HANDLER_METHOD)) {
848 return true;
849 }
850 return super.shouldMethodToCallParameterBeUsed(methodToCallParameterName,
851 methodToCallParameterValue, request);
852 }
853
854 public MessageMap getMessageMapFromPreviousRequest() {
855 return this.errorMapFromPreviousRequest;
856 }
857
858 public void setMessageMapFromPreviousRequest(MessageMap errorMapFromPreviousRequest) {
859 this.errorMapFromPreviousRequest = errorMapFromPreviousRequest;
860 }
861
862 @Override
863 public void setDerivedValuesOnForm(HttpServletRequest request) {
864 super.setDerivedValuesOnForm(request);
865
866 String docTypeName = getDocTypeName();
867 if (StringUtils.isNotBlank(docTypeName)) {
868 DataDictionary dataDictionary = KRADServiceLocatorWeb.getDataDictionaryService().getDataDictionary();
869
870 Class<? extends DerivedValuesSetter> derivedValuesSetterClass = null;
871 KNSDocumentEntry documentEntry = (KNSDocumentEntry) dataDictionary.getDocumentEntry(docTypeName);
872 derivedValuesSetterClass = (documentEntry).getDerivedValuesSetterClass();
873
874 if (derivedValuesSetterClass != null) {
875 DerivedValuesSetter derivedValuesSetter = null;
876 try {
877 derivedValuesSetter = derivedValuesSetterClass.newInstance();
878 }
879
880 catch (Exception e) {
881 LOG.error("Unable to instantiate class " + derivedValuesSetterClass.getName(), e);
882 throw new RuntimeException("Unable to instantiate class " + derivedValuesSetterClass.getName(), e);
883 }
884 derivedValuesSetter.setDerivedValues(this, request);
885 }
886 }
887 }
888
889 protected String getDefaultDocumentTypeName() {
890 return "";
891 }
892
893
894 protected void instantiateDocument() {
895 if (document == null && StringUtils.isNotBlank(getDefaultDocumentTypeName())) {
896 Class<? extends Document> documentClass = getDocumentClass();
897 try {
898 Document document = documentClass.newInstance();
899 setDocument(document);
900 } catch (Exception e) {
901 LOG.error("Unable to instantiate document class " + documentClass.getName() + " document type " + getDefaultDocumentTypeName());
902 throw new RuntimeException(e);
903 }
904 }
905 }
906
907
908 private Class<? extends Document> getDocumentClass() {
909 return KRADServiceLocatorWeb.getDataDictionaryService().getDocumentClassByTypeName(getDefaultDocumentTypeName());
910 }
911
912
913 protected void initializeHeaderNavigationTabs() {
914 if (StringUtils.isNotBlank(getDefaultDocumentTypeName())) {
915 final KNSDocumentEntry docEntry = (KNSDocumentEntry) KRADServiceLocatorWeb.getDataDictionaryService().getDataDictionary().getDocumentEntry(getDocumentClass().getName());
916 final List<HeaderNavigation> navList = docEntry.getHeaderNavigationList();
917 final HeaderNavigation[] list = new HeaderNavigation[navList.size()];
918 super.setHeaderNavigationTabs(navList.toArray(list));
919 }
920 }
921
922 public List<ActionRequest> getActionRequests() {
923 return actionRequests;
924 }
925
926 public void setActionRequests(List<ActionRequest> actionRequests) {
927 this.actionRequests = actionRequests;
928 }
929
930 public List<String> getSelectedActionRequests() {
931 return selectedActionRequests;
932 }
933
934 public void setSelectedActionRequests(List<String> selectedActionRequests) {
935 this.selectedActionRequests = selectedActionRequests;
936 }
937
938 public List<ActionRequest> getActionRequestsRequiringApproval() {
939 List<ActionRequest> actionRequests = getActionRequests();
940 List<ActionRequest> actionRequestsApprove = new ArrayList<ActionRequest>();;
941
942 for (ActionRequest actionRequest: actionRequests) {
943 if ((StringUtils.equals(actionRequest.getActionRequested().getCode(), ActionRequestType.APPROVE.getCode())) ||
944 (StringUtils.equals(actionRequest.getActionRequested().getCode(), ActionRequestType.COMPLETE.getCode()))) {
945 actionRequestsApprove.add(actionRequest);
946 }
947 }
948 return actionRequestsApprove;
949 }
950
951 public String getSuperUserAnnotation() {
952 return superUserAnnotation;
953 }
954
955 public void setSuperUserAnnotation(String superUserAnnotation) {
956 this.superUserAnnotation = superUserAnnotation;
957 }
958
959 public boolean isSuperUserActionAvaliable() {
960 List<ActionRequest> actionRequests = getActionRequestsRequiringApproval();
961 boolean hasSingleActionToTake = false;
962 boolean canSuperUserApprove = false;
963 boolean canSuperUserDisapprove = false;
964
965 hasSingleActionToTake = ( isSuperUserApproveSingleActionRequestAuthorized() &&
966 isStateAllowsApproveSingleActionRequest() &&
967 !actionRequests.isEmpty());
968 if (!hasSingleActionToTake) {
969 canSuperUserApprove = (isSuperUserApproveDocumentAuthorized() && isStateAllowsApproveOrDisapprove());
970 }
971 if (!canSuperUserApprove) {
972 canSuperUserDisapprove = (isSuperUserDisapproveDocumentAuthorized() && isStateAllowsApproveOrDisapprove());
973 }
974
975 return (hasSingleActionToTake || canSuperUserApprove || canSuperUserDisapprove) ;
976 }
977
978 public boolean isSuperUserApproveSingleActionRequestAuthorized() {
979 String principalId = GlobalVariables.getUserSession().getPrincipalId();
980 String docId = this.getDocId();
981 DocumentType documentType = KewApiServiceLocator.getDocumentTypeService().getDocumentTypeByName(docTypeName);
982 String docTypeId = null;
983 if (documentType != null) {
984 docTypeId = documentType.getId();
985 }
986 List<RouteNodeInstance> routeNodeInstances= KewApiServiceLocator.getWorkflowDocumentService().getRouteNodeInstances(docId);
987 String documentStatus = KewApiServiceLocator.getWorkflowDocumentService().getDocumentStatus(docId).getCode();
988 return (( KewApiServiceLocator.getDocumentTypeService().canSuperUserApproveSingleActionRequest(
989 principalId, this.getDocTypeName(), routeNodeInstances, documentStatus)) ||
990 (KewApiServiceLocator.getDocumentTypeService().isSuperUserForDocumentTypeId(principalId, docTypeId))) ;
991 }
992
993 public boolean isSuperUserApproveDocumentAuthorized() {
994 String principalId = GlobalVariables.getUserSession().getPrincipalId();
995 String docId = this.getDocId();
996 DocumentType documentType = KewApiServiceLocator.getDocumentTypeService().getDocumentTypeByName(docTypeName);
997 String docTypeId = null;
998 if (documentType != null) {
999 docTypeId = documentType.getId();
1000 }
1001 List<RouteNodeInstance> routeNodeInstances= KewApiServiceLocator.getWorkflowDocumentService().getRouteNodeInstances(docId);
1002 String documentStatus = KewApiServiceLocator.getWorkflowDocumentService().getDocumentStatus(docId).getCode();
1003 return ((KewApiServiceLocator.getDocumentTypeService().canSuperUserApproveDocument(
1004 principalId, this.getDocTypeName(), routeNodeInstances, documentStatus)) ||
1005 (KewApiServiceLocator.getDocumentTypeService().isSuperUserForDocumentTypeId(principalId, docTypeId))) ;
1006 }
1007
1008 public boolean isSuperUserDisapproveDocumentAuthorized() {
1009 String principalId = GlobalVariables.getUserSession().getPrincipalId();
1010 String docId = this.getDocId();
1011 DocumentType documentType = KewApiServiceLocator.getDocumentTypeService().getDocumentTypeByName(docTypeName);
1012 String docTypeId = null;
1013 if (documentType != null) {
1014 docTypeId = documentType.getId();
1015 }
1016 List<RouteNodeInstance> routeNodeInstances= KewApiServiceLocator.getWorkflowDocumentService().getRouteNodeInstances(docId);
1017 String documentStatus = KewApiServiceLocator.getWorkflowDocumentService().getDocumentStatus(docId).getCode();
1018 return ((KewApiServiceLocator.getDocumentTypeService().canSuperUserDisapproveDocument(
1019 principalId, this.getDocTypeName(), routeNodeInstances, documentStatus)) ||
1020 (KewApiServiceLocator.getDocumentTypeService().isSuperUserForDocumentTypeId(principalId, docTypeId))) ;
1021 }
1022
1023 public boolean isSuperUserAuthorized() {
1024 String docId = this.getDocId();
1025 if (StringUtils.isBlank(docId) || ObjectUtils.isNull(docTypeName)) {
1026 return false;
1027 }
1028
1029 DocumentType documentType = KewApiServiceLocator.getDocumentTypeService().getDocumentTypeByName(docTypeName);
1030 String docTypeId = null;
1031 if (documentType != null) {
1032 docTypeId = documentType.getId();
1033 }
1034 String principalId = GlobalVariables.getUserSession().getPrincipalId();
1035 List<RouteNodeInstance> routeNodeInstances= KewApiServiceLocator.getWorkflowDocumentService().getRouteNodeInstances(
1036 docId);
1037 String documentStatus = KewApiServiceLocator.getWorkflowDocumentService().getDocumentStatus(docId).getCode();
1038 return ((KewApiServiceLocator.getDocumentTypeService().canSuperUserApproveSingleActionRequest(
1039 principalId, this.getDocTypeName(), routeNodeInstances, documentStatus)) ||
1040 (KewApiServiceLocator.getDocumentTypeService().canSuperUserApproveDocument(
1041 principalId, this.getDocTypeName(), routeNodeInstances, documentStatus)) ||
1042 (KewApiServiceLocator.getDocumentTypeService().canSuperUserDisapproveDocument (
1043 principalId, this.getDocTypeName(), routeNodeInstances, documentStatus)) ||
1044 (KewApiServiceLocator.getDocumentTypeService().isSuperUserForDocumentTypeId(principalId, docTypeId))) ;
1045 }
1046
1047 public boolean isStateAllowsApproveOrDisapprove() {
1048 if(this.getDocument().getDocumentHeader().hasWorkflowDocument()) {
1049 DocumentStatus status = null;
1050 WorkflowDocument document = WorkflowDocumentFactory.loadDocument(GlobalVariables.getUserSession().getPrincipalId(),
1051 this.getDocument().getDocumentHeader().getWorkflowDocument().getDocumentId());
1052 if (ObjectUtils.isNotNull(document)) {
1053 status = document.getStatus();
1054 } else {
1055 status = this.getDocument().getDocumentHeader().getWorkflowDocument().getStatus();
1056 }
1057 return !(isStateProcessedOrDisapproved(status) ||
1058 isStateInitiatedFinalCancelled(status) ||
1059 StringUtils.equals(status.getCode(), DocumentStatus.SAVED.getCode()));
1060 } else {
1061 return false;
1062 }
1063 }
1064
1065 public boolean isStateAllowsApproveSingleActionRequest() {
1066 if(this.getDocument().getDocumentHeader().hasWorkflowDocument()) {
1067 DocumentStatus status = null;
1068 WorkflowDocument document = WorkflowDocumentFactory.loadDocument(GlobalVariables.getUserSession().getPrincipalId(),
1069 this.getDocument().getDocumentHeader().getWorkflowDocument().getDocumentId());
1070 if (ObjectUtils.isNotNull(document)) {
1071 status = document.getStatus();
1072 } else {
1073 status = this.getDocument().getDocumentHeader().getWorkflowDocument().getStatus();
1074 }
1075 return !(isStateInitiatedFinalCancelled(status));
1076 } else {
1077 return false;
1078 }
1079 }
1080
1081 public boolean isStateProcessedOrDisapproved(DocumentStatus status) {
1082 return (StringUtils.equals(status.getCode(), DocumentStatus.PROCESSED.getCode()) ||
1083 StringUtils.equals(status.getCode(), DocumentStatus.DISAPPROVED.getCode()));
1084 }
1085
1086 public boolean isStateInitiatedFinalCancelled(DocumentStatus status) {
1087 return (StringUtils.equals(status.getCode(), DocumentStatus.INITIATED.getCode()) ||
1088 StringUtils.equals(status.getCode(), DocumentStatus.FINAL.getCode()) ||
1089 StringUtils.equals(status.getCode(), DocumentStatus.CANCELED.getCode()));
1090 }
1091 }