001/**
002 * Copyright 2005-2016 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.krms.api.repository.context;
017
018import org.kuali.rice.core.api.CoreConstants;
019import org.kuali.rice.core.api.mo.AbstractDataTransferObject;
020import org.kuali.rice.core.api.util.jaxb.MapStringStringAdapter;
021import org.w3c.dom.Element;
022
023import javax.xml.bind.annotation.XmlAccessType;
024import javax.xml.bind.annotation.XmlAccessorType;
025import javax.xml.bind.annotation.XmlAnyElement;
026import javax.xml.bind.annotation.XmlElement;
027import javax.xml.bind.annotation.XmlRootElement;
028import javax.xml.bind.annotation.XmlType;
029import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
030import java.util.Collection;
031import java.util.HashMap;
032import java.util.Map;
033
034/**
035 * A set of criteria for selecting a {@link ContextDefinition}.
036 * 
037 * @author Kuali Rice Team (rice.collab@kuali.org)
038 *
039 */
040@XmlRootElement(name = ContextSelectionCriteria.Constants.ROOT_ELEMENT_NAME)
041@XmlAccessorType(XmlAccessType.NONE)
042@XmlType(name = ContextSelectionCriteria.Constants.TYPE_NAME, propOrder = {
043                ContextSelectionCriteria.Elements.NAMESPACE_CODE,
044                ContextSelectionCriteria.Elements.NAME,
045                ContextSelectionCriteria.Elements.CONTEXT_QUALIFIERS,
046        CoreConstants.CommonElements.FUTURE_ELEMENTS
047})
048public final class ContextSelectionCriteria extends AbstractDataTransferObject {
049
050        @XmlElement(name = Elements.NAMESPACE_CODE, required = true)
051        private final String namespaceCode;
052        
053        @XmlElement(name = Elements.NAME, required = false)
054        private final String name;
055        
056        @XmlElement(name = Elements.CONTEXT_QUALIFIERS)
057        @XmlJavaTypeAdapter(value = MapStringStringAdapter.class)
058        private final Map<String, String> contextQualifiers;
059        
060    @SuppressWarnings("unused")
061    @XmlAnyElement
062    private final Collection<Element> _futureElements = null;
063        
064    /**
065     * Only used by JAXB.
066     */
067    @SuppressWarnings("unused")
068        private ContextSelectionCriteria() {
069                this.namespaceCode = null;
070                this.name = null;
071                this.contextQualifiers = null;
072        }
073
074    /**
075     * Private constructor used by factory methods
076     */
077    private ContextSelectionCriteria(String namespaceCode, String name, Map<String, String> contextQualifiers) {
078                this.namespaceCode = namespaceCode;
079                this.name = name;
080                this.contextQualifiers = new HashMap<String, String>();
081                if (contextQualifiers != null) {
082                        this.contextQualifiers.putAll(contextQualifiers);
083                }
084        }
085        
086    /**
087     * Factory method returns a new context selection criteria object with the fields set to
088     * the parameters provided.
089     * @param namespaceCode the namespace of the context
090     * @param name the name of the context
091     * @param contextQualifiers a Map of name value pair strings representing the list of qualifiers
092     * to use as selection criteria.
093     */
094        public static ContextSelectionCriteria newCriteria(String namespaceCode, String name, Map<String, String> contextQualifiers) {
095                return new ContextSelectionCriteria(namespaceCode, name, contextQualifiers);
096        }
097        
098    /**
099     * Factory method returns a new context selection criteria object with the namespace and contextQualifiers fields
100     * set to the parameters provided. The name field is set to null.
101     * @param namespaceCode the namespace of the context
102     * @param contextQualifiers a Map of name value pair strings representing the list of qualifiers
103     * to use as selection criteria.
104     */
105        public static ContextSelectionCriteria newCriteria(String namespaceCode, Map<String, String> contextQualifiers) {
106                return newCriteria(namespaceCode, null, contextQualifiers);
107        }
108        
109    /**
110    * Factory method returns a new context selection criteria object with the contextQualifiers property
111    * set to the parameter provided. The name and namespace properties are set to null.
112    * @param contextQualifiers a Map of name value pair strings representing the list of qualifiers
113    * to use as selection criteria.
114    */
115        public static ContextSelectionCriteria newCriteria(Map<String, String> contextQualifiers) {
116                return newCriteria(null, contextQualifiers);
117        }
118
119    /**
120     * Returns the namespace of the context.
121     * @return the namespace code of the context
122     */
123        public String getNamespaceCode() {
124                return this.namespaceCode;
125        }
126
127    /**
128     * Returns the name of the context
129     * @return the name of the context
130     */
131        public String getName() {
132                return this.name;
133        }
134
135    /**
136     * Returns the list of qualifiers as a map to name, value pair strings.
137     * @return a map containing the qualifier name, value pairs.
138     */
139        public Map<String, String> getContextQualifiers() {
140                return this.contextQualifiers;
141        }
142        
143    /**
144     * Defines some internal constants used on this class.
145     */
146    static class Constants {
147        final static String ROOT_ELEMENT_NAME = "contextSelectionCriteria";
148        final static String TYPE_NAME = "ContextSelectionCriteriaType";
149    }
150    
151        /**
152     * A private class which exposes constants which define the XML element names to use
153     * when this object is marshaled to XML.
154     */
155    static class Elements {
156        final static String NAMESPACE_CODE = "namespaceCode";
157        final static String NAME = "name";
158        final static String CONTEXT_QUALIFIERS = "contextQualifiers";
159    }
160        
161}