View Javadoc

1   /**
2    * Copyright 2005-2014 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.rule.service.impl;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.jdom.Element;
20  import org.kuali.rice.core.api.impex.ExportDataSet;
21  import org.kuali.rice.kew.exception.WorkflowServiceErrorException;
22  import org.kuali.rice.kew.exception.WorkflowServiceErrorImpl;
23  import org.kuali.rice.kew.rule.RuleDelegationBo;
24  import org.kuali.rice.kew.rule.bo.RuleTemplateBo;
25  import org.kuali.rice.kew.rule.dao.RuleDelegationDAO;
26  import org.kuali.rice.kew.rule.service.RuleDelegationService;
27  import org.kuali.rice.kew.rule.service.RuleTemplateService;
28  import org.kuali.rice.kew.service.KEWServiceLocator;
29  import org.kuali.rice.kew.xml.RuleXmlParser;
30  import org.kuali.rice.kew.xml.export.RuleDelegationXmlExporter;
31  import org.kuali.rice.kim.api.group.Group;
32  import org.kuali.rice.kim.api.group.GroupService;
33  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
34  
35  import java.io.InputStream;
36  import java.util.ArrayList;
37  import java.util.Collection;
38  import java.util.Collections;
39  import java.util.List;
40  import java.util.Map;
41  
42  
43  /**
44   * @author Kuali Rice Team (rice.collab@kuali.org)
45   */
46  public class RuleDelegationServiceImpl implements RuleDelegationService {
47  	private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger
48  			.getLogger(RuleDelegationServiceImpl.class);
49  	
50  	private static final String XML_PARSE_ERROR = "general.error.parsexml";
51  	
52      private RuleDelegationDAO dao;
53  
54      public List<RuleDelegationBo> findByDelegateRuleId(String ruleId) {
55          if (ruleId == null) return Collections.EMPTY_LIST;
56          return dao.findByDelegateRuleId(ruleId);
57      }
58  
59      public void save(RuleDelegationBo ruleDelegation) {
60          dao.save(ruleDelegation);
61      }
62  
63      public void setRuleDelegationDAO(RuleDelegationDAO dao) {
64          this.dao = dao;
65      }
66      public List<RuleDelegationBo> findAllCurrentRuleDelegations(){
67          return dao.findAllCurrentRuleDelegations();
68      }
69      public void delete(String ruleDelegationId){
70          dao.delete(ruleDelegationId);
71      }
72  
73      public RuleDelegationBo findByRuleDelegationId(String ruleDelegationId){
74          return dao.findByRuleDelegationId(ruleDelegationId);
75      }
76  
77      public List<RuleDelegationBo> findByResponsibilityId(String responsibilityId) {
78      	//return dao.findByResponsibilityIdWithCurrentRule(responsibilityId);
79      	return findByResponsibilityId(responsibilityId, false);
80      }
81  
82      public List<RuleDelegationBo> search(String parentRuleBaseVaueId, String parentResponsibilityId,  String docTypeName, String ruleId, String ruleTemplateId, String ruleDescription, String groupId, String principalId,
83              String delegationType, Boolean activeInd, Map extensionValues, String workflowIdDirective) {
84          return dao.search(parentRuleBaseVaueId, parentResponsibilityId, docTypeName, ruleId, ruleTemplateId, ruleDescription, groupId, principalId, delegationType,
85                  activeInd, extensionValues, workflowIdDirective);
86      }
87  
88      public List<RuleDelegationBo> searchByTemplate(String parentRuleBaseVaueId, String parentResponsibilityId,  String docTypeName, String ruleTemplateName, String ruleDescription, String groupId, String principalId,
89              Boolean workgroupMember, String delegationType, Boolean activeInd, Map extensionValues, Collection<String> actionRequestCodes) {
90  
91          if ( (StringUtils.isEmpty(docTypeName)) &&
92                  (StringUtils.isEmpty(ruleTemplateName)) &&
93                  (StringUtils.isEmpty(ruleDescription)) &&
94                  (StringUtils.isEmpty(groupId)) &&
95                  (StringUtils.isEmpty(principalId)) &&
96                  (extensionValues.isEmpty()) &&
97                  (actionRequestCodes.isEmpty()) ) {
98              // all fields are empty
99              throw new IllegalArgumentException("At least one criterion must be sent");
100         }
101 
102         RuleTemplateBo ruleTemplate = getRuleTemplateService().findByRuleTemplateName(ruleTemplateName);
103         String ruleTemplateId = null;
104         if (ruleTemplate != null) {
105             ruleTemplateId = ruleTemplate.getId();
106         }
107 
108         if ( ( (extensionValues != null) && (!extensionValues.isEmpty()) ) &&
109                 (ruleTemplateId == null) ) {
110             // cannot have extensions without a correct template
111             throw new IllegalArgumentException("A Rule Template Name must be given if using Rule Extension values");
112         }
113 
114         Collection<String> workgroupIds = new ArrayList<String>();
115         if (principalId != null) {
116             if ( (workgroupMember == null) || (workgroupMember.booleanValue()) ) {
117                 workgroupIds = getGroupService().getGroupIdsByPrincipalId(principalId);
118             } else {
119                 // user was passed but workgroups should not be parsed... do nothing
120             }
121         } else if (groupId != null) {
122             Group group = KEWServiceLocator.getIdentityHelperService().getGroup(groupId);
123             if (group == null) {
124                 throw new IllegalArgumentException("Group does not exist in for given group id: " + groupId);
125             } else  {
126                 workgroupIds.add(group.getId());
127             }
128         }
129 
130         return dao.search(parentRuleBaseVaueId, parentResponsibilityId, docTypeName, ruleTemplateId, ruleDescription, workgroupIds, principalId,
131                 delegationType,activeInd, extensionValues, actionRequestCodes);
132     }
133     
134     public void loadXml(InputStream inputStream, String principalId) {
135     	RuleXmlParser parser = new RuleXmlParser();
136         try {
137             parser.parseRuleDelegations(inputStream);
138         } catch (Exception e) { //any other exception
139             LOG.error("Error loading xml file", e);
140             WorkflowServiceErrorException wsee = new WorkflowServiceErrorException("Error loading xml file", new WorkflowServiceErrorImpl("Error loading xml file", XML_PARSE_ERROR));
141             wsee.initCause(e);
142             throw wsee;
143         }
144 	}
145 
146 	public Element export(ExportDataSet dataSet) {
147 		RuleDelegationXmlExporter exporter = new RuleDelegationXmlExporter();
148 		return exporter.export(dataSet);
149 	}
150 	
151 	@Override
152 	public boolean supportPrettyPrint() {
153 		return true;
154 	}
155 
156 	private GroupService getGroupService() {
157         return KimApiServiceLocator.getGroupService();
158     }
159 
160     private RuleTemplateService getRuleTemplateService() {
161         return KEWServiceLocator.getRuleTemplateService();
162     }
163     
164 
165     
166     public List findByResponsibilityId(String responsibilityId, boolean ignoreCache) {
167    		return dao.findByResponsibilityIdWithCurrentRule(responsibilityId);
168     }
169 }