001    /**
002     * Copyright 2005-2013 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.krad.bo;
017    
018    import org.apache.commons.lang.StringUtils;
019    import org.hibernate.annotations.Type;
020    
021    import javax.persistence.Column;
022    import javax.persistence.Id;
023    import javax.persistence.MappedSuperclass;
024    
025    @MappedSuperclass
026    public class KualiCodeBase extends PersistableBusinessObjectBase implements KualiCode {
027    
028        private static final long serialVersionUID = 1194744068788100482L;
029            // Code and Name will be overridden by Column annotations in their children classes
030        @Id
031        @Column(name="CODE")
032        protected String code;
033        @Column(name="NM")
034        protected String name;
035        @Type(type="yes_no")
036        @Column(name="ACTV_IND")
037        protected boolean active;
038    
039        public KualiCodeBase() {
040            this.active = true;
041        }
042    
043        public KualiCodeBase(String code) {
044            this();
045            this.code = code;
046        }
047    
048        /**
049         * @return Getter for the Code.
050         */
051        public String getCode() {
052            return code;
053        }
054    
055        /**
056         * @param code - Setter for the Code.
057         */
058        public void setCode(String code) {
059            this.code = code;
060        }
061    
062    
063        /**
064         * @return Getter for the Name.
065         */
066        public String getName() {
067            return name;
068        }
069    
070    
071        /**
072         * @param name - Setter for the name.
073         */
074        public void setName(String name) {
075            this.name = name;
076        }
077    
078    
079        /**
080         * @return Getter for the active field.
081         */
082        public boolean isActive() {
083            return active;
084        }
085    
086    
087        /**
088         * @param name - Setter for the active field.
089         */
090        public void setActive(boolean a) {
091            this.active = a;
092        }
093    
094        /**
095         * @return Returns the code and description in format: xx - xxxxxxxxxxxxxxxx
096         */
097        public String getCodeAndDescription() { 
098            return KualiCodeBase.getCodeAndDescription(getCode(), getName()); 
099        } 
100    
101        /**
102         * Static helper method to allow other classes to provide consistent "code and description"
103         * behavior, even if not extending from this class.
104         */
105            public static String getCodeAndDescription(String code, String desc) {
106                    if (code != null) {
107                            if (desc == null) {
108                                    return code;
109                            } else {
110                                    return code + " - " + desc;
111                            }
112                    }
113                    return "";
114            }
115    
116        /**
117         * Implements equals comparing code to code.
118         * 
119         * @see java.lang.Object#equals(java.lang.Object)
120         */
121        public boolean equals(Object obj) {
122            if (obj instanceof KualiCodeBase) {
123                return StringUtils.equals(this.getCode(), ((KualiCodeBase) obj).getCode());
124            }
125            return false;
126        }
127    
128        /**
129         * Overriding equals requires writing a hashCode method.
130         * 
131         * @see java.lang.Object#hashCode()
132         */
133        public int hashCode() {
134            int hashCode = 0;
135    
136            if (getCode() != null) {
137                hashCode = getCode().hashCode();
138            }
139    
140            return hashCode;
141        }
142    }