001 /** 002 * Copyright 2005-2012 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 */ 016 package org.kuali.rice.kim.api.common.attribute; 017 018 import org.apache.commons.lang.StringUtils; 019 import org.kuali.rice.core.api.CoreConstants; 020 import org.kuali.rice.core.api.mo.AbstractDataTransferObject; 021 import org.kuali.rice.core.api.mo.ModelBuilder; 022 import org.w3c.dom.Element; 023 024 import javax.xml.bind.annotation.XmlAccessType; 025 import javax.xml.bind.annotation.XmlAccessorType; 026 import javax.xml.bind.annotation.XmlAnyElement; 027 import javax.xml.bind.annotation.XmlElement; 028 import javax.xml.bind.annotation.XmlRootElement; 029 import javax.xml.bind.annotation.XmlType; 030 import java.io.Serializable; 031 import java.util.Collection; 032 033 /** 034 * An immutable representation of a {@link KimAttributeContract}. 035 * 036 * <p>To construct an instance of a KimAttribute, use the {@link KimAttribute.Builder} class.<p/> 037 * 038 * @see KimAttributeContract 039 */ 040 @XmlRootElement(name = KimAttribute.Constants.ROOT_ELEMENT_NAME) 041 @XmlAccessorType(XmlAccessType.NONE) 042 @XmlType(name = KimAttribute.Constants.TYPE_NAME, propOrder = { 043 KimAttribute.Elements.ID, 044 KimAttribute.Elements.COMPONENT_NAME, 045 KimAttribute.Elements.ATTRIBUTE_NAME, 046 KimAttribute.Elements.NAMESPACE_CODE, 047 KimAttribute.Elements.ATTRIBUTE_LABEL, 048 KimAttribute.Elements.ACTIVE, 049 CoreConstants.CommonElements.VERSION_NUMBER, 050 CoreConstants.CommonElements.OBJECT_ID, 051 CoreConstants.CommonElements.FUTURE_ELEMENTS 052 }) 053 public final class KimAttribute extends AbstractDataTransferObject implements KimAttributeContract { 054 private static final long serialVersionUID = 1L; 055 056 @XmlElement(name = KimAttribute.Elements.ID, required = false) 057 private final String id; 058 059 @XmlElement(name = KimAttribute.Elements.COMPONENT_NAME, required = true) 060 private final String componentName; 061 062 @XmlElement(name = KimAttribute.Elements.ATTRIBUTE_NAME, required = true) 063 private final String attributeName; 064 065 @XmlElement(name = KimAttribute.Elements.NAMESPACE_CODE, required = true) 066 private final String namespaceCode; 067 068 @XmlElement(name = KimAttribute.Elements.ATTRIBUTE_LABEL, required = false) 069 private final String attributeLabel; 070 071 @XmlElement(name = KimAttribute.Elements.ACTIVE, required = false) 072 private final boolean active; 073 074 @XmlElement(name = CoreConstants.CommonElements.VERSION_NUMBER, required = false) 075 private final Long versionNumber; 076 077 @XmlElement(name = CoreConstants.CommonElements.OBJECT_ID, required = false) 078 private final String objectId; 079 080 @SuppressWarnings("unused") 081 @XmlAnyElement 082 private final Collection<Element> _futureElements = null; 083 084 /** 085 * This constructor should never be called except during JAXB unmarshalling. 086 */ 087 private KimAttribute() { 088 this.id = null; 089 this.componentName = null; 090 this.attributeName = null; 091 this.namespaceCode = null; 092 this.attributeLabel = null; 093 this.active = false; 094 this.versionNumber = Long.valueOf(1L); 095 this.objectId = null; 096 } 097 098 private KimAttribute(Builder builder) { 099 this.id = builder.getId(); 100 this.componentName = builder.getComponentName(); 101 this.attributeName = builder.getAttributeName(); 102 this.namespaceCode = builder.getNamespaceCode(); 103 this.attributeLabel = builder.getAttributeLabel(); 104 this.active = builder.isActive(); 105 this.versionNumber = builder.getVersionNumber(); 106 this.objectId = builder.getObjectId(); 107 } 108 109 @Override 110 public String getId() { 111 return id; 112 } 113 114 @Override 115 public String getComponentName() { 116 return componentName; 117 } 118 119 @Override 120 public String getAttributeName() { 121 return attributeName; 122 } 123 124 @Override 125 public String getNamespaceCode() { 126 return namespaceCode; 127 } 128 129 @Override 130 public String getAttributeLabel() { 131 return attributeLabel; 132 } 133 134 @Override 135 public Long getVersionNumber() { 136 return versionNumber; 137 } 138 139 @Override 140 public boolean isActive() { 141 return active; 142 } 143 144 @Override 145 public String getObjectId() { 146 return objectId; 147 } 148 149 /** 150 * This builder constructs an KimAttribute enforcing the constraints of the {@link KimAttributeContract}. 151 */ 152 public static final class Builder implements KimAttributeContract, ModelBuilder, Serializable { 153 private String id; 154 private String componentName; 155 private String attributeName; 156 private String namespaceCode; 157 private String attributeLabel; 158 private boolean active; 159 private Long versionNumber = 1L; 160 private String objectId; 161 162 private Builder(String componentName, String attributeName, String namespaceCode) { 163 setComponentName(componentName); 164 setAttributeName(attributeName); 165 setNamespaceCode(namespaceCode); 166 } 167 168 /** 169 * creates a KimAttribute with the required fields. 170 */ 171 public static Builder create(String componentName, String attributeName, String namespaceCode) { 172 return new Builder(componentName, attributeName, namespaceCode); 173 } 174 175 /** 176 * creates a KimAttribute from an existing {@link KimAttributeContract}. 177 */ 178 public static Builder create(KimAttributeContract contract) { 179 Builder builder = new Builder(contract.getComponentName(), contract.getAttributeName(), contract.getNamespaceCode()); 180 builder.setId(contract.getId()); 181 builder.setAttributeLabel(contract.getAttributeLabel()); 182 builder.setActive(contract.isActive()); 183 builder.setVersionNumber(contract.getVersionNumber()); 184 builder.setObjectId(contract.getObjectId()); 185 return builder; 186 } 187 188 189 @Override 190 public String getId() { 191 return id; 192 } 193 194 public void setId(final String id) { 195 this.id = id; 196 } 197 198 @Override 199 public String getComponentName() { 200 return componentName; 201 } 202 203 public void setComponentName(final String componentName) { 204 if (StringUtils.isBlank(componentName)) { 205 throw new IllegalArgumentException("componentName is blank"); 206 } 207 208 this.componentName = componentName; 209 } 210 211 @Override 212 public String getAttributeName() { 213 return attributeName; 214 } 215 216 public void setAttributeName(final String attributeName) { 217 if (StringUtils.isBlank(attributeName)) { 218 throw new IllegalArgumentException("attributeName is blank"); 219 } 220 221 this.attributeName = attributeName; 222 } 223 224 @Override 225 public String getNamespaceCode() { 226 return namespaceCode; 227 } 228 229 public void setNamespaceCode(final String namespaceCode) { 230 if (StringUtils.isBlank(namespaceCode)) { 231 throw new IllegalArgumentException("namespaceCode is blank"); 232 } 233 234 this.namespaceCode = namespaceCode; 235 } 236 237 @Override 238 public String getAttributeLabel() { 239 return attributeLabel; 240 } 241 242 public void setAttributeLabel(final String attributeLabel) { 243 this.attributeLabel = attributeLabel; 244 } 245 246 @Override 247 public boolean isActive() { 248 return active; 249 } 250 251 public void setActive(final boolean active) { 252 this.active = active; 253 } 254 255 @Override 256 public Long getVersionNumber() { 257 return versionNumber; 258 } 259 260 public void setVersionNumber(final Long versionNumber) { 261 if (versionNumber <= 0) { 262 throw new IllegalArgumentException("versionNumber is invalid"); 263 } 264 265 this.versionNumber = versionNumber; 266 } 267 268 @Override 269 public String getObjectId() { 270 return objectId; 271 } 272 273 public void setObjectId(final String objectId) { 274 this.objectId = objectId; 275 } 276 277 @Override 278 public KimAttribute build() { 279 return new KimAttribute(this); 280 } 281 } 282 283 284 /** 285 * Defines some internal constants used on this class. 286 */ 287 static class Constants { 288 static final String ROOT_ELEMENT_NAME = "kimAttribute"; 289 static final String TYPE_NAME = "KimAttributeType"; 290 } 291 292 /** 293 * A private class which exposes constants which define the XML element names to use 294 * when this object is marshalled to XML. 295 */ 296 static class Elements { 297 static final String ID = "id"; 298 static final String COMPONENT_NAME = "componentName"; 299 static final String ATTRIBUTE_NAME = "attributeName"; 300 static final String NAMESPACE_CODE = "namespaceCode"; 301 static final String ATTRIBUTE_LABEL = "attributeLabel"; 302 static final String ACTIVE = "active"; 303 } 304 305 }