Search in sources :

Example 6 with QuestionRelation

use of i5.las2peer.services.noracleService.model.QuestionRelation in project Distributed-Noracle-Backend by Distributed-Noracle.

the class QuestionRelationsResource method changeQuestionRelation.

@PUT
@Path("/{relationId}")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@ApiResponses({ @ApiResponse(code = HttpURLConnection.HTTP_OK, message = "Changes a question relation", response = QuestionRelation.class), @ApiResponse(code = HttpURLConnection.HTTP_BAD_REQUEST, message = "No relation id given", response = ExceptionEntity.class), @ApiResponse(code = HttpURLConnection.HTTP_FORBIDDEN, message = "Access Denied", response = ExceptionEntity.class), @ApiResponse(code = HttpURLConnection.HTTP_NOT_FOUND, message = "Relation Not Found", response = ExceptionEntity.class), @ApiResponse(code = HttpURLConnection.HTTP_INTERNAL_ERROR, message = "Internal Server Error", response = ExceptionEntity.class) })
public QuestionRelation changeQuestionRelation(@PathParam("relationId") String relationId, @ApiParam(required = true) ChangeQuestionRelationPojo changeQuestionRelationPojo) throws ServiceInvocationException {
    Gson gson = new Gson();
    String changeRelationPojoJson = gson.toJson(changeQuestionRelationPojo);
    JSONParser p = new JSONParser(JSONParser.MODE_PERMISSIVE);
    try {
        JSONObject obj = (JSONObject) p.parse(changeRelationPojoJson);
        obj.put("relId", relationId);
        obj.put("uid", Context.getCurrent().getMainAgent().getIdentifier());
        Context.get().monitorEvent(MonitoringEvent.SERVICE_CUSTOM_MESSAGE_4, obj.toJSONString());
    } catch (ParseException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    // Try to log training data
    try {
        String from = getQuestionText(changeQuestionRelationPojo.getQuestionId1());
        String to = getQuestionText(changeQuestionRelationPojo.getQuestionId2());
        JSONObject trainingData = new JSONObject();
        trainingData.put("from", from);
        trainingData.put("to", to);
        if (changeQuestionRelationPojo.getDirected())
            Context.get().monitorEvent(MonitoringEvent.SERVICE_CUSTOM_MESSAGE_42, trainingData.toString());
        else
            Context.get().monitorEvent(MonitoringEvent.SERVICE_CUSTOM_MESSAGE_43, trainingData.toString());
    } catch (ServiceInvocationException e) {
    /* getDirected will return null at this point */
    // Context.get().monitorEvent(MonitoringEvent.SERVICE_CUSTOM_ERROR_42,
    // changeQuestionRelationPojo.getDirected().toString());
    }
    return changeQuestionRelation(relationId, changeQuestionRelationPojo.getName(), changeQuestionRelationPojo.getQuestionId1(), changeQuestionRelationPojo.getQuestionId2(), changeQuestionRelationPojo.getDirected());
}
Also used : JSONObject(net.minidev.json.JSONObject) Gson(com.google.gson.Gson) JSONParser(net.minidev.json.parser.JSONParser) ParseException(net.minidev.json.parser.ParseException) ServiceInvocationException(i5.las2peer.api.execution.ServiceInvocationException) ApiResponses(io.swagger.annotations.ApiResponses)

Example 7 with QuestionRelation

use of i5.las2peer.services.noracleService.model.QuestionRelation in project Distributed-Noracle-Backend by Distributed-Noracle.

the class QuestionRelationsResource method getQuestions.

@GET
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@ApiResponses({ @ApiResponse(code = HttpURLConnection.HTTP_OK, message = "A list of relations from the network", response = QuestionRelationList.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 getQuestions(@PathParam("spaceId") String spaceId, @QueryParam("order") String order, @QueryParam("limit") Integer limit, @QueryParam("startAt") Integer startAt) throws ServiceInvocationException {
    QuestionRelationList questionRelationList = getQuestionRelations(spaceId, order, limit, startAt);
    VotedQuestionRelationList votedQuestionRelationList = new VotedQuestionRelationList();
    for (QuestionRelation questionRelation : questionRelationList) {
        VotedQuestionRelation votedQuestionRelation = new VotedQuestionRelation(questionRelation);
        String objectId = RelationVotesResource.buildObjectId(spaceId, questionRelation.getRelationId());
        Serializable rmiResult = Context.get().invoke(new ServiceNameVersion(NoracleVoteService.class.getCanonicalName(), NoracleService.API_VERSION), "getAllVotes", objectId);
        if (rmiResult instanceof VoteList) {
            votedQuestionRelation.setVotes((VoteList) rmiResult);
        }
        votedQuestionRelationList.add(votedQuestionRelation);
    }
    ResponseBuilder responseBuilder = Response.ok(votedQuestionRelationList);
    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)

Example 8 with QuestionRelation

use of i5.las2peer.services.noracleService.model.QuestionRelation in project Distributed-Noracle-Backend by Distributed-Noracle.

the class NoracleQuestionRelationService method getQuestionRelation.

@Override
public QuestionRelation getQuestionRelation(String relationId) throws ServiceInvocationException {
    if (relationId == null || relationId.isEmpty()) {
        throw new InvocationBadArgumentException("No relation id given");
    }
    Envelope env;
    try {
        env = Context.get().requestEnvelope(getQuestionRelationEnvelopeIdentifier(relationId));
    } catch (EnvelopeAccessDeniedException e) {
        throw new ServiceAccessDeniedException("Envelope Access Denied");
    } catch (EnvelopeOperationFailedException e) {
        throw new InternalServiceException("Could not fetch relation envelope", e);
    } catch (EnvelopeNotFoundException e) {
        throw new ResourceNotFoundException("Relation Not Found");
    }
    QuestionRelation relation = (QuestionRelation) env.getContent();
    return relation;
}
Also used : EnvelopeOperationFailedException(i5.las2peer.api.persistency.EnvelopeOperationFailedException) EnvelopeNotFoundException(i5.las2peer.api.persistency.EnvelopeNotFoundException) QuestionRelation(i5.las2peer.services.noracleService.model.QuestionRelation) EnvelopeAccessDeniedException(i5.las2peer.api.persistency.EnvelopeAccessDeniedException) Envelope(i5.las2peer.api.persistency.Envelope)

Aggregations

Envelope (i5.las2peer.api.persistency.Envelope)4 EnvelopeAccessDeniedException (i5.las2peer.api.persistency.EnvelopeAccessDeniedException)4 EnvelopeOperationFailedException (i5.las2peer.api.persistency.EnvelopeOperationFailedException)4 QuestionRelation (i5.las2peer.services.noracleService.model.QuestionRelation)4 EnvelopeNotFoundException (i5.las2peer.api.persistency.EnvelopeNotFoundException)3 ApiResponses (io.swagger.annotations.ApiResponses)3 Gson (com.google.gson.Gson)2 ServiceInvocationException (i5.las2peer.api.execution.ServiceInvocationException)2 ServiceNameVersion (i5.las2peer.api.p2p.ServiceNameVersion)2 Serializable (java.io.Serializable)2 JSONObject (net.minidev.json.JSONObject)2 JSONParser (net.minidev.json.parser.JSONParser)2 ParseException (net.minidev.json.parser.ParseException)2 InternalServiceException (i5.las2peer.api.execution.InternalServiceException)1 Agent (i5.las2peer.api.security.Agent)1 AnonymousAgent (i5.las2peer.api.security.AnonymousAgent)1 QuestionRelationList (i5.las2peer.services.noracleService.model.QuestionRelationList)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 Instant (java.time.Instant)1