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