revised writeComment method

This commit is contained in:
Massimiliano Assante 2023-02-08 15:37:24 +01:00
parent cb583b0a50
commit f67c38ef2d
1 changed files with 31 additions and 29 deletions

View File

@ -38,6 +38,7 @@ import org.gcube.vomanagement.usermanagement.UserManager;
import org.gcube.vomanagement.usermanagement.model.GCubeUser; import org.gcube.vomanagement.usermanagement.model.GCubeUser;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import com.liferay.portlet.journal.FeedIdException;
import com.webcohesion.enunciate.metadata.rs.RequestHeader; import com.webcohesion.enunciate.metadata.rs.RequestHeader;
import com.webcohesion.enunciate.metadata.rs.RequestHeaders; import com.webcohesion.enunciate.metadata.rs.RequestHeaders;
import com.webcohesion.enunciate.metadata.rs.ResponseCode; import com.webcohesion.enunciate.metadata.rs.ResponseCode;
@ -185,48 +186,49 @@ public class Comments {
public Response writeComment( public Response writeComment(
@NotNull(message="Comment to write is missing") @NotNull(message="Comment to write is missing")
@Valid @Valid
CommentInputBean comment) throws ValidationException{ CommentInputBean comment) throws ValidationException {
Caller caller = AuthorizationProvider.instance.get(); Caller caller = AuthorizationProvider.instance.get();
String username = caller.getClient().getId(); String username = caller.getClient().getId();
logger.debug("Request of writing a comment coming from user " + username); logger.debug("Request of writing a comment coming from user " + username);
String context = ScopeProvider.instance.get(); String context = ScopeProvider.instance.get();
ResponseBean responseBean = new ResponseBean(); ResponseBean responseBean = new ResponseBean();
Status status = Status.OK; Status status = Status.OK;
// parse
String postId = comment.getPostid();
String commentText = comment.getText();
String userid = username;
Date time = new Date();
boolean result = false;
Comment theComment = null;
try { try {
String postId = comment.getPostid();
String commentText = comment.getText();
String userid = username;
Date time = new Date();
String postOwnerId = CassandraConnection.getInstance().getDatabookStore().readPost(postId).getKey(); String postOwnerId = CassandraConnection.getInstance().getDatabookStore().readPost(postId).getKey();
theComment = SocialUtils.commentPost(userid, time, postId, commentText, postOwnerId, context); Comment theComment = SocialUtils.commentPost(userid, time, postId, commentText, postOwnerId, context);
logger.info("Added comment " + theComment.toString()); if (theComment != null)
} catch(Exception e){ logger.info("Added comment " + theComment.toString());
e.printStackTrace(); else {
logger.error("Unable to write comment");
responseBean.setMessage("Unable to write comment, something went wrong please see server log");
responseBean.setSuccess(false);
status = Status.INTERNAL_SERVER_ERROR;
return Response.status(status).entity(responseBean).build();
}
responseBean.setResult(true);
responseBean.setSuccess(true);
return Response.status(status).entity(responseBean).build();
} catch(FeedIDNotFoundException ex) {
logger.error("Unable to find a post comment", ex);
responseBean.setMessage("Could not reach the DB to write the comment, something went wrong");
responseBean.setSuccess(false);
status = Status.INTERNAL_SERVER_ERROR;
return Response.status(status).entity(responseBean).build();
}
catch(Exception e) {
logger.error("Unable to write comment", e);
responseBean.setMessage("Could not reach the DB to write the comment, something went wrong"); responseBean.setMessage("Could not reach the DB to write the comment, something went wrong");
responseBean.setSuccess(false); responseBean.setSuccess(false);
status = Status.INTERNAL_SERVER_ERROR; status = Status.INTERNAL_SERVER_ERROR;
return Response.status(status).entity(responseBean).build(); return Response.status(status).entity(responseBean).build();
} }
if(theComment != null){
logger.info("Comment correctly written by user " + username);
responseBean.setResult(result);
responseBean.setSuccess(true);
return Response.status(status).entity(responseBean).build();
} else {
logger.error("Unable to write comment.");
responseBean.setMessage("Unable to write comment");
responseBean.setSuccess(false);
status = Status.INTERNAL_SERVER_ERROR;
return Response.status(status).entity(responseBean).build();
}
} }
} }