From cabca992d1e923a994b8c8e64db41fb00493a7c5 Mon Sep 17 00:00:00 2001 From: Paulo Gustavo Veiga Date: Thu, 31 Jan 2013 22:50:21 -0300 Subject: [PATCH] Map are loaded using Rest service. --- .../main/javascript/RestPersistenceManager.js | 33 +++++++++++++++---- wise-editor/doc/ImplementingPersistence.md | 1 + .../wisemapping/rest/MindmapController.java | 9 +++++ .../src/main/webapp/jsp/mindmapEditor.jsp | 14 ++++---- 4 files changed, 42 insertions(+), 15 deletions(-) diff --git a/mindplot/src/main/javascript/RestPersistenceManager.js b/mindplot/src/main/javascript/RestPersistenceManager.js index a7cc8bd4..f7c284da 100644 --- a/mindplot/src/main/javascript/RestPersistenceManager.js +++ b/mindplot/src/main/javascript/RestPersistenceManager.js @@ -20,13 +20,13 @@ mindplot.RESTPersistenceManager = new Class({ Extends:mindplot.PersistenceManager, initialize:function (options) { this.parent(); - $assert(options.saveUrl, "saveUrl can not be null"); + $assert(options.documentUrl, "documentUrl can not be null"); $assert(options.revertUrl, "revertUrl can not be null"); $assert(options.lockUrl, "lockUrl can not be null"); $assert(options.session, "session can not be null"); $assert(options.timestamp, "timestamp can not be null"); - this.saveUrl = options.saveUrl; + this.documentUrl = options.documentUrl; this.revertUrl = options.revertUrl; this.lockUrl = options.lockUrl; this.timestamp = options.timestamp; @@ -56,7 +56,7 @@ mindplot.RESTPersistenceManager = new Class({ }, 10000); var request = new Request({ - url:this.saveUrl.replace("{id}", mapId) + "?" + query, + url:this.documentUrl.replace("{id}", mapId) + "?" + query, method:'put', async:!sync, @@ -100,10 +100,6 @@ mindplot.RESTPersistenceManager = new Class({ } events.onError(userMsg); persistence.onSave = false; - -// if (this.status != 0) { -// throw new Error("responseText:" + responseText + ",status:" + this.status); -// } }, headers:{"Content-Type":"application/json", "Accept":"application/json"}, @@ -164,6 +160,29 @@ mindplot.RESTPersistenceManager = new Class({ severity = "INFO"; } return {severity:severity, message:message}; + }, + + loadMapDom:function (mapId) { + // Let's try to open one from the local directory ... + var xml; + var xmlRequest = new Request({ + url:this.documentUrl.replace("{id}", mapId) + "/xml", + method:'get', + async:false, + headers:{"Content-Type":"text/plain","Accept":"application/xml"}, + onSuccess:function (responseText) { + xml = responseText; + } + }); + xmlRequest.send(); + + // If I could not load it from a file, hard code one. + if (xml == null) { + throw new Error("Map could not be loaded"); + } + + var parser = new DOMParser(); + return parser.parseFromString(xml, "text/xml"); } } ); diff --git a/wise-editor/doc/ImplementingPersistence.md b/wise-editor/doc/ImplementingPersistence.md index d536db7b..25d42e19 100644 --- a/wise-editor/doc/ImplementingPersistence.md +++ b/wise-editor/doc/ImplementingPersistence.md @@ -30,6 +30,7 @@ function createStorageManager(mindplot) { }, loadMapDom : function(mapId) { + var xml; var xmlRequest = new Request({ url: this.backendUrl + mapId, method: 'get', diff --git a/wise-webapp/src/main/java/com/wisemapping/rest/MindmapController.java b/wise-webapp/src/main/java/com/wisemapping/rest/MindmapController.java index 441e9d1c..daa81038 100644 --- a/wise-webapp/src/main/java/com/wisemapping/rest/MindmapController.java +++ b/wise-webapp/src/main/java/com/wisemapping/rest/MindmapController.java @@ -173,6 +173,15 @@ public class MindmapController extends BaseController { return lockInfo.getTimestamp(); } + @RequestMapping(method = RequestMethod.GET, value = "/maps/{id}/document/xml",consumes = {"text/plain"}, produces = {"application/xml"}) + @ResponseBody + public String retrieveDocument(@PathVariable int id, @NotNull HttpServletResponse response) throws WiseMappingException, IOException { + response.setCharacterEncoding("UTF-8"); + final Mindmap mindmap = mindmapService.findMindmapById(id); + return mindmap.getXmlStr(); + } + + private void verifyLock(@NotNull Mindmap mindmap, @NotNull User user, long session, long timestamp) throws WiseMappingException { // The lock was lost, reclaim as the ownership of it. diff --git a/wise-webapp/src/main/webapp/jsp/mindmapEditor.jsp b/wise-webapp/src/main/webapp/jsp/mindmapEditor.jsp index ee4b6350..bae4744a 100644 --- a/wise-webapp/src/main/webapp/jsp/mindmapEditor.jsp +++ b/wise-webapp/src/main/webapp/jsp/mindmapEditor.jsp @@ -31,14 +31,13 @@ $(document).addEvent('loadcomplete', function (resource) { try { var mapId = '${mindmap.id}'; - var mapXml = '${mindmap.xmlAsJsLiteral}'; - // Configure designer options ... var options = loadDesignerOptions(); + options.persistenceManager = new mindplot.RESTPersistenceManager( { - saveUrl:"c/restful/maps/{id}/document", + documentUrl:"c/restful/maps/{id}/document", revertUrl:"c/restful/maps/{id}/history/latest", lockUrl:"c/restful/maps/{id}/lock", timestamp: ${lockTimestamp}, @@ -58,12 +57,11 @@ // Build designer ... var designer = buildDesigner(options); - // Load map from XML ... - var parser = new DOMParser(); - var domDocument = parser.parseFromString(mapXml, "text/xml"); - - var mindmap = mindplot.PersistenceManager.loadFromDom(mapId, domDocument); + // Load map from XML file persisted on disk... + var persistence = mindplot.PersistenceManager.getInstance(); + var mindmap = mindmap = persistence.load(mapId); designer.loadMap(mindmap); + } catch (e) { logStackTrace(e); throw e;