From 50a03247564f61a43d84c0ba33a86080bbae7e83 Mon Sep 17 00:00:00 2001 From: Markus Kreth Date: Fri, 2 Nov 2018 16:15:09 +0100 Subject: [PATCH] Jasper Calendar started --- pom.xml | 5 + .../ui/components/CalendarComponent.java | 61 +++++- .../ui/components/CalendarCreator.java | 187 +++++++++++++++++ .../resources/jasper/calendar_month.jrxml | 188 ++++++++++++++++++ .../ui/components/CalendarCreatorTest.java | 39 ++++ 5 files changed, 478 insertions(+), 2 deletions(-) create mode 100644 src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/CalendarCreator.java create mode 100644 src/main/resources/jasper/calendar_month.jrxml create mode 100644 src/test/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/CalendarCreatorTest.java diff --git a/pom.xml b/pom.xml index 1b1cc52..34e9dc2 100644 --- a/pom.xml +++ b/pom.xml @@ -197,6 +197,11 @@ 3.23.1-GA + + net.sf.jasperreports + jasperreports + 6.7.0 + diff --git a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/CalendarComponent.java b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/CalendarComponent.java index 32cd1b8..4ca0428 100644 --- a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/CalendarComponent.java +++ b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/CalendarComponent.java @@ -1,11 +1,19 @@ package de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.components; +import java.io.IOException; +import java.io.PipedInputStream; +import java.io.PipedOutputStream; import java.time.LocalDateTime; import java.time.Month; import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; import java.util.Collection; +import java.util.Date; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.vaadin.addon.calendar.Calendar; import org.vaadin.addon.calendar.item.BasicItemProvider; import org.vaadin.addon.calendar.ui.CalendarComponentEvents.BackwardHandler; @@ -15,7 +23,10 @@ import org.vaadin.addon.calendar.ui.CalendarComponentEvents.ItemClickHandler; import com.vaadin.contextmenu.ContextMenu; import com.vaadin.contextmenu.MenuItem; +import com.vaadin.server.StreamResource; import com.vaadin.shared.Registration; +import com.vaadin.ui.AbstractComponent; +import com.vaadin.ui.BrowserFrame; import com.vaadin.ui.Button; import com.vaadin.ui.Button.ClickEvent; import com.vaadin.ui.CustomComponent; @@ -23,14 +34,19 @@ import com.vaadin.ui.HorizontalLayout; import com.vaadin.ui.Label; import com.vaadin.ui.Notification; import com.vaadin.ui.VerticalLayout; +import com.vaadin.ui.Window; import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.ClubEvent; +import net.sf.jasperreports.engine.JRException; +import net.sf.jasperreports.engine.JasperExportManager; +import net.sf.jasperreports.engine.JasperPrint; public class CalendarComponent extends CustomComponent { private static final long serialVersionUID = -9152173211931554059L; + private transient final Logger log = LoggerFactory.getLogger(getClass()); - private transient DateTimeFormatter dfMonth = DateTimeFormatter.ofPattern("MMMM uu"); + private transient DateTimeFormatter dfMonth = DateTimeFormatter.ofPattern("MMMM uuuu"); private Label monthName; @@ -67,7 +83,48 @@ public class CalendarComponent extends CustomComponent { } private void calendarExport(MenuItem ev1) { - Notification.show("Do Export"); + + try { + JasperPrint print = CalendarCreator.createCalendar(new Date()); + Window window = new Window(); + window.setCaption("View PDF"); + AbstractComponent e = createEmbedded(print); + window.setContent(e); + window.setModal(true); + window.setWidth("50%"); + window.setHeight("90%"); + monthName.getUI().addWindow(window); + } catch (JRException e) { + log.error("Error Creating Jasper Report.", e); + Notification.show("Fehler bei PDF: " + e); + } catch (IOException e1) { + log.error("Error Creating Jasper Report.", e1); + Notification.show("Fehler bei PDF: " + e1); + } + } + + private AbstractComponent createEmbedded(JasperPrint print) throws IOException, JRException { + + PipedInputStream in = new PipedInputStream(); + final PipedOutputStream out = new PipedOutputStream(in); + + final StreamResource resource = new StreamResource(() -> in, "invoice.pdf"); + resource.setMIMEType("application/pdf"); + + BrowserFrame c = new BrowserFrame("PDF invoice", resource); + c.setSizeFull(); + + ExecutorService exec = Executors.newSingleThreadExecutor(); + exec.execute(() -> { + try { + JasperExportManager.exportReportToPdfStream(print, out); + } catch (JRException e) { + log.error("Error on Export to Pdf.", e); + throw new RuntimeException(e); + } + }); + exec.shutdown(); + return c; } private void updateMonthText(ZonedDateTime startDate) { 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 new file mode 100644 index 0000000..90b0f46 --- /dev/null +++ b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/CalendarCreator.java @@ -0,0 +1,187 @@ +package de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.components; + +import java.io.InputStream; +import java.sql.Timestamp; +import java.time.YearMonth; +import java.time.ZoneId; +import java.util.ArrayList; +import java.util.Calendar; +import java.util.Date; +import java.util.GregorianCalendar; +import java.util.HashMap; +import java.util.List; +import java.util.Locale; +import java.util.Map; + +import net.sf.jasperreports.engine.JRDataSource; +import net.sf.jasperreports.engine.JRException; +import net.sf.jasperreports.engine.JRField; +import net.sf.jasperreports.engine.JasperCompileManager; +import net.sf.jasperreports.engine.JasperFillManager; +import net.sf.jasperreports.engine.JasperPrint; +import net.sf.jasperreports.engine.JasperReport; +import net.sf.jasperreports.view.JasperViewer; + +public class CalendarCreator { + + private Date date; + private YearMonth yearMonthObject; + private int daysInMonth; + + public static JasperPrint createCalendar(Date date) throws JRException { + return new CalendarCreator(date).createCalendar(); + } + + CalendarCreator(Date date) { + this.date = date; + + yearMonthObject = YearMonth.from(date.toInstant() + .atZone(ZoneId.systemDefault()) + .toLocalDate()); + + daysInMonth = yearMonthObject.lengthOfMonth(); + } + + JasperPrint createCalendar() throws JRException { + + Map parameters = new HashMap<>(); + Timestamp timestamp = new Timestamp(date.getTime()); + parameters.put("Date", timestamp); + + InputStream jrxmlResource = CalendarCreator.class.getResourceAsStream("/jasper/calendar_month.jrxml"); + JasperReport report = JasperCompileManager + .compileReport(jrxmlResource); + JasperPrint print = JasperFillManager.fillReport(report, + parameters, new CalendarSource(yearMonthObject.atDay(1).getDayOfWeek().getValue() -1, daysInMonth)); + + return print; + } + + private static class CalendarSource implements JRDataSource { + + private final List days; + private int index = -1; + + public CalendarSource(int prefix, int daysInMonth) { + days = new ArrayList<>(); + for (int i=0, limit = daysInMonth + prefix; i=days.size()) { + return false; + } + index++; + return true; + } + + @Override + 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())); + return days.get(index); + } + + } + + public static void main(String[] args) throws JRException { + + Locale.setDefault(Locale.GERMANY); + + Calendar cal = new GregorianCalendar(2018, Calendar.AUGUST, 1); + JasperViewer v1 = new JasperViewer(createCalendar(cal.getTime())); + 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); + JasperViewer v3 = new JasperViewer(createCalendar(cal.getTime())); + v3.setVisible(true); + } + + /** + * + * @author markus + * + */ + public static class DateMetadata { + + private Calendar date; + private int dow; + private int dom; + private int doy; + private int days; + private int cells; + private int limit; + + public DateMetadata(java.sql.Date date) { + this(toCalendar(date.getTime())); + } + + public DateMetadata(long date) { + this(toCalendar(date)); + } + + public DateMetadata(Calendar date) { + this.date = (Calendar) date.clone(); + date.set(Calendar.DAY_OF_MONTH, 1); + dow = date.get(Calendar.DAY_OF_WEEK); + +// dow =new Date(date.getYear(), date.getMonth(), 1).getDay(); + dom = date.get(Calendar.MONTH) + 1; + doy = date.get(Calendar.YEAR); + days = YearMonth.of(doy, dom).lengthOfMonth(); + cells = dow-1+days; + limit = cells>28?(cells>35?42:35):28; + } + + /** + * + * @param date + */ + public DateMetadata(Date date) { + this(toCalendar(date.getTime())); + } + + public Date getDate() { + return date.getTime(); + } + + public int getDow() { + return dow; + } + + public int getDom() { + return dom; + } + + public int getDoy() { + return doy; + } + + public int getDays() { + return days; + } + + public int getCells() { + return cells; + } + + public int getLimit() { + return limit; + } + + + } + + private static Calendar toCalendar(long time) { + Calendar cal = Calendar.getInstance(); + cal.setTimeInMillis(time); + return cal; + } +} diff --git a/src/main/resources/jasper/calendar_month.jrxml b/src/main/resources/jasper/calendar_month.jrxml new file mode 100644 index 0000000..56c73be --- /dev/null +++ b/src/main/resources/jasper/calendar_month.jrxml @@ -0,0 +1,188 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + 28?($P{Cells}.intValue()>35?42:35):28]]> + + + + + + + + + + + <band height="60"> + <textField pattern="yyyy MMMM"> + <reportElement x="0" y="0" width="770" height="30" uuid="21293131-3839-43aa-b449-b19867eee2da"/> + <textElement textAlignment="Center"> + <font size="16" isBold="true"/> + </textElement> + <textFieldExpression><![CDATA[$P{Date}]]></textFieldExpression> + </textField> + <textField pattern="EEEE"> + <reportElement mode="Opaque" x="0" y="30" width="110" height="30" forecolor="#FFFCFC" backcolor="#403D3D" uuid="1b600f02-858c-4f32-b65e-d3796aeafcf9"> + <property name="com.jaspersoft.studio.unit.y" value="pixel"/> + </reportElement> + <box> + <topPen lineWidth="0.0" lineStyle="Solid"/> + <leftPen lineWidth="0.0" lineStyle="Solid"/> + <bottomPen lineWidth="0.0" lineStyle="Solid"/> + <rightPen lineWidth="0.0" lineStyle="Solid" lineColor="#FFFCFC"/> + </box> + <textElement textAlignment="Center"> + <font size="14" isBold="true"/> + </textElement> + <textFieldExpression><![CDATA[new java.util.GregorianCalendar(2018, Calendar.OCTOBER, 1).getTime()]]></textFieldExpression> + </textField> + <textField pattern="EEEE"> + <reportElement mode="Opaque" x="110" y="30" width="110" height="30" forecolor="#FFFCFC" backcolor="#403D3D" uuid="2dab9c59-9860-4414-afb1-d3237c6ee8c8"> + <property name="com.jaspersoft.studio.unit.y" value="pixel"/> + </reportElement> + <box> + <topPen lineWidth="1.0" lineStyle="Solid"/> + <leftPen lineWidth="1.0" lineStyle="Solid"/> + <bottomPen lineWidth="1.0" lineStyle="Solid"/> + <rightPen lineWidth="1.0" lineStyle="Solid"/> + </box> + <textElement textAlignment="Center"> + <font size="14" isBold="true"/> + </textElement> + <textFieldExpression><![CDATA[new java.util.GregorianCalendar(2018, Calendar.OCTOBER, 2).getTime()]]></textFieldExpression> + </textField> + <textField pattern="EEEE"> + <reportElement mode="Opaque" x="220" y="30" width="110" height="30" forecolor="#FFFCFC" backcolor="#403D3D" uuid="39d4a93c-d2d6-4c1c-b572-4bcb18ef01aa"> + <property name="com.jaspersoft.studio.unit.y" value="pixel"/> + </reportElement> + <box> + <topPen lineWidth="1.0" lineStyle="Solid"/> + <leftPen lineWidth="1.0" lineStyle="Solid"/> + <bottomPen lineWidth="1.0" lineStyle="Solid"/> + <rightPen lineWidth="1.0" lineStyle="Solid"/> + </box> + <textElement textAlignment="Center"> + <font size="14" isBold="true"/> + </textElement> + <textFieldExpression><![CDATA[new java.util.GregorianCalendar(2018, Calendar.OCTOBER, 3).getTime()]]></textFieldExpression> + </textField> + <textField pattern="EEEE"> + <reportElement mode="Opaque" x="330" y="30" width="110" height="30" forecolor="#FFFCFC" backcolor="#403D3D" uuid="cec2cdaa-3eee-4cd0-9691-d69410726991"> + <property name="com.jaspersoft.studio.unit.y" value="pixel"/> + </reportElement> + <box> + <topPen lineWidth="1.0" lineStyle="Solid"/> + <leftPen lineWidth="1.0" lineStyle="Solid"/> + <bottomPen lineWidth="1.0" lineStyle="Solid"/> + <rightPen lineWidth="1.0" lineStyle="Solid"/> + </box> + <textElement textAlignment="Center"> + <font size="14" isBold="true"/> + </textElement> + <textFieldExpression><![CDATA[new java.util.GregorianCalendar(2018, Calendar.OCTOBER, 4).getTime()]]></textFieldExpression> + </textField> + <textField pattern="EEEE"> + <reportElement mode="Opaque" x="440" y="30" width="110" height="30" forecolor="#FFFCFC" backcolor="#403D3D" uuid="3ecffb41-397a-4243-b12e-e8f5738341b1"> + <property name="com.jaspersoft.studio.unit.y" value="pixel"/> + </reportElement> + <box> + <topPen lineWidth="1.0" lineStyle="Solid"/> + <leftPen lineWidth="1.0" lineStyle="Solid"/> + <bottomPen lineWidth="1.0" lineStyle="Solid"/> + <rightPen lineWidth="1.0" lineStyle="Solid"/> + </box> + <textElement textAlignment="Center"> + <font size="14" isBold="true"/> + </textElement> + <textFieldExpression><![CDATA[new java.util.GregorianCalendar(2018, Calendar.OCTOBER, 5).getTime()]]></textFieldExpression> + </textField> + <textField pattern="EEEE"> + <reportElement mode="Opaque" x="550" y="30" width="110" height="30" forecolor="#FFFCFC" backcolor="#C20802" uuid="f2f2c473-7afb-4177-81a0-280f32264894"> + <property name="com.jaspersoft.studio.unit.y" value="pixel"/> + </reportElement> + <box> + <topPen lineWidth="1.0" lineStyle="Solid"/> + <leftPen lineWidth="1.0" lineStyle="Solid"/> + <bottomPen lineWidth="1.0" lineStyle="Solid"/> + <rightPen lineWidth="1.0" lineStyle="Solid"/> + </box> + <textElement textAlignment="Center"> + <font size="14" isBold="true"/> + </textElement> + <textFieldExpression><![CDATA[new java.util.GregorianCalendar(2018, Calendar.OCTOBER, 6).getTime()]]></textFieldExpression> + </textField> + <textField pattern="EEEE"> + <reportElement mode="Opaque" x="660" y="30" width="110" height="30" forecolor="#FFFCFC" backcolor="#FC0703" uuid="00bc94f5-abd4-40ef-845a-0d9a5fa6fba0"> + <property name="com.jaspersoft.studio.unit.y" value="pixel"/> + </reportElement> + <box> + <topPen lineWidth="1.0" lineStyle="Solid"/> + <leftPen lineWidth="1.0" lineStyle="Solid"/> + <bottomPen lineWidth="1.0" lineStyle="Solid"/> + <rightPen lineWidth="1.0" lineStyle="Solid"/> + </box> + <textElement textAlignment="Center"> + <font size="14" isBold="true"/> + </textElement> + <textFieldExpression><![CDATA[new java.util.GregorianCalendar(2018, Calendar.OCTOBER, 7).getTime()]]></textFieldExpression> + </textField> + </band> + + + + + + + + + + + + + + + + + + 0&&$V{Date}.intValue()<=$P{Days}.intValue()]]> + + + + + + + + + + + + + + diff --git a/src/test/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/CalendarCreatorTest.java b/src/test/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/CalendarCreatorTest.java new file mode 100644 index 0000000..6a96c66 --- /dev/null +++ b/src/test/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/CalendarCreatorTest.java @@ -0,0 +1,39 @@ +package de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.components; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.util.GregorianCalendar; + +import org.junit.jupiter.api.Test; + +import com.ibm.icu.util.Calendar; + +import de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.components.CalendarCreator.DateMetadata; + +class CalendarCreatorTest { + + @Test + void testMetadataDays() { + DateMetadata meta = new DateMetadata(new GregorianCalendar(2018, Calendar.AUGUST, 21, 17, 11, 13).getTimeInMillis()); + assertEquals(31, meta.getDays()); + + meta = new DateMetadata(new GregorianCalendar(2018, Calendar.SEPTEMBER, 21, 17, 11, 13).getTimeInMillis()); + assertEquals(30, meta.getDays()); + + meta = new DateMetadata(new GregorianCalendar(2018, Calendar.FEBRUARY, 21, 17, 11, 13).getTimeInMillis()); + assertEquals(28, meta.getDays()); + } + + @Test + void testMetadataCells() { + DateMetadata meta = new DateMetadata(new GregorianCalendar(2018, Calendar.AUGUST, 21, 17, 11, 13).getTimeInMillis()); + assertEquals(34, meta.getCells()); + + meta = new DateMetadata(new GregorianCalendar(2018, Calendar.SEPTEMBER, 21, 17, 11, 13).getTimeInMillis()); + assertEquals(36, meta.getCells()); + + meta = new DateMetadata(new GregorianCalendar(2018, Calendar.FEBRUARY, 21, 17, 11, 13).getTimeInMillis()); + assertEquals(32, meta.getCells()); + } + +}