View Javadoc

1   /**
2    * Copyright 2005-2011 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.krad.bo;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.hibernate.annotations.Type;
20  
21  import javax.persistence.Column;
22  import javax.persistence.Id;
23  import javax.persistence.MappedSuperclass;
24  
25  @MappedSuperclass
26  public class KualiCodeBase extends PersistableBusinessObjectBase implements KualiCode {
27  
28      private static final long serialVersionUID = 1194744068788100482L;
29  	// Code and Name will be overridden by Column annotations in their children classes
30      @Id
31      @Column(name="CODE")
32      protected String code;
33      @Column(name="NM")
34      protected String name;
35      @Type(type="yes_no")
36      @Column(name="ACTV_IND")
37      protected boolean active;
38  
39      public KualiCodeBase() {
40          this.active = true;
41      }
42  
43      public KualiCodeBase(String code) {
44          this();
45          this.code = code;
46      }
47  
48      /**
49       * @return Getter for the Code.
50       */
51      public String getCode() {
52          return code;
53      }
54  
55      /**
56       * @param code - Setter for the Code.
57       */
58      public void setCode(String code) {
59          this.code = code;
60      }
61  
62  
63      /**
64       * @return Getter for the Name.
65       */
66      public String getName() {
67          return name;
68      }
69  
70  
71      /**
72       * @param name - Setter for the name.
73       */
74      public void setName(String name) {
75          this.name = name;
76      }
77  
78  
79      /**
80       * @return Getter for the active field.
81       */
82      public boolean isActive() {
83          return active;
84      }
85  
86  
87      /**
88       * @param name - Setter for the active field.
89       */
90      public void setActive(boolean a) {
91          this.active = a;
92      }
93  
94      /**
95       * @return Returns the code and description in format: xx - xxxxxxxxxxxxxxxx
96       */
97      public String getCodeAndDescription() { 
98      	return KualiCodeBase.getCodeAndDescription(getCode(), getName()); 
99      } 
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 }