/
VCalendar.java
1320 lines (1261 loc) · 51.6 KB
/
VCalendar.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package jfxtras.labs.icalendarfx;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.Reader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
import javafx.util.Callback;
import jfxtras.labs.icalendarfx.components.SimpleVComponentFactory;
import jfxtras.labs.icalendarfx.components.VComponent;
import jfxtras.labs.icalendarfx.components.VDisplayable;
import jfxtras.labs.icalendarfx.components.VEvent;
import jfxtras.labs.icalendarfx.components.VFreeBusy;
import jfxtras.labs.icalendarfx.components.VJournal;
import jfxtras.labs.icalendarfx.components.VPersonal;
import jfxtras.labs.icalendarfx.components.VTimeZone;
import jfxtras.labs.icalendarfx.components.VTodo;
import jfxtras.labs.icalendarfx.content.MultiLineContent;
import jfxtras.labs.icalendarfx.itip.AbstractITIPFactory;
import jfxtras.labs.icalendarfx.itip.DefaultITIPFactory;
import jfxtras.labs.icalendarfx.itip.Processable;
import jfxtras.labs.icalendarfx.properties.PropertyType;
import jfxtras.labs.icalendarfx.properties.calendar.CalendarScale;
import jfxtras.labs.icalendarfx.properties.calendar.Method;
import jfxtras.labs.icalendarfx.properties.calendar.Method.MethodType;
import jfxtras.labs.icalendarfx.properties.calendar.ProductIdentifier;
import jfxtras.labs.icalendarfx.properties.calendar.Version;
import jfxtras.labs.icalendarfx.properties.component.misc.NonStandardProperty;
import jfxtras.labs.icalendarfx.properties.component.misc.RequestStatus;
import jfxtras.labs.icalendarfx.utilities.DateTimeUtilities;
import jfxtras.labs.icalendarfx.utilities.ICalendarUtilities;
import jfxtras.labs.icalendarfx.utilities.UnfoldingStringIterator;
/**
* iCalendar Object
* RFC 5545, 3.4, page 50
*
* Parent calendar object represents a collection of calendaring and scheduling information
*
* @author David Bal
*
*/
public class VCalendar extends VParentBase
{
// version of this project, not associated with the iCalendar specification version
// public static String myVersion = "1.0";
private static final String NAME = "VCALENDAR";
@Override public String name() { return NAME; }
private static final String FIRST_CONTENT_LINE = "BEGIN:" + NAME;
private static final String LAST_CONTENT_LINE = "END:" + NAME;
/*
* Calendar properties
*/
/**
* CALSCALE: RFC 5545 iCalendar 3.7.1. page 76
* This property defines the calendar scale used for the
* calendar information specified in the iCalendar object.
*
* This project is based on the Gregorian calendar scale.
* The Gregorian calendar scale is assumed if this property is not
* specified in the iCalendar object. It is expected that other
* calendar scales will be defined in other specifications or by
* future versions of iCalendar.
*
* The default value is "GREGORIAN"
*
* Example:
* CALSCALE:GREGORIAN
* */
ObjectProperty<CalendarScale> calendarScaleProperty()
{
if (calendarScale == null)
{
calendarScale = new SimpleObjectProperty<CalendarScale>(this, CalendarProperty.CALENDAR_SCALE.toString());
orderer().registerSortOrderProperty(calendarScale);
}
return calendarScale;
}
public CalendarScale getCalendarScale() { return (calendarScale == null) ? null : calendarScaleProperty().get(); }
private ObjectProperty<CalendarScale> calendarScale;
public void setCalendarScale(String calendarScale) { setCalendarScale(CalendarScale.parse(calendarScale)); }
public void setCalendarScale(CalendarScale calendarScale) { calendarScaleProperty().set(calendarScale); }
public VCalendar withCalendarScale(CalendarScale calendarScale)
{
setCalendarScale(calendarScale);
return this;
}
public VCalendar withCalendarScale(String calendarScale)
{
setCalendarScale(calendarScale);
return this;
}
/**
* METHOD: RFC 5545 iCalendar 3.7.2. page 77
* This property defines the iCalendar object method
* associated with the calendar object
*
* Example:
* METHOD:REQUEST
* */
ObjectProperty<Method> methodProperty()
{
if (method == null)
{
method = new SimpleObjectProperty<Method>(this, CalendarProperty.METHOD.toString());
orderer().registerSortOrderProperty(method);
}
return method;
}
public Method getMethod() { return (method == null) ? null : methodProperty().get(); }
private ObjectProperty<Method> method;
public void setMethod(String method) { setMethod(Method.parse(method)); }
public void setMethod(Method method) { methodProperty().set(method); }
public void setMethod(MethodType method) { methodProperty().set(new Method(method)); }
public VCalendar withMethod(Method method)
{
setMethod(method);
return this;
}
public VCalendar withMethod(MethodType method)
{
setMethod(method);
return this;
}
public VCalendar withMethod(String method)
{
setMethod(method);
return this;
}
/**
* PRODID: RFC 5545 iCalendar 3.7.3. page 78
* This property specifies the identifier for the product that
* created the iCalendar object
*
* This project is named JFxtras iCalendar
*
* Example:
* PRODID:-//JFxtras//JFXtras iCalendar 1.0//EN
* */
ObjectProperty<ProductIdentifier> productIdentifierProperty()
{
if (productIdentifier == null)
{
productIdentifier = new SimpleObjectProperty<ProductIdentifier>(this, CalendarProperty.PRODUCT_IDENTIFIER.toString());
orderer().registerSortOrderProperty(productIdentifier);
}
return productIdentifier;
}
public ProductIdentifier getProductIdentifier() { return (productIdentifier == null) ? null : productIdentifierProperty().get(); }
private ObjectProperty<ProductIdentifier> productIdentifier;
public void setProductIdentifier(String productIdentifier) { setProductIdentifier(ProductIdentifier.parse(productIdentifier)); }
public void setProductIdentifier(ProductIdentifier productIdentifier) { productIdentifierProperty().set(productIdentifier); }
public VCalendar withProductIdentifier(ProductIdentifier productIdentifier)
{
setProductIdentifier(productIdentifier);
return this;
}
public VCalendar withProductIdentifier(String productIdentifier)
{
setProductIdentifier(productIdentifier);
return this;
}
/**
* VERSION: RFC 5545 iCalendar 3.7.4. page 79
* This property specifies the identifier corresponding to the
* highest version number or the minimum and maximum range of the
* iCalendar specification that is required in order to interpret the
* iCalendar object.
*
* This project complies with version 2.0
*
* Example:
* VERSION:2.0
* */
ObjectProperty<Version> versionProperty()
{
if (version == null)
{
version = new SimpleObjectProperty<Version>(this, CalendarProperty.VERSION.toString());
orderer().registerSortOrderProperty(version);
}
return version;
}
public Version getVersion() { return (version == null) ? null : versionProperty().get(); }
private ObjectProperty<Version> version;
public void setVersion(String version) { setVersion(Version.parse(version)); }
public void setVersion(Version version) { versionProperty().set(version); }
public VCalendar withVersion(Version version)
{
setVersion(version);
return this;
}
public VCalendar withVersion(String version)
{
setVersion(version);
return this;
}
public VCalendar withVersion()
{
setVersion(new Version());
return this;
}
/**
* Provides a framework for defining non-standard properties.
*/
public ObjectProperty<ObservableList<NonStandardProperty>> nonStandardProperty()
{
if (nonStandardProps == null)
{
nonStandardProps = new SimpleObjectProperty<>(this, PropertyType.NON_STANDARD.toString());
}
return nonStandardProps;
}
private ObjectProperty<ObservableList<NonStandardProperty>> nonStandardProps;
public ObservableList<NonStandardProperty> getNonStandard()
{
return (nonStandardProps == null) ? null : nonStandardProps.get();
}
public void setNonStandard(ObservableList<NonStandardProperty> nonStandardProps)
{
if (nonStandardProps != null)
{
orderer().registerSortOrderProperty(nonStandardProps);
} else
{
orderer().unregisterSortOrderProperty(nonStandardProperty().get());
}
nonStandardProperty().set(nonStandardProps);
}
/**
* Sets the value of the {@link #nonStandardProperty()} by parsing a vararg of
* iCalendar content text representing individual {@link NonStandardProperty} objects.
*
* @return - this class for chaining
*/
public VCalendar withNonStandard(String...nonStandardProps)
{
List<NonStandardProperty> a = Arrays.stream(nonStandardProps)
.map(c -> NonStandardProperty.parse(c))
.collect(Collectors.toList());
setNonStandard(FXCollections.observableArrayList(a));
return this;
}
/**
* Sets the value of the {@link #nonStandardProperty()}
*
* @return - this class for chaining
*/
public VCalendar withNonStandard(ObservableList<NonStandardProperty> nonStandardProps)
{
setNonStandard(nonStandardProps);
return this;
}
/**
* Sets the value of the {@link #nonStandardProperty()} from a vararg of {@link NonStandardProperty} objects.
*
* @return - this class for chaining
*/
public VCalendar withNonStandard(NonStandardProperty...nonStandardProps)
{
setNonStandard(FXCollections.observableArrayList(nonStandardProps));
return this;
}
// /**
// *<p>Allows other properties registered
// * with IANA to be specified in any calendar components.</p>
// */
// public ObjectProperty<ObservableList<IANAProperty>> ianaProperty()
// {
// if (ianaProps == null)
// {
// ianaProps = new SimpleObjectProperty<>(this, PropertyType.IANA_PROPERTY.toString());
// }
// return ianaProps;
// }
// public ObservableList<IANAProperty> getIana()
// {
// return (ianaProps == null) ? null : ianaProps.get();
// }
// private ObjectProperty<ObservableList<IANAProperty>> ianaProps;
// public void setIana(ObservableList<IANAProperty> ianaProps)
// {
// if (ianaProps != null)
// {
// orderer().registerSortOrderProperty(ianaProps);
// } else
// {
// orderer().unregisterSortOrderProperty(ianaProperty().get());
// }
// ianaProperty().set(ianaProps);
// }
// /**
// * Sets the value of the {@link #IANAProperty()} by parsing a vararg of
// * iCalendar content text representing individual {@link IANAProperty} objects.
// *
// * @return - this class for chaining
// */
// public VCalendar withIana(String...ianaProps)
// {
// List<IANAProperty> a = Arrays.stream(ianaProps)
// .map(c -> IANAProperty.parse(c))
// .collect(Collectors.toList());
// setIana(FXCollections.observableArrayList(a));
// return this;
// }
// /**
// * Sets the value of the {@link #IANAProperty()}
// *
// * @return - this class for chaining
// */
// public VCalendar withIana(ObservableList<IANAProperty> ianaProps)
// {
// setIana(ianaProps);
// return this;
// }
// /**
// * Sets the value of the {@link #IANAProperty()} from a vararg of {@link IANAProperty} objects.
// *
// * @return - this class for chaining
// */
// public VCalendar withIana(IANAProperty...ianaProps)
// {
// setIana(FXCollections.observableArrayList(ianaProps));
// return this;
// }
//
/*
* Calendar Components
*/
/**
* VEVENT: RFC 5545 iCalendar 3.6.1. page 52
*
* A grouping of component properties that describe an event.
*
*/
public ObservableList<VEvent> getVEvents() { return vEvents; }
private ObservableList<VEvent> vEvents = FXCollections.observableArrayList();
// private ObservableList<VEvent> vEvents = FXCollections.observableArrayList(
// (VEvent v) -> new Observable[] { v.summaryProperty(), v.descriptionProperty(), v.dateTimeStartProperty(), v.recurrenceRuleProperty() });
// TODO - ADD MORE PROPERTIES TO ABOVE EXTRACTOR
public void setVEvents(ObservableList<VEvent> vEvents) { this.vEvents = vEvents; }
public VCalendar withVEvent(ObservableList<VEvent> vEvents)
{
setVEvents(vEvents);
return this;
}
public VCalendar withVEvent(String...vEvents)
{
Arrays.stream(vEvents).forEach(c -> getVEvents().add(VEvent.parse(c)));
return this;
}
public VCalendar withVEvents(VEvent...vEvents)
{
getVEvents().addAll(vEvents);
return this;
}
/**
* VTODO: RFC 5545 iCalendar 3.6.2. page 55
*
* A grouping of component properties that describe a task that needs to be completed.
*
*/
public ObservableList<VTodo> getVTodos() { return vTodos; }
private ObservableList<VTodo> vTodos = FXCollections.observableArrayList();
public void setVTodos(ObservableList<VTodo> vTodos) { this.vTodos = vTodos; }
public VCalendar withVTodo(ObservableList<VTodo> vTodos)
{
setVTodos(vTodos);
return this;
}
public VCalendar withVTodo(String...vTodos)
{
Arrays.stream(vTodos).forEach(c -> getVTodos().add(VTodo.parse(c)));
return this;
}
public VCalendar withVTodos(VTodo...vTodos)
{
getVTodos().addAll(vTodos);
return this;
}
/**
* VJOURNAL: RFC 5545 iCalendar 3.6.3. page 57
*
* A grouping of component properties that describe a task that needs to be completed.
*
* @see VComponent
* @see VJournal
*/
public ObservableList<VJournal> getVJournals() { return vJournals; }
private ObservableList<VJournal> vJournals = FXCollections.observableArrayList();
public void setVJournals(ObservableList<VJournal> vJournals) { this.vJournals = vJournals; }
public VCalendar withVJournal(ObservableList<VJournal> vJournals) { setVJournals(vJournals); return this; }
public VCalendar withVJournal(String...vJournals)
{
Arrays.stream(vJournals).forEach(c -> getVJournals().add(VJournal.parse(c)));
return this;
}
public VCalendar withVJournals(VJournal...vJournals)
{
getVJournals().addAll(vJournals);
return this;
}
/**
* VFREEBUSY: RFC 5545 iCalendar 3.6.4. page 59
*
* @see VFreeBusy
*/
public ObservableList<VFreeBusy> getVFreeBusies() { return vFreeBusys; }
private ObservableList<VFreeBusy> vFreeBusys = FXCollections.observableArrayList();
public void setVFreeBusys(ObservableList<VFreeBusy> vFreeBusys) { this.vFreeBusys = vFreeBusys; }
public VCalendar withVFreeBusy(ObservableList<VFreeBusy> vFreeBusys) { setVFreeBusys(vFreeBusys); return this; }
public VCalendar withVFreeBusy(String...vFreeBusys)
{
Arrays.stream(vFreeBusys).forEach(c -> getVFreeBusies().add(VFreeBusy.parse(c)));
return this;
}
public VCalendar withVFreeBusys(VFreeBusy...vFreeBusys)
{
getVFreeBusies().addAll(vFreeBusys);
return this;
}
/**
* VTIMEZONE: RFC 5545 iCalendar 3.6.5. page 62
*
* @see VTimeZone
*/
public ObservableList<VTimeZone> getVTimeZones() { return vTimeZones; }
private ObservableList<VTimeZone> vTimeZones = FXCollections.observableArrayList();
public void setVTimeZones(ObservableList<VTimeZone> vTimeZones) { this.vTimeZones = vTimeZones; }
public VCalendar withVTimeZones(ObservableList<VTimeZone> vTimeZones) { setVTimeZones(vTimeZones); return this; }
public VCalendar withVTimeZones(String...vTimeZones)
{
Arrays.stream(vTimeZones).forEach(c -> getVTimeZones().add(VTimeZone.parse(c)));
return this;
}
public VCalendar withVTimeZones(VTimeZone...vTimeZones)
{
getVTimeZones().addAll(vTimeZones);
return this;
}
/**
* A convenience method that adds a VComponent to one of the ObservableLists based on
* its type such as VEVENT, VTODO, etc.
*
* @param newVComponent - VComponent to add
* @return true if add was successful, false otherwise
*/
public boolean addVComponent(VComponent newVComponent)
{
if (newVComponent instanceof VEvent)
{
getVEvents().add((VEvent) newVComponent);
} else if (newVComponent instanceof VTodo)
{
getVTodos().add((VTodo) newVComponent);
} else if (newVComponent instanceof VJournal)
{
getVJournals().add((VJournal) newVComponent);
} else if (newVComponent instanceof VFreeBusy)
{
getVFreeBusies().add((VFreeBusy) newVComponent);
} else if (newVComponent instanceof VTimeZone)
{
getVTimeZones().add((VTimeZone) newVComponent);
} else
{
throw new RuntimeException("Unsuppored VComponent type:" + newVComponent.getClass());
}
return true;
}
public boolean removeVComponent(VComponent vComponent)
{
if (vComponent instanceof VEvent)
{
return getVEvents().remove(vComponent);
} else if (vComponent instanceof VTodo)
{
return getVTodos().remove(vComponent);
} else if (vComponent instanceof VJournal)
{
return getVJournals().remove(vComponent);
} else if (vComponent instanceof VFreeBusy)
{
return getVFreeBusies().remove(vComponent);
} else if (vComponent instanceof VTimeZone)
{
return getVTimeZones().remove(vComponent);
} else
{
throw new RuntimeException("Unsuppored VComponent type:" + vComponent.getClass());
}
}
/** Create a VComponent by parsing component text and add it to the appropriate list
* @see #addVComponent(VComponent)*/
@Deprecated // not worth having
public void addVComponent(String contentText)
{
VComponent vComponent = SimpleVComponentFactory.emptyVComponent(contentText);
vComponent.parseContent(contentText);
addVComponent(vComponent);
}
/** Add a collection of {@link VComponent} to the correct ObservableList based on
* its type, such as VEVENT, VTODO, etc.
*
* @param newVComponents collection of {@link VComponent} to add
* @return true if add was successful, false otherwise
*/
public boolean addAllVComponents(Collection<? extends VComponent> newVComponents)
{
return newVComponents.stream().map(v -> addVComponent(v)).allMatch(b -> true);
}
/** Add a varargs of {@link VComponent} to the correct ObservableList based on
* its type, such as VEVENT, VTODO, etc.
*
* @param newVComponents collection of {@link VComponent} to add
* @return true if add was successful, false otherwise
*/
public boolean addAllVComponents(VComponent... newVComponents)
{
return addAllVComponents(Arrays.asList(newVComponents));
}
// public boolean removeVComponent(VComponent vComponent)
// {
// if (vComponent instanceof VEvent)
// {
// return getVEvents().remove(vComponent);
// } else if (vComponent instanceof VTodo)
// {
// return getVTodos().remove(vComponent);
// } else if (vComponent instanceof VJournal)
// {
// return getVJournals().remove(vComponent);
// } else if (vComponent instanceof VFreeBusy)
// {
// return getVFreeBusies().remove(vComponent);
// } else if (vComponent instanceof VTimeZone)
// {
// return getVTimeZones().remove(vComponent);
// } else
// {
// throw new RuntimeException("Unsuppored VComponent type:" + vComponent.getClass());
// }
// }
/** Convenience method that returns all {@link VComponent VComponents} regardless of type (e.g.
* {@link VEvent}, {@link VTodo}, etc.)
* @return unmodifiable list of all {@link VComponent VComponents}
*/
public List<VComponent> getAllVComponents()
{
List<VComponent> vComponents = new ArrayList<>();
vComponents.addAll(getVEvents());
vComponents.addAll(getVTodos());
vComponents.addAll(getVJournals());
vComponents.addAll(getVFreeBusies());
vComponents.addAll(getVTimeZones());
return Collections.unmodifiableList(vComponents);
}
/** Convenience method that returns all {@link VComponent VComponents} regardless of type (e.g.
* {@link VEvent}, {@link VTodo}, etc.)
*
* @return unmodifiable list of all {@link VComponent VComponents}
*/
public List<? extends VComponent> getVComponents(Class<? extends VComponent> vComponentClass)
{
if (vComponentClass.equals(VEvent.class))
{
return getVEvents();
} else if (vComponentClass.equals(VTodo.class))
{
return getVTodos();
} else if (vComponentClass.equals(VJournal.class))
{
return getVJournals();
} else if (vComponentClass.equals(VFreeBusy.class))
{
return getVFreeBusies();
} else if (vComponentClass.equals(VTimeZone.class))
{
return getVTimeZones();
} else
{
throw new RuntimeException("Unsuppored VComponent type:" + vComponentClass);
}
}
/**
* A convenience method that returns parent list of the {@link VComponent} parameter.
* Returns null if component is not in any {@link VComponent} list.
*
* @param vComponent - VComponent to look up
*/
public List<? extends VComponent> getVComponents(VComponent vComponent)
{
if (vComponent instanceof VEvent)
{
return (getVEvents().contains(vComponent)) ? getVEvents() : null;
} else if (vComponent instanceof VTodo)
{
return (getVTodos().contains(vComponent)) ? getVTodos() : null;
} else if (vComponent instanceof VJournal)
{
return (getVJournals().contains(vComponent)) ? getVJournals() : null;
} else if (vComponent instanceof VFreeBusy)
{
return (getVFreeBusies().contains(vComponent)) ? getVFreeBusies() : null;
} else if (vComponent instanceof VTimeZone)
{
return (getVTimeZones().contains(vComponent)) ? getVTimeZones() : null;
} else
{
throw new RuntimeException("Unsuppored VComponent type:" + vComponent.getClass());
}
}
/** set AbstractITIPFactory to handle processing input VCalendar based on {@link Method} */
public void setMethodProcessFactory(AbstractITIPFactory iTIPFactory)
{
this.iTIPFactory = iTIPFactory;
}
/** get AbstractITIPFactory to handle processing input VCalendar based on {@link Method} */
public AbstractITIPFactory getITIPFactory()
{
return iTIPFactory;
}
private AbstractITIPFactory iTIPFactory;
/**
* Process the exchange of iCalendar object according to the iTIP methods identifies in RFC 5546
* based on the methods in {@link #getITIPFactory()}
*
* @param iTIPMessages iTIP VCalendars to process with {@link Method} populated
* @return - log of process operation
*/
public List<String> processITIPMessage(VCalendar... iTIPMessages)
{
return processITIPMessage(Arrays.asList(iTIPMessages));
}
/**
* Process the exchange of iCalendar object according to the iTIP methods identifies in RFC 5546
* based on the methods in {@link #getITIPFactory()}
*
* @param iTIPMessages iTIP VCalendars to process with {@link Method} populated
* @return - log of process operation
*/
public List<String> processITIPMessage(Collection<VCalendar> iTIPMessages)
{
List<String> log = new ArrayList<>();
iTIPMessages.forEach(message ->
{
final Processable methodProcess;
if (message.getMethod() == null)
{ // default to PUBLISH method if not present
methodProcess = getITIPFactory().getITIPMessageProcess(MethodType.PUBLISH);
// throw new IllegalArgumentException("VCalendar to be processed MUST have the METHOD property populated");
} else
{
MethodType method = message.getMethod().getValue();
methodProcess = getITIPFactory().getITIPMessageProcess(method);
}
List<String> methodLog = methodProcess.process(this, message);
log.addAll(methodLog);
});
return log;
}
/**
* Process the exchange of iCalendar object according to the iTIP methods identifies in RFC 5546.
* Input string can contain multiple iTIP VCALENDAR messages.
*
* @param iTIPMessages iTIP VCalendar Message strings
* @return - log of process operation
*/
public List<String> processITIPMessage(String iTIPMessages)
{
List<String> log = new ArrayList<>();
List<String> iTIPMessageList = new ArrayList<>();
StringBuilder builder = new StringBuilder(1000);
for(String line : iTIPMessages.split(System.lineSeparator()))
{
if (line.equals("BEGIN:VCALENDAR") && builder.length() > 0)
{
iTIPMessageList.add(builder.toString());
builder = new StringBuilder(1000);
}
builder.append(line + System.lineSeparator());
}
// handle last VCalendar message
if (builder.length() > 0)
{
iTIPMessageList.add(builder.toString());
}
iTIPMessageList.forEach(message ->
{
List<String> methodLog = processITIPMessage(VCalendar.parse(message));
log.addAll(methodLog);
});
return log;
}
/**
* Parse component text to new VComponent with {@link RequestStatus REQUEST-STATUS} properties containing
* the result of the process, such as success message or error report.
*
* @param contentText iCalendar content lines
* @return the created VComponent with {@link RequestStatus REQUEST-STATUS} populated to indicate success or failuer.
*/
@Deprecated // replace with addVCalendar
public VComponent importVComponent(String contentText)
{
VPersonal<?> vComponent = (VPersonal<?>) SimpleVComponentFactory.emptyVComponent(contentText);
List<String> contentLines = Arrays.asList(contentText.split(System.lineSeparator()));
UnfoldingStringIterator unfoldedLines = new UnfoldingStringIterator(contentLines.iterator());
// Iterator<String> unfoldedLines = ICalendarUtilities.unfoldLines(contentLines).iterator();
boolean useRequestStatus = true;
vComponent.parseContent(unfoldedLines, useRequestStatus);
// requestStatusErrors.stream().forEach(System.out::println);
// TODO - only check conflict if opaque
String conflict = (vComponent instanceof VEvent) ? DateTimeUtilities.checkScheduleConflict((VEvent) vComponent, getVEvents()) : null;
if (conflict != null)
{
final ObservableList<RequestStatus> requestStatus;
if (vComponent.getRequestStatus() == null)
{
requestStatus = FXCollections.observableArrayList();
vComponent.setRequestStatus(requestStatus);
} else
{
requestStatus = vComponent.getRequestStatus();
}
// remove success REQUEST-STATUS message, if present
Iterator<RequestStatus> rsIterator = requestStatus.iterator();
while (rsIterator.hasNext())
{
RequestStatus rs = rsIterator.next();
if (rs.getValue().substring(0, 3).equals("2.0"))
{
rsIterator.remove();
}
}
requestStatus.add(RequestStatus.parse("4.1;Event conflict with " + conflict));
}
final boolean isVComponentValidToAdd;
if (vComponent.getRequestStatus() == null)
{
isVComponentValidToAdd = true;
} else
{
isVComponentValidToAdd = ! vComponent.getRequestStatus().stream()
.map(s -> s.getValue())
.anyMatch(s -> (s.charAt(0) == '3') || (s.charAt(0) == '4')); // error codes start with 3 or 4
}
if (isVComponentValidToAdd)
{
addVComponent(vComponent);
}
return vComponent;
}
// TODO - NEED TO GO IN DATE TIME UTILITIES
/*
* need list of recurrences and duration from vComponent
* make list of recurrences and duration for all other opaque VEvents
* check if ANY recurrences are in between any others
*/
// private final static int CONFLICT_CHECK_QUANTITY = 100;
// private boolean checkScheduleConflict(VEvent vComponent)
// {
// // must be opaque to cause conflict, opaque is default
// TimeTransparencyType newTransparency = (vComponent.getTimeTransparency() == null) ? TimeTransparencyType.OPAQUE : vComponent.getTimeTransparency().getValue();
// if (newTransparency == TimeTransparencyType.TRANSPARENT)
// {
// return false;
// }
//
// LocalDate dtstart = LocalDate.from(vComponent.getDateTimeStart().getValue());
// TemporalAmount duration = vComponent.getActualDuration();
//
// // Make list of Pairs containing start and end temporals
// List<Pair<Temporal,Temporal>> eventTimes = new ArrayList<>();
// for (VEvent v : getVEvents())
// {
// // can only conflict with opaque events, opaque is default
// TimeTransparencyType myTransparency = (v.getTimeTransparency() == null) ? TimeTransparencyType.OPAQUE : v.getTimeTransparency().getValue();
// if (myTransparency == TimeTransparencyType.OPAQUE)
// {
// Temporal myDTStart = v.getDateTimeStart().getValue().with(dtstart);
// TemporalAmount actualDuration = v.getActualDuration();
// v.streamRecurrences(myDTStart)
// .limit(CONFLICT_CHECK_QUANTITY)
// .forEach(t -> eventTimes.add(new Pair<>(t, t.plus(actualDuration))));
// }
// }
//
// /* Check for conflicts:
// * Start and End must NOT:
// * Be after an event start
// * Be before same event end
// * Is new start before the existing end
// * Is new start after the existing start
// */
// return vComponent.streamRecurrences()
// .limit(CONFLICT_CHECK_QUANTITY)
// .anyMatch(newStart ->
// {
// Temporal newEnd = newStart.plus(duration);
// return eventTimes.stream().anyMatch(p ->
// {
// Temporal existingStart = p.getKey();
// Temporal existingEnd = p.getValue();
// // test start
// boolean isAfter = DateTimeUtilities.isAfter(newStart, existingStart);
// if (isAfter)
// {
// return DateTimeUtilities.isBefore(newStart, existingEnd);
// }
// // test end
// boolean isAfter2 = DateTimeUtilities.isAfter(newEnd, existingStart);
// if (isAfter2)
// {
// return DateTimeUtilities.isBefore(newEnd, existingEnd);
// }
// return false;
// });
// });
// }
/**
* Import new VComponent with {@link RequestStatus REQUEST-STATUS} properties containing
* the result of the process, such as success message or error report.
*
* @param contentText iCalendar content lines
* @return list of error messages if import failed, null if successful
*/
@Deprecated
public List<String> importVComponent(VComponent newVComponent)
{
throw new RuntimeException("not implemented");
}
private Map<String, List<VDisplayable<?>>> uidComponentsMap = new HashMap<>(); // public for testing
/**
* Map of Related Components - UID is key and List of all related VComponents is value.
* Note: if you only want child components you need to filter the list to only include components
* that have a RECURRENCE-ID
*/
public Map<String, List<VDisplayable<?>>> uidComponentsMap() { return Collections.unmodifiableMap(uidComponentsMap); }
/**
* RecurrenceID listener
* notifies parents when a child component with recurrenceID is created or removed
* also maintains {@link #uidToComponentMap}
*/
private ListChangeListener<VDisplayable<?>> displayableListChangeListener = (ListChangeListener.Change<? extends VDisplayable<?>> change) ->
{
while (change.next())
{
// System.out.println("remove orphans here1:" + change.wasReplaced() + " " + change.wasAdded() + " " + change.wasRemoved());
if (change.wasReplaced())
{
// System.out.println("replaced:" + change);
} else if (change.wasAdded())
{
change.getAddedSubList().forEach(vComponent ->
{
// set recurrence children callback (VComponents having RecurrenceIDs and matching UID to a recurrence parent)
vComponent.setRecurrenceChildrenListCallBack( (c) ->
{
if (c.getUniqueIdentifier() == null) return null;
return uidComponentsMap.get(c.getUniqueIdentifier().getValue())
.stream()
.filter(v -> v.getRecurrenceId() != null) // keep only children objects
.collect(Collectors.toList());
});
// set recurrence parent callback (the VComponent with matching UID and no RECURRENCEID)
vComponent.setRecurrenceParentListCallBack( (c) ->
{
if (c.getUniqueIdentifier() == null) return null;
return uidComponentsMap.get(c.getUniqueIdentifier().getValue())
.stream()
.filter(v -> v.getRecurrenceId() == null) // parents don't have RECURRENCEID
.findAny()
.orElse(null);
});
// add VComponent to map
if (vComponent.getUniqueIdentifier() != null)
{
String uid = vComponent.getUniqueIdentifier().getValue();
final List<VDisplayable<?>> relatedComponents;
if (uidComponentsMap.get(uid) == null)
{
relatedComponents = new ArrayList<>();
uidComponentsMap.put(uid, relatedComponents);
} else
{
relatedComponents = uidComponentsMap.get(uid);
}
relatedComponents.add(vComponent);
}
// deleteOrphans(vComponent);
});
} else if (change.wasRemoved())
{
// System.out.println("remove orphans here2:");
change.getRemoved().forEach(vComponent ->
{
String uid = vComponent.getUniqueIdentifier().getValue();
List<VDisplayable<?>> relatedComponents = uidComponentsMap.get(uid);
if (relatedComponents != null)
{
relatedComponents.remove(vComponent);
// System.out.println("remove orphans here3:");
if (relatedComponents.isEmpty())
{
uidComponentsMap.remove(uid);
}
}
// TODO - CHECK FOR ORPHANED CHILDREN
// NEED TO MAKE SURE A NEWLY ADDED COMPONENT CAN CLAIM SOME CHILDREN
});
} else
{
System.out.println(change);
// CHECK FOR ORPHANED CHILDREN HERE
// int fromIndex = change.getFrom();
// int toIndex = change.getTo();
//// change.reset();
// change.getList().subList(fromIndex, toIndex).forEach(v -> System.out.println(v.getRecurrenceRule()));
// change.getList().forEach(v -> System.out.println(System.identityHashCode(v)));
// SOME OTHER LISTENERS MUST NOT HAVE FIRED YET CAUSING TOCONTENT TO BE WRONG
// Maybe I can check for orphans anyway.
// change.getRemoved().forEach(System.out::println);
// System.exit(0);
}
}
// change.getList().forEach(v -> System.out.println(v));
};
// // Assumes the component is the only one with the children attached to it.
// // If a replace operation happened, the old component must be removed, but not its
// /*
// * If modify
// */
// @Deprecated // causes UnsupportedOperationException when called from displayableListChangeListener
// // Move to VDisplayable
// public void deleteOrphans(VDisplayable<?> vDisplayable)
// {
// boolean isParent = vDisplayable.getRecurrenceId() == null;
//// List<VDisplayable<?>> orhpanedChildren = Collections.emptyList(); // initialize with empty list
// if (isParent)
// {
// final String uid = vDisplayable.getUniqueIdentifier().getValue();
// List<VDisplayable<?>> orhpanedChildren = uidComponentsMap().get(uid)
// .stream()
// .filter(v -> v.getRecurrenceId() != null)
// .filter(v ->
// {
// Temporal myRecurrenceID = v.getRecurrenceId().getValue();
// Temporal cacheStart = vDisplayable.recurrenceCache().getClosestStart(myRecurrenceID);
// Temporal nextRecurrenceDateTime = vDisplayable.getRecurrenceRule().getValue()