View Javadoc
1   /**
2    * Copyright 2005-2016 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.kew.routeheader.service.impl;
17  
18  import java.math.BigDecimal;
19  import java.sql.Timestamp;
20  import java.util.ArrayList;
21  import java.util.Collection;
22  import java.util.HashMap;
23  import java.util.List;
24  import java.util.Map;
25  import java.util.Set;
26  
27  import org.apache.commons.lang.StringUtils;
28  import org.kuali.rice.kew.api.action.ActionItem;
29  import org.kuali.rice.kew.docsearch.SearchableAttributeValue;
30  import org.kuali.rice.kew.docsearch.dao.SearchableAttributeDAO;
31  import org.kuali.rice.kew.doctype.bo.DocumentType;
32  import org.kuali.rice.kew.exception.WorkflowServiceErrorException;
33  import org.kuali.rice.kew.exception.WorkflowServiceErrorImpl;
34  import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue;
35  import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValueContent;
36  import org.kuali.rice.kew.routeheader.dao.DocumentRouteHeaderDAO;
37  import org.kuali.rice.kew.routeheader.service.RouteHeaderService;
38  import org.kuali.rice.kew.service.KEWServiceLocator;
39  import org.kuali.rice.kew.api.KewApiConstants;
40  import org.kuali.rice.kim.api.identity.principal.Principal;
41  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
42  
43  
44  
45  public class RouteHeaderServiceImpl implements RouteHeaderService {
46  
47      private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(RouteHeaderServiceImpl.class);
48  
49      private DocumentRouteHeaderDAO routeHeaderDAO;
50      private SearchableAttributeDAO searchableAttributeDAO;
51  
52      public DocumentRouteHeaderValue getRouteHeader(String documentId) {
53          return getRouteHeaderDAO().findRouteHeader(documentId);
54      }
55  
56      public DocumentRouteHeaderValue getRouteHeader(String documentId, boolean clearCache) {
57      	return getRouteHeaderDAO().findRouteHeader(documentId, clearCache);
58      }
59  
60      public Collection<DocumentRouteHeaderValue> getRouteHeaders(Collection<String> documentIds) {
61      	return getRouteHeaderDAO().findRouteHeaders(documentIds);
62      }
63      
64      public Collection<DocumentRouteHeaderValue> getRouteHeaders(Collection<String> documentIds, boolean clearCache) {
65      	return getRouteHeaderDAO().findRouteHeaders(documentIds, clearCache);
66      }
67      
68      public Map<String,DocumentRouteHeaderValue> getRouteHeadersForActionItems(Collection<ActionItem> actionItems) {
69      	Map<String,DocumentRouteHeaderValue> routeHeaders = new HashMap<String,DocumentRouteHeaderValue>();
70      	List<String> documentIds = new ArrayList<String>(actionItems.size());
71      	for (ActionItem actionItem : actionItems) {
72      		documentIds.add(actionItem.getDocumentId());
73      	}
74      	Collection<DocumentRouteHeaderValue> actionItemRouteHeaders = getRouteHeaders(documentIds);
75      	if (actionItemRouteHeaders != null) {
76      		for (DocumentRouteHeaderValue routeHeader : actionItemRouteHeaders) {
77      			routeHeaders.put(routeHeader.getDocumentId(), routeHeader);
78      		}
79      	}
80      	return routeHeaders;
81      }
82      
83      public void lockRouteHeader(String documentId, boolean wait) {
84          getRouteHeaderDAO().lockRouteHeader(documentId, wait);
85          LOG.debug("Successfully locked document [docId=" + documentId + "]");
86      }
87  
88      public void saveRouteHeader(DocumentRouteHeaderValue routeHeader) {
89          this.getRouteHeaderDAO().saveRouteHeader(routeHeader);
90      }
91  
92      public void deleteRouteHeader(DocumentRouteHeaderValue routeHeader) {
93          getRouteHeaderDAO().deleteRouteHeader(routeHeader);
94      }
95  
96      public String getNextDocumentId() {
97          return getRouteHeaderDAO().getNextDocumentId();
98      }
99  
100     public Collection findPendingByResponsibilityIds(Set responsibilityIds) {
101         return getRouteHeaderDAO().findPendingByResponsibilityIds(responsibilityIds);
102     }
103 
104     public void clearRouteHeaderSearchValues(String documentId) {
105         getRouteHeaderDAO().clearRouteHeaderSearchValues(documentId);
106     }
107     
108     public void updateRouteHeaderSearchValues(String documentId, List<SearchableAttributeValue> searchAttributes) {
109     	getRouteHeaderDAO().clearRouteHeaderSearchValues(documentId);
110     	for (SearchableAttributeValue searchAttribute : searchAttributes) {
111     		getRouteHeaderDAO().save(searchAttribute);
112     	}
113     }
114 
115     public void validateRouteHeader(DocumentRouteHeaderValue routeHeader){
116         LOG.debug("Enter validateRouteHeader(..)");
117         List errors = new ArrayList();
118 
119         if (routeHeader.getDocRouteStatus() == null || routeHeader.getDocRouteStatus().trim().equals("")) {
120             errors.add(new WorkflowServiceErrorImpl("RouteHeader route status null.", "routeheader.routestatus.empty"));
121         } else if (!KewApiConstants.DOCUMENT_STATUSES.containsKey(routeHeader.getDocRouteStatus())){
122             errors.add(new WorkflowServiceErrorImpl("RouteHeader route status invalid.", "routeheader.routestatus.invalid"));
123         }
124 
125         if(routeHeader.getDocRouteLevel() == null || routeHeader.getDocRouteLevel().intValue() < 0){
126             errors.add(new WorkflowServiceErrorImpl("RouteHeader route level invalid.", "routeheader.routelevel.invalid"));
127         }
128 
129         if(routeHeader.getDateLastModified() == null){
130             errors.add(new WorkflowServiceErrorImpl("RouteHeader status modification date empty.", "routeheader.statusmoddate.empty"));
131         }
132 
133         if(routeHeader.getCreateDate() == null){
134             errors.add(new WorkflowServiceErrorImpl("RouteHeader status create date empty.", "routeheader.createdate.empty"));
135         }
136         if(routeHeader.getDocVersion() == null || routeHeader.getDocVersion().intValue() < 0){
137             errors.add(new WorkflowServiceErrorImpl("RouteHeader doc version invalid.", "routeheader.docversion.invalid"));
138         }
139 
140         if (routeHeader.getInitiatorWorkflowId () == null || routeHeader.getInitiatorWorkflowId().trim().equals("")) {
141             errors.add(new WorkflowServiceErrorImpl("RouteHeader initiator null.", "routeheader.initiator.empty"));
142         }
143         else
144         {
145            	Principal principal = KimApiServiceLocator.getIdentityService().getPrincipal(routeHeader.getInitiatorWorkflowId());
146             if(principal == null)
147             {
148                	errors.add(new WorkflowServiceErrorImpl("RouteHeader initiator id invalid.", "routeheader.initiator.invalid"));
149             }
150         }
151 
152         if(!StringUtils.isBlank(routeHeader.getDocumentTypeId())){
153             DocumentType docType = KEWServiceLocator.getDocumentTypeService().findById(routeHeader.getDocumentTypeId());
154             if(docType == null){
155                 errors.add(new WorkflowServiceErrorImpl("RouteHeader document type id invalid.", "routeheader.doctypeid.invalid"));
156             }
157         }
158 
159         LOG.debug("Exit validateRouteHeader(..) ");
160         if (!errors.isEmpty()) {
161             throw new WorkflowServiceErrorException("RouteHeader Validation Error", errors);
162         }
163     }
164 
165     public String getApplicationIdByDocumentId(String documentId) {
166     	return getRouteHeaderDAO().getApplicationIdByDocumentId(documentId);
167     }
168 
169     public DocumentRouteHeaderValueContent getContent(String documentId) {
170     	if (documentId == null) {
171     		return new DocumentRouteHeaderValueContent();
172     	}
173     	DocumentRouteHeaderValueContent content = getRouteHeaderDAO().getContent(documentId);
174     	if (content == null) {
175     		content = new DocumentRouteHeaderValueContent(documentId);
176     	}
177     	return content;
178     }
179 
180     public boolean hasSearchableAttributeValue(String documentId, String searchableAttributeKey, String searchableAttributeValue) {
181 	return getRouteHeaderDAO().hasSearchableAttributeValue(documentId, searchableAttributeKey, searchableAttributeValue);
182     }
183 
184     public String getDocumentStatus(String documentId) {
185     	return getRouteHeaderDAO().getDocumentStatus(documentId);
186     }
187     
188     public String getAppDocId(String documentId) {
189  	 	if (documentId == null) {
190  	 		return null;
191  	 	}
192  	 	return getRouteHeaderDAO().getAppDocId(documentId);
193     }
194 
195     public String getAppDocStatus(String documentId) {
196         if (documentId == null) {
197             return null;
198         }
199         return getRouteHeaderDAO().getAppDocStatus(documentId);
200     }
201 
202     public DocumentRouteHeaderDAO getRouteHeaderDAO() {
203         return routeHeaderDAO;
204     }
205 
206     public void setRouteHeaderDAO(DocumentRouteHeaderDAO routeHeaderDAO) {
207         this.routeHeaderDAO = routeHeaderDAO;
208     }
209 
210 	public List<Timestamp> getSearchableAttributeDateTimeValuesByKey(
211 			String documentId, String key) {
212 		return getSearchableAttributeDAO().getSearchableAttributeDateTimeValuesByKey(documentId, key);
213 	}
214 
215 	public List<BigDecimal> getSearchableAttributeFloatValuesByKey(
216 			String documentId, String key) {
217 		return getSearchableAttributeDAO().getSearchableAttributeFloatValuesByKey(documentId, key);
218 	}
219 
220 	public List<Long> getSearchableAttributeLongValuesByKey(String documentId,
221 			String key) {
222 		return getSearchableAttributeDAO().getSearchableAttributeLongValuesByKey(documentId, key);
223 	}
224 
225 	public List<String> getSearchableAttributeStringValuesByKey(
226 			String documentId, String key) {
227 
228 		return getSearchableAttributeDAO().getSearchableAttributeStringValuesByKey(documentId, key);
229 	}
230 
231 	public void setSearchableAttributeDAO(SearchableAttributeDAO searchableAttributeDAO) {
232 		this.searchableAttributeDAO = searchableAttributeDAO;
233 	}
234 
235 	public SearchableAttributeDAO getSearchableAttributeDAO() {
236 		return searchableAttributeDAO;
237 	}
238 
239 	public Collection findByDocTypeAndAppId(String documentTypeName,
240 			String appId) {
241 		return getRouteHeaderDAO().findByDocTypeAndAppId(documentTypeName, appId);
242 	}
243 }