/Users/lyon/j4p/src/addBk/addressBook/AddressBookDatabase.java

1    package addBk.addressBook; 
2     
3    // AddressBookDatabase 
4     
5    import java.awt.*; 
6    import java.io.*; 
7    import java.util.Vector; 
8    import java.util.zip.GZIPInputStream; 
9    import java.util.zip.GZIPOutputStream; 
10    
11   public class AddressBookDatabase 
12           implements Serializable { 
13       private Vector v = new Vector(); 
14       private int recordNumber = 0; 
15    
16       public AddressBookDatabase() { 
17           addRecord(new AddressBookRecord()); 
18       } 
19    
20    
21       public void sort() { 
22           //System.out.println("sorting"); 
23           //***** Un comment on JDK 1.2 or better to sort 
24           //Collections.sort(v); 
25       } 
26    
27       public AddressBookRecord getEmptyDbRecord() { 
28           AddressBookRecord abr = 
29                   new AddressBookRecord(); 
30           abr.name = "--- Empty Database ---"; 
31           abr.address = 
32                   "Use the <File> menu to\n <Open> a database object,\n <Merge> a CSV file"; 
33           abr.notes = "Or use the [EDIT] button to\n [Add Record]s to the empty DB"; 
34           abr.dial_1 = ""; 
35           abr.dial_2 = ""; 
36           abr.dial_3 = ""; 
37           return (abr); 
38       } 
39    
40       public AddressBookRecord readRecord() { 
41           return (AddressBookRecord) v.elementAt(recordNumber); 
42       } 
43    
44       public AddressBookRecord readRecord(int recNumber) { 
45           return (AddressBookRecord) v.elementAt(recNumber); 
46       } 
47    
48    
49       public void addRecord(AddressBookRecord abr) { 
50           v.addElement(abr); 
51           recordNumber = v.size() - 1; 
52       } 
53    
54       public void deleteRecord(AddressBookRecord abr) { 
55           v.removeElement(abr); 
56       } 
57    
58    
59       public void saveCurrentRecord(AddressBookRecord abr) { 
60           try { 
61               v.setElementAt(abr, recordNumber); 
62           } catch (ArrayIndexOutOfBoundsException e) { 
63    
64           } 
65       } 
66       public int getNumRecords() { 
67           return (v.size()); 
68       } 
69       public void deleteCurrentRecord() { 
70           v.removeElementAt(recordNumber); 
71       } 
72    
73       public void incrementRecord() { 
74           if (recordNumber < (v.size() - 1)) 
75               recordNumber++; 
76           System.out.println(recordNumber); 
77       } 
78    
79       public void decrementRecord() { 
80           if (recordNumber > 0) 
81               recordNumber--; 
82           System.out.println(recordNumber); 
83       } 
84    
85       public void setRecordNumber(int index) { 
86           if ((index >= 0) && (index <= v.size())) 
87               recordNumber = index; 
88           System.out.println(recordNumber); 
89       } 
90    
91       public void openDb() { 
92           try { 
93               System.out.println("Opening Address Db Object"); 
94               readDbObject();     // gzip format 
95               //readFromFile();   // object w/o gzip 
96           } catch (IOException e) { 
97               System.out.println("IOException"); 
98           } catch (ClassNotFoundException e) { 
99               System.out.println("ClassNotFoundException"); 
100          } 
101      } 
102   
103      public void saveDb() { 
104          try { 
105              System.out.println("Saving Address Db Object"); 
106              saveDbObject();     // gzip format 
107              //writeToFile();    // object w/o gzip 
108          } catch (IOException e) { 
109              System.out.println("IOERROR: " + e.getMessage()); 
110          } 
111      } 
112   
113      public void writeToFile() throws IOException { 
114          FileOutputStream outStream = new FileOutputStream("junkobj"); 
115          System.out.println("FileOutputStream OK"); 
116          ObjectOutputStream ooStream = new ObjectOutputStream(outStream); 
117          System.out.println("ObjectOutputStream OK"); 
118          ooStream.writeObject(this); 
119          ooStream.flush(); 
120          ooStream.close(); 
121          outStream.close(); 
122      } // writeToFile() 
123   
124      public void readFromFile() throws IOException, ClassNotFoundException { 
125          FileInputStream inStream = new FileInputStream("junkobj"); 
126          ObjectInputStream oiStream = new ObjectInputStream(inStream); 
127          AddressBookDatabase abd = (AddressBookDatabase) oiStream.readObject(); 
128          this.v = abd.v; 
129      } // readFromFile() 
130   
131      public void readDbObject() throws IOException 
132              , ClassNotFoundException { 
133          System.out.println("starting read..."); 
134          FileInputStream fis 
135                  = new FileInputStream(Futil.getReadFileName("Select input file")); 
136          GZIPInputStream gis 
137                  = new GZIPInputStream(fis); 
138          ObjectInputStream ois 
139                  = new ObjectInputStream(gis); 
140          AddressBookDatabase abd = (AddressBookDatabase) ois.readObject(); 
141          this.v = abd.v; 
142          ois.close(); 
143          gis.close(); 
144          fis.close(); 
145          System.out.println("restored v.size: " + this.v.size()); 
146      }// readObject 
147   
148   
149      public void saveDbObject() throws IOException { 
150          System.out.println("starting save..."); 
151          FileOutputStream fos 
152                  = new FileOutputStream(Futil.getWriteFileName("Select output file")); 
153          GZIPOutputStream gos 
154                  = new GZIPOutputStream(fos); 
155          ObjectOutputStream oos 
156                  = new ObjectOutputStream(gos); 
157          oos.writeObject(this); 
158          System.out.println("working with save..."); 
159          oos.flush(); 
160          oos.close(); 
161          gos.finish(); 
162          fos.close(); 
163          System.out.println("done with save..."); 
164          System.out.println("saved v.size: " + this.v.size()); 
165      } 
166   
167      static class Futil { 
168          public static String getReadFileName( 
169                  String prompt) { 
170              FileDialog fd = new FileDialog( 
171                      new Frame(), prompt); 
172              fd.setVisible(true); 
173              fd.setVisible(false); 
174              return fd.getDirectory() + fd.getFile(); 
175          } 
176   
177          public static String getWriteFileName( 
178                  String prompt) { 
179              FileDialog fd = new FileDialog( 
180                      new Frame(), prompt, FileDialog.SAVE); 
181              fd.setVisible(true); 
182              fd.setVisible(false); 
183              return fd.getDirectory() + fd.getFile(); 
184          } 
185      } 
186   
187  }