diff --git a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/CalendarCreator.java b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/CalendarCreator.java index 293f83d..3c692d6 100644 --- a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/CalendarCreator.java +++ b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/CalendarCreator.java @@ -1,14 +1,38 @@ package de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.components; import java.io.InputStream; +import java.time.LocalDate; +import java.time.ZonedDateTime; +import java.util.ArrayList; import java.util.Calendar; import java.util.Collection; import java.util.Collections; import java.util.Date; import java.util.HashMap; +import java.util.Iterator; +import java.util.List; import java.util.Locale; import java.util.Map; +import org.hibernate.Session; +import org.hibernate.SessionFactory; +import org.hibernate.Transaction; +import org.hibernate.cfg.Configuration; + +import de.kreth.vaadin.clubhelper.vaadinclubhelper.business.CalendarTaskRefresher; +import de.kreth.vaadin.clubhelper.vaadinclubhelper.dao.ClubEventDaoImpl; +import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.Adress; +import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.Attendance; +import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.ClubEvent; +import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.Contact; +import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.DeletedEntry; +import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.GroupDef; +import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.Person; +import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.Persongroup; +import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.Relative; +import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.Startpaesse; +import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.StartpassStartrechte; +import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.Version; import net.sf.jasperreports.engine.JRDataSource; import net.sf.jasperreports.engine.JRException; import net.sf.jasperreports.engine.JasperCompileManager; @@ -19,8 +43,11 @@ import net.sf.jasperreports.view.JasperViewer; public abstract class CalendarCreator { + private static final String HOLIDAY_CALENDAR = "Schulferien"; + public static JasperPrint createCalendar(Date date) throws JRException { - return new MonthlyCalendarCreator(date).setValues(Collections.emptyMap()).setHolidays(Collections.emptyList()).createCalendar(); + return new MonthlyCalendarCreator(date).setValues(Collections.emptyMap()) + .setHolidays(Collections.emptyList()).createCalendar(); } public static JasperPrint createCalendar(Date date, Map values, @@ -28,48 +55,118 @@ public abstract class CalendarCreator { return new MonthlyCalendarCreator(date).setValues(values).setHolidays(holidays).createCalendar(); } - public static JasperPrint createCalendar(int year) throws JRException { - return new YearlyCalendarCreator(year).createCalendar(); + @SuppressWarnings("unchecked") + public static JasperPrint createCalendar(int year, Map values, + List holidays) throws JRException { + return new YearlyCalendarCreator(year, (Map) values, holidays).createCalendar(); } public final JasperPrint createCalendar() throws JRException { Map parameters = new HashMap<>(); fillParameterMap(parameters); - + JasperReport report = JasperCompileManager.compileReport(jrxmlResource()); - JasperPrint print = JasperFillManager.fillReport(report, parameters, - getSource()); + return JasperFillManager.fillReport(report, parameters, getSource()); - return print; } protected abstract JRDataSource getSource(); - + protected abstract void fillParameterMap(Map parameters); - + protected abstract InputStream jrxmlResource(); - + public static void main(String[] args) throws JRException { Locale.setDefault(Locale.GERMANY); -// Calendar cal = new GregorianCalendar(2018, Calendar.DECEMBER, 1); -// -// Map events = new HashMap<>(); -// events.put(3, "Event at 3rd."); -// events.put(23, "Event at 23th."); - -// JasperViewer v1 = new JasperViewer( -// createCalendar(cal.getTime(), events, Arrays.asList(2, 3, 4, 5, 6, 22, 23, 24))); -// v1.setVisible(true); - JasperViewer v1 = new JasperViewer( - createCalendar(2018)); + List allevents = loadAllEvents(false); + + List holidays = filterHolidays(allevents); + + Map values = map(allevents, 2019); + + JasperViewer v1 = new JasperViewer(createCalendar(2019, values, holidays)); v1.setVisible(true); -// cal = new GregorianCalendar(2018, Calendar.FEBRUARY, 1); -// JasperViewer v3 = new JasperViewer(createCalendar(cal.getTime())); -// v3.setVisible(true); + } + + private static List filterHolidays(List allevents) { + List holidays = new ArrayList<>(); + Iterator iter = allevents.iterator(); + while (iter.hasNext()) { + ClubEvent item = iter.next(); + if (item.getOrganizerDisplayName().equals(HOLIDAY_CALENDAR)) { + iter.remove(); + LocalDate start = item.getStart().toLocalDate(); + LocalDate end = item.getEnd().toLocalDate(); + while (end.isAfter(start) || end.isEqual(start)) { + holidays.add(start); + start = start.plusDays(1); + } + } + } + + return holidays; + } + + protected static Map map(List allevents, int year) { + Map values = new HashMap<>(); + allevents.forEach(ev -> { + + LocalDate start = ev.getStart().toLocalDate(); + ZonedDateTime end = ev.getEnd(); + if (end != null) { + LocalDate endDate = end.toLocalDate(); + if (endDate.getYear() >= year && start.getYear() <= year) { + while (endDate.isAfter(start) || endDate.isEqual(start)) { + concatDayValue(values, ev, start); + start = start.plusDays(1); + } + } + } else { + if (start.getYear() == year) { + concatDayValue(values, ev, start); + } + } + + }); + return values; + } + + public static void concatDayValue(Map values, ClubEvent ev, LocalDate start) { + StringBuilder txt; + if (values.get(start) != null) { + txt = (StringBuilder) values.get(start); + } else { + txt = new StringBuilder(); + values.put(start, txt); + } + if (txt.length() > 0) { + txt.append("\n"); + } + txt.append(ev.getCaption()); + } + + public static List loadAllEvents(boolean withRefresh) { + Configuration configuration = createConfig(); + SessionFactory sessionFactory = configuration.buildSessionFactory(); + Session session = sessionFactory.openSession(); + ClubEventDaoImpl dao = new ClubEventDaoImpl(); + dao.setEntityManager(session); + if (withRefresh) { + updateEventsFromGoogleCalendar(session, dao); + } + return dao.listAll(); + } + + public static void updateEventsFromGoogleCalendar(Session session, ClubEventDaoImpl dao) { + CalendarTaskRefresher refresh = new CalendarTaskRefresher(); + refresh.setDao(dao); + Transaction tx = session.beginTransaction(); + refresh.synchronizeCalendarTasks(); + tx.commit(); } static Calendar toCalendar(long time) { @@ -77,4 +174,29 @@ public abstract class CalendarCreator { cal.setTimeInMillis(time); return cal; } + + public static Configuration createConfig() { + Configuration configuration = new Configuration(); + configuration.addAnnotatedClass(Adress.class); + configuration.addAnnotatedClass(Attendance.class); + configuration.addAnnotatedClass(Contact.class); + configuration.addAnnotatedClass(DeletedEntry.class); + configuration.addAnnotatedClass(GroupDef.class); + configuration.addAnnotatedClass(Person.class); + configuration.addAnnotatedClass(Persongroup.class); + configuration.addAnnotatedClass(Relative.class); + configuration.addAnnotatedClass(Startpaesse.class); + configuration.addAnnotatedClass(StartpassStartrechte.class); + configuration.addAnnotatedClass(Version.class); + configuration.addInputStream(CalendarCreator.class.getResourceAsStream("/schema/ClubEvent.hbm.xml")); + + configuration.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect"); + configuration.setProperty("hibernate.connection.driver_class", "com.mysql.jdbc.Driver"); + configuration.setProperty("hibernate.connection.url", + "jdbc:mysql://localhost:3306/clubhelper?useUnicode=yes&characterEncoding=utf8&serverTimezone=Europe/Berlin"); + configuration.setProperty("hibernate.connection.username", "markus"); + configuration.setProperty("hibernate.connection.password", "0773"); + return configuration; + } + } diff --git a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/YearlyCalendarCreator.java b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/YearlyCalendarCreator.java index 037a4f1..54afd83 100644 --- a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/YearlyCalendarCreator.java +++ b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/YearlyCalendarCreator.java @@ -2,6 +2,10 @@ package de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.components; import java.io.InputStream; import java.time.LocalDate; +import java.util.Arrays; +import java.util.Collections; +import java.util.Iterator; +import java.util.List; import java.util.Map; import de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.components.calendar.Year; @@ -14,10 +18,17 @@ public class YearlyCalendarCreator extends CalendarCreator { private Year year; public YearlyCalendarCreator(int year, Map values) { + this(year, values, Collections.emptyList()); + } + + public YearlyCalendarCreator(int year, Map values, List holidays) { if (values == null) { throw new NullPointerException("Calendar values must not be null!"); } - this.year = new Year(year, values); + if (holidays == null) { + throw new NullPointerException("holidays values must not be null!"); + } + this.year = new Year(year, values, holidays); } @Override @@ -37,14 +48,16 @@ public class YearlyCalendarCreator extends CalendarCreator { public static class EmptySource implements JRDataSource { + Iterator values = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12).iterator(); + @Override public boolean next() throws JRException { - return false; + return values.hasNext(); } @Override public Object getFieldValue(JRField jrField) throws JRException { - return null; + return values.next(); } } diff --git a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/calendar/Year.java b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/calendar/Year.java index 83a3a6c..d24f308 100644 --- a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/calendar/Year.java +++ b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/calendar/Year.java @@ -6,8 +6,11 @@ import java.time.Month; import java.time.format.TextStyle; import java.util.Collections; import java.util.HashMap; +import java.util.HashSet; +import java.util.List; import java.util.Locale; import java.util.Map; +import java.util.Set; public class Year { @@ -15,29 +18,28 @@ public class Year { private final Locale locale; private final Map monthWeeks; private final Map values; + private final Set holidays; public Year(int year) { - this(year, Collections.emptyMap(), Locale.getDefault()); + this(year, Collections.emptyMap(), Collections.emptyList(), Locale.getDefault()); } - public Year(int year, Map values) { - this(year, values, Locale.getDefault()); + public Year(int year, Map values, List holidays) { + this(year, values, holidays, Locale.getDefault()); } - public Year(int year, Map values, Locale locale) { + public Year(int year, Map values, List holidays, Locale locale) { if (year < 1900 || year > 2100) { throw new IllegalArgumentException("Year value must be between 1900 and 2100"); } this.date = LocalDate.of(year, 1, 1); this.locale = locale; + this.holidays = new HashSet<>(holidays); this.monthWeeks = new HashMap<>(); for (Month m : Month.values()) { monthWeeks.put(m, new WeeksOfMonth(m, year)); } this.values = values; - for (LocalDate d : values.keySet()) { - System.out.println(d + "\t" + values.get(d)); - } } public int getYear() { @@ -48,6 +50,10 @@ public class Year { return month.getDisplayName(TextStyle.FULL_STANDALONE, locale); } + public CharSequence getMonth(int month) { + return Month.of(month).getDisplayName(TextStyle.FULL_STANDALONE, locale); + } + /** * Day of month, numeric for the specified by parameters. * @@ -72,9 +78,18 @@ public class Year { return weeksOfMonth.getWeek(week - 1).get(dayOfWeek); } + public boolean isHoliday(Month month, short week, DayOfWeek dayOfWeek) { + LocalDate day = toDate(month, week, dayOfWeek); + return holidays.contains(day); + } + public CharSequence getContent(Month month, short week, DayOfWeek dayOfWeek) { - Integer res = getDayOfMonth(month, week, dayOfWeek); - LocalDate day = date.withMonth(month.getValue()).withDayOfMonth(res); + LocalDate day = toDate(month, week, dayOfWeek); return values.get(day); } + + public LocalDate toDate(Month month, short week, DayOfWeek dayOfWeek) { + Integer res = getDayOfMonth(month, week, dayOfWeek); + return date.withMonth(month.getValue()).withDayOfMonth(res); + } } diff --git a/src/test/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/calendar/YearTest.java b/src/test/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/calendar/YearTest.java index 8aa7118..92a483c 100644 --- a/src/test/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/calendar/YearTest.java +++ b/src/test/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/calendar/YearTest.java @@ -13,21 +13,21 @@ class YearTest { @Test() void test18thCenturyEx() { - assertThrows(IllegalArgumentException.class, () -> new Year(1899, Collections.emptyMap())); - assertThrows(IllegalArgumentException.class, () -> new Year(2101, Collections.emptyMap())); - new Year(1900, Collections.emptyMap()); - new Year(2100, Collections.emptyMap()); + assertThrows(IllegalArgumentException.class, () -> new Year(1899)); + assertThrows(IllegalArgumentException.class, () -> new Year(2101)); + new Year(1900); + new Year(2100); } @Test void testMondayIsFirst() { - Year y = new Year(2018, Collections.emptyMap()); + Year y = new Year(2018, Collections.emptyMap(), Collections.emptyList()); assertEquals("1", y.getDay(Month.OCTOBER, (short) 1, DayOfWeek.MONDAY)); } @Test void testThursdayIsFirst() { - Year y = new Year(2018, Collections.emptyMap()); + Year y = new Year(2018); assertEquals("", y.getDay(Month.NOVEMBER, (short) 1, DayOfWeek.MONDAY)); }