KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > webapps > addressbook > AddressBook


1 package org.jahia.webapps.addressbook;
2
3 import java.util.*;
4 import javax.servlet.*;
5 import javax.servlet.http.*;
6
7 import org.jahia.tools.*;
8 import org.jahia.tools.db.*;
9
10 /**
11  * Class AddressBook: represents the address book interface,
12  * provides the methods for managing the contacts
13  *
14  * @author Jerome Tamiotti <a HREF="mailto:tamiotti@xo3.com">tamiotti@xo3.com</a>
15  * @see org.jahia.webapps.addressbook.DbAddBook
16  * @see org.jahia.webapps.addressbook.Contact
17  */

18 public class AddressBook {
19
20     private int addBookId = 0;
21     private int ownerId = 0;
22     private int contextId= 0;
23     private DbAddBook databook;
24     private Vector contacts = new Vector();
25     private Vector categories = new Vector();
26     private Vector comms = new Vector();
27
28
29
30     /**
31          * The class constructor empty for ojb
32          *
33          * @param owner the name of this address book owner
34          */

35
36 public AddressBook() {}
37
38     /**
39      * The class constructor
40      *
41      * @param owner the name of this address book owner
42      */

43
44
45     public AddressBook (int addBookId, int ownerId) {
46
47         Tools.toConsole("AddressBook: constructor","Begin");
48         this.addBookId = addBookId;
49         this.ownerId = ownerId;
50         this.databook = new DbAddBook(this);
51         setContacts(AdbApplicationDB.loadContacts(addBookId));
52         setCategories(AdbApplicationDB.loadCategories(addBookId));
53         setComms(AdbApplicationDB.loadCommunications());
54         Tools.toConsole("AddressBook: constructor","End");
55     }
56     public AddressBook (int addBookId, int ownerId, int contextId) {
57
58                    Tools.toConsole("AddressBook: constructor","Begin");
59                    this.addBookId = addBookId;
60                    this.ownerId = ownerId;
61                    this.contextId =contextId;
62                    this.databook = new DbAddBook(this);
63                    setContacts(AdbApplicationDB.loadContacts(addBookId));
64                    setCategories(AdbApplicationDB.loadCategories(addBookId));
65                    setComms(AdbApplicationDB.loadCommunications());
66                    Tools.toConsole("AddressBook: constructor","End");
67        }
68
69
70     /**
71      * Set the id value
72      *
73      * @param name the id
74      */

75     public void setId(int id) {
76
77         this.addBookId = id;
78     }
79
80
81     /**
82      * Get the address book id
83      *
84      * @return the id
85      */

86     public int getId() {
87
88         return addBookId;
89     };
90
91
92     /**
93      * Set the owner value
94      *
95      * @param ownerId
96      */

97     public void setOwnerId(int ownerId) {
98
99         this.ownerId = ownerId;
100     }
101
102
103     /**
104      * Get the address book owner id
105      *
106      * @return the owner id
107      */

108     public int getOwnerId() {
109
110         return ownerId;
111     };
112
113
114     /**
115      * Set the list of contacts
116      *
117      * @param contacts a set of contacts
118      */

119     public void setContacts(Vector contacts) {
120         this.contacts = contacts;
121     }
122
123
124     /**
125      * Get the list of contacts
126      *
127      * @return a vector containing the contacts
128      */

129     public Vector getContacts() {
130
131         return this.contacts;
132     }
133
134
135     /**
136      * Get the list of categories
137      *
138      * @return a vector containing the categories
139      */

140     public Vector getCategories() {
141
142         return this.categories;
143     }
144
145     /**
146      * Set the list of categories
147      *
148      * @param a vector containing the categories
149      */

150     public void setCategories(Vector cats) {
151
152         this.categories = cats;
153     }
154
155
156     /**
157      * Get the list of comms
158      *
159      * @return a vector containing the comms
160      */

161     public Vector getComms() {
162
163         return this.comms;
164     }
165
166     /**
167      * Set the list of comms
168      *
169      * @param a vector containing the comms
170      */

171     public void setComms(Vector comms) {
172
173         this.comms = comms;
174     }
175
176
177     /**
178      * Return the position of the contact whose id = 'id'
179      * or -1 if not found
180      *
181      * @return the position of the wanted contact
182      */

183     public int getContactPos(int id) {
184
185         for(int i=0; i<this.contacts.size(); i++) {
186
187             Contact c = (Contact)this.contacts.get(i);
188             if (c.getId() == id) {
189                 return i;
190             }
191         }
192         return -1;
193     }
194
195
196     /**
197      * Return the position of the category whose id = 'id'
198      * or -1 if not found
199      *
200      * @param the id of the wanted category
201      * @return the position of the wanted category
202      */

203     public int getCatPos(int id) {
204
205         for(int i=0; i<this.categories.size(); i++) {
206
207             Category c = (Category)this.categories.get(i);
208             if (c.getId() == id) {
209                 return i;
210             }
211         }
212         return -1;
213     }
214
215
216     /**
217      * Return the position of the communication whose id = 'id'
218      * or -1 if not found
219      *
220      * @param the id of the wanted communication
221      * @return the position of the wanted communication
222      */

223     public int getCommPos(int id) {
224
225         for(int i=0; i<this.comms.size(); i++) {
226
227             Communication c = (Communication)this.comms.get(i);
228             if (c.getId() == id) {
229                 return i;
230             }
231         }
232         return -1;
233     }
234
235
236     /**
237      * Return the name of the category whose id = 'id'
238      * or null if not found
239      *
240      * @param the id of the wanted category
241      * @return the position of the wanted category
242      */

243     public String JavaDoc getCatName(int id) {
244
245         for(int i=0; i<this.categories.size(); i++) {
246
247             Category c = (Category)this.categories.get(i);
248             if (c.getId() == id) {
249                 return c.getName();
250             }
251         }
252         return "";
253     }
254
255
256     /**
257      * Return the name of the communication whose id = 'id'
258      * or null if not found
259      *
260      * @param the id of the wanted communication
261      * @return the position of the wanted communication
262      */

263     public String JavaDoc getCommName(int id) {
264
265         for(int i=0; i<this.comms.size(); i++) {
266
267             Communication c = (Communication)this.comms.get(i);
268             if (c.getId() == id) {
269                 return c.getName();
270             }
271         }
272         return "";
273     }
274
275
276     /**
277      * Return the in-memory contact given its id
278      *
279      * @param id the id to find
280      * @return the wanted contact
281      */

282     public Contact getContactById(int id) {
283
284         int pos = this.getContactPos(id);
285         if (pos != -1) {
286             return (Contact)this.contacts.get(pos);
287         }
288         return null;
289     }
290
291
292     /**
293      * Search on disk a contact given its id
294      *
295      * @param id the id to find
296      * @return the wanted contact
297      */

298     public Contact searchContactById(int id) {
299
300         return AdbApplicationDB.SearchContactById(id);
301     }
302
303
304    /**
305      * Search for contacts given these following criteria
306      * each name in 'fields' must contain the matching word in 'values'
307      *
308      * @param fields the fields to change
309      * @param values the values to match
310      * @param sortField the field to sort by
311      * @param sortOrder the order in which to sort
312      *
313      * @return the wanted contacts
314      */

315     public Vector search( String JavaDoc field, String JavaDoc value, String JavaDoc sortField, String JavaDoc sortOrder, int addbookId ) {
316
317       Vector foundContacts = new Vector();
318       Contact contact=null;
319       String JavaDoc lastname;
320       String JavaDoc firstname;
321       if( field.equals("all")) {
322         foundContacts = AdbApplicationDB.getSearchAll(value, addbookId);
323
324       }
325       if (field.equals("last_name")) {
326         foundContacts = AdbApplicationDB.getSearchLast(value, addbookId);
327       }
328       if (field.equals("first_name")) {
329         foundContacts = AdbApplicationDB.getSearchFirst(value, addbookId);
330       }
331
332       if (field.equals("address")) {
333         foundContacts = AdbApplicationDB.getSearchAddress(value, addbookId);
334       }
335       if (field.equals("city")) {
336         foundContacts = AdbApplicationDB.getSearchCity(value, addbookId);
337       }
338
339       if (field.equals("company")) {
340         foundContacts = AdbApplicationDB.getSearchCompany(value, addbookId);
341       }
342      if (field.equals("")) {
343        if(sortField.equals("last_name"))
344        foundContacts = AdbApplicationDB.loadContacts(this.addBookId);
345        if(sortField.equals("category"))
346        foundContacts = AdbApplicationDB.loadContactsCat(this.addBookId);
347      }
348
349
350       return foundContacts;
351     }
352
353
354     /**
355      * Method findPos: find the pos where to insert the contact,
356      * in order they are sorted alphabetically
357      *
358      * @param the contact to insert
359      * @return the position where to insert
360      */

361     public int findPos (Contact theContact) {
362
363         for(int i = 0; i < this.contacts.size(); i++) {
364             Contact c = (Contact)this.contacts.get(i);
365             if (c.getLastName().compareToIgnoreCase(theContact.getLastName()) > 0) {
366                 return i;
367             }
368         }
369         return this.contacts.size();
370     }
371
372
373     /**
374      * Add a contact in the address book
375      *
376      * @param new_contact the new contact to insert
377      */

378     public void addContact( Contact newContact ) {
379
380         Tools.toConsole("AddressBook: addContact","begin");
381                 AdbApplicationDB.insertContact(newContact);
382         //int id = this.databook.insert(newContact);
383
Tools.toConsole("AddressBook: addContact","insertion done");
384                 int id= this.getId();
385         setContacts(AdbApplicationDB.loadContacts(id));
386         Tools.toConsole("AddressBook: addContact","end");
387     }
388
389
390     /**
391      * Remove a contact from the address book
392      *
393      * @param contact_id the id of the contact to remove
394      */

395     public void removeContact(int contactId) {
396         Tools.toConsole("AddressBook: removeContact","begin");
397         // remove from disk
398
this.databook.delete(contactId);
399         Tools.toConsole("AddressBook: removeContact","contact deleted from disk");
400
401         // get pos in in-memory address book
402
int pos = this.getContactPos(contactId);
403         Tools.toConsole("AddressBook: removeContact","pos in memory = " + pos);
404         if (pos != -1) {
405             this.contacts.removeElementAt(pos);
406         }
407     }
408
409
410     /**
411      * Check the differences between the two contacts
412      * and update the fields which are not the same
413      *
414      * @param contactId the id of the contact to replace
415      * @param newContact the new contact
416      */

417     public void changeContact( int contactId, Contact newContact) {
418
419         Tools.toConsole("AddressBook: changeContact", "begin");
420
421         // change the data in memory
422
int pos = this.getContactPos(contactId);
423         if (pos != -1) {
424             this.contacts.setElementAt(newContact,pos);
425         }
426         // update on disk
427
this.databook.update(contactId, newContact);
428         Tools.toConsole("AddressBook: changeContact", "end");
429    }
430
431
432
433
434     /**
435      * Rename a category
436      *
437      * @param catId the id of the category to rename
438      * @param catName the new name
439      */

440     public void renameCategory(int catId, String JavaDoc catName) {
441
442         this.databook.renameCategory(catId, catName);
443                 int id=this.getId();
444         setCategories(AdbApplicationDB.loadCategories(id));
445     }
446
447     /**
448      * Delete a category
449      *
450      * @param catId the id of the category to remove
451      */

452     public void deleteCategory(int catId) {
453
454         int pos = getCatPos(catId);
455
456         if (pos != -1) {
457             this.categories.removeElementAt(pos);
458         }
459         this.databook.deleteCategory(catId);
460     }
461
462
463     /**
464      * Read a particular category
465      *
466      * @return the category label
467      */

468     public String JavaDoc readCategory(int id) {
469       String JavaDoc s = "Category";
470
471         return this.databook.readData("category",id );
472     }
473
474
475     /**
476      * Read all the categories
477      *
478      * @return a hashtable whose keys are categories ids and values their labels
479      */

480     //public Hashtable readCategories() {
481

482         //return this.databook.readAllData("category");
483
//}
484

485
486     /**
487      * Method save : save the content of the address book in a flat file
488      *
489      * @param filepath the full path of the file to create
490      */

491     public void save(String JavaDoc filename) {
492
493         this.databook.save(filename);
494     }
495
496
497 }
498
499
Popular Tags