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.actiontaken;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.joda.time.DateTime;
20  import org.kuali.rice.core.api.util.RiceConstants;
21  import org.kuali.rice.kew.actionrequest.ActionRequestValue;
22  import org.kuali.rice.kew.actionrequest.KimGroupRecipient;
23  import org.kuali.rice.kew.actionrequest.KimPrincipalRecipient;
24  import org.kuali.rice.kew.actionrequest.Recipient;
25  import org.kuali.rice.kew.actiontaken.dao.impl.ActionTakenDaoJpa;
26  import org.kuali.rice.kew.api.KewApiConstants;
27  import org.kuali.rice.kew.api.action.ActionTaken;
28  import org.kuali.rice.kew.api.action.ActionType;
29  import org.kuali.rice.kew.api.util.CodeTranslator;
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.Principal;
33  import org.kuali.rice.kim.api.role.Role;
34  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
35  import org.kuali.rice.krad.data.jpa.converters.Boolean01Converter;
36  import org.kuali.rice.krad.data.jpa.PortableSequenceGenerator;
37  
38  import javax.persistence.Column;
39  import javax.persistence.Convert;
40  import javax.persistence.Entity;
41  import javax.persistence.GeneratedValue;
42  import javax.persistence.Id;
43  import javax.persistence.NamedQueries;
44  import javax.persistence.NamedQuery;
45  import javax.persistence.OneToMany;
46  import javax.persistence.Table;
47  import javax.persistence.Transient;
48  import javax.persistence.Version;
49  import java.io.Serializable;
50  import java.sql.Timestamp;
51  import java.util.ArrayList;
52  import java.util.Collection;
53  import java.util.List;
54  import java.util.Map;
55  
56  /**
57   * Model object mapped to ojb for representing actions taken on documents by users. The type of the action is indicated
58   * by the {@link #actionTaken} code which will be a valid code value from {@link ActionType}.
59   *
60   * @author Kuali Rice Team (rice.collab@kuali.org)
61   */
62  @Entity
63  @Table(name = "KREW_ACTN_TKN_T")
64  @NamedQueries({
65          @NamedQuery(name = ActionTakenDaoJpa.GET_LAST_ACTION_TAKEN_DATE_NAME,
66                  query = ActionTakenDaoJpa.GET_LAST_ACTION_TAKEN_DATE_QUERY)
67  })
68  public class ActionTakenValue implements Serializable {
69  
70  	private static final long serialVersionUID = -81505450567067594L;
71  
72  	@Id
73  	@GeneratedValue(generator = "KREW_ACTN_TKN_S")
74      @PortableSequenceGenerator(name = "KREW_ACTN_TKN_S")
75  	@Column(name="ACTN_TKN_ID", nullable = false)
76      private String actionTakenId;
77  
78      @Column(name="DOC_HDR_ID", nullable = false)
79  	private String documentId;
80  
81      @Column(name="ACTN_CD", nullable = false)
82  	private String actionTaken;
83  
84  	@Column(name="ACTN_DT", nullable = false)
85  	private Timestamp actionDate;
86  
87      @Column(name="ANNOTN")
88      private String annotation = "";
89  
90      @Column(name="DOC_VER_NBR", nullable = false)
91  	private Integer docVersion;
92  
93      @Column(name="PRNCPL_ID", nullable = false)
94  	private String principalId;
95  
96      @Column(name="DLGTR_PRNCPL_ID")
97  	private String delegatorPrincipalId;
98  
99      @Column(name="DLGTR_GRP_ID")
100 	private String delegatorGroupId;
101 
102     @Column(name="CUR_IND")
103     @Convert(converter = Boolean01Converter.class)
104     private Boolean currentIndicator = Boolean.TRUE;
105 
106     @Version
107     @Column(name="VER_NBR")
108     private Integer lockVerNbr;
109 
110     @OneToMany(mappedBy = "actionTaken")
111 	private Collection<ActionRequestValue> actionRequests;
112 
113     @Transient private String actionDateString;
114 
115     public Principal getPrincipal() {
116     	return getPrincipalForId( principalId );
117     }
118 
119     public String getPrincipalDisplayName() {
120     	return KEWServiceLocator.getIdentityHelperService().getPerson(getPrincipalId()).getName();
121     }
122 
123     public Principal getDelegatorPrincipal() {
124     	return getPrincipalForId(delegatorPrincipalId);
125     }
126 
127     public Group getDelegatorGroup()
128     {
129 	    return KimApiServiceLocator.getGroupService()
130 	            .getGroup(String.valueOf(delegatorGroupId));
131     }
132 
133     public void setDelegator(Recipient recipient) {
134         if (recipient instanceof KimPrincipalRecipient) {
135             setDelegatorPrincipalId(((KimPrincipalRecipient)recipient).getPrincipalId());
136         } else if (recipient instanceof KimGroupRecipient) {
137             setDelegatorGroupId(((KimGroupRecipient)recipient).getGroup().getId());
138         } else {
139             setDelegatorPrincipalId(null);
140             setDelegatorGroupId(null);
141         }
142     }
143 
144     public boolean isForDelegator() {
145         return getDelegatorPrincipalId() != null || getDelegatorGroupId() != null || getDelegatorRoleId() != null;
146     }
147 
148     public String getDelegatorDisplayName() {
149         if (getDelegatorPrincipalId() != null) {
150         	return KEWServiceLocator.getIdentityHelperService().getPerson(this.getDelegatorPrincipalId()).getName();
151         } else if (getDelegatorGroupId() != null) {
152             return getDelegatorGroup().getName();
153         } else {
154             String delegatorRoleId = getDelegatorRoleId();
155             if (delegatorRoleId != null) {
156                 Role role = KimApiServiceLocator.getRoleService().getRole(delegatorRoleId);
157                 if(role != null) {
158                     return role.getName();
159                 } else {
160                     return "";
161                 }
162             } else {
163                 return "";
164             }
165         }
166     }
167 
168     private Principal getPrincipalForId(String principalId) {
169     	Principal principal = null;
170     	
171     	if (!StringUtils.isBlank(principalId)) {
172     		principal = KEWServiceLocator.getIdentityHelperService().getPrincipal(principalId);
173     	}
174     	
175     	return principal;
176     }
177 
178     public String getActionTakenLabel() {
179         return CodeTranslator.getActionTakenLabel(actionTaken);
180     }
181 
182     public Collection<ActionRequestValue> getActionRequests() {
183         if (actionRequests == null) {
184             setActionRequests(new ArrayList<ActionRequestValue>());
185         }
186         return actionRequests;
187     }
188 
189     public void setActionRequests(Collection<ActionRequestValue> actionRequests) {
190         this.actionRequests = actionRequests;
191     }
192 
193     public Timestamp getActionDate() {
194         return actionDate;
195     }
196 
197     public void setActionDate(Timestamp actionDate) {
198         this.actionDate = actionDate;
199     }
200 
201     public String getActionTaken() {
202         return actionTaken;
203     }
204 
205     public void setActionTaken(String actionTaken) {
206         this.actionTaken = actionTaken;
207     }
208 
209     public String getActionTakenId() {
210         return actionTakenId;
211     }
212 
213     public void setActionTakenId(String actionTakenId) {
214         this.actionTakenId = actionTakenId;
215     }
216 
217     public String getAnnotation() {
218         return annotation;
219     }
220 
221     public void setAnnotation(String annotation) {
222         this.annotation = annotation;
223     }
224 
225     public String getDelegatorPrincipalId() {
226         return delegatorPrincipalId;
227     }
228 
229     public void setDelegatorPrincipalId(String delegatorPrincipalId) {
230         this.delegatorPrincipalId = delegatorPrincipalId;
231     }
232 
233     public String getDelegatorGroupId() {
234         return delegatorGroupId;
235     }
236 
237     public void setDelegatorGroupId(String delegatorGroupId) {
238         this.delegatorGroupId = delegatorGroupId;
239     }
240 
241     public String getDelegatorRoleId() {
242         // this could (perhaps) happen when running a simulation
243         if (actionTakenId == null) {
244             return null;
245         }
246         ActionRequestValue actionRequest = KEWServiceLocator.getActionRequestService().getActionRequestForRole(actionTakenId);
247         if (actionRequest != null) {
248             return actionRequest.getRoleName();
249         } else {
250             return null;
251         }
252     }
253 
254     public Integer getDocVersion() {
255         return docVersion;
256     }
257 
258     public void setDocVersion(Integer docVersion) {
259         this.docVersion = docVersion;
260     }
261 
262     public Integer getLockVerNbr() {
263         return lockVerNbr;
264     }
265 
266     public void setLockVerNbr(Integer lockVerNbr) {
267         this.lockVerNbr = lockVerNbr;
268     }
269 
270     public String getDocumentId() {
271     	return documentId;
272     }
273 
274     public void setDocumentId(String documentId) {
275         this.documentId = documentId;
276     }
277 
278     public String getPrincipalId() {
279     	return principalId;
280     }
281     
282     public void setPrincipalId(String principalId) {
283     	this.principalId = principalId;
284     }
285 
286     public Boolean getCurrentIndicator() {
287         return currentIndicator;
288     }
289 
290     public void setCurrentIndicator(Boolean currentIndicator) {
291         this.currentIndicator = currentIndicator;
292     }
293 
294     public String getActionDateString() {
295         if(StringUtils.isBlank(actionDateString)) {
296             return RiceConstants.getDefaultDateFormat().format(getActionDate());
297         } else {
298             return actionDateString;
299         }
300     }
301     public void setActionDateString(String actionDateString) {
302         this.actionDateString = actionDateString;
303     }
304 
305     public boolean isApproval() {
306     	return KewApiConstants.ACTION_TAKEN_APPROVED_CD.equals(getActionTaken());
307     }
308 
309     public boolean isCompletion() {
310     	return KewApiConstants.ACTION_TAKEN_COMPLETED_CD.equals(getActionTaken());
311     }
312 
313     public ActionTakenValue deepCopy(Map<Object, Object> visited) {
314         if (visited.containsKey(this)) {
315             return (ActionTakenValue)visited.get(this);
316         }
317         ActionTakenValue copy = new ActionTakenValue();
318         visited.put(this, copy);
319         copy.actionTakenId = actionTakenId;
320         copy.documentId = documentId;
321         copy.actionTaken = actionTaken;
322         if (actionDate != null) {
323             copy.actionDate = new Timestamp(actionDate.getTime());
324         }
325         copy.annotation = annotation;
326         copy.docVersion = docVersion;
327         copy.principalId = principalId;
328         copy.delegatorPrincipalId = delegatorPrincipalId;
329         copy.delegatorGroupId = delegatorGroupId;
330         copy.currentIndicator = currentIndicator;
331         copy.lockVerNbr = lockVerNbr;
332         if (actionRequests != null) {
333             List<ActionRequestValue> copies = new ArrayList<ActionRequestValue>();
334             for (ActionRequestValue actionRequest : actionRequests) {
335                 copies.add(actionRequest.deepCopy(visited));
336             }
337             copy.actionRequests = copies;
338         }
339         copy.actionDateString = actionDateString;
340         return copy;
341     }
342     
343     public static ActionTaken to(ActionTakenValue actionTakenBo) {
344     	if (actionTakenBo == null) {
345     		return null;
346     	}
347     	ActionTaken.Builder builder = ActionTaken.Builder.create(actionTakenBo.getActionTakenId(),
348     			actionTakenBo.getDocumentId(),
349     			actionTakenBo.getPrincipalId(),
350     			ActionType.fromCode(actionTakenBo.getActionTaken()));
351         if (actionTakenBo.getActionDate() != null) {
352     	    builder.setActionDate(new DateTime(actionTakenBo.getActionDate().getTime()));
353         }
354     	builder.setAnnotation(actionTakenBo.getAnnotation());
355     	builder.setCurrent(actionTakenBo.getCurrentIndicator().booleanValue());
356     	builder.setDelegatorGroupId(actionTakenBo.getDelegatorGroupId());
357     	builder.setDelegatorPrincipalId(actionTakenBo.getDelegatorPrincipalId());
358     	return builder.build();
359     }
360 
361     public boolean isSuperUserAction() {
362         if ( KewApiConstants.ACTION_TAKEN_SU_ACTION_REQUEST_ACKNOWLEDGED_CD.equals(actionTaken) ||
363                 KewApiConstants.ACTION_TAKEN_SU_ACTION_REQUEST_FYI_CD.equals(actionTaken) ||
364                 KewApiConstants.ACTION_TAKEN_SU_ACTION_REQUEST_COMPLETED_CD.equals(actionTaken) ||
365                 KewApiConstants.ACTION_TAKEN_SU_ACTION_REQUEST_APPROVED_CD.equals(actionTaken) ||
366                 KewApiConstants.ACTION_TAKEN_SU_ROUTE_LEVEL_APPROVED_CD.equals(actionTaken) ||
367                 KewApiConstants.ACTION_TAKEN_SU_RETURNED_TO_PREVIOUS_CD.equals(actionTaken) ||
368                 KewApiConstants.ACTION_TAKEN_SU_DISAPPROVED_CD.equals(actionTaken) ||
369                 KewApiConstants.ACTION_TAKEN_SU_CANCELED_CD.equals(actionTaken) ||
370                 KewApiConstants.ACTION_TAKEN_SU_APPROVED_CD.equals(actionTaken)
371                 ) {
372             return true;
373         } else {
374             return false;
375         }
376     }
377 }