001/**
002 * Copyright 2005-2016 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 */
016package org.kuali.rice.kew.actiontaken;
017
018import org.apache.commons.lang.StringUtils;
019import org.hibernate.annotations.Fetch;
020import org.hibernate.annotations.FetchMode;
021import org.hibernate.annotations.GenericGenerator;
022import org.hibernate.annotations.Parameter;
023import org.joda.time.DateTime;
024import org.kuali.rice.core.api.util.RiceConstants;
025import org.kuali.rice.core.framework.persistence.jpa.OrmUtils;
026import org.kuali.rice.kew.actionrequest.ActionRequestValue;
027import org.kuali.rice.kew.actionrequest.KimGroupRecipient;
028import org.kuali.rice.kew.actionrequest.KimPrincipalRecipient;
029import org.kuali.rice.kew.actionrequest.Recipient;
030import org.kuali.rice.kew.actionrequest.service.ActionRequestService;
031import org.kuali.rice.kew.api.action.ActionTaken;
032import org.kuali.rice.kew.api.action.ActionType;
033import org.kuali.rice.kew.api.util.CodeTranslator;
034import org.kuali.rice.kew.service.KEWServiceLocator;
035import org.kuali.rice.kew.api.KewApiConstants;
036import org.kuali.rice.kim.api.group.Group;
037import org.kuali.rice.kim.api.identity.principal.Principal;
038import org.kuali.rice.kim.api.role.Role;
039import org.kuali.rice.kim.api.services.KimApiServiceLocator;
040
041import javax.persistence.Column;
042import javax.persistence.Entity;
043import javax.persistence.FetchType;
044import javax.persistence.GeneratedValue;
045import javax.persistence.Id;
046import javax.persistence.OneToMany;
047import javax.persistence.Table;
048import javax.persistence.Transient;
049import javax.persistence.Version;
050import java.io.Serializable;
051import java.sql.Timestamp;
052import java.util.ArrayList;
053import java.util.Collection;
054
055
056/**
057 * Model object mapped to ojb for representing actions taken on documents by
058 * users.
059 *
060 * @author Kuali Rice Team (rice.collab@kuali.org)
061 */
062@Entity
063//@Sequence(name="KREW_ACTN_TKN_S", property="actionTakenId")
064@Table(name="KREW_ACTN_TKN_T")
065public class ActionTakenValue implements Serializable {
066
067    /**
068         *
069         */
070        private static final long serialVersionUID = -81505450567067594L;
071        @Id
072        @GeneratedValue(generator="KREW_ACTN_TKN_S")
073        @GenericGenerator(name="KREW_ACTN_TKN_S",strategy="org.hibernate.id.enhanced.SequenceStyleGenerator",parameters={
074                        @Parameter(name="sequence_name",value="KREW_ACTN_TKN_S"),
075                        @Parameter(name="value_column",value="id")
076        })
077        @Column(name="ACTN_TKN_ID")
078    private String actionTakenId;
079    @Column(name="DOC_HDR_ID")//,insertable=false, updatable=false)
080        private String documentId;
081    @Column(name="ACTN_CD")
082        private String actionTaken;
083        @Column(name="ACTN_DT")
084        private Timestamp actionDate;
085    @Column(name="ANNOTN")
086    private String annotation = "";
087    @Column(name="DOC_VER_NBR")
088        private Integer docVersion;
089    @Version
090        @Column(name="VER_NBR")
091        private Integer lockVerNbr;
092    @Column(name="PRNCPL_ID")
093        private String principalId;
094    @Column(name="DLGTR_PRNCPL_ID")
095        private String delegatorPrincipalId;
096    @Column(name="DLGTR_GRP_ID")
097        private String delegatorGroupId;
098    //@ManyToOne(fetch=FetchType.EAGER, cascade={CascadeType.PERSIST})
099    //@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}