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.util.Calendar;
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.Address;
034: import org.objectweb.speedo.j2eedo.database.Department;
035: import org.objectweb.speedo.j2eedo.database.Employee;
036: import org.objectweb.speedo.j2eedo.database.Project;
037: import org.objectweb.util.monolog.Monolog;
038: import org.objectweb.util.monolog.api.BasicLevel;
039: import org.objectweb.util.monolog.api.Logger;
040: import org.objectweb.util.monolog.api.LoggerFactory;
041:
042: /**
043: * This class define the Java class to methods used to handle employee
044: * @author fmillevi@yahoo.com
045: *
046: */
047: public class EmployeeFactory {
048:
049: static Logger logger = Monolog.initialize().getLogger(
050: DatabaseImpl.class.getName());
051:
052: /**
053: * Creates a new employee in one of the existing department
054: * @throws JDOException
055: * @throws Exception
056: */
057: public void newEmployee(PollsSynchronizations pollsSync,
058: StringBuffer outStr, PersistenceManager pm) {
059: long id = DatabaseImpl.getDepartmentIdFromPool();
060: Department d = (Department) pm.getObjectById(pm
061: .newObjectIdInstance(Department.class, Long
062: .toString(id)), false);
063: Calendar cal = Calendar.getInstance();
064: cal.set(Alea.rand(1900, 2004), Alea.rand(1, 12), Alea.rand(1,
065: 28));
066: Employee e = new Employee("First-" + Alea.randomstring(4, 5),
067: "Last-" + Alea.randomstring(8, 10), cal.getTime(), d);
068: e.setSalary(Alea.rand(5000, 45000));
069: Address a = new Address();
070: a.setCity("City-" + Alea.randomstring(4, 5));
071: a.setStreet("Street-" + Alea.randomstring(4, 5));
072: a.setState("StateItf-" + Alea.randomstring(4, 5));
073: a.setZipcode("ZIP-" + Alea.randomstring(2, 3));
074: e.setAddress(a);
075: pm.makePersistent(e);
076: outStr.append("\nCreate a new employee:").append(
077: e.getAsString());
078: logger.log(BasicLevel.DEBUG, "Create a new employee having id="
079: + e.getEmpid());
080: pollsSync
081: .addInPool(DatabaseImpl.poolOfEmployeeId, e.getEmpid());
082: pm.setUserObject(pollsSync);
083: }
084:
085: /**
086: * Deletes an existing employee
087: * @throws JDOException
088: * @throws Exception
089: */
090: public void deleteEmployee(PollsSynchronizations pollsSync,
091: StringBuffer outStr, PersistenceManager pm) {
092: long id = DatabaseImpl.getEmployeeIdFromPool();
093: // remove id from static pool to avoid other threatment to use it
094: pollsSync.removeFromPool(DatabaseImpl.poolOfEmployeeId, id);
095: pm.setUserObject(pollsSync);
096: outStr.append("\nRemove the employee ").append(id);
097: logger.log(BasicLevel.DEBUG, "Delete employee having id=" + id);
098: Employee e = (Employee) pm.getObjectById(
099: pm.newObjectIdInstance(Employee.class, Long
100: .toString(id)), false);
101: Address a = e.getAddress();
102: pm.deletePersistent(a);
103: pm.deletePersistent(e);
104: }
105:
106: /**
107: * Gets employee's data
108: * @throws JDOException
109: * @throws Exception
110: */
111: public void getEmployee(StringBuffer outStr, PersistenceManager pm) {
112: long id = DatabaseImpl.getEmployeeIdFromPool();
113: outStr.append("\nGet the employe id ").append(id);
114: logger.log(BasicLevel.DEBUG, "Get employee having id=" + id);
115: Employee e = (Employee) (Employee) pm.getObjectById(
116: pm.newObjectIdInstance(Employee.class, Long
117: .toString(id)), false);
118: outStr.append(e.getAsString());
119: }
120:
121: /**
122: * Gets some employees. 4 queries are defines
123: * <ul><li>Get an employee by it's primary key</li>
124: * <li>Get employees having it's id between two values</li>
125: * <li>Get employees having the same manager</li>
126: * <li>Get employees member of one of 2 selected projects</li></ul>
127: * @throws JDOException
128: * @throws Exception
129: */
130: public void getEmployees(StringBuffer outStr, PersistenceManager pm) {
131: int testId = Alea.rand(0, 3);
132: switch (testId) {
133: case 0:
134: queryEmployee(outStr, pm);
135: break;
136: case 1:
137: queryEmployeeArrayParameters(outStr, pm);
138: break;
139: case 2:
140: queryEmployeeByManager(outStr, pm);
141: break;
142: case 3:
143: queryEmployeeOrderHiredateByProject(outStr, pm);
144: break;
145: }
146: }
147:
148: /**
149: * Increases an employee' salary
150: * @throws JDOException
151: * @throws Exception
152: */
153: public void increaseSalary(StringBuffer outStr,
154: PersistenceManager pm) {
155: long id = DatabaseImpl.getEmployeeIdFromPool();
156: outStr.append("\nIncrease salary for the employe ").append(id);
157: logger.log(BasicLevel.DEBUG,
158: "Increase the salary for the employee having id=" + id);
159: Employee e = (Employee) pm.getObjectById(
160: pm.newObjectIdInstance(Employee.class, Long
161: .toString(id)), false);
162: e.setSalary(e.getSalary() + 1);
163: }
164:
165: private void queryEmployee(StringBuffer outStr,
166: PersistenceManager pm) {
167: long id = DatabaseImpl.getEmployeeIdFromPool();
168: logger.log(BasicLevel.DEBUG, "Query employee having id=" + id);
169: Query query = pm.newQuery(Employee.class);
170: query.declareParameters("long aId");
171: query.setFilter("(empid==aId)");
172: try {
173: Collection col = (Collection) query.execute(new Long(id));
174: Iterator iter = col.iterator();
175: if (!iter.hasNext())
176: throw new RuntimeException(
177: "TestInheritanceBasic query on employee does not return any row");
178: Employee e = (Employee) iter.next();
179: if (e.getEmpid() != id)
180: throw new RuntimeException(
181: "TestInheritanceBasic query on employee returns an other row");
182: if (iter.hasNext())
183: throw new RuntimeException(
184: "TestInheritanceBasic query on a employee returns to many rows");
185: outStr.append("\nGet Employee").append(id);
186: outStr.append(", full name ").append(e.getFirstname());
187: outStr.append(" ").append(e.getLastname());
188: } finally {
189: query.closeAll();
190: }
191: }
192:
193: private void queryEmployeeOrderHiredateByProject(
194: StringBuffer outStr, PersistenceManager pm) {
195: Project p = (Project) pm.getObjectById(pm.newObjectIdInstance(
196: Project.class, Long.toString(DatabaseImpl
197: .getProjectIdFromPool())), false);
198: outStr.append("\nLook for members of the project ");
199: outStr.append(p.getName());
200: logger.log(BasicLevel.DEBUG,
201: "Query employee member of the project=" + p.getName());
202: Query query = pm.newQuery(Employee.class);
203: query
204: .declareParameters("org.objectweb.speedo.j2eedo.database.Project p");
205: query.setFilter("(projects.contains(p))");
206: query.setOrdering("hiredate ascending, salary descending");
207: try {
208: Collection col = (Collection) query.execute(p);
209: Iterator iter = col.iterator();
210: if (!iter.hasNext())
211: throw new RuntimeException(
212: "Query on employee does not return any row");
213: Employee e = null;
214: while (iter.hasNext()) {
215: e = (Employee) iter.next();
216: outStr.append("\n\tEmployee name:");
217: outStr.append(e.getFirstname());
218: outStr.append(" ");
219: outStr.append(e.getLastname());
220: }
221: } finally {
222: query.closeAll();
223: }
224: }
225:
226: private void queryEmployeeArrayParameters(StringBuffer outStr,
227: PersistenceManager pm) {
228: long idMin = DatabaseImpl.getEmployeeIdFromPool();
229: long idMax = DatabaseImpl.getEmployeeIdFromPool();
230: while (idMin == idMax) {
231: idMax = DatabaseImpl.getEmployeeIdFromPool();
232: }
233: if (idMin > idMax) {
234: // max < min ! inverse boundaries
235: long dummy = idMax;
236: idMax = idMin;
237: idMin = dummy;
238: }
239: outStr.append("\nLook for employees having id between ");
240: outStr.append(idMin);
241: outStr.append(" and ");
242: outStr.append(idMax);
243: logger.log(BasicLevel.DEBUG,
244: "Query employee having id between " + idMin + " and "
245: + idMax);
246:
247: Long param[] = new Long[] { new Long(idMin), new Long(idMax) };
248: Query query = pm.newQuery(Employee.class);
249: query.declareParameters("long idMin, long idMax");
250: query.setFilter("((empid>=idMin) && (empid<=idMax))");
251: query.setOrdering("empid descending");
252: try {
253: Collection col = (Collection) query.executeWithArray(param);
254: Iterator iter = col.iterator();
255: if (!iter.hasNext())
256: throw new RuntimeException(
257: "Query on employee does not return any row");
258: Employee e = (Employee) iter.next();
259: outStr.append("\n\tEmployee name (").append(e.getEmpid());
260: outStr.append("): ").append(e.getFirstname()).append(" ");
261: outStr.append(e.getLastname());
262: if (false && e.getEmpid() != idMax) {
263: throw new RuntimeException(
264: "Query on employee returns an other first row than expecting");
265: }
266: while (iter.hasNext()) {
267: e = (Employee) iter.next();
268: outStr.append("\n\tEmployee name (").append(
269: e.getEmpid());
270: outStr.append("): ").append(e.getFirstname()).append(
271: " ");
272: outStr.append(e.getLastname());
273: }
274: if (false && e.getEmpid() != idMin)
275: throw new RuntimeException(
276: "Query on employee returns an other last row than expecting");
277: } finally {
278: query.closeAll();
279: }
280: }
281:
282: private void queryEmployeeByManager(StringBuffer outStr,
283: PersistenceManager pm) {
284: Employee e = (Employee) pm.getObjectById(
285: pm
286: .newObjectIdInstance(Employee.class, Long
287: .toString(DatabaseImpl
288: .getEmployeeIdFromPool())),
289: false);
290: Employee boss = e.getManager();
291: outStr
292: .append("\nLook for employees having the same manager : ");
293: if (null != boss) {
294: outStr.append(boss.getLastname());
295: } else {
296: outStr.append("NULL");
297: }
298: logger
299: .log(BasicLevel.DEBUG,
300: "Query employee having the same manager :"
301: + ((null == boss) ? "null" : boss
302: .getLastname()));
303: Query query = pm.newQuery(Employee.class);
304: query
305: .declareParameters("org.objectweb.speedo.j2eedo.database.Employee e");
306: query.setFilter("(manager==e)");
307: try {
308: Collection col = (Collection) query.execute(boss);
309: for (Iterator iter = col.iterator(); iter.hasNext();) {
310: e = (Employee) iter.next();
311: outStr.append("\n\tEmployee name:");
312: outStr.append(e.getFirstname()).append(" ");
313: outStr.append(e.getLastname());
314: }
315: } finally {
316: query.closeAll();
317: }
318: }
319: }
|