StoreController.java 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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 sun.misc.Queue;
  25. import ui.model.Model;
  26. /**
  27. * Controller for Storage.
  28. *
  29. * @author Gruppe14
  30. */
  31. public class StoreController {
  32. public enum MODE {
  33. COMPLETE, PARTIAL
  34. }
  35. public enum TYPE {
  36. CATEGORY, CANVAS
  37. }
  38. public enum EDGETYPE {
  39. CANVAS, CONNECTION, NODE, OLD
  40. }
  41. public enum NUMTYPE {
  42. CATEGORY, OBJECT, ELEMENT, EDGE, CONNECTION, NODEEDGE, OLDEDGE, UNITGRAPH
  43. }
  44. public enum GRAPHTYPE {
  45. SWITCH, ELEMENT
  46. }
  47. private Model model;
  48. private Gson gson;
  49. private int nCat, nObj, nEle, nEdge, nConn, nNodeEdge, nOldEdge, nUnitGraph;
  50. /**
  51. * Constructor.
  52. *
  53. * @param model
  54. * the Model
  55. */
  56. public StoreController(Model model) {
  57. this.model = model;
  58. initGson();
  59. }
  60. /**
  61. * Writes the current State of the Modelling into a JSON File which can be
  62. * loaded.
  63. *
  64. * @param path
  65. * the Path
  66. *
  67. * @throws IOException
  68. * exception
  69. */
  70. public void writeSaveFile(String path) throws IOException {
  71. initNumeration();
  72. JsonObject file = new JsonObject();
  73. initialize(MODE.COMPLETE, file);
  74. storeCategory(file);
  75. storeCanvas(file);
  76. FileWriter writer = new FileWriter(path);
  77. writer.write(gson.toJson(file));
  78. writer.flush();
  79. writer.close();
  80. }
  81. /**
  82. * Writes the Autosave File.
  83. *
  84. * @param path
  85. * the Path
  86. * @throws IOException
  87. * Exception
  88. */
  89. public void writeAutosaveFile(String path) throws IOException {
  90. initNumeration();
  91. JsonObject file = new JsonObject();
  92. initialize(MODE.PARTIAL, file);
  93. storeCanvas(file);
  94. FileWriter writer = new FileWriter(path);
  95. writer.write(gson.toJson(file));
  96. writer.flush();
  97. writer.close();
  98. }
  99. /**
  100. * Write needed default parameter into the JsonObject. Can be extended later
  101. * on
  102. *
  103. * @param mode
  104. * @param file
  105. */
  106. private void initialize(MODE mode, JsonObject file) {
  107. // TODO Auto-generated method stub
  108. switch (mode) {
  109. case COMPLETE:
  110. file.add("MODE", new JsonPrimitive(mode.name()));
  111. file.add("IDCOUNTER", new JsonPrimitive(IdCounter.getCounter()));
  112. file.add("CANVAS_SIZE_X", new JsonPrimitive(model.getCanvasX()));
  113. file.add("CANVAS_SIZE_Y", new JsonPrimitive(model.getCanvasY()));
  114. break;
  115. case PARTIAL:
  116. file.add("MODE", new JsonPrimitive(mode.name()));
  117. file.add("IDCOUNTER", new JsonPrimitive(IdCounter.getCounter()));
  118. break;
  119. default:
  120. break;
  121. }
  122. }
  123. /**
  124. * Store all Categories and Object into a Json File via Serialization
  125. *
  126. * @param file
  127. */
  128. private void storeCategory(JsonObject file) {
  129. // TODO Auto-generated method stub
  130. for (Category cat : model.getCategories()) {
  131. String key = "CATEGORY" + getNumerator(NUMTYPE.CATEGORY);
  132. file.add(key, new JsonPrimitive(cat.getName()));
  133. for (AbstractCpsObject obj : cat.getObjects()) {
  134. file.add("CGOBJECT" + getNumerator(NUMTYPE.OBJECT), gson.toJsonTree(obj, AbstractCpsObject.class));
  135. if (obj instanceof HolonObject)
  136. elementsToJson(TYPE.CATEGORY, gson, file, obj);
  137. }
  138. }
  139. }
  140. /**
  141. * Travers through all Objects via BFS and Stores everything relevant
  142. *
  143. * @param file
  144. */
  145. private void storeCanvas(JsonObject file) {
  146. // TODO Auto-generated method stub
  147. Queue<AbstractCpsObject> queue = new Queue<>();
  148. AbstractCpsObject u = null;
  149. for (AbstractCpsObject cps : model.getObjectsOnCanvas()) {
  150. queue.enqueue(cps);
  151. }
  152. while (!queue.isEmpty()) {
  153. try {
  154. u = queue.dequeue();
  155. } catch (InterruptedException e) {
  156. // TODO Auto-generated catch block
  157. e.printStackTrace();
  158. }
  159. String key = "CVSOBJECT" + getNumerator(NUMTYPE.OBJECT);
  160. file.add(key, gson.toJsonTree(u, AbstractCpsObject.class));
  161. edgeToJson(EDGETYPE.CONNECTION, file, u.getID(), u.getConnections());
  162. if (u instanceof HolonObject)
  163. elementsToJson(TYPE.CANVAS, gson, file, u);
  164. if (u instanceof HolonSwitch)
  165. if (((HolonSwitch) u).getGraphPoints().size() != 0)
  166. unitgraphToJson(GRAPHTYPE.SWITCH, file, u.getID(), ((HolonSwitch) u).getGraphPoints());
  167. if (u instanceof CpsUpperNode) {
  168. for (AbstractCpsObject adjacent : ((CpsUpperNode) u).getNodes()) {
  169. queue.enqueue(adjacent);
  170. }
  171. edgeToJson(EDGETYPE.NODE, file, u.getID(), ((CpsUpperNode) u).getNodeEdges());
  172. edgeToJson(EDGETYPE.OLD, file, u.getID(), ((CpsUpperNode) u).getOldEdges());
  173. }
  174. }
  175. edgeToJson(EDGETYPE.CANVAS, file, 0, model.getEdgesOnCanvas());
  176. }
  177. /**
  178. *
  179. * @param type
  180. * @param gson
  181. * @param file
  182. * @param obj
  183. */
  184. private void elementsToJson(TYPE type, Gson gson, JsonObject file, AbstractCpsObject obj) {
  185. // TODO Auto-generated method stub
  186. JsonObject temp = new JsonObject();
  187. String key = null;
  188. for (HolonElement ele : ((HolonObject) obj).getElements()) {
  189. temp.add("properties", gson.toJsonTree(ele));
  190. temp.add("ID", new JsonPrimitive(obj.getID()));
  191. switch (type) {
  192. case CANVAS:
  193. key = "CVSELEMENT" + getNumerator(NUMTYPE.ELEMENT);
  194. break;
  195. case CATEGORY:
  196. key = "CGELEMENT" + getNumerator(NUMTYPE.ELEMENT);
  197. break;
  198. default:
  199. break;
  200. }
  201. file.add(key, gson.toJsonTree(temp));
  202. if (ele.getGraphPoints().size() != 0)
  203. unitgraphToJson(GRAPHTYPE.ELEMENT, file, ele.getId(), ele.getGraphPoints());
  204. temp = new JsonObject();
  205. }
  206. }
  207. /**
  208. *
  209. * @param ele
  210. */
  211. private void unitgraphToJson(GRAPHTYPE type, JsonObject file, int id, LinkedList<Point> graph) {
  212. JsonObject temp = new JsonObject();
  213. String key = null;
  214. for (int i = 0; i < graph.size(); i++) {
  215. temp.add("" + i, new JsonPrimitive(graph.get(i).x + ":" + graph.get(i).y));
  216. }
  217. switch (type) {
  218. case SWITCH:
  219. key = "SWUNITGRAPH" + getNumerator(NUMTYPE.UNITGRAPH);
  220. break;
  221. case ELEMENT:
  222. key = "ELEUNITGRAPH" + getNumerator(NUMTYPE.UNITGRAPH);
  223. break;
  224. default:
  225. break;
  226. }
  227. temp.add("ID", new JsonPrimitive(id));
  228. file.add(key, gson.toJsonTree(temp));
  229. }
  230. /**
  231. *
  232. * @param type
  233. * @param file
  234. * @param id
  235. * @param arr
  236. */
  237. private void edgeToJson(EDGETYPE type, JsonObject file, int id, ArrayList<CpsEdge> arr) {
  238. // TODO Auto-generated method stub
  239. String k = null;
  240. JsonObject temp = new JsonObject();
  241. for (CpsEdge edge : arr) {
  242. temp.add("properties", gson.toJsonTree(edge));
  243. temp.add("A", new JsonPrimitive(edge.getA().getID()));
  244. temp.add("B", new JsonPrimitive(edge.getB().getID()));
  245. switch (type) {
  246. case CANVAS:
  247. k = "CVSEDGE" + getNumerator(NUMTYPE.EDGE);
  248. break;
  249. case CONNECTION:
  250. k = "CONNEDGE" + getNumerator(NUMTYPE.CONNECTION);
  251. break;
  252. case NODE:
  253. k = "NODEEDGE" + getNumerator(NUMTYPE.NODEEDGE);
  254. temp.add("ID", new JsonPrimitive(id));
  255. break;
  256. case OLD:
  257. k = "OLDEDGE" + getNumerator(NUMTYPE.OLDEDGE);
  258. temp.add("ID", new JsonPrimitive(id));
  259. break;
  260. default:
  261. break;
  262. }
  263. file.add(k, gson.toJsonTree(temp));
  264. temp = new JsonObject();
  265. }
  266. }
  267. /**
  268. * Initialize the Gson with wanted parameters
  269. */
  270. private void initGson() {
  271. // TODO Auto-generated method stub
  272. GsonBuilder builder = new GsonBuilder();
  273. builder.excludeFieldsWithoutExposeAnnotation().serializeNulls().setPrettyPrinting();
  274. builder.registerTypeAdapter(AbstractCpsObject.class, new AbstractCpsObjectAdapter());
  275. builder.registerTypeAdapter(Position.class, new PositionAdapter());
  276. builder.registerTypeAdapter(Color.class, new ColorAdapter());
  277. // use the builder and make a instance of the Gson
  278. this.gson = builder.create();
  279. }
  280. /**
  281. * Just initialize the Numerators for the Json Keys. Maybe bad Style..
  282. */
  283. private void initNumeration() {
  284. this.nCat = this.nObj = this.nEle = this.nEdge = this.nConn = this.nNodeEdge = this.nOldEdge = 0;
  285. }
  286. /**
  287. * Get the wanted numerator and increment it
  288. *
  289. * @param type
  290. * @return
  291. */
  292. private int getNumerator(NUMTYPE type) {
  293. switch (type) {
  294. case CATEGORY:
  295. return nCat++;
  296. case OBJECT:
  297. return nObj++;
  298. case ELEMENT:
  299. return nEle++;
  300. case EDGE:
  301. return nEdge++;
  302. case CONNECTION:
  303. return nConn++;
  304. case NODEEDGE:
  305. return nNodeEdge++;
  306. case OLDEDGE:
  307. return nOldEdge++;
  308. case UNITGRAPH:
  309. return nUnitGraph++;
  310. default:
  311. break;
  312. }
  313. return -1;
  314. }
  315. }