001: package phoneList.business;
002:
003: import phoneList.spec.*;
004:
005: import java.util.Vector;
006: import phoneList.business.axis.*;
007:
008: /**
009: * PhoneList defines a set of static methods that are used
010: * to query and update phone information in the database.
011: */
012: public class PhoneListImpl implements phoneList.spec.PhoneList {
013:
014: public static boolean configured = false;
015: private static String server = "localhost";
016: private static String port = "9000";
017: private static String HTTP = "http://";
018: private static char COLON = ':';
019: private static String PHONEBOOK = "/phoneBook/PhoneBook";
020: private static PhoneListService service = new PhoneListServiceLocator();
021: private static phoneList.business.axis.PhoneList phoneBook;
022:
023: /**
024: * Get current server
025: * @return server
026: */
027:
028: public String getServer() {
029: return server;
030: }
031:
032: /**
033: * Get current port
034: * @return port
035: */
036:
037: public String getPort() {
038: return port;
039: }
040:
041: /**
042: * Sets Server and port and
043: * generates a new phoneList.business.axis.PhoneList accordingly
044: *
045: * @param srv the server name.
046: * @param prt the server port.
047: */
048:
049: public void setServerUrl(String srv, String prt) throws Exception {
050: phoneBook = service.getPhoneBook(new java.net.URL(HTTP + srv
051: + COLON + prt + PHONEBOOK));
052: server = srv;
053: port = prt;
054: }
055:
056: /**
057: * Queries the database for all people/phones.
058: *
059: * @return Vector[0] contains the people's first names.
060: * Vector[1] contains the people's last names.
061: * Vector[2] contains the phone numbers.
062: * Vector[3] contains the people's identifiers.
063: * @exception Exception all exceptions are thrown whenever an
064: * error occurs.
065: */
066:
067: public Vector[] getLists() throws Exception {
068: Vector[] results = new Vector[4];
069: Object[][] resultStrings = phoneBook.getLists();
070: if (resultStrings.length == 1) {
071: //case of jonas - axis implementation
072: for (int j = 0; j < resultStrings[0].length; j++) {
073: results[j] = new Vector();
074: Vector temp = (Vector) resultStrings[0][j];
075: for (int k = 0; k < temp.size(); k++) {
076: if (temp.elementAt(k) != null)
077: results[j].add((String) temp.elementAt(k));
078: }
079: }
080: } else {
081: for (int i = 0; i < resultStrings.length; i++) {
082: results[i] = new Vector();
083: for (int j = 0; j < resultStrings[i].length; j++) {
084: if (resultStrings[i][j] != null)
085: results[i].add((String) resultStrings[i][j]);
086: }
087: }
088: }
089: return results;
090: }
091:
092: /**
093: * Creates a new person and adds him/her to the database.
094: * Demonstrates how to use a
095: * transaction to insert data into the database.
096: *
097: * @param firstName the person's first name.
098: * @param lastName the person's last name.
099: * @param phoneNumber the person's phone number.
100: *
101: * @exception Exception all exceptions are thrown. A more
102: * robust application would handle them.....
103: */
104:
105: public void addPerson(String firstName, String lastName,
106: String phoneNumber) throws Exception {
107: phoneBook.addPerson(firstName, lastName, phoneNumber);
108: }
109:
110: /**
111: * Deletes a person from the database. Demonstrates how to use a
112: * transaction to delete data in the database.
113: *
114: * @param id the person's id
115: *
116: * @exception Exception all exceptions are thrown. A more
117: * robust application would handle them.....
118: */
119: public void deletePerson(String id) throws Exception {
120: phoneBook.deletePerson(id);
121: }
122:
123: /**
124: * Modifies a person's profile. Demonstrates how to use a
125: * transaction to update data in the database.
126: *
127: * @param id the person's id
128: * @param newFirstName the person's new first name.
129: * @param newLastName the person's new last name.
130: * @param newPhoneNumber the person's new phone number.
131: *
132: * @exception Exception all exceptions are thrown. A more
133: * robust application would handle them.....
134: */
135: public void modifyPerson(String id, String newFirstName,
136: String newLastName, String newPhoneNumber) throws Exception {
137: phoneBook.modifyPerson(id, newFirstName, newLastName,
138: newPhoneNumber);
139: }
140:
141: /**
142: * Queries a person from the database.
143: *
144: * @param id the person's id.
145: * @exception Exception all exceptions are thrown. A more
146: * robust applications would handle them.....
147: */
148: public Person getById(String id) throws Exception {
149: String[] personData = phoneBook.getPersonData(id);
150: PersonImpl person = new PersonImpl(personData[0],
151: personData[1], personData[2], id);
152: return person;
153:
154: }
155:
156: /**
157: *Is configarated
158: */
159: public boolean isConfigured() {
160: return configured;
161: }
162:
163: /**
164: *Sets the parameter configured to true
165: */
166: public void setConfigured() {
167: configured = true;
168: }
169:
170: }
|