-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#21 races can be added and removed from events.
- Loading branch information
Showing
14 changed files
with
467 additions
and
227 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
@@ -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(); | ||
} | ||
} | ||
|
@@ -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; | ||
} | ||
|
||
|
@@ -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; | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/>. | ||
*/ | ||
|
@@ -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"), | ||
|
@@ -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<TeamPosition> 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<Waypoint> 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() { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.