/shareddata/api/src/main/java/org/kuali/rice/shareddata/api/county/County.java

https://github.com/sbower/kuali-rice-1 · Java · 304 lines · 184 code · 46 blank · 74 comment · 4 complexity · 76597d2694b132321211bc4c814793eb MD5 · raw file

  1. /*
  2. * Copyright 2006-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.shareddata.api.county;
  17. import org.apache.commons.lang.StringUtils;
  18. import org.kuali.rice.core.api.CoreConstants;
  19. import org.kuali.rice.core.api.mo.AbstractDataTransferObject;
  20. import org.kuali.rice.core.api.mo.ModelBuilder;
  21. import org.kuali.rice.shareddata.api.SharedDataConstants;
  22. import org.w3c.dom.Element;
  23. import javax.xml.bind.annotation.XmlAccessType;
  24. import javax.xml.bind.annotation.XmlAccessorType;
  25. import javax.xml.bind.annotation.XmlAnyElement;
  26. import javax.xml.bind.annotation.XmlElement;
  27. import javax.xml.bind.annotation.XmlRootElement;
  28. import javax.xml.bind.annotation.XmlType;
  29. import java.io.Serializable;
  30. import java.util.Collection;
  31. /**
  32. * An immutable representation of a {@link CountyContract}.
  33. *
  34. * <p>To construct an instance of a County, use the {@link County.Builder} class.
  35. *
  36. * @see CountyContract
  37. * @author Kuali Rice Team (rice.collab@kuali.org)
  38. */
  39. @XmlRootElement(name = County.Constants.ROOT_ELEMENT_NAME)
  40. @XmlAccessorType(XmlAccessType.NONE)
  41. @XmlType(name = County.Constants.TYPE_NAME, propOrder = {
  42. County.Elements.CODE,
  43. County.Elements.NAME,
  44. County.Elements.COUNTRY_CODE,
  45. County.Elements.STATE_CODE,
  46. County.Elements.ACTIVE,
  47. CoreConstants.CommonElements.VERSION_NUMBER,
  48. CoreConstants.CommonElements.FUTURE_ELEMENTS
  49. })
  50. public final class County extends AbstractDataTransferObject implements CountyContract {
  51. private static final long serialVersionUID = 6097498602725305353L;
  52. @XmlElement(name = Elements.CODE, required = true)
  53. private final String code;
  54. @XmlElement(name = Elements.NAME, required = true)
  55. private final String name;
  56. @XmlElement(name = Elements.COUNTRY_CODE, required = true)
  57. private final String countryCode;
  58. @XmlElement(name = Elements.STATE_CODE, required = true)
  59. private final String stateCode;
  60. @XmlElement(name = Elements.ACTIVE, required = true)
  61. private final boolean active;
  62. @XmlElement(name = CoreConstants.CommonElements.VERSION_NUMBER, required = false)
  63. private final Long versionNumber;
  64. @SuppressWarnings("unused")
  65. @XmlAnyElement
  66. private final Collection<Element> _futureElements = null;
  67. /**
  68. * This constructor should never be called except during JAXB unmarshalling.
  69. */
  70. @SuppressWarnings("unused")
  71. private County() {
  72. this.code = null;
  73. this.name = null;
  74. this.countryCode = null;
  75. this.stateCode = null;
  76. this.active = false;
  77. this.versionNumber = null;
  78. }
  79. private County(Builder builder) {
  80. code = builder.getCode();
  81. name = builder.getName();
  82. countryCode = builder.getCountryCode();
  83. stateCode = builder.getStateCode();
  84. active = builder.isActive();
  85. versionNumber = builder.getVersionNumber();
  86. }
  87. /** {@inheritDoc} */
  88. @Override
  89. public String getCode() {
  90. return code;
  91. }
  92. /** {@inheritDoc} */
  93. @Override
  94. public String getName() {
  95. return name;
  96. }
  97. /** {@inheritDoc} */
  98. @Override
  99. public String getCountryCode() {
  100. return countryCode;
  101. }
  102. /** {@inheritDoc} */
  103. @Override
  104. public String getStateCode() {
  105. return stateCode;
  106. }
  107. /** {@inheritDoc} */
  108. @Override
  109. public boolean isActive() {
  110. return active;
  111. }
  112. /** {@inheritDoc} */
  113. @Override
  114. public Long getVersionNumber() {
  115. return versionNumber;
  116. }
  117. /**
  118. * This builder constructs an County enforcing the constraints of the {@link CountyContract}.
  119. */
  120. public static class Builder implements CountyContract, ModelBuilder, Serializable {
  121. private static final long serialVersionUID = 7077484401017765844L;
  122. private String code;
  123. private String name;
  124. private String countryCode;
  125. private String stateCode;
  126. private boolean active;
  127. private Long versionNumber;
  128. private Builder(String code, String name, String countryCode, String stateCode) {
  129. setCode(code);
  130. setName(name);
  131. setCountryCode(countryCode);
  132. setStateCode(stateCode);
  133. setVersionNumber(versionNumber);
  134. }
  135. /**
  136. * creates a County Builder with the required fields.
  137. */
  138. public static Builder create(String code, String name, String countryCode, String stateCode) {
  139. final Builder builder = new Builder(code, name, countryCode, stateCode);
  140. builder.setActive(true);
  141. return builder;
  142. }
  143. /**
  144. * creates a County Builder from an existing {@link CountyContract}.
  145. */
  146. public static Builder create(CountyContract contract) {
  147. final Builder builder = new Builder(contract.getCode(), contract.getName(), contract.getCountryCode(), contract.getStateCode());
  148. builder.setActive(contract.isActive());
  149. builder.setVersionNumber(contract.getVersionNumber());
  150. return builder;
  151. }
  152. @Override
  153. public String getCode() {
  154. return code;
  155. }
  156. /**
  157. * Sets the code to be used for the County created from this Builder.
  158. * @param code String code for a County
  159. * @throws IllegalArgumentException if the passed in code is null or a blank String.
  160. */
  161. public void setCode(String code) {
  162. if (StringUtils.isBlank(code)) {
  163. throw new IllegalArgumentException("code is blank");
  164. }
  165. this.code = code;
  166. }
  167. @Override
  168. public String getName() {
  169. return name;
  170. }
  171. /**
  172. * Sets the full name of the County created from this Builder.
  173. * @param name String representing the full name for the County
  174. * @throws IllegalArgumentException if the passed in name is null or a blank String.
  175. */
  176. public void setName(String name) {
  177. if (StringUtils.isBlank(name)) {
  178. throw new IllegalArgumentException("name is blank");
  179. }
  180. this.name = name;
  181. }
  182. @Override
  183. public String getCountryCode() {
  184. return countryCode;
  185. }
  186. /**
  187. * Sets the Country code to be associated with the County created from this Builder.
  188. * @param countryCode String representing the Country Code
  189. * @throws IllegalArgumentException if the passed in countryCode is null or a blank String.
  190. * @see org.kuali.rice.shareddata.api.country.CountryContract
  191. */
  192. public void setCountryCode(String countryCode) {
  193. if (StringUtils.isBlank(countryCode)) {
  194. throw new IllegalArgumentException("countryCode is blank");
  195. }
  196. this.countryCode = countryCode;
  197. }
  198. @Override
  199. public String getStateCode() {
  200. return stateCode;
  201. }
  202. /**
  203. * Sets the State code to be associated with the County created from this Builder.
  204. * @param stateCode String representing the State code
  205. * @throws IllegalArgumentException if the passed in statecode is null or a blank String.
  206. * @see org.kuali.rice.shareddata.api.state.StateContract
  207. */
  208. public void setStateCode(String stateCode) {
  209. if (StringUtils.isBlank(stateCode)) {
  210. throw new IllegalArgumentException("stateCode is blank");
  211. }
  212. this.stateCode = stateCode;
  213. }
  214. @Override
  215. public boolean isActive() {
  216. return active;
  217. }
  218. /**
  219. * Sets the active flag for the County created from this Builder.
  220. * @param active
  221. */
  222. public void setActive(boolean active) {
  223. this.active = active;
  224. }
  225. @Override
  226. public Long getVersionNumber() {
  227. return versionNumber;
  228. }
  229. public void setVersionNumber(Long versionNumber) {
  230. this.versionNumber = versionNumber;
  231. }
  232. @Override
  233. public County build() {
  234. return new County(this);
  235. }
  236. }
  237. /**
  238. * Defines some internal constants used on this class.
  239. */
  240. static class Constants {
  241. final static String ROOT_ELEMENT_NAME = "county";
  242. final static String TYPE_NAME = "CountyType";
  243. }
  244. /**
  245. * A private class which exposes constants which define the XML element names to use
  246. * when this object is marshalled to XML.
  247. */
  248. static class Elements {
  249. final static String CODE = "code";
  250. final static String NAME = "name";
  251. final static String COUNTRY_CODE = "countryCode";
  252. final static String STATE_CODE = "stateCode";
  253. final static String ACTIVE = "active";
  254. }
  255. public static class Cache {
  256. public static final String NAME = SharedDataConstants.Namespaces.SHAREDDATA_NAMESPACE_2_0 + "/" + County.Constants.TYPE_NAME;
  257. }
  258. }