|
|
|
@ -24,46 +24,63 @@ import net.sf.jasperreports.view.JasperViewer; |
|
|
|
|
|
|
|
|
|
|
|
public class CalendarCreator { |
|
|
|
public class CalendarCreator { |
|
|
|
|
|
|
|
|
|
|
|
private Date date; |
|
|
|
// private final Date date;
|
|
|
|
private YearMonth yearMonthObject; |
|
|
|
private final YearMonth yearMonthObject; |
|
|
|
private int daysInMonth; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static JasperPrint createCalendar(Date date) throws JRException { |
|
|
|
public static JasperPrint createCalendar(Date date) throws JRException { |
|
|
|
return new CalendarCreator(date).createCalendar(); |
|
|
|
return new CalendarCreator(date).createCalendar(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
CalendarCreator(Date date) { |
|
|
|
CalendarCreator(Date date) { |
|
|
|
this.date = date; |
|
|
|
// this.date = date;
|
|
|
|
|
|
|
|
|
|
|
|
yearMonthObject = YearMonth.from(date.toInstant() |
|
|
|
yearMonthObject = YearMonth |
|
|
|
|
|
|
|
.from(date.toInstant() |
|
|
|
.atZone(ZoneId.systemDefault()) |
|
|
|
.atZone(ZoneId.systemDefault()) |
|
|
|
.toLocalDate()); |
|
|
|
.toLocalDate() |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
daysInMonth = yearMonthObject.lengthOfMonth(); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
JasperPrint createCalendar() throws JRException { |
|
|
|
JasperPrint createCalendar() throws JRException { |
|
|
|
|
|
|
|
|
|
|
|
Map<String, Object> parameters = new HashMap<>(); |
|
|
|
Map<String, Object> parameters = new HashMap<>(); |
|
|
|
Timestamp timestamp = new Timestamp(date.getTime()); |
|
|
|
Timestamp timestamp = new Timestamp(yearMonthObject.atDay(1).atStartOfDay().atZone(ZoneId.systemDefault()).toInstant().toEpochMilli()); |
|
|
|
|
|
|
|
|
|
|
|
parameters.put("Date", timestamp); |
|
|
|
parameters.put("Date", timestamp); |
|
|
|
|
|
|
|
|
|
|
|
InputStream jrxmlResource = CalendarCreator.class.getResourceAsStream("/jasper/calendar_month.jrxml"); |
|
|
|
InputStream jrxmlResource = CalendarCreator.class.getResourceAsStream("/jasper/calendar_month.jrxml"); |
|
|
|
JasperReport report = JasperCompileManager |
|
|
|
JasperReport report = JasperCompileManager |
|
|
|
.compileReport(jrxmlResource); |
|
|
|
.compileReport(jrxmlResource); |
|
|
|
|
|
|
|
Map<Integer, String> values = createValues(); |
|
|
|
JasperPrint print = JasperFillManager.fillReport(report, |
|
|
|
JasperPrint print = JasperFillManager.fillReport(report, |
|
|
|
parameters, new CalendarSource(yearMonthObject.atDay(1).getDayOfWeek().getValue() -1, daysInMonth)); |
|
|
|
parameters, new CalendarSource(yearMonthObject, values )); |
|
|
|
|
|
|
|
|
|
|
|
return print; |
|
|
|
return print; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private Map<Integer, String> createValues() { |
|
|
|
|
|
|
|
Map<Integer, String> values = new HashMap<>(); |
|
|
|
|
|
|
|
for (int i=1; i<33; i++) { |
|
|
|
|
|
|
|
values.put(i -1, "Termine Tag " + i); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return values; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private static class CalendarSource implements JRDataSource { |
|
|
|
private static class CalendarSource implements JRDataSource { |
|
|
|
|
|
|
|
|
|
|
|
private final List<Integer> days; |
|
|
|
private final List<Integer> days; |
|
|
|
|
|
|
|
private final Map<Integer, String> dayContent; |
|
|
|
private int index = -1; |
|
|
|
private int index = -1; |
|
|
|
|
|
|
|
private int prefix; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public CalendarSource(YearMonth yearMonthObject, Map<Integer, String> dayContent) { |
|
|
|
|
|
|
|
|
|
|
|
public CalendarSource(int prefix, int daysInMonth) { |
|
|
|
|
|
|
|
days = new ArrayList<>(); |
|
|
|
days = new ArrayList<>(); |
|
|
|
|
|
|
|
this.dayContent = dayContent; |
|
|
|
|
|
|
|
prefix = yearMonthObject.atDay(1).getDayOfWeek().getValue() -1; |
|
|
|
|
|
|
|
int daysInMonth = yearMonthObject.lengthOfMonth(); |
|
|
|
|
|
|
|
|
|
|
|
for (int i=0, limit = daysInMonth + prefix; i<limit; i++) { |
|
|
|
for (int i=0, limit = daysInMonth + prefix; i<limit; i++) { |
|
|
|
days.add(i+1); |
|
|
|
days.add(i+1); |
|
|
|
} |
|
|
|
} |
|
|
|
@ -81,8 +98,13 @@ public class CalendarCreator { |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public Object getFieldValue(JRField jrField) throws JRException { |
|
|
|
public Object getFieldValue(JRField jrField) throws JRException { |
|
|
|
System.err.println(String.format("name=%s, type=%s, value=%d, parentProps=%s", jrField.getName(), jrField.getValueClass(), days.get(index), jrField.getParentProperties())); |
|
|
|
if (jrField.getName().equals("id")) { |
|
|
|
return days.get(index); |
|
|
|
return days.get(index); |
|
|
|
|
|
|
|
} else if (jrField.getName().equals("Field_Value") && days.get(index)>0) { |
|
|
|
|
|
|
|
return dayContent.get(index - prefix); |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
return ""; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
@ -91,14 +113,10 @@ public class CalendarCreator { |
|
|
|
|
|
|
|
|
|
|
|
Locale.setDefault(Locale.GERMANY); |
|
|
|
Locale.setDefault(Locale.GERMANY); |
|
|
|
|
|
|
|
|
|
|
|
Calendar cal = new GregorianCalendar(2018, Calendar.AUGUST, 1); |
|
|
|
Calendar cal = new GregorianCalendar(2018, Calendar.OCTOBER, 1); |
|
|
|
JasperViewer v1 = new JasperViewer(createCalendar(cal.getTime())); |
|
|
|
JasperViewer v1 = new JasperViewer(createCalendar(cal.getTime())); |
|
|
|
v1.setVisible(true); |
|
|
|
v1.setVisible(true); |
|
|
|
|
|
|
|
|
|
|
|
cal = new GregorianCalendar(2018, Calendar.SEPTEMBER, 1); |
|
|
|
|
|
|
|
JasperViewer v2 = new JasperViewer(createCalendar(cal.getTime())); |
|
|
|
|
|
|
|
v2.setVisible(true); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cal = new GregorianCalendar(2018, Calendar.FEBRUARY, 1); |
|
|
|
cal = new GregorianCalendar(2018, Calendar.FEBRUARY, 1); |
|
|
|
JasperViewer v3 = new JasperViewer(createCalendar(cal.getTime())); |
|
|
|
JasperViewer v3 = new JasperViewer(createCalendar(cal.getTime())); |
|
|
|
v3.setVisible(true); |
|
|
|
v3.setVisible(true); |
|
|
|
@ -132,7 +150,6 @@ public class CalendarCreator { |
|
|
|
date.set(Calendar.DAY_OF_MONTH, 1); |
|
|
|
date.set(Calendar.DAY_OF_MONTH, 1); |
|
|
|
dow = date.get(Calendar.DAY_OF_WEEK); |
|
|
|
dow = date.get(Calendar.DAY_OF_WEEK); |
|
|
|
|
|
|
|
|
|
|
|
// dow =new Date(date.getYear(), date.getMonth(), 1).getDay();
|
|
|
|
|
|
|
|
dom = date.get(Calendar.MONTH) + 1; |
|
|
|
dom = date.get(Calendar.MONTH) + 1; |
|
|
|
doy = date.get(Calendar.YEAR); |
|
|
|
doy = date.get(Calendar.YEAR); |
|
|
|
days = YearMonth.of(doy, dom).lengthOfMonth(); |
|
|
|
days = YearMonth.of(doy, dom).lengthOfMonth(); |
|
|
|
|