Skip to content

Commit

Permalink
#21 races can be added and removed from events.
Browse files Browse the repository at this point in the history
  • Loading branch information
britzke committed May 8, 2017
1 parent 99a0ef7 commit 953956a
Show file tree
Hide file tree
Showing 14 changed files with 467 additions and 227 deletions.
123 changes: 77 additions & 46 deletions src/main/java/de/rennspur/beans/RaceBean.java
Original file line number Diff line number Diff line change
@@ -1,42 +1,45 @@
/*
* This file is part of Renspur.
*
*
* Copyright (C) 2016 burghard.britzke [email protected]
*
*
* 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 <http://www.gnu.org/licenses/>.
*/
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:[email protected]
*/
@RequestScoped
Expand All @@ -47,42 +50,79 @@ public class RaceBean {
private EntityManager entityManager;

@Inject
@SelectedEvent
private Event selectedEvent;
private SelectedEventBean selectedEventBean;

private List<Race> 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();
}
}
Expand All @@ -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<Race> getRaces() {
System.out.println("RaceBean::getRaces(): " + races);
return races;
}

Expand All @@ -124,20 +171,4 @@ public List<Race> getRaces() {
public void setRaces(List<Race> 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;
}

}
}
6 changes: 6 additions & 0 deletions src/main/java/de/rennspur/beans/TeamBean.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -86,7 +87,12 @@ public String add() throws NoSuchAlgorithmException {
}
Team team = new Team(hash.toString());

List<Event> events = new ArrayList<Event>();
events.add(selectedEvent);
team.setEvents(events);

selectedTeamBean.setTeam(team);

return "team.xhtml?faces-redirect=true";
}

Expand Down
26 changes: 14 additions & 12 deletions src/main/java/de/rennspur/model/Race.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
/*
* This file is part of Renspur.
*
*
* Copyright (C) 2016 burghard.britzke, [email protected]
*
*
* 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 <http://www.gnu.org/licenses/>.
*/
Expand All @@ -33,20 +33,22 @@
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;
import javax.xml.bind.annotation.XmlTransient;

/**
* The persistent class for the RACES database table.
*
*
* @author burghard.britzke [email protected]
*/
@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"),
Expand All @@ -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;
Expand Down Expand Up @@ -133,7 +135,7 @@ public void setTeamPositions(List<TeamPosition> teamPositions) {

/**
* Add the given TeamPosition to the list of TeamPositions.
*
*
* @param teamPosition
* The TeamPosition to add.
* @return The added TeamPosition.
Expand All @@ -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.
Expand All @@ -169,7 +171,7 @@ public void setWaypoints(List<Waypoint> waypoints) {

/**
* Add a Waypoint to the list of Waypoints.
*
*
* @param waypoint
* The Waypoint to add.
* @return The added Waypoint.
Expand All @@ -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.
Expand All @@ -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() {
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/de/rennspur/model/Team.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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") })
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/translation_de.properties
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,15 @@ event=Veranstaltung
eventId=Event ID
eventName=Name
events=Veranstaltungen
finished=abgeschlossen
handicap=Handicap
handicapFactor=Handicap
hash=Hash
linkToRace=Live zum Rennen
name=Name
number=Nummer
outline=Übersicht
race=Rennen
races=Rennen
save=speichern
startDate=Start Datum
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/translation_en.properties
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,15 @@ event=Event
eventId=Event ID
eventName=Name
events=Events
finished=abgeschlossen
handicap=Handicap
handicapFactor=Handicap
hash=Hash
linkToRace=to Race (Live)
name=Name
number=Number
outline=Outline
race=Race
races=Races
save=Save
startDate=Start Date
Expand Down
3 changes: 1 addition & 2 deletions src/main/webapp/WEB-INF/web.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<param-name>primefaces.THEME</param-name>
<param-value>rennspur</param-value>
</context-param>

<context-param>
<param-name>org.jboss.weld.development</param-name>
<param-value>true</param-value>
Expand Down Expand Up @@ -48,6 +48,5 @@
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
<url-pattern>*.faces</url-pattern>
</servlet-mapping>
</web-app>
Loading

0 comments on commit 953956a

Please sign in to comment.