Jasper Calendar started

master
Markus Kreth 7 years ago
parent 858d342960
commit 50a0324756
  1. 5
      pom.xml
  2. 61
      src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/CalendarComponent.java
  3. 187
      src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/CalendarCreator.java
  4. 188
      src/main/resources/jasper/calendar_month.jrxml
  5. 39
      src/test/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/CalendarCreatorTest.java

@ -197,6 +197,11 @@
<version>3.23.1-GA</version>
</dependency>
<dependency>
<groupId>net.sf.jasperreports</groupId>
<artifactId>jasperreports</artifactId>
<version>6.7.0</version>
</dependency>
</dependencies>
<dependencyManagement>

@ -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) {

@ -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<String, Object> 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<Integer> days;
private int index = -1;
public CalendarSource(int prefix, int daysInMonth) {
days = new ArrayList<>();
for (int i=0, limit = daysInMonth + prefix; i<limit; i++) {
days.add(i+1);
}
}
@Override
public boolean next() throws JRException {
if (index+1>=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;
}
}

@ -0,0 +1,188 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.6.0.final using JasperReports Library version 6.6.0 -->
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="clubhelper_calendar" columnCount="7" printOrder="Horizontal" pageWidth="792" pageHeight="612" orientation="Landscape" whenNoDataType="AllSectionsNoDetail" columnWidth="110" leftMargin="11" rightMargin="11" topMargin="6" bottomMargin="6" uuid="32abf871-39b8-4cc7-b03c-d950a55e2bfb">
<property name="com.jaspersoft.studio.data.sql.tables" value=""/>
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="JRSrepo"/>
<property name="com.jaspersoft.studio.unit." value="pixel"/>
<style name="Style1" forecolor="#050505">
<conditionalStyle>
<conditionExpression><![CDATA[$F{id}.intValue()%7==0]]></conditionExpression>
<style forecolor="#FA0702"/>
</conditionalStyle>
</style>
<parameter name="Date" class="java.sql.Timestamp">
<defaultValueExpression><![CDATA[new Date()]]></defaultValueExpression>
</parameter>
<parameter name="DOW" class="java.lang.Integer" isForPrompting="false">
<defaultValueExpression><![CDATA[new Date($P{Date}.getYear(),$P{Date}.getMonth(),1).getDay()]]></defaultValueExpression>
</parameter>
<parameter name="DOM" class="java.lang.Integer" isForPrompting="false">
<defaultValueExpression><![CDATA[$P{Date}.getMonth()+1]]></defaultValueExpression>
</parameter>
<parameter name="DOY" class="java.lang.Integer" isForPrompting="false">
<defaultValueExpression><![CDATA[$P{Date}.getYear()+1900]]></defaultValueExpression>
</parameter>
<parameter name="Days" class="java.lang.Integer" isForPrompting="false">
<defaultValueExpression><![CDATA[java.time.YearMonth.of($P{DOY}.intValue(), $P{DOM}.intValue()).lengthOfMonth()]]></defaultValueExpression>
</parameter>
<parameter name="Cells" class="java.lang.Integer" isForPrompting="false">
<defaultValueExpression><![CDATA[$P{DOW}.intValue()-1+$P{Days}.intValue()]]></defaultValueExpression>
</parameter>
<parameter name="Limit" class="java.lang.Integer" isForPrompting="false">
<defaultValueExpression><![CDATA[$P{Cells}.intValue()>28?($P{Cells}.intValue()>35?42:35):28]]></defaultValueExpression>
</parameter>
<queryString>
<![CDATA[SELECT i as id FROM generate_series(1, $P!{Limit} ) i]]>
</queryString>
<field name="id" class="java.lang.Integer"/>
<field name="Field_Value" class="java.lang.String"/>
<variable name="Date" class="java.lang.Integer">
<variableExpression><![CDATA[$F{id}.intValue()-$P{DOW}.intValue()+1]]></variableExpression>
</variable>
<title>
<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>
</title>
<detail>
<band height="90" splitType="Stretch">
<frame>
<reportElement mode="Transparent" x="0" y="0" width="110" height="90" backcolor="#F7C86F" uuid="75440275-c439-4c3e-bb77-f9f4d8f48415">
<property name="com.jaspersoft.studio.unit.x" value="pixel"/>
<property name="com.jaspersoft.studio.unit.y" value="pixel"/>
<property name="com.jaspersoft.studio.unit.width" 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>
<textField>
<reportElement style="Style1" mode="Transparent" x="60" y="0" width="50" height="25" isRemoveLineWhenBlank="true" backcolor="#C6EDBB" uuid="733ff549-0109-4f55-b9de-476f0ad9fbeb">
<printWhenExpression><![CDATA[$V{Date}.intValue()>0&&$V{Date}.intValue()<=$P{Days}.intValue()]]></printWhenExpression>
</reportElement>
<textElement textAlignment="Right">
<font size="16" isBold="true"/>
</textElement>
<textFieldExpression><![CDATA[$V{Date}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="0" y="25" width="110" height="65" uuid="52fccbc5-19e8-40be-b02c-3d23afa55a7d"/>
<textFieldExpression><![CDATA[$F{Field_Value}]]></textFieldExpression>
</textField>
</frame>
</band>
</detail>
</jasperReport>

@ -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());
}
}
Loading…
Cancel
Save