Search in sources :

Example 16 with ServiceInvocationException

use of i5.las2peer.api.execution.ServiceInvocationException in project Distributed-Noracle-Backend by Distributed-Noracle.

the class NoracleVoteService method getAllVotes.

@Override
public VoteList getAllVotes(String objectId) throws ServiceInvocationException {
    VoteList result = new VoteList();
    for (int num = 1; num < MAX_VOTES_PER_OBJECT; num++) {
        try {
            String pubEnvId = getPublicVoteEnvelopeIdentifier(objectId, num);
            Envelope pubEnv = Context.get().requestEnvelope(pubEnvId);
            Vote vote = (Vote) pubEnv.getContent();
            int normalizedVal = vote.getValue();
            if (normalizedVal > 1) {
                normalizedVal = 1;
            } else if (normalizedVal < -1) {
                normalizedVal = -1;
            }
            Vote normalizedVote = new Vote(normalizedVal, vote.getVoterAgentId());
            result.add(normalizedVote);
        } catch (EnvelopeNotFoundException e) {
            logger.warning("EnvelopeNotFoundException inside NoracleVoteService.getAllVotes(...): " + e.getMessage());
            break;
        } catch (Exception e) {
            logger.warning("Exception inside NoracleVoteService.getAllVotes(...): " + e.getMessage());
        }
    }
    return result;
}
Also used : Vote(i5.las2peer.services.noracleService.model.Vote) EnvelopeNotFoundException(i5.las2peer.api.persistency.EnvelopeNotFoundException) VoteList(i5.las2peer.services.noracleService.model.VoteList) Envelope(i5.las2peer.api.persistency.Envelope) EnvelopeAccessDeniedException(i5.las2peer.api.persistency.EnvelopeAccessDeniedException) EnvelopeOperationFailedException(i5.las2peer.api.persistency.EnvelopeOperationFailedException) InvocationBadArgumentException(i5.las2peer.api.execution.InvocationBadArgumentException) ServiceAccessDeniedException(i5.las2peer.api.execution.ServiceAccessDeniedException) ServiceInvocationException(i5.las2peer.api.execution.ServiceInvocationException) InternalServiceException(i5.las2peer.api.execution.InternalServiceException) EnvelopeNotFoundException(i5.las2peer.api.persistency.EnvelopeNotFoundException)

Example 17 with ServiceInvocationException

use of i5.las2peer.api.execution.ServiceInvocationException 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 18 with ServiceInvocationException

use of i5.las2peer.api.execution.ServiceInvocationException 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 19 with ServiceInvocationException

use of i5.las2peer.api.execution.ServiceInvocationException 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 20 with ServiceInvocationException

use of i5.las2peer.api.execution.ServiceInvocationException in project Distributed-Noracle-Backend by Distributed-Noracle.

the class RelationVotesResource method getAllVotes.

@Override
public VoteList getAllVotes(String objectId) throws ServiceInvocationException {
    Serializable rmiResult = Context.get().invoke(new ServiceNameVersion(NoracleVoteService.class.getCanonicalName(), NoracleService.API_VERSION), "getAllVotes", objectId);
    VoteList vote;
    if (rmiResult instanceof VoteList) {
        vote = (VoteList) rmiResult;
    } else {
        throw new InternalServiceException("Unexpected result (" + rmiResult.getClass().getCanonicalName() + ") of RMI call");
    }
    return vote;
}
Also used : Serializable(java.io.Serializable) ServiceNameVersion(i5.las2peer.api.p2p.ServiceNameVersion) VoteList(i5.las2peer.services.noracleService.model.VoteList) InternalServiceException(i5.las2peer.api.execution.InternalServiceException)

Aggregations

EnvelopeAccessDeniedException (i5.las2peer.api.persistency.EnvelopeAccessDeniedException)19 EnvelopeOperationFailedException (i5.las2peer.api.persistency.EnvelopeOperationFailedException)19 Envelope (i5.las2peer.api.persistency.Envelope)17 EnvelopeNotFoundException (i5.las2peer.api.persistency.EnvelopeNotFoundException)16 ServiceNameVersion (i5.las2peer.api.p2p.ServiceNameVersion)15 Serializable (java.io.Serializable)13 InternalServiceException (i5.las2peer.api.execution.InternalServiceException)9 ApiResponses (io.swagger.annotations.ApiResponses)7 ServiceInvocationException (i5.las2peer.api.execution.ServiceInvocationException)6 Agent (i5.las2peer.api.security.Agent)6 AnonymousAgent (i5.las2peer.api.security.AnonymousAgent)6 Gson (com.google.gson.Gson)5 Question (i5.las2peer.services.noracleService.model.Question)5 Space (i5.las2peer.services.noracleService.model.Space)5 Vote (i5.las2peer.services.noracleService.model.Vote)5 JSONObject (net.minidev.json.JSONObject)5 JSONParser (net.minidev.json.parser.JSONParser)5 ParseException (net.minidev.json.parser.ParseException)5 SpaceSubscription (i5.las2peer.services.noracleService.model.SpaceSubscription)4 SerializationException (i5.las2peer.serialization.SerializationException)3