From 8889ef1a9e4e30e2b270f2e33b6a7806d2655090 Mon Sep 17 00:00:00 2001 From: Piotr Gawron <piotr.gawron@uni.lu> Date: Fri, 30 Dec 2016 15:06:24 +0100 Subject: [PATCH] listComments method added to API --- .../api/comment/CommentController.java | 47 ++++++++++ .../api/comment/CommentRestImpl.java | 89 +++++++++++++++++++ .../resources/applicationContext-rest.xml | 1 + .../java/lcsb/mapviewer/api/AllRestTests.java | 16 ++++ .../api/{project => }/RestTestFunctions.java | 19 ++-- .../api/comment/AllCommentTests.java | 11 +++ .../api/comment/CommentRestImplTest.java | 45 ++++++++++ .../api/project/AllProjectTests.java | 13 +++ .../api/project/ProjectRestImplTest.java | 1 + .../services/impl/CommentService.java | 13 ++- .../services/interfaces/ICommentService.java | 5 +- .../services/impl/CommentServiceTest.java | 11 ++- .../lcsb/mapviewer/bean/FeedbackBean.java | 74 ++------------- 13 files changed, 263 insertions(+), 82 deletions(-) create mode 100644 rest-api/src/main/java/lcsb/mapviewer/api/comment/CommentController.java create mode 100644 rest-api/src/main/java/lcsb/mapviewer/api/comment/CommentRestImpl.java create mode 100644 rest-api/src/test/java/lcsb/mapviewer/api/AllRestTests.java rename rest-api/src/test/java/lcsb/mapviewer/api/{project => }/RestTestFunctions.java (91%) create mode 100644 rest-api/src/test/java/lcsb/mapviewer/api/comment/AllCommentTests.java create mode 100644 rest-api/src/test/java/lcsb/mapviewer/api/comment/CommentRestImplTest.java create mode 100644 rest-api/src/test/java/lcsb/mapviewer/api/project/AllProjectTests.java diff --git a/rest-api/src/main/java/lcsb/mapviewer/api/comment/CommentController.java b/rest-api/src/main/java/lcsb/mapviewer/api/comment/CommentController.java new file mode 100644 index 0000000000..36a9a58d00 --- /dev/null +++ b/rest-api/src/main/java/lcsb/mapviewer/api/comment/CommentController.java @@ -0,0 +1,47 @@ +package lcsb.mapviewer.api.comment; + +import java.util.List; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.MediaType; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +import lcsb.mapviewer.api.BaseController; +import lcsb.mapviewer.api.QueryException; +import lcsb.mapviewer.services.SecurityException; +import lcsb.mapviewer.services.view.CommentView; + +@RestController +@RequestMapping("/comment") +public class CommentController extends BaseController { + + @Autowired + private CommentRestImpl commentController; + + @RequestMapping(value = "/getCommentList", method = { RequestMethod.GET, RequestMethod.POST }, produces = { MediaType.APPLICATION_JSON_VALUE }) + public List<CommentView> getOverlayList(@RequestParam(value = "token") String token, @RequestParam(value = "projectId") String projectId) + throws SecurityException, QueryException { + return commentController.getCommentList(token, projectId); + } + + /** + * @return the overlayController + * @see #commentController + */ + public CommentRestImpl getOverlayController() { + return commentController; + } + + /** + * @param overlayController + * the overlayController to set + * @see #commentController + */ + public void setOverlayController(CommentRestImpl overlayController) { + this.commentController = overlayController; + } + +} \ No newline at end of file diff --git a/rest-api/src/main/java/lcsb/mapviewer/api/comment/CommentRestImpl.java b/rest-api/src/main/java/lcsb/mapviewer/api/comment/CommentRestImpl.java new file mode 100644 index 0000000000..01e69a2349 --- /dev/null +++ b/rest-api/src/main/java/lcsb/mapviewer/api/comment/CommentRestImpl.java @@ -0,0 +1,89 @@ +package lcsb.mapviewer.api.comment; + +import java.util.List; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.transaction.annotation.Transactional; + +import lcsb.mapviewer.api.QueryException; +import lcsb.mapviewer.model.map.model.Model; +import lcsb.mapviewer.services.SecurityException; +import lcsb.mapviewer.services.interfaces.ICommentService; +import lcsb.mapviewer.services.interfaces.IModelService; +import lcsb.mapviewer.services.interfaces.IUserService; +import lcsb.mapviewer.services.view.AuthenticationToken; +import lcsb.mapviewer.services.view.CommentView; + +@Transactional(value = "txManager") +public class CommentRestImpl { + + @Autowired + private IUserService userService; + + @Autowired + private IModelService modelService; + + @Autowired + private ICommentService commentService; + + public List<CommentView> getCommentList(String token, String projectId) throws SecurityException, QueryException { + AuthenticationToken authenticationToken = userService.getToken(token); + Model model = modelService.getLastModelByProjectId(projectId, authenticationToken); + if (model == null) { + throw new QueryException("Project with given id doesn't exist"); + } + return commentService.getCommentsByMap(model, authenticationToken); + } + + /** + * @return the userService + * @see #userService + */ + public IUserService getUserService() { + return userService; + } + + /** + * @param userService + * the userService to set + * @see #userService + */ + public void setUserService(IUserService userService) { + this.userService = userService; + } + + /** + * @return the modelService + * @see #modelService + */ + public IModelService getModelService() { + return modelService; + } + + /** + * @param modelService + * the modelService to set + * @see #modelService + */ + public void setModelService(IModelService modelService) { + this.modelService = modelService; + } + + /** + * @return the commentService + * @see #commentService + */ + public ICommentService getCommentService() { + return commentService; + } + + /** + * @param commentService + * the commentService to set + * @see #commentService + */ + public void setCommentService(ICommentService commentService) { + this.commentService = commentService; + } + +} diff --git a/rest-api/src/main/resources/applicationContext-rest.xml b/rest-api/src/main/resources/applicationContext-rest.xml index ed15a01029..5619ac7445 100644 --- a/rest-api/src/main/resources/applicationContext-rest.xml +++ b/rest-api/src/main/resources/applicationContext-rest.xml @@ -12,6 +12,7 @@ <bean id="ConfigurationRestImpl" class="lcsb.mapviewer.api.configuration.ConfigurationRestImpl"/> + <bean id="CommentRestImpl" class="lcsb.mapviewer.api.comment.CommentRestImpl"/> <bean id="OverlayRestImpl" class="lcsb.mapviewer.api.overlay.OverlayRestImpl"/> <bean id="ProjectRestImpl" class="lcsb.mapviewer.api.project.ProjectRestImpl"/> diff --git a/rest-api/src/test/java/lcsb/mapviewer/api/AllRestTests.java b/rest-api/src/test/java/lcsb/mapviewer/api/AllRestTests.java new file mode 100644 index 0000000000..92d5536180 --- /dev/null +++ b/rest-api/src/test/java/lcsb/mapviewer/api/AllRestTests.java @@ -0,0 +1,16 @@ +package lcsb.mapviewer.api; + +import org.junit.runner.RunWith; +import org.junit.runners.Suite; +import org.junit.runners.Suite.SuiteClasses; + +import lcsb.mapviewer.api.comment.AllCommentTests; +import lcsb.mapviewer.api.project.AllProjectTests; + +@RunWith(Suite.class) +@SuiteClasses({ AllCommentTests.class, // + AllProjectTests.class,// +}) +public class AllRestTests { + +} diff --git a/rest-api/src/test/java/lcsb/mapviewer/api/project/RestTestFunctions.java b/rest-api/src/test/java/lcsb/mapviewer/api/RestTestFunctions.java similarity index 91% rename from rest-api/src/test/java/lcsb/mapviewer/api/project/RestTestFunctions.java rename to rest-api/src/test/java/lcsb/mapviewer/api/RestTestFunctions.java index 5566f83688..336cd7a914 100644 --- a/rest-api/src/test/java/lcsb/mapviewer/api/project/RestTestFunctions.java +++ b/rest-api/src/test/java/lcsb/mapviewer/api/RestTestFunctions.java @@ -1,4 +1,4 @@ -package lcsb.mapviewer.api.project; +package lcsb.mapviewer.api; import java.io.BufferedReader; import java.io.File; @@ -55,25 +55,26 @@ import lcsb.mapviewer.services.view.AuthenticationToken; @Transactional(value = "txManager") @Rollback(true) -@ContextConfiguration(locations = { "/applicationContext-persist.xml", "/applicationContext-annotation.xml", "/applicationContext-service.xml","/applicationContext-rest.xml" }) +@ContextConfiguration( + locations = { "/applicationContext-persist.xml", "/applicationContext-annotation.xml", "/applicationContext-service.xml", "/applicationContext-rest.xml" }) @RunWith(SpringJUnit4ClassRunner.class) public abstract class RestTestFunctions { - private Logger logger = Logger.getLogger(RestTestFunctions.class); + private Logger logger = Logger.getLogger(RestTestFunctions.class); - public double EPSILON = 1e-6; + public double EPSILON = 1e-6; @Autowired - protected PasswordEncoder passwordEncoder; + protected PasswordEncoder passwordEncoder; @Autowired - protected IUserService userService; + protected IUserService userService; @Autowired - protected DbUtils dbUtils; + protected DbUtils dbUtils; - AuthenticationToken token; + protected AuthenticationToken token; - AuthenticationToken adminToken; + protected AuthenticationToken adminToken; @Before public void generalSetUp() { diff --git a/rest-api/src/test/java/lcsb/mapviewer/api/comment/AllCommentTests.java b/rest-api/src/test/java/lcsb/mapviewer/api/comment/AllCommentTests.java new file mode 100644 index 0000000000..7fc7f124fd --- /dev/null +++ b/rest-api/src/test/java/lcsb/mapviewer/api/comment/AllCommentTests.java @@ -0,0 +1,11 @@ +package lcsb.mapviewer.api.comment; + +import org.junit.runner.RunWith; +import org.junit.runners.Suite; +import org.junit.runners.Suite.SuiteClasses; + +@RunWith(Suite.class) +@SuiteClasses({ CommentRestImplTest.class }) +public class AllCommentTests { + +} diff --git a/rest-api/src/test/java/lcsb/mapviewer/api/comment/CommentRestImplTest.java b/rest-api/src/test/java/lcsb/mapviewer/api/comment/CommentRestImplTest.java new file mode 100644 index 0000000000..162d386a17 --- /dev/null +++ b/rest-api/src/test/java/lcsb/mapviewer/api/comment/CommentRestImplTest.java @@ -0,0 +1,45 @@ +package lcsb.mapviewer.api.comment; + +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.Test; +import org.springframework.beans.factory.annotation.Autowired; + +import lcsb.mapviewer.api.QueryException; +import lcsb.mapviewer.api.RestTestFunctions; + +public class CommentRestImplTest extends RestTestFunctions { + + @Autowired + public CommentRestImpl commentRestImpl; + + @AfterClass + public static void tearDownAfterClass() throws Exception { + } + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testGetForNonExisting() throws Exception { + try { + commentRestImpl.getCommentList(token.getId(), "unk"); + fail("Exception expected"); + } catch (QueryException e2) { + assertTrue(e2.getMessage().contains("Project with given id doesn't exist")); + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + +} diff --git a/rest-api/src/test/java/lcsb/mapviewer/api/project/AllProjectTests.java b/rest-api/src/test/java/lcsb/mapviewer/api/project/AllProjectTests.java new file mode 100644 index 0000000000..befbc95854 --- /dev/null +++ b/rest-api/src/test/java/lcsb/mapviewer/api/project/AllProjectTests.java @@ -0,0 +1,13 @@ +package lcsb.mapviewer.api.project; + +import org.junit.runner.RunWith; +import org.junit.runners.Suite; +import org.junit.runners.Suite.SuiteClasses; + +@RunWith(Suite.class) +@SuiteClasses({ ModelMetaDataTest.class,// + ProjectMetaDataTest.class,// + ProjectRestImplTest.class }) +public class AllProjectTests { + +} diff --git a/rest-api/src/test/java/lcsb/mapviewer/api/project/ProjectRestImplTest.java b/rest-api/src/test/java/lcsb/mapviewer/api/project/ProjectRestImplTest.java index d571e4eaee..9911c0d844 100644 --- a/rest-api/src/test/java/lcsb/mapviewer/api/project/ProjectRestImplTest.java +++ b/rest-api/src/test/java/lcsb/mapviewer/api/project/ProjectRestImplTest.java @@ -14,6 +14,7 @@ import org.junit.Test; import org.mockito.Mockito; import org.springframework.beans.factory.annotation.Autowired; +import lcsb.mapviewer.api.RestTestFunctions; import lcsb.mapviewer.model.map.model.Model; import lcsb.mapviewer.services.interfaces.IModelService; 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 4e26e586b3..5e715ae222 100644 --- a/service/src/main/java/lcsb/mapviewer/services/impl/CommentService.java +++ b/service/src/main/java/lcsb/mapviewer/services/impl/CommentService.java @@ -21,8 +21,10 @@ 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.PrivilegeType; import lcsb.mapviewer.model.user.User; import lcsb.mapviewer.persist.dao.map.CommentDao; +import lcsb.mapviewer.services.UserAccessException; import lcsb.mapviewer.services.interfaces.ICommentService; import lcsb.mapviewer.services.interfaces.IConfigurationService; import lcsb.mapviewer.services.interfaces.IModelService; @@ -34,6 +36,7 @@ import lcsb.mapviewer.services.search.comment.FullCommentViewFactory; import lcsb.mapviewer.services.search.data.ElementIdentifier; import lcsb.mapviewer.services.search.data.ElementIdentifier.ElementIdentifierType; import lcsb.mapviewer.services.utils.EmailSender; +import lcsb.mapviewer.services.view.AuthenticationToken; import lcsb.mapviewer.services.view.CommentView; import lcsb.mapviewer.services.view.CommentViewFactory; @@ -183,14 +186,20 @@ public class CommentService implements ICommentService { } @Override - public List<CommentView> getCommentsByMap(Model model) { + public List<CommentView> getCommentsByMap(Model model, AuthenticationToken token) throws UserAccessException { + boolean editComments = userService.userHasPrivilege(token, PrivilegeType.EDIT_COMMENTS_PROJECT, model.getProject()); + boolean viewProject = userService.userHasPrivilege(token, PrivilegeType.VIEW_PROJECT, model.getProject()); + if (!editComments && !viewProject) { + throw new UserAccessException("You have no privileges to see comments for given project"); + } List<Comment> comments = commentDao.getCommentByModel(model, null, null); - List<CommentView> commentList = new ArrayList<CommentView>(); + List<CommentView> commentList = new ArrayList<>(); for (Comment comment : comments) { CommentView row = commentViewFactory.create(comment, model); commentList.add(row); } return commentList; + } @Override 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 5a693b923e..50073df439 100644 --- a/service/src/main/java/lcsb/mapviewer/services/interfaces/ICommentService.java +++ b/service/src/main/java/lcsb/mapviewer/services/interfaces/ICommentService.java @@ -7,9 +7,11 @@ import lcsb.mapviewer.model.map.Comment; import lcsb.mapviewer.model.map.model.Model; import lcsb.mapviewer.model.map.model.ModelData; import lcsb.mapviewer.model.user.User; +import lcsb.mapviewer.services.UserAccessException; import lcsb.mapviewer.services.search.ElementIdentifierDetails; import lcsb.mapviewer.services.search.comment.FullCommentView; import lcsb.mapviewer.services.search.data.ElementIdentifier; +import lcsb.mapviewer.services.view.AuthenticationToken; import lcsb.mapviewer.services.view.CommentView; /** @@ -63,8 +65,9 @@ public interface ICommentService { * @param model * given map * @return list of the comments to the map + * @throws UserAccessException */ - List<CommentView> getCommentsByMap(Model model); + List<CommentView> getCommentsByMap(Model model, AuthenticationToken token) throws UserAccessException; /** * Returns number of comments in the system. 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 f8dbf01337..e42d780989 100644 --- a/service/src/test/java/lcsb/mapviewer/services/impl/CommentServiceTest.java +++ b/service/src/test/java/lcsb/mapviewer/services/impl/CommentServiceTest.java @@ -23,6 +23,7 @@ import lcsb.mapviewer.model.map.reaction.Reaction; import lcsb.mapviewer.model.map.species.Element; import lcsb.mapviewer.services.ServiceTestFunctions; import lcsb.mapviewer.services.search.comment.FullCommentView; +import lcsb.mapviewer.services.view.AuthenticationToken; import lcsb.mapviewer.services.view.CommentView; @Rollback(true) @@ -30,8 +31,8 @@ public class CommentServiceTest extends ServiceTestFunctions { static Logger logger = Logger.getLogger(CommentServiceTest.class); Model model; Project project; - Element alias; - Element alias2; + Element alias; + Element alias2; private String projectId = "Some_id"; @Before @@ -191,7 +192,11 @@ public class CommentServiceTest extends ServiceTestFunctions { Reaction reaction = model.getReactionByReactionId("re3"); Comment comment = commentService.addComment("John Doe", "a@a.pl", "Conteneta 1", model, new Point2D.Double(0, 1), reaction, false, model); - List<CommentView> views = commentService.getCommentsByMap(model); + AuthenticationToken adminToken; + // assume that we have admin account with all the privileges + adminToken = userService.login("admin", "admin"); + + List<CommentView> views = commentService.getCommentsByMap(model, adminToken); assertNotNull(views.get(0).getTitle()); commentDao.delete(comment); diff --git a/web/src/main/java/lcsb/mapviewer/bean/FeedbackBean.java b/web/src/main/java/lcsb/mapviewer/bean/FeedbackBean.java index 3f3b3709b7..dc31384c26 100644 --- a/web/src/main/java/lcsb/mapviewer/bean/FeedbackBean.java +++ b/web/src/main/java/lcsb/mapviewer/bean/FeedbackBean.java @@ -1,8 +1,6 @@ package lcsb.mapviewer.bean; import java.awt.geom.Point2D; -import java.beans.PropertyChangeEvent; -import java.beans.PropertyChangeListener; import java.io.Serializable; import java.util.ArrayList; import java.util.List; @@ -69,12 +67,6 @@ public class FeedbackBean extends AbstractMarkerManagerBean<FullCommentView> imp */ private static final long serialVersionUID = 1L; - /** - * String representing {@link #showComments} property used by - * {@link #firePropertyChange(String, Object, Object)}. - */ - private static final String SHOW_COMMENTS_PROPERTY = "SHOW_COMMENTS_PROPERTY"; - /** * Default class logger. */ @@ -88,7 +80,7 @@ public class FeedbackBean extends AbstractMarkerManagerBean<FullCommentView> imp /** * List of possible elements to comment (names only). */ - private List<String> feedbackElementStringList = new ArrayList<String>(); + private List<String> feedbackElementStringList = new ArrayList<>(); /** * Information if the comment should be visible. @@ -115,11 +107,6 @@ public class FeedbackBean extends AbstractMarkerManagerBean<FullCommentView> imp */ private List<CommentView> commentList = null; - /** - * Boolean flag determining if comments should be visible or not. - */ - private String showComments = ""; - /** * Coordinates where comment should be placed. */ @@ -408,22 +395,20 @@ public class FeedbackBean extends AbstractMarkerManagerBean<FullCommentView> imp return; } if (!getUserHasCommentPrivilege()) { - commentList = new ArrayList<CommentView>(); + commentList = new ArrayList<>(); logger.warn("User has not privilages"); return; } else { - commentList = commentService.getCommentsByMap(getCurrentTopModel()); + commentList = commentService.getCommentsByMap(getCurrentTopModel(), userBean.getAuthenticationToken()); } } catch (Exception e) { - logger.error(e, e); - sendError("Internal server error. More info in logs."); + sendError("Internal server error. More info in logs.", e); } } @Override public void clear() { - setShowComments("false"); - setCommentList(new ArrayList<CommentView>()); + setCommentList(new ArrayList<>()); clearResults(); refreshDataInJavascript(); } @@ -601,38 +586,9 @@ public class FeedbackBean extends AbstractMarkerManagerBean<FullCommentView> imp this.commentList = commentList; } - /** - * @return the showComments - * @see #showComments - */ - protected String getShowComments() { - return showComments; - } - - /** - * @param showComments - * the showComments to set - * @see #showComments - */ - protected void setShowComments(String showComments) { - String oldShowComments = this.showComments; - this.showComments = showComments; - firePropertyChange(SHOW_COMMENTS_PROPERTY, oldShowComments, showComments); - } - @Override public void init() { clearNameAndEmail(); - PropertyChangeListener showCommentsChanged = new PropertyChangeListener() { - @Override - public void propertyChange(final PropertyChangeEvent arg0) { - if (SHOW_COMMENTS_PROPERTY.equals(arg0.getPropertyName())) { - refreshComments(); - } - } - - }; - addPropertyChangeListener(showCommentsChanged); } /** @@ -640,12 +596,8 @@ public class FeedbackBean extends AbstractMarkerManagerBean<FullCommentView> imp */ protected void refreshComments() { clearResults(); - if (getShowComments().equalsIgnoreCase("TRUE")) { - for (FullCommentView marker : commentService.getCommentMarkers(getCurrentTopModel())) { - addResult(marker); - } - } else { - logger.debug("comments off"); + for (FullCommentView marker : commentService.getCommentMarkers(getCurrentTopModel())) { + addResult(marker); } refreshDataInJavascript(); } @@ -655,18 +607,6 @@ public class FeedbackBean extends AbstractMarkerManagerBean<FullCommentView> imp return new FullCommentViewFactory().searchResultToElementIdentifier(view, getCurrentTopModel()); } - /** - * Method called by client browser to change value of {@link #showComments} - * field. - * - * @param actionEvent - * default jsf {@link ActionEvent} parameter - */ - public final void setShowComments(final ActionEvent actionEvent) { - // get the request param with the overlay name - setShowComments(getRequestParameter("showComments")); - } - @Override protected ElementIdentifierDetails getElementInformationForResult(ElementIdentifier element, FullCommentView result) { throw new NotImplementedException(); -- GitLab