SaveController.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. package ui.controller;
  2. import classes.*;
  3. import com.google.gson.JsonObject;
  4. import com.google.gson.JsonPrimitive;
  5. import com.google.gson.reflect.TypeToken;
  6. import org.apache.commons.compress.archivers.ArchiveException;
  7. import org.apache.commons.compress.archivers.ArchiveOutputStream;
  8. import org.apache.commons.compress.archivers.ArchiveStreamFactory;
  9. import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
  10. import org.apache.commons.compress.utils.IOUtils;
  11. import ui.model.Model;
  12. import java.awt.geom.Point2D;
  13. import java.io.*;
  14. import java.util.*;
  15. import java.util.List;
  16. /**
  17. * Controller for Storage.
  18. *
  19. * @author Gruppe14
  20. */
  21. public class SaveController {
  22. private Model model;
  23. private int nCat, nObj, nEle, nEdge, nConn, nNodeEdge, nOldEdge, nUnitGraph, nStatsGraph;
  24. /**
  25. * Constructor.
  26. *
  27. * @param model the Model
  28. */
  29. SaveController(Model model) {
  30. this.model = model;
  31. }
  32. /**
  33. * Writes the current State of the Modelling into a JSON File which can be
  34. * loaded.
  35. *
  36. * @param path the Path
  37. * @throws IOException exception
  38. */
  39. void writeSave(String path) throws IOException, ArchiveException {
  40. File dst = new File(path);
  41. File holonFile = File.createTempFile("tmp", ".json");
  42. holonFile.deleteOnExit();
  43. dst.delete();
  44. OutputStream output = new FileOutputStream(dst);
  45. ArchiveOutputStream stream = new ArchiveStreamFactory().createArchiveOutputStream(ArchiveStreamFactory.ZIP,
  46. output);
  47. initNumeration();
  48. JsonObject file = new JsonObject();
  49. initialize(MODE.COMPLETE, file);
  50. storeCategory(file);
  51. storeCanvas(file);
  52. storeData(stream);
  53. FileWriter writer = new FileWriter(holonFile);
  54. writer.write(model.getGson().toJson(file));
  55. writer.flush();
  56. writer.close();
  57. addFileToSave(holonFile, stream, false);
  58. stream.finish();
  59. output.close();
  60. }
  61. /**
  62. * Writes the windows status (its position and dimensions)
  63. *
  64. * @param path the Path where to save it
  65. */
  66. void writeWindowStatus(String path, int x, int y, int width, int height)
  67. throws IOException, ArchiveException {
  68. JsonObject file = new JsonObject();
  69. initialize(MODE.SIZE, file);
  70. storeWindowPosAndSize(file, x, y, width, height);
  71. FileWriter writer = new FileWriter(path);
  72. writer.write(model.getGson().toJson(file));
  73. writer.flush();
  74. writer.close();
  75. }
  76. /**
  77. * Writes the Autosave File.
  78. *
  79. * @param path the Path
  80. * @throws IOException Exception
  81. */
  82. void writeAutosave(String path) throws IOException {
  83. initNumeration();
  84. JsonObject file = new JsonObject();
  85. initialize(MODE.PARTIAL, file);
  86. storeCanvas(file);
  87. FileWriter writer = new FileWriter(path);
  88. writer.write(model.getGson().toJson(file));
  89. writer.flush();
  90. writer.close();
  91. }
  92. /**
  93. * Writes the Category File in Case of Changes
  94. */
  95. void writeCategory(String path) throws IOException {
  96. initNumeration();
  97. JsonObject file = new JsonObject();
  98. initialize(MODE.CATEGORY, file);
  99. storeCategory(file);
  100. FileWriter writer = new FileWriter(path);
  101. writer.write(model.getGson().toJson(file));
  102. writer.flush();
  103. writer.close();
  104. }
  105. /**
  106. * Write needed default parameter into the JsonObject. Can be extended later
  107. * on
  108. */
  109. private void initialize(MODE mode, JsonObject file) {
  110. switch (mode) {
  111. case COMPLETE:
  112. file.add("MODE", new JsonPrimitive(mode.name()));
  113. file.add("IDCOUNTER", new JsonPrimitive(IdCounter.getCounter()));
  114. file.add("IDCOUNTERELEMENT", new JsonPrimitive(IdCounterElem.getCounter()));
  115. file.add("CANVAS_SIZE_X", new JsonPrimitive(model.getCanvasX()));
  116. file.add("CANVAS_SIZE_Y", new JsonPrimitive(model.getCanvasY()));
  117. break;
  118. case PARTIAL:
  119. file.add("MODE", new JsonPrimitive(mode.name()));
  120. file.add("IDCOUNTER", new JsonPrimitive(IdCounter.getCounter()));
  121. file.add("IDCOUNTERELEMENT", new JsonPrimitive(IdCounterElem.getCounter()));
  122. file.add("CANVAS_SIZE_X", new JsonPrimitive(model.getCanvasX()));
  123. file.add("CANVAS_SIZE_Y", new JsonPrimitive(model.getCanvasY()));
  124. break;
  125. case CATEGORY:
  126. file.add("MODE", new JsonPrimitive(mode.name()));
  127. break;
  128. case SIZE:
  129. file.add("MODE", new JsonPrimitive(mode.name()));
  130. default:
  131. break;
  132. }
  133. }
  134. /**
  135. * Store all Categories and Object into a Json File via Serialization
  136. */
  137. private void storeCategory(JsonObject file) {
  138. // forall categories store them into the jsontree
  139. for (Category cat : model.getCategories()) {
  140. String key = "CATEGORY" + getNumerator(NUMTYPE.CATEGORY);
  141. file.add(key, new JsonPrimitive(cat.getName()));
  142. // forall object in the category store them into the jsontree
  143. for (AbstractCanvasObject obj : cat.getObjects()) {
  144. file.add("CGOBJECT" + getNumerator(NUMTYPE.OBJECT),
  145. model.getGson().toJsonTree(obj, AbstractCanvasObject.class));
  146. // if its a holonobject add elements too
  147. if (obj instanceof HolonObject)
  148. elementsToJson(TYPE.CATEGORY, file, obj);
  149. }
  150. }
  151. }
  152. private void storeWindowPosAndSize(JsonObject file, int x, int y, int width, int height) {
  153. file.add("POS_X", new JsonPrimitive(x));
  154. file.add("POS_Y", new JsonPrimitive(y));
  155. file.add("WIDTH", new JsonPrimitive(width));
  156. file.add("HEIGHT", new JsonPrimitive(height));
  157. }
  158. /**
  159. * Travers through all Objects via BFS and Stores everything relevant
  160. */
  161. private void storeCanvas(JsonObject file) {
  162. ArrayDeque<AbstractCanvasObject> queue = new ArrayDeque<>();
  163. AbstractCanvasObject u = null;
  164. // put all objects into queue since there is not starting object
  165. for (AbstractCanvasObject cps : model.getObjectsOnCanvas()) {
  166. queue.add(cps);
  167. }
  168. // while quene not empty
  169. while (!queue.isEmpty()) {
  170. // u = current node
  171. u = queue.pop();
  172. // add currentnode into jsontree
  173. String key = "CVSOBJECT" + getNumerator(NUMTYPE.OBJECT);
  174. file.add(key, model.getGson().toJsonTree(u, AbstractCanvasObject.class));
  175. // and its connections too
  176. edgeToJson(EDGETYPE.CONNECTION, file, u.getId(), u.getConnections());
  177. // if holonobject elements too
  178. if (u instanceof HolonObject)
  179. elementsToJson(TYPE.CANVAS, file, u);
  180. // if switch graphpoints too
  181. if (u instanceof HolonSwitch)
  182. if (((HolonSwitch) u).getGraphPoints().size() != 0)
  183. unitgraphToJson(GRAPHTYPE.SWITCH, file, u.getId(), ((HolonSwitch) u).getGraphPoints());
  184. // if uppernode put all nodes inside the uppernode into queue
  185. if (u instanceof GroupNode) {
  186. for (AbstractCanvasObject adjacent : ((GroupNode) u).getNodes()) {
  187. queue.add(adjacent);
  188. }
  189. }
  190. }
  191. // lastly add canvasedges into json
  192. edgeToJson(EDGETYPE.CANVAS, file, 0, model.getEdgesOnCanvas());
  193. }
  194. /**
  195. * Save wanted Data
  196. */
  197. private void storeData(ArchiveOutputStream stream) throws IOException {
  198. File images = new File(System.getProperty("user.home") + "/.config/HolonGUI/Images");
  199. File background = new File(System.getProperty("user.home") + "/.config/HolonGUI/BackgroundImages");
  200. addFilesToSave(images, stream);
  201. addFilesToSave(background, stream);
  202. }
  203. /**
  204. * Stores Category or Canvas Elements into Json
  205. */
  206. void elementsToJson(TYPE type, JsonObject file, AbstractCanvasObject obj) {
  207. JsonObject temp = new JsonObject();
  208. String key = null;
  209. // forall elements store them into json and include the id of the object
  210. for (HolonElement ele : ((HolonObject) obj).getElements()) {
  211. temp.add("properties", model.getGson().toJsonTree(ele));
  212. temp.add("ID", new JsonPrimitive(obj.getId()));
  213. temp.add("FlexList", model.getGson().toJsonTree(ele.flexList, new TypeToken<List<Flexibility>>() {
  214. }.getType()));
  215. // switch for deciding the key
  216. switch (type) {
  217. case CANVAS:
  218. key = "CVSELEMENT" + getNumerator(NUMTYPE.ELEMENT);
  219. break;
  220. case CATEGORY:
  221. key = "CGELEMENT" + getNumerator(NUMTYPE.ELEMENT);
  222. break;
  223. default:
  224. break;
  225. }
  226. file.add(key, model.getGson().toJsonTree(temp));
  227. // if there are gps add them into
  228. if (!ele.getGraphPoints().isEmpty())
  229. unitgraphTESTToJson(file, ele.getId(), ele.getGraphPoints());
  230. temp = new JsonObject();
  231. }
  232. }
  233. /**
  234. * Put the UnitGraphs of Switches or Elements into Json
  235. */
  236. void unitgraphToJson(GRAPHTYPE type, JsonObject file, int id, LinkedList<Point2D.Double> graph) {
  237. JsonObject temp = new JsonObject();
  238. String key = null;
  239. // forall points add them
  240. for (int i = 0; i < graph.size(); i++) {
  241. temp.add("" + i, new JsonPrimitive(graph.get(i).x + ":" + graph.get(i).y));
  242. }
  243. // decide key
  244. switch (type) {
  245. case SWITCH:
  246. key = "SWUNITGRAPH" + getNumerator(NUMTYPE.UNITGRAPH);
  247. break;
  248. case ELEMENT:
  249. key = "ELEUNITGRAPH" + getNumerator(NUMTYPE.UNITGRAPH);
  250. break;
  251. case TESTELEMENT:
  252. key = "ELETESTUNITGRAPH" + getNumerator(NUMTYPE.UNITGRAPH);
  253. break;
  254. default:
  255. break;
  256. }
  257. // add id of element so it can be found again
  258. temp.add("ID", new JsonPrimitive(id));
  259. file.add(key, model.getGson().toJsonTree(temp));
  260. }
  261. /**
  262. * Put the UnitGraphs of Switches or Elements into Json
  263. */
  264. void unitgraphTESTToJson(JsonObject file, int id, LinkedList<Point2D.Double> graph) {
  265. JsonObject temp = new JsonObject();
  266. String key = null;
  267. // forall points add them
  268. for (int i = 0; i < graph.size(); i++) {
  269. temp.add("" + i, new JsonPrimitive(graph.get(i).x + ":" + graph.get(i).y));
  270. }
  271. key = "ELETESTUNITGRAPH" + getNumerator(NUMTYPE.UNITGRAPH);
  272. // add id of element so it can be found again
  273. temp.add("ID", new JsonPrimitive(id));
  274. file.add(key, model.getGson().toJsonTree(temp));
  275. }
  276. /**
  277. * Canvas-Edge, Connections, Node-Edge and Old-Edges to json
  278. */
  279. private void edgeToJson(EDGETYPE type, JsonObject file, int id, ArrayList<Edge> arr) {
  280. String k = null;
  281. boolean b = false;
  282. JsonObject temp = new JsonObject();
  283. for (Edge edge : arr) {
  284. // add properties and only the ids from a and b
  285. temp.add("properties", model.getGson().toJsonTree(edge));
  286. temp.add("A", new JsonPrimitive(edge.getA().getId()));
  287. temp.add("B", new JsonPrimitive(edge.getB().getId()));
  288. // Key and occasionally the id of Uppernode
  289. switch (type) {
  290. case CANVAS:
  291. k = "CVSEDGE" + getNumerator(NUMTYPE.EDGE);
  292. break;
  293. case CONNECTION:
  294. k = "CONNEDGE" + getNumerator(NUMTYPE.CONNECTION);
  295. break;
  296. case NODE:
  297. temp.add("ID", new JsonPrimitive(id));
  298. k = "NODEEDGE" + getNumerator(NUMTYPE.NODEEDGE);
  299. break;
  300. case OLD:
  301. temp.add("ID", new JsonPrimitive(id));
  302. k = "OLDEDGE" + getNumerator(NUMTYPE.OLDEDGE);
  303. break;
  304. default:
  305. break;
  306. }
  307. // lookup if the CVS, NODE or OLDEDGE are also connections
  308. if (edge.getA().getConnections().contains(edge) && edge.getA().getConnections().contains(edge)
  309. && !type.equals(EDGETYPE.CONNECTION))
  310. b = true;
  311. temp.add("connection", new JsonPrimitive(b));
  312. file.add(k, model.getGson().toJsonTree(temp));
  313. temp = new JsonObject();
  314. }
  315. }
  316. /**
  317. * Differs Between Single file or whole Directory to Store
  318. */
  319. private void addFilesToSave(File src, ArchiveOutputStream stream) throws IOException {
  320. if (!src.exists())
  321. return;
  322. ArrayList<File> files = new ArrayList<>();
  323. files.addAll(Arrays.asList(src.listFiles()));
  324. for (File file : files) {
  325. if (file.isDirectory())
  326. addFilesToSave(file, stream);
  327. else
  328. addFileToSave(file, stream, true);
  329. }
  330. }
  331. /**
  332. * Add a File into the Archive
  333. */
  334. private void addFileToSave(File src, ArchiveOutputStream stream, boolean dir) throws IOException {
  335. String entryName = (dir ? src.getCanonicalPath() : src.getName());
  336. entryName = checkOS(entryName);
  337. ZipArchiveEntry entry = new ZipArchiveEntry(entryName);
  338. stream.putArchiveEntry(entry);
  339. BufferedInputStream input = new BufferedInputStream(new FileInputStream(src));
  340. IOUtils.copy(input, stream);
  341. input.close();
  342. stream.closeArchiveEntry();
  343. }
  344. private String checkOS(String entryName) {
  345. String os = System.getProperty("os.name").toLowerCase();
  346. String ret = entryName;
  347. String partition = null;
  348. if (os.contains("windows")) {
  349. partition = ret.substring(0, ret.indexOf(":") + 1);
  350. ret = ret.replace(System.getProperty("user.home") + "\\.config\\HolonGUI\\", "");
  351. ret = ret.replace(partition, "");
  352. }
  353. if (os.contains("mac")) {
  354. // dosmth
  355. ret = ret.replace(System.getProperty("user.home") + "/.config/HolonGUI/", "");
  356. }
  357. if (os.contains("linux")) {
  358. // dosmth
  359. ret = ret.replace(System.getProperty("user.home") + "/.config/HolonGUI/", "");
  360. }
  361. if (os.contains("solaris")) {
  362. // dosmth
  363. }
  364. return ret;
  365. }
  366. /**
  367. * Just initialize the Numerators for the Json Keys. Maybe bad Style..
  368. */
  369. void initNumeration() {
  370. this.nCat = this.nObj = this.nEle = this.nEdge = this.nConn = this.nNodeEdge = this.nOldEdge = this.nStatsGraph = 0;
  371. }
  372. /**
  373. * Get the wanted numerator and increment it
  374. */
  375. int getNumerator(NUMTYPE type) {
  376. switch (type) {
  377. case CATEGORY:
  378. return nCat++;
  379. case OBJECT:
  380. return nObj++;
  381. case ELEMENT:
  382. return nEle++;
  383. case EDGE:
  384. return nEdge++;
  385. case CONNECTION:
  386. return nConn++;
  387. case NODEEDGE:
  388. return nNodeEdge++;
  389. case OLDEDGE:
  390. return nOldEdge++;
  391. case UNITGRAPH:
  392. return nUnitGraph++;
  393. case STATSGRAPH:
  394. return nStatsGraph++;
  395. default:
  396. break;
  397. }
  398. return -1;
  399. }
  400. public enum MODE {
  401. COMPLETE, PARTIAL, CATEGORY, SIZE,
  402. }
  403. public enum TYPE {
  404. CATEGORY, CANVAS
  405. }
  406. public enum EDGETYPE {
  407. CANVAS, CONNECTION, NODE, OLD, LAYER
  408. }
  409. public enum NUMTYPE {
  410. CATEGORY, OBJECT, ELEMENT, EDGE, CONNECTION, NODEEDGE, OLDEDGE, UNITGRAPH, STATSGRAPH
  411. }
  412. public enum GRAPHTYPE {
  413. SWITCH, ELEMENT, TESTELEMENT
  414. }
  415. }