private RuleTemplateBo parseRuleTemplate(Element element, List<RuleTemplateBo> ruleTemplates)
      throws XmlException {
    String name = element.getChildText(NAME, RULE_TEMPLATE_NAMESPACE);
    String description = element.getChildText(DESCRIPTION, RULE_TEMPLATE_NAMESPACE);
    Attribute allowOverwriteAttrib = element.getAttribute("allowOverwrite");

    boolean allowOverwrite = false;
    if (allowOverwriteAttrib != null) {
      allowOverwrite = Boolean.valueOf(allowOverwriteAttrib.getValue()).booleanValue();
    }
    if (org.apache.commons.lang.StringUtils.isEmpty(name)) {
      throw new XmlException("RuleTemplate must have a name");
    }
    if (org.apache.commons.lang.StringUtils.isEmpty(description)) {
      throw new XmlException("RuleTemplate must have a description");
    }

    // look up the rule template by name first
    RuleTemplateBo ruleTemplate =
        KEWServiceLocator.getRuleTemplateService().findByRuleTemplateName(name);

    if (ruleTemplate == null) {
      // if it does not exist create a new one
      ruleTemplate = new RuleTemplateBo();
    } else {
      // if it does exist, update it, only if allowOverwrite is set
      if (!allowOverwrite) {
        throw new RuntimeException(
            "Attempting to overwrite template " + name + " without allowOverwrite set");
      }

      // the name should be equal if one was actually found
      assert (name.equals(ruleTemplate.getName()))
          : "Existing template definition name does not match incoming definition name";
    }

    // overwrite simple properties
    ruleTemplate.setName(name);
    ruleTemplate.setDescription(description);

    // update the delegation template
    updateDelegationTemplate(element, ruleTemplate, ruleTemplates);

    // update the attribute relationships
    updateRuleTemplateAttributes(element, ruleTemplate);

    // save the rule template first so that the default/template rule that is generated
    // in the process of setting defaults is associated properly with this rule template
    ruleTemplate = KEWServiceLocator.getRuleTemplateService().save(ruleTemplate);

    // update the default options
    updateRuleTemplateDefaultOptions(element, ruleTemplate);

    ruleTemplate = KEWServiceLocator.getRuleTemplateService().save(ruleTemplate);

    return ruleTemplate;
  }
 private void exportRuleTemplate(Element parent, RuleTemplateBo ruleTemplate) {
   Element templateElement = renderer.renderElement(parent, RULE_TEMPLATE);
   renderer.renderTextElement(templateElement, NAME, ruleTemplate.getName());
   renderer.renderTextElement(templateElement, DESCRIPTION, ruleTemplate.getDescription());
   if (ruleTemplate.getDelegationTemplate() != null) {
     renderer.renderTextElement(
         templateElement, DELEGATION_TEMPLATE, ruleTemplate.getDelegationTemplate().getName());
   }
   exportAttributes(templateElement, ruleTemplate.getActiveRuleTemplateAttributes());
   exportDefaults(templateElement, ruleTemplate);
 }
 private void assertRuleTemplateExport(
     RuleTemplateBo oldRuleTemplate, RuleTemplateBo newRuleTemplate) {
   assertFalse(
       "Ids should be different.", oldRuleTemplate.getId().equals(newRuleTemplate.getId()));
   assertEquals(oldRuleTemplate.getDescription(), newRuleTemplate.getDescription());
   assertEquals(oldRuleTemplate.getName(), newRuleTemplate.getName());
   if (oldRuleTemplate.getDelegationTemplate() != null) {
     assertRuleTemplateExport(
         oldRuleTemplate.getDelegationTemplate(), newRuleTemplate.getDelegationTemplate());
   } else {
     assertNull(newRuleTemplate.getDelegationTemplate());
   }
   assertAttributes(
       oldRuleTemplate.getRuleTemplateAttributes(),
       newRuleTemplate.getRuleTemplateAttributes(),
       "attribute");
   assertAttributes(
       oldRuleTemplate.getActiveRuleTemplateAttributes(),
       newRuleTemplate.getActiveRuleTemplateAttributes(),
       "active attribute");
   assertOptions(
       oldRuleTemplate.getRuleTemplateOptions(), newRuleTemplate.getRuleTemplateOptions());
 }
  protected void assertExport() throws Exception {
    // export all existing rule templates and their dependencies (rule attributes)
    List oldRuleTemplates = KEWServiceLocator.getRuleTemplateService().findAll();
    KewExportDataSet dataSet = new KewExportDataSet();
    dataSet.getRuleTemplates().addAll(oldRuleTemplates);
    dataSet.getRuleAttributes().addAll(KEWServiceLocator.getRuleAttributeService().findAll());
    byte[] xmlBytes =
        CoreApiServiceLocator.getXmlExporterService().export(dataSet.createExportDataSet());
    assertTrue("XML should be non empty.", xmlBytes != null && xmlBytes.length > 0);

    // now clear the tables
    new ClearDatabaseLifecycle(getPerTestTablesToClear(), getPerTestTablesNotToClear()).start();

    // import the exported xml
    loadXmlStream(new BufferedInputStream(new ByteArrayInputStream(xmlBytes)));

    List newRuleTemplates = KEWServiceLocator.getRuleTemplateService().findAll();
    assertEquals(
        "Should have same number of old and new RuleTemplates.",
        oldRuleTemplates.size(),
        newRuleTemplates.size());
    for (Iterator iterator = oldRuleTemplates.iterator(); iterator.hasNext(); ) {
      RuleTemplateBo oldRuleTemplate = (RuleTemplateBo) iterator.next();
      boolean foundTemplate = false;
      for (Iterator iterator2 = newRuleTemplates.iterator(); iterator2.hasNext(); ) {
        RuleTemplateBo newRuleTemplate = (RuleTemplateBo) iterator2.next();
        if (oldRuleTemplate.getName().equals(newRuleTemplate.getName())) {
          assertRuleTemplateExport(oldRuleTemplate, newRuleTemplate);
          foundTemplate = true;
        }
      }
      assertTrue(
          "Could not locate the new rule template for name " + oldRuleTemplate.getName(),
          foundTemplate);
    }
  }
  /**
   * Updates the rule template delegation template with the one specified in the XML (if any)
   *
   * @param ruleTemplateElement the XML ruleTemplate element
   * @param updatedRuleTemplate the rule template to update
   * @param parsedRuleTemplates the rule templates parsed in this parsing run
   * @throws XmlException if a delegation template was specified but could not be found
   */
  protected void updateDelegationTemplate(
      Element ruleTemplateElement,
      RuleTemplateBo updatedRuleTemplate,
      List<RuleTemplateBo> parsedRuleTemplates)
      throws XmlException {
    String delegateTemplateName =
        ruleTemplateElement.getChildText(DELEGATION_TEMPLATE, RULE_TEMPLATE_NAMESPACE);

    if (delegateTemplateName != null) {
      // if a delegateTemplate was set in the XML, then look it up and set it on the RuleTemplate
      // object
      // first try looking up an existing delegateTemplate in the system
      RuleTemplateBo delegateTemplate =
          KEWServiceLocator.getRuleTemplateService().findByRuleTemplateName(delegateTemplateName);

      // if not found, try the list of templates currently parsed
      if (delegateTemplate == null) {
        for (RuleTemplateBo rt : parsedRuleTemplates) {
          if (delegateTemplateName.equalsIgnoreCase(rt.getName())) {
            // set the expected next rule template id on the target delegateTemplate
            String ruleTemplateId =
                KEWServiceLocator.getRuleTemplateService().getNextRuleTemplateId();
            rt.setId(ruleTemplateId);
            delegateTemplate = rt;
            break;
          }
        }
      }

      if (delegateTemplate == null) {
        throw new XmlException("Cannot find delegation template " + delegateTemplateName);
      }

      updatedRuleTemplate.setDelegationTemplateId(delegateTemplate.getDelegationTemplateId());
      updatedRuleTemplate.setDelegationTemplate(delegateTemplate);
    } else {
      // the previously referenced template is left in the system
    }
  }