001/*-
002 * #%L
003 * HAPI FHIR Server - SQL Migration
004 * %%
005 * Copyright (C) 2014 - 2023 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.migrate.taskdef;
021
022import ca.uhn.fhir.jpa.migrate.DriverTypeEnum;
023import ca.uhn.fhir.jpa.migrate.JdbcUtils;
024import ca.uhn.fhir.jpa.migrate.tasks.api.ISchemaInitializationProvider;
025import org.apache.commons.lang3.builder.EqualsBuilder;
026import org.apache.commons.lang3.builder.HashCodeBuilder;
027import org.slf4j.Logger;
028import org.slf4j.LoggerFactory;
029
030import java.sql.SQLException;
031import java.util.List;
032import java.util.Set;
033
034public class InitializeSchemaTask extends BaseTask {
035        private static final String DESCRIPTION_PREFIX = "Initialize schema for ";
036        private static final Logger ourLog = LoggerFactory.getLogger(InitializeSchemaTask.class);
037        private final ISchemaInitializationProvider mySchemaInitializationProvider;
038        private boolean myInitializedSchema;
039
040        public InitializeSchemaTask(
041                        String theProductVersion,
042                        String theSchemaVersion,
043                        ISchemaInitializationProvider theSchemaInitializationProvider) {
044                super(theProductVersion, theSchemaVersion);
045                mySchemaInitializationProvider = theSchemaInitializationProvider;
046                setDescription(DESCRIPTION_PREFIX + mySchemaInitializationProvider.getSchemaDescription());
047        }
048
049        @Override
050        public boolean isRunDuringSchemaInitialization() {
051                return true;
052        }
053
054        @Override
055        public void validate() {
056                // nothing
057        }
058
059        @Override
060        public void doExecute() throws SQLException {
061                DriverTypeEnum driverType = getDriverType();
062
063                Set<String> tableNames = JdbcUtils.getTableNames(getConnectionProperties());
064                String schemaExistsIndicatorTable = mySchemaInitializationProvider.getSchemaExistsIndicatorTable();
065                if (tableNames.contains(schemaExistsIndicatorTable)) {
066                        logInfo(
067                                        ourLog,
068                                        "The table {} already exists.  Skipping schema initialization for {}",
069                                        schemaExistsIndicatorTable,
070                                        driverType);
071                        return;
072                }
073
074                logInfo(
075                                ourLog,
076                                "Initializing {} schema for {}",
077                                driverType,
078                                mySchemaInitializationProvider.getSchemaDescription());
079
080                List<String> sqlStatements = mySchemaInitializationProvider.getSqlStatements(driverType);
081
082                for (String nextSql : sqlStatements) {
083                        executeSql(null, nextSql);
084                }
085
086                if (mySchemaInitializationProvider.canInitializeSchema()) {
087                        myInitializedSchema = true;
088                }
089
090                logInfo(
091                                ourLog,
092                                "{} schema for {} initialized successfully",
093                                driverType,
094                                mySchemaInitializationProvider.getSchemaDescription());
095        }
096
097        @Override
098        public boolean initializedSchema() {
099                return myInitializedSchema;
100        }
101
102        @Override
103        protected void generateEquals(EqualsBuilder theBuilder, BaseTask theOtherObject) {
104                InitializeSchemaTask otherObject = (InitializeSchemaTask) theOtherObject;
105                theBuilder.append(mySchemaInitializationProvider, otherObject.mySchemaInitializationProvider);
106        }
107
108        @Override
109        protected void generateHashCode(HashCodeBuilder theBuilder) {
110                theBuilder.append(mySchemaInitializationProvider);
111        }
112
113        public ISchemaInitializationProvider getSchemaInitializationProvider() {
114                return mySchemaInitializationProvider;
115        }
116}