KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > speedo > j2eedo > bo > DepartmentFactory


1 /*
2  * Speedo: an implementation of JDO compliant personality on top of JORM generic
3  * I/O sub-system.
4  * Copyright (C) 2001-2004 France Telecom R&D
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  * Release: 1.0
21  *
22  * Created on 15 mars 2004
23  * @author fmillevi@yahoo.com
24  *
25  */

26 package org.objectweb.speedo.j2eedo.bo;
27
28 import java.util.Calendar JavaDoc;
29 import java.util.Iterator JavaDoc;
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 /**
45  * This class define the Java class to methods used to handle department
46  * @author fmillevi@yahoo.com
47  */

48 public class DepartmentFactory {
49     static Logger logger = null;
50     private StringBuffer JavaDoc outStr;
51     private PersistenceManager pm;
52     /**
53      * the constant <code>MIN_EMPLOYEE_PER_DEPARTMENT</code> defines
54      * the minimum employee number to be created per department
55      */

56     public static final int MIN_EMPLOYEE_PER_DEPARTMENT = 10;
57     /**
58      * the constant <code>MAX_EMPLOYEE_PER_DEPARTMENT</code> defines
59      * the maximum employee number to be created per department
60      */

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     /**
68      * Initializes the StringBuffer used to store the treatment result
69      * @param outStr The outStr to set.
70      */

71     public void setOutStr(StringBuffer JavaDoc outStr) {
72         this.outStr = outStr;
73     }
74
75     /**
76      * Initializes the JDO persistence manager
77      * @param pm The pm to set.
78      */

79     public void setPm(PersistenceManager pm) {
80         this.pm = pm;
81     }
82
83     /**
84      * Create a new department and it's employees
85      * @throws JDOException if error occurs while creating the deparment
86      * @throws Exception
87      */

88     public void newDepartmentWithEmployees(PollsSynchronizations pollsSync) throws JDOException, Exception JavaDoc {
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 JavaDoc 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     /**
130      * Gets the department definition
131      * @throws JDOException if error occurs while getting the deparment
132      * @throws Exception
133      */

134     public void getDepartment() throws JDOException, Exception JavaDoc {
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     /**
147      * Affects all employees and project to an other department and removes the former department
148      * @throws JDOException
149      * @throws Exception
150      */

151     public void mergeDepartment(PollsSynchronizations pollsSync) throws JDOException, Exception JavaDoc {
152         long initialDeptid =
153             DatabaseImpl.getDepartmentIdFromPool();
154         long destinationDeptid =
155             DatabaseImpl.getDepartmentIdFromPool();
156         while (initialDeptid == destinationDeptid)
157             destinationDeptid =
158                 DatabaseImpl.getDepartmentIdFromPool();
159
160         // remove id from static pool to avoid other threatment to use it
161
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         // move projects
186
Iterator JavaDoc 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         // move employees
195
Iterator JavaDoc 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         // remove department
204
pm.deletePersistent(initialDepartment);
205     }
206
207     /**
208      * Creates a new department, selects an existing department and shifts to the
209      * new department :
210      * <ul><li>50% of the projects</li><li>50% of the employees</li></ul>
211      * @throws JDOException
212      * @throws Exception
213      */

214     public void splitDepartment(PollsSynchronizations pollsSync) throws JDOException, Exception JavaDoc {
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         // move projects
240
Iterator JavaDoc 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         // move employees
251
Iterator JavaDoc 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     /**
263      * Selects a department, gets the list of employees. The first returned employee is the
264      * new manager for the following employees
265      * @throws JDOException
266      * @throws Exception
267      */

268     public void setManagerForADepartment() throws JDOException, Exception JavaDoc {
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         // the first person from a department is the boss;
282
Iterator JavaDoc 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