private void assertAllRuleDelegationsHaveUniqueNames(List<RuleDelegationBo> ruleDelegations)
     throws Exception {
   List<RuleBaseValues> rules = new ArrayList<RuleBaseValues>();
   for (RuleDelegationBo ruleDelegation : ruleDelegations) {
     rules.add(ruleDelegation.getDelegationRule());
   }
   assertAllRulesHaveUniqueNames(rules);
 }
 private void exportDefaults(Element parent, RuleTemplateBo ruleTemplate) {
   RuleBaseValues defaultRuleValues =
       KEWServiceLocator.getRuleService().findDefaultRuleByRuleTemplateId(ruleTemplate.getId());
   if (defaultRuleValues != null) {
     RuleDelegationBo defaultDelegationValues = getDefaultDelegationValues(defaultRuleValues);
     Element defaultsElement = renderer.renderElement(parent, RULE_DEFAULTS);
     if (defaultDelegationValues != null) {
       renderer.renderTextElement(
           defaultsElement,
           DELEGATION_TYPE,
           defaultDelegationValues.getDelegationType().getCode());
     }
     renderer.renderTextElement(defaultsElement, DESCRIPTION, defaultRuleValues.getDescription());
     if (defaultRuleValues.getFromDateValue() != null) {
       renderer.renderDateElement(
           defaultsElement, FROM_DATE, defaultRuleValues.getFromDateValue());
     }
     if (defaultRuleValues.getToDateValue() != null) {
       renderer.renderDateElement(defaultsElement, TO_DATE, defaultRuleValues.getToDateValue());
     }
     renderer.renderBooleanElement(
         defaultsElement, FORCE_ACTION, defaultRuleValues.isForceAction(), false);
     renderer.renderBooleanElement(defaultsElement, ACTIVE, defaultRuleValues.isActive(), true);
     if (defaultDelegationValues == null) {
       RuleTemplateOptionBo defaultActionOption = ruleTemplate.getDefaultActionRequestValue();
       RuleTemplateOptionBo supportsComplete = ruleTemplate.getComplete();
       RuleTemplateOptionBo supportsApprove = ruleTemplate.getApprove();
       RuleTemplateOptionBo supportsAck = ruleTemplate.getAcknowledge();
       RuleTemplateOptionBo supportsFYI = ruleTemplate.getFyi();
       if (defaultActionOption != null) {
         String defaultActionValue =
             (defaultActionOption == null ? null : defaultActionOption.getValue());
         renderer.renderTextElement(defaultsElement, DEFAULT_ACTION_REQUESTED, defaultActionValue);
       }
       if (supportsComplete != null) {
         String supportsCompleteValue = supportsComplete.getValue();
         renderer.renderTextElement(defaultsElement, SUPPORTS_COMPLETE, supportsCompleteValue);
       }
       if (supportsApprove != null) {
         String supportsApproveValue = supportsApprove.getValue();
         renderer.renderTextElement(defaultsElement, SUPPORTS_APPROVE, supportsApproveValue);
       }
       if (supportsAck != null) {
         String supportsAckValue = supportsAck.getValue();
         renderer.renderTextElement(defaultsElement, SUPPORTS_ACKNOWLEDGE, supportsAckValue);
       }
       if (supportsFYI != null) {
         String supportsFYIValue = supportsFYI.getValue();
         renderer.renderTextElement(defaultsElement, SUPPORTS_FYI, supportsFYIValue);
       }
     }
   }
 }
 private void assertDelegations(List oldDelegations, List newDelegations) {
   assertEquals(oldDelegations.size(), newDelegations.size());
   for (Iterator iterator = oldDelegations.iterator(); iterator.hasNext(); ) {
     RuleDelegationBo oldDelegation = (RuleDelegationBo) iterator.next();
     boolean foundDelegation = false;
     for (Iterator iterator2 = newDelegations.iterator(); iterator2.hasNext(); ) {
       RuleDelegationBo newDelegation = (RuleDelegationBo) iterator2.next();
       if (oldDelegation
           .getDelegationRule()
           .getName()
           .equals(newDelegation.getDelegationRule().getName())) {
         assertEquals(oldDelegation.getDelegationType(), newDelegation.getDelegationType());
         assertFalse(
             oldDelegation.getResponsibilityId().equals(newDelegation.getResponsibilityId()));
         assertRuleExport(oldDelegation.getDelegationRule(), newDelegation.getDelegationRule());
         foundDelegation = true;
         break;
       }
     }
     assertTrue("Could not locate delegation.", foundDelegation);
   }
 }
  /**
   * Updates the default/template rule options with those in the defaults element
   *
   * @param defaultsElement the ruleDefaults element
   * @param updatedRuleTemplate the Rule Template being updated
   * @return whether this is a delegation rule template
   */
  protected boolean updateRuleDefaults(Element defaultsElement, RuleTemplateBo updatedRuleTemplate)
      throws XmlException {
    // NOTE: implementation detail: in contrast with the other options, the delegate template, and
    // the rule attributes,
    // we unconditionally blow away the default rule and re-create it (we don't update the existing
    // one, if there is one)
    if (updatedRuleTemplate.getId() != null) {
      RuleBaseValues ruleDefaults =
          KEWServiceLocator.getRuleService()
              .findDefaultRuleByRuleTemplateId(updatedRuleTemplate.getId());
      if (ruleDefaults != null) {
        List ruleDelegationDefaults =
            KEWServiceLocator.getRuleDelegationService().findByDelegateRuleId(ruleDefaults.getId());
        // delete the rule
        KEWServiceLocator.getRuleService().delete(ruleDefaults.getId());
        // delete the associated rule delegation defaults
        for (Iterator iterator = ruleDelegationDefaults.iterator(); iterator.hasNext(); ) {
          RuleDelegationBo ruleDelegation = (RuleDelegationBo) iterator.next();
          KEWServiceLocator.getRuleDelegationService().delete(ruleDelegation.getRuleDelegationId());
        }
      }
    }

    boolean isDelegation = false;

    if (defaultsElement != null) {
      String delegationTypeCode =
          defaultsElement.getChildText(DELEGATION_TYPE, RULE_TEMPLATE_NAMESPACE);
      DelegationType delegationType = null;
      isDelegation = !org.apache.commons.lang.StringUtils.isEmpty(delegationTypeCode);

      String description = defaultsElement.getChildText(DESCRIPTION, RULE_TEMPLATE_NAMESPACE);

      // would normally be validated via schema but might not be present if invoking RuleXmlParser
      // directly
      if (description == null) {
        throw new XmlException("Description must be specified in rule defaults");
      }

      String fromDate = defaultsElement.getChildText(FROM_DATE, RULE_TEMPLATE_NAMESPACE);
      String toDate = defaultsElement.getChildText(TO_DATE, RULE_TEMPLATE_NAMESPACE);
      // toBooleanObject ensures that if the value is null (not set) that the Boolean object will
      // likewise be null (will not default to a value)
      Boolean forceAction =
          BooleanUtils.toBooleanObject(
              defaultsElement.getChildText(FORCE_ACTION, RULE_TEMPLATE_NAMESPACE));
      Boolean active =
          BooleanUtils.toBooleanObject(
              defaultsElement.getChildText(ACTIVE, RULE_TEMPLATE_NAMESPACE));

      if (isDelegation) {
        delegationType = DelegationType.parseCode(delegationTypeCode);
        if (delegationType == null) {
          throw new XmlException(
              "Invalid delegation type '"
                  + delegationType
                  + "'."
                  + "  Expected one of: "
                  + DelegationType.PRIMARY.getCode()
                  + ","
                  + DelegationType.SECONDARY.getCode());
        }
      }

      // create our "default rule" which encapsulates the defaults for the rule
      RuleBaseValues ruleDefaults = new RuleBaseValues();

      // set simple values
      ruleDefaults.setRuleTemplate(updatedRuleTemplate);
      ruleDefaults.setRuleTemplateId(updatedRuleTemplate.getId());
      ruleDefaults.setDocTypeName(DUMMY_DOCUMENT_TYPE);
      ruleDefaults.setTemplateRuleInd(Boolean.TRUE);
      ruleDefaults.setCurrentInd(Boolean.TRUE);
      ruleDefaults.setVersionNbr(new Integer(0));
      ruleDefaults.setDescription(description);

      // these are non-nullable fields, so default them if they were not set in the defaults section
      ruleDefaults.setForceAction(Boolean.valueOf(BooleanUtils.isTrue(forceAction)));
      ruleDefaults.setActive(Boolean.valueOf(BooleanUtils.isTrue(active)));

      if (ruleDefaults.getActivationDate() == null) {
        ruleDefaults.setActivationDate(new Timestamp(System.currentTimeMillis()));
      }

      ruleDefaults.setFromDateValue(formatDate("fromDate", fromDate));
      ruleDefaults.setToDateValue(formatDate("toDate", toDate));

      // ok, if this is a "Delegate Template", then we need to set this other RuleDelegation object
      // which contains
      // some delegation-related info
      RuleDelegationBo ruleDelegationDefaults = null;
      if (isDelegation) {
        ruleDelegationDefaults = new RuleDelegationBo();
        ruleDelegationDefaults.setDelegationRule(ruleDefaults);
        ruleDelegationDefaults.setDelegationType(delegationType);
        ruleDelegationDefaults.setResponsibilityId(KewApiConstants.ADHOC_REQUEST_RESPONSIBILITY_ID);
      }

      // explicitly save the new rule delegation defaults and default rule
      KEWServiceLocator.getRuleTemplateService()
          .saveRuleDefaults(ruleDelegationDefaults, ruleDefaults);
    } else {
      // do nothing, rule defaults will be deleted if ruleDefaults element is omitted
    }

    return isDelegation;
  }