use of org.sagacity.sqltoy.model.IgnoreKeyCaseMap in project sagacity-sqltoy by chenrenfei.
the class MacroUtilsTest method main.
public static void main(String[] args) {
String macroStr = "@substr(${corpId},0,2)-SE@substr(@day(yyMMdd),0,4)";
IgnoreKeyCaseMap keyValues = new IgnoreKeyCaseMap();
keyValues.put("corpId", "HX02");
keyValues.put("bizDate", DateUtil.parseString("2020-02-12"));
System.err.println(MacroUtils.replaceMacros(macroStr, keyValues));
}
use of org.sagacity.sqltoy.model.IgnoreKeyCaseMap in project sagacity-sqltoy by chenrenfei.
the class RedisIdGenerator method getId.
/*
* (non-Javadoc)
*
* @see org.sagacity.sqltoy.plugins.id.IdGenerator#getId(java.lang.String,
* java.lang.String, java.lang.Object[], int)
*/
@Override
public Object getId(String tableName, String signature, String[] relatedColumns, Object[] relatedColValue, Date bizDate, String idJavaType, int length, int sequencSize) {
String key = (signature == null ? "" : signature);
// 主键生成依赖业务的相关字段值
IgnoreKeyCaseMap<String, Object> keyValueMap = new IgnoreKeyCaseMap<String, Object>();
if (relatedColumns != null && relatedColumns.length > 0) {
for (int i = 0; i < relatedColumns.length; i++) {
keyValueMap.put(relatedColumns[i], relatedColValue[i]);
}
}
// 替换signature中的@df() 和@case()等宏表达式
String realKey = MacroUtils.replaceMacros(key, keyValueMap);
// 没有宏
if (realKey.equals(key)) {
// 长度够放下6位日期 或没有设置长度且流水长度小于6,则默认增加一个6位日期作为前置
if ((length <= 0 && sequencSize < 6) || (length - realKey.length() > 6)) {
Date realBizDate = (bizDate == null ? new Date() : bizDate);
realKey = realKey.concat(DateUtil.formatDate(realBizDate, (dateFormat == null) ? "yyMMdd" : dateFormat));
}
}
// 参数替换
if (!keyValueMap.isEmpty()) {
realKey = MacroUtils.replaceParams(realKey, keyValueMap);
}
// 结合redis计数取末尾几位顺序数
Long result;
// update 2019-1-24 key命名策略改为SQLTOY_GL_ID:tableName:xxx 便于redis检索
if (tableName != null) {
result = generateId(realKey.equals("") ? tableName : tableName.concat(":").concat(realKey));
} else {
result = generateId(realKey);
}
return realKey.concat(StringUtil.addLeftZero2Len("" + result, (sequencSize > 0) ? sequencSize : length - realKey.length()));
}
use of org.sagacity.sqltoy.model.IgnoreKeyCaseMap in project sagacity-sqltoy by chenrenfei.
the class BeanUtil method reflectBeansToList.
/**
* @todo 利用java.lang.reflect并结合页面的property, 从对象中取出对应方法的值,组成一个List
* @param datas
* @param properties
* @param reflectPropsHandler
* @return
* @throws Exception
*/
public static List reflectBeansToList(List datas, String[] properties, ReflectPropsHandler reflectPropsHandler) throws RuntimeException {
if (null == datas || datas.isEmpty() || null == properties || properties.length < 1) {
return null;
}
List resultList = new ArrayList();
try {
Object rowObject = null;
int methodLength = properties.length;
// 判断是否存在属性值处理反调
boolean hasHandler = (reflectPropsHandler != null) ? true : false;
// 存在反调,则将对象的属性和属性所在的顺序放入hashMap中,便于后面反调中通过属性调用
if (hasHandler) {
HashMap<String, Integer> propertyIndexMap = new HashMap<String, Integer>();
for (int i = 0; i < methodLength; i++) {
propertyIndexMap.put(properties[i].toLowerCase(), i);
}
reflectPropsHandler.setPropertyIndexMap(propertyIndexMap);
}
// 判断是否有级联
boolean hasInnerClass = false;
for (String prop : properties) {
if (prop.contains(".")) {
hasInnerClass = true;
break;
}
}
// 级联含子对象模式(属性名称:staff.name 模式)
if (hasInnerClass) {
Object[] rowAry;
for (int i = 0, n = datas.size(); i < n; i++) {
rowObject = datas.get(i);
if (rowObject != null) {
List rowList = new ArrayList();
rowAry = reflectBeanToAry(rowObject, properties, null, reflectPropsHandler);
for (Object cell : rowAry) {
rowList.add(cell);
}
resultList.add(rowList);
}
}
return resultList;
}
// 非级联模式
Method[] realMethods = null;
boolean inited = false;
Object[] params = new Object[] {};
// 增加map类型支持
boolean isMap = false;
if (datas.get(0) != null) {
Class valueClass = datas.get(0).getClass();
if (valueClass.equals(HashMap.class) || valueClass.equals(ConcurrentHashMap.class) || valueClass.equals(Map.class) || valueClass.equals(ConcurrentMap.class) || HashMap.class.equals(valueClass) || LinkedHashMap.class.equals(valueClass) || ConcurrentHashMap.class.equals(valueClass) || Map.class.equals(valueClass)) {
isMap = true;
}
}
Iterator iter;
String fieldLow;
Map.Entry<String, Object> entry;
Map rowMap;
for (int i = 0, n = datas.size(); i < n; i++) {
rowObject = datas.get(i);
if (null != rowObject) {
List dataList = new ArrayList();
// 2021-10-09 支持map类型
if (isMap) {
if (rowObject instanceof IgnoreKeyCaseMap) {
rowMap = (IgnoreKeyCaseMap) rowObject;
for (int j = 0; j < methodLength; j++) {
dataList.add(rowMap.get(properties[j]));
}
} else {
rowMap = (Map) rowObject;
// 考虑key大小写兼容
for (int j = 0; j < methodLength; j++) {
fieldLow = properties[j].toLowerCase();
iter = rowMap.entrySet().iterator();
while (iter.hasNext()) {
entry = (Map.Entry<String, Object>) iter.next();
if (entry.getKey().toLowerCase().equals(fieldLow)) {
dataList.add(entry.getValue());
break;
}
}
}
}
} else {
// 第一行数据
if (!inited) {
realMethods = matchGetMethods(rowObject.getClass(), properties);
inited = true;
}
for (int j = 0; j < methodLength; j++) {
if (realMethods[j] != null) {
dataList.add(realMethods[j].invoke(rowObject, params));
} else {
dataList.add(null);
}
}
}
// 反调对数据值进行加工处理
if (hasHandler) {
reflectPropsHandler.setRowIndex(i);
reflectPropsHandler.setRowList(dataList);
reflectPropsHandler.process();
resultList.add(reflectPropsHandler.getRowList());
} else {
resultList.add(dataList);
}
} else {
if (logger.isDebugEnabled()) {
logger.debug("BeanUtil.reflectBeansToList 方法,第:{}行数据为null,如果是sql查询请检查写法是否正确!", i);
} else {
err.println("BeanUtil.reflectBeansToList 方法,第:{" + i + "}行数据为null,如果是sql查询请检查写法是否正确!");
}
resultList.add(null);
}
}
} catch (Exception e) {
logger.error("反射Java Bean获取数据组装List集合异常!{}", e.getMessage());
e.printStackTrace();
throw new RuntimeException("反射Java Bean获取数据组装List集合异常!" + e.getMessage());
}
return resultList;
}
Aggregations