1 18 19 package sync4j.exchange.items.note.dao; 20 21 import java.io.IOException ; 22 import java.text.MessageFormat ; 23 import java.util.ArrayList ; 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 46 public class NoteDAO extends ItemDAO { 47 48 50 private static final String 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 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 TAG_DATE = "date" ; 83 public static final String TAG_DESCRIPTION = 84 "textdescription" ; 85 public static final String TAG_RESPONSE = "a:response" ; 86 public static final String TAG_SUBJECT = "subject" ; 87 public static final String TAG_REPLUID = "repluid" ; 88 public static final String TAG_LAST_MODIFIED = "getlastmodified" ; 89 90 92 private WebDavHttpTransport webDavHttp = null ; 93 94 96 public NoteDAO(String exchangeServerHost , 97 int exchangeServerPort ) 98 throws DataAccessException { 99 100 this.webDavHttp = new WebDavHttpTransport(exchangeServerHost, 101 exchangeServerPort); 102 103 } 104 106 118 public Note setNote(Note note , 119 String username , 120 String credentials , 121 String exchangeFolder ) 122 throws DataAccessException { 123 124 String webDavMsg = null ; 125 126 String response = null ; 127 128 String id = null ; 129 130 String webDavNoteMsg = null ; 131 String webDavHeaderMsg = null ; 132 133 String date = null ; 134 135 String server = null ; 136 String 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 [] { 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 [] { 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 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 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 210 public void removeNote(Note note , 211 String username , 212 String credentials , 213 String exchangeFolder ) 214 throws DataAccessException { 215 216 String webDavNoteMsg = null ; 217 String webDavHeaderMsg = null ; 218 String response = null ; 219 220 String server = null ; 221 String 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 [] { 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 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 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 277 public Note[] getNotes (String username , 278 String credentials , 279 String [] ids , 280 String exchangeFolder ) 281 throws DataAccessException { 282 283 Note[] exchangeNotes = null ; 284 285 String clause = null ; 286 287 clause = getClause(ids); 288 289 exchangeNotes = getNotes(username, credentials, clause, exchangeFolder); 290 291 return exchangeNotes; 292 } 293 294 306 public Note[] getNotes(String username, 307 String credentials, 308 String fields[], 309 Object values[], 310 String exchangeFolder) throws DataAccessException { 311 312 String clause = null ; 313 314 clause = getClause(fields, values); 315 316 return getNotes(username, credentials, clause, exchangeFolder); 317 } 318 319 321 331 private Note[] getNotesFromWebDavMsg(String webDavMsg) 332 throws DataAccessException { 333 334 ArrayList notes = null ; 335 String [] resps = null ; 336 String [] msg = null ; 337 338 String response = null ; 339 String id = null ; 340 String replUid = null ; 341 342 String isFolder = null ; 343 int count = 0 ; 344 345 msg = new String [] {webDavMsg}; 346 347 notes = new ArrayList (); 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 385 private Note getNoteFromResponseTag(String id, String msgResponse) 386 throws DataAccessException { 387 388 Note n = null ; 389 String lastUpdate = null ; 390 String 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 431 private Note[] getNotes(String username, 432 String credentials, 433 String clause, 434 String exchangeFolder) throws DataAccessException { 435 436 Note[] exchangeNotes = null; 437 438 String response = null; 439 String webDavNoteMsg = null; 440 String webDavHeaderMsg = null; 441 442 String server = null; 443 String resource = null; 444 445 server = getServerFromExchangeFolder(exchangeFolder); 446 resource = getResourceFromExchangeFolder(exchangeFolder); 447 448 webDavNoteMsg = 449 MessageFormat.format(WEBDAV_MSG_SELECT_NOTES, 450 new Object [] {server, username, resource, clause}); 451 452 453 webDavHeaderMsg = 454 MessageFormat.format(ItemDAO.WEBDAV_HEADER_SELECT, 455 456 new Object [] { 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 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 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 501 private static String getClause (String [] fields, Object [] values) { 502 503 StringBuffer clause = new StringBuffer (); 504 505 int l = fields.length; 506 507 if (l == 0) { 508 return ""; 509 } 510 511 clause.append("where "); 512 513 StringBuffer tmp = null; 514 String valueTmp = null; 515 516 for (int i = 0; i < l; i++) { 517 tmp = new StringBuffer (); 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 |