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