use of i5.las2peer.api.execution.InternalServiceException in project Distributed-Noracle-Backend by Distributed-Noracle.
the class NoracleQuestionService method changeQuestionDepth.
@Override
public Question changeQuestionDepth(String questionId, int depth) throws ServiceInvocationException {
if (questionId == null) {
throw new InvocationBadArgumentException("No question id given");
}
try {
Envelope questionEnvelope = Context.get().requestEnvelope(getQuestionEnvelopeIdentifier(questionId));
Question question = (Question) questionEnvelope.getContent();
question.setDepth(depth);
question.setTimestampLastModified(Instant.now().toString());
questionEnvelope.setContent(question);
Context.get().storeEnvelope(questionEnvelope);
return question;
} catch (EnvelopeNotFoundException e) {
throw new ResourceNotFoundException("Question 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.InternalServiceException in project Distributed-Noracle-Backend by Distributed-Noracle.
the class NoracleQuestionService method changeQuestionText.
@Override
public Question changeQuestionText(String questionId, String text) throws ServiceInvocationException {
if (questionId == null) {
throw new InvocationBadArgumentException("No question id given");
}
try {
Envelope questionEnvelope = Context.get().requestEnvelope(getQuestionEnvelopeIdentifier(questionId));
Question question = (Question) questionEnvelope.getContent();
question.setText(text);
question.setTimestampLastModified(Instant.now().toString());
questionEnvelope.setContent(question);
Context.get().storeEnvelope(questionEnvelope);
return question;
} catch (EnvelopeNotFoundException e) {
throw new ResourceNotFoundException("Question 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.InternalServiceException in project Distributed-Noracle-Backend by Distributed-Noracle.
the class NoracleQuestionService method getQuestion.
@Override
public Question getQuestion(String questionId) throws ServiceInvocationException {
if (questionId == null || questionId.isEmpty()) {
throw new InvocationBadArgumentException("No question id given");
}
Envelope env;
try {
env = Context.get().requestEnvelope(getQuestionEnvelopeIdentifier(questionId));
} catch (EnvelopeAccessDeniedException e) {
throw new ServiceAccessDeniedException("Envelope Access Denied");
} catch (EnvelopeOperationFailedException e) {
throw new InternalServiceException("Could not fetch question envelope", e);
} catch (EnvelopeNotFoundException e) {
throw new ResourceNotFoundException("Question Not Found");
}
Question question = (Question) env.getContent();
return question;
}
use of i5.las2peer.api.execution.InternalServiceException 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.InternalServiceException 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;
}
Aggregations