package com.smartgwt.sample.server.dataModel;

import com.google.common.base.Strings;
import org.apache.commons.lang.StringUtils;
import org.hibernate.annotations.Index;

import javax.persistence.*;
import java.io.Serializable;

@Entity
@Table(name = "T_APPLICANT")
public class Applicant implements Serializable, Comparable<Applicant> {

    private static final long serialVersionUID = -3681549357808542156L;

    @Id
    @Column(nullable = false)
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Basic
    @Column(unique = true, length = 63)
    @Index(name = "applicantJobNumber_index")
    private String jobNumber;

    @Basic
    private String name;

    @ManyToOne
    @JoinColumn(name = "priceModelId", referencedColumnName = "id", nullable = true)
    private PriceModel priceModel;

    @Basic
    private int prorationRate = 100;

    @Override
    public String toString() {
        return "Applicant [" + "\n - jobNumber=" + jobNumber + "\n - id=" + id + "]";
    }

    @Override
    public boolean equals(Object obj) {
        if (obj instanceof Applicant) {
            // this.id = id;
            if (StringUtils.equals(jobNumber, ((Applicant) obj).jobNumber)
                    && StringUtils.equals(name, ((Applicant) obj).name)) {
                return true;
            }
        }
        return false;
    }

    /*----------------- Getter and setter --------------------*/
    public Long getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public String getJobNumber() {
        return jobNumber;
    }

    public void setJobNumber(String jobNumber) {
        this.jobNumber = jobNumber;
    }

    public PriceModel getPriceModel() {
        return priceModel;
    }

    public void setPriceModel(PriceModel priceModel) {
        this.priceModel = priceModel;
    }

    public int getProrationRate() {
        return prorationRate;
    }

    public void setProrationRate(int prorationRate) {
        this.prorationRate = prorationRate;
    }
    @Override
    public int compareTo(Applicant that) {
        final String thisName = Strings.nullToEmpty(this.getName());
        final String thatName = Strings.nullToEmpty(that.getName());
        return thisName.compareTo(thatName);
    }

}
