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: * Release: 1.0
021: *
022: * Created on 15 mars 2004
023: * @author fmillevi@yahoo.com
024: *
025: */
026: package org.objectweb.speedo.j2eedo.bo;
027:
028: import java.util.Calendar;
029: import java.util.Iterator;
030:
031: import javax.jdo.JDOException;
032: import javax.jdo.PersistenceManager;
033:
034: import org.objectweb.speedo.Alea;
035: import org.objectweb.speedo.j2eedo.database.Address;
036: import org.objectweb.speedo.j2eedo.database.Department;
037: import org.objectweb.speedo.j2eedo.database.Employee;
038: import org.objectweb.speedo.j2eedo.database.Project;
039: import org.objectweb.util.monolog.Monolog;
040: import org.objectweb.util.monolog.api.BasicLevel;
041: import org.objectweb.util.monolog.api.Logger;
042: import org.objectweb.util.monolog.api.LoggerFactory;
043:
044: /**
045: * This class define the Java class to methods used to handle department
046: * @author fmillevi@yahoo.com
047: */
048: public class DepartmentFactory {
049: static Logger logger = Monolog.initialize().getLogger(
050: DatabaseImpl.class.getName());
051: /**
052: * the constant <code>MIN_EMPLOYEE_PER_DEPARTMENT</code> defines
053: * the minimum employee number to be created per department
054: */
055: public static final int MIN_EMPLOYEE_PER_DEPARTMENT = 10;
056: /**
057: * the constant <code>MAX_EMPLOYEE_PER_DEPARTMENT</code> defines
058: * the maximum employee number to be created per department
059: */
060: public static final int MAX_EMPLOYEE_PER_DEPARTMENT = 70;
061:
062: /**
063: * Create a new department and it's employees
064: * @throws JDOException if error occurs while creating the deparment
065: * @throws Exception
066: */
067: public void newDepartmentWithEmployees(
068: PollsSynchronizations pollsSync, StringBuffer outStr,
069: PersistenceManager pm) {
070: outStr.append("\nCreate new deparment with its employees");
071: Department d = new Department("Dept-" + Alea.randomstring(4, 9));
072: pm.makePersistent(d);
073: pollsSync.addInPool(DatabaseImpl.poolOfDepartmentId, d
074: .getDeptid());
075: logger.log(BasicLevel.DEBUG, "Create a new department id :"
076: + d.getDeptid());
077: int nbEmployees = Alea.rand(MIN_EMPLOYEE_PER_DEPARTMENT,
078: MAX_EMPLOYEE_PER_DEPARTMENT);
079: outStr.append("\n\tAdd new Department : ")
080: .append(d.getDeptid());
081: outStr.append(" with ").append(nbEmployees)
082: .append(" employees");
083: Employee boss = null;
084: for (int i = 0; i < nbEmployees; i++) {
085: Calendar cal = Calendar.getInstance();
086: cal.set(Alea.rand(1900, 2004), Alea.rand(1, 12), Alea.rand(
087: 1, 28));
088: Employee e = new Employee("First-"
089: + Alea.randomstring(4, 5), "Last-"
090: + Alea.randomstring(8, 10), cal.getTime(), d);
091: e.setSalary(Alea.rand(5000, 45000));
092: Address a = new Address();
093: a.setCity("City-" + Alea.randomstring(4, 5));
094: a.setStreet("Street-" + Alea.randomstring(4, 5));
095: a.setState("StateItf-" + Alea.randomstring(4, 5));
096: a.setZipcode("ZIP-" + Alea.randomstring(2, 3));
097: e.setAddress(a);
098: pm.makePersistent(e);
099: if (0 == i) {
100: boss = e;
101: outStr.append("\nThe boss: ");
102: } else {
103: e.setManager(boss);
104: }
105: outStr.append(e.getAsString());
106: pollsSync.addInPool(DatabaseImpl.poolOfEmployeeId, e
107: .getEmpid());
108:
109: }
110: pm.setUserObject(pollsSync);
111: }
112:
113: /**
114: * Gets the department definition
115: * @throws JDOException if error occurs while getting the deparment
116: * @throws Exception
117: */
118: public void getDepartment(StringBuffer outStr, PersistenceManager pm) {
119: long id = DatabaseImpl.getDepartmentIdFromPool();
120: logger.log(BasicLevel.DEBUG, "Get Department having id :" + id);
121: outStr.append("\nGet the department id :").append(id);
122: Department d = (Department) pm.getObjectById(pm
123: .newObjectIdInstance(Department.class, Long
124: .toString(id)), false);
125: outStr.append(d.getAsString());
126: }
127:
128: /**
129: * Affects all employees and project to an other department and removes the former department
130: * @throws JDOException
131: * @throws Exception
132: */
133: public void mergeDepartment(PollsSynchronizations pollsSync,
134: StringBuffer outStr, PersistenceManager pm) {
135: long initialDeptid = DatabaseImpl.getDepartmentIdFromPool();
136: long destinationDeptid = DatabaseImpl.getDepartmentIdFromPool();
137: while (initialDeptid == destinationDeptid) {
138: destinationDeptid = DatabaseImpl.getDepartmentIdFromPool();
139: }
140:
141: // remove id from static pool to avoid other threatment to use it
142: pollsSync.removeFromPool(DatabaseImpl.poolOfDepartmentId,
143: initialDeptid);
144: pm.setUserObject(pollsSync);
145:
146: outStr.append("\nRemove Dept : " + initialDeptid);
147: logger.log(BasicLevel.DEBUG, "Delete the department id :"
148: + initialDeptid);
149:
150: Department initialDepartment = (Department) pm.getObjectById(pm
151: .newObjectIdInstance(Department.class, Long
152: .toString(initialDeptid)), false);
153:
154: outStr.append("\nMerge with Dept : " + destinationDeptid);
155: Department destinationDepartment = (Department) pm
156: .getObjectById(pm.newObjectIdInstance(Department.class,
157: Long.toString(destinationDeptid)), false);
158:
159: // move projects
160: Iterator projects = initialDepartment.getProjects().iterator();
161: Project p = null;
162: while (projects.hasNext()) {
163: p = (Project) projects.next();
164: p.setDepartment(destinationDepartment);
165: outStr.append("\nMove project : ").append(p.getId());
166: }
167:
168: // move employees
169: Iterator persons = initialDepartment.getEmployees().iterator();
170: Employee e = null;
171: while (persons.hasNext()) {
172: e = (Employee) persons.next();
173: e.setDepartment(destinationDepartment);
174: outStr.append("\nAffect employee : ").append(e.getId());
175: }
176:
177: // remove department
178: pm.deletePersistent(initialDepartment);
179: }
180:
181: /**
182: * Creates a new department, selects an existing department and shifts to the
183: * new department :
184: * <ul><li>50% of the projects</li><li>50% of the employees</li></ul>
185: * @throws JDOException
186: * @throws Exception
187: */
188: public void splitDepartment(PollsSynchronizations pollsSync,
189: StringBuffer outStr, PersistenceManager pm) {
190: long initialDeptid = DatabaseImpl.getDepartmentIdFromPool();
191: outStr.append("\nSplit Dept : " + initialDeptid);
192: logger.log(BasicLevel.DEBUG, "Split the department id :"
193: + initialDeptid);
194:
195: Department initialDepartment = (Department) pm.getObjectById(pm
196: .newObjectIdInstance(Department.class, Long
197: .toString(initialDeptid)), false);
198:
199: Department d = new Department(initialDepartment.getName()
200: + "-splited");
201: pm.makePersistent(d);
202:
203: pollsSync.addInPool(DatabaseImpl.poolOfDepartmentId, d
204: .getDeptid());
205: pm.setUserObject(pollsSync);
206:
207: outStr.append("\n with Dept : " + d.getDeptid());
208:
209: // move projects
210: Iterator projects = initialDepartment.getProjects().iterator();
211: Project p = null;
212: int nbProject = Math.round(initialDepartment.getProjects()
213: .size() / 2);
214: while (projects.hasNext() && nbProject > 0) {
215: nbProject--;
216: p = (Project) projects.next();
217: p.setDepartment(d);
218: outStr.append("\nMove project : " + p.getId());
219: }
220:
221: // move employees
222: Iterator persons = initialDepartment.getEmployees().iterator();
223: Employee e = null;
224: int nbEmployee = Math.round(initialDepartment.getEmployees()
225: .size() / 2);
226: while (persons.hasNext() && nbEmployee > 0) {
227: nbEmployee--;
228: e = (Employee) persons.next();
229: e.setDepartment(d);
230: outStr.append("\nAffect employee : " + e.getId());
231: }
232: }
233:
234: /**
235: * Selects a department, gets the list of employees. The first returned employee is the
236: * new manager for the following employees
237: * @throws JDOException
238: * @throws Exception
239: */
240: public void setManagerForADepartment(StringBuffer outStr,
241: PersistenceManager pm) {
242: outStr.append("\nUpdate the boss for a department's employees");
243: Department d = (Department) pm.getObjectById(pm
244: .newObjectIdInstance(Department.class, Long
245: .toString(DatabaseImpl
246: .getDepartmentIdFromPool())), false);
247: logger.log(BasicLevel.DEBUG,
248: "Update manager for the department having id :"
249: + d.getDeptid());
250: // the first person from a department is the boss;
251: Iterator persons = d.getEmployees().iterator();
252: Employee boss = null;
253: Employee otherEmp = null;
254: if (persons.hasNext()) {
255: boss = (Employee) persons.next();
256: outStr.append("\nThe boss of the department '");
257: outStr.append(boss.getDepartment().getName());
258: outStr.append("' is now: ").append(boss.getFirstname());
259: while (persons.hasNext()) {
260: otherEmp = (Employee) persons.next();
261: otherEmp.setManager(boss);
262: outStr.append("\nThe employee ").append(
263: otherEmp.getEmpid());
264: outStr.append(" has a new manager.");
265: }
266: }
267: }
268: }
|