SaveController.java 15 KB

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