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