View Javadoc

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