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.packages.loader;
021
022import ca.uhn.fhir.context.FhirContext;
023import ca.uhn.fhir.i18n.Msg;
024import ca.uhn.fhir.rest.server.exceptions.InternalErrorException;
025import org.hl7.fhir.instance.model.api.IBaseResource;
026import org.hl7.fhir.utilities.npm.NpmPackage;
027
028import java.io.IOException;
029import java.util.ArrayList;
030import java.util.Collections;
031import java.util.List;
032
033public class PackageResourceParsingSvc {
034
035        private final FhirContext myFhirContext;
036
037        public PackageResourceParsingSvc(FhirContext theContext) {
038                myFhirContext = theContext;
039        }
040
041        /**
042         * Parses out resource of theType from provided package
043         * @param theType - the resource type
044         * @param thePkg - the npm package
045         * @return - a list of all resources that match type theType in package thePkg
046         */
047        public List<IBaseResource> parseResourcesOfType(String theType, NpmPackage thePkg) {
048                if (!thePkg.getFolders().containsKey("package")) {
049                        return Collections.emptyList();
050                }
051                ArrayList<IBaseResource> resources = new ArrayList<>();
052                List<String> filesForType = null;
053                try {
054                        filesForType = thePkg.getFolders().get("package").getTypes().get(theType);
055                } catch (IOException e) {
056                        throw new InternalErrorException(
057                                        Msg.code(2370) + "Cannot install resource of type " + theType + ": Could not get types", e);
058                }
059                if (filesForType != null) {
060                        for (String file : filesForType) {
061                                try {
062                                        byte[] content = thePkg.getFolders().get("package").fetchFile(file);
063                                        resources.add(myFhirContext.newJsonParser().parseResource(new String(content)));
064                                } catch (IOException e) {
065                                        throw new InternalErrorException(
066                                                        Msg.code(1289) + "Cannot install resource of type " + theType + ": Could not fetch file "
067                                                                        + file,
068                                                        e);
069                                }
070                        }
071                }
072                return resources;
073        }
074}