Search in sources :

Example 16 with ServiceNameVersion

use of i5.las2peer.api.p2p.ServiceNameVersion in project Distributed-Noracle-Backend by Distributed-Noracle.

the class QuestionsResource method getQuestionsWeb.

@GET
@Produces(MediaType.APPLICATION_JSON)
@ApiResponses({ @ApiResponse(code = HttpURLConnection.HTTP_OK, message = "A list of questions from the network", response = QuestionList.class), @ApiResponse(code = HttpURLConnection.HTTP_BAD_REQUEST, message = "No space id given", response = ExceptionEntity.class), @ApiResponse(code = HttpURLConnection.HTTP_FORBIDDEN, message = "Access Denied", response = ExceptionEntity.class), @ApiResponse(code = HttpURLConnection.HTTP_INTERNAL_ERROR, message = "Internal Server Error", response = ExceptionEntity.class) })
public Response getQuestionsWeb(@PathParam("spaceId") String spaceId, @QueryParam("order") String order, @QueryParam("limit") Integer limit, @QueryParam("startat") Integer startAt) throws ServiceInvocationException {
    QuestionList questionList = getQuestions(spaceId, order, limit, startAt);
    VotedQuestionList votedQuestionList = new VotedQuestionList();
    for (Question question : questionList) {
        VotedQuestion votedQuestion = new VotedQuestion(question);
        String objectId = QuestionVotesResource.buildObjectId(spaceId, question.getQuestionId());
        Serializable rmiResult = Context.get().invoke(new ServiceNameVersion(NoracleVoteService.class.getCanonicalName(), NoracleService.API_VERSION), "getAllVotes", objectId);
        if (rmiResult instanceof VoteList) {
            votedQuestion.setVotes((VoteList) rmiResult);
        }
        votedQuestionList.add(votedQuestion);
    }
    ResponseBuilder responseBuilder = Response.ok(votedQuestionList);
    String queryOrder = order != null ? "order=" + order : "";
    String queryLimit = limit != null ? "limit=" + Integer.toString(limit) : "";
    String queryStartAt = "";
    if (startAt != null && limit != null) {
        if (order.equalsIgnoreCase("desc")) {
            queryStartAt = "startat=" + Integer.toString(startAt - limit);
        } else {
            queryStartAt = "startat=" + Integer.toString(startAt + limit);
        }
    }
    String nextLinkStr = "";
    for (String param : new String[] { queryOrder, queryLimit, queryStartAt }) {
        if (param != null && !param.isEmpty()) {
            if (nextLinkStr.isEmpty()) {
                nextLinkStr += "?";
            } else {
                nextLinkStr += "&";
            }
            nextLinkStr += param;
        }
    }
    if (!nextLinkStr.isEmpty()) {
        responseBuilder.header(HttpHeaders.LINK, "<" + nextLinkStr + ">; rel=\"next\"");
    }
    return responseBuilder.build();
}
Also used : Serializable(java.io.Serializable) ServiceNameVersion(i5.las2peer.api.p2p.ServiceNameVersion) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) ApiResponses(io.swagger.annotations.ApiResponses)

Aggregations

ServiceNameVersion (i5.las2peer.api.p2p.ServiceNameVersion)16 Serializable (java.io.Serializable)13 InternalServiceException (i5.las2peer.api.execution.InternalServiceException)6 ApiResponses (io.swagger.annotations.ApiResponses)5 Gson (com.google.gson.Gson)3 Space (i5.las2peer.services.noracleService.model.Space)3 URI (java.net.URI)3 URISyntaxException (java.net.URISyntaxException)3 JSONObject (net.minidev.json.JSONObject)3 JSONParser (net.minidev.json.parser.JSONParser)3 ParseException (net.minidev.json.parser.ParseException)3 ServiceInvocationException (i5.las2peer.api.execution.ServiceInvocationException)2 Envelope (i5.las2peer.api.persistency.Envelope)2 EnvelopeAccessDeniedException (i5.las2peer.api.persistency.EnvelopeAccessDeniedException)2 EnvelopeOperationFailedException (i5.las2peer.api.persistency.EnvelopeOperationFailedException)2 Question (i5.las2peer.services.noracleService.model.Question)2 SpaceSubscription (i5.las2peer.services.noracleService.model.SpaceSubscription)2 Vote (i5.las2peer.services.noracleService.model.Vote)2 VoteList (i5.las2peer.services.noracleService.model.VoteList)2 Instant (java.time.Instant)2