001package ca.uhn.fhir.jpa.dao.expunge;
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.mdm.api.IMdmSettings;
024import ca.uhn.fhir.mdm.rules.config.MdmSettings;
025import org.springframework.beans.factory.annotation.Autowired;
026import org.springframework.stereotype.Service;
027
028import javax.annotation.Nonnull;
029import java.util.ArrayList;
030import java.util.Comparator;
031import java.util.List;
032
033@Service
034public class ResourceTableFKProvider {
035        @Autowired(required = false)
036        IMdmSettings myMdmSettings;
037
038        @Nonnull
039        public List<ResourceForeignKey> getResourceForeignKeys() {
040                List<ResourceForeignKey> retval = new ArrayList<>();
041
042                // To find all the FKs that need to be included here, run the following SQL in the INFORMATION_SCHEMA:
043                // SELECT FKTABLE_NAME, FKCOLUMN_NAME FROM CROSS_REFERENCES WHERE PKTABLE_NAME = 'HFJ_RESOURCE'
044
045                // Add some secondary related records that don't have foreign keys
046                retval.add(new ResourceForeignKey("HFJ_HISTORY_TAG", "RES_ID"));//NOT covered by index.
047                retval.add(new ResourceForeignKey("HFJ_RES_VER_PROV", "RES_PID"));
048
049                //These have the possibility of touching all resource types.
050                retval.add(new ResourceForeignKey("HFJ_FORCED_ID", "RESOURCE_PID"));
051                retval.add(new ResourceForeignKey("HFJ_IDX_CMP_STRING_UNIQ", "RES_ID"));
052                retval.add(new ResourceForeignKey("HFJ_IDX_CMB_TOK_NU", "RES_ID"));
053                retval.add(new ResourceForeignKey("HFJ_RES_LINK", "SRC_RESOURCE_ID"));
054                retval.add(new ResourceForeignKey("HFJ_RES_LINK", "TARGET_RESOURCE_ID"));
055                retval.add(new ResourceForeignKey("HFJ_RES_PARAM_PRESENT", "RES_ID"));
056                retval.add(new ResourceForeignKey("HFJ_RES_TAG", "RES_ID"));
057                retval.add(new ResourceForeignKey("HFJ_RES_VER", "RES_ID"));
058                retval.add(new ResourceForeignKey("HFJ_SPIDX_COORDS", "RES_ID"));
059                retval.add(new ResourceForeignKey("HFJ_SPIDX_DATE", "RES_ID"));
060                retval.add(new ResourceForeignKey("HFJ_SPIDX_NUMBER", "RES_ID"));
061                retval.add(new ResourceForeignKey("HFJ_SPIDX_QUANTITY", "RES_ID"));
062                retval.add(new ResourceForeignKey("HFJ_SPIDX_QUANTITY_NRML", "RES_ID"));
063                retval.add(new ResourceForeignKey("HFJ_SPIDX_STRING", "RES_ID"));
064                retval.add(new ResourceForeignKey("HFJ_SPIDX_TOKEN", "RES_ID"));
065                retval.add(new ResourceForeignKey("HFJ_SPIDX_URI", "RES_ID"));
066                retval.add(new ResourceForeignKey("MPI_LINK", "GOLDEN_RESOURCE_PID"));
067                retval.add(new ResourceForeignKey("MPI_LINK", "TARGET_PID"));
068                retval.add(new ResourceForeignKey("MPI_LINK", "PERSON_PID"));
069
070                //These only touch certain resource types.
071                retval.add(new ResourceForeignKey("TRM_CODESYSTEM_VER", "RES_ID"));
072                retval.add(new ResourceForeignKey("TRM_CODESYSTEM", "RES_ID"));
073                retval.add(new ResourceForeignKey("TRM_VALUESET", "RES_ID"));
074                retval.add(new ResourceForeignKey("TRM_CONCEPT_MAP", "RES_ID"));
075                retval.add(new ResourceForeignKey("NPM_PACKAGE_VER", "BINARY_RES_ID"));
076                retval.add(new ResourceForeignKey("NPM_PACKAGE_VER_RES", "BINARY_RES_ID"));
077
078                retval.add(new ResourceForeignKey("HFJ_SUBSCRIPTION_STATS", "RES_ID"));
079
080                return retval;
081        }
082        @Nonnull
083        public List<ResourceForeignKey> getResourceForeignKeysByResourceType(String theResourceType) {
084                List<ResourceForeignKey> retval = new ArrayList<>();
085                //These have the possibility of touching all resource types.
086                retval.add(new ResourceForeignKey("HFJ_HISTORY_TAG", "RES_ID"));
087                retval.add(new ResourceForeignKey("HFJ_RES_VER_PROV", "RES_PID"));
088                retval.add(new ResourceForeignKey("HFJ_FORCED_ID", "RESOURCE_PID"));
089                retval.add(new ResourceForeignKey("HFJ_IDX_CMP_STRING_UNIQ", "RES_ID"));
090                retval.add(new ResourceForeignKey("HFJ_IDX_CMB_TOK_NU", "RES_ID"));
091                retval.add(new ResourceForeignKey("HFJ_RES_LINK", "SRC_RESOURCE_ID"));
092                retval.add(new ResourceForeignKey("HFJ_RES_LINK", "TARGET_RESOURCE_ID"));
093                retval.add(new ResourceForeignKey("HFJ_RES_PARAM_PRESENT", "RES_ID"));
094                retval.add(new ResourceForeignKey("HFJ_RES_TAG", "RES_ID"));//TODO GGG: Res_ID + TAG_ID? is that enough?
095                retval.add(new ResourceForeignKey("HFJ_RES_VER", "RES_ID"));//TODO GGG: RES_ID + updated? is that enough?
096                retval.add(new ResourceForeignKey("HFJ_SPIDX_COORDS", "RES_ID"));
097                retval.add(new ResourceForeignKey("HFJ_SPIDX_DATE", "RES_ID"));
098                retval.add(new ResourceForeignKey("HFJ_SPIDX_NUMBER", "RES_ID"));
099                retval.add(new ResourceForeignKey("HFJ_SPIDX_QUANTITY", "RES_ID"));
100                retval.add(new ResourceForeignKey("HFJ_SPIDX_QUANTITY_NRML", "RES_ID"));
101                retval.add(new ResourceForeignKey("HFJ_SPIDX_STRING", "RES_ID"));
102                retval.add(new ResourceForeignKey("HFJ_SPIDX_TOKEN", "RES_ID"));
103                retval.add(new ResourceForeignKey("HFJ_SPIDX_URI", "RES_ID"));
104
105                if (myMdmSettings != null &&  myMdmSettings.isEnabled()) {
106                        retval.add(new ResourceForeignKey("MPI_LINK", "GOLDEN_RESOURCE_PID"));//NOT covered by index.
107                        retval.add(new ResourceForeignKey("MPI_LINK", "TARGET_PID"));//Possibly covered, partial index
108                        retval.add(new ResourceForeignKey("MPI_LINK", "PERSON_PID"));//TODO GGG: I don't even think we need this... this field is deprecated, and the deletion is covered by GOLDEN_RESOURCE_PID
109                }
110
111                switch (theResourceType.toLowerCase()) {
112                        case "binary":
113                                retval.add(new ResourceForeignKey("NPM_PACKAGE_VER", "BINARY_RES_ID"));//Not covered
114                                retval.add(new ResourceForeignKey("NPM_PACKAGE_VER_RES", "BINARY_RES_ID"));//Not covered
115                                break;
116                        case "subscription":
117                                retval.add(new ResourceForeignKey("HFJ_SUBSCRIPTION_STATS", "RES_ID"));//Covered by index.
118                                break;
119                        case "codesystem":
120                                retval.add(new ResourceForeignKey("TRM_CODESYSTEM_VER", "RES_ID"));//Not covered
121                                retval.add(new ResourceForeignKey("TRM_CODESYSTEM", "RES_ID"));//Not covered
122                                break;
123                        case "valueset":
124                                retval.add(new ResourceForeignKey("TRM_VALUESET", "RES_ID"));//Not covered
125                                break;
126                        case "conceptmap":
127                                retval.add(new ResourceForeignKey("TRM_CONCEPT_MAP", "RES_ID"));//Not covered
128                                break;
129                        default:
130                }
131                return retval;
132        }
133
134}