diff --git a/build.gradle b/build.gradle index af2d690..3e2b90c 100644 --- a/build.gradle +++ b/build.gradle @@ -42,7 +42,7 @@ dependencies { implementation 'org.webjars:jquery-ui:1.13.2' implementation 'org.webjars:fullcalendar:5.11.3' - implementation 'com.google.code.gson:gson:2.9.0'; + implementation 'com.google.code.gson:gson:2.10.1'; implementation 'org.webjars.bowergithub.datatables:datatables:1.10.21' diff --git a/src/main/java/de/jottyfan/timetrack/modules/calendar/CalendarService.java b/src/main/java/de/jottyfan/timetrack/modules/calendar/CalendarService.java index f5ea9e7..2e3fd33 100644 --- a/src/main/java/de/jottyfan/timetrack/modules/calendar/CalendarService.java +++ b/src/main/java/de/jottyfan/timetrack/modules/calendar/CalendarService.java @@ -1,7 +1,14 @@ package de.jottyfan.timetrack.modules.calendar; +import java.time.LocalDate; +import java.time.LocalDateTime; + +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; + /** * * @author jotty @@ -9,23 +16,19 @@ import org.springframework.stereotype.Service; */ @Service public class CalendarService { + + @Autowired + private DummyRepository dummyRepository; // TODO: implement a davical database reader as a repository // TODO: implement a caldav client as repository // TODO: implement a radicale file reader as repository - // TODO: create resource management database table for the calendar - + // TODO: create resource management database table for the calendar credentials + public String getJsonEvents() { - StringBuilder buf = new StringBuilder("["); - - // TODO: use List instead - - buf.append( - "{ id: 'example3', title: 'all day event', start: '2023-05-08', allDay: true, backgroundColor: 'darkcyan'},"); - buf.append(" { id: 'i1', title: 'overlapping', start: '2023-05-08T10:00', end: '2023-05-09T10:00'},"); - buf.append(" { id: 'i2', title: 'developing', start: '2023-05-09T08:00', end: '2023-05-09T10:15'}"); - buf.append("]"); - return buf.toString(); + Gson gson = new GsonBuilder().registerTypeAdapter(LocalDate.class, new LocalDateAdapter()) + .registerTypeAdapter(LocalDateTime.class, new LocalDateTimeAdapter()).create(); + return gson.toJson(dummyRepository.getExamples()); } } diff --git a/src/main/java/de/jottyfan/timetrack/modules/calendar/DummyRepository.java b/src/main/java/de/jottyfan/timetrack/modules/calendar/DummyRepository.java new file mode 100644 index 0000000..b043f57 --- /dev/null +++ b/src/main/java/de/jottyfan/timetrack/modules/calendar/DummyRepository.java @@ -0,0 +1,42 @@ +package de.jottyfan.timetrack.modules.calendar; + +import java.time.LocalDate; +import java.util.ArrayList; +import java.util.List; + +import org.springframework.stereotype.Repository; + +/** + * + * @author jotty + * + */ +@Repository +public class DummyRepository { + + /** + * get example dates + * + * @return the example dates + */ + public List getExamples() { + List list = new ArrayList<>(); + + EventBean event1 = EventBean.ofAllDayEvent("e1", "all day event", LocalDate.of(2023, 5, 8)); + event1.setBackgroundColor("darkcyan"); + + list.add(event1); + + EventBean event2 = EventBean.ofEvent("e2", "overlapping", LocalDate.of(2023, 5, 8).atTime(10, 0), + LocalDate.of(2023, 5, 9).atTime(10, 0)); + + list.add(event2); + + EventBean event3 = EventBean.ofEvent("e3", "developing", LocalDate.of(2023, 5, 10).atTime(8, 0), + LocalDate.of(2023, 5, 10).atTime(10, 0)); + + list.add(event3); + + return list; + } +} diff --git a/src/main/java/de/jottyfan/timetrack/modules/calendar/EventBean.java b/src/main/java/de/jottyfan/timetrack/modules/calendar/EventBean.java index 7038729..66c0ca5 100644 --- a/src/main/java/de/jottyfan/timetrack/modules/calendar/EventBean.java +++ b/src/main/java/de/jottyfan/timetrack/modules/calendar/EventBean.java @@ -1,6 +1,7 @@ package de.jottyfan.timetrack.modules.calendar; import java.io.Serializable; +import java.time.LocalDate; import java.time.LocalDateTime; import java.util.ArrayList; import java.util.List; @@ -42,24 +43,42 @@ public class EventBean implements Serializable { classNames = new ArrayList<>(); } - @Override - public String toString() { - StringBuilder buf = new StringBuilder("{"); - buf.append("id:'").append(id).append("',"); - buf.append("groupId:'").append(groupId).append("',"); - buf.append("allDay:'").append(allDay).append("',"); - // TODO: extend; also, only on demand (not null), or better use Gson for the json conversion -// .append(", start=").append(start).append(", end=").append(end).append(", startStr=").append(startStr) -// .append(", endStr=").append(endStr).append(", title=").append(title).append(", url=").append(url) -// .append(", classNames=").append(classNames).append(", editable=").append(editable).append(", startEditable=") -// .append(startEditable).append(", durationEditable=").append(durationEditable).append(", resourceEditable=") -// .append(resourceEditable).append(", display=").append(display).append(", overlap=").append(overlap) -// .append(", constraint=").append(constraint).append(", backgroundColor=").append(backgroundColor) -// .append(", boderColor=").append(boderColor).append(", textColor=").append(textColor).append(", extendedProps=") -// .append(extendedProps).append(", source=").append(source).append("]"); - return buf.toString(); + /** + * create new all day event + * + * @param id the ID of the event + * @param title the title of the event + * @param start the day of the event + * @return the event + */ + public static final EventBean ofAllDayEvent(String id, String title, LocalDate start) { + EventBean bean = new EventBean(); + bean.setId(id); + bean.setTitle(title); + bean.setStart(start == null ? null : start.atStartOfDay()); + bean.setAllDay(true); + return bean; } - + + /** + * create new event + * + * @param id the ID of the event + * @param title the title of the event + * @param start the datetime of the start + * @param end the datetime of the end + * @return the event + */ + public static final EventBean ofEvent(String id, String title, LocalDateTime start, LocalDateTime end) { + EventBean bean = new EventBean(); + bean.setId(id); + bean.setTitle(title); + bean.setStart(start); + bean.setEnd(end); + bean.setAllDay(false); + return bean; + } + /** * @return the id */ diff --git a/src/main/java/de/jottyfan/timetrack/modules/calendar/LocalDateAdapter.java b/src/main/java/de/jottyfan/timetrack/modules/calendar/LocalDateAdapter.java new file mode 100644 index 0000000..934e905 --- /dev/null +++ b/src/main/java/de/jottyfan/timetrack/modules/calendar/LocalDateAdapter.java @@ -0,0 +1,22 @@ +package de.jottyfan.timetrack.modules.calendar; + +import java.lang.reflect.Type; +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; + +import com.google.gson.JsonElement; +import com.google.gson.JsonPrimitive; +import com.google.gson.JsonSerializationContext; +import com.google.gson.JsonSerializer; + +/** + * + * @author jotty + * + */ +public class LocalDateAdapter implements JsonSerializer { + + public JsonElement serialize(LocalDate date, Type typeOfSrc, JsonSerializationContext context) { + return new JsonPrimitive(date.format(DateTimeFormatter.ISO_LOCAL_DATE)); + } +} \ No newline at end of file diff --git a/src/main/java/de/jottyfan/timetrack/modules/calendar/LocalDateTimeAdapter.java b/src/main/java/de/jottyfan/timetrack/modules/calendar/LocalDateTimeAdapter.java new file mode 100644 index 0000000..009a3b8 --- /dev/null +++ b/src/main/java/de/jottyfan/timetrack/modules/calendar/LocalDateTimeAdapter.java @@ -0,0 +1,22 @@ +package de.jottyfan.timetrack.modules.calendar; + +import java.lang.reflect.Type; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; + +import com.google.gson.JsonElement; +import com.google.gson.JsonPrimitive; +import com.google.gson.JsonSerializationContext; +import com.google.gson.JsonSerializer; + +/** + * + * @author jotty + * + */ +public class LocalDateTimeAdapter implements JsonSerializer { + + public JsonElement serialize(LocalDateTime date, Type typeOfSrc, JsonSerializationContext context) { + return new JsonPrimitive(date.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME)); + } +} \ No newline at end of file diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 2e4bd65..eb5e23e 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -10,7 +10,7 @@ server.port = 8083 server.servlet.context-path=/timetrack # keycloak -keycloak.auth-server-url = http://localhost:8080/ +keycloak.auth-server-url = https://www.jottyfan.de/auth keycloak.realm = jottyfan keycloak.resource = timetrack keycloak.public-client = true