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