StoreController.java 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. package ui.controller;
  2. import java.awt.Point;
  3. import java.io.FileWriter;
  4. import java.io.IOException;
  5. import java.util.ListIterator;
  6. import org.json.simple.JSONArray;
  7. import org.json.simple.JSONObject;
  8. import com.google.gson.Gson;
  9. import com.google.gson.GsonBuilder;
  10. import Serializer.PositionSerializer;
  11. import classes.Category;
  12. import classes.CpsEdge;
  13. import classes.AbstractCpsObject;
  14. import classes.HolonElement;
  15. import classes.HolonObject;
  16. import classes.HolonSwitch;
  17. import classes.HolonTransformer;
  18. import classes.IdCounter;
  19. import classes.Position;
  20. import ui.model.Model;
  21. /**
  22. * Controller for Storage.
  23. *
  24. * @author Gruppe14
  25. */
  26. public class StoreController {
  27. private Model model;
  28. /**
  29. * Constructor.
  30. *
  31. * @param model
  32. * the Model
  33. */
  34. public StoreController(Model model) {
  35. this.model = model;
  36. }
  37. /**
  38. * Writes the current State of the Modelling into a JSON File which can be
  39. * loaded.
  40. *
  41. * @param path
  42. * the Path
  43. *
  44. * @throws IOException
  45. * exception
  46. */
  47. public void writeSaveFile(String path) throws IOException {
  48. Gson gson = new GsonBuilder().serializeNulls().excludeFieldsWithoutExposeAnnotation().registerTypeAdapter(Position.class, new PositionSerializer()).create();
  49. JSONObject json = new JSONObject();
  50. int i = 1;
  51. // json.put("MODE", "ALL");
  52. // json.put("ID", IdCounter.getCounter());
  53. // json.put("SIZEX", model.getCanvasX());
  54. // json.put("SIZEY", model.getCanvasY());
  55. // writeCategory(json);
  56. // writeCategoryObjects(json);
  57. // writeCanvasObjects(json);
  58. // writeCategoryElements(json);
  59. // writeCanvasElements(json);
  60. // writeEdges(json);
  61. // writeElementGraph(json);
  62. for (AbstractCpsObject cps : model.getObjectsOnCanvas()) {
  63. gson.toJson(cps);
  64. json.put("CVS" + i++, gson.toJson(cps));
  65. }
  66. FileWriter writer = new FileWriter(path);
  67. writer.write(json.toJSONString());
  68. getClass();
  69. writer.flush();
  70. writer.close();
  71. }
  72. /**
  73. * Write the Canvas File.
  74. *
  75. * @param path
  76. * the Path
  77. * @throws IOException
  78. * Exception
  79. */
  80. public void writeCanvasFile(String path) throws IOException {
  81. JSONObject json = new JSONObject();
  82. json.put("MODE", "CANVAS");
  83. json.put("ID", IdCounter.getCounter());
  84. writeCanvasObjects(json);
  85. writeCanvasElements(json);
  86. writeEdges(json);
  87. writeElementGraph(json);
  88. FileWriter writer = new FileWriter(path);
  89. writer.write(json.toJSONString());
  90. getClass();
  91. writer.flush();
  92. writer.close();
  93. }
  94. /**
  95. * Write the Category File.
  96. *
  97. * @param path
  98. * the Path
  99. * @throws IOException
  100. * exception
  101. */
  102. public void writeCategoryFile(String path) throws IOException {
  103. JSONObject json = new JSONObject();
  104. json.put("MODE", "CATEGORY");
  105. // eventuell muss man ID auch Speichern
  106. writeCategory(json);
  107. writeCategoryObjects(json);
  108. writeCategoryElements(json);
  109. FileWriter writer = new FileWriter(path);
  110. writer.write(json.toJSONString());
  111. getClass();
  112. writer.flush();
  113. writer.close();
  114. }
  115. /**
  116. * writes all Categories into a JSONObject.
  117. *
  118. * @param json
  119. * JSON Object
  120. * @throws IOException
  121. * exception
  122. */
  123. public void writeCategory(JSONObject json) {
  124. JSONArray arr = new JSONArray();
  125. for (Category cat : model.getCategories())
  126. arr.add(cat.getName());
  127. json.put("CG", arr);
  128. }
  129. /**
  130. * writes all Objects in Category into a JSONObject.
  131. *
  132. * @param json
  133. * JSON Object
  134. */
  135. public void writeCategoryObjects(JSONObject json) {
  136. JSONArray arr = new JSONArray();
  137. int i = 1;
  138. for (Category cats : model.getCategories())
  139. for (AbstractCpsObject cps : cats.getObjects()) {
  140. arr.add(getObjectType(cps));
  141. arr.add(cps.getSav());
  142. arr.add(cps.getObjName());
  143. arr.add(cps.getImage());
  144. json.put("CGO" + i++, arr);
  145. arr = new JSONArray();
  146. }
  147. }
  148. /**
  149. * Wrte Canvas Objects.
  150. *
  151. * @param json
  152. * JSON Object
  153. */
  154. public void writeCanvasObjects(JSONObject json) {
  155. JSONArray arr = new JSONArray();
  156. int i = 1;
  157. for (AbstractCpsObject cps : model.getObjectsOnCanvas()) {
  158. arr.add(getObjectType(cps));
  159. arr.add(cps.getObjName());
  160. arr.add(cps.getName());
  161. arr.add(cps.getID());
  162. arr.add(cps.getImage());
  163. arr.add(cps.getPosition().x);
  164. arr.add(cps.getPosition().y);
  165. json.put("CVSO" + i++, arr);
  166. arr = new JSONArray();
  167. }
  168. }
  169. /**
  170. * writes all Elements in Objects in Category into a JSONObject.
  171. *
  172. * @param json
  173. * JSON Object
  174. */
  175. public void writeCategoryElements(JSONObject json) {
  176. JSONArray arr = new JSONArray();
  177. int i = 1;
  178. for (Category cats : model.getCategories())
  179. for (AbstractCpsObject cps : cats.getObjects())
  180. if (cps instanceof HolonObject)
  181. for (HolonElement ele : ((HolonObject) cps).getElements()) {
  182. arr.add(ele.getSav());
  183. arr.add(ele.getObj());
  184. arr.add(ele.getEleName());
  185. arr.add(ele.getAmount());
  186. arr.add(ele.getEnergy());
  187. json.put("CGE" + i++, arr);
  188. arr = new JSONArray();
  189. }
  190. }
  191. /**
  192. * Write Canvas Elements.
  193. *
  194. * @param json JSON Objects
  195. */
  196. public void writeCanvasElements(JSONObject json) {
  197. JSONArray arr = new JSONArray();
  198. int i = 1;
  199. for (AbstractCpsObject cps : model.getObjectsOnCanvas()) {
  200. if (cps instanceof HolonObject)
  201. for (HolonElement ele : ((HolonObject) cps).getElements()) {
  202. arr.add(ele.getSav());
  203. arr.add(cps.getID());
  204. arr.add(ele.getEleName());
  205. arr.add(ele.getAmount());
  206. arr.add(ele.getEnergy());
  207. arr.add(Boolean.compare(ele.getActive(), true) + 1);
  208. json.put("CVSE" + i++, arr);
  209. arr = new JSONArray();
  210. }
  211. }
  212. }
  213. /**
  214. * write all Edges into a JSONObject.
  215. *
  216. * @param json JSON Object
  217. */
  218. public void writeEdges(JSONObject json) {
  219. JSONArray arr = new JSONArray();
  220. int i = 1;
  221. for (CpsEdge edge : model.getEdgesOnCanvas()) {
  222. arr.add(edge.getA().getID());
  223. arr.add(edge.getB().getID());
  224. arr.add(edge.getCapacity());
  225. arr.add(edge.getFlow());
  226. json.put("EDGE" + i++, arr);
  227. arr = new JSONArray();
  228. }
  229. }
  230. /**
  231. * writes the Graph from all Elements into a JSONObject.
  232. *
  233. * @param json JSON Object
  234. */
  235. public void writeElementGraph(JSONObject json) {
  236. ListIterator<Point> iterator;
  237. JSONArray arr = new JSONArray();
  238. int i = 1;
  239. for (Category cats : model.getCategories())
  240. for (AbstractCpsObject cps : cats.getObjects())
  241. if (cps instanceof HolonObject)
  242. for (HolonElement ele : ((HolonObject) cps).getElements())
  243. if (!ele.getGraphPoints().isEmpty()) {
  244. iterator = ele.getGraphPoints().listIterator();
  245. arr.add(ele.getSav());
  246. arr.add(ele.getObj());
  247. arr.add(ele.getEleName());
  248. while (iterator.hasNext()) {
  249. Point p = iterator.next();
  250. arr.add((int) p.getX());
  251. arr.add((int) p.getY());
  252. }
  253. json.put("CGGP" + i++, arr);
  254. arr = new JSONArray();
  255. }
  256. i = 1;
  257. for (AbstractCpsObject cps : model.getObjectsOnCanvas()) {
  258. if (cps instanceof HolonObject)
  259. for (HolonElement ele : ((HolonObject) cps).getElements())
  260. if (!ele.getGraphPoints().isEmpty()) {
  261. iterator = ele.getGraphPoints().listIterator();
  262. arr.add(ele.getSav());
  263. arr.add(cps.getID());
  264. arr.add(ele.getEleName());
  265. while (iterator.hasNext()) {
  266. Point p = iterator.next();
  267. arr.add((int) p.getX());
  268. arr.add((int) p.getY());
  269. }
  270. json.put("CVSGP" + i++, arr);
  271. arr = new JSONArray();
  272. }
  273. }
  274. }
  275. /**
  276. * Return the Object Type.
  277. *
  278. * @param cps AbstractCpsObject
  279. * @return The Object Type
  280. */
  281. public String getObjectType(AbstractCpsObject cps) {
  282. if (cps instanceof HolonObject)
  283. return "HolonObject";
  284. if (cps instanceof HolonTransformer)
  285. return "HolonTransformer";
  286. if (cps instanceof HolonSwitch)
  287. return "HolonSwitch";
  288. else
  289. return "CpsNode";
  290. }
  291. }