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