use of i5.las2peer.api.execution.InvocationBadArgumentException in project Distributed-Noracle-Backend by Distributed-Noracle.
the class NoracleSpaceService method getSubscribers.
@Override
public SpaceSubscribersList getSubscribers(String spaceId) throws ServiceInvocationException {
if (spaceId == null || spaceId.isEmpty()) {
throw new InvocationBadArgumentException("No space id given");
}
try {
UserAgentImpl inviteAgent = this.getInviteAgent(spaceId);
GroupAgent memberAgent = this.getMemberAgent(spaceId);
memberAgent.unlock(Context.get().getMainAgent());
String[] memberIds = memberAgent.getMemberList();
SpaceSubscribersList subscribers = new SpaceSubscribersList();
for (int i = 0; i < memberIds.length; i++) {
if (!memberIds[i].equals(inviteAgent.getIdentifier())) {
UserAgentImpl agent = (UserAgentImpl) Context.get().fetchAgent(memberIds[i]);
NoracleAgentProfile profile = new NoracleAgentProfile();
profile.setName(agent.getLoginName());
subscribers.add(profile);
}
}
return subscribers;
} catch (Exception e) {
throw new ServiceInvocationException("Could not fetch list", e);
}
}
use of i5.las2peer.api.execution.InvocationBadArgumentException 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);
}
}
use of i5.las2peer.api.execution.InvocationBadArgumentException 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;
}
use of i5.las2peer.api.execution.InvocationBadArgumentException 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);
}
}
use of i5.las2peer.api.execution.InvocationBadArgumentException 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;
}
Aggregations