KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > pim > business > ContactManager


1 package org.enhydra.pim.business;
2
3 import java.math.BigDecimal JavaDoc;
4 import java.util.Vector JavaDoc;
5
6 import org.enhydra.pim.business.api.ContactI;
7 import org.enhydra.pim.business.api.ContactTypeI;
8 import org.enhydra.pim.business.api.PersonI;
9 import org.enhydra.pim.business.base.Contact;
10 import org.enhydra.pim.business.base.ContactType;
11 import org.enhydra.pim.business.base.Person;
12 import org.enhydra.pim.data.production.ContactDO;
13 import org.enhydra.pim.data.production.ContactQuery;
14 import org.enhydra.pim.data.production.ContactTypeDO;
15 import org.enhydra.pim.data.production.PersonDO;
16 import org.enhydra.pim.exception.EnhydraPimDatabaseException;
17 import org.enhydra.pim.exception.EnhydraPimException;
18 import org.enhydra.pim.exception.EnhydraPimLogicException;
19
20 import com.lutris.appserver.server.sql.DBTransaction;
21 import com.lutris.appserver.server.sql.ObjectId;
22 import com.lutris.dods.builder.generator.query.NonUniqueQueryException;
23
24 /**
25  * @author P.Djojic Apr 19, 2005 11:49:41 PM
26  *
27  * TODO ContactManager
28  */

29 public class ContactManager implements ContactManagerI{
30
31
32     /**
33      * @param contact_data
34      * @param contact_type
35      * @param person
36      * @param note
37      */

38     public ContactI newContact(String JavaDoc contact_data, ContactTypeI contact_type, PersonI person, String JavaDoc note) {
39         return new Contact(contact_data, contact_type, person, note);
40     }
41
42     /**
43      * @param handle
44      * @param contact_data
45      * @param contact_type
46      * @param person
47      * @param note
48      */

49     public ContactI newContact(BigDecimal JavaDoc handle, String JavaDoc contact_data, ContactTypeI contact_type, PersonI person, String JavaDoc note) {
50         return new Contact(handle, contact_data, contact_type, person, note);
51     }
52
53
54     
55     ContactI contactFromDO(ContactDO contactDO) throws EnhydraPimDatabaseException {
56         ContactI contact = null;
57         try {
58             PersonI person = (new PersonManager()).personFromDO(contactDO.getPerson());
59             ContactType contactType = (ContactType) (new ContactTypeManager()).findContactType(contactDO
60                             .getContact_type().getOId().toBigDecimal());
61             contact = new Contact(contactDO.getOId().toBigDecimal(), contactDO.getContact_data(), contactType, person,
62                             contactDO.getNote());
63         } catch (Exception JavaDoc e) {
64             e.printStackTrace();
65             PimBase.logError("[id=3391]Data access error");
66             throw new EnhydraPimDatabaseException("[id=3391]Data access error");
67         }
68         return contact;
69     }
70
71     ContactDO fillContactDO(ContactI contact, ContactDO contactDO) throws EnhydraPimDatabaseException {
72         DBTransaction dbTrans = null;
73         try {
74             dbTrans = PimBase.getPrimaryDatabase().createTransaction();
75             contactDO.setContact_data(contact.getContact_data());
76             contactDO.setContact_type(ContactTypeDO.createExisting(contact.getContact_type().getHandle(), dbTrans));
77             contactDO.setPerson(PersonDO.createExisting(contact.getPerson().getHandle(), dbTrans));
78             contactDO.setNote(contact.getNote());
79         } catch (Exception JavaDoc e) {
80             e.printStackTrace();
81             PimBase.logError("[id=3381]Data access error");
82             throw new EnhydraPimDatabaseException("[id=3381]Data access error");
83         } finally {
84             if (dbTrans != null)
85                 dbTrans.release();
86         }
87         return contactDO;
88     }
89
90     public ContactI addContact(ContactI contact, PersonI person, ContactTypeI cType) throws EnhydraPimException {
91
92         if (contact == null) {
93             PimBase.logDebug("[id=3300]Invalid Contact (null)");
94             throw new EnhydraPimLogicException("[id=3300]Invalid Contact (null)");
95         }
96         if ((person == null)) {
97             PimBase.logDebug("[id=3301]Invalid Contact Person(null)");
98             throw new EnhydraPimLogicException("[id=3301]Invalid Contact Person(null)");
99         }
100         if ((cType == null)) {
101             PimBase.logDebug("[id=3311]Invalid Contact Type (null)");
102             throw new EnhydraPimLogicException("[id=3311]Invalid Contact Type (null)");
103         }
104
105         DBTransaction dbTrans = null;
106         try {
107             dbTrans = PimBase.getPrimaryDatabase().createTransaction();
108             ContactDO contactDO = ContactDO.createVirgin(dbTrans);
109             PersonDO personDO = PersonDO.createExisting(person.getHandle(), dbTrans);
110             ContactTypeDO cTypeDO = ContactTypeDO.createExisting(cType.getHandle(), dbTrans);
111             contactDO.setContact_data(contact.getContact_data());
112             contactDO.setContact_type(cTypeDO);
113             contactDO.setPerson(personDO);
114             contactDO.setNote(contact.getNote());
115             contactDO.save(dbTrans);
116             dbTrans.write();
117             dbTrans.commit();
118             contact.setHandle(contactDO.getOId().toBigDecimal());
119         } catch (Exception JavaDoc e) {
120             e.printStackTrace();
121             PimBase.logError("[id=3302]Database access error: Contact can't be registred");
122             throw new EnhydraPimDatabaseException("[id=3302]Database access error: Contact can't be registred");
123         } finally {
124             if (dbTrans != null)
125                 dbTrans.release();
126         }
127         return contact;
128     }
129
130     public ContactI updateContact(ContactI contact) throws EnhydraPimException {
131         if (contact == null) {
132             PimBase.logDebug("[id=3330]Invalid Contact (null)");
133             throw new EnhydraPimLogicException("[id=3330]Invalid Contact (null)");
134         }
135         if ((contact.getPerson() == null)) {
136             PimBase.logDebug("[id=3331]Invalid Contact Person(null)");
137             throw new EnhydraPimLogicException("[id=3331]Invalid Contact Person(null)");
138         }
139         if ((contact.getContact_type() == null)) {
140             PimBase.logDebug("[id=3332]Invalid Contact Type (null)");
141             throw new EnhydraPimLogicException("[id=3332]Invalid Contact Type (null)");
142         }
143         DBTransaction dbTrans = null;
144         try {
145             dbTrans = PimBase.getPrimaryDatabase().createTransaction();
146             ContactDO contactDO = ContactDO.createVirgin(dbTrans);
147             PersonDO personDO = PersonDO.createExisting(contact.getPerson().getHandle(), dbTrans);
148             ContactTypeDO cTypeDO = ContactTypeDO.createExisting(contact.getContact_type().getHandle(), dbTrans);
149             contactDO.setContact_data(contact.getContact_data());
150             contactDO.setContact_type(cTypeDO);
151             contactDO.setPerson(personDO);
152             contactDO.setNote(contact.getNote());
153             contactDO.save(dbTrans);
154             dbTrans.commit();
155         } catch (Exception JavaDoc e) {
156             e.printStackTrace();
157             PimBase.logError("[id=3309]Database access error: Note can't be registred");
158             throw new EnhydraPimDatabaseException("[id=3309]Database access error: Note can't be registred");
159         } finally {
160             if (dbTrans != null)
161                 dbTrans.release();
162         }
163         return contact;
164     }
165
166     public Vector JavaDoc getPersonContacts(PersonI person) throws EnhydraPimException {
167         Vector JavaDoc personContacts = new Vector JavaDoc();
168         if (person == null) {
169             PimBase.logDebug("[id=3304]Invalid Contact Person");
170             throw new EnhydraPimLogicException("[id=3304]Invalid Contact Person");
171         }
172         DBTransaction dbTrans = null;
173         try {
174             dbTrans = PimBase.getPrimaryDatabase().createTransaction();
175
176             PersonDO personDO = PersonDO.createExisting(person.getHandle(), dbTrans);
177             ContactQuery contactQuery = new ContactQuery(dbTrans);
178             contactQuery.setQueryPerson(personDO);
179             ContactDO[] contactDOs = contactQuery.getDOArray();
180             if (contactDOs != null) {
181                 for (int i = 0; i < contactDOs.length; i++) {
182                     ContactTypeDO contactTypeDO = contactDOs[i].getContact_type();
183                     ContactType contactType = new ContactType(contactTypeDO.getOId().toBigDecimal(), contactTypeDO
184                                     .getContact_type());
185                     Contact newContact = new Contact(contactDOs[i].get_OId().toBigDecimal(), contactDOs[i]
186                                     .getContact_data(), contactType, person, contactDOs[i].getNote());
187                     personContacts.add(newContact);
188                 }
189             }
190         } catch (NonUniqueQueryException e) {
191             PimBase.logDebug("[id=3335]More then one user have same username and password");
192             throw new EnhydraPimLogicException("[id=3335]More then one user have same username and password");
193         } catch (Exception JavaDoc e) {
194             e.printStackTrace();
195             PimBase.logError("[id=3336]Database access error");
196             throw new EnhydraPimDatabaseException("[id=3336]Database access error");
197         } finally {
198             if (dbTrans != null)
199                 dbTrans.release();
200         }
201         return personContacts;
202     }
203
204     public void removeContact(ContactI contact) throws EnhydraPimException {
205         DBTransaction dbTrans = null;
206         try {
207             dbTrans = PimBase.getPrimaryDatabase().createTransaction();
208             ContactDO contactDO = ContactDO.createExisting(contact.getHandle(), dbTrans);
209             if (contactDO != null) {
210                 contactDO.delete(dbTrans);
211                 dbTrans.write();
212             }
213             dbTrans.commit();
214         } catch (Exception JavaDoc e) {
215             e.printStackTrace();
216             PimBase.logError("[id=3333]Database access error");
217             throw new EnhydraPimDatabaseException("[id=3333]Database access error");
218         } finally {
219             if (dbTrans != null)
220                 dbTrans.release();
221         }
222     }
223
224     public ContactI findContact(ObjectId oid) throws EnhydraPimException {
225         return findContact(oid.toBigDecimal());
226     }
227
228     public ContactI findContact(BigDecimal JavaDoc handle) throws EnhydraPimException {
229         DBTransaction dbTrans = null;
230         Contact contact = null;
231         try {
232
233             PersonManager pMan = new PersonManager();
234             ContactTypeManager ctMan = new ContactTypeManager();
235
236             dbTrans = PimBase.getPrimaryDatabase().createTransaction();
237
238             ContactDO contactDO = ContactDO.createExisting(handle, dbTrans);
239             PersonDO personDO = contactDO.getPerson();
240             Person person = (Person) pMan.findPerson(contactDO.getPerson().getOId());
241             ContactType contactType = (ContactType) ctMan.findContactType(contactDO.getContact_type().getOId()
242                             .toBigDecimal());
243
244             if (contactDO != null) {
245                 contact = new Contact(handle, contactDO.getContact_data(), contactType, person, contactDO.getNote());
246             }
247             dbTrans.commit();
248         } catch (EnhydraPimLogicException ex) {
249             throw ex;
250         } catch (Exception JavaDoc e) {
251             e.printStackTrace();
252             PimBase.logError("[id=3311]Database access error");
253             throw new EnhydraPimDatabaseException("[id=3311]Database access error");
254         } finally {
255             if (dbTrans != null)
256                 dbTrans.release();
257         }
258         return contact;
259     }
260
261 }
Popular Tags