1 4 package com.nightlabs.ipanema.person; 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.ejb.SessionContext ; 13 import javax.jdo.FetchPlan; 14 import javax.jdo.JDOObjectNotFoundException; 15 import javax.jdo.PersistenceManager; 16 import javax.jdo.Query; 17 18 import org.apache.log4j.Logger; 19 20 import com.nightlabs.ModuleException; 21 import com.nightlabs.ipanema.base.BaseSessionBeanImpl; 22 import com.nightlabs.ipanema.person.id.PersonID; 23 import com.nightlabs.jdo.NLJDOHelper; 24 import com.nightlabs.jdo.search.SearchFilter; 25 import com.nightlabs.util.Utils; 26 27 32 33 41 public abstract class PersonManagerBean extends BaseSessionBeanImpl implements 42 SessionBean { 43 private static final Logger LOGGER = Logger.getLogger(PersonManagerBean.class); 44 45 48 public void setSessionContext(SessionContext sessionContext) 49 throws EJBException , RemoteException { 50 super.setSessionContext(sessionContext); 51 } 52 53 57 public void ejbCreate() throws CreateException { 58 } 65 66 71 public void ejbRemove() throws EJBException , RemoteException { 72 } 73 74 81 public PersonStruct getFullPersonStructure() throws ModuleException { 82 PersistenceManager pm = getPersistenceManager(); 83 try { 84 pm.getFetchPlan().addGroup(FetchPlan.ALL); 85 PersonStruct ps = PersonStruct.getPersonStruct(pm); 86 PersonStruct result = (PersonStruct) pm.detachCopy(ps); 87 return result; 88 89 } finally { 90 pm.close(); 91 } 92 } 93 94 101 public long createPersonID() throws ModuleException { 102 PersistenceManager pm = this.getPersistenceManager(); 103 try { 104 long nextPersonID = PersonRegistry.getRegistry(pm).createPersonID(); 105 LOGGER.info("createPersonID() returning "+nextPersonID+" as next PersonID"); 106 return nextPersonID; 107 } 108 finally { 109 pm.close(); 110 } 111 } 112 113 121 public Person getPerson(PersonID personID, String [] fetchGroups) 122 throws ModuleException, JDOObjectNotFoundException { 123 PersistenceManager pm = this.getPersistenceManager(); 124 pm.getExtent(Person.class, true); 125 Person person = (Person) pm.getObjectById(personID, true); 126 127 if (fetchGroups != null) { 128 for (int i = 0; i < fetchGroups.length; i++) { 129 pm.getFetchPlan().addGroup(fetchGroups[i]); 130 } 131 } 132 133 Person result = (Person) pm.detachCopy(person); 134 pm.close(); 135 136 return result; 137 } 138 139 147 public Person getPerson(PersonID personID) throws ModuleException, 148 JDOObjectNotFoundException { 149 PersistenceManager pm = this.getPersistenceManager(); 150 try { 151 Object o = pm.getObjectById(personID, true); 152 long startTime = System.currentTimeMillis(); 153 pm.getFetchPlan().addGroup(FetchPlan.ALL); 155 Person ret = (Person) pm.detachCopy(o); 156 return ret; 157 } finally { 158 pm.close(); 159 } 160 161 } 162 163 172 public Collection searchPerson(SearchFilter personSearchFilter, String [] fetchGroups) 173 throws ModuleException, JDOObjectNotFoundException { 174 PersistenceManager pm = this.getPersistenceManager(); 175 176 Collection persons = personSearchFilter.executeQuery(pm); 177 178 pm.getFetchPlan().clearGroups(); 180 if (fetchGroups != null) 181 pm.getFetchPlan().setGroups(Utils.array2ArrayList(fetchGroups)); 182 183 Collection result = pm.detachCopyAll(persons); 184 pm.close(); 185 186 return result; 187 } 188 189 199 public Person storePerson(Person person, boolean get, String [] fetchGroups) 200 throws ModuleException 201 { 202 PersistenceManager pm = getPersistenceManager(); 203 try { 204 return (Person) NLJDOHelper.storeJDO(pm, person, get, fetchGroups); 205 } finally { 206 pm.close(); 207 } 208 } 209 210 219 public Collection getPersons(String [] fetchGroups) throws ModuleException { 220 PersistenceManager pm = getPersistenceManager(); 222 try { 223 224 Query query = pm.newQuery(Person.class); 225 226 Collection elements = (Collection )query.execute(); 227 for (int i=0; i<fetchGroups.length; i++) 228 pm.getFetchPlan().addGroup(fetchGroups[i]); 229 230 long time = System.currentTimeMillis(); 231 Collection result = pm.detachCopyAll(elements); 232 time = System.currentTimeMillis() - time; 233 LOGGER.info("Detach of "+result.size()+" Persons took "+((double)time / (double)1000)); 234 return result; 235 } 236 finally { 237 pm.close(); 238 } 239 } 240 }
| Popular Tags
|