StoreController.java 7.9 KB

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