Commit 36334fb7 authored by Piotr Gawron's avatar Piotr Gawron
Browse files

comment rest api improved

parent e8fc9a3c
package lcsb.mapviewer.persist.dao.map.layout.alias;
package lcsb.mapviewer.persist.dao.map.species;
import lcsb.mapviewer.model.map.species.Element;
import lcsb.mapviewer.persist.dao.BaseDao;
......@@ -9,12 +9,12 @@ import lcsb.mapviewer.persist.dao.BaseDao;
* @author Piotr Gawron
*
*/
public class AliasDao extends BaseDao<Element> {
public class ElementDao extends BaseDao<Element> {
/**
* Default constructor.
*/
public AliasDao() {
public ElementDao() {
super(Element.class);
}
......
/**
* Data access objects for aliases.
*/
package lcsb.mapviewer.persist.dao.map.layout.alias;
package lcsb.mapviewer.persist.dao.map.species;
......@@ -38,7 +38,7 @@
<bean id="DataMiningSetDao" class="lcsb.mapviewer.persist.dao.map.graph.DataMiningSetDao" parent="BaseDao"/>
<bean id="AliasDao" class="lcsb.mapviewer.persist.dao.map.layout.alias.AliasDao" parent="BaseDao"/>
<bean id="ElementDao" class="lcsb.mapviewer.persist.dao.map.species.ElementDao" parent="BaseDao"/>
<bean id="ReferenceGenomeDao" class="lcsb.mapviewer.persist.dao.map.layout.ReferenceGenomeDao" parent="BaseDao"/>
<bean id="ReferenceGenomeGeneMappingDao" class="lcsb.mapviewer.persist.dao.map.layout.ReferenceGenomeGeneMappingDao" parent="BaseDao"/>
......
......@@ -49,7 +49,7 @@ import lcsb.mapviewer.persist.dao.log.LogDao;
import lcsb.mapviewer.persist.dao.map.CommentDao;
import lcsb.mapviewer.persist.dao.map.ModelDao;
import lcsb.mapviewer.persist.dao.map.ReactionDao;
import lcsb.mapviewer.persist.dao.map.layout.alias.AliasDao;
import lcsb.mapviewer.persist.dao.map.species.ElementDao;
import lcsb.mapviewer.persist.dao.map.statistics.SearchHistoryDao;
import lcsb.mapviewer.persist.dao.user.PrivilegeDao;
import lcsb.mapviewer.persist.dao.user.UserDao;
......@@ -100,7 +100,7 @@ public abstract class PersistTestFunctions {
protected ReactionDao reactionDao;
@Autowired
protected AliasDao aliasDao;
protected ElementDao aliasDao;
protected User user;
......
package lcsb.mapviewer.api.comment;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
......@@ -12,7 +13,6 @@ 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")
......@@ -22,9 +22,9 @@ public class CommentController extends BaseController {
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);
public List<Map<String,Object>> getOverlayList(@RequestParam(value = "token") String token, @RequestParam(value = "projectId") String projectId,
@RequestParam(value = "columns", defaultValue = "") String columns) throws SecurityException, QueryException {
return commentController.getCommentList(token, projectId, columns);
}
/**
......
package lcsb.mapviewer.api.comment;
import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import lcsb.mapviewer.api.QueryException;
import lcsb.mapviewer.model.Project;
import lcsb.mapviewer.model.map.Comment;
import lcsb.mapviewer.model.map.model.Model;
import lcsb.mapviewer.model.map.reaction.Reaction;
import lcsb.mapviewer.model.map.species.Element;
import lcsb.mapviewer.model.user.PrivilegeType;
import lcsb.mapviewer.persist.dao.map.ReactionDao;
import lcsb.mapviewer.persist.dao.map.species.ElementDao;
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.overlay.IconManager;
import lcsb.mapviewer.services.view.AuthenticationToken;
import lcsb.mapviewer.services.view.CommentView;
@Transactional(value = "txManager")
public class CommentRestImpl {
Logger logger = Logger.getLogger(CommentRestImpl.class);
@Autowired
private IUserService userService;
......@@ -26,13 +42,30 @@ public class CommentRestImpl {
@Autowired
private ICommentService commentService;
public List<CommentView> getCommentList(String token, String projectId) throws SecurityException, QueryException {
@Autowired
private ReactionDao reactionDao;
@Autowired
private ElementDao elementDao;
public List<Map<String, Object>> getCommentList(String token, String projectId, String columns) 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);
Project project = model.getProject();
boolean isAdmin = userService.userHasPrivilege(authenticationToken, PrivilegeType.EDIT_COMMENTS_PROJECT, project);
Set<String> columnsSet = createCommentColumnSet(columns, isAdmin);
List<Map<String, Object>> result = new ArrayList<>();
List<Comment> comments = commentService.getCommentsByProject(project, authenticationToken);
for (Comment comment : comments) {
result.add(preparedComment(comment, columnsSet, isAdmin));
}
return result;
}
/**
......@@ -86,4 +119,151 @@ public class CommentRestImpl {
this.commentService = commentService;
}
private Map<String, Object> preparedComment(Comment comment, Set<String> columnsSet, boolean admin) {
Map<String, Object> result = new HashMap<>();
for (String string : columnsSet) {
String column = string.toLowerCase();
Object value = null;
if (column.equals("id") || column.equals("idobject")) {
value = comment.getId();
} else if (column.equals("modelid")) {
value = comment.getModelData().getId();
} else if (column.equals("title")) {
value = getTitle(comment);
} else if (column.equals("pinned")) {
value = comment.isPinned();
} else if (column.equals("content")) {
value = comment.getContent();
} else if (column.equals("removed")) {
value = comment.isDeleted();
} else if (column.equals("coord")) {
value = getCoordinates(comment);
} else if (column.equals("icon")) {
value = IconManager.getInstance().getCommentIcon();
} else if (admin) {
if (column.equals("author")) {
value = comment.getName();
} else if (column.equals("email")) {
value = comment.getEmail();
} else {
value = "Unknown column";
}
} else {
value = "Unknown column";
}
result.put(string, value);
}
return result;
}
private String getTitle(Comment comment) {
String title = "";
if (comment.getCoordinates() != null) {
title = "Comment (coord: " + String.format("%.2f", comment.getCoordinates().getX()) + ", " + String.format("%.2f", comment.getCoordinates().getY())
+ ")";
}
if (comment.getTableName() != null) {
if (comment.getTableName().getName().contains("Reaction")) {
Reaction reaction = reactionDao.getById(comment.getTableId());
if (reaction != null) {
title = "Reaction " + reaction.getIdReaction();
} else {
logger.warn("Invalid reaction dbID: " + comment.getTableId());
}
} else {
Element alias = elementDao.getById(comment.getTableId());
if (alias != null) {
title = alias.getName();
} else {
logger.warn("Invalid alias dbID: " + comment.getTableId());
}
}
}
return title;
}
private Point2D getCoordinates(Comment comment) {
Point2D coordinates = comment.getCoordinates();
if (comment.getCoordinates() != null) {
coordinates = comment.getCoordinates();
}
if (comment.getTableName() != null) {
if (comment.getTableName().getName().contains("Reaction")) {
Reaction reaction = reactionDao.getById(comment.getTableId());
if (reaction != null) {
coordinates = reaction.getCenterPoint();
} else {
logger.warn("Invalid reaction dbID: " + comment.getTableId());
}
} else {
Element alias = elementDao.getById(comment.getTableId());
if (alias != null) {
coordinates = alias.getCenter();
} else {
logger.warn("Invalid alias dbID: " + comment.getTableId());
}
}
}
return coordinates;
}
private Set<String> createCommentColumnSet(String columns, boolean admin) {
Set<String> columnsSet = new HashSet<>();
if (columns.equals("")) {
columnsSet.add("title");
columnsSet.add("icon");
columnsSet.add("content");
columnsSet.add("pinned");
columnsSet.add("removed");
columnsSet.add("coord");
columnsSet.add("modelId");
columnsSet.add("id");
if (admin) {
columnsSet.add("author");
columnsSet.add("email");
}
} else {
for (String str : columns.split(",")) {
columnsSet.add(str);
}
}
return columnsSet;
}
/**
* @return the reactionDao
* @see #reactionDao
*/
public ReactionDao getReactionDao() {
return reactionDao;
}
/**
* @param reactionDao
* the reactionDao to set
* @see #reactionDao
*/
public void setReactionDao(ReactionDao reactionDao) {
this.reactionDao = reactionDao;
}
/**
* @return the elementDao
* @see #elementDao
*/
public ElementDao getElementDao() {
return elementDao;
}
/**
* @param elementDao
* the elementDao to set
* @see #elementDao
*/
public void setElementDao(ElementDao elementDao) {
this.elementDao = elementDao;
}
}
......@@ -32,7 +32,7 @@ public class CommentRestImplTest extends RestTestFunctions {
@Test
public void testGetForNonExisting() throws Exception {
try {
commentRestImpl.getCommentList(token.getId(), "unk");
commentRestImpl.getCommentList(token.getId(), "unk", "");
fail("Exception expected");
} catch (QueryException e2) {
assertTrue(e2.getMessage().contains("Project with given id doesn't exist"));
......
......@@ -16,6 +16,7 @@ import lcsb.mapviewer.common.ObjectUtils;
import lcsb.mapviewer.common.Pair;
import lcsb.mapviewer.common.exception.InvalidArgumentException;
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;
......@@ -404,4 +405,20 @@ public class CommentService implements ICommentService {
return true;
}
@Override
public List<Comment> getCommentsByProject(Project project, AuthenticationToken token) throws UserAccessException {
boolean editComments = userService.userHasPrivilege(token, PrivilegeType.EDIT_COMMENTS_PROJECT, project);
boolean viewProject = userService.userHasPrivilege(token, PrivilegeType.VIEW_PROJECT, project);
if (!editComments && !viewProject) {
throw new UserAccessException("You have no privileges to see comments for given project");
}
List<Comment> comments = new ArrayList<>();
for (ModelData model : project.getModels()) {
List<Comment> modelComments = commentDao.getCommentByModel(model, null, null);
comments.addAll(modelComments);
}
return comments;
}
}
......@@ -3,6 +3,7 @@ package lcsb.mapviewer.services.interfaces;
import java.awt.geom.Point2D;
import java.util.List;
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;
......@@ -68,6 +69,8 @@ public interface ICommentService {
* @throws UserAccessException
*/
List<CommentView> getCommentsByMap(Model model, AuthenticationToken token) throws UserAccessException;
List<Comment> getCommentsByProject(Project project, AuthenticationToken token) throws UserAccessException;
/**
* Returns number of comments in the system.
......
......@@ -64,7 +64,7 @@ import lcsb.mapviewer.persist.dao.log.LogDao;
import lcsb.mapviewer.persist.dao.map.CommentDao;
import lcsb.mapviewer.persist.dao.map.ModelDao;
import lcsb.mapviewer.persist.dao.map.ReactionDao;
import lcsb.mapviewer.persist.dao.map.layout.alias.AliasDao;
import lcsb.mapviewer.persist.dao.map.species.ElementDao;
import lcsb.mapviewer.persist.dao.map.statistics.SearchHistoryDao;
import lcsb.mapviewer.persist.dao.user.PrivilegeDao;
import lcsb.mapviewer.persist.dao.user.UserDao;
......@@ -178,7 +178,7 @@ public abstract class ServiceTestFunctions {
protected ReactionDao reactionDao;
@Autowired
protected AliasDao aliasDao;
protected ElementDao aliasDao;
protected User user;
protected User user2 = null;
......
......@@ -49,7 +49,7 @@ import lcsb.mapviewer.model.user.User;
import lcsb.mapviewer.model.user.UserAnnotationSchema;
import lcsb.mapviewer.model.user.UserClassAnnotators;
import lcsb.mapviewer.model.user.UserClassValidAnnotations;
import lcsb.mapviewer.persist.dao.map.layout.alias.AliasDao;
import lcsb.mapviewer.persist.dao.map.species.ElementDao;
import lcsb.mapviewer.services.ServiceTestFunctions;
import lcsb.mapviewer.services.UserAccessException;
import lcsb.mapviewer.services.overlay.AnnotatedObjectTreeRow;
......@@ -68,7 +68,7 @@ public class ProjectServiceTest extends ServiceTestFunctions {
String projectId = "Some_id";
@Autowired
AliasDao aliasDao;
ElementDao aliasDao;
// assume that we have admin account with all the privileges
AuthenticationToken adminToken;
......
......@@ -63,7 +63,7 @@ import lcsb.mapviewer.persist.dao.log.LogDao;
import lcsb.mapviewer.persist.dao.map.CommentDao;
import lcsb.mapviewer.persist.dao.map.ModelDao;
import lcsb.mapviewer.persist.dao.map.ReactionDao;
import lcsb.mapviewer.persist.dao.map.layout.alias.AliasDao;
import lcsb.mapviewer.persist.dao.map.species.ElementDao;
import lcsb.mapviewer.persist.dao.map.statistics.SearchHistoryDao;
import lcsb.mapviewer.persist.dao.user.PrivilegeDao;
import lcsb.mapviewer.persist.dao.user.UserDao;
......@@ -186,7 +186,7 @@ public abstract class WebTestFunctions {
protected ReactionDao reactionDao;
@Autowired
protected AliasDao aliasDao;
protected ElementDao aliasDao;
@Autowired
protected ModelViewFactory modelViewFactory;
......
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