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 SubjectType createSubject(HomeCommunityType hc, AssertionType assertion) {
    SubjectHelper subjHelp = new SubjectHelper();
    // SubjectType subject = subjHelp.subjectFactory(event.getAssertion().getHomeCommunity(),
    // event.getAssertion());
    SubjectType subject = subjHelp.subjectFactory(hc, assertion);

    subject.setSubjectCategory(SubjectHelper.SubjectCategory);

    return 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;
  }