001    /**
002     * Copyright 2005-2012 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     */
016    package org.kuali.rice.kew.rule.service.impl;
017    
018    import java.io.InputStream;
019    import java.util.ArrayList;
020    import java.util.Collection;
021    import java.util.List;
022    
023    import org.jdom.Element;
024    import org.kuali.rice.core.api.impex.ExportDataSet;
025    import org.kuali.rice.kew.exception.WorkflowServiceErrorException;
026    import org.kuali.rice.kew.exception.WorkflowServiceErrorImpl;
027    import org.kuali.rice.kew.rule.RuleBaseValues;
028    import org.kuali.rice.kew.rule.RuleDelegationBo;
029    import org.kuali.rice.kew.rule.RuleTemplateOptionBo;
030    import org.kuali.rice.kew.rule.bo.RuleTemplateBo;
031    import org.kuali.rice.kew.rule.bo.RuleTemplateAttributeBo;
032    import org.kuali.rice.kew.rule.dao.RuleDAO;
033    import org.kuali.rice.kew.rule.dao.RuleDelegationDAO;
034    import org.kuali.rice.kew.rule.dao.RuleTemplateAttributeDAO;
035    import org.kuali.rice.kew.rule.dao.RuleTemplateDAO;
036    import org.kuali.rice.kew.rule.dao.RuleTemplateOptionDAO;
037    import org.kuali.rice.kew.rule.service.RuleAttributeService;
038    import org.kuali.rice.kew.rule.service.RuleTemplateService;
039    import org.kuali.rice.kew.service.KEWServiceLocator;
040    import org.kuali.rice.kew.xml.RuleTemplateXmlParser;
041    import org.kuali.rice.kew.xml.export.RuleTemplateXmlExporter;
042    
043    
044    public class RuleTemplateServiceImpl implements RuleTemplateService {
045    
046        private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(RuleTemplateServiceImpl.class);
047    
048        private static final String RULE_TEMPLATE_NAME_REQUIRED = "rule.template.name.required";
049    
050        private static final String RULE_TEMPLATE_DESCRIPTION_REQUIRED = "rule.template.description.required";
051    
052        private static final String XML_PARSE_ERROR = "general.error.parsexml";
053    
054        private RuleTemplateDAO ruleTemplateDAO;
055    
056        private RuleTemplateAttributeDAO ruleTemplateAttributeDAO;
057    
058        private RuleTemplateOptionDAO ruleTemplateOptionDAO;
059    
060        private RuleDAO ruleDAO;
061    
062        private RuleDelegationDAO ruleDelegationDAO;
063    
064        /*
065         * (non-Javadoc)
066         *
067         * @see org.kuali.rice.kew.rule.RuleTemplateAttributeService#delete(java.lang.Long)
068         */
069    //    public void deleteRuleTemplateAttribute(Long ruleTemplateAttributeId, List ruleTemplateAttributes) {
070    //
071    //        RuleTemplateAttribute ruleTemplateAttributeRemove = findByRuleTemplateAttributeId(ruleTemplateAttributeId);
072    //
073    //        for (int i = ruleTemplateAttributeRemove.getDisplayOrder().intValue() + 1; i <= ruleTemplateAttributes.size(); i++) {
074    //            RuleTemplateAttribute ruleTemplateAttributeUpdate = (RuleTemplateAttribute) ruleTemplateAttributes.get(i - 1);
075    //            ruleTemplateAttributeUpdate.setDisplayOrder(new Integer(i - 1));
076    //            getRuleTemplateAttributeDAO().save(ruleTemplateAttributeUpdate);
077    //        }
078    //        getRuleTemplateAttributeDAO().delete(ruleTemplateAttributeId);
079    //    }
080    
081        public void deleteRuleTemplateOption(String ruleTemplateOptionId) {
082            getRuleTemplateOptionDAO().delete(ruleTemplateOptionId);
083        }
084    
085        public RuleTemplateBo findByRuleTemplateName(String ruleTemplateName) {
086            return (getRuleTemplateDAO().findByRuleTemplateName(ruleTemplateName));
087        }
088    
089        /*
090         * (non-Javadoc)
091         *
092         * @see org.kuali.rice.kew.rule.RuleTemplateAttributeService#findByRuleTemplateAttributeId(java.lang.Long)
093         */
094        public RuleTemplateAttributeBo findByRuleTemplateAttributeId(String ruleTemplateAttributeId) {
095            return getRuleTemplateAttributeDAO().findByRuleTemplateAttributeId(ruleTemplateAttributeId);
096        }
097    
098        public List<RuleTemplateBo> findAll() {
099            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    }