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