use of i5.las2peer.services.noracleService.model.Question 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.services.noracleService.model.Question in project Distributed-Noracle-Backend by Distributed-Noracle.
the class NoracleQuestionService method retrieveQuestion.
private boolean retrieveQuestion(QuestionList result, String spaceId, int questionNumber) throws EnvelopeNotFoundException {
try {
Envelope spaceQuestionEnv = Context.get().requestEnvelope(buildSpaceQuestionNumberId(spaceId, questionNumber));
String questionId = (String) spaceQuestionEnv.getContent();
Envelope questionEnv = Context.get().requestEnvelope(getQuestionEnvelopeIdentifier(questionId));
Question question = (Question) questionEnv.getContent();
// TODO check if author is a member of this space?
// String authorId = question.getAuthorId();
// if (authorId == null || authorId.isEmpty()) {
// return false;
// }
result.add(question);
return true;
} catch (EnvelopeNotFoundException e) {
throw e;
} catch (Exception e) {
// XXX logging
}
return false;
}
use of i5.las2peer.services.noracleService.model.Question in project Distributed-Noracle-Backend by Distributed-Noracle.
the class QuestionRelationsResource method changeQuestionRelation.
@PUT
@Path("/{relationId}")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@ApiResponses({ @ApiResponse(code = HttpURLConnection.HTTP_OK, message = "Changes a question relation", response = QuestionRelation.class), @ApiResponse(code = HttpURLConnection.HTTP_BAD_REQUEST, message = "No relation id given", response = ExceptionEntity.class), @ApiResponse(code = HttpURLConnection.HTTP_FORBIDDEN, message = "Access Denied", response = ExceptionEntity.class), @ApiResponse(code = HttpURLConnection.HTTP_NOT_FOUND, message = "Relation Not Found", response = ExceptionEntity.class), @ApiResponse(code = HttpURLConnection.HTTP_INTERNAL_ERROR, message = "Internal Server Error", response = ExceptionEntity.class) })
public QuestionRelation changeQuestionRelation(@PathParam("relationId") String relationId, @ApiParam(required = true) ChangeQuestionRelationPojo changeQuestionRelationPojo) throws ServiceInvocationException {
Gson gson = new Gson();
String changeRelationPojoJson = gson.toJson(changeQuestionRelationPojo);
JSONParser p = new JSONParser(JSONParser.MODE_PERMISSIVE);
try {
JSONObject obj = (JSONObject) p.parse(changeRelationPojoJson);
obj.put("relId", relationId);
obj.put("uid", Context.getCurrent().getMainAgent().getIdentifier());
Context.get().monitorEvent(MonitoringEvent.SERVICE_CUSTOM_MESSAGE_4, obj.toJSONString());
} catch (ParseException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// Try to log training data
try {
String from = getQuestionText(changeQuestionRelationPojo.getQuestionId1());
String to = getQuestionText(changeQuestionRelationPojo.getQuestionId2());
JSONObject trainingData = new JSONObject();
trainingData.put("from", from);
trainingData.put("to", to);
if (changeQuestionRelationPojo.getDirected())
Context.get().monitorEvent(MonitoringEvent.SERVICE_CUSTOM_MESSAGE_42, trainingData.toString());
else
Context.get().monitorEvent(MonitoringEvent.SERVICE_CUSTOM_MESSAGE_43, trainingData.toString());
} catch (ServiceInvocationException e) {
/* getDirected will return null at this point */
// Context.get().monitorEvent(MonitoringEvent.SERVICE_CUSTOM_ERROR_42,
// changeQuestionRelationPojo.getDirected().toString());
}
return changeQuestionRelation(relationId, changeQuestionRelationPojo.getName(), changeQuestionRelationPojo.getQuestionId1(), changeQuestionRelationPojo.getQuestionId2(), changeQuestionRelationPojo.getDirected());
}
use of i5.las2peer.services.noracleService.model.Question in project Distributed-Noracle-Backend by Distributed-Noracle.
the class QuestionRelationsResource method getQuestionText.
private String getQuestionText(String questionId) throws ServiceInvocationException {
String qText = "";
Serializable rmiResult = Context.get().invoke(new ServiceNameVersion(NoracleQuestionService.class.getCanonicalName(), NoracleService.API_VERSION), "getQuestion", questionId);
if (rmiResult instanceof Question)
qText = ((Question) rmiResult).getText();
return qText;
}
use of i5.las2peer.services.noracleService.model.Question in project Distributed-Noracle-Backend by Distributed-Noracle.
the class QuestionsResource method getQuestionsWeb.
@GET
@Produces(MediaType.APPLICATION_JSON)
@ApiResponses({ @ApiResponse(code = HttpURLConnection.HTTP_OK, message = "A list of questions from the network", response = QuestionList.class), @ApiResponse(code = HttpURLConnection.HTTP_BAD_REQUEST, message = "No space id given", response = ExceptionEntity.class), @ApiResponse(code = HttpURLConnection.HTTP_FORBIDDEN, message = "Access Denied", response = ExceptionEntity.class), @ApiResponse(code = HttpURLConnection.HTTP_INTERNAL_ERROR, message = "Internal Server Error", response = ExceptionEntity.class) })
public Response getQuestionsWeb(@PathParam("spaceId") String spaceId, @QueryParam("order") String order, @QueryParam("limit") Integer limit, @QueryParam("startat") Integer startAt) throws ServiceInvocationException {
QuestionList questionList = getQuestions(spaceId, order, limit, startAt);
VotedQuestionList votedQuestionList = new VotedQuestionList();
for (Question question : questionList) {
VotedQuestion votedQuestion = new VotedQuestion(question);
String objectId = QuestionVotesResource.buildObjectId(spaceId, question.getQuestionId());
Serializable rmiResult = Context.get().invoke(new ServiceNameVersion(NoracleVoteService.class.getCanonicalName(), NoracleService.API_VERSION), "getAllVotes", objectId);
if (rmiResult instanceof VoteList) {
votedQuestion.setVotes((VoteList) rmiResult);
}
votedQuestionList.add(votedQuestion);
}
ResponseBuilder responseBuilder = Response.ok(votedQuestionList);
String queryOrder = order != null ? "order=" + order : "";
String queryLimit = limit != null ? "limit=" + Integer.toString(limit) : "";
String queryStartAt = "";
if (startAt != null && limit != null) {
if (order.equalsIgnoreCase("desc")) {
queryStartAt = "startat=" + Integer.toString(startAt - limit);
} else {
queryStartAt = "startat=" + Integer.toString(startAt + limit);
}
}
String nextLinkStr = "";
for (String param : new String[] { queryOrder, queryLimit, queryStartAt }) {
if (param != null && !param.isEmpty()) {
if (nextLinkStr.isEmpty()) {
nextLinkStr += "?";
} else {
nextLinkStr += "&";
}
nextLinkStr += param;
}
}
if (!nextLinkStr.isEmpty()) {
responseBuilder.header(HttpHeaders.LINK, "<" + nextLinkStr + ">; rel=\"next\"");
}
return responseBuilder.build();
}
Aggregations