001    /**
002     * Copyright 2005-2014 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     */
016    package org.kuali.rice.location.impl.state;
017    
018    import org.kuali.rice.krad.bo.PersistableBusinessObjectBase;
019    import org.kuali.rice.krad.data.jpa.converters.BooleanYNConverter;
020    import org.kuali.rice.location.api.state.State;
021    import org.kuali.rice.location.framework.state.StateEbo;
022    import org.kuali.rice.location.impl.country.CountryBo;
023    
024    import javax.persistence.Column;
025    import javax.persistence.Convert;
026    import javax.persistence.Entity;
027    import javax.persistence.FetchType;
028    import javax.persistence.Id;
029    import javax.persistence.IdClass;
030    import javax.persistence.JoinColumn;
031    import javax.persistence.ManyToOne;
032    import javax.persistence.Table;
033    
034    @IdClass(StateId.class)
035    @Entity
036    @Table(name = "KRLC_ST_T")
037    public class StateBo extends PersistableBusinessObjectBase implements StateEbo {
038    
039        private static final long serialVersionUID = 6755670624476337736L;
040    
041        @Id
042        @Column(name = "POSTAL_STATE_CD")
043        private String code;
044    
045        @Id
046        @Column(name = "POSTAL_CNTRY_CD")
047        private String countryCode;
048    
049        @Column(name = "POSTAL_STATE_NM")
050        private String name;
051    
052        @Column(name = "ACTV_IND")
053        @Convert(converter = BooleanYNConverter.class)
054        private boolean active;
055    
056        @ManyToOne(targetEntity = CountryBo.class, fetch = FetchType.EAGER)
057        @JoinColumn(name = "POSTAL_CNTRY_CD", insertable = false, updatable = false)
058        private CountryBo country;
059    
060        @Override
061        public String getCode() {
062            return code;
063        }
064    
065        public void setCode(String code) {
066            this.code = code;
067        }
068    
069        @Override
070        public String getCountryCode() {
071            return countryCode;
072        }
073    
074        public void setCountryCode(String countryCode) {
075            this.countryCode = countryCode;
076        }
077    
078        @Override
079        public String getName() {
080            return name;
081        }
082    
083        public void setName(String name) {
084            this.name = name;
085        }
086    
087        @Override
088        public boolean isActive() {
089            return active;
090        }
091    
092        @Override
093        public void setActive(boolean active) {
094            this.active = active;
095        }
096    
097        public CountryBo getCountry() {
098            return country;
099        }
100    
101        public void setCountry(CountryBo country) {
102            this.country = country;
103        }
104    
105        /**
106         * Converts a mutable bo to its immutable counterpart
107         * @param bo the mutable business object
108         * @return An immutable State if the passed in mutable is not null.  If the mutable reference was null, then null
109         * is returned.
110         */
111        public static State to(StateBo bo) {
112            if (bo == null) {
113                return null;
114            }
115    
116            return State.Builder.create(bo).build();
117        }
118    
119        /**
120         * Converts a immutable object to its mutable counterpart
121         * @param im immutable object
122         * @return a new mutable CountryBo if the passed in immutable is not null.  If the immutable reference was null,
123         * then null is returned.
124         */
125        public static StateBo from(State im) {
126            if (im == null) {
127                return null;
128            }
129    
130            StateBo bo = new StateBo();
131            bo.code = im.getCode();
132            bo.countryCode = im.getCountryCode();
133            bo.name = im.getName();
134            bo.active = im.isActive();
135            bo.versionNumber = im.getVersionNumber();
136    
137            return bo;
138        }
139    
140    }
141