Coverage Report - org.kuali.rice.krms.api.repository.rule.RuleDefinition
 
Classes in this File Line Coverage Branch Coverage Complexity
RuleDefinition
0%
0/46
0%
0/8
1.722
RuleDefinition$1
N/A
N/A
1.722
RuleDefinition$Builder
0%
0/74
0%
0/30
1.722
RuleDefinition$Constants
0%
0/1
N/A
1.722
RuleDefinition$Elements
0%
0/1
N/A
1.722
 
 1  
 /**
 2  
  * Copyright 2005-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.krms.api.repository.rule;
 17  
 
 18  
 import org.apache.commons.lang.StringUtils;
 19  
 import org.kuali.rice.core.api.CoreConstants;
 20  
 import org.kuali.rice.core.api.mo.AbstractDataTransferObject;
 21  
 import org.kuali.rice.core.api.mo.ModelBuilder;
 22  
 import org.kuali.rice.core.api.util.jaxb.MapStringStringAdapter;
 23  
 import org.kuali.rice.krms.api.repository.action.ActionDefinition;
 24  
 import org.kuali.rice.krms.api.repository.action.ActionDefinitionContract;
 25  
 import org.kuali.rice.krms.api.repository.proposition.PropositionDefinition;
 26  
 
 27  
 import javax.xml.bind.annotation.XmlAccessType;
 28  
 import javax.xml.bind.annotation.XmlAccessorType;
 29  
 import javax.xml.bind.annotation.XmlAnyElement;
 30  
 import javax.xml.bind.annotation.XmlElement;
 31  
 import javax.xml.bind.annotation.XmlElementWrapper;
 32  
 import javax.xml.bind.annotation.XmlRootElement;
 33  
 import javax.xml.bind.annotation.XmlTransient;
 34  
 import javax.xml.bind.annotation.XmlType;
 35  
 import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
 36  
 import java.io.Serializable;
 37  
 import java.util.ArrayList;
 38  
 import java.util.Collection;
 39  
 import java.util.Collections;
 40  
 import java.util.HashMap;
 41  
 import java.util.List;
 42  
 import java.util.Map;
 43  
 
 44  
 /**
 45  
  * Concrete model object implementation of KRMS Repository Rule 
 46  
  * immutable. 
 47  
  * Instances of Rule can be (un)marshalled to and from XML.
 48  
  *
 49  
  * @see RuleDefinitionContract
 50  
  */
 51  0
 @XmlRootElement(name = RuleDefinition.Constants.ROOT_ELEMENT_NAME)
 52  
 @XmlAccessorType(XmlAccessType.NONE)
 53  
 @XmlType(name = RuleDefinition.Constants.TYPE_NAME, propOrder = {
 54  
                 RuleDefinition.Elements.ID,
 55  
                 RuleDefinition.Elements.NAME,
 56  
         RuleDefinition.Elements.NAMESPACE,
 57  
         RuleDefinition.Elements.DESCRIPTION,
 58  
                 RuleDefinition.Elements.TYPE_ID,
 59  
                 RuleDefinition.Elements.PROPOSITION,
 60  
                 RuleDefinition.Elements.ACTIONS,
 61  
                 RuleDefinition.Elements.ATTRIBUTES,
 62  
         CoreConstants.CommonElements.VERSION_NUMBER,
 63  
                 CoreConstants.CommonElements.FUTURE_ELEMENTS
 64  
 })
 65  0
 public final class RuleDefinition extends AbstractDataTransferObject implements RuleDefinitionContract {
 66  
         private static final long serialVersionUID = 2783959459503209577L;
 67  
 
 68  
         @XmlElement(name = Elements.ID, required=true)
 69  
         private final String id;
 70  
     @XmlElement(name = Elements.NAME, required=true)
 71  
         private final String name;
 72  
         @XmlElement(name = Elements.NAMESPACE, required=true)
 73  
         private final String namespace;
 74  
     @XmlElement(name = Elements.DESCRIPTION, required=false)
 75  
     private final String description;
 76  
         @XmlElement(name = Elements.TYPE_ID, required=true)
 77  
         private final String typeId;
 78  
         @XmlElement(name = Elements.PROPOSITION, required=true)
 79  
         private final PropositionDefinition proposition;
 80  
         
 81  
         @XmlElementWrapper(name = Elements.ACTIONS)
 82  
         @XmlElement(name = Elements.ACTION, required=false)
 83  
         private final List<ActionDefinition> actions;
 84  
         
 85  
         @XmlElement(name = Elements.ATTRIBUTES, required = false)
 86  
         @XmlJavaTypeAdapter(value = MapStringStringAdapter.class)
 87  
         private final Map<String, String> attributes;
 88  
         
 89  
     @XmlElement(name = CoreConstants.CommonElements.VERSION_NUMBER, required = false)
 90  
     private final Long versionNumber;
 91  
             
 92  0
         @SuppressWarnings("unused")
 93  
     @XmlAnyElement
 94  
     private final Collection<org.w3c.dom.Element> _futureElements = null;
 95  
         
 96  
         @XmlTransient
 97  
         private String propId;
 98  
 
 99  
         /** 
 100  
      * This constructor should never be called.  
 101  
      * It is only present for use during JAXB unmarshalling. 
 102  
      */
 103  0
     private RuleDefinition() {
 104  0
             this.id = null;
 105  0
             this.name = null;
 106  0
             this.namespace = null;
 107  0
         this.description = null;
 108  0
             this.typeId = null;
 109  0
             this.propId = null;
 110  0
             this.proposition = null;
 111  0
             this.actions = null;
 112  0
             this.attributes = null;
 113  0
         this.versionNumber = null;
 114  0
     }
 115  
     
 116  
     /**
 117  
          * Constructs a KRMS Repository Rule object from the given builder.  
 118  
          * This constructor is private and should only ever be invoked from the builder.
 119  
          * 
 120  
          * @param builder the Builder from which to construct the Rule
 121  
          */
 122  0
     private RuleDefinition(Builder builder) {
 123  0
         this.id = builder.getId();
 124  0
         this.name = builder.getName();
 125  0
         this.namespace = builder.getNamespace();
 126  0
         this.typeId = builder.getTypeId();
 127  0
         this.propId = builder.getPropId();
 128  0
         this.description = builder.getDescription();
 129  
         
 130  0
         if (builder.getProposition() != null) {
 131  0
             this.proposition = builder.getProposition().build();
 132  
         } else {
 133  0
             this.proposition = null;
 134  
         }
 135  
         
 136  0
         List<ActionDefinition> actionList = new ArrayList<ActionDefinition> ();
 137  0
         if (builder.getActions() != null){
 138  0
                 for (ActionDefinition.Builder b : builder.actions){
 139  0
                         actionList.add(b.build());
 140  
                 }
 141  0
             this.actions = Collections.unmodifiableList(actionList);
 142  
         } else {
 143  0
             this.actions = Collections.emptyList();
 144  
         }
 145  0
         if (builder.attributes != null){
 146  0
                 this.attributes = Collections.unmodifiableMap(builder.getAttributes());
 147  
         } else {
 148  0
                 this.attributes = null;
 149  
         }
 150  0
         this.versionNumber = builder.getVersionNumber();
 151  0
     }
 152  
     
 153  
         @Override
 154  
         public String getId() {
 155  0
                 return this.id;
 156  
         }
 157  
 
 158  
         @Override
 159  
         public String getName() {
 160  0
                 return this.name;
 161  
         }
 162  
 
 163  
     public String getDescription() {
 164  0
         return this.description;
 165  
     }
 166  
 
 167  
     @Override
 168  
         public String getNamespace() {
 169  0
                 return this.namespace;
 170  
         }
 171  
 
 172  
         @Override
 173  
         public String getTypeId() {
 174  0
                 return this.typeId;
 175  
         }
 176  
 
 177  
         @Override
 178  
         public String getPropId(){
 179  0
                 return this.propId;
 180  
         }
 181  
 
 182  
         @Override
 183  
         public PropositionDefinition getProposition(){
 184  0
                 return this.proposition;
 185  
         }
 186  
         
 187  
         @Override
 188  
         public List<ActionDefinition> getActions(){
 189  0
                 return this.actions;
 190  
         }
 191  
                 
 192  
         @Override
 193  
         public Map<String, String> getAttributes() {
 194  0
                 return this.attributes;
 195  
         }
 196  
 
 197  
     @Override
 198  
     public Long getVersionNumber() {
 199  0
         return versionNumber;
 200  
     }
 201  
         
 202  
         /**
 203  
      * This builder is used to construct instances of KRMS Repository Rule.  It enforces the constraints of the {@link RuleDefinitionContract}.
 204  
      */
 205  0
     public static class Builder implements RuleDefinitionContract, ModelBuilder, Serializable {                
 206  
         private static final long serialVersionUID = -7850514191699945347L;
 207  
         
 208  
                 private String id;
 209  
         private String name;
 210  
         private String description;
 211  
         private String namespace;
 212  
         private String typeId;
 213  
         private String propId;
 214  
         private PropositionDefinition.Builder proposition;
 215  
         private List<ActionDefinition.Builder> actions;
 216  
         private Map<String, String> attributes;
 217  
         private Long versionNumber;
 218  
 
 219  
                 /**
 220  
                  * Private constructor for creating a builder with all of it's required attributes.
 221  
                  */
 222  0
         private Builder(String ruleId, String name, String namespace, String typeId, String propId) {
 223  0
             setId(ruleId);
 224  0
             setName(name);
 225  0
             setNamespace(namespace);
 226  0
             setTypeId(typeId);
 227  0
             setPropId(propId);
 228  0
             setAttributes(new HashMap<String, String>());
 229  0
         }
 230  
         
 231  
         public static Builder create(String ruleId, String name, String namespace, String typeId, String propId){
 232  0
                 return new Builder(ruleId, name, namespace, typeId, propId);
 233  
         }
 234  
         
 235  
         /**
 236  
          * Creates a builder by populating it with data from the given {@link RuleDefinitionContract}.
 237  
          * 
 238  
          * @param contract the contract from which to populate this builder
 239  
          * @return an instance of the builder populated with data from the contract
 240  
          */
 241  
         public static Builder create(RuleDefinitionContract contract) {
 242  0
                 if (contract == null) {
 243  0
                 throw new IllegalArgumentException("contract is null");
 244  
             }
 245  
 
 246  0
                 List <ActionDefinition.Builder> actionList = new ArrayList<ActionDefinition.Builder>();
 247  0
                 if (contract.getActions() != null){
 248  0
                         for (ActionDefinitionContract actionContract : contract.getActions()){
 249  0
                                 ActionDefinition.Builder actBuilder = ActionDefinition.Builder.create(actionContract);
 250  0
                                 actionList.add(actBuilder);
 251  0
                         }
 252  
                 }
 253  
                 
 254  0
             Builder builder =  new Builder(contract.getId(), contract.getName(),
 255  
                             contract.getNamespace(), contract.getTypeId(), contract.getPropId());
 256  0
             if (contract.getProposition() != null) {
 257  0
                 builder.setProposition(PropositionDefinition.Builder.create(contract.getProposition()));
 258  
             }
 259  0
                 if (contract.getAttributes() != null){
 260  0
                 builder.setAttributes(new HashMap<String, String>(contract.getAttributes()));
 261  
                 }
 262  0
             builder.setActions(actionList);
 263  0
             builder.setVersionNumber(contract.getVersionNumber());
 264  0
             builder.setDescription(contract.getDescription());
 265  0
             return builder;
 266  
         }
 267  
 
 268  
                 /**
 269  
                  * Sets the value of the id on this builder to the given value.
 270  
                  * 
 271  
                  * @param ruleId the id value to set, must not be null or blank
 272  
                  * @throws IllegalArgumentException if the id is null or blank
 273  
                  */
 274  
 
 275  
         public void setId(String ruleId) {
 276  0
             if (ruleId != null && StringUtils.isBlank(ruleId)) {
 277  0
                 throw new IllegalArgumentException("rule ID must be null or else non-blank");
 278  
             }
 279  0
                         this.id = ruleId;
 280  0
                 }
 281  
      
 282  
         public void setName(String name) {
 283  0
             if (StringUtils.isBlank(name)) {
 284  0
                 throw new IllegalArgumentException("name is blank");
 285  
             }
 286  0
             this.name = name;
 287  0
         }
 288  
      
 289  
         public void setDescription(String description) {
 290  0
             if (description != null && StringUtils.isBlank(description)) {
 291  0
                 throw new IllegalArgumentException("description is non-null but is blank");
 292  
             }
 293  0
             this.description = description;
 294  0
         }
 295  
      
 296  
         public void setNamespace(String namespace) {
 297  0
             if (StringUtils.isBlank(namespace)) {
 298  0
                 throw new IllegalArgumentException("namespace is blank");
 299  
             }
 300  0
                         this.namespace = namespace;
 301  0
                 }
 302  
      
 303  
                 public void setTypeId(String typeId) {
 304  0
                         this.typeId = typeId;
 305  0
                 }
 306  
                 
 307  
                 public void setPropId(String propId) {
 308  0
                     if (propId != null && StringUtils.isBlank(propId)) {
 309  0
                         throw new IllegalArgumentException("propId must be null or non-blank");
 310  
                     }
 311  0
                         this.propId = propId;
 312  0
                 }
 313  
                 
 314  
                 public void setProposition(PropositionDefinition.Builder prop) {
 315  0
                         this.proposition = prop;
 316  0
                         this.setPropId(prop.getId());
 317  0
                 }
 318  
                 
 319  
                 public void setActions(List<ActionDefinition.Builder> actions) {
 320  0
                         if (actions == null){
 321  0
                                 this.actions = Collections.unmodifiableList(new ArrayList<ActionDefinition.Builder>());
 322  0
                                 return;
 323  
                         }
 324  0
                         this.actions = Collections.unmodifiableList(actions);
 325  0
                 }
 326  
                 
 327  
                 public void setAttributes(Map<String, String> attributes){
 328  0
                         if (attributes == null){
 329  0
                                 this.attributes = Collections.emptyMap();
 330  
                         }
 331  0
                         this.attributes = Collections.unmodifiableMap(attributes);
 332  0
                 }
 333  
                 
 334  
         public void setVersionNumber(Long versionNumber){
 335  0
             this.versionNumber = versionNumber;
 336  0
         }
 337  
         
 338  
                 @Override
 339  
                 public String getId() {
 340  0
                         return id;
 341  
                 }
 342  
 
 343  
                 @Override
 344  
                 public String getName() {
 345  0
                         return name;
 346  
                 }
 347  
                 
 348  
                 @Override
 349  
                 public String getDescription() {
 350  0
                     return description;
 351  
                 }
 352  
 
 353  
                 @Override
 354  
                 public String getNamespace() {
 355  0
                         return namespace;
 356  
                 }
 357  
 
 358  
                 @Override
 359  
                 public String getTypeId() {
 360  0
                         return typeId;
 361  
                 }
 362  
 
 363  
                 @Override
 364  
                 public String getPropId() {
 365  0
                         return propId;
 366  
                 }
 367  
 
 368  
                 @Override
 369  
                 public PropositionDefinition.Builder getProposition() {
 370  0
                         return proposition;
 371  
                 }
 372  
 
 373  
                 @Override
 374  
                 public List<ActionDefinition.Builder> getActions(){
 375  0
                         return actions;
 376  
                 }
 377  
                 @Override
 378  
                 public Map<String, String> getAttributes() {
 379  0
                         return attributes;
 380  
                 }
 381  
 
 382  
         @Override
 383  
         public Long getVersionNumber() {
 384  0
             return versionNumber;
 385  
         }
 386  
 
 387  
                 /**
 388  
                  * Builds an instance of a Rule based on the current state of the builder.
 389  
                  * 
 390  
                  * @return the fully-constructed Rule
 391  
                  */
 392  
         @Override
 393  
         public RuleDefinition build() {
 394  0
             return new RuleDefinition(this);
 395  
         }
 396  
                 
 397  
     }
 398  
         
 399  
         /**
 400  
          * Defines some internal constants used on this class.
 401  
          */
 402  0
         public static class Constants {
 403  
                 final static String ROOT_ELEMENT_NAME = "rule";
 404  
                 final static String TYPE_NAME = "RuleType";
 405  
         }
 406  
         
 407  
         /**
 408  
          * A private class which exposes constants which define the XML element names to use
 409  
          * when this object is marshalled to XML.
 410  
          */
 411  0
         public static class Elements {
 412  
                 final static String ID = "id";
 413  
         final static String NAME = "name";
 414  
         final static String DESCRIPTION = "description";
 415  
                 final static String NAMESPACE = "namespace";
 416  
                 final static String TYPE_ID = "typeId";
 417  
                 final static String PROPOSITION = "proposition";
 418  
                 final static String ACTIONS = "actions";
 419  
                 final static String ACTION = "action";
 420  
                 final static String ATTRIBUTES = "attributes";
 421  
         }
 422  
 
 423  
 }