001/**
002 * Copyright 2005-2016 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.location.impl.postalcode;
017
018import org.kuali.rice.krad.bo.PersistableBusinessObjectBase;
019import org.kuali.rice.krad.data.jpa.converters.BooleanYNConverter;
020import org.kuali.rice.location.api.postalcode.PostalCode;
021import org.kuali.rice.location.framework.postalcode.PostalCodeEbo;
022import org.kuali.rice.location.impl.country.CountryBo;
023import org.kuali.rice.location.impl.county.CountyBo;
024import org.kuali.rice.location.impl.state.StateBo;
025
026import javax.persistence.Column;
027import javax.persistence.Convert;
028import javax.persistence.Entity;
029import javax.persistence.FetchType;
030import javax.persistence.Id;
031import javax.persistence.IdClass;
032import javax.persistence.JoinColumn;
033import javax.persistence.JoinColumns;
034import javax.persistence.ManyToOne;
035import javax.persistence.Table;
036
037@IdClass(PostalCodeId.class)
038@Entity
039@Table(name = "KRLC_PSTL_CD_T")
040public class PostalCodeBo extends PersistableBusinessObjectBase implements PostalCodeEbo {
041
042    private static final long serialVersionUID = 3951927731999264335L;
043
044    @Id
045    @Column(name = "POSTAL_CD")
046    private String code;
047
048    @Id
049    @Column(name = "POSTAL_CNTRY_CD")
050    private String countryCode;
051
052    @Column(name = "POSTAL_CITY_NM")
053    private String cityName;
054
055    @Column(name = "POSTAL_STATE_CD")
056    private String stateCode;
057
058    @Column(name = "COUNTY_CD")
059    private String countyCode;
060
061    @Column(name = "ACTV_IND")
062    @Convert(converter = BooleanYNConverter.class)
063    private boolean active;
064
065    @ManyToOne(targetEntity = CountryBo.class, fetch = FetchType.EAGER)
066    @JoinColumn(name = "POSTAL_CNTRY_CD", insertable = false, updatable = false)
067    private CountryBo country;
068
069    @ManyToOne(targetEntity = StateBo.class, fetch = FetchType.EAGER)
070    @JoinColumns(value = {
071            @JoinColumn(name = "POSTAL_STATE_CD", referencedColumnName="POSTAL_STATE_CD", insertable = false, updatable = false),
072            @JoinColumn(name = "POSTAL_CNTRY_CD", referencedColumnName="POSTAL_CNTRY_CD",insertable = false, updatable = false)
073    })
074    private StateBo state;
075
076    @ManyToOne(targetEntity = CountyBo.class, fetch = FetchType.EAGER)
077    @JoinColumns(value = {
078            @JoinColumn(name = "COUNTY_CD", referencedColumnName="COUNTY_CD", insertable = false, updatable = false),
079            @JoinColumn(name="POSTAL_STATE_CD", referencedColumnName="STATE_CD", insertable = false, updatable = false),
080            @JoinColumn(name="POSTAL_CNTRY_CD", referencedColumnName="POSTAL_CNTRY_CD", insertable = false, updatable = false)
081    })
082    private CountyBo county;
083
084    @Override
085    public String getCode() {
086        return code;
087    }
088
089    public void setCode(String code) {
090        this.code = code;
091    }
092
093    @Override
094    public String getCountryCode() {
095        return countryCode;
096    }
097
098    public void setCountryCode(String countryCode) {
099        this.countryCode = countryCode;
100    }
101
102    @Override
103    public String getCityName() {
104        return cityName;
105    }
106
107    public void setCityName(String cityName) {
108        this.cityName = cityName;
109    }
110
111    @Override
112    public String getStateCode() {
113        return stateCode;
114    }
115
116    public void setStateCode(String stateCode) {
117        this.stateCode = stateCode;
118    }
119
120    @Override
121    public String getCountyCode() {
122        return countyCode;
123    }
124
125    public void setCountyCode(String countyCode) {
126        this.countyCode = countyCode;
127    }
128
129    @Override
130    public boolean isActive() {
131        return active;
132    }
133
134    @Override
135    public void setActive(boolean active) {
136        this.active = active;
137    }
138
139    public CountryBo getCountry() {
140        return country;
141    }
142
143    public void setCountry(CountryBo country) {
144        this.country = country;
145    }
146
147    public StateBo getState() {
148        return state;
149    }
150
151    public void setState(StateBo state) {
152        this.state = state;
153    }
154
155    public CountyBo getCounty() {
156        return county;
157    }
158
159    public void setCounty(CountyBo county) {
160        this.county = county;
161    }
162
163    /**
164     * Converts a mutable bo to its immutable counterpart
165     * @param bo the mutable business object
166     * @return An immutable PostalCode if the passed in mutable is not null.  If the mutable reference was null, then
167     * null is returned.
168     */
169    public static PostalCode to(PostalCodeBo bo) {
170        if (bo == null) {
171            return null;
172        }
173
174        return PostalCode.Builder.create(bo).build();
175    }
176
177    /**
178     * Converts a immutable object to its mutable counterpart
179     * @param im immutable object
180     * @return a new mutable PostalCodeBo if the passed in mutable is not null.  If the immutable reference was null,
181     * then null is returned.
182     */
183    public static PostalCodeBo from(PostalCode im) {
184        if (im == null) {
185            return null;
186        }
187
188        PostalCodeBo bo = new PostalCodeBo();
189        bo.code = im.getCode();
190        bo.countryCode = im.getCountryCode();
191        bo.cityName = im.getCityName();
192        bo.active = im.isActive();
193        bo.stateCode = im.getStateCode();
194        bo.cityName = im.getCityName();
195        bo.versionNumber = im.getVersionNumber();
196
197        return bo;
198    }
199}
200