1 22 package org.objectweb.speedo.j2eedo.bo; 23 24 import java.math.BigDecimal ; 25 import java.util.Collection ; 26 import java.util.Iterator ; 27 28 import javax.jdo.JDOException; 29 import javax.jdo.PersistenceManager; 30 import javax.jdo.Query; 31 32 import org.objectweb.speedo.Alea; 33 import org.objectweb.speedo.j2eedo.database.Department; 34 import org.objectweb.speedo.j2eedo.database.Employee; 35 import org.objectweb.speedo.j2eedo.database.Project; 36 import org.objectweb.util.monolog.Monolog; 37 import org.objectweb.util.monolog.api.BasicLevel; 38 import org.objectweb.util.monolog.api.Logger; 39 import org.objectweb.util.monolog.api.LoggerFactory; 40 41 45 public class ProjectFactory { 46 static Logger logger = null; 47 private StringBuffer outStr; 48 private PersistenceManager pm; 49 53 public static final int MIN_MEMBER_PER_PROJECT = 5; 54 58 public static final int MAX_MEMBER_PER_PROJECT = 20; 59 static { 60 LoggerFactory lf = Monolog.initialize(); 61 logger = lf.getLogger(DatabaseImpl.class.getName()); 62 } 63 64 68 public void setOutStr(StringBuffer outStr) { 69 this.outStr = outStr; 70 } 71 72 76 public void setPm(PersistenceManager pm) { 77 this.pm = pm; 78 } 79 80 87 public void getProjects() throws JDOException, Exception { 88 int testId = Alea.rand(0, 1); 89 switch (testId) { 90 case 0 : 91 this.queryProject(); 92 break; 93 case 1 : 94 this.queryProjectsByMembers(); 95 break; 96 } 97 } 98 99 private void queryProject() throws JDOException, Exception { 100 long id = DatabaseImpl.getProjectIdFromPool(); 101 logger.log(BasicLevel.DEBUG, "Query project having id :" + id); 102 Query query = pm.newQuery(Project.class); 103 query.declareParameters("long aId"); 104 query.setFilter("(proid==aId)"); 105 Collection col = (Collection ) query.execute(new Long (id)); 106 Iterator iter = col.iterator(); 107 Project p = null; 108 try { 109 if (!iter.hasNext()) { 110 throw new Exception ("No project found with the id: " + id); 111 } 112 p = (Project) iter.next(); 113 if (p.getProid() != id) 114 throw new Exception ("Bad project found (the id is different, expected: " + id + ", found:" + p.getProid()); 115 if (iter.hasNext()) 116 throw new Exception ("More than oen project corresponding to the id: " + id); 117 } finally { 118 query.closeAll(); 119 } 120 outStr.append("\nProject name:").append(p.getName()); 121 outStr.append(" budget:").append(p.getBudget()); 122 } 123 124 private void queryProjectsByMembers() throws JDOException, Exception { 125 long id1 = DatabaseImpl.getEmployeeIdFromPool(); 126 long id2 = DatabaseImpl.getEmployeeIdFromPool(); 127 while (id2 == id1) { 128 id2 = DatabaseImpl.getEmployeeIdFromPool(); 129 } 130 logger.log(BasicLevel.DEBUG, 131 "Query project having members :" + id1 + " and " + id2); 132 outStr.append("\nThe project for the employee "); 133 outStr.append(id1).append(" and ").append(id2); 134 135 Query query = pm.newQuery(Project.class); 136 query.declareParameters("long id1, long id2"); 137 query.declareVariables("org.objectweb.speedo.j2eedo.database.Employee e"); 138 query.setFilter("((members.contains(e)) && ((e.empid==id1) || (e.empid==id2)) )"); 139 140 Collection col = (Collection ) query.execute(new Long (id1), new Long (id2)); 141 Iterator iter = col.iterator(); 142 try { 143 while (iter.hasNext()) { 144 Project p = (Project) iter.next(); 145 outStr.append(p.getAsString()); 146 } 147 } finally { 148 query.closeAll(); 149 } 150 151 } 152 153 158 public void getProject() throws JDOException, Exception { 159 long id = DatabaseImpl.getProjectIdFromPool(); 160 logger.log(BasicLevel.DEBUG, "Get project having id :" + id); 161 outStr.append("\nGet the project id :"); 162 outStr.append(id); 163 Project p = (Project) pm.getObjectById( 164 pm.newObjectIdInstance(Project.class, Long.toString(id)), 165 false); 166 outStr.append(p.getAsString()); 167 } 168 169 174 public void deleteProject(PollsSynchronizations pollsSync) throws JDOException, Exception { 175 long id = DatabaseImpl.getProjectIdFromPool(); 176 pollsSync.removeFromPool(DatabaseImpl.poolOfProjectId, id); 178 pm.setUserObject( pollsSync); 179 logger.log(BasicLevel.DEBUG, "Delete project having id :" + id); 180 outStr.append("\nRemove project :").append(id); 181 pm.deletePersistent( 182 (Project) pm.getObjectById( 183 pm.newObjectIdInstance(Project.class, Long.toString(id)), 184 false)); 185 } 186 187 193 public void splitProject(PollsSynchronizations pollsSync) throws JDOException, Exception { 194 long id = DatabaseImpl.getProjectIdFromPool(); 195 logger.log(BasicLevel.DEBUG, "Split project having id :" + id); 196 outStr.append("\nSplit project :").append(id); 197 198 Project p = (Project) pm.getObjectById( 199 pm.newObjectIdInstance( 200 Project.class, 201 Long.toString(id)), 202 false); 203 Project newP = new Project(p.getName() + "-splited", p.getDepartment()); 204 newP.setBudget(BigDecimal.valueOf(Alea.rand(1, 150000))); 205 newP.setMembers(p.getMembers()); 206 207 pm.makePersistent(newP); 208 pollsSync.addInPool(DatabaseImpl.poolOfProjectId, newP.getProid()); 209 pm.setUserObject( pollsSync); 210 211 this.outStr.append("\nAdd project : "); 212 this.outStr.append(p.getProid()); 213 } 214 215 221 public void newProjectWithEmployees(PollsSynchronizations pollsSync) throws JDOException, Exception { 222 Department d = 223 (Department) pm.getObjectById( 224 pm.newObjectIdInstance( 225 Department.class, 226 Long.toString( 227 DatabaseImpl.getDepartmentIdFromPool())), 228 false); 229 230 Project p = new Project("Name-" + Alea.randomstring(4, 5), d); 231 p.setBudget(BigDecimal.valueOf(Alea.rand(1, 150000))); 232 pm.makePersistent(p); 233 234 logger.log(BasicLevel.DEBUG, "Create a new project id :" + p.getProid()); 235 outStr.append("\nAdd project : ").append(p.getProid()); 236 pollsSync.addInPool(DatabaseImpl.poolOfProjectId, p.getProid()); 237 pm.setUserObject( pollsSync); 238 239 int nbEmployees = Alea.rand(MIN_MEMBER_PER_PROJECT, MAX_MEMBER_PER_PROJECT); 240 Employee e = null; 241 for (int i = 0; i < nbEmployees; i++) { 242 e = (Employee) pm.getObjectById( 243 pm.newObjectIdInstance( 244 Employee.class, 245 Long.toString( 246 DatabaseImpl.getEmployeeIdFromPool())), 247 false); 248 if (!p.addMember(e)) { 249 i--; 250 } 251 } 252 outStr.append(" with ").append(nbEmployees).append(" members"); 253 } 254 } 255 | Popular Tags |