City.java

package com.tradecloud.domain.place;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.tradecloud.common.base.StaticDataEntityBase;
import com.tradecloud.domain.common.IntegratedStaticDataEntityBase;

import javax.persistence.*;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

/**
 * City representation.
 */
@Entity
@Table(name = "city", uniqueConstraints = {@UniqueConstraint(columnNames = {"code"})})
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "City")
//@Cacheable(value = "city")
public class City extends IntegratedStaticDataEntityBase {

    /**
     * UID.
     */
    private static final long serialVersionUID = 1L;

    private static final int CITY_CODE_LENGTH = 3;

    @ManyToOne(fetch = FetchType.EAGER)
    @XmlElement(name = "Country")
    @JsonIgnore
    private Country country;

    public City() {
    }

    public City(Country country) {
        this.country = country;
    }

    public City(String cityCode, Country country) {
        this(cityCode, null, country);
    }

    public City(String cityCode, String cityName, Country country) {
        if (cityCode.length() != CITY_CODE_LENGTH) {
            throw new IllegalArgumentException("City code must be of length 3");
        }
        this.country = country;
        setCode(cityCode);
        setName(cityName);
    }

    public Country getCountry() {
        return country;
    }

    public void setCountry(Country country) {
        this.country = country;
    }

    @Override
    public int compareTo(StaticDataEntityBase o) {
        if (getName() != null && o.getName() != null) {
            return getName().compareToIgnoreCase(o.getName());
        }
        return 0;
    }
}