KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sync4j > exchange > items > note > dao > NoteDAO


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.note.dao;
20
21 import java.io.IOException JavaDoc;
22 import java.text.MessageFormat JavaDoc;
23 import java.util.ArrayList JavaDoc;
24
25 import sync4j.exchange.httptransport.WebDavHttpTransport;
26 import sync4j.exchange.items.note.model.Note;
27 import sync4j.exchange.items.common.dao.ItemDAO;
28 import sync4j.exchange.AuthenticationException;
29 import sync4j.exchange.ExchangeAccessException;
30 import sync4j.exchange.xml.XmlParseException;
31 import sync4j.exchange.xml.XmlParser;
32
33 import sync4j.exchange.util.StringTools;
34
35 import sync4j.exchange.DataAccessException;
36
37
38 /*
39  * This class implements methods to access note data
40  * in exchange server datastore
41  *
42  * @author Fabio Maggi @ Funambol
43  * @version $Id: NoteDAO.java,v 1.15 2005/07/04 13:46:22 nichele Exp $
44  *
45  **/

46 public class NoteDAO extends ItemDAO {
47
48     //---------------------------------------------------------------- Constants
49

50     private static final String JavaDoc WEBDAV_MSG_PROPPATCH_UPDATE_NOTE =
51     "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n" +
52     "<D:propertyupdate xmlns:D=\"DAV:\" " +
53     "xmlns:EX=\"http://schemas.microsoft.com/exchange/\" " +
54     "xmlns:HN=\"urn:schemas:httpmail:\">\n" +
55     "<D:set>\n" +
56     " <D:prop>\n" +
57     " <D:contentclass>urn:content-classes:person</D:contentclass>\n" +
58     " <EX:outlookmessageclass>IPM.StickyNote</EX:outlookmessageclass>\n" +
59     " <HN:subject>{0}</HN:subject>\n" +
60     " <HN:textdescription>{1}</HN:textdescription>\n" +
61     " <HN:date>{2}</HN:date>\n" +
62     " </D:prop>\n" +
63     "</D:set>\n" +
64     "</D:propertyupdate>" ;
65
66     private static final String JavaDoc WEBDAV_MSG_SELECT_NOTES =
67     "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n" +
68     "<D:searchrequest xmlns:D =\"DAV:\">\n" +
69     "<D:sql>\n" +
70     "Select " +
71     "\"urn:schemas:httpmail:textdescription\" AS textdescription, " +
72     "\"urn:schemas:httpmail:subject\" AS subject, " +
73     "\"urn:schemas:httpmail:date\" AS date, " +
74     "\"http://schemas.microsoft.com/repl/repl-uid\" AS repluid, " +
75     "\"DAV:getlastmodified\" AS getlastmodified, " +
76     "\"DAV:isfolder\" AS isfolder " +
77     "FROM \"/{0}/{1}/{2}\" " +
78     "{3} " +
79     "</D:sql>\n" +
80     "</D:searchrequest>" ;
81
82     public static final String JavaDoc TAG_DATE = "date" ;
83     public static final String JavaDoc TAG_DESCRIPTION =
84         "textdescription" ;
85     public static final String JavaDoc TAG_RESPONSE = "a:response" ;
86     public static final String JavaDoc TAG_SUBJECT = "subject" ;
87     public static final String JavaDoc TAG_REPLUID = "repluid" ;
88     public static final String JavaDoc TAG_LAST_MODIFIED = "getlastmodified" ;
89
90     //------------------------------------------------------------- Private data
91

92     private WebDavHttpTransport webDavHttp = null ;
93
94     //------------------------------------------------------------- Costructors
95

96     public NoteDAO(String JavaDoc exchangeServerHost ,
97                    int exchangeServerPort )
98         throws DataAccessException {
99
100         this.webDavHttp = new WebDavHttpTransport(exchangeServerHost,
101                                                           exchangeServerPort);
102
103     }
104     //------------------------------------------------------------- Public methods
105

106     /**
107      * insert / update note
108      *
109      * @param note
110      * @param username
111      * @param credentials
112      * @param exchangeFolder
113      *
114      * @return new / updated note
115      *
116      * @throws sync4j.exchange.util.DataAccessException
117      **/

118     public Note setNote(Note note ,
119                         String JavaDoc username ,
120                         String JavaDoc credentials ,
121                         String JavaDoc exchangeFolder )
122     throws DataAccessException {
123
124         String JavaDoc webDavMsg = null ;
125
126         String JavaDoc response = null ;
127
128         String JavaDoc id = null ;
129
130         String JavaDoc webDavNoteMsg = null ;
131         String JavaDoc webDavHeaderMsg = null ;
132
133         String JavaDoc date = null ;
134
135         String JavaDoc server = null ;
136         String JavaDoc resource = null ;
137
138         server = getServerFromExchangeFolder (exchangeFolder);
139         resource = getResourceFromExchangeFolder (exchangeFolder );
140
141         date = dateToWebDavTag(note.getDate());
142
143         webDavNoteMsg
144             = MessageFormat.format(WEBDAV_MSG_PROPPATCH_UPDATE_NOTE,
145               new Object JavaDoc[] {
146                 StringTools.escapeXml(note.getSubject()) ,
147                 StringTools.escapeXml(note.getTextDescription()),
148                 date
149               }
150         );
151
152         webDavHeaderMsg
153             = MessageFormat.format(ItemDAO.WEBDAV_HEADER_PROPPATCH,
154                 new Object JavaDoc[] {
155                     "/" +
156                         server +
157                         "/" +
158                         username +
159                         "/" +
160                         resource ,
161                     note.getHref()
162               }
163         );
164
165
166         try {
167             response = this.webDavHttp.sendRequest(webDavHeaderMsg, credentials, webDavNoteMsg, FILE_ENCODING);
168         } catch (Exception JavaDoc e) {
169             throw new
170                 DataAccessException("Error getting Exchange server response", e);
171         }
172
173         try {
174             int s = getStatusFromResponse(response);
175             checkResponseStatus(s);
176         } catch (Exception JavaDoc e) {
177
178             throw new DataAccessException("USER " +
179                                           username +
180                                           " URI " +
181                                           exchangeFolder +
182                                           " getting Exchange note" ,
183                                           e) ;
184
185         }
186
187         try {
188             id = XmlParser.getRuidFromResponse(response);
189         } catch (XmlParseException e) {
190             throw new DataAccessException("Error getting note id", e);
191         }
192
193         note.setId(id);
194
195         return note;
196
197     }
198
199     /**
200      * Delete a <i>Note</i> from Exchange server.
201      *
202      * @param note
203      * @param username
204      * @param credentials
205      * @param exchangeFolder
206      *
207      * @throws sync4j.exchange.util.DataAccessException
208      *
209      */

210     public void removeNote(Note note ,
211                            String JavaDoc username ,
212                            String JavaDoc credentials ,
213                            String JavaDoc exchangeFolder )
214         throws DataAccessException {
215
216         String JavaDoc webDavNoteMsg = null ;
217         String JavaDoc webDavHeaderMsg = null ;
218         String JavaDoc response = null ;
219
220         String JavaDoc server = null ;
221         String JavaDoc resource = null ;
222
223         server = getServerFromExchangeFolder (exchangeFolder );
224         resource = getResourceFromExchangeFolder (exchangeFolder );
225
226         webDavNoteMsg = "" ;
227
228         webDavHeaderMsg =
229             MessageFormat.format(ItemDAO.WEBDAV_HEADER_REMOVE,
230             new Object JavaDoc[] {
231                         "/" +
232                             server +
233                             "/" +
234                             username +
235                             "/" +
236                             resource ,
237                         note.getHref()
238             }
239         );
240
241
242         try {
243             response = this.webDavHttp.sendRequest(webDavHeaderMsg, credentials, webDavNoteMsg, FILE_ENCODING);
244         } catch (IOException JavaDoc e) {
245             throw new
246                 DataAccessException("Error getting Exchange server response", e);
247         }
248
249         try {
250             int s = getStatusFromResponse(response);
251             checkResponseStatus(s);
252         } catch (Exception JavaDoc e) {
253
254             throw new DataAccessException("USER " +
255                                           username +
256                                           " URI " +
257                                           exchangeFolder +
258                                           " removing exchange note" ,
259                                           e) ;
260
261         }
262
263     }
264
265     /**
266      * get notes from Exchange Server
267      *
268      * @param username
269      * @param credentials
270      * @param ids
271      * @param exchangeFolder
272      *
273      * @return array of find notes
274      *
275      * @throws sync4j.exchange.util.DataAccessException
276      **/

277     public Note[] getNotes (String JavaDoc username ,
278                             String JavaDoc credentials ,
279                             String JavaDoc[] ids ,
280                             String JavaDoc exchangeFolder )
281         throws DataAccessException {
282
283         Note[] exchangeNotes = null ;
284
285         String JavaDoc clause = null ;
286
287         clause = getClause(ids);
288
289         exchangeNotes = getNotes(username, credentials, clause, exchangeFolder);
290
291         return exchangeNotes;
292     }
293
294     /**
295      * get task array from Exchange Server
296      *
297      * @param username
298      * @param credentials
299      * @param fields
300      * @param values
301      * @param exchangeFolder
302      * @return array of find tasks
303      *
304      * @throws sync4j.exchange.util.DataAccessException
305      **/

306     public Note[] getNotes(String JavaDoc username,
307                            String JavaDoc credentials,
308                            String JavaDoc fields[],
309                            Object JavaDoc values[],
310                            String JavaDoc exchangeFolder) throws DataAccessException {
311
312         String JavaDoc clause = null ;
313
314         clause = getClause(fields, values);
315
316         return getNotes(username, credentials, clause, exchangeFolder);
317     }
318
319      //------------------------------------------------------------- Private methods
320

321     /**
322      * Create a <i>Note</i> array object from WebDav msg.
323      *
324      * @param id
325      * @param webDavMsg
326      *
327      * @return a newly create <i>Note</i> object initialized with the
328      * fields in the result set
329      *
330      */

331     private Note[] getNotesFromWebDavMsg(String JavaDoc webDavMsg)
332     throws DataAccessException {
333
334         ArrayList JavaDoc notes = null ;
335         String JavaDoc [] resps = null ;
336         String JavaDoc [] msg = null ;
337
338         String JavaDoc response = null ;
339         String JavaDoc id = null ;
340         String JavaDoc replUid = null ;
341
342         String JavaDoc isFolder = null ;
343         int count = 0 ;
344
345         msg = new String JavaDoc[] {webDavMsg};
346
347         notes = new ArrayList JavaDoc();
348
349         try {
350
351             resps = XmlParser.getXMLTag(msg, TAG_RESPONSE);
352
353             for (int i = 0, l = resps.length; i < l; i++) {
354
355                 response = resps[i] ;
356                 replUid = XmlParser.getXMLInitTagValue(response, TAG_REPLUID);
357                 id = getIdFromReplUid(replUid) ;
358
359                 isFolder = XmlParser.getXMLInitTagValue(response,
360                                                             TAG_IS_FOLDER) ;
361                 if(PROP_NO_FOLDER.equals(isFolder)) {
362                     notes.add(getNoteFromResponseTag(id, response));
363                     count ++;
364                 }
365             }
366
367         } catch (XmlParseException e) {
368             throw new DataAccessException("Error parsing note item", e);
369         }
370
371         return (Note[])notes.toArray(new Note[count]);
372
373     }
374
375     /**
376      * Create a <i>Note</i> object from response tag.
377      *
378      * @param id
379      * @param msgResponse
380      *
381      * @return a newly create <i>Note</i> object initialized with the
382      * fields in the result set
383      *
384      */

385     private Note getNoteFromResponseTag(String JavaDoc id, String JavaDoc msgResponse)
386     throws DataAccessException {
387
388         Note n = null ;
389         String JavaDoc lastUpdate = null ;
390         String JavaDoc date = null ;
391
392         n = new Note(id);
393
394         try {
395
396             n.setTextDescription (XmlParser.getXMLInitTagValue
397                                             (msgResponse, TAG_DESCRIPTION)) ;
398
399             n.setSubject (XmlParser.getXMLInitTagValue
400                                             (msgResponse, TAG_SUBJECT)) ;
401
402             date = XmlParser.getXMLInitTagValue
403                                             (msgResponse, TAG_DATE) ;
404
405             lastUpdate = XmlParser.getXMLInitTagValue
406                                             (msgResponse, TAG_LAST_MODIFIED) ;
407
408             n.setDate (XmlParser.webDavTagToDate(date)) ;
409
410             n.setLastModified (XmlParser.webDavTagToDate
411                                             (lastUpdate)) ;
412
413         } catch (XmlParseException e) {
414             throw new DataAccessException("Error parsing note item", e);
415         }
416
417         return n;
418     }
419
420     /**
421      * get tasks from Exchange Server with the given clause
422      *
423      * @param username
424      * @param credentials
425      * @param clause
426      * @param exchangeFolder
427      * @return array of find task
428      *
429      * @throws sync4j.exchange.util.DataAccessException
430      **/

431     private Note[] getNotes(String JavaDoc username,
432                             String JavaDoc credentials,
433                             String JavaDoc clause,
434                             String JavaDoc exchangeFolder) throws DataAccessException {
435
436         Note[] exchangeNotes = null;
437
438         String JavaDoc response = null;
439         String JavaDoc webDavNoteMsg = null;
440         String JavaDoc webDavHeaderMsg = null;
441
442         String JavaDoc server = null;
443         String JavaDoc resource = null;
444
445         server = getServerFromExchangeFolder(exchangeFolder);
446         resource = getResourceFromExchangeFolder(exchangeFolder);
447
448         webDavNoteMsg =
449             MessageFormat.format(WEBDAV_MSG_SELECT_NOTES,
450             new Object JavaDoc[] {server, username, resource, clause});
451
452
453         webDavHeaderMsg =
454             MessageFormat.format(ItemDAO.WEBDAV_HEADER_SELECT,
455
456             new Object JavaDoc[] {
457                           "/" +
458                           server +
459                           "/" +
460                           username +
461                           "/" +
462                           resource
463             }
464         );
465
466
467
468         try {
469             response = this.webDavHttp.sendRequest(webDavHeaderMsg, credentials, webDavNoteMsg, FILE_ENCODING);
470         } catch (IOException JavaDoc e) {
471             throw new
472                 DataAccessException("Error getting Exchange server response", e);
473         }
474
475         try {
476             int s = getStatusFromResponse(response);
477             checkResponseStatus(s);
478         } catch (Exception JavaDoc e) {
479
480             throw new DataAccessException("USER " +
481                                           username +
482                                           " URI " +
483                                           exchangeFolder +
484                                           " getting exchange note" ,
485                                           e) ;
486
487         }
488
489         exchangeNotes = getNotesFromWebDavMsg(response);
490         return exchangeNotes;
491     }
492
493     /**
494      * Build a clause
495      * to webdav select request
496      * between ids item array
497      *
498      * @param ids
499      * @return webdav select clause
500      **/

501     private static String JavaDoc getClause (String JavaDoc[] fields, Object JavaDoc[] values) {
502
503         StringBuffer JavaDoc clause = new StringBuffer JavaDoc();
504
505         int l = fields.length;
506
507         if (l == 0) {
508             return "";
509         }
510
511         clause.append("where ");
512
513         StringBuffer JavaDoc tmp = null;
514         String JavaDoc valueTmp = null;
515
516         for (int i = 0; i < l; i++) {
517             tmp = new StringBuffer JavaDoc();
518
519             valueTmp = "";
520
521             if (TAG_SUBJECT.equals(fields[i])) {
522                 tmp.append("\"urn:schemas:httpmail:subject\"");
523
524                 if (values[i] != null) {
525                     tmp.append("='");
526                     valueTmp = String.valueOf(values[i]);
527                     valueTmp.replaceAll("'","''");
528                     valueTmp = StringTools.escapeXml(valueTmp);
529                     tmp.append(valueTmp).append("'");
530                 } else {
531                     tmp.append(" is null ");
532                 }
533
534             }
535
536             if (i != 0 && tmp.length() != 0) {
537                 clause.append(" and ").append(tmp);
538             } else if (tmp.length() != 0) {
539                 clause.append(tmp);
540             }
541
542         }
543
544         clause.append("\r\n");
545
546         return clause.toString();
547     }
548 }
549
Popular Tags