Commit 8889ef1a authored by Piotr Gawron's avatar Piotr Gawron
Browse files

listComments method added to API

parent bf707a77
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
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;
}
}
......@@ -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"/>
......
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 {
}
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() {
......
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 {
}
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;
}
}
}
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 {
}
......@@ -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;
......
......@@ -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
......
......@@ -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.
......
......@@ -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);
......
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();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment