From 163d8d3811142e7f2eb518ae66a1a6ec48b5479d Mon Sep 17 00:00:00 2001 From: Zihlu Wang Date: Tue, 30 Apr 2024 11:10:50 +0800 Subject: [PATCH 01/17] refactor: renamed DateAndTimeFormatter to Formatters BREAKING CHANGE: com.onixbyte.webcal.config.DateAndTimeFormatter is being moved to com.onixbyte.webcal.config.Formatters --- .../config/{DateAndTimeFormatter.java => Formatters.java} | 8 +++++++- .../java/com/onixbyte/webcal/impl/WebCalendarEvent.java | 6 +++--- 2 files changed, 10 insertions(+), 4 deletions(-) rename webcal/src/main/java/com/onixbyte/webcal/config/{DateAndTimeFormatter.java => Formatters.java} (90%) diff --git a/webcal/src/main/java/com/onixbyte/webcal/config/DateAndTimeFormatter.java b/webcal/src/main/java/com/onixbyte/webcal/config/Formatters.java similarity index 90% rename from webcal/src/main/java/com/onixbyte/webcal/config/DateAndTimeFormatter.java rename to webcal/src/main/java/com/onixbyte/webcal/config/Formatters.java index 57b0eb8..89c1d31 100644 --- a/webcal/src/main/java/com/onixbyte/webcal/config/DateAndTimeFormatter.java +++ b/webcal/src/main/java/com/onixbyte/webcal/config/Formatters.java @@ -26,7 +26,7 @@ * * @author Zihlu Wang */ -public final class DateAndTimeFormatter { +public final class Formatters { /** * Get the {@link java.time.format.DateTimeFormatter datetime formatter} @@ -45,4 +45,10 @@ public static DateTimeFormatter getUtcDatetimeFormatter() { private static DateTimeFormatter utcDateTimeFormatter; + /** + * Private constructor to prevent this class being initialised. + */ + private Formatters() { + } + } diff --git a/webcal/src/main/java/com/onixbyte/webcal/impl/WebCalendarEvent.java b/webcal/src/main/java/com/onixbyte/webcal/impl/WebCalendarEvent.java index 07b64f1..de26f5e 100644 --- a/webcal/src/main/java/com/onixbyte/webcal/impl/WebCalendarEvent.java +++ b/webcal/src/main/java/com/onixbyte/webcal/impl/WebCalendarEvent.java @@ -19,7 +19,7 @@ import com.onixbyte.webcal.WebCalendarNode; import com.onixbyte.webcal.config.Classification; -import com.onixbyte.webcal.config.DateAndTimeFormatter; +import com.onixbyte.webcal.config.Formatters; import java.text.MessageFormat; import java.time.Duration; @@ -269,8 +269,8 @@ public String resolve() { END:{0}""", /* 0 - tag */TAG, /* 1 - uid */ Optional.ofNullable(uid).orElse(UUID.randomUUID().toString()) + "@" + domainName, - /* 2 - dtstamp */ now.format(DateAndTimeFormatter.getUtcDatetimeFormatter()), - /* 3 - start time */ start.atZone(ZoneId.systemDefault()).format(DateAndTimeFormatter.getUtcDatetimeFormatter()), + /* 2 - dtstamp */ now.format(Formatters.getUtcDatetimeFormatter()), + /* 3 - start time */ start.atZone(ZoneId.systemDefault()).format(Formatters.getUtcDatetimeFormatter()), /* 4 - summary */ Optional.ofNullable(summary).map((item) -> "\nSUMMARY:" + item).orElse(""), /* 5 - categories */ Optional.ofNullable(categories) .map((item) -> !item.isEmpty() ? "\nCATEGORIES:" + resolveCategories() : null).orElse(""), From 77cc93405ae7713db4b57b8212280285cad780e2 Mon Sep 17 00:00:00 2001 From: Zihlu Wang Date: Wed, 8 May 2024 10:47:33 +0800 Subject: [PATCH 02/17] refactor: refactor the iCalendar tools BREAKING CHANGES: The java classes just created, but not implemented yet, DO NOT USE THIS COMMIT TO BUILD! [skip ci] --- .../com/onixbyte/icalendar/CalendarUtil.java | 15 + .../icalendar/component/Calendar.java | 113 +++++++ .../component/CalendarComponent.java | 35 +++ .../onixbyte/icalendar/component/Event.java | 23 ++ .../config/Formatters.java | 2 +- .../config/package-info.java | 4 +- .../icalendar/constant/CalendarUserType.java | 16 + .../icalendar/constant/Classification.java | 36 +++ .../{webcal => icalendar}/package-info.java | 8 +- .../com/onixbyte/icalendar/property/Prop.java | 7 + .../property/calendar/CalendarScale.java | 22 ++ .../icalendar/property/calendar/Method.java | 35 +++ .../property/calendar/ProductIdentifier.java | 54 ++++ .../icalendar/property/calendar/Version.java | 21 ++ .../icalendar/property/component/Attach.java | 23 ++ .../property/component/DateTimeCreated.java | 9 + .../property/component/DateTimeStamp.java | 70 +++++ .../property/component/LastModified.java | 9 + .../property/component/UniqueIdentifier.java | 51 +++ .../parameter/AlarmTriggerRelationship.java | 9 + .../parameter/AlternateRepresentation.java | 48 +++ .../property/parameter/CalendarUserType.java | 44 +++ .../property/parameter/CommonName.java | 35 +++ .../property/parameter/Delegatee.java | 9 + .../property/parameter/Delegator.java | 47 +++ .../parameter/DirectoryEntryReference.java | 9 + .../property/parameter/FormatType.java | 26 ++ .../property/parameter/FreeBusyTimeType.java | 9 + .../parameter/GroupOrListMembership.java | 9 + .../property/parameter/InlineEncoding.java | 9 + .../property/parameter/Language.java | 9 + .../property/parameter/ParticipationRole.java | 9 + .../parameter/ParticipationStatus.java | 9 + .../parameter/RecurrenceIdentifierRange.java | 9 + .../property/parameter/RelationshipType.java | 9 + .../property/parameter/RsvpExpectation.java | 9 + .../icalendar/property/parameter/SentBy.java | 9 + .../parameter/TimeZoneIdentifier.java | 9 + .../property/parameter/ValueDataType.java | 9 + .../java/com/onixbyte/webcal/WebCalendar.java | 215 ------------- .../com/onixbyte/webcal/WebCalendarNode.java | 124 -------- .../webcal/config/Classification.java | 74 ----- .../webcal/impl/WebCalendarEvent.java | 293 ------------------ .../onixbyte/icalendar/test/TestCalendar.java | 40 +++ .../onixbyte/webcal/test/TestWebCalendar.java | 56 ---- 45 files changed, 921 insertions(+), 769 deletions(-) create mode 100644 webcal/src/main/java/com/onixbyte/icalendar/CalendarUtil.java create mode 100644 webcal/src/main/java/com/onixbyte/icalendar/component/Calendar.java create mode 100644 webcal/src/main/java/com/onixbyte/icalendar/component/CalendarComponent.java create mode 100644 webcal/src/main/java/com/onixbyte/icalendar/component/Event.java rename webcal/src/main/java/com/onixbyte/{webcal => icalendar}/config/Formatters.java (97%) rename webcal/src/main/java/com/onixbyte/{webcal => icalendar}/config/package-info.java (90%) create mode 100644 webcal/src/main/java/com/onixbyte/icalendar/constant/CalendarUserType.java create mode 100644 webcal/src/main/java/com/onixbyte/icalendar/constant/Classification.java rename webcal/src/main/java/com/onixbyte/{webcal => icalendar}/package-info.java (84%) create mode 100644 webcal/src/main/java/com/onixbyte/icalendar/property/Prop.java create mode 100644 webcal/src/main/java/com/onixbyte/icalendar/property/calendar/CalendarScale.java create mode 100644 webcal/src/main/java/com/onixbyte/icalendar/property/calendar/Method.java create mode 100644 webcal/src/main/java/com/onixbyte/icalendar/property/calendar/ProductIdentifier.java create mode 100644 webcal/src/main/java/com/onixbyte/icalendar/property/calendar/Version.java create mode 100644 webcal/src/main/java/com/onixbyte/icalendar/property/component/Attach.java create mode 100644 webcal/src/main/java/com/onixbyte/icalendar/property/component/DateTimeCreated.java create mode 100644 webcal/src/main/java/com/onixbyte/icalendar/property/component/DateTimeStamp.java create mode 100644 webcal/src/main/java/com/onixbyte/icalendar/property/component/LastModified.java create mode 100644 webcal/src/main/java/com/onixbyte/icalendar/property/component/UniqueIdentifier.java create mode 100644 webcal/src/main/java/com/onixbyte/icalendar/property/parameter/AlarmTriggerRelationship.java create mode 100644 webcal/src/main/java/com/onixbyte/icalendar/property/parameter/AlternateRepresentation.java create mode 100644 webcal/src/main/java/com/onixbyte/icalendar/property/parameter/CalendarUserType.java create mode 100644 webcal/src/main/java/com/onixbyte/icalendar/property/parameter/CommonName.java create mode 100644 webcal/src/main/java/com/onixbyte/icalendar/property/parameter/Delegatee.java create mode 100644 webcal/src/main/java/com/onixbyte/icalendar/property/parameter/Delegator.java create mode 100644 webcal/src/main/java/com/onixbyte/icalendar/property/parameter/DirectoryEntryReference.java create mode 100644 webcal/src/main/java/com/onixbyte/icalendar/property/parameter/FormatType.java create mode 100644 webcal/src/main/java/com/onixbyte/icalendar/property/parameter/FreeBusyTimeType.java create mode 100644 webcal/src/main/java/com/onixbyte/icalendar/property/parameter/GroupOrListMembership.java create mode 100644 webcal/src/main/java/com/onixbyte/icalendar/property/parameter/InlineEncoding.java create mode 100644 webcal/src/main/java/com/onixbyte/icalendar/property/parameter/Language.java create mode 100644 webcal/src/main/java/com/onixbyte/icalendar/property/parameter/ParticipationRole.java create mode 100644 webcal/src/main/java/com/onixbyte/icalendar/property/parameter/ParticipationStatus.java create mode 100644 webcal/src/main/java/com/onixbyte/icalendar/property/parameter/RecurrenceIdentifierRange.java create mode 100644 webcal/src/main/java/com/onixbyte/icalendar/property/parameter/RelationshipType.java create mode 100644 webcal/src/main/java/com/onixbyte/icalendar/property/parameter/RsvpExpectation.java create mode 100644 webcal/src/main/java/com/onixbyte/icalendar/property/parameter/SentBy.java create mode 100644 webcal/src/main/java/com/onixbyte/icalendar/property/parameter/TimeZoneIdentifier.java create mode 100644 webcal/src/main/java/com/onixbyte/icalendar/property/parameter/ValueDataType.java delete mode 100644 webcal/src/main/java/com/onixbyte/webcal/WebCalendar.java delete mode 100644 webcal/src/main/java/com/onixbyte/webcal/WebCalendarNode.java delete mode 100644 webcal/src/main/java/com/onixbyte/webcal/config/Classification.java delete mode 100644 webcal/src/main/java/com/onixbyte/webcal/impl/WebCalendarEvent.java create mode 100644 webcal/src/test/java/com/onixbyte/icalendar/test/TestCalendar.java delete mode 100644 webcal/src/test/java/com/onixbyte/webcal/test/TestWebCalendar.java diff --git a/webcal/src/main/java/com/onixbyte/icalendar/CalendarUtil.java b/webcal/src/main/java/com/onixbyte/icalendar/CalendarUtil.java new file mode 100644 index 0000000..a94d8c2 --- /dev/null +++ b/webcal/src/main/java/com/onixbyte/icalendar/CalendarUtil.java @@ -0,0 +1,15 @@ +package com.onixbyte.icalendar; + +/** + * CalendarUtil + * + * @author Zihlu WANG + */ +public final class CalendarUtil { + + private CalendarUtil() { + } + + + +} diff --git a/webcal/src/main/java/com/onixbyte/icalendar/component/Calendar.java b/webcal/src/main/java/com/onixbyte/icalendar/component/Calendar.java new file mode 100644 index 0000000..40e6eed --- /dev/null +++ b/webcal/src/main/java/com/onixbyte/icalendar/component/Calendar.java @@ -0,0 +1,113 @@ +/* + * Copyright (C) 2023-2024 OnixByte. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.onixbyte.icalendar.component; + +import com.onixbyte.icalendar.property.calendar.CalendarScale; +import com.onixbyte.icalendar.property.calendar.Method; +import com.onixbyte.icalendar.property.calendar.ProductIdentifier; +import com.onixbyte.icalendar.property.calendar.Version; + +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; + +/** + * {@code WebCalendar} class represents a web calendar in iCalendar format. + *

+ * It allows users to create and customise calendar components and events and + * generate an iCalendar string containing all the calendar information. + *

+ * Usage Example: + *

+ * WebCalendar calendar = new WebCalendar()
+ *         .setName("My Web Calendar")
+ *         .setCompanyName("CodeCrafters Inc.")
+ *         .setProductName("WebCal")
+ *         .setDomainName("codecrafters.org.cn")
+ *         .setMethod("PUBLISH")
+ *         .addEvent(event1)
+ *         .addEvent(event2);
+ * String iCalendarString = calendar.resolve();
+ * 
+ *

+ * The {@code WebCalendar} class is designed to generate an iCalendar string + * conforming to the iCalendar specification, which can be used to share + * calendar data with other calendar applications or services. + * + * @author Zihlu Wang + * @version 1.1.0 + * @since 1.0.0 + */ +public final class Calendar { + + private static final String COMPONENT_NAME = "VCALENDAR"; + + /** + * This property are OPTIONAL, but MUST NOT occur more than once. + */ + private CalendarScale scale; + + /** + * This property are OPTIONAL, but MUST NOT occur more than once. + */ + private Method method; + + /** + * This property are REQUIRED, but MUST NOT occur more than once. + */ + private ProductIdentifier productIdentifier; + + /** + * This property are REQUIRED, but MUST NOT occur more than once. + */ + private final Version version = Version.VERSION_2_0; + + private String calendarName; + + private final List components = new ArrayList<>(); + + /** + * Generate and resolve the iCalendar string for the web calendar. + * + * @return the resolved iCalendar string + */ + public String resolve() { + var calendarBuilder = new StringBuilder(); + calendarBuilder.append("BEGIN:").append(COMPONENT_NAME).append('\n'); + + calendarBuilder.append(version.resolve()).append('\n'); + calendarBuilder.append(productIdentifier.resolve()).append('\n'); + calendarBuilder.append("X-WR-CALNAME:").append(calendarName).append('\n'); + + Optional.ofNullable(scale) + .ifPresent((_scale) -> calendarBuilder.append(_scale.resolve()).append('\n')); + Optional.ofNullable(method) + .ifPresent((_method) -> calendarBuilder.append(_method.resolve()).append('\n')); + + if (!components.isEmpty()) { + for (var component : components) { + calendarBuilder.append(component.resolve()).append('\n'); + } + } + + calendarBuilder.append("END:").append(COMPONENT_NAME).append('\n'); + return calendarBuilder.toString(); + } + +} + diff --git a/webcal/src/main/java/com/onixbyte/icalendar/component/CalendarComponent.java b/webcal/src/main/java/com/onixbyte/icalendar/component/CalendarComponent.java new file mode 100644 index 0000000..f3c8610 --- /dev/null +++ b/webcal/src/main/java/com/onixbyte/icalendar/component/CalendarComponent.java @@ -0,0 +1,35 @@ +/* + * Copyright (C) 2023 CodeCraftersCN. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.onixbyte.icalendar.component; + +/** + * The abstract sealed class {@code WebCalendarNode} represents a node in a web calendar, such as an event, a to-do + * item, or an alarm. It provides common properties and methods for all calendar components and events. + *

+ * Subclasses of {@code WebCalendarNode} should implement the {@link #resolve()} method to generate the corresponding iCalendar content for the specific calendar component or event. + * + * @author Zihlu Wang + * @version 1.1.0 + * @since 1.0.0 + */ +public abstract class CalendarComponent { + + public abstract String resolve(); + +} + diff --git a/webcal/src/main/java/com/onixbyte/icalendar/component/Event.java b/webcal/src/main/java/com/onixbyte/icalendar/component/Event.java new file mode 100644 index 0000000..3ef6848 --- /dev/null +++ b/webcal/src/main/java/com/onixbyte/icalendar/component/Event.java @@ -0,0 +1,23 @@ +package com.onixbyte.icalendar.component; + +import com.onixbyte.icalendar.property.component.DateTimeStamp; +import com.onixbyte.icalendar.property.component.UniqueIdentifier; + +/** + * Event + * + * @author Zihlu WANG + */ +public class Event extends CalendarComponent { + + private DateTimeStamp dtStamp; + + private UniqueIdentifier uid; + + + + @Override + public String resolve() { + return ""; + } +} diff --git a/webcal/src/main/java/com/onixbyte/webcal/config/Formatters.java b/webcal/src/main/java/com/onixbyte/icalendar/config/Formatters.java similarity index 97% rename from webcal/src/main/java/com/onixbyte/webcal/config/Formatters.java rename to webcal/src/main/java/com/onixbyte/icalendar/config/Formatters.java index 89c1d31..4668723 100644 --- a/webcal/src/main/java/com/onixbyte/webcal/config/Formatters.java +++ b/webcal/src/main/java/com/onixbyte/icalendar/config/Formatters.java @@ -15,7 +15,7 @@ * limitations under the License. */ -package com.onixbyte.webcal.config; +package com.onixbyte.icalendar.config; import java.time.ZoneOffset; import java.time.format.DateTimeFormatter; diff --git a/webcal/src/main/java/com/onixbyte/webcal/config/package-info.java b/webcal/src/main/java/com/onixbyte/icalendar/config/package-info.java similarity index 90% rename from webcal/src/main/java/com/onixbyte/webcal/config/package-info.java rename to webcal/src/main/java/com/onixbyte/icalendar/config/package-info.java index de753b5..f02aab7 100644 --- a/webcal/src/main/java/com/onixbyte/webcal/config/package-info.java +++ b/webcal/src/main/java/com/onixbyte/icalendar/config/package-info.java @@ -23,11 +23,11 @@ *

The classes in this package include:

* * * @since 1.0.0 */ -package com.onixbyte.webcal.config; \ No newline at end of file +package com.onixbyte.icalendar.config; \ No newline at end of file diff --git a/webcal/src/main/java/com/onixbyte/icalendar/constant/CalendarUserType.java b/webcal/src/main/java/com/onixbyte/icalendar/constant/CalendarUserType.java new file mode 100644 index 0000000..5b9b554 --- /dev/null +++ b/webcal/src/main/java/com/onixbyte/icalendar/constant/CalendarUserType.java @@ -0,0 +1,16 @@ +package com.onixbyte.icalendar.constant; + +public enum CalendarUserType { + + INDIVIDUAL, + GROUP, + RESOURCE, + ROOM, + UNKNOWN, + ; + + @Override + public String toString() { + return name(); + } +} diff --git a/webcal/src/main/java/com/onixbyte/icalendar/constant/Classification.java b/webcal/src/main/java/com/onixbyte/icalendar/constant/Classification.java new file mode 100644 index 0000000..3828e94 --- /dev/null +++ b/webcal/src/main/java/com/onixbyte/icalendar/constant/Classification.java @@ -0,0 +1,36 @@ +package com.onixbyte.icalendar.constant; + +/** + * This property defines the access classification for a calendar component. + *

+ * The property can be specified once in a {@link CalendarEvent CalEvent}, {@link + * com.onixbyte.icalendar.impl.CalTodo CalTodo}, or {@link com.onixbyte.icalendar.impl.CalJournal CalJournal} + * calendar properties. + */ +public enum Classification { + + /** + * Public events mean that anyone can view and access their detailed information. These events are typically used + * in public calendars such as holiday calendars or company-wide event calendars. For public events, anyone with + * access to the calendar can see all the event details. + */ + PUBLIC, + /** + * Private events mean that only invited or specifically authorized individuals can view and access their detailed + * information. Private events are visible to the owner of the calendar but not to others. This classification is + * commonly used for personal appointments, private meetings, etc. + */ + PRIVATE, + /** + * Confidential events have detailed information that is not visible to anyone, including the owner of the calendar. + * Only individuals who have been granted specific permissions can access the detailed information of confidential + * events. This classification is typically used for sensitive business meetings, personal privacy matters, etc. + */ + CONFIDENTIAL, + ; + + @Override + public String toString() { + return name(); + } +} diff --git a/webcal/src/main/java/com/onixbyte/webcal/package-info.java b/webcal/src/main/java/com/onixbyte/icalendar/package-info.java similarity index 84% rename from webcal/src/main/java/com/onixbyte/webcal/package-info.java rename to webcal/src/main/java/com/onixbyte/icalendar/package-info.java index c15787d..3f87d13 100644 --- a/webcal/src/main/java/com/onixbyte/webcal/package-info.java +++ b/webcal/src/main/java/com/onixbyte/icalendar/package-info.java @@ -23,16 +23,16 @@ * The main classes and modules in this package include: *