parent
4179449460
commit
9da96db4d4
@ -1,7 +1,18 @@ |
|||||||
package de.kreth.vaadin.clubhelper.vaadinclubhelper.dao; |
package de.kreth.vaadin.clubhelper.vaadinclubhelper.dao; |
||||||
|
|
||||||
|
import javax.persistence.NoResultException; |
||||||
|
|
||||||
import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.Person; |
import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.Person; |
||||||
|
|
||||||
public interface PersonDao extends IDao<Person> { |
public interface PersonDao extends IDao<Person> { |
||||||
|
|
||||||
|
/** |
||||||
|
* Returns Person from Storage by username and (encrypted) password. |
||||||
|
* |
||||||
|
* @param username login username |
||||||
|
* @param password encrypted password |
||||||
|
* @return |
||||||
|
* @throws NoResultException if no result is found. |
||||||
|
*/ |
||||||
|
Person findLoginUser(String username, String password) throws NoResultException; |
||||||
} |
} |
||||||
|
|||||||
@ -1,16 +1,24 @@ |
|||||||
package de.kreth.vaadin.clubhelper.vaadinclubhelper.dao; |
package de.kreth.vaadin.clubhelper.vaadinclubhelper.dao; |
||||||
|
|
||||||
|
import javax.persistence.TypedQuery; |
||||||
|
|
||||||
import org.springframework.stereotype.Repository; |
import org.springframework.stereotype.Repository; |
||||||
|
|
||||||
import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.Person; |
import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.Person; |
||||||
|
|
||||||
@Repository |
@Repository |
||||||
public class PersonDaoImpl extends AbstractDaoImpl<Person> |
public class PersonDaoImpl extends AbstractDaoImpl<Person> implements PersonDao { |
||||||
implements |
|
||||||
PersonDao { |
|
||||||
|
|
||||||
public PersonDaoImpl() { |
public PersonDaoImpl() { |
||||||
super(Person.class); |
super(Person.class); |
||||||
} |
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public Person findLoginUser(String username, String password) { |
||||||
|
TypedQuery<Person> query = em.createNamedQuery(Person.QUERY_FINDLOGIN, Person.class); |
||||||
|
query.setParameter("username", username); |
||||||
|
query.setParameter("password", password); |
||||||
|
return query.getSingleResult(); |
||||||
|
} |
||||||
|
|
||||||
} |
} |
||||||
|
|||||||
@ -0,0 +1,52 @@ |
|||||||
|
package de.kreth.vaadin.clubhelper.vaadinclubhelper.ui; |
||||||
|
|
||||||
|
import com.vaadin.navigator.Navigator; |
||||||
|
import com.vaadin.navigator.View; |
||||||
|
import com.vaadin.navigator.ViewChangeListener.ViewChangeEvent; |
||||||
|
import com.vaadin.ui.Alignment; |
||||||
|
import com.vaadin.ui.LoginForm; |
||||||
|
import com.vaadin.ui.Notification; |
||||||
|
import com.vaadin.ui.VerticalLayout; |
||||||
|
|
||||||
|
import de.kreth.vaadin.clubhelper.vaadinclubhelper.dao.PersonDao; |
||||||
|
import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.Person; |
||||||
|
|
||||||
|
public class LoginUI extends VerticalLayout implements View { |
||||||
|
|
||||||
|
private static final long serialVersionUID = 4339018452507960084L; |
||||||
|
public static final String VIEW_NAME = "LoginUI"; |
||||||
|
|
||||||
|
private Navigator navigator; |
||||||
|
private String parameters; |
||||||
|
|
||||||
|
public LoginUI(PersonDao personDao) { |
||||||
|
|
||||||
|
LoginForm lf = new LoginForm(); |
||||||
|
lf.addLoginListener(e -> { |
||||||
|
|
||||||
|
String username = e.getLoginParameter("username"); |
||||||
|
String password = e.getLoginParameter("password"); |
||||||
|
|
||||||
|
try { |
||||||
|
Person loggedin = personDao.findLoginUser(username, password); |
||||||
|
this.getSession().setAttribute(Person.SESSION_LOGIN, loggedin); |
||||||
|
navigator.navigateTo(MainView.VIEW_NAME + '/' + parameters); |
||||||
|
} catch (final Exception ex) { |
||||||
|
String message = "Incorrect user or password:" + ex.getMessage() + e.getLoginParameter("username") + ":" |
||||||
|
+ e.getLoginParameter("password"); |
||||||
|
Notification.show(message, Notification.Type.ERROR_MESSAGE); |
||||||
|
} |
||||||
|
}); |
||||||
|
addComponent(lf); |
||||||
|
setComponentAlignment(lf, Alignment.MIDDLE_CENTER); |
||||||
|
setSizeFull(); |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void enter(ViewChangeEvent event) { |
||||||
|
View.super.enter(event); |
||||||
|
navigator = event.getNavigator(); |
||||||
|
parameters = event.getParameters(); |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,139 @@ |
|||||||
|
package de.kreth.vaadin.clubhelper.vaadinclubhelper.ui; |
||||||
|
|
||||||
|
import java.util.List; |
||||||
|
import java.util.Set; |
||||||
|
import java.util.concurrent.ExecutorService; |
||||||
|
import java.util.concurrent.Executors; |
||||||
|
|
||||||
|
import org.slf4j.Logger; |
||||||
|
import org.slf4j.LoggerFactory; |
||||||
|
import org.vaadin.addon.calendar.ui.CalendarComponentEvents; |
||||||
|
|
||||||
|
import com.vaadin.event.selection.SelectionEvent; |
||||||
|
import com.vaadin.navigator.Navigator; |
||||||
|
import com.vaadin.navigator.View; |
||||||
|
import com.vaadin.navigator.ViewChangeListener.ViewChangeEvent; |
||||||
|
import com.vaadin.ui.HorizontalLayout; |
||||||
|
import com.vaadin.ui.UI; |
||||||
|
|
||||||
|
import de.kreth.vaadin.clubhelper.vaadinclubhelper.business.EventBusiness; |
||||||
|
import de.kreth.vaadin.clubhelper.vaadinclubhelper.dao.GroupDao; |
||||||
|
import de.kreth.vaadin.clubhelper.vaadinclubhelper.dao.PersonDao; |
||||||
|
import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.ClubEvent; |
||||||
|
import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.Person; |
||||||
|
import de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.components.CalendarComponent; |
||||||
|
import de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.components.PersonEditDialog; |
||||||
|
import de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.components.PersonGrid; |
||||||
|
|
||||||
|
public class MainView extends HorizontalLayout implements View { |
||||||
|
|
||||||
|
public static final String VIEW_NAME = ""; |
||||||
|
|
||||||
|
private static final long serialVersionUID = 4831071242146146399L; |
||||||
|
private final Logger LOGGER = LoggerFactory.getLogger(getClass()); |
||||||
|
|
||||||
|
private PersonGrid personGrid; |
||||||
|
|
||||||
|
private CalendarComponent calendar; |
||||||
|
|
||||||
|
private Person loggedinPerson; |
||||||
|
private PersonDao personDao; |
||||||
|
private GroupDao groupDao; |
||||||
|
private EventBusiness eventBusiness; |
||||||
|
private Navigator navigator; |
||||||
|
|
||||||
|
public MainView(PersonDao personDao, GroupDao groupDao, EventBusiness eventBusiness) { |
||||||
|
|
||||||
|
this.personDao = personDao; |
||||||
|
this.groupDao = groupDao; |
||||||
|
this.eventBusiness = eventBusiness; |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void enter(ViewChangeEvent event) { |
||||||
|
View.super.enter(event); |
||||||
|
if (this.navigator == null) { |
||||||
|
|
||||||
|
navigator = event.getNavigator(); |
||||||
|
|
||||||
|
personGrid = new PersonGrid(groupDao, personDao); |
||||||
|
personGrid.setId("main.person"); |
||||||
|
personGrid.setCaption("Personen"); |
||||||
|
personGrid.onClosedFunction(() -> detailClosed()); |
||||||
|
personGrid.onPersonSelect(ev -> personSelectionChange(ev)); |
||||||
|
personGrid.onPersonEdit(p -> onPersonEdit(p)); |
||||||
|
|
||||||
|
calendar = new CalendarComponent(); |
||||||
|
calendar.setId("main.calendar"); |
||||||
|
calendar.setHandler(this::onItemClick); |
||||||
|
|
||||||
|
setSizeFull(); |
||||||
|
addComponents(calendar); |
||||||
|
|
||||||
|
setSizeFull(); |
||||||
|
|
||||||
|
ExecutorService exec = Executors.newSingleThreadExecutor(); |
||||||
|
exec.execute(() -> { |
||||||
|
|
||||||
|
final List<ClubEvent> events = eventBusiness.loadEvents(); |
||||||
|
LOGGER.info("Loaded events: {}", events); |
||||||
|
final UI ui = calendar.getUI(); |
||||||
|
ui.access(() -> { |
||||||
|
calendar.setItems(events); |
||||||
|
ui.push(); |
||||||
|
}); |
||||||
|
|
||||||
|
}); |
||||||
|
exec.shutdown(); |
||||||
|
LOGGER.info("Loaded UI and started fetch of Events"); |
||||||
|
} else { |
||||||
|
LOGGER.info("{} already initialized - opening Person View."); |
||||||
|
openPersonViewForEvent(eventBusiness.getCurrent()); |
||||||
|
calendar.setToday(eventBusiness.getCurrent().getStart()); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
private void onPersonEdit(Person p) { |
||||||
|
PersonEditDialog dlg = new PersonEditDialog(groupDao.listAll(), p, personDao); |
||||||
|
getUI().addWindow(dlg); |
||||||
|
} |
||||||
|
|
||||||
|
private void personSelectionChange(SelectionEvent<Person> ev) { |
||||||
|
Set<Person> selected = ev.getAllSelectedItems(); |
||||||
|
LOGGER.debug("Selection changed to: {}", selected); |
||||||
|
eventBusiness.changePersons(selected); |
||||||
|
} |
||||||
|
|
||||||
|
private void detailClosed() { |
||||||
|
LOGGER.debug("Closing detail view."); |
||||||
|
removeComponent(personGrid); |
||||||
|
} |
||||||
|
|
||||||
|
private void onItemClick(CalendarComponentEvents.ItemClickEvent event) { |
||||||
|
|
||||||
|
loggedinPerson = (Person) getSession().getAttribute(Person.SESSION_LOGIN); |
||||||
|
ClubEvent ev = (ClubEvent) event.getCalendarItem(); |
||||||
|
if (loggedinPerson != null) { |
||||||
|
openPersonViewForEvent(ev); |
||||||
|
} else { |
||||||
|
eventBusiness.setSelected(ev); |
||||||
|
navigator.navigateTo(LoginUI.VIEW_NAME + '/' + ev.getId()); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
public void openPersonViewForEvent(ClubEvent ev) { |
||||||
|
LOGGER.debug("Opening detail view for {}", ev); |
||||||
|
|
||||||
|
removeComponent(personGrid); |
||||||
|
addComponent(personGrid); |
||||||
|
|
||||||
|
eventBusiness.setSelected(null); |
||||||
|
personGrid.setEnabled(false); |
||||||
|
personGrid.setEvent(ev); |
||||||
|
personGrid.setEnabled(true); |
||||||
|
personGrid.setVisible(true); |
||||||
|
eventBusiness.setSelected(ev); |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
@ -0,0 +1,187 @@ |
|||||||
|
package de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.components; |
||||||
|
|
||||||
|
import java.io.IOException; |
||||||
|
import java.io.PipedInputStream; |
||||||
|
import java.io.PipedOutputStream; |
||||||
|
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.Supplier; |
||||||
|
|
||||||
|
import org.slf4j.Logger; |
||||||
|
import org.slf4j.LoggerFactory; |
||||||
|
|
||||||
|
import com.vaadin.contextmenu.ContextMenu; |
||||||
|
import com.vaadin.icons.VaadinIcons; |
||||||
|
import com.vaadin.server.StreamResource; |
||||||
|
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.HorizontalLayout; |
||||||
|
import com.vaadin.ui.Label; |
||||||
|
import com.vaadin.ui.MenuBar.MenuItem; |
||||||
|
import com.vaadin.ui.Notification; |
||||||
|
import com.vaadin.ui.Window; |
||||||
|
|
||||||
|
import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.ClubEvent; |
||||||
|
import de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.components.CalendarComponent.ClubEventProvider; |
||||||
|
import net.sf.jasperreports.engine.JRException; |
||||||
|
import net.sf.jasperreports.engine.JasperExportManager; |
||||||
|
import net.sf.jasperreports.engine.JasperPrint; |
||||||
|
|
||||||
|
public class HeadComponent extends HorizontalLayout { |
||||||
|
|
||||||
|
private static final long serialVersionUID = -7915475211371903028L; |
||||||
|
|
||||||
|
private transient final Logger log = LoggerFactory.getLogger(getClass()); |
||||||
|
private transient DateTimeFormatter dfMonth = DateTimeFormatter.ofPattern("MMMM uuuu"); |
||||||
|
|
||||||
|
private Label monthName; |
||||||
|
|
||||||
|
private final Supplier<ZonedDateTime> startTime; |
||||||
|
private final Supplier<ZonedDateTime> endTime; |
||||||
|
|
||||||
|
private ClubEventProvider dataProvider; |
||||||
|
|
||||||
|
public HeadComponent(Supplier<ZonedDateTime> startTime, Supplier<ZonedDateTime> endTime, |
||||||
|
ClubEventProvider dataProvider) { |
||||||
|
|
||||||
|
monthName = new Label(); |
||||||
|
monthName.setId("calendar.month"); |
||||||
|
monthName.setStyleName("title_label"); |
||||||
|
|
||||||
|
Button popupButton = new Button(VaadinIcons.MENU); |
||||||
|
popupButton.setId("calendar.menu"); |
||||||
|
popupButton.addClickListener(ev -> openPopupMenu(ev)); |
||||||
|
this.addComponent(monthName); |
||||||
|
this.addComponent(popupButton); |
||||||
|
this.startTime = startTime; |
||||||
|
this.endTime = endTime; |
||||||
|
this.dataProvider = dataProvider; |
||||||
|
} |
||||||
|
|
||||||
|
public void updateMonthText(ZonedDateTime startDate) { |
||||||
|
String monthValue = dfMonth.format(startDate); |
||||||
|
log.debug("Changed Month title to {}", monthValue); |
||||||
|
monthName.setValue(monthValue); |
||||||
|
} |
||||||
|
|
||||||
|
private void openPopupMenu(ClickEvent ev) { |
||||||
|
ContextMenu contextMenu = new ContextMenu(ev.getButton(), true); |
||||||
|
contextMenu.addItem("Export Monat", ev1 -> calendarExport(ev1)); |
||||||
|
contextMenu.addItem("Export Jahr", ev1 -> calendarExport(ev1)); |
||||||
|
contextMenu.open(210, 40); |
||||||
|
} |
||||||
|
|
||||||
|
private void calendarExport(MenuItem ev1) { |
||||||
|
|
||||||
|
boolean monthOnly = ev1.getText().contains("Monat"); |
||||||
|
List<ClubEvent> items; |
||||||
|
ZonedDateTime start; |
||||||
|
if (monthOnly) { |
||||||
|
start = startTime.get(); |
||||||
|
ZonedDateTime end = endTime.get(); |
||||||
|
log.debug("exporting Calendar from {} to {}", start, end); |
||||||
|
items = dataProvider.getItems(start, end); |
||||||
|
} else { |
||||||
|
start = startTime.get().withDayOfYear(1); |
||||||
|
ZonedDateTime end = start.withMonth(12).withDayOfMonth(31); |
||||||
|
log.debug("exporting Calendar from {} to {}", start, end); |
||||||
|
items = dataProvider.getItems(start, end); |
||||||
|
} |
||||||
|
|
||||||
|
Map<LocalDate, StringBuilder> values = new HashMap<>(); |
||||||
|
List<LocalDate> holidays = CalendarCreator.filterHolidays(items); |
||||||
|
|
||||||
|
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()); |
||||||
|
}); |
||||||
|
} |
||||||
|
|
||||||
|
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%"); |
||||||
|
monthName.getUI().addWindow(window); |
||||||
|
log.trace("Added pdf window for {}", calendarMonth); |
||||||
|
} catch (JRException e) { |
||||||
|
log.error("Error Creating Jasper Report for {}", calendarMonth, e); |
||||||
|
Notification.show("Fehler bei PDF: " + e); |
||||||
|
} catch (IOException e1) { |
||||||
|
log.error("Error Creating Jasper Report for {}", calendarMonth, e1); |
||||||
|
Notification.show("Fehler bei PDF: " + e1); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
private AbstractComponent createEmbedded(String title, JasperPrint print) throws IOException { |
||||||
|
|
||||||
|
PipedInputStream in = new PipedInputStream(); |
||||||
|
final PipedOutputStream out = new PipedOutputStream(in); |
||||||
|
|
||||||
|
final StreamResource resource = new StreamResource(() -> in, title); |
||||||
|
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); |
||||||
|
} finally { |
||||||
|
try { |
||||||
|
out.close(); |
||||||
|
} catch (IOException e) { |
||||||
|
log.warn("Error closing Jasper output stream.", e); |
||||||
|
} |
||||||
|
} |
||||||
|
}); |
||||||
|
exec.shutdown(); |
||||||
|
return c; |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
@ -0,0 +1,19 @@ |
|||||||
|
package de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.components; |
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertThrows; |
||||||
|
|
||||||
|
import org.junit.jupiter.api.BeforeEach; |
||||||
|
import org.junit.jupiter.api.Test; |
||||||
|
|
||||||
|
class YearlyCalendarCreatorTest { |
||||||
|
|
||||||
|
@BeforeEach |
||||||
|
void setUp() throws Exception { |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
void testYearlyCalendarCreatorIntMapOfLocalDateCharSequenceCollectionOfLocalDate() { |
||||||
|
assertThrows(NullPointerException.class, () -> new YearlyCalendarCreator(2018, null, null)); |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
Loading…
Reference in new issue