Search in sources :

Example 6 with Vote

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

the class NoracleVoteService method setVote.

@Override
public Vote setVote(String agentId, String objectId, int vote) throws ServiceInvocationException {
    Agent mainAgent = Context.get().getMainAgent();
    if (objectId == null || objectId.isEmpty()) {
        throw new InvocationBadArgumentException("No object id given");
    } else if (mainAgent instanceof AnonymousAgent) {
        throw new ServiceAccessDeniedException("You have to be logged in to vote");
    }
    String persEnvId = getAgentVoteEnvelopeIdentifier(agentId, objectId);
    try {
        try {
            Envelope persEnv = Context.get().requestEnvelope(persEnvId);
            VoteEntry voteEntry = (VoteEntry) persEnv.getContent();
            int pubIndex = voteEntry.getPubIndex();
            String pubEnvId = getPublicVoteEnvelopeIdentifier(objectId, pubIndex);
            Vote pubVote = updateOrCreatePubVoteEnv(pubEnvId, vote, mainAgent.getIdentifier());
            voteEntry = (VoteEntry) persEnv.getContent();
            voteEntry.setVote(pubVote);
            persEnv.setContent(voteEntry);
            Context.get().storeEnvelope(persEnv);
            return pubVote;
        } catch (EnvelopeNotFoundException e) {
            Envelope persEnv = Context.get().createEnvelope(persEnvId);
            String pubEnvId = null;
            int pubIndex = -1;
            for (int num = 1; num < MAX_VOTES_PER_OBJECT; num++) {
                try {
                    pubEnvId = getPublicVoteEnvelopeIdentifier(objectId, num);
                    Context.get().requestEnvelope(pubEnvId);
                } catch (EnvelopeNotFoundException e2) {
                    // found non taken vote index
                    pubIndex = num;
                    break;
                } catch (Exception e2) {
                // XXX logging
                }
            }
            if (pubEnvId == null || pubEnvId.isEmpty()) {
                throw new InternalServiceException("Public envelope id is null");
            } else if (pubIndex == -1) {
                throw new InternalServiceException("Too many votes for this object");
            }
            Vote pubVote = updateOrCreatePubVoteEnv(pubEnvId, vote, mainAgent.getIdentifier());
            VoteEntry voteEntry = new VoteEntry(objectId, pubIndex, pubVote);
            persEnv.setContent(voteEntry);
            Context.get().storeEnvelope(persEnv);
            return pubVote;
        }
    } catch (EnvelopeAccessDeniedException e) {
        throw new ServiceAccessDeniedException("Envelope Access Denied");
    } catch (EnvelopeOperationFailedException e) {
        throw new InternalServiceException("Could not create envelope for vote", e);
    }
}
Also used : AnonymousAgent(i5.las2peer.api.security.AnonymousAgent) Agent(i5.las2peer.api.security.Agent) Vote(i5.las2peer.services.noracleService.model.Vote) VoteEntry(i5.las2peer.services.noracleService.model.VoteEntry) EnvelopeOperationFailedException(i5.las2peer.api.persistency.EnvelopeOperationFailedException) InvocationBadArgumentException(i5.las2peer.api.execution.InvocationBadArgumentException) AnonymousAgent(i5.las2peer.api.security.AnonymousAgent) ServiceAccessDeniedException(i5.las2peer.api.execution.ServiceAccessDeniedException) EnvelopeAccessDeniedException(i5.las2peer.api.persistency.EnvelopeAccessDeniedException) 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) InternalServiceException(i5.las2peer.api.execution.InternalServiceException) EnvelopeNotFoundException(i5.las2peer.api.persistency.EnvelopeNotFoundException)

Example 7 with Vote

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

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

the class NoracleVoteService method updateOrCreatePubVoteEnv.

private Vote updateOrCreatePubVoteEnv(String pubEnvId, int vote, String agentId) throws EnvelopeAccessDeniedException, EnvelopeOperationFailedException {
    Vote pubVote;
    try {
        Envelope pubEnv = Context.get().requestEnvelope(pubEnvId);
        pubVote = (Vote) pubEnv.getContent();
        pubVote.setValue(vote);
        pubEnv.setContent(pubVote);
        Context.get().storeEnvelope(pubEnv);
    } catch (EnvelopeNotFoundException e) {
        Envelope pubEnv = Context.get().createEnvelope(pubEnvId);
        pubEnv.setPublic();
        pubVote = new Vote(vote, agentId);
        pubEnv.setContent(pubVote);
        Context.get().storeEnvelope(pubEnv);
    }
    return pubVote;
}
Also used : Vote(i5.las2peer.services.noracleService.model.Vote) EnvelopeNotFoundException(i5.las2peer.api.persistency.EnvelopeNotFoundException) Envelope(i5.las2peer.api.persistency.Envelope)

Example 9 with Vote

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

Example 10 with Vote

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

the class NoracleVoteService method getAgentVote.

@Override
public Vote getAgentVote(String objectId, String agentId) throws ServiceInvocationException {
    String persEnvId = getAgentVoteEnvelopeIdentifier(agentId, objectId);
    try {
        Envelope persEnv = Context.get().requestEnvelope(persEnvId);
        VoteEntry voteEntry = (VoteEntry) persEnv.getContent();
        return voteEntry.getVote();
    } catch (EnvelopeAccessDeniedException e) {
        throw new InternalServiceException("Someone hijacked your vote envelope", e);
    } catch (EnvelopeNotFoundException e) {
        return new Vote(0, agentId);
    } catch (EnvelopeOperationFailedException e) {
        throw new InternalServiceException("Retrieving vote failed", e);
    }
}
Also used : Vote(i5.las2peer.services.noracleService.model.Vote) VoteEntry(i5.las2peer.services.noracleService.model.VoteEntry) EnvelopeOperationFailedException(i5.las2peer.api.persistency.EnvelopeOperationFailedException) EnvelopeNotFoundException(i5.las2peer.api.persistency.EnvelopeNotFoundException) EnvelopeAccessDeniedException(i5.las2peer.api.persistency.EnvelopeAccessDeniedException) Envelope(i5.las2peer.api.persistency.Envelope) InternalServiceException(i5.las2peer.api.execution.InternalServiceException)

Aggregations

InternalServiceException (i5.las2peer.api.execution.InternalServiceException)7 Vote (i5.las2peer.services.noracleService.model.Vote)7 ServiceNameVersion (i5.las2peer.api.p2p.ServiceNameVersion)4 Envelope (i5.las2peer.api.persistency.Envelope)4 EnvelopeNotFoundException (i5.las2peer.api.persistency.EnvelopeNotFoundException)4 VoteList (i5.las2peer.services.noracleService.model.VoteList)4 Serializable (java.io.Serializable)4 ServiceInvocationException (i5.las2peer.api.execution.ServiceInvocationException)3 EnvelopeAccessDeniedException (i5.las2peer.api.persistency.EnvelopeAccessDeniedException)3 EnvelopeOperationFailedException (i5.las2peer.api.persistency.EnvelopeOperationFailedException)3 InvocationBadArgumentException (i5.las2peer.api.execution.InvocationBadArgumentException)2 ServiceAccessDeniedException (i5.las2peer.api.execution.ServiceAccessDeniedException)2 VoteEntry (i5.las2peer.services.noracleService.model.VoteEntry)2 Gson (com.google.gson.Gson)1 Agent (i5.las2peer.api.security.Agent)1 AnonymousAgent (i5.las2peer.api.security.AnonymousAgent)1 ApiResponses (io.swagger.annotations.ApiResponses)1 JSONObject (net.minidev.json.JSONObject)1 JSONParser (net.minidev.json.parser.JSONParser)1 ParseException (net.minidev.json.parser.ParseException)1