public CheckPolicyRequestType transformEntityAlertToCheckPolicy(
      RespondingGatewaySendAlertMessageType message, String target) {

    CheckPolicyRequestType result = new CheckPolicyRequestType();
    if (message == null) {
      log.error("Request is null.");
      return result;
    }
    if (target == null || target.isEmpty()) {
      log.error("target is missing");
      return result;
    }

    if (message.getEDXLDistribution() == null) {
      log.error("missing body");
      return result;
    }
    if (message.getAssertion() == null) {
      log.error("missing assertion");
      return result;
    }
    if (message.getAssertion().getHomeCommunity() == null) {
      log.error("missing home community");
      return result;
    }

    EDXLDistribution body = message.getEDXLDistribution();
    // RequestType request = getRequestType(patDiscReq, event.getAssertion());
    RequestType request = new RequestType();
    AttributeHelper attrHelper = new AttributeHelper();

    log.debug("transformEntityAlertToCheckPolicy - adding subject");
    SubjectHelper subjHelp = new SubjectHelper();
    // SubjectType subject = subjHelp.subjectFactory(event.getAssertion().getHomeCommunity(),
    // event.getAssertion());
    SubjectType subject = new SubjectType();
    subject.setSubjectCategory(SubjectHelper.SubjectCategory);
    log.debug("transformEntityAlertToCheckPolicy - adding subject");
    request.getSubject().add(subject);

    ResourceType resource = new ResourceType();
    resource
        .getAttribute()
        .add(
            attrHelper.attributeFactory(
                Constants.HomeCommunityAttributeId,
                Constants.DataTypeString,
                message.getAssertion().getHomeCommunity().getHomeCommunityId()));

    request.getResource().add(resource);

    log.debug("transformEntityAlertToCheckPolicy - adding assertion data");
    AssertionHelper assertHelp = new AssertionHelper();
    assertHelp.appendAssertionDataToRequest(request, message.getAssertion());

    request.setAction(ActionHelper.actionFactory(ActionOutValue));
    result.setAssertion(message.getAssertion());
    result.setRequest(request);
    return result;
  }
  protected RequestType getRequestType(PRPAIN201305UV02 event, AssertionType assertion) {
    addDebugLog("Begin -- PatientDiscoveryPolicyTransformHelper.getRequestType()");
    RequestType request = new RequestType();

    setSubjectToRequestType(event, request, assertion);

    ResourceType resource = null;
    AttributeHelper attrHelper = new AttributeHelper();
    II ii = extractPatientIdentifier(event);
    if (ii != null) {
      resource = new ResourceType();
      resource
          .getAttribute()
          .add(
              attrHelper.attributeFactory(
                  PatientAssigningAuthorityAttributeId, Constants.DataTypeString, ii.getRoot()));
      String sStrippedPatientId = PatientIdFormatUtil.parsePatientId(ii.getExtension());
      log.debug("transformSubjectAddedInToCheckPolicy: sStrippedPatientId = " + sStrippedPatientId);
      resource
          .getAttribute()
          .add(
              attrHelper.attributeFactory(
                  PatientIdAttributeId, Constants.DataTypeString, sStrippedPatientId));
      request.getResource().add(resource);
    }
    request.getResource().add(resource);
    addDebugLog("End -- PatientDiscoveryPolicyTransformHelper.getRequestType()");
    return request;
  }
  public CheckPolicyRequestType transformNhinAlertToCheckPolicy(
      EDXLDistribution message, AssertionType assertion) {
    CheckPolicyRequestType result = new CheckPolicyRequestType();

    RequestType request = new RequestType();

    if (assertion == null) {
      log.error("Missing Assertion");
      return result;
    }
    if (message == null) {
      log.error("Missing message");
      return result;
    }
    log.debug("transformAdminDistributionNhincToCheckPolicy - adding assertion data");
    AssertionHelper assertHelp = new AssertionHelper();
    assertHelp.appendAssertionDataToRequest(request, assertion);

    request.setAction(ActionHelper.actionFactory(ActionInValue));

    result.setAssertion(assertion);
    result.setRequest(request);

    return result;
  }
  /**
   * Transform method to create a CheckPolicyRequest object from a 201306 message
   *
   * @param request
   * @return CheckPolicyRequestType
   */
  public CheckPolicyRequestType transformPatientDiscoveryNhincToCheckPolicy(
      PatDiscReqEventType event) {
    addDebugLog(
        "Begin -- PatientDiscoveryPolicyTransformHelper.transformPatientDiscoveryNhincToCheckPolicy()");
    CheckPolicyRequestType checkPolicyRequest = null;

    if (event == null) {
      addErrorLog("Request is null.");
      return checkPolicyRequest;
    } else {
      checkPolicyRequest = new CheckPolicyRequestType();
    }

    PRPAIN201306UV02 patDiscReq = event.getPRPAIN201306UV02();
    RequestType request = new RequestType();

    SubjectHelper subjHelp = new SubjectHelper();
    SubjectType subject =
        subjHelp.subjectFactory(event.getSendingHomeCommunity(), event.getAssertion());
    log.debug("transformPatientDiscoveryNhincToCheckPolicy - adding subject");
    request.getSubject().add(subject);

    II qualifiedPatientIdentifier = extractPatientIdentifier(patDiscReq);
    if (qualifiedPatientIdentifier != null) {
      ResourceType resource = new ResourceType();
      AttributeHelper attrHelper = new AttributeHelper();
      resource
          .getAttribute()
          .add(
              attrHelper.attributeFactory(
                  PatientAssigningAuthorityAttributeId,
                  Constants.DataTypeString,
                  qualifiedPatientIdentifier.getRoot()));
      String sStrippedPatientId =
          PatientIdFormatUtil.parsePatientId(qualifiedPatientIdentifier.getExtension());
      log.debug(
          "transformPatientDiscoveryNhincToCheckPolicy: sStrippedPatientId = "
              + sStrippedPatientId);
      resource
          .getAttribute()
          .add(
              attrHelper.attributeFactory(
                  PatientIdAttributeId, Constants.DataTypeString, sStrippedPatientId));

      request.getResource().add(resource);
    }

    log.debug("transformPatientDiscoveryNhincToCheckPolicy - adding assertion data");
    AssertionHelper assertHelp = new AssertionHelper();
    assertHelp.appendAssertionDataToRequest(request, event.getAssertion());

    request.setAction(ActionHelper.actionFactory(ActionInValue));

    checkPolicyRequest.setRequest(request);
    checkPolicyRequest.setAssertion(event.getAssertion());
    addDebugLog(
        "End -- PatientDiscoveryPolicyTransformHelper.transformPatientDiscoveryNhincToCheckPolicy()");
    return checkPolicyRequest;
  }
  public static CheckPolicyRequestType transformSubscribeToCheckPolicy(SubscribeEventType event) {
    CheckPolicyRequestType genericPolicyRequest = new CheckPolicyRequestType();
    RequestType request = new RequestType();
    if (InboundOutboundChecker.isInbound(event.getDirection())) {
      request.setAction(ActionHelper.actionFactory(ActionInValue));
    }
    if (InboundOutboundChecker.isOutbound(event.getDirection())) {
      request.setAction(ActionHelper.actionFactory(ActionOutValue));
    }

    SubjectHelper subjHelp = new SubjectHelper();
    SubjectType subject =
        subjHelp.subjectFactory(event.getSendingHomeCommunity(), event.getMessage().getAssertion());
    request.getSubject().add(subject);

    AdhocQueryRequest adhocReq = new AdhocQueryRequest();
    AdhocQueryType adhocQuery = null;
    adhocQuery = getAdhocQuery(event.getMessage().getSubscribe());
    adhocReq.setAdhocQuery(adhocQuery);
    String patId = AdhocQueryTransformHelper.extractPatientIdentifierId(adhocReq);
    String assignAuth =
        AdhocQueryTransformHelper.extractPatientIdentifierAssigningAuthority(adhocReq);

    ResourceType resource = new ResourceType();
    AttributeHelper attrHelper = new AttributeHelper();

    if (NullChecker.isNotNullish(assignAuth)) {
      resource
          .getAttribute()
          .add(
              attrHelper.attributeFactory(
                  PatientAssigningAuthorityAttributeId, Constants.DataTypeString, assignAuth));
    }

    if (NullChecker.isNotNullish(patId)) {
      String sStrippedPatientId = PatientIdFormatUtil.parsePatientId(patId);
      LOG.debug("transformSubscribeToCheckPolicy: sStrippedPatientId = " + sStrippedPatientId);
      resource
          .getAttribute()
          .add(
              attrHelper.attributeFactory(
                  PatientIdAttributeId, Constants.DataTypeString, sStrippedPatientId));
    }

    setTopic(event, resource);

    request.getResource().add(resource);

    AssertionHelper assertHelp = new AssertionHelper();
    assertHelp.appendAssertionDataToRequest(request, event.getMessage().getAssertion());

    genericPolicyRequest.setRequest(request);
    genericPolicyRequest.setAssertion(event.getMessage().getAssertion());
    return genericPolicyRequest;
  }
 protected void setSubjectToRequestType(
     RespondingGatewayPRPAIN201305UV02RequestType event, RequestType request) {
   // change to get the info from the receiver node
   HomeCommunityType receiverHomeCommunity = getReceiverHomeCommunityFrom201305(event);
   SubjectType subject =
       new SubjectHelper().subjectFactory(receiverHomeCommunity, event.getAssertion());
   request.getSubject().add(subject);
 }
 protected void setSubjectToRequestType(
     PRPAIN201305UV02 event, RequestType request, AssertionType assertion) {
   HomeCommunityType senderHomeCommunity = getHomeCommunityFrom201306(event);
   SubjectType subject = new SubjectHelper().subjectFactory(senderHomeCommunity, assertion);
   request.getSubject().add(subject);
 }
  /**
   * Transform method to create a CheckPolicyRequest object from a 201305 request
   *
   * @param request
   * @return CheckPolicyRequestType
   */
  public CheckPolicyRequestType transformPatientDiscoveryEntityToCheckPolicy(
      RespondingGatewayPRPAIN201305UV02RequestType event) {
    addDebugLog(
        "Begin -- PatientDiscoveryPolicyTransformHelper.transformPatientDiscoveryEntityToCheckPolicy()");

    CheckPolicyRequestType checkPolicyRequest = null;

    if (event == null) {
      addErrorLog("Request is null.");
      return checkPolicyRequest;
    } else {
      checkPolicyRequest = new CheckPolicyRequestType();
    }

    PRPAIN201305UV02 patDiscReq = event.getPRPAIN201305UV02();
    // RequestType request = getRequestType(patDiscReq, event.getAssertion());
    RequestType request = new RequestType();

    SubjectHelper subjHelp = new SubjectHelper();
    // SubjectType subject = subjHelp.subjectFactory(event.getAssertion().getHomeCommunity(),
    // event.getAssertion());
    SubjectType subject = new SubjectType();
    subject.setSubjectCategory(SubjectHelper.SubjectCategory);
    log.debug("transformPatientDiscoveryNhincToCheckPolicy - adding subject");
    request.getSubject().add(subject);

    II qualifiedPatientIdentifier = extractPatientIdentifier(patDiscReq);
    if (qualifiedPatientIdentifier != null) {
      ResourceType resource = new ResourceType();
      AttributeHelper attrHelper = new AttributeHelper();
      resource
          .getAttribute()
          .add(
              attrHelper.attributeFactory(
                  PatientAssigningAuthorityAttributeId,
                  Constants.DataTypeString,
                  qualifiedPatientIdentifier.getRoot()));
      String sStrippedPatientId =
          PatientIdFormatUtil.parsePatientId(qualifiedPatientIdentifier.getExtension());
      log.debug(
          "transformPatientDiscoveryNhincToCheckPolicy: sStrippedPatientId = "
              + sStrippedPatientId);
      resource
          .getAttribute()
          .add(
              attrHelper.attributeFactory(
                  PatientIdAttributeId, Constants.DataTypeString, sStrippedPatientId));

      HomeCommunityType homeCommunityId = null;

      if (event != null
          && event.getPRPAIN201305UV02() != null
          && NullChecker.isNotNullish(event.getPRPAIN201305UV02().getReceiver())
          && event.getPRPAIN201305UV02().getReceiver().get(0) != null
          && event.getPRPAIN201305UV02().getReceiver().get(0).getDevice() != null
          && event.getPRPAIN201305UV02().getReceiver().get(0).getDevice().getAsAgent() != null
          && event.getPRPAIN201305UV02().getReceiver().get(0).getDevice().getAsAgent().getValue()
              != null
          && event
                  .getPRPAIN201305UV02()
                  .getReceiver()
                  .get(0)
                  .getDevice()
                  .getAsAgent()
                  .getValue()
                  .getRepresentedOrganization()
              != null
          && event
                  .getPRPAIN201305UV02()
                  .getReceiver()
                  .get(0)
                  .getDevice()
                  .getAsAgent()
                  .getValue()
                  .getRepresentedOrganization()
                  .getValue()
              != null
          && NullChecker.isNotNullish(
              event
                  .getPRPAIN201305UV02()
                  .getReceiver()
                  .get(0)
                  .getDevice()
                  .getAsAgent()
                  .getValue()
                  .getRepresentedOrganization()
                  .getValue()
                  .getId())
          && event
                  .getPRPAIN201305UV02()
                  .getReceiver()
                  .get(0)
                  .getDevice()
                  .getAsAgent()
                  .getValue()
                  .getRepresentedOrganization()
                  .getValue()
                  .getId()
                  .get(0)
              != null
          && NullChecker.isNotNullish(
              event
                  .getPRPAIN201305UV02()
                  .getReceiver()
                  .get(0)
                  .getDevice()
                  .getAsAgent()
                  .getValue()
                  .getRepresentedOrganization()
                  .getValue()
                  .getId()
                  .get(0)
                  .getRoot())) {
        homeCommunityId = new HomeCommunityType();
        homeCommunityId.setHomeCommunityId(
            event
                .getPRPAIN201305UV02()
                .getReceiver()
                .get(0)
                .getDevice()
                .getAsAgent()
                .getValue()
                .getRepresentedOrganization()
                .getValue()
                .getId()
                .get(0)
                .getRoot());
      }
      resource
          .getAttribute()
          .add(
              attrHelper.attributeFactory(
                  Constants.HomeCommunityAttributeId,
                  Constants.DataTypeString,
                  subjHelp.determineSendingHomeCommunityId(homeCommunityId, event.getAssertion())));

      request.getResource().add(resource);
    }

    log.debug("transformPatientDiscoveryNhincToCheckPolicy - adding assertion data");
    AssertionHelper assertHelp = new AssertionHelper();
    assertHelp.appendAssertionDataToRequest(request, event.getAssertion());

    request.setAction(ActionHelper.actionFactory(ActionOutValue));

    checkPolicyRequest.setRequest(request);
    checkPolicyRequest.setAssertion(event.getAssertion());

    addDebugLog(
        "End -- PatientDiscoveryPolicyTransformHelper.transformPatientDiscoveryEntityToCheckPolicy()");
    return checkPolicyRequest;
  }