001    /*
002     * Copyright 2005-2008 The Kuali Foundation
003     *
004     *
005     * Licensed under the Educational Community License, Version 2.0 (the "License");
006     * you may not use this file except in compliance with the License.
007     * You may obtain a copy of the License at
008     *
009     * http://www.opensource.org/licenses/ecl2.php
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.kuali.rice.kew.rule.service.impl;
018    
019    import java.io.InputStream;
020    import java.util.ArrayList;
021    import java.util.Collection;
022    import java.util.List;
023    
024    import org.jdom.Element;
025    import org.kuali.rice.core.api.impex.ExportDataSet;
026    import org.kuali.rice.kew.exception.WorkflowServiceErrorException;
027    import org.kuali.rice.kew.exception.WorkflowServiceErrorImpl;
028    import org.kuali.rice.kew.rule.RuleBaseValues;
029    import org.kuali.rice.kew.rule.RuleDelegation;
030    import org.kuali.rice.kew.rule.RuleTemplateOption;
031    import org.kuali.rice.kew.rule.bo.RuleTemplate;
032    import org.kuali.rice.kew.rule.bo.RuleTemplateAttribute;
033    import org.kuali.rice.kew.rule.dao.RuleDAO;
034    import org.kuali.rice.kew.rule.dao.RuleDelegationDAO;
035    import org.kuali.rice.kew.rule.dao.RuleTemplateAttributeDAO;
036    import org.kuali.rice.kew.rule.dao.RuleTemplateDAO;
037    import org.kuali.rice.kew.rule.dao.RuleTemplateOptionDAO;
038    import org.kuali.rice.kew.rule.service.RuleAttributeService;
039    import org.kuali.rice.kew.rule.service.RuleTemplateService;
040    import org.kuali.rice.kew.service.KEWServiceLocator;
041    import org.kuali.rice.kew.xml.RuleTemplateXmlParser;
042    import org.kuali.rice.kew.xml.export.RuleTemplateXmlExporter;
043    
044    
045    public class RuleTemplateServiceImpl implements RuleTemplateService {
046    
047        private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(RuleTemplateServiceImpl.class);
048    
049        private static final String RULE_TEMPLATE_NAME_REQUIRED = "rule.template.name.required";
050    
051        private static final String RULE_TEMPLATE_DESCRIPTION_REQUIRED = "rule.template.description.required";
052    
053        private static final String XML_PARSE_ERROR = "general.error.parsexml";
054    
055        private RuleTemplateDAO ruleTemplateDAO;
056    
057        private RuleTemplateAttributeDAO ruleTemplateAttributeDAO;
058    
059        private RuleTemplateOptionDAO ruleTemplateOptionDAO;
060    
061        private RuleDAO ruleDAO;
062    
063        private RuleDelegationDAO ruleDelegationDAO;
064    
065        /*
066         * (non-Javadoc)
067         *
068         * @see org.kuali.rice.kew.rule.RuleTemplateAttributeService#delete(java.lang.Long)
069         */
070    //    public void deleteRuleTemplateAttribute(Long ruleTemplateAttributeId, List ruleTemplateAttributes) {
071    //
072    //        RuleTemplateAttribute ruleTemplateAttributeRemove = findByRuleTemplateAttributeId(ruleTemplateAttributeId);
073    //
074    //        for (int i = ruleTemplateAttributeRemove.getDisplayOrder().intValue() + 1; i <= ruleTemplateAttributes.size(); i++) {
075    //            RuleTemplateAttribute ruleTemplateAttributeUpdate = (RuleTemplateAttribute) ruleTemplateAttributes.get(i - 1);
076    //            ruleTemplateAttributeUpdate.setDisplayOrder(new Integer(i - 1));
077    //            getRuleTemplateAttributeDAO().save(ruleTemplateAttributeUpdate);
078    //        }
079    //        getRuleTemplateAttributeDAO().delete(ruleTemplateAttributeId);
080    //    }
081    
082        public void deleteRuleTemplateOption(String ruleTemplateOptionId) {
083            getRuleTemplateOptionDAO().delete(ruleTemplateOptionId);
084        }
085    
086        public RuleTemplate findByRuleTemplateName(String ruleTemplateName) {
087            return (getRuleTemplateDAO().findByRuleTemplateName(ruleTemplateName));
088        }
089    
090        /*
091         * (non-Javadoc)
092         *
093         * @see org.kuali.rice.kew.rule.RuleTemplateAttributeService#findByRuleTemplateAttributeId(java.lang.Long)
094         */
095        public RuleTemplateAttribute findByRuleTemplateAttributeId(String ruleTemplateAttributeId) {
096            return getRuleTemplateAttributeDAO().findByRuleTemplateAttributeId(ruleTemplateAttributeId);
097        }
098    
099        public List<RuleTemplate> 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(String ruleTemplateId) {
213            LOG.debug("findByRuleTemplateId RuleTemplateServiceImpl");
214            return getRuleTemplateDAO().findByRuleTemplateId(ruleTemplateId);
215        }
216    
217        public void delete(String 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        @Override
295            public boolean supportPrettyPrint() {
296                    return true;
297            }
298    
299        public String getNextRuleTemplateId() {
300            return getRuleTemplateDAO().getNextRuleTemplateId();
301        }
302    
303    }