From 953956aa66da4de3b029b3d3d22923d03de9d75e Mon Sep 17 00:00:00 2001 From: "burghard.britzke" Date: Mon, 8 May 2017 06:55:31 +0200 Subject: [PATCH] #21 races can be added and removed from events. --- src/main/java/de/rennspur/beans/RaceBean.java | 123 +++++--- src/main/java/de/rennspur/beans/TeamBean.java | 6 + src/main/java/de/rennspur/model/Race.java | 26 +- src/main/java/de/rennspur/model/Team.java | 3 +- src/main/resources/translation_de.properties | 2 + src/main/resources/translation_en.properties | 2 + src/main/webapp/WEB-INF/web.xml | 3 +- src/main/webapp/admin/AddRace.xhtml | 25 -- src/main/webapp/admin/Clubs.xhtml | 2 +- src/main/webapp/admin/Races.xhtml | 24 +- src/main/webapp/admin/event.xhtml | 95 ++++-- src/main/webapp/admin/index.xhtml | 12 +- src/main/webapp/resources/js/rennspur.js | 271 ++++++++++++------ .../de/rennspur/test/beans/RaceBeanTest.java | 100 +++++-- 14 files changed, 467 insertions(+), 227 deletions(-) delete mode 100644 src/main/webapp/admin/AddRace.xhtml diff --git a/src/main/java/de/rennspur/beans/RaceBean.java b/src/main/java/de/rennspur/beans/RaceBean.java index 06f29b7..fa3e03b 100644 --- a/src/main/java/de/rennspur/beans/RaceBean.java +++ b/src/main/java/de/rennspur/beans/RaceBean.java @@ -1,42 +1,45 @@ /* * This file is part of Renspur. - * + * * Copyright (C) 2016 burghard.britzke bubi@charmides.in-berlin.de - * + * * Rennspur is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. - * + * * Rennspur is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. - * + * * You should have received a copy of the GNU Affero General Public License * along with Rennspur. If not, see . */ package de.rennspur.beans; +import java.io.IOException; import java.util.List; import javax.annotation.PostConstruct; import javax.enterprise.context.RequestScoped; +import javax.faces.context.FacesContext; +import javax.faces.event.ActionEvent; import javax.inject.Inject; import javax.inject.Named; import javax.persistence.EntityManager; import javax.persistence.EntityTransaction; -import javax.persistence.NoResultException; import javax.persistence.Query; -import de.rennspur.annotations.SelectedEvent; +import org.primefaces.event.SelectEvent; + import de.rennspur.model.Event; import de.rennspur.model.Race; /** * The RaceBean is a request scoped bean, which provides the list of races and * the actual race for the selected event, which is currently running - * + * * @author burghard.britzke mailto:bubi@charmides.in-berlin.de */ @RequestScoped @@ -47,42 +50,79 @@ public class RaceBean { private EntityManager entityManager; @Inject - @SelectedEvent - private Event selectedEvent; + private SelectedEventBean selectedEventBean; private List races; - private Race selectedRace; + /** + * Initializes the list of races with all races of the selected event. + * Merges the selected {@link Event}. + */ @SuppressWarnings("unchecked") @PostConstruct public void init() { - if (selectedEvent == null) { + if (selectedEventBean.getEvent() == null) { throw new IllegalArgumentException( "RaceBean cannot be instantiated without a selected event"); } Query q = entityManager.createNamedQuery("Race.findRacesByEvent"); - q.setParameter("event", selectedEvent); + q.setParameter("event", selectedEventBean.getEvent()); races = q.getResultList(); // make the selected event be managed again - selectedEvent = entityManager.merge(selectedEvent); + selectedEventBean + .setEvent(entityManager.merge(selectedEventBean.getEvent())); } /** - * Add an event to the events managed by this bean. - * - * @param event - * The event, which is to be added. - * @return The event, which has been added. + * Initializes the selectedRaceBean to a new {@link Race} and navigates to + * race.xhtml in order to enable the user to enter data for a race. + * + * @return */ - public void addRace() { - try { - EntityTransaction et = entityManager.getTransaction(); - et.begin(); + public void add(ActionEvent actionEvent) { + Race race = new Race(); + race.setEvent(selectedEventBean.getEvent()); + race.setNumber(races.size() + 1); + races.add(race); + selectedEventBean.getEvent().addRace(race); + + EntityTransaction transaction = entityManager.getTransaction(); + transaction.begin(); + selectedEventBean + .setEvent(entityManager.merge(selectedEventBean.getEvent())); + transaction.commit(); + } - entityManager.merge(selectedRace); - et.commit(); + /** + * Action method to remove a race from the races, managed by this bean. + * + * @return The event, which has been removed. + */ + public void removeLast(ActionEvent actionEvent) { + int lastIndex = races.size() - 1; + if (lastIndex >= 0) { + Race lastRace = races.get(lastIndex); + Event event = selectedEventBean.getEvent(); + EntityTransaction t = entityManager.getTransaction(); + t.begin(); + entityManager.remove(lastRace); + t.commit(); + event.removeRace(lastRace); + races.remove(lastRace); + } + } - } catch (NoResultException e) { + /** + * Handler for a double click on a row. + * + * @param event + * The primefaces SelectEvent for the selected race row. + */ + public void onDblselect(SelectEvent event) { + try { + FacesContext.getCurrentInstance().getExternalContext() + .redirect("race.xhtml"); + } catch (IOException e) { e.printStackTrace(); } } @@ -103,17 +143,24 @@ public void setEntityManager(EntityManager entityManager) { } /** - * @return the selectedEvent + * @return the selectedEventBean */ - public Event getSelectedEvent() { - return selectedEvent; + public SelectedEventBean getSelectedEventBean() { + return selectedEventBean; + } + + /** + * @param selectedEventBean + * the selectedEventBean to set + */ + public void setSelectedEventBean(SelectedEventBean selectedEventBean) { + this.selectedEventBean = selectedEventBean; } /** * @return the races */ public List getRaces() { - System.out.println("RaceBean::getRaces(): " + races); return races; } @@ -124,20 +171,4 @@ public List getRaces() { public void setRaces(List races) { this.races = races; } - - /** - * @return the selectedRace - */ - public Race getSelectedRace() { - return selectedRace; - } - - /** - * @param selectedRace - * the selectedRace to set - */ - public void setSelectedRace(Race selectedRace) { - this.selectedRace = selectedRace; - } - -} +} \ No newline at end of file diff --git a/src/main/java/de/rennspur/beans/TeamBean.java b/src/main/java/de/rennspur/beans/TeamBean.java index 9d0ebd5..450bef5 100644 --- a/src/main/java/de/rennspur/beans/TeamBean.java +++ b/src/main/java/de/rennspur/beans/TeamBean.java @@ -3,6 +3,7 @@ import java.io.IOException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; +import java.util.ArrayList; import java.util.Date; import java.util.List; @@ -86,7 +87,12 @@ public String add() throws NoSuchAlgorithmException { } Team team = new Team(hash.toString()); + List events = new ArrayList(); + events.add(selectedEvent); + team.setEvents(events); + selectedTeamBean.setTeam(team); + return "team.xhtml?faces-redirect=true"; } diff --git a/src/main/java/de/rennspur/model/Race.java b/src/main/java/de/rennspur/model/Race.java index 70c739d..b929875 100644 --- a/src/main/java/de/rennspur/model/Race.java +++ b/src/main/java/de/rennspur/model/Race.java @@ -1,18 +1,18 @@ /* * This file is part of Renspur. - * + * * Copyright (C) 2016 burghard.britzke, bubi@charmides.in-berlin.de - * + * * Rennspur is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. - * + * * Rennspur is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. - * + * * You should have received a copy of the GNU Affero General Public License * along with Rennspur. If not, see . */ @@ -33,6 +33,7 @@ import javax.persistence.NamedQuery; import javax.persistence.OneToMany; import javax.persistence.Table; +import javax.persistence.UniqueConstraint; import javax.validation.constraints.NotNull; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; @@ -40,13 +41,14 @@ /** * The persistent class for the RACES database table. - * + * * @author burghard.britzke bubi@charmides.in-berlin.de */ @XmlAccessorType(XmlAccessType.FIELD) @Entity -@Table(name = "RACES") +@Table(name = "RACES", uniqueConstraints = @UniqueConstraint(columnNames = { + "event","number" })) @NamedQueries({ @NamedQuery(name = "Race.findAll", query = "SELECT r FROM Race r order by r.number"), @NamedQuery(name = "Race.findRaceByID", query = "SELECT r FROM Race r WHERE r.id=:id"), @@ -67,7 +69,7 @@ public class Race implements Serializable { private boolean finished; // bi-directional many-to-one association to Event - @ManyToOne (fetch = FetchType.EAGER) + @ManyToOne(fetch = FetchType.EAGER) @NotNull @JoinColumn(name = "EVENTS_ID", nullable = false) private Event event; @@ -133,7 +135,7 @@ public void setTeamPositions(List teamPositions) { /** * Add the given TeamPosition to the list of TeamPositions. - * + * * @param teamPosition * The TeamPosition to add. * @return The added TeamPosition. @@ -147,7 +149,7 @@ public TeamPosition addTeamPosition(TeamPosition teamPosition) { /** * Removes the given TeamPosition from the list of TeamPositions. - * + * * @param teamPosition * The TeamPosition to be removed. * @return The removed TeamPosition. @@ -169,7 +171,7 @@ public void setWaypoints(List waypoints) { /** * Add a Waypoint to the list of Waypoints. - * + * * @param waypoint * The Waypoint to add. * @return The added Waypoint. @@ -183,7 +185,7 @@ public Waypoint addWaypoint(Waypoint waypoint) { /** * Removes a given Waypoint from the list of Waypoints. - * + * * @param waypoint * The Waypoint to remove. * @return The removed Waypoint. @@ -197,7 +199,7 @@ public Waypoint removeWaypoint(Waypoint waypoint) { /** * Converts the Race to a human readable string. - * + * * @see java.lang.Object#toString() */ public String toString() { diff --git a/src/main/java/de/rennspur/model/Team.java b/src/main/java/de/rennspur/model/Team.java index 07d1262..a1b38a8 100644 --- a/src/main/java/de/rennspur/model/Team.java +++ b/src/main/java/de/rennspur/model/Team.java @@ -24,7 +24,6 @@ import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.Entity; -import javax.persistence.FetchType; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; @@ -93,7 +92,7 @@ public class Team implements Serializable { @XmlTransient // bi-directional many-to-many association to Event - @ManyToMany + @ManyToMany (cascade=CascadeType.PERSIST) @JoinTable(name = "TEAM_EVENTS", joinColumns = { @JoinColumn(name = "TEAMS_ID") }, inverseJoinColumns = { @JoinColumn(name = "EVENTS_ID") }) diff --git a/src/main/resources/translation_de.properties b/src/main/resources/translation_de.properties index 6deb3f2..ea39b89 100644 --- a/src/main/resources/translation_de.properties +++ b/src/main/resources/translation_de.properties @@ -33,6 +33,7 @@ event=Veranstaltung eventId=Event ID eventName=Name events=Veranstaltungen +finished=abgeschlossen handicap=Handicap handicapFactor=Handicap hash=Hash @@ -40,6 +41,7 @@ linkToRace=Live zum Rennen name=Name number=Nummer outline=Übersicht +race=Rennen races=Rennen save=speichern startDate=Start Datum diff --git a/src/main/resources/translation_en.properties b/src/main/resources/translation_en.properties index 6a38943..700ac0f 100644 --- a/src/main/resources/translation_en.properties +++ b/src/main/resources/translation_en.properties @@ -33,6 +33,7 @@ event=Event eventId=Event ID eventName=Name events=Events +finished=abgeschlossen handicap=Handicap handicapFactor=Handicap hash=Hash @@ -40,6 +41,7 @@ linkToRace=to Race (Live) name=Name number=Number outline=Outline +race=Race races=Races save=Save startDate=Start Date diff --git a/src/main/webapp/WEB-INF/web.xml b/src/main/webapp/WEB-INF/web.xml index 3bd7531..f843daa 100644 --- a/src/main/webapp/WEB-INF/web.xml +++ b/src/main/webapp/WEB-INF/web.xml @@ -20,7 +20,7 @@ primefaces.THEME rennspur - + org.jboss.weld.development true @@ -48,6 +48,5 @@ Faces Servlet /faces/* - *.faces \ No newline at end of file diff --git a/src/main/webapp/admin/AddRace.xhtml b/src/main/webapp/admin/AddRace.xhtml deleted file mode 100644 index 40ea0d7..0000000 --- a/src/main/webapp/admin/AddRace.xhtml +++ /dev/null @@ -1,25 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/src/main/webapp/admin/Clubs.xhtml b/src/main/webapp/admin/Clubs.xhtml index fc89d1a..a00d033 100644 --- a/src/main/webapp/admin/Clubs.xhtml +++ b/src/main/webapp/admin/Clubs.xhtml @@ -41,7 +41,7 @@ - + diff --git a/src/main/webapp/admin/Races.xhtml b/src/main/webapp/admin/Races.xhtml index a8cb439..fde71ad 100644 --- a/src/main/webapp/admin/Races.xhtml +++ b/src/main/webapp/admin/Races.xhtml @@ -2,19 +2,19 @@ - + - + @@ -32,21 +51,35 @@ style="font-weight:bold" /> - - - - - + + - + + + #{translation.races} + + + + + + + + + + + + + @@ -61,6 +94,21 @@
diff --git a/src/main/webapp/admin/index.xhtml b/src/main/webapp/admin/index.xhtml index 4624722..2e62c34 100644 --- a/src/main/webapp/admin/index.xhtml +++ b/src/main/webapp/admin/index.xhtml @@ -2,24 +2,24 @@ - Rennspur + Rennspur <ui:insert name="title"/> diff --git a/src/main/webapp/resources/js/rennspur.js b/src/main/webapp/resources/js/rennspur.js index ce088ea..b75b01a 100644 --- a/src/main/webapp/resources/js/rennspur.js +++ b/src/main/webapp/resources/js/rennspur.js @@ -1,144 +1,243 @@ var navbar_initialized = false; -$(document).ready(function(){ - window_width = $(window).width(); - - // check if there is an image set for the sidebar's background - lbd.checkSidebarImage(); - - // Init navigation toggle for small screens - if(window_width <= 991){ - lbd.initRightMenu(); - } +$(document).ready( + function () { + window_width = $(window).width(); - // Activate the switches with icons - if($('.switch').length != 0){ - $('.switch')['bootstrapSwitch'](); - } - // Activate regular switches - if($("[data-toggle='switch']").length != 0){ - $("[data-toggle='switch']").wrap('
').parent().bootstrapSwitch(); - } - - $('.nav-tabs a #top').tab('show'); -}); + // check if there is an image set for the sidebar's background + lbd.checkSidebarImage(); + + // Init navigation toggle for small screens + if (window_width <= 991) { + lbd.initRightMenu(); + } -// activate collapse right menu when the windows is resized -$(window).resize(function(){ - if($(window).width() <= 991){ + // Activate the switches with icons + if ($('.switch').length != 0) { + $('.switch')['bootstrapSwitch'](); + } + // Activate regular switches + if ($("[data-toggle='switch']").length != 0) { + $("[data-toggle='switch']").wrap('
') + .parent().bootstrapSwitch(); + } + + $('.nav-tabs a #top').tab('show'); + }); + +// activate collapse right menu when the windows is resized +$(window).resize(function () { + if ($(window).width() <= 991) { lbd.initRightMenu(); } }); - + lbd = { - misc:{ - navbar_menu_visible: 0 + misc : { + navbar_menu_visible : 0 }, - - checkSidebarImage: function(){ + + checkSidebarImage : function () { $sidebar = $('.sidebar'); image_src = $sidebar.data('image'); - - if(image_src !== undefined){ - sidebar_container = '