View Javadoc
1   /**
2    * Copyright 2005-2014 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.kew.actionitem;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.apache.commons.lang.builder.ToStringBuilder;
20  import org.joda.time.DateTime;
21  import org.kuali.rice.core.api.delegation.DelegationType;
22  import org.kuali.rice.core.api.util.RiceConstants;
23  import org.kuali.rice.kew.api.KewApiConstants;
24  import org.kuali.rice.kew.api.action.ActionItemContract;
25  import org.kuali.rice.kew.api.action.RecipientType;
26  import org.kuali.rice.kew.api.actionlist.DisplayParameters;
27  import org.kuali.rice.kew.api.preferences.Preferences;
28  import org.kuali.rice.kew.api.util.CodeTranslator;
29  import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue;
30  import org.kuali.rice.kew.service.KEWServiceLocator;
31  import org.kuali.rice.kim.api.group.Group;
32  import org.kuali.rice.kim.api.identity.principal.EntityNamePrincipalName;
33  import org.kuali.rice.kim.api.identity.principal.Principal;
34  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
35  import org.kuali.rice.krad.data.jpa.PortableSequenceGenerator;
36  
37  import javax.persistence.CascadeType;
38  import javax.persistence.Column;
39  import javax.persistence.FetchType;
40  import javax.persistence.GeneratedValue;
41  import javax.persistence.Id;
42  import javax.persistence.JoinColumn;
43  import javax.persistence.ManyToOne;
44  import javax.persistence.MappedSuperclass;
45  import javax.persistence.Transient;
46  import java.io.Serializable;
47  import java.sql.Timestamp;
48  import java.util.HashMap;
49  import java.util.Map;
50  
51  /**
52   * This is the model for action items. These are displayed as the action list as well.  Mapped to ActionItemService.
53   * NOTE: This object contains denormalized fields that have been copied from related ActionRequestValue and DocumentRouteHeaderValue
54   * objects for performance reasons.  These should be preserved and their related objects should not be added to the OJB
55   * mapping as we do not want them loaded for each ActionItem object.
56   *
57   * @author Kuali Rice Team (rice.collab@kuali.org)
58   *
59   */
60  @MappedSuperclass
61  public class ActionItemBase implements ActionItemContract, Serializable {
62  
63      private static final long serialVersionUID = -1079562205125660151L;
64  
65      @Id
66      @GeneratedValue(generator = "KREW_ACTN_ITM_S")
67      @PortableSequenceGenerator(name = "KREW_ACTN_ITM_S")
68      @Column(name = "ACTN_ITM_ID")
69      private String id;
70  
71      @Column(name = "PRNCPL_ID")
72      private String principalId;
73      
74      @Column(name = "ASND_DT")
75      private Timestamp dateAssigned;
76  
77      @Column(name = "RQST_CD")
78      private String actionRequestCd;
79  
80      @Column(name = "ACTN_RQST_ID")
81      private String actionRequestId;
82  
83      @Column(name = "DOC_HDR_ID")
84      private String documentId;
85  
86      @Column(name = "GRP_ID")
87      private String groupId;
88  
89      @Column(name = "DOC_HDR_TTL")
90      private String docTitle;
91  
92      @Column(name = "DOC_TYP_LBL")
93      private String docLabel;
94  
95      @Column(name = "DOC_HDLR_URL")
96      private String docHandlerURL;
97  
98      @Column(name = "DOC_TYP_NM")
99      private String docName;
100 
101     @Column(name = "RSP_ID")
102     private String responsibilityId = "1";
103 
104     @Column(name = "ROLE_NM")
105     private String roleName;
106 
107     @Column(name = "DLGN_PRNCPL_ID")
108     private String delegatorPrincipalId;
109 
110     @Column(name = "DLGN_GRP_ID")
111     private String delegatorGroupId;
112 
113     @Column(name = "DLGN_TYP")
114     private String delegationType;
115 
116     @Column(name = "RQST_LBL")
117     private String requestLabel;
118 
119     @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.DETACH)
120     @JoinColumn(name = "DOC_HDR_ID", insertable = false, updatable = false)
121     private DocumentRouteHeaderValue routeHeader;
122 
123     // used by Document Operations screen
124     @Transient
125     private String dateAssignedStringValue;
126 
127     @Transient
128     private Timestamp lastApprovedDate;
129 
130     @Transient
131     private Map<String, String> customActions = new HashMap<String, String>();
132 
133     @Transient
134     private String rowStyleClass;
135 
136     @Transient
137     private Integer actionListIndex;
138 
139     @Transient
140     private String delegatorName = "";
141 
142     @Transient
143     private String groupName = "";
144 
145     @Transient
146     private DisplayParameters displayParameters;
147 
148     @Transient
149     private boolean isInitialized = false;
150 
151     @Transient
152     private DocumentRouteHeaderValue minimalRouteHeader;
153 
154     @Transient
155     private boolean lastApprovedDateInitialized = false;
156 
157     @Transient
158     private boolean delegatorNameInitialized = false;
159 
160     @Transient
161     private boolean groupNameInitialized = false;
162 
163     @Deprecated
164     @Override
165     public String getActionToTake() {
166         // deprecated, always return null (see the contract javadoc for more details)
167         return null;
168     }
169 
170     @Deprecated
171     @Override
172     public String getDateAssignedString() {
173         // deprecated, always return null (see the contract javadoc for more details)
174         return null;
175     }
176 
177     public String getDateAssignedStringValue() {
178         if (StringUtils.isBlank(dateAssignedStringValue)) {
179             return RiceConstants.getDefaultDateFormat().format(getDateAssigned());
180         }
181         return dateAssignedStringValue;
182     }
183 
184     public void setDateAssignedStringValue(String dateAssignedStringValue) {
185         this.dateAssignedStringValue = dateAssignedStringValue;
186     }
187 
188     @Override
189     public String getId() {
190         return id;
191     }
192 
193     @Override
194     public String getPrincipalId() {
195         return principalId;
196     }
197 
198     public Timestamp getDateAssigned() {
199         return dateAssigned;
200     }
201 
202     @Override
203     public DateTime getDateTimeAssigned() {
204         return new DateTime(dateAssigned);
205     }
206 
207     @Override
208     public String getActionRequestCd() {
209         return actionRequestCd;
210     }
211 
212     @Override
213     public String getActionRequestId() {
214         return actionRequestId;
215     }
216 
217     @Override
218     public String getDocumentId() {
219         return documentId;
220     }
221 
222     @Override
223     public String getGroupId() {
224         return groupId;
225     }
226 
227     @Override
228     public String getDocTitle() {
229         return docTitle;
230     }
231 
232     @Override
233     public String getDocLabel() {
234         return docLabel;
235     }
236 
237     @Override
238     public String getDocHandlerURL() {
239         return docHandlerURL;
240     }
241 
242     @Override
243     public String getDocName() {
244         return docName;
245     }
246 
247     @Override
248     public String getResponsibilityId() {
249         return responsibilityId;
250     }
251 
252     @Override
253     public String getRoleName() {
254         return roleName;
255     }
256 
257     @Override
258     public String getDelegatorPrincipalId() {
259         return delegatorPrincipalId;
260     }
261 
262     @Override
263     public String getDelegatorGroupId() {
264         return delegatorGroupId;
265     }
266 
267     @Override
268     public DelegationType getDelegationType() {
269         return DelegationType.fromCode(delegationType);
270     }
271 
272     public String getRequestLabel() {
273         return this.requestLabel;
274     }
275 
276     private Group getGroup(String groupId) {
277         if (StringUtils.isBlank(groupId)) {
278             return null;
279         }
280         return KimApiServiceLocator.getGroupService().getGroup(groupId);
281     }
282 
283     public Group getGroup(){
284         return getGroup(groupId);
285     }
286 
287     public String getRecipientTypeCode() {
288         String recipientTypeCode = RecipientType.PRINCIPAL.getCode();
289         if (getRoleName() != null) {
290             recipientTypeCode = RecipientType.ROLE.getCode();
291         }
292         if (getGroupId() != null) {
293             recipientTypeCode = RecipientType.GROUP.getCode();
294         }
295         return recipientTypeCode;
296     }
297 
298     public String getActionRequestLabel() {
299         if (StringUtils.isNotBlank(getRequestLabel())) {
300             return getRequestLabel();
301         }
302         return CodeTranslator.getActionRequestLabel(getActionRequestCd());
303     }
304 
305     public boolean isWorkgroupItem() {
306         return getGroupId() != null;
307     }
308 
309     public Principal getPrincipal(){
310         return KimApiServiceLocator.getIdentityService().getPrincipal(principalId);
311     }
312 
313     public void setResponsibilityId(String responsibilityId) {
314         this.responsibilityId = responsibilityId;
315     }
316 
317     public void setDocName(String docName) {
318         this.docName = docName;
319     }
320 
321     public void setActionRequestCd(String actionRequestCd) {
322         this.actionRequestCd = actionRequestCd;
323     }
324 
325     public void setDateAssigned(Timestamp dateAssigned) {
326         this.dateAssigned = dateAssigned;
327     }
328 
329     public void setPrincipalId(String principalId) {
330         this.principalId = principalId;
331     }
332 
333     public void setDocumentId(String documentId) {
334         this.documentId = documentId;
335     }
336 
337     public void setId(String id) {
338         this.id = id;
339     }
340 
341     public void setActionRequestId(String actionRequestId) {
342         this.actionRequestId = actionRequestId;
343     }
344 
345     public void setDocHandlerURL(String docHandlerURL) {
346         this.docHandlerURL = docHandlerURL;
347     }
348 
349     public void setGroupId(String groupId) {
350         this.groupId = groupId;
351     }
352 
353     public void setDocLabel(String docLabel) {
354         this.docLabel = docLabel;
355     }
356 
357     public void setDocTitle(String docTitle) {
358         this.docTitle = docTitle;
359     }
360 
361     public void setRoleName(String roleName) {
362         this.roleName = roleName;
363     }
364 
365     public void setDelegatorPrincipalId(String delegatorPrincipalId) {
366         this.delegatorPrincipalId = delegatorPrincipalId;
367     }
368 
369     public void setDelegatorGroupId(String delegatorGroupId) {
370         this.delegatorGroupId = delegatorGroupId;
371     }
372 
373     public void setDelegationType(DelegationType delegationType) {
374         this.delegationType = delegationType == null ? null : delegationType.getCode();
375     }
376 
377     public void setRequestLabel(String requestLabel) {
378         this.requestLabel = requestLabel;
379     }
380 
381     @Deprecated
382     @Override
383     public Integer getActionItemIndex() {
384         // deprecated, always return null (see the contract javadoc for more details)
385         return null;
386     }
387 
388     @Override
389     public String toString() {
390         return new ToStringBuilder(this).append("id", id)
391                 .append("principalId", principalId)
392                 .append("dateAssigned", dateAssigned)
393                 .append("actionRequestCd", actionRequestCd)
394                 .append("actionRequestId", actionRequestId)
395                 .append("documentId", documentId)
396                 .append("groupId", groupId)
397                 .append("docTitle", docTitle)
398                 .append("docLabel", docLabel)
399                 .append("docHandlerURL", docHandlerURL)
400                 .append("docName", docName)
401                 .append("responsibilityId", responsibilityId)
402                 .append("roleName", roleName)
403                 .append("delegatorPrincipalId", delegatorPrincipalId)
404                 .append("delegatorGroupId", delegatorGroupId)
405                 .append("delegationType", delegationType)
406                 .toString();
407     }
408 
409     public String getRouteHeaderRouteStatus() {
410         return getMinimalRouteHeader().getDocRouteStatus();
411     }
412 
413     public String getRouteHeaderCombinedStatus() {
414         return getMinimalRouteHeader().getCombinedStatus();
415     }
416 
417     public Timestamp getRouteHeaderCreateDate() {
418         return getMinimalRouteHeader().getCreateDate();
419     }
420 
421     public String getRouteHeaderInitiatorName() {
422         return getMinimalRouteHeader().getInitiatorDisplayName();
423     }
424 
425     public Timestamp getRouteHeaderApprovedDate() {
426         return getMinimalRouteHeader().getApprovedDate();
427     }
428 
429     public String getRouteHeaderCurrentRouteLevelName() {
430         return getMinimalRouteHeader().getCurrentRouteLevelName();
431     }
432 
433     public String getRouteHeaderInitiatorWorkflowId() {
434         return getMinimalRouteHeader().getInitiatorWorkflowId();
435     }
436 
437     public Integer getActionListIndex() {
438         return actionListIndex;
439     }
440 
441     public void setActionListIndex(Integer actionListIndex) {
442         this.actionListIndex = actionListIndex;
443     }
444 
445     public Timestamp getLastApprovedDate() {
446         initializeLastApprovedDate();
447         return this.lastApprovedDate;
448     }
449 
450     public Map<String, String> getCustomActions() {
451         return customActions;
452     }
453 
454     public void setCustomActions(Map<String, String> customActions) {
455         this.customActions = customActions;
456     }
457 
458     public String getRowStyleClass() {
459         return rowStyleClass;
460     }
461 
462     public void setRowStyleClass(String rowStyleClass) {
463         this.rowStyleClass = rowStyleClass;
464     }
465 
466     public String getDelegatorName() {
467         initializeDelegatorName();
468         return delegatorName;
469     }
470 
471     public String getGroupName() {
472         initializeGroupName();
473         return groupName;
474     }
475 
476     public void initialize(Preferences preferences) {
477         // always re-initialize row style class, just in case they changed a preference!
478         initializeRowStyleClass(preferences);
479         if (isInitialized) {
480             return;
481         }
482         if (KewApiConstants.PREFERENCES_YES_VAL.equals(preferences.getShowWorkgroupRequest())) {
483             initializeGroupName();
484         }
485         if (KewApiConstants.PREFERENCES_YES_VAL.equals(preferences.getShowDelegator())) {
486             initializeDelegatorName();
487         }
488         if (KewApiConstants.PREFERENCES_YES_VAL.equals(preferences.getShowDateApproved())) {
489             initializeLastApprovedDate();
490         }
491         isInitialized = true;
492     }
493 
494     private void initializeRowStyleClass(Preferences preferences) {
495         //set background colors for document statuses
496         String docRouteStatus = getRouteHeaderRouteStatus();
497         if (KewApiConstants.ROUTE_HEADER_CANCEL_CD.equalsIgnoreCase(docRouteStatus)) {
498             setRowStyleClass(KewApiConstants.ACTION_LIST_COLOR_PALETTE.get(preferences.getColorCanceled()));
499         } else if (KewApiConstants.ROUTE_HEADER_DISAPPROVED_CD.equalsIgnoreCase(docRouteStatus)) {
500             setRowStyleClass(KewApiConstants.ACTION_LIST_COLOR_PALETTE.get(preferences.getColorDisapproved()));
501         } else if (KewApiConstants.ROUTE_HEADER_ENROUTE_CD.equalsIgnoreCase(docRouteStatus)) {
502             setRowStyleClass(KewApiConstants.ACTION_LIST_COLOR_PALETTE.get(preferences.getColorEnroute()));
503         } else if (KewApiConstants.ROUTE_HEADER_EXCEPTION_CD.equalsIgnoreCase(docRouteStatus)) {
504             setRowStyleClass(KewApiConstants.ACTION_LIST_COLOR_PALETTE.get(preferences.getColorException()));
505         } else if (KewApiConstants.ROUTE_HEADER_FINAL_CD.equalsIgnoreCase(docRouteStatus)) {
506             setRowStyleClass(KewApiConstants.ACTION_LIST_COLOR_PALETTE.get(preferences.getColorFinal()));
507         } else if (KewApiConstants.ROUTE_HEADER_INITIATED_CD.equalsIgnoreCase(docRouteStatus)) {
508             setRowStyleClass(KewApiConstants.ACTION_LIST_COLOR_PALETTE.get(preferences.getColorInitiated()));
509         } else if (KewApiConstants.ROUTE_HEADER_PROCESSED_CD.equalsIgnoreCase(docRouteStatus)) {
510             setRowStyleClass(KewApiConstants.ACTION_LIST_COLOR_PALETTE.get(preferences.getColorProcessed()));
511         } else if (KewApiConstants.ROUTE_HEADER_SAVED_CD.equalsIgnoreCase(docRouteStatus)) {
512             setRowStyleClass(KewApiConstants.ACTION_LIST_COLOR_PALETTE.get(preferences.getColorSaved()));
513         }
514     }
515 
516     private void initializeGroupName() {
517         if (!groupNameInitialized) {
518             if (getGroupId() != null) {
519                 Group group = this.getGroup();
520                 groupName = group.getName();
521             }
522             groupNameInitialized = true;
523         }
524     }
525 
526     private void initializeDelegatorName() {
527         if (!delegatorNameInitialized) {
528             if (getDelegatorPrincipalId() != null) {
529                 EntityNamePrincipalName name = KimApiServiceLocator.getIdentityService().getDefaultNamesForPrincipalId(getDelegatorPrincipalId());
530                 if (name != null) {
531                     delegatorName = name.getDefaultName().getCompositeName();
532                 }
533             }
534             if (getDelegatorGroupId() != null) {
535                 Group delegatorGroup = KimApiServiceLocator.getGroupService().getGroup(getDelegatorGroupId());
536                 if (delegatorGroup !=null)
537                     delegatorName = delegatorGroup.getName();
538             }
539             delegatorNameInitialized = true;
540         }
541     }
542 
543     private void initializeLastApprovedDate() {
544         if (!lastApprovedDateInitialized) {
545             lastApprovedDate = KEWServiceLocator.getActionTakenService().getLastApprovedDate(getDocumentId());
546             lastApprovedDateInitialized = true;
547         }
548     }
549 
550     public DisplayParameters getDisplayParameters() {
551         return displayParameters;
552     }
553 
554     public void setDisplayParameters(DisplayParameters displayParameters) {
555         this.displayParameters = displayParameters;
556     }
557 
558     public DocumentRouteHeaderValue getRouteHeader() {
559         return routeHeader;
560     }
561 
562     public void setRouteHeader(DocumentRouteHeaderValue routeHeader) {
563         this.routeHeader = routeHeader;
564     }
565 
566     public DocumentRouteHeaderValue getMinimalRouteHeader() {
567         if ( minimalRouteHeader == null ) {
568             minimalRouteHeader = KEWServiceLocator.getActionListService().getMinimalRouteHeader(documentId);
569         }
570         return minimalRouteHeader;
571     }
572 
573     protected <T extends ActionItemBase> T deepCopy(Map<Object, Object> visited, Class<T> type) {
574         if (visited.containsKey(this)) {
575             return (T)visited.get(this);
576         }
577         T copy = null;
578         try {
579             copy = type.newInstance();
580         } catch (Exception e) {
581             throw new IllegalArgumentException(e);
582         }
583         visited.put(this, copy);
584         copy.setId(id);
585         copy.setPrincipalId(principalId);
586         if (dateAssigned != null) {
587             copy.setDateAssigned(new Timestamp(dateAssigned.getTime()));
588         }
589         copy.setActionRequestCd(actionRequestCd);
590         copy.setActionRequestId(actionRequestId);
591         copy.setDocumentId(documentId);
592         copy.setGroupId(groupId);
593         copy.setDocTitle(docTitle);
594         copy.setDocLabel(docLabel);
595         copy.setDocHandlerURL(docHandlerURL);
596         copy.setDocName(docName);
597         copy.setResponsibilityId(responsibilityId);
598         copy.setRoleName(roleName);
599         copy.setDelegatorPrincipalId(delegatorPrincipalId);
600         copy.setDelegatorGroupId(delegatorGroupId);
601         copy.setDelegationType(DelegationType.fromCode(delegationType));
602         copy.setRequestLabel(requestLabel);
603         copy.setDateAssignedStringValue(dateAssignedStringValue);
604         if (routeHeader != null) {
605             copy.setRouteHeader(routeHeader.deepCopy(visited));
606         }
607         return copy;
608     }
609 
610 
611 }