moving api methods to MindmapController

This commit is contained in:
Ezequiel Bergamaschi 2014-02-05 02:11:23 -03:00 committed by Ezequiel Bergamaschi
parent d089b471fc
commit d555309c22
8 changed files with 120 additions and 50 deletions

View File

@ -56,4 +56,24 @@ public class Label {
public void setColor(@NotNull String color) { public void setColor(@NotNull String color) {
this.color = color; this.color = color;
} }
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Label)) return false;
Label label = (Label) o;
return id == label.id && creator.getId() == label.creator.getId()
&& !(parent != null ? !parent.equals(label.parent) : label.parent != null);
}
@Override
public int hashCode() {
int result = id;
result = 31 * result + title.hashCode();
result = 31 * result + creator.hashCode();
result = 31 * result + (parent != null ? parent.hashCode() : 0);
return result;
}
} }

View File

@ -7,6 +7,13 @@ public class LabelMindmap implements Serializable{
private int mindmapId; private int mindmapId;
private int labelId; private int labelId;
public LabelMindmap(int labelId, int mindmapId) {
this.mindmapId = mindmapId;
this.labelId = labelId;
}
public LabelMindmap() {}
public int getMindmapId() { public int getMindmapId() {
return mindmapId; return mindmapId;
} }

View File

@ -318,7 +318,7 @@ public class Mindmap {
return result; return result;
} }
//creo que no se usa mas
public boolean hasLabel(@NotNull final String name) { public boolean hasLabel(@NotNull final String name) {
for (Label label : this.labels) { for (Label label : this.labels) {
if (label.getTitle().equals(name)) { if (label.getTitle().equals(name)) {
@ -327,4 +327,19 @@ public class Mindmap {
} }
return false; return false;
} }
@Nullable public Label findLabel(int labelId) {
Label result = null;
for (Label label : this.labels) {
if (label.getId() == labelId) {
result = label;
break;
}
}
return result;
}
public void removeLabel(@NotNull final Label label) {
this.labels.remove(label);
}
} }

View File

@ -37,9 +37,6 @@ public class LabelController extends BaseController {
@Qualifier("labelService") @Qualifier("labelService")
@Autowired @Autowired
private LabelService labelService; private LabelService labelService;
@Qualifier("mindmapService")
@Autowired
private MindmapService mindmapService;
@RequestMapping(method = RequestMethod.POST, value = "/labels", consumes = {"application/json"}) @RequestMapping(method = RequestMethod.POST, value = "/labels", consumes = {"application/json"})
@ -67,28 +64,6 @@ public class LabelController extends BaseController {
return new RestLabelList(all); return new RestLabelList(all);
} }
@RequestMapping(method = RequestMethod.POST, value = "/labels/maps", consumes = {"application/json"})
@ResponseStatus(value = HttpStatus.ACCEPTED)
public void linkToMindMaps(@RequestBody RestLabel restLabel, @RequestParam(required = true) String ids, @NotNull HttpServletResponse response) throws WiseMappingException {
int id = restLabel.getId();
Label label = labelService.getLabelById(id);
if (label == null) {
// create label..
validate(restLabel);
createLabel(restLabel);
label = restLabel.getDelegated();
response.setHeader("ResourceId", Integer.toString(label.getId()));
}
for (String mindmapId : ids.split(",")) {
final Mindmap mindmap = mindmapService.findMindmapById(Integer.parseInt(mindmapId));
if (mindmap == null) {
throw new MapCouldNotFoundException("Map could not be found. Id:" + id);
}
mindmap.addLabel(label);
mindmapService.updateMindmap(mindmap, false);
}
}
@RequestMapping(method = RequestMethod.DELETE, value = "/labels/{id}") @RequestMapping(method = RequestMethod.DELETE, value = "/labels/{id}")
@ResponseStatus(value = HttpStatus.NO_CONTENT) @ResponseStatus(value = HttpStatus.NO_CONTENT)
public void deleteLabelById(@PathVariable int id) throws WiseMappingException { public void deleteLabelById(@PathVariable int id) throws WiseMappingException {

View File

@ -31,11 +31,13 @@ import com.wisemapping.importer.ImporterFactory;
import com.wisemapping.model.Collaboration; import com.wisemapping.model.Collaboration;
import com.wisemapping.model.CollaborationProperties; import com.wisemapping.model.CollaborationProperties;
import com.wisemapping.model.CollaborationRole; import com.wisemapping.model.CollaborationRole;
import com.wisemapping.model.Label;
import com.wisemapping.model.MindMapHistory; import com.wisemapping.model.MindMapHistory;
import com.wisemapping.model.Mindmap; import com.wisemapping.model.Mindmap;
import com.wisemapping.model.User; import com.wisemapping.model.User;
import com.wisemapping.rest.model.RestCollaboration; import com.wisemapping.rest.model.RestCollaboration;
import com.wisemapping.rest.model.RestCollaborationList; import com.wisemapping.rest.model.RestCollaborationList;
import com.wisemapping.rest.model.RestLabel;
import com.wisemapping.rest.model.RestMindmap; import com.wisemapping.rest.model.RestMindmap;
import com.wisemapping.rest.model.RestMindmapHistory; import com.wisemapping.rest.model.RestMindmapHistory;
import com.wisemapping.rest.model.RestMindmapHistoryList; import com.wisemapping.rest.model.RestMindmapHistoryList;
@ -617,4 +619,32 @@ public class MindmapController extends BaseController {
result.rejectValue(fieldName, "error.not-specified", null, message); result.rejectValue(fieldName, "error.not-specified", null, message);
return new ValidationException(result); return new ValidationException(result);
} }
@RequestMapping(method = RequestMethod.DELETE, value = "/labels/maps/{id}")
@ResponseStatus(value = HttpStatus.NO_CONTENT)
public void removeLabel(@RequestBody RestLabel restLabel, @PathVariable int id) throws WiseMappingException {
final Mindmap mindmap = findMindmapById(id);
final User currentUser = Utils.getUser();
final Label delegated = restLabel.getDelegated();
assert currentUser != null;
delegated.setCreator(currentUser);
mindmapService.removeLabel(mindmap, delegated);
}
@RequestMapping(method = RequestMethod.POST, value = "/labels/maps", consumes = {"application/json"})
@ResponseStatus(value = HttpStatus.ACCEPTED)
public void addLabel(@RequestBody RestLabel restLabel, @RequestParam(required = true) String ids) throws WiseMappingException {
int labelId = restLabel.getId();
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

@ -69,4 +69,8 @@ public interface MindmapService {
boolean isAdmin(@Nullable User user); boolean isAdmin(@Nullable User user);
void purgeHistory(int mapId) throws IOException; void purgeHistory(int mapId) throws IOException;
void addLabel(@NotNull final Mindmap mindmap, @NotNull final Label label);
void removeLabel(@NotNull final Mindmap mindmap, @NotNull final Label label);
} }

View File

@ -87,6 +87,18 @@ public class MindmapServiceImpl
mindmapManager.purgeHistory(mapId); mindmapManager.purgeHistory(mapId);
} }
@Override
public void addLabel(@NotNull Mindmap mindmap, @NotNull final Label label) {
mindmap.addLabel(label);
mindmapManager.saveMindmap(mindmap);
}
@Override
public void removeLabel(@NotNull Mindmap mindmap, @NotNull Label label) {
mindmap.removeLabel(label);
mindmapManager.saveMindmap(mindmap);
}
@Override @Override
public Mindmap getMindmapByTitle(String title, User user) { public Mindmap getMindmapByTitle(String title, User user) {
return mindmapManager.getMindmapByTitle(title, user); return mindmapManager.getMindmapByTitle(title, user);

View File

@ -257,15 +257,13 @@ $(function () {
$(document).on('click', '#createLabelBtn', $(document).on('click', '#createLabelBtn',
function () { function () {
var mapIds = $('#mindmapListTable').dataTableExt.getSelectedMapsIds(); var mapIds = $('#mindmapListTable').dataTableExt.getSelectedMapsIds();
var url = mapIds.length == 0
? "c/restful/labels"
: "c/restful/labels/maps?ids=" + jQuery.makeArray(mapIds).join(',');
$("#new-folder-dialog-modal").dialogForm({ $("#new-folder-dialog-modal").dialogForm({
url: url, url: "c/restful/labels",
postUpdate: function(data, id) { postUpdate: function(data, id) {
createLabelItem(data, id); createLabelItem(data, id);
tagMindmaps(data.id || id, data.title, data.color); if (mapIds.length > 0) {
linkLabelToMindmap(mapIds, {id: id, title: data.title, color: data.color});
}
} }
}); });
} }
@ -281,7 +279,6 @@ $(function () {
if (labels) { if (labels) {
prepareLabelList(labels); prepareLabelList(labels);
$(document).one('click', '.chooseLabel', $(document).one('click', '.chooseLabel',
function () { function () {
var mapIds = $('#mindmapListTable').dataTableExt.getSelectedMapsIds(); var mapIds = $('#mindmapListTable').dataTableExt.getSelectedMapsIds();
@ -289,15 +286,7 @@ $(function () {
var labelId = $(this).attr('value'); var labelId = $(this).attr('value');
var labelName = $(this).text(); var labelName = $(this).text();
var labelColor = $(this).attr('color'); var labelColor = $(this).attr('color');
jQuery.ajax("c/restful/labels/maps?ids=" + jQuery.makeArray(mapIds).join(','), { linkLabelToMindmap(mapIds, {id: labelId, title: labelName, color: labelColor});
type:'POST',
//dataType: "json",
contentType:"application/json; charset=utf-8",
data: JSON.stringify({id: labelId}),
success: function() {
tagMindmaps(labelId, labelName, labelColor);
}
});
} }
} }
); );
@ -458,11 +447,13 @@ $(function () {
$(document).on('click', ".closeTag", function() { $(document).on('click', ".closeTag", function() {
var me = $(this); var me = $(this);
var mindmapId = me.parents("td").find("a").attr("value");
var data = { var data = {
mindmapId: me.parents("td").find("a").attr("value"), id: me.attr("value"),
labelId: me.attr("value") title: me.attr("name"),
color: me.css('background-color')
}; };
jQuery.ajax("c/restful/labels/maps", { jQuery.ajax("c/restful/labels/maps/" + mindmapId, {
async:false, async:false,
//dataType:'json', comentado momentaneamente, problema con jquery 2.1.0 //dataType:'json', comentado momentaneamente, problema con jquery 2.1.0
data:JSON.stringify(data), data:JSON.stringify(data),
@ -515,7 +506,7 @@ function labelTagsAsHtml(labels) {
'</div>' + '</div>' +
"</td>" + "</td>" +
//"<td style='padding: 0; background-color: #d8d4d4'></td>" + //"<td style='padding: 0; background-color: #d8d4d4'></td>" +
"<td class='closeTag' style='background-color:" + label.color +"' value='" + label.id + "' >" + "<td class='closeTag' style='background-color:" + label.color +"' name='" + label.title +"'value='" + label.id + "' >" +
"<span style='top: -1px;position: relative;font-size: 11px' title='delete label'>x</span>"+ "<span style='top: -1px;position: relative;font-size: 11px' title='delete label'>x</span>"+
"</td>" + "</td>" +
"</tr></tbody>" + "</tr></tbody>" +
@ -579,3 +570,19 @@ function prepareLabelList(labels) {
labelList.append('<li><div class="listSeparator"></div></li>') labelList.append('<li><div class="listSeparator"></div></li>')
labelList.append(defaultValue); labelList.append(defaultValue);
} }
function linkLabelToMindmap(mapIds, label) {
jQuery.ajax("c/restful/labels/maps?ids=" + jQuery.makeArray(mapIds).join(','), {
type: 'POST',
//dataType: "json",
contentType: "application/json; charset=utf-8",
data: JSON.stringify({
id: label.id,
title: label.title,
color: label.color
}),
success: function () {
tagMindmaps(label.id, label.title, label.color);
}
});
}