Coverage Report - org.kuali.rice.krms.api.repository.context.ContextDefinition
 
Classes in this File Line Coverage Branch Coverage Complexity
ContextDefinition
70%
26/37
50%
3/6
1.613
ContextDefinition$1
N/A
N/A
1.613
ContextDefinition$Builder
74%
40/54
59%
13/22
1.613
ContextDefinition$Constants
50%
1/2
N/A
1.613
ContextDefinition$Elements
0%
0/1
N/A
1.613
 
 1  
 package org.kuali.rice.krms.api.repository.context;
 2  
 
 3  
 import java.io.Serializable;
 4  
 import java.util.Collection;
 5  
 import java.util.Collections;
 6  
 import java.util.HashMap;
 7  
 import java.util.HashSet;
 8  
 import java.util.Map;
 9  
 import java.util.Set;
 10  
 
 11  
 import javax.xml.bind.annotation.XmlAccessType;
 12  
 import javax.xml.bind.annotation.XmlAccessorType;
 13  
 import javax.xml.bind.annotation.XmlAnyElement;
 14  
 import javax.xml.bind.annotation.XmlElement;
 15  
 import javax.xml.bind.annotation.XmlElementWrapper;
 16  
 import javax.xml.bind.annotation.XmlRootElement;
 17  
 import javax.xml.bind.annotation.XmlType;
 18  
 import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
 19  
 
 20  
 import org.apache.commons.lang.StringUtils;
 21  
 import org.apache.commons.lang.builder.EqualsBuilder;
 22  
 import org.apache.commons.lang.builder.HashCodeBuilder;
 23  
 import org.apache.commons.lang.builder.ToStringBuilder;
 24  
 import org.kuali.rice.core.api.CoreConstants;
 25  
 import org.kuali.rice.core.api.mo.ModelBuilder;
 26  
 import org.kuali.rice.core.api.mo.ModelObjectComplete;
 27  
 import org.kuali.rice.core.util.jaxb.MapStringStringAdapter;
 28  
 import org.kuali.rice.krms.api.repository.agenda.AgendaDefinition;
 29  
 import org.kuali.rice.krms.api.repository.agenda.AgendaDefinitionContract;
 30  
 
 31  
 /**
 32  
  * An immutable representation of a context definition.  A context definition
 33  
  * defines information about a context which can be loaded into the rules
 34  
  * engine for evaluation.
 35  
  * 
 36  
  * A context definition includes a list of agendas which are valid within the
 37  
  * context.  Typically, during rule engine execution, one or more of these
 38  
  * agendas is selected for execution based on a given set of selection criteria.
 39  
  * 
 40  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 41  
  *
 42  
  */
 43  
 @XmlRootElement(name = ContextDefinition.Constants.ROOT_ELEMENT_NAME)
 44  
 @XmlAccessorType(XmlAccessType.NONE)
 45  
 @XmlType(name = ContextDefinition.Constants.TYPE_NAME, propOrder = {
 46  
                 ContextDefinition.Elements.ID,
 47  
                 ContextDefinition.Elements.NAMESPACE,
 48  
                 ContextDefinition.Elements.NAME,
 49  
                 ContextDefinition.Elements.TYPE_ID,
 50  
                 ContextDefinition.Elements.AGENDAS,
 51  
                 ContextDefinition.Elements.ATTRIBUTES,
 52  
         CoreConstants.CommonElements.VERSION_NUMBER,
 53  
         CoreConstants.CommonElements.FUTURE_ELEMENTS
 54  
 })
 55  2
 public final class ContextDefinition implements ContextDefinitionContract, ModelObjectComplete {
 56  
         
 57  
         private static final long serialVersionUID = -6639428234851623868L;
 58  
 
 59  
         @XmlElement(name = Elements.ID, required = false)
 60  
         private final String id;
 61  
         
 62  
         @XmlElement(name = Elements.NAME, required = true)
 63  
     private final String name;
 64  
         
 65  
         @XmlElement(name = Elements.NAMESPACE, required = true)
 66  
     private final String namespace;
 67  
         
 68  
         @XmlElement(name = Elements.TYPE_ID, required = false)
 69  
     private final String typeId;
 70  
         
 71  
         @XmlElementWrapper(name = Elements.AGENDAS)
 72  
         @XmlElement(name = Elements.AGENDA, required = false)
 73  
         private final Set<AgendaDefinition> agendas;
 74  
             
 75  
         @XmlElement(name = Elements.ATTRIBUTES, required = false)
 76  
         @XmlJavaTypeAdapter(value = MapStringStringAdapter.class)
 77  
         private final Map<String, String> attributes;
 78  
         
 79  
         @XmlElement(name = CoreConstants.CommonElements.VERSION_NUMBER, required = false)
 80  
     private final Long versionNumber;
 81  
         
 82  4
     @SuppressWarnings("unused")
 83  
     @XmlAnyElement
 84  
     private final Collection<org.w3c.dom.Element> _futureElements = null;
 85  
 
 86  
     /**
 87  
      * Used only by JAXB.
 88  
      */
 89  2
     private ContextDefinition() {
 90  2
             this.id = null;
 91  2
             this.name = null;
 92  2
             this.namespace = null;
 93  2
             this.typeId = null;
 94  2
             this.agendas = null;
 95  2
             this.versionNumber = null;
 96  2
             this.attributes = null;
 97  2
     }
 98  
     
 99  2
     private ContextDefinition(Builder builder) {
 100  2
             this.id = builder.getId();
 101  2
             this.name = builder.getName();
 102  2
             this.namespace = builder.getNamespace();
 103  2
             this.typeId = builder.getTypeId();
 104  2
             this.agendas = constructAgendas(builder.getAgendas());
 105  2
             this.versionNumber = builder.getVersionNumber();
 106  2
         if (builder.getAttributes() != null){
 107  2
                 this.attributes = Collections.unmodifiableMap(new HashMap<String, String>(builder.getAttributes()));
 108  
         } else {
 109  0
                 this.attributes = null;
 110  
         }
 111  2
     }
 112  
     
 113  
     private static Set<AgendaDefinition> constructAgendas(Set<AgendaDefinition.Builder> agendaBuilders) {
 114  2
             Set<AgendaDefinition> agendas = new HashSet<AgendaDefinition>();
 115  2
             if (agendaBuilders != null) {
 116  2
                     for (AgendaDefinition.Builder agendaBuilder : agendaBuilders) {
 117  0
                             agendas.add(agendaBuilder.build());
 118  
                     }
 119  
             }
 120  2
             return agendas;
 121  
     }
 122  
     
 123  
     @Override
 124  
         public String getId() {
 125  0
                 return id;
 126  
         }
 127  
 
 128  
         @Override
 129  
         public String getNamespace() {
 130  0
                 return namespace;
 131  
         }
 132  
 
 133  
         @Override
 134  
         public String getName() {
 135  0
                 return name;
 136  
         }
 137  
 
 138  
         @Override
 139  
         public String getTypeId() {
 140  0
                 return typeId;
 141  
         }
 142  
         
 143  
         @Override
 144  
         public Set<AgendaDefinition> getAgendas() {
 145  0
                 return Collections.unmodifiableSet(this.agendas);
 146  
         }
 147  
         
 148  
         @Override
 149  
         public Map<String, String> getAttributes() {
 150  0
                 return this.attributes; 
 151  
         }
 152  
 
 153  
         @Override
 154  
         public Long getVersionNumber() {
 155  0
                 return versionNumber;
 156  
         }
 157  
         
 158  
         /**
 159  
          * A builder which can be used to construct ContextDefinition instances.  Enforces the
 160  
          * constraints of the {@link ContextDefinitionContract}.  This class is the only means
 161  
          * by which a {@link ContextDefinition} object can be constructed.
 162  
          * 
 163  
          * @author Kuali Rice Team (rice.collab@kuali.org)
 164  
          *
 165  
          */
 166  2
         public static final class Builder implements ContextDefinitionContract, ModelBuilder, Serializable  {
 167  
             
 168  
             private static final long serialVersionUID = -219369603932108436L;
 169  
             
 170  
                 private String id;
 171  
                 private String namespace;
 172  
         private String name;
 173  
         private String typeId;
 174  
         private Set<AgendaDefinition.Builder> agendas;
 175  
         private Map<String, String> attributes;
 176  
         private Long versionNumber;
 177  
         
 178  15
         private Builder(String namespace, String name) {
 179  15
                 setNamespace(namespace);
 180  11
                 setName(name);
 181  8
                 setAgendas(new HashSet<AgendaDefinition.Builder>());
 182  8
             setAttributes(new HashMap<String, String>());
 183  8
         }
 184  
         
 185  
         /**
 186  
          * Creates a context definition builder with the given required values
 187  
          * 
 188  
          * @param namespace the namespace code of the context definition to create, must not be null or blank
 189  
          * @param name the name of the context definition to create, must not be null or blank
 190  
          * 
 191  
          * @return a builder with the required values already initialized
 192  
          * 
 193  
          * @throws IllegalArgumentException if the given namespace is null or blank
 194  
          * @throws IllegalArgumentException if the given name is null or blank
 195  
          */
 196  
         public static Builder create(String namespace, String name) {
 197  15
                 return new Builder(namespace, name);
 198  
         }
 199  
         
 200  
         /**
 201  
          * Creates a populates a builder with the data on the given ContextDefinitionContract.
 202  
          * This is similar in nature to a "copy constructor" for Style.
 203  
          * 
 204  
          * @param contract an object implementing the ContextDefinitionContract from which
 205  
          * to copy property values
 206  
          *  
 207  
          * @return a builder with the values from the contract already initialized
 208  
          * 
 209  
          * @throws IllegalArgumentException if the given contract is null
 210  
          */
 211  
         public static Builder create(ContextDefinitionContract contract) {
 212  0
                 if (contract == null) {
 213  0
                         throw new IllegalArgumentException("contract was null");
 214  
                 }
 215  0
                 Builder builder = create(contract.getNamespace(), contract.getName());
 216  0
                 builder.setId(contract.getId());
 217  0
                 builder.setTypeId(contract.getTypeId());
 218  0
                 builder.setVersionNumber(contract.getVersionNumber());
 219  0
                 builder.setAgendas(contract.getAgendas());
 220  0
             if (contract.getAttributes() != null) {
 221  0
                 builder.setAttributes(new HashMap<String, String>(contract.getAttributes()));
 222  
             }
 223  0
                 return builder;
 224  
         }
 225  
         
 226  
         @Override
 227  
         public ContextDefinition build() {
 228  2
                 return new ContextDefinition(this);
 229  
         }
 230  
         
 231  
                 @Override
 232  
                 public Long getVersionNumber() {
 233  2
                         return this.versionNumber;
 234  
                 }
 235  
 
 236  
                 @Override
 237  
                 public String getId() {
 238  2
                         return this.id;
 239  
                 }
 240  
 
 241  
                 @Override
 242  
                 public String getNamespace() {
 243  2
                         return this.namespace;
 244  
                 }
 245  
 
 246  
                 @Override
 247  
                 public String getName() {
 248  2
                         return this.name;
 249  
                 }
 250  
 
 251  
                 @Override
 252  
                 public String getTypeId() {
 253  2
                         return this.typeId;
 254  
                 }
 255  
                 
 256  
                 @Override
 257  
                 public Set<AgendaDefinition.Builder> getAgendas() {
 258  2
                         return agendas;
 259  
                 }
 260  
 
 261  
                 @Override
 262  
                 public Map<String, String> getAttributes() {
 263  4
                         return attributes;
 264  
                 }
 265  
 
 266  
                 /**
 267  
          * Sets the id for the context definition that will be created by this builder.
 268  
          * 
 269  
          * @param id the id to set
 270  
          */
 271  
                 public void setId(String id) {
 272  5
                         if (id != null){
 273  5
                                 if (StringUtils.isBlank(id)){
 274  2
                                         throw new IllegalArgumentException("context id is blank");                                        
 275  
                                 }
 276  
                         }
 277  3
                         this.id = id;
 278  3
                 }
 279  
 
 280  
                 /**
 281  
                  * Sets the namespace code for the context definition that will be created
 282  
                  * by this builder.  The namespace code must not be blank or null.
 283  
                  * 
 284  
                  * @param namespace the namespace to set on this builder, must not be
 285  
                  * null or blank
 286  
                  * 
 287  
                  * @throws IllegalArgumentException if the given namespace code is null or blank
 288  
                  */
 289  
                 public void setNamespace(String namespace) {
 290  15
                         if (StringUtils.isBlank(namespace)) {
 291  4
                                 throw new IllegalArgumentException("namespace is blank");
 292  
                         }
 293  11
                         this.namespace = namespace;
 294  11
                 }
 295  
 
 296  
                 /**
 297  
                  * Sets the name for the context definition that will be created
 298  
                  * by this builder.  The name must not be blank or null.
 299  
                  * 
 300  
                  * @param name the name to set on this builder, must not be
 301  
                  * null or blank
 302  
                  * 
 303  
                  * @throws IllegalArgumentException if the given name is null or blank
 304  
                  */
 305  
                 public void setName(String name) {
 306  11
                         if (StringUtils.isBlank(name)) {
 307  3
                                 throw new IllegalArgumentException("name is blank");
 308  
                         }
 309  8
                         this.name = name;
 310  8
                 }
 311  
 
 312  
                 /**
 313  
          * Sets the typeId for the context definition that will be created by this builder.
 314  
          * 
 315  
          * @param typeId the typeId to set
 316  
          */
 317  
                 public void setTypeId(String typeId) {
 318  5
                         if (typeId != null){
 319  5
                                 if (StringUtils.isBlank(typeId)){
 320  2
                                         throw new IllegalArgumentException("type id is blank");                                        
 321  
                                 }
 322  
                         }
 323  3
                         this.typeId = typeId;
 324  3
                 }
 325  
                 
 326  
                 public void setAgendas(Set<? extends AgendaDefinitionContract> agendaContracts) {
 327  8
                         this.agendas = new HashSet<AgendaDefinition.Builder>();
 328  8
                         if (agendaContracts != null) for (AgendaDefinitionContract agendaContract : agendaContracts) {
 329  0
                                 this.agendas.add(AgendaDefinition.Builder.create(agendaContract));
 330  
                         }
 331  8
                 }
 332  
 
 333  
                 public void setAttributes(Map<String, String> attributes){
 334  8
                         if (attributes == null){
 335  0
                                 this.attributes = Collections.emptyMap();
 336  
                         }
 337  8
                         this.attributes = Collections.unmodifiableMap(attributes);
 338  8
                 }
 339  
                 
 340  
                 /**
 341  
          * Sets the version number for the style that will be returned by this
 342  
          * builder.
 343  
          * 
 344  
          * <p>In general, this value should not be manually set on the builder,
 345  
          * but rather copied from an existing {@link ContextDefinitionContract} when
 346  
          * invoking {@link Builder#create(ContextDefinitionContract)}.
 347  
          * 
 348  
          * @param versionNumber the version number to set
 349  
          */
 350  
                 public void setVersionNumber(Long versionNumber) {
 351  0
                         this.versionNumber = versionNumber;
 352  0
                 }
 353  
                 
 354  
         }
 355  
 
 356  
         @Override
 357  
     public int hashCode() {
 358  0
         return HashCodeBuilder.reflectionHashCode(this, Constants.HASH_CODE_EQUALS_EXCLUDE);
 359  
     }
 360  
 
 361  
     @Override
 362  
     public boolean equals(Object obj) {
 363  1
         return EqualsBuilder.reflectionEquals(obj, this, Constants.HASH_CODE_EQUALS_EXCLUDE);
 364  
     }
 365  
 
 366  
     @Override
 367  
     public String toString() {
 368  0
         return ToStringBuilder.reflectionToString(this);
 369  
     }
 370  
         
 371  
         /**
 372  
      * Defines some internal constants used on this class.
 373  
      */
 374  0
     public static class Constants {
 375  
         final static String ROOT_ELEMENT_NAME = "context";
 376  
         final static String TYPE_NAME = "ContextDefinitionType";
 377  1
         final static String[] HASH_CODE_EQUALS_EXCLUDE = {CoreConstants.CommonElements.FUTURE_ELEMENTS};
 378  
     }
 379  
 
 380  
     /**
 381  
      * A private class which exposes constants which define the XML element names to use
 382  
      * when this object is marshalled to XML.
 383  
      */
 384  0
     public static class Elements {
 385  
         final static String ID = "id";
 386  
         final static String NAMESPACE = "namespace";
 387  
         final static String NAME = "name";
 388  
         final static String TYPE_ID = "typeId";
 389  
         final static String AGENDA = "agenda";
 390  
         final static String AGENDAS = "agendas";
 391  
                 final static String ATTRIBUTES = "attributes";
 392  
     }
 393  
                 
 394  
         
 395  
         
 396  
 }