001/** 002 * Copyright 2005-2016 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.location.api.campus; 017 018import org.apache.commons.lang.StringUtils; 019import org.kuali.rice.core.api.CoreConstants; 020import org.kuali.rice.core.api.mo.AbstractDataTransferObject; 021import org.kuali.rice.core.api.mo.ModelBuilder; 022import org.kuali.rice.location.api.LocationConstants; 023import org.w3c.dom.Element; 024 025import javax.xml.bind.annotation.XmlAccessType; 026import javax.xml.bind.annotation.XmlAccessorType; 027import javax.xml.bind.annotation.XmlAnyElement; 028import javax.xml.bind.annotation.XmlElement; 029import javax.xml.bind.annotation.XmlRootElement; 030import javax.xml.bind.annotation.XmlType; 031import java.io.Serializable; 032import java.util.Collection; 033 034/** 035 * An immutable representation of a {@link CampusTypeContract}. 036 * 037 * <p>To construct an instance of a CampusType, use the {@link CampusType.Builder} class. 038 * 039 * @see CampusTypeContract 040 * @author Kuali Rice Team (rice.collab@kuali.org) 041 */ 042@XmlRootElement(name = CampusType.Constants.ROOT_ELEMENT_NAME) 043@XmlAccessorType(XmlAccessType.NONE) 044@XmlType(name = CampusType.Constants.TYPE_NAME, propOrder = { 045 CampusType.Elements.CODE, 046 CampusType.Elements.NAME, 047 CampusType.Elements.ACTIVE, 048 CoreConstants.CommonElements.VERSION_NUMBER, 049 CoreConstants.CommonElements.OBJECT_ID, 050 CoreConstants.CommonElements.FUTURE_ELEMENTS 051}) 052public final class CampusType extends AbstractDataTransferObject implements CampusTypeContract { 053 private static final long serialVersionUID = -6325716665728047946L; 054 055 @XmlElement(name = Elements.CODE, required=true) 056 private final String code; 057 058 @XmlElement(name = Elements.NAME, required=false) 059 private final String name; 060 061 @XmlElement(name = Elements.ACTIVE, required=false) 062 private final boolean active; 063 064 @XmlElement(name = CoreConstants.CommonElements.VERSION_NUMBER, required = false) 065 private final Long versionNumber; 066 067 @XmlElement(name = CoreConstants.CommonElements.OBJECT_ID, required = false) 068 private final String objectId; 069 070 @SuppressWarnings("unused") 071 @XmlAnyElement 072 private final Collection<Element> _futureElements = null; 073 074 /** 075 * This constructor should never be called. It is only present for use during JAXB unmarshalling. 076 */ 077 private CampusType() { 078 this.code = null; 079 this.name = null; 080 this.active = false; 081 this.versionNumber = null; 082 this.objectId = null; 083 } 084 085 /** 086 * Constructs a CampusType from the given builder. This constructor is private and should only 087 * ever be invoked from the builder. 088 * 089 * @param builder the Builder from which to construct the campus type 090 */ 091 private CampusType(Builder builder) { 092 this.code = builder.getCode(); 093 this.name = builder.getName(); 094 this.active = builder.isActive(); 095 this.versionNumber = builder.getVersionNumber(); 096 this.objectId = builder.getObjectId(); 097 } 098 099 /** {@inheritDoc} */ 100 @Override 101 public String getCode() { 102 return this.code; 103 } 104 105 /** {@inheritDoc} */ 106 @Override 107 public String getName() { 108 return this.name; 109 } 110 111 /** {@inheritDoc} */ 112 @Override 113 public boolean isActive() { 114 return this.active; 115 } 116 117 /** {@inheritDoc} */ 118 @Override 119 public Long getVersionNumber() { 120 return this.versionNumber; 121 } 122 123 /** {@inheritDoc} */ 124 @Override 125 public String getObjectId() { 126 return objectId; 127 } 128 129 /** 130 * This builder is used to construct instances of CampusType. It enforces the constraints of the {@link CampusTypeContract}. 131 */ 132 public static class Builder implements CampusTypeContract, ModelBuilder, Serializable { 133 private static final long serialVersionUID = 1807428861457935238L; 134 135 private String code; 136 private String name; 137 private boolean active; 138 private Long versionNumber; 139 private String objectId; 140 141 /** 142 * Private constructor for creating a builder with all of it's required attributes. 143 */ 144 private Builder(String code) { 145 setCode(code); 146 setActive(true); 147 } 148 149 /** 150 * Creates a builder from the given campus type code. 151 * 152 * @param code the campus type code 153 * @return an instance of the builder with the code already populated 154 * @throws IllegalArgumentException if the code is null or blank 155 */ 156 public static Builder create(String code) { 157 return new Builder(code); 158 } 159 160 /** 161 * Creates a builder by populating it with data from the given {@link CampusTypeContract}. 162 * 163 * @param contract the contract from which to populate this builder 164 * @return an instance of the builder populated with data from the contract 165 */ 166 public static Builder create(CampusTypeContract contract) { 167 if (contract == null) { 168 throw new IllegalArgumentException("contract is null"); 169 } 170 Builder builder = new Builder(contract.getCode()); 171 builder.setName(contract.getName()); 172 builder.setActive(contract.isActive()); 173 builder.setVersionNumber(contract.getVersionNumber()); 174 builder.setObjectId(contract.getObjectId()); 175 return builder; 176 } 177 178 /** 179 * Sets the value of the code on this builder to the given value. 180 * 181 * @param code the code value to set, must not be null or blank 182 * @throws IllegalArgumentException if the code is null or blank 183 */ 184 public void setCode(String code) { 185 if (StringUtils.isBlank(code)) { 186 throw new IllegalArgumentException("code is blank"); 187 } 188 this.code = code; 189 } 190 191 public void setName(String name) { 192 this.name = name; 193 } 194 195 public void setActive(boolean active) { 196 this.active = active; 197 } 198 199 public void setVersionNumber(Long versionNumber) { 200 this.versionNumber = versionNumber; 201 } 202 203 public void setObjectId(String objectId) { 204 this.objectId = objectId; 205 } 206 207 @Override 208 public String getCode() { 209 return code; 210 } 211 212 @Override 213 public String getName() { 214 return name; 215 } 216 217 @Override 218 public boolean isActive() { 219 return active; 220 } 221 222 @Override 223 public Long getVersionNumber() { 224 return versionNumber; 225 } 226 227 @Override 228 public String getObjectId() { 229 return objectId; 230 } 231 232 /** 233 * Builds an instance of a CampusType based on the current state of the builder. 234 * 235 * @return the fully-constructed CampusType 236 */ 237 @Override 238 public CampusType build() { 239 return new CampusType(this); 240 } 241 242 } 243 244 /** 245 * Defines some internal constants used on this class. 246 */ 247 static class Constants { 248 final static String ROOT_ELEMENT_NAME = "campusType"; 249 final static String TYPE_NAME = "CampusTypeType"; 250 } 251 252 /** 253 * A private class which exposes constants which define the XML element names to use 254 * when this object is marshalled to XML. 255 */ 256 static class Elements { 257 final static String CODE = "code"; 258 final static String NAME = "name"; 259 final static String ACTIVE = "active"; 260 } 261 262 public static class Cache { 263 public static final String NAME = LocationConstants.Namespaces.LOCATION_NAMESPACE_2_0 + "/" + CampusType.Constants.TYPE_NAME; 264 } 265}