commit f19bcf070dc09918ec73698a952ba1651b974b26
Author: Tomáš Šimandl <tomas.simandl@gmail.com>
Date:   Thu Sep 27 13:19:25 2018 +0200

    Added possibility to upload raw JSON (front-end -- back-end JSON)

diff --git a/sources/src/main/java/cz/zcu/kiv/offscreen/servlets/UploadFiles.java b/sources/src/main/java/cz/zcu/kiv/offscreen/servlets/UploadFiles.java
index 40b9d14..49cd2bd 100644
--- a/sources/src/main/java/cz/zcu/kiv/offscreen/servlets/UploadFiles.java
+++ b/sources/src/main/java/cz/zcu/kiv/offscreen/servlets/UploadFiles.java
@@ -43,13 +43,18 @@ public class UploadFiles extends BaseServlet {
 
     @Override
     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
-        String jsonGraph = new FileLoader().loadFile(request);
+        Map<String, String> jsonData = new FileLoader().loadFile(request);
 
-        if (Strings.isNullOrEmpty(jsonGraph)) {
+        if(!jsonData.containsKey("file") || !jsonData.containsKey("jsonFileFormat")) {
+            request.setAttribute("errorMessage", "<strong>Invalid request!</strong><br/>");
+            doGet(request, response);
+
+        } else if (Strings.isNullOrEmpty(jsonData.get("file"))) {
             request.setAttribute("errorMessage", "<strong>Unsupported file</strong><br/>");
             doGet(request, response);
         } else {
-            request.getSession().setAttribute("json_graph", jsonGraph);
+            request.getSession().setAttribute("json_graph", jsonData.get("file"));
+            request.getSession().setAttribute("json_graph_type", jsonData.get("jsonFileFormat"));
             response.sendRedirect(getServletContext().getInitParameter("HOME_URL") + "graph");
         }
     }
diff --git a/sources/src/main/java/cz/zcu/kiv/offscreen/servlets/api/LoadGraphData.java b/sources/src/main/java/cz/zcu/kiv/offscreen/servlets/api/LoadGraphData.java
index c0d37a4..8cf3073 100644
--- a/sources/src/main/java/cz/zcu/kiv/offscreen/servlets/api/LoadGraphData.java
+++ b/sources/src/main/java/cz/zcu/kiv/offscreen/servlets/api/LoadGraphData.java
@@ -50,18 +50,23 @@ public class LoadGraphData extends BaseServlet {
      */
     private void getDiagramFromSession(HttpServletRequest request, HttpServletResponse response) throws IOException {
         String jsonToDisplay = (String) request.getSession().getAttribute("json_graph");
+        String jsonType = (String) request.getSession().getAttribute("json_graph_type");
 
-        if (!Strings.isNullOrEmpty(jsonToDisplay)) {
-            GraphManager graphManager = new GraphJSONDataLoader(jsonToDisplay).LoadData();
+        if (!Strings.isNullOrEmpty(jsonToDisplay) && jsonType != null) {
 
-            String configLocation = ConfigurationLoader.getConfigLocation(request.getServletContext());
-            JSONConfigLoader configLoader = new JSONConfigLoader(graphManager, configLocation);
+            String rawJson;
 
-            Graph graph = graphManager.createGraph(configLoader);
-            JSONObject json = JSONObject.fromObject(graph);
+            switch (jsonType) {
+                case "spade":
+                    String configLocation = ConfigurationLoader.getConfigLocation(request.getServletContext());
+                    rawJson = convertSpadeToRawJson(jsonToDisplay, configLocation);
+                    break;
+                default:
+                    rawJson = jsonToDisplay;
+            }
 
             response.setStatus(HttpServletResponse.SC_OK);
-            response.getWriter().write(json.toString());
+            response.getWriter().write(rawJson);
             response.getWriter().flush();
             return;
         }
@@ -69,6 +74,20 @@ public class LoadGraphData extends BaseServlet {
         response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
     }
 
+    /**
+     * Convert input spade JSON to frontend backend JSON and return it.
+     */
+    private String convertSpadeToRawJson(String spadeJson, String configLocation){
+        GraphManager graphManager = new GraphJSONDataLoader(spadeJson).LoadData();
+
+        JSONConfigLoader configLoader = new JSONConfigLoader(graphManager, configLocation);
+
+        Graph graph = graphManager.createGraph(configLoader);
+        JSONObject json = JSONObject.fromObject(graph);
+
+        return json.toString();
+    }
+
     /**
      * Add json of diagram which is taken from database to response or set http status code to UNAUTHORIZED.
      * Permissions of user to this diagram is checked.
diff --git a/sources/src/main/webapp/uploadFiles.jsp b/sources/src/main/webapp/uploadFiles.jsp
index 74ab1c4..6eefa70 100644
--- a/sources/src/main/webapp/uploadFiles.jsp
+++ b/sources/src/main/webapp/uploadFiles.jsp
@@ -41,6 +41,14 @@
 						<input type="file" name="file" id="file">
 					</div>
 
+					<div class="form-field">
+						<label for="jsonFileFormat">Select type of JSON:</label><br>
+						<select name="jsonFileFormat" id="jsonFileFormat">
+							<option value="spade">Spade JSON</option>
+							<option value="raw">Raw JSON</option>
+						</select>
+					</div>
+
 					<button type="submit">Start visualization</button>
 				</form>
 			</div>
