1 26 package org.objectweb.speedo.j2eedo.bo; 27 28 import java.util.Calendar ; 29 import java.util.Iterator ; 30 31 import javax.jdo.JDOException; 32 import javax.jdo.PersistenceManager; 33 34 import org.objectweb.speedo.Alea; 35 import org.objectweb.speedo.j2eedo.database.Address; 36 import org.objectweb.speedo.j2eedo.database.Department; 37 import org.objectweb.speedo.j2eedo.database.Employee; 38 import org.objectweb.speedo.j2eedo.database.Project; 39 import org.objectweb.util.monolog.Monolog; 40 import org.objectweb.util.monolog.api.BasicLevel; 41 import org.objectweb.util.monolog.api.Logger; 42 import org.objectweb.util.monolog.api.LoggerFactory; 43 44 48 public class DepartmentFactory { 49 static Logger logger = null; 50 private StringBuffer outStr; 51 private PersistenceManager pm; 52 56 public static final int MIN_EMPLOYEE_PER_DEPARTMENT = 10; 57 61 public static final int MAX_EMPLOYEE_PER_DEPARTMENT = 70; 62 static { 63 LoggerFactory lf = Monolog.initialize(); 64 logger = lf.getLogger(DatabaseImpl.class.getName()); 65 } 66 67 71 public void setOutStr(StringBuffer outStr) { 72 this.outStr = outStr; 73 } 74 75 79 public void setPm(PersistenceManager pm) { 80 this.pm = pm; 81 } 82 83 88 public void newDepartmentWithEmployees(PollsSynchronizations pollsSync) throws JDOException, Exception { 89 Department d = new Department("Dept-" + Alea.randomstring(4, 9)); 90 pm.makePersistent(d); 91 logger.log( 92 BasicLevel.DEBUG, 93 "Create a new department id :" + d.getDeptid()); 94 int nbEmployees = Alea.rand(MIN_EMPLOYEE_PER_DEPARTMENT, MAX_EMPLOYEE_PER_DEPARTMENT); 95 Employee boss = null; 96 for (int i = 0; i < nbEmployees; i++) { 97 Calendar cal = Calendar.getInstance(); 98 cal.set(Alea.rand(1900, 2004), Alea.rand(1, 12), Alea.rand(1, 28)); 99 Employee e = 100 new Employee( 101 "First-" + Alea.randomstring(4, 5), 102 "Last-" + Alea.randomstring(8, 10), 103 cal.getTime(), 104 d); 105 e.setSalary(Alea.rand(5000, 45000)); 106 Address a = new Address(); 107 a.setCity("City-" + Alea.randomstring(4, 5)); 108 a.setStreet("Street-" + Alea.randomstring(4, 5)); 109 a.setState("State-" + Alea.randomstring(4, 5)); 110 a.setZipcode("ZIP-" + Alea.randomstring(2, 3)); 111 e.setAddress(a); 112 pm.makePersistent(e); 113 if (0 == i) { 114 boss = e; 115 } else { 116 e.setManager(boss); 117 } 118 pollsSync.addInPool(DatabaseImpl.poolOfEmployeeId, e.getEmpid()); 119 } 120 pollsSync.addInPool(DatabaseImpl.poolOfDepartmentId, d.getDeptid()); 121 pm.setUserObject(pollsSync); 122 this.outStr.append("\n\tAdd new Department : "); 123 this.outStr.append(d.getDeptid()); 124 this.outStr.append(" with "); 125 this.outStr.append(nbEmployees); 126 this.outStr.append(" employees"); 127 } 128 129 134 public void getDepartment() throws JDOException, Exception { 135 long id = DatabaseImpl.getDepartmentIdFromPool(); 136 logger.log(BasicLevel.DEBUG, "Get Department having id :" + id); 137 this.outStr.append("\nGet the department id :"); 138 this.outStr.append(id); 139 Department d = 140 (Department) pm.getObjectById( 141 pm.newObjectIdInstance(Department.class, Long.toString(id)), 142 false); 143 this.outStr.append(d.getAsString()); 144 } 145 146 151 public void mergeDepartment(PollsSynchronizations pollsSync) throws JDOException, Exception { 152 long initialDeptid = 153 DatabaseImpl.getDepartmentIdFromPool(); 154 long destinationDeptid = 155 DatabaseImpl.getDepartmentIdFromPool(); 156 while (initialDeptid == destinationDeptid) 157 destinationDeptid = 158 DatabaseImpl.getDepartmentIdFromPool(); 159 160 pollsSync.removeFromPool(DatabaseImpl.poolOfDepartmentId, initialDeptid); 162 pm.setUserObject(pollsSync); 163 164 this.outStr.append("\nRemove Dept : " + initialDeptid); 165 166 logger.log( 167 BasicLevel.DEBUG, 168 "Delete the department id :" + initialDeptid); 169 170 Department initialDepartment = 171 (Department) pm.getObjectById( 172 pm.newObjectIdInstance( 173 Department.class, 174 Long.toString(initialDeptid)), 175 false); 176 177 this.outStr.append("\nMerge with Dept : " + destinationDeptid); 178 Department destinationDepartment = 179 (Department) pm.getObjectById( 180 pm.newObjectIdInstance( 181 Department.class, 182 Long.toString(destinationDeptid)), 183 false); 184 185 Iterator projects = initialDepartment.getProjects().iterator(); 187 Project p = null; 188 while (projects.hasNext()) { 189 p = (Project) projects.next(); 190 p.setDepartment(destinationDepartment); 191 this.outStr.append("\nMove project : " + p.getId()); 192 } 193 194 Iterator persons = initialDepartment.getEmployees().iterator(); 196 Employee e = null; 197 while (persons.hasNext()) { 198 e = (Employee) persons.next(); 199 e.setDepartment(destinationDepartment); 200 this.outStr.append("\nAffect employee : " + e.getId()); 201 } 202 203 pm.deletePersistent(initialDepartment); 205 } 206 207 214 public void splitDepartment(PollsSynchronizations pollsSync) throws JDOException, Exception { 215 long initialDeptid = 216 DatabaseImpl.getDepartmentIdFromPool(); 217 218 this.outStr.append("\nSplit Dept : " + initialDeptid); 219 220 logger.log( 221 BasicLevel.DEBUG, 222 "Split the department id :" + initialDeptid); 223 224 Department initialDepartment = 225 (Department) pm.getObjectById( 226 pm.newObjectIdInstance( 227 Department.class, 228 Long.toString(initialDeptid)), 229 false); 230 231 Department d = new Department(initialDepartment.getName()+"-splited"); 232 pm.makePersistent(d); 233 234 pollsSync.addInPool(DatabaseImpl.poolOfDepartmentId, d.getDeptid()); 235 pm.setUserObject(pollsSync); 236 237 this.outStr.append("\n with Dept : " + d.getDeptid() ); 238 239 Iterator projects = initialDepartment.getProjects().iterator(); 241 Project p = null; 242 int nbProject = Math.round(initialDepartment.getProjects().size()/2); 243 while (projects.hasNext() && nbProject>0) { 244 nbProject--; 245 p = (Project) projects.next(); 246 p.setDepartment(d); 247 this.outStr.append("\nMove project : " + p.getId()); 248 } 249 250 Iterator persons = initialDepartment.getEmployees().iterator(); 252 Employee e = null; 253 int nbEmployee = Math.round(initialDepartment.getEmployees().size()/2); 254 while (persons.hasNext() && nbEmployee >0) { 255 nbEmployee--; 256 e = (Employee) persons.next(); 257 e.setDepartment(d); 258 this.outStr.append("\nAffect employee : " + e.getId()); 259 } 260 } 261 262 268 public void setManagerForADepartment() throws JDOException, Exception { 269 Department d = 270 (Department) pm.getObjectById( 271 pm.newObjectIdInstance( 272 Department.class, 273 Long.toString( 274 DatabaseImpl.getDepartmentIdFromPool())), 275 false); 276 277 logger.log( 278 BasicLevel.DEBUG, 279 "Update manager for the department having id :" + d.getDeptid()); 280 281 Iterator persons = d.getEmployees().iterator(); 283 Employee boss = null; 284 Employee otherEmp = null; 285 if (persons.hasNext()) { 286 boss = (Employee) persons.next(); 287 this.outStr.append("\nThe boss of the department '"); 288 this.outStr.append(boss.getDepartment().getName()); 289 this.outStr.append("' is now:"); 290 this.outStr.append(boss.getFirstname()); 291 while (persons.hasNext()) { 292 otherEmp = (Employee) persons.next(); 293 otherEmp.setManager(boss); 294 } 295 } 296 } 297 } 298 | Popular Tags |