001package ca.uhn.fhir.jpa.migrate.tasks; 002 003/*- 004 * #%L 005 * HAPI FHIR Server - SQL Migration 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.i18n.Msg; 024import ca.uhn.fhir.context.ConfigurationException; 025import ca.uhn.fhir.jpa.migrate.DriverTypeEnum; 026import ca.uhn.fhir.jpa.migrate.tasks.api.ISchemaInitializationProvider; 027import com.google.common.base.Charsets; 028import org.apache.commons.io.IOUtils; 029import org.apache.commons.lang3.builder.HashCodeBuilder; 030 031import javax.annotation.Nonnull; 032import java.io.IOException; 033import java.io.InputStream; 034import java.util.ArrayList; 035import java.util.List; 036 037import static org.apache.commons.lang3.StringUtils.isBlank; 038 039public class SchemaInitializationProvider implements ISchemaInitializationProvider { 040 041 private final String mySchemaExistsIndicatorTable; 042 private final boolean myCanInitializeSchema; 043 private String mySchemaFileClassPath; 044 private String mySchemaDescription; 045 046 /** 047 * @param theSchemaFileClassPath pathname to script used to initialize schema 048 * @param theSchemaExistsIndicatorTable a table name we can use to determine if this schema has already been initialized 049 * @param theCanInitializeSchema this is a "root" schema initializer that creates the primary tables used by this app 050 */ 051 public SchemaInitializationProvider(String theSchemaDescription, String theSchemaFileClassPath, String theSchemaExistsIndicatorTable, boolean theCanInitializeSchema) { 052 mySchemaDescription = theSchemaDescription; 053 mySchemaFileClassPath = theSchemaFileClassPath; 054 mySchemaExistsIndicatorTable = theSchemaExistsIndicatorTable; 055 myCanInitializeSchema = theCanInitializeSchema; 056 } 057 058 @Override 059 public List<String> getSqlStatements(DriverTypeEnum theDriverType) { 060 List<String> retval = new ArrayList<>(); 061 062 String initScript = mySchemaFileClassPath + "/" + getInitScript(theDriverType); 063 try { 064 InputStream sqlFileInputStream = SchemaInitializationProvider.class.getResourceAsStream(initScript); 065 if (sqlFileInputStream == null) { 066 throw new ConfigurationException(Msg.code(49) + "Schema initialization script " + initScript + " not found on classpath"); 067 } 068 // Assumes no escaped semicolons... 069 String sqlString = IOUtils.toString(sqlFileInputStream, Charsets.UTF_8); 070 String sqlStringNoComments = preProcessSqlString(theDriverType, sqlString); 071 String[] statements = sqlStringNoComments.split("\\;"); 072 for (String statement : statements) { 073 String cleanedStatement = preProcessSqlStatement(theDriverType, statement); 074 if (!isBlank(cleanedStatement)) { 075 retval.add(cleanedStatement); 076 } 077 } 078 } catch (IOException e) { 079 throw new ConfigurationException(Msg.code(50) + "Error reading schema initialization script " + initScript, e); 080 } 081 return retval; 082 } 083 084 protected String preProcessSqlString(DriverTypeEnum theDriverType, String sqlString) { 085 return sqlString; 086 } 087 088 protected String preProcessSqlStatement(DriverTypeEnum theDriverType, String sqlStatement) { 089 return sqlStatement; 090 } 091 092 @Nonnull 093 protected String getInitScript(DriverTypeEnum theDriverType) { 094 return theDriverType.getSchemaFilename(); 095 } 096 097 @Override 098 public boolean equals(Object theO) { 099 if (this == theO) return true; 100 101 if (theO == null || getClass() != theO.getClass()) return false; 102 103 SchemaInitializationProvider that = (SchemaInitializationProvider) theO; 104 105 return this.getClass().getSimpleName() == that.getClass().getSimpleName(); 106 } 107 108 @Override 109 public int hashCode() { 110 return new HashCodeBuilder(17, 37) 111 .append(this.getClass().getSimpleName()) 112 .toHashCode(); 113 } 114 115 @Override 116 public String getSchemaExistsIndicatorTable() { 117 return mySchemaExistsIndicatorTable; 118 } 119 120 public SchemaInitializationProvider setSchemaFileClassPath(String theSchemaFileClassPath) { 121 mySchemaFileClassPath = theSchemaFileClassPath; 122 return this; 123 } 124 125 @Override 126 public String getSchemaDescription() { 127 return mySchemaDescription; 128 } 129 130 @Override 131 public SchemaInitializationProvider setSchemaDescription(String theSchemaDescription) { 132 mySchemaDescription = theSchemaDescription; 133 return this; 134 } 135 136 @Override 137 public boolean canInitializeSchema() { 138 return myCanInitializeSchema; 139 } 140} 141