View Javadoc

1   /*
2    * Copyright 2005-2008 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.kns.bo;
17  
18  import java.util.LinkedHashMap;
19  
20  import javax.persistence.Column;
21  import javax.persistence.Id;
22  import javax.persistence.MappedSuperclass;
23  
24  import org.apache.commons.lang.StringUtils;
25  import org.hibernate.annotations.Type;
26  
27  @MappedSuperclass
28  public class KualiCodeBase extends PersistableBusinessObjectBase implements KualiCode {
29  
30      private static final long serialVersionUID = 1194744068788100482L;
31  	// Code and Name will be overridden by Column annotations in their children classes
32      @Id
33      @Column(name="CODE")
34      protected String code;
35      @Column(name="NM")
36      protected String name;
37      @Type(type="yes_no")
38      @Column(name="ACTV_IND")
39      protected boolean active;
40  
41      public KualiCodeBase() {
42          this.active = true;
43      }
44  
45      public KualiCodeBase(String code) {
46          this();
47          this.code = code;
48      }
49  
50      /**
51       * @return Getter for the Code.
52       */
53      public String getCode() {
54          return code;
55      }
56  
57      /**
58       * @param code - Setter for the Code.
59       */
60      public void setCode(String code) {
61          this.code = code;
62      }
63  
64  
65      /**
66       * @return Getter for the Name.
67       */
68      public String getName() {
69          return name;
70      }
71  
72  
73      /**
74       * @param name - Setter for the name.
75       */
76      public void setName(String name) {
77          this.name = name;
78      }
79  
80  
81      /**
82       * @return Getter for the active field.
83       */
84      public boolean isActive() {
85          return active;
86      }
87  
88  
89      /**
90       * @param name - Setter for the active field.
91       */
92      public void setActive(boolean a) {
93          this.active = a;
94      }
95  
96      /**
97       * @return Returns the code and description in format: xx - xxxxxxxxxxxxxxxx
98       */
99      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 }