001: /*
002: * Created on 2003.12.12
003: */
004: package jspPhoneBook.business;
005:
006: import java.util.Vector;
007: import java.sql.SQLException;
008: import jspPhoneBook.data.*;
009: import org.enhydra.dods.DODS;
010: import com.lutris.appserver.server.sql.*;
011:
012: /**
013: * PhoneList defines a set of static methods that are used
014: * to query and update phone information in the database.
015: */
016: public class PersonList {
017:
018: /**
019: * Queries the database for all people/phones.
020: *
021: * @return Vector[0] contains the people's first names.
022: * Vector[1] contains the people's last names.
023: * Vector[2] contains the phone numbers.
024: * Vector[3] contains the people's identifiers.
025: * @exception Exception all exceptions are thrown whenever an
026: * error occurs.
027: */
028: public static Vector[] getLists() throws Exception {
029: Vector[] results = new Vector[4];
030: results[0] = new Vector();
031: results[1] = new Vector();
032: results[2] = new Vector();
033: results[3] = new Vector();
034:
035: PersonQuery personQuery = new PersonQuery();
036: PersonDO[] personDOs = personQuery.getDOArray();
037: PersonDO personX = null;
038: if (personDOs.length > 0) {
039: for (int i = 0; i < personDOs.length; i++) {
040: results[0].addElement(personDOs[i].getFirstName());
041: results[1].addElement(personDOs[i].getLastName());
042: results[2].addElement(personDOs[i].getPhoneNumber());
043: results[3].addElement("" + personDOs[i].get_Handle());
044: }
045: }
046: return results;
047: }
048:
049: /**
050: * Creates a new person and adds him/her to the database.
051: * Demonstrates how to use a
052: * transaction to insert data into the database.
053: *
054: * @param firstName the person's first name.
055: * @param lastName the person's last name.
056: * @param phoneNumber the person's phone number.
057: *
058: * @exception Exception all exceptions are thrown. A more
059: * robust application would handle them.....
060: */
061: public static void addPerson(String firstName, String lastName,
062: String phoneNumber) throws Exception {
063: // DBTransaction db = DODS.getDatabaseManager().createTransaction();
064: // try {
065: PersonDO newGuy = PersonDO.createVirgin();
066: newGuy.setFirstName(firstName);
067: newGuy.setLastName(lastName);
068: newGuy.setPhoneNumber(phoneNumber);
069: newGuy.save();
070: newGuy.commit();
071: // db.commit();
072: // } catch (SQLException sqle) {
073: // db.rollback();
074: // throw sqle;
075: // } finally {
076: // // You must call this to free any resources (such as
077: // // connections) that were allocated by the transaction.
078: // db.release();
079: // }
080: }
081:
082: /**
083: * Deletes a person from the database. Demonstrates how to use a
084: * transaction to delete data in the database.
085: *
086: * @param id the person's id
087: *
088: * @exception Exception all exceptions are thrown. A more
089: * robust application would handle them.....
090: */
091: public static void deletePerson(String id) throws Exception {
092: // DBTransaction db = DODS.getDatabaseManager().createTransaction();
093: PersonDO personDO = getPersonDOById(id);
094: if (personDO == null) {
095: return;
096: }
097: // try {
098: // Multiple data objects could be update, deleted, inserted
099: // here as a single transaction.....
100: personDO.delete();
101: // db.commit();
102: // } catch (SQLException sqle) {
103: // db.rollback();
104: // throw sqle;
105: // } finally {
106: // // You must call this to free any resources (such as
107: // // connections) that were allocated by the transaction.
108: // db.release();
109: // }
110:
111: }
112:
113: /**
114: * Modifies a person's profile. Demonstrates how to use a
115: * transaction to update data in the database.
116: *
117: * @param id the person's id
118: * @param newFirstName the person's new first name.
119: * @param newLastName the person's new last name.
120: * @param newPhoneNumber the person's new phone number.
121: *
122: * @exception Exception all exceptions are thrown. A more
123: * robust application would handle them.....
124: */
125: public static void modifyPerson(String id, String newFirstName,
126: String newLastName, String newPhoneNumber) throws Exception {
127: // DBTransaction db = DODS.getDatabaseManager().createTransaction();
128: PersonDO personDO = getPersonDOById(id);
129: if (personDO == null)
130: return;
131: personDO.setFirstName(newFirstName);
132: personDO.setLastName(newLastName);
133: personDO.setPhoneNumber(newPhoneNumber);
134: // try {
135: // Multiple data objects could be update, deleted, inserted
136: // here as a single transaction.....
137: personDO.save();
138: personDO.commit();
139: // db.commit();
140: // } catch (SQLException sqle) {
141: // db.rollback();
142: // throw sqle;
143: // } finally {
144: // // You must call this to free any resources (such as
145: // // connections) that were allocated by the transaction.
146: // db.release();
147: // }
148:
149: }
150:
151: /**
152: * Queries a person from the database.
153: *
154: * @param id the person's id.
155: * @exception Exception all exceptions are thrown. A more
156: * robust applications would handle them.....
157: */
158: public static Person getById(String id) throws Exception {
159: PersonDO personDO = getPersonDOById(id);
160: if (personDO == null) {
161: return null;
162: }
163: return new Person(personDO);
164: }
165:
166: /**
167: * Queries a person from the database.
168: *
169: * @param id the person's id.
170: * @exception Exception all exceptions are thrown. A more
171: * robust applications would handle them.....
172: */
173: private static PersonDO getPersonDOById(String id) throws Exception {
174: ObjectId oid = new ObjectId(id);
175: PersonQuery personQuery = new PersonQuery();
176: personQuery.setQueryOId(oid);
177: PersonDO personX = personQuery.getNextDO();
178: return personX;
179: }
180:
181: }
|