Coverage Report - org.kuali.rice.kew.rule.service.impl.RuleTemplateServiceImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
RuleTemplateServiceImpl
0%
0/85
0%
0/34
1.769
 
 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.core.api.impex.ExportDataSet;
 26  
 import org.kuali.rice.kew.exception.WorkflowServiceErrorException;
 27  
 import org.kuali.rice.kew.exception.WorkflowServiceErrorImpl;
 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  0
 public class RuleTemplateServiceImpl implements RuleTemplateService {
 46  
 
 47  0
     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(String ruleTemplateOptionId) {
 83  0
         getRuleTemplateOptionDAO().delete(ruleTemplateOptionId);
 84  0
     }
 85  
 
 86  
     public RuleTemplate findByRuleTemplateName(String ruleTemplateName) {
 87  0
         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(String ruleTemplateAttributeId) {
 96  0
         return getRuleTemplateAttributeDAO().findByRuleTemplateAttributeId(ruleTemplateAttributeId);
 97  
     }
 98  
 
 99  
     public List<RuleTemplate> findAll() {
 100  0
         return ruleTemplateDAO.findAll();
 101  
     }
 102  
 
 103  
     public List findByRuleTemplate(RuleTemplate ruleTemplate) {
 104  0
         return ruleTemplateDAO.findByRuleTemplate(ruleTemplate);
 105  
     }
 106  
 
 107  
     public void save(RuleTemplate ruleTemplate) {
 108  0
         LOG.debug("save RuleTemplateServiceImpl");
 109  0
         validate(ruleTemplate);
 110  0
         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  0
         getRuleTemplateDAO().save(ruleTemplate);
 133  0
         LOG.debug("end save RuleTemplateServiceImpl");
 134  0
     }
 135  
 
 136  
     public void save(RuleTemplateAttribute ruleTemplateAttribute) {
 137  0
         ruleTemplateAttributeDAO.save(ruleTemplateAttribute);
 138  0
     }
 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  0
         KEWServiceLocator.getRuleService().saveRule(ruleBaseValues, false);
 145  0
         if (ruleDelegation != null) {
 146  0
                 KEWServiceLocator.getRuleService().saveRule(ruleDelegation.getDelegationRuleBaseValues(), false);
 147  0
             KEWServiceLocator.getRuleDelegationService().save(ruleDelegation);
 148  
         }
 149  0
     }
 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  0
         if (ruleTemplate != null && ruleTemplate.getRuleTemplateId() != null) {
 158  
             // for every rule template attribute
 159  0
             for (RuleTemplateAttribute ruleTemplateAttribute: ruleTemplate.getRuleTemplateAttributes()) {
 160  
                 // if the rule template is not set on the attribute, set it
 161  0
                 if (ruleTemplateAttribute.getRuleTemplate() == null || ruleTemplateAttribute.getRuleTemplateId() == null) {
 162  0
                     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  0
                 if (ruleTemplateAttribute.getRuleAttribute() == null) {
 166  0
                     RuleAttributeService ruleAttributeService = (RuleAttributeService) KEWServiceLocator.getService(KEWServiceLocator.RULE_ATTRIBUTE_SERVICE);
 167  0
                     ruleTemplateAttribute.setRuleAttribute(ruleAttributeService.findByRuleAttributeId(ruleTemplateAttribute.getRuleAttributeId()));
 168  0
                 }
 169  
             }
 170  
             // for every rule template option
 171  0
             for (RuleTemplateOption option: ruleTemplate.getRuleTemplateOptions()) {
 172  
                 // if the rule template is not set on the option, set it
 173  0
                 if (option.getRuleTemplate() == null || option.getRuleTemplateId() == null) {
 174  0
                     option.setRuleTemplate(ruleTemplate);
 175  
                 }
 176  
             }
 177  
         }
 178  0
     }
 179  
 
 180  
     private void validate(RuleTemplate ruleTemplate) {
 181  0
         LOG.debug("validating ruleTemplate");
 182  0
         Collection errors = new ArrayList();
 183  0
         if (ruleTemplate.getName() == null || ruleTemplate.getName().trim().equals("")) {
 184  0
             errors.add(new WorkflowServiceErrorImpl("Please enter a rule template name.", RULE_TEMPLATE_NAME_REQUIRED));
 185  0
             LOG.error("Rule template name is missing");
 186  
         } else {
 187  0
             ruleTemplate.setName(ruleTemplate.getName().trim());
 188  0
             if (ruleTemplate.getRuleTemplateId() == null) {
 189  0
                 RuleTemplate nameInUse = findByRuleTemplateName(ruleTemplate.getName());
 190  0
                 if (nameInUse != null) {
 191  0
                     errors.add(new WorkflowServiceErrorImpl("Rule template name already in use", "rule.template.name.duplicate"));
 192  0
                     LOG.error("Rule template name already in use");
 193  
                 }
 194  
             }
 195  
         }
 196  0
         if (ruleTemplate.getDescription() == null || ruleTemplate.getDescription().equals("")) {
 197  0
             errors.add(new WorkflowServiceErrorImpl("Please enter a rule template description.", RULE_TEMPLATE_DESCRIPTION_REQUIRED));
 198  0
             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  0
         LOG.debug("end validating ruleTemplate");
 207  0
         if (!errors.isEmpty()) {
 208  0
             throw new WorkflowServiceErrorException("RuleTemplate Validation Error", errors);
 209  
         }
 210  0
     }
 211  
 
 212  
     public RuleTemplate findByRuleTemplateId(String ruleTemplateId) {
 213  0
         LOG.debug("findByRuleTemplateId RuleTemplateServiceImpl");
 214  0
         return getRuleTemplateDAO().findByRuleTemplateId(ruleTemplateId);
 215  
     }
 216  
 
 217  
     public void delete(String ruleTemplateId) {
 218  0
         LOG.debug("delete RuleTemplateServiceImpl");
 219  0
         getRuleTemplateDAO().delete(ruleTemplateId);
 220  0
         LOG.debug("end delete RuleTemplateServiceImpl");
 221  0
     }
 222  
 
 223  
     public RuleTemplateDAO getRuleTemplateDAO() {
 224  0
         return ruleTemplateDAO;
 225  
     }
 226  
 
 227  
     public void setRuleTemplateDAO(RuleTemplateDAO ruleTemplateDAO) {
 228  0
         this.ruleTemplateDAO = ruleTemplateDAO;
 229  0
     }
 230  
 
 231  
     /**
 232  
      * @return Returns the ruleTemplateAttributeDAO.
 233  
      */
 234  
     public RuleTemplateAttributeDAO getRuleTemplateAttributeDAO() {
 235  0
         return ruleTemplateAttributeDAO;
 236  
     }
 237  
 
 238  
     /**
 239  
      * @param ruleTemplateAttributeDAO
 240  
      *            The ruleTemplateAttributeDAO to set.
 241  
      */
 242  
     public void setRuleTemplateAttributeDAO(RuleTemplateAttributeDAO ruleTemplateAttributeDAO) {
 243  0
         this.ruleTemplateAttributeDAO = ruleTemplateAttributeDAO;
 244  0
     }
 245  
 
 246  
     public RuleDAO getRuleDAO() {
 247  0
         return ruleDAO;
 248  
     }
 249  
 
 250  
     public void setRuleDAO(RuleDAO ruleDAO) {
 251  0
         this.ruleDAO = ruleDAO;
 252  0
     }
 253  
 
 254  
     public RuleDelegationDAO getRuleDelegationDAO() {
 255  0
         return ruleDelegationDAO;
 256  
     }
 257  
 
 258  
     public void setRuleDelegationDAO(RuleDelegationDAO ruleDelegationDAO) {
 259  0
         this.ruleDelegationDAO = ruleDelegationDAO;
 260  0
     }
 261  
 
 262  
     /**
 263  
      * @return Returns the ruleTemplateOptionDAO.
 264  
      */
 265  
     public RuleTemplateOptionDAO getRuleTemplateOptionDAO() {
 266  0
         return ruleTemplateOptionDAO;
 267  
     }
 268  
 
 269  
     /**
 270  
      * @param ruleTemplateOptionDAO
 271  
      *            The ruleTemplateOptionDAO to set.
 272  
      */
 273  
     public void setRuleTemplateOptionDAO(RuleTemplateOptionDAO ruleTemplateOptionDAO) {
 274  0
         this.ruleTemplateOptionDAO = ruleTemplateOptionDAO;
 275  0
     }
 276  
 
 277  
     public void loadXml(InputStream inputStream, String principalId) {
 278  0
         RuleTemplateXmlParser parser = new RuleTemplateXmlParser();
 279  
         try {
 280  0
             parser.parseRuleTemplates(inputStream);
 281  0
         } catch (Exception e) { //any other exception
 282  0
             LOG.error("Error loading xml file", e);
 283  0
             WorkflowServiceErrorException wsee = new WorkflowServiceErrorException("Error loading xml file", new WorkflowServiceErrorImpl("Error loading xml file", XML_PARSE_ERROR));
 284  0
             wsee.initCause(e);
 285  0
             throw wsee;
 286  0
         }
 287  0
     }
 288  
 
 289  
     public Element export(ExportDataSet dataSet) {
 290  0
         RuleTemplateXmlExporter exporter = new RuleTemplateXmlExporter();
 291  0
         return exporter.export(dataSet);
 292  
     }
 293  
     
 294  
     @Override
 295  
         public boolean supportPrettyPrint() {
 296  0
                 return true;
 297  
         }
 298  
 
 299  
     public String getNextRuleTemplateId() {
 300  0
         return getRuleTemplateDAO().getNextRuleTemplateId();
 301  
     }
 302  
 
 303  
 }