2012-02-12 21:57:11 -03:00
|
|
|
package com.wisemapping.rest;
|
|
|
|
|
|
|
|
|
2012-02-21 14:22:43 -03:00
|
|
|
import com.wisemapping.exceptions.WiseMappingException;
|
2012-02-12 21:57:11 -03:00
|
|
|
import com.wisemapping.model.MindMap;
|
2012-02-17 10:42:20 -03:00
|
|
|
import com.wisemapping.model.MindmapUser;
|
|
|
|
import com.wisemapping.model.User;
|
2012-02-19 21:07:24 -03:00
|
|
|
import com.wisemapping.rest.model.RestMindmap;
|
2012-03-15 21:13:47 -03:00
|
|
|
import com.wisemapping.rest.model.RestMindmapInfo;
|
2012-02-20 14:42:07 -03:00
|
|
|
import com.wisemapping.rest.model.RestMindmapList;
|
2012-02-21 14:22:43 -03:00
|
|
|
import com.wisemapping.security.Utils;
|
2012-02-12 21:57:11 -03:00
|
|
|
import com.wisemapping.service.MindmapService;
|
2012-04-07 12:45:35 -03:00
|
|
|
import com.wisemapping.validator.MapInfoValidator;
|
2012-03-14 01:49:05 -03:00
|
|
|
import org.jetbrains.annotations.NotNull;
|
2012-02-16 01:16:51 -03:00
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
2012-02-21 20:04:17 -03:00
|
|
|
import org.springframework.http.HttpStatus;
|
2012-02-12 21:57:11 -03:00
|
|
|
import org.springframework.stereotype.Controller;
|
2012-04-07 12:45:35 -03:00
|
|
|
import org.springframework.validation.BeanPropertyBindingResult;
|
|
|
|
import org.springframework.validation.BindingResult;
|
2012-02-21 14:22:43 -03:00
|
|
|
import org.springframework.web.bind.annotation.*;
|
2012-02-16 01:16:51 -03:00
|
|
|
import org.springframework.web.servlet.ModelAndView;
|
2012-02-12 21:57:11 -03:00
|
|
|
|
2012-03-14 01:49:05 -03:00
|
|
|
import javax.servlet.http.HttpServletResponse;
|
2012-02-12 21:57:11 -03:00
|
|
|
import java.io.IOException;
|
2012-02-20 14:42:07 -03:00
|
|
|
import java.util.ArrayList;
|
2012-02-21 14:22:43 -03:00
|
|
|
import java.util.Calendar;
|
2012-02-17 10:42:20 -03:00
|
|
|
import java.util.List;
|
2012-02-12 21:57:11 -03:00
|
|
|
|
2012-03-14 22:10:44 -03:00
|
|
|
|
2012-02-12 21:57:11 -03:00
|
|
|
@Controller
|
2012-03-13 15:57:30 -03:00
|
|
|
public class MindmapController extends BaseController {
|
2012-02-16 01:16:51 -03:00
|
|
|
@Autowired
|
2012-02-12 21:57:11 -03:00
|
|
|
private MindmapService mindmapService;
|
|
|
|
|
2012-02-21 17:41:51 -03:00
|
|
|
@RequestMapping(method = RequestMethod.GET, value = "/maps/{id}", produces = {"application/json", "text/html", "application/xml"})
|
2012-02-12 21:57:11 -03:00
|
|
|
@ResponseBody
|
2012-02-16 01:16:51 -03:00
|
|
|
public ModelAndView getMindmap(@PathVariable int id) throws IOException {
|
2012-02-12 21:57:11 -03:00
|
|
|
final MindMap mindMap = mindmapService.getMindmapById(id);
|
2012-02-19 21:07:24 -03:00
|
|
|
final RestMindmap map = new RestMindmap(mindMap);
|
2012-02-16 01:16:51 -03:00
|
|
|
return new ModelAndView("mapView", "map", map);
|
2012-02-12 21:57:11 -03:00
|
|
|
}
|
2012-02-17 10:42:20 -03:00
|
|
|
|
2012-02-21 17:41:51 -03:00
|
|
|
@RequestMapping(method = RequestMethod.GET, value = "/maps", produces = {"application/json", "text/html", "application/xml"})
|
2012-02-17 10:42:20 -03:00
|
|
|
public ModelAndView getMindmaps() throws IOException {
|
|
|
|
final User user = com.wisemapping.security.Utils.getUser();
|
|
|
|
|
2012-02-20 14:42:07 -03:00
|
|
|
final List<MindmapUser> mapsByUser = mindmapService.getMindmapUserByUser(user);
|
|
|
|
final List<MindMap> mindmaps = new ArrayList<MindMap>();
|
|
|
|
for (MindmapUser mindmapUser : mapsByUser) {
|
|
|
|
mindmaps.add(mindmapUser.getMindMap());
|
|
|
|
}
|
|
|
|
|
|
|
|
final RestMindmapList restMindmapList = new RestMindmapList(mindmaps);
|
|
|
|
return new ModelAndView("mapsView", "list", restMindmapList);
|
2012-02-17 10:42:20 -03:00
|
|
|
}
|
2012-02-21 14:22:43 -03:00
|
|
|
|
2012-02-21 18:14:28 -03:00
|
|
|
@RequestMapping(method = RequestMethod.PUT, value = "/maps/{id}", consumes = {"application/xml", "application/json"}, produces = {"application/json", "text/html", "application/xml"})
|
2012-02-21 20:04:17 -03:00
|
|
|
@ResponseStatus(value = HttpStatus.NO_CONTENT)
|
|
|
|
public void updateMap(@RequestBody RestMindmap restMindmap, @PathVariable int id, @RequestParam(required = false) boolean minor) throws IOException, WiseMappingException {
|
2012-02-21 14:22:43 -03:00
|
|
|
|
|
|
|
final MindMap mindMap = mindmapService.getMindmapById(id);
|
|
|
|
final User user = Utils.getUser();
|
|
|
|
|
2012-03-14 01:49:05 -03:00
|
|
|
// Validate arguments ...
|
2012-02-21 14:22:43 -03:00
|
|
|
final String properties = restMindmap.getProperties();
|
2012-03-14 01:49:05 -03:00
|
|
|
if (properties == null) {
|
|
|
|
throw new IllegalArgumentException("Map properties can not be null");
|
|
|
|
}
|
2012-02-21 14:22:43 -03:00
|
|
|
mindMap.setProperties(properties);
|
|
|
|
|
2012-03-14 01:49:05 -03:00
|
|
|
// Validate content ...
|
2012-02-21 14:22:43 -03:00
|
|
|
final String xml = restMindmap.getXml();
|
2012-03-14 01:49:05 -03:00
|
|
|
if (xml == null) {
|
|
|
|
throw new IllegalArgumentException("Map xml can not be null");
|
|
|
|
}
|
2012-02-21 14:22:43 -03:00
|
|
|
mindMap.setXmlStr(xml);
|
2012-03-14 01:49:05 -03:00
|
|
|
|
|
|
|
// Update map ...
|
|
|
|
updateMindmap(minor, mindMap, user);
|
2012-02-21 14:22:43 -03:00
|
|
|
}
|
2012-02-21 20:04:17 -03:00
|
|
|
|
2012-03-15 01:47:42 -03:00
|
|
|
@RequestMapping(method = RequestMethod.PUT, value = "/maps/{id}/title", consumes = {"text/plain"}, produces = {"application/json", "text/html", "application/xml"})
|
|
|
|
@ResponseStatus(value = HttpStatus.NO_CONTENT)
|
|
|
|
public void changeMapTitle(@RequestBody String title, @PathVariable int id) throws WiseMappingException {
|
|
|
|
|
|
|
|
final MindMap mindMap = mindmapService.getMindmapById(id);
|
|
|
|
final User user = Utils.getUser();
|
|
|
|
|
|
|
|
// Is there a map with the same name ?
|
|
|
|
if (mindmapService.getMindmapByTitle(title, user) != null) {
|
|
|
|
throw new IllegalArgumentException("Map already exists with this title");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update map ...
|
|
|
|
final MindMap mindmap = mindmapService.getMindmapById(id);
|
|
|
|
mindmap.setTitle(title);
|
|
|
|
updateMindmap(true, mindMap, user);
|
|
|
|
}
|
|
|
|
|
|
|
|
@RequestMapping(method = RequestMethod.PUT, value = "/maps/{id}/description", consumes = {"text/plain"}, produces = {"application/json", "text/html", "application/xml"})
|
|
|
|
@ResponseStatus(value = HttpStatus.NO_CONTENT)
|
|
|
|
public void changeMapDescription(@RequestBody String description, @PathVariable int id) throws WiseMappingException {
|
|
|
|
|
|
|
|
final MindMap mindMap = mindmapService.getMindmapById(id);
|
|
|
|
final User user = Utils.getUser();
|
|
|
|
|
|
|
|
// Update map ...
|
|
|
|
final MindMap mindmap = mindmapService.getMindmapById(id);
|
|
|
|
mindmap.setDescription(description);
|
|
|
|
updateMindmap(true, mindMap, user);
|
|
|
|
}
|
|
|
|
|
2012-05-20 21:46:55 -03:00
|
|
|
@RequestMapping(method = RequestMethod.PUT, value = "/maps/{id}/publish", consumes = {"text/plain"}, produces = {"application/json", "text/html", "application/xml"})
|
|
|
|
@ResponseStatus(value = HttpStatus.NO_CONTENT)
|
|
|
|
public void changeMapPublish(@RequestBody String value, @PathVariable int id) throws WiseMappingException {
|
|
|
|
|
|
|
|
final MindMap mindMap = mindmapService.getMindmapById(id);
|
|
|
|
final User user = Utils.getUser();
|
|
|
|
|
|
|
|
if (!mindMap.getOwner().equals(user)) {
|
|
|
|
throw new IllegalArgumentException("No enough to execute this operation");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update map status ...
|
|
|
|
mindMap.setPublic(Boolean.parseBoolean(value));
|
|
|
|
updateMindmap(true, mindMap, user);
|
|
|
|
|
|
|
|
}
|
2012-03-15 01:21:46 -03:00
|
|
|
|
|
|
|
@RequestMapping(method = RequestMethod.DELETE, value = "/maps/{id}")
|
|
|
|
@ResponseStatus(value = HttpStatus.NO_CONTENT)
|
2012-05-20 21:46:55 -03:00
|
|
|
public void updateMap( @PathVariable int id) throws IOException, WiseMappingException {
|
2012-03-15 01:21:46 -03:00
|
|
|
final User user = Utils.getUser();
|
|
|
|
final MindMap mindmap = mindmapService.getMindmapById(id);
|
|
|
|
mindmapService.removeMindmap(mindmap, user);
|
|
|
|
}
|
|
|
|
|
2012-04-06 17:05:42 -03:00
|
|
|
@RequestMapping(method = RequestMethod.DELETE, value = "/maps/batch")
|
|
|
|
@ResponseStatus(value = HttpStatus.NO_CONTENT)
|
2012-05-19 21:36:34 -03:00
|
|
|
public void batchDelete(@RequestParam(required = true) String ids) throws IOException, WiseMappingException {
|
2012-04-06 17:05:42 -03:00
|
|
|
final User user = Utils.getUser();
|
|
|
|
final String[] mapsIds = ids.split(",");
|
|
|
|
for (final String mapId : mapsIds) {
|
|
|
|
final MindMap mindmap = mindmapService.getMindmapById(Integer.parseInt(mapId));
|
|
|
|
mindmapService.removeMindmap(mindmap, user);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-14 01:49:05 -03:00
|
|
|
@RequestMapping(method = RequestMethod.PUT, value = "/maps/{id}/xml", consumes = {"application/xml"}, produces = {"application/json", "text/html", "application/xml"})
|
2012-03-13 15:57:30 -03:00
|
|
|
@ResponseStatus(value = HttpStatus.NO_CONTENT)
|
2012-03-14 01:49:05 -03:00
|
|
|
public void updateMapXml(@RequestBody String xml, @PathVariable int id, @RequestParam(required = false) boolean minor) throws IOException, WiseMappingException {
|
2012-03-13 15:57:30 -03:00
|
|
|
|
|
|
|
final MindMap mindMap = mindmapService.getMindmapById(id);
|
|
|
|
final User user = Utils.getUser();
|
|
|
|
|
2012-03-14 01:49:05 -03:00
|
|
|
if (xml == null || xml.isEmpty()) {
|
|
|
|
throw new IllegalArgumentException("Map xml can not be null");
|
|
|
|
}
|
|
|
|
mindMap.setXmlStr(xml);
|
|
|
|
|
|
|
|
// Update map ...
|
|
|
|
updateMindmap(minor, mindMap, user);
|
|
|
|
}
|
|
|
|
|
2012-03-15 01:21:46 -03:00
|
|
|
private void updateMindmap(boolean minor, @NotNull final MindMap mindMap, @NotNull final User user) throws WiseMappingException {
|
2012-03-13 15:57:30 -03:00
|
|
|
final Calendar now = Calendar.getInstance();
|
|
|
|
mindMap.setLastModificationTime(now);
|
|
|
|
mindMap.setLastModifierUser(user.getUsername());
|
2012-03-14 01:49:05 -03:00
|
|
|
mindmapService.updateMindmap(mindMap, minor);
|
|
|
|
}
|
2012-03-13 15:57:30 -03:00
|
|
|
|
2012-03-14 01:49:05 -03:00
|
|
|
@RequestMapping(method = RequestMethod.POST, value = "/maps", consumes = {"application/xml", "application/json"})
|
|
|
|
@ResponseStatus(value = HttpStatus.CREATED)
|
|
|
|
public void createMap(@RequestBody RestMindmap restMindmap, @NotNull HttpServletResponse response) throws IOException, WiseMappingException {
|
2012-03-13 15:57:30 -03:00
|
|
|
|
2012-04-07 12:45:35 -03:00
|
|
|
// Validate ...
|
|
|
|
final BindingResult result = new BeanPropertyBindingResult(restMindmap, "");
|
|
|
|
new MapInfoValidator(mindmapService).validate(restMindmap.getDelegated(), result);
|
|
|
|
if (result.hasErrors()) {
|
|
|
|
throw new ValidationException(result);
|
2012-03-14 01:49:05 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Some basic validations ...
|
|
|
|
final User user = Utils.getUser();
|
|
|
|
|
|
|
|
// If the user has not specified the xml content, add one ...
|
|
|
|
final MindMap delegated = restMindmap.getDelegated();
|
|
|
|
String xml = restMindmap.getXml();
|
|
|
|
if (xml == null || xml.isEmpty()) {
|
|
|
|
xml = MindMap.getDefaultMindmapXml(restMindmap.getTitle());
|
|
|
|
}
|
|
|
|
delegated.setOwner(user);
|
2012-03-15 21:13:47 -03:00
|
|
|
delegated.setXmlStr(xml);
|
2012-03-14 01:49:05 -03:00
|
|
|
|
|
|
|
// Add new mindmap ...
|
|
|
|
mindmapService.addMindmap(delegated, user);
|
|
|
|
|
|
|
|
// Return the new created map ...
|
|
|
|
response.setHeader("Location", "/service/maps/" + delegated.getId());
|
2012-04-06 20:28:25 -03:00
|
|
|
response.setHeader("ResourceId", Integer.toString(delegated.getId()));
|
2012-03-13 15:57:30 -03:00
|
|
|
}
|
|
|
|
|
2012-03-15 21:13:47 -03:00
|
|
|
@RequestMapping(method = RequestMethod.POST, value = "/maps/{id}", consumes = {"application/xml", "application/json"})
|
|
|
|
@ResponseStatus(value = HttpStatus.CREATED)
|
|
|
|
public void copyMap(@RequestBody RestMindmapInfo restMindmap, @PathVariable int id, @NotNull HttpServletResponse response) throws IOException, WiseMappingException {
|
2012-05-20 21:46:55 -03:00
|
|
|
// Validate ...
|
2012-04-07 20:05:50 -03:00
|
|
|
final BindingResult result = new BeanPropertyBindingResult(restMindmap, "");
|
|
|
|
new MapInfoValidator(mindmapService).validate(restMindmap.getDelegated(), result);
|
|
|
|
if (result.hasErrors()) {
|
|
|
|
throw new ValidationException(result);
|
2012-03-15 21:13:47 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Some basic validations ...
|
|
|
|
final User user = Utils.getUser();
|
|
|
|
|
|
|
|
// Create a shallowCopy of the map ...
|
|
|
|
final MindMap mindMap = mindmapService.getMindmapById(id);
|
|
|
|
final MindMap clonedMap = mindMap.shallowClone();
|
|
|
|
clonedMap.setTitle(restMindmap.getTitle());
|
|
|
|
clonedMap.setDescription(restMindmap.getDescription());
|
|
|
|
clonedMap.setOwner(user);
|
|
|
|
|
|
|
|
// Add new mindmap ...
|
|
|
|
mindmapService.addMindmap(clonedMap, user);
|
|
|
|
|
|
|
|
// Return the new created map ...
|
|
|
|
response.setHeader("Location", "/service/maps/" + clonedMap.getId());
|
2012-04-07 12:45:35 -03:00
|
|
|
response.setHeader("ResourceId", Integer.toString(clonedMap.getId()));
|
2012-03-15 21:13:47 -03:00
|
|
|
}
|
|
|
|
|
2012-02-12 21:57:11 -03:00
|
|
|
}
|