PlaceOfCustom.java

package com.tradecloud.domain.place;

import com.tradecloud.domain.common.IntegratedStaticDataEntityBase;
import com.tradecloud.domain.exception.InvalidEntityException;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
import java.util.List;

/**
 * Created by ds on 2015/04/09.
 */
@Entity
@Table(name = "placeofcustom")
@XmlRootElement(name = "PlaceOfCustom")
public class PlaceOfCustom extends IntegratedStaticDataEntityBase {

    private static final long serialVersionUID = 1L;

    @XmlType(name = "PlaceOfCustomType", namespace = "http://www.tradecloud.com/schema/place")
    public enum Type {
        ENTRY("Entry"), EXIT("Exit"), ENTRY_AND_EXIT("Entry and Exit"), CLEARING_TERMINAL("Clearing Terminal");
        private final String value;

        Type(String v) {
            value = v;
        }

        public String value() {
            return value;
        }

        public static Type fromValue(String v) {
            for (Type c : Type.values()) {
                if (c.value.equals(v)) {
                    return c;
                }
            }
            throw new IllegalArgumentException(v);
        }
    }

    @Enumerated(EnumType.STRING)
    @NotNull
    private Type type = Type.EXIT;

    @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.DETACH)
    private List<PlaceOfCustom> districtOffices;

    @Override
    public void setCode(String code) {
        if (this.getType() == null) {
            throw new InvalidEntityException("placeofcustom.Type is required");
        }
        if (code == null) {
            throw new InvalidEntityException("placeofcustom.Code is required");
        }
        if (type.equals(Type.CLEARING_TERMINAL) && code.length() != 2) {
            throw new InvalidEntityException("placeofcustom.Code length for " + type.value() + " must be = 2");
        }
        if ((type.equals(Type.ENTRY) || type.equals(Type.EXIT) || type.equals(Type.ENTRY_AND_EXIT)) && code.length() != 3) {
            throw new InvalidEntityException("placeofcustom.Code length for " + type.value() + " must be = 3");
        }
        super.setCode(code);
    }

    public PlaceOfCustom() {
    }

    public PlaceOfCustom(String code, String name) {
        setCode(code);
        setName(name);
        setActive(true);
    }

    public Type getType() {
        return type;
    }

    public void setType(Type type) {
        this.type = type;
    }

    public List<PlaceOfCustom> getDistrictOffices() {
        return districtOffices;
    }

    public void setDistrictOffices(List<PlaceOfCustom> districtOffices) {
        this.districtOffices = districtOffices;
    }

    @Override
    public boolean equals(Object obj) {
        return super.equals(obj);
    }
}