Search in sources :

Example 11 with Question

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

the class NoracleQuestionRelationService method changeQuestionRelation.

@Override
public QuestionRelation changeQuestionRelation(String relationId, String name, String questionId1, String questionId2, Boolean directed) throws ServiceInvocationException {
    if (relationId == null) {
        throw new InvocationBadArgumentException("No relation id given");
    }
    try {
        Envelope relationEnvelope = Context.get().requestEnvelope(getQuestionRelationEnvelopeIdentifier(relationId));
        QuestionRelation relation = (QuestionRelation) relationEnvelope.getContent();
        if (name != null) {
            relation.setName(name);
        }
        if (questionId1 != null) {
            relation.setFirstQuestionId(questionId1);
        }
        if (questionId2 != null) {
            relation.setSecondQuestionId(questionId2);
        }
        if (directed != null) {
            relation.setDirected(directed);
        }
        relation.setTimestampLastModified(Instant.now().toString());
        relationEnvelope.setContent(relation);
        Context.get().storeEnvelope(relationEnvelope);
        return relation;
    } catch (EnvelopeNotFoundException e) {
        throw new ResourceNotFoundException("Relation not found");
    } catch (EnvelopeAccessDeniedException e) {
        throw new ServiceAccessDeniedException("Envelope Access Denied");
    } catch (EnvelopeOperationFailedException e) {
        throw new InternalServiceException("Could not fetch question envelope", e);
    }
}
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)

Example 12 with Question

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

the class NoracleQuestionService method retrieveQuestion.

private boolean retrieveQuestion(QuestionList result, String spaceId, int questionNumber) throws EnvelopeNotFoundException {
    try {
        Envelope spaceQuestionEnv = Context.get().requestEnvelope(buildSpaceQuestionNumberId(spaceId, questionNumber));
        String questionId = (String) spaceQuestionEnv.getContent();
        Envelope questionEnv = Context.get().requestEnvelope(getQuestionEnvelopeIdentifier(questionId));
        Question question = (Question) questionEnv.getContent();
        // TODO check if author is a member of this space?
        // String authorId = question.getAuthorId();
        // if (authorId == null || authorId.isEmpty()) {
        // return false;
        // }
        result.add(question);
        return true;
    } catch (EnvelopeNotFoundException e) {
        throw e;
    } catch (Exception e) {
    // XXX logging
    }
    return false;
}
Also used : EnvelopeNotFoundException(i5.las2peer.api.persistency.EnvelopeNotFoundException) Question(i5.las2peer.services.noracleService.model.Question) Envelope(i5.las2peer.api.persistency.Envelope) EnvelopeAccessDeniedException(i5.las2peer.api.persistency.EnvelopeAccessDeniedException) EnvelopeOperationFailedException(i5.las2peer.api.persistency.EnvelopeOperationFailedException) EnvelopeNotFoundException(i5.las2peer.api.persistency.EnvelopeNotFoundException)

Example 13 with Question

use of i5.las2peer.services.noracleService.model.Question 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 14 with Question

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

the class QuestionRelationsResource method getQuestionText.

private String getQuestionText(String questionId) throws ServiceInvocationException {
    String qText = "";
    Serializable rmiResult = Context.get().invoke(new ServiceNameVersion(NoracleQuestionService.class.getCanonicalName(), NoracleService.API_VERSION), "getQuestion", questionId);
    if (rmiResult instanceof Question)
        qText = ((Question) rmiResult).getText();
    return qText;
}
Also used : Serializable(java.io.Serializable) ServiceNameVersion(i5.las2peer.api.p2p.ServiceNameVersion)

Example 15 with Question

use of i5.las2peer.services.noracleService.model.Question 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

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