Search in sources :

Example 6 with VoteList

use of i5.las2peer.services.noracleService.model.VoteList 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)4 VoteList (i5.las2peer.services.noracleService.model.VoteList)4 Serializable (java.io.Serializable)4 InternalServiceException (i5.las2peer.api.execution.InternalServiceException)3 Vote (i5.las2peer.services.noracleService.model.Vote)2 ApiResponses (io.swagger.annotations.ApiResponses)2 ResponseBuilder (javax.ws.rs.core.Response.ResponseBuilder)2 InvocationBadArgumentException (i5.las2peer.api.execution.InvocationBadArgumentException)1 ServiceAccessDeniedException (i5.las2peer.api.execution.ServiceAccessDeniedException)1 ServiceInvocationException (i5.las2peer.api.execution.ServiceInvocationException)1 Envelope (i5.las2peer.api.persistency.Envelope)1 EnvelopeAccessDeniedException (i5.las2peer.api.persistency.EnvelopeAccessDeniedException)1 EnvelopeNotFoundException (i5.las2peer.api.persistency.EnvelopeNotFoundException)1 EnvelopeOperationFailedException (i5.las2peer.api.persistency.EnvelopeOperationFailedException)1 Test (org.junit.Test)1