- Add more tests to save map

- Add new rest operations for saving map content
- Fix password encoding bug
- Fix bug that allow different users being created with the same password.
This commit is contained in:
Paulo Gustavo Veiga
2012-03-14 01:49:05 -03:00
parent 8716ff4feb
commit eb6aac4a5e
18 changed files with 297 additions and 144 deletions

View File

@@ -7,14 +7,17 @@ import com.wisemapping.model.MindmapUser;
import com.wisemapping.model.User;
import com.wisemapping.rest.model.RestMindmap;
import com.wisemapping.rest.model.RestMindmapList;
import com.wisemapping.rest.model.RestUser;
import com.wisemapping.security.Utils;
import com.wisemapping.service.MindmapService;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
@@ -55,43 +58,83 @@ public class MindmapController extends BaseController {
final MindMap mindMap = mindmapService.getMindmapById(id);
final User user = Utils.getUser();
// Validate arguments ...
final String properties = restMindmap.getProperties();
if (properties == null) {
throw new IllegalArgumentException("Map properties can not be null");
}
mindMap.setProperties(properties);
final Calendar now = Calendar.getInstance();
mindMap.setLastModificationTime(now);
mindMap.setLastModifierUser(user.getUsername());
final Calendar lastModification = Calendar.getInstance();
lastModification.setTime(new Date());
mindMap.setLastModificationTime(lastModification);
// Validate content ...
final String xml = restMindmap.getXml();
if (xml == null) {
throw new IllegalArgumentException("Map xml can not be null");
}
mindMap.setXmlStr(xml);
mindmapService.updateMindmap(mindMap, minor);
// Update map ...
updateMindmap(minor, mindMap, user);
}
@RequestMapping(method = RequestMethod.POST, value = "/maps", consumes = {"application/xml", "application/json"}, produces = {"application/json", "text/html", "application/xml"})
@RequestMapping(method = RequestMethod.PUT, value = "/maps/{id}/xml", consumes = {"application/xml"}, produces = {"application/json", "text/html", "application/xml"})
@ResponseStatus(value = HttpStatus.NO_CONTENT)
public void createMap(@RequestBody RestMindmap restMindmap, @PathVariable int id, @RequestParam(required = false) boolean minor) throws IOException, WiseMappingException {
public void updateMapXml(@RequestBody String xml, @PathVariable int id, @RequestParam(required = false) boolean minor) throws IOException, WiseMappingException {
final MindMap mindMap = mindmapService.getMindmapById(id);
final User user = Utils.getUser();
final String properties = restMindmap.getProperties();
mindMap.setProperties(properties);
if (xml == null || xml.isEmpty()) {
throw new IllegalArgumentException("Map xml can not be null");
}
mindMap.setXmlStr(xml);
// Update map ...
updateMindmap(minor, mindMap, user);
}
private void updateMindmap(boolean minor, MindMap mindMap, User user) throws WiseMappingException {
final Calendar now = Calendar.getInstance();
mindMap.setLastModificationTime(now);
mindMap.setLastModifierUser(user.getUsername());
final Calendar lastModification = Calendar.getInstance();
lastModification.setTime(new Date());
mindMap.setLastModificationTime(lastModification);
final String xml = restMindmap.getXml();
mindMap.setXmlStr(xml);
mindmapService.updateMindmap(mindMap, minor);
}
@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 {
final String title = restMindmap.getTitle();
if (title == null || title.isEmpty()) {
throw new IllegalArgumentException("Map title can not be null");
}
final String description = restMindmap.getDescription();
if (description == null || description.isEmpty()) {
throw new IllegalArgumentException("Map details can not be null");
}
// Some basic validations ...
final User user = Utils.getUser();
final MindMap mindMap = mindmapService.getMindmapByTitle(title, user);
if (mindMap != null) {
throw new IllegalArgumentException("Map already exists with title '" + title + "'");
}
// 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.setXmlStr(xml);
delegated.setOwner(user);
// Add new mindmap ...
mindmapService.addMindmap(delegated, user);
// Return the new created map ...
response.setHeader("Location", "/service/maps/" + delegated.getId());
}
}