FoodWave.java 4.23 KB
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fi.insomnia.bortal.model;

import java.util.Calendar;
import java.util.Iterator;
import java.util.List;
import java.util.ArrayList;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.eclipse.persistence.annotations.OptimisticLocking;
import org.eclipse.persistence.annotations.OptimisticLockingType;

/**
 * 
 */
@Entity
@Table(name = "food_waves")
@OptimisticLocking(type = OptimisticLockingType.CHANGED_COLUMNS)
public class FoodWave extends GenericEntity {

    private static final long serialVersionUID = 9221716203467295049L;

    @Column(name = "wave_name", nullable = false)
    private String name;

    @Lob
    @Column(name = "wave_description")
    private String description;

    @Column(name = "wave_time")
    @Temporal(TemporalType.TIMESTAMP)
    private Calendar time;

    @Column(name = "max_foods")
    private Integer maximumFoods;

    @Column(name = "wave_closed", nullable = false, columnDefinition = "boolean default false")
    private boolean closed = false;

    @OneToMany(mappedBy = "foodWave")
    private List<AccountEvent> accountEvents;

    @OneToMany(mappedBy = "foodwave")
    private List<BillLine> billLines;
    
    @OneToMany(mappedBy = "foodwave")
    private List<BillLine> unpaidBills;

    @ManyToOne
    @JoinColumn(name = "template_id", referencedColumnName = "id", nullable = false)
    private FoodWaveTemplate template;

    public FoodWave() {
        super();
    }

    public FoodWave(String waveName, boolean waveClosed) {
        super();
        this.name = waveName;
        this.closed = waveClosed;
    }

    public String getName() {
        return name;
    }

    public void setName(String waveName) {
        this.name = waveName;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String waveDescription) {
        this.description = waveDescription;
    }

    public Calendar getTime() {
        return time;
    }

    public void setTime(Calendar waveTime) {
        this.time = waveTime;
    }

    public boolean getClosed() {
        return closed;
    }

    public boolean isClosed() {
        return closed;
    }

    public void setClosed(boolean waveClosed) {
        this.closed = waveClosed;
    }

    public List<AccountEvent> getAccountEvents() {
        return accountEvents;
    }

    public void setAccountEvents(List<AccountEvent> accountEventList) {
        this.accountEvents = accountEventList;
    }

    public void setTemplate(FoodWaveTemplate template) {
        this.template = template;
    }

    public FoodWaveTemplate getTemplate() {
        return template;
    }

    public boolean isFull() {
        return false;
    }

    /**
     * Check if foodwave is orderable
     * 
     * That means that it's not closed, full and it's in future
     * 
     * @return
     */
    public boolean isOrderable() {

        if (isClosed()) {
            return false;
        }

        if (getTime().before(Calendar.getInstance())) {
            return false;
        }

        if (isFull()) {
            return false;
        }

        return true;
    }

    public List<BillLine> getBillLines() {
        return billLines;
    }
    public Integer getMaximumFoods() {
        return maximumFoods;
    }

    public void setBillLines(List<BillLine> billLines) {
        this.billLines = billLines;
    }

    public int getReservedCount() {
        int retval = 0;

        if (getAccountEvents() != null) {
            retval += getAccountEvents().size();
        }

        if (getBillLines() != null) {
            retval += getBillLines().size();
        }

        return retval;
    }
    public void setMaximumFoods(Integer maximumFoods) {
        this.maximumFoods = maximumFoods;
    }

    public List<BillLine> getUnpaidBills() {
        return unpaidBills;
    }

    public void setUnpaidBills(List<BillLine> unpaidBills) {
        this.unpaidBills = unpaidBills;
    }

}