KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > cowsultants > itracker > ejb > beans > session > ProjectHandlerBean


1 /*
2  * This software was designed and created by Jason Carroll.
3  * Copyright (c) 2002, 2003, 2004 Jason Carroll.
4  * The author can be reached at jcarroll@cowsultants.com
5  * ITracker website: http://www.cowsultants.com
6  * ITracker forums: http://www.cowsultants.com/phpBB/index.php
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it only under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  */

18
19 package cowsultants.itracker.ejb.beans.session;
20
21 import java.util.*;
22 import javax.ejb.CreateException JavaDoc;
23 import javax.ejb.FinderException JavaDoc;
24 import javax.ejb.RemoveException JavaDoc;
25 import javax.ejb.SessionBean JavaDoc;
26 import javax.ejb.SessionContext JavaDoc;
27 import javax.naming.*;
28 import javax.rmi.*;
29
30 import cowsultants.itracker.ejb.beans.entity.*;
31 import cowsultants.itracker.ejb.client.interfaces.*;
32 import cowsultants.itracker.ejb.client.models.*;
33 import cowsultants.itracker.ejb.client.util.Logger;
34
35
36 public class ProjectHandlerBean implements SessionBean JavaDoc {
37     ComponentLocalHome cHome = null;
38     CustomFieldLocalHome cfHome = null;
39     IDGeneratorHome idHome = null;
40     ProjectLocalHome pHome = null;
41     UserLocalHome uHome = null;
42     VersionLocalHome vHome = null;
43
44     public ProjectModel getProject(Integer JavaDoc projectId) {
45         try {
46             ProjectLocal project = pHome.findByPrimaryKey(projectId);
47             return project.getModel();
48         } catch(FinderException JavaDoc fe) {
49         }
50         return null;
51     }
52
53     public ProjectModel[] getAllProjects() {
54         int i = 0;
55         ProjectModel[] projectArray = new ProjectModel[0];
56
57         try {
58             Collection projects = pHome.findAll();
59             projectArray = new ProjectModel[projects.size()];
60             for(Iterator iterator = projects.iterator(); iterator.hasNext(); i++) {
61                 projectArray[i] = ((ProjectLocal) iterator.next()).getModel();
62             }
63         } catch(FinderException JavaDoc fe) {
64         }
65         return projectArray;
66     }
67
68     public int getNumberProjects() {
69         try {
70             Collection projects = pHome.findAll();
71             return projects.size();
72         } catch(FinderException JavaDoc fe) {
73         }
74         return 0;
75     }
76
77     public ProjectModel[] getAllAvailableProjects() {
78         int i = 0;
79         ProjectModel[] projectArray = new ProjectModel[0];
80
81         try {
82             Collection projects = pHome.findAllAvailable();
83             projectArray = new ProjectModel[projects.size()];
84             for(Iterator iterator = projects.iterator(); iterator.hasNext(); i++) {
85                 projectArray[i] = ((ProjectLocal) iterator.next()).getModel();
86             }
87         } catch(FinderException JavaDoc fe) {
88         }
89         return projectArray;
90     }
91
92     public ProjectModel createProject(ProjectModel model) {
93         try {
94             IDGenerator idGen = idHome.create();
95             ProjectLocal project = pHome.create(idGen.getId(ProjectLocal.ID_NAME));
96             project.setModel(model);
97             return project.getModel();
98         } catch(CreateException JavaDoc ce) {
99         }
100         return null;
101     }
102
103     public ProjectModel updateProject(ProjectModel model) {
104         try {
105             ProjectLocal project = pHome.findByPrimaryKey(model.getId());
106             project.setModel(model);
107             return project.getModel();
108         } catch(FinderException JavaDoc fe) {
109         }
110         return null;
111     }
112
113     public boolean deleteProject(ProjectModel model) {
114         try {
115             ProjectLocal project = pHome.findByPrimaryKey(model.getId());
116             project.remove();
117             return true;
118         } catch(FinderException JavaDoc fe) {
119             Logger.logDebug("Could not find project " + model.getId() + ".", fe);
120         } catch(RemoveException JavaDoc re) {
121             Logger.logError("Could not delete project " + model.getId() + ".", re);
122         }
123
124         return false;
125     }
126
127     public ComponentModel updateProjectComponent(ComponentModel model) {
128         try {
129             ComponentLocal component = cHome.findByPrimaryKey(model.getId());
130             component.setModel(model);
131             return component.getModel();
132         } catch(FinderException JavaDoc fe) {
133         }
134         return null;
135     }
136
137     public ComponentModel addProjectComponent(Integer JavaDoc projectId, ComponentModel model) {
138         try {
139             IDGenerator idGen = idHome.create();
140             ComponentLocal component = cHome.create(idGen.getId(ComponentLocal.ID_NAME));
141             component.setModel(model);
142
143             ProjectLocal project = pHome.findByPrimaryKey(projectId);
144             Collection components = project.getComponents();
145             components.add(component);
146             model.setId(component.getId());
147             model.setProjectId(projectId);
148 // return component.getModel();
149
return model;
150         } catch(CreateException JavaDoc ce) {
151         } catch(FinderException JavaDoc fe) {
152         }
153
154         return null;
155     }
156
157     public boolean removeProjectComponent(Integer JavaDoc projectId, Integer JavaDoc componentId) {
158         try {
159             ProjectLocal project = pHome.findByPrimaryKey(projectId);
160             ComponentLocal component = cHome.findByPrimaryKey(componentId);
161             Collection components = project.getComponents();
162             components.remove(component);
163             component.remove();
164             return true;
165         } catch(RemoveException JavaDoc re) {
166         } catch(FinderException JavaDoc fe) {
167         }
168         return false;
169     }
170
171     public ComponentModel getProjectComponent(Integer JavaDoc componentId) {
172         try {
173             ComponentLocal component = cHome.findByPrimaryKey(componentId);
174             return component.getModel();
175         } catch(FinderException JavaDoc fe) {
176         }
177         return null;
178     }
179
180     public ComponentModel[] getProjectComponents(Integer JavaDoc projectId) {
181         int i = 0;
182         ComponentModel[] componentArray = new ComponentModel[0];
183
184         try {
185             Collection components = cHome.findByProjectId(projectId);
186             componentArray = new ComponentModel[components.size()];
187             for(Iterator iterator = components.iterator(); iterator.hasNext(); i++) {
188                 componentArray[i] = ((ComponentLocal) iterator.next()).getModel();
189             }
190         } catch(FinderException JavaDoc fe) {
191         }
192
193         return componentArray;
194     }
195
196     public VersionModel addProjectVersion(Integer JavaDoc projectId, VersionModel model) {
197         try {
198             IDGenerator idGen = idHome.create();
199             VersionLocal version = vHome.create(idGen.getId(VersionLocal.ID_NAME));
200             version.setModel(model);
201
202             ProjectLocal project = pHome.findByPrimaryKey(projectId);
203             Collection versions = project.getVersions();
204             versions.add(version);
205             model.setId(version.getId());
206             model.setProjectId(projectId);
207 // return version.getModel();
208
return model;
209         } catch(CreateException JavaDoc ce) {
210         } catch(FinderException JavaDoc fe) {
211         }
212
213         return null;
214     }
215
216     public boolean removeProjectVersion(Integer JavaDoc projectId, Integer JavaDoc versionId) {
217         try {
218             ProjectLocal project = pHome.findByPrimaryKey(projectId);
219             VersionLocal version = vHome.findByPrimaryKey(versionId);
220             Collection versions = project.getVersions();
221             versions.remove(version);
222             version.remove();
223             return true;
224         } catch(RemoveException JavaDoc re) {
225         } catch(FinderException JavaDoc fe) {
226         }
227         return false;
228     }
229
230     public VersionModel updateProjectVersion(VersionModel model) {
231         try {
232             VersionLocal version = vHome.findByPrimaryKey(model.getId());
233             version.setModel(model);
234             return version.getModel();
235         } catch(FinderException JavaDoc fe) {
236         }
237         return null;
238     }
239
240     public VersionModel getProjectVersion(Integer JavaDoc versionId) {
241         try {
242             VersionLocal version = vHome.findByPrimaryKey(versionId);
243             return version.getModel();
244         } catch(FinderException JavaDoc fe) {
245         }
246         return null;
247     }
248
249     public VersionModel[] getProjectVersions(Integer JavaDoc projectId) {
250         int i = 0;
251         VersionModel[] versionArray = new VersionModel[0];
252
253         try {
254             Collection versions = vHome.findByProjectId(projectId);
255             versionArray = new VersionModel[versions.size()];
256             for(Iterator iterator = versions.iterator(); iterator.hasNext(); i++) {
257                 versionArray[i] = ((VersionLocal) iterator.next()).getModel();
258             }
259         } catch(FinderException JavaDoc fe) {
260         } catch(ClassCastException JavaDoc cce) {
261         }
262         return versionArray;
263     }
264
265     public UserModel[] getProjectOwners(Integer JavaDoc projectId) {
266         int i = 0;
267         UserModel[] userArray = new UserModel[0];
268
269         try {
270             ProjectLocal project = pHome.findByPrimaryKey(projectId);
271             Collection users = project.getOwners();
272             userArray = new UserModel[users.size()];
273             for(Iterator iterator = users.iterator(); iterator.hasNext(); i++) {
274                 userArray[i] = ((UserLocal) iterator.next()).getModel();
275             }
276         } catch(FinderException JavaDoc fe) {
277         }
278
279         return userArray;
280     }
281
282     public boolean setProjectOwners(ProjectModel model, HashSet newOwners) {
283         try {
284             ProjectLocal project = pHome.findByPrimaryKey(model.getId());
285
286             Collection owners = project.getOwners();
287
288             owners.clear();
289             if(newOwners != null && ! newOwners.isEmpty()) {
290                 for(Iterator iterator = newOwners.iterator(); iterator.hasNext(); ) {
291                     Integer JavaDoc ownerId = (Integer JavaDoc) iterator.next();
292                     UserLocal owner = uHome.findByPrimaryKey(ownerId);
293                     owners.add(owner);
294                 }
295             }
296             return true;
297         } catch(FinderException JavaDoc fe) {
298         }
299         return false;
300     }
301
302     public CustomFieldModel[] getProjectFields(Integer JavaDoc projectId) {
303         int i = 0;
304         CustomFieldModel[] fieldArray = new CustomFieldModel[0];
305
306         try {
307             ProjectLocal project = pHome.findByPrimaryKey(projectId);
308             Collection fields = project.getCustomFields();
309             fieldArray = new CustomFieldModel[fields.size()];
310             for(Iterator iterator = fields.iterator(); iterator.hasNext(); i++) {
311                 fieldArray[i] = ((CustomFieldLocal) iterator.next()).getModel();
312             }
313         } catch(FinderException JavaDoc fe) {
314         }
315
316         return fieldArray;
317     }
318
319     public CustomFieldModel[] getProjectFields(Integer JavaDoc projectId, Locale locale) {
320         CustomFieldModel[] fieldArray = getProjectFields(projectId);
321
322         for(int i = 0; i < fieldArray.length; i++) {
323             fieldArray[i].setLabels(locale);
324         }
325
326         return fieldArray;
327     }
328
329     public boolean setProjectFields(ProjectModel model, HashSet newFields) {
330         try {
331             ProjectLocal project = pHome.findByPrimaryKey(model.getId());
332
333             Collection fields = project.getCustomFields();
334
335             fields.clear();
336             if(newFields != null && ! newFields.isEmpty()) {
337                 for(Iterator iterator = newFields.iterator(); iterator.hasNext(); ) {
338                     Integer JavaDoc fieldId = (Integer JavaDoc) iterator.next();
339                     CustomFieldLocal field = cfHome.findByPrimaryKey(fieldId);
340                     fields.add(field);
341                 }
342             }
343             return true;
344         } catch(FinderException JavaDoc fe) {
345         }
346         return false;
347     }
348
349     public int getTotalNumberIssuesByProject(Integer JavaDoc projectId) {
350         try {
351             ProjectLocal project = pHome.findByPrimaryKey(projectId);
352             return project.getTotalNumberIssues();
353         } catch(FinderException JavaDoc fe) {
354         }
355
356         return 0;
357     }
358
359     public int getTotalNumberIssuesByVersion(Integer JavaDoc versionId) {
360         try {
361             VersionLocal version = vHome.findByPrimaryKey(versionId);
362             return version.getTotalNumberIssues();
363         } catch(FinderException JavaDoc fe) {
364         }
365
366         return 0;
367     }
368
369     public int getTotalNumberIssuesByComponent(Integer JavaDoc componentId) {
370         try {
371             ComponentLocal component = cHome.findByPrimaryKey(componentId);
372             return component.getTotalNumberIssues();
373         } catch(FinderException JavaDoc fe) {
374         }
375
376         return 0;
377     }
378
379     public void ejbCreate() {
380         try {
381             InitialContext ic = new InitialContext();
382             Object JavaDoc idRef = ic.lookup("java:comp/env/" + IDGenerator.JNDI_NAME);
383             idHome = (IDGeneratorHome) PortableRemoteObject.narrow(idRef, IDGeneratorHome.class);
384
385             pHome = (ProjectLocalHome) ic.lookup("java:comp/env/" + ProjectLocal.JNDI_NAME);
386             cHome = (ComponentLocalHome) ic.lookup("java:comp/env/" + ComponentLocal.JNDI_NAME);
387             cfHome = (CustomFieldLocalHome) ic.lookup("java:comp/env/" + CustomFieldLocal.JNDI_NAME);
388             uHome = (UserLocalHome) ic.lookup("java:comp/env/" + UserLocal.JNDI_NAME);
389             vHome = (VersionLocalHome) ic.lookup("java:comp/env/" + VersionLocal.JNDI_NAME);
390         } catch(NamingException ne) {
391             Logger.logError("Exception while looking up home interfaces.", ne);
392         }
393     }
394
395     public void setSessionContext(SessionContext JavaDoc value) {}
396     public void ejbActivate() {}
397     public void ejbPassivate() {}
398     public void ejbRemove() {}
399 }
400   
Popular Tags