KickJava   Java API By Example, From Geeks To Geeks.

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


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.OwnerI;
7 import org.enhydra.pim.business.api.PersonI;
8 import org.enhydra.pim.business.base.Owner;
9 import org.enhydra.pim.business.base.Person;
10 import org.enhydra.pim.data.backup.ContactTypeDO;
11 import org.enhydra.pim.data.production.ContactDO;
12 import org.enhydra.pim.data.production.OwnerDO;
13 import org.enhydra.pim.data.production.PersonDO;
14 import org.enhydra.pim.data.production.PersonQuery;
15 import org.enhydra.pim.exception.EnhydraPimDatabaseException;
16 import org.enhydra.pim.exception.EnhydraPimException;
17 import org.enhydra.pim.exception.EnhydraPimLogicException;
18
19 import com.lutris.appserver.server.sql.DBTransaction;
20 import com.lutris.appserver.server.sql.ObjectId;
21 import com.lutris.dods.builder.generator.query.NonUniqueQueryException;
22
23 /**
24  * @author P.Djojic Apr 19, 2005 11:49:25 PM
25  *
26  * TODO PersonManager
27  */

28
29 public class PersonManager implements PersonManagerI{
30
31
32     /**
33      * @param user
34      * @param firstname
35      * @param lastname
36      * @param nicname
37      * @param note
38      */

39     public PersonI newPerson(OwnerI owner, String JavaDoc firstname, String JavaDoc lastname, String JavaDoc nicname, String JavaDoc note) {
40         return new Person( owner, firstname, lastname, nicname, note);
41     }
42
43     /**
44      * @param handle
45      * @param user
46      * @param firstname
47      * @param lastname
48      * @param nicname
49      * @param note
50      */

51     public PersonI newPerson(BigDecimal JavaDoc handle, OwnerI owner, String JavaDoc firstname, String JavaDoc lastname, String JavaDoc nicname, String JavaDoc note) {
52         return new Person(handle, owner, firstname, lastname, nicname, note);
53     }
54
55     
56     PersonI personFromDO(PersonDO personDO) throws EnhydraPimDatabaseException {
57         PersonI person = null;
58         OwnerI owner = null;
59         try {
60             owner = (new UserManager()).ownerFromDO(personDO.getOwner());
61             person = new Person(personDO.get_OId().toBigDecimal(), owner, personDO.getFirstname(), personDO
62                             .getLastname(), personDO.getNicname(), personDO.getNote());
63         } catch (Exception JavaDoc e) {
64             e.printStackTrace();
65             PimBase.logError("[id=2091]Data access error");
66             throw new EnhydraPimDatabaseException("[id=1091]Data access error");
67         }
68         return person;
69     }
70
71     PersonDO fillPersonDO(PersonI person, PersonDO personDO) throws EnhydraPimDatabaseException {
72         DBTransaction dbTrans = null;
73         try {
74             dbTrans = PimBase.getPrimaryDatabase().createTransaction();
75             personDO.setOwner(OwnerDO.createExisting(person.getOwner().getHandle(), dbTrans));
76             personDO.setFirstname(person.getFirstname());
77             personDO.setLastname(person.getLastname());
78             personDO.setNicname(person.getNicname());
79             personDO.setNote(person.getNote());
80         } catch (Exception JavaDoc e) {
81             e.printStackTrace();
82             PimBase.logError("[id=2081]Data access error");
83             throw new EnhydraPimDatabaseException("[id=1081]Data access error");
84         } finally {
85             if (dbTrans != null)
86                 dbTrans.release();
87         }
88         return personDO;
89     }
90
91     public PersonI addPerson(PersonI person, OwnerI owner) throws EnhydraPimException {
92
93         if (person == null) {
94             PimBase.logDebug("[id=2800]Invalid Person (null)");
95             throw new EnhydraPimLogicException("[id=2800]Invalid Person (null)");
96         }
97         if ((owner == null) || !owner.isValid()) {
98             PimBase.logDebug("[id=2801]Invalid User");
99             throw new EnhydraPimLogicException("[id=2801]Invalid User");
100         }
101         DBTransaction dbTrans = null;
102         try {
103             dbTrans = PimBase.getPrimaryDatabase().createTransaction();
104
105             PersonDO personDO = PersonDO.createVirgin(dbTrans);
106             OwnerDO ownerDO = OwnerDO.createExisting(owner.getHandle(), dbTrans);
107
108             personDO.setOwner(ownerDO);
109             personDO.setFirstname(person.getFirstname());
110             personDO.setLastname(person.getLastname());
111             personDO.setNicname(person.getNicname());
112             personDO.setNote(person.getNote());
113
114             personDO.save(dbTrans);
115             dbTrans.commit();
116             person.setHandle(personDO.getOId().toBigDecimal());
117         } catch (Exception JavaDoc e) {
118             e.printStackTrace();
119             PimBase.logError("[id=2802]Database access error: Person can't be registred");
120             throw new EnhydraPimDatabaseException("[id=2802]Database access error: Person can't be registred");
121         } finally {
122             if (dbTrans != null)
123                 dbTrans.release();
124         }
125         return person;
126     }
127
128     public PersonI updatePerson(PersonI person, OwnerI owner) throws EnhydraPimException {
129
130         if (person == null) {
131             PimBase.logDebug("[id=2807]Invalid Person=null");
132             throw new EnhydraPimLogicException("[id=2807]Invalid Person=null");
133         }
134         if ((owner == null) || !owner.isValid()) {
135             PimBase.logDebug("[id=2808]Invalid User");
136             throw new EnhydraPimLogicException("[id=2808]Invalid User");
137         }
138         DBTransaction dbTrans = null;
139         try {
140             dbTrans = PimBase.getPrimaryDatabase().createTransaction();
141
142             PersonDO personDO = PersonDO.createExisting(person.getHandle(), dbTrans);
143             OwnerDO ownerDO = OwnerDO.createExisting(owner.getHandle(), dbTrans);
144
145             personDO.setOwner(ownerDO);
146             personDO.setFirstname(person.getFirstname());
147             personDO.setLastname(person.getLastname());
148             personDO.setNicname(person.getNicname());
149             personDO.setNote(person.getNote());
150
151             personDO.save(dbTrans);
152             dbTrans.commit();
153         } catch (Exception JavaDoc e) {
154             e.printStackTrace();
155             PimBase.logError("[id=2809]Database access error: Person can't be registred");
156             throw new EnhydraPimDatabaseException("[id=2809]Database access error: Person can't be registred");
157         } finally {
158             if (dbTrans != null)
159                 dbTrans.release();
160         }
161         return person;
162     }
163
164     public Vector JavaDoc getOwnerPersons(OwnerI owner) throws EnhydraPimException {
165         Vector JavaDoc ownerPersons = new Vector JavaDoc();
166         if ((owner == null) || !owner.isValid()) {
167             PimBase.logDebug("[id=2804]Invalid User");
168             throw new EnhydraPimLogicException("[id=2804]Invalid User");
169         }
170         DBTransaction dbTrans = null;
171         try {
172             dbTrans = PimBase.getPrimaryDatabase().createTransaction();
173
174             OwnerDO ownerDO = OwnerDO.createExisting(owner.getHandle(), dbTrans);
175             PersonQuery personQuery = new PersonQuery(dbTrans);
176             personQuery.setQueryOwner(ownerDO);
177             PersonDO[] personDOs = personQuery.getDOArray();
178             if (personDOs != null) {
179                 for (int i = 0; i < personDOs.length; i++) {
180                     Person newPerson = new Person(personDOs[i].get_OId().toBigDecimal(), owner, personDOs[i]
181                                     .getFirstname(), personDOs[i].getLastname(), personDOs[i].getNicname(),
182                                     personDOs[i].getNote());
183                     ownerPersons.add(newPerson);
184                 }
185             }
186         } catch (NonUniqueQueryException e) {
187             PimBase.logDebug("[id=2805]More then one user have same username and password");
188             throw new EnhydraPimLogicException("[id=2805]More then one user have same username and password");
189         } catch (Exception JavaDoc e) {
190             e.printStackTrace();
191             PimBase.logError("[id=2806]Database access error");
192             throw new EnhydraPimDatabaseException("[id=2806]Database access error");
193         } finally {
194             if (dbTrans != null)
195                 dbTrans.release();
196         }
197         return ownerPersons;
198     }
199
200     public void removePerson(PersonI person) throws EnhydraPimException {
201         DBTransaction dbTrans = null;
202         try {
203             dbTrans = PimBase.getPrimaryDatabase().createTransaction();
204             PersonDO personDO = PersonDO.createExisting(person.getHandle(), dbTrans);
205             if (personDO != null) {
206                 personDO.delete(dbTrans);
207             }
208             dbTrans.commit();
209         } catch (Exception JavaDoc e) {
210             e.printStackTrace();
211             PimBase.logError("[id=2803]Database access error");
212             throw new EnhydraPimDatabaseException("[id=2803]Database access error");
213         } finally {
214             if (dbTrans != null)
215                 dbTrans.release();
216         }
217     }
218
219     public PersonI findPerson(ObjectId oid) throws EnhydraPimException {
220         return findPerson(oid.toBigDecimal());
221     }
222
223     public PersonI findPerson(BigDecimal JavaDoc handle) throws EnhydraPimException {
224         DBTransaction dbTrans = null;
225         Person person = null;
226         try {
227             UserManager uMan = new UserManager();
228             dbTrans = PimBase.getPrimaryDatabase().createTransaction();
229             PersonDO personDO = PersonDO.createExisting(handle, dbTrans);
230             if (personDO != null && personDO.getOwner() != null) {
231                 Owner owner = (Owner) uMan.checkOwner(personDO.getOwner().getUsername(), personDO.getOwner()
232                                 .getPassword());
233                 if (owner == null) {
234                     PimBase.logError("[id=2810]Unknown User");
235                     throw new EnhydraPimLogicException("[id=2810]Unknown User");
236                 }
237                 person = new Person(personDO.get_OId().toBigDecimal(), owner, personDO.getFirstname(), personDO
238                                 .getLastname(), personDO.getNicname(), personDO.getNote());
239             }
240             dbTrans.commit();
241         } catch (EnhydraPimLogicException ex) {
242             throw ex;
243         } catch (Exception JavaDoc e) {
244             e.printStackTrace();
245             PimBase.logError("[id=2811]Database access error");
246             throw new EnhydraPimDatabaseException("[id=2811]Database access error");
247         } finally {
248             if (dbTrans != null)
249                 dbTrans.release();
250         }
251         return person;
252     }
253
254     public Vector JavaDoc findPersonsByQuery(OwnerI owner, String JavaDoc firstname, String JavaDoc lastname, String JavaDoc nicname, String JavaDoc note,
255                     String JavaDoc contactType, String JavaDoc contactData) throws EnhydraPimException {
256         return findPersonsByQuery(owner.getHandle(), firstname, lastname, nicname, note, contactType, contactData);
257     }
258
259     public Vector JavaDoc findPersonsByQuery(BigDecimal JavaDoc ownerhandle, String JavaDoc firstname, String JavaDoc lastname, String JavaDoc nicname,
260                     String JavaDoc note, String JavaDoc contactType, String JavaDoc contactData) throws EnhydraPimException {
261         DBTransaction dbTrans = null;
262         Person person = null;
263         OwnerDO ownerDO = null;
264         OwnerI owner = null;
265         Vector JavaDoc resultVec = new Vector JavaDoc();
266         try {
267             UserManager uMan = new UserManager();
268             dbTrans = PimBase.getPrimaryDatabase().createTransaction();
269
270             if (ownerhandle != null) {
271                 ownerDO = OwnerDO.createExisting(ownerhandle);
272                 owner = (new UserManager()).checkOwner(ownerDO.getUsername(), ownerDO.getPassword());
273             }
274
275             PersonQuery personQuery = new PersonQuery(dbTrans);
276             personQuery.getQueryBuilder().addWhere(OwnerDO.PrimaryKey, PersonDO.Owner);
277             personQuery.getQueryBuilder().addWhere(PersonDO.PrimaryKey, ContactDO.Person);
278             personQuery.getQueryBuilder().addWhere(ContactTypeDO.PrimaryKey, ContactDO.Contact_type);
279
280             if (ownerDO != null) {
281                 personQuery.setQueryOwner(ownerDO);
282             }
283             if (firstname != null && firstname.length() > 0) {
284                 personQuery.setQueryFirstname(firstname);
285             }
286             if (lastname != null && lastname.length() > 0) {
287                 personQuery.setQueryLastname(lastname);
288             }
289             if (nicname != null && nicname.length() > 0) {
290                 personQuery.setQueryNicname(nicname);
291             }
292             if (note != null && note.length() > 0) {
293                 personQuery.setQueryNote(note);
294             }
295             if (contactType != null && contactType.length() > 0) {
296                 personQuery.getQueryBuilder().addWhere(ContactTypeDO.Contact_type, contactType);
297             }
298             if (contactData != null && contactData.length() > 0) {
299                 personQuery.getQueryBuilder().addWhere(ContactDO.Contact_data, contactData);
300             }
301
302             PersonDO[] personDOs = personQuery.getDOArray();
303
304             if (personDOs != null) {
305                 for (int i = 0; i < personDOs.length; i++) {
306                     Person newPerson = new Person(personDOs[i].get_OId().toBigDecimal(), owner, personDOs[i]
307                                     .getFirstname(), personDOs[i].getLastname(), personDOs[i].getNicname(),
308                                     personDOs[i].getNote());
309                     resultVec.add(newPerson);
310                 }
311             }
312         } catch (EnhydraPimLogicException ex) {
313             throw ex;
314         } catch (Exception JavaDoc e) {
315             e.printStackTrace();
316             PimBase.logError("[id=2891]Database access error");
317             throw new EnhydraPimDatabaseException("[id=2891]Database access error");
318         } finally {
319             if (dbTrans != null)
320                 dbTrans.release();
321         }
322         return resultVec;
323     }
324
325 }
Popular Tags