提交 c2d07424 编写于 作者: sq3536's avatar sq3536

init

上级 4f6069f5
*volumes
*target
.settings
*node_modules
*bin
*.project
*.classpath
*.factorypath
.history
.vscode
.idea
**.iml
*.jar
*.log
.DS_Store
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.0</version>
</parent>
<packaging>pom</packaging>
<artifactId>ibiz-boot-starter-parent</artifactId>
<groupId>net.ibizsys.plugin</groupId>
<version>2.4.0-SNAPSHOT</version>
<name>iBiz Boot Starter Parent</name>
<description>iBiz Boot Starter Parent</description>
<properties>
<revision>2.4.0-SNAPSHOT</revision>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<ibiz-boot-starter.version>2.4.0-SNAPSHOT</ibiz-boot-starter.version>
<ibiz.cloud.version>8.1.0.132</ibiz.cloud.version>
<spring-cloud.version>2020.0.1</spring-cloud.version>
<spring-cloud-starter-bootstrap.version>3.0.1</spring-cloud-starter-bootstrap.version>
<fastjson.version>1.2.83</fastjson.version>
<zalando-problem-spring-web.version>0.23.0</zalando-problem-spring-web.version>
<maven.deploy.skip>false</maven.deploy.skip>
<maven-jar-plugin.version>2.6</maven-jar-plugin.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>net.ibizsys.plugin</groupId>
<artifactId>ibiz-plugin-cloud</artifactId>
<version>${ibiz.cloud.version}</version>
</dependency>
<dependency>
<groupId>net.ibizsys.plugin</groupId>
<artifactId>ibiz-plugin-redis</artifactId>
<version>${ibiz.cloud.version}</version>
</dependency>
<dependency>
<groupId>net.ibizsys.plugin</groupId>
<artifactId>ibiz-plugin-mybatisplus-spring-boot-starter</artifactId>
<version>${ibiz.cloud.version}</version>
</dependency>
<dependency>
<groupId>net.ibizsys.plugin</groupId>
<artifactId>ibiz-plugin-zookeeper</artifactId>
<version>${ibiz.cloud.version}</version>
</dependency>
<dependency>
<groupId>net.ibizsys.plugin</groupId>
<artifactId>ibiz-plugin-poi</artifactId>
<version>${ibiz.cloud.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>${fastjson.version}</version>
</dependency>
<dependency>
<groupId>org.zalando</groupId>
<artifactId>problem-spring-web</artifactId>
<version>${zalando-problem-spring-web.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
<version>${spring-cloud-starter-bootstrap.version}</version>
</dependency>
<dependency>
<groupId>net.ibizsys.plugin</groupId>
<artifactId>ibiz-boot-starter</artifactId>
<version>${ibiz-boot-starter.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<distributionManagement>
<repository>
<id>ibizmvnrepository</id>
<url>http://172.16.240.220:8081/repository/releases</url>
</repository>
<snapshotRepository>
<id>ibizmvnrepository</id>
<url>http://172.16.240.220:8081/repository/snapshots</url>
</snapshotRepository>
</distributionManagement>
<repositories>
<repository>
<id>ibizmvnrepository</id>
<name>ibizmvnrepository</name>
<url>http://172.16.240.220:8081/repository/public/</url>
<layout>default</layout>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
</snapshots>
</repository>
</repositories>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>ibiz-boot-starter</artifactId>
<groupId>net.ibizsys.plugin</groupId>
<version>2.4.0-SNAPSHOT</version>
<name>iBiz Boot Starter</name>
<description>iBiz Boot Starter</description>
<parent>
<groupId>net.ibizsys.plugin</groupId>
<artifactId>ibiz-boot-starter-parent</artifactId>
<version>2.4.0-SNAPSHOT</version>
<relativePath>../ibiz-boot-starter-parent/pom.xml</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>net.ibizsys.plugin</groupId>
<artifactId>ibiz-plugin-cloud</artifactId>
</dependency>
<dependency>
<groupId>net.ibizsys.plugin</groupId>
<artifactId>ibiz-plugin-redis</artifactId>
</dependency>
<dependency>
<groupId>net.ibizsys.plugin</groupId>
<artifactId>ibiz-plugin-mybatisplus-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>net.ibizsys.plugin</groupId>
<artifactId>ibiz-plugin-zookeeper</artifactId>
</dependency>
<dependency>
<groupId>net.ibizsys.plugin</groupId>
<artifactId>ibiz-plugin-poi</artifactId>
</dependency>
<!-- Lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
</dependency>
<dependency>
<groupId>com.github.ben-manes.caffeine</groupId>
<artifactId>caffeine</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- webflux -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<!-- webservice -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web-services</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.batch</groupId>
<artifactId>spring-batch-core</artifactId>
</dependency>
<!-- <dependency>-->
<!-- <groupId>org.springframework.cloud</groupId>-->
<!-- <artifactId>spring-cloud-starter-bootstrap</artifactId>-->
<!-- </dependency>-->
<dependency>
<groupId>org.zalando</groupId>
<artifactId>problem-spring-web</artifactId>
</dependency>
</dependencies>
<build>
<sourceDirectory>src/main/java</sourceDirectory>
<resources>
<resource>
<directory>src/main/java</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.0.0</version>
<configuration>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.7</version>
<configuration>
<!-- 更新元数据 -->
<updateReleaseInfo>true</updateReleaseInfo>
<skip>${maven.deploy.skip}</skip>
</configuration>
</plugin>
</plugins>
</build>
</project>
\ No newline at end of file
package net.ibizsys.central.plugin.boot.core.cache;
import com.github.benmanes.caffeine.cache.CaffeineSpec;
import net.ibizsys.central.plugin.boot.core.cache.cacheManager.CaffeineCacheManager;
import org.springframework.boot.autoconfigure.cache.CacheProperties;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.util.StringUtils;
/**
* Caffeine缓存配置类
*/
@EnableCaching
@Configuration
@EnableConfigurationProperties(CacheProperties.class)
@ConditionalOnExpression("'${ibiz.cacheLevel:None}'.equals('L1')")
public class CaffeineCacheConfig {
/**
* Caffeine配置:设置过期时间
* @return
*/
@Bean
@Primary
public CacheManager cacheManager(CacheProperties cacheProperties) {
CaffeineCacheManager caffeineCacheManager=new CaffeineCacheManager();
String strCacheConfig = cacheProperties.getCaffeine().getSpec();
if (StringUtils.hasText(strCacheConfig)) {
caffeineCacheManager.setCaffeineCacheConfig(CaffeineSpec.parse(strCacheConfig));
}
return caffeineCacheManager;
}
}
\ No newline at end of file
package net.ibizsys.central.plugin.boot.core.cache;
import net.ibizsys.central.plugin.boot.core.cache.cache.Globs;
import net.ibizsys.central.plugin.boot.core.cache.cacheManager.LayeringCacheManager;
import net.ibizsys.central.plugin.boot.core.cache.listener.RedisMessageListener;
import net.ibizsys.central.plugin.boot.core.cache.redis.CustomJacksonSerializer;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.benmanes.caffeine.cache.CaffeineSpec;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.cache.CacheProperties;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheWriter;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.util.StringUtils;
import java.time.Duration;
/**
* 缓存配置类
* 1级缓存为caffeine
* 2级缓存为redis
*/
@EnableCaching
@Configuration
@EnableConfigurationProperties(CacheProperties.class)
@ConditionalOnExpression("'${ibiz.cacheLevel:None}'.equals('L2')")
public class RedisCacheConfig {
@Value("${spring.cache.redis.time-to-live:3600}")
private long timeToLive;
/**
* 分层缓存管理器
* @param redisConnectionFactory
* @param cacheProperties
* @return
*/
@Bean
@Primary
public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory , CacheProperties cacheProperties){
LayeringCacheManager layeringCacheManager=new LayeringCacheManager(redisCacheWriter(redisConnectionFactory),redisCacheConfiguration(),redisTemplate(redisConnectionFactory));
String strCacheConfig = cacheProperties.getCaffeine().getSpec();
if (StringUtils.hasText(strCacheConfig)) {
layeringCacheManager.setCaffeineCacheConfig(CaffeineSpec.parse(strCacheConfig));
}
return layeringCacheManager;
}
/**
* redis配置:设置过期时间及序列化方式
* @return
*/
@Bean
public RedisCacheConfiguration redisCacheConfiguration(){
CustomJacksonSerializer jackson2JsonRedisSerializer = new CustomJacksonSerializer(Object.class);
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(om);
RedisCacheConfiguration configuration = RedisCacheConfiguration.defaultCacheConfig()
.entryTtl(Duration.ofSeconds(timeToLive))
.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer()))
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer))
.disableCachingNullValues();
return configuration;
}
/**
* 创建redis缓存
* @param connectionFactory
* @return
*/
@Bean
public RedisCacheWriter redisCacheWriter(RedisConnectionFactory connectionFactory){
RedisCacheWriter redisCacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(connectionFactory);
return redisCacheWriter;
}
/**
* 发送redis广播
* @param factory
* @return
*/
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setConnectionFactory(factory);
CustomJacksonSerializer jacksonSerial = new CustomJacksonSerializer(Object.class);
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
jacksonSerial.setObjectMapper(om);
template.setValueSerializer(jacksonSerial);
template.setKeySerializer(new StringRedisSerializer());
template.setHashKeySerializer(new StringRedisSerializer());
template.setHashValueSerializer(jacksonSerial);
template.afterPropertiesSet();
return template;
}
/**
* 监听redis指定频道
* @param redisConnectionFactory
* @param cacheManager
* @param redisTemplate
* @return
*/
@Bean
RedisMessageListenerContainer redisContainer(RedisConnectionFactory redisConnectionFactory, CacheManager cacheManager , RedisTemplate redisTemplate) {
RedisMessageListenerContainer container = new RedisMessageListenerContainer();
MessageListenerAdapter messageListener=new RedisMessageListener(cacheManager,redisTemplate);
container.setConnectionFactory(redisConnectionFactory);
container.addMessageListener(messageListener, Globs.RedisChannelTopic.REDIS_CACHE_DELETE_TOPIC.getChannelTopic());
container.addMessageListener(messageListener, Globs.RedisChannelTopic.REDIS_CACHE_CLEAR_TOPIC.getChannelTopic());
container.addMessageListener(messageListener, Globs.RedisChannelTopic.REDIS_CACHE_DYNAMICMODEL_TOPIC.getChannelTopic());
return container;
}
}
\ No newline at end of file
package net.ibizsys.central.plugin.boot.core.cache.cache;
import com.github.benmanes.caffeine.cache.Cache;
import org.springframework.cache.caffeine.CaffeineCache;
import javax.validation.constraints.NotNull;
import java.util.Set;
import java.util.stream.Collectors;
/**
* 自定义的Caffeine缓存
*/
public class CusCaffeineCache extends CaffeineCache {
public CusCaffeineCache(String name, Cache<Object, Object> cache) {
super(name,cache);
}
public CusCaffeineCache(String name, Cache<Object, Object> cache, boolean allowNullValues) {
super(name,cache,allowNullValues);
}
@Override
public void evict(@NotNull Object key) {
if (key instanceof String && ((String) key).startsWith("glob:")) {
String globPattern = ((String)key).split("glob:")[1];
// 将Glob匹配转换成正则匹配
String regexPattern = Globs.toUnixRegexPattern(globPattern);
// String regexPattern = Globs.toWindowsRegexPattern(globPattern);
Cache<Object,Object> cache = super.getNativeCache();
Set<Object> keySet = cache.asMap().keySet();
keySet = keySet.stream().filter(o->o.toString().matches(regexPattern)).collect(Collectors.toSet());
cache.invalidateAll(keySet);
}else{
super.evict(key);
}
}
}
package net.ibizsys.central.plugin.boot.core.cache.cache;
import org.springframework.data.redis.cache.RedisCache;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheWriter;
import javax.validation.constraints.NotNull;
/**
* 自定义的redis缓存
*/
public class CusRedisCache extends RedisCache {
public CusRedisCache(String name, RedisCacheWriter redisCacheWriter, RedisCacheConfiguration configuration) {
super(name, redisCacheWriter, configuration);
}
@Override
public void evict(@NotNull Object key) {
if (key instanceof String && ((String) key).startsWith("glob:")) {
String globPattern = ((String)key).split("glob:")[1];
byte[] globPatternBytes = super.getCacheConfiguration().getConversionService().convert(globPattern,byte[].class);
if(globPatternBytes!=null){
super.getNativeCache().clean(super.getName(),globPatternBytes);
}
}else{
super.evict(key);
}
}
}
package net.ibizsys.central.plugin.boot.core.cache.cache;
import org.springframework.data.redis.listener.ChannelTopic;
import java.util.regex.PatternSyntaxException;
/**
* Copy from class sun.nio.fs.Globs
*/
public class Globs {
private Globs() { }
private static final String regexMetaChars = ".^$+{[]|()";
private static final String globMetaChars = "\\*?[{";
private static boolean isRegexMeta(char c) {
return regexMetaChars.indexOf(c) != -1;
}
private static boolean isGlobMeta(char c) {
return globMetaChars.indexOf(c) != -1;
}
private static char EOL = 0; //TBD
private static char next(String glob, int i) {
if (i < glob.length()) {
return glob.charAt(i);
}
return EOL;
}
/**
* Creates a regex pattern from the given glob expression.
*
* @throws PatternSyntaxException
*/
private static String toRegexPattern(String globPattern, boolean isDos) {
boolean inGroup = false;
StringBuilder regex = new StringBuilder("^");
int i = 0;
while (i < globPattern.length()) {
char c = globPattern.charAt(i++);
switch (c) {
case '\\':
// escape special characters
if (i == globPattern.length()) {
throw new PatternSyntaxException("No character to escape",
globPattern, i - 1);
}
char next = globPattern.charAt(i++);
if (isGlobMeta(next) || isRegexMeta(next)) {
regex.append('\\');
}
regex.append(next);
break;
case '/':
if (isDos) {
regex.append("\\\\");
} else {
regex.append(c);
}
break;
case '[':
// don't match name separator in class
if (isDos) {
regex.append("[[^\\\\]&&[");
} else {
regex.append("[[^/]&&[");
}
if (next(globPattern, i) == '^') {
// escape the regex negation char if it appears
regex.append("\\^");
i++;
} else {
// negation
if (next(globPattern, i) == '!') {
regex.append('^');
i++;
}
// hyphen allowed at start
if (next(globPattern, i) == '-') {
regex.append('-');
i++;
}
}
boolean hasRangeStart = false;
char last = 0;
while (i < globPattern.length()) {
c = globPattern.charAt(i++);
if (c == ']') {
break;
}
if (c == '/' || (isDos && c == '\\')) {
throw new PatternSyntaxException("Explicit 'name separator' in class",
globPattern, i - 1);
}
// TBD: how to specify ']' in a class?
if (c == '\\' || c == '[' ||
c == '&' && next(globPattern, i) == '&') {
// escape '\', '[' or "&&" for regex class
regex.append('\\');
}
regex.append(c);
if (c == '-') {
if (!hasRangeStart) {
throw new PatternSyntaxException("Invalid range",
globPattern, i - 1);
}
if ((c = next(globPattern, i++)) == EOL || c == ']') {
break;
}
if (c < last) {
throw new PatternSyntaxException("Invalid range",
globPattern, i - 3);
}
regex.append(c);
hasRangeStart = false;
} else {
hasRangeStart = true;
last = c;
}
}
if (c != ']') {
throw new PatternSyntaxException("Missing ']", globPattern, i - 1);
}
regex.append("]]");
break;
case '{':
if (inGroup) {
throw new PatternSyntaxException("Cannot nest groups",
globPattern, i - 1);
}
regex.append("(?:(?:");
inGroup = true;
break;
case '}':
if (inGroup) {
regex.append("))");
inGroup = false;
} else {
regex.append('}');
}
break;
case ',':
if (inGroup) {
regex.append(")|(?:");
} else {
regex.append(',');
}
break;
case '*':
if (next(globPattern, i) == '*') {
// crosses directory boundaries
regex.append(".*");
i++;
} else {
// within directory boundary
if (isDos) {
regex.append("[^\\\\]*");
} else {
regex.append("[^/]*");
}
}
break;
case '?':
if (isDos) {
regex.append("[^\\\\]");
} else {
regex.append("[^/]");
}
break;
default:
if (isRegexMeta(c)) {
regex.append('\\');
}
regex.append(c);
}
}
if (inGroup) {
throw new PatternSyntaxException("Missing '}", globPattern, i - 1);
}
return regex.append('$').toString();
}
public static String toUnixRegexPattern(String globPattern) {
return toRegexPattern(globPattern, false);
}
public static String toWindowsRegexPattern(String globPattern) {
return toRegexPattern(globPattern, true);
}
/**
* redis频道
*/
public enum RedisChannelTopic {
REDIS_CACHE_DELETE_TOPIC("redis:cache:delete:topic1", "删除redis缓存消息频道"),
REDIS_CACHE_CLEAR_TOPIC("redis:cache:clear:topic2", "清空redis缓存消息频道"),
REDIS_CACHE_DYNAMICMODEL_TOPIC("redis:cache:dynamicmodel:topic3", "动态模型频道");
String channelTopic;
String label;
RedisChannelTopic(String channelTopic, String label) {
this.channelTopic = channelTopic;
this.label = label;
}
public ChannelTopic getChannelTopic() {
return new ChannelTopic(channelTopic);
}
public static RedisChannelTopic getChannelTopicEnum(String channelTopic) {
for (RedisChannelTopic awardTypeEnum : RedisChannelTopic.values()) {
if (awardTypeEnum.getChannelTopicStr().equals(channelTopic)) {
return awardTypeEnum;
}
}
return null;
}
public String getChannelTopicStr() {
return channelTopic;
}
}
}
package net.ibizsys.central.plugin.boot.core.cache.cache;
import lombok.extern.slf4j.Slf4j;
import net.ibizsys.central.plugin.boot.core.cache.listener.RedisPublisher;
import org.springframework.cache.caffeine.CaffeineCache;
import org.springframework.cache.support.AbstractValueAdaptingCache;
import org.springframework.cache.support.NullValue;
import org.springframework.data.redis.cache.RedisCache;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.util.ObjectUtils;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Callable;
/**
* 缓存分层类
* 1级缓存为caffeine
* 2级缓存为redis
*/
@Slf4j
public class LayeringCache extends AbstractValueAdaptingCache {
/**
* 缓存的名称
*/
private String cacheName;
/**
* Caffeine缓存
*/
private CaffeineCache caffeineCache;
/**
* redis缓存
*/
private RedisCache redisCache;
/**
* redis消息发布
*/
RedisOperations<? extends Object, ? extends Object> redisOperations;
public LayeringCache(String cacheName , CaffeineCache caffeineCache, RedisCache redisCache, RedisOperations redisOperations) {
super(true);
this.cacheName = cacheName;
this.caffeineCache = caffeineCache;
this.redisCache = redisCache;
this.redisOperations=redisOperations;
}
@Override
public String getName() {
return this.cacheName;
}
@Override
public Object getNativeCache() {
return this;
}
@Override
public ValueWrapper get(Object key) {
ValueWrapper wrapper = caffeineCache.get(key);
Object value = ObjectUtils.isEmpty(wrapper) ? null : wrapper.get();
log.debug("查询一级缓存 key:{} ", key, value);
if (ObjectUtils.isEmpty(value)) {
wrapper = redisCache.get(key);
value = ObjectUtils.isEmpty(wrapper) ? null : wrapper.get();
log.debug("查询二级缓存 key:{} ", key);
if (!ObjectUtils.isEmpty(value)) {
caffeineCache.put(key, value);
log.debug("查询二级缓存,并将数据放到一级缓存。 key:{} ", key);
}
}
return wrapper;
}
@Override
public <T> T get(Object key, Class<T> type) {
T value = caffeineCache.get(key, type);
log.debug("查询一级缓存 key:{}", key);
if (value == null) {
value = redisCache.get(key, type);
caffeineCache.put(key, value);
log.debug("查询二级缓存,并将数据放到一级缓存。 key:{}", key);
}
return value;
}
@SuppressWarnings("unchecked")
@Override
public <T> T get(Object key, Callable<T> valueLoader) {
T value = (T) caffeineCache.getNativeCache().get(key, k -> getSecondCacheValue(k, valueLoader));
if(value==null) {
value = (T) getSecondCacheValue(key, valueLoader);
}
if (value instanceof NullValue) {
return null;
}
return value;
}
@Override
public void put(Object key, Object value) {
if(value!=null) {
caffeineCache.put(key, value);
redisCache.put(key, value);
}
}
@Override
public ValueWrapper putIfAbsent(Object key, Object value) {
caffeineCache.putIfAbsent(key, value);
return redisCache.putIfAbsent(key, value);
}
@Override
public void evict(Object key) {
redisCache.evict(key); //清除redis中的二级缓存
caffeineCache.evict(key);//清除本机一级缓存
Map<String, Object> message = new HashMap<>();
message.put("cacheName", cacheName);
message.put("key", key);
RedisPublisher redisPublisher = new RedisPublisher(redisOperations, Globs.RedisChannelTopic.REDIS_CACHE_DELETE_TOPIC.getChannelTopic());// 创建redis发布者
redisPublisher.publisher(message);//发布消息,清除其它集群机器中的一级缓存
log.debug(String.format("清除二级缓存数据[%s]", key));
}
@Override
public void clear() {
redisCache.clear(); //清除redis中的二级缓存
caffeineCache.clear();//清除本机一级缓存
Map<String, Object> message = new HashMap<>();
message.put("cacheName", cacheName);
RedisPublisher redisPublisher = new RedisPublisher(redisOperations, Globs.RedisChannelTopic.REDIS_CACHE_CLEAR_TOPIC.getChannelTopic());// 创建redis发布者
redisPublisher.publisher(message);//发布消息,清除其它集群机器中的一级缓存
}
@Override
protected Object lookup(Object key) {
Object value = caffeineCache.get(key);
log.debug("查询一级缓存 key:{}", key);
if (value == null) {
value = redisCache.get(key);
log.debug("查询二级缓存 key:{}", key);
}
return value;
}
/**
* 获取caffeine缓存
* @return
*/
public CaffeineCache getFirstCache() {
return this.caffeineCache;
}
/**
* 获取redis缓存
* @return
*/
public RedisCache getSecondCache() {
return this.redisCache;
}
/**
* 查询二级缓存
* @param key
* @param valueLoader
* @return
*/
private <T> Object getSecondCacheValue(Object key, Callable<T> valueLoader) {
T value = redisCache.get(key, valueLoader);
log.debug("查询二级缓存 key:{}", key);
return toStoreValue(value);
}
}
package net.ibizsys.central.plugin.boot.core.cache.cacheManager;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.CaffeineSpec;
import lombok.Data;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.cache.caffeine.CaffeineCache;
import org.springframework.util.ObjectUtils;
import java.util.Collection;
import java.util.Collections;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.TimeUnit;
/**
* Caffeine本地缓存
*/
@Data
public class CaffeineCacheManager implements CacheManager {
private final ConcurrentMap<String, Cache> cacheMap = new ConcurrentHashMap<String, Cache>(16);
private static final int default_expire_after_write = 1;
private static final int default_initial_capacity = 5;
private static final int default_maximum_size = 1_000;
private Caffeine<Object, Object> cacheBuilder = Caffeine.newBuilder()
.expireAfterAccess(default_expire_after_write, TimeUnit.HOURS)
.initialCapacity(default_initial_capacity)
.maximumSize(default_maximum_size);
/**
* 获取缓存对象
* @param cacheName
* @return
*/
@Override
public Cache getCache(String cacheName) {
Cache cache = this.cacheMap.get(cacheName);
if (cache == null) {
synchronized (this.cacheMap) {
cache = this.cacheMap.get(cacheName);
if (cache == null) {
cache = createCache(cacheName);
this.cacheMap.put(cacheName, cache);
}
}
}
return cache;
}
/**
* 创建缓存
* @param cacheName
* @return
*/
protected Cache createCache(String cacheName) {
return new CaffeineCache(cacheName, this.cacheBuilder.build(), true);
}
/**
* 获取缓存名
* @return
*/
@Override
public Collection<String> getCacheNames() {
return Collections.unmodifiableSet(this.cacheMap.keySet());
}
/**
* 缓存配置[缓存容量大小、时长等]
* @param caffeineCacheConfig
*/
public void setCaffeineCacheConfig(CaffeineSpec caffeineCacheConfig) {
Caffeine<Object, Object> cacheBuilder = Caffeine.from(caffeineCacheConfig);
if (!ObjectUtils.nullSafeEquals(this.cacheBuilder, cacheBuilder)) {
this.cacheBuilder = cacheBuilder;
}
}
}
package net.ibizsys.central.plugin.boot.core.cache.cacheManager;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.CaffeineSpec;
import lombok.Data;
import net.ibizsys.central.plugin.boot.core.cache.cache.CusCaffeineCache;
import net.ibizsys.central.plugin.boot.core.cache.cache.CusRedisCache;
import net.ibizsys.central.plugin.boot.core.cache.cache.LayeringCache;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheWriter;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.ObjectUtils;
import java.util.Collection;
import java.util.Collections;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.TimeUnit;
/**
* 缓存分层
* 1级缓存为caffeine
* 2级缓存为redis
*/
@Data
public class LayeringCacheManager implements CacheManager {
private final ConcurrentMap<String, Cache> cacheMap = new ConcurrentHashMap<String, Cache>(16);
private static final int default_expire_after_write = 1;
private static final int default_initial_capacity = 5;
private static final int default_maximum_size = 1_000;
private Caffeine<Object, Object> cacheBuilder = Caffeine.newBuilder()
.expireAfterAccess(default_expire_after_write, TimeUnit.HOURS)
.initialCapacity(default_initial_capacity)
.maximumSize(default_maximum_size);
private RedisCacheWriter redisCacheWriter;
private RedisCacheConfiguration redisConfiguration;
private RedisTemplate<String, Object> redisTemplate;
public LayeringCacheManager(RedisCacheWriter redisCacheWriter , RedisCacheConfiguration redisConfiguration , RedisTemplate<String, Object> redisTemplate ){
this.redisCacheWriter=redisCacheWriter;
this.redisConfiguration=redisConfiguration;
this.redisTemplate= redisTemplate;
}
/**
* 获取缓存对象
* @param cacheName
* @return
*/
@Override
public Cache getCache(String cacheName) {
Cache cache = this.cacheMap.get(cacheName);
if (cache == null) {
synchronized (this.cacheMap) {
cache = this.cacheMap.get(cacheName);
if (cache == null) {
cache = createCache(cacheName);
this.cacheMap.put(cacheName, cache);
}
}
}
return cache;
}
/**
* 获取集合中的缓存
* @return
*/
@Override
public Collection<String> getCacheNames() {
return Collections.unmodifiableSet(this.cacheMap.keySet());
}
/**
* 创建缓存
* @param cacheName
* @return
*/
protected Cache createCache(String cacheName) {
return new LayeringCache(cacheName,new CusCaffeineCache(cacheName, this.cacheBuilder.build(), true),new CusRedisCache(cacheName, redisCacheWriter, redisConfiguration),redisTemplate);
}
/**
* 缓存配置[缓存容量大小、时长等]
* @param caffeineCacheConfig
*/
public void setCaffeineCacheConfig(CaffeineSpec caffeineCacheConfig) {
Caffeine<Object, Object> cacheBuilder = Caffeine.from(caffeineCacheConfig);
if (!ObjectUtils.nullSafeEquals(this.cacheBuilder, cacheBuilder)) {
this.cacheBuilder = cacheBuilder;
}
}
}
package net.ibizsys.central.plugin.boot.core.cache.listener;
import lombok.extern.slf4j.Slf4j;
import net.ibizsys.central.plugin.boot.core.cache.cache.Globs;
import net.ibizsys.central.plugin.boot.core.cache.cache.LayeringCache;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import java.util.Map;
/**
* redis消息的订阅者
*/
@Slf4j
public class RedisMessageListener extends MessageListenerAdapter {
private CacheManager cacheManager;
private RedisTemplate redisTemplate;
public RedisMessageListener(CacheManager cacheManager, RedisTemplate redisTemplate){
this.cacheManager=cacheManager;
this.redisTemplate=redisTemplate;
}
@Override
public void onMessage(Message message, byte[] pattern) {
Globs.RedisChannelTopic channelTopic = Globs.RedisChannelTopic.getChannelTopicEnum(new String(message.getChannel()));
Map<String, Object> map = null;
RedisSerializer serializer=redisTemplate.getValueSerializer();
Object result=serializer.deserialize(message.getBody());
if(result instanceof Map){
map= (Map<String, Object>) result;
}
if(StringUtils.isEmpty(map)|| (!map.containsKey("cacheName"))){
log.debug("解析缓存数据失败,无法获取指定值!");
return ;
}
log.debug("redis消息订阅者接收到频道【{}】发布的消息。消息内容:{}", channelTopic.getChannelTopicStr(), result.toString());
String cacheName = (String) map.get("cacheName");
Cache cache = cacheManager.getCache(cacheName);// 根据缓存名称获取多级缓存
if (cache != null && cache instanceof LayeringCache) { // 判断缓存是否是多级缓存
switch (channelTopic) {
case REDIS_CACHE_DELETE_TOPIC: // 获取一级缓存,并删除一级缓存数据
Object cacheKey = map.get("key");
if(!ObjectUtils.isEmpty(cacheKey)){
((LayeringCache) cache).getFirstCache().evict(cacheKey);
((LayeringCache) cache).getSecondCache().evict(cacheKey);
log.debug("同步删除缓存{}数据,key:{},", cacheName, cacheKey.toString());
}
else{
log.debug("同步删除缓存失败,{}缓存键值为空!",cacheName);
}
break;
case REDIS_CACHE_CLEAR_TOPIC:// 获取一级缓存,并删除一级缓存数据
((LayeringCache) cache).getFirstCache().clear();
((LayeringCache) cache).getSecondCache().clear();
log.debug("同步清除缓存{}数据", cacheName);
break;
default:
log.debug("接收到没有定义的订阅消息频道数据");
break;
}
}
}
}
package net.ibizsys.central.plugin.boot.core.cache.listener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.listener.ChannelTopic;
/**
* redis消息的发布者
*/
public class RedisPublisher {
private static final Logger logger = LoggerFactory.getLogger(RedisPublisher.class);
RedisOperations<? extends Object, ? extends Object> redisOperations;
/**
* 频道名称
*/
ChannelTopic channelTopic;
/**
* @param redisOperations Redis客户端
* @param channelTopic 频道名称
*/
public RedisPublisher(RedisOperations<? extends Object, ? extends Object> redisOperations, ChannelTopic channelTopic) {
this.channelTopic = channelTopic;
this.redisOperations = redisOperations;
}
/**
* 发布消息到频道(Channel)
*
* @param message 消息内容
*/
public void publisher(Object message) {
redisOperations.convertAndSend(channelTopic.toString(), message);
logger.debug("redis消息发布者向频道【{}】发布了【{}】消息", channelTopic.toString(), message.toString());
}
}
package net.ibizsys.central.plugin.boot.core.cache.redis;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.SerializationException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class CustomJacksonSerializer<T> extends Jackson2JsonRedisSerializer<T> {
public static final String DEFAULT_PACKAGE ="[\\w+\\.]+\\.AuthenticationUser";
public static final String CLASSNAME_EX="_$$_";
public static final String CLASSNAME_EX_PATTEN="(_\\$\\$_)(\\w+)";
public CustomJacksonSerializer(Class type) {
super(type);
}
@Override
public T deserialize(byte[] bytes) throws SerializationException {
String serializerContent = new String(bytes, DEFAULT_CHARSET);
Matcher matcher = Pattern.compile(DEFAULT_PACKAGE).matcher(serializerContent);
if(serializerContent.contains(CLASSNAME_EX)){
serializerContent=serializerContent.replaceAll(CLASSNAME_EX_PATTEN,"");
}
return super.deserialize(serializerContent.getBytes());
}
}
package net.ibizsys.central.plugin.boot.core.dict;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.annotation.JSONField;
import com.alibaba.fastjson.util.TypeUtils;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.experimental.Accessors;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import java.util.*;
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class Catalog
{
private String code;
private String name;
@JSONField(name = "items")
@JsonProperty("items")
private List<Option> options = new ArrayList<>();
public Option findCodeItem(Object value)
{
return findCodeItem(value,this.options);
}
public Option findCodeItem(Object value, List<Option> options){
for(Option codeItem : options){
if(value != null && codeItem.getValue() != null && value.equals(codeItem.getValue()))
return codeItem;
else if (!ObjectUtils.isEmpty(codeItem.getChildren()))
{
Option rt=findCodeItem(value,codeItem.getChildren());
if(rt!=null)
return rt;
}
}
return null;
}
private Map<String, Object> advancedSettings;
public Catalog putAdvancedSettings(String settings)
{
try
{
if(!StringUtils.isEmpty(settings))
advancedSettings= JSON.parseObject(settings, Map.class);
}catch (Exception ex){}
return this;
}
@JsonIgnore
@JSONField(serialize = false)
public String getUrl() {
return getSetting("url",null);
}
@JsonIgnore
@JSONField(serialize = false)
public String getRequestMethod() {
return getSetting("requestMethod","GET");
}
@JsonIgnore
@JSONField(serialize = false)
public String getRequestBody() {
return getSetting("requestBody","{}");
}
@JsonIgnore
@JSONField(serialize = false)
public String getNodeProp() {
return getSetting("nodeProp","");
}
@JsonIgnore
@JSONField(serialize = false)
public String getValueProp() {
return getSetting("valueProp","value");
}
@JsonIgnore
@JSONField(serialize = false)
public String getLabelProp() {
return getSetting("labelProp","label");
}
@JsonIgnore
@JSONField(serialize = false)
public String getParentProp() {
return getSetting("parentProp","parent");
}
@JsonIgnore
@JSONField(serialize = false)
public String getChildrenProp() {
return getSetting("childrenProp","children");
}
@JsonIgnore
@JSONField(serialize = false)
public String getDisabledProp() {
return getSetting("disabledProp","disabled");
}
@JsonIgnore
@JSONField(serialize = false)
public String getFilterProp() {
return getSetting("filterProp","filter");
}
@JsonIgnore
@JSONField(serialize = false)
public String getExpiredProp() {
return getSetting("expiredProp","expired");
}
@JsonIgnore
@JSONField(serialize = false)
public String getIconClassProp() {
return getSetting("iconClassProp","iconClass");
}
public Catalog setNodes(List<Map> nodes)
{
return setNodes(nodes,null,null);
}
public Catalog setNodes(List<Map> nodes, String filter, String subCode)
{
Map<String, List<Option>> map = new LinkedHashMap<>();
Option subOption=null;
for(Map item:unfoldList(nodes,null))
{
Map<String, Object> extension = new HashMap<>();
if(!StringUtils.isEmpty(item.get("extension")))
extension = JSONObject.parseObject(item.get("extension").toString(), Map.class);
String pid= getStringValue(item.get(this.getParentProp()), getStringValue(item.get("pvalue"),"_root"));
if(StringUtils.isEmpty(pid))pid="_root";
List<Option> list=null;
if(!map.containsKey(pid))
{
list=new ArrayList<>();
map.put(pid,list);
}
else
list=map.get(pid);
Option option=new Option().setValue(getStringValue(item.get(this.getValueProp()), getStringValue(item.get("id"),null)))
.setId(getStringValue(item.get(this.getValueProp()), getStringValue(item.get("id"),null)))
.setDisabled(getBooleanValue(item.get(this.getDisabledProp()),false)|| getBooleanValue(item.get(this.getExpiredProp()),false))
.setFilter(getStringValue(item.get(this.getFilterProp()),null)).setIconClass(getStringValue(item.get(this.getIconClassProp()),null))
.setLabel(getStringValue(item.get(this.getLabelProp()), getStringValue(item.get("text"),"")))
.setParent(getStringValue(item.get(this.getParentProp()), getStringValue(item.get("pvalue"),""))).setExtension(extension);
if(option.getValue().equals(subCode))
subOption=option;
if(StringUtils.isEmpty(filter)||option.getFilterSet().contains(filter))
list.add(option);
}
List<Option> codeItemTreeList = loop(map, (StringUtils.isEmpty(subCode))?"_root":subCode);
if(subOption!=null)
{
subOption.setChildren(codeItemTreeList).setParent("");
this.getOptions().add(subOption.setParent(null));
}
else
this.setOptions(codeItemTreeList);
return this;
}
private List<Map> unfoldList(List<Map> nodes, String parent)
{
List<Map> unfoldList = new ArrayList<>();
for(Map item:nodes)
{
if((!StringUtils.isEmpty(parent))&& StringUtils.isEmpty(getStringValue(item.get(this.getParentProp()), getStringValue(item.get("pvalue"),null))))
item.put(this.getParentProp(),parent);
String value = getStringValue(item.get(this.getValueProp()), getStringValue(item.get("id"),null));
unfoldList.add(item);
Object child=item.get(this.getChildrenProp());
if(!ObjectUtils.isEmpty(child))
{
if(child instanceof List && ((List) child).get(0) instanceof Map)
unfoldList.addAll(unfoldList((List)child,value));
item.remove(this.getChildrenProp());
}
}
return unfoldList;
}
private List<Option> loop(Map<String, List<Option>> listCodeItem, Object parentValue) {
List<Option> trees = new ArrayList<Option>();
if(listCodeItem.containsKey(parentValue)) {
for (Option codeItem : listCodeItem.get(parentValue)) {
List<Option> childCodeItem = loop(listCodeItem, codeItem.getValue());
if (childCodeItem.size() > 0)
codeItem.setChildren(childCodeItem);
trees.add(codeItem);
}
}
return trees;
}
private String getSetting(String key,String defaultValue)
{
return advancedSettings!=null&&advancedSettings.get(key)!=null?advancedSettings.get(key).toString():defaultValue;
}
public static <V> V getValue(Object objValue,Class<V> clazz,Object defaultValue)
{
if(objValue==null) return defaultValue==null?null:(V)defaultValue;
return TypeUtils.castToJavaBean(objValue,clazz);
}
public static String getStringValue(Object objValue,String defaultValue)
{
return getValue(objValue,String.class,defaultValue);
}
public static Boolean getBooleanValue(Object objValue,Boolean defaultValue)
{
return getValue(objValue,Boolean.class,defaultValue);
}
}
package net.ibizsys.central.plugin.boot.core.dict;
import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.experimental.Accessors;
import org.springframework.util.StringUtils;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class CodeItem
{
private String id;
private Object value;
private String label;
@JsonIgnore
@JSONField(serialize = false)
private List<CodeItem> children;
private String iconClass;
private String filter;
private Boolean disabled;
@JSONField(name = "pvalue")
@JsonProperty("pvalue")
private String parent;
private Map<String, Object> extension;
@JSONField(name = "text")
@JsonProperty("text")
public String getText()
{
return label;
}
@JsonIgnore
@JSONField(serialize = false)
public Set<String> getFilterSet()
{
Set<String> set=new HashSet<>();
if(!StringUtils.isEmpty(filter))
for(String str:filter.split(";|,"))
set.add(str);
return set;
}
}
package net.ibizsys.central.plugin.boot.core.dict;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.annotation.JSONField;
import com.alibaba.fastjson.util.TypeUtils;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.experimental.Accessors;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import java.util.*;
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class CodeList
{
@JSONField(name = "srfkey")
@JsonProperty("srfkey")
private String code;
private String name;
@JSONField(name = "emptytext")
@JsonProperty("emptytext")
public String getEmptytext()
{
return "";
}
@JSONField(name = "items")
@JsonProperty("items")
private List<CodeItem> options = new ArrayList<>();
public CodeItem findCodeItem(Object value){
for(CodeItem codeItem : options){
if(value != null && codeItem.getValue() != null && value.equals(codeItem.getValue()))
return codeItem;
}
return null;
}
public CodeItem findChildren(Object value){
CodeItem resultCodeItem = this.findCodeItem(value);
List<CodeItem> children = new ArrayList<>();
for(CodeItem codeItem : options){
if(value != null && resultCodeItem.getValue() != null && codeItem.getParent() != null && codeItem.getParent().equals(resultCodeItem.getValue()))
children.add(codeItem);
}
resultCodeItem.setChildren(children);
return resultCodeItem;
}
private Map<String, Object> advancedSettings;
public CodeList putAdvancedSettings(String settings)
{
try
{
if(!StringUtils.isEmpty(settings))
advancedSettings= JSON.parseObject(settings, Map.class);
}catch (Exception ex){}
return this;
}
@JsonIgnore
@JSONField(serialize = false)
public String getUrl() {
return getSetting("url",null);
}
@JsonIgnore
@JSONField(serialize = false)
public String getRequestMethod() {
return getSetting("requestMethod","GET");
}
@JsonIgnore
@JSONField(serialize = false)
public String getRequestBody() {
return getSetting("requestBody","{}");
}
@JsonIgnore
@JSONField(serialize = false)
public String getNodeProp() {
return getSetting("nodeProp","");
}
@JsonIgnore
@JSONField(serialize = false)
public String getValueProp() {
return getSetting("valueProp","value");
}
@JsonIgnore
@JSONField(serialize = false)
public String getLabelProp() {
return getSetting("labelProp","label");
}
@JsonIgnore
@JSONField(serialize = false)
public String getParentProp() {
return getSetting("parentProp","parent");
}
@JsonIgnore
@JSONField(serialize = false)
public String getChildrenProp() {
return getSetting("childrenProp","children");
}
@JsonIgnore
@JSONField(serialize = false)
public String getDisabledProp() {
return getSetting("disabledProp","disabled");
}
@JsonIgnore
@JSONField(serialize = false)
public String getFilterProp() {
return getSetting("filterProp","filter");
}
@JsonIgnore
@JSONField(serialize = false)
public String getExpiredProp() {
return getSetting("expiredProp","expired");
}
@JsonIgnore
@JSONField(serialize = false)
public String getIconClassProp() {
return getSetting("iconClassProp","iconClass");
}
public CodeList setNodes(List<Map> nodes, String filter, String subCode)
{
Map<String, List<CodeItem>> map = new LinkedHashMap<>();
List<CodeItem> alllist = new ArrayList<>();
CodeItem subOption=null;
for(Map item:unfoldList(nodes,null))
{
Map<String, Object> extension = new HashMap<>();
if(!StringUtils.isEmpty(item.get("extension")))
extension = JSONObject.parseObject(item.get("extension").toString(), Map.class);
String pid= getStringValue(item.get(this.getParentProp()), getStringValue(item.get("pvalue"),"_root"));
if(StringUtils.isEmpty(pid))pid="_root";
List<CodeItem> list=null;
if(!map.containsKey(pid))
{
list=new ArrayList<>();
map.put(pid,list);
}
else
list=map.get(pid);
CodeItem option=new CodeItem().setValue(getStringValue(item.get(this.getValueProp()), getStringValue(item.get("id"),null)))
.setId(getStringValue(item.get(this.getValueProp()), getStringValue(item.get("id"),null)))
.setDisabled(getBooleanValue(item.get(this.getDisabledProp()),false)|| getBooleanValue(item.get(this.getExpiredProp()),false))
.setFilter(getStringValue(item.get(this.getFilterProp()),null)).setIconClass(getStringValue(item.get(this.getIconClassProp()),null))
.setLabel(getStringValue(item.get(this.getLabelProp()), getStringValue(item.get("text"),"")))
.setParent(getStringValue(item.get(this.getParentProp()), getStringValue(item.get("pvalue"),""))).setExtension(extension);
if(option.getValue().equals(subCode))
subOption=option;
if(StringUtils.isEmpty(filter)||option.getFilterSet().contains(filter)) {
alllist.add(option);
list.add(option);
}
}
if(subOption!=null)
{
List<CodeItem> codeItemTreeList = loop(map, (StringUtils.isEmpty(subCode))?"_root":subCode);
codeItemTreeList.add(0,subOption.setParent(null));
this.setOptions(codeItemTreeList);
}
else
this.setOptions(alllist);
return this;
}
private List<Map> unfoldList(List<Map> nodes, String parent)
{
List<Map> unfoldList = new ArrayList<>();
for(Map item:nodes)
{
if((!StringUtils.isEmpty(parent))&& StringUtils.isEmpty(getStringValue(item.get(this.getParentProp()), getStringValue(item.get("pvalue"),null))))
item.put(this.getParentProp(),parent);
String value = getStringValue(item.get(this.getValueProp()), getStringValue(item.get("id"),null));
unfoldList.add(item);
Object child=item.get(this.getChildrenProp());
if(!ObjectUtils.isEmpty(child))
{
if(child instanceof List && ((List) child).get(0) instanceof Map)
unfoldList.addAll(unfoldList((List)child,value));
item.remove(this.getChildrenProp());
}
}
return unfoldList;
}
private List<CodeItem> loop(Map<String, List<CodeItem>> listCodeItem, Object parentValue) {
List<CodeItem> trees = new ArrayList<CodeItem>();
if(listCodeItem.containsKey(parentValue)) {
for (CodeItem codeItem : listCodeItem.get(parentValue)) {
trees.add(codeItem);
List<CodeItem> childCodeItem = loop(listCodeItem, codeItem.getValue());
if (childCodeItem.size() > 0)
trees.addAll(childCodeItem);
}
}
return trees;
}
private String getSetting(String key,String defaultValue)
{
return advancedSettings!=null&&advancedSettings.get(key)!=null?advancedSettings.get(key).toString():defaultValue;
}
public static <V> V getValue(Object objValue,Class<V> clazz,Object defaultValue)
{
if(objValue==null) return defaultValue==null?null:(V)defaultValue;
return TypeUtils.castToJavaBean(objValue,clazz);
}
public static String getStringValue(Object objValue,String defaultValue)
{
return getValue(objValue,String.class,defaultValue);
}
public static Boolean getBooleanValue(Object objValue,Boolean defaultValue)
{
return getValue(objValue,Boolean.class,defaultValue);
}
}
package net.ibizsys.central.plugin.boot.core.dict;
import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.experimental.Accessors;
import org.springframework.util.StringUtils;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class Option
{
private String id;
private Object value;
private String label;
private List<Option> children;
private String iconClass;
private String filter;
private Boolean disabled;
@JsonIgnore
@JSONField(serialize = false)
private String parent;
private Map<String, Object> extension;
@JsonIgnore
@JSONField(serialize = false)
public Set<String> getFilterSet()
{
Set<String> set=new HashSet<>();
if(!StringUtils.isEmpty(filter))
for(String str:filter.split(";|,"))
set.add(str);
return set;
}
}
package net.ibizsys.central.plugin.boot.core.domain;
import com.alibaba.fastjson.annotation.JSONField;
import com.alibaba.fastjson.util.TypeUtils;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.*;
import lombok.extern.slf4j.Slf4j;
import net.ibizsys.central.dataentity.IDataEntityRuntime;
import net.ibizsys.central.dataentity.service.IDEMethodDTO;
import net.ibizsys.central.dataentity.service.IDEMethodDTORuntime;
import net.ibizsys.central.plugin.boot.core.helper.FieldCache;
import net.ibizsys.central.util.IEntityDTO;
import net.ibizsys.model.dataentity.defield.IPSDEField;
import net.ibizsys.model.dataentity.service.IPSDEMethodDTOField;
import net.ibizsys.runtime.dataentity.DataEntityRuntimeException;
import net.ibizsys.runtime.dataentity.service.DEMethodDTOFieldTypes;
import net.ibizsys.runtime.dataentity.service.DEMethodDTOTypes;
import net.ibizsys.runtime.util.DataTypeUtils;
import net.ibizsys.runtime.util.DataTypes;
import net.ibizsys.runtime.util.IEntity;
import org.springframework.cglib.beans.BeanMap;
import org.springframework.data.annotation.Transient;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.Timestamp;
import java.util.*;
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Slf4j
public class BaseData implements IEntityDTO {
@JsonIgnore
@JSONField(serialize = false)
private transient IDEMethodDTORuntime iDEMethodDTORuntime = null;
@JsonIgnore
@JSONField(serialize = false)
private boolean actionInputDTO = false;
public void setActionInputDTO(Boolean actionInputDTO)
{
this.actionInputDTO=actionInputDTO.booleanValue();
}
@JsonIgnore
@JSONField(serialize = false)
private boolean enableAny = false;
public void setEnableAny(Boolean enableAny)
{
this.actionInputDTO=enableAny.booleanValue();
}
@Override
public void init(IDEMethodDTORuntime iDEMethodDTORuntime, Object dtoData, boolean bDTOData, boolean bIn) throws Exception {
this.iDEMethodDTORuntime = iDEMethodDTORuntime;
if(this.getDEMethodDTORuntime() == null) {
throw new Exception("传入实体方法DTO运行时对象无效");
}
enableAny = this.getDEMethodDTORuntime().isEnableAny();
if(DEMethodDTOTypes.DEACTIONINPUT.equals(this.getDEMethodDTORuntime().getPSDEMethodDTO().getType())) {
actionInputDTO = true;
}
reload(dtoData, bDTOData, bIn);
onInit();
}
protected void onInit() throws Exception {
}
@Override
public void init(IDEMethodDTORuntime iDEMethodDTORuntime, Object dtoData, boolean bDTOData) throws Exception {
init(iDEMethodDTORuntime, dtoData, bDTOData, false);
}
@Override
@JsonIgnore
@JSONField(serialize = false)
public IDEMethodDTORuntime getDEMethodDTORuntime() {
return this.iDEMethodDTORuntime;
}
@Override
public void setDEMethodDTORuntime(IDEMethodDTORuntime iDEMethodDTORuntime) {
this.iDEMethodDTORuntime = iDEMethodDTORuntime;
this.enableAny = false;
this.actionInputDTO = false;
if(this.getDEMethodDTORuntime() != null) {
enableAny = this.getDEMethodDTORuntime().isEnableAny();
if(DEMethodDTOTypes.DEACTIONINPUT.equals(this.getDEMethodDTORuntime().getPSDEMethodDTO().getType())) {
actionInputDTO = true;
}
}
}
@JsonIgnore
@JSONField(serialize = false)
private Set<String> focusNull=new HashSet<>();
public void setFocusNull(Set<String> focusNull)
{
if(focusNull==null)
this.focusNull.clear();
else
this.focusNull=focusNull;
}
public void modify(String field,Object val) {
if(val==null) {
focusNull.add(field);
}
else {
focusNull.remove(field);
}
}
@JsonIgnore
@JSONField(serialize = false)
public Set<String> getFocusNull() {
if(focusNull.size()>0 && paramMap.containsKey("dirtyflagenable"))
{
Set<String> nocheck=new HashSet<>();
for(String key:focusNull)
{
if(!paramMap.containsKey(key+"dirtyflag")) {
nocheck.add(key);
}
}
for(String key:nocheck) {
focusNull.remove(key);
}
}
return focusNull;
}
@JsonIgnore
@JSONField(serialize = false)
private Map<String,Object> paramMap=new HashMap<String,Object>();
public void setParamMap(Map<String,Object> paramMap)
{
if(paramMap==null)
this.paramMap.clear();
else
this.paramMap=paramMap;
}
@JsonAnyGetter
@JSONField(name = "_any", unwrapped = true, serialize = true, deserialize = false)
public Map<String , Object> anyObject() {
return paramMap;
}
@JsonIgnore
@JSONField(serialize = false)
private Map<String , Object> _allParamMap=null;
@JsonIgnore
@JSONField(serialize = false)
public Map<String , Object> any() {
if(_allParamMap==null)
_allParamMap = new HashMap<>();
_allParamMap.clear();
_allParamMap.putAll(this.paramMap);
_allParamMap.putAll(this.getMap());
focusNull.forEach(item->_allParamMap.put(item,null));
return _allParamMap;
}
@JsonIgnore
@JSONField(serialize = false)
@Transient
private BeanMap map;
@JsonIgnore
@JSONField(serialize = false)
@Transient
private BeanMap getMap()
{
if(map==null) {
map=BeanMap.create(this);
}
return map;
}
@Override
public Object get(String strName) {
if(this.getDEMethodDTORuntime() != null) {
if(isActionInputDTO()) {
IPSDEMethodDTOField iPSDEMethodDTOField = this.getDEMethodDTORuntime().getPSDEMethodDTOField(strName, true);
if(iPSDEMethodDTOField == null) {
if(this.isEnableAny()) {
return getObject(strName.toLowerCase());
}
return null;
}
return getObject(iPSDEMethodDTOField.getLowerCaseName());
}
else{
IPSDEMethodDTOField iPSDEMethodDTOField = this.getDEMethodDTORuntime().getPSDEMethodDTOFieldByDEField(strName, true);
if(iPSDEMethodDTOField == null) {
if(this.isEnableAny()) {
return getObject(strName.toLowerCase());
}
return null;
}
return getObject(iPSDEMethodDTOField.getLowerCaseName());
}
}
else {
return getObject(strName);
}
}
public Object getObject(String strName) {
String fieldRealName= FieldCache.getFieldRealName(this.getClass(),strName);
if(!StringUtils.isEmpty(fieldRealName)) {
return getMap().get(fieldRealName);
}
else {
return this.paramMap.get(strName);
}
}
public boolean contains(String strName) {
if(this.getDEMethodDTORuntime() != null) {
if(isActionInputDTO()) {
IPSDEMethodDTOField iPSDEMethodDTOField = this.getDEMethodDTORuntime().getPSDEMethodDTOField(strName, true);
if(iPSDEMethodDTOField == null) {
if(this.isEnableAny()) {
return containsKey(strName.toLowerCase());
}
return false;
}
return containsKey(iPSDEMethodDTOField.getLowerCaseName());
}
else {
IPSDEMethodDTOField iPSDEMethodDTOField = this.getDEMethodDTORuntime().getPSDEMethodDTOFieldByDEField(strName, true);
if(iPSDEMethodDTOField == null) {
if(this.isEnableAny()) {
return containsKey(strName.toLowerCase());
}
return false;
}
return containsKey(iPSDEMethodDTOField.getLowerCaseName());
}
}
else {
return containsKey(strName);
}
}
public boolean containsKey(String strName) {
if(getFocusNull().contains(strName))
return true;
String fieldRealName= FieldCache.getFieldRealName(this.getClass(),strName);
if(!StringUtils.isEmpty(fieldRealName)) {
if(getFocusNull().contains(strName))
return true;
return getMap().get(fieldRealName)!=null;
}
else {
return this.paramMap.containsKey(strName);
}
}
public void set(String strName, String strValue) {
if(this.getDEMethodDTORuntime() != null) {
IPSDEMethodDTOField iPSDEMethodDTOField = null;
if(isActionInputDTO()) {
iPSDEMethodDTOField = this.getDEMethodDTORuntime().getPSDEMethodDTOField(strName, true);
}
else{
iPSDEMethodDTOField = this.getDEMethodDTORuntime().getPSDEMethodDTOFieldByDEField(strName, true);
}
if(iPSDEMethodDTOField != null) {
int nStdDataType = iPSDEMethodDTOField.getStdDataType();
if(nStdDataType != DataTypes.UNKNOWN && !DataTypeUtils.isStringDataType(nStdDataType)) {
Object objValue = null;
try {
objValue = this.getDEMethodDTORuntime().getSystemRuntime().convertValue(nStdDataType, strValue);
} catch (Exception ex) {
throw new DataEntityRuntimeException(this.getDEMethodDTORuntime().getDataEntityRuntime()
,String.format("转化数据[%1$s]至[%2$s]发生异常,%3$s", strValue, DataTypeUtils.getTypeName(nStdDataType), ex.getMessage())
,ex);
}
set(strName, objValue);
return ;
}
}
}
set(strName, (Object)strValue);
}
public void set(String strName, Object objValue) {
if(this.getDEMethodDTORuntime() != null) {
if(isActionInputDTO()) {
IPSDEMethodDTOField iPSDEMethodDTOField = this.getDEMethodDTORuntime().getPSDEMethodDTOField(strName, true);
if(iPSDEMethodDTOField == null) {
if(this.isEnableAny()) {
setObject(strName.toLowerCase(), objValue);
}
else {
log.warn(String.format("DTO对象[%1$s]不支持属性[%2$s],无法设置值", this.getDEMethodDTORuntime().getId(), strName));
}
}
else
setObject(iPSDEMethodDTOField.getLowerCaseName(), objValue);
}
else{
IPSDEMethodDTOField iPSDEMethodDTOField = this.getDEMethodDTORuntime().getPSDEMethodDTOFieldByDEField(strName, true);
if(iPSDEMethodDTOField == null) {
if(this.isEnableAny()) {
setObject(strName.toLowerCase(), objValue);
}
else {
log.warn(String.format("DTO对象[%1$s]不支持属性[%2$s],无法设置值", this.getDEMethodDTORuntime().getId(), strName));
}
}
else
setObject(iPSDEMethodDTOField.getLowerCaseName(), objValue);
}
}
else {
setObject(strName, objValue);
}
}
@JsonAnySetter
@JSONField(name = "_any", unwrapped = true, serialize = false, deserialize = true)
public void setObject(String field, Object value) {
String fieldRealName=FieldCache.getFieldRealName(this.getClass(),field);
if(!StringUtils.isEmpty(fieldRealName)) {
if (value == null) {
getMap().put(fieldRealName, null);
}
else {
getMap().put(fieldRealName, FieldCache.fieldValueOf(this.getClass(), fieldRealName, value));
}
}
else {
this.paramMap.put(field,value);
}
}
public void reset(String strName) {
if(this.getDEMethodDTORuntime() != null) {
if(isActionInputDTO()) {
IPSDEMethodDTOField iPSDEMethodDTOField = this.getDEMethodDTORuntime().getPSDEMethodDTOField(strName, true);
if(iPSDEMethodDTOField == null) {
if(this.isEnableAny()) {
resetObject(strName.toLowerCase());
}
else {
log.warn(String.format("DTO对象[%1$s]不支持属性[%2$s],无法重置值", this.getDEMethodDTORuntime().getId(), strName));
}
}
else
resetObject(iPSDEMethodDTOField.getLowerCaseName());
}
else {
IPSDEMethodDTOField iPSDEMethodDTOField = this.getDEMethodDTORuntime().getPSDEMethodDTOFieldByDEField(strName, true);
if(iPSDEMethodDTOField == null) {
if(this.isEnableAny()) {
resetObject(strName.toLowerCase());
}
else {
log.warn(String.format("DTO对象[%1$s]不支持属性[%2$s],无法重置值", this.getDEMethodDTORuntime().getId(), strName));
}
}
else
resetObject(iPSDEMethodDTOField.getLowerCaseName());
}
}
else {
resetObject(strName);
}
}
public void resetObject(String strName) {
String fieldRealName= FieldCache.getFieldRealName(this.getClass(),strName);
if(!StringUtils.isEmpty(fieldRealName)) {
getMap().put(fieldRealName,null);
focusNull.remove(fieldRealName);
}
else {
this.paramMap.remove(strName);
}
}
@JsonIgnore
@JSONField(serialize = false)
public void resetAll() {
getMap().keySet().forEach(key->{
if(!key.equals("map"))
map.put(key,null);
});
this.paramMap.clear();
this.focusNull.clear();
}
public void copyTo(IEntity iEntity) {
copyTo(iEntity, false);
}
@Override
public void copyTo(IEntity iEntity, boolean bDTOData) {
Assert.notNull(iEntity, "传入目标对象无效");
Map<String,Object> map=toMap();
if(!bDTOData && getDEMethodDTORuntime() != null) {
if(map == null || map.size() == 0) {
return ;
}
List<IPSDEMethodDTOField> psDEMethodDTOFieldList = getDEMethodDTORuntime().getPSDEMethodDTO().getPSDEMethodDTOFields();
if(psDEMethodDTOFieldList != null) {
for(IPSDEMethodDTOField iPSDEMethodDTOField : psDEMethodDTOFieldList) {
String strFieldName = iPSDEMethodDTOField.getLowerCaseName();
if(!map.containsKey(strFieldName)) {
continue;
}
Object objValue = map.remove(strFieldName);
if(iPSDEMethodDTOField.getPSDEField() != null) {
iEntity.set(iPSDEMethodDTOField.getPSDEField().getLowerCaseName(), objValue);
}
else {
iEntity.set(strFieldName, objValue);
}
}
}
}
for(java.util.Map.Entry<String, Object> entry : map.entrySet()) {
iEntity.set(entry.getKey(), entry.getValue());
}
}
public void copyTo(Map<String, Object> destMap) {
this.copyTo(destMap, false);
}
@Override
public void copyTo(Map<String, Object> destMap, boolean bDTOData) {
Assert.notNull(destMap, "传入目标对象无效");
Map<String,Object> map=toMap();
if(!bDTOData && getDEMethodDTORuntime() != null) {
if(map == null || map.size() == 0) {
return ;
}
List<IPSDEMethodDTOField> psDEMethodDTOFieldList = getDEMethodDTORuntime().getPSDEMethodDTO().getPSDEMethodDTOFields();
if(psDEMethodDTOFieldList != null) {
for(IPSDEMethodDTOField iPSDEMethodDTOField : psDEMethodDTOFieldList) {
String strFieldName = iPSDEMethodDTOField.getLowerCaseName();
if(!map.containsKey(strFieldName)) {
continue;
}
Object objValue = map.remove(strFieldName);
if(iPSDEMethodDTOField.getPSDEField() != null) {
destMap.put(iPSDEMethodDTOField.getPSDEField().getLowerCaseName(), objValue);
}
else {
destMap.put(strFieldName, objValue);
}
}
}
for(java.util.Map.Entry<String, Object> entry : map.entrySet()) {
destMap.put(entry.getKey(), entry.getValue());
}
}
else {
destMap.putAll(map);
}
}
@Override
public void reload(Object objData, boolean bDTOData, boolean bIn) throws Exception {
if(objData == null) {
this.resetAll();
return;
}
if(bDTOData) {
//DTO 数据为直接缓存
if (objData instanceof IEntityDTO) {
//需要判断DTO是否一致
IEntityDTO srcEntityDTO = (IEntityDTO)objData;
if(srcEntityDTO.getDEMethodDTORuntime()!=null && this.getDEMethodDTORuntime() !=null && this.getDEMethodDTORuntime().getId().equals(srcEntityDTO.getDEMethodDTORuntime().getId())) {
((IEntityDTO) objData).copyTo(this,true);
}
else {
//不一致,使用复制方式
this.resetAll();
if(getDEMethodDTORuntime()!=null) {
List<IPSDEMethodDTOField> psDEMethodDTOFieldList = getDEMethodDTORuntime().getPSDEMethodDTO().getPSDEMethodDTOFields();
if(psDEMethodDTOFieldList != null) {
for(IPSDEMethodDTOField iPSDEMethodDTOField : psDEMethodDTOFieldList) {
Object objValue = null;
IPSDEField iPSDEField = iPSDEMethodDTOField.getPSDEField();
if(iPSDEField != null){
if(srcEntityDTO.contains(iPSDEField.getLowerCaseName())) {
objValue = srcEntityDTO.get(iPSDEField.getLowerCaseName());
}
else {
continue;
}
}
else {
if(srcEntityDTO.contains(iPSDEMethodDTOField.getLowerCaseName())) {
objValue = srcEntityDTO.get(iPSDEMethodDTOField.getLowerCaseName());
}
else {
continue;
}
}
setObject(iPSDEMethodDTOField.getLowerCaseName(), objValue);
}
}
//复制非DTO属性
if(srcEntityDTO.any()!=null) {
for(java.util.Map.Entry<String ,Object> entry:srcEntityDTO.any().entrySet()) {
String strName = entry.getKey().toLowerCase();
if(!this.containsKey(strName)) {
setObject(strName, entry.getValue());
}
}
}
}
else {
((IEntityDTO) objData).copyTo(this,true);
}
}
}
else {
Map<String,Object> srcmap = null;
if (objData instanceof Map) {
srcmap = (Map<String,Object>)objData;
}
else
if (objData instanceof IEntity) {
srcmap = ((IEntity)objData).any();
}
else {
throw new Exception("无法识别的数据对象");
}
Map<String, Object> map = new HashMap<>();
map.putAll(srcmap);
//对成员数据进行转化
if(getDEMethodDTORuntime() != null) {
List<IPSDEMethodDTOField> psDEMethodDTOFieldList = getDEMethodDTORuntime().getPSDEMethodDTO().getPSDEMethodDTOFields();
if(psDEMethodDTOFieldList != null) {
for(IPSDEMethodDTOField iPSDEMethodDTOField : psDEMethodDTOFieldList) {
Object dtoData = map.get(iPSDEMethodDTOField.getLowerCaseName());
if(DEMethodDTOFieldTypes.DTO.equals(iPSDEMethodDTOField.getType())
||DEMethodDTOFieldTypes.DTOS.equals(iPSDEMethodDTOField.getType())) {
if(dtoData != null) {
IDataEntityRuntime refDataEntityRuntime = this.getDEMethodDTORuntime().getSystemRuntime().getDataEntityRuntime(iPSDEMethodDTOField.getRefPSDataEntityMust().getId());
if(DEMethodDTOFieldTypes.DTOS.equals(iPSDEMethodDTOField.getType())) {
//列表模式
if(!(dtoData instanceof List)) {
throw new DataEntityRuntimeException(this.getDEMethodDTORuntime().getDataEntityRuntime(), this.getDEMethodDTORuntime(), String.format("属性[%1$s]传入数据类型不正确", iPSDEMethodDTOField.getName()));
}
List list = (List)dtoData;
List dtoList= new ArrayList();
for(Object item : list) {
IDEMethodDTO iDEMethodDTO = refDataEntityRuntime.getDEMethodDTO(iPSDEMethodDTOField.getRefPSDEMethodDTOMust(), item);
dtoList.add(iDEMethodDTO);
}
map.put(iPSDEMethodDTOField.getLowerCaseName(), dtoList);
}
else {
IDEMethodDTO iDEMethodDTO = refDataEntityRuntime.getDEMethodDTO(iPSDEMethodDTOField.getRefPSDEMethodDTOMust(), dtoData);
map.put(iPSDEMethodDTOField.getLowerCaseName(), iDEMethodDTO);
}
}
continue;
}
else {
if(dtoData != null && dtoData instanceof String) {
int nStdDataType = iPSDEMethodDTOField.getStdDataType();
if(nStdDataType != DataTypes.UNKNOWN && !DataTypeUtils.isStringDataType(nStdDataType)) {
Object objValue = null;
try {
objValue = this.getDEMethodDTORuntime().getSystemRuntime().convertValue(nStdDataType, (String)dtoData);
} catch (Exception ex) {
throw new DataEntityRuntimeException(this.getDEMethodDTORuntime().getDataEntityRuntime()
,String.format("转化数据[%1$s]至[%2$s]发生异常,%3$s", dtoData, DataTypeUtils.getTypeName(nStdDataType), ex.getMessage())
,ex);
}
map.put(iPSDEMethodDTOField.getLowerCaseName(), objValue);
}
}
continue;
}
}
}
}
setAny(map);
}
}
else {
this.resetAll();
//需要进行转化
Map<String,Object> srcmap = null;
if (objData instanceof Map) {
srcmap = (Map<String,Object>)objData;
}
else
if (objData instanceof IEntity) {
srcmap = ((IEntity)objData).any();
}
else {
throw new Exception("无法识别的数据对象");
}
Map<String, Object> map = new HashMap<>();
map.putAll(srcmap);
if (map!=null) {
if(getDEMethodDTORuntime()!=null) {
List<IPSDEMethodDTOField> psDEMethodDTOFieldList = getDEMethodDTORuntime().getPSDEMethodDTO().getPSDEMethodDTOFields();
if(psDEMethodDTOFieldList != null) {
for(IPSDEMethodDTOField iPSDEMethodDTOField : psDEMethodDTOFieldList) {
Object objValue = null;
IPSDEField iPSDEField = iPSDEMethodDTOField.getPSDEField();
if(iPSDEField != null){
if(map.containsKey(iPSDEField.getLowerCaseName())) {
objValue = map.remove(iPSDEField.getLowerCaseName());
}
else
if(map.containsKey(iPSDEField.getName())) {
objValue = map.remove(iPSDEField.getName());
}
else {
continue;
}
}
else {
if(map.containsKey(iPSDEMethodDTOField.getLowerCaseName())) {
objValue = map.remove(iPSDEMethodDTOField.getLowerCaseName());
}
else {
continue;
}
}
setObject(iPSDEMethodDTOField.getLowerCaseName(), objValue);
}
}
//复制非DTO属性
for(java.util.Map.Entry<String ,Object> entry:map.entrySet()) {
String strName = entry.getKey().toLowerCase();
if(!containsKey(strName)) {
setObject(strName, entry.getValue());
}
}
}
else {
setAny(map);
}
}
}
if(bIn) {
this.resetReadOnlyFields();
}
}
@Override
public void reload(Object objData, boolean bDTOData) throws Exception {
reload(objData, bDTOData, false);
}
@JsonIgnore
@JSONField(serialize = false)
@Override
public void resetReadOnlyFields() {
if(getDEMethodDTORuntime()!=null) {
List<IPSDEMethodDTOField> psDEMethodDTOFieldList = getDEMethodDTORuntime().getPSDEMethodDTO().getPSDEMethodDTOFields();
if(psDEMethodDTOFieldList != null) {
for(IPSDEMethodDTOField iPSDEMethodDTOField : psDEMethodDTOFieldList) {
if(iPSDEMethodDTOField.isReadOnly()) {
resetObject(iPSDEMethodDTOField.getLowerCaseName());
}
}
}
}
}
@JsonIgnore
protected void setAny(Map<String, Object> map){
this.setAny(map, false);
}
@JsonIgnore
protected void setAny(Map<String, Object> map, boolean bCopyMode){
{
for(java.util.Map.Entry<String, Object> entry : map.entrySet()) {
this.setObject(entry.getKey(), entry.getValue());
}
}
}
public IEntityDTO put(String strName, Object objValue) {
this.set(strName,objValue);
return this;
}
@Override
public IEntityDTO put(String strName, String strValue) {
this.set(strName,strValue);
return this;
}
public void putAll(Map<String, Object> map) {
for(java.util.Map.Entry<String, Object> entry : map.entrySet()) {
this.set(entry.getKey(), entry.getValue());
}
}
@JsonIgnore
@JSONField(serialize = false)
public Map<String, Object> toMap()
{
Map<String, Object> map = new HashMap<>();
map.putAll(this.paramMap);
map.putAll(this.getMap());
focusNull.forEach(item->map.put(item,null));
return map;
}
@Override
public Timestamp getTimestamp(String strName, Timestamp def) {
try {
return get(strName,Timestamp.class,def);
}
catch (Exception ex) {
throw new RuntimeException(ex);
}
}
@Override
public BigDecimal getBigDecimal(String strName, BigDecimal fDefault) {
try {
return get(strName,BigDecimal.class,fDefault);
}
catch (Exception ex) {
throw new RuntimeException(ex);
}
}
@Override
public Integer getInteger(String strName, Integer def) {
try {
return get(strName,Integer.class,def);
}
catch (Exception ex) {
throw new RuntimeException(ex);
}
}
@Override
public BigInteger getBigInteger(String strName, BigInteger nDefault) {
try {
return get(strName,BigInteger.class,nDefault);
}
catch (Exception ex) {
throw new RuntimeException(ex);
}
}
@Override
public Boolean getBoolean(String strName, Boolean def) {
try {
return get(strName,Boolean.class,def);
}
catch (Exception ex) {
throw new RuntimeException(ex);
}
}
@Override
public Double getDouble(String strName, Double def) {
try {
return get(strName,Double.class,def);
}
catch (Exception ex) {
throw new RuntimeException(ex);
}
}
@Override
public Float getFloat(String strName, Float def) {
try {
return get(strName,Float.class,def);
}
catch (Exception ex) {
throw new RuntimeException(ex);
}
}
@Override
public Long getLong(String strName, Long def) {
try {
return get(strName,Long.class,def);
}
catch (Exception ex) {
throw new RuntimeException(ex);
}
}
@Override
public String getString(String strName, String strDefault) {
try {
String str = get(strName,String.class,"");
if(StringUtils.isEmpty(str))
return strDefault;
return str;
}
catch (Exception ex) {
throw new RuntimeException(ex);
}
}
public <V> V get(String strName,Class<V> clazz,Object defaultValue)
{
Object objValue=this.get(strName);
if(objValue==null) return defaultValue==null?null:(V)defaultValue;
return TypeUtils.castToJavaBean(objValue,clazz);
}
public static <V> V getValue(Object objValue,Class<V> clazz,Object defaultValue)
{
if(objValue==null) return defaultValue==null?null:(V)defaultValue;
return TypeUtils.castToJavaBean(objValue,clazz);
}
}
package net.ibizsys.central.plugin.boot.core.domain;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import lombok.AllArgsConstructor;
import lombok.Data;
@Data
@JsonInclude(Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
@AllArgsConstructor
public class FileItem
{
private String id;
private String name;
private String fileid;
private String filename;
private long size;
private String ext;
}
package net.ibizsys.central.plugin.boot.core.errors;
import org.zalando.problem.AbstractThrowableProblem;
import org.zalando.problem.Status;
import java.net.URI;
import java.util.HashMap;
import java.util.Map;
public class BadRequestAlertException extends AbstractThrowableProblem {
private final String entityName;
private final String errorKey;
public BadRequestAlertException(String defaultMessage, String entityName, String errorKey) {
this(ErrorConstants.DEFAULT_TYPE, defaultMessage, entityName, errorKey);
}
public BadRequestAlertException(URI type, String defaultMessage, String entityName, String errorKey) {
super(type, defaultMessage, Status.BAD_REQUEST, null, null, null, getAlertParameters(entityName, errorKey));
this.entityName = entityName;
this.errorKey = errorKey;
}
public String getEntityName() {
return entityName;
}
public String getErrorKey() {
return errorKey;
}
private static Map<String, Object> getAlertParameters(String entityName, String errorKey) {
Map<String, Object> parameters = new HashMap<>();
parameters.put("message", "error." + errorKey);
parameters.put("params", entityName);
return parameters;
}
}
package net.ibizsys.central.plugin.boot.core.errors;
import org.zalando.problem.AbstractThrowableProblem;
import java.util.HashMap;
import java.util.Map;
import static org.zalando.problem.Status.BAD_REQUEST;
public class CustomParameterizedException extends AbstractThrowableProblem {
private static final long serialVersionUID = 1L;
private static final String PARAM = "param";
public CustomParameterizedException(String message, String... params) {
this(message, toParamMap(params));
}
public CustomParameterizedException(String message, Map<String, Object> paramMap) {
super(ErrorConstants.PARAMETERIZED_TYPE, "处理发生异常", BAD_REQUEST, null, null, null, toProblemParameters(message, paramMap));
}
public static Map<String, Object> toParamMap(String... params) {
Map<String, Object> paramMap = new HashMap<>();
if (params != null && params.length > 0) {
for (int i = 0; i < params.length; i++) {
paramMap.put(PARAM + i, params[i]);
}
}
return paramMap;
}
public static Map<String, Object> toProblemParameters(String message, Map<String, Object> paramMap) {
Map<String, Object> parameters = new HashMap<>();
parameters.put("message", message);
parameters.put("params", paramMap);
return parameters;
}
}
package net.ibizsys.central.plugin.boot.core.errors;
import java.net.URI;
public final class ErrorConstants {
public static final String ERR_CONCURRENCY_FAILURE = "处理请求发生错误";
public static final String ERR_VALIDATION = "数据校验发生错误";
public static final String PROBLEM_BASE_URL = "";
public static final URI DEFAULT_TYPE = URI.create(PROBLEM_BASE_URL + "/problem-with-message");
public static final URI CONSTRAINT_VIOLATION_TYPE = URI.create(PROBLEM_BASE_URL + "/constraint-violation");
public static final URI PARAMETERIZED_TYPE = URI.create(PROBLEM_BASE_URL + "/parameterized");
private ErrorConstants() {
}
}
package net.ibizsys.central.plugin.boot.core.errors;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.dao.ConcurrencyFailureException;
import org.springframework.dao.DataAccessException;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.multipart.MaxUploadSizeExceededException;
import org.zalando.problem.DefaultProblem;
import org.zalando.problem.Problem;
import org.zalando.problem.ProblemBuilder;
import org.zalando.problem.Status;
import org.zalando.problem.spring.web.advice.ProblemHandling;
import org.zalando.problem.spring.web.advice.validation.ConstraintViolationProblem;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;
@ControllerAdvice
public class ExceptionTranslator implements ProblemHandling {
@Override
public ResponseEntity<Problem> process(@Nullable ResponseEntity<Problem> entity, NativeWebRequest request) {
if (entity == null || entity.getBody() == null) {
return entity;
}
Problem problem = entity.getBody();
if (!(problem instanceof ConstraintViolationProblem || problem instanceof DefaultProblem)) {
return entity;
}
ProblemBuilder builder = Problem.builder()
.withType(Problem.DEFAULT_TYPE.equals(problem.getType()) ? ErrorConstants.DEFAULT_TYPE : problem.getType())
.withStatus(problem.getStatus())
.withTitle(problem.getTitle())
.with("path", request.getNativeRequest(HttpServletRequest.class).getRequestURI());
if (problem instanceof ConstraintViolationProblem) {
builder
.with("violations", ((ConstraintViolationProblem) problem).getViolations())
.with("message", ErrorConstants.ERR_VALIDATION);
return new ResponseEntity<>(builder.build(), entity.getHeaders(), entity.getStatusCode());
} else {
builder
.withCause(((DefaultProblem) problem).getCause())
.withDetail(problem.getDetail())
.withInstance(problem.getInstance());
problem.getParameters().forEach(builder::with);
if (!problem.getParameters().containsKey("message") && problem.getStatus() != null) {
builder.with("message", "error.http." + problem.getStatus().getStatusCode());
}
return new ResponseEntity<>(builder.build(), entity.getHeaders(), entity.getStatusCode());
}
}
@Override
public ResponseEntity<Problem> handleMethodArgumentNotValid(MethodArgumentNotValidException ex, @Nonnull NativeWebRequest request) {
BindingResult result = ex.getBindingResult();
List<FieldErrorVM> fieldErrors = result.getFieldErrors().stream()
.map(f -> new FieldErrorVM(f.getObjectName(), f.getField(), f.getDefaultMessage()))
.collect(Collectors.toList());
Problem problem = Problem.builder()
.withType(ErrorConstants.CONSTRAINT_VIOLATION_TYPE)
.withTitle(ErrorConstants.ERR_VALIDATION)
.withStatus(defaultConstraintViolationStatus())
.with("message", ErrorConstants.ERR_VALIDATION )
.with("fieldErrors", fieldErrors)
.build();
return create(ex, problem, request);
}
@ExceptionHandler(BadRequestAlertException.class)
public ResponseEntity<Problem> handleBadRequestAlertException(BadRequestAlertException ex, NativeWebRequest request) {
return create(ex, request, createFailureAlert(ex.getEntityName(), ex.getErrorKey(), ex.getMessage()));
}
@ExceptionHandler(ConcurrencyFailureException.class)
public ResponseEntity<Problem> handleConcurrencyFailure(ConcurrencyFailureException ex, NativeWebRequest request) {
Problem problem = Problem.builder()
.withStatus(Status.CONFLICT)
.with("message", ErrorConstants.ERR_CONCURRENCY_FAILURE)
.build();
return create(ex, problem, request);
}
@ExceptionHandler(DataAccessException.class)
public ResponseEntity<Problem> handlerDataAccessFailure(DataAccessException ex, NativeWebRequest request){
Problem problem = Problem.builder()
.withStatus(Status.INTERNAL_SERVER_ERROR)
.with("message", ex.getMessage())
.build();
return create(ex, problem, request,createFailureAlert(ex.getClass().getSimpleName(), ex.getClass().getSimpleName(), ex.getMessage()));
}
public static HttpHeaders createFailureAlert(String entityName, String errorKey, String defaultMessage) {
HttpHeaders headers = new HttpHeaders();
headers.add("X-ibz-error", errorKey);
headers.add("X-ibz-params", entityName);
return headers;
}
/**
* 上传文件大小超出限制异常
*/
@Value("${spring.servlet.multipart.max-file-size}")
private String maxFileSize;
@ExceptionHandler(MaxUploadSizeExceededException.class)
public ResponseEntity<Problem> handlerMaxUploadFile(MaxUploadSizeExceededException ex, NativeWebRequest request){
Problem problem = Problem.builder()
.withStatus(Status.BAD_REQUEST)
.withDetail("上传文件不能大于"+maxFileSize)
.with("message", "上传文件不能大于"+maxFileSize)
.with("exmessage",""+ex.getMessage())
.build();
return create(ex, problem, request);
}
@ExceptionHandler(Exception.class)
public ResponseEntity<Problem> handlerTest(Exception ex, NativeWebRequest request){
Problem problem = Problem.builder()
.withStatus(Status.INTERNAL_SERVER_ERROR)
.withDetail("内部服务器异常")
.with("message", "内部服务器异常")
.with("exmessage",""+ex.getMessage())
.build();
return create(ex, problem, request);
}
}
package net.ibizsys.central.plugin.boot.core.errors;
import java.io.Serializable;
public class FieldErrorVM implements Serializable {
private static final long serialVersionUID = 1L;
private final String objectName;
private final String field;
private final String message;
public FieldErrorVM(String dto, String field, String message) {
this.objectName = dto;
this.field = field;
this.message = message;
}
public String getObjectName() {
return objectName;
}
public String getField() {
return field;
}
public String getMessage() {
return message;
}
}
package net.ibizsys.central.plugin.boot.core.errors;
import org.zalando.problem.AbstractThrowableProblem;
import org.zalando.problem.Status;
public class InternalServerErrorException extends AbstractThrowableProblem {
public InternalServerErrorException(String message) {
super(ErrorConstants.DEFAULT_TYPE, message, Status.INTERNAL_SERVER_ERROR);
}
}
package net.ibizsys.central.plugin.boot.core.filter;
import com.alibaba.fastjson.annotation.JSONField;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import net.ibizsys.central.dataentity.ds.IDEFilterDTORuntime;
import net.ibizsys.central.dataentity.service.IDEMethodDTORuntime;
import net.ibizsys.central.plugin.boot.core.domain.BaseData;
import net.ibizsys.central.plugin.boot.core.helper.FieldCache;
import net.ibizsys.central.plugin.boot.core.helper.QueryHelper;
import net.ibizsys.central.util.ISearchContextDTO;
import net.ibizsys.central.util.PageRequest;
import net.ibizsys.central.util.SearchContextDTO;
import net.ibizsys.model.dataentity.defield.IPSDEField;
import net.ibizsys.model.dataentity.ds.IPSDEDataSet;
import net.ibizsys.model.dataentity.ds.IPSDEFilterDTOField;
import net.ibizsys.runtime.util.*;
import org.springframework.cglib.beans.BeanMap;
import org.springframework.data.annotation.Transient;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Slf4j
public class BaseFilter<T> extends BaseData implements ISearchContextDTO {
@JsonIgnore
@JSONField(serialize = false)
private transient IDEFilterDTORuntime iDEFilterDTORuntime = null;
@JsonIgnore
@JSONField(serialize = false)
private Pageable pageable = null;
public void setPageable(Pageable pageable){
this.pageable=pageable;
}
@JsonIgnore
@JSONField(serialize = false)
private Sort pageSort = null;
@Override
public void init(IDEMethodDTORuntime iDEMethodDTORuntime, Object objData, boolean bDTOData) throws Exception {
if(!(iDEMethodDTORuntime instanceof IDEFilterDTORuntime)) {
throw new Exception(String.format("传入实体方法DTO运行时对象不正确"));
}
if(!bDTOData) {
throw new Exception(String.format("仅支持传入DTO"));
}
this.iDEFilterDTORuntime = (IDEFilterDTORuntime)iDEMethodDTORuntime;
if (objData instanceof Map) {
Map<String,Object> map = (Map<String,Object>)objData;
this.setAny(map, true);
}
onInit();
}
protected void onInit() throws Exception {
if (sort !=null) {
String strSortInfo = (String) sort;
strSortInfo = strSortInfo.trim();
this.setPageSort(strSortInfo);
}
this.setPageable(PageRequest.of(page, size, offset));
Object objSearchConds = this.getSearchConds();
if(objSearchConds == null) {
if(this.getDEMethodDTORuntime()!=null) {
//计算查询条件
java.util.List<IPSDEFilterDTOField> psDEFilterDTOFieldList = this.getDEMethodDTORuntime().getPSDEFilterDTO().getPSDEFilterDTOFields();
if(psDEFilterDTOFieldList != null) {
for(IPSDEFilterDTOField iPSDEFilterDTOField : psDEFilterDTOFieldList) {
Object objValue = this.get(iPSDEFilterDTOField.getLowerCaseName());
if(ObjectUtils.isEmpty(objValue)) {
continue;
}
SearchFieldCond searchFieldCond = new SearchFieldCond();
searchFieldCond.setFieldName(iPSDEFilterDTOField.getPSDEFieldMust().getName());
searchFieldCond.setParamMode(true);
searchFieldCond.setCondOp(iPSDEFilterDTOField.getPSDEFSearchModeMust().getValueOP());
searchFieldCond.setValue(iPSDEFilterDTOField.getLowerCaseName());
searchFieldCond.setDataType(iPSDEFilterDTOField.getStdDataType());
searchFieldCond.setValueFunc(iPSDEFilterDTOField.getPSDEFSearchModeMust().getValueFunc());
this.getSearchCondsIf().add(searchFieldCond);
}
}
if(!ObjectUtils.isEmpty(query)) {
//获取所有支持快速搜索的属性
List<IPSDEField> psDEFieldList = this.getDEMethodDTORuntime().getDataEntityRuntime().getQuickSearchPSDEFields();
if(psDEFieldList != null && psDEFieldList.size() > 0) {
SearchGroupCond searchGroupCond = new SearchGroupCond();
searchGroupCond.setCondOp(Conditions.OR);
for(IPSDEField iPSDEField: psDEFieldList ) {
SearchFieldCond searchFieldCond = new SearchFieldCond();
searchFieldCond.setFieldName(iPSDEField.getName());
searchFieldCond.setParamMode(true);
searchFieldCond.setCondOp(Conditions.LIKE);
searchFieldCond.setValue(PARAM_QUERY);
searchFieldCond.setDataType(iPSDEField.getStdDataType());
searchGroupCond.getSearchCondsIf().add(searchFieldCond);
}
this.getSearchCondsIf().add(searchGroupCond);
}
}
}
Object predefinedcond = this.get(PARAM_PREDEFINEDCOND);
if(predefinedcond instanceof String) {
if(!ObjectUtils.isEmpty(predefinedcond)) {
SearchContextDTO.addSearchPredefinedCond(this, (String)predefinedcond, null);
}
}
}
else {
//实际展开条件
if(objSearchConds instanceof List) {
List list = (List)objSearchConds;
for(Object objItem : list) {
if(!(objItem instanceof Map)) {
continue;
}
ISearchCond iSearchCond = net.ibizsys.central.util.SearchGroupCond.getSearchCond((Map)objItem, true);
if(iSearchCond != null) {
this.getSearchCondsIf().add(iSearchCond);
}
}
}
}
}
@Override
@JsonIgnore
@JSONField(serialize = false)
public IDEFilterDTORuntime getDEMethodDTORuntime() {
return this.iDEFilterDTORuntime;
}
@Override
public void setDEMethodDTORuntime(IDEFilterDTORuntime iDEFilterDTORuntime) {
this.iDEFilterDTORuntime = iDEFilterDTORuntime;
}
@JsonIgnore
@JSONField(serialize = false)
private List<ISearchCond> searchconds = null;
@JsonIgnore
@JSONField(serialize = false)
public List<ISearchCond> getSearchCondsIf() {
if(this.searchconds == null) {
this.searchconds = new ArrayList<ISearchCond>();
}
return this.searchconds;
}
@Override
public BaseFilter setFieldCond(String strFieldName, String strCondOp, Object objValue) {
SearchFieldCond searchFieldCond = new SearchFieldCond();
searchFieldCond.setFieldName(strFieldName);
searchFieldCond.setCondOp(strCondOp);
if(!Conditions.ISNULL.equals(strCondOp)
&& !Conditions.ISNOTNULL.equals(strCondOp)) {
searchFieldCond.setParamMode(true);
String strParamName = "";
int nIndex = 1;
while(true) {
if(nIndex == 1) {
strParamName = String.format("n_%1$s_%2$s", strFieldName, strCondOp).toLowerCase();
}
else {
strParamName = String.format("n_%1$s_%2$s_%3$s", strFieldName, strCondOp, nIndex).toLowerCase();
}
nIndex ++;
if(!contains(strParamName)) {
searchFieldCond.setValue(strParamName);
this.getParamMap().put(strParamName, objValue);
break;
}
}
//
// String strParamName = String.format("n_%1$s_%2$s", strFieldName, strCondOp).toLowerCase();
// searchFieldCond.setValue(strParamName);
// this.getParamMap().put(strParamName, objValue);
}
this.getSearchCondsIf().add(searchFieldCond);
return this;
}
/**
* 指定属性值等于条件值
* @param strFieldName
*/
@Override
public BaseFilter eq(String strFieldName, Object objValue) {
setFieldCond(strFieldName, Conditions.EQ, objValue);
return this;
}
/**
* 指定属性值不等于条件值
* @param strFieldName
*/
@Override
public BaseFilter ne(String strFieldName, Object objValue) {
setFieldCond(strFieldName, Conditions.NOTEQ, objValue);
return this;
}
/**
* 指定属性值大于条件值
* @param strFieldName
*/
@Override
public BaseFilter gt(String strFieldName, Object objValue) {
setFieldCond(strFieldName, Conditions.GT, objValue);
return this;
}
/**
* 指定属性值大于等于条件值
* @param strFieldName
*/
@Override
public BaseFilter gte(String strFieldName, Object objValue) {
setFieldCond(strFieldName, Conditions.GTANDEQ, objValue);
return this;
}
/**
* 指定属性值小于条件值
* @param strFieldName
*/
@Override
public BaseFilter lt(String strFieldName, Object objValue) {
setFieldCond(strFieldName, Conditions.LT, objValue);
return this;
}
/**
* 指定属性值小于等于条件值
* @param strFieldName
*/
@Override
public BaseFilter lte(String strFieldName, Object objValue) {
setFieldCond(strFieldName, Conditions.LTANDEQ, objValue);
return this;
}
/**
* 指定属性值匹配条件值
* @param strFieldName
*/
@Override
public BaseFilter like(String strFieldName, Object objValue) {
setFieldCond(strFieldName, Conditions.LIKE, objValue);
return this;
}
/**
* 指定属性值左侧匹配条件值
* @param strFieldName
*/
@Override
public BaseFilter ll(String strFieldName, Object objValue) {
setFieldCond(strFieldName, Conditions.LEFTLIKE, objValue);
return this;
}
/**
* 指定属性值右侧匹配条件值
* @param strFieldName
*/
@Override
public BaseFilter rl(String strFieldName, Object objValue) {
setFieldCond(strFieldName, Conditions.RIGHTLIKE, objValue);
return this;
}
/**
* 指定属性值为空
* @param strFieldName
*/
@Override
public BaseFilter nvl(String strFieldName) {
setFieldCond(strFieldName, Conditions.ISNULL, null);
return this;
}
/**
* 指定属性值不为空
* @param strFieldName
*/
@Override
public BaseFilter nn(String strFieldName) {
setFieldCond(strFieldName, Conditions.ISNOTNULL, null);
return this;
}
/**
* 指定属性值在值范围中
* @param strFieldName
*/
@Override
public BaseFilter in(String strFieldName, Object objValue) {
setFieldCond(strFieldName, Conditions.IN, objValue);
return this;
}
/**
* 指定属性值不在值范围中
* @param strFieldName
*/
@Override
public BaseFilter ni(String strFieldName, Object objValue) {
setFieldCond(strFieldName, Conditions.NOTIN, objValue);
return this;
}
/**
* 添加自定义条件
* @param strCustomCond
*/
@Override
public BaseFilter custom(String strCustomCond) {
SearchCustomCond searchCustomCond = new SearchCustomCond();
searchCustomCond.setCustomCond(strCustomCond);
this.getSearchCondsIf().add(searchCustomCond);
return this;
}
/**
* 添加预定义条件
*/
@Override
public BaseFilter predefined(String strPredefinedCond, String strPredefinedType) {
SearchPredefinedCond searchPredefinedCond = new SearchPredefinedCond();
searchPredefinedCond.setPredefinedCond(strPredefinedCond);
searchPredefinedCond.setPredefinedType(strPredefinedType);
this.getSearchCondsIf().add(searchPredefinedCond);
return this;
}
@Override
@JsonIgnore
@JSONField(serialize = false)
public net.ibizsys.central.util.SearchGroupCond and() {
net.ibizsys.central.util.SearchGroupCond searchGroupCond = new net.ibizsys.central.util.SearchGroupCond(this);
searchGroupCond.setCondOp(Conditions.AND);
this.getSearchCondsIf().add(searchGroupCond);
return searchGroupCond;
}
@Override
@JsonIgnore
@JSONField(serialize = false)
public net.ibizsys.central.util.SearchGroupCond or() {
net.ibizsys.central.util.SearchGroupCond searchGroupCond = new net.ibizsys.central.util.SearchGroupCond(this);
searchGroupCond.setCondOp(Conditions.OR);
this.getSearchCondsIf().add(searchGroupCond);
return searchGroupCond;
}
@Override
public BaseFilter sort(String strSortInfo) {
this.setPageSort(strSortInfo);
return this;
}
@Override
@JsonIgnore
@JSONField(serialize = false)
public BaseFilter all() {
this.setCount(false);
return this.limit(Integer.MAX_VALUE);
}
@Override
public BaseFilter limit(int nSize) {
return limit(0, nSize);
}
@Override
public BaseFilter limit(int nStart, int nSize) {
this.setPageable(STARTPAGE, nSize, nStart);
return this;
}
/**
* 设置直接变量值
* @param strName
* @param objValue
* @return
*/
@Override
public BaseFilter param(String strName, Object objValue) {
this.set(strName, objValue);
return this;
}
@Override
@JsonIgnore
@JSONField(serialize = false)
public String getCondOp() {
return Conditions.AND;
}
@Override
@JsonIgnore
@JSONField(serialize = false)
public boolean isNotMode() {
return false;
}
@Override
@JsonIgnore
@JSONField(serialize = false)
public String getCondType() {
return CONDTYPE_GROUP;
}
public String query;
@Override
@JsonIgnore
@JSONField(serialize = false)
public String getDrillDownCond() {
Object drilldowncond = this.get(PARAM_DRILLDOWNCOND);
if(drilldowncond instanceof String) {
return (String)drilldowncond;
}
return null;
}
@Override
@JsonIgnore
@JSONField(serialize = false)
public String getGroupCond() {
Object groupcond = this.get(PARAM_GROUPCOND);
if(groupcond instanceof String) {
return (String)groupcond;
}
return null;
}
public Integer size = 20;
public Integer page = 0;
public Long offset = 0L;
public String sort ;
public void setCount(boolean count)
{
this.setObject("count",count);
}
@JsonIgnore
@JSONField(serialize = false)
public boolean getCount(){return this.getBoolean("count",true).booleanValue();}
@JsonIgnore
@JSONField(serialize = false)
public boolean isCount(){return this.getBoolean("count",true).booleanValue();}
public void setSort(String sort)
{
if(!StringUtils.isEmpty(sort))
this.setPageSort(sort);
this.sort=sort;
}
public String getSort() {
if(this.getPageSort() != null && this.getPageSort() != Sort.unsorted()) {
java.util.Iterator<Sort.Order> orders = pageSort.iterator();
if(orders != null) {
String strSortInfo = "";
while (orders.hasNext()) {
if(StringUtils.hasLength(strSortInfo)) {
strSortInfo += ";";
}
Sort.Order order = orders.next();
if(order.isAscending()) {
strSortInfo += String.format("%1$s,asc", order.getProperty());
}
else {
strSortInfo += String.format("%1$s,desc", order.getProperty());
}
}
return strSortInfo;
}
}
return sort;
}
public long getOffset() {
if(this.getPageable() != null) {
return this.getPageable().getOffset();
}
return offset;
}
public int getSize() {
if(this.getPageable() != null) {
return this.getPageable().getPageSize();
}
return size;
}
public int getPage() {
if(this.getPageable() != null) {
return this.getPageable().getPageNumber();
}
return page;
}
@Override
public BaseFilter count(boolean bCount) {
this.setObject("count",bCount);
return this;
}
public void setPageable(int nPageNumber, int nPageSize, long nOffset) {
this.setPageable(PageRequest.of(nPageNumber, nPageSize, nOffset));
}
@Override
@JsonIgnore
@JSONField(serialize = false)
public List<ISearchCond> getSearchConds() {
return searchconds;
}
public void setPageSort(String strSortInfo) {
Sort sort = null;
if (StringUtils.hasLength(strSortInfo)) {
List<Sort.Order> list = new ArrayList<Sort.Order>();
String[] parts = strSortInfo.split("[;]");
for(String strPart : parts) {
String[] items = strPart.split("[,]");
if (items.length == 1) {
list.add(Sort.Order.asc(items[0]));
} else if (items.length == 2) {
if ("DESC".equalsIgnoreCase(items[1])) {
list.add(Sort.Order.desc(items[0]));
} else {
list.add(Sort.Order.asc(items[0]));
}
}
}
if (list.size() > 0) {
sort = Sort.by(list);
}
}
this.setPageSort(sort);
}
@Override
public void setPageSort(Sort sort) {
this.pageSort=sort;
}
public Object get(String strName){
return getObject(strName);
}
@JsonIgnore
@JSONField(serialize = false)
@Transient
private BeanMap map;
@JsonIgnore
@JSONField(serialize = false)
@Transient
private BeanMap getMap()
{
if(map==null) {
map=BeanMap.create(this);
}
return map;
}
public Object getObject(String strName)
{
String fieldRealName= FieldCache.getFieldRealName(BaseFilter.class,strName);
if(StringUtils.isEmpty(fieldRealName)&&(!this.getClass().isAssignableFrom(BaseFilter.class))) {
fieldRealName=FieldCache.getFieldRealName(this.getClass(),strName);
}
if(!StringUtils.isEmpty(fieldRealName)) {
return getMap().get(fieldRealName);
}
else {
return this.getParamMap().get(strName);
}
}
public QueryWrapper<T> getSearchCond(boolean gen)
{
return QueryHelper.getQueryWrapper(this);
}
public void modify(String field,Object val) {
}
public void set(String field, String value) {
setObject(field,value);
}
public void set(String field, Object value) {
setObject(field,value);
}
@JsonAnySetter
@JSONField(name = "_any", unwrapped = true, serialize = false, deserialize = true)
public void setObject(String field, Object value) {
try
{
switch (field.toLowerCase())
{
case "size":
this.setSize(DataTypeUtils.getIntegerValue(value,20));
break;
case "page":
this.setPage(DataTypeUtils.getIntegerValue(value,0));
break;
case "offset":
this.setOffset(DataTypeUtils.getLongValue(value,0L));
break;
case "pagesort":
if(value instanceof Sort)
this.setPageSort((Sort)value);
else
this.setPageSort(DataTypeUtils.getStringValue(value,null));
break;
case "searchconds":
this.setSearchconds((List)value);
break;
case "query":
this.setQuery(DataTypeUtils.getStringValue(value,null));
break;
case "sort":
this.setSort(DataTypeUtils.getStringValue(value,null));
break;
case "idefilterdtoruntime":
if(value instanceof IDEFilterDTORuntime)
this.setIDEFilterDTORuntime((IDEFilterDTORuntime)value);
else
this.setIDEFilterDTORuntime(null);
break;
default:
String fieldRealName= FieldCache.getFieldRealName(this.getClass(),field);
if(!StringUtils.isEmpty(fieldRealName)) {
if (value == null) {
getMap().put(fieldRealName, null);
}
else {
getMap().put(fieldRealName, FieldCache.fieldValueOf(this.getClass(), fieldRealName, value));
}
}
else {
this.getParamMap().put(field,value);
}
}
}catch (Exception ex){}
}
@JsonIgnore
@JSONField(serialize = false)
private IPSDEDataSet dataSet;
}
package net.ibizsys.central.plugin.boot.core.helper;
import org.springframework.beans.BeanUtils;
public class CachedBeanCopier {
public static void copy(Object srcObj, Object destObj) {
copy(srcObj,destObj,false);
}
public static void copy(Object srcObj, Object destObj,boolean useConverter) {
if(srcObj==null||destObj==null) {
return;
}
BeanUtils.copyProperties(srcObj, destObj);
}
}
package net.ibizsys.central.plugin.boot.core.helper;
import com.alibaba.fastjson.annotation.JSONField;
import com.alibaba.fastjson.util.TypeUtils;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.experimental.Accessors;
import net.ibizsys.central.plugin.boot.core.filter.BaseFilter;
import org.springframework.data.annotation.Id;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
/**
* 实体对象属性缓存类
*/
public class FieldCache {
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public static class FieldItem{
private String codeName;
private String fieldName;
private String columnName;
private String jsonName;
private String logicName;
private Field field;
}
private static Hashtable<String, Hashtable<String,FieldItem>> cacheMap = new Hashtable<>();
private static Hashtable<String, List<FieldItem>> cacheList = new Hashtable<>();
private static Hashtable<String, FieldItem> cacheDEKeyField = new Hashtable<>();
private static Object objLock1=new Object();
/**
* 将实体对象中的属性存入缓存中
* @param
* @return
*/
public static <T> Hashtable<String,FieldItem> getFieldMap(Class<T> clazz) {
String className=clazz.getName();
if(className.indexOf("_$")>0) {
className=className.substring(0, className.lastIndexOf("_$"));
}
if(cacheMap.containsKey(className)) {
return cacheMap.get(className);
}
synchronized (objLock1) {
if(cacheMap.containsKey(className)) {
return cacheMap.get(className);
}
Hashtable<String,FieldItem> result = new Hashtable<String,FieldItem>();
List<FieldItem> list = new ArrayList<>();
Field[] fields=clazz.getDeclaredFields();
for(Field field:fields){
FieldItem item=new FieldItem().setCodeName(field.getName()).setFieldName(field.getName()).setColumnName(field.getName()).setJsonName(field.getName()).setLogicName(field.getName()).setField(field);
JSONField jsField=field.getAnnotation(JSONField.class);
if(!ObjectUtils.isEmpty(jsField)) {
if(!StringUtils.isEmpty(jsField.name()))
item.setJsonName(jsField.name());
}
else
{
JsonProperty jsonProperty=field.getAnnotation(JsonProperty.class);
if((!ObjectUtils.isEmpty(jsonProperty))&&(!StringUtils.isEmpty(jsonProperty.value())))
item.setJsonName(jsonProperty.value());
}
ApiModelProperty apiModelProperty=field.getAnnotation(ApiModelProperty.class);
if(!ObjectUtils.isEmpty(apiModelProperty)) {
if(!StringUtils.isEmpty(apiModelProperty.value()))
item.setFieldName(apiModelProperty.value()).setColumnName(apiModelProperty.value());
if(!StringUtils.isEmpty(apiModelProperty.notes()))
item.setLogicName(apiModelProperty.notes());
}
if(!cacheDEKeyField.containsKey(className))
{
if(!ObjectUtils.isEmpty(field.getAnnotation(Id.class)))
cacheDEKeyField.put(className,item);
}
list.add(item);
result.put(item.getCodeName(),item);
result.put(item.getFieldName(),item);
result.put(item.getColumnName(),item);
result.put(item.getJsonName(),item);
result.put(item.getCodeName().toLowerCase(),item);
result.put(item.getFieldName().toLowerCase(),item);
result.put(item.getColumnName().toLowerCase(),item);
result.put(item.getJsonName().toLowerCase(),item);
}
cacheMap.put(className, result);
cacheList.put(className,list);
return result;
}
}
public static Hashtable<String,FieldItem> getFieldMap(String className) {
if(className.indexOf("_$")>0) {
className=className.substring(0, className.lastIndexOf("_$"));
}
if(cacheMap.containsKey(className)) {
return cacheMap.get(className);
}
Class clazz = null;
try {
clazz = Class.forName(className);
return getFieldMap(clazz);
}
catch (Exception ex) {
cacheMap.put(className, new Hashtable<String,FieldItem>());
return cacheMap.get(className);
}
}
/**
* 从缓存中查询实体对象主键
* @param
* @return
*/
public static <T> String getKeyField(Class<T> clazz) {
String className=clazz.getName();
if(className.indexOf("_$")>0) {
className=className.substring(0, className.lastIndexOf("_$"));
}
getFieldMap(className);
if(cacheDEKeyField.containsKey(className)) {
return cacheDEKeyField.get(className).getCodeName();
}
return "";
}
public static <T> String getFieldRealName(Class<T> clazz,String fieldname) {
FieldItem field=getField(clazz,fieldname);
if(field!=null) {
return field.getCodeName();
}
return "";
}
public static <T> FieldItem getField(Class<T> clazz,String fieldname) {
Hashtable<String,FieldItem> fields= getFieldMap(clazz);
if(fields.containsKey(fieldname)) {
return fields.get(fieldname);
}
else if(fields.containsKey(fieldname.toLowerCase())) {
return fields.get(fieldname.toLowerCase());
}
else if(fields.containsKey(fieldname.toLowerCase().replace("_",""))) {
return fields.get(fieldname.toLowerCase().replace("_",""));
}
else {
return null;
}
}
public static <T> Object fieldValueOf(Class<T> clazz,String fieldname,Object fieldValue) {
if(fieldValue==null)
return null;
Object resultValue=fieldValue;
FieldItem item=getField(clazz,fieldname);
if(item!=null) {
Class<?> type=item.getField().getType();
resultValue = TypeUtils.castToJavaBean(fieldValue,type);
}
return resultValue;
}
}
\ No newline at end of file
package net.ibizsys.central.plugin.boot.core.helper;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
*
* 单复数转换类
* 2018年12月30日
*/
public class Inflector {
private static final Pattern UNDERSCORE_PATTERN_1 = Pattern.compile("([A-Z]+)([A-Z][a-z])");
private static final Pattern UNDERSCORE_PATTERN_2 = Pattern.compile("([a-z\\d])([A-Z])");
private static List<RuleAndReplacement> plurals = new ArrayList<RuleAndReplacement>();
private static List<RuleAndReplacement> singulars = new ArrayList<RuleAndReplacement>();
private static List<String> uncountables = new ArrayList<String>();
private static Inflector instance;
private Inflector() {
initialize();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
// 单数转复数
System.out.println(Inflector.getInstance().pluralize("water"));
System.out.println(Inflector.getInstance().pluralize("box"));
System.out.println(Inflector.getInstance().pluralize("tomato"));
// 复数转单数
System.out.println(Inflector.getInstance().singularize("apples"));
}
private void initialize() {
plural("$", "s");
plural("s$", "s");
plural("(ax|test)is$", "$1es");
plural("(octop|vir)us$", "$1i");
plural("(alias|status)$", "$1es");
plural("(bu)s$", "$1es");
plural("(buffal|tomat)o$", "$1oes");
plural("([ti])um$", "$1a");
plural("sis$", "ses");
plural("(?:([^f])fe|([lr])f)$", "$1$2ves");
plural("(hive)$", "$1s");
plural("([^aeiouy]|qu)y$", "$1ies");
plural("([^aeiouy]|qu)ies$", "$1y");
plural("(x|ch|ss|sh)$", "$1es");
plural("(matr|vert|ind)ix|ex$", "$1ices");
plural("([m|l])ouse$", "$1ice");
plural("(ox)$", "$1es");
plural("(quiz)$", "$1zes");
singular("s$", "");
singular("(n)ews$", "$1ews");
singular("([ti])a$", "$1um");
singular("((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$", "$1$2sis");
singular("(^analy)ses$", "$1sis");
singular("([^f])ves$", "$1fe");
singular("(hive)s$", "$1");
singular("(tive)s$", "$1");
singular("([lr])ves$", "$1f");
singular("([^aeiouy]|qu)ies$", "$1y");
singular("(s)eries$", "$1eries");
singular("(m)ovies$", "$1ovie");
singular("(x|ch|ss|sh)es$", "$1");
singular("([m|l])ice$", "$1ouse");
singular("(bus)es$", "$1");
singular("(o)es$", "$1");
singular("(shoe)s$", "$1");
singular("(cris|ax|test)es$", "$1is");
singular("([octop|vir])i$", "$1us");
singular("(alias|status)es$", "$1");
singular("^(ox)es", "$1");
singular("(vert|ind)ices$", "$1ex");
singular("(matr)ices$", "$1ix");
singular("(quiz)zes$", "$1");
irregular("person", "people");
irregular("man", "men");
irregular("child", "children");
irregular("sex", "sexes");
irregular("move", "moves");
uncountable(new String[] { "equipment", "information", "rice", "money", "species", "series", "fish", "sheep" });
}
public static Inflector getInstance() {
if (instance == null) {
instance = new Inflector();
}
return instance;
}
public String underscore(String camelCasedWord) {
String underscoredWord = UNDERSCORE_PATTERN_1.matcher(camelCasedWord).replaceAll("$1_$2");
underscoredWord = UNDERSCORE_PATTERN_2.matcher(underscoredWord).replaceAll("$1_$2");
underscoredWord = underscoredWord.replace('-', '_').toLowerCase();
return underscoredWord;
}
public String pluralize(String word) {
if (uncountables.contains(word.toLowerCase())) {
return word;
}
return replaceWithFirstRule(word, plurals);
}
public String singularize(String word) {
if (uncountables.contains(word.toLowerCase())) {
return word;
}
return replaceWithFirstRule(word, singulars);
}
private String replaceWithFirstRule(String word, List<RuleAndReplacement> ruleAndReplacements) {
for (RuleAndReplacement rar : ruleAndReplacements) {
String rule = rar.getRule();
String replacement = rar.getReplacement();
// Return if we find a match.
Matcher matcher = Pattern.compile(rule, Pattern.CASE_INSENSITIVE).matcher(word);
if (matcher.find()) {
return matcher.replaceAll(replacement);
}
}
return word;
}
public String tableize(String className) {
return pluralize(underscore(className));
}
public String tableize(Class<?> klass) {
String className = klass.getName().replace(klass.getPackage().getName() + ".", "");
return tableize(className);
}
public static void plural(String rule, String replacement) {
plurals.add(0, new RuleAndReplacement(rule, replacement));
}
public static void singular(String rule, String replacement) {
singulars.add(0, new RuleAndReplacement(rule, replacement));
}
public static void irregular(String singular, String plural) {
plural(singular, plural);
singular(plural, singular);
}
public static void uncountable(String... words) {
for (String word : words) {
uncountables.add(word);
}
}
}
class RuleAndReplacement {
private String rule;
private String replacement;
public RuleAndReplacement(String rule, String replacement) {
this.rule = rule;
this.replacement = replacement;
}
public String getReplacement() {
return replacement;
}
public void setReplacement(String replacement) {
this.replacement = replacement;
}
public String getRule() {
return rule;
}
public void setRule(String rule) {
this.rule = rule;
}
}
\ No newline at end of file
package net.ibizsys.central.plugin.boot.core.helper;
import org.springframework.core.convert.converter.Converter;
import java.sql.Timestamp;
import java.time.*;
import java.util.Date;
/**
* <p>JSR310DateConverters class.</p>
*/
public final class JSR310DateConverters {
private JSR310DateConverters() {
}
public static class TimestampToDateConverter implements Converter<Timestamp, Date> {
public static final TimestampToDateConverter INSTANCE = new TimestampToDateConverter();
private TimestampToDateConverter() {
}
@Override
public Date convert(Timestamp source) {
return source == null ? null : new Date(source.getTime());
}
}
public static class DateToTimestampConverter implements Converter<Date, Timestamp> {
public static final DateToTimestampConverter INSTANCE = new DateToTimestampConverter();
private DateToTimestampConverter() {
}
@Override
public Timestamp convert(Date source) {
return source == null ? null : new Timestamp(source.getTime());
}
}
public static class LocalDateToDateConverter implements Converter<LocalDate, Date> {
public static final LocalDateToDateConverter INSTANCE = new LocalDateToDateConverter();
private LocalDateToDateConverter() {
}
@Override
public Date convert(LocalDate source) {
return source == null ? null : Date.from(source.atStartOfDay(ZoneId.systemDefault()).toInstant());
}
}
public static class DateToLocalDateConverter implements Converter<Date, LocalDate> {
public static final DateToLocalDateConverter INSTANCE = new DateToLocalDateConverter();
private DateToLocalDateConverter() {
}
@Override
public LocalDate convert(Date source) {
return source == null ? null : ZonedDateTime.ofInstant(source.toInstant(), ZoneId.systemDefault())
.toLocalDate();
}
}
public static class ZonedDateTimeToDateConverter implements Converter<ZonedDateTime, Date> {
public static final ZonedDateTimeToDateConverter INSTANCE = new ZonedDateTimeToDateConverter();
private ZonedDateTimeToDateConverter() {
}
@Override
public Date convert(ZonedDateTime source) {
return source == null ? null : Date.from(source.toInstant());
}
}
public static class DateToZonedDateTimeConverter implements Converter<Date, ZonedDateTime> {
public static final DateToZonedDateTimeConverter INSTANCE = new DateToZonedDateTimeConverter();
private DateToZonedDateTimeConverter() {
}
@Override
public ZonedDateTime convert(Date source) {
return source == null ? null : ZonedDateTime.ofInstant(source.toInstant(), ZoneId.systemDefault());
}
}
public static class LocalDateTimeToDateConverter implements Converter<LocalDateTime, Date> {
public static final LocalDateTimeToDateConverter INSTANCE = new LocalDateTimeToDateConverter();
private LocalDateTimeToDateConverter() {
}
@Override
public Date convert(LocalDateTime source) {
return source == null ? null : Date.from(source.atZone(ZoneId.systemDefault()).toInstant());
}
}
public static class DateToLocalDateTimeConverter implements Converter<Date, LocalDateTime> {
public static final DateToLocalDateTimeConverter INSTANCE = new DateToLocalDateTimeConverter();
private DateToLocalDateTimeConverter() {
}
@Override
public LocalDateTime convert(Date source) {
return source == null ? null : LocalDateTime.ofInstant(source.toInstant(), ZoneId.systemDefault());
}
}
public static class DurationToLongConverter implements Converter<Duration, Long> {
public static final DurationToLongConverter INSTANCE = new DurationToLongConverter();
private DurationToLongConverter() {
}
@Override
public Long convert(Duration source) {
return source == null ? null : source.toNanos();
}
}
public static class LongToDurationConverter implements Converter<Long, Duration> {
public static final LongToDurationConverter INSTANCE = new LongToDurationConverter();
private LongToDurationConverter() {
}
@Override
public Duration convert(Long source) {
return source == null ? null : Duration.ofNanos(source);
}
}
}
\ No newline at end of file
package net.ibizsys.central.plugin.boot.core.helper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import net.ibizsys.central.dataentity.IDataEntityRuntime;
import net.ibizsys.central.dataentity.ds.IDEDataQueryCodeRuntime;
import net.ibizsys.central.plugin.boot.core.filter.BaseFilter;
import net.ibizsys.central.util.ISearchContext;
import net.ibizsys.model.dataentity.defield.IPSDEField;
import net.ibizsys.model.dataentity.ds.IPSDEDataQueryCodeExp;
import net.ibizsys.runtime.dataentity.defield.DEFTypes;
import net.ibizsys.runtime.security.UserContext;
import net.ibizsys.runtime.util.*;
import org.springframework.util.ObjectUtils;
import java.util.List;
import java.util.function.Consumer;
public class QueryHelper {
public static <T> QueryWrapper<T> getQueryWrapper(BaseFilter filter)
{
QueryWrapper query=Wrappers.query();
calcSearchCondItems(query,Conditions.AND,filter.getSearchConds());
return query;
}
protected static <T> void calcSearchCondItems(QueryWrapper<T> query, String groupMode, List<ISearchCond> items) {
int i = 0;
for (ISearchCond iSearchCond : items) {
if (i > 0) {
if (groupMode.equals(Conditions.OR)) {
query.or();
}
}
switch (iSearchCond.getCondType()) {
case ISearchCond.CONDTYPE_GROUP:
searchCondGroup((ISearchGroupCond) iSearchCond,query);
break;
case ISearchCond.CONDTYPE_DEFIELD:
searchDEField((ISearchFieldCond) iSearchCond,query);
break;
case ISearchCond.CONDTYPE_CUSTOM:
query.apply(((ISearchCustomCond) iSearchCond).getCustomCond());
break;
}
i++;
}
}
protected static <T> void searchCondGroup(ISearchGroupCond cond,QueryWrapper<T> query) {
List<ISearchCond> items = cond.getSearchConds();
if (!ObjectUtils.isEmpty(items)) {
query.and(new Consumer<QueryWrapper<T>>() {
@Override
public void accept(QueryWrapper<T> objectQueryWrapper) {
switch (cond.getCondOp()) {
case Conditions.AND:
objectQueryWrapper.and(new Consumer<QueryWrapper<T>>() {
@Override
public void accept(QueryWrapper<T> subQuery) {
calcSearchCondItems(subQuery, Conditions.AND, items);
}
});
break;
case Conditions.OR:
objectQueryWrapper.or(new Consumer<QueryWrapper<T>>() {
@Override
public void accept(QueryWrapper<T> subQuery) {
calcSearchCondItems(subQuery, Conditions.OR, items);
}
});
break;
}
}
});
}
}
protected static <T> void searchDEField(ISearchFieldCond cond,QueryWrapper<T> query) {
String column = cond.getFieldName();
Object val = cond.getValue();
switch (cond.getCondOp()) {
// 等于
case Conditions.EQ:
query.eq(column, val);
break;
// 不等于
case Conditions.NOTEQ:
query.ne(column, val);
break;
// 大于
case Conditions.GT:
query.gt(column, val);
break;
// 大于等于
case Conditions.GTANDEQ:
query.ge(column, val);
break;
// 小于
case Conditions.LT:
query.lt(column, val);
break;
// 小于等于
case Conditions.LTANDEQ:
query.le(column, val);
break;
// 为空
case Conditions.ISNULL:
query.isNull(column);
break;
// 不为空
case Conditions.ISNOTNULL:
query.isNotNull(column);
break;
// 文本包含
case Conditions.LIKE:
case Conditions.USERLIKE:
query.like(column, val);
break;
// 值在范围中
case Conditions.IN:
query.in(column, val);
break;
// 值不在范围中
case Conditions.NOTIN:
query.notIn(column, val);
break;
// 文本左包含
case Conditions.LEFTLIKE:
query.likeRight(column, val);
break;
// 文本右包含
case Conditions.RIGHTLIKE:
query.likeLeft(column, val);
break;
}
}
}
package net.ibizsys.central.plugin.boot.core.helper;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
@Component
public class SpringContextHolder implements ApplicationContextAware
{
private static ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
SpringContextHolder.applicationContext = applicationContext;
}
public static ApplicationContext getApplicationContext() {
assertApplicationContext();
return applicationContext;
}
@SuppressWarnings("unchecked")
public static <T> T getBean(String beanName) {
assertApplicationContext();
return (T) applicationContext.getBean(beanName);
}
public static <T> T getBean(Class<T> requiredType) {
assertApplicationContext();
return applicationContext.getBean(requiredType);
}
private static void assertApplicationContext() {
if (SpringContextHolder.applicationContext == null) {
throw new RuntimeException("applicaitonContext属性为null,请检查是否注入了SpringContextHolder!");
}
}
}
\ No newline at end of file
package net.ibizsys.central.plugin.boot.core.helper;
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.Ticker;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.apache.commons.lang3.tuple.Pair;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class StringAdvUtils {
/**
* Set the cache size (entry count) of the sanitizedNameCache, camelizedWordsCache and underscoreWordsCache.
*/
public static final String NAME_CACHE_SIZE_PROPERTY = "cn.ibizlab.codegen.utils.namecache.cachesize";
/**
* Set the cache expiry (in seconds) of the sanitizedNameCache, camelizedWordsCache and underscoreWordsCache.
*/
public static final String NAME_CACHE_EXPIRY_PROPERTY = "cn.ibizlab.codegen.utils.namecache.expireafter.seconds";
// A cache of camelized words. The camelize() method is invoked many times with the same
// arguments, this cache is used to optimized performance.
private static Cache<Pair<String, Boolean>, String> camelizedWordsCache;
// A cache of underscored words, used to optimize the performance of the underscore() method.
private static Cache<String, String> underscoreWordsCache;
// A cache of escaped words, used to optimize the performance of the escape() method.
private static Cache<EscapedNameOptions, String> escapedWordsCache;
static {
int cacheSize = 200;
int cacheExpiry = 5;
camelizedWordsCache = Caffeine.newBuilder()
.maximumSize(cacheSize)
.expireAfterAccess(cacheExpiry, TimeUnit.SECONDS)
.ticker(Ticker.systemTicker())
.build();
escapedWordsCache = Caffeine.newBuilder()
.maximumSize(cacheSize)
.expireAfterAccess(cacheExpiry, TimeUnit.SECONDS)
.ticker(Ticker.systemTicker())
.build();
underscoreWordsCache = Caffeine.newBuilder()
.maximumSize(cacheSize)
.expireAfterAccess(cacheExpiry, TimeUnit.SECONDS)
.ticker(Ticker.systemTicker())
.build();
}
private static Pattern capitalLetterPattern = Pattern.compile("([A-Z]+)([A-Z][a-z][a-z]+)");
private static Pattern lowercasePattern = Pattern.compile("([a-z\\d])([A-Z])");
private static Pattern pkgSeparatorPattern = Pattern.compile("\\.");
private static Pattern dollarPattern = Pattern.compile("\\$");
/**
* Underscore the given word.
* Copied from Twitter elephant bird
* https://github.com/twitter/elephant-bird/blob/master/core/src/main/java/com/twitter/elephantbird/util/Strings.java
*
* @param word The word
* @return The underscored version of the word
*/
public static String underscore(final String word) {
return underscoreWordsCache.get(word, wordToUnderscore -> {
String result;
String replacementPattern = "$1_$2";
// Replace package separator with slash.
result = pkgSeparatorPattern.matcher(wordToUnderscore).replaceAll("/");
// Replace $ with two underscores for inner classes.
result = dollarPattern.matcher(result).replaceAll("__");
// Replace capital letter with _ plus lowercase letter.
result = capitalLetterPattern.matcher(result).replaceAll(replacementPattern);
result = lowercasePattern.matcher(result).replaceAll(replacementPattern);
result = result.replace('-', '_');
// replace space with underscore
result = result.replace(' ', '_');
result = result.toLowerCase(Locale.ROOT);
return result;
});
}
/**
* Dashize the given word.
*
* @param word The word
* @return The dashized version of the word, e.g. "my-name"
*/
public static String dashize(String word) {
return underscore(word).replaceAll("[_ ]+", "-");
}
/**
* Camelize name (parameter, property, method, etc) with upper case for first letter
* copied from Twitter elephant bird
* https://github.com/twitter/elephant-bird/blob/master/core/src/main/java/com/twitter/elephantbird/util/Strings.java
*
* @param word string to be camelize
* @return camelized string
*/
public static String camelize(String word) {
return camelize(word, false);
}
public static String pascalcase(String word) {
return camelize(word, false);
}
public static String camelcase(String word) {
return camelize(word, true);
}
public static String spinalcase(String word) {
return dashize(word);
}
public static String snakecase(String word) {
return underscore(word);
}
public static String pluralize(String word) {
return Inflector.getInstance().pluralize(camelcase(word).toLowerCase());
}
private static Pattern camelizeSlashPattern = Pattern.compile("\\/(.?)");
private static Pattern camelizeUppercasePattern = Pattern.compile("(\\.?)(\\w)([^\\.]*)$");
private static Pattern camelizeUnderscorePattern = Pattern.compile("(_)(.)");
private static Pattern camelizeHyphenPattern = Pattern.compile("(-)(.)");
private static Pattern camelizeDollarPattern = Pattern.compile("\\$");
private static Pattern camelizeSimpleUnderscorePattern = Pattern.compile("_");
/**
* Camelize name (parameter, property, method, etc)
*
* @param inputWord string to be camelize
* @param lowercaseFirstLetter lower case for first letter if set to true
* @return camelized string
*/
public static String camelize(final String inputWord, boolean lowercaseFirstLetter) {
Pair<String, Boolean> key = new ImmutablePair<>(underscore(inputWord), lowercaseFirstLetter);
return camelizedWordsCache.get(key, pair -> {
String word = pair.getKey();
Boolean lowerFirstLetter = pair.getValue();
// Replace all slashes with dots (package separator)
Matcher m = camelizeSlashPattern.matcher(word);
while (m.find()) {
word = m.replaceFirst("." + m.group(1)/*.toUpperCase()*/);
m = camelizeSlashPattern.matcher(word);
}
// case out dots
String[] parts = word.split("\\.");
StringBuilder f = new StringBuilder();
for (String z : parts) {
if (z.length() > 0) {
f.append(Character.toUpperCase(z.charAt(0))).append(z.substring(1));
}
}
word = f.toString();
m = camelizeSlashPattern.matcher(word);
while (m.find()) {
word = m.replaceFirst(Character.toUpperCase(m.group(1).charAt(0)) + m.group(1).substring(1)/*.toUpperCase()*/);
m = camelizeSlashPattern.matcher(word);
}
// Uppercase the class name.
m = camelizeUppercasePattern.matcher(word);
if (m.find()) {
String rep = m.group(1) + m.group(2).toUpperCase(Locale.ROOT) + m.group(3);
rep = camelizeDollarPattern.matcher(rep).replaceAll("\\\\\\$");
word = m.replaceAll(rep);
}
// Remove all underscores (underscore_case to camelCase)
m = camelizeUnderscorePattern.matcher(word);
while (m.find()) {
String original = m.group(2);
String upperCase = original.toUpperCase(Locale.ROOT);
if (original.equals(upperCase)) {
word = camelizeSimpleUnderscorePattern.matcher(word).replaceFirst("");
} else {
word = m.replaceFirst(upperCase);
}
m = camelizeUnderscorePattern.matcher(word);
}
// Remove all hyphens (hyphen-case to camelCase)
m = camelizeHyphenPattern.matcher(word);
while (m.find()) {
word = m.replaceFirst(m.group(2).toUpperCase(Locale.ROOT));
m = camelizeHyphenPattern.matcher(word);
}
if (lowerFirstLetter && word.length() > 0) {
int i = 0;
char charAt = word.charAt(i);
while (i + 1 < word.length() && !((charAt >= 'a' && charAt <= 'z') || (charAt >= 'A' && charAt <= 'Z'))) {
i = i + 1;
charAt = word.charAt(i);
}
i = i + 1;
word = word.substring(0, i).toLowerCase(Locale.ROOT) + word.substring(i);
}
// remove all underscore
word = camelizeSimpleUnderscorePattern.matcher(word).replaceAll("");
return word;
});
}
private static class EscapedNameOptions {
public EscapedNameOptions(String name, Set<String> specialChars, List<String> charactersToAllow, String appendToReplacement) {
this.name = name;
this.appendToReplacement = appendToReplacement;
if (specialChars != null) {
this.specialChars = Collections.unmodifiableSet(specialChars);
} else {
this.specialChars = Collections.emptySet();
}
if (charactersToAllow != null) {
this.charactersToAllow = Collections.unmodifiableList(charactersToAllow);
} else {
this.charactersToAllow = Collections.emptyList();
}
}
private String name;
private String appendToReplacement;
private Set<String> specialChars;
private List<String> charactersToAllow;
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
EscapedNameOptions that = (EscapedNameOptions) o;
return Objects.equals(name, that.name) &&
Objects.equals(appendToReplacement, that.appendToReplacement) &&
Objects.equals(specialChars, that.specialChars) &&
Objects.equals(charactersToAllow, that.charactersToAllow);
}
@Override
public int hashCode() {
return Objects.hash(name, appendToReplacement, specialChars, charactersToAllow);
}
}
/**
* Return the name with escaped characters.
*
* @param name the name to be escaped
* @param replacementMap map of replacement characters for non-allowed characters
* @param charactersToAllow characters that are not escaped
* @param appendToReplacement String to append to replaced characters.
* @return the escaped word
* <p>
* throws Runtime exception as word is not escaped properly.
*/
public static String escape(final String name, final Map<String, String> replacementMap,
final List<String> charactersToAllow, final String appendToReplacement) {
EscapedNameOptions ns = new EscapedNameOptions(name, replacementMap.keySet(), charactersToAllow, appendToReplacement);
return StringAdvUtils.escapedWordsCache.get(ns, wordToEscape -> {
String result = name.chars().mapToObj(c -> {
String character = String.valueOf((char) c);
if (charactersToAllow != null && charactersToAllow.contains(character)) {
return character;
} else if (replacementMap.containsKey(character)) {
return replacementMap.get(character) + (appendToReplacement != null ? appendToReplacement: "");
} else {
return character;
}
}).reduce( (c1, c2) -> c1 + c2).orElse(null);
if (result != null) return result;
throw new RuntimeException("Word '" + name + "' could not be escaped.");
});
}
}
package net.ibizsys.central.plugin.boot.core.helper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.AnnotationBeanNameGenerator;
@Slf4j
public class UniqueNameGenerator extends AnnotationBeanNameGenerator {
@Override
public String generateBeanName(BeanDefinition definition, BeanDefinitionRegistry registry) {
//全限定类名
String beanName = definition.getBeanClassName();
return beanName;
}
}
package net.ibizsys.central.plugin.boot.core.runtime;
import com.alibaba.fastjson.JSON;
import net.ibizsys.central.ISystemGateway;
import net.ibizsys.central.dataentity.IDataEntityRuntimeContext;
import net.ibizsys.central.dataentity.service.DEServiceBase;
import net.ibizsys.central.dataentity.service.IDEService;
import net.ibizsys.central.plugin.boot.core.domain.BaseData;
import net.ibizsys.central.plugin.boot.core.filter.BaseFilter;
import net.ibizsys.central.plugin.boot.core.helper.StringAdvUtils;
import net.ibizsys.central.plugin.boot.core.service.IBaseService;
import net.ibizsys.central.plugin.boot.core.service.IMPService;
import net.ibizsys.central.plugin.boot.core.service.impl.BaseServiceImpl;
import net.ibizsys.central.plugin.boot.core.service.impl.MPServiceImpl;
import net.ibizsys.model.dataentity.action.IPSDEAction;
import net.ibizsys.model.dataentity.ds.IPSDEDataSet;
import net.ibizsys.model.dataentity.service.IPSDEMethodDTO;
import net.ibizsys.runtime.dataentity.service.DEMethodReturnTypes;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.util.Assert;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.util.HashMap;
import java.util.Map;
public class BootServiceImpl<T extends BaseData, F extends BaseFilter> extends DEServiceBase implements IBootService<T,F> {
private Class<T> tClass;
private Class<F> fClass;
@Override
public T createEntityDTO(IPSDEMethodDTO iPSDEMethodDTO) {
if(iPSDEMethodDTO!=null&&iPSDEMethodDTO.getName()!=null&&iPSDEMethodDTO.getName().endsWith("DTO")) {
try {
T t = (T) tClass.newInstance();
return t;
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
@Override
public F createSearchContextDTO(IPSDEMethodDTO iPSDEMethodDTO) {
try {
F f = (F) fClass.newInstance();
return f;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
private IBootSystemRuntime systemRuntime;
@Override
public IBootSystemRuntime getSystemRuntime() {
return this.systemRuntime;
}
public void setSystemRuntime(IBootSystemRuntime systemRuntime) {
this.systemRuntime=systemRuntime;
}
private IDataEntityRuntimeContext dataEntityRuntimeContext;
public IDataEntityRuntimeContext getDataEntityRuntimeContext() {
return dataEntityRuntimeContext;
}
public void setDataEntityRuntimeContext(IDataEntityRuntimeContext dataEntityRuntimeContext) {
if(this.dataEntityRuntimeContext==null)
this.dataEntityRuntimeContext = dataEntityRuntimeContext;
}
private String dataEntityId;
public void setDataEntityId(String dataEntityId)
{
this.dataEntityId = dataEntityId;
}
@Override
public String getDataEntityId() {
return dataEntityId;
}
private IBaseService baseService;
public void setBaseService(IBaseService baseService)
{
if(baseService==null)
return;
this.baseService = baseService;
this.setDataEntityId(baseService.getDataEntityId());
Class serviceClass=baseService.getClass();
while (serviceClass!=null) {
if(!ObjectUtils.isEmpty(serviceClass.getInterfaces())) {
Class interfaceClass=serviceClass.getInterfaces()[0];
if(!ObjectUtils.isEmpty(interfaceClass.getInterfaces())) {
if(interfaceClass.getInterfaces()[0].isAssignableFrom(IBaseService.class)
||interfaceClass.getInterfaces()[0].isAssignableFrom(IMPService.class)) {
serviceClass=interfaceClass;
break;
}
}
}
serviceClass = serviceClass.getSuperclass();
}
if(!ObjectUtils.isEmpty(serviceClass.getGenericInterfaces())) {
ParameterizedType ptype = (ParameterizedType) serviceClass.getGenericInterfaces()[0];
tClass = (Class) ptype.getActualTypeArguments()[0];
fClass = (Class) ptype.getActualTypeArguments()[1];
}
}
public IBaseService getBaseService(){
return baseService;
}
public BootServiceImpl(){}
public BootServiceImpl(IBaseService baseService) {
this.setBaseService(baseService);
BootSystemRuntime.addLocalDEServiceMap(baseService.getDataEntityId(),this);
}
public Object executeAction(String strActionName, IPSDEAction iPSDEAction, Object[] args) throws Throwable {
if(getBaseService()!=null) {
if(args.length>0&&args[0]!=null) {
// if(args[0] instanceof BaseData)
// {
// Method beforeMethod = getBaseService().getServiceMethod("before"+StringAdvUtils.pascalcase(strActionName), args[0].getClass());
// if (beforeMethod != null)
// beforeMethod.invoke(this.getBaseService(), args[0]);
// }
Method method = getBaseService().getServiceMethod(StringAdvUtils.camelcase(strActionName), args[0].getClass());
if (method != null) {
Object rt = null;
if (iPSDEAction != null && iPSDEAction.getPSDEActionReturn() != null && DEMethodReturnTypes.VOID.equals(iPSDEAction.getPSDEActionReturn().getType()))
method.invoke(this.getBaseService(), args[0]);
else
rt = method.invoke(this.getBaseService(), args[0]);
// if(args[0] instanceof BaseData)
// {
// Method afterMethod = getBaseService().getServiceMethod("after"+StringAdvUtils.pascalcase(strActionName), args[0].getClass());
// if (afterMethod != null)
// afterMethod.invoke(this.getBaseService(), args[0]);
// }
return rt;
}
}
return getBaseService().executeAction(strActionName, iPSDEAction, args);
}
return this.getDataEntityRuntime().executeAction(strActionName, iPSDEAction, args, true);
}
public Object executeActionReal(IDataEntityRuntimeContext iDataEntityRuntimeContext, String strActionName, IPSDEAction iPSDEAction, Object[] args, Object actionData) throws Throwable{
Assert.notNull(iDataEntityRuntimeContext, "传入实体运行时上下文对象无效");
this.setDataEntityRuntimeContext(iDataEntityRuntimeContext);
if(getBaseService()!=null) {
if(args.length>0&&args[0]!=null) {
Method method = getBaseService().getServiceMethod("on"+StringAdvUtils.pascalcase(strActionName), args[0].getClass());
if (method != null) {
Object rt = null;
if (iPSDEAction != null && iPSDEAction.getPSDEActionReturn() != null && DEMethodReturnTypes.VOID.equals(iPSDEAction.getPSDEActionReturn().getType()))
method.invoke(this.getBaseService(), args[0]);
else
rt = method.invoke(this.getBaseService(), args[0]);
return rt;
}
}
return getBaseService().executeActionReal(iDataEntityRuntimeContext, strActionName, iPSDEAction, args, actionData);
}
return iDataEntityRuntimeContext.executeActionReal(strActionName, iPSDEAction, args, actionData);
}
public Object fetchDataSet(String strDataSetName, IPSDEDataSet iPSDEDataSet, Object[] args) throws Throwable {
if(getBaseService()!=null) {
if(args.length>0&&args[0]!=null) {
((F) args[0]).setDataSet(iPSDEDataSet);
// if(args[0] instanceof BaseFilter)
// {
// Method beforeMethod = getBaseService().getServiceMethod("beforeFetch"+StringAdvUtils.pascalcase(strDataSetName), args[0].getClass());
// if (beforeMethod != null)
// beforeMethod.invoke(this.getBaseService(), args[0]);
// }
Method method = getBaseService().getServiceMethod("fetch" + StringAdvUtils.pascalcase(strDataSetName), args[0].getClass());
if (method != null)
return method.invoke(this.getBaseService(), args[0]);
}
return getBaseService().fetchDataSet(strDataSetName,iPSDEDataSet,args);
}
return this.getDataEntityRuntime().fetchDataSet(strDataSetName, iPSDEDataSet, args, true);
}
public Object fetchDataSetReal(IDataEntityRuntimeContext iDataEntityRuntimeContext, String strDataSetName, IPSDEDataSet iPSDEDataSet, Object[] args, Object actionData) throws Throwable{
Assert.notNull(iDataEntityRuntimeContext, "传入实体运行时上下文对象无效");
this.setDataEntityRuntimeContext(iDataEntityRuntimeContext);
if(getBaseService()!=null) {
if(args.length>0&&args[0]!=null) {
((F) args[0]).setDataSet(iPSDEDataSet);
Method method = getBaseService().getServiceMethod("onFetch" + StringAdvUtils.pascalcase(strDataSetName), args[0].getClass());
if (method != null)
return method.invoke(this.getBaseService(), args[0]);
}
return getBaseService().fetchDataSetReal(iDataEntityRuntimeContext, strDataSetName, iPSDEDataSet, args, actionData);
}
return iDataEntityRuntimeContext.fetchDataSetReal(strDataSetName, iPSDEDataSet, args, actionData);
}
}
package net.ibizsys.central.plugin.boot.core.runtime;
import net.ibizsys.central.SystemGateway;
import net.ibizsys.central.database.ISysDBSchemeRuntime;
import net.ibizsys.central.database.SysDBSchemeRuntime;
import net.ibizsys.central.dataentity.DataEntityRuntime;
import net.ibizsys.central.dataentity.IDataEntityRuntime;
import net.ibizsys.central.dataentity.service.IDEService;
import net.ibizsys.model.IPSSystemService;
import net.ibizsys.model.PSModelServiceImpl;
import net.ibizsys.model.dataentity.IPSDataEntity;
import org.springframework.context.annotation.Primary;
import java.lang.annotation.Annotation;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
public class BootSystemRuntime extends net.ibizsys.central.cloud.core.ServiceSystemRuntime implements IBootSystemRuntime {
private static IBootSystemRuntime instance = null;
public static IBootSystemRuntime getInstance() {
return getInstance(false);
}
private static Map<String, IBootService> localDEServiceMap = new ConcurrentHashMap();
public static synchronized void addLocalDEServiceMap(String dataEntityid,IBootService service)
{
if(localDEServiceMap.containsKey(dataEntityid)&&service.getBaseService().getClass().getAnnotation(Primary.class)==null)
return;
localDEServiceMap.put(dataEntityid,service);
}
public static IBootSystemRuntime getInstance(boolean bTryMode) {
if(BootSystemRuntime.instance != null || bTryMode) {
return BootSystemRuntime.instance;
}
throw new RuntimeException("核心系统运行时实例不存在");
}
public static void setInstance(IBootSystemRuntime instance) {
BootSystemRuntime.instance = instance;
}
@Override
protected void onStart() throws Exception {
super.onStart();
if(getInstance(true) == null) {
setInstance(this);
}
}
@Override
public IDEService getDEService(IDataEntityRuntime iDataEntityRuntime) {
IBootService service= localDEServiceMap.get(iDataEntityRuntime.getId());
if(service==null)
return super.getDEService(iDataEntityRuntime);
if(service.getSystemRuntime()==null)
service.setSystemRuntime(this);
return service;
}
public boolean isEnableDEService(IPSDataEntity iPSDataEntity) {
return this.localDEServiceMap.containsKey(iPSDataEntity.getId());
}
@Override
protected IDataEntityRuntime createDefaultDataEntityRuntime() {
return new DataEntityRuntime();
}
@Override
protected ISysDBSchemeRuntime createDefaultSysDBSchemeRuntime() {
return new SysDBSchemeRuntime();
}
@Override
public String getName() {
return "iBizBoot";
}
@Override
public void registerDEServiceObj(String strDataEntityId, String strDEServiceObj) {
super.registerDEServiceObj(strDataEntityId,strDEServiceObj);
}
}
package net.ibizsys.central.plugin.boot.core.runtime;
import net.ibizsys.central.dataentity.IDataEntityRuntimeContext;
import net.ibizsys.central.dataentity.service.IDEService;
import net.ibizsys.central.plugin.boot.core.domain.BaseData;
import net.ibizsys.central.plugin.boot.core.filter.BaseFilter;
import net.ibizsys.central.plugin.boot.core.helper.StringAdvUtils;
import net.ibizsys.central.plugin.boot.core.service.IBaseService;
import net.ibizsys.central.util.EntityDTO;
import net.ibizsys.central.util.IEntityDTO;
import net.ibizsys.central.util.ISearchContextDTO;
import net.ibizsys.central.util.SearchContextDTO;
import net.ibizsys.model.dataentity.action.IPSDEAction;
import net.ibizsys.model.dataentity.ds.IPSDEDataSet;
import net.ibizsys.model.dataentity.service.IPSDEMethodDTO;
import org.springframework.util.Assert;
import java.lang.reflect.*;
public interface IBootService<T extends BaseData, F extends BaseFilter> extends IDEService {
IBootSystemRuntime getSystemRuntime();
void setSystemRuntime(IBootSystemRuntime systemRuntime);
IBaseService getBaseService();
default IDataEntityRuntimeContext getDataEntityRuntimeContext() {
return null;
}
default void setDataEntityRuntimeContext(IDataEntityRuntimeContext dataEntityRuntimeContext) { }
}
package net.ibizsys.central.plugin.boot.core.runtime;
public interface IBootSystemRuntime extends net.ibizsys.central.cloud.core.IServiceSystemRuntime {
default void registerDEServiceObj(String strDataEntityId, String strDEServiceObj) {
}
}
package net.ibizsys.central.plugin.boot.core.service;
import net.ibizsys.central.dataentity.IDataEntityRuntime;
import net.ibizsys.central.dataentity.IDataEntityRuntimeContext;
import net.ibizsys.central.plugin.boot.core.domain.BaseData;
import net.ibizsys.central.plugin.boot.core.filter.BaseFilter;
import net.ibizsys.central.plugin.boot.core.helper.StringAdvUtils;
import net.ibizsys.central.plugin.boot.core.runtime.IBootService;
import net.ibizsys.central.plugin.boot.core.runtime.IBootSystemRuntime;
import net.ibizsys.model.dataentity.action.IPSDEAction;
import net.ibizsys.model.dataentity.ds.IPSDEDataSet;
import org.springframework.data.domain.Page;
import java.io.Serializable;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
public interface IBaseService<T extends BaseData, F extends BaseFilter> {
String getDataEntityId();
default IBootService getRuntimeService() {
return null;
}
default void setRuntimeService(IBootService runtimeService) { }
default IBootSystemRuntime getSystemRuntime(){
return getRuntimeService().getSystemRuntime();
}
default IDataEntityRuntime getDataEntityRuntime(){
return getRuntimeService().getDataEntityRuntime();
}
Method getServiceMethod(String name, Class clazz);
default IDataEntityRuntimeContext getDataEntityRuntimeContext() {
return getRuntimeService().getDataEntityRuntimeContext();
}
default Object executeAction(String strActionName, IPSDEAction iPSDEAction, Object[] args) throws Throwable {
return this.getRuntimeService().getDataEntityRuntime().executeAction(strActionName, iPSDEAction, args, true);
}
default Object fetchDataSet(String strDataSetName, IPSDEDataSet iPSDEDataSet, Object[] args) throws Throwable {
return this.getRuntimeService().getDataEntityRuntime().fetchDataSet(strDataSetName, iPSDEDataSet, args, true);
}
default Object executeActionReal(IDataEntityRuntimeContext iDataEntityRuntimeContext, String strActionName, IPSDEAction iPSDEAction, Object[] args, Object actionData) throws Throwable{
return iDataEntityRuntimeContext.executeActionReal(strActionName, iPSDEAction, args, actionData);
}
default Object fetchDataSetReal(IDataEntityRuntimeContext iDataEntityRuntimeContext, String strDataSetName, IPSDEDataSet iPSDEDataSet, Object[] args, Object actionData) throws Throwable{
return iDataEntityRuntimeContext.fetchDataSetReal(strDataSetName, iPSDEDataSet, args, actionData);
}
default void beforeAction(String strActionName,Object arg) throws Throwable{
Method beforeMethod = getServiceMethod("before"+ StringAdvUtils.pascalcase(strActionName), arg.getClass());
if (beforeMethod != null)
beforeMethod.invoke(this, arg);
}
default void beforeFetch(String strActionName,Object arg) throws Throwable{
Method beforeMethod = getServiceMethod("beforeFetch"+ StringAdvUtils.pascalcase(strActionName), arg.getClass());
if (beforeMethod != null)
beforeMethod.invoke(this, arg);
}
default void afterAction(String strActionName,Object arg) throws Throwable{
Method afterMethod = getServiceMethod("after"+ StringAdvUtils.pascalcase(strActionName), arg.getClass());
if (afterMethod != null)
afterMethod.invoke(this, arg);
}
default boolean create(T dto) throws Throwable {
beforeAction("Create",dto);
this.getRuntimeService().getDataEntityRuntime().executeAction("Create", null, new Object[]{dto}, true);
afterAction("Create",dto);
return true;
}
default boolean onCreate(T dto) throws Throwable {
this.getDataEntityRuntimeContext().executeActionReal("Create", null, new Object[]{dto}, null);
return true;
}
default boolean update(T dto) throws Throwable {
beforeAction("Update",dto);
this.getRuntimeService().getDataEntityRuntime().executeAction("Update", null, new Object[]{dto}, true);
afterAction("Update",dto);
return true;
}
default boolean onUpdate(T dto) throws Throwable {
this.getDataEntityRuntimeContext().executeActionReal("Update", null, new Object[]{dto}, null);
return true;
}
default boolean save(T dto) throws Throwable {
beforeAction("Save",dto);
this.getRuntimeService().getDataEntityRuntime().executeAction("Save", null, new Object[]{dto}, true);
afterAction("Save",dto);
return true;
}
default boolean onSave(T dto) throws Throwable {
this.getDataEntityRuntimeContext().executeActionReal("Save", null, new Object[]{dto}, null);
return true;
}
default <K extends Serializable> boolean remove(K key) throws Throwable {
beforeAction("Remove",key);
List keys=new ArrayList();
keys.add(key);
if(!remove(keys))
return false;
afterAction("Remove",key);
return true;
}
default boolean remove(List keys) throws Throwable {
beforeAction("Remove",keys);
this.getRuntimeService().getDataEntityRuntime().executeAction("Remove", null, new Object[]{keys}, true);
afterAction("Remove",keys);
return true;
}
default boolean onRemove(List keys) throws Throwable {
this.getDataEntityRuntimeContext().executeActionReal("Remove", null, new Object[]{keys}, null);
return true;
}
default <K extends Serializable> T get(K key) throws Throwable {
T dto = (T)this.getRuntimeService().getDataEntityRuntime().executeAction("Get", null, new Object[]{key}, true);
if(dto!=null)
afterAction("Get",dto);
return dto;
}
default <K extends Serializable> T onGet(K key) throws Throwable {
return (T)this.getDataEntityRuntimeContext().executeActionReal("Get", null, new Object[]{key}, null);
}
default T getDraft(T dto) throws Throwable {
beforeAction("GetDraft",dto);
T rtdto = (T)this.getRuntimeService().getDataEntityRuntime().executeAction("GetDraft", null, new Object[]{dto}, true);
if(dto!=null)
afterAction("GetDraft",rtdto);
return rtdto;
}
default T onGetDraft(T dto) throws Throwable {
return (T)this.getDataEntityRuntimeContext().executeActionReal("GetDraft", null, new Object[]{dto}, null);
}
default Integer checkKey(T dto) throws Throwable {
beforeAction("CheckKey",dto);
return (Integer) this.getRuntimeService().getDataEntityRuntime().executeAction("CheckKey", null, new Object[]{dto}, true);
}
default Integer onCheckKey(T dto) throws Throwable {
return (Integer) this.getDataEntityRuntimeContext().executeActionReal("CheckKey", null, new Object[]{dto}, null);
}
default Page<T> fetchDefault(F dto) throws Throwable {
beforeFetch("Default",dto);
return (Page) this.getRuntimeService().getDataEntityRuntime().fetchDataSet("DEFAULT", dto.getDataSet(), new Object[]{dto}, true);
}
default Page<T> onFetchDefault(F dto) throws Throwable {
return (Page) this.getDataEntityRuntimeContext().fetchDataSetReal("DEFAULT", dto.getDataSet(), new Object[]{dto}, null);
}
default List<T> selectDefault(F dto) throws Throwable {
return (List)this.getRuntimeService().getDataEntityRuntime().selectDataQuery("DEFAULT", dto);
}
default List<T> selectSimple(F dto) throws Throwable {
return (List)this.getRuntimeService().getDataEntityRuntime().selectDataQuery("SIMPLE", dto);
}
default List<T> selectView(F dto) throws Throwable {
return (List)this.getRuntimeService().getDataEntityRuntime().selectDataQuery("VIEW", dto);
}
}
package net.ibizsys.central.plugin.boot.core.service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import net.ibizsys.central.dataentity.IDataEntityRuntime;
import net.ibizsys.central.dataentity.IDataEntityRuntimeContext;
import net.ibizsys.central.plugin.boot.core.domain.BaseData;
import net.ibizsys.central.plugin.boot.core.filter.BaseFilter;
import net.ibizsys.central.plugin.boot.core.runtime.IBootService;
import net.ibizsys.central.plugin.boot.core.runtime.IBootSystemRuntime;
import net.ibizsys.model.dataentity.action.IPSDEAction;
import net.ibizsys.model.dataentity.ds.IPSDEDataSet;
import org.springframework.data.domain.Page;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
public interface IMPService<T extends BaseData, F extends BaseFilter, M extends BaseMapper<T>> extends IBaseService<T,F>{
default M getBaseMapper(){
return null;
}
default List<T> list(QueryWrapper<T> queryWrapper) {
return this.getBaseMapper().selectList(queryWrapper);
}
default Integer count(QueryWrapper<T> queryWrapper) {
return this.getBaseMapper().selectCount(queryWrapper);
}
default T getOne(QueryWrapper<T> queryWrapper) {
return this.getBaseMapper().selectOne(queryWrapper);
}
default <K extends Serializable> T getById(K key) {
return this.getBaseMapper().selectById(key);
}
}
package net.ibizsys.central.plugin.boot.core.service.impl;
import net.ibizsys.central.plugin.boot.core.domain.BaseData;
import net.ibizsys.central.plugin.boot.core.filter.BaseFilter;
import net.ibizsys.central.plugin.boot.core.runtime.BootServiceImpl;
import net.ibizsys.central.plugin.boot.core.runtime.IBootService;
import net.ibizsys.central.plugin.boot.core.service.IBaseService;
import javax.annotation.PostConstruct;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
public abstract class BaseServiceImpl<T extends BaseData, F extends BaseFilter> implements IBaseService<T, F> {
private IBootService runtimeService;
@Override
public IBootService getRuntimeService(){
return runtimeService;
}
public void setRuntimeService(IBootService runtimeService) {
this.runtimeService = runtimeService;
}
@PostConstruct
private void postConstruct() throws Exception {
runtimeService = new BootServiceImpl<T,F>(this);
}
private Object lockMethods = new Object();
private Map<String, Method> serviceMethods=new HashMap<>();
public Method getServiceMethod(String name,Class clazz) {
Method method = null;
synchronized (lockMethods)
{
String tag=name;
if(clazz.isArray())
tag=tag+"List";
if(serviceMethods.containsKey(tag))
return serviceMethods.get(tag);
try {
method=this.getClass().getMethod(name,clazz);
} catch (NoSuchMethodException e) {
}
serviceMethods.put(tag,method);
}
return method;
}
}
package net.ibizsys.central.plugin.boot.core.service.impl;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import net.ibizsys.central.plugin.boot.core.domain.BaseData;
import net.ibizsys.central.plugin.boot.core.filter.BaseFilter;
import net.ibizsys.central.plugin.boot.core.service.IMPService;
import org.springframework.beans.factory.annotation.Autowired;
public abstract class MPServiceImpl<T extends BaseData, F extends BaseFilter, M extends BaseMapper<T>> extends BaseServiceImpl<T,F> implements IMPService<T, F, M> {
@Autowired
protected M baseMapper;
public M getBaseMapper() {
return this.baseMapper;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>net.ibizsys.plugin</groupId>
<artifactId>ibiz-boot-starter-parent</artifactId>
<version>2.4.0-SNAPSHOT</version>
<relativePath>ibiz-boot-starter-parent/pom.xml</relativePath>
</parent>
<groupId>net.ibizsys.plugin</groupId>
<artifactId>ibiz-boot-starters</artifactId>
<version>2.4.0-SNAPSHOT</version>
<packaging>pom</packaging>
<name>ibiz-boot-starters</name>
<description>ibiz-boot-starters</description>
<properties>
<revision>2.4.0-SNAPSHOT</revision>
</properties>
<modules>
<module>ibiz-boot-starter-parent</module>
<module>ibiz-boot-starter</module>
</modules>
</project>
\ No newline at end of file
Markdown 格式
0% or
您添加了 0 到此讨论。请谨慎行事。
先完成此消息的编辑!
想要评论请 注册