Revize 2b29dfb9
Přidáno uživatelem Tomáš Šimandl před více než 6 roky(ů)
sources/src/main/java/cz/zcu/kiv/offscreen/graph/loader/GraphJSONDataLoader.java | ||
---|---|---|
1 | 1 |
package cz.zcu.kiv.offscreen.graph.loader; |
2 | 2 |
|
3 |
import com.google.common.base.Strings; |
|
3 | 4 |
import cz.zcu.kiv.offscreen.graph.Attribute; |
4 | 5 |
import cz.zcu.kiv.offscreen.graph.EdgeArchetypeInfo; |
5 | 6 |
import cz.zcu.kiv.offscreen.graph.GraphManager; |
... | ... | |
39 | 40 |
this.file = file; |
40 | 41 |
} |
41 | 42 |
|
43 |
public GraphJSONDataLoader(String json){ |
|
44 |
loadedJSON = json; |
|
45 |
} |
|
46 |
|
|
42 | 47 |
private void loadJSON() throws IOException { |
43 | 48 |
loadedJSON = FileUtils.readFileToString(file, "UTF-8"); |
44 | 49 |
} |
... | ... | |
46 | 51 |
public GraphManager LoadData() throws IOException { |
47 | 52 |
this.graphManager = new GraphManager(); |
48 | 53 |
|
49 |
loadJSON(); |
|
54 |
if(Strings.isNullOrEmpty(loadedJSON)) { |
|
55 |
loadJSON(); |
|
56 |
} |
|
50 | 57 |
JSONObject json = JSONObject.fromObject(loadedJSON); |
51 | 58 |
|
52 | 59 |
JSONArray attributeTypes = getLoadedAttributeTypes(json); |
sources/src/main/java/cz/zcu/kiv/offscreen/servlets/LoadGraphData.java | ||
---|---|---|
1 | 1 |
package cz.zcu.kiv.offscreen.servlets; |
2 | 2 |
|
3 |
import cz.zcu.kiv.offscreen.graph.Graph;
|
|
3 |
import com.google.common.base.Strings;
|
|
4 | 4 |
import cz.zcu.kiv.offscreen.api.GraphExport; |
5 |
import cz.zcu.kiv.offscreen.graph.Graph; |
|
5 | 6 |
import cz.zcu.kiv.offscreen.graph.GraphManager; |
6 | 7 |
import cz.zcu.kiv.offscreen.graph.loader.DemoDiagramLoader; |
7 | 8 |
import cz.zcu.kiv.offscreen.graph.loader.GraphJSONDataLoader; |
8 | 9 |
import cz.zcu.kiv.offscreen.graph.loader.JSONConfigLoader; |
9 | 10 |
import cz.zcu.kiv.offscreen.loader.configuration.ConfigurationLoader; |
10 |
import cz.zcu.kiv.offscreen.session.SessionManager; |
|
11 |
import cz.zcu.kiv.offscreen.storage.FileManager; |
|
12 | 11 |
import net.sf.json.JSONObject; |
13 | 12 |
|
13 |
import javax.servlet.ServletException; |
|
14 | 14 |
import javax.servlet.http.HttpServlet; |
15 | 15 |
import javax.servlet.http.HttpServletRequest; |
16 | 16 |
import javax.servlet.http.HttpServletResponse; |
... | ... | |
28 | 28 |
* graph is returned as JSON in response body. |
29 | 29 |
*/ |
30 | 30 |
@Override |
31 |
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { |
|
32 |
// initialize file manager |
|
33 |
String workingDirectory; |
|
34 |
if (request.getParameter("diagram_hash") == null) { |
|
35 |
workingDirectory = SessionManager.getSessionValue(request, "JSESSIONID"); |
|
36 |
} else { |
|
37 |
workingDirectory = request.getParameter("diagram_hash"); |
|
38 |
} |
|
39 |
|
|
40 |
if (request.getParameter("graphVersion") != null) { |
|
41 |
workingDirectory += File.separator + request.getParameter("graphVersion"); |
|
42 |
} else { |
|
43 |
workingDirectory += File.separator + SessionManager.getSessionValue(request, "graphVersion"); |
|
44 |
} |
|
45 |
|
|
46 |
String storageLocation = ConfigurationLoader.getStorageLocation(request.getServletContext()); |
|
47 |
|
|
48 |
FileManager fileManager = new FileManager(workingDirectory, storageLocation); |
|
31 |
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { |
|
49 | 32 |
|
50 | 33 |
response.setContentType("application/json"); |
51 | 34 |
response.setCharacterEncoding("UTF-8"); |
52 | 35 |
|
53 | 36 |
if (request.getSession().getAttribute("demo_id") == null) { |
54 |
File[] uploadedFiles = fileManager.getUploadedComponents().toArray(new File[0]); |
|
55 |
File fileToDisplay = uploadedFiles[0]; |
|
56 | 37 |
|
57 |
GraphManager graphManager = new GraphJSONDataLoader(fileToDisplay).LoadData(); |
|
58 |
String configLocation = ConfigurationLoader.getConfigLocation(request.getServletContext()); |
|
59 |
JSONConfigLoader configLoader = new JSONConfigLoader(graphManager, configLocation); |
|
60 |
Graph graph = graphManager.createGraph(configLoader); |
|
61 |
GraphExport export = new GraphExport(graph); |
|
62 |
JSONObject json = JSONObject.fromObject(export); |
|
38 |
String jsonToDisplay = (String)request.getSession().getAttribute("json_graph"); |
|
39 |
request.getSession().removeAttribute("json_graph"); |
|
63 | 40 |
|
64 |
String resultJsonString = json.toString(); |
|
41 |
if (!Strings.isNullOrEmpty(jsonToDisplay)) { |
|
42 |
GraphManager graphManager = new GraphJSONDataLoader(jsonToDisplay).LoadData(); |
|
43 |
String configLocation = ConfigurationLoader.getConfigLocation(request.getServletContext()); |
|
44 |
JSONConfigLoader configLoader = new JSONConfigLoader(graphManager, configLocation); |
|
45 |
Graph graph = graphManager.createGraph(configLoader); |
|
46 |
GraphExport export = new GraphExport(graph); |
|
47 |
JSONObject json = JSONObject.fromObject(export); |
|
65 | 48 |
|
66 |
response.getWriter().write(resultJsonString);
|
|
49 |
String resultJsonString = json.toString();
|
|
67 | 50 |
|
51 |
response.getWriter().write(resultJsonString); |
|
52 |
} else { |
|
53 |
response.getWriter().write(""); |
|
54 |
} |
|
68 | 55 |
} else { |
69 | 56 |
String demoId = request.getSession().getAttribute("demo_id").toString(); |
70 | 57 |
String path = "/WEB-INF" + File.separator + "demoDiagram" + File.separator + demoId + ".json"; |
sources/src/main/java/cz/zcu/kiv/offscreen/servlets/ShowGraph.java | ||
---|---|---|
36 | 36 |
boolean showSaveButton = request.getParameter("diagram_id") != null && request.getParameter("diagram_hash") != null; |
37 | 37 |
request.setAttribute("show_icon_save", showSaveButton); |
38 | 38 |
|
39 |
|
|
40 |
/* |
|
39 | 41 |
// is it only a demo diagram? |
40 | 42 |
if (request.getParameter("demo_id") != null) { |
41 | 43 |
request.getSession().setAttribute("demo_id", request.getParameter("demo_id")); |
... | ... | |
140 | 142 |
} |
141 | 143 |
|
142 | 144 |
request.getSession().setAttribute("id_diagram", request.getParameter("id_diagram")); |
143 |
} |
|
145 |
}*/
|
|
144 | 146 |
|
145 | 147 |
// render |
146 | 148 |
RequestDispatcher rd = getServletContext().getRequestDispatcher("/showGraph.jsp"); |
sources/src/main/java/cz/zcu/kiv/offscreen/servlets/UploadFiles.java | ||
---|---|---|
1 | 1 |
package cz.zcu.kiv.offscreen.servlets; |
2 | 2 |
|
3 |
import java.io.File;
|
|
4 |
import java.io.IOException;
|
|
5 |
import java.util.ArrayList;
|
|
6 |
import java.util.HashMap;
|
|
7 |
import java.util.List;
|
|
8 |
import java.util.Map; |
|
3 |
import com.google.common.base.Strings;
|
|
4 |
import cz.zcu.kiv.offscreen.storage.FileManager;
|
|
5 |
import cz.zcu.kiv.offscreen.user.DB;
|
|
6 |
import cz.zcu.kiv.offscreen.user.Diagram;
|
|
7 |
import org.apache.commons.fileupload.servlet.ServletFileUpload;
|
|
8 |
|
|
9 | 9 |
import javax.servlet.RequestDispatcher; |
10 | 10 |
import javax.servlet.ServletException; |
11 |
import javax.servlet.http.Cookie; |
|
12 | 11 |
import javax.servlet.http.HttpServlet; |
13 | 12 |
import javax.servlet.http.HttpServletRequest; |
14 | 13 |
import javax.servlet.http.HttpServletResponse; |
15 |
|
|
16 |
import org.apache.commons.fileupload.servlet.ServletFileUpload; |
|
17 |
|
|
18 |
import cz.zcu.kiv.offscreen.loader.configuration.ConfigurationLoader; |
|
19 |
import cz.zcu.kiv.offscreen.session.SessionManager; |
|
20 |
import cz.zcu.kiv.offscreen.storage.FileManager; |
|
21 |
import cz.zcu.kiv.offscreen.user.DB; |
|
22 |
import cz.zcu.kiv.offscreen.user.Diagram; |
|
14 |
import java.io.IOException; |
|
15 |
import java.util.ArrayList; |
|
16 |
import java.util.List; |
|
17 |
import java.util.Map; |
|
23 | 18 |
|
24 | 19 |
/** |
25 | 20 |
* |
... | ... | |
34 | 29 |
|
35 | 30 |
@Override |
36 | 31 |
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { |
37 |
DB db = new DB(getServletContext()); |
|
38 | 32 |
|
39 |
String diagram_id = request.getParameter("diagram_id"); |
|
40 |
if (diagram_id != null) { |
|
41 |
Integer diagramId = Integer.parseInt(diagram_id); |
|
42 |
|
|
43 |
Diagram diagram_edit = new Diagram(db, diagramId); |
|
44 |
Map<String, String> diag_param = diagram_edit.getDiagramParam(diagramId); |
|
45 |
|
|
46 |
request.setAttribute("diagram_name", diag_param.get("name")); |
|
47 |
request.setAttribute("diagram_user_id", diag_param.get("user_id")); |
|
48 |
request.setAttribute("diagram_public_checked", (diag_param.get("public").compareTo("1") == 0 ? " checked=\"checked\" " : "")); |
|
49 |
} else { |
|
50 |
request.setAttribute("diagram_name", ""); |
|
51 |
request.setAttribute("diagram_public_checked", ""); |
|
52 |
request.setAttribute("diagram_user_id", "-1"); |
|
33 |
if (ServletFileUpload.isMultipartContent(request)) { |
|
34 |
String jsonGraph = new FileManager("","").loadFile(request); |
|
35 |
|
|
36 |
if(Strings.isNullOrEmpty(jsonGraph)){ |
|
37 |
request.setAttribute("errorMessage", "<strong>Unsupported file</strong><br/>"); |
|
38 |
} else { |
|
39 |
request.getSession().setAttribute("json_graph", jsonGraph); |
|
40 |
response.sendRedirect("/graph"); |
|
41 |
return; |
|
42 |
} |
|
53 | 43 |
} |
54 | 44 |
|
55 |
// initialize file manager |
|
56 |
String workingDirectory; |
|
57 |
if (request.getParameter("diagram_hash") == null) { |
|
58 |
workingDirectory = SessionManager.getSessionValue(request, "JSESSIONID"); |
|
59 |
} else { |
|
60 |
workingDirectory = request.getParameter("diagram_hash"); |
|
61 |
} |
|
45 |
// String diagram_id = request.getParameter("diagram_id"); |
|
46 |
// if (diagram_id != null) { |
|
47 |
// Integer diagramId = Integer.parseInt(diagram_id); |
|
48 |
// |
|
49 |
// Diagram diagram_edit = new Diagram(db, diagramId); |
|
50 |
// Map<String, String> diag_param = diagram_edit.getDiagramParam(diagramId); |
|
51 |
// |
|
52 |
// request.setAttribute("diagram_name", diag_param.get("name")); |
|
53 |
// request.setAttribute("diagram_user_id", diag_param.get("user_id")); |
|
54 |
// request.setAttribute("diagram_public_checked", (diag_param.get("public").compareTo("1") == 0 ? " checked=\"checked\" " : "")); |
|
55 |
// } else { |
|
56 |
// request.setAttribute("diagram_name", ""); |
|
57 |
// request.setAttribute("diagram_public_checked", ""); |
|
58 |
// request.setAttribute("diagram_user_id", "-1"); |
|
59 |
// } |
|
62 | 60 |
|
63 |
int version = 1; |
|
64 |
workingDirectory += File.separator + String.valueOf(version); |
|
61 |
// initialize file manager |
|
62 |
// String workingDirectory; |
|
63 |
// if (request.getParameter("diagram_hash") == null) { |
|
64 |
// workingDirectory = SessionManager.getSessionValue(request, "JSESSIONID"); |
|
65 |
// } else { |
|
66 |
// workingDirectory = request.getParameter("diagram_hash"); |
|
67 |
// } |
|
68 |
// |
|
69 |
// int version = 1; |
|
70 |
// workingDirectory += File.separator + String.valueOf(version); |
|
71 |
// |
|
72 |
// String storageLocation = ConfigurationLoader.getStorageLocation(request.getServletContext()); |
|
73 |
|
|
74 |
// FileManager fileManager = new FileManager(workingDirectory, storageLocation); |
|
75 |
// if (!fileManager.isExistStorage()) { |
|
76 |
// fileManager.createStorageDirectory(); |
|
77 |
// } |
|
65 | 78 |
|
66 |
String storageLocation = ConfigurationLoader.getStorageLocation(request.getServletContext()); |
|
79 |
// save files from request |
|
80 |
// String result; |
|
81 |
// if (ServletFileUpload.isMultipartContent(request)) { |
|
82 |
// result = fileManager.saveFile(request); |
|
83 |
// } else { |
|
84 |
// result = ""; |
|
85 |
// } |
|
67 | 86 |
|
68 |
FileManager fileManager = new FileManager(workingDirectory, storageLocation); |
|
69 |
if (!fileManager.isExistStorage()) { |
|
70 |
fileManager.createStorageDirectory(); |
|
71 |
} |
|
87 |
// response.addCookie(new Cookie("graphVersion", String.valueOf(version))); |
|
72 | 88 |
|
73 |
// save files from request |
|
74 |
String result; |
|
75 |
if (ServletFileUpload.isMultipartContent(request)) { |
|
76 |
result = fileManager.saveFile(request); |
|
77 |
} else { |
|
78 |
result = ""; |
|
79 |
} |
|
80 | 89 |
|
81 |
response.addCookie(new Cookie("graphVersion", String.valueOf(version)));
|
|
90 |
DB db = new DB(getServletContext());
|
|
82 | 91 |
|
83 |
// response |
|
84 | 92 |
List<Map<String, String>> userDiagramList = new ArrayList<>(); |
85 | 93 |
if (request.getSession().getAttribute("logged_user") == "1") { |
86 | 94 |
int loggedUserId = (int) request.getSession().getAttribute("logged_user_id"); |
87 | 95 |
|
88 |
userDiagramList = new Diagram(db).getDiagramListByUserId(loggedUserId);
|
|
96 |
userDiagramList = new Diagram(db).getDiagramListByUserId(loggedUserId);
|
|
89 | 97 |
} |
90 | 98 |
request.setAttribute("diagramNames", userDiagramList); |
91 | 99 |
|
92 | 100 |
List<Map<String, String>> publicDiagramList = new Diagram(db).getDiagramPublicList(); |
93 | 101 |
request.setAttribute("diagramPublic", publicDiagramList); |
94 |
|
|
95 |
request.setAttribute("componentNames", fileManager.getUploadedComponentsNames()); |
|
96 |
request.setAttribute("errorMessage", result); |
|
97 | 102 |
|
98 | 103 |
// render |
99 | 104 |
RequestDispatcher rd = getServletContext().getRequestDispatcher("/uploadFiles.jsp"); |
sources/src/main/java/cz/zcu/kiv/offscreen/storage/FileManager.java | ||
---|---|---|
47 | 47 |
return componentName.endsWith("." + ACCEPTED_EXTENSION_FILE); |
48 | 48 |
} |
49 | 49 |
|
50 |
public String loadFile(HttpServletRequest request){ |
|
51 |
|
|
52 |
DiskFileItemFactory factory = new DiskFileItemFactory(); |
|
53 |
|
|
54 |
ServletFileUpload upload = new ServletFileUpload(factory); |
|
55 |
upload.setSizeMax(MAX_FILE_SIZE); |
|
56 |
|
|
57 |
try { |
|
58 |
// Parse the request to get file item. |
|
59 |
List<FileItem> fileItems = upload.parseRequest(request); |
|
60 |
FileItem fileItem = fileItems.get(0); |
|
61 |
|
|
62 |
if (isAcceptedExtension(fileItem.getName()) && fileItem.getSize() > 0) { |
|
63 |
logger.debug(fileItem.getName() + " - " + fileItem.getContentType()); |
|
64 |
return fileItem.getString("UTF-8"); |
|
65 |
|
|
66 |
} else { |
|
67 |
return ""; |
|
68 |
} |
|
69 |
|
|
70 |
} catch (Exception ex) { |
|
71 |
ex.printStackTrace(); |
|
72 |
} |
|
73 |
return ""; |
|
74 |
} |
|
75 |
|
|
50 | 76 |
public String saveFile(HttpServletRequest request) { |
51 | 77 |
logger.trace("ENTRY"); |
52 | 78 |
|
sources/src/main/webapp/uploadFiles.jsp | ||
---|---|---|
35 | 35 |
<p class="errorMessage">${errorMessage}</p> |
36 | 36 |
</c:if> |
37 | 37 |
|
38 |
<h3>Upload SPADe data:</h5>
|
|
38 |
<h5>Upload SPADe data:</h5>
|
|
39 | 39 |
|
40 |
<form name="uploadForm" method="post" enctype="multipart/form-data">
|
|
40 |
<form action="/" method="post" enctype="multipart/form-data">
|
|
41 | 41 |
<div class="form-field"> |
42 | 42 |
<input type="file" name="file"> |
43 | 43 |
</div> |
44 | 44 |
|
45 |
<input type="submit" value="Upload"> |
|
45 |
<hr class="verticalSeparator"> |
|
46 |
|
|
47 |
<input type="submit" value="Start visualization"> |
|
46 | 48 |
</form> |
47 | 49 |
|
48 |
<c:if test="${not empty componentNames}"> |
|
50 |
<%--<c:if test="${not empty componentNames}">
|
|
49 | 51 |
<hr class="verticalSeparator"> |
50 | 52 |
|
51 | 53 |
<h3>Uploaded components:</h3> |
... | ... | |
62 | 64 |
} |
63 | 65 |
%> |
64 | 66 |
<c:forEach items="${componentNames}" var="componentName"> |
65 |
<li id="${componentName}">${componentName}
|
|
67 |
<li id="${componentName}">${componentName} |
|
66 | 68 |
<% if (request.getParameter("diagram_id") == null || ( request.getSession().getAttribute("logged_user_id") != null && |
67 |
request.getAttribute("diagram_user_id") != null &&
|
|
69 |
request.getAttribute("diagram_user_id") != null && |
|
68 | 70 |
request.getAttribute("diagram_user_id").toString().compareTo(request.getSession().getAttribute("logged_user_id").toString()) == 0 )) { %> |
69 | 71 |
<a href="delete-component?name=${componentName}<%= request.getAttribute("url_diagram_id") %>"><img src="images/button_cancel.png" alt="delete" class="imgDelete"/></a></li> |
70 | 72 |
<% } %> |
71 | 73 |
</c:forEach> |
72 | 74 |
</ul> |
73 | 75 |
</c:if> |
74 |
|
|
75 | 76 |
<hr class="verticalSeparator"> |
76 | 77 |
|
77 | 78 |
<form name="diagramForm" action="graph" method="post"> |
78 |
<input type="submit" value="Start visualization" ${not empty componentNames ? "" : "disabled='disabled'"}>
|
|
79 |
<input type="submit" value="Start visualization" ${not empty componentNames ? "" : "disabled='disabled'"}> |
|
79 | 80 |
</form> |
81 |
--%> |
|
80 | 82 |
</div> |
81 | 83 |
</main> |
82 | 84 |
</div> |
Také k dispozici: Unified diff
joined load graph and start visualization steps on uploadFiles page. Removed saving uploaded file to file system.