diff --git a/caf-commons/caf-commons-runtime/src/main/java/io/iec/edp/caf/commons/runtime/CafEnvironment.java b/caf-commons/caf-commons-runtime/src/main/java/io/iec/edp/caf/commons/runtime/CafEnvironment.java index 119755211831ec24d60e98451e2b925b020f78b0..5938a405a85ca96f7c17f21538c0bcbd9ab13fb1 100644 --- a/caf-commons/caf-commons-runtime/src/main/java/io/iec/edp/caf/commons/runtime/CafEnvironment.java +++ b/caf-commons/caf-commons-runtime/src/main/java/io/iec/edp/caf/commons/runtime/CafEnvironment.java @@ -445,4 +445,8 @@ public class CafEnvironment { return Boolean.parseBoolean(enabled); } + public static boolean enablePrimaryDataService() { + var enabled = getEnvironment().getProperty("caf-datasource.enablePrimaryDataService", "false"); + return Boolean.parseBoolean(enabled); + } } diff --git a/caf-tenancy/caf-tenancy-core/src/main/java/io/iec/edp/caf/tenancy/core/extensions/MultiTenantConnectionProviderImpl.java b/caf-tenancy/caf-tenancy-core/src/main/java/io/iec/edp/caf/tenancy/core/extensions/MultiTenantConnectionProviderImpl.java index 5abe8986ada5b278224894bb80bb33cbfe7f9e6f..38bc96ba9c38f03513d1f4373ce55fbc09039635 100644 --- a/caf-tenancy/caf-tenancy-core/src/main/java/io/iec/edp/caf/tenancy/core/extensions/MultiTenantConnectionProviderImpl.java +++ b/caf-tenancy/caf-tenancy-core/src/main/java/io/iec/edp/caf/tenancy/core/extensions/MultiTenantConnectionProviderImpl.java @@ -17,6 +17,7 @@ package io.iec.edp.caf.tenancy.core.extensions; import io.iec.edp.caf.commons.exception.ExceptionLevel; +import io.iec.edp.caf.commons.runtime.CafEnvironment; import io.iec.edp.caf.tenancy.api.exception.NoDataSourceConfigFound; import org.hibernate.cfg.AvailableSettings; import org.hibernate.engine.config.spi.ConfigurationService; @@ -44,6 +45,9 @@ public class MultiTenantConnectionProviderImpl extends AbstractDataSourceBasedMu */ @Override protected DataSource selectDataSource(String tenantIdentifier) { + if (CafEnvironment.enablePrimaryDataService()) { + return TenantDataSourceProvider.getTenantDataSourceWithPrimaryDataService(tenantIdentifier); + } return TenantDataSourceProvider.getTenantDataSource(tenantIdentifier); } @@ -65,6 +69,7 @@ public class MultiTenantConnectionProviderImpl extends AbstractDataSourceBasedMu } TenantDataSourceProvider.addDataSource( TenantDataSourceProvider.MASTERDB, hikariDataSource); + TenantDataSourceProvider.addDataSource("emcDataSource", hikariDataSource); } /** diff --git a/caf-tenancy/caf-tenancy-core/src/main/java/io/iec/edp/caf/tenancy/core/extensions/TenantDataSourceProvider.java b/caf-tenancy/caf-tenancy-core/src/main/java/io/iec/edp/caf/tenancy/core/extensions/TenantDataSourceProvider.java index bd1244b217c1d43d36ef7b897f75c6e5513c6f57..e89ea24990be5285ed0572b5e427ee780206e198 100644 --- a/caf-tenancy/caf-tenancy-core/src/main/java/io/iec/edp/caf/tenancy/core/extensions/TenantDataSourceProvider.java +++ b/caf-tenancy/caf-tenancy-core/src/main/java/io/iec/edp/caf/tenancy/core/extensions/TenantDataSourceProvider.java @@ -134,6 +134,60 @@ public class TenantDataSourceProvider { return dataSourceMap.get(tenantIdentifier); } + public static DataSource getTenantDataSourceWithPrimaryDataService(String tenantIdentifier) { + if (tenantIdentifier.equals(MASTERDB)) { + return dataSourceMap.get("emcDataSource"); + } + String[] tis = tenantIdentifier.split(","); + + DataSource ds = dataSourceMap.get(tenantIdentifier); + if (ds == null) { + try { + locker.lock(); + if (dataSourceMap.containsKey(tenantIdentifier) == false) { + tenantService = SpringBeanUtils.getBean(ITenantService.class); + DbConnectionInfo dbConnectionInfo = null; + //存在事务时,必须进行事务隔离,避免死循环(事务环境只走一次resolveTenantIdentifier) + if(TransactionSynchronizationManager.isSynchronizationActive()) { + dbConnectionInfo=tenantService.getDBConnInfoTrans(Integer.parseInt(tis[0]), tis[1], tis[2]); + }else{ + dbConnectionInfo = tenantService.getDBConnInfo(Integer.parseInt(tis[0]), tis[1], tis[2]); + } + + if (dbConnectionInfo == null) { +// 根据租户标识:%s 未取到数据库连接信息,默认返回主库数据源,请求URL:%s! + log.info(String.format("According to tenant ID:%s, database connection information was not retrieved. By default, the main database data source is returned. Request URL:%s!", tenantIdentifier, getRequestUrl())); + ds = dataSourceMap.get(MASTERDB); + hikariConnectionMap.putIfAbsent(tenantIdentifier,ds); + dataSourceMap.putIfAbsent(tenantIdentifier, ds); + } else { +// 根据租户标识:%s 已取到数据库连接信息,数据源编号:%s + log.info(String.format("According to tenant ID:%s, database connection information has been retrieved, data source number:%s", tenantIdentifier, dbConnectionInfo.getId())); +// 对应数据库连接: + log.info(tenantIdentifier+" corresponding database connection: "+dbConnectionInfo.getConnectionString()); + ds = connectionMap.get(dbConnectionInfo.getId()); + if(ds ==null) { + ds = HelperTools.toDataSource(dbConnectionInfo); + hikariConnectionMap.putIfAbsent(tenantIdentifier,ds); + ds = handleDatasource(ds); + dataSourceMap.putIfAbsent(tenantIdentifier, ds); + connectionMap.putIfAbsent(dbConnectionInfo.getId(), ds); + }else { + dataSourceMap.putIfAbsent(tenantIdentifier, ds); + } + } + } + } catch (Exception ex) { +// "根据租户标识确定数据源时出错,默认返回主库数据源!租户标识:%s;异常信息:%s," + log.warn(String.format("An error occurred while determining the data source based on the tenant identity. By default, the main database data source is returned! Tenant identity:%s; Exception information:%s ", tenantIdentifier, ex.toString())); + throw ex; + } finally { + locker.unlock(); + } + } + return dataSourceMap.get(tenantIdentifier); + } + public static HikariDataSource getHikariDataSource(String tenantIdentifier){ try{ DataSource ds = hikariConnectionMap.get(tenantIdentifier); diff --git a/caf-tenancy/caf-tenancy-repository-jpa/pom.xml b/caf-tenancy/caf-tenancy-repository-jpa/pom.xml index a1e55e6cb62fde6d01142572fc7bb6e4c48d0ee2..bf0a3c73bef6981c9db871fb9aabef9f6e45f9c7 100644 --- a/caf-tenancy/caf-tenancy-repository-jpa/pom.xml +++ b/caf-tenancy/caf-tenancy-repository-jpa/pom.xml @@ -44,5 +44,13 @@ io.iec.edp caf-i18n-api + + io.iec.edp + caf-rpc-api + + + io.iec.edp + caf-rpc-client + diff --git a/caf-tenancy/caf-tenancy-repository-jpa/src/main/java/io/iec/edp/caf/tenancy/core/persistence/AppInstanceRpcPersistence.java b/caf-tenancy/caf-tenancy-repository-jpa/src/main/java/io/iec/edp/caf/tenancy/core/persistence/AppInstanceRpcPersistence.java new file mode 100644 index 0000000000000000000000000000000000000000..42c668be8901c89e2f66aca82d96b5de82fb7023 --- /dev/null +++ b/caf-tenancy/caf-tenancy-repository-jpa/src/main/java/io/iec/edp/caf/tenancy/core/persistence/AppInstanceRpcPersistence.java @@ -0,0 +1,20 @@ +package io.iec.edp.caf.tenancy.core.persistence; + +import io.iec.edp.caf.rpc.api.annotation.GspServiceBundle; +import io.iec.edp.caf.rpc.api.annotation.RpcParam; +import io.iec.edp.caf.tenancy.api.entity.AppInstanceInfo; +import io.iec.edp.caf.tenancy.api.entity.SuDbMapping; + +import java.util.List; + +@GspServiceBundle(applicationName = "runtime", serviceUnitName= "sys") +public interface AppInstanceRpcPersistence { + + AppInstanceInfo findByCodeAndTenantId(@RpcParam(paramName = "appInstanceCode") String appInstanceCode, @RpcParam(paramName = "tenantId") int tenantId); + + List findAllByTenantId(@RpcParam(paramName = "tenantId") int tenantId); + + void save(@RpcParam(paramName = "appInstanceInfo") AppInstanceInfo appInstanceInfo); + + void delete(@RpcParam(paramName = "appInstanceInfo") AppInstanceInfo appInstanceInfo); +} diff --git a/caf-tenancy/caf-tenancy-repository-jpa/src/main/java/io/iec/edp/caf/tenancy/core/persistence/ClusterAppRpcPersistence.java b/caf-tenancy/caf-tenancy-repository-jpa/src/main/java/io/iec/edp/caf/tenancy/core/persistence/ClusterAppRpcPersistence.java new file mode 100644 index 0000000000000000000000000000000000000000..14af0f8f3a9827f15c827c202091293e4af4c1be --- /dev/null +++ b/caf-tenancy/caf-tenancy-repository-jpa/src/main/java/io/iec/edp/caf/tenancy/core/persistence/ClusterAppRpcPersistence.java @@ -0,0 +1,13 @@ +package io.iec.edp.caf.tenancy.core.persistence; + +import io.iec.edp.caf.rpc.api.annotation.GspServiceBundle; +import io.iec.edp.caf.rpc.api.annotation.RpcParam; +import io.iec.edp.caf.tenancy.api.entity.ClusterApp; + +@GspServiceBundle(applicationName = "runtime", serviceUnitName= "sys") +public interface ClusterAppRpcPersistence { + + void save(@RpcParam(paramName = "clusterApp") ClusterApp clusterApp); + + void deleteById(@RpcParam(paramName = "id") String id); +} diff --git a/caf-tenancy/caf-tenancy-repository-jpa/src/main/java/io/iec/edp/caf/tenancy/core/persistence/ClusterRpcPersistence.java b/caf-tenancy/caf-tenancy-repository-jpa/src/main/java/io/iec/edp/caf/tenancy/core/persistence/ClusterRpcPersistence.java new file mode 100644 index 0000000000000000000000000000000000000000..2144deb810c40db13d2a0efb7a90741c35e7642f --- /dev/null +++ b/caf-tenancy/caf-tenancy-repository-jpa/src/main/java/io/iec/edp/caf/tenancy/core/persistence/ClusterRpcPersistence.java @@ -0,0 +1,15 @@ +package io.iec.edp.caf.tenancy.core.persistence; + +import io.iec.edp.caf.rpc.api.annotation.GspServiceBundle; +import io.iec.edp.caf.rpc.api.annotation.RpcParam; +import io.iec.edp.caf.tenancy.api.entity.ClusterInfo; + +@GspServiceBundle(applicationName = "runtime", serviceUnitName= "sys") +public interface ClusterRpcPersistence { + + ClusterInfo findById(@RpcParam(paramName = "id") String id); + + ClusterInfo save(@RpcParam(paramName = "clusterApp") ClusterInfo clusterInfo); + + void deleteById(@RpcParam(paramName = "id") String id); +} diff --git a/caf-tenancy/caf-tenancy-repository-jpa/src/main/java/io/iec/edp/caf/tenancy/core/persistence/DbConnectionRpcPersistence.java b/caf-tenancy/caf-tenancy-repository-jpa/src/main/java/io/iec/edp/caf/tenancy/core/persistence/DbConnectionRpcPersistence.java new file mode 100644 index 0000000000000000000000000000000000000000..7a2642509fd34a30db3f448f80616d565f0ad617 --- /dev/null +++ b/caf-tenancy/caf-tenancy-repository-jpa/src/main/java/io/iec/edp/caf/tenancy/core/persistence/DbConnectionRpcPersistence.java @@ -0,0 +1,22 @@ +package io.iec.edp.caf.tenancy.core.persistence; + +import io.iec.edp.caf.rpc.api.annotation.GspServiceBundle; +import io.iec.edp.caf.rpc.api.annotation.RpcParam; +import io.iec.edp.caf.tenancy.api.entity.ClusterInfo; +import io.iec.edp.caf.tenancy.api.entity.DbConnectionInfo; + +import java.util.List; + +@GspServiceBundle(applicationName = "runtime", serviceUnitName= "sys") +public interface DbConnectionRpcPersistence { + + DbConnectionInfo findById(@RpcParam(paramName = "id") String id); + + List findByAppInstanceId(@RpcParam(paramName = "appInstanceId") String appInstanceId); + + void saveAll(@RpcParam(paramName = "dbConnectionInfos") List dbConnectionInfos); + + void deleteAll(@RpcParam(paramName = "dbConnectionInfos") List dbConnectionInfos); + + List findAll(); +} diff --git a/caf-tenancy/caf-tenancy-repository-jpa/src/main/java/io/iec/edp/caf/tenancy/core/persistence/DbMappingRpcPersistence.java b/caf-tenancy/caf-tenancy-repository-jpa/src/main/java/io/iec/edp/caf/tenancy/core/persistence/DbMappingRpcPersistence.java new file mode 100644 index 0000000000000000000000000000000000000000..b38e92476aad4921e89d4c24dcac252b2351a77c --- /dev/null +++ b/caf-tenancy/caf-tenancy-repository-jpa/src/main/java/io/iec/edp/caf/tenancy/core/persistence/DbMappingRpcPersistence.java @@ -0,0 +1,19 @@ +package io.iec.edp.caf.tenancy.core.persistence; + +import io.iec.edp.caf.rpc.api.annotation.GspServiceBundle; +import io.iec.edp.caf.rpc.api.annotation.RpcParam; +import io.iec.edp.caf.tenancy.api.entity.SuDbMapping; + +import java.util.List; + +@GspServiceBundle(applicationName = "runtime", serviceUnitName= "sys") +public interface DbMappingRpcPersistence { + + List findAllByAppInstanceId(@RpcParam(paramName = "appInstanceId") String appInstanceId); + + void saveAll(@RpcParam(paramName = "mappings") List mappings); + + void deleteAll(@RpcParam(paramName = "mappings") List mappings); + + +} diff --git a/caf-tenancy/caf-tenancy-repository-jpa/src/main/java/io/iec/edp/caf/tenancy/core/persistence/TenantExtendRpcPersistence.java b/caf-tenancy/caf-tenancy-repository-jpa/src/main/java/io/iec/edp/caf/tenancy/core/persistence/TenantExtendRpcPersistence.java new file mode 100644 index 0000000000000000000000000000000000000000..95626527ac3c83331351c07eb0dca779436ff3f3 --- /dev/null +++ b/caf-tenancy/caf-tenancy-repository-jpa/src/main/java/io/iec/edp/caf/tenancy/core/persistence/TenantExtendRpcPersistence.java @@ -0,0 +1,16 @@ +package io.iec.edp.caf.tenancy.core.persistence; + +import io.iec.edp.caf.rpc.api.annotation.GspServiceBundle; +import io.iec.edp.caf.rpc.api.annotation.RpcParam; +import io.iec.edp.caf.tenancy.api.entity.SuDbMapping; +import io.iec.edp.caf.tenancy.api.entity.TenantExtend; + +import java.util.List; + +@GspServiceBundle(applicationName = "runtime", serviceUnitName= "sys") +public interface TenantExtendRpcPersistence { + + TenantExtend findByTenantId(@RpcParam(paramName = "tenantId") Integer tenantId); + TenantExtend findByExtendTenant(@RpcParam(paramName = "extendId") String extendId); + void save(@RpcParam(paramName = "tenantExtend") TenantExtend tenantExtend); +} diff --git a/caf-tenancy/caf-tenancy-repository-jpa/src/main/java/io/iec/edp/caf/tenancy/core/persistence/TenantRoutingRpcPersistence.java b/caf-tenancy/caf-tenancy-repository-jpa/src/main/java/io/iec/edp/caf/tenancy/core/persistence/TenantRoutingRpcPersistence.java new file mode 100644 index 0000000000000000000000000000000000000000..96eef7c5fd48b44e9afb112a6f2f078f79c6b616 --- /dev/null +++ b/caf-tenancy/caf-tenancy-repository-jpa/src/main/java/io/iec/edp/caf/tenancy/core/persistence/TenantRoutingRpcPersistence.java @@ -0,0 +1,12 @@ +package io.iec.edp.caf.tenancy.core.persistence; + +import io.iec.edp.caf.rpc.api.annotation.GspServiceBundle; +import io.iec.edp.caf.rpc.api.annotation.RpcParam; +import io.iec.edp.caf.tenancy.api.entity.Tenant; + +@GspServiceBundle(applicationName = "runtime", serviceUnitName= "sys") +public interface TenantRoutingRpcPersistence { + + Integer findAvailableTenantId(@RpcParam(paramName = "suName") String suName, @RpcParam(paramName = "tenantDim1") String tenantDim1); + Integer findAvailableTenantIdByDim2(@RpcParam(paramName = "suName") String suName, @RpcParam(paramName = "tenantDim1") String tenantDim1, @RpcParam(paramName = "tenantDim2") String tenantDim2); +} diff --git a/caf-tenancy/caf-tenancy-repository-jpa/src/main/java/io/iec/edp/caf/tenancy/core/persistence/TenantRpcPersistence.java b/caf-tenancy/caf-tenancy-repository-jpa/src/main/java/io/iec/edp/caf/tenancy/core/persistence/TenantRpcPersistence.java new file mode 100644 index 0000000000000000000000000000000000000000..fe4135e436f612cf45763a9c0d44723779305ed4 --- /dev/null +++ b/caf-tenancy/caf-tenancy-repository-jpa/src/main/java/io/iec/edp/caf/tenancy/core/persistence/TenantRpcPersistence.java @@ -0,0 +1,19 @@ +package io.iec.edp.caf.tenancy.core.persistence; + +import io.iec.edp.caf.rpc.api.annotation.GspServiceBundle; +import io.iec.edp.caf.rpc.api.annotation.RpcParam; +import io.iec.edp.caf.tenancy.api.entity.EmbeddedTenant; +import io.iec.edp.caf.tenancy.api.entity.Tenant; +import io.iec.edp.caf.tenancy.api.entity.TenantExtend; + +import java.util.List; + +@GspServiceBundle(applicationName = "runtime", serviceUnitName= "sys") +public interface TenantRpcPersistence { + + EmbeddedTenant findById(@RpcParam(paramName = "tenantId") int tenantId); + void deleteById(@RpcParam(paramName = "tenantId") int tenantId); + List findAll(); + EmbeddedTenant findByCode(@RpcParam(paramName = "tenantCode") String tenantCode); + void save(@RpcParam(paramName = "tenant") EmbeddedTenant tenant); +} diff --git a/caf-tenancy/caf-tenancy-repository-jpa/src/main/java/io/iec/edp/caf/tenancy/core/service/ClusterAppMgr.java b/caf-tenancy/caf-tenancy-repository-jpa/src/main/java/io/iec/edp/caf/tenancy/core/service/ClusterAppMgr.java index 3cd1428d3fd50674024db5d309f5cfe0840b5ff5..27a00c5ac5294cf9eea9ad17cdb37725f9e673ef 100644 --- a/caf-tenancy/caf-tenancy-repository-jpa/src/main/java/io/iec/edp/caf/tenancy/core/service/ClusterAppMgr.java +++ b/caf-tenancy/caf-tenancy-repository-jpa/src/main/java/io/iec/edp/caf/tenancy/core/service/ClusterAppMgr.java @@ -17,9 +17,13 @@ package io.iec.edp.caf.tenancy.core.service; +import io.iec.edp.caf.commons.runtime.CafEnvironment; +import io.iec.edp.caf.rpc.client.RpcClassHolder; import io.iec.edp.caf.tenancy.api.entity.ClusterApp; import io.iec.edp.caf.tenancy.api.service.ClusterAppService; import io.iec.edp.caf.tenancy.core.TenantCache; +import io.iec.edp.caf.tenancy.core.persistence.ClusterAppRpcPersistence; +import io.iec.edp.caf.tenancy.core.persistence.ClusterRpcPersistence; import io.iec.edp.caf.tenancy.core.repository.ClusterAppRepository; import io.iec.edp.caf.tenancy.core.utils.DataValidator; @@ -35,10 +39,15 @@ public class ClusterAppMgr implements ClusterAppService { private final TenantCache tenantCache; - public ClusterAppMgr(ClusterAppRepository instanceClusterRelationRepository, TenantCache tenantCache) + private ClusterAppRpcPersistence clusterAppRpcPersistence; + + private final RpcClassHolder rpcClassHolder; + + public ClusterAppMgr(ClusterAppRepository instanceClusterRelationRepository, TenantCache tenantCache, RpcClassHolder rpcClassHolder) { this.instanceClusterRelationRepository = instanceClusterRelationRepository; this.tenantCache = tenantCache; + this.rpcClassHolder = rpcClassHolder; } /** @@ -74,7 +83,11 @@ public class ClusterAppMgr implements ClusterAppService { ClusterApp clusterAppInfo = new ClusterApp(); clusterAppInfo.setId(appInstanceId); clusterAppInfo.setClusterId(clusterId); - this.instanceClusterRelationRepository.save(clusterAppInfo); + if (CafEnvironment.enablePrimaryDataService()) { + getClusterAppRpcPersistence().save(clusterAppInfo); + } else { + this.instanceClusterRelationRepository.save(clusterAppInfo); + } tenantCache.set(cacheKey, clusterId); } @@ -97,7 +110,11 @@ public class ClusterAppMgr implements ClusterAppService { ClusterApp clusterAppInfo = new ClusterApp(); clusterAppInfo.setId(appInstanceId); clusterAppInfo.setClusterId(clusterId); - this.instanceClusterRelationRepository.save(clusterAppInfo); + if (CafEnvironment.enablePrimaryDataService()) { + getClusterAppRpcPersistence().save(clusterAppInfo); + } else { + this.instanceClusterRelationRepository.save(clusterAppInfo); + } tenantCache.set(cacheKey, clusterId); @@ -115,7 +132,11 @@ public class ClusterAppMgr implements ClusterAppService { { DataValidator.checkForEmptyString(id, "ClusterId"); String cacheKey = this.getCacheKey(id); - this.instanceClusterRelationRepository.deleteById(id); + if (CafEnvironment.enablePrimaryDataService()) { + getClusterAppRpcPersistence().deleteById(id); + } else { + this.instanceClusterRelationRepository.deleteById(id); + } tenantCache.remove(cacheKey); return true; } @@ -146,4 +167,12 @@ public class ClusterAppMgr implements ClusterAppService { { return String.format("ClusterRelation_%s", id); } + + + private ClusterAppRpcPersistence getClusterAppRpcPersistence(){ + if(this.clusterAppRpcPersistence==null){ + this.clusterAppRpcPersistence = rpcClassHolder.getRpcClass("sys", ClusterAppRpcPersistence.class); + } + return this.clusterAppRpcPersistence; + } } diff --git a/caf-tenancy/caf-tenancy-repository-jpa/src/main/java/io/iec/edp/caf/tenancy/core/service/ClusterManager.java b/caf-tenancy/caf-tenancy-repository-jpa/src/main/java/io/iec/edp/caf/tenancy/core/service/ClusterManager.java index 4c42193b6a34c20739869d71bdda2ec6732f693e..c067e55ea7c3227bfdda3d621eec9b37caf4ba6d 100644 --- a/caf-tenancy/caf-tenancy-repository-jpa/src/main/java/io/iec/edp/caf/tenancy/core/service/ClusterManager.java +++ b/caf-tenancy/caf-tenancy-repository-jpa/src/main/java/io/iec/edp/caf/tenancy/core/service/ClusterManager.java @@ -16,8 +16,12 @@ package io.iec.edp.caf.tenancy.core.service; +import io.iec.edp.caf.commons.runtime.CafEnvironment; +import io.iec.edp.caf.rpc.client.RpcClassHolder; import io.iec.edp.caf.tenancy.api.service.ClusterService; import io.iec.edp.caf.tenancy.core.TenantCache; +import io.iec.edp.caf.tenancy.core.persistence.ClusterAppRpcPersistence; +import io.iec.edp.caf.tenancy.core.persistence.ClusterRpcPersistence; import io.iec.edp.caf.tenancy.core.repository.ClusterRepository; import io.iec.edp.caf.tenancy.core.utils.DataValidator; import io.iec.edp.caf.tenancy.api.entity.*; @@ -36,10 +40,15 @@ public class ClusterManager implements ClusterService { private final TenantCache tenantCache; - public ClusterManager(ClusterRepository repo, TenantCache tenantCache) + private ClusterRpcPersistence clusterRpcPersistence; + + private final RpcClassHolder rpcClassHolder; + + public ClusterManager(ClusterRepository repo, TenantCache tenantCache, RpcClassHolder rpcClassHolder) { this.repo = repo; this.tenantCache = tenantCache; + this.rpcClassHolder = rpcClassHolder; } /// @@ -62,7 +71,11 @@ public class ClusterManager implements ClusterService { var info = tenantCache.get(cacheKey, ClusterInfo.class); if (info == null) { - info = this.repo.findById(id).get(); + if (CafEnvironment.enablePrimaryDataService()) { + info = getClusterRpcPersistence().findById(id); + } else { + info = this.repo.findById(id).get(); + } if (info != null) { tenantCache.set(cacheKey, info); } @@ -84,7 +97,11 @@ public class ClusterManager implements ClusterService { { DataValidator.checkForNullReference(info, "ClusterInfo"); String cacheKey = this.getCacheKey(info.getId()); - info = this.repo.save(info); + if (CafEnvironment.enablePrimaryDataService()) { + info = getClusterRpcPersistence().save(info); + } else { + info = this.repo.save(info); + } tenantCache.set(cacheKey, info); } @@ -100,7 +117,11 @@ public class ClusterManager implements ClusterService { DataValidator.checkForNullReference(info, "ClusterInfo"); String cacheKey = this.getCacheKey(info.getId()); - info = this.repo.save(info); + if (CafEnvironment.enablePrimaryDataService()) { + info = getClusterRpcPersistence().save(info); + } else { + info = this.repo.save(info); + } tenantCache.set(cacheKey, info); return true; } @@ -117,7 +138,11 @@ public class ClusterManager implements ClusterService { DataValidator.checkForEmptyString(id, "ClusterId"); String cacheKey = this.getCacheKey(id); - this.repo.deleteById(id); + if (CafEnvironment.enablePrimaryDataService()) { + getClusterRpcPersistence().deleteById(id); + } else { + this.repo.deleteById(id); + } tenantCache.remove(cacheKey); return true; } @@ -151,4 +176,10 @@ public class ClusterManager implements ClusterService { return String.format("Cluster_%s", id); } + private ClusterRpcPersistence getClusterRpcPersistence(){ + if(this.clusterRpcPersistence==null){ + this.clusterRpcPersistence = rpcClassHolder.getRpcClass("sys", ClusterRpcPersistence.class); + } + return this.clusterRpcPersistence; + } } diff --git a/caf-tenancy/caf-tenancy-repository-jpa/src/main/java/io/iec/edp/caf/tenancy/core/service/DbConnectionManager.java b/caf-tenancy/caf-tenancy-repository-jpa/src/main/java/io/iec/edp/caf/tenancy/core/service/DbConnectionManager.java index 2172bd9202a6eab96c202b58ac2bfabca64f2210..8713d4260812f30afc00e57db4a979ea0f6354ba 100644 --- a/caf-tenancy/caf-tenancy-repository-jpa/src/main/java/io/iec/edp/caf/tenancy/core/service/DbConnectionManager.java +++ b/caf-tenancy/caf-tenancy-repository-jpa/src/main/java/io/iec/edp/caf/tenancy/core/service/DbConnectionManager.java @@ -17,9 +17,13 @@ package io.iec.edp.caf.tenancy.core.service; +import io.iec.edp.caf.commons.runtime.CafEnvironment; +import io.iec.edp.caf.rpc.client.RpcClassHolder; import io.iec.edp.caf.tenancy.api.entity.DbConnectionInfo; import io.iec.edp.caf.tenancy.api.service.DbConnectionService; import io.iec.edp.caf.tenancy.core.TenantCache; +import io.iec.edp.caf.tenancy.core.persistence.ClusterRpcPersistence; +import io.iec.edp.caf.tenancy.core.persistence.DbConnectionRpcPersistence; import io.iec.edp.caf.tenancy.core.repository.DbConnectionRepository; import io.iec.edp.caf.tenancy.core.repository.SuDbMappingRepository; import io.iec.edp.caf.tenancy.core.utils.DataValidator; @@ -47,11 +51,16 @@ public class DbConnectionManager implements DbConnectionService { private final ConcurrentHashMap connInfos = new ConcurrentHashMap<>(); - public DbConnectionManager(DbConnectionRepository repository, SuDbMappingRepository mappingRepository, TenantCache tenantCache) + private DbConnectionRpcPersistence dbConnectionRpcPersistence; + + private final RpcClassHolder rpcClassHolder; + + public DbConnectionManager(DbConnectionRepository repository, SuDbMappingRepository mappingRepository, TenantCache tenantCache, RpcClassHolder rpcClassHolder) { this.repository = repository; // this.mappingRepository = mappingRepository; this.tenantCache = tenantCache; + this.rpcClassHolder = rpcClassHolder; } /** @@ -72,7 +81,11 @@ public class DbConnectionManager implements DbConnectionService { if (info == null) { - info = this.repository.findById(id).orElse(null); + if (CafEnvironment.enablePrimaryDataService()) { + info = getDbConnectionRpcPersistence().findById(id); + } else { + info = this.repository.findById(id).orElse(null); + } if (info != null) { //解密连接字符串,感觉也不太合适,暂时先放这儿吧 @@ -97,7 +110,12 @@ public class DbConnectionManager implements DbConnectionService { { DataValidator.checkForEmptyString(appInstanceId, "appInstanceId"); - var dbConns = this.repository.findByAppInstanceId(appInstanceId); + List dbConns = new ArrayList<>(); + if (CafEnvironment.enablePrimaryDataService()) { + dbConns = getDbConnectionRpcPersistence().findByAppInstanceId(appInstanceId); + } else { + dbConns = this.repository.findByAppInstanceId(appInstanceId); + } return dbConns; } @@ -115,7 +133,11 @@ public class DbConnectionManager implements DbConnectionService { infos.forEach(x -> list.add(x) ); - this.repository.saveAll(infos); + if (CafEnvironment.enablePrimaryDataService()) { + getDbConnectionRpcPersistence().saveAll(infos); + } else { + this.repository.saveAll(infos); + } } /// @@ -130,7 +152,11 @@ public class DbConnectionManager implements DbConnectionService { public boolean update(List infos) { DataValidator.checkForNullReference(infos, "List"); - this.repository.saveAll(infos); + if (CafEnvironment.enablePrimaryDataService()) { + getDbConnectionRpcPersistence().saveAll(infos); + } else { + this.repository.saveAll(infos); + } //先删除,再获取最新的并缓存 for(var s : infos) @@ -156,7 +182,11 @@ public class DbConnectionManager implements DbConnectionService { var infos = this.getByAppInstanceId(appInstanceId); if (infos != null && infos.size() > 0) { - this.repository.deleteAll(infos); + if (CafEnvironment.enablePrimaryDataService()) { + getDbConnectionRpcPersistence().deleteAll(infos); + } else { + this.repository.deleteAll(infos); + } for (var item : infos) { String cacheKey = this.getCacheKey(item.getId()); @@ -179,4 +209,10 @@ public class DbConnectionManager implements DbConnectionService { return String.format("DB_%s", dbConnectionId); } + private DbConnectionRpcPersistence getDbConnectionRpcPersistence(){ + if(this.dbConnectionRpcPersistence==null){ + this.dbConnectionRpcPersistence = rpcClassHolder.getRpcClass("sys", DbConnectionRpcPersistence.class); + } + return this.dbConnectionRpcPersistence; + } } diff --git a/caf-tenancy/caf-tenancy-repository-jpa/src/main/java/io/iec/edp/caf/tenancy/core/service/DbMappingManager.java b/caf-tenancy/caf-tenancy-repository-jpa/src/main/java/io/iec/edp/caf/tenancy/core/service/DbMappingManager.java index de1e32688b9140c9afa0d0e8ebb0dae7a9a3b6a7..a4a82029f258d861505805f94024d241cfd2eaa4 100644 --- a/caf-tenancy/caf-tenancy-repository-jpa/src/main/java/io/iec/edp/caf/tenancy/core/service/DbMappingManager.java +++ b/caf-tenancy/caf-tenancy-repository-jpa/src/main/java/io/iec/edp/caf/tenancy/core/service/DbMappingManager.java @@ -16,9 +16,13 @@ package io.iec.edp.caf.tenancy.core.service; +import io.iec.edp.caf.commons.runtime.CafEnvironment; +import io.iec.edp.caf.rpc.client.RpcClassHolder; import io.iec.edp.caf.tenancy.api.entity.SuDbMapping; import io.iec.edp.caf.tenancy.api.service.DbMappingService; import io.iec.edp.caf.tenancy.core.TenantCache; +import io.iec.edp.caf.tenancy.core.persistence.DbConnectionRpcPersistence; +import io.iec.edp.caf.tenancy.core.persistence.DbMappingRpcPersistence; import io.iec.edp.caf.tenancy.core.repository.SuDbMappingRepository; import io.iec.edp.caf.tenancy.core.utils.DataValidator; import lombok.var; @@ -38,12 +42,17 @@ public class DbMappingManager implements DbMappingService { private final TenantCache tenantCache; + private DbMappingRpcPersistence dbMappingRpcPersistence; + + private final RpcClassHolder rpcClassHolder; + // private ConcurrentHashMap connInfos = new ConcurrentHashMap<>(); - public DbMappingManager(SuDbMappingRepository repository, TenantCache tenantCache) + public DbMappingManager(SuDbMappingRepository repository, TenantCache tenantCache, RpcClassHolder rpcClassHolder) { this.repository = repository; this.tenantCache = tenantCache; + this.rpcClassHolder = rpcClassHolder; } /// @@ -61,7 +70,11 @@ public class DbMappingManager implements DbMappingService { public List getAll(String appInstanceId) { DataValidator.checkForEmptyString(appInstanceId, "appInstanceId"); - return this.repository.findAllByAppInstanceId(appInstanceId); + if (CafEnvironment.enablePrimaryDataService()) { + return getDbMappingRpcPersistence().findAllByAppInstanceId(appInstanceId); + } else { + return this.repository.findAllByAppInstanceId(appInstanceId); + } } /** @@ -116,7 +129,11 @@ public class DbMappingManager implements DbMappingService { DataValidator.checkForNullReference(mappings, "List"); String cacheKey = this.getCacheKey(appInstanceId); - this.repository.saveAll(mappings); + if (CafEnvironment.enablePrimaryDataService()) { + getDbMappingRpcPersistence().saveAll(mappings); + } else { + this.repository.saveAll(mappings); + } for ( SuDbMapping item : mappings ) { @@ -139,7 +156,11 @@ public class DbMappingManager implements DbMappingService { DataValidator.checkForNullReference(mappings, "List"); String cacheKey = this.getCacheKey(appInstanceId); - this.repository.saveAll(mappings); + if (CafEnvironment.enablePrimaryDataService()) { + getDbMappingRpcPersistence().saveAll(mappings); + } else { + this.repository.saveAll(mappings); + } for (SuDbMapping item : mappings) { @@ -164,7 +185,11 @@ public class DbMappingManager implements DbMappingService { if (mappings != null) { String cacheKey = this.getCacheKey(appInstanceId); - this.repository.deleteAll(mappings); + if (CafEnvironment.enablePrimaryDataService()) { + getDbMappingRpcPersistence().deleteAll(mappings); + } else { + this.repository.deleteAll(mappings); + } for (var item : mappings) { @@ -199,4 +224,11 @@ public class DbMappingManager implements DbMappingService { { return String.format("map_%s", appInstanceId); } + + private DbMappingRpcPersistence getDbMappingRpcPersistence(){ + if(this.dbMappingRpcPersistence==null){ + this.dbMappingRpcPersistence = rpcClassHolder.getRpcClass("sys", DbMappingRpcPersistence.class); + } + return this.dbMappingRpcPersistence; + } } diff --git a/caf-tenancy/caf-tenancy-repository-jpa/src/main/java/io/iec/edp/caf/tenancy/core/service/TenantAppInstanceMgr.java b/caf-tenancy/caf-tenancy-repository-jpa/src/main/java/io/iec/edp/caf/tenancy/core/service/TenantAppInstanceMgr.java index db9ce10cd13c5757eaeef4e4ff5951560a77ac3e..77b780016a56de57dc3e7d9a7e7365a9644610ee 100644 --- a/caf-tenancy/caf-tenancy-repository-jpa/src/main/java/io/iec/edp/caf/tenancy/core/service/TenantAppInstanceMgr.java +++ b/caf-tenancy/caf-tenancy-repository-jpa/src/main/java/io/iec/edp/caf/tenancy/core/service/TenantAppInstanceMgr.java @@ -17,18 +17,23 @@ package io.iec.edp.caf.tenancy.core.service; import io.iec.edp.caf.commons.exception.ExceptionLevel; +import io.iec.edp.caf.commons.runtime.CafEnvironment; import io.iec.edp.caf.commons.transaction.JpaTransaction; import io.iec.edp.caf.commons.transaction.TransactionPropagation; +import io.iec.edp.caf.rpc.client.RpcClassHolder; import io.iec.edp.caf.tenancy.api.entity.AppInstanceInfo; import io.iec.edp.caf.tenancy.api.entity.DbConnectionInfo; import io.iec.edp.caf.tenancy.api.exception.NoMatchingAppInstanceFound; import io.iec.edp.caf.tenancy.api.service.*; import io.iec.edp.caf.tenancy.core.TenantCache; +import io.iec.edp.caf.tenancy.core.persistence.AppInstanceRpcPersistence; +import io.iec.edp.caf.tenancy.core.persistence.DbMappingRpcPersistence; import io.iec.edp.caf.tenancy.core.repository.AppInstanceRepository; import io.iec.edp.caf.tenancy.core.utils.DataValidator; import lombok.extern.slf4j.Slf4j; import lombok.var; +import java.util.ArrayList; import java.util.List; import java.util.concurrent.ConcurrentHashMap; @@ -53,8 +58,12 @@ public class TenantAppInstanceMgr implements TenantAppInstanceService { private final TenantCache tenantCache; + private AppInstanceRpcPersistence appInstanceRpcPersistence; + + private final RpcClassHolder rpcClassHolder; + public TenantAppInstanceMgr(DbConnectionService dbMgr, DbMappingService mappingMgr, ClusterService clusterManager, ClusterAppService relationManager, - AppInstanceRepository appRepository, TenantCache tenantCache) + AppInstanceRepository appRepository, TenantCache tenantCache, RpcClassHolder rpcClassHolder) { this.dbMgr = dbMgr; this.mappingMgr = mappingMgr; @@ -62,6 +71,7 @@ public class TenantAppInstanceMgr implements TenantAppInstanceService { this.relationManager = relationManager; this.appRepository = appRepository; this.tenantCache = tenantCache; + this.rpcClassHolder = rpcClassHolder; } private final ConcurrentHashMap> tenantAppInfos = new ConcurrentHashMap<>(); @@ -82,11 +92,20 @@ public class TenantAppInstanceMgr implements TenantAppInstanceService { info = tenantCache.hashGet(getCacheKey(tenantId), appInstanceCode, AppInstanceInfo.class); if (info == null) { - info = this.appRepository.findByCodeAndTenantId(appInstanceCode,tenantId); + if (CafEnvironment.enablePrimaryDataService()) { + info = getAppInstanceRpcPersistence().findByCodeAndTenantId(appInstanceCode, tenantId); + } else { + info = this.appRepository.findByCodeAndTenantId(appInstanceCode,tenantId); + } //现在应用实例还是写死的pg01,暂时加上兼容处理,后续删除 if (info == null) { - List infos = this.appRepository.findAllByTenantId(tenantId); + List infos = new ArrayList<>(); + if (CafEnvironment.enablePrimaryDataService()) { + infos = getAppInstanceRpcPersistence().findAllByTenantId(tenantId); + } else { + infos = this.appRepository.findAllByTenantId(tenantId); + } if (infos != null && infos.size() > 0) info = infos.get(0); } @@ -118,7 +137,11 @@ public class TenantAppInstanceMgr implements TenantAppInstanceService { var infos = tenantAppInfos.getOrDefault(tenantId, null); if (infos == null) { - infos = this.appRepository.findAllByTenantId(tenantId); + if (CafEnvironment.enablePrimaryDataService()) { + infos = getAppInstanceRpcPersistence().findAllByTenantId(tenantId); + } else { + infos = this.appRepository.findAllByTenantId(tenantId); + } if (infos != null && infos.size() > 0) { @@ -152,7 +175,11 @@ public class TenantAppInstanceMgr implements TenantAppInstanceService { { DataValidator.checkForNullReference(appInstanceInfo, "appInstanceInfo"); - this.appRepository.save(appInstanceInfo); + if (CafEnvironment.enablePrimaryDataService()) { + getAppInstanceRpcPersistence().save(appInstanceInfo); + } else { + this.appRepository.save(appInstanceInfo); + } // if (appInstanceInfo.getSuDBMappings() != null) // { // this.mappingMgr.add(appInstanceInfo.getId(), appInstanceInfo.getSuDBMappings()); @@ -193,7 +220,11 @@ public class TenantAppInstanceMgr implements TenantAppInstanceService { this.dbMgr.removeByAppInstanceId(info.getId()); this.mappingMgr.removeByAppInstanceId(info.getId()); - this.appRepository.delete(info); + if (CafEnvironment.enablePrimaryDataService()) { + getAppInstanceRpcPersistence().delete(info); + } else { + this.appRepository.delete(info); + } //删除缓存 //删除缓存 @@ -259,4 +290,10 @@ public class TenantAppInstanceMgr implements TenantAppInstanceService { return String.format("App_%s", tenantId); } + private AppInstanceRpcPersistence getAppInstanceRpcPersistence(){ + if(this.appInstanceRpcPersistence==null){ + this.appInstanceRpcPersistence = rpcClassHolder.getRpcClass("sys", AppInstanceRpcPersistence.class); + } + return this.appInstanceRpcPersistence; + } } diff --git a/caf-tenancy/caf-tenancy-repository-jpa/src/main/java/io/iec/edp/caf/tenancy/core/service/TenantExtendService.java b/caf-tenancy/caf-tenancy-repository-jpa/src/main/java/io/iec/edp/caf/tenancy/core/service/TenantExtendService.java index 6a318e00a3c52481ef9f48d2579eadba1b1cb32c..8aa474ba9a1582437b0e1b4ceaac1136fef59183 100644 --- a/caf-tenancy/caf-tenancy-repository-jpa/src/main/java/io/iec/edp/caf/tenancy/core/service/TenantExtendService.java +++ b/caf-tenancy/caf-tenancy-repository-jpa/src/main/java/io/iec/edp/caf/tenancy/core/service/TenantExtendService.java @@ -16,30 +16,58 @@ package io.iec.edp.caf.tenancy.core.service; +import io.iec.edp.caf.commons.runtime.CafEnvironment; +import io.iec.edp.caf.rpc.client.RpcClassHolder; import io.iec.edp.caf.tenancy.api.ITenantExtendService; import io.iec.edp.caf.tenancy.api.entity.TenantExtend; +import io.iec.edp.caf.tenancy.core.persistence.AppInstanceRpcPersistence; +import io.iec.edp.caf.tenancy.core.persistence.TenantExtendRpcPersistence; import io.iec.edp.caf.tenancy.core.repository.TenantExtendRepository; public class TenantExtendService implements ITenantExtendService { private final TenantExtendRepository extendRepository; - public TenantExtendService(TenantExtendRepository extendRepository){ + private TenantExtendRpcPersistence tenantExtendRpcPersistence; + + private final RpcClassHolder rpcClassHolder; + + public TenantExtendService(TenantExtendRepository extendRepository, RpcClassHolder rpcClassHolder){ this.extendRepository = extendRepository; + this.rpcClassHolder = rpcClassHolder; } @Override public TenantExtend getTenantExtendByTenantId(Integer tenantId) { - return extendRepository.findByTenantId(tenantId); + if (CafEnvironment.enablePrimaryDataService()) { + return getTenantExtendRpcPersistence().findByTenantId(tenantId); + } else { + return extendRepository.findByTenantId(tenantId); + } } @Override public TenantExtend getTenantExtendByExtendId(String extendId) { - return extendRepository.findByExtendTenant(extendId); + if (CafEnvironment.enablePrimaryDataService()) { + return getTenantExtendRpcPersistence().findByExtendTenant(extendId); + } else { + return extendRepository.findByExtendTenant(extendId); + } } @Override public void saveTenantExtend(TenantExtend tenantExtend) { - extendRepository.save(tenantExtend); + if (CafEnvironment.enablePrimaryDataService()) { + getTenantExtendRpcPersistence().save(tenantExtend); + } else { + extendRepository.save(tenantExtend); + } + } + + private TenantExtendRpcPersistence getTenantExtendRpcPersistence(){ + if(this.tenantExtendRpcPersistence==null){ + this.tenantExtendRpcPersistence = rpcClassHolder.getRpcClass("sys", TenantExtendRpcPersistence.class); + } + return this.tenantExtendRpcPersistence; } } diff --git a/caf-tenancy/caf-tenancy-repository-jpa/src/main/java/io/iec/edp/caf/tenancy/core/service/TenantManager.java b/caf-tenancy/caf-tenancy-repository-jpa/src/main/java/io/iec/edp/caf/tenancy/core/service/TenantManager.java index a021ca0962cc762764808db49e3fd30ace920398..96ec94427fc99d5788f21934e5d97d756d46de9b 100644 --- a/caf-tenancy/caf-tenancy-repository-jpa/src/main/java/io/iec/edp/caf/tenancy/core/service/TenantManager.java +++ b/caf-tenancy/caf-tenancy-repository-jpa/src/main/java/io/iec/edp/caf/tenancy/core/service/TenantManager.java @@ -18,11 +18,13 @@ package io.iec.edp.caf.tenancy.core.service; import io.iec.caf.data.jpa.utils.TemporaryHibernateIdentifier; import io.iec.edp.caf.commons.exception.ExceptionLevel; +import io.iec.edp.caf.commons.runtime.CafEnvironment; import io.iec.edp.caf.commons.transaction.JpaTransaction; import io.iec.edp.caf.commons.transaction.TransactionPropagation; import io.iec.edp.caf.commons.utils.SpringBeanUtils; import io.iec.edp.caf.data.multilang.CAFMultiLanguageColumn; import io.iec.edp.caf.i18n.api.LanguageSuffixProvider; +import io.iec.edp.caf.rpc.client.RpcClassHolder; import io.iec.edp.caf.tenancy.api.entity.AppInstanceInfo; import io.iec.edp.caf.tenancy.api.entity.EmbeddedTenant; import io.iec.edp.caf.tenancy.api.entity.Tenant; @@ -30,6 +32,8 @@ import io.iec.edp.caf.tenancy.api.exception.TenantException; import io.iec.edp.caf.tenancy.api.service.TenantAppInstanceService; import io.iec.edp.caf.tenancy.api.service.TenantPersistenceService; import io.iec.edp.caf.tenancy.core.TenantCache; +import io.iec.edp.caf.tenancy.core.persistence.TenantExtendRpcPersistence; +import io.iec.edp.caf.tenancy.core.persistence.TenantRpcPersistence; import io.iec.edp.caf.tenancy.core.repository.TenantRepository; import io.iec.edp.caf.tenancy.core.utils.DataValidator; import lombok.var; @@ -61,12 +65,18 @@ public class TenantManager implements TenantPersistenceService { private final TenantAppInstanceService appInstMgr; private LanguageSuffixProvider suffixProvider; - public TenantManager(TenantRepository repository, TenantCache tenantCache,TenantAppInstanceService appInstMgr,LanguageSuffixProvider suffixProvider) + + private TenantRpcPersistence tenantRpcPersistence; + + private final RpcClassHolder rpcClassHolder; + + public TenantManager(TenantRepository repository, TenantCache tenantCache,TenantAppInstanceService appInstMgr,LanguageSuffixProvider suffixProvider, RpcClassHolder rpcClassHolder) { this.repository = repository; this.tenantCache = tenantCache; this.appInstMgr=appInstMgr; this.suffixProvider=suffixProvider; + this.rpcClassHolder = rpcClassHolder; } /// @@ -92,9 +102,16 @@ public class TenantManager implements TenantPersistenceService { { if (tenant == null) { setLanguageFieldSuffix(language); - var tmp = this.repository.findById(tenantId); - if (tmp.isPresent()) { - tenant = new Tenant(tmp.get(),language); + if (CafEnvironment.enablePrimaryDataService()) { + EmbeddedTenant tmp = getTenantRpcPersistence().findById(tenantId); + if (tmp != null) { + tenant = new Tenant(tmp,language); + } + } else { + var tmp = this.repository.findById(tenantId); + if (tmp.isPresent()) { + tenant = new Tenant(tmp.get(),language); + } } this.addCache(tenantId, language, tenant); } @@ -114,9 +131,16 @@ public class TenantManager implements TenantPersistenceService { } public void deleteTenantById(Integer tenantId){ - Optional result=this.repository.findById(tenantId); - if(result.isPresent()){ - this.repository.deleteById(tenantId); + if (CafEnvironment.enablePrimaryDataService()) { + EmbeddedTenant result = getTenantRpcPersistence().findById(tenantId); + if(result != null){ + getTenantRpcPersistence().deleteById(tenantId); + } + } else { + Optional result=this.repository.findById(tenantId); + if(result.isPresent()){ + this.repository.deleteById(tenantId); + } } } /** @@ -137,7 +161,12 @@ public class TenantManager implements TenantPersistenceService { } else{ setLanguageFieldSuffix(language); - List allEmbeddedTenants = this.repository.findAll(); + List allEmbeddedTenants = new ArrayList<>(); + if (CafEnvironment.enablePrimaryDataService()) { + allEmbeddedTenants = getTenantRpcPersistence().findAll(); + } else { + allEmbeddedTenants = this.repository.findAll(); + } List allTenants=new ArrayList<>(); if(allEmbeddedTenants!=null&&allEmbeddedTenants.size()>0){ for(EmbeddedTenant embeddedTenant:allEmbeddedTenants){ @@ -158,7 +187,12 @@ public class TenantManager implements TenantPersistenceService { DataValidator.checkForEmptyString(tenantCode, "tenantCode"); setLanguageFieldSuffix(language); - EmbeddedTenant embeddedTenant=this.repository.findByCode(tenantCode); + EmbeddedTenant embeddedTenant; + if (CafEnvironment.enablePrimaryDataService()) { + embeddedTenant = getTenantRpcPersistence().findByCode(tenantCode); + } else { + embeddedTenant = this.repository.findByCode(tenantCode); + } Tenant tenant=null; if(embeddedTenant!=null){ tenant=new Tenant(embeddedTenant,language); @@ -197,7 +231,11 @@ public class TenantManager implements TenantPersistenceService { { DataValidator.checkForNullReference(id, "TenantId"); - this.repository.deleteById(id); + if (CafEnvironment.enablePrimaryDataService()) { + getTenantRpcPersistence().deleteById(id); + } else { + this.repository.deleteById(id); + } this.removeCache(id); return true; } @@ -227,17 +265,28 @@ public class TenantManager implements TenantPersistenceService { } private void saveEmbeddedTenant(Tenant tenant) { - Optional result = this.repository.findById(tenant.getId()); - if (result.isPresent()) { + if (CafEnvironment.enablePrimaryDataService()) { + EmbeddedTenant result = getTenantRpcPersistence().findById(tenant.getId()); EmbeddedTenant embeddedTenant = new EmbeddedTenant(tenant); - EmbeddedTenant selectTenant = result.get(); - selectTenant.getName().setValue(tenant.getName()); - embeddedTenant.setName(selectTenant.getName()); - this.repository.save(embeddedTenant); - }else{ - EmbeddedTenant embeddedTenant=new EmbeddedTenant(tenant); - embeddedTenant.setName(new CAFMultiLanguageColumn()); - embeddedTenant.getName().setValue(tenant.getName()); + if (result != null) { + result.getName().setValue(tenant.getName()); + embeddedTenant.setName(result.getName()); + }else{ + embeddedTenant.setName(new CAFMultiLanguageColumn()); + embeddedTenant.getName().setValue(tenant.getName()); + } + getTenantRpcPersistence().save(embeddedTenant); + } else { + Optional result = this.repository.findById(tenant.getId()); + EmbeddedTenant embeddedTenant = new EmbeddedTenant(tenant); + if (result.isPresent()) { + EmbeddedTenant selectTenant = result.get(); + selectTenant.getName().setValue(tenant.getName()); + embeddedTenant.setName(selectTenant.getName()); + }else{ + embeddedTenant.setName(new CAFMultiLanguageColumn()); + embeddedTenant.getName().setValue(tenant.getName()); + } this.repository.save(embeddedTenant); } } @@ -320,4 +369,10 @@ public class TenantManager implements TenantPersistenceService { } } + private TenantRpcPersistence getTenantRpcPersistence(){ + if(this.tenantRpcPersistence==null){ + this.tenantRpcPersistence = rpcClassHolder.getRpcClass("sys", TenantRpcPersistence.class); + } + return this.tenantRpcPersistence; + } } diff --git a/caf-tenancy/caf-tenancy-repository-jpa/src/main/java/io/iec/edp/caf/tenancy/core/service/TenantRoutingManager.java b/caf-tenancy/caf-tenancy-repository-jpa/src/main/java/io/iec/edp/caf/tenancy/core/service/TenantRoutingManager.java index 7111e2731217ea2e81ad9c3fb3a5559d26fc0934..fb37c8fecef9a73d9ba7ad342a59d6f8de17028c 100644 --- a/caf-tenancy/caf-tenancy-repository-jpa/src/main/java/io/iec/edp/caf/tenancy/core/service/TenantRoutingManager.java +++ b/caf-tenancy/caf-tenancy-repository-jpa/src/main/java/io/iec/edp/caf/tenancy/core/service/TenantRoutingManager.java @@ -16,8 +16,12 @@ package io.iec.edp.caf.tenancy.core.service; +import io.iec.edp.caf.commons.runtime.CafEnvironment; import io.iec.edp.caf.commons.utils.StringUtils; +import io.iec.edp.caf.rpc.client.RpcClassHolder; import io.iec.edp.caf.tenancy.api.service.TenantRoutingService; +import io.iec.edp.caf.tenancy.core.persistence.TenantExtendRpcPersistence; +import io.iec.edp.caf.tenancy.core.persistence.TenantRoutingRpcPersistence; import io.iec.edp.caf.tenancy.core.repository.TenantRoutingRepository; /** @@ -32,16 +36,36 @@ public class TenantRoutingManager implements TenantRoutingService { //private final HashMap tenantRoutingCache = new HashMap<>(); - public TenantRoutingManager(TenantRoutingRepository repository) { + private TenantRoutingRpcPersistence tenantRoutingRpcPersistence; + + private final RpcClassHolder rpcClassHolder; + + public TenantRoutingManager(TenantRoutingRepository repository, RpcClassHolder rpcClassHolder) { this.repository = repository; + this.rpcClassHolder = rpcClassHolder; } public Integer getTenantId(String suName, String tenantDim1, String tenantDim2) { - if (StringUtils.isEmpty(tenantDim2)) { - return repository.findAvailableTenantId(suName, tenantDim1); + if (CafEnvironment.enablePrimaryDataService()) { + if (StringUtils.isEmpty(tenantDim2)) { + return getTenantRoutingRpcPersistence().findAvailableTenantId(suName, tenantDim1); + } else { + return getTenantRoutingRpcPersistence().findAvailableTenantIdByDim2(suName, tenantDim1, tenantDim2); + } } else { - return repository.findAvailableTenantId(suName, tenantDim1, tenantDim2); + if (StringUtils.isEmpty(tenantDim2)) { + return repository.findAvailableTenantId(suName, tenantDim1); + } else { + return repository.findAvailableTenantId(suName, tenantDim1, tenantDim2); + } + } + } + + private TenantRoutingRpcPersistence getTenantRoutingRpcPersistence(){ + if(this.tenantRoutingRpcPersistence==null){ + this.tenantRoutingRpcPersistence = rpcClassHolder.getRpcClass("sys", TenantRoutingRpcPersistence.class); } + return this.tenantRoutingRpcPersistence; } }