001 /** 002 * Copyright 2005-2011 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.krms.api.repository.term; 017 018 import java.io.Serializable; 019 import java.util.Collection; 020 021 import javax.xml.bind.annotation.XmlAccessType; 022 import javax.xml.bind.annotation.XmlAccessorType; 023 import javax.xml.bind.annotation.XmlAnyElement; 024 import javax.xml.bind.annotation.XmlElement; 025 import javax.xml.bind.annotation.XmlRootElement; 026 import javax.xml.bind.annotation.XmlType; 027 028 import org.apache.commons.lang.StringUtils; 029 import org.kuali.rice.core.api.CoreConstants; 030 import org.kuali.rice.core.api.mo.AbstractDataTransferObject; 031 import org.kuali.rice.core.api.mo.ModelBuilder; 032 import org.kuali.rice.krms.api.repository.BuilderUtils.Transformer; 033 034 /** 035 * Immutable DTO for TermParameters. An instance represents a single parameter on a Term. 036 * Construction must be done via the {@link Builder} inner class. 037 * 038 * @author Kuali Rice Team (rice.collab@kuali.org) 039 * 040 */ 041 @XmlRootElement(name = TermParameterDefinition.Constants.ROOT_ELEMENT_NAME) 042 @XmlAccessorType(XmlAccessType.NONE) 043 @XmlType(name = TermParameterDefinition.Constants.TYPE_NAME, propOrder = { 044 TermParameterDefinition.Elements.ID, 045 TermParameterDefinition.Elements.NAME, 046 TermParameterDefinition.Elements.VALUE, 047 CoreConstants.CommonElements.VERSION_NUMBER, 048 CoreConstants.CommonElements.FUTURE_ELEMENTS 049 }) 050 public final class TermParameterDefinition extends AbstractDataTransferObject implements TermParameterDefinitionContract { 051 052 private static final long serialVersionUID = 1L; 053 054 @XmlElement(name = Elements.ID, required=true) 055 private final String id; 056 057 private final String termId; 058 059 @XmlElement(name = Elements.NAME, required=true) 060 private final String name; 061 062 @XmlElement(name = Elements.VALUE) 063 private final String value; 064 065 @XmlElement(name = CoreConstants.CommonElements.VERSION_NUMBER, required = false) 066 private final Long versionNumber; 067 068 @SuppressWarnings("unused") 069 @XmlAnyElement 070 private final Collection<org.w3c.dom.Element> _futureElements = null; 071 072 // For JAXB use only, shouldn't be invoked directly 073 private TermParameterDefinition() { 074 id = null; 075 termId = null; 076 name = null; 077 value = null; 078 versionNumber = null; 079 } 080 081 private TermParameterDefinition(Builder builder) { 082 id = builder.getId(); 083 termId = builder.getTermId(); 084 name = builder.getName(); 085 value = builder.getValue(); 086 versionNumber = builder.getVersionNumber(); 087 } 088 089 public static class Builder implements TermParameterDefinitionContract, ModelBuilder, Serializable { 090 091 private static final long serialVersionUID = 1L; 092 093 private String id; 094 private String termId; 095 private String name; 096 private String value; 097 private Long versionNumber; 098 099 private static final String NON_NULL_NON_EMPTY_ERROR = 100 " must be non-null and must contain non-whitespace chars"; 101 102 public static final Transformer<TermParameterDefinitionContract, TermParameterDefinition.Builder> toBuilder = 103 new Transformer<TermParameterDefinitionContract, TermParameterDefinition.Builder>() { 104 public Builder transform(TermParameterDefinitionContract input) { 105 return Builder.create(input); 106 }; 107 }; 108 109 private Builder(String id, String termId, String name, String value) { 110 setId(id); 111 setTermId(termId); 112 setName(name); 113 setValue(value); 114 } 115 116 /** 117 * static factory to create a {@link Builder} from fields 118 * 119 * @param id must be null, or contain non-whitespace 120 * @param termId must be null, or contain non-whitespace 121 * @param name must be non-null 122 * @param value 123 */ 124 public static Builder create(String id, String termId, String name, String value) { 125 return new Builder(id, termId, name, value); 126 } 127 128 /** 129 * static factory to create a {@link Builder} from a {@link TermParameterDefinitionContract} 130 * 131 * @param termParameterDefinition 132 */ 133 public static Builder create(TermParameterDefinitionContract termParameterDefinition) { 134 Builder builder = new Builder(termParameterDefinition.getId(), 135 termParameterDefinition.getTermId(), 136 termParameterDefinition.getName(), 137 termParameterDefinition.getValue()); 138 builder.setVersionNumber(termParameterDefinition.getVersionNumber()); 139 return builder; 140 } 141 142 // Setters: 143 144 /** 145 * @param id the id to set. for {@link TermParameterDefinition}s used in creational 146 * service methods, it must be null. Otherwise, it must be non-null and contain non-whitespace chars. 147 * @throws IllegalArgumentException if id is all whitespace chars 148 */ 149 public void setId(String id) { 150 if (id != null && StringUtils.isBlank(id)) { 151 throw new IllegalArgumentException("id must contain non-whitespace chars"); 152 } 153 this.id = id; 154 } 155 156 /** 157 * @param termId the termId to set 158 */ 159 public void setTermId(String termId) { 160 if (termId != null && StringUtils.isBlank(termId)) { 161 throw new IllegalArgumentException("termId must contain non-whitespace chars"); 162 } 163 this.termId = termId; 164 } 165 166 /** 167 * @param name the name to set. Must be non-null and contain non-whitespace chars. 168 * @throws IllegalArgumentException if name is null or is all whitespace chars 169 */ 170 public void setName(String name) { 171 if (StringUtils.isBlank(name)) { 172 throw new IllegalArgumentException("name" + NON_NULL_NON_EMPTY_ERROR); 173 } 174 this.name = name; 175 } 176 177 /** 178 * @param value the value to set. May be null or empty. 179 */ 180 public void setValue(String value) { 181 this.value = value; 182 } 183 184 /** 185 * @param versionNumber the versionNumber to set. May be null. 186 */ 187 public void setVersionNumber(Long versionNumber){ 188 this.versionNumber = versionNumber; 189 } 190 191 // Getters: 192 193 /** 194 * @return the id 195 */ 196 @Override 197 public String getId() { 198 return this.id; 199 } 200 201 /** 202 * @return the termId 203 */ 204 @Override 205 public String getTermId() { 206 return this.termId; 207 } 208 209 /** 210 * @return the name 211 */ 212 @Override 213 public String getName() { 214 return this.name; 215 } 216 217 /** 218 * @return the value 219 */ 220 @Override 221 public String getValue() { 222 return this.value; 223 } 224 225 /** 226 * @return the version number 227 */ 228 @Override 229 public Long getVersionNumber() { 230 return this.versionNumber; 231 } 232 233 /** 234 * return a {@link TermParameterDefinition} instance constructed from this {@link Builder} 235 * @see org.kuali.rice.core.api.mo.ModelBuilder#build() 236 */ 237 @Override 238 public TermParameterDefinition build() { 239 return new TermParameterDefinition(this); 240 } 241 242 } 243 244 245 /** 246 * @return the id 247 */ 248 @Override 249 public String getId() { 250 return this.id; 251 } 252 /** 253 * @return the termId 254 */ 255 @Override 256 public String getTermId() { 257 return termId; 258 } 259 /** 260 * @return the name 261 */ 262 @Override 263 public String getName() { 264 return this.name; 265 } 266 /** 267 * @return the value 268 */ 269 @Override 270 public String getValue() { 271 return this.value; 272 } 273 274 /** 275 * @see org.kuali.rice.core.api.mo.common.Versioned#getVersionNumber() 276 */ 277 @Override 278 public Long getVersionNumber() { 279 return versionNumber; 280 } 281 282 public static class Constants { 283 public static final String ROOT_ELEMENT_NAME = "TermParameterDefinition"; 284 public static final String TYPE_NAME = "TermParameterDefinitionType"; 285 } 286 287 public static class Elements { 288 public static final String ID = "id"; 289 public static final String TERM_ID = "termId"; 290 public static final String NAME = "name"; 291 public static final String VALUE = "value"; 292 } 293 }