From f67c38ef2d2f25d20fe349ca1a1911343c627f24 Mon Sep 17 00:00:00 2001 From: Massimiliano Assante Date: Wed, 8 Feb 2023 15:37:24 +0100 Subject: [PATCH] revised writeComment method --- .../networking/ws/methods/v2/Comments.java | 60 ++++++++++--------- 1 file changed, 31 insertions(+), 29 deletions(-) diff --git a/src/main/java/org/gcube/portal/social/networking/ws/methods/v2/Comments.java b/src/main/java/org/gcube/portal/social/networking/ws/methods/v2/Comments.java index 1357766..2a27812 100644 --- a/src/main/java/org/gcube/portal/social/networking/ws/methods/v2/Comments.java +++ b/src/main/java/org/gcube/portal/social/networking/ws/methods/v2/Comments.java @@ -38,6 +38,7 @@ import org.gcube.vomanagement.usermanagement.UserManager; import org.gcube.vomanagement.usermanagement.model.GCubeUser; import org.slf4j.LoggerFactory; +import com.liferay.portlet.journal.FeedIdException; import com.webcohesion.enunciate.metadata.rs.RequestHeader; import com.webcohesion.enunciate.metadata.rs.RequestHeaders; import com.webcohesion.enunciate.metadata.rs.ResponseCode; @@ -185,48 +186,49 @@ public class Comments { public Response writeComment( @NotNull(message="Comment to write is missing") @Valid - CommentInputBean comment) throws ValidationException{ - + CommentInputBean comment) throws ValidationException { Caller caller = AuthorizationProvider.instance.get(); String username = caller.getClient().getId(); logger.debug("Request of writing a comment coming from user " + username); String context = ScopeProvider.instance.get(); ResponseBean responseBean = new ResponseBean(); 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 { + String postId = comment.getPostid(); + String commentText = comment.getText(); + String userid = username; + Date time = new Date(); + String postOwnerId = CassandraConnection.getInstance().getDatabookStore().readPost(postId).getKey(); - theComment = SocialUtils.commentPost(userid, time, postId, commentText, postOwnerId, context); - logger.info("Added comment " + theComment.toString()); - } catch(Exception e){ - e.printStackTrace(); + Comment theComment = SocialUtils.commentPost(userid, time, postId, commentText, postOwnerId, context); + if (theComment != null) + logger.info("Added comment " + theComment.toString()); + 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.setSuccess(false); status = Status.INTERNAL_SERVER_ERROR; 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(); - } - } }