1
2
3
4
5
6
7
8
9
10
11
12
13
14
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.services.KimApiServiceLocator;
39
40 import javax.persistence.Column;
41 import javax.persistence.Entity;
42 import javax.persistence.FetchType;
43 import javax.persistence.GeneratedValue;
44 import javax.persistence.Id;
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
54
55
56
57
58
59
60
61 @Entity
62
63 @Table(name="KREW_ACTN_TKN_T")
64 public class ActionTakenValue implements Serializable {
65
66
67
68
69 private static final long serialVersionUID = -81505450567067594L;
70 @Id
71 @GeneratedValue(generator="KREW_ACTN_TKN_S")
72 @GenericGenerator(name="KREW_ACTN_TKN_S",strategy="org.hibernate.id.enhanced.SequenceStyleGenerator",parameters={
73 @Parameter(name="sequence_name",value="KREW_ACTN_TKN_S"),
74 @Parameter(name="value_column",value="id")
75 })
76 @Column(name="ACTN_TKN_ID")
77 private String actionTakenId;
78 @Column(name="DOC_HDR_ID")
79 private String documentId;
80 @Column(name="ACTN_CD")
81 private String actionTaken;
82 @Column(name="ACTN_DT")
83 private Timestamp actionDate;
84 @Column(name="ANNOTN")
85 private String annotation = "";
86 @Column(name="DOC_VER_NBR")
87 private Integer docVersion;
88 @Version
89 @Column(name="VER_NBR")
90 private Integer lockVerNbr;
91 @Column(name="PRNCPL_ID")
92 private String principalId;
93 @Column(name="DLGTR_PRNCPL_ID")
94 private String delegatorPrincipalId;
95 @Column(name="DLGTR_GRP_ID")
96 private String delegatorGroupId;
97
98
99
100 @OneToMany(fetch=FetchType.EAGER, mappedBy="actionTaken")
101 @Fetch(value = FetchMode.SELECT)
102 private Collection<ActionRequestValue> actionRequests;
103 @Column(name="CUR_IND")
104 private Boolean currentIndicator = Boolean.TRUE;
105 @Transient
106 private String actionDateString;
107
108 public Principal getPrincipal() {
109 return getPrincipalForId( principalId );
110 }
111
112
113 public void beforeInsert(){
114 OrmUtils.populateAutoIncValue(this, KEWServiceLocator.getEntityManagerFactory().createEntityManager());
115 }
116
117 public String getPrincipalDisplayName() {
118
119 return KEWServiceLocator.getIdentityHelperService().getPerson(getPrincipalId()).getName();
120 }
121
122 public Principal getDelegatorPrincipal() {
123 return getPrincipalForId(delegatorPrincipalId);
124 }
125
126 public Group getDelegatorGroup()
127 {
128 return KimApiServiceLocator.getGroupService()
129 .getGroup(String.valueOf(delegatorGroupId));
130 }
131
132 public void setDelegator(Recipient recipient) {
133 if (recipient instanceof KimPrincipalRecipient) {
134 setDelegatorPrincipalId(((KimPrincipalRecipient)recipient).getPrincipalId());
135 } else if (recipient instanceof KimGroupRecipient) {
136 setDelegatorGroupId(((KimGroupRecipient)recipient).getGroup().getId());
137 } else {
138 setDelegatorPrincipalId(null);
139 setDelegatorGroupId(null);
140 }
141 }
142
143 public boolean isForDelegator() {
144 return getDelegatorPrincipalId() != null || getDelegatorGroupId() != null;
145 }
146
147 public String getDelegatorDisplayName() {
148 if (! isForDelegator()) {
149 return "";
150 }
151 if (getDelegatorPrincipalId() != null) {
152
153 return KEWServiceLocator.getIdentityHelperService().getPerson(this.getDelegatorPrincipalId()).getName();
154 } else {
155 return getDelegatorGroup().getName();
156 }
157 }
158
159 private Principal getPrincipalForId(String principalId) {
160 Principal principal = null;
161
162 if (!StringUtils.isBlank(principalId)) {
163 principal = KEWServiceLocator.getIdentityHelperService().getPrincipal(principalId);
164 }
165
166 return principal;
167 }
168
169 public String getActionTakenLabel() {
170 return CodeTranslator.getActionTakenLabel(actionTaken);
171 }
172
173 public Collection<ActionRequestValue> getActionRequests() {
174 if (actionRequests == null) {
175 setActionRequests(new ArrayList<ActionRequestValue>());
176 }
177 return actionRequests;
178 }
179
180 public void setActionRequests(Collection<ActionRequestValue> actionRequests) {
181 this.actionRequests = actionRequests;
182 }
183
184
185
186
187
188
189
190
191
192 public Timestamp getActionDate() {
193 return actionDate;
194 }
195
196
197 public void setActionDate(Timestamp actionDate) {
198 this.actionDate = actionDate;
199 }
200
201
202 public String getActionTaken() {
203 return actionTaken;
204 }
205
206
207 public void setActionTaken(String actionTaken) {
208 this.actionTaken = actionTaken;
209 }
210
211
212 public String getActionTakenId() {
213 return actionTakenId;
214 }
215
216 public void setActionTakenId(String actionTakenId) {
217 this.actionTakenId = actionTakenId;
218 }
219
220 public String getAnnotation() {
221 return annotation;
222 }
223
224 public void setAnnotation(String annotation) {
225 this.annotation = annotation;
226 }
227
228 public String getDelegatorPrincipalId() {
229 return delegatorPrincipalId;
230 }
231
232 public void setDelegatorPrincipalId(String delegatorPrincipalId) {
233 this.delegatorPrincipalId = delegatorPrincipalId;
234 }
235
236 public String getDelegatorGroupId() {
237 return delegatorGroupId;
238 }
239 public void setDelegatorGroupId(String delegatorGroupId) {
240 this.delegatorGroupId = delegatorGroupId;
241 }
242 public Integer getDocVersion() {
243 return docVersion;
244 }
245
246 public void setDocVersion(Integer docVersion) {
247 this.docVersion = docVersion;
248 }
249
250 public Integer getLockVerNbr() {
251 return lockVerNbr;
252 }
253
254 public void setLockVerNbr(Integer lockVerNbr) {
255 this.lockVerNbr = lockVerNbr;
256 }
257
258 public String getDocumentId() {
259 return documentId;
260 }
261
262 public void setDocumentId(String documentId) {
263 this.documentId = documentId;
264 }
265
266 public String getPrincipalId() {
267 return principalId;
268 }
269
270 public void setPrincipalId(String principalId) {
271 this.principalId = principalId;
272 }
273
274 public Boolean getCurrentIndicator() {
275 return currentIndicator;
276 }
277
278 public void setCurrentIndicator(Boolean currentIndicator) {
279 this.currentIndicator = currentIndicator;
280 }
281
282 public Collection getRootActionRequests() {
283 return getActionRequestService().getRootRequests(getActionRequests());
284 }
285
286 private ActionRequestService getActionRequestService() {
287 return (ActionRequestService) KEWServiceLocator.getService(KEWServiceLocator.ACTION_REQUEST_SRV);
288 }
289
290 public String getActionDateString() {
291 if(actionDateString == null || actionDateString.trim().equals("")){
292 return RiceConstants.getDefaultDateFormat().format(getActionDate());
293 } else {
294 return actionDateString;
295 }
296 }
297 public void setActionDateString(String actionDateString) {
298 this.actionDateString = actionDateString;
299 }
300
301 public boolean isApproval() {
302 return KewApiConstants.ACTION_TAKEN_APPROVED_CD.equals(getActionTaken());
303 }
304
305 public boolean isCompletion() {
306 return KewApiConstants.ACTION_TAKEN_COMPLETED_CD.equals(getActionTaken());
307 }
308
309 public static ActionTaken to(ActionTakenValue actionTakenBo) {
310 if (actionTakenBo == null) {
311 return null;
312 }
313 ActionTaken.Builder builder = ActionTaken.Builder.create(actionTakenBo.getActionTakenId(),
314 actionTakenBo.getDocumentId(),
315 actionTakenBo.getPrincipalId(),
316 ActionType.fromCode(actionTakenBo.getActionTaken()));
317 if (actionTakenBo.getActionDate() != null) {
318 builder.setActionDate(new DateTime(actionTakenBo.getActionDate().getTime()));
319 }
320 builder.setAnnotation(actionTakenBo.getAnnotation());
321 builder.setCurrent(actionTakenBo.getCurrentIndicator().booleanValue());
322 builder.setDelegatorGroupId(actionTakenBo.getDelegatorGroupId());
323 builder.setDelegatorPrincipalId(actionTakenBo.getDelegatorPrincipalId());
324 return builder.build();
325 }
326
327 }