get label by id with user and validate label exists on link to mindmap

This commit is contained in:
Ezequiel Bergamaschi 2014-02-11 02:04:09 -03:00
parent c7674825a6
commit e9ca68732e
6 changed files with 29 additions and 14 deletions

View File

@ -17,7 +17,7 @@ public interface LabelManager {
List<Label> getAllLabels(@NotNull final User user);
@Nullable
Label getLabelById(int id);
Label getLabelById(int id, @NotNull final User user);
@Nullable
Label getLabelByTitle(@NotNull final String title, @NotNull final User user);

View File

@ -29,19 +29,16 @@ public class LabelManagerImpl extends HibernateDaoSupport
@Nullable
@Override
public Label getLabelById(int id) {
return getHibernateTemplate().get(Label.class, id);
public Label getLabelById(int id, @NotNull final User user) {
List<Label> labels = getHibernateTemplate().find("from com.wisemapping.model.Label wisemapping where id=? and creator=?", new Object[]{id, user});
return getFirst(labels);
}
@Nullable
@Override
public Label getLabelByTitle(@NotNull String title, @NotNull final User user) {
Label result = null;
final List<Label> labels = getHibernateTemplate().find("from com.wisemapping.model.Label wisemapping where title=? and creator=?", new Object[]{title, user});
if (labels != null && !labels.isEmpty()) {
result = labels.get(0);
}
return result;
return getFirst(labels);
}
@Override
@ -49,5 +46,12 @@ public class LabelManagerImpl extends HibernateDaoSupport
getHibernateTemplate().delete(label);
}
@Nullable private Label getFirst(List<Label> labels) {
Label result = null;
if (labels != null && !labels.isEmpty()) {
result = labels.get(0);
}
return result;
}
}

View File

@ -69,7 +69,7 @@ public class LabelController extends BaseController {
@ResponseStatus(value = HttpStatus.NO_CONTENT)
public void deleteLabelById(@PathVariable int id) throws WiseMappingException {
final User user = Utils.getUser();
final Label label = labelService.getLabelById(id);
final Label label = labelService.getLabelById(id, user);
if (label == null) {
throw new LabelCouldNotFoundException("Label could not be found. Id: " + id);
}

View File

@ -20,6 +20,7 @@ package com.wisemapping.rest;
import com.mangofactory.swagger.annotations.ApiIgnore;
import com.wisemapping.exceptions.ImportUnexpectedException;
import com.wisemapping.exceptions.LabelCouldNotFoundException;
import com.wisemapping.exceptions.MapCouldNotFoundException;
import com.wisemapping.exceptions.MultipleSessionsOpenException;
import com.wisemapping.exceptions.SessionExpiredException;
@ -45,6 +46,7 @@ import com.wisemapping.rest.model.RestMindmapInfo;
import com.wisemapping.rest.model.RestMindmapList;
import com.wisemapping.security.Utils;
import com.wisemapping.service.CollaborationException;
import com.wisemapping.service.LabelService;
import com.wisemapping.service.LockInfo;
import com.wisemapping.service.LockManager;
import com.wisemapping.service.MindmapService;
@ -88,6 +90,10 @@ public class MindmapController extends BaseController {
@Autowired
private MindmapService mindmapService;
@Qualifier("labelService")
@Autowired
private LabelService labelService;
@RequestMapping(method = RequestMethod.GET, value = "/maps/{id}", produces = {"application/json", "application/xml", "text/html"})
@ResponseBody
public RestMindmap retrieve(@PathVariable int id) throws WiseMappingException {
@ -634,13 +640,18 @@ public class MindmapController extends BaseController {
@ResponseStatus(value = HttpStatus.OK)
public void addLabel(@RequestBody RestLabel restLabel, @RequestParam(required = true) String ids) throws WiseMappingException {
int labelId = restLabel.getId();
final User user = Utils.getUser();
final Label delegated = restLabel.getDelegated();
delegated.setCreator(user);
final Label found = labelService.getLabelById(labelId, user);
if (found == null) {
throw new LabelCouldNotFoundException("Label could not be found. Id: " + labelId);
}
for (String id : ids.split(",")) {
final int mindmapId = Integer.parseInt(id);
final Mindmap mindmap = findMindmapById(mindmapId);
final Label label = mindmap.findLabel(labelId);
if (label == null) {
final Label delegated = restLabel.getDelegated();
delegated.setCreator(Utils.getUser());
mindmapService.addLabel(mindmap, delegated);
}
}

View File

@ -15,7 +15,7 @@ public interface LabelService {
@NotNull List<Label> getAll(@NotNull final User user);
@Nullable
Label getLabelById(int id);
Label getLabelById(int id, @NotNull final User user);
public Label getLabelByTitle(@NotNull String title, @NotNull final User user);

View File

@ -31,8 +31,8 @@ public class LabelServiceImpl implements LabelService {
}
@Override @Nullable
public Label getLabelById(int id) {
return labelManager.getLabelById(id);
public Label getLabelById(int id, @NotNull final User user) {
return labelManager.getLabelById(id, user);
}
@Nullable