diff --git a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/commands/AbstractExportAction.java b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/commands/AbstractExportAction.java new file mode 100644 index 0000000..37ab913 --- /dev/null +++ b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/commands/AbstractExportAction.java @@ -0,0 +1,126 @@ +package de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.commands; + +import java.time.LocalDate; +import java.time.ZonedDateTime; +import java.time.format.DateTimeFormatter; +import java.util.Date; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.function.BiConsumer; +import java.util.function.Supplier; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.vaadin.ui.Notification; + +import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.ClubEvent; +import de.kreth.vaadin.clubhelper.vaadinclubhelper.jasper.CalendarCreator; +import de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.components.CalendarComponent.ClubEventProvider; +import net.sf.jasperreports.engine.JRException; +import net.sf.jasperreports.engine.JasperPrint; + +public abstract class AbstractExportAction implements ClubCommand { + + protected final transient Logger log = LoggerFactory.getLogger(getClass()); + + private transient DateTimeFormatter dfMonth = DateTimeFormatter.ofPattern("MMMM uuuu"); + + private final Supplier startTime; + + private final Supplier endTime; + + private final ClubEventProvider dataProvider; + + private final BiConsumer printConsumer; + + public AbstractExportAction(Supplier startTime, + Supplier endTime, ClubEventProvider dataProvider, + BiConsumer printConsumer) { + super(); + this.startTime = startTime; + this.endTime = endTime; + this.dataProvider = dataProvider; + this.printConsumer = printConsumer; + + } + + @Override + public void execute() { + + boolean monthOnly = getMonthOnly(); + List items; + ZonedDateTime start; + ZonedDateTime end; + if (monthOnly) { + start = startTime.get(); + end = endTime.get(); + items = dataProvider.getItems(start, end); + } + else { + start = startTime.get().withDayOfYear(1); + end = start.withMonth(12).withDayOfMonth(31); + items = dataProvider.getItems(start, end); + } + + Map values = new HashMap<>(); + List holidays = CalendarCreator.filterHolidays(items); + + log.debug("exporting Calendar from {} to {}, itemCount = {}", start, end, items.size()); + + for (ClubEvent ev : items) { + + ZonedDateTime evStart = ev.getStart(); + ZonedDateTime evEnd = ev.getEnd(); + + log.trace("Added to eventsd: {}", ev); + + CalendarCreator.iterateDays(evStart.toLocalDate(), evEnd.toLocalDate(), day -> { + + StringBuilder content; + if (values.containsKey(day)) { + content = values.get(day); + content.append("\n"); + } + else { + content = new StringBuilder(); + values.put(day, content); + } + content.append(ev.getCaption()); + if (ev.getLocation() != null && ev.getLocation().isBlank() == false) { + content.append(" (").append(ev.getLocation()).append(")"); + } + }); + } + + String calendarMonth; + if (monthOnly) { + calendarMonth = dfMonth.format(start); + } + else { + calendarMonth = "Jahr " + start.getYear(); + } + + try { + JasperPrint print; + if (monthOnly) { + print = CalendarCreator.createCalendar(new Date(start.toInstant().toEpochMilli()), values, holidays); + } + else { + print = CalendarCreator.createYearCalendar(start.getYear(), values, holidays); + } + log.trace("Created Jasper print for {}", calendarMonth); + + printConsumer.accept(calendarMonth, print); + log.trace("Added pdf window for {}", calendarMonth); + } + catch (JRException | RuntimeException e) { + log.error("Error Creating Jasper Report for {}", calendarMonth, e); + Notification.show("Fehler bei PDF: " + e); + } + } + + protected abstract boolean getMonthOnly(); + +} diff --git a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/commands/ClubCommand.java b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/commands/ClubCommand.java new file mode 100644 index 0000000..601655d --- /dev/null +++ b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/commands/ClubCommand.java @@ -0,0 +1,16 @@ +package de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.commands; + +import com.vaadin.server.Resource; + +public interface ClubCommand { + + String getLabel(); + + /** + * may be null if no Icon is supported. + * @return + */ + Resource getIcon(); + + void execute(); +} diff --git a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/commands/ExportCalendarMonthCommand.java b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/commands/ExportCalendarMonthCommand.java new file mode 100644 index 0000000..2b9f5bf --- /dev/null +++ b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/commands/ExportCalendarMonthCommand.java @@ -0,0 +1,34 @@ +package de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.commands; + +import java.time.ZonedDateTime; +import java.util.function.BiConsumer; +import java.util.function.Supplier; + +import com.vaadin.server.Resource; + +import de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.components.CalendarComponent.ClubEventProvider; +import net.sf.jasperreports.engine.JasperPrint; + +public class ExportCalendarMonthCommand extends AbstractExportAction { + + public ExportCalendarMonthCommand(Supplier startTime, Supplier endTime, + ClubEventProvider dataProvider, BiConsumer printConsumer) { + super(startTime, endTime, dataProvider, printConsumer); + } + + @Override + public String getLabel() { + return "Export Monat"; + } + + @Override + public Resource getIcon() { + return null; + } + + @Override + protected boolean getMonthOnly() { + return true; + } + +} diff --git a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/commands/ExportCalendarYearCommand.java b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/commands/ExportCalendarYearCommand.java new file mode 100644 index 0000000..5484336 --- /dev/null +++ b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/commands/ExportCalendarYearCommand.java @@ -0,0 +1,34 @@ +package de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.commands; + +import java.time.ZonedDateTime; +import java.util.function.BiConsumer; +import java.util.function.Supplier; + +import com.vaadin.server.Resource; + +import de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.components.CalendarComponent.ClubEventProvider; +import net.sf.jasperreports.engine.JasperPrint; + +public class ExportCalendarYearCommand extends AbstractExportAction { + + public ExportCalendarYearCommand(Supplier startTime, Supplier endTime, + ClubEventProvider dataProvider, BiConsumer printConsumer) { + super(startTime, endTime, dataProvider, printConsumer); + } + + @Override + public String getLabel() { + return "Export Jahr"; + } + + @Override + public Resource getIcon() { + return null; + } + + @Override + protected boolean getMonthOnly() { + return false; + } + +} diff --git a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/commands/ExportCsvCommand.java b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/commands/ExportCsvCommand.java new file mode 100644 index 0000000..5a249c3 --- /dev/null +++ b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/commands/ExportCsvCommand.java @@ -0,0 +1,103 @@ +package de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.commands; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.StringWriter; +import java.nio.charset.StandardCharsets; +import java.util.List; +import java.util.stream.Collectors; + +import org.springframework.context.ApplicationContext; + +import com.vaadin.data.provider.Query; +import com.vaadin.icons.VaadinIcons; +import com.vaadin.server.FileDownloader; +import com.vaadin.server.Resource; +import com.vaadin.server.StreamResource; +import com.vaadin.ui.AbstractComponent; +import com.vaadin.ui.Button; +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.business.CsvExporter; +import de.kreth.vaadin.clubhelper.vaadinclubhelper.business.EventBusiness; +import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.ClubEvent; +import de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.components.EventGrid; + +public class ExportCsvCommand implements ClubCommand { + + private final AbstractComponent button; + + private final ApplicationContext context; + + public ExportCsvCommand(AbstractComponent button, ApplicationContext context) { + super(); + this.button = button; + this.context = context; + } + + @Override + public String getLabel() { + return "Export Termintabelle"; + } + + @Override + public Resource getIcon() { + return null; + } + + @Override + public void execute() { + + EventBusiness eventBusiness = context.getBean(EventBusiness.class); + EventGrid grid = new EventGrid(eventBusiness, true); + + HorizontalLayout head = new HorizontalLayout(); + Button downloadButton = new Button("Download", VaadinIcons.DOWNLOAD); + + FileDownloader downloader = new FileDownloader(csvDownload(grid)); + downloader.extend(downloadButton); + + head.addComponents(new Label("Veranstaltungen"), downloadButton); + + VerticalLayout layout = new VerticalLayout(); + layout.addComponents(head, grid); + + Window window = new Window(); + window.setCaption("Veranstaltungen"); + window.setContent(layout); + window.setModal(true); + window.setWidth("50%"); + window.setHeight("90%"); + + button.getUI().addWindow(window); + } + + private StreamResource csvDownload(EventGrid grid) { + List items = grid.getDataProvider() + .fetch(new Query<>()) + .collect(Collectors.toList()); + CsvExporter exporter = new CsvExporter(); + StringWriter writer = new StringWriter(); + + try { + exporter.export(items, writer); + } + catch (IOException e) { + Notification.show("Fehler beim Erzeugen der Veranstaltungen"); + throw new RuntimeException(e); + } + + InputStream in = new ByteArrayInputStream(writer.toString().getBytes(StandardCharsets.UTF_8)); + final StreamResource resource = new StreamResource(() -> in, "Veranstaltungen.csv"); + resource.setMIMEType("application/pdf"); + + return resource; + + } + +} diff --git a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/commands/LoginCommand.java b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/commands/LoginCommand.java new file mode 100644 index 0000000..401c6a9 --- /dev/null +++ b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/commands/LoginCommand.java @@ -0,0 +1,34 @@ +package de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.commands; + +import com.vaadin.icons.VaadinIcons; +import com.vaadin.server.Resource; + +import de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.navigation.ClubhelperNavigation.ClubNavigator; +import de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.navigation.ClubhelperViews; + +public class LoginCommand implements ClubCommand { + + private final ClubNavigator navigator; + + public LoginCommand(ClubNavigator navigator2) { + super(); + this.navigator = navigator2; + } + + @Override + public void execute() { + + navigator.navigateTo(ClubhelperViews.LoginUI.name()); + } + + @Override + public String getLabel() { + return "Anmelden"; + } + + @Override + public Resource getIcon() { + return VaadinIcons.USER; + } + +} diff --git a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/commands/LogoutCommand.java b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/commands/LogoutCommand.java new file mode 100644 index 0000000..c19bf89 --- /dev/null +++ b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/commands/LogoutCommand.java @@ -0,0 +1,38 @@ +package de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.commands; + +import com.vaadin.icons.VaadinIcons; +import com.vaadin.server.Resource; + +import de.kreth.vaadin.clubhelper.vaadinclubhelper.security.SecurityVerifier; +import de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.navigation.ClubhelperNavigation.ClubNavigator; +import de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.navigation.ClubhelperViews; + +public class LogoutCommand implements ClubCommand { + + private final ClubNavigator navigator; + + private final SecurityVerifier securityVerifier; + + public LogoutCommand(ClubNavigator navigator2, SecurityVerifier securityVerifier) { + super(); + this.navigator = navigator2; + this.securityVerifier = securityVerifier; + } + + @Override + public String getLabel() { + return "Abmelden"; + } + + @Override + public Resource getIcon() { + return VaadinIcons.USER; + } + + @Override + public void execute() { + securityVerifier.setLoggedinPerson(null); + navigator.navigateTo(ClubhelperViews.MainView.name()); + } + +} diff --git a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/commands/OpenPersonEditorCommand.java b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/commands/OpenPersonEditorCommand.java new file mode 100644 index 0000000..bc5cf0c --- /dev/null +++ b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/commands/OpenPersonEditorCommand.java @@ -0,0 +1,31 @@ +package de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.commands; + +import com.vaadin.server.Resource; + +import de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.navigation.ClubhelperNavigation.ClubNavigator; +import de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.navigation.ClubhelperViews; + +public class OpenPersonEditorCommand implements ClubCommand { + + private ClubNavigator navigator; + + public OpenPersonEditorCommand(ClubNavigator navigator2) { + this.navigator = navigator2; + } + + @Override + public String getLabel() { + return "Personen verwalten"; + } + + @Override + public Resource getIcon() { + return null; + } + + @Override + public void execute() { + navigator.navigateTo(ClubhelperViews.PersonEditView.name()); + } + +} diff --git a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/commands/RevertableCommand.java b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/commands/RevertableCommand.java new file mode 100644 index 0000000..cf205b1 --- /dev/null +++ b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/commands/RevertableCommand.java @@ -0,0 +1,10 @@ +package de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.commands; + +public interface RevertableCommand extends ClubCommand { + + /** + * Macht das Kommando wieder Rückgängig. + */ + void revert(); + +} diff --git a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/menu/ClubhelperMenuBar.java b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/menu/ClubhelperMenuBar.java new file mode 100644 index 0000000..178c109 --- /dev/null +++ b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/menu/ClubhelperMenuBar.java @@ -0,0 +1,52 @@ +package de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.components.menu; + +import java.util.Arrays; +import java.util.List; + +import com.vaadin.ui.MenuBar; + +public class ClubhelperMenuBar extends MenuBar { + + private final MenuItem fileMenuItem; + + private final MenuItem editMenuItem; + + private final MenuItem viewMenuItem; + + private final MenuItem settingsItem; + + public ClubhelperMenuBar(MenuItemState initialState) { + fileMenuItem = addItem("Datei"); + editMenuItem = addItem("Bearbeiten"); + viewMenuItem = addItem("Ansicht"); + settingsItem = addItem("Einstellungen"); + applyState(initialState); + } + + public void applyState(MenuItemState state) { + for (MenuItem item : getAllMainMenus()) { + item.removeChildren(); + } + state.applyMenuStates(this); + } + + public MenuItem getFileMenuItem() { + return fileMenuItem; + } + + public MenuItem getEditMenuItem() { + return editMenuItem; + } + + public MenuItem getViewMenuItem() { + return viewMenuItem; + } + + public MenuItem getSettingsItem() { + return settingsItem; + } + + public List getAllMainMenus() { + return Arrays.asList(fileMenuItem, editMenuItem, viewMenuItem, settingsItem); + } +} diff --git a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/menu/LoggedOffState.java b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/menu/LoggedOffState.java new file mode 100644 index 0000000..97a585b --- /dev/null +++ b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/menu/LoggedOffState.java @@ -0,0 +1,49 @@ +package de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.components.menu; + +import org.springframework.context.ApplicationContext; + +import com.vaadin.ui.MenuBar.Command; +import com.vaadin.ui.MenuBar.MenuItem; + +import de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.commands.ClubCommand; +import de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.commands.ExportCsvCommand; +import de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.commands.LoginCommand; +import de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.navigation.ClubhelperNavigation; +import de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.navigation.ClubhelperNavigation.ClubNavigator; + +public class LoggedOffState implements MenuItemState { + + private final ClubNavigator navigator; + + private ApplicationContext context; + + public LoggedOffState(ApplicationContext context) { + super(); + this.navigator = context.getBean(ClubhelperNavigation.class).getNavigator(); + this.context = context; + } + + @Override + public void applyMenuStates(ClubhelperMenuBar menuBar) { + + for (MenuItem item : menuBar.getAllMainMenus()) { + item.setVisible(false); + } + MenuItem fileMenu = menuBar.getFileMenuItem(); + fileMenu.setVisible(true); + LoginCommand loginCommand = new LoginCommand(navigator); + fileMenu.addItem(loginCommand.getLabel(), new Command() { + + @Override + public void menuSelected(MenuItem selectedItem) { + loginCommand.execute(); + } + }); + MenuItem editMenu = menuBar.getEditMenuItem(); + editMenu.setVisible(true); + ClubCommand exportCsvCommand = new ExportCsvCommand(menuBar, context); + editMenu.addItem(exportCsvCommand.getLabel(), ev -> exportCsvCommand.execute()); + + } + +} diff --git a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/menu/LoggedinMenuitemState.java b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/menu/LoggedinMenuitemState.java new file mode 100644 index 0000000..d74063e --- /dev/null +++ b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/menu/LoggedinMenuitemState.java @@ -0,0 +1,9 @@ +package de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.components.menu; + +public class LoggedinMenuitemState implements MenuItemState { + + @Override + public void applyMenuStates(ClubhelperMenuBar menuBar) { + } + +} diff --git a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/menu/MenuItemState.java b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/menu/MenuItemState.java new file mode 100644 index 0000000..351a2c6 --- /dev/null +++ b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/menu/MenuItemState.java @@ -0,0 +1,6 @@ +package de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.components.menu; + +public interface MenuItemState { + + void applyMenuStates(ClubhelperMenuBar menuBar); +} diff --git a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/navigation/ClubhelperNavigation.java b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/navigation/ClubhelperNavigation.java index 3ea373a..c1d863e 100644 --- a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/navigation/ClubhelperNavigation.java +++ b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/navigation/ClubhelperNavigation.java @@ -79,6 +79,10 @@ public class ClubhelperNavigation implements ApplicationContextAware { }); } + public ClubNavigator getNavigator() { + return navi; + } + class ViewFactory { private Page page; @@ -112,6 +116,9 @@ public class ClubhelperNavigation implements ApplicationContextAware { private final Stack navigationViewNames = new Stack<>(); + private ClubNavigator() { + } + ClubNavigator init(UI ui) { init(ui, null, new SingleComponentContainerViewDisplay(ui)); return this; diff --git a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/navigation/DesktopHeadView.java b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/navigation/DesktopHeadView.java index 3664e52..3d7ef3e 100644 --- a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/navigation/DesktopHeadView.java +++ b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/navigation/DesktopHeadView.java @@ -19,6 +19,7 @@ public class DesktopHeadView extends HeadView { Function startTime, Function endTime, ClubEventProvider dataProvider, SecurityVerifier securityVerifier) { + super(context, navigator, startTime, endTime, dataProvider, securityVerifier); } diff --git a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/navigation/HeadView.java b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/navigation/HeadView.java index 5ecebf9..0940580 100644 --- a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/navigation/HeadView.java +++ b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/navigation/HeadView.java @@ -1,32 +1,19 @@ package de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.navigation; -import java.io.ByteArrayInputStream; import java.io.IOException; -import java.io.InputStream; import java.io.PipedInputStream; import java.io.PipedOutputStream; -import java.io.StringWriter; -import java.nio.charset.StandardCharsets; -import java.time.LocalDate; import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; -import java.util.Date; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; import java.util.function.Function; -import java.util.stream.Collectors; +import java.util.function.Supplier; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.context.ApplicationContext; import com.vaadin.contextmenu.ContextMenu; -import com.vaadin.data.provider.Query; import com.vaadin.icons.VaadinIcons; -import com.vaadin.server.FileDownloader; import com.vaadin.server.StreamResource; import com.vaadin.ui.AbstractComponent; import com.vaadin.ui.Alignment; @@ -36,20 +23,19 @@ import com.vaadin.ui.Button.ClickEvent; import com.vaadin.ui.Component; import com.vaadin.ui.HorizontalLayout; import com.vaadin.ui.Label; -import com.vaadin.ui.MenuBar.MenuItem; -import com.vaadin.ui.Notification; -import com.vaadin.ui.VerticalLayout; import com.vaadin.ui.Window; -import de.kreth.vaadin.clubhelper.vaadinclubhelper.business.CsvExporter; -import de.kreth.vaadin.clubhelper.vaadinclubhelper.business.EventBusiness; -import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.ClubEvent; import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.Person; -import de.kreth.vaadin.clubhelper.vaadinclubhelper.jasper.CalendarCreator; import de.kreth.vaadin.clubhelper.vaadinclubhelper.security.SecurityGroups; import de.kreth.vaadin.clubhelper.vaadinclubhelper.security.SecurityVerifier; +import de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.commands.ClubCommand; +import de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.commands.ExportCalendarMonthCommand; +import de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.commands.ExportCalendarYearCommand; +import de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.commands.ExportCsvCommand; +import de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.commands.LoginCommand; +import de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.commands.LogoutCommand; +import de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.commands.OpenPersonEditorCommand; import de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.components.CalendarComponent.ClubEventProvider; -import de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.components.EventGrid; import de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.navigation.ClubhelperNavigation.ClubNavigator; import net.sf.jasperreports.engine.JRException; import net.sf.jasperreports.engine.JasperExportManager; @@ -63,8 +49,6 @@ public class HeadView extends HorizontalLayout { private final ClubEventProvider dataProvider; - private int monthItemId; - private final Button personLabel; protected final Label monthName; @@ -79,12 +63,13 @@ public class HeadView extends HorizontalLayout { private ApplicationContext context; - public HeadView(ApplicationContext context, ClubNavigator navigator, Function startTime, + public HeadView(ApplicationContext context, ClubNavigator navigator2, + Function startTime, Function endTime, ClubEventProvider dataProvider, SecurityVerifier securityVerifier) { this.context = context; - this.navigator = navigator; + this.navigator = navigator2; this.securityVerifier = securityVerifier; this.startTime = startTime; this.endTime = endTime; @@ -133,27 +118,44 @@ public class HeadView extends HorizontalLayout { switch (button.getId()) { case "head.menu": - MenuItem menuItem = contextMenu.addItem("Export Monat", ev1 -> calendarExport(button, ev1)); - monthItemId = menuItem.getId(); - contextMenu.addItem("Export Jahr", ev1 -> calendarExport(button, ev1)); - contextMenu.addItem("Export Termintaabelle", ev1 -> calendarCsv(button, ev1)); + + Supplier start = () -> startTime.apply(button); + Supplier end = () -> endTime.apply(button); + + ClubCommand exportCalendarMonthCommand = new ExportCalendarMonthCommand(start, end, + dataProvider, this::showPrint); + contextMenu.addItem(exportCalendarMonthCommand.getLabel(), + ev1 -> exportCalendarMonthCommand.execute()); + + ClubCommand exportCalendarYearCommand = new ExportCalendarYearCommand(start, end, + dataProvider, this::showPrint); + contextMenu.addItem(exportCalendarYearCommand.getLabel(), ev1 -> exportCalendarYearCommand.execute()); + + ClubCommand exportCsvCommand = new ExportCsvCommand(button, context); + contextMenu.addItem(exportCsvCommand.getLabel(), ev1 -> exportCsvCommand.execute()); + if (securityVerifier.isLoggedin() && securityVerifier.isPermitted(SecurityGroups.ADMIN, SecurityGroups.UEBUNGSLEITER)) { - contextMenu.addItem("Personen verwalten", - ev1 -> navigator.navigateTo(ClubhelperViews.PersonEditView.name())); + + ClubCommand openPersonEditor = new OpenPersonEditorCommand(navigator); + contextMenu.addItem(openPersonEditor.getLabel(), + ev1 -> openPersonEditor.execute()); } contextMenu.open(50, 50); break; case "head.user": if (securityVerifier.isLoggedin()) { - contextMenu.addItem("Abmelden", ev1 -> { - securityVerifier.setLoggedinPerson(null); - navigator.navigateTo(ClubhelperViews.MainView.name()); + LogoutCommand logoutCommand = new LogoutCommand(navigator, securityVerifier); + contextMenu.addItem(logoutCommand.getLabel(), ev1 -> { + logoutCommand.execute(); }); } else { - contextMenu.addItem("Anmelden", ev1 -> navigator.navigateTo(ClubhelperViews.LoginUI.name())); + LoginCommand loginCommand = new LoginCommand(navigator); + contextMenu.addItem(loginCommand.getLabel(), ev1 -> { + loginCommand.execute(); + }); } int width = getUI().getPage().getBrowserWindowWidth(); @@ -165,136 +167,25 @@ public class HeadView extends HorizontalLayout { } - private void calendarCsv(Button button, MenuItem ev1) { - - EventBusiness eventBusiness = context.getBean(EventBusiness.class); - EventGrid grid = new EventGrid(eventBusiness, true); - - HorizontalLayout head = new HorizontalLayout(); - Button downloadButton = new Button("Download", VaadinIcons.DOWNLOAD); - - FileDownloader downloader = new FileDownloader(csvDownload(grid)); - downloader.extend(downloadButton); - - head.addComponents(new Label("Veranstaltungen"), downloadButton); - - VerticalLayout layout = new VerticalLayout(); - layout.addComponents(head, grid); - + private void showPrint(String calendarMonth, JasperPrint print) { Window window = new Window(); - window.setCaption("Veranstaltungen"); - window.setContent(layout); - window.setModal(true); - window.setWidth("50%"); - window.setHeight("90%"); - - button.getUI().addWindow(window); - } - - private StreamResource csvDownload(EventGrid grid) { - List items = grid.getDataProvider() - .fetch(new Query<>()) - .collect(Collectors.toList()); - CsvExporter exporter = new CsvExporter(); - StringWriter writer = new StringWriter(); - + window.setCaption("View PDF"); + AbstractComponent e; try { - exporter.export(items, writer); + e = createEmbedded(calendarMonth, print); } - catch (IOException e) { - Notification.show("Fehler beim Erzeugen der Veranstaltungen"); - throw new RuntimeException(e); - } - - InputStream in = new ByteArrayInputStream(writer.toString().getBytes(StandardCharsets.UTF_8)); - final StreamResource resource = new StreamResource(() -> in, "Veranstaltungen.csv"); - resource.setMIMEType("application/pdf"); - - return resource; - - } + catch (Exception e1) { - private void calendarExport(Button source, MenuItem ev1) { - - boolean monthOnly = ev1.getId() == monthItemId; - List items; - ZonedDateTime start; - ZonedDateTime end; - if (monthOnly) { - start = startTime.apply(source); - end = endTime.apply(source); - items = dataProvider.getItems(start, end); - } - else { - start = startTime.apply(source).withDayOfYear(1); - end = start.withMonth(12).withDayOfMonth(31); - items = dataProvider.getItems(start, end); - } - - Map values = new HashMap<>(); - List holidays = CalendarCreator.filterHolidays(items); - - log.debug("exporting Calendar from {} to {}, itemCount = {}", start, end, items.size()); - - for (ClubEvent ev : items) { - - ZonedDateTime evStart = ev.getStart(); - ZonedDateTime evEnd = ev.getEnd(); - - log.trace("Added to eventsd: {}", ev); - - CalendarCreator.iterateDays(evStart.toLocalDate(), evEnd.toLocalDate(), day -> { - - StringBuilder content; - if (values.containsKey(day)) { - content = values.get(day); - content.append("\n"); - } - else { - content = new StringBuilder(); - values.put(day, content); - } - content.append(ev.getCaption()); - if (ev.getLocation() != null && ev.getLocation().isBlank() == false) { - content.append(" (").append(ev.getLocation()).append(")"); - } - }); - } - - String calendarMonth; - if (monthOnly) { - calendarMonth = dfMonth.format(start); - } - else { - calendarMonth = "Jahr " + start.getYear(); - } - - try { - JasperPrint print; - if (monthOnly) { - print = CalendarCreator.createCalendar(new Date(start.toInstant().toEpochMilli()), values, holidays); - } - else { - print = CalendarCreator.createYearCalendar(start.getYear(), values, holidays); - } - log.trace("Created Jasper print for {}", calendarMonth); - Window window = new Window(); - window.setCaption("View PDF"); - AbstractComponent e = createEmbedded(calendarMonth, print); - window.setContent(e); - window.setModal(true); - window.setWidth("50%"); - window.setHeight("90%"); - personLabel.getUI().addWindow(window); - log.trace("Added pdf window for {}", calendarMonth); - } - catch (JRException | IOException | RuntimeException e) { - log.error("Error Creating Jasper Report for {}", calendarMonth, e); - Notification.show("Fehler bei PDF: " + e); + return; } + window.setContent(e); + window.setModal(true); + window.setWidth("50%"); + window.setHeight("90%"); + personLabel.getUI().addWindow(window); } - private AbstractComponent createEmbedded(String title, JasperPrint print) throws IOException { + private AbstractComponent createEmbedded(String title, JasperPrint print) throws IOException, JRException { final PipedInputStream in = new PipedInputStream(); final PipedOutputStream out = new PipedOutputStream(in); @@ -303,20 +194,11 @@ public class HeadView extends HorizontalLayout { resource.setMIMEType("application/pdf"); BrowserFrame c = new BrowserFrame("PDF invoice", resource); + c.setSizeFull(); - ExecutorService exec = Executors.newSingleThreadExecutor(); try { - exec.execute(() -> { - try { - JasperExportManager.exportReportToPdfStream(print, out); - } - catch (JRException e) { - throw new RuntimeException("Error on Export to Pdf.", e); - } - finally { - } - }); + JasperExportManager.exportReportToPdfStream(print, out); } finally { try { @@ -328,7 +210,6 @@ public class HeadView extends HorizontalLayout { } } - exec.shutdown(); return c; }