Search in sources :

Example 11 with EnvelopeNotFoundException

use of i5.las2peer.api.persistency.EnvelopeNotFoundException 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 12 with EnvelopeNotFoundException

use of i5.las2peer.api.persistency.EnvelopeNotFoundException 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 13 with EnvelopeNotFoundException

use of i5.las2peer.api.persistency.EnvelopeNotFoundException in project Distributed-Noracle-Backend by Distributed-Noracle.

the class NoracleAgentService method updateSpaceSubscription.

@Override
public SpaceSubscription updateSpaceSubscription(String agentId, String spaceId, String[] selectedQuestions) throws ServiceInvocationException {
    Agent mainAgent = Context.get().getMainAgent();
    String envIdentifier = buildSubscriptionId(agentId);
    try {
        Envelope env = Context.get().requestEnvelope(envIdentifier);
        SpaceSubscriptionList spaceSubscriptionList = (SpaceSubscriptionList) env.getContent();
        for (SpaceSubscription spaceSubscription : spaceSubscriptionList) {
            if (spaceSubscription.getSpaceId().equals(spaceId)) {
                spaceSubscription.setSelectedQuestionIds(selectedQuestions);
                env.setContent(spaceSubscriptionList);
                try {
                    Context.get().storeEnvelope(env, mainAgent);
                } catch (EnvelopeAccessDeniedException e) {
                    throw new ServiceAccessDeniedException("Envelope Access Denied");
                } catch (EnvelopeOperationFailedException e) {
                    throw new InternalServiceException("Could not store space subscription envelope", e);
                }
                return spaceSubscription;
            }
        }
        throw new ResourceNotFoundException("Space subscription not found");
    } catch (EnvelopeAccessDeniedException e) {
        throw new ServiceAccessDeniedException("Envelope Access Denied");
    } catch (EnvelopeOperationFailedException e) {
        throw new InternalServiceException("Could not fetch space subscription envelope", e);
    } catch (EnvelopeNotFoundException e) {
        throw new ResourceNotFoundException("No space subscriptions found");
    }
}
Also used : AnonymousAgent(i5.las2peer.api.security.AnonymousAgent) Agent(i5.las2peer.api.security.Agent) EnvelopeOperationFailedException(i5.las2peer.api.persistency.EnvelopeOperationFailedException) EnvelopeNotFoundException(i5.las2peer.api.persistency.EnvelopeNotFoundException) SpaceSubscriptionList(i5.las2peer.services.noracleService.model.SpaceSubscriptionList) EnvelopeAccessDeniedException(i5.las2peer.api.persistency.EnvelopeAccessDeniedException) Envelope(i5.las2peer.api.persistency.Envelope) SpaceSubscription(i5.las2peer.services.noracleService.model.SpaceSubscription)

Example 14 with EnvelopeNotFoundException

use of i5.las2peer.api.persistency.EnvelopeNotFoundException in project Distributed-Noracle-Backend by Distributed-Noracle.

the class NoracleAgentService method subscribeToSpace.

@Override
public SpaceSubscription subscribeToSpace(String spaceId, String spaceSecret) throws ServiceInvocationException {
    Agent mainAgent = Context.get().getMainAgent();
    if (spaceId == null || spaceId.isEmpty()) {
        throw new InvocationBadArgumentException("No space id given");
    } else if (mainAgent instanceof AnonymousAgent) {
        throw new ServiceAccessDeniedException("You have to be logged in to subscribe to a space");
    }
    Context.get().invoke(new ServiceNameVersion(NoracleSpaceService.class.getCanonicalName(), NoracleService.API_VERSION), "joinSpace", spaceId, spaceSecret);
    SpaceSubscription subscription = new SpaceSubscription(spaceId, spaceSecret);
    String envIdentifier = buildSubscriptionId(mainAgent.getIdentifier());
    Envelope env;
    SpaceSubscriptionList subscriptionList;
    try {
        try {
            env = Context.get().requestEnvelope(envIdentifier);
            subscriptionList = (SpaceSubscriptionList) env.getContent();
        } catch (EnvelopeNotFoundException e) {
            env = Context.get().createEnvelope(envIdentifier);
            subscriptionList = new SpaceSubscriptionList();
        }
    } catch (EnvelopeAccessDeniedException e) {
        throw new ServiceAccessDeniedException("Envelope Access Denied");
    } catch (EnvelopeOperationFailedException e) {
        throw new InternalServiceException("Could not create envelope for space subscription", e);
    }
    subscriptionList.add(subscription);
    env.setContent(subscriptionList);
    try {
        Context.get().storeEnvelope(env, mainAgent);
    } catch (EnvelopeAccessDeniedException e) {
        throw new ServiceAccessDeniedException("Envelope Access Denied");
    } catch (EnvelopeOperationFailedException e) {
        throw new InternalServiceException("Could not store space subscription envelope", e);
    }
    return subscription;
}
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) SpaceSubscriptionList(i5.las2peer.services.noracleService.model.SpaceSubscriptionList) EnvelopeAccessDeniedException(i5.las2peer.api.persistency.EnvelopeAccessDeniedException) Envelope(i5.las2peer.api.persistency.Envelope) ServiceNameVersion(i5.las2peer.api.p2p.ServiceNameVersion) EnvelopeNotFoundException(i5.las2peer.api.persistency.EnvelopeNotFoundException) SpaceSubscription(i5.las2peer.services.noracleService.model.SpaceSubscription)

Example 15 with EnvelopeNotFoundException

use of i5.las2peer.api.persistency.EnvelopeNotFoundException in project Distributed-Noracle-Backend by Distributed-Noracle.

the class NoracleAgentService method updateAgentProfile.

@Override
public NoracleAgentProfile updateAgentProfile(String agentName) throws ServiceInvocationException {
    Agent mainAgent = Context.get().getMainAgent();
    String envIdentifier = buildAgentProfileId(mainAgent.getIdentifier());
    Envelope env;
    NoracleAgentProfile profile;
    // look for existing profile, otherwise create one
    try {
        try {
            env = Context.get().requestEnvelope(envIdentifier);
            profile = (NoracleAgentProfile) env.getContent();
        } catch (EnvelopeNotFoundException e) {
            env = Context.get().createEnvelope(envIdentifier);
            profile = new NoracleAgentProfile();
        }
    } catch (EnvelopeAccessDeniedException e) {
        throw new ServiceAccessDeniedException("Envelope access denied");
    } catch (EnvelopeOperationFailedException e) {
        throw new InternalServiceException("Could not create new envelope for noracle agent profile", e);
    }
    profile.setName(agentName);
    env.setContent(profile);
    env.setPublic();
    try {
        Context.get().storeEnvelope(env, mainAgent);
    } catch (EnvelopeAccessDeniedException e) {
        throw new ServiceAccessDeniedException("Envelope access denied");
    } catch (EnvelopeOperationFailedException e) {
        throw new InternalServiceException("Storing envelope with noracle agent profile failed", e);
    }
    return profile;
}
Also used : AnonymousAgent(i5.las2peer.api.security.AnonymousAgent) Agent(i5.las2peer.api.security.Agent) NoracleAgentProfile(i5.las2peer.services.noracleService.model.NoracleAgentProfile) EnvelopeOperationFailedException(i5.las2peer.api.persistency.EnvelopeOperationFailedException) EnvelopeNotFoundException(i5.las2peer.api.persistency.EnvelopeNotFoundException) EnvelopeAccessDeniedException(i5.las2peer.api.persistency.EnvelopeAccessDeniedException) Envelope(i5.las2peer.api.persistency.Envelope)

Aggregations

Envelope (i5.las2peer.api.persistency.Envelope)20 EnvelopeNotFoundException (i5.las2peer.api.persistency.EnvelopeNotFoundException)18 EnvelopeAccessDeniedException (i5.las2peer.api.persistency.EnvelopeAccessDeniedException)17 EnvelopeOperationFailedException (i5.las2peer.api.persistency.EnvelopeOperationFailedException)17 Agent (i5.las2peer.api.security.Agent)5 AnonymousAgent (i5.las2peer.api.security.AnonymousAgent)5 Question (i5.las2peer.services.noracleService.model.Question)4 Vote (i5.las2peer.services.noracleService.model.Vote)4 InternalServiceException (i5.las2peer.api.execution.InternalServiceException)3 SpaceSubscription (i5.las2peer.services.noracleService.model.SpaceSubscription)3 SpaceSubscriptionList (i5.las2peer.services.noracleService.model.SpaceSubscriptionList)3 InvocationBadArgumentException (i5.las2peer.api.execution.InvocationBadArgumentException)2 ServiceAccessDeniedException (i5.las2peer.api.execution.ServiceAccessDeniedException)2 ServiceInvocationException (i5.las2peer.api.execution.ServiceInvocationException)2 QuestionRelation (i5.las2peer.services.noracleService.model.QuestionRelation)2 VoteEntry (i5.las2peer.services.noracleService.model.VoteEntry)2 ServiceNameVersion (i5.las2peer.api.p2p.ServiceNameVersion)1 UserAgentImpl (i5.las2peer.security.UserAgentImpl)1 NoracleAgentProfile (i5.las2peer.services.noracleService.model.NoracleAgentProfile)1 QuestionRelationList (i5.las2peer.services.noracleService.model.QuestionRelationList)1