View Javadoc

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