Source code: openfuture/bugbase/servlet/BugBaseServletClient.java
1 package openfuture.bugbase.servlet;
2 /*
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public
5 * License as published by the Free Software Foundation; either
6 * version 2 of the License, or (at your option) any later version.<p>
7 *
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.<p>
12 *
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA<br>
16 * http://www.gnu.org/copyleft/lesser.html
17 */
18
19 import java.io.IOException;
20 import java.io.ObjectInputStream;
21 import java.io.ObjectOutputStream;
22 import java.io.Serializable;
23 import java.net.MalformedURLException;
24 import java.net.URL;
25 import java.net.URLConnection;
26 import java.util.Hashtable;
27 import java.util.Iterator;
28 import java.util.LinkedList;
29 import openfuture.bugbase.domain.BugReport;
30 import openfuture.bugbase.domain.Login;
31 import openfuture.bugbase.domain.User;
32 import openfuture.bugbase.error.BugBaseException;
33 import openfuture.bugbase.model.TransactionResult;
34 import openfuture.bugbase.model.Version;
35 import openfuture.bugbase.xobjects.BugBaseQuery;
36 import openfuture.bugbase.xobjects.BugBaseQueryResult;
37 import openfuture.bugbase.xobjects.QueryResultMasterData;
38 import openfuture.bugbase.xobjects.QueryResultVersions;
39
40
41 // Configuration Management Information:
42 // -------------------------------------
43 // $Id: BugBaseServletClient.java,v 1.6 2001/10/31 22:55:15 wreissen Exp $
44 //
45 // Version History:
46 // ----------------
47 // $Log: BugBaseServletClient.java,v $
48 // Revision 1.6 2001/10/31 22:55:15 wreissen
49 // getUserList() implemented
50 //
51 // Revision 1.5 2001/10/27 17:15:37 wreissen
52 // javadoc improved
53 //
54 // Revision 1.4 2001/10/14 18:54:40 wreissen
55 // - getDoctors without arguments
56 // - setDoctors removed
57 // - query status implemented
58 // - documentation fixed
59 // - doQuery uniformly used
60 //
61 // Revision 1.3 2001/07/01 06:46:30 wreissen
62 // fixed errors in DOS/Unix-translation.
63 //
64 // Revision 1.4 2001/04/03 05:22:25 wreissen
65 // getBugReportList: author and doctor as search criteria added.
66 //
67 // Revision 1.3 2001/03/12 14:20:44 wreissen
68 // - searching with search string implemented.
69 // - retrieving single bug reports added.
70 //
71 // Revision 1.2 2001/03/08 06:50:57 wreissen
72 // validDoctorUser added.
73 //
74 // Revision 1.1 2001/01/14 17:00:40 wreissen
75 // Moved from openfuture.bugbase.app.applet.
76 //
77 // Revision 1.6 2000/11/30 06:12:35 wreissen
78 // updateBugReports contains a user as argument, project name is
79 // no longer used as argument.
80 //
81 // Revision 1.5 2000/11/29 10:28:05 wreissen
82 // multiple bug reports may be updated concurrently.
83 //
84 // Revision 1.4 2000/11/20 14:26:28 wreissen
85 // - addAttachment implemented
86 // - addBugReport returns the bug report ID
87 //
88 // Revision 1.3 2000/11/16 18:20:21 wreissen
89 // doctorLogin implemented.
90 //
91 // Revision 1.2 2000/11/15 12:07:07 wreissen
92 // mail server integrated.
93 //
94 // Revision 1.1 2000/09/27 15:53:23 wreissen
95 // moved to openfuture.
96 //
97 // Revision 1.12 2000/09/03 17:59:40 wreissen
98 // - adminLogin implemented
99 // - createConnection renamed to login
100 //
101 // Revision 1.11 2000/08/28 10:14:04 wreissen
102 // If the login failed, a BugBaseException is thrown.
103 //
104 // Revision 1.10 2000/08/04 15:03:24 wreissen
105 // methods added:
106 // - addBugReport
107 // - getPackages
108 //
109 // Revision 1.9 2000/07/10 05:44:48 wreissen
110 // - unified to queries with BugBaseQuery
111 // - exceptions introduced.
112 //
113 // Revision 1.8 2000/06/27 15:18:00 wreissen
114 // createConnection implemented, validateUser dropped.
115 //
116 // Revision 1.7 2000/06/13 04:57:46 wreissen
117 // new methods implemented:
118 // - getUserData
119 // - saveUserData
120 // - getGroups
121 //
122 // Revision 1.6 2000/06/08 05:05:11 wreissen
123 // returned object of setProjects fixed.
124 //
125 // Revision 1.5 2000/05/30 05:18:44 wreissen
126 // - persistency updating introduced.
127 // - user information stored in an extra table
128 //
129 // Revision 1.4 2000/04/25 05:10:51 wreissen
130 // bugs fixed: wrong return types from service.
131 //
132 // Revision 1.3 2000/03/27 05:39:40 wreissen
133 // return of QueryResultEmpty no longer ignored,
134 // since JAR file wold not contain it, if not explicitly referenced
135 // by some class.
136 //
137 // Revision 1.2 2000/03/06 06:13:42 wreissen
138 // - executeQuery added.
139 // - QueryBugReportList contains filter arguments.
140 //
141 // Revision 1.1 2000/02/14 06:49:44 wreissen
142 // initial version.
143 //
144 //
145 // ***********************************************************************************
146
147 /**
148 * <p>This class is the proxy to {@link BugBaseServlet}. It communicates with
149 * serialized objects via HTTP with the servlet.</p>
150 *
151 *
152 * Created: Sun Feb 06 16:58:12 2000
153 *
154 * @author <a href="mailto:wolfgang@openfuture.de">Wolfgang Reissenberger</a>
155 * @version $Revision: 1.6 $
156 */
157
158 public class BugBaseServletClient {
159
160 private URL url;
161 private Integer sessionID;
162
163
164 /**
165 * Creates a new <code>BugBaseServletClient</code> instance.
166 *
167 * @param newUrl URL of
168 * {@link openfuture.bugbase.servlet.BugBaseServlet BugBaseServlet}
169 * @exception MalformedURLException if an error occurs
170 */
171 public BugBaseServletClient(String newUrl) throws MalformedURLException {
172 url = new URL(newUrl);
173 }
174
175 /**
176 * Retrieve the list of bug reports matching the given criteria.
177 *
178 * @param project project name
179 * @param filterReported state at least reported?
180 * @param filterStarted state at least started?
181 * @param filterFixed state at least fixed?
182 * @param filterRejected state at least rejected?
183 * @param searchString string that should occur either in the
184 * title or in the description
185 * @param reporterId user ID of the bug reporter
186 * @param doctorId user ID of the bug doctor
187 * @return list of {@link openfuture.bugbase.domain.BugReport bug reports}.
188 * @exception BugBaseException if an error occurs
189 */
190 public LinkedList getBugReportList(String project,
191 Boolean filterReported,
192 Boolean filterStarted,
193 Boolean filterFixed,
194 Boolean filterRejected,
195 String searchString,
196 String reporterId,
197 String doctorId)
198
199 throws BugBaseException {
200 LinkedList parameter = new LinkedList();
201 parameter.add(project);
202
203 parameter.add(filterReported);
204 parameter.add(filterStarted);
205 parameter.add(filterFixed);
206 parameter.add(filterRejected);
207
208 parameter.add(searchString);
209 parameter.add(reporterId);
210 parameter.add(doctorId);
211
212 BugBaseQuery query = createQuery(BugBaseQuery.QUERY_BUG_REPORT_LIST,
213 parameter);
214
215 BugBaseQueryResult result = doQuery(query);
216
217 if (result.getQueryType() == BugBaseQueryResult.QR_FAILED) {
218 throw (new BugBaseException("xx", result.getResult().toString(),
219 "1.1", "Cannot get list of bug reports."));
220 } else {
221 return ((LinkedList) result.getResult());
222 }
223 }
224
225
226
227 /**
228 * Retrieve a single bug report.
229 *
230 * @param id ID of the bug report.
231 * @return bug report matching the given ID
232 * @exception BugBaseException if an error occurs
233 */
234 public BugReport getBugReport(Integer id)
235 throws BugBaseException {
236 LinkedList parameter = new LinkedList();
237 parameter.add(id);
238
239 BugBaseQuery query = createQuery(BugBaseQuery.QUERY_BUG_REPORT,
240 parameter);
241
242 BugBaseQueryResult result = doQuery(query);
243
244 if (result.getQueryType() == BugBaseQueryResult.QR_FAILED) {
245 throw (new BugBaseException("xx", result.getResult().toString(),
246 "xx", "Cannot get bug report."));
247 } else {
248 return ((BugReport) result.getResult());
249 }
250 }
251
252 /**
253 * Add a new bug report. The number of the report is returned.
254 *
255 * @param project project name
256 * @param report the bug report
257 * @return number of the report
258 * @exception BugBaseException if an error occurs
259 */
260 public Integer addBugReport(String project, BugReport report)
261 throws BugBaseException {
262 LinkedList parameter = new LinkedList();
263 parameter.add(project);
264 parameter.add(report);
265 BugBaseQuery query = createQuery(BugBaseQuery.QUERY_ADD_BUGREPORT,
266 parameter);
267
268 BugBaseQueryResult result = doQuery(query);
269
270 if (result.getQueryType() == BugBaseQueryResult.QR_FAILED) {
271 throw (new BugBaseException("xx", result.getResult().toString(),
272 "1.2", "Cannot add new bug report."));
273 }
274
275 return((Integer) result.getResult());
276 }
277
278 /**
279 * Add a new bug report. The number of the report is returned.
280 *
281 * @param project project name.
282 * @param report new bug report.
283 * @param sessionID session ID.
284 * @return number of the report
285 * @exception BugBaseException if an error occurs
286 */
287 public synchronized Integer addBugReport(String project, BugReport report,
288 Integer sessionID)
289 throws BugBaseException {
290 LinkedList parameter = new LinkedList();
291 parameter.add(project);
292 parameter.add(report);
293 BugBaseQuery query = createQuery(BugBaseQuery.QUERY_ADD_BUGREPORT,
294 parameter);
295
296 BugBaseQueryResult result = doQuery(query);
297
298 if (result.getQueryType() == BugBaseQueryResult.QR_FAILED) {
299 throw (new BugBaseException("xx", result.getResult().toString(),
300 "1.2", "Cannot add new bug report."));
301 }
302
303 return((Integer) result.getResult());
304 }
305
306 /**
307 * Retrieve the list of projects registered at Bug Base.
308 *
309 * @return list of projects
310 * @exception BugBaseException if an error occurs
311 */
312 public LinkedList getProjects() throws BugBaseException {
313 BugBaseQuery query = createQuery(BugBaseQuery.QUERY_PROJECTS,
314 null);
315
316 BugBaseQueryResult result = doQuery(query);
317
318 if (result.getQueryType() == BugBaseQueryResult.QR_FAILED) {
319 throw (new BugBaseException("xx", result.getResult().toString(),
320 "1.3", "Cannot get list of projects."));
321 } else {
322 return ((LinkedList) result.getResult());
323 }
324 }
325
326
327 /**
328 * Set the list of projects registered in Bug Base
329 *
330 * @param projects list of projects
331 * @exception BugBaseException if an error occurs
332 */
333 public void setProjects(LinkedList projects) throws BugBaseException {
334 LinkedList parameter = new LinkedList();
335 parameter.add(projects);
336 BugBaseQuery query = createQuery(BugBaseQuery.QUERY_PROJECTS,
337 parameter);
338
339 BugBaseQueryResult result = doQuery(query);
340
341 if (result.getQueryType() == BugBaseQueryResult.QR_FAILED) {
342 throw (new BugBaseException("xx", result.getResult().toString(),
343 "1.4", "Cannot set list of projects."));
344 }
345 }
346
347
348 /**
349 * Retrieve the list of packages for a given project.
350 *
351 * @param project project name
352 * @return list of packages
353 * @exception BugBaseException if an error occurs
354 */
355 public LinkedList getPackages(String project) throws BugBaseException {
356 LinkedList parameter = new LinkedList();
357 parameter.add(project);
358 BugBaseQuery query = createQuery(BugBaseQuery.QUERY_PACKAGES,
359 parameter);
360
361 BugBaseQueryResult result = doQuery(query);
362
363 if (result.getQueryType() == BugBaseQueryResult.QR_FAILED) {
364 throw (new BugBaseException("xx", result.getResult().toString(),
365 "1.5", "Cannot get list of packages."));
366 } else {
367 return ((LinkedList) result.getResult());
368 }
369 }
370
371
372 /**
373 * Retrieves the list of packages for a certain project.
374 *
375 * @param project project name
376 * @param sessionID session ID
377 * @return list of package names
378 * @exception BugBaseException if an error occurs
379 */
380 public LinkedList getPackages(String project, Integer sessionID)
381 throws BugBaseException {
382 LinkedList parameter = new LinkedList();
383 parameter.add(project);
384 BugBaseQuery query = createQuery(BugBaseQuery.QUERY_PACKAGES,
385 parameter);
386
387 BugBaseQueryResult result = doQuery(query);
388
389 if (result.getQueryType() == BugBaseQueryResult.QR_FAILED) {
390 throw (new BugBaseException("xx", result.getResult().toString(),
391 "1.5", "Cannot get list of packages."));
392 } else {
393 return ((LinkedList) result.getResult());
394 }
395 }
396
397
398 /**
399 * Set the list of packages for a given project
400 *
401 * @param project project name
402 * @param packages list of package names
403 * @exception BugBaseException if an error occurs
404 */
405 public void setPackages(String project, LinkedList packages) throws BugBaseException {
406 LinkedList parameter = new LinkedList();
407 parameter.add(project);
408 parameter.add(packages);
409 BugBaseQuery query = createQuery(BugBaseQuery.QUERY_PACKAGES,
410 parameter);
411
412 BugBaseQueryResult result = doQuery(query);
413
414 if (result.getQueryType() == BugBaseQueryResult.QR_FAILED) {
415 throw (new BugBaseException("xx", result.getResult().toString(),
416 "1.6", "Cannot set packages."));
417 }
418 }
419
420
421 /**
422 * Retrieve the list of users belonging to the 'doctors' group
423 *
424 * @return list of users belonging to the 'doctors' group
425 * @exception BugBaseException if an error occurs
426 */
427 public LinkedList getDoctors() throws BugBaseException {
428 BugBaseQuery query = createQuery(BugBaseQuery.QUERY_DOCTORS, null);
429
430 BugBaseQueryResult result = doQuery(query);
431
432 if (result.getQueryType() == BugBaseQueryResult.QR_FAILED) {
433 throw (new BugBaseException("xx", result.getResult().toString(),
434 "1.7", "Cannot get list of doctors."));
435 } else {
436 return ((LinkedList) result.getResult());
437 }
438 }
439
440
441
442 /**
443 * Update the given bug report. if sendEmail is true, the
444 * bug reporter will be notified via email about the changes.
445 *
446 * @param user user modifying the bug reports
447 * @param reports list of bug reports to be updated
448 * @param sendEmail should an email be sent?
449 * @exception BugBaseException if an error occurs
450 */
451 public void updateBugReports(User user, LinkedList reports,
452 Boolean sendEmail) throws BugBaseException {
453 LinkedList parameter = new LinkedList();
454 parameter.add(user);
455 parameter.add(reports);
456 parameter.add(sendEmail);
457 BugBaseQuery query = createQuery(BugBaseQuery.QUERY_UPDATE_BUGREPORTS,
458 parameter);
459
460 BugBaseQueryResult result = doQuery(query);
461
462 if (result.getQueryType() == BugBaseQueryResult.QR_FAILED) {
463 throw (new BugBaseException("xx", result.getResult().toString(),
464 "1.9", "Cannot update bug report."));
465 }
466 }
467
468
469 /**
470 * Update the given bug report.
471 *
472 * @param user user modifying the bug reports
473 * @param report bug report to be updated
474 * @exception BugBaseException if an error occurs
475 */
476 public void updateBugReport(User user, BugReport report)
477 throws BugBaseException {
478 LinkedList parameter = new LinkedList();
479 parameter.add(user);
480 parameter.add(report);
481 BugBaseQuery query = createQuery(BugBaseQuery.QUERY_UPDATE_BUGREPORT,
482 parameter);
483
484 BugBaseQueryResult result = doQuery(query);
485
486 if (result.getQueryType() == BugBaseQueryResult.QR_FAILED) {
487 throw (new BugBaseException("xx", result.getResult().toString(),
488 "1.9", "Cannot update bug report."));
489 }
490 }
491
492
493 /**
494 * Execute a database query.
495 *
496 * @param command SQL command
497 * @return result set returned by the SQL query
498 * @exception BugBaseException if an error occurs
499 */
500 public LinkedList executeQuery(String command) throws BugBaseException {
501 LinkedList parameter = new LinkedList();
502 parameter.add(command);
503 BugBaseQuery query = createQuery(BugBaseQuery.QUERY_EXECUTE,
504 parameter);
505
506 BugBaseQueryResult result = doQuery(query);
507
508 if (result.getQueryType() == BugBaseQueryResult.QR_FAILED) {
509 throw (new BugBaseException("xx", result.getResult().toString(),
510 "1.10", "Cannot execute query."));
511 } else {
512 return ((LinkedList) result.getResult());
513 }
514 }
515
516
517 /**
518 * Retrieve the versions of the
519 * {@link openfuture.bugbase.servlet.BugBaseServlet BugBaseServlet},
520 * {@link openfuture.bugbase.domain.Persistency persistency} and the
521 * available versions from the
522 * {@link openfuture.bugbase.domain.VersionManager version manager}.
523 *
524 * @return version informations
525 * @exception BugBaseException if an error occurs
526 */
527 public QueryResultVersions getVersions() throws BugBaseException {
528 BugBaseQuery query = createQuery(BugBaseQuery.QUERY_VERSIONS, null);
529
530 BugBaseQueryResult result = doQuery(query);
531
532 if (result.getQueryType() == BugBaseQueryResult.QR_FAILED) {
533 throw (new BugBaseException("xx", result.getResult().toString(),
534 "1.11", "Cannot get list of versions."));
535 } else {
536 return ((QueryResultVersions) result.getResult());
537 }
538 }
539
540
541 /**
542 * Retrieve the list of available updates.
543 *
544 * @return list of available updates
545 * @exception BugBaseException if an error occurs
546 */
547 public LinkedList getAvailableUpdates() throws BugBaseException {
548 BugBaseQuery query = createQuery(BugBaseQuery.QUERY_AVAILABLE_UPDATES,
549 null);
550
551 BugBaseQueryResult result = doQuery(query);
552
553 if (result.getQueryType() == BugBaseQueryResult.QR_FAILED) {
554 throw (new BugBaseException("xx", result.getResult().toString(),
555 "1.12", "Cannot get list of available updates."));
556 } else {
557 return ((LinkedList) result.getResult());
558 }
559 }
560
561
562 /**
563 * Update the persistency to a given version.
564 *
565 * @param version new persistency version
566 * @return result of this transaction
567 * @exception BugBaseException if an error occurs
568 */
569 public TransactionResult updatePersistency(Version version)
570 throws BugBaseException {
571 LinkedList parameter = new LinkedList();
572 parameter.add(version);
573 BugBaseQuery query = createQuery(BugBaseQuery.QUERY_UPDATE,
574 parameter);
575
576 BugBaseQueryResult result = doQuery(query);
577
578 if (result.getQueryType() == BugBaseQueryResult.QR_FAILED) {
579 throw (new BugBaseException("xx", result.getResult().toString(),
580 "1.13", "Cannot update persistency."));
581 } else {
582 return ((TransactionResult) result.getResult());
583 }
584 }
585
586
587
588 /**
589 * Retrieve the list of user data. This method may
590 * only be used by admin users.
591 *
592 * @return the list of user data
593 * @exception BugBaseException if an error occurs
594 */
595 public LinkedList getUserData() throws BugBaseException {
596 BugBaseQuery query = createQuery(BugBaseQuery.QUERY_USERDATA,
597 null);
598
599 BugBaseQueryResult result = doQuery(query);
600
601 if (result.getQueryType() == BugBaseQueryResult.QR_FAILED) {
602 throw (new BugBaseException("xx", result.getResult().toString(),
603 "1.14", "Cannot get user data."));
604 } else {
605 return ((LinkedList) result.getResult());
606 }
607 }
608
609
610 /**
611 * Retrieve the list of user data except for their passwords.
612 * This method may be executed by ordinary users.
613 *
614 * @return the list of user data
615 * @exception BugBaseException if an error occurs
616 */
617 public LinkedList getUserList() throws BugBaseException {
618 BugBaseQuery query = createQuery(BugBaseQuery.QUERY_USERLIST,
619 null);
620
621 BugBaseQueryResult result = doQuery(query);
622
623 if (result.getQueryType() == BugBaseQueryResult.QR_FAILED) {
624 throw (new BugBaseException("xx", result.getResult().toString(),
625 "1.14", "Cannot get user data."));
626 } else {
627 return ((LinkedList) result.getResult());
628 }
629 }
630
631
632 /**
633 * Retrieve the list of all group IDs
634 *
635 * @return the list of all group IDs
636 * @exception BugBaseException if an error occurs
637 */
638 public LinkedList getGroups() throws BugBaseException {
639 BugBaseQuery query = createQuery(BugBaseQuery.QUERY_GROUPDATA,
640 null);
641
642 BugBaseQueryResult result = doQuery(query);
643
644 if (result.getQueryType() == BugBaseQueryResult.QR_FAILED) {
645 throw (new BugBaseException("xx", result.getResult().toString(),
646 "1.15", "Cannot get list of user groups."));
647 } else {
648 return ((LinkedList) result.getResult());
649 }
650 }
651
652
653 /**
654 * Save user data. Update only dirty users. The users from deleted
655 * are deleted.
656 *
657 * @param users list of users to be updated
658 * @param deleted list of users to be deleted
659 * @exception BugBaseException if an error occurs
660 * @see openfuture.bugbase.domain.User
661 */
662 public void saveUserData(LinkedList users, LinkedList deleted) throws BugBaseException {
663 LinkedList parameter = new LinkedList();
664 parameter.add(users);
665 parameter.add(deleted);
666
667 BugBaseQueryResult result =
668 (BugBaseQueryResult) doQuery(createQuery(BugBaseQuery.QUERY_SAVE_USERDATA,
669 parameter));
670 if (result.getQueryType() == BugBaseQueryResult.QR_FAILED) {
671 if (result.getResult() instanceof BugBaseException) {
672 BugBaseException ex = (BugBaseException) result.getResult();
673 throw ex;
674 }
675
676 throw (new BugBaseException("xx", result.getResult().toString(),
677 "1.16", "Cannot save user data."));
678 } else {
679 // Set the user to state clean
680 if (users != null) {
681 Iterator it = users.iterator();
682 while (it.hasNext()) {
683 User user = (User) it.next();
684 user.setClean();
685 }
686 }
687 }
688
689 }
690
691
692 /**
693 * Test the connection using a question to the
694 * {@link openfuture.bugbase.servlet.BugBaseServlet BugBaseServlet}.
695 *
696 * @param question the question to be posed
697 * @return the answer
698 * @exception BugBaseException if an error occurs
699 */
700 public String test(String question) throws BugBaseException {
701 LinkedList parameter = new LinkedList();
702 parameter.add(question);
703 BugBaseQuery query = createQuery(BugBaseQuery.QUERY_TEST,
704 parameter);
705
706 BugBaseQueryResult result = doQuery(query);
707
708 if (result.getQueryType() == BugBaseQueryResult.QR_FAILED) {
709 throw (new BugBaseException("xx", result.getResult().toString(),
710 "1.17", "Test failed."));
711 } else {
712 return ((String) result.getResult());
713 }
714 }
715
716 /**
717 * Retrieve the list of doctors and project packages
718 *
719 * @param project project name
720 * @return masterdata for the given project
721 * @exception BugBaseException if an error occurs
722 */
723 public QueryResultMasterData getMasterData(String project) throws BugBaseException {
724 LinkedList parameter = new LinkedList();
725 parameter.add(project);
726 BugBaseQuery query = createQuery(BugBaseQuery.QUERY_MASTERDATA,
727 parameter);
728
729 BugBaseQueryResult result = doQuery(query);
730
731 if (result.getQueryType() == BugBaseQueryResult.QR_FAILED) {
732 throw (new BugBaseException("xx", result.getResult().toString(),
733 "1.18", "Cannot retrieve master data."));
734 } else {
735 return ((QueryResultMasterData) result.getResult());
736 }
737 }
738
739
740 /**
741 * Tries to establish a new connection to the database.
742 *
743 * @param login user data
744 * @return user data, if succeeded, null otherwise.
745 * @exception BugBaseException if an error occurs
746 */
747 public User login(Login login) throws BugBaseException {
748 LinkedList parameter = new LinkedList();
749 parameter.add(login);
750
751 BugBaseQueryResult result =
752 (BugBaseQueryResult) doQuery(createQuery(BugBaseQuery.QUERY_LOGIN,
753 parameter));
754 if (result.getQueryType() == BugBaseQueryResult.QR_OK) {
755 return((User) result.getResult());
756 } else {
757 throw (new BugBaseException("xx", result.getResult().toString(),
758 "1.20", "Login failed."));
759
760 }
761 }
762
763
764 /**
765 * Tries to establish a new connection to the database
766 * as administrator.
767 *
768 * @param login user data
769 * @return user data, if succeeded, null otherwise.
770 * @exception BugBaseException if an error occurs
771 */
772 public User adminLogin(Login login) throws BugBaseException {
773 LinkedList parameter = new LinkedList();
774 parameter.add(login);
775
776 BugBaseQueryResult result =
777 (BugBaseQueryResult) doQuery(createQuery(BugBaseQuery.QUERY_ADMIN_LOGIN,
778 parameter));
779 if (result.getQueryType() == BugBaseQueryResult.QR_OK) {
780 return((User) result.getResult());
781 } else {
782 throw (new BugBaseException("xx", result.getResult().toString(),
783 "1.20", "Login failed."));
784
785 }
786 }
787
788
789 /**
790 * Tries to establish a new connection to the database
791 * as doctor.
792 *
793 * @param login user data
794 * @return user data, if succeeded, null otherwise.
795 * @exception BugBaseException if an error occurs
796 */
797 public User doctorLogin(Login login) throws BugBaseException {
798 LinkedList parameter = new LinkedList();
799 parameter.add(login);
800
801 BugBaseQueryResult result =
802 (BugBaseQueryResult) doQuery(createQuery(BugBaseQuery.QUERY_DOCTOR_LOGIN,
803 parameter));
804 if (result.getQueryType() == BugBaseQueryResult.QR_OK) {
805 return((User) result.getResult());
806 } else {
807 throw (new BugBaseException("xx", result.getResult().toString(),
808 "1.20", "Login failed."));
809
810 }
811 }
812
813
814
815 /**
816 * Check, if the user belongs to the group 'doctors'.
817 *
818 * @param user user data
819 * @return true, if the user is an bugbase doctor
820 */
821 public boolean validDoctorUser(User user) {
822 if (user == null) return false;
823
824 LinkedList groups = user.getGroupList();
825 if (groups == null) return false;
826 else return (groups.contains("doctors"));
827 }
828
829
830
831 /**
832 * Change the current users password.
833 *
834 * @param login current login information
835 * @param newPassword new password
836 * @exception BugBaseException if an error occurs
837 */
838 public void changePassword(Login login, String newPassword) throws BugBaseException {
839 LinkedList parameter = new LinkedList();
840 parameter.add(login);
841 parameter.add(newPassword);
842
843 BugBaseQueryResult result =
844 (BugBaseQueryResult) doQuery(createQuery(BugBaseQuery.QUERY_CHANGE_PASSWORD,
845 parameter));
846 if (result.getQueryType() == BugBaseQueryResult.QR_FAILED) {
847 throw (new BugBaseException("xx", result.getResult().toString(),
848 "1.19", "Cannot change password."));
849 }
850 }
851
852
853
854
855 /**
856 * Add an attachment to a given bug report
857 *
858 * @param reportID the ID of the bug report
859 * @param attachmentPath relative path where the attachment file
860 * is located.
861 * @exception BugBaseException if an error occurs
862 */
863 public void addAttachment(Integer reportID, String attachmentPath)
864 throws BugBaseException {
865 LinkedList parameter = new LinkedList();
866 parameter.add(reportID);
867 parameter.add(attachmentPath);
868
869 BugBaseQueryResult result =
870 (BugBaseQueryResult) doQuery(createQuery(BugBaseQuery.QUERY_ADD_ATTACHMENT,
871 parameter));
872 if (result.getQueryType() == BugBaseQueryResult.QR_FAILED) {
873 throw (new BugBaseException("xx", result.getResult().toString(),
874 "1.23", "Cannot add attachment."));
875 }
876 }
877
878
879 /**
880 * Retrieve the status information from the
881 * {@link openfuture.bugbase.servlet.BugBaseServlet BugBaseServlet}.
882 *
883 * @return a <code>Hashtable</code> holding the information
884 * as key/value pair.
885 * @exception BugBaseException if an error occurs
886 */
887 public Hashtable getStatus() throws BugBaseException {
888 BugBaseQueryResult result =
889 (BugBaseQueryResult) doQuery(createQuery(BugBaseQuery.QUERY_STATUS,
890 null));
891 if (result.getQueryType() == BugBaseQueryResult.QR_OK) {
892 return((Hashtable) result.getResult());
893 } else {
894 throw (new BugBaseException("xx", result.getResult().toString(),
895 "1.24", "Cannot check status."));
896 }
897 }
898
899 /**
900 * Execute a query and set the session ID obtained by
901 * the query result.
902 *
903 * @param query a <code>BugBaseQuery</code> value
904 * @return result of the query
905 * @exception BugBaseException if an error occurs
906 */
907 private BugBaseQueryResult doQuery(BugBaseQuery query) throws BugBaseException {
908
909 BugBaseQueryResult result = (BugBaseQueryResult) service(query);
910 if (result != null) {
911 setSessionID(result.getSessionID());
912 }
913 return(result);
914 }
915
916
917 /**
918 * Creates a new query instance and sets its session ID.
919 *
920 * @param queryType query type
921 * @param parameter query parameter
922 * @return new instance
923 * @see openfuture.bugbase.xobjects.BugBaseQuery
924 */
925 private BugBaseQuery createQuery(int queryType, LinkedList parameter) {
926 BugBaseQuery query = new BugBaseQuery(queryType, parameter);
927 query.setSessionID(getSessionID());
928
929 return query;
930 }
931
932
933 /**
934 * Execute a query over a HTTP connection. Both the query and
935 * the query result are serialized.
936 *
937 * @param request query object
938 * @return query result object
939 * @exception BugBaseException if an error occurs
940 */
941 private Serializable service(Serializable request) throws BugBaseException {
942 URLConnection urlc = null;
943
944 try {
945 urlc = url.openConnection();
946
947 urlc.setDoInput(true);
948 urlc.setDoOutput(true);
949
950 // write request to servlet
951 ObjectOutputStream out =
952 new ObjectOutputStream(urlc.getOutputStream());
953
954 out.writeObject(request);
955 out.close();
956
957 // read the answer
958 ObjectInputStream in =
959 new ObjectInputStream(urlc.getInputStream());
960 return ((Serializable) in.readObject());
961
962 } catch (IOException exp) {
963 exp.printStackTrace();
964 return null;
965 } catch (ClassNotFoundException exp) {
966 exp.printStackTrace();
967 return null;
968 }
969
970 }
971
972
973
974 /**
975 * Get the value of sessionID.
976 * @return Value of sessionID.
977 */
978 public Integer getSessionID() {return sessionID;}
979
980 /**
981 * Set the value of sessionID, if either the current session ID
982 * is null or the given value is not null.
983 * @param v Value to assign to sessionID.
984 */
985 public void setSessionID(Integer v) {
986 if (this.sessionID == null || v != null) {
987 this.sessionID = v;
988 }
989 }
990 } // BugBaseServletClient