StoreController.java 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. package ui.controller;
  2. import java.awt.Color;
  3. import java.awt.Point;
  4. import java.io.FileWriter;
  5. import java.io.IOException;
  6. import java.util.ArrayList;
  7. import java.util.LinkedList;
  8. import com.google.gson.Gson;
  9. import com.google.gson.GsonBuilder;
  10. import com.google.gson.JsonObject;
  11. import com.google.gson.JsonPrimitive;
  12. import TypeAdapter.AbstractCpsObjectAdapter;
  13. import TypeAdapter.ColorAdapter;
  14. import TypeAdapter.PositionAdapter;
  15. import classes.Category;
  16. import classes.CpsEdge;
  17. import classes.CpsUpperNode;
  18. import classes.AbstractCpsObject;
  19. import classes.HolonElement;
  20. import classes.HolonObject;
  21. import classes.HolonSwitch;
  22. import classes.IdCounter;
  23. import classes.Position;
  24. import ui.model.Model;
  25. /**
  26. * Controller for Storage.
  27. *
  28. * @author Gruppe14
  29. */
  30. public class StoreController {
  31. public enum MODE {
  32. COMPLETE, PARTIAL
  33. }
  34. public enum TYPE {
  35. CATEGORY, CANVAS
  36. }
  37. public enum EDGETYPE {
  38. CANVAS, CONNECTION, NODE, OLD
  39. }
  40. public enum NUMTYPE {
  41. CATEGORY, OBJECT, ELEMENT, EDGE, CONNECTION, NODEEDGE, OLDEDGE, UNITGRAPH
  42. }
  43. public enum GRAPHTYPE {
  44. SWITCH, ELEMENT
  45. }
  46. private Model model;
  47. private Gson gson;
  48. private int nCat, nObj, nEle, nEdge, nConn, nNodeEdge, nOldEdge, nUnitGraph;
  49. /**
  50. * Constructor.
  51. *
  52. * @param model
  53. * the Model
  54. */
  55. public StoreController(Model model) {
  56. this.model = model;
  57. initGson();
  58. }
  59. /**
  60. * Writes the current State of the Modelling into a JSON File which can be
  61. * loaded.
  62. *
  63. * @param path
  64. * the Path
  65. *
  66. * @throws IOException
  67. * exception
  68. */
  69. public void writeSaveFile(String path) throws IOException {
  70. initNumeration();
  71. JsonObject file = new JsonObject();
  72. initialize(MODE.COMPLETE, file);
  73. storeCategory(file);
  74. storeCanvas(file);
  75. FileWriter writer = new FileWriter(path);
  76. writer.write(gson.toJson(file));
  77. writer.flush();
  78. writer.close();
  79. }
  80. /**
  81. * Writes the Autosave File.
  82. *
  83. * @param path
  84. * the Path
  85. * @throws IOException
  86. * Exception
  87. */
  88. public void writeAutosaveFile(String path) throws IOException {
  89. initNumeration();
  90. JsonObject file = new JsonObject();
  91. initialize(MODE.PARTIAL, file);
  92. storeCanvas(file);
  93. FileWriter writer = new FileWriter(path);
  94. writer.write(gson.toJson(file));
  95. writer.flush();
  96. writer.close();
  97. }
  98. /**
  99. * Write needed default parameter into the JsonObject. Can be extended later
  100. * on
  101. *
  102. * @param mode
  103. * @param file
  104. */
  105. private void initialize(MODE mode, JsonObject file) {
  106. // TODO Auto-generated method stub
  107. switch (mode) {
  108. case COMPLETE:
  109. file.add("MODE", new JsonPrimitive(mode.name()));
  110. file.add("IDCOUNTER", new JsonPrimitive(IdCounter.getCounter()));
  111. file.add("CANVAS_SIZE_X", new JsonPrimitive(model.getCanvasX()));
  112. file.add("CANVAS_SIZE_Y", new JsonPrimitive(model.getCanvasY()));
  113. break;
  114. case PARTIAL:
  115. file.add("MODE", new JsonPrimitive(mode.name()));
  116. file.add("IDCOUNTER", new JsonPrimitive(IdCounter.getCounter()));
  117. break;
  118. default:
  119. break;
  120. }
  121. }
  122. /**
  123. * Store all Categories and Object into a Json File via Serialization
  124. *
  125. * @param file
  126. */
  127. private void storeCategory(JsonObject file) {
  128. // TODO Auto-generated method stub
  129. // forall categories store them into the jsontree
  130. for (Category cat : model.getCategories()) {
  131. String key = "CATEGORY" + getNumerator(NUMTYPE.CATEGORY);
  132. file.add(key, new JsonPrimitive(cat.getName()));
  133. // forall object in the category store them into the jsontree
  134. for (AbstractCpsObject obj : cat.getObjects()) {
  135. file.add("CGOBJECT" + getNumerator(NUMTYPE.OBJECT), gson.toJsonTree(obj, AbstractCpsObject.class));
  136. // if its a holonobject add elements too
  137. if (obj instanceof HolonObject)
  138. elementsToJson(TYPE.CATEGORY, file, obj);
  139. }
  140. }
  141. }
  142. /**
  143. * Travers through all Objects via BFS and Stores everything relevant
  144. *
  145. * @param file
  146. */
  147. private void storeCanvas(JsonObject file) {
  148. // TODO Auto-generated method stub
  149. LinkedList<AbstractCpsObject> queue = new LinkedList<>();
  150. AbstractCpsObject u = null;
  151. // put all objects into queue since there is not starting object
  152. for (AbstractCpsObject cps : model.getObjectsOnCanvas()) {
  153. queue.add(cps);
  154. }
  155. // while quene not empty
  156. while (!queue.isEmpty()) {
  157. // u = current node
  158. u = queue.poll();
  159. // add currentnode into jsontree
  160. String key = "CVSOBJECT" + getNumerator(NUMTYPE.OBJECT);
  161. file.add(key, gson.toJsonTree(u, AbstractCpsObject.class));
  162. // and its connections too
  163. edgeToJson(EDGETYPE.CONNECTION, file, u.getID(), u.getConnections());
  164. // if holonobject elements too
  165. if (u instanceof HolonObject)
  166. elementsToJson(TYPE.CANVAS, file, u);
  167. // if switch graphpoints too
  168. if (u instanceof HolonSwitch)
  169. if (((HolonSwitch) u).getGraphPoints().size() != 0)
  170. unitgraphToJson(GRAPHTYPE.SWITCH, file, u.getID(), ((HolonSwitch) u).getGraphPoints());
  171. // if uppernode put all nodes inside the uppernode into queue
  172. if (u instanceof CpsUpperNode) {
  173. for (AbstractCpsObject adjacent : ((CpsUpperNode) u).getNodes()) {
  174. queue.add(adjacent);
  175. }
  176. // dont forget to add the nodeedges and oldedges
  177. edgeToJson(EDGETYPE.NODE, file, u.getID(), ((CpsUpperNode) u).getNodeEdges());
  178. edgeToJson(EDGETYPE.OLD, file, u.getID(), ((CpsUpperNode) u).getOldEdges());
  179. }
  180. }
  181. // lastly add canvasedges into json
  182. edgeToJson(EDGETYPE.CANVAS, file, 0, model.getEdgesOnCanvas());
  183. }
  184. /**
  185. * Stores Category or Canvas Elements into Json
  186. *
  187. * @param type
  188. * @param gson
  189. * @param file
  190. * @param obj
  191. */
  192. private void elementsToJson(TYPE type, JsonObject file, AbstractCpsObject obj) {
  193. // TODO Auto-generated method stub
  194. JsonObject temp = new JsonObject();
  195. String key = null;
  196. // forall elements store them into json and include the id of the object
  197. for (HolonElement ele : ((HolonObject) obj).getElements()) {
  198. temp.add("properties", gson.toJsonTree(ele));
  199. temp.add("ID", new JsonPrimitive(obj.getID()));
  200. // switch for deciding the key
  201. switch (type) {
  202. case CANVAS:
  203. key = "CVSELEMENT" + getNumerator(NUMTYPE.ELEMENT);
  204. break;
  205. case CATEGORY:
  206. key = "CGELEMENT" + getNumerator(NUMTYPE.ELEMENT);
  207. break;
  208. default:
  209. break;
  210. }
  211. file.add(key, gson.toJsonTree(temp));
  212. // if there are gps add them into
  213. if (ele.getGraphPoints().size() != 0)
  214. unitgraphToJson(GRAPHTYPE.ELEMENT, file, ele.getId(), ele.getGraphPoints());
  215. temp = new JsonObject();
  216. }
  217. }
  218. /**
  219. * Put the UnitGraphs of Switches or Elements into Json
  220. *
  221. * @param ele
  222. */
  223. private void unitgraphToJson(GRAPHTYPE type, JsonObject file, int id, LinkedList<Point> graph) {
  224. JsonObject temp = new JsonObject();
  225. String key = null;
  226. // forall points add them
  227. for (int i = 0; i < graph.size(); i++) {
  228. temp.add("" + i, new JsonPrimitive(graph.get(i).x + ":" + graph.get(i).y));
  229. }
  230. // decide key
  231. switch (type) {
  232. case SWITCH:
  233. key = "SWUNITGRAPH" + getNumerator(NUMTYPE.UNITGRAPH);
  234. break;
  235. case ELEMENT:
  236. key = "ELEUNITGRAPH" + getNumerator(NUMTYPE.UNITGRAPH);
  237. break;
  238. default:
  239. break;
  240. }
  241. // add id of element so it can be found again
  242. temp.add("ID", new JsonPrimitive(id));
  243. file.add(key, gson.toJsonTree(temp));
  244. }
  245. /**
  246. * Canvas-Edge, Connections, Node-Edge and Old-Edges to json
  247. *
  248. * @param type
  249. * @param file
  250. * @param id
  251. * @param arr
  252. */
  253. private void edgeToJson(EDGETYPE type, JsonObject file, int id, ArrayList<CpsEdge> arr) {
  254. // TODO Auto-generated method stub
  255. String k = null;
  256. boolean b = false;
  257. JsonObject temp = new JsonObject();
  258. for (CpsEdge edge : arr) {
  259. // add properties and only the ids from a and b
  260. temp.add("properties", gson.toJsonTree(edge));
  261. temp.add("A", new JsonPrimitive(edge.getA().getID()));
  262. temp.add("B", new JsonPrimitive(edge.getB().getID()));
  263. // Key and occasionally the id of Uppernode
  264. switch (type) {
  265. case CANVAS:
  266. k = "CVSEDGE" + getNumerator(NUMTYPE.EDGE);
  267. break;
  268. case CONNECTION:
  269. k = "CONNEDGE" + getNumerator(NUMTYPE.CONNECTION);
  270. break;
  271. case NODE:
  272. temp.add("ID", new JsonPrimitive(id));
  273. k = "NODEEDGE" + getNumerator(NUMTYPE.NODEEDGE);
  274. break;
  275. case OLD:
  276. temp.add("ID", new JsonPrimitive(id));
  277. k = "OLDEDGE" + getNumerator(NUMTYPE.OLDEDGE);
  278. break;
  279. default:
  280. break;
  281. }
  282. // lookup if the CVS, NODE or OLDEDGE are also connections
  283. if (edge.getA().getConnections().contains(edge) && edge.getA().getConnections().contains(edge)
  284. && !type.equals(EDGETYPE.CANVAS))
  285. b = true;
  286. temp.add("connection", new JsonPrimitive(b));
  287. file.add(k, gson.toJsonTree(temp));
  288. temp = new JsonObject();
  289. }
  290. }
  291. /**
  292. * Initialize the Gson with wanted parameters
  293. */
  294. private void initGson() {
  295. // TODO Auto-generated method stub
  296. GsonBuilder builder = new GsonBuilder();
  297. builder.excludeFieldsWithoutExposeAnnotation().serializeNulls().setPrettyPrinting();
  298. builder.registerTypeAdapter(AbstractCpsObject.class, new AbstractCpsObjectAdapter());
  299. builder.registerTypeAdapter(Position.class, new PositionAdapter());
  300. builder.registerTypeAdapter(Color.class, new ColorAdapter());
  301. // use the builder and make a instance of the Gson
  302. this.gson = builder.create();
  303. }
  304. /**
  305. * Just initialize the Numerators for the Json Keys. Maybe bad Style..
  306. */
  307. private void initNumeration() {
  308. this.nCat = this.nObj = this.nEle = this.nEdge = this.nConn = this.nNodeEdge = this.nOldEdge = 0;
  309. }
  310. /**
  311. * Get the wanted numerator and increment it
  312. *
  313. * @param type
  314. * @return
  315. */
  316. private int getNumerator(NUMTYPE type) {
  317. switch (type) {
  318. case CATEGORY:
  319. return nCat++;
  320. case OBJECT:
  321. return nObj++;
  322. case ELEMENT:
  323. return nEle++;
  324. case EDGE:
  325. return nEdge++;
  326. case CONNECTION:
  327. return nConn++;
  328. case NODEEDGE:
  329. return nNodeEdge++;
  330. case OLDEDGE:
  331. return nOldEdge++;
  332. case UNITGRAPH:
  333. return nUnitGraph++;
  334. default:
  335. break;
  336. }
  337. return -1;
  338. }
  339. }