View Javadoc

1   /*
2    * Copyright 2011 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 1.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/ecl1.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 java.util.Collection;
19  import java.util.HashMap;
20  import java.util.Map;
21  
22  import javax.xml.bind.annotation.XmlAccessType;
23  import javax.xml.bind.annotation.XmlAccessorType;
24  import javax.xml.bind.annotation.XmlAnyElement;
25  import javax.xml.bind.annotation.XmlElement;
26  import javax.xml.bind.annotation.XmlRootElement;
27  import javax.xml.bind.annotation.XmlType;
28  import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
29  
30  import org.apache.commons.lang.builder.EqualsBuilder;
31  import org.apache.commons.lang.builder.HashCodeBuilder;
32  import org.apache.commons.lang.builder.ToStringBuilder;
33  import org.kuali.rice.core.api.CoreConstants;
34  import org.kuali.rice.core.util.jaxb.MapStringStringAdapter;
35  import org.w3c.dom.Element;
36  
37  /**
38   * A set of criteria for selecting a {@link ContextDefinition}.
39   * 
40   * @author Kuali Rice Team (rice.collab@kuali.org)
41   *
42   */
43  @XmlRootElement(name = ContextSelectionCriteria.Constants.ROOT_ELEMENT_NAME)
44  @XmlAccessorType(XmlAccessType.NONE)
45  @XmlType(name = ContextSelectionCriteria.Constants.TYPE_NAME, propOrder = {
46  		ContextSelectionCriteria.Elements.NAMESPACE_CODE,
47  		ContextSelectionCriteria.Elements.NAME,
48  		ContextSelectionCriteria.Elements.CONTEXT_QUALIFIERS,
49          CoreConstants.CommonElements.FUTURE_ELEMENTS
50  })
51  public final class ContextSelectionCriteria {
52  
53  	@XmlElement(name = Elements.NAMESPACE_CODE, required = true)
54  	private final String namespaceCode;
55  	
56  	@XmlElement(name = Elements.NAME, required = false)
57  	private final String name;
58  	
59  	@XmlElement(name = Elements.CONTEXT_QUALIFIERS)
60  	@XmlJavaTypeAdapter(value = MapStringStringAdapter.class)
61  	private final Map<String, String> contextQualifiers;
62  	
63      @SuppressWarnings("unused")
64      @XmlAnyElement
65      private final Collection<Element> _futureElements = null;
66  	
67      /**
68       * Only used by JAXB.
69       */
70      @SuppressWarnings("unused")
71  	private ContextSelectionCriteria() {
72  		this.namespaceCode = null;
73  		this.name = null;
74  		this.contextQualifiers = null;
75  	}
76  	
77  	private ContextSelectionCriteria(String namespaceCode, String name, Map<String, String> contextQualifiers) {
78  		this.namespaceCode = namespaceCode;
79  		this.name = name;
80  		this.contextQualifiers = new HashMap<String, String>();
81  		if (contextQualifiers != null) {
82  			this.contextQualifiers.putAll(contextQualifiers);
83  		}
84  	}
85  	
86  	public static ContextSelectionCriteria newCriteria(String namespaceCode, String name, Map<String, String> contextQualifiers) {
87  		return new ContextSelectionCriteria(namespaceCode, name, contextQualifiers);
88  	}
89  	
90  	public static ContextSelectionCriteria newCriteria(String namespaceCode, Map<String, String> contextQualifiers) {
91  		return newCriteria(namespaceCode, null, contextQualifiers);
92  	}
93  	
94  	public static ContextSelectionCriteria newCriteria(Map<String, String> contextQualifiers) {
95  		return newCriteria(null, contextQualifiers);
96  	}
97  	
98  	public String getNamespaceCode() {
99  		return this.namespaceCode;
100 	}
101 
102 	public String getName() {
103 		return this.name;
104 	}
105 
106 	public Map<String, String> getContextQualifiers() {
107 		return this.contextQualifiers;
108 	}
109 
110 	@Override
111     public int hashCode() {
112         return HashCodeBuilder.reflectionHashCode(this, Constants.HASH_CODE_EQUALS_EXCLUDE);
113     }
114 
115     @Override
116     public boolean equals(Object obj) {
117         return EqualsBuilder.reflectionEquals(obj, this, Constants.HASH_CODE_EQUALS_EXCLUDE);
118     }
119 
120     @Override
121     public String toString() {
122         return ToStringBuilder.reflectionToString(this);
123     }
124 	
125     /**
126      * Defines some internal constants used on this class.
127      */
128     static class Constants {
129         final static String ROOT_ELEMENT_NAME = "contextSelectionCriteria";
130         final static String TYPE_NAME = "ContextSelectionCriteriaType";
131         final static String[] HASH_CODE_EQUALS_EXCLUDE = {CoreConstants.CommonElements.FUTURE_ELEMENTS};
132     }
133     
134 	/**
135      * A private class which exposes constants which define the XML element names to use
136      * when this object is marshaled to XML.
137      */
138     static class Elements {
139         final static String NAMESPACE_CODE = "namespaceCode";
140         final static String NAME = "name";
141         final static String CONTEXT_QUALIFIERS = "contextQualifiers";
142     }
143 	
144 }