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.routeheader.service.impl;
017
018import java.math.BigDecimal;
019import java.sql.Timestamp;
020import java.util.ArrayList;
021import java.util.Collection;
022import java.util.HashMap;
023import java.util.List;
024import java.util.Map;
025import java.util.Set;
026
027import org.apache.commons.lang.StringUtils;
028import org.kuali.rice.kew.api.action.ActionItem;
029import org.kuali.rice.kew.docsearch.SearchableAttributeValue;
030import org.kuali.rice.kew.docsearch.dao.SearchableAttributeDAO;
031import org.kuali.rice.kew.doctype.bo.DocumentType;
032import org.kuali.rice.kew.exception.WorkflowServiceErrorException;
033import org.kuali.rice.kew.exception.WorkflowServiceErrorImpl;
034import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue;
035import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValueContent;
036import org.kuali.rice.kew.routeheader.dao.DocumentRouteHeaderDAO;
037import org.kuali.rice.kew.routeheader.service.RouteHeaderService;
038import org.kuali.rice.kew.service.KEWServiceLocator;
039import org.kuali.rice.kew.api.KewApiConstants;
040import org.kuali.rice.kim.api.identity.principal.Principal;
041import org.kuali.rice.kim.api.services.KimApiServiceLocator;
042
043
044
045public class RouteHeaderServiceImpl implements RouteHeaderService {
046
047    private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(RouteHeaderServiceImpl.class);
048
049    private DocumentRouteHeaderDAO routeHeaderDAO;
050    private SearchableAttributeDAO searchableAttributeDAO;
051
052    public DocumentRouteHeaderValue getRouteHeader(String documentId) {
053        return getRouteHeaderDAO().findRouteHeader(documentId);
054    }
055
056    public DocumentRouteHeaderValue getRouteHeader(String documentId, boolean clearCache) {
057        return getRouteHeaderDAO().findRouteHeader(documentId, clearCache);
058    }
059
060    public Collection<DocumentRouteHeaderValue> getRouteHeaders(Collection<String> documentIds) {
061        return getRouteHeaderDAO().findRouteHeaders(documentIds);
062    }
063    
064    public Collection<DocumentRouteHeaderValue> getRouteHeaders(Collection<String> documentIds, boolean clearCache) {
065        return getRouteHeaderDAO().findRouteHeaders(documentIds, clearCache);
066    }
067    
068    public Map<String,DocumentRouteHeaderValue> getRouteHeadersForActionItems(Collection<ActionItem> actionItems) {
069        Map<String,DocumentRouteHeaderValue> routeHeaders = new HashMap<String,DocumentRouteHeaderValue>();
070        List<String> documentIds = new ArrayList<String>(actionItems.size());
071        for (ActionItem actionItem : actionItems) {
072                documentIds.add(actionItem.getDocumentId());
073        }
074        Collection<DocumentRouteHeaderValue> actionItemRouteHeaders = getRouteHeaders(documentIds);
075        if (actionItemRouteHeaders != null) {
076                for (DocumentRouteHeaderValue routeHeader : actionItemRouteHeaders) {
077                        routeHeaders.put(routeHeader.getDocumentId(), routeHeader);
078                }
079        }
080        return routeHeaders;
081    }
082    
083    public void lockRouteHeader(String documentId, boolean wait) {
084        getRouteHeaderDAO().lockRouteHeader(documentId, wait);
085        LOG.debug("Successfully locked document [docId=" + documentId + "]");
086    }
087
088    public void saveRouteHeader(DocumentRouteHeaderValue routeHeader) {
089        this.getRouteHeaderDAO().saveRouteHeader(routeHeader);
090    }
091
092    public void deleteRouteHeader(DocumentRouteHeaderValue routeHeader) {
093        getRouteHeaderDAO().deleteRouteHeader(routeHeader);
094    }
095
096    public String getNextDocumentId() {
097        return getRouteHeaderDAO().getNextDocumentId();
098    }
099
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}