package com.bloatit.model.data;

import java.math.BigDecimal;
import java.util.Date;

import javax.persistence.Basic;
import javax.persistence.Entity;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;

import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;
import org.hibernate.search.annotations.DateBridge;
import org.hibernate.search.annotations.Field;
import org.hibernate.search.annotations.Index;
import org.hibernate.search.annotations.IndexedEmbedded;
import org.hibernate.search.annotations.Resolution;
import org.hibernate.search.annotations.Store;

import com.bloatit.model.data.util.NonOptionalParameterException;

/**
 * An offer is a developer offer to a demand.
 */
@Entity
public final class DaoOffer extends DaoKudosable {

    /**
     * This is demand on which this offer is done.
     */
    @ManyToOne(optional = false)
    private DaoDemand demand;

    /**
     * Remember a description is a title with some content. (Translatable)
     */
    @OneToOne
    @Cascade(value = { CascadeType.ALL })
    @IndexedEmbedded
    private DaoDescription description;

    /**
     * Because the 'lot' is not already implemented I added a dateExpire field. It is
     * really not very useful...
     */
    @Basic(optional = false)
    @Field(index = Index.UN_TOKENIZED, store = Store.YES)
    @DateBridge(resolution = Resolution.DAY)
    private Date dateExpire;

    /**
     * The amount represents the money the member want to have to make his offer. (this
     * will also probably will be modified by the 'lot' implementation)
     */
    @Basic(optional = false)
    private BigDecimal amount;

    // TODO comment (don't forget the clone)
    public DaoOffer(final DaoMember member, final DaoDemand demand, final BigDecimal amount, final DaoDescription text, final Date dateExpire) {
        super(member);
        if (demand == null || text == null || dateExpire == null) {
            throw new NonOptionalParameterException();
        }
        this.demand = demand;
        this.description = text;
        this.dateExpire = (Date) dateExpire.clone();
        this.amount = amount;
    }

    public Date getDateExpire() {
        return (Date) dateExpire.clone();
    }

    public void setDateExpire(final Date dateExpire) {
        this.dateExpire = (Date) dateExpire.clone();
    }

    public DaoDemand getDemand() {
        return demand;
    }

    public DaoDescription getDescription() {
        return description;
    }

    public BigDecimal getAmount() {
        return amount;
    }
    
    // ======================================================================
    // For hibernate mapping
    // ======================================================================

    protected DaoOffer() {
        super();
    }
}

@@ -603,24 +603,71 @@
 ons.Store;%0A%0A
+import com.bloatit.common.FatalErrorException;%0A
 import com.b
@@ -720,16 +720,16 @@
 eption;%0A
-
 %0A/**%0A * 
@@ -1718,47 +1718,627 @@
    /
-/ TODO comment (don't forget the clone)
+**%0A     * Create a DaoOffer.%0A     *%0A     * @param member is the author of the offer. Must be non null.%0A     * @param demand is the demand on which this offer is made. Must be non null.%0A     * @param amount is the amount of the offer. Must be non null, and %3E 0.%0A     * @param text is the description of the demand. Must be non null.%0A     * @param dateExpire is the date when this offer should be finish. Must be non null,%0A     *        and in the future.%0A     * @throws NonOptionalParameterException if a parameter is null.%0A     * @throws FatalErrorException if the amount is %3C 0 or if the Date is in the future.%0A     */
 %0A   
@@ -2631,24 +2631,290 @@
 ;%0A        %7D%0A
+        if (amount.compareTo(BigDecimal.ZERO) %3C 0) %7B%0A            throw new FatalErrorException(%22Amount must be %3E 0%22);%0A        %7D%0A        if (dateExpire.before(new Date())) %7B%0A            throw new FatalErrorException(%22Make sure the date is in the future.%22);%0A        %7D%0A
         this
@@ -3462,20 +3462,16 @@
 ;%0A    %7D%0A
-    
 %0A    // 

fix the DaoOffer bug on negative value