001package ca.uhn.fhir.jpa.dao.r4;
002
003/*
004 * #%L
005 * HAPI FHIR JPA Server
006 * %%
007 * Copyright (C) 2014 - 2021 Smile CDR, Inc.
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.jpa.dao.BaseHapiFhirSystemDao;
024import ca.uhn.fhir.jpa.dao.FhirResourceDaoMessageHeaderDstu2;
025import ca.uhn.fhir.jpa.model.entity.TagDefinition;
026import ca.uhn.fhir.rest.api.RestOperationTypeEnum;
027import ca.uhn.fhir.rest.api.server.RequestDetails;
028import ca.uhn.fhir.rest.server.interceptor.IServerInterceptor.ActionRequestDetails;
029import org.hl7.fhir.instance.model.api.IBaseBundle;
030import org.hl7.fhir.r4.model.Bundle;
031import org.hl7.fhir.r4.model.Meta;
032
033import javax.persistence.TypedQuery;
034import java.util.Collection;
035import java.util.List;
036
037public class FhirSystemDaoR4 extends BaseHapiFhirSystemDao<Bundle, Meta> {
038
039        @Override
040        public Meta metaGetOperation(RequestDetails theRequestDetails) {
041                // Notify interceptors
042                ActionRequestDetails requestDetails = new ActionRequestDetails(theRequestDetails);
043                notifyInterceptors(RestOperationTypeEnum.META, requestDetails);
044
045                String sql = "SELECT d FROM TagDefinition d WHERE d.myId IN (SELECT DISTINCT t.myTagId FROM ResourceTag t)";
046                TypedQuery<TagDefinition> q = myEntityManager.createQuery(sql, TagDefinition.class);
047                List<TagDefinition> tagDefinitions = q.getResultList();
048
049                return toMeta(tagDefinitions);
050        }
051
052        @Override
053        public IBaseBundle processMessage(RequestDetails theRequestDetails, IBaseBundle theMessage) {
054                return FhirResourceDaoMessageHeaderDstu2.throwProcessMessageNotImplemented();
055        }
056
057
058        protected Meta toMeta(Collection<TagDefinition> tagDefinitions) {
059                Meta retVal = new Meta();
060                for (TagDefinition next : tagDefinitions) {
061                        switch (next.getTagType()) {
062                                case PROFILE:
063                                        retVal.addProfile(next.getCode());
064                                        break;
065                                case SECURITY_LABEL:
066                                        retVal.addSecurity().setSystem(next.getSystem()).setCode(next.getCode()).setDisplay(next.getDisplay());
067                                        break;
068                                case TAG:
069                                        retVal.addTag().setSystem(next.getSystem()).setCode(next.getCode()).setDisplay(next.getDisplay());
070                                        break;
071                        }
072                }
073                return retVal;
074        }
075
076}