001package ca.uhn.fhir.jpa.config;
002
003/*-
004 * #%L
005 * HAPI FHIR JPA Server
006 * %%
007 * Copyright (C) 2014 - 2023 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.util.ReflectionUtil;
024import com.google.common.annotations.VisibleForTesting;
025import org.apache.commons.lang3.StringUtils;
026import org.apache.commons.lang3.Validate;
027import org.hibernate.dialect.Dialect;
028import org.hibernate.search.engine.cfg.BackendSettings;
029import org.springframework.beans.factory.annotation.Autowired;
030import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
031
032import javax.sql.DataSource;
033
034public class HibernatePropertiesProvider {
035
036        @Autowired
037        private LocalContainerEntityManagerFactoryBean myEntityManagerFactory;
038        private Dialect myDialect;
039        private String myHibernateSearchBackend;
040
041        @VisibleForTesting
042        public void setDialectForUnitTest(Dialect theDialect) {
043                myDialect = theDialect;
044        }
045
046        public Dialect getDialect() {
047                Dialect dialect = myDialect;
048                if (dialect == null) {
049                        String dialectClass = (String) myEntityManagerFactory.getJpaPropertyMap().get("hibernate.dialect");
050                        dialect = ReflectionUtil.newInstanceOrReturnNull(dialectClass, Dialect.class);
051                        Validate.notNull(dialect, "Unable to create instance of class: %s", dialectClass);
052                        myDialect = dialect;
053                }
054                return dialect;
055        }
056
057        public String getHibernateSearchBackend(){
058                String hibernateSearchBackend = myHibernateSearchBackend;
059                if (StringUtils.isBlank(hibernateSearchBackend)) {
060                        hibernateSearchBackend = (String) myEntityManagerFactory.getJpaPropertyMap().get(BackendSettings.backendKey(BackendSettings.TYPE));
061                        Validate.notNull(hibernateSearchBackend, BackendSettings.backendKey(BackendSettings.TYPE) + " property is unset!");
062                        myHibernateSearchBackend = hibernateSearchBackend;
063                }
064                return myHibernateSearchBackend;
065        }
066
067
068        public DataSource getDataSource() {
069                return myEntityManagerFactory.getDataSource();
070        }
071}