1 4 package com.nightlabs.ipanema.config; 5 6 import java.rmi.RemoteException ; 7 import java.util.Collection ; 8 9 import javax.ejb.CreateException ; 10 import javax.ejb.EJBException ; 11 import javax.ejb.SessionBean ; 12 import javax.jdo.Extent; 13 import javax.jdo.PersistenceManager; 14 import javax.jdo.Query; 15 16 import com.nightlabs.ModuleException; 17 import com.nightlabs.inheritance.InheritenceManager; 18 import com.nightlabs.ipanema.base.BaseSessionBeanImpl; 19 import com.nightlabs.ipanema.config.id.ConfigID; 20 import com.nightlabs.ipanema.security.User; 21 import com.nightlabs.ipanema.security.UserGroup; 22 import com.nightlabs.jdo.NLJDOHelper; 23 24 33 34 public abstract class ConfigManagerBean extends BaseSessionBeanImpl implements SessionBean 35 { 36 39 public void ejbCreate() throws CreateException  40 { 41 } 42 47 public void ejbRemove() throws EJBException , RemoteException { } 48 49 50 65 public UserConfigModule getUserConfigModule(ConfigID userConfigID, Class cfModClass, String cfModID, String fetchGroups[]) 66 throws ModuleException 67 { 68 PersistenceManager pm; 69 pm = getPersistenceManager(); 70 try 71 { 72 UserConfig userConfig = UserConfig.getUserConfig(pm, userConfigID.organisationID, userConfigID.configID); 73 return getUserConfigModule(pm, userConfig, cfModClass, cfModID, fetchGroups); 74 } finally { 75 pm.close(); 76 } 77 } 78 79 94 public UserConfigModule getUserConfigModule(UserConfig userConfig, Class cfModClass, String cfModID, String fetchGroups[]) 95 throws ModuleException 96 { 97 PersistenceManager pm; 98 pm = getPersistenceManager(); 99 try 100 { 101 return getUserConfigModule(pm, userConfig, cfModClass, cfModID, fetchGroups); 102 } finally { 103 pm.close(); 104 } 105 } 106 107 public static UserConfigModule getUserConfigModule(PersistenceManager pm, UserConfig userConfig, Class cfModClass, String cfModID, String fetchGroups[]) 108 throws ModuleException 109 { 110 UserConfigModule configModule = UserConfigModule.getConfigModuleForUserConfig(pm, userConfig, cfModClass, cfModID); 111 if (configModule == null) { 112 try { 113 configModule = UserConfigModule.createNewConfigModule(pm, true, cfModClass, userConfig, cfModID); 114 } catch (Exception e) { 115 throw new ModuleException(e); 116 } 117 } 118 119 if (fetchGroups != null) 120 pm.getFetchPlan().setGroups(fetchGroups); 121 else 122 pm.getFetchPlan().clearGroups(); 123 124 return (UserConfigModule)pm.detachCopy(configModule); 125 } 126 127 139 public UserConfigModule getUserConfigModule(Class cfModClass, String cfModID, String fetchGroups[]) 140 throws ModuleException 141 { 142 PersistenceManager pm; 143 pm = getPersistenceManager(); 144 try 145 { 146 User user = User.getUser(pm, getPrincipal()); 147 UserConfig userConfig = UserConfig.getUserConfig(pm, user); 148 UserConfigGroup userConfigGroup = UserConfigGroup.getUserConfigGroupForUser(pm, user); 149 UserConfigModule personalConfigModule = UserConfigModule.getConfigModuleForUserConfig(pm, userConfig, cfModClass, cfModID); 150 if (personalConfigModule == null) { 151 try { 152 personalConfigModule = UserConfigModule.createNewConfigModule(pm, true, cfModClass, userConfig, cfModID); 153 } catch (Exception e) { 154 throw new ModuleException("Unable to autocreate configModule of class "+cfModClass.getName(), e); 155 } 156 } 157 158 159 if (userConfigGroup != null) { 160 UserConfigModule groupConfigModule = UserConfigModule.getConfigModuleForUserConfig(pm, userConfigGroup, cfModClass, cfModID); 161 if (groupConfigModule != null) 162 (new InheritenceManager()).inheritAllFields(groupConfigModule, personalConfigModule); 163 } 164 165 if (fetchGroups != null) 166 pm.getFetchPlan().setGroups(fetchGroups); 167 168 UserConfigModule result = (UserConfigModule)pm.detachCopy(personalConfigModule); 169 return result; 170 } finally { 171 pm.close(); 172 } 173 } 174 175 180 public UserConfigModule storeUserConfigModule(UserConfigModule configModule, boolean get, String [] fetchGroups) 181 throws ModuleException 182 { 183 PersistenceManager pm; 184 pm = getPersistenceManager(); 185 try 186 { 187 User user = User.getUser(pm, getPrincipal()); 188 if (!configModule.getConfigID().equals(user.getUserID())) { 189 throw new IllegalArgumentException ("Attempt to store ConfigModule for other user or group"); 190 } 191 UserConfigModule result = (UserConfigModule)NLJDOHelper.storeJDO(pm, configModule, get, fetchGroups); 192 return result; 193 } finally { 194 pm.close(); 195 } 196 } 197 198 204 public UserConfigModule storeUserConfigModule(ConfigID userConfigID, UserConfigModule configModule, boolean get, String [] fetchGroups) 205 throws ModuleException 206 { 207 PersistenceManager pm; 208 pm = getPersistenceManager(); 209 try 210 { 211 UserConfig userConfig = UserConfig.getUserConfig(pm, userConfigID.organisationID, userConfigID.configID); 212 if (! (userConfig.getOrganisationID().equals(configModule.getOrganisationID()) && userConfig.getUserConfigID().equals(configModule.getConfigID()) ) ) 213 throw new IllegalArgumentException ("Attempt to store ConfigModule to UserConfigGroup that is not in this group (organisationID and userConfigID don't match)"); 214 return (UserConfigModule)NLJDOHelper.storeJDO(pm, configModule, get, fetchGroups); 215 } finally { 216 pm.close(); 217 } 218 } 219 220 227 public WorkstationConfig getWorkstationConfig(String organisationID, String workstationID, String [] fetchGroups) 228 throws ModuleException 229 { 230 PersistenceManager pm = getPersistenceManager(); 231 try 232 { 233 if (fetchGroups != null) 234 pm.getFetchPlan().setGroups(fetchGroups); 235 else 236 pm.getFetchPlan().clearGroups(); 237 238 Extent ext = pm.getExtent(WorkstationConfig.class, false); 239 Query query = pm.newQuery( 240 "SELECT FROM com.nightlabs.ipanema.config.WorkstationConfig " + 241 "WHERE " + 242 " organisationID == paramOrganisationID && " + 243 " configID == paramWorkstationID && " + 244 " implementationClassname == paramClassname " + 245 "PARAMETERS String paramOrganisationID, String paramWorkstationID, String paramClassname " + 246 "IMPORTS import java.lang.String"); 247 query.setUnique(true); 248 249 Object o = query.execute(organisationID, workstationID, WorkstationConfig.class.getName()); 250 if(o == null) 251 { 252 WorkstationConfig newConf = new WorkstationConfig(organisationID, workstationID); 253 pm.makePersistent(newConf); 254 return (WorkstationConfig)pm.detachCopy(newConf); 255 } 256 else 257 return (WorkstationConfig)pm.detachCopy(o); 258 } 259 finally 260 { 261 pm.close(); 262 } 263 } 264 265 271 public UserConfigModule storeUserConfigModule(UserConfig userConfig, UserConfigModule configModule, boolean get, String [] fetchGroups) 272 throws ModuleException 273 { 274 PersistenceManager pm; 275 pm = getPersistenceManager(); 276 try 277 { 278 if (! (userConfig.getOrganisationID().equals(configModule.getOrganisationID()) && userConfig.getUserConfigID().equals(configModule.getConfigID()) ) ) 279 throw new IllegalArgumentException ("Attempt to store ConfigModule to UserConfigGroup that is not in this group (organisationID and userConfigID don't match)"); 280 return (UserConfigModule)NLJDOHelper.storeJDO(pm, configModule, get, fetchGroups); 281 } finally { 282 pm.close(); 283 } 284 } 285 286 291 public Collection getUserConfigGroups(String [] fetchGroups) 292 throws ModuleException 293 { 294 PersistenceManager pm; 295 pm = getPersistenceManager(); 296 try 297 { 298 Collection groups = (Collection )pm.newQuery(UserConfigGroup.class).execute(); 299 300 if (fetchGroups != null) 301 pm.getFetchPlan().setGroups(fetchGroups); 302 else 303 pm.getFetchPlan().clearGroups(); 304 305 Collection result = pm.detachCopyAll(groups); 306 return result; 307 } finally { 308 pm.close(); 309 } 310 } 311 312 317 public Collection getUserConfigs(String [] fetchGroups) 318 throws ModuleException 319 { 320 PersistenceManager pm; 321 pm = getPersistenceManager(); 322 try 323 { 324 Collection groups = (Collection )pm.newQuery(UserConfig.class).execute(); 325 326 if (fetchGroups != null) 327 pm.getFetchPlan().setGroups(fetchGroups); 328 else 329 pm.getFetchPlan().clearGroups(); 330 331 Collection result = pm.detachCopyAll(groups); 332 return result; 333 } finally { 334 pm.close(); 335 } 336 } 337 338 343 public Collection getUserConfigsForGroup(UserConfigGroup group, String [] fetchGroups) 344 throws ModuleException 345 { 346 PersistenceManager pm; 347 pm = getPersistenceManager(); 348 try 349 { 350 Collection userConfigs = UserConfig.getUserConfigsForGroup(pm, group.getOrganisationID(), group.getUserConfigID()); 351 352 if (fetchGroups != null) 353 pm.getFetchPlan().setGroups(fetchGroups); 354 else 355 pm.getFetchPlan().clearGroups(); 356 357 Collection result = pm.detachCopyAll(userConfigs); 358 return result; 359 } finally { 360 pm.close(); 361 } 362 } 363 364 369 public UserConfigGroup addUserConfigGroup(String userConfigID, String groupName, boolean get, String [] fetchGroups) 370 throws ModuleException 371 { 372 PersistenceManager pm; 373 pm = getPersistenceManager(); 374 try 375 { 376 UserConfigGroup group = new UserConfigGroup(getOrganisationID(), userConfigID); 377 group.setName(groupName); 378 pm.makePersistent(group); 379 380 if (get) { 381 if (fetchGroups != null) 382 pm.getFetchPlan().setGroups(fetchGroups); 383 else 384 pm.getFetchPlan().clearGroups(); 385 UserConfigGroup result = (UserConfigGroup)pm.detachCopy(group); 386 return result; 387 } 388 return null; 389 } finally { 390 pm.close(); 391 } 392 } 393 394 399 public UserConfigSetup getUserConfigSetup(String [] groupsFetchGropus, String [] configsFetchGroups) 400 throws ModuleException 401 { 402 PersistenceManager pm; 403 pm = getPersistenceManager(); 404 try 405 { 406 return UserConfigSetup.getUserConfigSetup(pm, getOrganisationID(), groupsFetchGropus, configsFetchGroups); 407 } finally { 408 pm.close(); 409 } 410 } 411 412 417 public void storeUserConfigSetup(UserConfigSetup setup) 418 throws ModuleException 419 { 420 PersistenceManager pm; 421 pm = getPersistenceManager(); 422 try 423 { 424 UserConfigSetup.storeUserConfigSetup(pm, setup); 425 } finally { 426 pm.close(); 427 } 428 } 429 430 }
| Popular Tags
|