| 1 | |
package org.kuali.rice.krms.api.repository; |
| 2 | |
|
| 3 | |
import java.io.Serializable; |
| 4 | |
import java.util.ArrayList; |
| 5 | |
import java.util.Arrays; |
| 6 | |
import java.util.Collection; |
| 7 | |
import java.util.Collections; |
| 8 | |
import java.util.List; |
| 9 | |
|
| 10 | |
import javax.xml.bind.annotation.XmlAccessType; |
| 11 | |
import javax.xml.bind.annotation.XmlAccessorType; |
| 12 | |
import javax.xml.bind.annotation.XmlAnyElement; |
| 13 | |
import javax.xml.bind.annotation.XmlElement; |
| 14 | |
import javax.xml.bind.annotation.XmlRootElement; |
| 15 | |
import javax.xml.bind.annotation.XmlTransient; |
| 16 | |
import javax.xml.bind.annotation.XmlType; |
| 17 | |
|
| 18 | |
import org.apache.commons.lang.StringUtils; |
| 19 | |
import org.apache.commons.lang.builder.EqualsBuilder; |
| 20 | |
import org.apache.commons.lang.builder.HashCodeBuilder; |
| 21 | |
import org.apache.commons.lang.builder.ToStringBuilder; |
| 22 | |
import org.kuali.rice.core.api.mo.ModelBuilder; |
| 23 | |
import org.kuali.rice.core.api.mo.ModelObjectComplete; |
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | 0 | @XmlRootElement(name = Rule.Constants.ROOT_ELEMENT_NAME, namespace = Rule.Constants.KRMSNAMESPACE) |
| 33 | |
@XmlAccessorType(XmlAccessType.NONE) |
| 34 | |
@XmlType(name = Rule.Constants.TYPE_NAME, propOrder = { |
| 35 | |
Rule.Elements.ID, |
| 36 | |
Rule.Elements.NAME, |
| 37 | |
Rule.Elements.NAMESPACE, |
| 38 | |
Rule.Elements.TYPE_ID, |
| 39 | |
Rule.Elements.PROPOSITION, |
| 40 | |
Rule.Elements.ACTION, |
| 41 | |
Rule.Elements.ATTRIBUTES, |
| 42 | |
"_elements" |
| 43 | |
}) |
| 44 | 0 | public final class Rule implements RuleContract, ModelObjectComplete{ |
| 45 | |
private static final long serialVersionUID = 2783959459503209577L; |
| 46 | |
|
| 47 | |
@XmlElement(name = Elements.ID, required=true, namespace = Rule.Constants.KRMSNAMESPACE) |
| 48 | |
private String ruleId; |
| 49 | |
@XmlElement(name = Elements.NAME, required=true, namespace = Rule.Constants.KRMSNAMESPACE) |
| 50 | |
private String name; |
| 51 | |
@XmlElement(name = Elements.NAMESPACE, required=true, namespace = Rule.Constants.KRMSNAMESPACE) |
| 52 | |
private String namespace; |
| 53 | |
@XmlElement(name = Elements.TYPE_ID, required=true, namespace = Rule.Constants.KRMSNAMESPACE) |
| 54 | |
private String typeId; |
| 55 | |
@XmlElement(name = Elements.PROPOSITION, required=true, namespace = Rule.Constants.KRMSNAMESPACE) |
| 56 | |
private Proposition proposition; |
| 57 | |
@XmlElement(name = Elements.ACTION, required=false, namespace = Rule.Constants.KRMSNAMESPACE) |
| 58 | |
private Action action; |
| 59 | |
@XmlElement(name = Elements.ATTRIBUTES, required=false, namespace = Rule.Constants.KRMSNAMESPACE) |
| 60 | |
private List<RuleAttribute> attributes; |
| 61 | |
|
| 62 | 0 | @SuppressWarnings("unused") |
| 63 | |
@XmlAnyElement |
| 64 | |
private final Collection<org.w3c.dom.Element> _elements = null; |
| 65 | |
|
| 66 | |
@XmlTransient |
| 67 | |
private String propId; |
| 68 | |
@XmlTransient |
| 69 | |
private String actionId; |
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | 0 | private Rule() { |
| 76 | 0 | this.ruleId = null; |
| 77 | 0 | this.name = null; |
| 78 | 0 | this.namespace = null; |
| 79 | 0 | this.typeId = null; |
| 80 | 0 | this.propId = null; |
| 81 | 0 | this.actionId = null; |
| 82 | 0 | this.proposition = null; |
| 83 | 0 | this.action = null; |
| 84 | 0 | this.attributes = null; |
| 85 | 0 | } |
| 86 | |
|
| 87 | |
|
| 88 | |
|
| 89 | |
|
| 90 | |
|
| 91 | |
|
| 92 | |
|
| 93 | 0 | private Rule(Builder builder) { |
| 94 | 0 | this.ruleId = builder.getRuleId(); |
| 95 | 0 | this.name = builder.getName(); |
| 96 | 0 | this.namespace = builder.getNamespace(); |
| 97 | 0 | this.typeId = builder.getTypeId(); |
| 98 | 0 | this.propId = builder.getPropId(); |
| 99 | 0 | this.proposition = builder.getProposition().build(); |
| 100 | 0 | this.actionId = builder.getActionId(); |
| 101 | 0 | this.action = builder.getAction().build(); |
| 102 | 0 | List<RuleAttribute> attrList = new ArrayList<RuleAttribute>(); |
| 103 | 0 | for (RuleAttribute.Builder b : builder.attributes){ |
| 104 | 0 | attrList.add(b.build()); |
| 105 | |
} |
| 106 | 0 | this.attributes = Collections.unmodifiableList(attrList); |
| 107 | 0 | } |
| 108 | |
|
| 109 | |
@Override |
| 110 | |
public String getRuleId() { |
| 111 | 0 | return this.actionId; |
| 112 | |
} |
| 113 | |
|
| 114 | |
@Override |
| 115 | |
public String getName() { |
| 116 | 0 | return this.name; |
| 117 | |
} |
| 118 | |
|
| 119 | |
@Override |
| 120 | |
public String getNamespace() { |
| 121 | 0 | return this.namespace; |
| 122 | |
} |
| 123 | |
|
| 124 | |
@Override |
| 125 | |
public String getTypeId() { |
| 126 | 0 | return this.typeId; |
| 127 | |
} |
| 128 | |
|
| 129 | |
@Override |
| 130 | |
public String getPropId(){ |
| 131 | 0 | return this.propId; |
| 132 | |
} |
| 133 | |
|
| 134 | |
@Override |
| 135 | |
public Proposition getProposition(){ |
| 136 | 0 | return this.proposition; |
| 137 | |
} |
| 138 | |
|
| 139 | |
@Override |
| 140 | |
public String getActionId(){ |
| 141 | 0 | return this.actionId; |
| 142 | |
} |
| 143 | |
|
| 144 | |
@Override |
| 145 | |
public Action getAction(){ |
| 146 | 0 | return this.action; |
| 147 | |
} |
| 148 | |
|
| 149 | |
|
| 150 | |
@Override |
| 151 | |
public List<RuleAttribute> getAttributes() { |
| 152 | 0 | return this.attributes; |
| 153 | |
} |
| 154 | |
|
| 155 | |
|
| 156 | |
|
| 157 | |
|
| 158 | 0 | public static class Builder implements RuleContract, ModelBuilder, Serializable { |
| 159 | |
|
| 160 | |
private String ruleId; |
| 161 | |
private String name; |
| 162 | |
private String namespace; |
| 163 | |
private String typeId; |
| 164 | |
private String propId; |
| 165 | |
private String actionId; |
| 166 | |
private Proposition.Builder proposition; |
| 167 | |
private Action.Builder action; |
| 168 | |
private List<RuleAttribute.Builder> attributes; |
| 169 | |
|
| 170 | |
|
| 171 | |
|
| 172 | |
|
| 173 | 0 | private Builder(String ruleId, String name, String namespace, String typeId, String propId) { |
| 174 | 0 | setRuleId(ruleId); |
| 175 | 0 | setName(name); |
| 176 | 0 | setNamespace(namespace); |
| 177 | 0 | setTypeId(typeId); |
| 178 | 0 | setPropId(propId); |
| 179 | 0 | } |
| 180 | |
|
| 181 | |
public Builder actionId (String actionId){ |
| 182 | 0 | setActionId(actionId); |
| 183 | 0 | return this; |
| 184 | |
} |
| 185 | |
|
| 186 | |
public Builder action (Action.Builder action){ |
| 187 | 0 | setAction(action); |
| 188 | 0 | return this; |
| 189 | |
} |
| 190 | |
|
| 191 | |
public Builder proposition (Proposition.Builder proposition){ |
| 192 | 0 | setProposition(proposition); |
| 193 | 0 | return this; |
| 194 | |
} |
| 195 | |
|
| 196 | |
public Builder attributes (List<RuleAttribute.Builder> attributes){ |
| 197 | 0 | setAttributes(attributes); |
| 198 | 0 | return this; |
| 199 | |
} |
| 200 | |
|
| 201 | |
public static Builder create(String ruleId, String name, String namespace, String typeId, String propId){ |
| 202 | 0 | return new Builder(ruleId, name, namespace, typeId, propId); |
| 203 | |
} |
| 204 | |
|
| 205 | |
|
| 206 | |
|
| 207 | |
|
| 208 | |
|
| 209 | |
|
| 210 | |
public static Builder create(RuleContract contract) { |
| 211 | 0 | if (contract == null) { |
| 212 | 0 | throw new IllegalArgumentException("contract is null"); |
| 213 | |
} |
| 214 | 0 | List <RuleAttribute.Builder> attrBuilderList = new ArrayList<RuleAttribute.Builder>(); |
| 215 | 0 | if (contract.getAttributes() != null){ |
| 216 | 0 | for (RuleAttributeContract attrContract : contract.getAttributes()){ |
| 217 | 0 | RuleAttribute.Builder myBuilder = RuleAttribute.Builder.create(attrContract); |
| 218 | 0 | attrBuilderList.add(myBuilder); |
| 219 | 0 | } |
| 220 | |
} |
| 221 | 0 | Builder builder = new Builder(contract.getRuleId(), contract.getName(), |
| 222 | |
contract.getNamespace(), contract.getTypeId(), contract.getPropId()) |
| 223 | |
.proposition(Proposition.Builder.create(contract.getProposition())) |
| 224 | |
.actionId(contract.getActionId()) |
| 225 | |
.action(Action.Builder.create(contract.getAction())) |
| 226 | |
.attributes(attrBuilderList); |
| 227 | 0 | return builder; |
| 228 | |
} |
| 229 | |
|
| 230 | |
|
| 231 | |
|
| 232 | |
|
| 233 | |
|
| 234 | |
|
| 235 | |
|
| 236 | |
|
| 237 | |
public void setRuleId(String ruleId) { |
| 238 | 0 | if (StringUtils.isBlank(ruleId)) { |
| 239 | 0 | throw new IllegalArgumentException("ruleId is blank"); |
| 240 | |
} |
| 241 | 0 | this.ruleId = ruleId; |
| 242 | 0 | } |
| 243 | |
|
| 244 | |
public void setName(String name) { |
| 245 | 0 | if (StringUtils.isBlank(name)) { |
| 246 | 0 | throw new IllegalArgumentException("name is blank"); |
| 247 | |
} |
| 248 | 0 | this.name = name; |
| 249 | 0 | } |
| 250 | |
|
| 251 | |
public void setNamespace(String namespace) { |
| 252 | 0 | if (StringUtils.isBlank(namespace)) { |
| 253 | 0 | throw new IllegalArgumentException("namespace is blank"); |
| 254 | |
} |
| 255 | 0 | this.namespace = namespace; |
| 256 | 0 | } |
| 257 | |
|
| 258 | |
public void setTypeId(String typeId) { |
| 259 | 0 | if (StringUtils.isBlank(typeId)) { |
| 260 | 0 | throw new IllegalArgumentException("KRMS type id is blank"); |
| 261 | |
} |
| 262 | 0 | this.typeId = typeId; |
| 263 | 0 | } |
| 264 | |
|
| 265 | |
public void setPropId(String propId) { |
| 266 | 0 | this.propId = propId; |
| 267 | 0 | } |
| 268 | |
|
| 269 | |
public void setProposition(Proposition.Builder prop) { |
| 270 | 0 | this.proposition = prop; |
| 271 | 0 | this.setPropId(prop.getPropId()); |
| 272 | 0 | } |
| 273 | |
|
| 274 | |
public void setActionId(String actionId) { |
| 275 | 0 | this.actionId = actionId; |
| 276 | 0 | } |
| 277 | |
|
| 278 | |
public void setAction(Action.Builder action) { |
| 279 | 0 | this.action = action; |
| 280 | 0 | setActionId(action.getActionId()); |
| 281 | 0 | } |
| 282 | |
|
| 283 | |
public void setAttributes(List<RuleAttribute.Builder> attributes){ |
| 284 | 0 | if (attributes == null){ |
| 285 | 0 | this.attributes = Collections.unmodifiableList(new ArrayList<RuleAttribute.Builder>()); |
| 286 | 0 | return; |
| 287 | |
} |
| 288 | 0 | this.attributes = Collections.unmodifiableList(attributes); |
| 289 | 0 | } |
| 290 | |
|
| 291 | |
@Override |
| 292 | |
public String getRuleId() { |
| 293 | 0 | return actionId; |
| 294 | |
} |
| 295 | |
|
| 296 | |
@Override |
| 297 | |
public String getName() { |
| 298 | 0 | return name; |
| 299 | |
} |
| 300 | |
|
| 301 | |
@Override |
| 302 | |
public String getNamespace() { |
| 303 | 0 | return namespace; |
| 304 | |
} |
| 305 | |
|
| 306 | |
@Override |
| 307 | |
public String getTypeId() { |
| 308 | 0 | return typeId; |
| 309 | |
} |
| 310 | |
|
| 311 | |
@Override |
| 312 | |
public String getPropId() { |
| 313 | 0 | return propId; |
| 314 | |
} |
| 315 | |
|
| 316 | |
@Override |
| 317 | |
public String getActionId() { |
| 318 | 0 | return actionId; |
| 319 | |
} |
| 320 | |
|
| 321 | |
@Override |
| 322 | |
public Proposition.Builder getProposition() { |
| 323 | 0 | return proposition; |
| 324 | |
} |
| 325 | |
|
| 326 | |
@Override |
| 327 | |
public Action.Builder getAction(){ |
| 328 | 0 | return action; |
| 329 | |
} |
| 330 | |
@Override |
| 331 | |
public List<RuleAttribute.Builder> getAttributes() { |
| 332 | 0 | return attributes; |
| 333 | |
} |
| 334 | |
|
| 335 | |
|
| 336 | |
|
| 337 | |
|
| 338 | |
|
| 339 | |
|
| 340 | |
@Override |
| 341 | |
public Rule build() { |
| 342 | 0 | return new Rule(this); |
| 343 | |
} |
| 344 | |
|
| 345 | |
} |
| 346 | |
@Override |
| 347 | |
public int hashCode() { |
| 348 | 0 | return HashCodeBuilder.reflectionHashCode(this, Constants.HASH_CODE_EQUALS_EXCLUDE); |
| 349 | |
} |
| 350 | |
|
| 351 | |
@Override |
| 352 | |
public boolean equals(Object obj) { |
| 353 | 0 | return EqualsBuilder.reflectionEquals(obj, this, Constants.HASH_CODE_EQUALS_EXCLUDE); |
| 354 | |
} |
| 355 | |
|
| 356 | |
@Override |
| 357 | |
public String toString() { |
| 358 | 0 | return ToStringBuilder.reflectionToString(this); |
| 359 | |
} |
| 360 | |
|
| 361 | |
|
| 362 | |
|
| 363 | |
|
| 364 | 0 | static class Constants { |
| 365 | |
final static String KRMSNAMESPACE = "http://rice.kuali.org/schema/krms"; |
| 366 | |
final static String ROOT_ELEMENT_NAME = "Rule"; |
| 367 | |
final static String TYPE_NAME = "RuleType"; |
| 368 | 0 | final static String[] HASH_CODE_EQUALS_EXCLUDE = { "_elements" }; |
| 369 | |
} |
| 370 | |
|
| 371 | |
|
| 372 | |
|
| 373 | |
|
| 374 | |
|
| 375 | 0 | public static class Elements { |
| 376 | |
final static String ID = "ruleId"; |
| 377 | |
final static String NAME = "name"; |
| 378 | |
final static String NAMESPACE = "namespace"; |
| 379 | |
final static String TYPE_ID = "typeId"; |
| 380 | |
final static String PROPOSITION = "proposition"; |
| 381 | |
final static String ACTION = "action"; |
| 382 | |
final static String ATTRIBUTES = "attribute"; |
| 383 | |
} |
| 384 | |
|
| 385 | |
} |