From bd7a13be8e980d15aacb994032f4c4d4384f9a4e Mon Sep 17 00:00:00 2001 From: Piotr Gawron <piotr.gawron@uni.lu> Date: Fri, 26 Jul 2019 14:30:55 +0200 Subject: [PATCH] comment contains only information about submodel (not the top level model) --- .../lcsb/mapviewer/model/map/Comment.java | 42 +------------------ .../lcsb/mapviewer/model/map/CommentTest.java | 4 -- ...25__comment_submodel_column_is_removed.sql | 11 +++++ .../persist/dao/map/CommentDaoTest.java | 6 +++ .../projects/comments/CommentRestImpl.java | 4 +- .../services/impl/CommentService.java | 20 ++------- .../services/interfaces/ICommentService.java | 4 +- .../services/impl/CommentServiceTest.java | 21 ++++------ .../web/CommentControllerIntegrationTest.java | 1 - 9 files changed, 33 insertions(+), 80 deletions(-) create mode 100644 persist/src/main/resources/db/migration/14.0.0~alpha.0/V14.0.0.20190725__comment_submodel_column_is_removed.sql diff --git a/model/src/main/java/lcsb/mapviewer/model/map/Comment.java b/model/src/main/java/lcsb/mapviewer/model/map/Comment.java index 8747682ed2..6bfd0f17db 100644 --- a/model/src/main/java/lcsb/mapviewer/model/map/Comment.java +++ b/model/src/main/java/lcsb/mapviewer/model/map/Comment.java @@ -50,15 +50,9 @@ public class Comment implements Serializable { /** * The model that was commented. */ - @ManyToOne(fetch = FetchType.LAZY) + @ManyToOne(fetch = FetchType.LAZY, optional=false) private ModelData model; - /** - * The model that was commented. - */ - @ManyToOne(fetch = FetchType.LAZY) - private ModelData submodel; - /** * User who gave the feedback (if logged in). */ @@ -314,38 +308,4 @@ public class Comment implements Serializable { this.model = model2; } - /** - * @return the submodel - * @see #submodel - */ - public ModelData getSubmodelData() { - return submodel; - } - - /** - * @param submodel - * the submodel to set - * @see #submodel - */ - public void setSubmodelData(ModelData submodel) { - this.submodel = submodel; - } - - /** - * @return the submodel - * @see #submodel - */ - public Model getSubmodel() { - return this.submodel.getModel(); - } - - /** - * @param submodel - * the submodel to set - * @see #submodel - */ - public void setSubmodel(Model submodel) { - this.submodel = submodel.getModelData(); - } - } diff --git a/model/src/test/java/lcsb/mapviewer/model/map/CommentTest.java b/model/src/test/java/lcsb/mapviewer/model/map/CommentTest.java index b21e990c59..5e4044e69d 100644 --- a/model/src/test/java/lcsb/mapviewer/model/map/CommentTest.java +++ b/model/src/test/java/lcsb/mapviewer/model/map/CommentTest.java @@ -64,10 +64,6 @@ public class CommentTest extends ModelTestFunctions { assertEquals(pinned, comment.isPinned()); comment.setRemoveReason(removeReason); assertEquals(removeReason, comment.getRemoveReason()); - comment.setSubmodel(submodel); - assertEquals(submodel, comment.getSubmodel()); - comment.setSubmodelData(submodel.getModelData()); - assertEquals(submodel.getModelData(), comment.getSubmodelData()); comment.setTableId(tableId); assertEquals(tableId, comment.getTableId()); comment.setTableName(tableName); diff --git a/persist/src/main/resources/db/migration/14.0.0~alpha.0/V14.0.0.20190725__comment_submodel_column_is_removed.sql b/persist/src/main/resources/db/migration/14.0.0~alpha.0/V14.0.0.20190725__comment_submodel_column_is_removed.sql new file mode 100644 index 0000000000..25aa099906 --- /dev/null +++ b/persist/src/main/resources/db/migration/14.0.0~alpha.0/V14.0.0.20190725__comment_submodel_column_is_removed.sql @@ -0,0 +1,11 @@ +--remove submodel_id column +update comment_table set model_id = submodel_id where submodel_id is not null; +ALTER TABLE comment_table drop COLUMN submodel_id; + +--comment must be attached to a model +delete from comment_table where model_id is null; +ALTER TABLE comment_table ALTER COLUMN model_id SET NOT NULL; + +-- pinned should be not null +update comment_table set pinned = false where pinned is null; +ALTER TABLE comment_table ALTER COLUMN pinned SET NOT NULL; diff --git a/persist/src/test/java/lcsb/mapviewer/persist/dao/map/CommentDaoTest.java b/persist/src/test/java/lcsb/mapviewer/persist/dao/map/CommentDaoTest.java index 4a0e19615c..1b4f4edd7e 100644 --- a/persist/src/test/java/lcsb/mapviewer/persist/dao/map/CommentDaoTest.java +++ b/persist/src/test/java/lcsb/mapviewer/persist/dao/map/CommentDaoTest.java @@ -48,9 +48,15 @@ public class CommentDaoTest extends PersistTestFunctions { @Test public void testGetById() throws Exception { + Model model = createModel(); + project.addModel(model); + modelDao.add(model); + projectDao.update(project); + int counter = (int) commentDao.getCount(); Comment comment = new Comment(); comment.setUser(user); + comment.setModel(model); commentDao.add(comment); int counter2 = (int) commentDao.getCount(); assertEquals(counter + 1, counter2); diff --git a/rest-api/src/main/java/lcsb/mapviewer/api/projects/comments/CommentRestImpl.java b/rest-api/src/main/java/lcsb/mapviewer/api/projects/comments/CommentRestImpl.java index 5527253a14..24dad6f434 100644 --- a/rest-api/src/main/java/lcsb/mapviewer/api/projects/comments/CommentRestImpl.java +++ b/rest-api/src/main/java/lcsb/mapviewer/api/projects/comments/CommentRestImpl.java @@ -107,7 +107,7 @@ public class CommentRestImpl extends BaseRestImpl { value = getId(comment); break; case "modelid": - value = comment.getSubmodelData().getId(); + value = comment.getModelData().getId(); break; case "title": value = getTitle(comment); @@ -279,7 +279,7 @@ public class CommentRestImpl extends BaseRestImpl { throw new QueryException("Unknown type of commented object: " + elementType); } - Comment comment = commentService.addComment(name, email, content, model, pointCoordinates, commentedObject, pinned, + Comment comment = commentService.addComment(name, email, content, pointCoordinates, commentedObject, pinned, submodel); return preparedComment(comment, createCommentColumnSet("")); diff --git a/service/src/main/java/lcsb/mapviewer/services/impl/CommentService.java b/service/src/main/java/lcsb/mapviewer/services/impl/CommentService.java index 3e4268803e..709b3560a5 100644 --- a/service/src/main/java/lcsb/mapviewer/services/impl/CommentService.java +++ b/service/src/main/java/lcsb/mapviewer/services/impl/CommentService.java @@ -12,13 +12,10 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import lcsb.mapviewer.common.ObjectUtils; -import lcsb.mapviewer.common.exception.InvalidClassException; import lcsb.mapviewer.model.Project; import lcsb.mapviewer.model.map.Comment; import lcsb.mapviewer.model.map.model.Model; import lcsb.mapviewer.model.map.model.ModelData; -import lcsb.mapviewer.model.map.reaction.Reaction; -import lcsb.mapviewer.model.map.species.Element; import lcsb.mapviewer.model.user.User; import lcsb.mapviewer.persist.dao.map.CommentDao; import lcsb.mapviewer.services.interfaces.*; @@ -79,26 +76,17 @@ public class CommentService implements ICommentService { } @Override - public Comment addComment(String name, String email, String content, Model model, Point2D coordinates, Object object, + public Comment addComment(String name, String email, String content, Point2D coordinates, Object object, boolean pinned, Model submodel) { Comment comment = new Comment(); comment.setName(name); comment.setEmail(email); comment.setContent(content); - comment.setModel(model); + comment.setModel(submodel); comment.setCoordinates(coordinates); if (object != null) { comment.setTableName(object.getClass()); comment.setTableId(ObjectUtils.getIdOfObject(object)); - if (object instanceof Element) { - comment.setSubmodel(((Element) object).getModel()); - } else if (object instanceof Reaction) { - comment.setSubmodel(((Reaction) object).getModel()); - } else { - throw new InvalidClassException("Cannot comment on object of class: " + object.getClass()); - } - } else { - comment.setSubmodel(submodel); } comment.setUser(null); comment.setPinned(pinned); @@ -106,8 +94,8 @@ public class CommentService implements ICommentService { try { EmailSender emailSender = new EmailSender(configurationService); - emailSender.sendEmail("New comment appeard in the " + model.getProject().getProjectId(), content, - model.getProject().getNotifyEmail()); + emailSender.sendEmail("New comment appeard in the " + submodel.getProject().getProjectId(), content, + submodel.getProject().getNotifyEmail()); } catch (MessagingException e) { logger.error("Problem with sending email.", e); } diff --git a/service/src/main/java/lcsb/mapviewer/services/interfaces/ICommentService.java b/service/src/main/java/lcsb/mapviewer/services/interfaces/ICommentService.java index 913e4340da..1f0b0ee31f 100644 --- a/service/src/main/java/lcsb/mapviewer/services/interfaces/ICommentService.java +++ b/service/src/main/java/lcsb/mapviewer/services/interfaces/ICommentService.java @@ -30,8 +30,6 @@ public interface ICommentService { * email of the person that comments * @param content * content of the comment - * @param model - * which map we comment on * @param submodel * on which submodel we comment on * @param coordinates @@ -39,7 +37,7 @@ public interface ICommentService { * * @return comment object created based on the data provided as parameters */ - Comment addComment(String name, String email, String content, Model model, Point2D coordinates, Object object, + Comment addComment(String name, String email, String content, Point2D coordinates, Object object, boolean pinned, Model submodel); /** diff --git a/service/src/test/java/lcsb/mapviewer/services/impl/CommentServiceTest.java b/service/src/test/java/lcsb/mapviewer/services/impl/CommentServiceTest.java index 83363c520e..0e3c1dfd79 100644 --- a/service/src/test/java/lcsb/mapviewer/services/impl/CommentServiceTest.java +++ b/service/src/test/java/lcsb/mapviewer/services/impl/CommentServiceTest.java @@ -62,16 +62,11 @@ public class CommentServiceTest extends ServiceTestFunctions { @Test public void testGetAgregatedComments() throws Exception { - commentService.addComment("John Doe", "a@a.pl", "Conteneta 1", model, new Point2D.Double(0, 1), alias, false, - model); - commentService.addComment("John Doe", "a@a.pl", "Contenetb 2", model, new Point2D.Double(0, 2), alias, false, - model); - commentService.addComment("John Doe", "a@a.pl", "Contenetc 3", model, new Point2D.Double(0, 3), alias2, false, - model); - commentService.addComment("John Doe", "a@a.pl", "Contenetc 4", model, new Point2D.Double(0, 4), null, false, - model); - commentService.addComment("John Doe", "a@a.pl", "Contenetc 5", model, new Point2D.Double(0, 5), null, false, - model); + commentService.addComment("John Doe", "a@a.pl", "Conteneta 1", new Point2D.Double(0, 1), alias, false, model); + commentService.addComment("John Doe", "a@a.pl", "Contenetb 2", new Point2D.Double(0, 2), alias, false, model); + commentService.addComment("John Doe", "a@a.pl", "Contenetc 3", new Point2D.Double(0, 3), alias2, false, model); + commentService.addComment("John Doe", "a@a.pl", "Contenetc 4", new Point2D.Double(0, 4), null, false, model); + commentService.addComment("John Doe", "a@a.pl", "Contenetc 5", new Point2D.Double(0, 5), null, false, model); CommentService service = new CommentService(null, null, null, null); service.setCommentDao(commentDao); List<List<Comment>> comments = service.getAgregatedComments(model, null); @@ -92,7 +87,7 @@ public class CommentServiceTest extends ServiceTestFunctions { @Test public void testAddComment() throws Exception { long counter = commentService.getCommentCount(); - Comment feedback = commentService.addComment("a", "b", "c", model, new Point2D.Double(0, 0), null, false, model); + Comment feedback = commentService.addComment("a", "b", "c", new Point2D.Double(0, 0), null, false, model); long counter2 = commentService.getCommentCount(); assertEquals(counter + 1, counter2); commentDao.delete(feedback); @@ -101,7 +96,7 @@ public class CommentServiceTest extends ServiceTestFunctions { @Test public void testAddCommentForReaction() throws Exception { long counter = commentService.getCommentCount(); - Comment feedback = commentService.addComment("a", "b", "c", model, new Point2D.Double(0, 0), + Comment feedback = commentService.addComment("a", "b", "c", new Point2D.Double(0, 0), model.getReactions().iterator().next(), false, model); long counter2 = commentService.getCommentCount(); assertEquals(counter + 1, counter2); @@ -112,7 +107,7 @@ public class CommentServiceTest extends ServiceTestFunctions { public void testAddCommentForAlias() throws Exception { Element alias = model.getElementByElementId("sa1"); long counter = commentService.getCommentCount(); - Comment feedback = commentService.addComment("a", "b", "c", model, new Point2D.Double(0, 0), alias, false, model); + Comment feedback = commentService.addComment("a", "b", "c", new Point2D.Double(0, 0), alias, false, model); long counter2 = commentService.getCommentCount(); assertEquals(counter + 1, counter2); commentDao.delete(feedback); diff --git a/web/src/test/java/lcsb/mapviewer/web/CommentControllerIntegrationTest.java b/web/src/test/java/lcsb/mapviewer/web/CommentControllerIntegrationTest.java index 1fb757ca94..50e40f3869 100644 --- a/web/src/test/java/lcsb/mapviewer/web/CommentControllerIntegrationTest.java +++ b/web/src/test/java/lcsb/mapviewer/web/CommentControllerIntegrationTest.java @@ -702,7 +702,6 @@ public class CommentControllerIntegrationTest extends ControllerIntegrationTest private Comment createComment() { Comment comment = new Comment(); - comment.setSubmodelData(map); comment.setModel(map); comment.setCoordinates(new Point2D.Double(10, 20)); return comment; -- GitLab