use of i5.las2peer.services.noracleService.model.QuestionRelation 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.QuestionRelation in project Distributed-Noracle-Backend by Distributed-Noracle.
the class QuestionRelationsResource method getQuestions.
@GET
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@ApiResponses({ @ApiResponse(code = HttpURLConnection.HTTP_OK, message = "A list of relations from the network", response = QuestionRelationList.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 getQuestions(@PathParam("spaceId") String spaceId, @QueryParam("order") String order, @QueryParam("limit") Integer limit, @QueryParam("startAt") Integer startAt) throws ServiceInvocationException {
QuestionRelationList questionRelationList = getQuestionRelations(spaceId, order, limit, startAt);
VotedQuestionRelationList votedQuestionRelationList = new VotedQuestionRelationList();
for (QuestionRelation questionRelation : questionRelationList) {
VotedQuestionRelation votedQuestionRelation = new VotedQuestionRelation(questionRelation);
String objectId = RelationVotesResource.buildObjectId(spaceId, questionRelation.getRelationId());
Serializable rmiResult = Context.get().invoke(new ServiceNameVersion(NoracleVoteService.class.getCanonicalName(), NoracleService.API_VERSION), "getAllVotes", objectId);
if (rmiResult instanceof VoteList) {
votedQuestionRelation.setVotes((VoteList) rmiResult);
}
votedQuestionRelationList.add(votedQuestionRelation);
}
ResponseBuilder responseBuilder = Response.ok(votedQuestionRelationList);
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();
}
use of i5.las2peer.services.noracleService.model.QuestionRelation in project Distributed-Noracle-Backend by Distributed-Noracle.
the class NoracleQuestionRelationService method getQuestionRelation.
@Override
public QuestionRelation getQuestionRelation(String relationId) throws ServiceInvocationException {
if (relationId == null || relationId.isEmpty()) {
throw new InvocationBadArgumentException("No relation id given");
}
Envelope env;
try {
env = Context.get().requestEnvelope(getQuestionRelationEnvelopeIdentifier(relationId));
} catch (EnvelopeAccessDeniedException e) {
throw new ServiceAccessDeniedException("Envelope Access Denied");
} catch (EnvelopeOperationFailedException e) {
throw new InternalServiceException("Could not fetch relation envelope", e);
} catch (EnvelopeNotFoundException e) {
throw new ResourceNotFoundException("Relation Not Found");
}
QuestionRelation relation = (QuestionRelation) env.getContent();
return relation;
}
Aggregations