commit 2b29dfb90dcce0e12c352177eaae394996c096d8
Author: Tomáš Šimandl <tomas.simandl@gmail.com>
Date:   Fri Aug 24 11:24:31 2018 +0200

    joined load graph and start visualization steps on uploadFiles page. Removed saving uploaded file to file system.

diff --git a/sources/src/main/java/cz/zcu/kiv/offscreen/graph/loader/GraphJSONDataLoader.java b/sources/src/main/java/cz/zcu/kiv/offscreen/graph/loader/GraphJSONDataLoader.java
index ae7b5d9..ce7f691 100644
--- a/sources/src/main/java/cz/zcu/kiv/offscreen/graph/loader/GraphJSONDataLoader.java
+++ b/sources/src/main/java/cz/zcu/kiv/offscreen/graph/loader/GraphJSONDataLoader.java
@@ -1,5 +1,6 @@
 package cz.zcu.kiv.offscreen.graph.loader;
 
+import com.google.common.base.Strings;
 import cz.zcu.kiv.offscreen.graph.Attribute;
 import cz.zcu.kiv.offscreen.graph.EdgeArchetypeInfo;
 import cz.zcu.kiv.offscreen.graph.GraphManager;
@@ -39,6 +40,10 @@ public class GraphJSONDataLoader {
         this.file = file;
     }
 
+    public GraphJSONDataLoader(String json){
+        loadedJSON = json;
+    }
+
     private void loadJSON() throws IOException {
         loadedJSON = FileUtils.readFileToString(file, "UTF-8");
     }
@@ -46,7 +51,9 @@ public class GraphJSONDataLoader {
     public GraphManager LoadData() throws IOException {
         this.graphManager = new GraphManager();
 
-        loadJSON();
+        if(Strings.isNullOrEmpty(loadedJSON)) {
+            loadJSON();
+        }
         JSONObject json = JSONObject.fromObject(loadedJSON);
 
         JSONArray attributeTypes = getLoadedAttributeTypes(json);
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 d28b488..a6e1cb8 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
@@ -1,16 +1,16 @@
 package cz.zcu.kiv.offscreen.servlets;
 
-import cz.zcu.kiv.offscreen.graph.Graph;
+import com.google.common.base.Strings;
 import cz.zcu.kiv.offscreen.api.GraphExport;
+import cz.zcu.kiv.offscreen.graph.Graph;
 import cz.zcu.kiv.offscreen.graph.GraphManager;
 import cz.zcu.kiv.offscreen.graph.loader.DemoDiagramLoader;
 import cz.zcu.kiv.offscreen.graph.loader.GraphJSONDataLoader;
 import cz.zcu.kiv.offscreen.graph.loader.JSONConfigLoader;
 import cz.zcu.kiv.offscreen.loader.configuration.ConfigurationLoader;
-import cz.zcu.kiv.offscreen.session.SessionManager;
-import cz.zcu.kiv.offscreen.storage.FileManager;
 import net.sf.json.JSONObject;
 
+import javax.servlet.ServletException;
 import javax.servlet.http.HttpServlet;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
@@ -28,43 +28,30 @@ public class LoadGraphData extends HttpServlet {
      * graph is returned as JSON in response body.
      */
     @Override
-    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
-        // initialize file manager
-        String workingDirectory;
-        if (request.getParameter("diagram_hash") == null) {
-            workingDirectory = SessionManager.getSessionValue(request, "JSESSIONID");
-        } else {
-            workingDirectory = request.getParameter("diagram_hash");
-        }
-
-        if (request.getParameter("graphVersion") != null) {
-            workingDirectory += File.separator + request.getParameter("graphVersion");
-        } else {
-            workingDirectory += File.separator + SessionManager.getSessionValue(request, "graphVersion");
-        }
-
-        String storageLocation = ConfigurationLoader.getStorageLocation(request.getServletContext());
-
-        FileManager fileManager = new FileManager(workingDirectory, storageLocation);
+    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
 
         response.setContentType("application/json");
         response.setCharacterEncoding("UTF-8");
 
         if (request.getSession().getAttribute("demo_id") == null) {
-            File[] uploadedFiles = fileManager.getUploadedComponents().toArray(new File[0]);
-            File fileToDisplay = uploadedFiles[0];
 
-            GraphManager graphManager = new GraphJSONDataLoader(fileToDisplay).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);
+            String jsonToDisplay = (String)request.getSession().getAttribute("json_graph");
+            request.getSession().removeAttribute("json_graph");
 
-            String resultJsonString = json.toString();
+            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);
 
-            response.getWriter().write(resultJsonString);
+                String resultJsonString = json.toString();
 
+                response.getWriter().write(resultJsonString);
+            } else {
+                response.getWriter().write("");
+            }
         } else {
             String demoId = request.getSession().getAttribute("demo_id").toString();
             String path = "/WEB-INF" + File.separator + "demoDiagram" + File.separator + demoId + ".json";
diff --git a/sources/src/main/java/cz/zcu/kiv/offscreen/servlets/ShowGraph.java b/sources/src/main/java/cz/zcu/kiv/offscreen/servlets/ShowGraph.java
index 61dd50a..70ab77e 100644
--- a/sources/src/main/java/cz/zcu/kiv/offscreen/servlets/ShowGraph.java
+++ b/sources/src/main/java/cz/zcu/kiv/offscreen/servlets/ShowGraph.java
@@ -36,6 +36,8 @@ public class ShowGraph extends HttpServlet {
 		boolean showSaveButton = request.getParameter("diagram_id") != null && request.getParameter("diagram_hash") != null;
 		request.setAttribute("show_icon_save", showSaveButton);
 
+
+/*
 		// is it only a demo diagram?
 		if (request.getParameter("demo_id") != null) {
 			request.getSession().setAttribute("demo_id", request.getParameter("demo_id"));
@@ -140,7 +142,7 @@ public class ShowGraph extends HttpServlet {
 			}
 
 			request.getSession().setAttribute("id_diagram", request.getParameter("id_diagram"));
-        }
+        }*/
 
         // render
 		RequestDispatcher rd = getServletContext().getRequestDispatcher("/showGraph.jsp");
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 782acea..9c7451c 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
@@ -1,25 +1,20 @@
 package cz.zcu.kiv.offscreen.servlets;
 
-import java.io.File;
-import java.io.IOException;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
+import com.google.common.base.Strings;
+import cz.zcu.kiv.offscreen.storage.FileManager;
+import cz.zcu.kiv.offscreen.user.DB;
+import cz.zcu.kiv.offscreen.user.Diagram;
+import org.apache.commons.fileupload.servlet.ServletFileUpload;
+
 import javax.servlet.RequestDispatcher;
 import javax.servlet.ServletException;
-import javax.servlet.http.Cookie;
 import javax.servlet.http.HttpServlet;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
-
-import org.apache.commons.fileupload.servlet.ServletFileUpload;
-
-import cz.zcu.kiv.offscreen.loader.configuration.ConfigurationLoader;
-import cz.zcu.kiv.offscreen.session.SessionManager;
-import cz.zcu.kiv.offscreen.storage.FileManager;
-import cz.zcu.kiv.offscreen.user.DB;
-import cz.zcu.kiv.offscreen.user.Diagram;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
 
 /**
  *
@@ -34,66 +29,76 @@ public class UploadFiles extends HttpServlet {
 
     @Override
     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
-        DB db = new DB(getServletContext());
 
-        String diagram_id = request.getParameter("diagram_id");
-        if (diagram_id != null) {
-            Integer diagramId = Integer.parseInt(diagram_id);
-
-	        Diagram diagram_edit = new Diagram(db, diagramId);
-	        Map<String, String> diag_param = diagram_edit.getDiagramParam(diagramId);
-	        
-	        request.setAttribute("diagram_name", diag_param.get("name"));
-	        request.setAttribute("diagram_user_id", diag_param.get("user_id"));
-	        request.setAttribute("diagram_public_checked", (diag_param.get("public").compareTo("1") == 0 ? " checked=\"checked\" " : ""));
-        } else {
-        	request.setAttribute("diagram_name", "");
-        	request.setAttribute("diagram_public_checked", "");
-        	request.setAttribute("diagram_user_id", "-1");
+        if (ServletFileUpload.isMultipartContent(request)) {
+            String jsonGraph = new FileManager("","").loadFile(request);
+
+            if(Strings.isNullOrEmpty(jsonGraph)){
+                request.setAttribute("errorMessage", "<strong>Unsupported file</strong><br/>");
+            } else {
+                request.getSession().setAttribute("json_graph", jsonGraph);
+                response.sendRedirect("/graph");
+                return;
+            }
         }
 
-        // initialize file manager
-        String workingDirectory;
-        if (request.getParameter("diagram_hash") == null) {
-            workingDirectory = SessionManager.getSessionValue(request, "JSESSIONID");
-        } else {
-            workingDirectory = request.getParameter("diagram_hash");
-        }
+//        String diagram_id = request.getParameter("diagram_id");
+//        if (diagram_id != null) {
+//            Integer diagramId = Integer.parseInt(diagram_id);
+//
+//	        Diagram diagram_edit = new Diagram(db, diagramId);
+//	        Map<String, String> diag_param = diagram_edit.getDiagramParam(diagramId);
+//
+//	        request.setAttribute("diagram_name", diag_param.get("name"));
+//	        request.setAttribute("diagram_user_id", diag_param.get("user_id"));
+//	        request.setAttribute("diagram_public_checked", (diag_param.get("public").compareTo("1") == 0 ? " checked=\"checked\" " : ""));
+//        } else {
+//        	request.setAttribute("diagram_name", "");
+//        	request.setAttribute("diagram_public_checked", "");
+//        	request.setAttribute("diagram_user_id", "-1");
+//        }
 
-        int version = 1;
-        workingDirectory += File.separator + String.valueOf(version);
+        // initialize file manager
+//        String workingDirectory;
+//        if (request.getParameter("diagram_hash") == null) {
+//            workingDirectory = SessionManager.getSessionValue(request, "JSESSIONID");
+//        } else {
+//            workingDirectory = request.getParameter("diagram_hash");
+//        }
+//
+//        int version = 1;
+//        workingDirectory += File.separator + String.valueOf(version);
+//
+//        String storageLocation = ConfigurationLoader.getStorageLocation(request.getServletContext());
+
+//        FileManager fileManager = new FileManager(workingDirectory, storageLocation);
+//        if (!fileManager.isExistStorage()) {
+//            fileManager.createStorageDirectory();
+//        }
 
-        String storageLocation = ConfigurationLoader.getStorageLocation(request.getServletContext());
+        // save files from request
+//        String result;
+//        if (ServletFileUpload.isMultipartContent(request)) {
+//            result = fileManager.saveFile(request);
+//        } else {
+//            result = "";
+//        }
 
-        FileManager fileManager = new FileManager(workingDirectory, storageLocation);
-        if (!fileManager.isExistStorage()) {
-            fileManager.createStorageDirectory();
-        }
+//        response.addCookie(new Cookie("graphVersion", String.valueOf(version)));
 
-        // save files from request
-        String result;
-        if (ServletFileUpload.isMultipartContent(request)) {
-            result = fileManager.saveFile(request);
-        } else {
-            result = "";
-        }
 
-        response.addCookie(new Cookie("graphVersion", String.valueOf(version)));
+        DB db = new DB(getServletContext());
 
-        // response
         List<Map<String, String>> userDiagramList = new ArrayList<>();
         if (request.getSession().getAttribute("logged_user") == "1") {
             int loggedUserId = (int) request.getSession().getAttribute("logged_user_id");
 
-			userDiagramList = new Diagram(db).getDiagramListByUserId(loggedUserId);
+            userDiagramList = new Diagram(db).getDiagramListByUserId(loggedUserId);
         }
         request.setAttribute("diagramNames", userDiagramList);
 
         List<Map<String, String>> publicDiagramList = new Diagram(db).getDiagramPublicList();
         request.setAttribute("diagramPublic", publicDiagramList);
-        
-        request.setAttribute("componentNames", fileManager.getUploadedComponentsNames());
-        request.setAttribute("errorMessage", result);
 
         // render
         RequestDispatcher rd = getServletContext().getRequestDispatcher("/uploadFiles.jsp");
diff --git a/sources/src/main/java/cz/zcu/kiv/offscreen/storage/FileManager.java b/sources/src/main/java/cz/zcu/kiv/offscreen/storage/FileManager.java
index bae9042..7cdebde 100644
--- a/sources/src/main/java/cz/zcu/kiv/offscreen/storage/FileManager.java
+++ b/sources/src/main/java/cz/zcu/kiv/offscreen/storage/FileManager.java
@@ -47,6 +47,32 @@ public class FileManager {
         return componentName.endsWith("." + ACCEPTED_EXTENSION_FILE);
     }
 
+    public String loadFile(HttpServletRequest request){
+
+        DiskFileItemFactory factory = new DiskFileItemFactory();
+
+        ServletFileUpload upload = new ServletFileUpload(factory);
+        upload.setSizeMax(MAX_FILE_SIZE);
+
+        try {
+            // Parse the request to get file item.
+            List<FileItem> fileItems = upload.parseRequest(request);
+            FileItem fileItem = fileItems.get(0);
+
+            if (isAcceptedExtension(fileItem.getName()) && fileItem.getSize() > 0) {
+                logger.debug(fileItem.getName() + " - " + fileItem.getContentType());
+                return fileItem.getString("UTF-8");
+
+            } else {
+                return "";
+            }
+
+        } catch (Exception ex) {
+            ex.printStackTrace();
+        }
+        return "";
+    }
+
     public String saveFile(HttpServletRequest request) {
         logger.trace("ENTRY");
 
diff --git a/sources/src/main/webapp/uploadFiles.jsp b/sources/src/main/webapp/uploadFiles.jsp
index a630158..c16c0fc 100644
--- a/sources/src/main/webapp/uploadFiles.jsp
+++ b/sources/src/main/webapp/uploadFiles.jsp
@@ -35,17 +35,19 @@
 						<p class="errorMessage">${errorMessage}</p>
 					</c:if>
 
-					<h3>Upload SPADe data:</h5>
+					<h5>Upload SPADe data:</h5>
 
-					<form name="uploadForm" method="post" enctype="multipart/form-data">
+					<form action="/" method="post" enctype="multipart/form-data">
 						<div class="form-field">
 							<input type="file" name="file">
 						</div>
 
-						<input type="submit" value="Upload">
+						<hr class="verticalSeparator">
+
+						<input type="submit" value="Start visualization">
 					</form>
 
-					<c:if test="${not empty componentNames}">
+					<%--<c:if test="${not empty componentNames}">
 						<hr class="verticalSeparator">
 
 						<h3>Uploaded components:</h3>
@@ -62,21 +64,21 @@
 							}
 							%>
 							<c:forEach items="${componentNames}" var="componentName">
-								<li id="${componentName}">${componentName}                                
+								<li id="${componentName}">${componentName}
 								<% if (request.getParameter("diagram_id") == null || ( request.getSession().getAttribute("logged_user_id") != null &&
-										request.getAttribute("diagram_user_id") != null && 
+										request.getAttribute("diagram_user_id") != null &&
 										request.getAttribute("diagram_user_id").toString().compareTo(request.getSession().getAttribute("logged_user_id").toString()) == 0  )) { %>
 									<a href="delete-component?name=${componentName}<%= request.getAttribute("url_diagram_id") %>"><img src="images/button_cancel.png" alt="delete" class="imgDelete"/></a></li>
 								<% } %>
 							</c:forEach>
 						</ul>
 					</c:if>
-
 					<hr class="verticalSeparator">
 
 					<form name="diagramForm" action="graph" method="post">
-						<input type="submit" value="Start visualization" ${not empty componentNames ? "" : "disabled='disabled'"}>
+					<input type="submit" value="Start visualization" ${not empty componentNames ? "" : "disabled='disabled'"}>
 					</form>
+--%>
 				</div>
 			</main>
 		</div>
