001/*
002 * #%L
003 * HAPI FHIR JPA Server
004 * %%
005 * Copyright (C) 2014 - 2024 Smile CDR, Inc.
006 * %%
007 * Licensed under the Apache License, Version 2.0 (the "License");
008 * you may not use this file except in compliance with the License.
009 * You may obtain a copy of the License at
010 *
011 * http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 * #L%
019 */
020package ca.uhn.fhir.jpa.dao.r4;
021
022import ca.uhn.fhir.jpa.dao.BaseHapiFhirSystemDao;
023import ca.uhn.fhir.jpa.dao.JpaResourceDao;
024import ca.uhn.fhir.jpa.model.entity.TagDefinition;
025import ca.uhn.fhir.rest.api.server.RequestDetails;
026import jakarta.persistence.TypedQuery;
027import org.hl7.fhir.instance.model.api.IBaseBundle;
028import org.hl7.fhir.r4.model.Bundle;
029import org.hl7.fhir.r4.model.Meta;
030
031import java.util.Collection;
032import java.util.List;
033
034public class FhirSystemDaoR4 extends BaseHapiFhirSystemDao<Bundle, Meta> {
035
036        @Override
037        public Meta metaGetOperation(RequestDetails theRequestDetails) {
038                String sql = "SELECT d FROM TagDefinition d WHERE d.myId IN (SELECT DISTINCT t.myTagId FROM ResourceTag t)";
039                TypedQuery<TagDefinition> q = myEntityManager.createQuery(sql, TagDefinition.class);
040                List<TagDefinition> tagDefinitions = q.getResultList();
041
042                return toMeta(tagDefinitions);
043        }
044
045        @Override
046        public IBaseBundle processMessage(RequestDetails theRequestDetails, IBaseBundle theMessage) {
047                return JpaResourceDao.throwProcessMessageNotImplemented();
048        }
049
050        protected Meta toMeta(Collection<TagDefinition> tagDefinitions) {
051                Meta retVal = new Meta();
052                for (TagDefinition next : tagDefinitions) {
053                        switch (next.getTagType()) {
054                                case PROFILE:
055                                        retVal.addProfile(next.getCode());
056                                        break;
057                                case SECURITY_LABEL:
058                                        retVal.addSecurity()
059                                                        .setSystem(next.getSystem())
060                                                        .setCode(next.getCode())
061                                                        .setDisplay(next.getDisplay());
062                                        break;
063                                case TAG:
064                                        retVal.addTag()
065                                                        .setSystem(next.getSystem())
066                                                        .setCode(next.getCode())
067                                                        .setDisplay(next.getDisplay());
068                                        break;
069                        }
070                }
071                return retVal;
072        }
073}