KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sync4j > exchange > engine > source > ExchangeCalendarSyncSource


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.engine.source;
20
21 import java.io.ByteArrayInputStream JavaDoc;
22 import java.io.Serializable JavaDoc;
23 import java.security.Principal JavaDoc;
24 import java.sql.Timestamp JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.Date JavaDoc;
27 import java.util.logging.Logger JavaDoc;
28 import java.util.logging.Level JavaDoc;
29
30 import sync4j.exchange.xml.XmlParser;
31
32 import sync4j.foundation.pdi.converter.*;
33 import sync4j.foundation.pdi.parser.*;
34
35 import sync4j.framework.logging.Sync4jLogger;
36 import sync4j.framework.engine.SyncItemImpl;
37 import sync4j.framework.engine.SyncItem;
38 import sync4j.framework.engine.SyncItemKey;
39 import sync4j.framework.engine.SyncProperty;
40 import sync4j.framework.engine.SyncItemState;
41 import sync4j.framework.engine.source.SyncSource;
42 import sync4j.framework.engine.source.SyncSourceException;
43 import sync4j.framework.engine.source.AbstractSyncSource;
44 import sync4j.framework.security.Sync4jPrincipal;
45 import sync4j.framework.security.Sync4jPrincipal;
46 import sync4j.framework.server.store.NotFoundException;
47 import sync4j.framework.tools.Base64;
48
49 import sync4j.exchange.items.calendar.model.Calendar;
50 import sync4j.exchange.items.calendar.manager.CalendarManager;
51 import sync4j.exchange.items.calendar.CalendarParseException;
52 import sync4j.exchange.DataAccessException;
53
54 /**
55  * This class define <i>SyncSource</i>
56  * between SyncServer and Exchange Server calendar items
57  *
58  * @author Fabio Maggi @ Funambol
59  *
60  * version $Id: ExchangeCalendarSyncSource.java,v 1.21 2005/06/25 13:43:30 nichele Exp $
61  */

62 public class ExchangeCalendarSyncSource
63 extends ExchangeSyncSource
64 implements SyncSource, Serializable JavaDoc {
65
66     // --------------------------------------------------------------- Constants
67

68     private static final String JavaDoc EXCHANGE_HREF_EXTENSION = ".eml";
69
70     // --------------------------------------------------------------- Private data
71

72     private CalendarManager cm = null ;
73
74     // --------------------------------------------------------------- Constructors
75

76     public ExchangeCalendarSyncSource() {
77     }
78
79     // --------------------------------------------------------------- Public Methods
80

81    /**
82     * @see SyncSource
83     */

84     public SyncItem setSyncItem(Principal JavaDoc principal, SyncItem syncItem)
85     throws SyncSourceException {
86
87         Calendar calendar = null ;
88         Calendar newCalendar = null ;
89
90         String JavaDoc href = null ;
91         String JavaDoc content = null ;
92         String JavaDoc itemKey = null ;
93         String JavaDoc username = null ;
94         String JavaDoc credentials = null ;
95
96         boolean isAddCalendar = false ;
97
98         try {
99
100             username = ((Sync4jPrincipal)principal).getUsername() ;
101
102             credentials = ((Sync4jPrincipal)principal).getEncodedCredentials() ;
103
104             itemKey = syncItem.getKey().getKeyAsString();
105
106             //
107
// search href [eg. 1234.eml] in exchange items
108
//
109
href = getHref(itemKey);
110
111             if (href == null) {
112                 //
113
// if href not found, new item is created
114
// default href = this.CurrentTimesMillis()
115
//
116
href = String.valueOf(System.currentTimeMillis()) +
117                        EXCHANGE_HREF_EXTENSION;
118                 isAddCalendar = true;
119             }
120
121             if (log.isLoggable(Level.FINEST)) {
122                 if (!isAddCalendar) {
123                     log.finest(itemKey + " already exists: updating");
124                 } else {
125                     log.finest(itemKey + " is new: adding");
126                 }
127             }
128
129             calendar = getCalendarFromSyncItem(syncItem);
130
131             calendar.setHref(href);
132
133             cm = getCalendarManager();
134
135             newCalendar = cm.setCalendar (calendar ,
136                                           username ,
137                                           credentials ,
138                                           exchangeFolder);
139
140             if (isAddCalendar) {
141                 toChangedItems(newCalendar.getId(), ITEM_ADD);
142             } else {
143                 toChangedItems(newCalendar.getId(), ITEM_UPDATE);
144             }
145
146
147             syncItem = getSyncItem(newCalendar);
148
149             return syncItem;
150
151         } catch (Exception JavaDoc e) {
152             if (newCalendar != null) {
153                 if (isAddCalendar) {
154                     toChangedItems(newCalendar.getId(), ITEM_ADD_ERROR);
155                 } else {
156                     toChangedItems(newCalendar.getId(), ITEM_UPDATE_ERROR);
157                 }
158             }
159             throw new SyncSourceException( "Error setting the item " +
160                                            syncItem + " " + e.getMessage(), e) ;
161         }
162
163     }
164
165     /**
166      * @see SyncSource
167      */

168     public SyncItem[] setSyncItems(Principal JavaDoc principal, SyncItem[] syncItems)
169     throws SyncSourceException {
170
171         ArrayList JavaDoc syncItemsInError = new ArrayList JavaDoc() ;
172         ArrayList JavaDoc ret = new ArrayList JavaDoc() ;
173
174         for (int i=0, l = syncItems.length; i < l; ++i) {
175             try {
176                 ret.add(setSyncItem(principal, syncItems[i]));
177             } catch (SyncSourceException e) {
178                 syncItemsInError.add(syncItems[i]);
179             }
180         }
181
182         if (syncItemsInError.size() > 0) {
183             throw new SyncSourceException("Error setting the following items: " +
184                                           syncItemsInError.toString()
185                                          );
186         }
187
188         if (log.isLoggable(Level.SEVERE)) {
189             log.severe("Exchange SyncSource " +
190                        sourceURI +
191                        " - set syncItems");
192         }
193
194         return (SyncItem[])ret.toArray(new SyncItem[] {});
195
196     }
197
198     /**
199      * @see SyncSource
200      */

201     public SyncItem getSyncItemFromId(Principal JavaDoc principal,
202                                       SyncItemKey syncItemKey)
203     throws SyncSourceException{
204
205         SyncItem[] syncItems =
206             getSyncItemsFromIds(principal, new SyncItemKey[] {syncItemKey});
207
208         if ((syncItems == null) || (syncItems.length == 0)) {
209             return null;
210         }
211
212         return syncItems[0];
213
214     }
215
216     /**
217      * @see SyncSource
218      */

219     public SyncItem[] getSyncItemsFromIds(Principal JavaDoc principal,
220                                           SyncItemKey[] syncItemKeys)
221     throws SyncSourceException {
222
223         ArrayList JavaDoc syncItems = null ;
224         Calendar calendar = null ;
225
226         String JavaDoc username = null ;
227         String JavaDoc credentials = null ;
228
229         String JavaDoc id = null ;
230
231         username = ((Sync4jPrincipal) principal).getUsername() ;
232         credentials = ((Sync4jPrincipal) principal).getEncodedCredentials() ;
233
234         syncItems = new ArrayList JavaDoc();
235
236         int l = syncItemKeys.length;
237
238         for (int i=0; ((syncItemKeys != null) && (i < l)); ++i) {
239
240             id = syncItemKeys[i].getKeyAsString();
241
242             try {
243                 cm = getCalendarManager();
244                 calendar = cm.getCalendarById(username ,
245                                               credentials ,
246                                               id ,
247                                               this.exchangeFolder);
248             } catch (DataAccessException e) {
249
250                 Throwable JavaDoc previous = e.getCause();
251
252                 if (previous instanceof NotFoundException) {
253
254                     Logger JavaDoc log = Sync4jLogger.getLogger("source");
255                     if (log.isLoggable(Level.SEVERE)) {
256                         log.severe
257                             ("Calendar not found while reading Exchange database: " +
258                               e.getMessage() );
259                     }
260
261                     Logger.
262                         getLogger(LOG_NAME).
263                             throwing(getClass().
264                                 getName(), "readExchangeDatabase", e);
265
266                 } else{
267                     throw new SyncSourceException("Error reading items", e);
268                 }
269             }
270
271             if (calendar != null) {
272                 calendar.setModificationStatus(SyncItemState.NEW) ;
273                 syncItems.add(getSyncItem(calendar)) ;
274             }
275
276         }
277
278         return (SyncItem[]) syncItems.toArray(new SyncItem[syncItems.size()]);
279
280     }
281
282     /**
283      * @see SyncSource
284      */

285     public SyncItemKey[] getNewSyncItemKeys(Principal JavaDoc principal,
286                                             Timestamp JavaDoc since )
287     throws SyncSourceException {
288         return null;
289     }
290
291     /**
292      * @see SyncSource
293      */

294     public SyncItem[] getNewSyncItems(Principal JavaDoc principal,
295                                       Timestamp JavaDoc since )
296     throws SyncSourceException {
297
298         String JavaDoc ids[] = null;
299
300         ids = this.getNewItemIds();
301
302         if (log.isLoggable(Level.SEVERE)) {
303             log.severe("Exchange SyncSource " +
304                        sourceURI +
305                        " - getting new sync items" );
306         }
307
308         if(ids == null || !(ids.length > 0)) {
309             return new SyncItem[0];
310         }
311
312         return filterSyncItems(principal, ids, SyncItemState.NEW);
313
314     }
315
316     /**
317      * @see SyncSource
318      */

319     public SyncItemKey[] getDeletedSyncItemKeys(Principal JavaDoc principal,
320                                                 Timestamp JavaDoc since )
321     throws SyncSourceException {
322         return null;
323     }
324
325     /**
326      * @see SyncSource
327      */

328     public SyncItem[] getDeletedSyncItems(Principal JavaDoc principal,
329                                           Timestamp JavaDoc since )
330     throws SyncSourceException {
331
332         String JavaDoc [] ids = null ;
333         SyncItem[] syncItems = null ;
334
335         ids = this.getDeleteItemIds();
336
337         int l = ids.length;
338
339         syncItems = new SyncItem[l];
340
341         for (int i = 0; i < l; i++) {
342
343             syncItems[i] = new SyncItemImpl(this, ids[i], SyncItemState.DELETED);
344         }
345
346         if (log.isLoggable(Level.SEVERE)) {
347             log.severe("Exchange SyncSource " +
348                        sourceURI +
349                        " - getting deleted sync items" );
350         }
351
352         return syncItems;
353
354     }
355
356     /**
357      * @see SyncSource
358      */

359     public SyncItem[] getUpdatedSyncItems(Principal JavaDoc principal, Timestamp JavaDoc since)
360     throws SyncSourceException {
361
362         String JavaDoc ids[] = null;
363
364         ids = this.getUpdateItemIds();
365
366         if (log.isLoggable(Level.SEVERE)) {
367             log.severe("Exchange SyncSource " +
368                        sourceURI +
369                        " - getting updated sync items" );
370         }
371
372         if(ids == null || !(ids.length > 0)) {
373             return new SyncItem[0];
374         }
375
376         return filterSyncItems(principal, ids, SyncItemState.UPDATED);
377
378     }
379
380     public SyncItemKey[] getUpdatedSyncItemKeys(Principal JavaDoc principal,
381                                                 Timestamp JavaDoc since)
382     throws SyncSourceException {
383         return null;
384     }
385
386     /**
387      * @see SyncSource
388      */

389     public void removeSyncItem(Principal JavaDoc principal, SyncItem syncItem)
390     throws SyncSourceException {
391
392         Calendar calendar = null ;
393
394         String JavaDoc username = null ;
395         String JavaDoc credentials = null ;
396
397         String JavaDoc id = null ;
398         String JavaDoc href = null ;
399
400         id = syncItem.getKey().getKeyAsString() ;
401
402         username = ((Sync4jPrincipal) principal).getUsername() ;
403         credentials = ((Sync4jPrincipal) principal).getEncodedCredentials() ;
404
405         href = getHref(id);
406
407         try {
408             if (href != null) {
409                 calendar = new Calendar(id) ;
410                 calendar.setHref(href) ;
411                 this.cm.removeCalendar(calendar ,
412                                        username ,
413                                        credentials ,
414                                        exchangeFolder) ;
415                 toChangedItems(calendar.getId(), ITEM_REMOVE);
416             } else {
417                 toChangedItems(calendar.getId(), ITEM_REMOVE_ERROR);
418             }
419         } catch (DataAccessException e) {
420            toChangedItems(calendar.getId(), ITEM_REMOVE_ERROR);
421            throw new SyncSourceException("Error reading items", e);
422         }
423
424         if (log.isLoggable(Level.SEVERE)) {
425             log.severe("Exchange SyncSource " +
426                        sourceURI +
427                        " - removing sync items" );
428         }
429
430
431     }
432
433     /**
434      * @see SyncSource
435      */

436     public void removeSyncItems(Principal JavaDoc principal, SyncItem[] syncItems)
437     throws SyncSourceException {
438
439         ArrayList JavaDoc syncItemsInError = new ArrayList JavaDoc();
440
441         for (int i=0, l = syncItems.length ; i < l; ++i) {
442             try {
443                 removeSyncItem(principal, syncItems[i]);
444             } catch (SyncSourceException e) {
445                 syncItemsInError.add(syncItems[i]);
446             }
447         } // next i
448

449         if (syncItemsInError.size() > 0) {
450             throw new SyncSourceException( "Error deleting the following items: "
451                                          + syncItemsInError.toString()
452                                          );
453         }
454
455         if (log.isLoggable(Level.SEVERE)) {
456             log.severe("Exchange SyncSource " +
457                        sourceURI +
458                        " - removing sync items" );
459         }
460
461     }
462
463     /**
464      * @see SyncSource
465      */

466     public SyncItem[] getAllSyncItems(Principal JavaDoc principal)
467     throws SyncSourceException {
468         return filterSyncItems(principal, SyncItemState.NEW);
469     }
470
471     /**
472      * The same as <i>getSyncItemFromId()</i>
473      * @see SyncSource
474      */

475     public SyncItem getSyncItemFromTwin(Principal JavaDoc principal, SyncItem syncItem)
476             throws SyncSourceException {
477
478         String JavaDoc username = ((Sync4jPrincipal) principal).getUsername();
479         String JavaDoc credentials = ((Sync4jPrincipal) principal).getEncodedCredentials();
480         SyncItem syncTwin = null;
481
482         try {
483             this.cm = getCalendarManager();
484             Calendar calendar = getCalendarFromSyncItem(syncItem);
485             Calendar twin = this.cm.getCalendarTwin(calendar,
486                     username,
487                     credentials,
488                     exchangeFolder);
489
490             if (twin != null) {
491                 syncTwin = getSyncItem(twin);
492             }
493
494         } catch (DataAccessException ex) {
495             throw new SyncSourceException("Error getting twin", ex);
496         }
497
498         return syncTwin;
499     }
500
501
502     /**
503      * The same as <i>getSyncItemFromIds()</i>
504      *
505      * @see SyncSource
506      */

507     public SyncItem[] getSyncItemsFromTwins(Principal JavaDoc principal,
508                                             SyncItem[] syncItems) throws
509             SyncSourceException {
510
511         ArrayList JavaDoc items = new ArrayList JavaDoc();
512
513         for (int i = 0, l = syncItems.length; i < l; ++i) {
514             items.add(getSyncItemFromTwin(principal, syncItems[i]));
515         }
516
517         return (SyncItem[]) items.toArray(new SyncItem[items.size()]);
518
519     }
520     // --------------------------------------------------------- Private methods
521

522     private Calendar getCalendar(String JavaDoc id ,
523                                  String JavaDoc content ,
524                                  Date JavaDoc t )
525     throws SyncSourceException {
526
527         Calendar c = new Calendar(id);
528
529         c.setLastUpdate(t) ;
530
531         if (content != null && content.length() > 0) {
532
533             if (isEncode()) {
534                 content = new String JavaDoc (Base64.decode(content));
535             }
536
537             if (TYPE_ICAL.equals(getType())) {
538                 ical2Calendar(content, c);
539             } else {
540                 sife2Calendar(content, c);
541             }
542         }
543
544         return c;
545
546     }
547
548
549
550     /**
551      * Filters the SyncItems in the synchronization database (after a refresh)
552      * based on the given principal, ids, state
553      *
554      * @param principal principal
555      * @param ids an array containing calendar id
556      * @param state calendar state
557      *
558      * @throws SyncSourceException in case of IO errors
559      *
560      * @return an array of SyncItem objects whose state is equal to the given
561      * state.
562      */

563     private SyncItem[] filterSyncItems(Principal JavaDoc principal ,
564                                        String JavaDoc[] ids ,
565                                        char state)
566     throws SyncSourceException {
567
568        Calendar[] calendars = null ;
569
570        String JavaDoc username = null ;
571        String JavaDoc credentials = null ;
572
573        username = ((Sync4jPrincipal) principal).getUsername() ;
574        credentials = ((Sync4jPrincipal) principal).getEncodedCredentials() ;
575
576        try {
577
578            this.cm = getCalendarManager();
579
580
581            calendars = this.cm.getCalendars (username ,
582                                              credentials ,
583                                              ids ,
584                                              this.exchangeFolder) ;
585
586         } catch (DataAccessException e) {
587             throw new SyncSourceException("Error reading calendar: " +
588                                           e.getMessage(), e);
589         }
590
591         return getSyncItems(calendars, state);
592
593     }
594
595     /**
596      * Filters the SyncItems in the synchronization database (after a refresh)
597      * based on the given principal, state
598      *
599      * @param principal principal
600      * @param ids an array containing calendar id
601      * @param state calendar state
602      *
603      * @throws SyncSourceException in case of IO errors
604      *
605      * @return an array of SyncItem objects whose state is equal to the given
606      * state.
607      */

608     private SyncItem[] filterSyncItems(Principal JavaDoc principal ,
609                                        char state)
610     throws SyncSourceException {
611
612        Calendar[] calendars = null ;
613
614        String JavaDoc username = null ;
615        String JavaDoc credentials = null ;
616
617        username = ((Sync4jPrincipal) principal).getUsername() ;
618        credentials = ((Sync4jPrincipal) principal).getEncodedCredentials() ;
619
620        try {
621
622            this.cm = getCalendarManager();
623
624            calendars = this.cm.getAllCalendars (username ,
625                                                 credentials ,
626                                                 this.exchangeFolder) ;
627
628         } catch (DataAccessException e) {
629             throw new SyncSourceException("Error reading calendar: " +
630                                            e.getMessage(), e);
631         }
632
633         return getSyncItems(calendars, state);
634
635     }
636
637     /**
638      * Create SyncItem array from Calendar array
639      *
640      * @param calendars
641      * @param state
642      * @return SyncItem array
643      * @trows SyncSourceException
644      */

645     private SyncItem[] getSyncItems(Calendar[] calendars,
646                                     char state)
647     throws SyncSourceException {
648
649         SyncItem[] syncItems = null;
650
651         int l = calendars.length;
652
653         syncItems = new SyncItem [l];
654
655         for (int i=0; i < l; i++) {
656             calendars [i].setModificationStatus(state) ;
657             syncItems [i] = getSyncItem(calendars[i]) ;
658         }
659
660         return syncItems;
661
662     }
663
664     /**
665      * Create SyncItem from Calendar
666      *
667      * @param calendar
668      * @return SyncItem
669      * @trows SyncSourceException
670      */

671     private SyncItem getSyncItem(Calendar exchangeCalendar)
672     throws SyncSourceException {
673
674         SyncItem syncItem = null ;
675         String JavaDoc content = null ;
676
677         sync4j.foundation.pdi.event.Calendar
678                     calendar = new sync4j.foundation.pdi.event.Calendar();
679
680         syncItem = new SyncItemImpl(this ,
681                                     exchangeCalendar.getId () ,
682                                     exchangeCalendar.getModificationStatus ());
683
684         calendar.setEvent(exchangeCalendar);
685         if (TYPE_ICAL.equals(getType())) {
686             content = calendar2ical(calendar);
687         } else {
688             content = calendar2sife(calendar);
689         }
690
691         if (this.isEncode()) {
692             syncItem.setProperty(
693                 new SyncProperty(SyncItem.PROPERTY_BINARY_CONTENT,
694                                  Base64.encode(content.getBytes())
695             ));
696
697            syncItem.setProperty(
698                new SyncProperty(SyncItem.PROPERTY_FORMAT,"b64")
699            );
700
701         } else {
702             syncItem.setProperty(
703                 new SyncProperty(SyncItem.PROPERTY_BINARY_CONTENT,
704                                  content.getBytes())
705             );
706         }
707
708         return syncItem;
709
710     }
711
712     /**
713      * Converts the given calendar into a ical String
714      *
715      * @param c the calendar to convert
716      *
717      * @return the ical
718      *
719      * @throws SyncSourceException in case of convertion errors
720      */

721     private String JavaDoc calendar2ical(sync4j.foundation.pdi.event.Calendar c)
722     throws SyncSourceException {
723         try {
724             return new CalendarToIcalendar(deviceTimeZone, deviceCharset).convert(c);
725         } catch (ConverterException e) {
726             throw new SyncSourceException( "Conversion error for item "
727                                           + ((Calendar)c.getEvent()).getId()
728                                           + ": "
729                                           + e.getMessage()
730                                           , e
731                                           );
732         }
733     }
734
735     /**
736      * Converts the given calendar into a sife String
737      *
738      * @param c the calendar to convert
739      *
740      * @return the sife document
741      *
742      * @throws SyncSourceException in case of convertion errors
743      */

744     private String JavaDoc calendar2sife(sync4j.foundation.pdi.event.Calendar c)
745     throws SyncSourceException {
746         try {
747             return new CalendarToXML(deviceTimeZone, deviceCharset).convert(c);
748         } catch (ConverterException e) {
749             throw new SyncSourceException( "Convertion error for item "
750                                           + ((Calendar)c.getEvent()).getId()
751                                           + ": "
752                                           + e.getMessage()
753                                           , e
754                                           );
755         }
756     }
757
758
759     /**
760      *
761      * Fill the given CalEvent object with the properties in the given ical
762      *
763      * @param content iCal content
764      * @param c the calendar to feed
765      *
766      * @throws SyncSourceException
767      */

768     private void ical2Calendar (String JavaDoc content, Calendar c)
769     throws SyncSourceException {
770
771         //
772
// just make sure we have something to do...
773
//
774
if ((content == null) || (content.length()==0)) {
775             return;
776         }
777
778
779         ByteArrayInputStream JavaDoc is = null ;
780         ICalendarParser parser = null ;
781
782         sync4j.foundation.pdi.event.Calendar calendar = null ;
783         sync4j.foundation.pdi.event.Event event = null ;
784
785         try {
786
787             /**
788              * This is used to replace the String "=\r\n" with the
789              * String "\r\n ": This replace is necessary because some devices do
790              * not send the correct line delimiter for long lines.
791              */

792             content = content.replaceAll("=\r\n","\r\n ");
793
794             is = new ByteArrayInputStream JavaDoc (content.getBytes());
795
796             //converting the vcard into a Contact object
797

798             parser = new ICalendarParser(is, deviceTimeZoneDescr, deviceCharset);
799             parser.setLogger(log);
800
801             calendar = (sync4j.foundation.pdi.event.Calendar)parser.iCalendar();
802
803             //
804
// We now have to convert a portal calendar into a foundation calendar
805
//
806
event = calendar.getEvent();
807
808             c.setAlarm(event.getAlarm());
809             c.setCategories(event.getCategories());
810             c.setClassEvent(event.getClassEvent());
811             c.setDescription(event.getDescription());
812             c.setLatitude(event.getLatitude());
813             c.setLongitude(event.getLongitude());
814             c.setLocation(event.getLocation());
815             c.setPriority(event.getPriority());
816             c.setStatus(event.getStatus());
817             c.setSummary(event.getSummary());
818             c.setDtEnd(event.getDtEnd());
819             c.setDtStart(event.getDtStart());
820             c.setDuration(event.getDuration());
821             c.setTransp(event.getTransp());
822             c.setOrganizer(event.getOrganizer());
823             c.setUrl(event.getUrl());
824             c.setUid(event.getUid());
825             c.setRrule(event.getRrule());
826             c.setContact(event.getContact());
827             c.setCreated(event.getCreated());
828             c.setDtStamp(event.getDtStamp());
829             c.setLastModified(event.getLastModified());
830             c.setSequence(event.getSequence());
831             c.setXTag(event.getXTags());
832             c.setAllDay(new Boolean JavaDoc(event.isAllDay()));
833             c.setMeetingStatus(event.getMeetingStatus());
834             c.setMileage(event.getMileage());
835             c.setReminder(event.getReminder());
836             c.setReplyTime(event.getReplyTime());
837             c.setRecurrencePattern(event.getRecurrencePattern());
838
839         } catch (Exception JavaDoc e) {
840             throw new SyncSourceException( "Convertion error for item "
841                                           + c.getId()
842                                           + ": "
843                                           + e.getMessage()
844                                           , e
845                                           );
846         } finally {
847             if (is != null) try { is.close(); } catch (Exception JavaDoc e) {}
848         }
849     }
850
851
852     /**
853      *
854      * Fill the given CalEvent object with the properties in the given sife
855      * document
856      *
857      * @param content iCal content
858      * @param c the calendar to feed
859      *
860      * @throws SyncSourceException
861      */

862     private void sife2Calendar (String JavaDoc content, Calendar c)
863     throws SyncSourceException {
864         //
865
// just make sure we have something to do...
866
//
867
if ((content == null) || (content.length()==0)) {
868             return;
869         }
870
871
872         ByteArrayInputStream JavaDoc is = null ;
873         XMLEventParser parser = null ;
874
875         sync4j.foundation.pdi.event.Calendar calendar = null ;
876         sync4j.foundation.pdi.event.Event event = null ;
877
878         try {
879             is = new ByteArrayInputStream JavaDoc (content.getBytes());
880
881             //converting the vcard into a Contact object
882

883             parser = new XMLEventParser(is);
884
885             calendar = (sync4j.foundation.pdi.event.Calendar)parser.parse();
886
887             //
888
// We now have to convert a portal contact into a foundation contact
889
//
890
event = calendar.getEvent();
891
892             c.setAlarm(event.getAlarm());
893             c.setCategories(event.getCategories());
894             c.setClassEvent(event.getClassEvent());
895             c.setDescription(event.getDescription());
896             c.setLatitude(event.getLatitude());
897             c.setLongitude(event.getLongitude());
898             c.setLocation(event.getLocation());
899             c.setPriority(event.getPriority());
900             c.setStatus(event.getStatus());
901             c.setSummary(event.getSummary());
902             c.setDtEnd(event.getDtEnd());
903             c.setDtStart(event.getDtStart());
904             c.setDuration(event.getDuration());
905             c.setTransp(event.getTransp());
906             c.setOrganizer(event.getOrganizer());
907             c.setUrl(event.getUrl());
908             c.setUid(event.getUid());
909             c.setRrule(event.getRrule());
910             c.setContact(event.getContact());
911             c.setCreated(event.getCreated());
912             c.setDtStamp(event.getDtStamp());
913             c.setLastModified(event.getLastModified());
914             c.setSequence(event.getSequence());
915             c.setXTag(event.getXTags());
916             c.setAllDay(new Boolean JavaDoc(event.isAllDay()));
917             c.setMeetingStatus(event.getMeetingStatus());
918             c.setMileage(event.getMileage());
919             c.setReminder(event.getReminder());
920             c.setReplyTime(event.getReplyTime());
921             c.setRecurrencePattern(event.getRecurrencePattern());
922
923         } catch (Exception JavaDoc e) {
924             throw new SyncSourceException( "Parsing error for item "
925                                           + c.getId()
926                                           + ": "
927                                           + e.getMessage()
928                                           , e
929                                           );
930         } finally {
931             if (is != null) try { is.close(); } catch (Exception JavaDoc e) {}
932         }
933     }
934
935     /**
936      *
937      * Create CalendarManager istance
938      * (if not already exist)
939      *
940      * @trows DataAccessException
941      */

942     private CalendarManager getCalendarManager()
943     throws DataAccessException {
944
945         if (this.cm == null) {
946             this.cm = new CalendarManager(this.getHost() ,
947                                           this.getPort());
948         }
949
950         return this.cm;
951
952     }
953
954     /**
955      * Creates a new Calendar from the given syncItem
956      * @param syncItem SyncItem
957      * @return Calendar
958      */

959     private Calendar getCalendarFromSyncItem(SyncItem syncItem)
960             throws SyncSourceException {
961
962         String JavaDoc content = null;
963         Calendar calendar = null;
964
965         String JavaDoc itemKey = syncItem.getKey().getKeyAsString();
966
967         byte[] itemContent =
968                 (byte[]) syncItem.getPropertyValue(
969                         SyncItem.PROPERTY_BINARY_CONTENT);
970
971         if (itemContent == null) {
972             itemContent = new byte[0];
973         }
974
975         content = new String JavaDoc(itemContent);
976
977         Date JavaDoc t =
978                 (Timestamp JavaDoc) syncItem.getPropertyValue(SyncItem.PROPERTY_TIMESTAMP);
979
980         calendar = getCalendar(itemKey, content, t);
981
982         return calendar;
983     }
984 }
985
Popular Tags