diff --git a/wherehows-frontend/app/controllers/api/v1/Dataset.java b/wherehows-frontend/app/controllers/api/v1/Dataset.java index bbe810e6ec..924b0f3555 100644 --- a/wherehows-frontend/app/controllers/api/v1/Dataset.java +++ b/wherehows-frontend/app/controllers/api/v1/Dataset.java @@ -420,7 +420,6 @@ public class Dataset extends Controller { } public static Result postDatasetComment(int id) { - String body = request().body().asText(); ObjectNode result = Json.newObject(); String username = session("user"); Map params = request().body().asFormUrlEncoded(); @@ -428,6 +427,7 @@ public class Dataset extends Controller { if (StringUtils.isNotBlank(username)) { if (DatasetsDAO.postComment(id, params, username)) { result.put("status", "success"); + return ok(result); } else { result.put("status", "failed"); result.put("error", "true"); @@ -440,22 +440,15 @@ public class Dataset extends Controller { result.put("msg", "Unauthorized User."); return badRequest(result); } - - return ok(result); } public static Result putDatasetComment(int id, int commentId) { - String body = request().body().asText(); ObjectNode result = Json.newObject(); String username = session("user"); Map params = request().body().asFormUrlEncoded(); if (StringUtils.isNotBlank(username)) { - if (!params.containsKey("id")) { - params.put("id", new String[]{String.valueOf(commentId)}); - } - - if (DatasetsDAO.postComment(id, params, username)) { + if (DatasetsDAO.postComment(id, commentId, params, username)) { result.put("status", "success"); return ok(result); } else { diff --git a/wherehows-frontend/app/dao/DatasetsDAO.java b/wherehows-frontend/app/dao/DatasetsDAO.java index 5f47fbb29d..4aaa833afa 100644 --- a/wherehows-frontend/app/dao/DatasetsDAO.java +++ b/wherehows-frontend/app/dao/DatasetsDAO.java @@ -982,77 +982,38 @@ public class DatasetsDAO extends AbstractMySQLOpenSourceDAO return result; } - public static boolean postComment(int datasetId, Map commentMap, String user) - { - boolean result = false; - if ((commentMap == null) || commentMap.size() == 0) - { + public static boolean postComment(int datasetId, Map commentMap, String user) { + return postComment(datasetId, 0, commentMap, user); + } + + public static boolean postComment(int datasetId, int commentId, Map commentMap, String user) { + if (commentMap == null || commentMap.size() == 0) { return false; } - String text = ""; - if (commentMap.containsKey("text")) { - String[] textArray = commentMap.get("text"); - if (textArray != null && textArray.length > 0) - { - text = textArray[0]; - } - } - if (StringUtils.isBlank(text)) - { - return false; + if (!commentMap.containsKey("text") || commentMap.get("text") == null || commentMap.get("text").length == 0) { + return false; } + String text = commentMap.get("text")[0]; String type = "Comment"; if (commentMap.containsKey("type")) { String[] typeArray = commentMap.get("type"); - if (typeArray != null && typeArray.length > 0) - { + if (typeArray != null && typeArray.length > 0) { type = typeArray[0]; } } - Integer commentId = 0; - if (commentMap.containsKey("id")) { - String[] idArray = commentMap.get("id"); - if (idArray != null && idArray.length > 0) - { - String idStr = idArray[0]; - try - { - commentId = Integer.parseInt(idStr); - } - catch(NumberFormatException e) - { - Logger.error("DatasetDAO postComment wrong id parameter. Error message: " + - e.getMessage()); - commentId = 0; - } - } - } - Integer userId = UserDAO.getUserIDByUserName(user); - - if (userId != null && userId !=0) - { - if (commentId != null && commentId != 0) - { - int row = getJdbcTemplate().update(UPDATE_DATASET_COMMENT, text, type, commentId); - if (row > 0) - { - result = true; - } - } - else - { - int row = getJdbcTemplate().update(CREATE_DATASET_COMMENT, text, userId, datasetId, type); - if (row > 0) - { - result = true; - } - } + if (userId == null || userId == 0) { + return false; + } + + if (commentId > 0) { + return getJdbcTemplate().update(UPDATE_DATASET_COMMENT, text, type, commentId) > 0; + } else { + return getJdbcTemplate().update(CREATE_DATASET_COMMENT, text, userId, datasetId, type) > 0; } - return result; } public static boolean deleteComment(int id)