KickJava   Java API By Example, From Geeks To Geeks.

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


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  *
21  */

22 package org.objectweb.speedo.j2eedo.bo;
23
24 import java.math.BigDecimal JavaDoc;
25 import java.util.Collection JavaDoc;
26 import java.util.Iterator JavaDoc;
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 /**
42  * This class define the Java class to methods used to handle employee
43  * @author fmillevi@yahoo.com
44  */

45 public class ProjectFactory {
46     static Logger logger = null;
47     private StringBuffer JavaDoc outStr;
48     private PersistenceManager pm;
49     /**
50      * the constant <code>MIN_MEMBER_PER_PROJECT</code> defines
51      * the minimum members number to be created per project
52      */

53     public static final int MIN_MEMBER_PER_PROJECT = 5;
54     /**
55      * the constant <code>MAX_MEMBER_PER_PROJECT</code> defines
56      * the maximum members number to be created per project
57      */

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

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

76     public void setPm(PersistenceManager pm) {
77         this.pm = pm;
78     }
79
80     /**
81      * Gets some projects. 2 queries are defines
82      * <ul><li>Get a project by it's primary key</li>
83      * <li>Get projects having one of 2 selected employees</li></ul>
84      * @throws JDOException
85      * @throws Exception
86      */

87     public void getProjects() throws JDOException, Exception JavaDoc {
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 JavaDoc {
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 JavaDoc col = (Collection JavaDoc) query.execute(new Long JavaDoc(id));
106         Iterator JavaDoc iter = col.iterator();
107         Project p = null;
108         try {
109             if (!iter.hasNext()) {
110                 throw new Exception JavaDoc("No project found with the id: " + id);
111             }
112             p = (Project) iter.next();
113             if (p.getProid() != id)
114                 throw new Exception JavaDoc("Bad project found (the id is different, expected: " + id + ", found:" + p.getProid());
115             if (iter.hasNext())
116                 throw new Exception JavaDoc("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 JavaDoc {
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 JavaDoc col = (Collection JavaDoc) query.execute(new Long JavaDoc(id1), new Long JavaDoc(id2));
141         Iterator JavaDoc 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     /**
154      * Gets a project
155      * @throws JDOException
156      * @throws Exception
157      */

158     public void getProject() throws JDOException, Exception JavaDoc {
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     /**
170      * Deletes a project
171      * @throws JDOException
172      * @throws Exception
173      */

174     public void deleteProject(PollsSynchronizations pollsSync) throws JDOException, Exception JavaDoc {
175         long id = DatabaseImpl.getProjectIdFromPool();
176         // remove id from static pool to avoid other threatment to use it
177
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     /**
188      * Selects an existing project, creates a new project and duplicates member list to
189      * the new project
190      * @throws JDOException
191      * @throws Exception
192      */

193     public void splitProject(PollsSynchronizations pollsSync) throws JDOException, Exception JavaDoc {
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     /**
216      * Selects an existing department, create a new project and affects some
217      * existing employees
218      * @throws JDOException
219      * @throws Exception
220      */

221     public void newProjectWithEmployees(PollsSynchronizations pollsSync) throws JDOException, Exception JavaDoc {
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