PageRenderTime 59ms CodeModel.GetById 49ms RepoModel.GetById 1ms app.codeStats 0ms

/src/mpv5/webshopinterface/WSIManager.java

http://mp-rechnungs-und-kundenverwaltung.googlecode.com/
Java | 191 lines | 108 code | 36 blank | 47 comment | 8 complexity | 1cf5831fc9c82825d4b5113bb20acd06 MD5 | raw file
Possible License(s): LGPL-3.0, Apache-2.0, GPL-3.0, GPL-2.0, AGPL-3.0, JSON, BSD-3-Clause
  1. /*
  2. * This file is part of YaBS.
  3. *
  4. * YaBS is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * YaBS is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with YaBS. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. package mpv5.webshopinterface;
  18. //~--- non-JDK imports --------------------------------------------------------
  19. import mpv5.db.common.Context;
  20. import mpv5.db.common.DatabaseObject;
  21. import mpv5.db.common.NodataFoundException;
  22. import mpv5.db.objects.WebShop;
  23. import mpv5.logging.Log;
  24. import mpv5.ui.dialogs.Popup;
  25. import mpv5.webshopinterface.wsdjobs.addContactJob;
  26. import mpv5.webshopinterface.wsdjobs.newContactsJob;
  27. import mpv5.webshopinterface.wsdjobs.newOrdersJob;
  28. import mpv5.webshopinterface.wsdjobs.newSystemMessages;
  29. import mpv5.webshopinterface.wsdjobs.updatedContactsJob;
  30. import mpv5.webshopinterface.wsdjobs.updatedOrdersJob;
  31. //~--- JDK imports ------------------------------------------------------------
  32. import java.net.URL;
  33. import java.util.ArrayList;
  34. import java.util.Collection;
  35. import java.util.HashMap;
  36. import java.util.Iterator;
  37. import java.util.List;
  38. import java.util.Set;
  39. import java.util.Vector;
  40. import java.util.logging.Level;
  41. import java.util.logging.Logger;
  42. import javax.naming.OperationNotSupportedException;
  43. /**
  44. * Manages the web shops and related tasks
  45. */
  46. public class WSIManager {
  47. private static WSIManager instance;
  48. private ArrayList<WebShop> shops = new ArrayList<WebShop>();
  49. private HashMap<WebShop, WSDaemon> shopDeamons = new HashMap<WebShop, WSDaemon>();
  50. private WSIManager() {
  51. reset();
  52. }
  53. /**
  54. * Get the one and only WSI Manager instance
  55. * @return
  56. */
  57. public static WSIManager instanceOf() {
  58. return (instance == null)
  59. ? instance = new WSIManager()
  60. : instance;
  61. }
  62. /**
  63. * Starts the WSI Manager
  64. */
  65. public void start() {
  66. Log.Debug(this, "Initiating WebShop clients..");
  67. for (int i = 0; i < shops.size(); i++) {
  68. WebShop webShop = shops.get(i);
  69. try {
  70. WSDaemon d = new WSDaemon(webShop);
  71. d.addJob(new newContactsJob(d));
  72. d.addJob(new newOrdersJob(d));
  73. d.addJob(new newSystemMessages(d));
  74. d.addJob(new updatedContactsJob(d));
  75. d.addJob(new updatedOrdersJob(d));
  76. d.addJob(new addContactJob(d));
  77. d.start();
  78. shopDeamons.put(webShop, d);
  79. Log.Debug(this, "WebShop client: " + d + " started.");
  80. } catch (Exception ex) {
  81. Log.Debug(ex);
  82. }
  83. }
  84. Log.Debug(this, "Done with initiating WebShop clients.");
  85. }
  86. /**
  87. * @return the shops
  88. */
  89. public ArrayList<WebShop> getShops() {
  90. return shops;
  91. }
  92. /**
  93. * Creates objects from the given map/list. Assumes data in the format [int id, String key, String value].
  94. * @param <T>
  95. * @param data
  96. * @param template
  97. * @return
  98. */
  99. @SuppressWarnings("unchecked")
  100. public static <T extends DatabaseObject> List<T> createObjects(Object data, T template) {
  101. Vector<T> list = new Vector<T>();
  102. List<String[]> rawlist = new Vector<String[]>();
  103. if (data instanceof HashMap) {
  104. HashMap m = (HashMap) data;
  105. Collection<HashMap> d = m.values();
  106. for (Iterator<HashMap> it = d.iterator(); it.hasNext(); ) {
  107. HashMap hashMap = it.next();
  108. rawlist.add(new String[] { String.valueOf(hashMap.get("id")), String.valueOf(hashMap.get("key")),
  109. String.valueOf(hashMap.get("value")) });
  110. }
  111. } else // else if (data instanceof List) {
  112. // List n = (List) data;
  113. // for (int i = 0; i < n.size(); i++) {
  114. // rawlist.add(new String[]{String.valueOf(((Object[]) n.get(i))[0]), String.valueOf(((Object[]) n.get(i))[1]), String.valueOf(((Object[]) n.get(i))[2])});
  115. // }
  116. // } else
  117. {
  118. throw new IllegalArgumentException("Only HashMap is supported here! You provided: " + data.getClass());
  119. }
  120. HashMap<String, T> result = new HashMap<String, T>();
  121. for (int i = 0; i < rawlist.size(); i++) {
  122. String[] strings = rawlist.get(i);
  123. String id = strings[0];
  124. String key = strings[1];
  125. String value = strings[2];
  126. if (!result.containsKey(id)) {
  127. result.put(id, (T) DatabaseObject.getObject(template.getContext()));
  128. }
  129. try {
  130. result.get(id).parse(key, value);
  131. } catch (Exception ex) {
  132. Log.Debug(ex);
  133. }
  134. }
  135. list.addAll(result.values());
  136. Log.Debug(WSIManager.class, "Found sets: " + list.size());
  137. return list;
  138. }
  139. /**
  140. * Reset. Call start() afterwards.
  141. */
  142. public void reset() {
  143. try {
  144. shops.clear();
  145. Set<WebShop> k = shopDeamons.keySet();
  146. for (Iterator i = k.iterator(); i.hasNext(); ) {
  147. shopDeamons.get((WebShop) i.next()).kill();
  148. }
  149. shops.addAll(DatabaseObject.toObjectList(DatabaseObject.getObjects(Context.getWebShop()), new WebShop()));
  150. } catch (NodataFoundException ex) {
  151. Log.Debug(this, ex.getMessage());
  152. }
  153. }
  154. }
  155. //~ Formatted by Jindent --- http://www.jindent.com