PageRenderTime 41ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/src/mpv5/globals/GlobalSettings.java

http://mp-rechnungs-und-kundenverwaltung.googlecode.com/
Java | 252 lines | 138 code | 38 blank | 76 comment | 36 complexity | 2058fe612c22e6b4b048afd6b3c77437 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. package mpv5.globals;
  2. import java.util.List;
  3. import mpv5.data.PropertyStore;
  4. import mpv5.db.common.Context;
  5. import mpv5.db.common.NodataFoundException;
  6. import mpv5.db.common.QueryCriteria;
  7. import mpv5.db.common.QueryCriteria2;
  8. import mpv5.db.common.QueryData;
  9. import mpv5.db.common.QueryHandler;
  10. import mpv5.db.common.QueryParameter;
  11. import mpv5.logging.Log;
  12. import mpv5.utils.text.TypeConversion;
  13. /**
  14. *
  15. *
  16. */
  17. public class GlobalSettings {
  18. private static PropertyStore cookie = new PropertyStore();
  19. private static PropertyStore predefinedSettings = new PropertyStore(new String[][]{
  20. });
  21. /**
  22. * Applies the environmental settings
  23. */
  24. public static synchronized void apply() {
  25. }
  26. /**
  27. * Get a properties value, or 0 if N/A
  28. * @param name
  29. * @return
  30. */
  31. @SuppressWarnings("unchecked")
  32. public synchronized static int getIntegerProperty(String name) {
  33. return getIntegerProperty(name, 0);
  34. }
  35. public static int getIntegerProperty(String name, int defaultVal) {
  36. if (cookie.getProperty(name) != null) {
  37. return Integer.valueOf(cookie.getProperty(name));
  38. } else if (predefinedSettings.getProperty(name) != null) {
  39. cookie.changeProperty(name, predefinedSettings.getProperty(name));
  40. } else {
  41. cookie.changeProperty(name, defaultVal + "");
  42. }
  43. return Integer.valueOf(cookie.getProperty(name));
  44. }
  45. /**
  46. * Get a properties value, or false if N/A
  47. * @param name
  48. * @return
  49. */
  50. @SuppressWarnings("unchecked")
  51. public synchronized static boolean getBooleanProperty(String name) {
  52. return getBooleanProperty(name, false);
  53. }
  54. /**
  55. * Get a properties value, or false if N/A
  56. * @param name
  57. * @param defaultVal
  58. * @return
  59. */
  60. @SuppressWarnings("unchecked")
  61. public synchronized static boolean getBooleanProperty(String name, boolean defaultVal) {
  62. if (cookie.getProperty(name) != null) {
  63. return TypeConversion.stringToBoolean(cookie.getProperty(name));
  64. } else if (predefinedSettings.getProperty(name) != null) {
  65. cookie.changeProperty(name, predefinedSettings.getProperty(name));
  66. } else {
  67. cookie.changeProperty(name, String.valueOf(defaultVal));
  68. }
  69. return TypeConversion.stringToBoolean(cookie.getProperty(name));
  70. }
  71. /**
  72. * Get a properties value, or 0 if N/A
  73. * @param name
  74. * @return
  75. */
  76. @SuppressWarnings("unchecked")
  77. public synchronized static double getDoubleProperty(String name) {
  78. return getDoubleProperty(name, 0d);
  79. }
  80. /**
  81. * Get a properties value, or 0 if N/A
  82. * @param name
  83. * @param defaultVal
  84. * @return
  85. */
  86. @SuppressWarnings("unchecked")
  87. public synchronized static double getDoubleProperty(String name, double defaultVal) {
  88. if (cookie.getProperty(name) != null) {
  89. return Double.valueOf(cookie.getProperty(name));
  90. } else if (predefinedSettings.getProperty(name) != null) {
  91. cookie.changeProperty(name, predefinedSettings.getProperty(name));
  92. } else {
  93. cookie.changeProperty(name, defaultVal + "");
  94. }
  95. return Double.valueOf(cookie.getProperty(name));
  96. }
  97. /**
  98. * Get a properties value, or the String "null" if N/A
  99. * @param name
  100. * @return
  101. */
  102. public static synchronized String getProperty(String name) {
  103. if (cookie.getProperty(name) != null) {
  104. return cookie.getProperty(name);
  105. } else if (predefinedSettings.getProperty(name) != null) {
  106. cookie.changeProperty(name, predefinedSettings.getProperty(name));
  107. } else {
  108. cookie.changeProperty(name, "null");
  109. }
  110. return cookie.getProperty(name);
  111. }
  112. /**
  113. * Get a properties value, or the String "null" if N/A
  114. * @param name
  115. * @param defaultVal
  116. * @return
  117. */
  118. public static synchronized String getProperty(String name, String defaultVal) {
  119. if (cookie.getProperty(name) != null) {
  120. return cookie.getProperty(name);
  121. } else if (predefinedSettings.getProperty(name) != null) {
  122. cookie.changeProperty(name, predefinedSettings.getProperty(name));
  123. } else {
  124. cookie.changeProperty(name, defaultVal);
  125. }
  126. return cookie.getProperty(name);
  127. }
  128. private static int connectionID = 2;
  129. /**
  130. * Specify the connection id to be used from the config table, default is 2
  131. * @param id
  132. */
  133. public static void setConnectionID(Integer id) {
  134. connectionID = id;
  135. Log.Debug(GlobalSettings.class, "Using conn id: " + id);
  136. }
  137. /**
  138. * Get the connection id to be used from the config file
  139. * @return
  140. */
  141. public static int getConnectionID() {
  142. return connectionID;
  143. }
  144. /**
  145. *
  146. * @return
  147. */
  148. public static PropertyStore getPropertyStore() {
  149. PropertyStore p = new PropertyStore();
  150. p.addAll(predefinedSettings.getList().toArray(new String[][]{}));
  151. p.addAll(cookie.getList().toArray(new String[][]{}));
  152. return p;
  153. }
  154. /**
  155. * Returns True if the local property store does contain a value with the given key name
  156. * @param propertyname
  157. * @return True if the key exists
  158. */
  159. public static boolean hasProperty(String propertyname) {
  160. return (cookie.hasProperty(propertyname) && !cookie.getProperty(propertyname).equals("null")) || (predefinedSettings.hasProperty(propertyname) && !predefinedSettings.getProperty(propertyname).equals("null"));
  161. }
  162. /**
  163. * Add or change a property
  164. * @param name
  165. * @param value
  166. */
  167. public static synchronized void setProperty(String name, String value) {
  168. if (value == null) {
  169. value = "null";
  170. }
  171. Log.Debug(GlobalSettings.class, "Changing property '" + name + "' to: " + value);
  172. cookie.changeProperty(name, value);
  173. }
  174. /**
  175. * Read the global settings from DB
  176. * @throws java.lang.Exception
  177. */
  178. public static synchronized void read() throws Exception {
  179. try {
  180. try {
  181. Log.Debug(GlobalSettings.class, "Reading in global settings where ID =" + connectionID);
  182. QueryCriteria2 c = new QueryCriteria2();
  183. c.and(new QueryParameter(Context.getGlobalSettings(), "groupsids", connectionID, QueryParameter.EQUALS));
  184. Object[][] data = QueryHandler.instanceOf().clone(Context.getGlobalSettings()).select("cname, value", c).getData();
  185. cookie.addAll(data);
  186. Log.Debug(GlobalSettings.class, "Finished global settings.");
  187. } catch (NodataFoundException nodataFoundException) {
  188. Log.Debug(GlobalSettings.class, "No global settings found..");
  189. }
  190. } catch (Exception e) {
  191. Log.Debug(GlobalSettings.class, e);
  192. }
  193. }
  194. /**
  195. * Save the global settings to DB
  196. */
  197. public synchronized static void save() {
  198. //Remove old data
  199. QueryHandler.instanceOf().clone(Context.getGlobalSettings()).delete(new QueryCriteria("groupsids", connectionID));
  200. //Write new values
  201. List<String[]> list = cookie.getList();
  202. for (int i = 0; i < list.size(); i++) {
  203. String[] val = list.get(i);
  204. QueryData data = new QueryData();
  205. data.add("groupsids", connectionID);
  206. data.add("cname", val[0]);
  207. data.add("value", val[1]);
  208. QueryHandler.instanceOf().clone(Context.getGlobalSettings()).insert(data, null);
  209. }
  210. }
  211. public static void removeAll() {
  212. cookie.removeAll();
  213. }
  214. }