001 /**
002 * Copyright 2005-2012 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package org.kuali.rice.kew.actiontaken;
017
018 import org.apache.commons.lang.StringUtils;
019 import org.hibernate.annotations.Fetch;
020 import org.hibernate.annotations.FetchMode;
021 import org.hibernate.annotations.GenericGenerator;
022 import org.hibernate.annotations.Parameter;
023 import org.joda.time.DateTime;
024 import org.kuali.rice.core.api.util.RiceConstants;
025 import org.kuali.rice.core.framework.persistence.jpa.OrmUtils;
026 import org.kuali.rice.kew.actionrequest.ActionRequestValue;
027 import org.kuali.rice.kew.actionrequest.KimGroupRecipient;
028 import org.kuali.rice.kew.actionrequest.KimPrincipalRecipient;
029 import org.kuali.rice.kew.actionrequest.Recipient;
030 import org.kuali.rice.kew.actionrequest.service.ActionRequestService;
031 import org.kuali.rice.kew.api.action.ActionTaken;
032 import org.kuali.rice.kew.api.action.ActionType;
033 import org.kuali.rice.kew.api.util.CodeTranslator;
034 import org.kuali.rice.kew.service.KEWServiceLocator;
035 import org.kuali.rice.kew.api.KewApiConstants;
036 import org.kuali.rice.kim.api.group.Group;
037 import org.kuali.rice.kim.api.identity.principal.Principal;
038 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
039
040 import javax.persistence.Column;
041 import javax.persistence.Entity;
042 import javax.persistence.FetchType;
043 import javax.persistence.GeneratedValue;
044 import javax.persistence.Id;
045 import javax.persistence.OneToMany;
046 import javax.persistence.Table;
047 import javax.persistence.Transient;
048 import javax.persistence.Version;
049 import java.io.Serializable;
050 import java.sql.Timestamp;
051 import java.util.ArrayList;
052 import java.util.Collection;
053
054
055 /**
056 * Model object mapped to ojb for representing actions taken on documents by
057 * users.
058 *
059 * @author Kuali Rice Team (rice.collab@kuali.org)
060 */
061 @Entity
062 //@Sequence(name="KREW_ACTN_TKN_S", property="actionTakenId")
063 @Table(name="KREW_ACTN_TKN_T")
064 public class ActionTakenValue implements Serializable {
065
066 /**
067 *
068 */
069 private static final long serialVersionUID = -81505450567067594L;
070 @Id
071 @GeneratedValue(generator="KREW_ACTN_TKN_S")
072 @GenericGenerator(name="KREW_ACTN_TKN_S",strategy="org.hibernate.id.enhanced.SequenceStyleGenerator",parameters={
073 @Parameter(name="sequence_name",value="KREW_ACTN_TKN_S"),
074 @Parameter(name="value_column",value="id")
075 })
076 @Column(name="ACTN_TKN_ID")
077 private String actionTakenId;
078 @Column(name="DOC_HDR_ID")//,insertable=false, updatable=false)
079 private String documentId;
080 @Column(name="ACTN_CD")
081 private String actionTaken;
082 @Column(name="ACTN_DT")
083 private Timestamp actionDate;
084 @Column(name="ANNOTN")
085 private String annotation = "";
086 @Column(name="DOC_VER_NBR")
087 private Integer docVersion;
088 @Version
089 @Column(name="VER_NBR")
090 private Integer lockVerNbr;
091 @Column(name="PRNCPL_ID")
092 private String principalId;
093 @Column(name="DLGTR_PRNCPL_ID")
094 private String delegatorPrincipalId;
095 @Column(name="DLGTR_GRP_ID")
096 private String delegatorGroupId;
097 //@ManyToOne(fetch=FetchType.EAGER, cascade={CascadeType.PERSIST})
098 //@JoinColumn(name="DOC_HDR_ID")
099 //private DocumentRouteHeaderValue routeHeader;
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 //@PrePersist
113 public void beforeInsert(){
114 OrmUtils.populateAutoIncValue(this, KEWServiceLocator.getEntityManagerFactory().createEntityManager());
115 }
116
117 public String getPrincipalDisplayName() {
118 // TODO this stinks to have to have a dependency on UserSession here
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 // TODO this stinks to have to have a dependency on UserSession here
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 //public DocumentRouteHeaderValue getRouteHeader() {
185 // return routeHeader;
186 //}
187
188 //public void setRouteHeader(DocumentRouteHeaderValue routeHeader) {
189 // this.routeHeader = routeHeader;
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 }