KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jspPhoneBook > business > PersonList


1 /*
2  * Created on 2003.12.12
3  */

4 package jspPhoneBook.business;
5
6 import java.util.Vector JavaDoc;
7 import java.sql.SQLException JavaDoc;
8 import jspPhoneBook.data.*;
9 import org.enhydra.dods.DODS;
10 import com.lutris.appserver.server.sql.*;
11
12 /**
13  * PhoneList defines a set of static methods that are used
14  * to query and update phone information in the database.
15  */

16 public class PersonList {
17
18     /**
19      * Queries the database for all people/phones.
20      *
21      * @return Vector[0] contains the people's first names.
22      * Vector[1] contains the people's last names.
23      * Vector[2] contains the phone numbers.
24      * Vector[3] contains the people's identifiers.
25      * @exception Exception all exceptions are thrown whenever an
26      * error occurs.
27      */

28     public static Vector JavaDoc[] getLists() throws Exception JavaDoc {
29         Vector JavaDoc[] results = new Vector JavaDoc[4];
30         results[0] = new Vector JavaDoc();
31         results[1] = new Vector JavaDoc();
32         results[2] = new Vector JavaDoc();
33         results[3] = new Vector JavaDoc();
34  
35         PersonQuery personQuery = new PersonQuery();
36         PersonDO[] personDOs = personQuery.getDOArray();
37         PersonDO personX =null;
38         if (personDOs.length>0){
39             for (int i=0 ; i<personDOs.length; i++){
40                   results[0].addElement(personDOs[i].getFirstName());
41                   results[1].addElement(personDOs[i].getLastName());
42                   results[2].addElement(personDOs[i].getPhoneNumber());
43                   results[3].addElement("" + personDOs[i].get_Handle());
44             }
45          }
46         return results;
47     }
48
49     /**
50      * Creates a new person and adds him/her to the database.
51      * Demonstrates how to use a
52      * transaction to insert data into the database.
53      *
54      * @param firstName the person's first name.
55      * @param lastName the person's last name.
56      * @param phoneNumber the person's phone number.
57      *
58      * @exception Exception all exceptions are thrown. A more
59      * robust application would handle them.....
60      */

61     public static void addPerson(String JavaDoc firstName,
62                                  String JavaDoc lastName,
63                                  String JavaDoc phoneNumber)
64     throws Exception JavaDoc {
65 // DBTransaction db = DODS.getDatabaseManager().createTransaction();
66
// try {
67
PersonDO newGuy = PersonDO.createVirgin();
68         newGuy.setFirstName(firstName);
69         newGuy.setLastName(lastName);
70         newGuy.setPhoneNumber(phoneNumber);
71         newGuy.save();
72         newGuy.commit();
73 // db.commit();
74
// } catch (SQLException sqle) {
75
// db.rollback();
76
// throw sqle;
77
// } finally {
78
// // You must call this to free any resources (such as
79
// // connections) that were allocated by the transaction.
80
// db.release();
81
// }
82
}
83
84     /**
85      * Deletes a person from the database. Demonstrates how to use a
86      * transaction to delete data in the database.
87      *
88      * @param id the person's id
89      *
90      * @exception Exception all exceptions are thrown. A more
91      * robust application would handle them.....
92      */

93     public static void deletePerson(String JavaDoc id) throws Exception JavaDoc {
94 // DBTransaction db = DODS.getDatabaseManager().createTransaction();
95
PersonDO personDO = getPersonDOById(id);
96        if (personDO == null) {
97             return;
98        }
99 // try {
100
// Multiple data objects could be update, deleted, inserted
101
// here as a single transaction.....
102
personDO.delete();
103 // db.commit();
104
// } catch (SQLException sqle) {
105
// db.rollback();
106
// throw sqle;
107
// } finally {
108
// // You must call this to free any resources (such as
109
// // connections) that were allocated by the transaction.
110
// db.release();
111
// }
112

113   }
114
115     /**
116      * Modifies a person's profile. Demonstrates how to use a
117      * transaction to update data in the database.
118      *
119      * @param id the person's id
120      * @param newFirstName the person's new first name.
121      * @param newLastName the person's new last name.
122      * @param newPhoneNumber the person's new phone number.
123      *
124      * @exception Exception all exceptions are thrown. A more
125      * robust application would handle them.....
126      */

127     public static void modifyPerson(String JavaDoc id,
128                                     String JavaDoc newFirstName,
129                                     String JavaDoc newLastName,
130                                     String JavaDoc newPhoneNumber)
131         throws Exception JavaDoc {
132 // DBTransaction db = DODS.getDatabaseManager().createTransaction();
133
PersonDO personDO = getPersonDOById(id);
134         if (personDO == null) return;
135         personDO.setFirstName(newFirstName);
136         personDO.setLastName(newLastName);
137         personDO.setPhoneNumber(newPhoneNumber);
138 // try {
139
// Multiple data objects could be update, deleted, inserted
140
// here as a single transaction.....
141
personDO.save();
142           personDO.commit();
143 // db.commit();
144
// } catch (SQLException sqle) {
145
// db.rollback();
146
// throw sqle;
147
// } finally {
148
// // You must call this to free any resources (such as
149
// // connections) that were allocated by the transaction.
150
// db.release();
151
// }
152

153     }
154
155     /**
156      * Queries a person from the database.
157      *
158      * @param id the person's id.
159      * @exception Exception all exceptions are thrown. A more
160      * robust applications would handle them.....
161      */

162     public static Person getById(String JavaDoc id) throws Exception JavaDoc {
163         PersonDO personDO = getPersonDOById(id);
164         if (personDO == null) {
165             return null;
166         }
167         return new Person(personDO);
168     }
169     
170     /**
171      * Queries a person from the database.
172      *
173      * @param id the person's id.
174      * @exception Exception all exceptions are thrown. A more
175      * robust applications would handle them.....
176      */

177     private static PersonDO getPersonDOById(String JavaDoc id) throws Exception JavaDoc {
178         ObjectId oid = new ObjectId(id);
179         PersonQuery personQuery = new PersonQuery();
180         personQuery.setQueryOId(oid);
181           PersonDO personX = personQuery.getNextDO();
182         return personX ;
183     }
184     
185 }
186
187
Popular Tags