KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > cowsultants > itracker > ejb > beans > webservice > IssueServiceBean


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.webservice;
20
21 import java.util.*;
22 import java.rmi.*;
23 import javax.ejb.*;
24 import javax.jms.*;
25 import javax.naming.*;
26 import javax.rmi.*;
27
28 import cowsultants.itracker.ejb.beans.entity.*;
29 import cowsultants.itracker.ejb.beans.message.*;
30 import cowsultants.itracker.ejb.client.exceptions.*;
31 import cowsultants.itracker.ejb.client.interfaces.*;
32 import cowsultants.itracker.ejb.client.models.*;
33 import cowsultants.itracker.ejb.client.resources.*;
34 import cowsultants.itracker.ejb.client.util.*;
35
36 import org.apache.axis.*;
37 import org.apache.axis.attachments.*;
38
39 public class IssueServiceBean implements SessionBean {
40     private InitialContext ic = null;
41     private IssueHandlerHome ihHome = null;
42     private ProjectHandlerHome phHome = null;
43     private UserHandlerHome uhHome = null;
44
45     public IssueModel getIssue(Integer JavaDoc issueId, String JavaDoc login, String JavaDoc authentication, int authType) throws IssueException {
46         IssueModel issue = null;
47
48         try {
49             UserHandler uh = uhHome.create();
50
51             UserModel user = uh.checkLogin(login, authentication, authType, AuthenticationConstants.REQ_SOURCE_API);
52
53             if(user == null) {
54                 throw new AuthenticatorException(AuthenticatorException.UNKNOWN_USER);
55             }
56
57             HashMap permissions = uh.getUserPermissions(user, AuthenticationConstants.REQ_SOURCE_API);
58
59             IssueHandler ih = ihHome.create();
60             issue = ih.getIssue(issueId);
61             if(IssueUtilities.canViewIssue(issue, user, permissions)) {
62                 issue.setComponents(ih.getIssueComponents(issueId));
63                 issue.setVersions(ih.getIssueVersions(issueId));
64                 issue.setHistory(ih.getIssueHistory(issueId));
65                 issue.setAttachments(ih.getIssueAttachments(issueId));
66             }
67         } catch(Exception JavaDoc e) {
68             Logger.logInfo("Exception while accessing issue " + issueId + " for user " + login, e);
69             throw new IssueException("Exception while accessing issue " + issueId + " for user " + login + ". Message: " + e.getMessage());
70         }
71         return issue;
72     }
73
74     public IssueModel[] getAllIssues(String JavaDoc login, String JavaDoc authentication, int authType) throws IssueException {
75         Vector issueVector = new Vector();
76         IssueModel[] issueArray = new IssueModel[0];
77
78         try {
79             UserHandler uh = uhHome.create();
80             UserModel user = uh.checkLogin(login, authentication, authType, AuthenticationConstants.REQ_SOURCE_API);
81
82             if(user == null) {
83                 throw new AuthenticatorException(AuthenticatorException.UNKNOWN_USER);
84             }
85
86             HashMap permissions = uh.getUserPermissions(user, AuthenticationConstants.REQ_SOURCE_API);
87
88             IssueHandler ih = ihHome.create();
89             IssueModel[] issues = ih.getAllIssues();
90             for(int i = 0; i < issues.length; i++) {
91                 if(IssueUtilities.canViewIssue(issues[i], user, permissions)) {
92                     issueVector.add(issues[i]);
93                 }
94             }
95
96             issueArray = new IssueModel[issueVector.size()];
97             issueVector.copyInto(issueArray);
98         } catch(Exception JavaDoc e) {
99             Logger.logInfo("Exception while getting issue list for user " + login, e);
100             throw new IssueException("Exception while getting issue list for user " + login + ". Message: " + e.getMessage());
101         }
102
103         return issueArray;
104     }
105
106     public IssueModel createIssue(IssueModel issue, String JavaDoc login, String JavaDoc authentication, int authType) throws IssueException {
107         IssueModel createdIssue = null;
108
109         if(issue == null || issue.getProjectId() == null) {
110             throw new IssueException("Invalid issue or project");
111         }
112
113         try {
114             UserHandler uh = uhHome.create();
115             UserModel user = uh.checkLogin(login, authentication, authType, AuthenticationConstants.REQ_SOURCE_API);
116
117             if(user == null) {
118                 throw new AuthenticatorException(AuthenticatorException.UNKNOWN_USER);
119             }
120
121             HashMap permissions = uh.getUserPermissions(user, AuthenticationConstants.REQ_SOURCE_API);
122             if(! UserUtilities.hasPermission(permissions, issue.getProjectId(), UserUtilities.PERMISSION_CREATE)) {
123                 throw new IssueException("User " + login + " does not have permission to create issues for project.");
124             }
125
126             ProjectHandler ph = phHome.create();
127             ProjectModel project = ph.getProject(issue.getProjectId());
128             if(project == null) {
129                 throw new IssueException("Invalid project specified.");
130             } else if(project.getStatus() != ProjectUtilities.STATUS_ACTIVE) {
131                 throw new IssueException("Issue's project is not active.");
132             }
133
134             IssueHandler ih = ihHome.create();
135             issue.setStatus(IssueUtilities.STATUS_NEW);
136             createdIssue = ih.createIssue(issue, issue.getProjectId(), user.getId(), user.getId());
137
138             Logger.logDebug("Created new issue: " + createdIssue.toString());
139
140             if(createdIssue == null) {
141                 throw new IssueException("Error creating issue.");
142             }
143
144             IssueFieldModel[] issueFields = issue.getFields();
145             if(issueFields != null && issueFields.length > 0) {
146                 ih.setIssueFields(createdIssue.getId(), issueFields);
147             }
148
149             HashSet tempIds = new HashSet();
150             for(int i = 0; i < issue.getVersions().length; i++) {
151                 tempIds.add(issue.getVersions()[i].getId());
152             }
153             ih.setIssueVersions(createdIssue.getId(), tempIds, user.getId());
154
155             tempIds.clear();
156             for(int i = 0; i < issue.getComponents().length; i++) {
157                 tempIds.add(issue.getComponents()[i].getId());
158             }
159             ih.setIssueComponents(createdIssue.getId(), tempIds, user.getId());
160
161             if(issue.getHistory().length == 1) {
162                 IssueHistoryModel history = issue.getHistory()[0];
163                 history.setIssueId(createdIssue.getId());
164                 history.setUserId(user.getId());
165                 ih.addIssueHistory(history);
166             }
167
168             String JavaDoc initialOwner = issue.getOwnerLogin();
169             if(initialOwner != null && ! initialOwner.equals("")) {
170                 UserModel owner = uh.getUserByLogin(initialOwner);
171                 if(owner != null) {
172                     if(UserUtilities.hasPermission(permissions, UserUtilities.PERMISSION_ASSIGN_OTHERS) ||
173                       (UserUtilities.hasPermission(permissions, UserUtilities.PERMISSION_ASSIGN_SELF) &&
174                        user.getId().equals(owner.getId()))) {
175                         ih.assignIssue(createdIssue.getId(), owner.getId(), user.getId());
176                     }
177                 }
178             }
179
180             // Now add any attachments
181
addIssueAttachments(createdIssue, user, ih);
182         } catch(AuthenticatorException le) {
183             Logger.logInfo("AuthenticatorException for user " + login + " while creating issue.");
184             throw new IssueException("Unknown user " + login + " while creating issue.");
185         } catch(CreateException ce) {
186             Logger.logInfo("CreateException for user " + login + " while creating issue. " + ce.getMessage());
187             throw new IssueException("Error creating issue.");
188         }
189         return createdIssue;
190     }
191
192     public IssueModel updateIssue(IssueModel issue, String JavaDoc login, String JavaDoc authentication, int authType) throws IssueException {
193         IssueModel updatedIssue = null;
194
195         if(issue == null || issue.getId() == null) {
196             throw new IssueException("Invalid issue or project");
197         }
198
199         try {
200             boolean restrictedUpdate = false;
201             UserHandler uh = uhHome.create();
202             UserModel user = uh.checkLogin(login, authentication, authType, AuthenticationConstants.REQ_SOURCE_API);
203
204             if(user == null) {
205                 throw new AuthenticatorException(AuthenticatorException.UNKNOWN_USER);
206             }
207
208             IssueHandler ih = ihHome.create();
209             IssueModel existingIssue = ih.getIssue(issue.getId());
210
211             HashMap permissions = uh.getUserPermissions(user, AuthenticationConstants.REQ_SOURCE_API);
212             if(! IssueUtilities.canEditIssue(existingIssue, user.getId(), permissions)) {
213                 throw new IssueException("User " + login + " does not have permission to this issue.");
214             }
215
216             if(! UserUtilities.hasPermission(permissions, existingIssue.getProjectId(), UserUtilities.PERMISSION_EDIT_FULL)) {
217                 restrictedUpdate = true;
218             }
219
220             ProjectHandler ph = phHome.create();
221             ProjectModel project = ph.getProject(existingIssue.getProjectId());
222             if(project == null) {
223                 throw new IssueException("Invalid project specified.");
224             } else if(project.getStatus() != ProjectUtilities.STATUS_ACTIVE) {
225                 throw new IssueException("Issue's project is not active.");
226             }
227
228             // If the update is restricted, then only the description, custom fields,
229
// history entries and attachments can be updated. The issue can also be closed if
230
// authorized
231
if(restrictedUpdate) {
232                 existingIssue.setDescription(issue.getDescription());
233                 int newStatus = issue.getStatus();
234                 if(existingIssue.getStatus() >= IssueUtilities.STATUS_RESOLVED && newStatus >= IssueUtilities.STATUS_CLOSED &&
235                    UserUtilities.hasPermission(permissions, UserUtilities.PERMISSION_CLOSE)) {
236                     issue.setStatus(newStatus);
237                 }
238
239                 updatedIssue = ih.updateIssue(existingIssue, user.getId());
240             } else {
241                 // Check the issue status
242
int previousStatus = existingIssue.getStatus();
243
244                 // Reopened the issue. Reset the resolution field.
245
if((issue.getStatus() >= IssueUtilities.STATUS_ASSIGNED && issue.getStatus() < IssueUtilities.STATUS_RESOLVED) &&
246                    (previousStatus >= IssueUtilities.STATUS_RESOLVED && previousStatus < IssueUtilities.STATUS_END)) {
247                     issue.setResolution("");
248                 }
249
250                 if(issue.getStatus() >= IssueUtilities.STATUS_CLOSED && ! UserUtilities.hasPermission(permissions, project.getId(), UserUtilities.PERMISSION_CLOSE)) {
251                     if(previousStatus < IssueUtilities.STATUS_CLOSED) {
252                         issue.setStatus(previousStatus);
253                     } else {
254                         issue.setStatus(IssueUtilities.STATUS_RESOLVED);
255                     }
256                 }
257
258                 if(issue.getStatus() < IssueUtilities.STATUS_NEW || issue.getStatus() >= IssueUtilities.STATUS_END) {
259                     issue.setStatus(previousStatus);
260                 }
261
262                 if(issue.getStatus() < IssueUtilities.STATUS_NEW) {
263                     issue.setStatus(IssueUtilities.STATUS_NEW);
264                 } else if(issue.getStatus() >= IssueUtilities.STATUS_END) {
265                     if(! UserUtilities.hasPermission(permissions, project.getId(), UserUtilities.PERMISSION_CLOSE)) {
266                         issue.setStatus(IssueUtilities.STATUS_RESOLVED);
267                     } else {
268                         issue.setStatus(IssueUtilities.STATUS_CLOSED);
269                     }
270                 }
271
272                 updatedIssue = ih.updateIssue(issue, user.getId());
273             }
274
275             if(updatedIssue == null) {
276                 throw new IssueException("Error updating issue.");
277             }
278
279             if(issue.getHistory().length == 1) {
280                 IssueHistoryModel history = issue.getHistory()[0];
281                 history.setIssueId(updatedIssue.getId());
282                 history.setUserId(user.getId());
283                 ih.addIssueHistory(history);
284             }
285
286             IssueFieldModel[] issueFields = issue.getFields();
287             if(issueFields != null && issueFields.length > 0) {
288                 ih.setIssueFields(updatedIssue.getId(), issueFields);
289             }
290
291             if(! restrictedUpdate) {
292                 HashSet tempIds = new HashSet();
293                 for(int i = 0; i < issue.getVersions().length; i++) {
294                     tempIds.add(issue.getVersions()[i].getId());
295                 }
296                 ih.setIssueVersions(updatedIssue.getId(), tempIds, user.getId());
297
298                 tempIds.clear();
299                 for(int i = 0; i < issue.getComponents().length; i++) {
300                     tempIds.add(issue.getComponents()[i].getId());
301                 }
302                 ih.setIssueComponents(updatedIssue.getId(), tempIds, user.getId());
303             }
304
305             String JavaDoc currentOwner = existingIssue.getOwnerLogin();
306             String JavaDoc newOwner = issue.getOwnerLogin();
307             if(newOwner != null && ! newOwner.equals("") && ! newOwner.equals(currentOwner)) {
308                 if(UserUtilities.hasPermission(permissions, UserUtilities.PERMISSION_ASSIGN_OTHERS) ||
309                    (UserUtilities.hasPermission(permissions, UserUtilities.PERMISSION_ASSIGN_SELF) && user.getLogin().equals(newOwner)) ||
310                    (UserUtilities.hasPermission(permissions, UserUtilities.PERMISSION_UNASSIGN_SELF) && user.getLogin().equals(currentOwner) && newOwner.equals(""))
311                   ) {
312                     UserModel owner = uh.getUserByLogin(newOwner);
313                     ih.assignIssue(updatedIssue.getId(), owner.getId(), user.getId());
314                 }
315             }
316
317             // Now add any attachments
318
addIssueAttachments(updatedIssue, user, ih);
319         } catch(AuthenticatorException le) {
320             Logger.logInfo("AuthenticatorException for user " + login + " while updating issue.");
321             throw new IssueException("Unknown user " + login + " while updating issue.");
322         } catch(CreateException ce) {
323             Logger.logInfo("CreateException for user " + login + " while updating issue. " + ce.getMessage());
324             throw new IssueException("Error updating issue.");
325         }
326         return updatedIssue;
327     }
328
329     public void assignIssue(Integer JavaDoc issueId, String JavaDoc ownerLogin, String JavaDoc login, String JavaDoc authentication, int authType) throws IssueException {
330         if(issueId == null || ownerLogin == null) {
331             return;
332         }
333
334         try {
335             UserHandler uh = uhHome.create();
336             UserModel user = uh.checkLogin(login, authentication, authType, AuthenticationConstants.REQ_SOURCE_API);
337
338             if(user == null) {
339                 throw new AuthenticatorException(AuthenticatorException.UNKNOWN_USER);
340             }
341
342             if(! ownerLogin.equals("")) {
343                 UserModel owner = uh.getUserByLogin(ownerLogin);
344                 if(owner != null) {
345                     HashMap permissions = uh.getUserPermissions(user, AuthenticationConstants.REQ_SOURCE_API);
346                     if(UserUtilities.hasPermission(permissions, UserUtilities.PERMISSION_ASSIGN_OTHERS) ||
347                       (UserUtilities.hasPermission(permissions, UserUtilities.PERMISSION_ASSIGN_SELF) &&
348                        user.getId().equals(owner.getId()))) {
349                         IssueHandler ih = ihHome.create();
350                         ih.assignIssue(issueId, owner.getId(), user.getId());
351                     }
352                 }
353             }
354         } catch(AuthenticatorException le) {
355             Logger.logInfo("AuthenticatorException for user " + login + " while assigning issue.");
356             throw new IssueException("Unknown user " + login + " while assigning issue.");
357         } catch(CreateException ce) {
358             Logger.logInfo("CreateException for user " + login + " while assigning issue. " + ce.getMessage());
359             throw new IssueException("Error assigning issue.");
360         }
361     }
362
363     public void ejbCreate() {
364         try {
365             ic = new InitialContext();
366             Object JavaDoc ihRef = ic.lookup("java:comp/env/" + IssueHandler.JNDI_NAME);
367             ihHome = (IssueHandlerHome) PortableRemoteObject.narrow(ihRef, IssueHandlerHome.class);
368
369             Object JavaDoc phRef = ic.lookup("java:comp/env/" + ProjectHandler.JNDI_NAME);
370             phHome = (ProjectHandlerHome) PortableRemoteObject.narrow(phRef, ProjectHandlerHome.class);
371
372             Object JavaDoc uhRef = ic.lookup("java:comp/env/" + UserHandler.JNDI_NAME);
373             uhHome = (UserHandlerHome) PortableRemoteObject.narrow(uhRef, UserHandlerHome.class);
374         } catch(NamingException ne) {
375             Logger.logError("Exception while looking up home interfaces.", ne);
376         }
377     }
378
379     public void setSessionContext(SessionContext value) {}
380     public void ejbActivate() {}
381     public void ejbPassivate() {}
382     public void ejbRemove() {}
383
384     private void addIssueAttachments(IssueModel issue, UserModel user, IssueHandler issueHandler) {
385         if(issue.getAttachments().length > 0) {
386             org.apache.axis.Message requestMessage = AxisEngine.getCurrentMessageContext().getRequestMessage();
387             IssueAttachmentModel[] attachments = issue.getAttachments();
388             if(attachments.length == requestMessage.countAttachments()) {
389                 Logger.logDebug("Issue contains " + attachments.length + " attachments");
390                 for(Iterator iter = requestMessage.getAttachments(); iter.hasNext(); ) {
391                     AttachmentPart attachmentPart = (AttachmentPart) iter.next();
392                     try {
393                         if(attachmentPart != null) {
394                             Logger.logDebug("File: " + attachmentPart.getAttachmentFile() + " Id: " + attachmentPart.getContentId());
395                             String JavaDoc attachmentId = (attachmentPart.getContentId() == null ? "" : attachmentPart.getContentId());
396                             Logger.logDebug("Attachment " + attachmentId + " found");
397                             for(int i = 0; i < attachments.length; i++) {
398                                 if(attachmentId.equalsIgnoreCase(attachments[i].getOriginalFileName())) {
399                                     byte[] data = new byte[attachmentPart.getSize()];
400                                     Logger.logDebug("Attachment " + attachmentId + " contains " + data.length + " bytes");
401                                     attachmentPart.getDataHandler().getInputStream().read(data);
402                                     attachments[i].setIssueId(issue.getId());
403                                     attachments[i].setUserId(user.getId());
404                                     issueHandler.addIssueAttachment(attachments[i], data);
405                                 }
406                             }
407                         }
408                     } catch(Exception JavaDoc e) {
409                         Logger.logError("Error saving attachment for issue " + issue.getId(), e);
410                     }
411                 }
412             }
413         }
414     }
415 }
416   
Popular Tags