Skip to content
Merged

1769 #1774

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,22 @@ public static CustomEvent perform(

List<String> newAttendees = inputParameters.getList(ATTENDEES, String.class, List.of());

event.getAttendees()
.addAll(
List<EventAttendee> attendees = event.getAttendees();

if (attendees == null) {
event.setAttendees(
newAttendees
.stream()
.map(attendee -> new EventAttendee().setEmail(attendee))
.toList());
} else {
event.getAttendees()
.addAll(
newAttendees
.stream()
.map(attendee -> new EventAttendee().setEmail(attendee))
.toList());
}

Event updatedEvent = calendar
.events()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import static com.bytechef.component.google.calendar.constant.GoogleCalendarConstants.CALENDAR_ID_PROPERTY;
import static com.bytechef.component.google.calendar.constant.GoogleCalendarConstants.DATE_RANGE;
import static com.bytechef.component.google.calendar.constant.GoogleCalendarConstants.FROM;
import static com.bytechef.component.google.calendar.constant.GoogleCalendarConstants.LOCAL_TIME_MIN;
import static com.bytechef.component.google.calendar.constant.GoogleCalendarConstants.TO;
import static com.bytechef.component.google.calendar.util.GoogleCalendarUtils.getCustomEvents;

Expand All @@ -32,7 +33,9 @@
import com.bytechef.component.definition.Parameters;
import com.bytechef.component.google.calendar.util.GoogleCalendarUtils.CustomEvent;
import java.io.IOException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.temporal.Temporal;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
Expand Down Expand Up @@ -79,7 +82,22 @@ public static List<Interval> perform(

List<CustomEvent> customEvents = new ArrayList<>(getCustomEvents(inputParameters, connectionParameters));

customEvents.sort(Comparator.comparing(CustomEvent::startTime));
customEvents.sort(Comparator.comparing(CustomEvent::startTime, (s1, s2) -> {
if (s1 instanceof LocalDateTime l1 && s2 instanceof LocalDateTime l2) {
return l1.compareTo(l2);
}
if (s1 instanceof LocalDateTime l1 && s2 instanceof LocalDate l2) {
return l1.compareTo(LocalDateTime.of(l2, LOCAL_TIME_MIN));
}
if (s1 instanceof LocalDate l1 && s2 instanceof LocalDateTime l2) {
return LocalDateTime.of(l1, LOCAL_TIME_MIN)
.compareTo(l2);
} else if (s1 instanceof LocalDate l1 && s2 instanceof LocalDate l2) {
return l1.compareTo(l2);
} else {
throw new IllegalArgumentException("Unsupported type");
}
}));

return getIntervals(customEvents, inputParameters.getMap(DATE_RANGE, LocalDateTime.class, Map.of()));
}
Expand All @@ -96,14 +114,26 @@ private static List<Interval> getIntervals(List<CustomEvent> customEvents, Map<S
LocalDateTime previousEndTime = from;

for (CustomEvent customEvent : customEvents) {
LocalDateTime startTime = customEvent.startTime();
LocalDateTime endTime = customEvent.endTime();
Temporal startTime = customEvent.startTime();
Temporal endTime = customEvent.endTime();

if (startTime.isAfter(previousEndTime)) {
intervals.add(new Interval(previousEndTime, startTime));
}
if (startTime instanceof LocalDateTime start && endTime instanceof LocalDateTime end) {
if (start.isAfter(previousEndTime)) {
intervals.add(new Interval(previousEndTime, start));
}

previousEndTime = previousEndTime.isAfter(endTime) ? previousEndTime : endTime;
previousEndTime = previousEndTime.isAfter(end) ? previousEndTime : end;
} else if (startTime instanceof LocalDate start && endTime instanceof LocalDate end) {

if (LocalDateTime.of(start, LOCAL_TIME_MIN)
.isAfter(previousEndTime)) {

intervals.add(new Interval(previousEndTime, LocalDateTime.of(start, LOCAL_TIME_MIN)));
}

previousEndTime = previousEndTime.isAfter(LocalDateTime.of(end, LOCAL_TIME_MIN))
? previousEndTime : LocalDateTime.of(end, LOCAL_TIME_MIN);
}
}

if (previousEndTime.isBefore(to)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.bytechef.component.definition.ComponentDsl.ModifiableStringProperty;
import com.bytechef.component.definition.OptionsDataSource.ActionOptionsFunction;
import com.bytechef.component.google.calendar.util.GoogleCalendarUtils;
import java.time.LocalTime;

/**
* @author Monika Kušter
Expand Down Expand Up @@ -56,6 +57,8 @@ private GoogleCalendarConstants() {
public static final String LOCATION = "location";
public static final String MAX_RESULTS = "maxResults";
public static final String METHOD = "method";
public static final LocalTime LOCAL_TIME_MAX = LocalTime.MAX;
public static final LocalTime LOCAL_TIME_MIN = LocalTime.MIN;
public static final String MINUTES = "minutes";
public static final String ORGANIZER = "organizer";
public static final String Q = "q";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,18 @@
import static com.bytechef.component.definition.ComponentDsl.option;
import static com.bytechef.component.google.calendar.constant.GoogleCalendarConstants.ALL_DAY;
import static com.bytechef.component.google.calendar.constant.GoogleCalendarConstants.CALENDAR_ID;
import static com.bytechef.component.google.calendar.constant.GoogleCalendarConstants.DATE_RANGE;
import static com.bytechef.component.google.calendar.constant.GoogleCalendarConstants.EVENT_TYPE;
import static com.bytechef.component.google.calendar.constant.GoogleCalendarConstants.FROM;
import static com.bytechef.component.google.calendar.constant.GoogleCalendarConstants.LOCAL_TIME_MAX;
import static com.bytechef.component.google.calendar.constant.GoogleCalendarConstants.LOCAL_TIME_MIN;
import static com.bytechef.component.google.calendar.constant.GoogleCalendarConstants.MAX_RESULTS;
import static com.bytechef.component.google.calendar.constant.GoogleCalendarConstants.Q;
import static com.bytechef.component.google.calendar.constant.GoogleCalendarConstants.TO;

import com.bytechef.component.definition.Context;
import com.bytechef.component.definition.Option;
import com.bytechef.component.definition.Parameters;
import com.bytechef.component.google.calendar.constant.GoogleCalendarConstants;
import com.bytechef.google.commons.GoogleServices;
import com.google.api.client.util.DateTime;
import com.google.api.services.calendar.Calendar;
Expand All @@ -41,8 +43,11 @@
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.temporal.Temporal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
Expand All @@ -56,14 +61,20 @@ public class GoogleCalendarUtils {
private GoogleCalendarUtils() {
}

public static LocalDateTime convertEventDateTimeToLocalDateTime(EventDateTime eventDateTime) {
public static Date convertToDateViaSqlTimestamp(LocalDateTime dateToConvert) {
return dateToConvert == null ? null : java.sql.Timestamp.valueOf(dateToConvert);
}

public static Temporal convertToTemporalFromEventDateTime(EventDateTime eventDateTime) {
DateTime dateTime = eventDateTime.getDateTime();

return LocalDateTime.ofInstant(Instant.parse(dateTime.toString()), ZoneId.systemDefault());
}
if (dateTime != null) {
return LocalDateTime.ofInstant(Instant.parse(dateTime.toString()), ZoneId.systemDefault());
}

public static Date convertToDateViaSqlTimestamp(LocalDateTime dateToConvert) {
return dateToConvert == null ? null : java.sql.Timestamp.valueOf(dateToConvert);
DateTime allDayDate = eventDateTime.getDate();

return LocalDate.parse(allDayDate.toString(), DateTimeFormatter.ISO_LOCAL_DATE);
}

public static EventDateTime createEventDateTime(Parameters inputParameters, String time) {
Expand All @@ -86,7 +97,7 @@ public static EventDateTime createEventDateTime(Parameters inputParameters, Stri
public static CustomEvent createCustomEvent(Event event) {
return new CustomEvent(
event.getICalUID(), event.getId(), event.getSummary(), event.getDescription(),
convertEventDateTimeToLocalDateTime(event.getStart()), convertEventDateTimeToLocalDateTime(event.getEnd()),
convertToTemporalFromEventDateTime(event.getStart()), convertToTemporalFromEventDateTime(event.getEnd()),
event.getEtag(), event.getEventType(), event.getHtmlLink(), event.getStatus(), event.getLocation(),
event.getHangoutLink(), event.getAttendees(), event.getAttachments(), event.getReminders());
}
Expand Down Expand Up @@ -125,43 +136,71 @@ public static List<CustomEvent> getCustomEvents(Parameters inputParameters, Para
.execute()
.getItems();

Map<String, LocalDateTime> timePeriod =
inputParameters.getMap(GoogleCalendarConstants.DATE_RANGE, LocalDateTime.class, Map.of());
Map<String, LocalDateTime> timePeriod = inputParameters.getMap(DATE_RANGE, LocalDateTime.class, Map.of());

LocalDateTime from = timePeriod.get(FROM);
LocalDateTime to = timePeriod.get(TO);

return convertToCustomEvents(filterEvents(from, to, items));
}

private static List<Event> filterEvents(LocalDateTime from, LocalDateTime to, List<Event> items) {
if (from == null && to == null) {
return convertToCustomEvents(items);
return items;
} else if (from != null && to == null) {
List<Event> result = items.stream()
.filter(event -> convertEventDateTimeToLocalDateTime(event.getEnd()).isAfter(from))
.toList();

return convertToCustomEvents(result);
return items.stream()
.filter(event -> isAfter(event.getEnd(), from))
.toList();

} else if (from == null) {
List<Event> result = items.stream()
.filter(event -> convertEventDateTimeToLocalDateTime(event.getStart()).isBefore(to))
.toList();

return convertToCustomEvents(result);
return items.stream()
.filter(event -> isBefore(event.getStart(), to))
.toList();
} else {
List<Event> result = new ArrayList<>();
return items.stream()
.filter(event -> isWithinRange(event, from, to))
.toList();
}
}

for (Event event : items) {
LocalDateTime startLocalDateTime = convertEventDateTimeToLocalDateTime(event.getStart());
LocalDateTime endLocalDateTime = convertEventDateTimeToLocalDateTime(event.getEnd());
private static boolean isAfter(EventDateTime eventDateTime, LocalDateTime from) {
Temporal temporal = convertToTemporalFromEventDateTime(eventDateTime);

if ((startLocalDateTime.isAfter(from) && startLocalDateTime.isBefore(to)) ||
(endLocalDateTime.isAfter(from) && endLocalDateTime.isBefore(to)) ||
(startLocalDateTime.isBefore(from) && endLocalDateTime.isAfter(to))) {
result.add(event);
}
}
return temporal instanceof LocalDateTime localDateTime ? localDateTime.isAfter(from)
: LocalDateTime.of(((LocalDate) temporal).minusDays(1), LOCAL_TIME_MAX)
.isAfter(from);
}

private static boolean isBefore(EventDateTime eventDateTime, LocalDateTime to) {
Temporal temporal = convertToTemporalFromEventDateTime(eventDateTime);

return convertToCustomEvents(result);
return temporal instanceof LocalDateTime localDateTime ? localDateTime.isBefore(to)
: LocalDateTime.of((LocalDate) temporal, LOCAL_TIME_MIN)
.isBefore(to);
}

private static boolean isWithinRange(Event event, LocalDateTime from, LocalDateTime to) {
Temporal start = convertToTemporalFromEventDateTime(event.getStart());
Temporal end = convertToTemporalFromEventDateTime(event.getEnd());

if (start instanceof LocalDateTime startLDT && end instanceof LocalDateTime endLDT) {

return (startLDT.isAfter(from) && startLDT.isBefore(to)) ||
(endLDT.isAfter(from) && endLDT.isBefore(to)) ||
(startLDT.isBefore(from) && endLDT.isAfter(to));

} else if (start instanceof LocalDate startLD && end instanceof LocalDate endLD) {
LocalDateTime startMin = LocalDateTime.of(startLD, LOCAL_TIME_MIN);
LocalDateTime endMax = LocalDateTime.of(endLD.minusDays(1), LOCAL_TIME_MAX);

return (startMin.isAfter(from) && startMin.isBefore(to)) ||
(endMax.isAfter(from) && endMax.isBefore(to)) ||
(startMin.isBefore(from) && endMax.isAfter(to));
}

return false;
}

private static List<CustomEvent> convertToCustomEvents(List<Event> eventList) {
Expand Down Expand Up @@ -196,7 +235,7 @@ public static List<Option<String>> getEventIdOptions(

@SuppressFBWarnings("EI")
public record CustomEvent(
String iCalUID, String id, String summary, String description, LocalDateTime startTime, LocalDateTime endTime,
String iCalUID, String id, String summary, String description, Temporal startTime, Temporal endTime,
String etag, String eventType, String htmlLink, String status, String location, String hangoutLink,
List<EventAttendee> attendeeList, List<EventAttachment> attachments, Event.Reminders reminders) {
}
Expand Down
Loading
Loading