KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sync4j > exchange > items > contact > manager > ContactManager


1 /**
2  * Copyright (C) 2003-2005 Funambol
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18
19 package sync4j.exchange.items.contact.manager;
20
21 import java.io.IOException JavaDoc;
22
23 import java.util.List JavaDoc;
24
25 import sync4j.exchange.items.contact.dao.ContactDAO;
26 import sync4j.exchange.items.contact.model.Contact;
27 import sync4j.exchange.DataAccessException;
28
29 import sync4j.foundation.pdi.contact.ContactDetail;
30 import sync4j.foundation.pdi.contact.Email;
31 import sync4j.foundation.pdi.contact.PersonalDetail;
32
33 /*
34  * This class defines methods to access contacts data
35  * in exchange server datastore
36  *
37  * @author Fabio Maggi @ Funambol
38  * @version $Id: ContactManager.java,v 1.8 2005/06/20 14:04:02 fabius Exp $
39  *
40  **/

41 public class ContactManager {
42
43     //------------------------------------------------------------- Constants
44

45     //------------------------------------------------------------- Private Data
46

47     ContactDAO cd = null ;
48
49     //------------------------------------------------------------- Costructor
50

51     public ContactManager(String JavaDoc exchangeServerHost ,
52                           int exchangeServerPort )
53         throws DataAccessException {
54
55         this.cd = new ContactDAO (exchangeServerHost ,
56                                   exchangeServerPort);
57     }
58
59     //------------------------------------------------------------- Public methods
60

61     /**
62      * get all contacts
63      *
64      * @param username
65      * @param credentials
66      * @param exchangeFolder
67      *
68      * @return array of find contacts
69      *
70      * @throws sync4j.exchange.util.DataAccessException
71      **/

72     public Contact[] getAllContacts(String JavaDoc username ,
73                                     String JavaDoc credentials ,
74                                     String JavaDoc exchangeFolder )
75         throws DataAccessException {
76
77         return this.cd.getContacts(username ,
78                                    credentials ,
79                                    new String JavaDoc[0] ,
80                                    exchangeFolder );
81
82     }
83
84     /**
85      * get contacts
86      *
87      * @param username
88      * @param credentials
89      * @param ids
90      * @param exchangeFolder
91      *
92      * @return array of find contacts
93      *
94      * @throws sync4j.exchange.util.DataAccessException
95      **/

96     public Contact[] getContacts(String JavaDoc username ,
97                                  String JavaDoc credentials ,
98                                  String JavaDoc[] ids ,
99                                  String JavaDoc exchangeFolder )
100         throws DataAccessException {
101
102         return this.cd.getContacts(username ,
103                                    credentials ,
104                                    ids ,
105                                    exchangeFolder );
106
107     }
108
109     /**
110      * get contact by key
111      *
112      * @param id
113      * @param username
114      * @param credentials
115      *
116      * @return find contact
117      *
118      * @throws sync4j.exchange.util.DataAccessException
119      **/

120     public Contact getContactById(String JavaDoc username ,
121                                   String JavaDoc credentials ,
122                                   String JavaDoc id ,
123                                   String JavaDoc exchangeFolder )
124         throws DataAccessException {
125
126         Contact[] contacts = null;
127
128         contacts = this.cd.getContacts(username ,
129                                        credentials ,
130                                        new String JavaDoc[] {id} ,
131                                        exchangeFolder );
132
133         if (contacts == null || !(contacts.length > 0)) {
134             return null;
135         }
136
137         return contacts[0];
138
139     }
140
141     /**
142      * get contact twin
143      *
144      * @param contact the Contact object
145      * @param username
146      * @param credentials
147      * @param exchangeFolder
148      *
149      * @return find contact
150      *
151      * @throws sync4j.exchange.util.DataAccessException
152      **/

153     public Contact getContactTwin(Contact contact ,
154                                    String JavaDoc username ,
155                                    String JavaDoc credentials ,
156                                    String JavaDoc exchangeFolder)
157     throws DataAccessException {
158
159         Contact[] contacts = null;
160
161
162         String JavaDoc firstName = null ;
163         String JavaDoc lastName = null ;
164         String JavaDoc middleName = null ;
165
166         String JavaDoc emailAddress = null ;
167
168         List JavaDoc emails = null ;
169
170         Email email = null ;
171
172         PersonalDetail personalDetail = null ;
173         ContactDetail personalContactDetail = null ;
174
175         personalDetail = contact.getPersonalDetail();
176
177         if (personalDetail != null) {
178             emails = personalDetail.getEmails();
179         }
180
181         if (emails != null && emails != null) {
182
183             for (int i=0, l = emails.size(); i < l; i++) {
184
185                 email = (Email) emails.get(i);
186
187                 if ((ContactDAO.FIELD_TYPE_EMAIL1_ADDRESS).equals(email.getEmailType())) {
188                     emailAddress = email.getPropertyValueAsString();
189                 }
190
191             }
192
193         }
194
195         if (contact.getName().getFirstName() != null) {
196             firstName = contact.getName().
197                             getFirstName ().
198                                 getPropertyValueAsString();
199         }
200         if (contact.getName().getLastName() != null) {
201             lastName = contact.getName().
202                                 getLastName().
203                                     getPropertyValueAsString();
204         }
205         if (contact.getName().getMiddleName() != null) {
206             middleName = contact.getName().
207                                 getMiddleName().
208                                     getPropertyValueAsString();
209         }
210
211         String JavaDoc[] fields = {
212                           ContactDAO.TAG_FIRSTNAME,
213                           ContactDAO.TAG_LASTNAME,
214                           ContactDAO.TAG_MIDDLENAME,
215                           ContactDAO.TAG_EMAIL1
216         };
217
218         Object JavaDoc[] values = {
219                           firstName,
220                           lastName,
221                           middleName,
222                           emailAddress
223         };
224
225
226         contacts = this.cd.getContacts(username ,
227                                        credentials ,
228                                        fields ,
229                                        values ,
230                                        exchangeFolder );
231
232         if (contacts == null || !(contacts.length > 0)) {
233             return null;
234         }
235
236         return contacts[0];
237     }
238
239     /**
240      * add contact
241      *
242      * @param contact
243      * @param username
244      * @param credentials
245      * @param exchangeFolder
246      *
247      * @return new contact
248      *
249      * @throws sync4j.exchange.util.DataAccessException
250      **/

251     public Contact setContact(Contact contact ,
252                               String JavaDoc username ,
253                               String JavaDoc credentials ,
254                               String JavaDoc exchangeFolder )
255         throws DataAccessException {
256
257         return this.cd.setContact(contact ,
258                                   username ,
259                                   credentials ,
260                                   exchangeFolder );
261
262     }
263
264     /**
265      * remove contact
266      *
267      * @param contact
268      * @param username
269      * @param principal
270      * @param exchangeFolder
271      *
272      * @throws sync4j.exchange.util.DataAccessException
273      **/

274     public void removeContact(Contact contact ,
275                               String JavaDoc username ,
276                               String JavaDoc principal ,
277                               String JavaDoc exchangeFolder )
278         throws DataAccessException {
279
280         this.cd.removeContact(contact ,
281                               username ,
282                               principal ,
283                               exchangeFolder );
284
285     }
286
287 }
288
Popular Tags