001/**
002 * Copyright 2005-2017 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.impl.document.search;
017
018import org.apache.commons.lang.StringUtils;
019import org.kuali.rice.kew.api.document.Document;
020import org.kuali.rice.kew.api.document.DocumentStatus;
021import org.kuali.rice.kew.api.document.search.DocumentSearchResult;
022import org.kuali.rice.kew.doctype.bo.DocumentType;
023import org.kuali.rice.kew.service.KEWServiceLocator;
024import org.kuali.rice.kim.api.group.Group;
025import org.kuali.rice.kim.api.identity.Person;
026import org.kuali.rice.kim.api.identity.name.EntityName;
027import org.kuali.rice.kim.api.identity.principal.EntityNamePrincipalName;
028import org.kuali.rice.kim.api.identity.principal.Principal;
029import org.kuali.rice.kim.api.services.KimApiServiceLocator;
030import org.kuali.rice.kim.impl.group.GroupBo;
031import org.kuali.rice.krad.bo.BusinessObject;
032
033import java.sql.Timestamp;
034
035/**
036 * Defines the business object that specifies the criteria used on document searches.
037 *
038 * @author Kuali Rice Team (rice.collab@kuali.org)
039 */
040public class DocumentSearchCriteriaBo implements BusinessObject {
041
042    private String documentTypeName;
043    private String documentId;
044    private String statusCode;
045    private String applicationDocumentId;
046    private String applicationDocumentStatus;
047    private String title;
048    private String initiatorPrincipalName;
049    private String initiatorPrincipalId;
050    private String viewerPrincipalName;
051    private String viewerPrincipalId;
052    private String groupViewerName;
053    private String groupViewerId;
054    private String approverPrincipalName;
055    private String approverPrincipalId;
056    private String routeNodeName;
057    private String routeNodeLogic;
058    private Timestamp dateCreated;
059    private Timestamp dateLastModified;
060    private Timestamp dateApproved;
061    private Timestamp dateFinalized;
062    private Timestamp dateApplicationDocumentStatusChanged;
063    private String saveName;
064
065    @Override
066    public void refresh() {
067        // nothing to refresh
068    }
069
070    public String getDocumentTypeName() {
071        return documentTypeName;
072    }
073
074    public void setDocumentTypeName(String documentTypeName) {
075        this.documentTypeName = documentTypeName;
076    }
077
078    public String getDocumentId() {
079        return documentId;
080    }
081
082    public void setDocumentId(String documentId) {
083        this.documentId = documentId;
084    }
085
086    public String getStatusCode() {
087        return statusCode;
088    }
089
090    public void setStatusCode(String statusCode) {
091        this.statusCode = statusCode;
092    }
093
094    public String getApplicationDocumentId() {
095        return applicationDocumentId;
096    }
097
098    public void setApplicationDocumentId(String applicationDocumentId) {
099        this.applicationDocumentId = applicationDocumentId;
100    }
101
102    public String getApplicationDocumentStatus() {
103        return applicationDocumentStatus;
104    }
105
106    public void setApplicationDocumentStatus(String applicationDocumentStatus) {
107        this.applicationDocumentStatus = applicationDocumentStatus;
108    }
109
110    public String getTitle() {
111        return title;
112    }
113
114    public void setTitle(String title) {
115        this.title = title;
116    }
117
118    public String getInitiatorPrincipalName() {
119        return initiatorPrincipalName;
120    }
121
122    public void setInitiatorPrincipalName(String initiatorPrincipalName) {
123        this.initiatorPrincipalName = initiatorPrincipalName;
124    }
125
126    public String getInitiatorPrincipalId() {
127        return initiatorPrincipalId;
128    }
129
130    public void setInitiatorPrincipalId(String initiatorPrincipalId) {
131        this.initiatorPrincipalId = initiatorPrincipalId;
132    }
133
134    public String getViewerPrincipalName() {
135        return viewerPrincipalName;
136    }
137
138    public void setViewerPrincipalName(String viewerPrincipalName) {
139        this.viewerPrincipalName = viewerPrincipalName;
140    }
141
142    public String getViewerPrincipalId() {
143        return viewerPrincipalId;
144    }
145
146    public void setViewerPrincipalId(String viewerPrincipalId) {
147        this.viewerPrincipalId = viewerPrincipalId;
148    }
149
150    public String getGroupViewerName() {
151        return groupViewerName;
152    }
153
154    public void setGroupViewerName(String groupViewerName) {
155        this.groupViewerName = groupViewerName;
156    }
157
158    public String getGroupViewerId() {
159        return groupViewerId;
160    }
161
162    public void setGroupViewerId(String groupViewerId) {
163        this.groupViewerId = groupViewerId;
164    }
165
166    public String getApproverPrincipalName() {
167        return approverPrincipalName;
168    }
169
170    public void setApproverPrincipalName(String approverPrincipalName) {
171        this.approverPrincipalName = approverPrincipalName;
172    }
173
174    public String getApproverPrincipalId() {
175        return approverPrincipalId;
176    }
177
178    public void setApproverPrincipalId(String approverPrincipalId) {
179        this.approverPrincipalId = approverPrincipalId;
180    }
181
182    public String getRouteNodeName() {
183        return routeNodeName;
184    }
185
186    public void setRouteNodeName(String routeNodeName) {
187        this.routeNodeName = routeNodeName;
188    }
189
190    public String getRouteNodeLogic() {
191        return routeNodeLogic;
192    }
193
194    public void setRouteNodeLogic(String routeNodeLogic) {
195        this.routeNodeLogic = routeNodeLogic;
196    }
197
198    public Timestamp getDateCreated() {
199        return dateCreated;
200    }
201
202    public void setDateCreated(Timestamp dateCreated) {
203        this.dateCreated = dateCreated;
204    }
205
206    public Timestamp getDateLastModified() {
207        return dateLastModified;
208    }
209
210    public void setDateLastModified(Timestamp dateLastModified) {
211        this.dateLastModified = dateLastModified;
212    }
213
214    public Timestamp getDateApproved() {
215        return dateApproved;
216    }
217
218    public void setDateApproved(Timestamp dateApproved) {
219        this.dateApproved = dateApproved;
220    }
221
222    public Timestamp getDateFinalized() {
223        return dateFinalized;
224    }
225
226    public void setDateFinalized(Timestamp dateFinalized) {
227        this.dateFinalized = dateFinalized;
228    }
229
230    public Timestamp getDateApplicationDocumentStatusChanged() {
231        return dateApplicationDocumentStatusChanged;
232    }
233
234    public void setDateApplicationDocumentStatusChanged(Timestamp dateApplicationDocumentStatusChanged) {
235        this.dateApplicationDocumentStatusChanged = dateApplicationDocumentStatusChanged;
236    }
237
238    public String getSaveName() {
239        return saveName;
240    }
241
242    public void setSaveName(String saveName) {
243        this.saveName = saveName;
244    }
245
246    public DocumentType getDocumentType() {
247        if (documentTypeName == null) {
248            return null;
249        }
250        return KEWServiceLocator.getDocumentTypeService().findByName(documentTypeName);
251    }
252
253    public Person getInitiatorPerson() {
254        if (initiatorPrincipalId == null) {
255            return null;
256        }
257        return KimApiServiceLocator.getPersonService().getPerson(initiatorPrincipalId);
258    }
259
260    /**
261     * Gets the initiators display name, if the  principal is not found the the initiator principal id is returned.
262     * @return The principal composite name if it exists, otherwise the initiator principal id
263     */
264    public String getInitiatorDisplayName() {
265
266        if (StringUtils.isNotBlank(initiatorPrincipalId)) {
267            EntityNamePrincipalName entityNamePrincipalName = KimApiServiceLocator.getIdentityService().getDefaultNamesForPrincipalId(initiatorPrincipalId);
268            if (entityNamePrincipalName != null){
269                EntityName entityName = entityNamePrincipalName.getDefaultName();
270                return entityName == null ? initiatorPrincipalId : entityName.getCompositeName();
271            }
272        }
273
274        return initiatorPrincipalId;
275    }
276
277    public Person getApproverPerson() {
278        if (approverPrincipalName == null) {
279            return null;
280        }
281        return KimApiServiceLocator.getPersonService().getPersonByPrincipalName(approverPrincipalName);
282    }
283
284    public Person getViewerPerson() {
285        if (viewerPrincipalName == null) {
286            return null;
287        }
288        return KimApiServiceLocator.getPersonService().getPersonByPrincipalName(viewerPrincipalName);
289    }
290
291    public GroupBo getGroupViewer() {
292        if (groupViewerId == null) {
293            return null;
294        }
295        Group grp = KimApiServiceLocator.getGroupService().getGroup(groupViewerId);
296        if (null != grp){
297            return GroupBo.from(grp);
298        }  else {
299            return null;
300        }
301    }
302
303    public String getStatusLabel() {
304        if (statusCode == null) {
305            return "";
306        }
307        return DocumentStatus.fromCode(statusCode).getLabel();
308    }
309
310    public String getDocumentTypeLabel() {
311        DocumentType documentType = getDocumentType();
312        if (documentType != null) {
313            return documentType.getLabel();
314        }
315        return "";
316    }
317
318    /**
319     * Returns the route image which can be used to construct the route log link in custom lookup helper code.
320     */
321    public String getRouteLog() {
322        return "<img alt=\"Route Log for Document\" src=\"images/my_route_log.gif\"/>";
323    }
324
325    public void populateFromDocumentSearchResult(DocumentSearchResult result) {
326        Document document = result.getDocument();
327        documentTypeName = document.getDocumentTypeName();
328        documentId = document.getDocumentId();
329        statusCode = document.getStatus().getCode();
330        applicationDocumentId = document.getApplicationDocumentId();
331        applicationDocumentStatus = document.getApplicationDocumentStatus();
332        title = document.getTitle();
333        initiatorPrincipalName = principalIdToName(document.getInitiatorPrincipalId());
334        initiatorPrincipalId = document.getInitiatorPrincipalId();
335        dateCreated = new Timestamp(document.getDateCreated().getMillis());
336    }
337
338    /**
339     * Returns the principal name for the given principal id, if the principal is not found then the principal id is
340     *  returned.
341     * @param principalId the unique identifier for the principal
342     * @return the principal name for the given principal id, if the principal is not found then the principal id is
343     *  returned.
344     */
345    private String principalIdToName(String principalId) {
346        if (StringUtils.isNotBlank(principalId)) {
347            Principal principal =  KimApiServiceLocator.getIdentityService().getPrincipal(principalId);
348            if (principal != null){
349                return principal.getPrincipalName();
350            }
351        }
352        return principalId;
353    }
354
355}
356