/**
   * Gets the information of the status of the different Guarantee Terms of an agreement. *
   *
   * <pre>
   * GET /agreements/{agreementId}/guaranteestatus
   *
   * Request:
   *   GET /agreements HTTP/1.1
   *
   * Response:
   *   HTTP/1.1 200 Ok
   *   Content-type: application/xml
   *
   * {@code
   * {"agreementId":"{agreementId}","value":"FULFILLED|VIOLATED|NON_DETERMINED",
   * "GuaranteeTermStatus":
   * [{"name":"{gt_name1}","value":"FULFILLED|VIOLATED|NON_DETERMINED"},
   * {"name":"{gt_name2}","value":"FULFILLED|VIOLATED|NON_DETERMINED"}]}
   * }
   *
   * </pre>
   *
   * Example:
   * <li>curl -H "Content-type: application/xml"
   *     http://localhost:8080/sla-service/agreements/{agreementId}/guaranteestatus
   *
   * @return Json information with Guarantee Status
   */
  @GET
  @Path("{id}/guaranteestatus")
  @Consumes(MediaType.APPLICATION_JSON)
  @Produces(MediaType.APPLICATION_JSON)
  public Response getStatusAgreementJson(@PathParam("id") String agreement_id) {
    logger.debug(
        "StartOf getStatusAgreementJson - REQUEST for /agreements/"
            + agreement_id
            + "/guaranteestatus");

    Response result;
    try {
      AgreementHelper agreementRestService = getAgreementHelper();
      String serializedAgreement =
          agreementRestService.getAgreementStatus(agreement_id, MediaType.APPLICATION_JSON);
      if (serializedAgreement != null) {
        result = buildResponse(200, serializedAgreement);
      } else {
        result = buildResponse(404, printError(404, "No agreement with " + agreement_id));
      }
    } catch (HelperException e) {
      logger.info("getStatusAgreementJson exception:" + e.getMessage());
      return buildResponse(e);
    }
    logger.debug("EndOf getStatusAgreementJson");
    return result;
  }
 /**
  * Creates a new agreement
  *
  * <pre>
  *  POST /agreements
  *
  *  Request:
  *    POST /agreements HTTP/1.1
  *    Accept: application/xml
  *
  *  Response:
  *    HTTP/1.1 201 Created
  *    Content-type: application/xml
  *    Location: http://.../agreements/$uuid
  *
  *  {@code
  *    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  *    <message code="201" message= "The agreement has been stored successfully in the SLA Repository Database"/>
  *  }
  *
  * </pre>
  *
  * Example:
  * <li>curl -H "Content-type: application/xml" [email protected]
  *     localhost:8080/sla-service/agreements -X POST
  *
  * @return XML information with the different details of the agreement
  */
 @POST
 @Consumes(MediaType.APPLICATION_XML)
 @Produces(MediaType.APPLICATION_XML)
 public Response createAgreement(
     @Context HttpHeaders hh, @Context UriInfo uriInfo, String payload) {
   logger.debug("StartOf createAgreement - Insert /agreements");
   String location = null;
   try {
     AgreementHelper agreementRestService = getAgreementHelper();
     location =
         agreementRestService.createAgreement(hh, uriInfo.getAbsolutePath().toString(), payload);
   } catch (HelperException e) {
     logger.info("createAgreement exception", e);
     return buildResponse(e);
   }
   Response result =
       buildResponsePOST(
           HttpStatus.CREATED,
           printMessage(
               HttpStatus.CREATED,
               "The agreement has been stored successfully in the SLA Repository Database with location:"
                   + location),
           location);
   logger.debug("EndOf createAgreement");
   return result;
 }
  /**
   * Gets the information of an specific agreement. If the agreement it is not
   * in the database, it returns 404 with empty payload
   *
   *
   * <pre>
   *  GET /agreements/{id}
   *
   *  Request:
   *    GET /agreements HTTP/1.1
   *
   *  Response:
   *    HTTP/1.1 200 OK
   *    Content-type: application/xml
   *
   *    <?xml version="1.0" encoding="UTF-8"?>
   *    <wsag:Agreement>...</wsag:Agreement>
   *
   *
   *
   * Example: <li>curl
   * http://localhost:8080/sla-service/agreements/agreement04</li>
   *
   *
   * @param id
   *            of the agreement
   * @return XML information with the different details of the agreement
   */
  @GET
  @Path("{id}")
  @Produces(MediaType.APPLICATION_XML)
  public Response getAgreementById(@PathParam("id") String agreement_id) {
    logger.debug("StartOf getAgreementById REQUEST for /agreements/" + agreement_id);

    Response result;

    try {
      AgreementHelper agreementRestService = getAgreementHelper();
      String serializedAgreement = agreementRestService.getAgreementByID(agreement_id);
      if (serializedAgreement != null) {
        result = buildResponse(200, serializedAgreement);
      } else {
        result =
            buildResponse(
                404,
                printError(
                    404,
                    "There are no getAgreements with agreementId "
                        + agreement_id
                        + " in the SLA Repository Database"));
      }
    } catch (HelperException e) {
      logger.info("getAgreementById exception:" + e.getMessage());
      result = buildResponse(e);
    }

    logger.debug("EndOf getAgreementById");
    return result;
  }
  /**
   * Deletes an agreement, passing the corresponding agreement_id as parameter.
   *
   * <pre>
   *  DELETE /agreements/{agreement_id}
   *
   *  Request:
   *      DELETE /agreements HTTP/1.1
   *
   *  Response:
   *    HTTP/1.1 200 Ok
   *    Content-type: application/xml
   *
   *  {@code
   *    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
   *    <message code="201" message= "The agreement has been deleted successfully in the SLA Repository Database"/>
   *  }
   *
   * </pre>
   *
   * Example:
   * <li>curl -X DELETE localhost:8080/sla-service/agreements/agreement04
   *
   * @throws Exception
   */
  @DELETE
  @Path("{agreementId}")
  @Produces(MediaType.APPLICATION_XML)
  public Response deleteAgreement(@PathParam("agreementId") String agreementId) {
    logger.debug("DELETE /agreements/" + agreementId);

    AgreementHelper agreementRestService = getAgreementHelper();
    boolean deleted = agreementRestService.deleteByAgreementId(agreementId);
    if (deleted)
      return buildResponse(
          204,
          printMessage(204, "The agreement with id:" + agreementId + " was deleted successfully"));
    else
      return buildResponse(
          404,
          printError(
              404,
              "There is no agreement with id " + agreementId + " in the SLA Repository Database"));
  }
  @GET
  @Path("active")
  @Produces(MediaType.APPLICATION_XML)
  public Response getActiveAgreements() {
    logger.debug("StartOf getActiveAgreements - Get active agreements");
    long actualDate = new Date().getTime();

    Response result;
    try {
      AgreementHelper agreementRestService = getAgreementHelper();
      String serializedAgreement = agreementRestService.getActiveAgreements(actualDate);
      result = buildResponse(200, serializedAgreement);
    } catch (HelperException e) {
      logger.info("getActiveAgreements exception:" + e.getMessage());
      return buildResponse(e);
    }
    logger.debug("EndOf getActiveAgreements");
    return result;
  }
  /**
   * Gets a the list of available agreements from where we can get metrics, host information, etc.
   *
   * <pre>
   *  GET /agreements{?providerId,consumerId,active}
   *
   *  Request:
   *   GET /agreements HTTP/1.1
   *
   *  Response:
   *   HTTP/1.1 200 OK
   *   Content-type: application/xml
   *   {@code
   *   <?xml version="1.0" encoding="UTF-8"?>
   *   <collection href="/agreements">
   *   <items offset="0" total="1">
   *   <wsag:Agreement xmlns:wsag="http://www.ggf.org/namespaces/ws-agreement"
   *     AgreementId="d25eea60-7cfe-11e3-baa7-0800200c9a66">
   *     ...
   *   </wsag:Agreement>
   *   </items>
   *   </collection>
   *   }
   *
   * </pre>
   *
   * Example:
   * <li>curl http://localhost:8080/sla-service/agreements
   * <li>curl http://localhost:8080/sla-service/agreements?consumerId=user-10343
   *
   * @throws JAXBException
   */
  @GET
  @Produces(MediaType.APPLICATION_XML)
  public Response getAgreements(
      @QueryParam("consumerId") String consumerId,
      @QueryParam("providerId") String providerId,
      @QueryParam("active") BooleanParam active) {
    logger.debug("StartOf getAgreements - REQUEST for /agreements");
    try {
      AgreementHelper agreementRestService = getAgreementHelper();
      String serializedAgreement =
          agreementRestService.getAgreements(consumerId, providerId, BooleanParam.getValue(active));

      Response result = buildResponse(200, serializedAgreement);
      logger.debug("EndOf getAgreements");
      return result;

    } catch (HelperException e) {
      logger.info("getAgreements exception:" + e.getMessage());
      return buildResponse(e);
    }
  }