Search in sources :

Example 1 with QuestionRelation

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

the class QuestionRelationsResource method createQuestionRelation.

@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.TEXT_HTML)
@ApiResponses({ @ApiResponse(code = HttpURLConnection.HTTP_CREATED, message = "Relation successfully created"), @ApiResponse(code = HttpURLConnection.HTTP_UNAUTHORIZED, message = "You have to be logged in to create a relation", response = ExceptionEntity.class), @ApiResponse(code = HttpURLConnection.HTTP_INTERNAL_ERROR, message = "Internal Server Error", response = ExceptionEntity.class) })
public Response createQuestionRelation(@PathParam("spaceId") String spaceId, @ApiParam(required = true) CreateRelationPojo createRelationPojo) throws ServiceInvocationException {
    QuestionRelation rel = createQuestionRelation(spaceId, createRelationPojo.getName(), createRelationPojo.getFirstQuestionId(), createRelationPojo.getSecondQuestionId(), createRelationPojo.isDirected());
    try {
        Gson gson = new Gson();
        String createRelationPojoJson = gson.toJson(createRelationPojo);
        JSONParser p = new JSONParser(JSONParser.MODE_PERMISSIVE);
        JSONObject obj = (JSONObject) p.parse(createRelationPojoJson);
        obj.put("spaceId", spaceId);
        obj.put("uid", Context.getCurrent().getMainAgent().getIdentifier());
        Context.get().monitorEvent(MonitoringEvent.SERVICE_CUSTOM_MESSAGE_3, obj.toJSONString());
        // Try to log training data
        try {
            String from = getQuestionText(createRelationPojo.getFirstQuestionId());
            String to = getQuestionText(createRelationPojo.getSecondQuestionId());
            JSONObject trainingData = new JSONObject();
            trainingData.put("unit", spaceId);
            trainingData.put("from", from);
            trainingData.put("to", to);
            if (createRelationPojo.isDirected())
                Context.get().monitorEvent(MonitoringEvent.SERVICE_CUSTOM_MESSAGE_40, trainingData.toString());
            else
                Context.get().monitorEvent(MonitoringEvent.SERVICE_CUSTOM_MESSAGE_41, trainingData.toString());
        } catch (ServiceInvocationException | NullPointerException e) {
            Context.get().monitorEvent(MonitoringEvent.SERVICE_CUSTOM_ERROR_40, e.getMessage());
        }
        Question q1 = null;
        Question q2 = null;
        Serializable rmiResult = Context.get().invoke(new ServiceNameVersion(NoracleQuestionService.class.getCanonicalName(), NoracleService.API_VERSION), "getQuestion", createRelationPojo.getFirstQuestionId());
        if (rmiResult instanceof Question) {
            q1 = (Question) rmiResult;
        }
        Serializable rmiResult2 = Context.get().invoke(new ServiceNameVersion(NoracleQuestionService.class.getCanonicalName(), NoracleService.API_VERSION), "getQuestion", createRelationPojo.getSecondQuestionId());
        if (rmiResult instanceof Question) {
            q2 = (Question) rmiResult2;
        }
        if (q1 != null && q2 != null) {
            try {
                Instant timestamp = Instant.parse(q1.getTimestampCreated());
                Instant timestamp2 = Instant.parse(q2.getTimestampCreated());
                if (timestamp.isBefore(timestamp2) && q2.getDepth() < 1) {
                    changeQuestionDepth(q2.getQuestionId(), q1.getDepth() + 1);
                    JSONObject obj2 = new JSONObject();
                    obj2.put("spaceId", spaceId);
                    obj2.put("qid", q2.getQuestionId());
                    obj2.put("uid", Context.getCurrent().getMainAgent().getIdentifier());
                    obj2.put("depth", q1.getDepth() + 1);
                    Context.get().monitorEvent(MonitoringEvent.SERVICE_CUSTOM_MESSAGE_10, obj2.toJSONString());
                } else {
                    if (q1.getDepth() < 1) {
                        changeQuestionDepth(q1.getQuestionId(), q2.getDepth() + 1);
                        JSONObject obj2 = new JSONObject();
                        obj2.put("spaceId", spaceId);
                        obj2.put("qid", q1.getQuestionId());
                        obj2.put("uid", Context.getCurrent().getMainAgent().getIdentifier());
                        obj2.put("depth", q2.getDepth() + 1);
                        Context.get().monitorEvent(MonitoringEvent.SERVICE_CUSTOM_MESSAGE_10, obj2.toJSONString());
                    }
                }
            } catch (Exception e) {
            }
        }
        return Response.created(new URI(null, null, SpacesResource.RESOURCE_NAME + "/" + spaceId + "/" + RESOURCE_NAME + "/" + rel.getRelationId(), null)).build();
    } catch (URISyntaxException | ParseException e) {
        throw new InternalServerErrorException(e);
    }
}
Also used : Serializable(java.io.Serializable) Instant(java.time.Instant) Gson(com.google.gson.Gson) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) URISyntaxException(java.net.URISyntaxException) ServiceInvocationException(i5.las2peer.api.execution.ServiceInvocationException) ParseException(net.minidev.json.parser.ParseException) InternalServiceException(i5.las2peer.api.execution.InternalServiceException) JSONObject(net.minidev.json.JSONObject) ServiceNameVersion(i5.las2peer.api.p2p.ServiceNameVersion) JSONParser(net.minidev.json.parser.JSONParser) ParseException(net.minidev.json.parser.ParseException) ServiceInvocationException(i5.las2peer.api.execution.ServiceInvocationException) ApiResponses(io.swagger.annotations.ApiResponses)

Example 2 with QuestionRelation

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

the class NoracleQuestionRelationService method getQuestionRelations.

@Override
public QuestionRelationList getQuestionRelations(String spaceId, String order, Integer limit, Integer startAt) throws ServiceInvocationException {
    if (spaceId == null || spaceId.isEmpty()) {
        throw new InvocationBadArgumentException("No space id given");
    } else if (limit != null && limit <= 0) {
        throw new InvocationBadArgumentException("Invalid limit given");
    } else if (startAt != null && startAt < 1) {
        throw new InvocationBadArgumentException("Invalid startAt given");
    }
    if (order == null || order.isEmpty()) {
        order = "asc";
    }
    if (limit == null) {
        limit = 10;
    }
    if (startAt == null) {
        startAt = 1;
    }
    int direction = order.equalsIgnoreCase("desc") ? -1 : 1;
    QuestionRelationList result = new QuestionRelationList();
    for (int relationNumber = startAt; relationNumber < startAt + direction * limit; relationNumber += direction) {
        try {
            Envelope spaceQuestionRelationEnv;
            try {
                spaceQuestionRelationEnv = Context.get().requestEnvelope(buildSpaceQuestionRelationNumberId(spaceId, relationNumber));
            } catch (EnvelopeNotFoundException e) {
                break;
            }
            String questionId = (String) spaceQuestionRelationEnv.getContent();
            Envelope questionEnv = Context.get().requestEnvelope(getQuestionRelationEnvelopeIdentifier(questionId));
            result.add((QuestionRelation) questionEnv.getContent());
        } catch (Exception e) {
        // XXX logging
        }
    }
    return result;
}
Also used : EnvelopeNotFoundException(i5.las2peer.api.persistency.EnvelopeNotFoundException) QuestionRelationList(i5.las2peer.services.noracleService.model.QuestionRelationList) 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 3 with QuestionRelation

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

the class NoracleQuestionRelationService method createQuestionRelation.

@Override
public QuestionRelation createQuestionRelation(String spaceId, String name, String questionId1, String questionId2, Boolean directed) throws ServiceInvocationException {
    Agent mainAgent = Context.get().getMainAgent();
    if (mainAgent instanceof AnonymousAgent) {
        throw new ServiceAccessDeniedException("You have to be logged in to create a relation");
    }
    String relationId = buildQuestionRelationId();
    Envelope env;
    try {
        env = Context.get().createEnvelope(getQuestionRelationEnvelopeIdentifier(relationId));
    } catch (EnvelopeAccessDeniedException e) {
        throw new ServiceAccessDeniedException("Envelope Access Denied");
    } catch (EnvelopeOperationFailedException e) {
        throw new InternalServiceException("Could not create envelope for relation", e);
    }
    env.setPublic();
    String strNow = Instant.now().toString();
    QuestionRelation relation = new QuestionRelation(relationId, spaceId, mainAgent.getIdentifier(), name, questionId1, questionId2, directed, strNow);
    env.setContent(relation);
    try {
        Context.get().storeEnvelope(env, mainAgent);
    } catch (EnvelopeAccessDeniedException e) {
        throw new ServiceAccessDeniedException("Envelope Access Denied");
    } catch (EnvelopeOperationFailedException e) {
        throw new InternalServiceException("Could not store relation envelope", e);
    }
    if (spaceId != null && !spaceId.isEmpty()) {
        linkQuestionRelationToSpace(spaceId, relationId);
    }
    return relation;
}
Also used : AnonymousAgent(i5.las2peer.api.security.AnonymousAgent) Agent(i5.las2peer.api.security.Agent) EnvelopeOperationFailedException(i5.las2peer.api.persistency.EnvelopeOperationFailedException) AnonymousAgent(i5.las2peer.api.security.AnonymousAgent) QuestionRelation(i5.las2peer.services.noracleService.model.QuestionRelation) EnvelopeAccessDeniedException(i5.las2peer.api.persistency.EnvelopeAccessDeniedException) Envelope(i5.las2peer.api.persistency.Envelope)

Example 4 with QuestionRelation

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

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

the class NoracleServiceTest method testQuestionRelations.

@Test
public void testQuestionRelations() {
    try {
        String testSpaceId = createAndFetchTestSpace().getSpaceId();
        String questionId1 = createTestQuestion(testSpaceId);
        String questionId2 = createTestQuestion(testSpaceId);
        QuestionRelation questionRelation = createTestQuestionRelation(testSpaceId, questionId1, questionId2);
        // TODO: fetch and test list
        // Assert.assertEquals(1, questionRelationList.size());
        Assert.assertEquals(testAgent.getIdentifier(), questionRelation.getAuthorId());
        Assert.assertEquals("duplicate", questionRelation.getName());
        Assert.assertEquals(questionId1, questionRelation.getFirstQuestionId());
        Assert.assertEquals(questionId2, questionRelation.getSecondQuestionId());
        Assert.assertEquals(false, questionRelation.isDirected());
    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail(e.toString());
    }
}
Also used : QuestionRelation(i5.las2peer.services.noracleService.model.QuestionRelation) Test(org.junit.Test)

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