001/**
002 * Copyright 2005-2014 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.ksb.api.bus.support;
017
018import org.kuali.rice.core.api.util.jaxb.MapStringStringAdapter;
019
020import javax.xml.bind.annotation.XmlAccessType;
021import javax.xml.bind.annotation.XmlAccessorType;
022import javax.xml.bind.annotation.XmlElement;
023import javax.xml.bind.annotation.XmlRootElement;
024import javax.xml.bind.annotation.XmlType;
025import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
026import java.util.Collections;
027import java.util.HashMap;
028import java.util.Map;
029
030@XmlRootElement(name = RestServiceConfiguration.Constants.ROOT_ELEMENT_NAME)
031@XmlAccessorType(XmlAccessType.NONE)
032@XmlType(name = RestServiceConfiguration.Constants.TYPE_NAME, propOrder = {
033                RestServiceConfiguration.Elements.RESOURCE_CLASS,
034                RestServiceConfiguration.Elements.RESOURCE_TO_CLASS_NAME_MAP
035})
036public final class RestServiceConfiguration extends AbstractServiceConfiguration {
037
038        private static final long serialVersionUID = -4226512121638441108L;
039
040        @XmlElement(name = Elements.RESOURCE_CLASS, required = false)
041        private final String resourceClass;
042        
043        @XmlJavaTypeAdapter(MapStringStringAdapter.class)
044        @XmlElement(name = Elements.RESOURCE_TO_CLASS_NAME_MAP, required = false)
045        private final Map<String, String> resourceToClassNameMap;
046        
047        /**
048     * Private constructor used only by JAXB.
049     */
050        private RestServiceConfiguration() {
051                super();
052                this.resourceClass = null;
053                this.resourceToClassNameMap = null;
054        }
055        
056        private RestServiceConfiguration(Builder builder) {
057                super(builder);
058                this.resourceClass = builder.getResourceClass();
059                if (builder.getResourceToClassNameMap() != null) {
060                        this.resourceToClassNameMap = new HashMap<String, String>(builder.getResourceToClassNameMap());
061                } else {
062                        this.resourceToClassNameMap = Collections.emptyMap();
063                }
064        }
065        
066        public static RestServiceConfiguration fromServiceDefinition(RestServiceDefinition restServiceDefinition) {
067                return Builder.create(restServiceDefinition).build();
068        }
069                
070        public String getResourceClass() {
071                return this.resourceClass;
072        }
073        
074        /**
075         * Returns a map of resource names to resource classes.  Can be null if
076         * there are no mapped resources on this service configuration.
077         * 
078         * @return the resource to class name map, or null if no resources have
079         * been mapped
080         */
081        public Map<String, String> getResourceToClassNameMap() {
082                return this.resourceToClassNameMap;
083        }
084        
085        /**
086         * @param className
087         * @return true if this service contains a resource for the given class name
088         */
089        public boolean hasClass(String className) {
090                if (resourceToClassNameMap == null) {
091                        return false;
092                }
093                return resourceToClassNameMap.containsValue(className);
094        }
095        
096        public static final class Builder extends AbstractServiceConfiguration.Builder<RestServiceConfiguration> {
097
098                private static final long serialVersionUID = 4300659121377259098L;
099
100                private String resourceClass;
101                private Map<String, String> resourceToClassNameMap;
102                                
103                public String getResourceClass() {
104                        return resourceClass;
105                }
106
107                public void setResourceClass(String resourceClass) {
108                        this.resourceClass = resourceClass;
109                }
110
111                public Map<String, String> getResourceToClassNameMap() {
112                        return resourceToClassNameMap;
113                }
114
115                public void setResourceToClassNameMap(Map<String, String> resourceToClassNameMap) {
116                        this.resourceToClassNameMap = resourceToClassNameMap;
117                }
118                
119                private Builder() {
120                }
121                
122                public static Builder create() {
123                        return new Builder();
124                }
125                
126                public static Builder create(RestServiceDefinition restServiceDefinition) {
127                        Builder builder = create();
128                        builder.copyServiceDefinitionProperties(restServiceDefinition);
129                        builder.setResourceClass(restServiceDefinition.getResourceClass());
130                        if (restServiceDefinition.getResourceToClassNameMap() != null) {
131                                builder.setResourceToClassNameMap(restServiceDefinition.getResourceToClassNameMap());
132                        }
133                        return builder;
134                }
135
136                @Override
137                public RestServiceConfiguration build() {
138                        return new RestServiceConfiguration(this);
139                }
140                
141        }
142        
143        /**
144     * Defines some internal constants used on this class.
145     */
146    static class Constants {
147        final static String ROOT_ELEMENT_NAME = "restServiceConfiguration";
148        final static String TYPE_NAME = "RestServiceConfigurationType";
149    }
150
151    /**
152     * A private class which exposes constants which define the XML element names to use
153     * when this object is marshalled to XML.
154     */
155     static class Elements {
156        protected final static String RESOURCE_CLASS = "resourceClass";
157        protected final static String RESOURCE_TO_CLASS_NAME_MAP = "resourceToClassNameMap";
158    }
159
160        
161}