From df91807a9509763ac7f79a0044acdf810a635c47 Mon Sep 17 00:00:00 2001 From: Markus Kreth Date: Tue, 22 Jan 2019 18:49:34 +0100 Subject: [PATCH] Event Type implemented in UI --- .../business/EventBusiness.java | 4 + .../vaadinclubhelper/dao/ClubEventDao.java | 2 + .../dao/ClubEventDaoImpl.java | 32 ++++++++ .../vaadinclubhelper/data/ClubEvent.java | 14 +++- .../data/CompetitionType.java | 25 +++++- .../vaadinclubhelper/ui/EventDetails.java | 8 +- .../vaadinclubhelper/ui/MainView.java | 2 +- .../ui/components/EventAltersgruppen.java | 27 ++++++- .../ui/components/SingleEventView.java | 81 +++++++++++++------ .../ui/components/ZonedDateTimeConverter.java | 25 ++++++ src/main/resources/schema/ClubEvent.hbm.xml | 2 + .../AbstractHibernateConfiguration.java | 2 + .../dao/ClubEventDataTest.java | 16 ++-- 13 files changed, 201 insertions(+), 39 deletions(-) create mode 100644 src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/ZonedDateTimeConverter.java diff --git a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/business/EventBusiness.java b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/business/EventBusiness.java index fd8a4c5..56c0f41 100644 --- a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/business/EventBusiness.java +++ b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/business/EventBusiness.java @@ -85,4 +85,8 @@ public class EventBusiness { public EventMeldung createMeldung() { return new EventMeldung(current); } + + public void storeEventType() { + clubEventDao.updateEventType(current); + } } diff --git a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/dao/ClubEventDao.java b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/dao/ClubEventDao.java index 5c411f8..6502b94 100644 --- a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/dao/ClubEventDao.java +++ b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/dao/ClubEventDao.java @@ -15,4 +15,6 @@ public interface ClubEventDao extends IDao { * @param persons complete List of Persons, including Persons already persisted. */ void addPersons(ClubEvent event, Collection persons); + + void updateEventType(ClubEvent obj); } diff --git a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/dao/ClubEventDaoImpl.java b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/dao/ClubEventDaoImpl.java index 0fce122..0d5142a 100644 --- a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/dao/ClubEventDaoImpl.java +++ b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/dao/ClubEventDaoImpl.java @@ -12,6 +12,7 @@ import org.springframework.stereotype.Repository; import org.springframework.transaction.annotation.Transactional; import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.ClubEvent; +import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.CompetitionType; import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.Person; @Repository @@ -22,6 +23,17 @@ public class ClubEventDaoImpl extends AbstractDaoImpl implements Club super(ClubEvent.class); } + @Override + public ClubEvent update(ClubEvent obj) { + CompetitionType competitionType = obj.getCompetitionType(); + if (competitionType != null) { + if (obj.getCompetitionType().getId() == null) { + obj.getCompetitionType().setId(obj.getId()); + } + } + return super.update(obj); + } + @Override public void addPersons(ClubEvent event, Collection updated) { List added = new ArrayList<>(updated); @@ -48,4 +60,24 @@ public class ClubEventDaoImpl extends AbstractDaoImpl implements Club persons2.addAll(added); } + @Override + public void updateEventType(ClubEvent obj) { + CompetitionType type = obj.getCompetitionType(); + if (type != null) { + Query query; + if (type.getId() == null) { + type.setId(obj.getId()); + query = entityManager.createNativeQuery( + "INSERT INTO clubevent_addon (id, competition_type) VALUES (:eventId,:eventtype)"); + } else { + query = entityManager + .createNativeQuery("UPDATE clubevent_addon SET competition_type=:eventtype WHERE id=:eventId"); + } + + query.setParameter("eventId", obj.getId()); + query.setParameter("eventtype", type.getType().name()); + query.executeUpdate(); + } + } + } diff --git a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/data/ClubEvent.java b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/data/ClubEvent.java index 480af6e..afd56a9 100644 --- a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/data/ClubEvent.java +++ b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/data/ClubEvent.java @@ -96,7 +96,7 @@ public class ClubEvent extends BasicItem implements EntityAccessor { return iCalUID; } - public Type getCompetitionType() { + public Type getType() { if (competitionType != null) { return competitionType.getType(); } else { @@ -104,6 +104,18 @@ public class ClubEvent extends BasicItem implements EntityAccessor { } } + public CompetitionType getCompetitionType() { + return competitionType; + } + + public void setType(Type competitionType) { + if (this.competitionType == null) { + this.competitionType = new CompetitionType(); + this.competitionType.setClubEvent(this); + } + this.competitionType.setType(competitionType); + } + public void setCompetitionType(CompetitionType competitionType) { this.competitionType = competitionType; } diff --git a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/data/CompetitionType.java b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/data/CompetitionType.java index a39cd69..fcfbf20 100644 --- a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/data/CompetitionType.java +++ b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/data/CompetitionType.java @@ -1,17 +1,31 @@ package de.kreth.vaadin.clubhelper.vaadinclubhelper.data; +import java.io.Serializable; + +import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.MapsId; +import javax.persistence.OneToOne; import javax.persistence.Table; @Entity @Table(name = "clubevent_addon") -public class CompetitionType { +public class CompetitionType implements Serializable { + + private static final long serialVersionUID = -6198405472773618194L; @Id private String id; + @Column(name = "competition_type", nullable = false, length = 45) private String type; + @OneToOne(mappedBy = "competitionType") + @JoinColumn(name = "id") + @MapsId + private ClubEvent clubEvent; + public String getId() { return id; } @@ -28,7 +42,16 @@ public class CompetitionType { this.type = type.name(); } + public void setClubEvent(ClubEvent clubEvent) { + this.clubEvent = clubEvent; + } + public static enum Type { EINZEL, SYNCHRON, DOPPELMINI, MANNSCHAFT, LIGA } + + @Override + public String toString() { + return type; + } } diff --git a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/EventDetails.java b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/EventDetails.java index 0c18595..6a8bdcd 100644 --- a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/EventDetails.java +++ b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/EventDetails.java @@ -53,14 +53,16 @@ public class EventDetails extends GridLayout implements NamedView { Navigator navigator = event.getNavigator(); - eventView = new SingleEventView(); + eventView = new SingleEventView(true); + eventView.addDataUpdatedListener(() -> eventBusiness.storeEventType()); + + eventAltersgruppen = new EventAltersgruppen(pflichtenDao, eventBusiness); + eventView.addDataUpdatedListener(eventAltersgruppen); personGrid = new PersonGrid(groupDao, personDao); 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())); Button createMeldung = new Button("Meldung"); diff --git a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/MainView.java b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/MainView.java index 0b63a82..bd3196c 100644 --- a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/MainView.java +++ b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/MainView.java @@ -81,7 +81,7 @@ public class MainView extends BorderLayout implements NamedView { public void initUI(ViewChangeEvent event) { navigator = event.getNavigator(); - eventView = new SingleEventView(); + eventView = new SingleEventView(false); eventView.setVisible(false); personGrid = new PersonGrid(groupDao, personDao); diff --git a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/EventAltersgruppen.java b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/EventAltersgruppen.java index e24931a..ef69bb3 100644 --- a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/EventAltersgruppen.java +++ b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/EventAltersgruppen.java @@ -25,6 +25,7 @@ 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.Column; import com.vaadin.ui.Grid.SelectionMode; import com.vaadin.ui.HorizontalLayout; import com.vaadin.ui.TextField; @@ -36,9 +37,12 @@ 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.ClubEvent; +import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.CompetitionType; +import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.CompetitionType.Type; import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.Pflicht; +import de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.events.DataUpdatedEvent; -public class EventAltersgruppen extends VerticalLayout { +public class EventAltersgruppen extends VerticalLayout implements DataUpdatedEvent { private static final long serialVersionUID = -7777374233838542085L; private final Logger LOGGER = LoggerFactory.getLogger(getClass()); @@ -50,6 +54,7 @@ public class EventAltersgruppen extends VerticalLayout { private final ListDataProvider provider; private final Binder binder; private final EventBusiness eventBusiness; + private Column pflichtColumn; public EventAltersgruppen(PflichtenDao pflichtenDao, EventBusiness eventBusiness) { @@ -88,7 +93,9 @@ public class EventAltersgruppen extends VerticalLayout { 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); + pflichtColumn = gruppen.addColumn(Altersgruppe::getPflicht).setCaption("Pflicht") + .setEditorBinding(pflichtBinding); + updateFinisched(); binder.addValueChangeListener(this::valueChange); gruppen.addSelectionListener(this::selectionChange); @@ -250,4 +257,20 @@ public class EventAltersgruppen extends VerticalLayout { } } + + @Override + public void updateFinisched() { + Type type = currentType(); + pflichtColumn.setHidden(Type.DOPPELMINI == type); + } + + public Type currentType() { + ClubEvent current = eventBusiness.getCurrent(); + CompetitionType competitionType = current.getCompetitionType(); + if (competitionType == null) { + return null; + } + Type type = competitionType.getType(); + return type; + } } diff --git a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/SingleEventView.java b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/SingleEventView.java index 237a2cd..1b4be4f 100644 --- a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/SingleEventView.java +++ b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/SingleEventView.java @@ -1,14 +1,22 @@ package de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.components; +import java.time.LocalDate; import java.time.ZonedDateTime; import java.time.temporal.ChronoUnit; +import com.vaadin.data.Binder; +import com.vaadin.data.HasValue.ValueChangeEvent; +import com.vaadin.ui.ComboBox; import com.vaadin.ui.CustomComponent; import com.vaadin.ui.DateField; import com.vaadin.ui.GridLayout; import com.vaadin.ui.TextField; import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.ClubEvent; +import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.CompetitionType; +import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.CompetitionType.Type; +import de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.events.DataUpdatedEvent; +import de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.events.DefaultDataUpdateHandler; public class SingleEventView extends CustomComponent { @@ -20,8 +28,12 @@ public class SingleEventView extends CustomComponent { private DateField startDate; private DateField endDate; + private ComboBox competitionType; - public SingleEventView() { + private Binder binder; + private DefaultDataUpdateHandler updateHandler = new DefaultDataUpdateHandler(); + + public SingleEventView(boolean showCompetitionType) { setCaption("Gewählte Veranstaltung"); addStyleName("bold-caption"); setWidth(50.0f, Unit.PERCENTAGE); @@ -45,15 +57,57 @@ public class SingleEventView extends CustomComponent { endDate = new DateField("Ende"); endDate.setEnabled(false); + endDate.addValueChangeListener(this::endDateVisibleCheck); + textLocation.setHeight(endDate.getHeight(), endDate.getHeightUnits()); - GridLayout layout = new GridLayout(2, 2); + binder = new Binder<>(ClubEvent.class); + binder.forField(textTitle).bind(ClubEvent::getCaption, ClubEvent::setCaption); + binder.forField(textLocation).bind(ClubEvent::getLocation, ClubEvent::setLocation); + ZonedDateTimeConverter converter = new ZonedDateTimeConverter(); + + binder.forField(startDate).withConverter(converter).bind(ClubEvent::getStart, ClubEvent::setStart); + binder.forField(endDate).withConverter(converter).bind(ClubEvent::getEnd, ClubEvent::setEnd); + binder.addStatusChangeListener(ev -> updateHandler.fireUpdateEvent()); + + GridLayout layout; + if (showCompetitionType) { + competitionType = new ComboBox<>(); + competitionType.setItems(Type.values()); + binder.forField(competitionType).bind(ClubEvent::getType, ClubEvent::setType); + layout = new GridLayout(2, 3); + } else { + layout = new GridLayout(2, 2); + } layout.setMargin(true); layout.setSpacing(true); layout.addComponents(textTitle, startDate, textLocation, endDate); + if (showCompetitionType) { + layout.addComponent(competitionType); + } setCompositionRoot(layout); } + void endDateVisibleCheck(ValueChangeEvent event) { + ZonedDateTime start = binder.getBean().getStart(); + ZonedDateTime end = binder.getBean().getEnd(); + if (start.until(end, ChronoUnit.DAYS) > 0) { + endDate.setValue(end.toLocalDate()); + endDate.setVisible(true); + } else { + endDate.setValue(null); + endDate.setVisible(false); + } + } + + public void addDataUpdatedListener(DataUpdatedEvent ev) { + updateHandler.add(ev); + } + + public boolean remove(DataUpdatedEvent o) { + return updateHandler.remove(o); + } + void setTitle(String value) { if (value == null) { value = ""; @@ -70,12 +124,9 @@ public class SingleEventView extends CustomComponent { public void setEvent(ClubEvent ev) { - if (ev != null) { + binder.setBean(ev); - setTitle(ev.getCaption()); - setLocation(ev.getLocation()); - setStartDate(ev.getStart()); - setEndDate(ev); + if (ev != null) { } else { setTitle(""); @@ -84,20 +135,4 @@ public class SingleEventView extends CustomComponent { } } - private void setEndDate(ClubEvent ev) { - ZonedDateTime start = ev.getStart(); - ZonedDateTime end = ev.getEnd(); - if (start.until(end, ChronoUnit.DAYS) > 0) { - endDate.setValue(end.toLocalDate()); - endDate.setVisible(true); - } else { - endDate.setValue(null); - endDate.setVisible(false); - } - } - - private void setStartDate(ZonedDateTime start) { - startDate.setValue(start.toLocalDate()); - } - } diff --git a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/ZonedDateTimeConverter.java b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/ZonedDateTimeConverter.java new file mode 100644 index 0000000..e48e119 --- /dev/null +++ b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/ZonedDateTimeConverter.java @@ -0,0 +1,25 @@ +package de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.components; + +import java.time.LocalDate; +import java.time.LocalTime; +import java.time.ZoneId; +import java.time.ZonedDateTime; + +import com.vaadin.data.Converter; +import com.vaadin.data.Result; +import com.vaadin.data.ValueContext; + +public class ZonedDateTimeConverter implements Converter { + + private static final long serialVersionUID = 7275177272115739588L; + + @Override + public Result convertToModel(LocalDate value, ValueContext context) { + return Result.ok(ZonedDateTime.of(value, LocalTime.MIDNIGHT, ZoneId.systemDefault())); + } + + @Override + public LocalDate convertToPresentation(ZonedDateTime value, ValueContext context) { + return value.toLocalDate(); + } +} diff --git a/src/main/resources/schema/ClubEvent.hbm.xml b/src/main/resources/schema/ClubEvent.hbm.xml index e3335d8..cfc251e 100644 --- a/src/main/resources/schema/ClubEvent.hbm.xml +++ b/src/main/resources/schema/ClubEvent.hbm.xml @@ -34,6 +34,8 @@ + diff --git a/src/test/java/de/kreth/vaadin/clubhelper/AbstractHibernateConfiguration.java b/src/test/java/de/kreth/vaadin/clubhelper/AbstractHibernateConfiguration.java index d012d59..a1c66f3 100644 --- a/src/test/java/de/kreth/vaadin/clubhelper/AbstractHibernateConfiguration.java +++ b/src/test/java/de/kreth/vaadin/clubhelper/AbstractHibernateConfiguration.java @@ -8,6 +8,7 @@ import org.reflections.Reflections; import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.BaseEntity; import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.ClubeventHasPerson; +import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.CompetitionType; public abstract class AbstractHibernateConfiguration implements HibernateConfiguration { @@ -17,6 +18,7 @@ public abstract class AbstractHibernateConfiguration implements HibernateConfigu Reflections reflections = new Reflections("de.kreth.vaadin.clubhelper.vaadinclubhelper.data"); entityClasses = new HashSet<>(reflections.getSubTypesOf(BaseEntity.class)); entityClasses.add(ClubeventHasPerson.class); + entityClasses.add(CompetitionType.class); } @Override diff --git a/src/test/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/dao/ClubEventDataTest.java b/src/test/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/dao/ClubEventDataTest.java index 971aeb2..dfd001a 100644 --- a/src/test/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/dao/ClubEventDataTest.java +++ b/src/test/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/dao/ClubEventDataTest.java @@ -19,7 +19,6 @@ import org.springframework.test.context.ContextConfiguration; import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.ClubEvent; import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.ClubeventHasPerson; -import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.CompetitionType; import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.CompetitionType.Type; import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.Person; import de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.tests.TestConfiguration; @@ -43,20 +42,21 @@ public class ClubEventDataTest { public void testEventAddon() { ClubEvent ev = testDatabaseHelper.creteEvent(); - CompetitionType competitionType = new CompetitionType(); - competitionType.setType(Type.EINZEL); - ev.setCompetitionType(competitionType); + ev.setType(Type.EINZEL); - testDatabaseHelper.transactional(() -> entityManager.persist(ev)); + testDatabaseHelper.transactional(() -> { + entityManager.persist(ev); +// entityManager.persist(ev.getCompetitionType()); + }); List allClubEvent = testDatabaseHelper.allClubEvent(); assertEquals(1, allClubEvent.size()); - assertEquals(Type.EINZEL, allClubEvent.get(0).getCompetitionType()); + assertEquals(Type.EINZEL, allClubEvent.get(0).getType()); - competitionType.setType(Type.DOPPELMINI); + ev.setType(Type.DOPPELMINI); testDatabaseHelper.transactional(() -> entityManager.merge(ev)); allClubEvent = testDatabaseHelper.allClubEvent(); assertEquals(1, allClubEvent.size()); - assertEquals(Type.DOPPELMINI, allClubEvent.get(0).getCompetitionType()); + assertEquals(Type.DOPPELMINI, allClubEvent.get(0).getType()); } @Test