KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sellwin > server > LocalDB


1 package sellwin.server;
2
3 import sellwin.domain.*;
4 import java.util.*;
5 import java.io.*;
6
7 // SellWin http://sourceforge.net/projects/sellwincrm
8
//Contact support@open-app.com for commercial help with SellWin
9
//This software is provided "AS IS", without a warranty of any kind.
10

11 /**
12  * This class runs on the user's client machine when
13  * operating SellWin in 'disconnected' mode. This class
14  * specifically simulates a simple database that holds
15  * all downloaded SellWin data.
16  */

17 public class LocalDB {
18     private String JavaDoc BASE;
19     public final static String JavaDoc EXTENSION = ".opp";
20     public final static String JavaDoc F_INDEX = "index.objs";
21     public final static String JavaDoc F_ROLES = "role.objs";
22     public final static String JavaDoc F_GROPS = "group.objs";
23     public final static String JavaDoc F_SPERS = "salesPerson.objs";
24     public final static String JavaDoc F_PRODS = "product.objs";
25     public final static String JavaDoc F_CAMPS = "campaign.objs";
26     public final static String JavaDoc F_CUSTS = "customer.objs";
27     public final static String JavaDoc F_TAX = "tax.objs";
28     public final static String JavaDoc F_DELETE = "deleted.objs";
29
30     /**
31      * construct a local database object
32      * @param base the base pathname used to find local
33      * data store files
34      */

35     public LocalDB(String JavaDoc base) {
36         BASE = base;
37     }
38
39     /**
40      * set the base path to the local store files
41      * @param b the base path name
42      */

43     public final void setBASE(String JavaDoc b) {
44         BASE = b;
45     }
46
47     /**
48      * get the base pathname to the local files
49      * @return the base path name
50      */

51     public final String JavaDoc getBASE() {
52         return BASE;
53     }
54
55     /**
56      * get the opportunity index list
57      * @return the ArrayList of OpportunityIndex structure
58      * @exception AngError thrown when an app error occurs
59      */

60     public final ArrayList getOppIndex()
61         throws AngError {
62
63         FileInputStream istream = null;
64         ArrayList oppIndex = null;
65
66         try {
67             istream = new FileInputStream(BASE + F_INDEX);
68             ObjectInputStream p = new ObjectInputStream(istream);
69             oppIndex = (ArrayList)p.readObject();
70         } catch (FileNotFoundException f) {
71             //if not found, then create an emtpy file to start with
72
System.out.println("creating " + BASE + F_INDEX);
73             oppIndex = new ArrayList();
74             saveOppIndex(oppIndex);
75         } catch (Exception JavaDoc e) {
76             e.printStackTrace();
77             throw new AngError(e.getMessage());
78         } finally {
79             try { istream.close(); } catch (Exception JavaDoc x) { }
80         }
81
82         return oppIndex;
83
84     }
85
86     /**
87      * save the opportunity index to local disk
88      * @param oppIndex the ArrayList we are to save
89      * @exception AngError thrown when an app error occurs
90      */

91     public final void saveOppIndex(ArrayList oppIndex)
92         throws AngError {
93
94         FileOutputStream ostream = null;
95
96         try {
97             System.out.println("saving " + BASE + F_INDEX);
98             ostream = new FileOutputStream(BASE + F_INDEX);
99             ObjectOutputStream p = new ObjectOutputStream(ostream);
100             p.writeObject(oppIndex);
101             p.flush();
102         } catch (Exception JavaDoc e) {
103             e.printStackTrace();
104             throw new AngError(e.getMessage());
105         } finally {
106             try { ostream.close(); } catch (Exception JavaDoc x) { }
107         }
108     }
109
110     /**
111      * lookup a single Opportunity
112      * @param oppName the opportunity name as search key
113      * @return the found Opportunity or null if not found
114      * @exception AngError thrown when an app error occurs
115      */

116     public final Opportunity getOpportunity(String JavaDoc oppName)
117         throws AngError {
118
119         FileInputStream istream = null;
120
121         try {
122             istream = new FileInputStream(generateOppFileName(oppName));
123             ObjectInputStream p = new ObjectInputStream(istream);
124             Opportunity o = (Opportunity)p.readObject();
125             return o;
126         } catch (Exception JavaDoc e) {
127             e.printStackTrace();
128             throw new AngError(e.getMessage());
129         } finally {
130             try { istream.close(); } catch (Exception JavaDoc x) { }
131         }
132     }
133
134     /**
135      * find an Opportunity by primary key
136      * @param pk the primary key of the Opportunity to search with
137      * @return the found Opportunity or null if not found
138      * @exception AngError thrown when an app error occurs
139      */

140     public final Opportunity getOpportunity(long pk)
141         throws AngError {
142
143         FileInputStream istream = null;
144
145         try {
146             istream = new FileInputStream(generateOppFileName(pk));
147             ObjectInputStream p = new ObjectInputStream(istream);
148             Opportunity o = (Opportunity)p.readObject();
149             return o;
150         } catch (Exception JavaDoc e) {
151             e.printStackTrace();
152             throw new AngError(e.getMessage());
153         } finally {
154             try { istream.close(); } catch (Exception JavaDoc x) { }
155         }
156     }
157
158     /**
159      * lookup an Opportunity by primary key and by using
160      * the opportunity index
161      * @param index the opportunity index structure to search in
162      * @param pk the primary key to search with
163      * @return the index into the opportunity index that matches
164      * @exception AngError thrown when an app error occurs
165      */

166     public int findOpp(ArrayList index, long pk) {
167
168         OppIndex oi = null;
169         for (int i=0;i<index.size();i++) {
170             oi = (OppIndex)index.get(i);
171             if (oi.getPK() == pk)
172                 return i;
173         }
174
175         return -1; //not found
176
}
177
178     /**
179      * save an Opportunity to local disk
180      * @param o the Opportunity to save
181      * @exception AngError thrown when an app error occurs
182      */

183     public final void saveOpportunity(Opportunity o)
184         throws AngError {
185
186         FileOutputStream ostream = null;
187
188         try {
189             System.out.println("saving "+ generateOppFileName(o.getPK()));
190             ostream = new FileOutputStream(generateOppFileName(o.getPK()));
191             ObjectOutputStream p = new ObjectOutputStream(ostream);
192             p.writeObject(o);
193             p.flush();
194
195             //save the new opp to the index file for future reads
196
ArrayList index = getOppIndex();
197             int i = findOpp(index, o.getPK());
198             OppIndex oi = null;
199             if (i >= 0) { //already exists so update index
200
oi = (OppIndex)index.get(i);
201                 oi.setName(o.getName());
202             } else { //doesn't exist so add to index
203
oi = new OppIndex(o.getName(), o.getStage(), o.getPK());
204                 index.add(oi);
205             }
206             saveOppIndex(index);
207         } catch (Exception JavaDoc e) {
208             e.printStackTrace();
209             throw new AngError(e.getMessage());
210         } finally {
211             try { ostream.close(); } catch (Exception JavaDoc x) { }
212         }
213     }
214
215     /**
216      * get the Opportunity Index using an Opportunity name
217      * @param oppName search with an Opportunity name
218      * @return the matching OppIndex
219      * @exception AngError thrown when an app error occurs
220      */

221     private OppIndex getOppIndex(String JavaDoc oppName)
222         throws AngError {
223         OppIndex oi = null;
224         ArrayList index = getOppIndex();
225         for (int i=0;i<index.size();i++) {
226             oi = (OppIndex)index.get(i);
227             if (oi.getName().equals(oppName))
228                 return oi;
229         }
230
231         throw new AngError("not found not correct");
232     }
233
234     /**
235      * generate a file name based upon an opportunity name
236      * @param oppName the Opportunity name to generate for
237      * @return a String containing the generated file name
238      * @exception AngError thrown when an app error occurs
239      */

240     public final String JavaDoc generateOppFileName(String JavaDoc oppName)
241         throws AngError {
242
243         ArrayList index = getOppIndex();
244         OppIndex oi = getOppIndex(oppName);
245         return generateOppFileName(oi.getPK());
246     }
247
248     /**
249      * generate an opportunity file name using a primary
250      * key value
251      * @param pk the primary key used to generate the file name
252      * @return a String containing the generated file name
253      * @exception AngError thrown when an app error occurs
254      */

255     public final String JavaDoc generateOppFileName(long pk) {
256         return new String JavaDoc(BASE + pk + EXTENSION);
257     }
258
259     /**
260      * get the list of UserRoles from local disk
261      * @return the ArrayList of UserRoles
262      * @exception AngError thrown when an app error occurs
263      */

264     public final ArrayList getRoleList()
265         throws AngError {
266
267         FileInputStream istream=null;
268         ArrayList roleList = null;
269
270         try {
271             istream = new FileInputStream(BASE + F_ROLES);
272             ObjectInputStream p = new ObjectInputStream(istream);
273             roleList = (ArrayList)p.readObject();
274         } catch (FileNotFoundException f) {
275             System.out.println("creating " + BASE + F_ROLES);
276             roleList = new ArrayList();
277             saveRoleList(roleList);
278         } catch (Exception JavaDoc e) {
279             e.printStackTrace();
280             throw new AngError(e.getMessage());
281         } finally {
282             try { istream.close(); } catch (Exception JavaDoc x) { }
283         }
284         return roleList;
285     }
286
287     /**
288      * save to local disk the list of UserRoles
289      * @param roles the ArrayList of UserRoles to svae
290      * @exception AngError thrown when an app error occurs
291      */

292     public final void saveRoleList(ArrayList roles)
293         throws AngError {
294
295         FileOutputStream ostream = null;
296
297         try {
298             System.out.println("saving " + BASE + F_ROLES);
299             ostream = new FileOutputStream(BASE + F_ROLES);
300             ObjectOutputStream p = new ObjectOutputStream(ostream);
301             p.writeObject(roles);
302             p.flush();
303         } catch (Exception JavaDoc e) {
304             throw new AngError(e.getMessage());
305         } finally {
306             try { ostream.close(); } catch (Exception JavaDoc x) { }
307         }
308     }
309
310     /**
311      * get the ArrayList of UserGroups from local disk
312      * @return the ArrayList that was retrieved
313      * @exception AngError thrown when an app error occurs
314      */

315     public final ArrayList getGroupList()
316         throws AngError {
317
318         FileInputStream istream = null;
319         ArrayList groupList = null;
320
321         try {
322             istream = new FileInputStream(BASE + F_GROPS);
323             ObjectInputStream p = new ObjectInputStream(istream);
324             groupList = (ArrayList)p.readObject();
325         } catch (FileNotFoundException f) {
326             System.out.println("creating " + BASE + F_GROPS);
327             groupList = new ArrayList();
328             saveGroupList(groupList);
329         } catch (Exception JavaDoc e) {
330             e.printStackTrace();
331             throw new AngError(e.getMessage());
332         } finally {
333             try { istream.close(); } catch (Exception JavaDoc x) { }
334         }
335         return groupList;
336     }
337
338     /**
339      * save the user group list to local disk
340      * @param groups the ArrayList of UserGroups to save
341      * @exception AngError thrown when an app error occurs
342      */

343     public final void saveGroupList(ArrayList groups)
344         throws AngError {
345
346         FileOutputStream ostream = null;
347
348         try {
349             System.out.println("saving " + BASE + F_GROPS);
350             ostream = new FileOutputStream(BASE + F_GROPS);
351             ObjectOutputStream p = new ObjectOutputStream(ostream);
352             p.writeObject(groups);
353             p.flush();
354         } catch (Exception JavaDoc e) {
355             throw new AngError(e.getMessage());
356         } finally {
357             try { ostream.close(); } catch (Exception JavaDoc x) { }
358         }
359     }
360
361     /**
362      * get the ArrayList of Products from local disk
363      * @return the Product list
364      * @exception AngError thrown when an app error occurs
365      */

366     public final ArrayList getProducts()
367         throws AngError {
368
369         FileInputStream istream = null;
370         ArrayList prodList = null;
371
372         try {
373             istream = new FileInputStream(BASE + F_PRODS);
374             ObjectInputStream p = new ObjectInputStream(istream);
375             prodList = (ArrayList)p.readObject();
376         } catch (FileNotFoundException f) {
377             System.out.println("creating " + BASE + F_PRODS);
378             prodList = new ArrayList();
379             saveProducts(prodList);
380         } catch (Exception JavaDoc e) {
381             e.printStackTrace();
382             throw new AngError(e.getMessage());
383         } finally {
384             try { istream.close(); } catch (Exception JavaDoc x) { }
385         }
386         return prodList;
387     }
388
389     /**
390      * save to local disk the list of Products
391      * @param prods the ArrayList of Products to save
392      * @exception AngError thrown when an app error occurs
393      */

394     public final void saveProducts(ArrayList prods)
395         throws AngError {
396
397         FileOutputStream ostream = null;
398
399         try {
400             ostream = new FileOutputStream(BASE + F_PRODS);
401             ObjectOutputStream p = new ObjectOutputStream(ostream);
402             p.writeObject(prods);
403             p.flush();
404         } catch (Exception JavaDoc e) {
405             throw new AngError(e.getMessage());
406         } finally {
407             try { ostream.close(); } catch (Exception JavaDoc x) { }
408         }
409     }
410
411     /**
412      * get the ArrayList of SalesPersons on local disk
413      * @return the ArrayList of SalesPersons
414      * @exception AngError thrown when an app error occurs
415      */

416     public final TreeMap getSalesPersons()
417         throws AngError {
418
419         FileInputStream istream = null;
420         TreeMap salesPersonList = null;
421
422         try {
423             istream = new FileInputStream(BASE + F_SPERS);
424             ObjectInputStream p = new ObjectInputStream(istream);
425             salesPersonList = (TreeMap)p.readObject();
426         } catch (FileNotFoundException f) {
427             System.out.println("creating " + BASE + F_SPERS);
428             salesPersonList = new TreeMap();
429             saveSalesPersons(salesPersonList);
430         } catch (Exception JavaDoc e) {
431             e.printStackTrace();
432             throw new AngError(e.getMessage());
433         } finally {
434             try { istream.close(); } catch (Exception JavaDoc x) { }
435         }
436         return salesPersonList;
437     }
438
439     /**
440      * save the SalesPersons to local disk
441      * @param persons the SalesPersons we are saving (funny eh?)
442      * @exception AngError thrown when an app error occurs
443      */

444     public final void saveSalesPersons(TreeMap persons)
445         throws AngError {
446
447         FileOutputStream ostream = null;
448
449         try {
450             ostream = new FileOutputStream(BASE + F_SPERS);
451             ObjectOutputStream p = new ObjectOutputStream(ostream);
452             p.writeObject(persons);
453             p.flush();
454         } catch (Exception JavaDoc e) {
455             throw new AngError(e.getMessage());
456         } finally {
457             try { ostream.close(); } catch (Exception JavaDoc x) { }
458         }
459     }
460
461     /**
462      * get the local ArrayList of Campaigns from disk
463      * @return the ArrayList of Campaigns found on disk
464      * @exception AngError thrown when an app error occurs
465      */

466     public final ArrayList getCampaigns()
467         throws AngError {
468
469         FileInputStream istream = null;
470         ArrayList campaigns = null;
471
472         try {
473             istream = new FileInputStream(BASE + F_CAMPS);
474             ObjectInputStream p = new ObjectInputStream(istream);
475             campaigns = (ArrayList)p.readObject();
476         } catch (FileNotFoundException f) {
477             campaigns = new ArrayList();
478             saveCampaigns(campaigns);
479         } catch (Exception JavaDoc e) {
480             e.printStackTrace();
481             throw new AngError(e.getMessage());
482         } finally {
483             try { istream.close(); } catch (Exception JavaDoc x) { }
484         }
485         return campaigns;
486     }
487
488     /**
489      * save to local disk the list of Campaigns
490      * @param campaigns the ArrayList we save
491      * @exception AngError thrown when an app error occurs
492      */

493     public final void saveCampaigns(ArrayList campaigns)
494         throws AngError {
495
496         FileOutputStream ostream = null;
497
498         try {
499             ostream = new FileOutputStream(BASE + F_CAMPS);
500             ObjectOutputStream p = new ObjectOutputStream(ostream);
501             p.writeObject(campaigns);
502             p.flush();
503         } catch (Exception JavaDoc e) {
504             throw new AngError(e.getMessage());
505         } finally {
506             try { ostream.close(); } catch (Exception JavaDoc x) { }
507         }
508     }
509
510     /**
511      * get the local List of Customers from disk
512      * @return the ArrayList of Customers we found
513      * @exception AngError thrown when an app error occurs
514      */

515     public final ArrayList getCustomers()
516         throws AngError {
517
518         FileInputStream istream = null;
519         ArrayList custList = null;
520
521         try {
522             istream = new FileInputStream(BASE + F_CUSTS);
523             ObjectInputStream p = new ObjectInputStream(istream);
524             custList = (ArrayList)p.readObject();
525         } catch (FileNotFoundException f) {
526             System.out.println("creating " + BASE + F_CUSTS);
527             custList = new ArrayList();
528             saveCustomers(custList);
529         } catch (Exception JavaDoc e) {
530             e.printStackTrace();
531             throw new AngError(e.getMessage());
532         } finally {
533             try { istream.close(); } catch (Exception JavaDoc x) { }
534         }
535         return custList;
536     }
537
538     /**
539      * sava a list of Customers to local disk
540      * @param custs the ArrayList we are saving
541      * @exception AngError thrown when an app error occurs
542      */

543     public final void saveCustomers(ArrayList custs)
544         throws AngError {
545
546         FileOutputStream ostream = null;
547
548         try {
549             ostream = new FileOutputStream(BASE + F_CUSTS);
550             ObjectOutputStream p = new ObjectOutputStream(ostream);
551             p.writeObject(custs);
552             p.flush();
553         } catch (Exception JavaDoc e) {
554             throw new AngError(e.getMessage());
555         } finally {
556             try { ostream.close(); } catch (Exception JavaDoc x) { }
557         }
558     }
559
560     /**
561      * get the ArrayList of 'deletes' from local disk, add the
562      * delete info, and persist to disk the 'deletes'
563      * @param objType class name of object that got deleted
564      * @param pk primary key of object that got deleted
565      * @exception AngError thrown when an app error occurs
566      */

567     public final void writeDelete(DeleteInfo deleteInfo)
568         throws AngError {
569
570         FileInputStream istream = null;
571         ArrayList deleteList = null;
572
573         try {
574             //open file
575
istream = new FileInputStream(BASE + F_DELETE);
576             ObjectInputStream p = new ObjectInputStream(istream);
577             deleteList = (ArrayList)p.readObject();
578
579             //add delete info to list
580
deleteList.add(deleteInfo);
581
582             //save deletes back to disk
583
saveDeletes(deleteList);
584
585         } catch (FileNotFoundException f) {
586             System.out.println("creating " + BASE + F_DELETE);
587             deleteList = new ArrayList();
588             deleteList.add(deleteInfo);
589             saveDeletes(deleteList);
590         } catch (Exception JavaDoc e) {
591             e.printStackTrace();
592             throw new AngError(e.getMessage());
593         } finally {
594             try { istream.close(); } catch (Exception JavaDoc x) { }
595         }
596     }
597
598     /**
599      * save back to disk the list of deletes
600      * @param deletes the list of deletes we want to save
601      */

602     public void saveDeletes(ArrayList deletes)
603         throws AngError {
604
605         FileOutputStream ostream = null;
606         try {
607             ostream = new FileOutputStream(BASE + F_DELETE);
608             ObjectOutputStream p = new ObjectOutputStream(ostream);
609             p.writeObject(deletes);
610             p.flush();
611         } catch (Exception JavaDoc e) {
612             throw new AngError(e.getMessage());
613         } finally {
614             try { ostream.close(); } catch (Exception JavaDoc x) { }
615         }
616     }
617
618     public final ArrayList getDeletes()
619         throws AngError {
620
621         FileInputStream istream = null;
622         ArrayList deletes = null;
623
624         try {
625             istream = new FileInputStream(BASE + F_DELETE);
626             ObjectInputStream p = new ObjectInputStream(istream);
627             deletes = (ArrayList)p.readObject();
628         } catch (FileNotFoundException f) {
629             deletes = new ArrayList();
630             saveDeletes(deletes);
631         } catch (Exception JavaDoc e) {
632             e.printStackTrace();
633             throw new AngError(e.getMessage());
634         } finally {
635             try { istream.close(); } catch (Exception JavaDoc x) { }
636         }
637         return deletes;
638     }
639
640     /**
641      * get the local List of State tax codes from disk
642      * @return the ArrayList of StateTax we found
643      * @exception AngError thrown when an app error occurs
644      */

645     public final ArrayList getStateTax()
646         throws AngError {
647
648         FileInputStream istream = null;
649         ArrayList taxList = null;
650
651         try {
652             istream = new FileInputStream(BASE + F_TAX);
653             ObjectInputStream p = new ObjectInputStream(istream);
654             taxList = (ArrayList)p.readObject();
655         } catch (FileNotFoundException f) {
656             System.out.println("creating " + BASE + F_TAX);
657             taxList = new ArrayList();
658             saveStateTax(taxList);
659         } catch (Exception JavaDoc e) {
660             e.printStackTrace();
661             throw new AngError(e.getMessage());
662         } finally {
663             try { istream.close(); } catch (Exception JavaDoc x) { }
664         }
665         return taxList;
666     }
667
668     /**
669      * sava a list of StateTax to local disk
670      * @param tax the ArrayList we are saving
671      * @exception AngError thrown when an app error occurs
672      */

673     public final void saveStateTax(ArrayList tax)
674         throws AngError {
675
676         FileOutputStream ostream = null;
677
678         try {
679             ostream = new FileOutputStream(BASE + F_TAX);
680             ObjectOutputStream p = new ObjectOutputStream(ostream);
681             p.writeObject(tax);
682             p.flush();
683         } catch (Exception JavaDoc e) {
684             throw new AngError(e.getMessage());
685         } finally {
686             try { ostream.close(); } catch (Exception JavaDoc x) { }
687         }
688     }
689 }
690
Popular Tags