View Javadoc

1   /*
2    * Copyright 2005-2007 The Kuali Foundation
3    *
4    *
5    * Licensed under the Educational Community License, Version 2.0 (the "License");
6    * you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    *
9    * http://www.opensource.org/licenses/ecl2.php
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.kuali.rice.kew.actiontaken;
18  
19  import org.apache.commons.beanutils.BeanUtils;
20  import org.apache.commons.lang.StringUtils;
21  import org.kuali.rice.core.jpa.annotations.Sequence;
22  import org.kuali.rice.core.util.OrmUtils;
23  import org.kuali.rice.core.util.RiceConstants;
24  import org.kuali.rice.kew.actionrequest.ActionRequestValue;
25  import org.kuali.rice.kew.actionrequest.KimGroupRecipient;
26  import org.kuali.rice.kew.actionrequest.KimPrincipalRecipient;
27  import org.kuali.rice.kew.actionrequest.Recipient;
28  import org.kuali.rice.kew.actionrequest.service.ActionRequestService;
29  import org.kuali.rice.kew.bo.WorkflowPersistable;
30  import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue;
31  import org.kuali.rice.kew.service.KEWServiceLocator;
32  import org.kuali.rice.kew.util.CodeTranslator;
33  import org.kuali.rice.kew.util.KEWConstants;
34  import org.kuali.rice.kim.bo.Group;
35  import org.kuali.rice.kim.bo.entity.KimPrincipal;
36  import org.kuali.rice.kim.service.KIMServiceLocator;
37  
38  import javax.persistence.*;
39  import java.sql.Timestamp;
40  import java.util.ArrayList;
41  import java.util.Collection;
42  
43  
44  /**
45   * Model object mapped to ojb for representing actions taken on documents by
46   * users.
47   *
48   * @author Kuali Rice Team (rice.collab@kuali.org)
49   */
50  @Entity
51  @Sequence(name="KREW_ACTN_TKN_S", property="actionTakenId")
52  @Table(name="KREW_ACTN_TKN_T")
53  public class ActionTakenValue implements WorkflowPersistable {
54  
55      /**
56  	 *
57  	 */
58  	private static final long serialVersionUID = -81505450567067594L;
59  	@Id
60  	@Column(name="ACTN_TKN_ID")
61      private Long actionTakenId;
62      @Column(name="DOC_HDR_ID",insertable=false, updatable=false)
63  	private Long routeHeaderId;
64      @Column(name="ACTN_CD")
65  	private String actionTaken;
66  	@Column(name="ACTN_DT")
67  	private Timestamp actionDate;
68      @Column(name="ANNOTN")
69      private String annotation = "";
70      @Column(name="DOC_VER_NBR")
71  	private Integer docVersion;
72      @Version
73  	@Column(name="VER_NBR")
74  	private Integer lockVerNbr;
75      @Column(name="PRNCPL_ID")
76  	private String principalId;
77      @Column(name="DLGTR_PRNCPL_ID")
78  	private String delegatorPrincipalId;
79      @Column(name="DLGTR_GRP_ID")
80  	private String delegatorGroupId;
81      @ManyToOne(fetch=FetchType.EAGER, cascade={CascadeType.PERSIST})
82      @JoinColumn(name="DOC_HDR_ID")
83      private DocumentRouteHeaderValue routeHeader;
84      @OneToMany(mappedBy="actionTaken")
85  	private Collection<ActionRequestValue> actionRequests;
86      @Column(name="CUR_IND")
87      private Boolean currentIndicator = Boolean.TRUE;
88      @Transient
89      private String actionDateString;
90  
91      public KimPrincipal getPrincipal() {
92      	return getPrincipalForId( principalId );
93      }
94      
95  	@PrePersist
96  	public void beforeInsert(){
97  		OrmUtils.populateAutoIncValue(this, KEWServiceLocator.getEntityManagerFactory().createEntityManager());		
98  	}
99  
100     public String getPrincipalDisplayName() {
101     	// TODO this stinks to have to have a dependency on UserSession here
102     	return KEWServiceLocator.getIdentityHelperService().getPerson(getPrincipalId()).getName();
103     }
104 
105     public KimPrincipal getDelegatorPrincipal() {
106     	return getPrincipalForId(delegatorPrincipalId);
107     }
108 
109     public Group getDelegatorGroup()
110     {
111 	    return KIMServiceLocator.getIdentityManagementService()
112 	            .getGroup(String.valueOf(delegatorGroupId));
113     }
114 
115     public void setDelegator(Recipient recipient) {
116         if (recipient instanceof KimPrincipalRecipient) {
117             setDelegatorPrincipalId(((KimPrincipalRecipient)recipient).getPrincipalId());
118         } else if (recipient instanceof KimGroupRecipient) {
119             setDelegatorGroupId(((KimGroupRecipient)recipient).getGroup().getGroupId());
120         } else {
121             setDelegatorPrincipalId(null);
122             setDelegatorGroupId(null);
123         }
124     }
125 
126     public boolean isForDelegator() {
127         return getDelegatorPrincipalId() != null || getDelegatorGroupId() != null;
128     }
129 
130     public String getDelegatorDisplayName() {
131         if (! isForDelegator()) {
132             return "";
133         }
134         if (getDelegatorPrincipalId() != null) {
135         	// TODO this stinks to have to have a dependency on UserSession here
136         	return KEWServiceLocator.getIdentityHelperService().getPerson(this.getDelegatorPrincipalId()).getName();
137         } else {
138             return getDelegatorGroup().getGroupName();
139       }
140     }
141     
142     private KimPrincipal getPrincipalForId(String principalId) {
143     	KimPrincipal principal = null;
144     	
145     	if (!StringUtils.isBlank(principalId)) {
146     		principal = KEWServiceLocator.getIdentityHelperService().getPrincipal(principalId);
147     	}
148     	
149     	return principal;
150     }
151 
152     public String getActionTakenLabel() {
153         return CodeTranslator.getActionTakenLabel(actionTaken);
154     }
155 
156     public Collection<ActionRequestValue> getActionRequests() {
157         if (actionRequests == null) {
158             setActionRequests(new ArrayList<ActionRequestValue>());
159         }
160         return actionRequests;
161     }
162 
163     public void setActionRequests(Collection<ActionRequestValue> actionRequests) {
164         this.actionRequests = actionRequests;
165     }
166 
167     public DocumentRouteHeaderValue getRouteHeader() {
168         return routeHeader;
169     }
170 
171     public void setRouteHeader(DocumentRouteHeaderValue routeHeader) {
172         this.routeHeader = routeHeader;
173     }
174 
175     public Timestamp getActionDate() {
176         return actionDate;
177     }
178 
179 
180     public void setActionDate(Timestamp actionDate) {
181         this.actionDate = actionDate;
182     }
183 
184 
185     public String getActionTaken() {
186         return actionTaken;
187     }
188 
189 
190     public void setActionTaken(String actionTaken) {
191         this.actionTaken = actionTaken;
192     }
193 
194 
195     public Long getActionTakenId() {
196         return actionTakenId;
197     }
198 
199     public void setActionTakenId(Long actionTakenId) {
200         this.actionTakenId = actionTakenId;
201     }
202 
203     public String getAnnotation() {
204         return annotation;
205     }
206 
207     public void setAnnotation(String annotation) {
208         this.annotation = annotation;
209     }
210 
211     public String getDelegatorPrincipalId() {
212         return delegatorPrincipalId;
213     }
214 
215     public void setDelegatorPrincipalId(String delegatorPrincipalId) {
216         this.delegatorPrincipalId = delegatorPrincipalId;
217     }
218 
219     public String getDelegatorGroupId() {
220         return delegatorGroupId;
221     }
222     public void setDelegatorGroupId(String delegatorGroupId) {
223         this.delegatorGroupId = delegatorGroupId;
224     }
225     public Integer getDocVersion() {
226         return docVersion;
227     }
228 
229     public void setDocVersion(Integer docVersion) {
230         this.docVersion = docVersion;
231     }
232 
233     public Integer getLockVerNbr() {
234         return lockVerNbr;
235     }
236 
237     public void setLockVerNbr(Integer lockVerNbr) {
238         this.lockVerNbr = lockVerNbr;
239     }
240 
241     public Long getRouteHeaderId() {
242 
243     	return routeHeaderId;
244     }
245 
246     public void setRouteHeaderId(Long routeHeaderId) {
247         this.routeHeaderId = routeHeaderId;
248     }
249 
250     public String getPrincipalId() {
251     	return principalId;
252     }
253     
254     public void setPrincipalId(String principalId) {
255     	this.principalId = principalId;
256     }
257 
258     public Boolean getCurrentIndicator() {
259         return currentIndicator;
260     }
261 
262     public void setCurrentIndicator(Boolean currentIndicator) {
263         this.currentIndicator = currentIndicator;
264     }
265 
266     public Collection getRootActionRequests() {
267         return getActionRequestService().getRootRequests(getActionRequests());
268     }
269 
270     public Object copy(boolean preserveKeys) {
271         ActionTakenValue clone = new ActionTakenValue();
272         try {
273             BeanUtils.copyProperties(clone, this);
274         } catch(Exception e) {
275             throw new RuntimeException(e);
276         }
277 
278         if (!preserveKeys) {
279             clone.setActionTakenId(null);
280         }
281         return clone;
282     }
283 
284     private ActionRequestService getActionRequestService() {
285         return (ActionRequestService) KEWServiceLocator.getService(KEWServiceLocator.ACTION_REQUEST_SRV);
286     }
287 
288     public String getActionDateString() {
289         if(actionDateString == null || actionDateString.trim().equals("")){
290             return RiceConstants.getDefaultDateFormat().format(getActionDate());
291         } else {
292             return actionDateString;
293         }
294     }
295     public void setActionDateString(String actionDateString) {
296         this.actionDateString = actionDateString;
297     }
298 
299     public boolean isApproval() {
300     	return KEWConstants.ACTION_TAKEN_APPROVED_CD.equals(getActionTaken());
301     }
302 
303     public boolean isCompletion() {
304     	return KEWConstants.ACTION_TAKEN_COMPLETED_CD.equals(getActionTaken());
305     }
306 }