Altersgruppe for Event can be stored by UI

master
Markus Kreth 7 years ago
parent 20e2afaf76
commit b95d79b1cf
  1. 5
      pom.xml
  2. 4
      src/main/java/META-INF/persistence.xml
  3. 33
      src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/business/EventBusiness.java
  4. 5
      src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/dao/AbstractDaoImpl.java
  5. 7
      src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/dao/AltersgruppeDao.java
  6. 14
      src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/dao/AltersgruppeDaoImpl.java
  7. 2
      src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/data/Altersgruppe.java
  8. 2
      src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/data/ClubEvent.java
  9. 21
      src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/EventDetails.java
  10. 240
      src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/EventAltersgruppen.java
  11. 6
      src/main/resources/application.properties
  12. 9
      src/main/resources/hibernate.cfg.xml
  13. 2
      src/main/resources/schema/ClubEvent.hbm.xml
  14. 6
      src/main/resources/simplelogger.properties

@ -93,6 +93,11 @@
<artifactId>border-layout-addon</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>org.vaadin.ui</groupId>
<artifactId>numberfield</artifactId>
<version>0.2.0</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>

@ -14,9 +14,11 @@
<class>de.kreth.vaadin.clubhelper.vaadinclubhelper.data.Startpass</class>
<class>de.kreth.vaadin.clubhelper.vaadinclubhelper.data.StartpassStartrechte</class>
<class>de.kreth.vaadin.clubhelper.vaadinclubhelper.data.Version</class>
<class>de.kreth.vaadin.clubhelper.vaadinclubhelper.data.Pflicht</class>
<class>de.kreth.vaadin.clubhelper.vaadinclubhelper.data.Altersgruppe</class>
<properties>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="javax.persistence.jdbc.url" value="localhost:3306"/>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/clubhelper?useUnicode=yes;characterEncoding=utf8"/>
<property name="javax.persistence.jdbc.user" value="root"/>
<property name="javax.persistence.jdbc.password" value="07!73"/>
<property name="javax.persistence.schema-generation.scripts.action" value="create"/>

@ -1,5 +1,7 @@
package de.kreth.vaadin.clubhelper.vaadinclubhelper.business;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Set;
@ -8,7 +10,9 @@ import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import de.kreth.vaadin.clubhelper.vaadinclubhelper.dao.AltersgruppeDao;
import de.kreth.vaadin.clubhelper.vaadinclubhelper.dao.ClubEventDao;
import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.Altersgruppe;
import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.ClubEvent;
import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.Person;
@ -20,6 +24,9 @@ public class EventBusiness {
@Autowired
ClubEventDao dao;
@Autowired
AltersgruppeDao altersgruppeDao;
private ClubEvent current;
public synchronized List<ClubEvent> loadEvents() {
@ -48,4 +55,30 @@ public class EventBusiness {
}
}
}
public Altersgruppe createAltersgruppe() {
Altersgruppe e = new Altersgruppe();
e.setStart(LocalDateTime.now().getYear() - 1);
e.setEnd(LocalDateTime.now().getYear());
e.setChanged(new Date());
e.setCreated(e.getChanged());
Set<Altersgruppe> altersgruppen = current.getAltersgruppen();
if (altersgruppen.contains(e)) {
for (Altersgruppe el : altersgruppen) {
if (el.equals(e)) {
return el;
}
}
} else {
altersgruppen.add(e);
e.setClubEvent(current);
}
return e;
}
public void storeAltersgruppe(Altersgruppe edited) {
altersgruppeDao.save(edited);
dao.update(current);
}
}

@ -23,8 +23,13 @@ public abstract class AbstractDaoImpl<T> implements IDao<T> {
@Override
@Transactional
public void save(T obj) {
if (em.contains(obj)) {
em.merge(obj);
} else {
em.persist(obj);
}
}
@Override
@Transactional

@ -0,0 +1,7 @@
package de.kreth.vaadin.clubhelper.vaadinclubhelper.dao;
import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.Altersgruppe;
public interface AltersgruppeDao extends IDao<Altersgruppe> {
}

@ -0,0 +1,14 @@
package de.kreth.vaadin.clubhelper.vaadinclubhelper.dao;
import org.springframework.stereotype.Repository;
import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.Altersgruppe;
@Repository
public class AltersgruppeDaoImpl extends AbstractDaoImpl<Altersgruppe> implements AltersgruppeDao {
public AltersgruppeDaoImpl() {
super(Altersgruppe.class);
}
}

@ -20,6 +20,8 @@ public class Altersgruppe extends BaseEntity implements Serializable {
private String bezeichnung;
private int start;
private int end;
@ManyToOne
private Pflicht pflicht;
@ManyToOne
@JoinColumn(name = "event_id")

@ -9,6 +9,7 @@ import java.util.Set;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.Transient;
import org.vaadin.addon.calendar.item.BasicItem;
@ -25,6 +26,7 @@ public class ClubEvent extends BasicItem {
private String iCalUID;
private String organizerDisplayName;
@ManyToMany
private Set<Person> persons;
private Set<Altersgruppe> altersgruppen;

@ -9,8 +9,9 @@ import com.vaadin.ui.GridLayout;
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.dao.PflichtenDao;
import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.ClubEvent;
import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.Person;
import de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.components.EventAltersgruppen;
import de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.components.PersonGrid;
import de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.components.SingleEventView;
@ -22,23 +23,26 @@ public class EventDetails extends GridLayout implements NamedView {
private final EventBusiness eventBusiness;
private final PersonDao personDao;
private final GroupDao groupDao;
private final PflichtenDao pflichtenDao;
private Person loggedinPerson;
private ClubEvent currentEvent;
private SingleEventView eventView;
private PersonGrid personGrid;
private EventAltersgruppen eventAltersgruppen;
public EventDetails(PersonDao personDao, GroupDao groupDao, EventBusiness eventBusiness) {
public EventDetails(PersonDao personDao, GroupDao groupDao, EventBusiness eventBusiness,
PflichtenDao pflichtenDao) {
super(3, 5);
this.eventBusiness = eventBusiness;
this.personDao = personDao;
this.groupDao = groupDao;
this.pflichtenDao = pflichtenDao;
}
@Override
public void enter(ViewChangeEvent event) {
Navigator navigator = event.getNavigator();
loggedinPerson = (Person) getSession().getAttribute(Person.SESSION_LOGIN);
currentEvent = eventBusiness.getCurrent();
eventView = new SingleEventView();
@ -50,12 +54,15 @@ public class EventDetails extends GridLayout implements NamedView {
personGrid.hideFilter();
personGrid.setSelectionMode(SelectionMode.NONE);
eventAltersgruppen = new EventAltersgruppen(pflichtenDao, eventBusiness);
Button back = new Button("Zurück");
back.addClickListener(ev -> navigator.navigateTo(((NamedView) event.getOldView()).getViewName()));
addComponent(eventView, 0, 0, 1, 0);
addComponent(personGrid, 0, 1, 1, 1);
addComponent(back, 1, 4);
addComponent(eventView, 0, 0);
addComponent(eventAltersgruppen, 1, 0);
addComponent(personGrid, 2, 0);
addComponent(back, 0, 4);
setSizeFull();
}

@ -0,0 +1,240 @@
package de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.components;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.vaadin.ui.NumberField;
import com.vaadin.data.Binder;
import com.vaadin.data.Binder.Binding;
import com.vaadin.data.HasValue.ValueChangeEvent;
import com.vaadin.data.ValidationResult;
import com.vaadin.data.ValueContext;
import com.vaadin.data.provider.DataProvider;
import com.vaadin.data.provider.ListDataProvider;
import com.vaadin.data.validator.AbstractValidator;
import com.vaadin.event.selection.SelectionEvent;
import com.vaadin.shared.ui.ValueChangeMode;
import com.vaadin.ui.Button;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.Component;
import com.vaadin.ui.CustomField;
import com.vaadin.ui.Grid;
import com.vaadin.ui.Grid.SelectionMode;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.components.grid.Editor;
import com.vaadin.ui.components.grid.EditorSaveEvent;
import de.kreth.vaadin.clubhelper.vaadinclubhelper.business.EventBusiness;
import de.kreth.vaadin.clubhelper.vaadinclubhelper.dao.PflichtenDao;
import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.Altersgruppe;
import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.Pflicht;
public class EventAltersgruppen extends VerticalLayout {
private static final long serialVersionUID = -7777374233838542085L;
private final Logger LOGGER = LoggerFactory.getLogger(getClass());
private static final int OLDEST_YEAR = 1900;
private static final int CURRENT_YEAR = LocalDateTime.now().getYear();
private final Grid<Altersgruppe> gruppen;
private final ListDataProvider<Altersgruppe> provider;
private final Binder<Altersgruppe> binder;
private final EventBusiness eventSupplier;
public EventAltersgruppen(PflichtenDao pflichtenDao, EventBusiness eventBusiness) {
setCaption("Altersklassen");
this.eventSupplier = eventBusiness;
Button addButton = new Button("Hinzufügen");
addButton.addClickListener(ev -> addGruppe());
HorizontalLayout buttonLayout = new HorizontalLayout(addButton);
provider = DataProvider.ofCollection(new ArrayList<Altersgruppe>());
gruppen = new Grid<>();
gruppen.setDataProvider(provider);
binder = gruppen.getEditor().getBinder();
setupGrid(pflichtenDao);
addComponents(buttonLayout, gruppen);
}
void setupGrid(PflichtenDao pflichtenDao) {
Binding<Altersgruppe, Pflicht> pflichtBinding = binder.bind(createPflichtenCombo(pflichtenDao.listAll()),
Altersgruppe::getPflicht, Altersgruppe::setPflicht);
Binding<Altersgruppe, String> bezBinding = binder.bind(createBezeichnungField("Altersgruppe.Bezeichnung"),
Altersgruppe::getBezeichnung, Altersgruppe::setBezeichnung);
IntNumberField vonField = new IntNumberField("Von", "Altersgruppe.Start");
Binding<Altersgruppe, Integer> bindingStart = binder.forField(vonField).withValidator(new YearValidator())
.bind(Altersgruppe::getStart, Altersgruppe::setStart);
Binding<Altersgruppe, Integer> bindingEnd = binder.forField(new IntNumberField("Bis", "Altersgruppe.End"))
.withValidator(new BisYearValidator(vonField)).bind(Altersgruppe::getEnd, Altersgruppe::setEnd);
vonField.addValueChangeListener(ev -> bindingEnd.validate());
gruppen.addColumn(Altersgruppe::getBezeichnung).setCaption("Bezeichnung").setEditorBinding(bezBinding);
gruppen.addColumn(Altersgruppe::getStart).setCaption("Von").setEditorBinding(bindingStart);
gruppen.addColumn(Altersgruppe::getEnd).setCaption("Bis").setEditorBinding(bindingEnd);
gruppen.addColumn(Altersgruppe::getPflicht).setCaption("Pflicht").setEditorBinding(pflichtBinding);
binder.addValueChangeListener(this::valueChange);
gruppen.addSelectionListener(this::selectionChange);
Editor<Altersgruppe> editor = gruppen.getEditor();
editor.setEnabled(true);
editor.addSaveListener(ev -> gridRowSaved(ev));
gruppen.setSelectionMode(SelectionMode.SINGLE);
}
private void gridRowSaved(EditorSaveEvent<Altersgruppe> ev) {
binder.validate();
if (binder.isValid()) {
Altersgruppe edited = binder.getBean();
validateAndStore(edited);
}
provider.refreshAll();
}
void selectionChange(SelectionEvent<Altersgruppe> event) {
Optional<Altersgruppe> selected = event.getFirstSelectedItem();
if (selected.isPresent()) {
binder.setBean(selected.get());
} else {
binder.setBean(null);
}
provider.refreshAll();
}
void valueChange(ValueChangeEvent<Object> event) {
Component component = event.getComponent();
Object value = event.getValue();
Object oldValue = event.getOldValue();
String componentId = component.getId();
LOGGER.trace("Changed: {}, value={}, old={}", componentId, value, oldValue);
}
public void validateAndStore(Altersgruppe edited) {
if (edited.getBezeichnung() != null && !edited.getBezeichnung().isBlank() && edited.getPflicht() != null
&& edited.getClubEvent() != null) {
eventSupplier.storeAltersgruppe(edited);
LOGGER.info("Stored: {}", edited);
}
}
TextField createBezeichnungField(String id) {
TextField textField = new TextField();
textField.setId(id);
return textField;
}
ComboBox<Pflicht> createPflichtenCombo(List<Pflicht> pflichtenList) {
ComboBox<Pflicht> pflichten = new ComboBox<>();
pflichten.setId("Altersgruppe.Pflicht");
pflichten.setItems(pflichtenList);
return pflichten;
}
private void addGruppe() {
Altersgruppe e = eventSupplier.createAltersgruppe();
provider.getItems().add(e);
binder.setBean(e);
provider.refreshAll();
}
private static class YearValidator extends AbstractValidator<Integer> {
private static final long serialVersionUID = 450137530250464249L;
private static final String ERROR_MESSAGE = String.format("Jahreszahl muss zwischen %d und %d liegen",
OLDEST_YEAR, CURRENT_YEAR);
protected YearValidator() {
super(ERROR_MESSAGE);
}
@Override
public ValidationResult apply(Integer value, ValueContext context) {
if (value >= OLDEST_YEAR && value <= CURRENT_YEAR) {
return ValidationResult.ok();
}
return ValidationResult.error(ERROR_MESSAGE);
}
}
private static class BisYearValidator extends YearValidator {
private static final long serialVersionUID = -7197197454502399416L;
final IntNumberField vonField;
public BisYearValidator(IntNumberField vonField) {
super();
this.vonField = vonField;
}
@Override
public ValidationResult apply(Integer value, ValueContext context) {
if (value > vonField.getValue()) {
return super.apply(value, context);
} else {
return ValidationResult.error("Bis Wert muss größer als Von Wert sein!");
}
}
}
private class IntNumberField extends CustomField<Integer> {
private static final long serialVersionUID = 2221967167572584942L;
private final NumberField field;
public IntNumberField(String caption, String id) {
field = new NumberField();
field.setDecimalAllowed(false);
field.setDecimalSeparatorAlwaysShown(false);
field.setGroupingUsed(false);
field.setNegativeAllowed(false);
field.setPlaceholder(String.format("Jahr zwischen 1900 und %d", CURRENT_YEAR));
field.setValueChangeMode(ValueChangeMode.LAZY);
field.setValueChangeTimeout(300);
field.setMinValue(OLDEST_YEAR);
field.setMaxValue(CURRENT_YEAR);
field.setCaption(caption);
field.setId(id);
}
@Override
public Integer getValue() {
String value = field.getValue();
if (value == null || value.isBlank()) {
return Integer.valueOf(0);
}
return Double.valueOf(value).intValue();
}
@Override
protected Component initContent() {
return field;
}
@Override
protected void doSetValue(Integer value) {
field.setValue(value.doubleValue());
}
}
}

@ -1,9 +1,7 @@
spring.datasource.url=jdbc:mysql://localhost:3306/clubhelper?useUnicode=yes&characterEncoding=utf8&serverTimezone=Europe/Berlin
spring.datasource.url=jdbc:mysql://localhost:3306/clubhelper?useUnicode=yes;characterEncoding=utf8
#;serverTimezone=Europe/Berlin
spring.datasource.username=markus
spring.datasource.password=0773
#security.ignored=/**
#security.basic.enable=false
#management.security.enabled=false
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy

@ -5,18 +5,9 @@
<hibernate-configuration>
<session-factory>
<!-- SQL Dialect
<property name="hibernate.dialect">org.hibernate.dialect.MySQL8Dialect</property>
-->
<!-- Database Connection Settings
<property name="hibernate.connection.datasource">java:comp/env/jdbc/clubhelperbackend</property>
-->
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="use_sql_comments">true</property>
<!-- Specifying Session Context -->
<property name="hibernate.current_session_context_class">org.hibernate.context.internal.JTASessionContext</property>
</session-factory>
</hibernate-configuration>

@ -30,7 +30,7 @@
</set>
<set name="altersgruppen" table="altersgruppe" lazy="false">
<key>
<column name="clubevent_id" not-null="true" />
<column name="event_id" not-null="true" />
</key>
<one-to-many class="de.kreth.vaadin.clubhelper.vaadinclubhelper.data.Altersgruppe" />
</set>

@ -3,6 +3,6 @@ org.slf4j.simpleLogger.logFile=System.err
org.slf4j.simpleLogger.showThreadName=false
org.slf4j.simpleLogger.log.de.kreth.vaadin.clubhelper=trace
org.slf4j.simpleLogger.log.org.hibernate=info
org.slf4j.simpleLogger.log.com.zaxxer.hikari=info
org.slf4j.simpleLogger.log.com.mysql=info
org.slf4j.simpleLogger.log.org=info
org.slf4j.simpleLogger.log.com=info
org.slf4j.simpleLogger.log.net=info
Loading…
Cancel
Save