001package ca.uhn.fhir.rest.server.provider.dstu2;
002
003/*
004 * #%L
005 * HAPI FHIR Structures - DSTU2 (FHIR v1.0.0)
006 * %%
007 * Copyright (C) 2014 - 2016 University Health Network
008 * %%
009 * Licensed under the Apache License, Version 2.0 (the "License");
010 * you may not use this file except in compliance with the License.
011 * You may obtain a copy of the License at
012 * 
013 *      http://www.apache.org/licenses/LICENSE-2.0
014 * 
015 * Unless required by applicable law or agreed to in writing, software
016 * distributed under the License is distributed on an "AS IS" BASIS,
017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018 * See the License for the specific language governing permissions and
019 * limitations under the License.
020 * #L%
021 */
022
023import java.util.ArrayList;
024import java.util.Collections;
025import java.util.Comparator;
026import java.util.List;
027
028import javax.servlet.http.HttpServletRequest;
029
030import ca.uhn.fhir.context.FhirContext;
031import ca.uhn.fhir.context.RuntimeResourceDefinition;
032import ca.uhn.fhir.model.api.IResource;
033import ca.uhn.fhir.model.dstu2.resource.StructureDefinition;
034import ca.uhn.fhir.model.primitive.IdDt;
035import ca.uhn.fhir.rest.annotation.IdParam;
036import ca.uhn.fhir.rest.annotation.Read;
037import ca.uhn.fhir.rest.annotation.Search;
038import ca.uhn.fhir.rest.server.IResourceProvider;
039import ca.uhn.fhir.rest.server.RestfulServer;
040
041public class ServerProfileProvider implements IResourceProvider {
042
043        private final FhirContext myContext;
044        private final RestfulServer myRestfulServer;
045
046        public ServerProfileProvider(RestfulServer theServer) {
047                myContext = theServer.getFhirContext();
048                myRestfulServer = theServer;
049        }
050        
051        @Override
052        public Class<? extends IResource> getResourceType() {
053                return StructureDefinition.class;
054        }
055        
056        @Read()
057        public StructureDefinition getProfileById(HttpServletRequest theRequest, @IdParam IdDt theId) {
058                RuntimeResourceDefinition retVal = myContext.getResourceDefinitionById(theId.getIdPart());
059                if (retVal==null) {
060                        return null;
061                }
062                String serverBase = getServerBase(theRequest);
063                return (StructureDefinition) retVal.toProfile(serverBase);
064        }
065
066        @Search()
067        public List<StructureDefinition> getAllProfiles(HttpServletRequest theRequest) {
068                final String serverBase = getServerBase(theRequest);
069                List<RuntimeResourceDefinition> defs = new ArrayList<RuntimeResourceDefinition>(myContext.getResourceDefinitions());
070                Collections.sort(defs, new Comparator<RuntimeResourceDefinition>() {
071                        @Override
072                        public int compare(RuntimeResourceDefinition theO1, RuntimeResourceDefinition theO2) {
073                                int cmp = theO1.getName().compareTo(theO2.getName());
074                                if (cmp==0) {
075                                        cmp=theO1.getResourceProfile(serverBase).compareTo(theO2.getResourceProfile(serverBase));
076                                }
077                                return cmp;
078                        }});
079                ArrayList<StructureDefinition> retVal = new ArrayList<StructureDefinition>();
080                for (RuntimeResourceDefinition next : defs) {
081                        retVal.add((StructureDefinition) next.toProfile(serverBase));
082                }
083                return retVal;
084        }
085
086        private String getServerBase(HttpServletRequest theHttpRequest) {
087                return myRestfulServer.getServerBaseForRequest(theHttpRequest);
088        }
089}