org.bwgz.swim.openlane.data.model.Event.java Source code

Java tutorial

Introduction

Here is the source code for org.bwgz.swim.openlane.data.model.Event.java

Source

/*
 * Copyright (C) 2012 bwgz.org
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 3 as 
 * published by the Free Software Foundation.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.bwgz.swim.openlane.data.model;

import java.io.Serializable;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.springframework.security.core.GrantedAuthority;

@Entity
@Table(name = "Events")
public class Event implements Serializable {
    private static final long serialVersionUID = -5420759364928911297L;

    private Long id;

    private String number;
    private Gender gender;
    private Long minAge;
    private Long maxAge;
    private Long distance;
    private Stroke stroke;

    private Meet meet;

    private Set<OpenLane> openLanes;
    private Set<Entry> entries;

    public Event() {
    }

    public Event(Long id) {
        this.id = id;
    }

    @Id
    @GeneratedValue
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getNumber() {
        return number;
    }

    public void setNumber(String number) {
        this.number = number;
    }

    @Enumerated(EnumType.STRING)
    public Gender getGender() {
        return gender;
    }

    public void setGender(Gender gender) {
        this.gender = gender;
    }

    public Long getMinAge() {
        return minAge;
    }

    public void setMinAge(Long minAge) {
        this.minAge = minAge;
    }

    public Long getMaxAge() {
        return maxAge;
    }

    public void setMaxAge(Long maxAge) {
        this.maxAge = maxAge;
    }

    public Long getDistance() {
        return distance;
    }

    public void setDistance(Long distance) {
        this.distance = distance;
    }

    public Stroke getStroke() {
        return stroke;
    }

    public void setStroke(Stroke stroke) {
        this.stroke = stroke;
    }

    @Transient
    public AgeGroup getAgeGroup() {
        return new AgeGroup(getMinAge(), getMaxAge());
    }

    public void setAgeGroup(AgeGroup ageGroup) {
        setMinAge(ageGroup.getMin());
        setMaxAge(ageGroup.getMax());
    }

    @ManyToOne(fetch = FetchType.EAGER)
    public Meet getMeet() {
        return meet;
    }

    public void setMeet(Meet meet) {
        this.meet = meet;
    }

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    public Set<OpenLane> getOpenLanes() {
        return openLanes;
    }

    public void setOpenLanes(Set<OpenLane> openLanes) {
        this.openLanes = openLanes;
    }

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    public Set<Entry> getEntries() {
        return entries;
    }

    public void setEntries(Set<Entry> entries) {
        this.entries = entries;
    }

    public OpenLane getOpenLane(Swimmer swimmer) {
        OpenLane result = null;

        for (OpenLane openLane : getOpenLanes()) {
            if (openLane.getSwimmer().getId().equals(swimmer.getId())) {
                result = openLane;
                break;
            }
        }

        return result;
    }

    public Boolean swimmerHasApplied(Swimmer swimmer) {
        return getOpenLane(swimmer) != null;
    }

    public Boolean swimmerHasBeenAccepted(Swimmer swimmer) {
        OpenLane openLane = getOpenLane(swimmer);

        return openLane != null ? openLane.getAccepted() != null : Boolean.FALSE;
    }

    public Entry getEntry(Swimmer swimmer) {
        Entry result = null;

        for (Entry entry : getEntries()) {
            if (entry.getSwimmer().getUsasId().equals(swimmer.getUsasId())) {
                result = entry;
                break;
            }
        }

        return result;
    }

    public Boolean swimmerHasEntered(Swimmer swimmer) {
        return getEntry(swimmer) != null;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append(super.toString()).append(": ");
        sb.append("Id: ").append(this.id).append("; ");
        sb.append("Number: ").append(this.number).append("; ");
        ;
        sb.append("Gender: ").append(this.gender).append("; ");
        sb.append("MinAge: ").append(this.minAge).append("; ");
        sb.append("MaxAge: ").append(this.maxAge).append("; ");
        sb.append("Distance: ").append(this.distance).append("; ");
        sb.append("Stroke: ").append(this.stroke).append("; ");
        sb.append("Distance: ").append(this.distance).append("; ");

        return sb.toString();
    }
}