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 - 2020 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 ca.uhn.fhir.context.FhirContext;
024import ca.uhn.fhir.context.RuntimeResourceDefinition;
025import ca.uhn.fhir.model.api.IResource;
026import ca.uhn.fhir.model.dstu2.resource.StructureDefinition;
027import ca.uhn.fhir.model.primitive.IdDt;
028import ca.uhn.fhir.rest.annotation.IdParam;
029import ca.uhn.fhir.rest.annotation.Read;
030import ca.uhn.fhir.rest.annotation.Search;
031import ca.uhn.fhir.rest.server.IResourceProvider;
032import ca.uhn.fhir.rest.server.RestfulServer;
033import ca.uhn.fhir.rest.server.servlet.ServletRequestDetails;
034
035import java.util.ArrayList;
036import java.util.Collections;
037import java.util.Comparator;
038import java.util.List;
039
040public class ServerProfileProvider implements IResourceProvider {
041
042        private final FhirContext myContext;
043        private final RestfulServer myRestfulServer;
044
045        public ServerProfileProvider(RestfulServer theServer) {
046                myContext = theServer.getFhirContext();
047                myRestfulServer = theServer;
048        }
049        
050        @Override
051        public Class<? extends IResource> getResourceType() {
052                return StructureDefinition.class;
053        }
054        
055        @Read()
056        public StructureDefinition getProfileById(ServletRequestDetails theRequest, @IdParam IdDt theId) {
057                RuntimeResourceDefinition retVal = myContext.getResourceDefinitionById(theId.getIdPart());
058                if (retVal==null) {
059                        return null;
060                }
061                String serverBase = getServerBase(theRequest);
062                return (StructureDefinition) retVal.toProfile(serverBase);
063        }
064
065        @Search()
066        public List<StructureDefinition> getAllProfiles(ServletRequestDetails theRequest) {
067                final String serverBase = getServerBase(theRequest);
068                List<RuntimeResourceDefinition> defs = new ArrayList<>(myContext.getResourceDefinitionsWithExplicitId());
069                Collections.sort(defs, new Comparator<RuntimeResourceDefinition>() {
070                        @Override
071                        public int compare(RuntimeResourceDefinition theO1, RuntimeResourceDefinition theO2) {
072                                int cmp = theO1.getName().compareTo(theO2.getName());
073                                if (cmp==0) {
074                                        cmp=theO1.getResourceProfile(serverBase).compareTo(theO2.getResourceProfile(serverBase));
075                                }
076                                return cmp;
077                        }});
078                ArrayList<StructureDefinition> retVal = new ArrayList<>();
079                for (RuntimeResourceDefinition next : defs) {
080                        retVal.add((StructureDefinition) next.toProfile(serverBase));
081                }
082                return retVal;
083        }
084
085        private String getServerBase(ServletRequestDetails theHttpRequest) {
086                return myRestfulServer.getServerBaseForRequest(theHttpRequest);
087        }
088}