1 6 package com.nightlabs.ipanema.config; 7 8 import java.io.Serializable ; 9 import java.lang.reflect.Constructor ; 10 import java.lang.reflect.InvocationTargetException ; 11 import java.util.Collection ; 12 import java.util.HashMap ; 13 import java.util.HashSet ; 14 import java.util.Map ; 15 import java.util.Set ; 16 17 import javax.jdo.PersistenceManager; 18 import javax.jdo.Query; 19 20 import com.nightlabs.config.CfModList; 21 import com.nightlabs.inheritance.FieldInheriter; 22 import com.nightlabs.inheritance.FieldMetaData; 23 import com.nightlabs.inheritance.Inheritable; 24 import com.nightlabs.inheritance.SimpleFieldInheriter; 25 26 49 public abstract class ConfigModule implements Serializable , Inheritable 50 { 51 public static final String FETCH_GROUP_THIS_CONFIG_MODULE = "ConfigModule.this"; 52 53 public static final String QUERY_GET_CONFIGMODULE = "getConfigModule"; 54 55 protected ConfigModule() 56 { 57 super(); 58 } 59 60 protected ConfigModule(Config cfg, String cfModID) 61 { 62 setPrimaryKeyFields(cfg.getOrganisationID(), cfg.getConfigID(), cfg.getClass().getName(), cfModID); 63 } 64 65 public ConfigModule(String organisationID, String configID, String implClassName, String cfModID) 66 { 67 setPrimaryKeyFields(organisationID, configID, implClassName, cfModID); 68 } 69 70 74 private String organisationID; 75 76 80 private String configID; 81 82 86 protected String implementationClassName; 87 88 92 private String cfModKey; 93 94 97 private boolean allowUserOverride; 98 99 102 private String cfModID; 103 104 119 protected Map fieldMetaDataMap = new HashMap (); 120 121 private void setPrimaryKeyFields(String organisationID, String configID, String implClassName, String cfModID) 122 { 123 this.organisationID = organisationID; 124 this.configID = configID; 125 this.implementationClassName = implClassName; 126 cfModKey = getCfModKey(this.getClass(), cfModID); 127 this.cfModID = cfModID; 128 } 129 130 public static String getCfModKey(Class cfModClass, String cfModID) { 131 String result = cfModClass.getName(); 132 if (cfModID != null && "".equals(cfModID)) 133 result += "/" + cfModID; 134 return result; 135 } 136 137 public String getOrganisationID() 138 { 139 return organisationID; 140 } 141 142 public String getConfigID() 143 { 144 return configID; 145 } 146 147 public String getCfModKey() 148 { 149 return cfModKey; 150 } 151 152 public String getCfModID() 153 { 154 return cfModID; 155 } 156 157 162 public boolean isAllowUserOverride() 163 { 164 return allowUserOverride; 165 } 166 167 public void setAllowUserOverride(boolean allowUserOverride) 168 { 169 this.allowUserOverride = allowUserOverride; 170 } 171 172 173 177 public abstract void init(); 178 179 private static SimpleFieldInheriter fieldInheriter = new SimpleFieldInheriter(); 180 181 public FieldInheriter getFieldInheriter(String fieldName) 182 { 183 return fieldInheriter; 184 } 185 186 private static Set nonInheritableFields = new HashSet (); 187 188 public FieldMetaData getFieldMetaData(String fieldName) 189 { 190 if (fieldName.startsWith("jdo")) 191 return null; 192 193 synchronized (nonInheritableFields) 194 { 195 if (nonInheritableFields.isEmpty()) 196 { 197 nonInheritableFields.add("organisationID"); 198 nonInheritableFields.add("configID"); 199 nonInheritableFields.add("cfModID"); 200 nonInheritableFields.add("cfModKey"); 201 nonInheritableFields.add("allowUserOverride"); 202 } 203 if (nonInheritableFields.contains(fieldName)) 204 return null; 205 } 206 207 ConfigModuleSimpleFieldMetaData fmd = (ConfigModuleSimpleFieldMetaData)fieldMetaDataMap.get(fieldName); 208 if (fmd == null) { 209 fmd = new ConfigModuleSimpleFieldMetaData(this, fieldName); 210 fieldMetaDataMap.put(fieldName, fmd); 211 } 212 return fmd; 213 } 214 215 public static ConfigModule createNewConfigModule(PersistenceManager pm, boolean makePersistent, Class cfModClass, Config config, String cfModID) 216 throws InstantiationException , IllegalAccessException , ClassCastException , SecurityException , NoSuchMethodException , IllegalArgumentException , InvocationTargetException  217 { 218 Constructor constructor = cfModClass.getConstructor(new Class []{Config.class, String .class}); 219 ConfigModule result = (ConfigModule)constructor.newInstance(new Object [] {config, cfModID}); 220 result.init(); 221 if (makePersistent) 222 pm.makePersistent(result); 223 return result; 224 } 225 226 public static ConfigModule getConfigModule(PersistenceManager pm, Config config, Class cfModClass, String cfModID) 227 { 228 Query q = pm.newNamedQuery(ConfigModule.class, QUERY_GET_CONFIGMODULE); 229 Map args = new HashMap (); 230 args.put("paramOrganisationID", config.getOrganisationID()); 231 args.put("paramConfigID", config.getConfigID()); 232 args.put("paramImplClassName", config.getClass().getName()); 233 args.put("paramCfModKey", getCfModKey(cfModClass, cfModID)); 234 q.setUnique(true); 235 return (ConfigModule)q.execute(args); 236 } 237 238 } 239
| Popular Tags
|