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