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 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 35 36 public AddressBook() {} 37 38 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 75 public void setId(int id) { 76 77 this.addBookId = id; 78 } 79 80 81 86 public int getId() { 87 88 return addBookId; 89 }; 90 91 92 97 public void setOwnerId(int ownerId) { 98 99 this.ownerId = ownerId; 100 } 101 102 103 108 public int getOwnerId() { 109 110 return ownerId; 111 }; 112 113 114 119 public void setContacts(Vector contacts) { 120 this.contacts = contacts; 121 } 122 123 124 129 public Vector getContacts() { 130 131 return this.contacts; 132 } 133 134 135 140 public Vector getCategories() { 141 142 return this.categories; 143 } 144 145 150 public void setCategories(Vector cats) { 151 152 this.categories = cats; 153 } 154 155 156 161 public Vector getComms() { 162 163 return this.comms; 164 } 165 166 171 public void setComms(Vector comms) { 172 173 this.comms = comms; 174 } 175 176 177 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 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 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 243 public String 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 263 public String 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 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 298 public Contact searchContactById(int id) { 299 300 return AdbApplicationDB.SearchContactById(id); 301 } 302 303 304 315 public Vector search( String field, String value, String sortField, String sortOrder, int addbookId ) { 316 317 Vector foundContacts = new Vector(); 318 Contact contact=null; 319 String lastname; 320 String 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 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 378 public void addContact( Contact newContact ) { 379 380 Tools.toConsole("AddressBook: addContact","begin"); 381 AdbApplicationDB.insertContact(newContact); 382 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 395 public void removeContact(int contactId) { 396 Tools.toConsole("AddressBook: removeContact","begin"); 397 this.databook.delete(contactId); 399 Tools.toConsole("AddressBook: removeContact","contact deleted from disk"); 400 401 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 417 public void changeContact( int contactId, Contact newContact) { 418 419 Tools.toConsole("AddressBook: changeContact", "begin"); 420 421 int pos = this.getContactPos(contactId); 423 if (pos != -1) { 424 this.contacts.setElementAt(newContact,pos); 425 } 426 this.databook.update(contactId, newContact); 428 Tools.toConsole("AddressBook: changeContact", "end"); 429 } 430 431 432 433 434 440 public void renameCategory(int catId, String catName) { 441 442 this.databook.renameCategory(catId, catName); 443 int id=this.getId(); 444 setCategories(AdbApplicationDB.loadCategories(id)); 445 } 446 447 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 468 public String readCategory(int id) { 469 String s = "Category"; 470 471 return this.databook.readData("category",id ); 472 } 473 474 475 480 482 485 486 491 public void save(String filename) { 492 493 this.databook.save(filename); 494 } 495 496 497 } 498 499 | Popular Tags |