View Javadoc
1   /**
2    * Copyright 2005-2015 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.kuali.rice.krad.data.jpa.converters.BooleanYNConverter;
20  
21  import javax.persistence.Column;
22  import javax.persistence.Convert;
23  import javax.persistence.Embeddable;
24  import javax.persistence.Id;
25  import javax.persistence.MappedSuperclass;
26  
27  @MappedSuperclass
28  public class KualiCodeBase extends PersistableBusinessObjectBase implements KualiCode {
29  
30      private static final long serialVersionUID = 1194744068788100482L;
31  
32      /**
33       * EclipseLink static weaving does not weave MappedSuperclass unless an Entity or Embedded is
34       * weaved which uses it, hence this class.
35       */
36      @Embeddable
37      private static final class WeaveMe extends KualiCodeBase {}
38  
39      // Code and Name will be overridden by Column annotations in their children classes
40      @Id
41      @Column(name = "CODE", length=10)
42      protected String code;
43      @Column(name = "NM", length=40)
44      protected String name;
45  
46      @Column(name = "ACTV_IND")
47      @Convert(converter = BooleanYNConverter.class)
48      protected Boolean active;
49  
50      public KualiCodeBase() {
51          this.active = true;
52      }
53  
54      public KualiCodeBase(String code) {
55          this();
56          this.code = code;
57      }
58  
59      /**
60       * @return Getter for the Code.
61       */
62      @Override
63      public String getCode() {
64          return code;
65      }
66  
67      /**
68       * @param code - Setter for the Code.
69       */
70      @Override
71      public void setCode(String code) {
72          this.code = code;
73      }
74  
75  
76      /**
77       * @return Getter for the Name.
78       */
79      @Override
80      public String getName() {
81          return name;
82      }
83  
84  
85      /**
86       * @param name - Setter for the name.
87       */
88      @Override
89      public void setName(String name) {
90          this.name = name;
91      }
92  
93  
94      /**
95       * @return Getter for the active field.
96       */
97      @Override
98      public boolean isActive() {
99          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 }