Ejemplo n.º 1
0
  @Override
  public StatementLRSResponse saveStatement(Statement statement) {
    StatementLRSResponse lrsResponse = new StatementLRSResponse();
    lrsResponse.setRequest(new HTTPRequest());

    lrsResponse.getRequest().setResource("statements");
    lrsResponse.getRequest().setContentType("application/json");

    try {
      lrsResponse
          .getRequest()
          .setContent(statement.toJSON(this.getVersion(), this.usePrettyJSON()).getBytes("UTF-8"));
    } catch (IOException ex) {
      lrsResponse.setErrMsg("Exception: " + ex.toString());
      return lrsResponse;
    }

    if (statement.getId() == null) {
      lrsResponse.getRequest().setMethod(HttpMethods.POST);
    } else {
      lrsResponse.getRequest().setMethod(HttpMethods.PUT);
      lrsResponse.getRequest().setQueryParams(new HashMap<String, String>());
      lrsResponse.getRequest().getQueryParams().put("statementId", statement.getId().toString());
    }

    lrsResponse.setResponse(makeSyncRequest(lrsResponse.getRequest()));
    int status = lrsResponse.getResponse().getStatus();

    lrsResponse.setContent(statement);

    // TODO: handle 409 conflict, etc.
    if (status == 200) {
      lrsResponse.setSuccess(true);
      String content = lrsResponse.getResponse().getContent();
      try {
        lrsResponse
            .getContent()
            .setId(
                UUID.fromString(
                    Mapper.getInstance().readValue(content, ArrayNode.class).get(0).textValue()));
      } catch (Exception ex) {
        lrsResponse.setErrMsg("Exception: " + ex.toString());
        lrsResponse.setSuccess(false);
      }
    } else if (status == 204) {
      lrsResponse.setSuccess(true);
    } else {
      lrsResponse.setSuccess(false);
    }

    return lrsResponse;
  }
Ejemplo n.º 2
0
  private StatementLRSResponse getStatement(String id, String paramName) {
    HTTPRequest request = new HTTPRequest();
    request.setMethod(HttpMethods.GET);
    request.setResource("statements");
    request.setQueryParams(new HashMap<String, String>());
    request.getQueryParams().put(paramName, id);

    HTTPResponse response = makeSyncRequest(request);
    int status = response.getStatus();

    StatementLRSResponse lrsResponse = new StatementLRSResponse(request, response);

    if (status == 200) {
      lrsResponse.setSuccess(true);
      try {
        lrsResponse.setContent(new Statement(new StringOfJSON(response.getContent())));
      } catch (Exception ex) {
        lrsResponse.setErrMsg("Exception: " + ex.toString());
        lrsResponse.setSuccess(false);
      }
    } else {
      lrsResponse.setSuccess(false);
    }

    return lrsResponse;
  }