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.rule.service.impl;
18  
19  import java.io.InputStream;
20  import java.util.ArrayList;
21  import java.util.Collection;
22  import java.util.List;
23  
24  import org.jdom.Element;
25  import org.kuali.rice.kew.exception.WorkflowServiceErrorException;
26  import org.kuali.rice.kew.exception.WorkflowServiceErrorImpl;
27  import org.kuali.rice.kew.export.ExportDataSet;
28  import org.kuali.rice.kew.rule.RuleBaseValues;
29  import org.kuali.rice.kew.rule.RuleDelegation;
30  import org.kuali.rice.kew.rule.RuleTemplateOption;
31  import org.kuali.rice.kew.rule.bo.RuleTemplate;
32  import org.kuali.rice.kew.rule.bo.RuleTemplateAttribute;
33  import org.kuali.rice.kew.rule.dao.RuleDAO;
34  import org.kuali.rice.kew.rule.dao.RuleDelegationDAO;
35  import org.kuali.rice.kew.rule.dao.RuleTemplateAttributeDAO;
36  import org.kuali.rice.kew.rule.dao.RuleTemplateDAO;
37  import org.kuali.rice.kew.rule.dao.RuleTemplateOptionDAO;
38  import org.kuali.rice.kew.rule.service.RuleAttributeService;
39  import org.kuali.rice.kew.rule.service.RuleTemplateService;
40  import org.kuali.rice.kew.service.KEWServiceLocator;
41  import org.kuali.rice.kew.xml.RuleTemplateXmlParser;
42  import org.kuali.rice.kew.xml.export.RuleTemplateXmlExporter;
43  
44  
45  public class RuleTemplateServiceImpl implements RuleTemplateService {
46  
47      private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(RuleTemplateServiceImpl.class);
48  
49      private static final String RULE_TEMPLATE_NAME_REQUIRED = "rule.template.name.required";
50  
51      private static final String RULE_TEMPLATE_DESCRIPTION_REQUIRED = "rule.template.description.required";
52  
53      private static final String XML_PARSE_ERROR = "general.error.parsexml";
54  
55      private RuleTemplateDAO ruleTemplateDAO;
56  
57      private RuleTemplateAttributeDAO ruleTemplateAttributeDAO;
58  
59      private RuleTemplateOptionDAO ruleTemplateOptionDAO;
60  
61      private RuleDAO ruleDAO;
62  
63      private RuleDelegationDAO ruleDelegationDAO;
64  
65      /*
66       * (non-Javadoc)
67       *
68       * @see org.kuali.rice.kew.rule.RuleTemplateAttributeService#delete(java.lang.Long)
69       */
70  //    public void deleteRuleTemplateAttribute(Long ruleTemplateAttributeId, List ruleTemplateAttributes) {
71  //
72  //        RuleTemplateAttribute ruleTemplateAttributeRemove = findByRuleTemplateAttributeId(ruleTemplateAttributeId);
73  //
74  //        for (int i = ruleTemplateAttributeRemove.getDisplayOrder().intValue() + 1; i <= ruleTemplateAttributes.size(); i++) {
75  //            RuleTemplateAttribute ruleTemplateAttributeUpdate = (RuleTemplateAttribute) ruleTemplateAttributes.get(i - 1);
76  //            ruleTemplateAttributeUpdate.setDisplayOrder(new Integer(i - 1));
77  //            getRuleTemplateAttributeDAO().save(ruleTemplateAttributeUpdate);
78  //        }
79  //        getRuleTemplateAttributeDAO().delete(ruleTemplateAttributeId);
80  //    }
81  
82      public void deleteRuleTemplateOption(Long ruleTemplateOptionId) {
83          getRuleTemplateOptionDAO().delete(ruleTemplateOptionId);
84      }
85  
86      public RuleTemplate findByRuleTemplateName(String ruleTemplateName) {
87          return (getRuleTemplateDAO().findByRuleTemplateName(ruleTemplateName));
88      }
89  
90      /*
91       * (non-Javadoc)
92       *
93       * @see org.kuali.rice.kew.rule.RuleTemplateAttributeService#findByRuleTemplateAttributeId(java.lang.Long)
94       */
95      public RuleTemplateAttribute findByRuleTemplateAttributeId(Long ruleTemplateAttributeId) {
96          return getRuleTemplateAttributeDAO().findByRuleTemplateAttributeId(ruleTemplateAttributeId);
97      }
98  
99      public List findAll() {
100         return ruleTemplateDAO.findAll();
101     }
102 
103     public List findByRuleTemplate(RuleTemplate ruleTemplate) {
104         return ruleTemplateDAO.findByRuleTemplate(ruleTemplate);
105     }
106 
107     public void save(RuleTemplate ruleTemplate) {
108         LOG.debug("save RuleTemplateServiceImpl");
109         validate(ruleTemplate);
110         fixAssociations(ruleTemplate);
111 //        if (ruleTemplate.getRuleTemplateId() != null) {
112 //            RuleTemplate previousRuleTemplate = findByRuleTemplateId(ruleTemplate.getRuleTemplateId());
113 //            if (previousRuleTemplate != null) {
114 //                for (Iterator iter = previousRuleTemplate.getRuleTemplateAttributes().iterator(); iter.hasNext();) {
115 //                    RuleTemplateAttribute previousAttribute = (RuleTemplateAttribute) iter.next();
116 //                    boolean found = false;
117 //
118 //                    for (Iterator iter2 = ruleTemplate.getRuleTemplateAttributes().iterator(); iter2.hasNext();) {
119 //                        RuleTemplateAttribute attribute = (RuleTemplateAttribute) iter2.next();
120 //                        if (previousAttribute.getRuleAttribute().getName().equals(attribute.getRuleAttribute().getName())) {
121 //                            found = true;
122 //                            break;
123 //                        }
124 //                    }
125 //                    if (!found) {
126 //                        getRuleTemplateAttributeDAO().delete(previousAttribute.getRuleTemplateAttributeId());
127 //                    }
128 //                }
129 //            }
130 //        }
131 
132         getRuleTemplateDAO().save(ruleTemplate);
133         LOG.debug("end save RuleTemplateServiceImpl");
134     }
135 
136     public void save(RuleTemplateAttribute ruleTemplateAttribute) {
137         ruleTemplateAttributeDAO.save(ruleTemplateAttribute);
138     }
139 
140     /**
141      * Saves the given RuleDelegation and RuleBaseValues as the defaults for this RuleTemplate
142      */
143     public void saveRuleDefaults(RuleDelegation ruleDelegation, RuleBaseValues ruleBaseValues) {
144         KEWServiceLocator.getRuleService().saveRule(ruleBaseValues, false);
145         if (ruleDelegation != null) {
146         	KEWServiceLocator.getRuleService().saveRule(ruleDelegation.getDelegationRuleBaseValues(), false);
147             KEWServiceLocator.getRuleDelegationService().save(ruleDelegation);
148         }
149     }
150 
151     /**
152      * Ensures that dependent objects have a reference to the specified rule template
153      * @param ruleTemplate the rule template whose associates to check
154      */
155     private void fixAssociations(RuleTemplate ruleTemplate) {
156         // if it's a valid rule template instance
157         if (ruleTemplate != null && ruleTemplate.getRuleTemplateId() != null) {
158             // for every rule template attribute
159             for (RuleTemplateAttribute ruleTemplateAttribute: ruleTemplate.getRuleTemplateAttributes()) {
160                 // if the rule template is not set on the attribute, set it
161                 if (ruleTemplateAttribute.getRuleTemplate() == null || ruleTemplateAttribute.getRuleTemplateId() == null) {
162                     ruleTemplateAttribute.setRuleTemplate(ruleTemplate);
163                 }
164                 // if the rule attribute is set, load up the rule attribute and set the BO on the ruletemplateattribute association object
165                 if (ruleTemplateAttribute.getRuleAttribute() == null) {
166                     RuleAttributeService ruleAttributeService = (RuleAttributeService) KEWServiceLocator.getService(KEWServiceLocator.RULE_ATTRIBUTE_SERVICE);
167                     ruleTemplateAttribute.setRuleAttribute(ruleAttributeService.findByRuleAttributeId(ruleTemplateAttribute.getRuleAttributeId()));
168                 }
169             }
170             // for every rule template option
171             for (RuleTemplateOption option: ruleTemplate.getRuleTemplateOptions()) {
172                 // if the rule template is not set on the option, set it
173                 if (option.getRuleTemplate() == null || option.getRuleTemplateId() == null) {
174                     option.setRuleTemplate(ruleTemplate);
175                 }
176             }
177         }
178     }
179 
180     private void validate(RuleTemplate ruleTemplate) {
181         LOG.debug("validating ruleTemplate");
182         Collection errors = new ArrayList();
183         if (ruleTemplate.getName() == null || ruleTemplate.getName().trim().equals("")) {
184             errors.add(new WorkflowServiceErrorImpl("Please enter a rule template name.", RULE_TEMPLATE_NAME_REQUIRED));
185             LOG.error("Rule template name is missing");
186         } else {
187             ruleTemplate.setName(ruleTemplate.getName().trim());
188             if (ruleTemplate.getRuleTemplateId() == null) {
189                 RuleTemplate nameInUse = findByRuleTemplateName(ruleTemplate.getName());
190                 if (nameInUse != null) {
191                     errors.add(new WorkflowServiceErrorImpl("Rule template name already in use", "rule.template.name.duplicate"));
192                     LOG.error("Rule template name already in use");
193                 }
194             }
195         }
196         if (ruleTemplate.getDescription() == null || ruleTemplate.getDescription().equals("")) {
197             errors.add(new WorkflowServiceErrorImpl("Please enter a rule template description.", RULE_TEMPLATE_DESCRIPTION_REQUIRED));
198             LOG.error("Rule template description is missing");
199         }
200         //        if (ruleTemplate.getRuleTemplateAttributes() == null ||
201         // ruleTemplate.getRuleTemplateAttributes().isEmpty()) {
202         //            errors.add(new WorkflowServiceErrorImpl("Please select at least one a
203         // rule template attribute.", RULE_TEMPLATE_ATTRIBUTE_REQUIRED));
204         //        }
205 
206         LOG.debug("end validating ruleTemplate");
207         if (!errors.isEmpty()) {
208             throw new WorkflowServiceErrorException("RuleTemplate Validation Error", errors);
209         }
210     }
211 
212     public RuleTemplate findByRuleTemplateId(Long ruleTemplateId) {
213         LOG.debug("findByRuleTemplateId RuleTemplateServiceImpl");
214         return getRuleTemplateDAO().findByRuleTemplateId(ruleTemplateId);
215     }
216 
217     public void delete(Long ruleTemplateId) {
218         LOG.debug("delete RuleTemplateServiceImpl");
219         getRuleTemplateDAO().delete(ruleTemplateId);
220         LOG.debug("end delete RuleTemplateServiceImpl");
221     }
222 
223     public RuleTemplateDAO getRuleTemplateDAO() {
224         return ruleTemplateDAO;
225     }
226 
227     public void setRuleTemplateDAO(RuleTemplateDAO ruleTemplateDAO) {
228         this.ruleTemplateDAO = ruleTemplateDAO;
229     }
230 
231     /**
232      * @return Returns the ruleTemplateAttributeDAO.
233      */
234     public RuleTemplateAttributeDAO getRuleTemplateAttributeDAO() {
235         return ruleTemplateAttributeDAO;
236     }
237 
238     /**
239      * @param ruleTemplateAttributeDAO
240      *            The ruleTemplateAttributeDAO to set.
241      */
242     public void setRuleTemplateAttributeDAO(RuleTemplateAttributeDAO ruleTemplateAttributeDAO) {
243         this.ruleTemplateAttributeDAO = ruleTemplateAttributeDAO;
244     }
245 
246     public RuleDAO getRuleDAO() {
247         return ruleDAO;
248     }
249 
250     public void setRuleDAO(RuleDAO ruleDAO) {
251         this.ruleDAO = ruleDAO;
252     }
253 
254     public RuleDelegationDAO getRuleDelegationDAO() {
255         return ruleDelegationDAO;
256     }
257 
258     public void setRuleDelegationDAO(RuleDelegationDAO ruleDelegationDAO) {
259         this.ruleDelegationDAO = ruleDelegationDAO;
260     }
261 
262     /**
263      * @return Returns the ruleTemplateOptionDAO.
264      */
265     public RuleTemplateOptionDAO getRuleTemplateOptionDAO() {
266         return ruleTemplateOptionDAO;
267     }
268 
269     /**
270      * @param ruleTemplateOptionDAO
271      *            The ruleTemplateOptionDAO to set.
272      */
273     public void setRuleTemplateOptionDAO(RuleTemplateOptionDAO ruleTemplateOptionDAO) {
274         this.ruleTemplateOptionDAO = ruleTemplateOptionDAO;
275     }
276 
277     public void loadXml(InputStream inputStream, String principalId) {
278         RuleTemplateXmlParser parser = new RuleTemplateXmlParser();
279         try {
280             parser.parseRuleTemplates(inputStream);
281         } catch (Exception e) { //any other exception
282             LOG.error("Error loading xml file", e);
283             WorkflowServiceErrorException wsee = new WorkflowServiceErrorException("Error loading xml file", new WorkflowServiceErrorImpl("Error loading xml file", XML_PARSE_ERROR));
284             wsee.initCause(e);
285             throw wsee;
286         }
287     }
288 
289     public Element export(ExportDataSet dataSet) {
290         RuleTemplateXmlExporter exporter = new RuleTemplateXmlExporter();
291         return exporter.export(dataSet);
292     }
293 
294     public Long getNextRuleTemplateId() {
295         return getRuleTemplateDAO().getNextRuleTemplateId();
296     }
297 
298 }