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.actionitem;
017
018import org.apache.commons.lang.StringUtils;
019import org.apache.commons.lang.builder.ToStringBuilder;
020import org.hibernate.annotations.GenericGenerator;
021import org.hibernate.annotations.Parameter;
022import org.joda.time.DateTime;
023import org.kuali.rice.core.api.delegation.DelegationType;
024import org.kuali.rice.core.api.util.RiceConstants;
025import org.kuali.rice.core.framework.persistence.jpa.OrmUtils;
026import org.kuali.rice.kew.api.action.ActionItemContract;
027import org.kuali.rice.kew.api.action.RecipientType;
028import org.kuali.rice.kew.api.util.CodeTranslator;
029import org.kuali.rice.kew.service.KEWServiceLocator;
030import org.kuali.rice.kew.web.RowStyleable;
031import org.kuali.rice.kim.api.group.Group;
032import org.kuali.rice.kim.api.identity.principal.Principal;
033import org.kuali.rice.kim.api.services.KimApiServiceLocator;
034
035import javax.persistence.Column;
036import javax.persistence.Entity;
037import javax.persistence.GeneratedValue;
038import javax.persistence.Id;
039import javax.persistence.Inheritance;
040import javax.persistence.InheritanceType;
041import javax.persistence.NamedQueries;
042import javax.persistence.NamedQuery;
043import javax.persistence.Table;
044import javax.persistence.Transient;
045import javax.persistence.Version;
046import java.io.Serializable;
047import java.sql.Timestamp;
048import java.util.ArrayList;
049import java.util.Collection;
050import java.util.HashMap;
051import java.util.List;
052import java.util.Map;
053
054/**
055 * This is the model for action items. These are displayed as the action list as well.  Mapped to ActionItemService.
056 * NOTE: This object contains denormalized fields that have been copied from related ActionRequestValue and DocumentRouteHeaderValue
057 * objects for performance reasons.  These should be preserved and their related objects should not be added to the OJB
058 * mapping as we do not want them loaded for each ActionItem object.
059 *
060 * @author Kuali Rice Team (rice.collab@kuali.org)
061 *
062 */
063
064
065@Entity
066@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
067@Table(name="KREW_ACTN_ITM_T")
068//@Sequence(name="KREW_ACTN_ITM_S",property="id")
069@NamedQueries({
070    @NamedQuery(name="ActionItem.QuickLinks.FindActionListStatsByPrincipalId", query="SELECT docName, COUNT(*) FROM ActionItem WHERE principalId = :principalId " +
071        "AND (delegationType IS null OR delegationType != :delegationType) GROUP BY docName")
072})
073public class ActionItem implements ActionItemContract, Serializable {
074
075    private static final long serialVersionUID = -1079562205125660151L;
076
077    @Id
078    @GeneratedValue(generator="KREW_ACTN_ITM_S")
079        @GenericGenerator(name="KREW_ACTN_ITM_S",strategy="org.hibernate.id.enhanced.SequenceStyleGenerator",parameters={
080                        @Parameter(name="sequence_name",value="KREW_ACTN_ITM_S"),
081                        @Parameter(name="value_column",value="id")
082        })
083    @Column(name="ACTN_ITM_ID")
084        private String id;
085    @Column(name="PRNCPL_ID")
086        private String principalId;
087        @Column(name="ASND_DT")
088        private Timestamp dateAssigned;
089    @Column(name="RQST_CD")
090        private String actionRequestCd;
091    @Column(name="ACTN_RQST_ID", nullable=false)
092        private String actionRequestId;
093    @Column(name="DOC_HDR_ID")//, insertable=false, updatable=false)
094        private String documentId;
095    @Column(name="GRP_ID")
096        private String groupId;
097    @Column(name="DOC_HDR_TTL")
098        private String docTitle;
099    @Column(name="DOC_TYP_LBL")
100        private String docLabel;
101    @Column(name="DOC_HDLR_URL")
102        private String docHandlerURL;
103    @Column(name="DOC_TYP_NM")
104        private String docName;
105    @Column(name="RSP_ID")
106    private String responsibilityId = "1";
107    @Column(name="ROLE_NM")
108        private String roleName;
109    @Column(name="DLGN_PRNCPL_ID")
110        private String delegatorPrincipalId;
111    @Column(name="DLGN_GRP_ID")
112        private String delegatorGroupId;
113    @Column(name="DLGN_TYP")
114        private String delegationType;
115    @Column(name="RQST_LBL")
116    private String requestLabel;
117
118    // used by Document Operations screen
119    @Transient
120    private String dateAssignedStringValue;
121
122
123    //@PrePersist
124    public void beforeInsert(){
125        OrmUtils.populateAutoIncValue(this, KEWServiceLocator.getEntityManagerFactory().createEntityManager());
126    }
127
128    @Deprecated
129    @Override
130    public String getActionToTake() {
131        // deprecated, always return null (see the contract javadoc for more details)
132        return null;
133    }
134
135    @Deprecated
136    @Override
137    public String getDateAssignedString() {
138        // deprecated, always return null (see the contract javadoc for more details)
139        return null;
140    }
141
142    public String getDateAssignedStringValue() {
143        if (StringUtils.isBlank(dateAssignedStringValue)) {
144            return RiceConstants.getDefaultDateFormat().format(getDateAssigned());
145        }
146        return dateAssignedStringValue;
147    }
148
149    public void setDateAssignedStringValue(String dateAssignedStringValue) {
150        this.dateAssignedStringValue = dateAssignedStringValue;
151    }
152    
153    public String getId() {
154        return id;
155    }
156    
157    public String getPrincipalId() {
158        return principalId;
159    }
160    
161    public Timestamp getDateAssigned() {
162        return dateAssigned;
163    }
164
165    public DateTime getDateTimeAssigned() {
166        return new DateTime(dateAssigned);
167    }
168    
169    public String getActionRequestCd() {
170        return actionRequestCd;
171    }
172    
173    public String getActionRequestId() {
174        return actionRequestId;
175    }
176    
177    public String getDocumentId() {
178        return documentId;
179    }
180    
181    public String getGroupId() {
182        return groupId;
183    }
184
185    public String getDocTitle() {
186        return docTitle;
187    }
188    
189    public String getDocLabel() {
190        return docLabel;
191    }
192    
193    public String getDocHandlerURL() {
194        return docHandlerURL;
195    }
196    
197    public String getDocName() {
198        return docName;
199    }
200
201    public String getResponsibilityId() {
202        return responsibilityId;
203    }
204
205    public String getRoleName() {
206        return roleName;
207    }
208
209    public String getDelegatorPrincipalId() {
210        return delegatorPrincipalId;
211    }
212
213    public String getDelegatorGroupId() {
214        return delegatorGroupId;
215    }
216
217    public DelegationType getDelegationType() {
218        return DelegationType.fromCode(delegationType);
219    }
220
221    public String getRequestLabel() {
222        return this.requestLabel;
223    }
224
225    private Group getGroup(String groupId) {
226        if (StringUtils.isBlank(groupId)) {
227                return null;
228        }
229        return KimApiServiceLocator.getGroupService().getGroup(groupId);
230    }
231
232    public Group getGroup(){
233        return getGroup(groupId);
234    }
235
236    public String getRecipientTypeCode() {
237        String recipientTypeCode = RecipientType.PRINCIPAL.getCode();
238        if (getRoleName() != null) {
239            recipientTypeCode = RecipientType.ROLE.getCode();
240        }
241        if (getGroupId() != null) {
242            recipientTypeCode = RecipientType.GROUP.getCode();
243        }
244        return recipientTypeCode;
245    }
246    
247    public String getActionRequestLabel() {
248        if (StringUtils.isNotBlank(getRequestLabel())) {
249                return getRequestLabel();
250        }
251        return CodeTranslator.getActionRequestLabel(getActionRequestCd());
252    }
253
254    public boolean isWorkgroupItem() {
255        return getGroupId() != null;
256    }
257    
258    public Principal getPrincipal(){
259        return KimApiServiceLocator.getIdentityService().getPrincipal(principalId);
260    }
261
262    public void setResponsibilityId(String responsibilityId) {
263        this.responsibilityId = responsibilityId;
264    }
265    
266    public void setDocName(String docName) {
267        this.docName = docName;
268    }
269
270    public void setActionRequestCd(String actionRequestCd) {
271        this.actionRequestCd = actionRequestCd;
272    }
273
274    public void setDateAssigned(Timestamp dateAssigned) {
275        this.dateAssigned = dateAssigned;
276    }
277
278    public void setPrincipalId(String principalId) {
279        this.principalId = principalId;
280    }
281    
282    public void setDocumentId(String documentId) {
283        this.documentId = documentId;
284    }
285
286    public void setId(String id) {
287        this.id = id;
288    }
289
290    public void setActionRequestId(String actionRequestId) {
291        this.actionRequestId = actionRequestId;
292    }
293
294    public void setDocHandlerURL(String docHandlerURL) {
295        this.docHandlerURL = docHandlerURL;
296    }
297
298    public void setGroupId(String groupId) {
299        this.groupId = groupId;
300    }
301
302    public void setDocLabel(String docLabel) {
303        this.docLabel = docLabel;
304    }
305
306    public void setDocTitle(String docTitle) {
307        this.docTitle = docTitle;
308    }
309    
310    public void setRoleName(String roleName) {
311        this.roleName = roleName;
312    }
313    
314    public void setDelegatorPrincipalId(String delegatorPrincipalId) {
315        this.delegatorPrincipalId = delegatorPrincipalId;
316    }
317    
318    public void setDelegatorGroupId(String delegatorGroupId) {
319        this.delegatorGroupId = delegatorGroupId;
320    }
321
322    public void setDelegationType(DelegationType delegationType) {
323        this.delegationType = delegationType == null ? null : delegationType.getCode();
324    }
325    
326    public void setRequestLabel(String requestLabel) {
327        this.requestLabel = requestLabel;
328    }
329
330    @Deprecated
331    @Override
332    public Integer getActionItemIndex() {
333        // deprecated, always return null (see the contract javadoc for more details)
334        return null;
335    }
336
337    public String toString() {
338        return new ToStringBuilder(this).append("id", id)
339                                        .append("principalId", principalId)
340                                        .append("dateAssigned", dateAssigned)
341                                        .append("actionRequestCd", actionRequestCd)
342                                        .append("actionRequestId", actionRequestId)
343                                        .append("documentId", documentId)
344                                        .append("groupId", groupId)
345                                        .append("docTitle", docTitle)
346                                        .append("docLabel", docLabel)
347                                        .append("docHandlerURL", docHandlerURL)
348                                        .append("docName", docName)
349                                        .append("responsibilityId", responsibilityId)
350                                        .append("roleName", roleName)
351                                        .append("delegatorPrincipalId", delegatorPrincipalId)
352                                        .append("delegatorGroupId", delegatorGroupId)
353                                        .append("delegationType", delegationType)
354                                        .toString();
355    }
356
357    public static org.kuali.rice.kew.api.action.ActionItem to(ActionItem bo) {
358        if (bo == null) {
359            return null;
360        }
361        return org.kuali.rice.kew.api.action.ActionItem.Builder.create(bo).build();
362    }
363    
364    public static List<org.kuali.rice.kew.api.action.ActionItem> to(Collection<ActionItem> bos) {
365        if (bos == null)
366            return null;
367        if (bos.isEmpty()) 
368            return new ArrayList<org.kuali.rice.kew.api.action.ActionItem>();
369        
370        List<org.kuali.rice.kew.api.action.ActionItem> dtos = new ArrayList<org.kuali.rice.kew.api.action.ActionItem>(bos.size());
371        for (ActionItem bo : bos) {
372            dtos.add(ActionItem.to(bo));
373        }
374        return dtos;
375    }
376}