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