Started Clubcommands for all Actions

master
Markus Kreth 6 years ago
parent 5d29a2d3fb
commit d99f17d719
  1. 126
      src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/commands/AbstractExportAction.java
  2. 16
      src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/commands/ClubCommand.java
  3. 34
      src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/commands/ExportCalendarMonthCommand.java
  4. 34
      src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/commands/ExportCalendarYearCommand.java
  5. 103
      src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/commands/ExportCsvCommand.java
  6. 34
      src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/commands/LoginCommand.java
  7. 38
      src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/commands/LogoutCommand.java
  8. 31
      src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/commands/OpenPersonEditorCommand.java
  9. 10
      src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/commands/RevertableCommand.java
  10. 52
      src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/menu/ClubhelperMenuBar.java
  11. 49
      src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/menu/LoggedOffState.java
  12. 9
      src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/menu/LoggedinMenuitemState.java
  13. 6
      src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/menu/MenuItemState.java
  14. 7
      src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/navigation/ClubhelperNavigation.java
  15. 1
      src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/navigation/DesktopHeadView.java
  16. 223
      src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/navigation/HeadView.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<ZonedDateTime> startTime;
private final Supplier<ZonedDateTime> endTime;
private final ClubEventProvider dataProvider;
private final BiConsumer<String, JasperPrint> printConsumer;
public AbstractExportAction(Supplier<ZonedDateTime> startTime,
Supplier<ZonedDateTime> endTime, ClubEventProvider dataProvider,
BiConsumer<String, JasperPrint> printConsumer) {
super();
this.startTime = startTime;
this.endTime = endTime;
this.dataProvider = dataProvider;
this.printConsumer = printConsumer;
}
@Override
public void execute() {
boolean monthOnly = getMonthOnly();
List<ClubEvent> 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<LocalDate, StringBuilder> values = new HashMap<>();
List<LocalDate> 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();
}

@ -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();
}

@ -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<ZonedDateTime> startTime, Supplier<ZonedDateTime> endTime,
ClubEventProvider dataProvider, BiConsumer<String, JasperPrint> 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;
}
}

@ -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<ZonedDateTime> startTime, Supplier<ZonedDateTime> endTime,
ClubEventProvider dataProvider, BiConsumer<String, JasperPrint> 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;
}
}

@ -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<ClubEvent> 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;
}
}

@ -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;
}
}

@ -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());
}
}

@ -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());
}
}

@ -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();
}

@ -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<MenuItem> getAllMainMenus() {
return Arrays.asList(fileMenuItem, editMenuItem, viewMenuItem, settingsItem);
}
}

@ -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());
}
}

@ -0,0 +1,9 @@
package de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.components.menu;
public class LoggedinMenuitemState implements MenuItemState {
@Override
public void applyMenuStates(ClubhelperMenuBar menuBar) {
}
}

@ -0,0 +1,6 @@
package de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.components.menu;
public interface MenuItemState {
void applyMenuStates(ClubhelperMenuBar menuBar);
}

@ -79,6 +79,10 @@ public class ClubhelperNavigation implements ApplicationContextAware {
}); });
} }
public ClubNavigator getNavigator() {
return navi;
}
class ViewFactory { class ViewFactory {
private Page page; private Page page;
@ -112,6 +116,9 @@ public class ClubhelperNavigation implements ApplicationContextAware {
private final Stack<ClubhelperViews> navigationViewNames = new Stack<>(); private final Stack<ClubhelperViews> navigationViewNames = new Stack<>();
private ClubNavigator() {
}
ClubNavigator init(UI ui) { ClubNavigator init(UI ui) {
init(ui, null, new SingleComponentContainerViewDisplay(ui)); init(ui, null, new SingleComponentContainerViewDisplay(ui));
return this; return this;

@ -19,6 +19,7 @@ public class DesktopHeadView extends HeadView {
Function<Component, ZonedDateTime> startTime, Function<Component, ZonedDateTime> startTime,
Function<Component, ZonedDateTime> endTime, ClubEventProvider dataProvider, Function<Component, ZonedDateTime> endTime, ClubEventProvider dataProvider,
SecurityVerifier securityVerifier) { SecurityVerifier securityVerifier) {
super(context, navigator, startTime, endTime, dataProvider, securityVerifier); super(context, navigator, startTime, endTime, dataProvider, securityVerifier);
} }

@ -1,32 +1,19 @@
package de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.navigation; package de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.navigation;
import java.io.ByteArrayInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.io.PipedInputStream; import java.io.PipedInputStream;
import java.io.PipedOutputStream; import java.io.PipedOutputStream;
import java.io.StringWriter;
import java.nio.charset.StandardCharsets;
import java.time.LocalDate;
import java.time.ZonedDateTime; import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter; 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.function.Function;
import java.util.stream.Collectors; import java.util.function.Supplier;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import com.vaadin.contextmenu.ContextMenu; import com.vaadin.contextmenu.ContextMenu;
import com.vaadin.data.provider.Query;
import com.vaadin.icons.VaadinIcons; import com.vaadin.icons.VaadinIcons;
import com.vaadin.server.FileDownloader;
import com.vaadin.server.StreamResource; import com.vaadin.server.StreamResource;
import com.vaadin.ui.AbstractComponent; import com.vaadin.ui.AbstractComponent;
import com.vaadin.ui.Alignment; import com.vaadin.ui.Alignment;
@ -36,20 +23,19 @@ import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Component; import com.vaadin.ui.Component;
import com.vaadin.ui.HorizontalLayout; import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label; 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 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.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.SecurityGroups;
import de.kreth.vaadin.clubhelper.vaadinclubhelper.security.SecurityVerifier; 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.CalendarComponent.ClubEventProvider;
import de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.components.EventGrid;
import de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.navigation.ClubhelperNavigation.ClubNavigator; import de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.navigation.ClubhelperNavigation.ClubNavigator;
import net.sf.jasperreports.engine.JRException; import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperExportManager; import net.sf.jasperreports.engine.JasperExportManager;
@ -63,8 +49,6 @@ public class HeadView extends HorizontalLayout {
private final ClubEventProvider dataProvider; private final ClubEventProvider dataProvider;
private int monthItemId;
private final Button personLabel; private final Button personLabel;
protected final Label monthName; protected final Label monthName;
@ -79,12 +63,13 @@ public class HeadView extends HorizontalLayout {
private ApplicationContext context; private ApplicationContext context;
public HeadView(ApplicationContext context, ClubNavigator navigator, Function<Component, ZonedDateTime> startTime, public HeadView(ApplicationContext context, ClubNavigator navigator2,
Function<Component, ZonedDateTime> startTime,
Function<Component, ZonedDateTime> endTime, ClubEventProvider dataProvider, Function<Component, ZonedDateTime> endTime, ClubEventProvider dataProvider,
SecurityVerifier securityVerifier) { SecurityVerifier securityVerifier) {
this.context = context; this.context = context;
this.navigator = navigator; this.navigator = navigator2;
this.securityVerifier = securityVerifier; this.securityVerifier = securityVerifier;
this.startTime = startTime; this.startTime = startTime;
this.endTime = endTime; this.endTime = endTime;
@ -133,27 +118,44 @@ public class HeadView extends HorizontalLayout {
switch (button.getId()) { switch (button.getId()) {
case "head.menu": case "head.menu":
MenuItem menuItem = contextMenu.addItem("Export Monat", ev1 -> calendarExport(button, ev1));
monthItemId = menuItem.getId(); Supplier<ZonedDateTime> start = () -> startTime.apply(button);
contextMenu.addItem("Export Jahr", ev1 -> calendarExport(button, ev1)); Supplier<ZonedDateTime> end = () -> endTime.apply(button);
contextMenu.addItem("Export Termintaabelle", ev1 -> calendarCsv(button, ev1));
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() if (securityVerifier.isLoggedin()
&& securityVerifier.isPermitted(SecurityGroups.ADMIN, SecurityGroups.UEBUNGSLEITER)) { && 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); contextMenu.open(50, 50);
break; break;
case "head.user": case "head.user":
if (securityVerifier.isLoggedin()) { if (securityVerifier.isLoggedin()) {
contextMenu.addItem("Abmelden", ev1 -> { LogoutCommand logoutCommand = new LogoutCommand(navigator, securityVerifier);
securityVerifier.setLoggedinPerson(null); contextMenu.addItem(logoutCommand.getLabel(), ev1 -> {
navigator.navigateTo(ClubhelperViews.MainView.name()); logoutCommand.execute();
}); });
} }
else { 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(); int width = getUI().getPage().getBrowserWindowWidth();
@ -165,136 +167,25 @@ public class HeadView extends HorizontalLayout {
} }
private void calendarCsv(Button button, MenuItem ev1) { private void showPrint(String calendarMonth, JasperPrint print) {
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 window = new Window();
window.setCaption("Veranstaltungen"); window.setCaption("View PDF");
window.setContent(layout); AbstractComponent e;
window.setModal(true);
window.setWidth("50%");
window.setHeight("90%");
button.getUI().addWindow(window);
}
private StreamResource csvDownload(EventGrid grid) {
List<ClubEvent> items = grid.getDataProvider()
.fetch(new Query<>())
.collect(Collectors.toList());
CsvExporter exporter = new CsvExporter();
StringWriter writer = new StringWriter();
try { try {
exporter.export(items, writer); e = createEmbedded(calendarMonth, print);
} }
catch (IOException e) { catch (Exception e1) {
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;
}
private void calendarExport(Button source, MenuItem ev1) { return;
boolean monthOnly = ev1.getId() == monthItemId;
List<ClubEvent> 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<LocalDate, StringBuilder> values = new HashMap<>();
List<LocalDate> 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);
} }
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 PipedInputStream in = new PipedInputStream();
final PipedOutputStream out = new PipedOutputStream(in); final PipedOutputStream out = new PipedOutputStream(in);
@ -303,20 +194,11 @@ public class HeadView extends HorizontalLayout {
resource.setMIMEType("application/pdf"); resource.setMIMEType("application/pdf");
BrowserFrame c = new BrowserFrame("PDF invoice", resource); BrowserFrame c = new BrowserFrame("PDF invoice", resource);
c.setSizeFull(); c.setSizeFull();
ExecutorService exec = Executors.newSingleThreadExecutor();
try { try {
exec.execute(() -> { JasperExportManager.exportReportToPdfStream(print, out);
try {
JasperExportManager.exportReportToPdfStream(print, out);
}
catch (JRException e) {
throw new RuntimeException("Error on Export to Pdf.", e);
}
finally {
}
});
} }
finally { finally {
try { try {
@ -328,7 +210,6 @@ public class HeadView extends HorizontalLayout {
} }
} }
exec.shutdown();
return c; return c;
} }

Loading…
Cancel
Save