001: /*
002: * Speedo: an implementation of JDO compliant personality on top of JORM generic
003: * I/O sub-system.
004: * Copyright (C) 2001-2004 France Telecom R&D
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: *
021: */
022: package org.objectweb.speedo.j2eedo.bo;
023:
024: import java.math.BigDecimal;
025: import java.util.Collection;
026: import java.util.Iterator;
027:
028: import javax.jdo.JDOException;
029: import javax.jdo.PersistenceManager;
030: import javax.jdo.Query;
031:
032: import org.objectweb.speedo.Alea;
033: import org.objectweb.speedo.j2eedo.database.Department;
034: import org.objectweb.speedo.j2eedo.database.Employee;
035: import org.objectweb.speedo.j2eedo.database.Project;
036: import org.objectweb.util.monolog.Monolog;
037: import org.objectweb.util.monolog.api.BasicLevel;
038: import org.objectweb.util.monolog.api.Logger;
039: import org.objectweb.util.monolog.api.LoggerFactory;
040:
041: /**
042: * This class define the Java class to methods used to handle employee
043: * @author fmillevi@yahoo.com
044: */
045: public class ProjectFactory {
046: static Logger logger = null;
047: /**
048: * the constant <code>MIN_MEMBER_PER_PROJECT</code> defines
049: * the minimum members number to be created per project
050: */
051: public static final int MIN_MEMBER_PER_PROJECT = 5;
052: /**
053: * the constant <code>MAX_MEMBER_PER_PROJECT</code> defines
054: * the maximum members number to be created per project
055: */
056: public static final int MAX_MEMBER_PER_PROJECT = 20;
057: static {
058: LoggerFactory lf = Monolog.initialize();
059: logger = lf.getLogger(DatabaseImpl.class.getName());
060: }
061:
062: /**
063: * Gets some projects. 2 queries are defines
064: * <ul><li>Get a project by it's primary key</li>
065: * <li>Get projects having one of 2 selected employees</li></ul>
066: * @throws JDOException
067: * @throws Exception
068: */
069: public void getProjects(StringBuffer outStr, PersistenceManager pm) {
070: int testId = Alea.rand(0, 1);
071: switch (testId) {
072: case 0:
073: queryProject(outStr, pm);
074: break;
075: case 1:
076: queryProjectsByMembers(outStr, pm);
077: break;
078: }
079: }
080:
081: private void queryProject(StringBuffer outStr, PersistenceManager pm) {
082: long id = DatabaseImpl.getProjectIdFromPool();
083: logger.log(BasicLevel.DEBUG, "Query project having id :" + id);
084: Query query = pm.newQuery(Project.class);
085: query.declareParameters("long aId");
086: query.setFilter("(proid==aId)");
087: Collection col = (Collection) query.execute(new Long(id));
088: Iterator iter = col.iterator();
089: Project p = null;
090: try {
091: if (!iter.hasNext()) {
092: throw new RuntimeException(
093: "No project found with the id: " + id);
094: }
095: p = (Project) iter.next();
096: if (p.getProid() != id)
097: throw new RuntimeException(
098: "Bad project found (the id is different, expected: "
099: + id + ", found:" + p.getProid());
100: if (iter.hasNext())
101: throw new RuntimeException(
102: "More than one project corresponding to the id: "
103: + id);
104: } finally {
105: query.closeAll();
106: }
107: outStr.append("\nGet project " + id);
108: outStr.append(", name:").append(p.getName());
109: outStr.append(", budget:").append(p.getBudget());
110: }
111:
112: private void queryProjectsByMembers(StringBuffer outStr,
113: PersistenceManager pm) {
114: long id1 = DatabaseImpl.getEmployeeIdFromPool();
115: long id2 = DatabaseImpl.getEmployeeIdFromPool();
116: while (id2 == id1) {
117: id2 = DatabaseImpl.getEmployeeIdFromPool();
118: }
119: logger.log(BasicLevel.DEBUG, "Query project having members :"
120: + id1 + " and " + id2);
121: outStr.append("\nThe project for the employee ");
122: outStr.append(id1).append(" and ").append(id2);
123:
124: Query query = pm.newQuery(Project.class);
125: query.declareParameters("long id1, long id2");
126: query
127: .declareVariables("org.objectweb.speedo.j2eedo.database.Employee e");
128: query
129: .setFilter("((members.contains(e)) && ((e.empid==id1) || (e.empid==id2)) )");
130:
131: Collection col = (Collection) query.execute(new Long(id1),
132: new Long(id2));
133: Iterator iter = col.iterator();
134: try {
135: while (iter.hasNext()) {
136: Project p = (Project) iter.next();
137: outStr.append(p.getAsString());
138: }
139: } finally {
140: query.closeAll();
141: }
142:
143: }
144:
145: /**
146: * Gets a project
147: * @throws JDOException
148: * @throws Exception
149: */
150: public void getProject(StringBuffer outStr, PersistenceManager pm) {
151: long id = DatabaseImpl.getProjectIdFromPool();
152: logger.log(BasicLevel.DEBUG, "Get project having id :" + id);
153: outStr.append("\nGet the project id :").append(id);
154: Project p = (Project) pm.getObjectById(pm.newObjectIdInstance(
155: Project.class, Long.toString(id)), false);
156: outStr.append(p.getAsString());
157: }
158:
159: /**
160: * Deletes a project
161: * @throws JDOException
162: * @throws Exception
163: */
164: public void deleteProject(PollsSynchronizations pollsSync,
165: StringBuffer outStr, PersistenceManager pm) {
166: long id = DatabaseImpl.getProjectIdFromPool();
167: // remove id from static pool to avoid other threatment to use it
168: pollsSync.removeFromPool(DatabaseImpl.poolOfProjectId, id);
169: pm.setUserObject(pollsSync);
170: logger.log(BasicLevel.DEBUG, "Delete project having id :" + id);
171: outStr.append("\nRemove project :").append(id);
172: pm.deletePersistent((Project) pm.getObjectById(pm
173: .newObjectIdInstance(Project.class, Long.toString(id)),
174: false));
175: }
176:
177: /**
178: * Selects an existing project, creates a new project and duplicates member list to
179: * the new project
180: * @throws JDOException
181: * @throws Exception
182: */
183: public void splitProject(PollsSynchronizations pollsSync,
184: StringBuffer outStr, PersistenceManager pm) {
185: long id = DatabaseImpl.getProjectIdFromPool();
186: logger.log(BasicLevel.DEBUG, "Split project having id :" + id);
187: outStr.append("\nSplit project :").append(id);
188: Project p = (Project) pm.getObjectById(pm.newObjectIdInstance(
189: Project.class, Long.toString(id)), false);
190: Project newP = new Project(p.getName() + "-splited", p
191: .getDepartment());
192: newP.setBudget(BigDecimal.valueOf(Alea.rand(1, 150000)));
193: newP.setMembers(p.getMembers());
194: pm.makePersistent(newP);
195: outStr.append(", new project is: ").append(newP.getProid());
196: pollsSync.addInPool(DatabaseImpl.poolOfProjectId, newP
197: .getProid());
198: pm.setUserObject(pollsSync);
199: }
200:
201: /**
202: * Selects an existing department, create a new project and affects some
203: * existing employees
204: * @throws JDOException
205: * @throws Exception
206: */
207: public void newProjectWithEmployees(
208: PollsSynchronizations pollsSync, StringBuffer outStr,
209: PersistenceManager pm) {
210: outStr.append("\nCreate new project with its members");
211: Department d = (Department) pm.getObjectById(pm
212: .newObjectIdInstance(Department.class, Long
213: .toString(DatabaseImpl
214: .getDepartmentIdFromPool())), false);
215:
216: Project p = new Project("Name-" + Alea.randomstring(4, 5), d);
217: p.setBudget(BigDecimal.valueOf(Alea.rand(1, 150000)));
218: pm.makePersistent(p);
219:
220: logger.log(BasicLevel.DEBUG, "Create a new project id :"
221: + p.getProid());
222: outStr.append("\nAdd project : ").append(p.getProid());
223: pollsSync.addInPool(DatabaseImpl.poolOfProjectId, p.getProid());
224: pm.setUserObject(pollsSync);
225:
226: int nbEmployees = Alea.rand(MIN_MEMBER_PER_PROJECT,
227: MAX_MEMBER_PER_PROJECT);
228: Employee e = null;
229: for (int i = 0; i < nbEmployees; i++) {
230: e = (Employee) pm.getObjectById(pm.newObjectIdInstance(
231: Employee.class, Long.toString(DatabaseImpl
232: .getEmployeeIdFromPool())), false);
233: if (!p.addMember(e)) {
234: i--;
235: }
236: }
237: outStr.append(" with ").append(nbEmployees).append(" members");
238: }
239: }
|