SwiftCompliantAddress.java

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.tradecloud.domain.supplier;

import com.tradecloud.common.base.PersistenceBase;
import com.tradecloud.domain.place.City;
import com.tradecloud.domain.place.Country;
import org.hibernate.annotations.ForeignKey;

import javax.persistence.Entity;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.validation.constraints.Pattern;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

/**
 * @author Fred
 */
@Entity
@Table(name = "swiftcompliantaddress")
@XmlRootElement(name = "SwiftCompliantAddress")
public class SwiftCompliantAddress extends PersistenceBase implements Comparable<SwiftCompliantAddress> {

    //  Maximum of 35 swift characters
    @ManyToOne
    @XmlElement(name = "City")
    @ForeignKey(name = "fk_city")
    private City city;

    //  Maximum of 35 swift characters
    @ManyToOne
    @XmlElement(name = "Country")
    @ForeignKey(name = "fk_country")
    private Country country;

    //  Maximum of 35 swift characters
    @Pattern(regexp = "([a-zA-Z_0-9\\s\\/\\-\\?\\:\\(\\)\\.\\,\\'\\+\\{\\}]*$){1,35}",
            message = "[Address Name Can only contain:: (a-z) and (A-Z) and These special charactors (/ - ? : ( ) . , ' + { })]")
    private String name;

    //  Maximum of 35 swift characters / - ? : ( ) . , ' + { }
    @XmlAttribute
    @Pattern(regexp = "([a-zA-Z_0-9\\s\\/\\-\\?\\:\\(\\)\\.\\,\\'\\+\\{\\}]*$){1,35}",
            message = "[Address Line 1 Can only contain:: (a-z) and (A-Z) and These special charactors (/ - ? : ( ) . , ' + { })]")
    private String addressLine1;

    //  Maximum of 35 swift characters
    @XmlAttribute
    @Pattern(regexp = "([a-zA-Z_0-9\\s\\/\\-\\?\\:\\(\\)\\.\\,\\'\\+\\{\\}]*$){1,35}",
            message = "[Address Line 2 Can only contain:: (a-z) and (A-Z) and These special charactors (/ - ? : ( ) . , ' + { })]")
    private String addressLine2;

    //  Maximum of 35 swift characters
    @XmlAttribute
    @Pattern(regexp = "([a-zA-Z_0-9\\s\\/\\-\\?\\:\\(\\)\\.\\,\\'\\+\\{\\}]*$){1,35}",
            message = "[Address Line 3 Can only contain:: (a-z) and (A-Z) and These special charactors (/ - ? : ( ) . , ' + { })]")
    private String addressLine3;

    //  Maximum of 35 swift characters
    @XmlAttribute
    @Pattern(regexp = "([a-zA-Z_0-9\\s\\/\\-\\?\\:\\(\\)\\.\\,\\'\\+\\{\\}]*$){1,35}",
            message = "[Postal Code Can only contain:: (a-z) and (A-Z) and These special charactors (/ - ? : ( ) . , ' + { })]")
    private String postCode;

    @Override
    public int compareTo(SwiftCompliantAddress sca) {
        if (getAddressLine1().compareToIgnoreCase(sca.getAddressLine1()) != 0) {
            return getAddressLine1().compareToIgnoreCase(getAddressLine1());
        }
        if (getCity().compareTo(sca.getCity()) != 0) {
            return getCity().compareTo(getCity());
        }
        if (getCountry().compareTo(sca.getCountry()) != 0) {
            return getCountry().compareTo(sca.getCountry());
        }
        if (getAddressLine2().compareToIgnoreCase(sca.getAddressLine2()) != 0) {
            return getAddressLine2().compareToIgnoreCase(getAddressLine2());
        }
        if (getAddressLine3().compareToIgnoreCase(sca.getAddressLine3()) != 0) {
            return getAddressLine3().compareToIgnoreCase(getAddressLine3());
        }
        if (getPostCode().compareToIgnoreCase(sca.getPostCode()) != 0) {
            return getPostCode().compareToIgnoreCase(getPostCode());
        }
        // All fields are equal
        return 0;
    }

    /**
     * @return the city
     */
    public City getCity() {
        return city;
    }

    /**
     * @param city the city to set
     */
    public void setCity(City city) {
        this.city = city;
    }

    /**
     * @return the country
     */
    public Country getCountry() {
        return country;
    }

    /**
     * @param country the country to set
     */
    public void setCountry(Country country) {
        this.country = country;
    }

    /**
     * @return the addressLine1
     */
    public String getAddressLine1() {
        return addressLine1;
    }

    /**
     * @param addressLine1 the addressLine1 to set
     */
    public void setAddressLine1(String addressLine1) {
        this.addressLine1 = addressLine1;
    }

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @return the addressLine2
     */
    public String getAddressLine2() {
        return addressLine2;
    }

    /**
     * @param addressLine2 the addressLine2 to set
     */
    public void setAddressLine2(String addressLine2) {
        this.addressLine2 = addressLine2;
    }

    /**
     * @return the addressLine3
     */
    public String getAddressLine3() {
        return addressLine3;
    }

    /**
     * @param addressLine3 the addressLine3 to set
     */
    public void setAddressLine3(String addressLine3) {
        this.addressLine3 = addressLine3;
    }

    /**
     * @return the postCode
     */
    public String getPostCode() {
        return postCode;
    }

    /**
     * @param postCode the postCode to set
     */
    public void setPostCode(String postCode) {
        this.postCode = postCode;
    }

    /**
     * Pretty print the address so that it prints as you'd read it on a single line. Used to simplify the UI
     *
     * @return Returns the address on a single line
     */
    public String prettyPrint() {
        StringBuilder builder = new StringBuilder();
        appendIfNotNull(name, builder);
        appendIfNotNull(addressLine1, builder);
        appendIfNotNull(addressLine2, builder);
        appendIfNotNull(addressLine3, builder);
        if (city != null) {
            appendIfNotNull(city.getName(), builder);
        }
        appendIfNotNull(postCode, builder);
        if (country != null) {
            appendIfNotNull(country.getName(), builder);
        }
        return builder.toString();
    }

    public String prettyPrintNoName() {
        StringBuilder builder = new StringBuilder();
        appendIfNotNull(name, builder);
        appendIfNotNull(addressLine1, builder);
        appendIfNotNull(addressLine2, builder);
        appendIfNotNull(addressLine3, builder);
        if (city != null) {
            appendIfNotNull(city.getName(), builder);
        }
        appendIfNotNull(postCode, builder);
        if (country != null) {
            appendIfNotNull(country.getName(), builder);
        }
        return builder.toString();
    }

    private void appendIfNotNull(String stringToAdd, StringBuilder builder) {
        if (stringToAdd != null) {
            if (builder.length() > 0) {
                builder.append(", ");
            }
            builder.append(stringToAdd);
        }
    }

    /**
     * Checks if any of the values are populated.
     *
     * @return boolean true if any of the values are populated, false otherwise
     */
    public boolean isPopulated() {
        if (addressLine1 == null && addressLine2 == null && addressLine3 == null && city == null && postCode == null
                && country == null) {
            return false;
        }
        return true;
    }
}