/**
  * Parses a rule template attribute
  *
  * @param element the attribute XML element
  * @param ruleTemplate the ruleTemplate to update
  * @return a parsed rule template attribute
  * @throws XmlException if the attribute does not exist
  */
 private RuleTemplateAttributeBo parseRuleTemplateAttribute(
     Element element, RuleTemplateBo ruleTemplate) throws XmlException {
   String attributeName = element.getChildText(NAME, RULE_TEMPLATE_NAMESPACE);
   String requiredValue = element.getChildText(REQUIRED, RULE_TEMPLATE_NAMESPACE);
   String activeValue = element.getChildText(ACTIVE, RULE_TEMPLATE_NAMESPACE);
   if (org.apache.commons.lang.StringUtils.isEmpty(attributeName)) {
     throw new XmlException("Attribute name must be non-empty");
   }
   boolean required = DEFAULT_ATTRIBUTE_REQUIRED;
   if (requiredValue != null) {
     required = Boolean.parseBoolean(requiredValue);
   }
   boolean active = DEFAULT_ATTRIBUTE_ACTIVE;
   if (activeValue != null) {
     active = Boolean.parseBoolean(activeValue);
   }
   RuleAttribute ruleAttribute =
       KEWServiceLocator.getRuleAttributeService().findByName(attributeName);
   if (ruleAttribute == null) {
     throw new XmlException("Could not locate rule attribute for name '" + attributeName + "'");
   }
   RuleTemplateAttributeBo templateAttribute = new RuleTemplateAttributeBo();
   templateAttribute.setRuleAttribute(ruleAttribute);
   templateAttribute.setRuleAttributeId(ruleAttribute.getId());
   templateAttribute.setRuleTemplate(ruleTemplate);
   templateAttribute.setRequired(Boolean.valueOf(required));
   templateAttribute.setActive(Boolean.valueOf(active));
   templateAttribute.setDisplayOrder(new Integer(templateAttributeCounter++));
   return templateAttribute;
 }
  /**
   * Updates the attributes set on the RuleTemplate
   *
   * @param ruleTemplateElement the XML ruleTemplate element
   * @param updatedRuleTemplate the RuleTemplate being updated
   * @throws XmlException if there was a problem parsing the rule template attributes
   */
  protected void updateRuleTemplateAttributes(
      Element ruleTemplateElement, RuleTemplateBo updatedRuleTemplate) throws XmlException {
    // add any newly defined rule template attributes to the rule template,
    // update the active and required flags of any existing ones.
    // if this is an update of an existing rule template, related attribute objects will be present
    // in this rule template object,
    // otherwise none will be present (so they'll all be new)

    Element attributesElement = ruleTemplateElement.getChild(ATTRIBUTES, RULE_TEMPLATE_NAMESPACE);
    List<RuleTemplateAttributeBo> incomingAttributes = new ArrayList<RuleTemplateAttributeBo>();
    if (attributesElement != null) {
      incomingAttributes.addAll(
          parseRuleTemplateAttributes(attributesElement, updatedRuleTemplate));
    }

    // inactivate all current attributes
    for (RuleTemplateAttributeBo currentRuleTemplateAttribute :
        updatedRuleTemplate.getRuleTemplateAttributes()) {
      String ruleAttributeName =
          (currentRuleTemplateAttribute.getRuleAttribute() != null)
              ? currentRuleTemplateAttribute.getRuleAttribute().getName()
              : "(null)";
      LOG.debug(
          "Inactivating rule template attribute with id "
              + currentRuleTemplateAttribute.getId()
              + " and rule attribute with name "
              + ruleAttributeName);
      currentRuleTemplateAttribute.setActive(Boolean.FALSE);
    }
    // NOTE: attributes are deactivated, not removed

    // add/update any new attributes
    for (RuleTemplateAttributeBo ruleTemplateAttribute : incomingAttributes) {
      RuleTemplateAttributeBo potentialExistingTemplateAttribute =
          updatedRuleTemplate.getRuleTemplateAttribute(ruleTemplateAttribute);
      if (potentialExistingTemplateAttribute != null) {
        // template attribute exists on rule template already; update the options
        potentialExistingTemplateAttribute.setActive(ruleTemplateAttribute.getActive());
        potentialExistingTemplateAttribute.setRequired(ruleTemplateAttribute.getRequired());
      } else {
        // template attribute does not yet exist on template so add it
        ruleTemplateAttribute.setRuleTemplate(updatedRuleTemplate);
        updatedRuleTemplate.getRuleTemplateAttributes().add(ruleTemplateAttribute);
      }
    }
  }