KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > phoneList > business > PhoneListImpl


1 package phoneList.business;
2
3 import java.util.Vector JavaDoc;
4 import java.sql.SQLException JavaDoc;
5 import phoneList.data.*;
6 import org.enhydra.dods.DODS;
7 import com.lutris.appserver.server.Enhydra;
8 import com.lutris.appserver.server.sql.DatabaseManager;
9 import com.lutris.appserver.server.sql.DBConnection;
10 import com.lutris.appserver.server.sql.DBQuery;
11 import com.lutris.appserver.server.sql.DBTransaction;
12 import com.lutris.appserver.server.sql.ObjectId;
13
14 import phoneList.spec.*;
15 /**
16  * PhoneList defines a set of static methods that are used
17  * to query and update phone information in the database.
18  */

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

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

64     public void addPerson(String JavaDoc firstName,
65                                  String JavaDoc lastName,
66                                  String JavaDoc phoneNumber)
67     throws Exception JavaDoc {
68         PersonDO newGuy = PersonDO.createVirgin();
69         newGuy.setFirstName(firstName);
70         newGuy.setLastName(lastName);
71         newGuy.setPhoneNumber(phoneNumber);
72         newGuy.save();
73         newGuy.commit();
74   }
75
76     /**
77      * Deletes a person from the database. Demonstrates how to use a
78      * transaction to delete data in the database.
79      *
80      * @param id the person's id
81      *
82      * @exception Exception all exceptions are thrown. A more
83      * robust application would handle them.....
84      */

85     public void deletePerson(String JavaDoc id) throws Exception JavaDoc {
86        PersonDO personDO = getPersonDOById(id);
87        if (personDO == null) {
88             return;
89        }
90             // Multiple data objects could be update, deleted, inserted
91
// here as a single transaction.....
92
personDO.delete();
93
94   }
95
96     /**
97      * Modifies a person's profile. Demonstrates how to use a
98      * transaction to update data in the database.
99      *
100      * @param id the person's id
101      * @param newFirstName the person's new first name.
102      * @param newLastName the person's new last name.
103      * @param newPhoneNumber the person's new phone number.
104      *
105      * @exception Exception all exceptions are thrown. A more
106      * robust application would handle them.....
107      */

108     public void modifyPerson(String JavaDoc id,
109                                     String JavaDoc newFirstName,
110                                     String JavaDoc newLastName,
111                                     String JavaDoc newPhoneNumber)
112         throws Exception JavaDoc {
113         PersonDO personDO = getPersonDOById(id);
114         if (personDO == null) return;
115         personDO.setFirstName(newFirstName);
116         personDO.setLastName(newLastName);
117         personDO.setPhoneNumber(newPhoneNumber);
118             // Multiple data objects could be update, deleted, inserted
119
// here as a single transaction.....
120
personDO.save();
121           personDO.commit();
122
123     }
124
125     /**
126      * Queries a person from the database.
127      *
128      * @param id the person's id.
129      * @exception Exception all exceptions are thrown. A more
130      * robust applications would handle them.....
131      */

132     public Person getById(String JavaDoc id) throws Exception JavaDoc {
133         PersonDO personDO = getPersonDOById(id);
134         if (personDO == null) {
135             return null;
136         }
137         return new PersonImpl(personDO);
138     }
139     
140     /**
141      * Queries a person from the database.
142      *
143      * @param id the person's id.
144      * @exception Exception all exceptions are thrown. A more
145      * robust applications would handle them.....
146      */

147      
148      public static String JavaDoc[] getPersonData(String JavaDoc id) throws Exception JavaDoc {
149         PersonDO personDO = getPersonDOById(id);
150         if (personDO == null) {
151             return null;
152         }
153         String JavaDoc personData[]=new String JavaDoc[3];
154         personData[0]=personDO.getFirstName();
155         personData[1]=personDO.getLastName();
156         personData[2]=personDO.getPhoneNumber();
157         return personData;
158     }
159     
160     /**
161      * Queries a person from the database.
162      *
163      * @param id the person's id.
164      * @exception Exception all exceptions are thrown. A more
165      * robust applications would handle them.....
166      */

167     private static PersonDO getPersonDOById(String JavaDoc id) throws Exception JavaDoc {
168         ObjectId oid = new ObjectId(id);
169         PersonQuery personQuery = new PersonQuery();
170         personQuery.setQueryOId(oid);
171           PersonDO personX = personQuery.getNextDO();
172         return personX ;
173     }
174     
175 }
176
177
Popular Tags