/*
   * (non-Javadoc)
   *
   * @see
   * gov.samhsa.consent2share.si.AbstractConsentMessageHandler#handleMessage
   * (java.lang.String)
   */
  @Override
  public String handleMessage(String data) throws Throwable {
    String messageId = UUID.randomUUID().toString();
    logger.debug("Consent Signed Message Received: ConsentId" + new String(data));

    Long consentId = Long.parseLong(data);

    // Get consent
    SimpleConsentDto consentDto = consentGetter.getConsent(consentId);

    // Save to XDS.b repository
    RegistryResponse response = null;
    try {
      response =
          xdsbRepository.provideAndRegisterDocumentSet(
              consentDto.getConsent(), domainId, XdsbDocumentType.PRIVACY_CONSENT, null, null);
      audit(messageId, consentDto, response);
    } catch (Throwable e) {
      logger.error("Failed to save in xds.b repository", e);

      throw e;
    }

    if (!URN_RESPONSE_SUCCESS.equals(response.getStatus())) {
      String errorMessage =
          "Failed to save in XDS.b repository becuase response status is not "
              + URN_RESPONSE_SUCCESS;
      if (response.getRegistryErrorList() != null)
        logger.error(response.getRegistryErrorList().getRegistryError().get(0).getCodeContext());
      logger.error(errorMessage);

      throw new Exception(errorMessage);
    }

    return "Saved in XDS.b repository";
  }
 /**
  * Audit.
  *
  * @param messageId the message id
  * @param consentDto the consent dto
  * @param registryResponse the registry response
  * @throws AuditException the audit exception
  */
 private void audit(
     String messageId, SimpleConsentDto consentDto, RegistryResponse registryResponse)
     throws AuditException {
   Map<PredicateKey, String> predicateMap = auditService.createPredicateMap();
   predicateMap.put(C2S_CONSENT_ID, consentDto.getConsentId());
   predicateMap.put(C2S_PATIENT_ID, Long.toString(consentDto.getPatientId()));
   predicateMap.put(DOMAIN_ID, domainId);
   if (registryResponse != null) {
     predicateMap.put(RESPONSE_STATUS, registryResponse.getStatus());
     try {
       predicateMap.put(RESPONSE_BODY, marshaller.marshal(registryResponse));
     } catch (SimpleMarshallerException e) {
       throw new AuditException(e.getMessage(), e);
     }
   }
   predicateMap.put(XACML_POLICY_ID, consentDto.getXacmlPolicyId());
   predicateMap.put(XACML_POLICY, consentDto.getConsent());
   auditService.audit(this, messageId, XDS_ADD_CONSENT, consentDto.geteId(), predicateMap);
 }