1 18 19 package sync4j.exchange.engine.source; 20 21 import java.io.Serializable ; 22 import java.security.Principal ; 23 import java.util.ArrayList ; 24 import java.util.Date ; 25 import java.util.HashMap ; 26 import java.util.Iterator ; 27 import java.util.logging.Logger ; 28 import java.util.logging.Level ; 29 import java.util.TimeZone ; 30 31 import sync4j.framework.core.AlertCode; 32 import sync4j.framework.logging.Sync4jLogger; 33 import sync4j.framework.engine.SyncItemImpl; 34 import sync4j.framework.engine.SyncItem; 35 import sync4j.framework.engine.SyncItemKey; 36 import sync4j.framework.engine.SyncProperty; 37 import sync4j.framework.engine.SyncItemState; 38 import sync4j.framework.engine.source.SyncSource; 39 import sync4j.framework.engine.source.SyncSourceException; 40 import sync4j.framework.engine.source.AbstractSyncSource; 41 import sync4j.framework.server.Sync4jDevice; 42 import sync4j.framework.server.store.PersistentStore; 43 import sync4j.framework.server.store.PersistentStoreException; 44 45 import sync4j.exchange.items.common.model.Item; 46 import sync4j.exchange.items.common.manager.ItemManager; 47 import sync4j.exchange.DataAccessException; 48 49 import sync4j.framework.security.Sync4jPrincipal; 50 51 import sync4j.server.config.Configuration; 52 53 67 public abstract class ExchangeSyncSource 68 extends AbstractSyncSource 69 implements SyncSource, Serializable { 70 71 73 protected static final String ITEM_ADD = "A" ; 74 protected static final String ITEM_REMOVE = "D" ; 75 protected static final String ITEM_UPDATE = "U" ; 76 77 protected static final String ITEM_ADD_ERROR = "AE" ; 78 protected static final String ITEM_REMOVE_ERROR = "DE" ; 79 protected static final String ITEM_UPDATE_ERROR = "UE" ; 80 81 protected static final String TYPE_VCARD = "text/x-vcard" ; 82 protected static final String TYPE_ICAL = "text/x-vcalendar" ; 83 84 86 private Item[] localItems = null ; 87 private Item[] exchangeItems = null ; 88 89 private String [] newItemIds = null ; 90 private String [] deleteItemIds = null ; 91 private String [] updateItemIds = null ; 92 93 private HashMap changedItems = null ; 94 95 protected String deviceTimeZoneDescr = null ; 96 protected TimeZone deviceTimeZone = null ; 97 98 protected String deviceCharset = null ; 99 100 protected Logger log = null ; 101 102 104 107 protected String exchangeFolder; 108 109 113 public String getExchangeFolder() { 114 return exchangeFolder; 115 } 116 117 121 public void setExchangeFolder(String exchangeFolder) { 122 this.exchangeFolder = exchangeFolder; 123 } 124 125 128 private String host; 129 130 134 public String getHost() { 135 return host; 136 } 137 138 142 public void setHost(String host) { 143 this.host = host; 144 } 145 146 149 private int port = 80; 150 151 155 public int getPort() { 156 return port; 157 } 158 159 163 public void setPort(int port) { 164 this.port = port; 165 } 166 167 170 private boolean encode = false; 171 172 176 public boolean isEncode() { 177 return encode; 178 } 179 180 184 public void setEncode(boolean encode) { 185 this.encode = encode; 186 } 187 188 191 private String schedule; 192 193 197 public String getSchedule() { 198 return schedule; 199 } 200 201 205 public void setSchedule(String schedule) { 206 this.schedule = schedule; 207 } 208 209 212 private boolean scheduleEnabled; 213 214 218 public boolean isSchedulEnablede() { 219 return scheduleEnabled; 220 } 221 222 226 public void setScheduleEnabled(boolean scheduleEnabled) { 227 this.scheduleEnabled = scheduleEnabled; 228 } 229 230 234 public String [] getNewItemIds() { 235 return this.newItemIds; 236 } 237 238 242 public String [] getUpdateItemIds() { 243 return this.updateItemIds; 244 } 245 246 250 public String [] getDeleteItemIds() { 251 return this.deleteItemIds; 252 } 253 254 256 259 public void beginSync(Principal principal, int syncMode) 260 throws SyncSourceException { 261 262 String principalId = null ; 263 String username = null ; 264 String credentials = null ; 265 266 ItemManager im = null ; 267 268 log = Sync4jLogger.getLogger("source"); 269 270 changedItems = new HashMap (); 271 272 principalId = ((Sync4jPrincipal) principal).getId() ; 273 username = ((Sync4jPrincipal) principal).getUsername() ; 274 credentials = ((Sync4jPrincipal) principal).getEncodedCredentials() ; 275 276 try { 277 String deviceId = ((Sync4jPrincipal)principal).getDeviceId(); 278 Sync4jDevice device = getDevice (deviceId) ; 279 String timezone = device.getTimeZone ( ) ; 280 if (device.getConvertDate ()) { 281 if (timezone != null && timezone.length() > 0) { 282 deviceTimeZoneDescr = timezone; 283 deviceTimeZone = TimeZone.getTimeZone(deviceTimeZoneDescr); 284 } 285 } 286 287 deviceCharset = device.getCharset(); 288 } catch (Exception e) { 289 throw new SyncSourceException(e.getMessage()); 290 } 291 292 293 try { 294 295 im = new ItemManager (this.host , 296 this.port); 297 298 if ((syncMode != AlertCode.SLOW) && 299 (syncMode != AlertCode.REFRESH_FROM_SERVER)) { 300 301 this.exchangeItems = im.getExchangeItems (username , 302 credentials , 303 this.exchangeFolder ) ; 304 305 this.localItems = im.getLocalItems (this.sourceURI, 306 principalId) ; 307 308 this.newItemIds = findNewItemIds () ; 309 this.updateItemIds = findUpdateItemIds () ; 310 this.deleteItemIds = findDeleteItemIds () ; 311 312 } 313 314 if (log.isLoggable(Level.SEVERE)) { 315 log.severe("Exchange SyncSource " + sourceURI + " - begineSync"); 316 } 317 318 } catch (DataAccessException e) { 319 throw new SyncSourceException("Error accessing items: " + 320 e.getMessage(), e); 321 } 322 323 324 } 325 326 329 public void endSync(Principal principal) 330 throws SyncSourceException { 331 332 ItemManager im = null ; 333 334 Item [] exchItems = null ; 335 String id = null ; 336 337 String principalId = null ; 338 String username = null ; 339 String credentials = null ; 340 341 principalId = ((Sync4jPrincipal) principal).getId() ; 342 username = ((Sync4jPrincipal) principal).getUsername() ; 343 credentials = ((Sync4jPrincipal) principal).getEncodedCredentials() ; 344 345 try { 346 347 im = new ItemManager (this.host , 348 this.port ); 349 350 exchItems = im.getExchangeItems (username , 358 credentials , 359 this.exchangeFolder ) ; 360 361 im.updateLocalItems (exchItems , 362 this.sourceURI , 363 principalId); 364 365 if (log.isLoggable(Level.SEVERE)) { 366 log.severe("Exchange SyncSource " + sourceURI + " - endSync"); 367 log.severe("Items changed: " + changedItems.size()); 368 369 Iterator i = changedItems.keySet().iterator(); 370 371 while(i.hasNext()) { 372 373 id = (String ) i.next(); 374 375 log.severe("Item id: " + id + 376 " Status: " + changedItems.get(id)); 377 } 378 379 } 380 381 } catch (DataAccessException e) { 382 throw new SyncSourceException("Error updating items:" + 383 e.getMessage(), e); 384 } 385 386 } 387 388 390 395 private String [] findNewItemIds () { 396 397 ArrayList newRows = null ; 398 String [] ids = null ; 399 Item exchangeItem = null ; 400 401 int count = 0 ; 402 403 newRows = new ArrayList (); 404 405 for (int i = 0, l = this.exchangeItems.length; i < l; i++) { 406 407 exchangeItem = this.exchangeItems[i]; 408 409 if (!this.isItemInArray(this.localItems, exchangeItem)) { 410 newRows.add(exchangeItem.getId()); 411 count++; 412 } 413 414 } 415 416 return (String [])newRows.toArray(new String [count]) ; 417 418 } 419 420 425 private String [] findDeleteItemIds () { 426 427 ArrayList deleteRows = null ; 428 String [] ids = null ; 429 Item localItem = null ; 430 431 int count = 0; 432 433 deleteRows = new ArrayList (); 434 435 for (int i=0, l = this.localItems.length; i < l; i++) { 436 437 localItem = localItems[i]; 438 439 if (!this.isItemInArray(this.exchangeItems, localItem)) { 440 deleteRows.add(localItem.getId()); 441 count++; 442 } 443 444 } 445 446 return (String [])deleteRows.toArray(new String [count]) ; 447 448 } 449 450 455 private String [] findUpdateItemIds () { 456 457 ArrayList updateRows = null ; 458 String [] ids = null ; 459 460 Item exchangeItem = null ; 461 Item localItem = null ; 462 463 Date excItemLastModified = null ; 464 Date localItemLastModified = null ; 465 466 String excItemLastModifiedStr = null; 467 String localItemLastModifiedStr = null ; 468 469 int count = 0 ; 470 471 updateRows = new ArrayList (); 472 473 for (int i = 0, l = this.exchangeItems.length; i < l; i++) { 474 475 exchangeItem = this.exchangeItems[i] ; 476 localItem = this.getItemById(this.localItems, 477 exchangeItem.getId()) ; 478 479 if (localItem != null) { 480 481 excItemLastModified = exchangeItem.getLastModified () ; 482 localItemLastModified = localItem.getLastModified () ; 483 484 excItemLastModifiedStr = String.valueOf(excItemLastModified) ; 489 localItemLastModifiedStr = String.valueOf(localItemLastModified) ; 490 491 if (!excItemLastModifiedStr.equals(localItemLastModifiedStr)) { 492 493 updateRows.add(exchangeItem.getId()); 494 count++; 495 496 } 497 498 } 499 500 } 501 502 return (String [])updateRows.toArray(new String [count]) ; 503 504 } 505 506 513 protected String getHref(String id) { 514 515 Item exchangeItem = null ; 516 String href = null ; 517 518 if (exchangeItems != null) { 522 523 for (int i=0, l = exchangeItems.length; i < l; i++) { 524 525 exchangeItem = exchangeItems[i]; 526 527 if (exchangeItem.getId().equals(id)) { 528 529 href = exchangeItem.getHref(); 530 break; 531 } 532 533 } 534 535 } 536 537 return href; 538 539 } 540 541 542 549 protected void toChangedItems (String id, String status) { 550 changedItems.put(id, status); 551 } 552 553 555 563 private Item getItemById(Item[] items, 564 String id) { 565 566 Item item = null; 567 Item sequenceItem = null; 568 569 for (int i = 0, l = items.length; i < l; i++) { 570 571 sequenceItem = items[i]; 572 573 if (sequenceItem.getId().equals(id)) { 574 item = sequenceItem; 575 break; 576 } 577 578 } 579 580 return item; 581 582 } 583 584 594 private boolean isIdInArray(Item[] items, 595 String id) { 596 597 boolean isFind = false; 598 599 for (int i = 0, l = items.length; i < l; i++) { 600 601 if (items[i].getId().equals(id)) { 602 isFind = true; 603 break; 604 } 605 606 } 607 608 return isFind; 609 610 } 611 612 622 private boolean isItemInArray(Item[] items, 623 Item item) { 624 625 return isIdInArray(items, item.getId()); 626 627 } 628 629 630 636 private Sync4jDevice getDevice(String deviceId) throws PersistentStoreException { 637 Sync4jDevice device = new Sync4jDevice(deviceId); 638 PersistentStore store = Configuration.getConfiguration().getStore(); 639 store.read(device); 640 return device; 641 } 642 643 644 } 645 | Popular Tags |