prepared holiday functions

master
Markus Kreth 7 years ago
parent 9b803d581c
commit a41abd6143
  1. 162
      src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/CalendarCreator.java
  2. 19
      src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/YearlyCalendarCreator.java
  3. 33
      src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/calendar/Year.java
  4. 12
      src/test/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/calendar/YearTest.java

@ -1,14 +1,38 @@
package de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.components; package de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.components;
import java.io.InputStream; import java.io.InputStream;
import java.time.LocalDate;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.Calendar; import java.util.Calendar;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.Date; import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Map; 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.JRDataSource;
import net.sf.jasperreports.engine.JRException; import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperCompileManager; import net.sf.jasperreports.engine.JasperCompileManager;
@ -19,8 +43,11 @@ import net.sf.jasperreports.view.JasperViewer;
public abstract class CalendarCreator { public abstract class CalendarCreator {
private static final String HOLIDAY_CALENDAR = "Schulferien";
public static JasperPrint createCalendar(Date date) throws JRException { public static JasperPrint createCalendar(Date date) throws JRException {
return new MonthlyCalendarCreator<CharSequence>(date).setValues(Collections.emptyMap()).setHolidays(Collections.emptyList()).createCalendar(); return new MonthlyCalendarCreator<CharSequence>(date).setValues(Collections.emptyMap())
.setHolidays(Collections.emptyList()).createCalendar();
} }
public static <T extends CharSequence> JasperPrint createCalendar(Date date, Map<Integer, T> values, public static <T extends CharSequence> JasperPrint createCalendar(Date date, Map<Integer, T> values,
@ -28,8 +55,10 @@ public abstract class CalendarCreator {
return new MonthlyCalendarCreator<T>(date).setValues(values).setHolidays(holidays).createCalendar(); return new MonthlyCalendarCreator<T>(date).setValues(values).setHolidays(holidays).createCalendar();
} }
public static JasperPrint createCalendar(int year) throws JRException { @SuppressWarnings("unchecked")
return new YearlyCalendarCreator(year).createCalendar(); public static <T extends CharSequence> JasperPrint createCalendar(int year, Map<LocalDate, T> values,
List<LocalDate> holidays) throws JRException {
return new YearlyCalendarCreator(year, (Map<LocalDate, CharSequence>) values, holidays).createCalendar();
} }
public final JasperPrint createCalendar() throws JRException { public final JasperPrint createCalendar() throws JRException {
@ -38,10 +67,8 @@ public abstract class CalendarCreator {
JasperReport report = JasperCompileManager.compileReport(jrxmlResource()); JasperReport report = JasperCompileManager.compileReport(jrxmlResource());
JasperPrint print = JasperFillManager.fillReport(report, parameters, return JasperFillManager.fillReport(report, parameters, getSource());
getSource());
return print;
} }
protected abstract JRDataSource getSource(); protected abstract JRDataSource getSource();
@ -54,22 +81,92 @@ public abstract class CalendarCreator {
Locale.setDefault(Locale.GERMANY); Locale.setDefault(Locale.GERMANY);
// Calendar cal = new GregorianCalendar(2018, Calendar.DECEMBER, 1); List<ClubEvent> allevents = loadAllEvents(false);
//
// Map<Integer, String> events = new HashMap<>(); List<LocalDate> holidays = filterHolidays(allevents);
// events.put(3, "Event at 3rd.");
// events.put(23, "Event at 23th."); Map<LocalDate, CharSequence> values = map(allevents, 2019);
// JasperViewer v1 = new JasperViewer( JasperViewer v1 = new JasperViewer(createCalendar(2019, values, holidays));
// createCalendar(cal.getTime(), events, Arrays.asList(2, 3, 4, 5, 6, 22, 23, 24)));
// v1.setVisible(true);
JasperViewer v1 = new JasperViewer(
createCalendar(2018));
v1.setVisible(true); v1.setVisible(true);
// cal = new GregorianCalendar(2018, Calendar.FEBRUARY, 1); }
// JasperViewer v3 = new JasperViewer(createCalendar(cal.getTime()));
// v3.setVisible(true); private static List<LocalDate> filterHolidays(List<ClubEvent> allevents) {
List<LocalDate> holidays = new ArrayList<>();
Iterator<ClubEvent> 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<LocalDate, CharSequence> map(List<ClubEvent> allevents, int year) {
Map<LocalDate, CharSequence> 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<LocalDate, CharSequence> 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<ClubEvent> 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) { static Calendar toCalendar(long time) {
@ -77,4 +174,29 @@ public abstract class CalendarCreator {
cal.setTimeInMillis(time); cal.setTimeInMillis(time);
return cal; 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;
}
} }

@ -2,6 +2,10 @@ package de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.components;
import java.io.InputStream; import java.io.InputStream;
import java.time.LocalDate; 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 java.util.Map;
import de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.components.calendar.Year; import de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.components.calendar.Year;
@ -14,10 +18,17 @@ public class YearlyCalendarCreator extends CalendarCreator {
private Year year; private Year year;
public YearlyCalendarCreator(int year, Map<LocalDate, CharSequence> values) { public YearlyCalendarCreator(int year, Map<LocalDate, CharSequence> values) {
this(year, values, Collections.emptyList());
}
public YearlyCalendarCreator(int year, Map<LocalDate, CharSequence> values, List<LocalDate> holidays) {
if (values == null) { if (values == null) {
throw new NullPointerException("Calendar values must not be 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 @Override
@ -37,14 +48,16 @@ public class YearlyCalendarCreator extends CalendarCreator {
public static class EmptySource implements JRDataSource { public static class EmptySource implements JRDataSource {
Iterator<Integer> values = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12).iterator();
@Override @Override
public boolean next() throws JRException { public boolean next() throws JRException {
return false; return values.hasNext();
} }
@Override @Override
public Object getFieldValue(JRField jrField) throws JRException { public Object getFieldValue(JRField jrField) throws JRException {
return null; return values.next();
} }
} }

@ -6,8 +6,11 @@ import java.time.Month;
import java.time.format.TextStyle; import java.time.format.TextStyle;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Map; import java.util.Map;
import java.util.Set;
public class Year { public class Year {
@ -15,29 +18,28 @@ public class Year {
private final Locale locale; private final Locale locale;
private final Map<Month, WeeksOfMonth> monthWeeks; private final Map<Month, WeeksOfMonth> monthWeeks;
private final Map<LocalDate, CharSequence> values; private final Map<LocalDate, CharSequence> values;
private final Set<LocalDate> holidays;
public Year(int year) { public Year(int year) {
this(year, Collections.emptyMap(), Locale.getDefault()); this(year, Collections.emptyMap(), Collections.emptyList(), Locale.getDefault());
} }
public Year(int year, Map<LocalDate, CharSequence> values) { public Year(int year, Map<LocalDate, CharSequence> values, List<LocalDate> holidays) {
this(year, values, Locale.getDefault()); this(year, values, holidays, Locale.getDefault());
} }
public Year(int year, Map<LocalDate, CharSequence> values, Locale locale) { public Year(int year, Map<LocalDate, CharSequence> values, List<LocalDate> holidays, Locale locale) {
if (year < 1900 || year > 2100) { if (year < 1900 || year > 2100) {
throw new IllegalArgumentException("Year value must be between 1900 and 2100"); throw new IllegalArgumentException("Year value must be between 1900 and 2100");
} }
this.date = LocalDate.of(year, 1, 1); this.date = LocalDate.of(year, 1, 1);
this.locale = locale; this.locale = locale;
this.holidays = new HashSet<>(holidays);
this.monthWeeks = new HashMap<>(); this.monthWeeks = new HashMap<>();
for (Month m : Month.values()) { for (Month m : Month.values()) {
monthWeeks.put(m, new WeeksOfMonth(m, year)); monthWeeks.put(m, new WeeksOfMonth(m, year));
} }
this.values = values; this.values = values;
for (LocalDate d : values.keySet()) {
System.out.println(d + "\t" + values.get(d));
}
} }
public int getYear() { public int getYear() {
@ -48,6 +50,10 @@ public class Year {
return month.getDisplayName(TextStyle.FULL_STANDALONE, locale); 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. * Day of month, numeric for the specified by parameters.
* *
@ -72,9 +78,18 @@ public class Year {
return weeksOfMonth.getWeek(week - 1).get(dayOfWeek); 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) { public CharSequence getContent(Month month, short week, DayOfWeek dayOfWeek) {
Integer res = getDayOfMonth(month, week, dayOfWeek); LocalDate day = toDate(month, week, dayOfWeek);
LocalDate day = date.withMonth(month.getValue()).withDayOfMonth(res);
return values.get(day); 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);
}
} }

@ -13,21 +13,21 @@ class YearTest {
@Test() @Test()
void test18thCenturyEx() { void test18thCenturyEx() {
assertThrows(IllegalArgumentException.class, () -> new Year(1899, Collections.emptyMap())); assertThrows(IllegalArgumentException.class, () -> new Year(1899));
assertThrows(IllegalArgumentException.class, () -> new Year(2101, Collections.emptyMap())); assertThrows(IllegalArgumentException.class, () -> new Year(2101));
new Year(1900, Collections.emptyMap()); new Year(1900);
new Year(2100, Collections.emptyMap()); new Year(2100);
} }
@Test @Test
void testMondayIsFirst() { 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)); assertEquals("1", y.getDay(Month.OCTOBER, (short) 1, DayOfWeek.MONDAY));
} }
@Test @Test
void testThursdayIsFirst() { void testThursdayIsFirst() {
Year y = new Year(2018, Collections.emptyMap()); Year y = new Year(2018);
assertEquals("", y.getDay(Month.NOVEMBER, (short) 1, DayOfWeek.MONDAY)); assertEquals("", y.getDay(Month.NOVEMBER, (short) 1, DayOfWeek.MONDAY));
} }

Loading…
Cancel
Save