/**
   * Create XACML policy using the data received from XACML policy wizard
   *
   * @param policyElementDTO policy element
   * @param subElementDTOs target elements
   * @param ruleElementDTOs rule elements
   * @return String object of the XACML policy
   * @throws EntitlementPolicyCreationException throws
   */
  public String createPolicy(
      PolicyElementDTO policyElementDTO,
      List<SubElementDTO> subElementDTOs,
      List<RuleElementDTO> ruleElementDTOs)
      throws EntitlementPolicyCreationException {

    Element policyElement = null;
    String ruleElementOrder = null;
    try {
      Document doc = createNewDocument();
      if (doc != null) {
        if (policyElementDTO != null) {
          policyElement = PolicyCreatorUtil.createPolicyElement(policyElementDTO, doc);
          doc.appendChild(policyElement);
          ruleElementOrder = policyElementDTO.getRuleElementOrder();
        }

        if (policyElementDTO != null) {
          if (subElementDTOs != null && subElementDTOs.size() > 0) {
            policyElement.appendChild(PolicyCreatorUtil.createTargetElement(subElementDTOs, doc));
          } else if (ruleElementDTOs != null && ruleElementDTOs.size() > 0) {
            policyElement.appendChild(doc.createElement(EntitlementPolicyConstants.TARGET_ELEMENT));
          }

          if (ruleElementDTOs != null && ruleElementDTOs.size() > 0) {
            if (ruleElementOrder != null && ruleElementOrder.trim().length() > 0) {
              String[] ruleIds =
                  ruleElementOrder.split(EntitlementPolicyConstants.ATTRIBUTE_SEPARATOR);
              for (String ruleId : ruleIds) {
                for (RuleElementDTO ruleElementDTO : ruleElementDTOs) {
                  if (ruleId.trim().equals(ruleElementDTO.getRuleId())) {
                    policyElement.appendChild(
                        PolicyCreatorUtil.createRuleElement(ruleElementDTO, doc));
                  }
                }
              }
            } else {
              for (RuleElementDTO ruleElementDTO : ruleElementDTOs) {
                policyElement.appendChild(PolicyCreatorUtil.createRuleElement(ruleElementDTO, doc));
              }
            }
          }
        }

        return PolicyCreatorUtil.getStringFromDocument(doc);
      }
    } catch (EntitlementPolicyCreationException e) {
      throw new EntitlementPolicyCreationException("Error While Creating XACML Policy", e);
    }

    return null;
  }
 /**
  * Create basic XACML request
  *
  * @param requestElementDTO request element
  * @return String object of the XACML request
  * @throws EntitlementPolicyCreationException throws
  */
 public String createBasicRequest(RequestElementDTO requestElementDTO)
     throws EntitlementPolicyCreationException {
   try {
     Document doc = createNewDocument();
     if (doc != null) {
       doc.appendChild(PolicyCreatorUtil.createBasicRequestElement(requestElementDTO, doc));
       return PolicyCreatorUtil.getStringFromDocument(doc);
     }
   } catch (EntitlementPolicyCreationException e) {
     throw new EntitlementPolicyCreationException("Error While Creating XACML Request", e);
   }
   return null;
 }
 /**
  * Create policy set using the added policy ot policy sets
  *
  * @param policySetDTO policy set element
  * @return String object of the XACML policy Set
  * @throws EntitlementPolicyCreationException throws
  */
 public String createPolicySet(PolicySetDTO policySetDTO)
     throws EntitlementPolicyCreationException {
   try {
     Document doc = createNewDocument();
     if (doc != null) {
       doc.appendChild(PolicyCreatorUtil.createPolicySetElement(policySetDTO, doc));
       StringBuilder policySet = new StringBuilder(PolicyCreatorUtil.getStringFromDocument(doc));
       if (policySetDTO.getPolicies() != null) {
         for (String policy : policySetDTO.getPolicies()) {
           policySet.insert(policySet.indexOf(">") + 1, policy);
         }
       }
       return policySet.toString();
     }
   } catch (EntitlementPolicyCreationException e) {
     throw new EntitlementPolicyCreationException("Error While Creating Policy Set", e);
   }
   return null;
 }