001package ca.uhn.fhir.jpa.api.model;
002
003/*
004 * #%L
005 * HAPI FHIR Storage api
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.model.cross.IBasePersistedResource;
024import ca.uhn.fhir.rest.api.server.storage.ResourcePersistentId;
025import org.hl7.fhir.instance.model.api.IBaseResource;
026import org.hl7.fhir.instance.model.api.IIdType;
027
028import java.util.function.Supplier;
029
030public class LazyDaoMethodOutcome extends DaoMethodOutcome {
031
032        private Supplier<EntityAndResource> myEntitySupplier;
033        private Supplier<IIdType> myIdSupplier;
034        private Runnable myEntitySupplierUseCallback;
035
036        /**
037         * Constructor
038         */
039        public LazyDaoMethodOutcome(ResourcePersistentId theResourcePersistentId) {
040                setPersistentId(theResourcePersistentId);
041        }
042
043        @Override
044        public IBasePersistedResource getEntity() {
045                IBasePersistedResource retVal = super.getEntity();
046                if (retVal == null) {
047                        tryToRunSupplier();
048                        retVal = super.getEntity();
049                }
050                return retVal;
051        }
052
053        private void tryToRunSupplier() {
054                if (myEntitySupplier != null) {
055                        EntityAndResource entityAndResource = myEntitySupplier.get();
056                        setEntity(entityAndResource.getEntity());
057                        setResource(entityAndResource.getResource());
058                        setId(entityAndResource.getResource().getIdElement());
059                        myEntitySupplierUseCallback.run();
060                }
061        }
062
063        @Override
064        public IIdType getId() {
065                IIdType retVal = super.getId();
066                if (retVal == null) {
067                        if (super.hasResource()) {
068                                retVal = getResource().getIdElement();
069                                setId(retVal);
070                        } else {
071                                if (myIdSupplier != null) {
072                                        retVal = myIdSupplier.get();
073                                        setId(retVal);
074                                }
075                        }
076                }
077                return retVal;
078        }
079
080        @Override
081        public IBaseResource getResource() {
082                IBaseResource retVal = super.getResource();
083                if (retVal == null) {
084                        tryToRunSupplier();
085                        retVal = super.getResource();
086                }
087                return retVal;
088        }
089
090        public void setEntitySupplier(Supplier<EntityAndResource> theEntitySupplier) {
091                myEntitySupplier = theEntitySupplier;
092        }
093
094        public void setEntitySupplierUseCallback(Runnable theEntitySupplierUseCallback) {
095                myEntitySupplierUseCallback = theEntitySupplierUseCallback;
096        }
097
098        public void setIdSupplier(Supplier<IIdType> theIdSupplier) {
099                myIdSupplier = theIdSupplier;
100        }
101
102
103        public static class EntityAndResource {
104                private final IBasePersistedResource myEntity;
105                private final IBaseResource myResource;
106
107                public EntityAndResource(IBasePersistedResource theEntity, IBaseResource theResource) {
108                        myEntity = theEntity;
109                        myResource = theResource;
110                }
111
112                public IBasePersistedResource getEntity() {
113                        return myEntity;
114                }
115
116                public IBaseResource getResource() {
117                        return myResource;
118                }
119        }
120
121}