001 /** 002 * Copyright 2005-2014 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.joda.time.DateTime; 020 import org.kuali.rice.core.api.util.RiceConstants; 021 import org.kuali.rice.kew.actionrequest.ActionRequestValue; 022 import org.kuali.rice.kew.actionrequest.KimGroupRecipient; 023 import org.kuali.rice.kew.actionrequest.KimPrincipalRecipient; 024 import org.kuali.rice.kew.actionrequest.Recipient; 025 import org.kuali.rice.kew.actionrequest.service.ActionRequestService; 026 import org.kuali.rice.kew.actiontaken.dao.impl.ActionTakenDaoJpa; 027 import org.kuali.rice.kew.api.KewApiConstants; 028 import org.kuali.rice.kew.api.action.ActionTaken; 029 import org.kuali.rice.kew.api.action.ActionType; 030 import org.kuali.rice.kew.api.util.CodeTranslator; 031 import org.kuali.rice.kew.service.KEWServiceLocator; 032 import org.kuali.rice.kim.api.group.Group; 033 import org.kuali.rice.kim.api.identity.principal.Principal; 034 import org.kuali.rice.kim.api.role.Role; 035 import org.kuali.rice.kim.api.services.KimApiServiceLocator; 036 import org.kuali.rice.krad.data.jpa.converters.Boolean01Converter; 037 import org.kuali.rice.krad.data.jpa.PortableSequenceGenerator; 038 039 import javax.persistence.Column; 040 import javax.persistence.Convert; 041 import javax.persistence.Entity; 042 import javax.persistence.GeneratedValue; 043 import javax.persistence.Id; 044 import javax.persistence.NamedQueries; 045 import javax.persistence.NamedQuery; 046 import javax.persistence.OneToMany; 047 import javax.persistence.Table; 048 import javax.persistence.Transient; 049 import javax.persistence.Version; 050 import java.io.Serializable; 051 import java.sql.Timestamp; 052 import java.util.ArrayList; 053 import java.util.Collection; 054 import java.util.List; 055 import java.util.Map; 056 057 /** 058 * Model object mapped to ojb for representing actions taken on documents by users. The type of the action is indicated 059 * by the {@link #actionTaken} code which will be a valid code value from {@link ActionType}. 060 * 061 * @author Kuali Rice Team (rice.collab@kuali.org) 062 */ 063 @Entity 064 @Table(name = "KREW_ACTN_TKN_T") 065 @NamedQueries({ 066 @NamedQuery(name = ActionTakenDaoJpa.GET_LAST_ACTION_TAKEN_DATE_NAME, 067 query = ActionTakenDaoJpa.GET_LAST_ACTION_TAKEN_DATE_QUERY) 068 }) 069 public class ActionTakenValue implements Serializable { 070 071 private static final long serialVersionUID = -81505450567067594L; 072 073 @Id 074 @GeneratedValue(generator = "KREW_ACTN_TKN_S") 075 @PortableSequenceGenerator(name = "KREW_ACTN_TKN_S") 076 @Column(name="ACTN_TKN_ID", nullable = false) 077 private String actionTakenId; 078 079 @Column(name="DOC_HDR_ID", nullable = false) 080 private String documentId; 081 082 @Column(name="ACTN_CD", nullable = false) 083 private String actionTaken; 084 085 @Column(name="ACTN_DT", nullable = false) 086 private Timestamp actionDate; 087 088 @Column(name="ANNOTN") 089 private String annotation = ""; 090 091 @Column(name="DOC_VER_NBR", nullable = false) 092 private Integer docVersion; 093 094 @Column(name="PRNCPL_ID", nullable = false) 095 private String principalId; 096 097 @Column(name="DLGTR_PRNCPL_ID") 098 private String delegatorPrincipalId; 099 100 @Column(name="DLGTR_GRP_ID") 101 private String delegatorGroupId; 102 103 @Column(name="CUR_IND") 104 @Convert(converter = Boolean01Converter.class) 105 private Boolean currentIndicator = Boolean.TRUE; 106 107 @Version 108 @Column(name="VER_NBR") 109 private Integer lockVerNbr; 110 111 @OneToMany(mappedBy = "actionTaken") 112 private Collection<ActionRequestValue> actionRequests; 113 114 @Transient private String actionDateString; 115 116 public Principal getPrincipal() { 117 return getPrincipalForId( principalId ); 118 } 119 120 public String getPrincipalDisplayName() { 121 return KEWServiceLocator.getIdentityHelperService().getPerson(getPrincipalId()).getName(); 122 } 123 124 public Principal getDelegatorPrincipal() { 125 return getPrincipalForId(delegatorPrincipalId); 126 } 127 128 public Group getDelegatorGroup() 129 { 130 return KimApiServiceLocator.getGroupService() 131 .getGroup(String.valueOf(delegatorGroupId)); 132 } 133 134 public void setDelegator(Recipient recipient) { 135 if (recipient instanceof KimPrincipalRecipient) { 136 setDelegatorPrincipalId(((KimPrincipalRecipient)recipient).getPrincipalId()); 137 } else if (recipient instanceof KimGroupRecipient) { 138 setDelegatorGroupId(((KimGroupRecipient)recipient).getGroup().getId()); 139 } else { 140 setDelegatorPrincipalId(null); 141 setDelegatorGroupId(null); 142 } 143 } 144 145 public boolean isForDelegator() { 146 return getDelegatorPrincipalId() != null || getDelegatorGroupId() != null || getDelegatorRoleId() != null; 147 } 148 149 public String getDelegatorDisplayName() { 150 if (getDelegatorPrincipalId() != null) { 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 Timestamp getActionDate() { 195 return actionDate; 196 } 197 198 public void setActionDate(Timestamp actionDate) { 199 this.actionDate = actionDate; 200 } 201 202 public String getActionTaken() { 203 return actionTaken; 204 } 205 206 public void setActionTaken(String actionTaken) { 207 this.actionTaken = actionTaken; 208 } 209 210 public String getActionTakenId() { 211 return actionTakenId; 212 } 213 214 public void setActionTakenId(String actionTakenId) { 215 this.actionTakenId = actionTakenId; 216 } 217 218 public String getAnnotation() { 219 return annotation; 220 } 221 222 public void setAnnotation(String annotation) { 223 this.annotation = annotation; 224 } 225 226 public String getDelegatorPrincipalId() { 227 return delegatorPrincipalId; 228 } 229 230 public void setDelegatorPrincipalId(String delegatorPrincipalId) { 231 this.delegatorPrincipalId = delegatorPrincipalId; 232 } 233 234 public String getDelegatorGroupId() { 235 return delegatorGroupId; 236 } 237 238 public void setDelegatorGroupId(String delegatorGroupId) { 239 this.delegatorGroupId = delegatorGroupId; 240 } 241 242 public String getDelegatorRoleId() { 243 // this could (perhaps) happen when running a simulation 244 if (actionTakenId == null) { 245 return null; 246 } 247 ActionRequestValue actionRequest = KEWServiceLocator.getActionRequestService().getActionRequestForRole(actionTakenId); 248 if (actionRequest != null) { 249 return actionRequest.getRoleName(); 250 } else { 251 return null; 252 } 253 } 254 255 public Integer getDocVersion() { 256 return docVersion; 257 } 258 259 public void setDocVersion(Integer docVersion) { 260 this.docVersion = docVersion; 261 } 262 263 public Integer getLockVerNbr() { 264 return lockVerNbr; 265 } 266 267 public void setLockVerNbr(Integer lockVerNbr) { 268 this.lockVerNbr = lockVerNbr; 269 } 270 271 public String getDocumentId() { 272 return documentId; 273 } 274 275 public void setDocumentId(String documentId) { 276 this.documentId = documentId; 277 } 278 279 public String getPrincipalId() { 280 return principalId; 281 } 282 283 public void setPrincipalId(String principalId) { 284 this.principalId = principalId; 285 } 286 287 public Boolean getCurrentIndicator() { 288 return currentIndicator; 289 } 290 291 public void setCurrentIndicator(Boolean currentIndicator) { 292 this.currentIndicator = currentIndicator; 293 } 294 295 public Collection getRootActionRequests() { 296 return getActionRequestService().getRootRequests(getActionRequests()); 297 } 298 299 private ActionRequestService getActionRequestService() { 300 return KEWServiceLocator.getService(KEWServiceLocator.ACTION_REQUEST_SRV); 301 } 302 303 public String getActionDateString() { 304 if(StringUtils.isBlank(actionDateString)) { 305 return RiceConstants.getDefaultDateFormat().format(getActionDate()); 306 } else { 307 return actionDateString; 308 } 309 } 310 public void setActionDateString(String actionDateString) { 311 this.actionDateString = actionDateString; 312 } 313 314 public boolean isApproval() { 315 return KewApiConstants.ACTION_TAKEN_APPROVED_CD.equals(getActionTaken()); 316 } 317 318 public boolean isCompletion() { 319 return KewApiConstants.ACTION_TAKEN_COMPLETED_CD.equals(getActionTaken()); 320 } 321 322 public ActionTakenValue deepCopy(Map<Object, Object> visited) { 323 if (visited.containsKey(this)) { 324 return (ActionTakenValue)visited.get(this); 325 } 326 ActionTakenValue copy = new ActionTakenValue(); 327 visited.put(this, copy); 328 copy.actionTakenId = actionTakenId; 329 copy.documentId = documentId; 330 copy.actionTaken = actionTaken; 331 if (actionDate != null) { 332 copy.actionDate = new Timestamp(actionDate.getTime()); 333 } 334 copy.annotation = annotation; 335 copy.docVersion = docVersion; 336 copy.principalId = principalId; 337 copy.delegatorPrincipalId = delegatorPrincipalId; 338 copy.delegatorGroupId = delegatorGroupId; 339 copy.currentIndicator = currentIndicator; 340 copy.lockVerNbr = lockVerNbr; 341 if (actionRequests != null) { 342 List<ActionRequestValue> copies = new ArrayList<ActionRequestValue>(); 343 for (ActionRequestValue actionRequest : actionRequests) { 344 copies.add(actionRequest.deepCopy(visited)); 345 } 346 copy.actionRequests = copies; 347 } 348 copy.actionDateString = actionDateString; 349 return copy; 350 } 351 352 public static ActionTaken to(ActionTakenValue actionTakenBo) { 353 if (actionTakenBo == null) { 354 return null; 355 } 356 ActionTaken.Builder builder = ActionTaken.Builder.create(actionTakenBo.getActionTakenId(), 357 actionTakenBo.getDocumentId(), 358 actionTakenBo.getPrincipalId(), 359 ActionType.fromCode(actionTakenBo.getActionTaken())); 360 if (actionTakenBo.getActionDate() != null) { 361 builder.setActionDate(new DateTime(actionTakenBo.getActionDate().getTime())); 362 } 363 builder.setAnnotation(actionTakenBo.getAnnotation()); 364 builder.setCurrent(actionTakenBo.getCurrentIndicator().booleanValue()); 365 builder.setDelegatorGroupId(actionTakenBo.getDelegatorGroupId()); 366 builder.setDelegatorPrincipalId(actionTakenBo.getDelegatorPrincipalId()); 367 return builder.build(); 368 } 369 370 public boolean isSuperUserAction() { 371 if ( KewApiConstants.ACTION_TAKEN_SU_ACTION_REQUEST_ACKNOWLEDGED_CD.equals(actionTaken) || 372 KewApiConstants.ACTION_TAKEN_SU_ACTION_REQUEST_FYI_CD.equals(actionTaken) || 373 KewApiConstants.ACTION_TAKEN_SU_ACTION_REQUEST_COMPLETED_CD.equals(actionTaken) || 374 KewApiConstants.ACTION_TAKEN_SU_ACTION_REQUEST_APPROVED_CD.equals(actionTaken) || 375 KewApiConstants.ACTION_TAKEN_SU_ROUTE_LEVEL_APPROVED_CD.equals(actionTaken) || 376 KewApiConstants.ACTION_TAKEN_SU_RETURNED_TO_PREVIOUS_CD.equals(actionTaken) || 377 KewApiConstants.ACTION_TAKEN_SU_DISAPPROVED_CD.equals(actionTaken) || 378 KewApiConstants.ACTION_TAKEN_SU_CANCELED_CD.equals(actionTaken) || 379 KewApiConstants.ACTION_TAKEN_SU_APPROVED_CD.equals(actionTaken) 380 ) { 381 return true; 382 } else { 383 return false; 384 } 385 } 386 }