001/*
002 *    Copyright 2024-2025, Warm-Flow (290631660@qq.com).
003 *
004 *    Licensed under the Apache License, Version 2.0 (the "License");
005 *    you may not use this file except in compliance with the License.
006 *    You may obtain a copy of the License at
007 *
008 *       https://www.apache.org/licenses/LICENSE-2.0
009 *
010 *    Unless required by applicable law or agreed to in writing, software
011 *    distributed under the License is distributed on an "AS IS" BASIS,
012 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *    See the License for the specific language governing permissions and
014 *    limitations under the License.
015 */
016package org.dromara.warm.flow.spring.boot.config;
017
018import org.dromara.warm.flow.core.constant.ExceptionCons;
019import org.dromara.warm.flow.core.exception.FlowException;
020import org.dromara.warm.flow.orm.utils.FlowJpaConfigCons;
021import org.dromara.warm.plugin.modes.sb.config.BeanConfig;
022import org.dromara.warm.plugin.modes.sb.utils.SpringUtil;
023import org.slf4j.Logger;
024import org.slf4j.LoggerFactory;
025import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
026import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
027import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
028import org.springframework.context.annotation.Bean;
029import org.springframework.context.annotation.Configuration;
030import org.springframework.context.annotation.DependsOn;
031import org.springframework.core.env.Environment;
032import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
033
034import javax.persistence.spi.PersistenceProvider;
035import javax.sql.DataSource;
036
037/**
038 * @author warm
039 * @description: 工作流bean注册配置
040 * @date: 2023/6/5 23:01
041 */
042@Configuration
043@ConditionalOnProperty(value = "warm-flow.enabled", havingValue = "true", matchIfMissing = true)
044public class FlowAutoConfig extends BeanConfig {
045
046    private static final Logger log = LoggerFactory.getLogger(FlowAutoConfig.class);
047
048    @Bean
049    @ConditionalOnMissingBean
050    public JpaProperties jpaProperties() {
051        return new JpaProperties();
052    }
053
054    @SuppressWarnings({"unchecked"})
055    @Bean(name = "entityManagerFactoryWarmFlow")
056    @DependsOn("warmFlowSpringUtil")
057    public LocalContainerEntityManagerFactoryBean primaryEntityManagerFactory(DataSource dataSource, JpaProperties jpaProperties) throws ClassNotFoundException {
058        LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
059        factory.setDataSource(dataSource);
060        factory.setJpaPropertyMap(jpaProperties.getProperties());
061        factory.setMappingResources("META-INF/warm-flow-orm.xml");
062        factory.setPersistenceXmlLocation("classpath:/META-INF/warm-flow-persistence.xml");
063        String jpaPersistenceProvider = SpringUtil.getBean(Environment.class).getProperty(FlowJpaConfigCons.JPA_PERSISTENCE_PROVIDER);
064        log.info("warm-flow use jpa persistence provider to be: {}", jpaPersistenceProvider);
065        if (jpaPersistenceProvider == null) {
066            throw new FlowException(ExceptionCons.JPA_PERSISTENCE_PROVIDER_NOT_FOUND);
067        }
068        factory.setPersistenceProviderClass((Class<? extends PersistenceProvider>) Class.forName(jpaPersistenceProvider));
069        factory.setPersistenceUnitName("warm-flow-jpa");
070        return factory;
071    }
072}