View Javadoc

1   /**
2    * Copyright 2005-2012 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.hibernate.annotations.Fetch;
20  import org.hibernate.annotations.FetchMode;
21  import org.hibernate.annotations.GenericGenerator;
22  import org.hibernate.annotations.Parameter;
23  import org.joda.time.DateTime;
24  import org.kuali.rice.core.api.util.RiceConstants;
25  import org.kuali.rice.core.framework.persistence.jpa.OrmUtils;
26  import org.kuali.rice.kew.actionrequest.ActionRequestValue;
27  import org.kuali.rice.kew.actionrequest.KimGroupRecipient;
28  import org.kuali.rice.kew.actionrequest.KimPrincipalRecipient;
29  import org.kuali.rice.kew.actionrequest.Recipient;
30  import org.kuali.rice.kew.actionrequest.service.ActionRequestService;
31  import org.kuali.rice.kew.api.action.ActionTaken;
32  import org.kuali.rice.kew.api.action.ActionType;
33  import org.kuali.rice.kew.api.util.CodeTranslator;
34  import org.kuali.rice.kew.service.KEWServiceLocator;
35  import org.kuali.rice.kew.api.KewApiConstants;
36  import org.kuali.rice.kim.api.group.Group;
37  import org.kuali.rice.kim.api.identity.principal.Principal;
38  import org.kuali.rice.kim.api.role.Role;
39  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
40  
41  import javax.persistence.Column;
42  import javax.persistence.Entity;
43  import javax.persistence.FetchType;
44  import javax.persistence.GeneratedValue;
45  import javax.persistence.Id;
46  import javax.persistence.OneToMany;
47  import javax.persistence.Table;
48  import javax.persistence.Transient;
49  import javax.persistence.Version;
50  import java.io.Serializable;
51  import java.sql.Timestamp;
52  import java.util.ArrayList;
53  import java.util.Collection;
54  
55  
56  /**
57   * Model object mapped to ojb for representing actions taken on documents by
58   * users.
59   *
60   * @author Kuali Rice Team (rice.collab@kuali.org)
61   */
62  @Entity
63  //@Sequence(name="KREW_ACTN_TKN_S", property="actionTakenId")
64  @Table(name="KREW_ACTN_TKN_T")
65  public class ActionTakenValue implements Serializable {
66  
67      /**
68  	 *
69  	 */
70  	private static final long serialVersionUID = -81505450567067594L;
71  	@Id
72  	@GeneratedValue(generator="KREW_ACTN_TKN_S")
73  	@GenericGenerator(name="KREW_ACTN_TKN_S",strategy="org.hibernate.id.enhanced.SequenceStyleGenerator",parameters={
74  			@Parameter(name="sequence_name",value="KREW_ACTN_TKN_S"),
75  			@Parameter(name="value_column",value="id")
76  	})
77  	@Column(name="ACTN_TKN_ID")
78      private String actionTakenId;
79      @Column(name="DOC_HDR_ID")//,insertable=false, updatable=false)
80  	private String documentId;
81      @Column(name="ACTN_CD")
82  	private String actionTaken;
83  	@Column(name="ACTN_DT")
84  	private Timestamp actionDate;
85      @Column(name="ANNOTN")
86      private String annotation = "";
87      @Column(name="DOC_VER_NBR")
88  	private Integer docVersion;
89      @Version
90  	@Column(name="VER_NBR")
91  	private Integer lockVerNbr;
92      @Column(name="PRNCPL_ID")
93  	private String principalId;
94      @Column(name="DLGTR_PRNCPL_ID")
95  	private String delegatorPrincipalId;
96      @Column(name="DLGTR_GRP_ID")
97  	private String delegatorGroupId;
98      //@ManyToOne(fetch=FetchType.EAGER, cascade={CascadeType.PERSIST})
99      //@JoinColumn(name="DOC_HDR_ID")
100     //private DocumentRouteHeaderValue routeHeader;
101     @OneToMany(fetch=FetchType.EAGER, mappedBy="actionTaken")
102     @Fetch(value = FetchMode.SELECT)
103 	private Collection<ActionRequestValue> actionRequests;
104     @Column(name="CUR_IND")
105     private Boolean currentIndicator = Boolean.TRUE;
106     @Transient
107     private String actionDateString;
108 
109     public Principal getPrincipal() {
110     	return getPrincipalForId( principalId );
111     }
112     
113 	//@PrePersist
114 	public void beforeInsert(){
115 		OrmUtils.populateAutoIncValue(this, KEWServiceLocator.getEntityManagerFactory().createEntityManager());		
116 	}
117 
118     public String getPrincipalDisplayName() {
119     	// TODO this stinks to have to have a dependency on UserSession here
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         	// TODO this stinks to have to have a dependency on UserSession here
151         	return KEWServiceLocator.getIdentityHelperService().getPerson(this.getDelegatorPrincipalId()).getName();
152         } else if (getDelegatorGroupId() != null) {
153             return getDelegatorGroup().getName();
154         } else {
155             String delegatorRoleId = getDelegatorRoleId();
156             if (delegatorRoleId != null) {
157                 Role role = KimApiServiceLocator.getRoleService().getRole(delegatorRoleId);
158                 if(role != null) {
159                     return role.getName();
160                 } else {
161                     return "";
162                 }
163             } else {
164                 return "";
165             }
166         }
167     }
168 
169     private Principal getPrincipalForId(String principalId) {
170     	Principal principal = null;
171     	
172     	if (!StringUtils.isBlank(principalId)) {
173     		principal = KEWServiceLocator.getIdentityHelperService().getPrincipal(principalId);
174     	}
175     	
176     	return principal;
177     }
178 
179     public String getActionTakenLabel() {
180         return CodeTranslator.getActionTakenLabel(actionTaken);
181     }
182 
183     public Collection<ActionRequestValue> getActionRequests() {
184         if (actionRequests == null) {
185             setActionRequests(new ArrayList<ActionRequestValue>());
186         }
187         return actionRequests;
188     }
189 
190     public void setActionRequests(Collection<ActionRequestValue> actionRequests) {
191         this.actionRequests = actionRequests;
192     }
193 
194     //public DocumentRouteHeaderValue getRouteHeader() {
195     //    return routeHeader;
196     //}
197 
198     //public void setRouteHeader(DocumentRouteHeaderValue routeHeader) {
199     //    this.routeHeader = routeHeader;
200     //}
201 
202     public Timestamp getActionDate() {
203         return actionDate;
204     }
205 
206 
207     public void setActionDate(Timestamp actionDate) {
208         this.actionDate = actionDate;
209     }
210 
211 
212     public String getActionTaken() {
213         return actionTaken;
214     }
215 
216 
217     public void setActionTaken(String actionTaken) {
218         this.actionTaken = actionTaken;
219     }
220 
221 
222     public String getActionTakenId() {
223         return actionTakenId;
224     }
225 
226     public void setActionTakenId(String actionTakenId) {
227         this.actionTakenId = actionTakenId;
228     }
229 
230     public String getAnnotation() {
231         return annotation;
232     }
233 
234     public void setAnnotation(String annotation) {
235         this.annotation = annotation;
236     }
237 
238     public String getDelegatorPrincipalId() {
239         return delegatorPrincipalId;
240     }
241 
242     public void setDelegatorPrincipalId(String delegatorPrincipalId) {
243         this.delegatorPrincipalId = delegatorPrincipalId;
244     }
245 
246     public String getDelegatorGroupId() {
247         return delegatorGroupId;
248     }
249 
250     public void setDelegatorGroupId(String delegatorGroupId) {
251         this.delegatorGroupId = delegatorGroupId;
252     }
253 
254     public String getDelegatorRoleId() {
255         ActionRequestValue actionRequest = KEWServiceLocator.getActionRequestService().getActionRequestForRole(actionTakenId);
256         if (actionRequest != null) {
257             return actionRequest.getRoleName();
258         } else {
259             return null;
260         }
261     }
262 
263     public Integer getDocVersion() {
264         return docVersion;
265     }
266 
267     public void setDocVersion(Integer docVersion) {
268         this.docVersion = docVersion;
269     }
270 
271     public Integer getLockVerNbr() {
272         return lockVerNbr;
273     }
274 
275     public void setLockVerNbr(Integer lockVerNbr) {
276         this.lockVerNbr = lockVerNbr;
277     }
278 
279     public String getDocumentId() {
280     	return documentId;
281     }
282 
283     public void setDocumentId(String documentId) {
284         this.documentId = documentId;
285     }
286 
287     public String getPrincipalId() {
288     	return principalId;
289     }
290     
291     public void setPrincipalId(String principalId) {
292     	this.principalId = principalId;
293     }
294 
295     public Boolean getCurrentIndicator() {
296         return currentIndicator;
297     }
298 
299     public void setCurrentIndicator(Boolean currentIndicator) {
300         this.currentIndicator = currentIndicator;
301     }
302 
303     public Collection getRootActionRequests() {
304         return getActionRequestService().getRootRequests(getActionRequests());
305     }
306 
307     private ActionRequestService getActionRequestService() {
308         return (ActionRequestService) KEWServiceLocator.getService(KEWServiceLocator.ACTION_REQUEST_SRV);
309     }
310 
311     public String getActionDateString() {
312         if(actionDateString == null || actionDateString.trim().equals("")){
313             return RiceConstants.getDefaultDateFormat().format(getActionDate());
314         } else {
315             return actionDateString;
316         }
317     }
318     public void setActionDateString(String actionDateString) {
319         this.actionDateString = actionDateString;
320     }
321 
322     public boolean isApproval() {
323     	return KewApiConstants.ACTION_TAKEN_APPROVED_CD.equals(getActionTaken());
324     }
325 
326     public boolean isCompletion() {
327     	return KewApiConstants.ACTION_TAKEN_COMPLETED_CD.equals(getActionTaken());
328     }
329     
330     public static ActionTaken to(ActionTakenValue actionTakenBo) {
331     	if (actionTakenBo == null) {
332     		return null;
333     	}
334     	ActionTaken.Builder builder = ActionTaken.Builder.create(actionTakenBo.getActionTakenId(),
335     			actionTakenBo.getDocumentId(),
336     			actionTakenBo.getPrincipalId(),
337     			ActionType.fromCode(actionTakenBo.getActionTaken()));
338         if (actionTakenBo.getActionDate() != null) {
339     	    builder.setActionDate(new DateTime(actionTakenBo.getActionDate().getTime()));
340         }
341     	builder.setAnnotation(actionTakenBo.getAnnotation());
342     	builder.setCurrent(actionTakenBo.getCurrentIndicator().booleanValue());
343     	builder.setDelegatorGroupId(actionTakenBo.getDelegatorGroupId());
344     	builder.setDelegatorPrincipalId(actionTakenBo.getDelegatorPrincipalId());
345     	return builder.build();
346     }
347 
348     public boolean isSuperUserAction() {
349         if ( KewApiConstants.ACTION_TAKEN_SU_ACTION_REQUEST_ACKNOWLEDGED_CD.equals(actionTaken) ||
350                 KewApiConstants.ACTION_TAKEN_SU_ACTION_REQUEST_FYI_CD.equals(actionTaken) ||
351                 KewApiConstants.ACTION_TAKEN_SU_ACTION_REQUEST_COMPLETED_CD.equals(actionTaken) ||
352                 KewApiConstants.ACTION_TAKEN_SU_ACTION_REQUEST_APPROVED_CD.equals(actionTaken) ||
353                 KewApiConstants.ACTION_TAKEN_SU_ROUTE_LEVEL_APPROVED_CD.equals(actionTaken) ||
354                 KewApiConstants.ACTION_TAKEN_SU_RETURNED_TO_PREVIOUS_CD.equals(actionTaken) ||
355                 KewApiConstants.ACTION_TAKEN_SU_DISAPPROVED_CD.equals(actionTaken) ||
356                 KewApiConstants.ACTION_TAKEN_SU_CANCELED_CD.equals(actionTaken) ||
357                 KewApiConstants.ACTION_TAKEN_SU_APPROVED_CD.equals(actionTaken)
358                 ) {
359             return true;
360         } else {
361             return false;
362         }
363     }
364 }