commit 0e3d84a4f4b7f7c81fb7331317699ad79f3250cd
Author: Tomáš Šimandl <tomas.simandl@gmail.com>
Date:   Mon Aug 27 12:30:31 2018 +0200

    LoadGraphData reformat

diff --git a/sources/src/main/java/cz/zcu/kiv/offscreen/servlets/LoadGraphData.java b/sources/src/main/java/cz/zcu/kiv/offscreen/servlets/LoadGraphData.java
index 8a78bc9..dcaefd0 100644
--- a/sources/src/main/java/cz/zcu/kiv/offscreen/servlets/LoadGraphData.java
+++ b/sources/src/main/java/cz/zcu/kiv/offscreen/servlets/LoadGraphData.java
@@ -40,54 +40,60 @@ public class LoadGraphData extends HttpServlet {
             String diagram_id = request.getParameter("diagramId");
 
             if (diagram_id == null) {
-                String jsonToDisplay;
-                jsonToDisplay = (String)request.getSession().getAttribute("json_graph");
-                request.getSession().removeAttribute("json_graph");
+                response.getWriter().write(getDiagramFromSession(request));
+            } else {
+                response.getWriter().write(getDiagramById(request, Integer.parseInt(diagram_id)));
+            }
+            
+        } else {
+            response.getWriter().write(getDemoDiagram(request));
+        }
+    }
 
-                if (!Strings.isNullOrEmpty(jsonToDisplay)) {
-                    GraphManager graphManager = new GraphJSONDataLoader(jsonToDisplay).LoadData();
-                    String configLocation = ConfigurationLoader.getConfigLocation(request.getServletContext());
-                    JSONConfigLoader configLoader = new JSONConfigLoader(graphManager, configLocation);
-                    Graph graph = graphManager.createGraph(configLoader);
-                    GraphExport export = new GraphExport(graph);
-                    JSONObject json = JSONObject.fromObject(export);
+    private String getDiagramFromSession(HttpServletRequest request) throws IOException {
 
-                    String resultJsonString = json.toString();
+        String jsonToDisplay = (String)request.getSession().getAttribute("json_graph");
+        request.getSession().removeAttribute("json_graph");
 
-                    response.getWriter().write(resultJsonString);
-                } else {
-                    response.getWriter().write("");
-                }
+        if (!Strings.isNullOrEmpty(jsonToDisplay)) {
+            GraphManager graphManager = new GraphJSONDataLoader(jsonToDisplay).LoadData();
+            String configLocation = ConfigurationLoader.getConfigLocation(request.getServletContext());
+            JSONConfigLoader configLoader = new JSONConfigLoader(graphManager, configLocation);
+            Graph graph = graphManager.createGraph(configLoader);
+            GraphExport export = new GraphExport(graph);
+            JSONObject json = JSONObject.fromObject(export);
 
-            } else {
-                Integer diagramId = Integer.parseInt(diagram_id);
+            return json.toString();
+        }
+        return "";
+    }
 
-                DB db = new DB(getServletContext());
-                Diagram diagram = new Diagram(db, diagramId);
+    private String getDiagramById(HttpServletRequest request, int diagramId){
 
-                if(!diagram.isPublic()){
+        DB db = new DB(getServletContext());
+        Diagram diagram = new Diagram(db, diagramId);
 
-                    Integer loggedUserId = (Integer) request.getSession().getAttribute("logged_user_id");
-                    if(loggedUserId == null || diagram.getUserId() != loggedUserId) {
-                        response.getWriter().write("");
-                        return;
-                    }
-                }
+        if(!diagram.isPublic()){
+            // Diagram is not public
 
-                response.getWriter().write(diagram.getJsonDiagram());
+            Integer loggedUserId = (Integer) request.getSession().getAttribute("logged_user_id");
+            if(loggedUserId == null || diagram.getUserId() != loggedUserId) {
+                return ""; // User is not logged in or is not owner of diagram
             }
+        }
 
+        return diagram.getJsonDiagram();
+    }
 
-        } else {
-            String demoId = request.getSession().getAttribute("demo_id").toString();
-            String path = "/WEB-INF" + File.separator + "demoDiagram" + File.separator + demoId + ".json";
+    private String getDemoDiagram(HttpServletRequest request){
+        String demoId = request.getSession().getAttribute("demo_id").toString();
+        String path = "/WEB-INF" + File.separator + "demoDiagram" + File.separator + demoId + ".json";
 
-            DemoDiagramLoader loader = new DemoDiagramLoader();
-            InputStream in = this.getServletContext().getResourceAsStream(path);
+        DemoDiagramLoader loader = new DemoDiagramLoader();
+        InputStream in = this.getServletContext().getResourceAsStream(path);
 
-            response.getWriter().write(loader.readDemoJSONFromFile(in));
+        request.getSession().setAttribute("demo_id", null);
 
-            request.getSession().setAttribute("demo_id", null);
-        }
+        return loader.readDemoJSONFromFile(in);
     }
 }
