| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489 |
- package ss.db;
- import java.io.File;
- import java.lang.reflect.Field;
- import java.lang.reflect.InvocationTargetException;
- import java.lang.reflect.Method;
- import java.lang.reflect.Modifier;
- import java.sql.Date;
- import java.sql.Timestamp;
- import java.util.Arrays;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import java.util.Map.Entry;
- import java.util.Objects;
- import java.util.Optional;
- import java.util.Set;
- import java.util.concurrent.atomic.AtomicInteger;
- import java.util.stream.Collectors;
- import java.util.stream.Stream;
- import jakarta.servlet.http.HttpServletRequest;
- import com.google.gson.Gson;
- import com.google.gson.reflect.TypeToken;
- import org.apache.commons.io.FileUtils;
- import org.apache.commons.lang3.StringUtils;
- import org.apache.logging.log4j.LogManager;
- import org.apache.logging.log4j.Logger;
- import ss.AppB;
- import ss.AppC;
- import ss.cb.CbU;
- import ss.cmn.lambda.LambdaFunction;
- import ss.cms.CmsU;
- import ss.editor.EditorU;
- import ss.log.SsLog;
- import ss.obj.ObjB;
- import ss.obj.ObjC;
- import ss.obj.ObjFieldB;
- import ss.obj.ObjU;
- import ss.user.UserRunB;
- public class DbU {
- // 增加,去掉 e.printStackTrace(),改用 log4j -- 按 IDEA 规范。Lin
- private static final Logger log4j = LogManager.getLogger(DbU.class);
- /* 去掉,直接写。Lin
- private final static int blob_radix = 16;
- */
- /// **
- // * blob数据转换成string
- /// *
- // 来自 GeneralDBAccess.blobBytesToString(byte[] bytes)。Lin
- /* 再去掉,没用到了 -- 数据库不支持 Blob 字段了
- public static String toStrByBlob(byte[] bytes) {
- return new BigInteger(1, bytes).toString(16); // 16 = blob_radix
- }
- */
- // 来自 GeneralDBAccess.blobStringToBytes(String blobstr)。Lin
- /* 再去掉,没用到了 -- 数据库不支持 Blob 字段了
- public static byte[] toBlobByStr(String blobstr) {
- return new BigInteger(blobstr, 16).toByteArray(); // 16 = blob_radix
- }
- */
-
- // 来自 SqlParam.obtainParamSqlType()。Lin
- // 增加 type 入口参数,原放在 SqlParam 里 -- 可以直接访问 SqlParmB.type
- // type 数据类型从 String 改为 int
- /* 去掉,parmTypeMap 直接对着 stmt.setXxx() 了。Lin
- public static int toSqlType(int type){
- /// * 改为 switch,改为 int,改用常量。Lin
- // int sqlType = -1;
- //
- // if( "int".equals(type) ) // TYPE_INT.。Lin
- // sqlType = java.sql.Types.INTEGER;
- // else if( "float".equals(type) ) // TYPE_FLOAT.。Lin
- // sqlType = java.sql.Types.FLOAT;
- // else if( "char".equals(type) ) // TYPE_CHAR.。Lin
- // sqlType = java.sql.Types.CHAR;
- // else if( "time".equals(type) ) // TYPE_DATETIME.。Lin
- // sqlType = java.sql.Types.TIMESTAMP;
- // else if( "clob".equals(type) ) // TYPE_CLOB.。Lin
- // sqlType = java.sql.Types.CLOB;
- // else if( "blob".equals(type) ) // TYPE_BLOB.。Lin
- // sqlType = java.sql.Types.BLOB;
- //
- // return sqlType;
- /// *
- switch (type) {
- case DbC.PARM_intType:
- return java.sql.Types.INTEGER;
- case DbC.PARM_floatType:
- return java.sql.Types.FLOAT;
- case DbC.PARM_stringType:
- return java.sql.Types.CHAR;
- case DbC.PARM_timestampType:
- return java.sql.Types.TIMESTAMP;
- case DbC.PARM_clobType:
- return java.sql.Types.CLOB;
- case DbC.PARM_blobType:
- return java.sql.Types.BLOB;
- }
-
- return -1;
- }
- */
-
- // === Po
-
- // 根据 Po,得到字段的 Type。Lin
- // 源自 ViewObjectUtil.readBeanType(BaseBean bean, Field f)
- // 入口参数 ObjFieldB 改为 fieldName,并移到前面 -- 统一
- // 返回值 String 改为 int
- /* 再改为 (int) toFieldTypeByPoType(Integer poType)。Lin
- * 再增加,序列化 Po 信息到 "/pmsWs/<项目号>/_cache/po/<对象名>"(PoCacheB)
- * 加载 .obj.xml 时,之前读的是 PMS 自己的 Po,不是项目的 Po -- 原项目的 po.jar,无法读取 Po 的信息
- public static int rdFieldTypeByPo(String fieldName, Po bean) {
- */ public static int toFieldTypeByPoType(Integer poType) {
- /* 再改为 (int) toFieldTypeByPoType(Integer poType)。Lin
- if (bean == null)
- return ObjC.FIELD_txtType; // "text"。Lin
- Integer type = bean.fieldTypeMap.get(fieldName); // String。(f.name)。Lin
- if (type == null)
- return ObjC.FIELD_txtType; // "text"。Lin
- */
- if (poType == null)
- return ObjC.FIELD_txtType; // "text"。Lin
- /* Po 增加 int、Integer、long、double、float、Float。Lin
- * String 改为 int
- * 改为 switch
- if ("Timestamp".equals(type)) {
- return "time";
- } else if ("Long".equals(type)) {
- return "number";
- } else if ("Double".equals(type)) {
- return "number";
- }
- */
- /* 再改为 (int) toFieldTypeByPoType(Integer poType)。Lin
- *
- switch (type) {
- */ return switch (poType) {
- case DbC.PO_StringType, DbC.PO_txtType -> ObjC.FIELD_txtType; // "text"。Lin
- case DbC.PO_intType, DbC.PO_IntegerType, // "int"/"Integer"。Lin
- DbC.PO_longType, DbC.PO_LongType, // "long"/"Long"。Lin
- /* 再去掉 float/Float、double/Double,转换为整数 -- 需要时再增加 BigInteger。Lin
- case DbC.PO_floatType: // "float"。lin
- case DbC.PO_FloatType: // "Float"。lin
- case DbC.PO_doubleType: // "double"。Lin
- case DbC.PO_DoubleType: // "Double"。Lin
- */
- /// 再保留浮点,存在无需运算 或 无需精确运算的情况 -- 如,REM。Lin
- DbC.PO_floatType, DbC.PO_FloatType, // "float"/"Float"。Lin
- DbC.PO_doubleType, DbC.PO_DoubleType -> // "double"/"Double"。Lin
- ///
- ObjC.FIELD_numType; // "number"。Lin
- /// 再增加 DbC.PO_booleanType/DbC.PO_BooleanType。Lin
- case DbC.PO_booleanType, DbC.PO_BooleanType -> ObjC.FIELD_boolType;
- ///
- /// 再增加,保留 ObjC.FIELD_dateType -- 与 Po 一样,保留 DbC.PO_DateType。Lin
- case DbC.PO_DateType -> ObjC.FIELD_dateType;
- ///
- case DbC.PO_TimestampType -> // "Timestamp"。Lin
- ObjC.FIELD_timeType; // "time"。Lin
- default -> {
- SsLog.log("DbU.toFieldTypeByPoType(,DbC.Po_xxxType 少了判断:" + poType);
- yield ObjC.FIELD_txtType; // "text"。Lin
- }
- };
- }
-
-
- // 来自:。Lin
- // BeanUtils.(Map<String, Object>) beansToMap(BaseBean bean)
- // ZGZ:ReSearchService.(Map<String, Object>) beansToMap(BaseBean bean)
- // DesignOutSide.(Map<String, String>) beansToMap(BaseBean... bean)
- // GzwjRemoteService.(Map<String, String>) beansToMap(BaseBean bean)
- // BeanUtils.beansToMap( 有过滤 null 值的字段
- // ZGZ:ReSearchService.beansToMap( 没有过滤 null 值的字段
- // DesignOutSide.beansToMap( 支持多个 bean,且把 "csc" 字段里的键值对也拆开写入 Map -- "csc" 本身不写入
- // 有过滤 null 值的字段,csc 里的 null 也过滤
- // GzwjRemoteService.beansToMap( 有过滤 null 值的字段
- // 增加 filterNull -- 区分 BeanUtils.beansToMap()
- // contCsc 为 true 时,没意义
- // 增加 contCsc -- 区分 DesignOutSide.beansToMap()
- /* 再去掉 contCsc,不把 DesignOutSide.beansToMap() 纳入 -- 需要支持多个 bean。Lin
- public static Map<String, Object> toMapByPo(Po bean, boolean filterNull, boolean contCsc) {
- */ public static Map<String, Object> toMapByPo(Po bean, boolean filterNull) throws InvocationTargetException, NoSuchMethodException, IllegalAccessException {
- Map<String, Object> jmap = new HashMap<>();
- /* 再去掉 contCsc,不把 DesignOutSide.beansToMap() 纳入 -- 需要支持多个 bean。Lin
- Map<String, Object> m; // 增加。Lin
- */
- /* 去掉 bean.getFieldMap(),直接用 Po.fieldTypeMap。Lin
- * Map<String, String[]> 改为 Map<String, String>
- for(Map.Entry<String, String[]> entry : (Set<Entry<String, String[]>>)bean.getFieldMap().entrySet()){
- String fieldName = entry.getKey();
- */ for (String fieldName : bean.fieldTypeMap.keySet()) {
- Object v = rdFieldValByPo(fieldName, bean); // BeanUtils.getFieldValue()。Lin
- /* 再去掉 contCsc,不把 DesignOutSide.beansToMap() 纳入 -- 需要支持多个 bean。Lin
- if (contCsc && fieldName.equals(DbC.PO_cscName)) { // "csc"。Lin
- /// 来自 DesignOutSide.(Map<String, String>) beansToMap(BaseBean... bean)。Lin
- if (v == null)
- continue;
- /// * 去掉 DesignOutSide.parseJsonToMap(,用 StrJson.toObjectMap(。Lin
- // * 在这加 {}
- // Map<String, String> m = parseJsonToMap(v.toString());
- /// *
- /// * 再,不加左、右的 "{"、"}" -- 在数据库的 参数串 字段里加。Lin
- // m = StrJson.toObjectMap("{"+ v.toString() +"}");
- /// *
- m = StrJson.toObjectMap(v.toString());
- if (m != null) {
- jmap.putAll(m);
- }
- ///
- //filter null
- } else if (!filterNull || v != null) { // 不过滤 null 值的字段。Lin
- */ if (!filterNull || v != null) { // 不过滤 null 值的字段。Lin
- jmap.put(fieldName, v);
- }
- }
- return jmap;
- }
- // 来自 DesignOutSide.(Map<String, String>) beansToMap(BaseBean... bean)。Lin
- // cont = 包含
- public static Map<String, String> toMapByPoContCsc(Po... bean) throws InvocationTargetException, NoSuchMethodException, IllegalAccessException {
- Map<String, String> jmap = new HashMap<>();
- /* 参数串字段 和 非参数串字段,合并在同一套 for 循环里。Lin
- for (int i = 0; i < bean.length; i++) {
- for (Map.Entry<String, String[]> entry : (Set<Entry<String, String[]>>) bean[i]
- .getFieldMap().entrySet()) {
- String fieldName = entry.getKey();
- if ("csc".equals(fieldName))
- continue;
- Object v = BeanUtils.getFieldValue(fieldName, bean[i]);
- if (v == null)
- continue;
- jmap.put(fieldName, v.toString());
- }
- }
- for (int i = 0; i < bean.length; i++) {
- for (Map.Entry<String, String[]> entry : (Set<Entry<String, String[]>>) bean[i]
- .getFieldMap().entrySet()) {
- try {
- String fieldName = entry.getKey();
- if (!"csc".equals(fieldName))
- continue;
- Object v = BeanUtils.getFieldValue(fieldName, bean[i]);
- if (v == null)
- continue;
- Map<String, String> m = parseJsonToMap(v.toString());
- if (m != null) {
- m.putAll(jmap);
- jmap = m;
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- */
- Gson gson = new Gson(); // 增加,不重复 new。Lin
- Object v; // 增加,不重复定义。Lin
- Map<String, String> m; // 增加,不重复定义。Lin
- /* 再改,IDEA:'for' loop can be replaced with enhanced 'for'。Lin
- for (int i = 0; i < bean.length; i++) {
- /// * 去掉 bean.getFieldMap(),直接用 Po.fieldTypeMap。Lin
- // * Map<String, String[]> 改为 Map<String, String>
- // * 去掉 try {}
- // for (Map.Entry<String, String[]> entry : (Set<Entry<String, String[]>>) bean[i].getFieldMap().entrySet()) {
- // try {
- // String fieldName = entry.getKey();
- /// *
- for (String fieldName : bean[i].fieldTypeMap.keySet()) {
- v = rdFieldValByPo(fieldName, bean[i]); // BeanUtils.getFieldValue()。Lin
- */
- for (Po po : bean) {
- for (String fieldName : po.fieldTypeMap.keySet()) {
- v = rdFieldValByPo(fieldName, po); // BeanUtils.getFieldValue()。Lin
- if (DbC.FIELD_poCscName.equals(fieldName)) { // ("csc".。Lin
- if (v == null)
- continue;
- m = gson.fromJson(String.valueOf(v), new TypeToken<Map<String, String>>() {}.getType());
- if (m != null)
- jmap.putAll(m);
- } else {
- if (v == null)
- continue;
- jmap.put(fieldName, String.valueOf(v));
- }
- }
- }
- return jmap;
- }
-
- // 来自 ViewObjectUtil.getFieldValue(BaseBean bean, ViewObject viewobject, Field field)。Lin
- // 去掉 viewobject,没用到
- // field 改为 field.name,并移到第一个位置
- public static Object rdFieldValByPo(String name, Po bean) throws InvocationTargetException, NoSuchMethodException, IllegalAccessException {
- /* 用 StringUtils.capitalize()。Lin
- String methodName = "get" + name.replaceFirst(name.substring(0, 1), name.substring(0, 1).toUpperCase());
- */ String methodName = "get"+ StringUtils.capitalize(name);
- Method method;
- try {
- method = bean.getClass().getMethod(methodName);
- /* 再改,IDEA:Redundant array creation for calling varargs method。Lin
- return method.invoke(bean, new Object[0]);
- */ return method.invoke(bean);
- } catch (Exception e) {
- /* 改,去掉 e.printStackTrace(),改用 log4j -- 按 IDEA 规范。Lin
- e.printStackTrace();
- */ log4j.error("DbU.rdFieldValByPo( 异常", e);
- /* 再改,改为抛出错误。Lin
- return null;
- */ throw e;
- }
- }
- // 来自 tt.bpm.common.BpmUtil.(String) getFieldValue(Object bean, ViewObject viewobject, Field f)。Lin
- // bean(Object) 改为 Po,专门用于 Po -- 用到 Object 再弄
- // 去掉 viewobject,没用到 -- 只是用于显示错误信息
- // f(ObjFieldB) 移到最前面 -- 统一
- public static String rdFieldValByCbByPo(ObjFieldB f, Po bean) throws Exception {
- String name = f.name;
- String codebook = f.cbName; // .getCodebook()。Lin
- /* 用 StringUtils.capitalize()。Lin
- String methodName = "get" + name.replaceFirst(name.substring(0, 1), name.substring(0, 1).toUpperCase());
- */ String methodName = "get"+ StringUtils.capitalize(name);
- Method method;
- try {
- method = bean.getClass().getMethod(methodName);
- /* 再改,IDEA:Redundant array creation for calling varargs method。Lin
- Object o = method.invoke(bean, new Object[0]);
- */ Object o = method.invoke(bean);
- String result = ((o == null)? null : o.toString());
- // 翻译codebook
- if (StringUtils.isNotBlank(codebook)) { // StringUtil.notNull()。Lin
- /* 去掉应用名前缀,不支持多个应用。Lin
- String[] a = codebook.split("\\.");
- if (a.length != 2) {
- throw new Exception("codebook解析失败 >>object:" + viewobject.getName() + ",field:" + f.getName() + ",codebook:" + codebook);
- }
- String codebookName = a[1];
- String appName = a[0];
- */
- /* 再去掉,直接用 codebook -- IDEA:Local variable 'codebookName' is redundant。Lin
- String codebookName = codebook;
- */
- // "获得codebook的数据"
- /* 再改,码本翻译只取那条记录。Lin
- /// * 去掉 appName。Lin
- // Map<String, Map<String, Object>> codebookData = CodebookUtil.getDataByCodebookName(codebookName, appName, null, null);// 获得codebook的数据
- /// *
- /// * 再去掉,直接用 codebook -- IDEA:Local variable 'codebookName' is redundant。Lin
- // Map<String, Map<String, Object>> codebookData = CbU.rdOpt(codebookName, null, null);
- // // null = filter,null = request。Lin
- /// *
- Map<String, Map<String, Object>> codebookData = CbU.rdOpt(codebook, null, null);
- Map<String, Object> oneRowMap = codebookData.get(result);
- Entry<String, Object> en = DsU.rdMapByIdx(oneRowMap, 1); // CommonUtils.getMapValueByIndex()。Lin
- result = ((en == null)? "" : en.getValue().toString());
- */
- /* 再改,去掉 fieldName(String) -- 改返回结果为 Map<Integer, Map<String, String>>。Lin
- result = CbU.transCb(result, codebook, null);
- */ result = CbU.transCb(result, codebook);
- }
- return result;
- } catch (Exception e) {
- /* 改,去掉 e.printStackTrace(),改用 log4j -- 按 IDEA 规范。Lin
- e.printStackTrace();
- */ log4j.error("DbU.rdFieldValByCbByPo( 异常", e);
- throw e;
- }
- // return null;
- }
-
- /**
- * 设置字段值
- *
- * @param propertyName
- * 字段名
- * @param obj
- * 实例对象
- * @param value
- * 新的字段值
- */
- // 来自 tt.util.BeanUtils.setFieldValue(Object obj, String propertyName, Object value)。Lin
- // Object 改为 Po,专门用于 Po
- // obj(Po) 移到最后面 -- 统一
- public static void wrPoField(String propertyName, Object value, Po obj) {
- try {
- Class<?> clazz = obj.getClass();
- Field field = clazz.getDeclaredField(propertyName);
- field.setAccessible(true);
- if (value == null || StringUtils.isBlank(value.toString())) { // StringUtil.isNull()。Lin
- field.set(obj, null);
- } else {
- String className = field.getType().getName();
- /* 改为 switch。Lin
- if ("java.lang.Integer".equals(className)
- || "int".equals(className)) {
- field.set(obj, Integer.valueOf(value.toString()));
- } else if ("java.lang.Long".equals(className)
- || "long".equals(className)) {
- field.set(obj, Long.valueOf(value.toString()));
- } else if ("java.lang.Byte".equals(className)
- || "byte".equals(className)) {
- field.set(obj, Byte.valueOf(value.toString()));
- } else if ("java.lang.Short".equals(className)
- || "short".equals(className)) {
- field.set(obj, Short.valueOf(value.toString()));
- } else if ("java.lang.Float".equals(className)
- || "float".equals(className)) {
- field.set(obj, Float.valueOf(value.toString()));
- } else if ("java.lang.Double".equals(className)
- || "double".equals(className)) {
- field.set(obj, Double.valueOf(value.toString()));
- } else if ("java.lang.Boolean".equals(className)
- || "boolean".equals(className)) {
- field.set(obj, Boolean.valueOf(value.toString()));
- } else if ("java.lang.Character".equals(className)
- || "char".equals(className)) {
- field.set(obj, Character.valueOf(value.toString().charAt(0)));
- } else if ("java.sql.Timestamp".equals(className)) {
- field.set(obj,
- new Timestamp(CommonUtils.getDateFromStr(value.toString())
- .getTime()));
- } else {
- field.set(obj, value);
- }
- */
- switch (className) {
- case "java.lang.String":
- field.set(obj, value);
- return;
- /* 再重构 Po -- 非空字段使用 基本数据类型。Lin
- * 去掉 short/Short、char/Character、byte/Byte -- 不支持
- case "int":
- case "java.lang.Integer":
- field.set(obj, Integer.valueOf(value.toString()));
- return;
- case "long":
- case "java.lang.Long":
- field.set(obj, Long.valueOf(value.toString()));
- return;
- case "float":
- case "java.lang.Float":
- field.set(obj, Float.valueOf(value.toString()));
- return;
- case "double":
- case "java.lang.Double":
- field.set(obj, Double.valueOf(value.toString()));
- case "short":
- case "java.lang.Short":
- field.set(obj, Short.valueOf(value.toString()));
- return;
- case "char":
- case "java.lang.Character":
- //noinspection UnnecessaryBoxing
- field.set(obj, Character.valueOf(value.toString().charAt(0)));
- return;
- case "byte":
- case "java.lang.Byte":
- field.set(obj, Byte.valueOf(value.toString()));
- return;
- case "boolean":
- case "java.lang.Boolean":
- field.set(obj, Boolean.valueOf(value.toString()));
- return;
- */
- case "int":
- field.set(obj, Integer.parseInt(value.toString()));
- return;
- case "java.lang.Integer":
- field.set(obj, Integer.valueOf(value.toString()));
- return;
- case "long":
- field.set(obj, Long.parseLong(value.toString()));
- return;
- case "java.lang.Long":
- field.set(obj, Long.valueOf(value.toString()));
- return;
- case "float":
- field.set(obj, Float.parseFloat(value.toString()));
- return;
- case "java.lang.Float":
- field.set(obj, Float.valueOf(value.toString()));
- return;
- case "double":
- field.set(obj, Double.parseDouble(value.toString()));
- return;
- case "java.lang.Double":
- field.set(obj, Double.valueOf(value.toString()));
- return;
- case "boolean":
- field.set(obj, Boolean.parseBoolean(value.toString()));
- return;
- case "java.lang.Boolean":
- field.set(obj, Boolean.valueOf(value.toString()));
- return;
- /// 增加 java.sql.Date。Lin
- case "java.sql.Date":
- /* 再去掉 StrDate.toDate(,只用 java.sql.Date。Lin
- field.set(obj, StrDate.toDate(value.toString()));
- */ field.set(obj, Date.valueOf(value.toString()));
- return;
- ///
- case "java.sql.Timestamp":
- /* 再去掉 StrDate.toDate(,只用 java.sql.Date。Lin
- field.set(obj, new Timestamp(StrDate.toDate(value.toString()).getTime()));
- */ field.set(obj, Timestamp.valueOf(value.toString()));
- return;
- default:
- field.set(obj, value);
- // return; // 再去掉,IDEA:'return' is unnecessary as the last statement in a 'void' method。Lin
- }
- }
- } catch (Exception e) {
- throw new RuntimeException(e);
- }
- }
- /**
- * 增加创建人员, 管理人员,管理部门
- *
- * @param bb;
- * @param yh;
- * @param ispersonal;
- * @throws Exception;
- */
- // 来自 DbService.updateManangeId(BaseBean bb, WdYh yh, boolean ispersonal, GeneralDBAccess gdba)。Lin
- // 再从 DbServ.wrSsField( 移到这,并改名为 wrSsFieldByPo(
- // 再增加 fieldNameSet(Set<String>) 入口/出口参数,去掉 gdba(Db)
- // 去掉方法里的 gdba.doUpdPo(,归入上层的 gdba.doInsPo(、gdba.doUpdPo( 去做
- // 把调用 DbU.wrSsFieldByPo( 前移到上层的 .doInsPo(、.doUpdPo( 前面
- public static void wrSsFieldByPo(Po bb, Set<String> fieldNameSet, UserRunB yh, boolean ispersonal) throws Exception {
- if (yh == null)
- return;
- /* 再去掉 updatePropertyNames,改为 入口/出口参数 fieldNameSet。Lin
- Set<String> updatePropertyNames = new HashSet<>();
- */
- Long bmid = yh.bmid; // .getBmid()。Lin
- Long ryid = yh.ryid; // .getRyid()。Lin
- // 创建人员ID
- /* 改。Lin
- if (bb.getFieldMappingItem("cjryid") != null) {
- */ if (bb.fieldTypeMap.get("cjryid") != null) {
- Method targetMethod = bb.getClass().getDeclaredMethod("setCjryid", Long.class);
- targetMethod.setAccessible(true);
- targetMethod.invoke(bb, ryid);
- /* 再去掉 updatePropertyNames,改为 入口/出口参数 fieldNameSet。Lin
- updatePropertyNames.add("cjryid");
- */ fieldNameSet.add("cjryid");
- }
- // 管理人员
- /* 改。Lin
- if (bb.getFieldMappingItem("glryid") != null && ispersonal) {
- */ if (bb.fieldTypeMap.get("glryid") != null && ispersonal) {
- Method targetMethod = bb.getClass().getDeclaredMethod("setGlryid", Long.class);
- targetMethod.setAccessible(true);
- targetMethod.invoke(bb, ryid);
- /* 再去掉 updatePropertyNames,改为 入口/出口参数 fieldNameSet。Lin
- updatePropertyNames.add("glryid");
- */ fieldNameSet.add("glryid");
- }
- // 管理部门
- /* 改。Lin
- if (bb.getFieldMappingItem("glbmid") != null && ispersonal == false) {
- */ if (bb.fieldTypeMap.get("glbmid") != null && !ispersonal) {
- Method targetMethod = bb.getClass().getDeclaredMethod("setGlbmid", Long.class);
- targetMethod.setAccessible(true);
- targetMethod.invoke(bb, bmid);
- /* 再去掉 updatePropertyNames,改为 入口/出口参数 fieldNameSet。Lin
- updatePropertyNames.add("glbmid");
- */ fieldNameSet.add("glbmid");
- }
- /* 再去掉 gdba.doUpdPo(,归入上层的 gdba.doInsPo(、gdba.doUpdPo( 去做。Lin
- * 把调用 DbU.wrSsFieldByPo( 前移到上层的 .doInsPo(、.doUpdPo( 前面
- * 返回 fieldNameSet
- /// * 再改,IDEA:'updatePropertyNames.size() > 0' can be replaced with '!updatePropertyNames.isEmpty()'。Lin
- // if (updatePropertyNames.size() > 0)
- /// *
- if (!(updatePropertyNames.isEmpty()))
- gdba.doUpdPo(bb, updatePropertyNames); // .updateById()。Lin
- */
- }
-
- // 来自 pms-core:Reflects.(<T> boolean) copyProperties(T from, T to,
- // boolean copyAllIfNoName, String... names)。Lin
- // 去掉 copyAllIfNoName(boolean),共用 names(String[]) -- names 为 null 时,为所有字段
- public static <T> void copyPoFieldArr(T from, T to, String... names) {
- Objects.requireNonNull(from);
- Objects.requireNonNull(to);
- Class<?> clazz = from.getClass();
- /* 再去掉 并行 -- 有时不能 copy 完整 -- 时行时不行 -- 少了 .forEach(。Lin
- * 还是不用 .parallel() -- 并行线程的开销大,数组内容少时,性能不高
- return Optional.ofNullable(names)
- .filter(LambdaFunction.notEmptyArray()) // (wd.core.util.lambda.Functions.。Lin
- .map(Stream::of)
- .orElseGet(() -> {
- String[] arr;
- /// * 改,去掉 copyAllIfNoName,共用 names -- names 为 null 时,为所有字段。Lin
- // if (copyAllIfNoName) {
- // arr = getAllFieldName(clazz, true);
- // } else {
- // arr = EMPTY_ARRAY;
- // }
- /// *
- arr = rdPoFieldName(clazz, true);
- return Stream.of(arr);})
- .parallel()
- /// * 去掉 Fields,直接写。Lin
- // .map(cn -> Fields.create(clazz, cn))
- /// *
- .map(cn -> rdPoField(clazz, cn))
- .filter(LambdaFunction.notNull()) // (wd.core.util.lambda.Functions.。Lin
- /// * 去掉 Reflects 和 Fields,直接写成 copyOnePoField()。Lin
- // .filter(field -> field.copyValue(from, to))
- // .count() > 0;
- /// *
- /// * 再改,IDEA:Can be replaced with 'stream.anyMatch()'。Lin
- // .filter(field -> copyOnePoField(field, from, to))
- // .count() > 0;
- /// *
- .anyMatch(field -> copyPoField(field, from, to));
- */
- String[] t = Optional.ofNullable(names)
- .filter(LambdaFunction.notEmptyArray())
- .orElseGet(() -> {
- String[] arr;
- arr = rdPoFieldName(clazz, true);
- return arr;});
- Arrays.stream(t)
- .forEach(cn -> {
- Field f = rdPoField(clazz, cn);
- if (f != null)
- copyPoField(f, from, to);
- });
- }
- // 参考自 pms-core:Reflects.(Object) getFieldValue(Field field, Object object) 和
- // pms-core:Reflects.(boolean) setFieldValue(Field field, Object object, Object value)。Lin
- // 合并为 copy 功能
- public static boolean copyPoField(Field field, Object from, Object to) {
- Object value;
- /// 改自 Reflects.getFieldValue()。Lin
- boolean access = field.canAccess(from); // .isAccessible()。已过时,改用 .canAccess(from)。Lin
- field.setAccessible(true);
- try {
- value = field.get(from);
- } catch (IllegalArgumentException | IllegalAccessException e) {
- /* 改,去掉 e.printStackTrace(),改用 log4j -- 按 IDEA 规范。Lin
- e.printStackTrace();
- */ log4j.error("DbU.copyOnePoField() 异常", e);
- return false;
- } finally {
- field.setAccessible(access);
- }
- ///
- /// 改自 Reflects.setFieldValue()。Lin
- access = field.canAccess(to); // .isAccessible()。已过时,改用 .canAccess(to)。Lin
- field.setAccessible(true);
- try {
- field.set(to, value);
- return true;
- } catch (IllegalArgumentException | IllegalAccessException e) {
- /* 改。Lin
- printStackTrace(e);
- if (!debug) {
- System.out
- .println("Failed to set the field value. If you want to know what happened, set reflect.debug =true.Field:"
- + field);
- }
- */
- /* 改,去掉 e.printStackTrace(),改用 log4j -- 按 IDEA 规范。Lin
- e.printStackTrace();
- */ log4j.error("DbU.copyOnePoField() 异常", e);
- return false;
- } finally {
- field.setAccessible(access);
- }
- ///
- }
-
- // 来自 pms-core:Reflects.(String[]) getAllFieldName(Class<? extends Object> clazz, boolean notFinal)。Lin
- /* 再改,IDEA:Wildcard type argument '?' explicitly extends 'java.lang.Object'。Lin
- public static String[] rdPoFieldName(Class<? extends Object> clazz, boolean notFinal) {
- */ public static String[] rdPoFieldName(Class<?> clazz, boolean notFinal) {
- return Stream.of(clazz.getDeclaredFields()).parallel()
- .filter(f -> !(notFinal && Modifier.isFinal(f.getModifiers())))
- .map(Field::getName).distinct().toArray(String[]::new);
- }
-
- // 来自 pms-core:Reflects.(Field) getField(Class<?> clazz, String fieldName)。Lin
- public static Field rdPoField(Class<?> clazz, String fieldName) {
- try {
- return clazz.getDeclaredField(fieldName);
- } catch (NoSuchFieldException | SecurityException e) {
- /* 改,去掉 e.printStackTrace(),改用 log4j -- 按 IDEA 规范。Lin
- e.printStackTrace(); // printStackTrace(e)。Lin
- */ log4j.error("DbU.rdPoField() 异常", e);
- return null;
- }
- }
- // 来自 GeneralDBAccess.getGetMethodName(String fieldName)。Lin
- // 根据传入的fieldName返回getXX方法名
- /* 去掉,直接用 "get"+ StringUtils.capitalize(fieldName)。Lin
- public static String cmptGetField(String fieldName) {
- return "get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1, fieldName.length());
- }
- */
- // 把对像o在request中有传值的参数名,返回为一个集合
- // 来自 GeneralDBAccess.(Set<String>) propertyInRequest(Object o, HttpServletRequest request)。Lin
- // Object 改为 Po,专门给 Po 用
- // 不用 ClazzU.rdAtrArrByClazz(c),改为 c.getDeclaredFields() -- XxxPo 不能继承 XxxPo
- /* 再去掉,用 DbU.loadPoFieldByReq( 返回的 fieldNameSet(Set<String>)。Lin
- * DbU.rdPoFieldNameByReq( 并入 DbU.loadPoFieldByReq(
- public static Set<String> rdPoFieldNameByReq(Po o, HttpServletRequest request) {
- Set<String> properties = new HashSet<>();
- Set<String> paramNames = request.getParameterMap().keySet();
- Class<?> c = o.getClass();
- /// * 再把 ClazzU.rdAtrArrByClazz(c) 改为 c.getDeclaredFields()。Lin
- // Field[] fields = ClazzU.rdAtrArrByClazz(c); // 获得本类和父类的所有属性 // WebUtil.getAllFieldsFromClass()。Lin
- /// *
- Field[] fields = c.getDeclaredFields(); // 获得本类的所有属性
- for (Field field : fields) {
- String propertyName = field.getName();
- if (paramNames.contains(propertyName)) {
- properties.add(propertyName);
- }
- }
- return properties;
- }
- */
- // 来自 WDCore_pms_product:(List<String>) getParamNameFromRequest(HttpServletRequest request, ViewObject vo)。Lin
- // vo 移到前面 -- 统一
- /* 再去掉,并入 DbU.loadPoFieldByReq( -- DbU.rdPoFieldByReq( 改为 loadPoFieldByReq(,增加返回 fieldNameSet(Set<String>)。Lin
- public static List<String> rdObjFieldNameByReq(ObjB vo, HttpServletRequest request) {
- List<String> paramNameList = new ArrayList<>();
- Map<String, ?> map = request.getParameterMap();
- for (ObjFieldB f : vo.fieldMap.values()) { // .getFields()。Lin
- String fieldName = f.name;
- if (map.containsKey(fieldName)) {
- paramNameList.add(fieldName);
- }
- }
- return paramNameList;
- }
- */
- // ===
- // private static final Class<?>[] STR_CLS = {String.class}; // 字符串类数组 // 再去掉,没用到。Lin
- /**
- * 从request中获取参数,放到相应bean的属性内。名称与bean内属性名不一致的request参数将被忽略 前提:request中必须有一个参数
- * obj=...,用来指定对像的类名。对象类必须提供无参数的默认构造函数。
- *
- * @param request http请求的request
- * @param objClass 对象类的绝对路径
- * isAsString 是否将所有数据全部作为String类型看待,而不区分。 true:
- * 全部作为String,对应的对象类的所有对应属性也是String类型;<br>
- * false:
- * 区分整数(Long)、小数(Double)、日期(Timestamp)和字符串(String)类型,自动匹配。<br>
- * 如果前三个类型匹配不到,则自动作为String处理,如果都匹配不到,则放弃本参数。
- * @return Object 新创建的bean,保存了request里面的所有相应(与bean属性具有相同名字的)的param。
- * @throws Exception;
- */
- // 来自 WebUtil.(Object) paramsFromRequest(HttpServletRequest request, String objClassName, boolean isAsString)。Lin
- // objClassName 移到前面
- // 专门给 Po 用
- // 返回值改为 Po
- // 只获取本类的所有属性 -- XxxPo 不能继承
- // 再,objClassName 改为 clazz(Class<? extends Po>)。Lin
- // 再,clazz 改为 objClass -- IDEA:Local variable 'objClass' is redundant。Lin
- // 再考虑把 objClass.getDeclaredFields() 循环改为 Po.fieldTypeMap.keySet() 循环
- // 不能改,Set 方法的入口参数类型与 Po.fieldTypeMap 里的有差异
- /* 再去掉 isAsString(boolean),都是 false。Lin
- public static Po rdPoByReqField(Class<? extends Po> objClass, HttpServletRequest request, boolean isAsString) throws Exception {
- */
- /* 再改为 Load,增加返回 fieldNameSet(Set<String>) -- null = 不返回。Lin
- * 原返回值的 Po,改为入口/出口参数 obj(Po)
- public static Po rdPoFieldByReq(Class<? extends Po> objClass, HttpServletRequest request) throws Exception {
- */ public static boolean loadPoFieldByReq(Po obj, Set<String> fieldNameSet,
- Class<? extends Po> objClass, HttpServletRequest request) throws Exception {
- // 取得类名
- // String className = objPkgName + "." + request.getParameter( REQ_OBJ
- // );
- /* 再,objClassName 改为 po(Po)。Lin
- * objClassName 改为 clazz(Class<Po>)
- Class<?> objClass = Class.forName(objClassName);
- */
- /* 再去掉,IDEA:Local variable 'objClass' is redundant。Lin
- * 入口参数 clazz 改为 objClass
- Class<? extends Po> objClass = clazz;
- */
- // Field[] fields = objClass.getFields();//获得本类和父类的所有属性
- // Field[] fields = objClass.getDeclaredFields();//获得本类和父类的所有属性
- /* 再把 ClazzU.rdAtrArrByClazz(objClass) 改为 objClass.getDeclaredFields()。Lin
- Field[] fields = ClazzU.rdAtrArrByClazz(objClass);// 获得本类和父类的所有属性 // WebUtil.getAllFieldsFromClass()。Lin
- */ Field[] fields = objClass.getDeclaredFields();// 获得本类的所有属性
- // 创建类实例
- /// 再,objClassName 改为 po(Po)。Lin
- // 还需要 new,不能共用 ObjB.po
- /* 再去掉,改为 Load 了 -- 增加返回 fieldNameSet(Set<String>)。Lin
- * obj(Po),同时也是返回值
- Po obj = objClass.getDeclaredConstructor().newInstance(); // Object。Lin
- */
- ///
- Class<?>[] methodParams = new Class[1];
- Object[] realParams = new Object[1];
- Method method;
- // 从request传来的参数必定非null,除非request中不存在指定名称的参数。
- // 因此根据request中存在的参数名枚举来获取的参数,必定在request中存在,
- // 从而参数值只可能是空串("",在没有输入的时候),或者具体的值。
- String fieldName;
- String typeName;
- String[] paramValueArr;
- /* 再改,IDEA:'for' loop can be replaced with enhanced 'for'。Lin
- for (int i = 0; i < fields.length; i++) {
- String fieldName = fields[i].getName();
- */ for (Field field : fields) {
- fieldName = field.getName();
- typeName = field.getType().getName();
- paramValueArr = request.getParameterValues(fieldName);
- // 如果参数列表中无与对象名称一致的参数,则忽略
- /* 再重构 Po -- 非空字段使用 基本数据类型。Lin
- * 值为 "" 时,也不处理 -- 使用 Po 里的默认值 -- Request 返回 "",paramValueArr.length = 1
- if (paramValueArr == null || paramValueArr.length == 0)
- */
- /* 再改,对 String 字段,值为 "" 不能去掉。Lin
- * 变动时,删除 地址 的值 -- Request 返回 "",paramValueArr.length = 1
- * 对其他字段,如 int,需要去掉 -- 使用 Po 里的默认值
- * 需要改为默认值时,需要录入默认值
- if (paramValueArr == null || paramValueArr.length == 0 ||
- Arrays.stream(paramValueArr).allMatch(StringUtils::isBlank))
- */
- /* 再改,对下拉列表,值为 "" 不能去掉。Lin
- * 变动时,删除下拉列表的值 -- Request 返回 "",paramValueArr.length = 1
- * 下面的 ObjU.toValByType( 会把 "" 转为 null -- 字符串的 "",也会转为 null
- if (paramValueArr == null || paramValueArr.length == 0 ||
- (!"java.lang.String".equals(typeName) && Arrays.stream(paramValueArr).allMatch(StringUtils::isBlank)))
- */ if (paramValueArr == null || paramValueArr.length == 0)
- continue;
- // 排除重复的参数值
- /* 再去掉 paramValues(Set<String>),直接用 paramValueArr(String[])。Lin
- Set<String> paramValues = new HashSet<>();
- /// * 再改,IDEA:Manual array to collection copy 。Lin
- // for (String s : paramValueArr) {
- // paramValues.add(s);
- // }
- /// *
- Collections.addAll(paramValues, paramValueArr);
- */
- /* 再改,IDEA:String concatenation '+=' in loop。Lin
- * 改为 new StringBuilder(),不出现空指针
- String paramFinalValue = null;// 保存到数据库的参数终值
- */
- /* 再改回 String,多个值里再用 StringBuilder。Lin
- StringBuilder paramFinalValue = new StringBuilder();// 保存到数据库的参数终值
- */ String paramFinalValue;// 保存到数据库的参数终值
- /* 再去掉 paramValues(Set<String>),直接用 paramValueArr(String[])。Lin
- if (paramValues.size() == 1) {// 如果只有一个参数值
- /// * 再改,IDEA:String concatenation '+=' in loop。Lin
- // paramFinalValue = paramValues.iterator().next();
- /// *
- paramFinalValue = new StringBuilder(paramValues.iterator().next());
- */
- if (paramValueArr.length == 1) {// 如果只有一个参数值
- paramFinalValue = paramValueArr[0];
- } else {// 如果有多个参数值
- StringBuilder paramValue = new StringBuilder(); // 增加,多个值里再用 StringBuilder。Lin
- /* 再改,多个参数的值一致时,当成一个值。Lin
- boolean fst = true;
- */ String last = null; // 上一个参数值。Lin
- // 把多个值拼成用 "|" 分隔的串
- /* 再去掉 paramValues(Set<String>),直接用 paramValueArr(String[])。Lin
- for (String s : paramValues) {
- */ for (String s : paramValueArr) {
- if (StringUtils.isBlank(s)) // 去掉 StrU.isNull(),直接用 StringUtils.isBlank()。Lin
- continue;
- /* 再改,多个参数的值一致时,当成一个值。Lin
- if (fst)
- fst = false;
- */
- if (last == null) {
- last = s;
- /* 再去掉,IDEA:String concatenation '+=' in loop。Lin
- * new StringBuilder() 移到上面了
- paramFinalValue = "";
- */
- } else {
- /// 再增加,多个参数的值一致时,当成一个值。Lin
- if (s.equals(last))
- continue;
- ///
- /* 再改,IDEA:String concatenation '+=' in loop。Lin
- paramFinalValue += WebC.SEP_parm; // CoreConstVars.WD_DB_DATA_SEPARATOR。Lin
- */
- /* 再改,多个值里再用 StringBuilder。Lin
- * 改 "|"(DbC.SEP_parm) 为 ","(AppC.SEP_abbrJson) 了 -- 简写 Json 时的分隔符
- paramFinalValue.append(DbC.SEP_parm); // CoreConstVars.WD_DB_DATA_SEPARATOR。Lin
- */ paramValue.append(AppC.SEP_abbrJson); // CoreConstVars.WD_DB_DATA_SEPARATOR。Lin
- }
- /* 再改,IDEA:String concatenation '+=' in loop。Lin
- paramFinalValue += s;
- */
- /* 再改,多个值里再用 StringBuilder。Lin
- paramFinalValue.append(s);
- */ paramValue.append(s);
- }
- paramFinalValue = paramValue.toString(); // 增加,多个值里再用 StringBuilder。Lin
- }
- /* 再去掉 isAsString(boolean),都是 false。Lin
- if (isAsString) {
- methodParams = STR_CLS;
- /// * 再改,IDEA:'for' loop can be replaced with enhanced 'for'。Lin
- // realParams[0] = request.getParameter(fields[i].getName());
- /// *
- realParams[0] = request.getParameter(field.getName());
- } else {
- */
- // 对于具体类型的值,如果是空串,则可认为是NULL(字符串则为"")
- // 如果是具体的值,则按可转化类型
- /* 再改,IDEA:'for' loop can be replaced with enhanced 'for'。Lin
- typeName = fields[i].getType().getName();
- */
- /* 再去掉,上移,上面也要用到了。Lin
- typeName = field.getType().getName();
- */
- /* 再改,IDEA:String concatenation '+=' in loop。Lin
- realParams[0] = ObjU.toValByType(paramFinalValue, typeName); // checkParamType()。Lin
- */
- /* 再改,paramFinalValue 从 StringBuilder 改回 String。Lin
- realParams[0] = ObjU.toValByType(paramFinalValue.toString(), typeName); // checkParamType()。Lin
- */ realParams[0] = ObjU.toValByType(paramFinalValue, typeName); // checkParamType()。Lin
- /* 再改,IDEA:'for' loop can be replaced with enhanced 'for'。Lin
- methodParams[0] = fields[i].getType();
- */ methodParams[0] = field.getType();
- /* 再去掉 isAsString(boolean),都是 false。Lin
- }
- */
- try {
- /* 再改,IDEA:'for' loop can be replaced with enhanced 'for'。Lin
- method = objClass
- .getMethod("set" + fields[i].getName().replaceFirst(fields[i].getName().substring(0, 1),
- fields[i].getName().substring(0, 1).toUpperCase()), methodParams);
- */
- /* 再改用 StringUtils.capitalize(。Lin
- method = objClass
- .getMethod("set" + field.getName().replaceFirst(field.getName().substring(0, 1),
- field.getName().substring(0, 1).toUpperCase()), methodParams);
- */ method = objClass.getMethod("set"+ StringUtils.capitalize(field.getName()), methodParams);
- method.invoke(obj, realParams);
- } catch (NoSuchMethodException e) {
- /* 再去掉,IDEA:'continue' is unnecessary as the last statement in a loop。Lin
- * > 增加 log4j -- IDEA:Empty 'catch' block
- continue;
- */ log4j.error("DbU.rdPoByReqField( 异常", e);
- }
- if (fieldNameSet != null)
- fieldNameSet.add(fieldName); // 增加,增加返回 fieldNameSet(Set<String>)。Lin
- }
- /* 再改,改为 Load 了 -- 增加返回 fieldNameSet(Set<String>)。Lin
- return obj;
- */ return true;
- }
- // ===
- // 来自 ViewObjectUtil.findWholeBeanMap(ViewObject vo, HttpServletRequest request)。Lin
- public static Map<String, Object> rdPoMapByReq(ObjB vo, HttpServletRequest request) throws InvocationTargetException, NoSuchMethodException, IllegalAccessException {
- Map<String, Object> result = new HashMap<>();
- Po bean = rdPoByReq(vo, request); // ViewObjectUtil.findBeanFromRequest()
- if (bean != null)
- result = DbU.toMapByPo(bean, true); // tt.util.BeanUtils.beansToMap( -- 有过滤 null 值的字段。Lin
- // true = filterNull。Lin
- /* 再改,IDEA:'result.size() == 0' can be replaced with 'result.isEmpty()'。Lin
- if (result.size() == 0) {
- */ if (result.isEmpty()) {
- /* 再改,只取 对象ID,有问题再说 -- 解决 大海捞针 问题。Lin
- * 没有即返回空的 result
- for (String xxa : request.getParameterMap().keySet()) { // ???大海捞针啊!!!Lin
- /// * 再改,去掉 ObjC.REQUEST_PLUGIN_POSSIBLE,key= 只需要一个属性及一个值,用于找到 <page 定义。Lin
- // * 把 <page key= 分开为 key= 和 def= -- def= 其他的录入页面字段值,支持多个属性及一个值
- // if (vo.fieldMap.containsKey(xxa) || ObjC.REQUEST_PLUGIN_POSSIBLE.equals(xxa)) {
- // // ViewObjectUtil.REQUEST_PLUGIN_POSSIBLE
- /// *
- if (vo.fieldMap.containsKey(xxa))
- result.put(xxa, request.getParameter(xxa));
- }
- */
- String idStr = request.getParameter(vo.idName);
- if (StringUtils.isNotBlank(idStr))
- result.put(vo.idName, Long.parseLong(idStr));
- }
- return result;
- }
- // 来自 ViewObjectUtil.findBeanFromRequest(ViewObject viewobj, HttpServletRequest request)。Lin
- // 调整语句顺序,使清晰。Lin
- public static Po rdPoByReq(ObjB viewobj, HttpServletRequest request) {
- try {
- if (request == null
- /* 再,.poName 改为 .poClazz。Lin
- || StringUtils.isBlank(viewobj.poName)) // 去掉 StrU.isNull(),直接用 StringUtils.isBlank()。Lin
- */ || viewobj.poClazz == null)
- return null;
- /// * 再去掉,永远取不到了 -- Po 改为 "RyPo" 了,不是 "Ry"。Lin
- // * 再不能去掉,对于 学员,写成 "xy" 的 Po(值是 RyPo),不是 "RyPo"
- if (request.getAttribute(viewobj.name) != null)
- return (Po) request.getAttribute(viewobj.name);
- /// *
- /* 再改,直接用 viewobj.poClazz,不用转来转去。Lin
- /// * 再,.poName 改为 .poClazz。Lin
- // Po thisBean = (Po) Class.forName(viewobj.poName).getDeclaredConstructor().newInstance();
- /// *
- Po thisBean = viewobj.poClazz.getDeclaredConstructor().newInstance();
- /// * 再去掉结尾的 "Po" -- 原为 "Ry",改为了 "RyPo" -- 学员(虚拟对象) 的 .clazzName 为 "RyPo"(实体对象)。Lin
- // * 再去掉 thisBean.objName 了 -- 不能反过来指定对象名 -- 虚拟对象,反过来是 一对多 了
- // * 去掉 StrU.lowerFstChar(),直接用 StringUtils.uncapitalize()
- // String key = StrU.lowerFstChar(thisBean.getClass().getSimpleName()); // CommonUtils.lowerFirstChar()。Lin
- /// *
- /// * 再,不用改了,用回 wrPoByReq() -- 原,去掉 wrPoByReq() 时,用 Po.objName 做为 Request 的 Key -- 导致学员(虚拟对象)取不到 Po。Lin
- // * 再去掉 thisBean.objName 了 -- 不能反过来指定对象名 -- 虚拟对象,反过来是 一对多 了
- // String key = thisBean.getClass().getSimpleName();
- // if (key.endsWith("Po"))
- // key = key.substring(0, key.length() -2);
- // key = StringUtils.uncapitalize(key);
- /// *
- String key = StringUtils.uncapitalize(thisBean.getClass().getSimpleName());
- */ String key = viewobj.poClazz.getSimpleName();
- if (request.getAttribute(key) != null)
- return (Po) request.getAttribute(key);
- } catch (Exception e) {
- /* 改,去掉 e.printStackTrace(),改用 log4j -- 按 IDEA 规范。Lin
- e.printStackTrace();
- */ log4j.error("DbU.rdPoByReq( 异常", e);
- }
- return null;
- }
- // 增加。Lin
- // "XxxPo" 里的 "xxx"
- public static String rdPurePoNameByClazz(Class<? extends Po> poClazz) {
- String poName = poClazz.getSimpleName();
- return poName.substring(0, poName.length() -2).toLowerCase();
- }
-
- /**
- * 用对象名设置bean
- * @param vo;
- * @param bean;
- * @param request;
- * @return String
- */
- // 来自 BeanUtils.putRequestBean(ViewObject vo, BaseBean bean,HttpServletRequest request)
- // 去掉 vo(ViewObject),Key 直接用 Po.objName(对象名)
- // 也不需要 lowerFirstChar() 了,不用类名
- /* 去掉,直接写 request.setAttribute(bean.objName, bean)。Lin
- * 返回值用 bean.objName
- * 再用回,不能去掉。Lin
- * 不能去掉 vo(ViewObject),即不能直接用 Po.objName(对象名) -- 学员(虚拟对象) 的 Po 是人员(实体对象),但学员的页面用学员(不是人员)
- * 再去掉 bb.objName 了 -- 不能反过来指定对象名 -- 虚拟对象,反过来是 一对多 了
- */
- public static String wrPoByReq(ObjB vo, Po bean, HttpServletRequest request) {
- /* 改了。Lin
- String key = null;
- if(vo!=null){
- key=vo.getName();
- }else{//兼容处理
- key=CommonUtils.lowerFirstChar(bean.getClass().getSimpleName());
- }
- */
- /* 再用回,不能用 bean.objName 做为 Key -- 学员(虚拟对象)的页面用学员,而不是人员。Lin
- * 再去掉 bean.objName 了,不能反过来指定对象名 -- 虚拟对象,反过来是 一对多
- String key = bean.objName;
- */
- String key;
- if (vo != null) {
- key = vo.name;
- } else {//兼容处理
- /* 去掉 CommonUtils.lowerFirstChar(),直接用 StringUtils.uncapitalize()。Lin
- key=CommonUtils.lowerFirstChar(bean.getClass().getSimpleName());
- */
- /* 再改为表名 -- Po 的类名是 <对象名>Po 了,如 RyPo。Lin
- key = StringUtils.uncapitalize(bean.getClass().getSimpleName());
- */ key = bean.tabName;
- }
- request.setAttribute(key, bean);
- return key;
- }
- // 增加。Lin
- /* 再去掉,没用到了 -- ObjB.poName 改为 .poClazz。Lin
- public static Po newPoClazz(String clazzName) throws Exception {
- Po po = null;
- if (StringUtils.isNotBlank(clazzName)) { // 去掉 StrU.notNull(),直接用 StringUtils.isNotBlank()。Lin
- try {
- po = (Po)Class.forName(clazzName).getDeclaredConstructor().newInstance();
- } catch (ClassNotFoundException e) {
- SsLog.print("classNotFound:" + clazzName);
- }
- }
-
- return po;
- }
- */
- /**
- * 如果html field为空,设置该字段为null
- *
- * @param bean;
- * @param vo;
- */
- // 来自 ViewObjectUtil.(BaseBean) filterHTMLBlankField(BaseBean bean, ViewObject vo)。Lin
- // 去掉返回值,写在入口参数 bean 里
- public static void clearBlankHtmlField(Po bean, ObjB vo) throws Exception {
- if (bean == null || vo == null)
- return; // return bean;。去掉返回值
- /* 去掉 .fields(List<Field>),共用 fieldMap(Map<String, ObjFieldB>)。Lin
- for (Field f : vo.getFields()) {
- */
- /* 再改,增加 ObjB.htmlFieldList 了。Lin
- for (ObjFieldB f : vo.fieldMap.values()) { // 逐一比较,以找出 HTML 的字段,大海捞针啊???Lin
- */ for (ObjFieldB f : vo.htmlFieldSet) {
- try {
- /* type 从 String 改为 int。Lin
- if (Field.TYPE_HTML.equals(f.getType())) {
- */
- /* 再去掉 if,增加 ObjB.cmsFieldList 了。Lin
- if (f.type == ObjC.FIELD_htmlType) {
- */
- /* tt.util.BeanUtils.getFieldValue() 归入了 ObjU.rdClazzAtr(),但这里用 DbU.rdPoField() -- bean 是 Po。Lin
- Object v = BeanUtils.getFieldValue(f.getName(), bean);
- */ Object v = DbU.rdFieldValByPo(f.name, bean);
- /* 改,去掉 appName。Lin
- if (StringUtil.isNull(getFieldDisplayValue(vo.getWebapp().getName(), f, v))) {
- */
- /* 再把 value(Object) 移到最前面。Lin
- if (StringUtils.isBlank(ObjU.makeFieldPlayDesc(f, v))) {
- */ if (StringUtils.isBlank(ObjU.toPlayDescByFieldVal(v, f))) {
- SsLog.print("III,clearBlankHtmlField(,blankContent:"+ v);
- /* tt.util.BeanUtils.setFieldValue()。Lin
- * Object 改为 Po,专门用于 Po
- * bean(Po) 移到最后面 -- 统一
- BeanUtils.setFieldValue(bean, f.getName(), null);
- */ wrPoField(f.name, null, bean); // null = value。Lin
- }
- // } // 再去掉 if,增加 ObjB.cmsFieldList 了。Lin
- } catch (Exception e) {
- /* 改,去掉 e.printStackTrace(),改用 log4j -- 按 IDEA 规范。Lin
- e.printStackTrace();
- */ log4j.error("DbU.clearBlankHtmlField( 异常", e);
- throw e; // 再增加,改为抛出错误。Lin
- }
- }
- /* 再去掉,IDEA:'return' is unnecessary as the last statement in a 'void' method。Lin
- return; // return bean;。去掉返回值
- */
- }
- /**
- * 没有上传 CMS 内容时,删除 内容 表记录并设置 cms 字段为 null
- *
- * @param bean;
- * @param vo;
- */
- // 抄自 clearBlankHtmlField。Lin
- /* 再改,增加处理 -- 有上传时,补写 内容.对象ID 值。Lin
- public static void clearBlankCmsField(Po bean, ObjB vo, Db gdba) {
- */ public static void fixCmsField(Po bean, ObjB vo, Db gdba) {
- if (bean == null || vo == null)
- return; // return bean;。去掉返回值
- for (ObjFieldB f : vo.cmsFieldSet) {
- try {
- Object v = DbU.rdFieldValByPo(f.name, bean);
- if (v == null) // 没有点击过 附件 按钮。Lin
- continue;
- long nrid = Long.parseLong(String.valueOf(v));
- if (CmsU.isBlankDgnFile("T-"+ nrid)) {
- SsLog.print("III,fixCmsField(,内容ID:"+ v);
- /* 再改,用 ObjC.JLZT_inEditStr,不直接用 ObjC.JLZT_inEdit。Lin
- SqlCmd cmd = new SqlCmd("delete from NR where NRID=:nrid and JLZTM="+ ObjC.JLZT_inEdit);
- */ SqlCmd cmd = new SqlCmd("delete from NR where NRID=:nrid and "+ ObjC.JLZT_inEditStr);
- cmd.wrLongParm("nrid", nrid); // .setInt()。Lin
- gdba.doModByC(cmd); // .modify()。Lin
- wrPoField(f.name, null, bean); // null = value。Lin
- /// 再增加,删除 CMS 文件。Lin
- File nrFolder = new File(AppB.appRoot + CmsU.cmptBaseDir("T-"+ nrid));
- FileUtils.deleteDirectory(nrFolder);
- ///
- }
- /// 再增加,有上传时,补写 内容.对象ID 值。Lin
- else {
- /* 再改,用 ObjC.JLZT_inEditStr,不直接用 ObjC.JLZT_inEdit。Lin
- SqlCmd cmd = new SqlCmd("update NR set DXID=:dxid where NRID=:nrid and JLZTM="+ ObjC.JLZT_inEdit);
- */ SqlCmd cmd = new SqlCmd("update NR set DXID=:dxid where NRID=:nrid and "+ ObjC.JLZT_inEditStr);
- cmd.wrLongParm("dxid", bean.rdIdVal());
- cmd.wrLongParm("nrid", nrid);
- gdba.doModByC(cmd); // .modify()。Lin
- }
- ///
- } catch (Exception e) {
- log4j.error("DbU.fixCmsField( 异常", e);
- }
- }
- }
- // 来自 tt.bpm.common.BpmUtil.(Set<String>) updateNameSet(BaseBean bean, String... columnNames)。Lin
- /* 再去掉 DbU.combFieldNameByPo(,直接用 Set.of( -- 空值的字段,也写!--有问题再说???Lin
- public static Set<String> combFieldNameByPo(Po bean, String... columnNames) {
- Set<String> rs = new TreeSet<>();
- for (String name : columnNames) {
- Object v = DbU.rdFieldValByPo(name, bean); // tt.util.BeanUtils.getFieldValue()。Lin
- if (v != null &&
- StringUtils.isNotBlank(v.toString())) { // v.toString().length() > 0。Lin
- rs.add(name);
- }
- }
- // Collections.addAll(rs, columnNames);
- return rs;
- }
- */
- // 来自 tt.bpm.common.BpmUtil.(Set<String>) nameSet(String... columnNames)。Lin
- // 另,只有一个字段名时,直接用 Collections.singleton(。Lin
- /* 再去掉,直接用 Set.of(。Lin
- public static Set<String> combFieldName(String... columnNames) {
- /// * 用新的写法。Lin
- // Set<String> rs = new TreeSet<>();
- // for (String name : columnNames) {
- // rs.add(name);
- // }
- // return rs;
- /// *
- /// * 再改,IDEA:Lin
- // * Local variable 'rs' is redundant
- // * Redundant 'distinct()' call: elements will be distinct anyway when collected to a Set
- // Set<String> rs = Arrays.stream(columnNames).distinct().collect(Collectors.toSet());
- // return rs;
- /// *
- return Arrays.stream(columnNames).collect(Collectors.toSet());
- }
- */
- // 来自 ViewObjectUtil.(Map) _compareObject(int type, ViewObject vo, BaseBean oldObj, BaseBean newObj,
- // List<String> paramNameList)。Lin
- // "比较oldObj和newObj,返回两个对象中的不同字段集合Map"
- // fieldNameList 前移到 type 后面
- /* 再改 fieldNameList(List<String>) 为 fieldNameSet(Set<String>)。Lin
- * DbU.rdObjFieldNameByReq( 并入 DbU.loadPoFieldByReq( -- DbU.rdPoFieldByReq( 改为 loadPoFieldByReq(,增加返回 fieldNameSet(Set<String>)
- public static Map<String, Object> cmpField(int type, List<String> fieldNameList, ObjB vo, Po oldObj, Po newObj) {
- */
- /* 再改为 cmpChgField(,专门用于比较变动时的改动字段 -- 抽取出共用程序,与 JrgwServ 共用。Lin
- * 去掉 type(int),只用到 type = 1
- * 1,返回 Arrays.asList(oValue, nValue);2 返回 newValue
- * ViewObjectUtil.(Map<String, Object>) compareFields(ViewObject vo, BaseBean oldObj, BaseBean newObj, List<String> paramNameList) 没用到
- * "比较oldObj和newObj,返回两个对象中的不同字段集合Map"
- * return (Map<String, Object>) _compareObject(2, vo, oldObj, newObj, paramNameList);
- * 返回值 Map<String, Object> 改为 Map<String, List<String>>
- * 纳入 BpmServ.saveChgField( 里的形成 wdObject(Map<String, List<String>>) 的程序
- * 把入口参数反过来,调整为 newObj(Po)、oldObj(Po)、vo(ObjB)、fieldNameSet(Set<String>)
- public static Map<String, Object> cmpField(int type, Set<String> fieldNameSet, ObjB vo, Po oldObj, Po newObj) {
- */ public static Map<String, List<String>> cmpChgField(Po newObj, Po oldObj, ObjB vo, Set<String> fieldNameSet) throws InvocationTargetException, NoSuchMethodException, IllegalAccessException {
- // ViewObject vo = findViewObject();
- Map<String, Object> result = new HashMap<>();
- /* 改为用 fieldNameList 来 for,提高性能。Lin
- for (Field f : vo.getFields()) {
- if (paramNameList == null) {
- } else if (!paramNameList.contains(f.getName())) {
- continue;
- }
- if (vo.getObjectIdField().equals(f))
- continue;
- */
- for (String fieldName : fieldNameSet) {
- ObjFieldB f = vo.fieldMap.get(fieldName);
- // 比较oldObj和newObj的区别
- /* tt.util.BeanUtils.getFieldValue() 归入了 ObjU.rdClazzAtr(),但这里用 DbU.rdPoField() -- bean 是 Po。Lin
- Object oldValue = BeanUtils.getFieldValue(f.getName(), oldObj);
- Object newValue = BeanUtils.getFieldValue(f.getName(), newObj);
- */
- Object oldValue = DbU.rdFieldValByPo(f.name, oldObj);
- Object newValue = DbU.rdFieldValByPo(f.name, newObj);
-
- // 空字符""
- /* 再改,IDEA:'equals("")' can be replaced with 'isEmpty()'。Lin
- if (oldValue != null && String.valueOf(oldValue).equals(""))
- */ if (oldValue != null && String.valueOf(oldValue).isEmpty())
- oldValue = null;
- /* 再改,IDEA:'equals("")' can be replaced with 'isEmpty()'。Lin
- if (newValue != null && String.valueOf(newValue).equals(""))
- */ if (newValue != null && String.valueOf(newValue).isEmpty())
- newValue = null;
- // 空值
- if (oldValue == null && newValue == null)
- continue;
- /* 改为 int 了。Lin
- if (Field.TYPE_HTML.equals(f.getType())) {
- */ if (f.type == ObjC.FIELD_htmlType) {
- if (EditorU.cmpEditorFile((String) oldValue, (String) newValue)) // ViewObjectUtil.compareUeditorFile()。Lin
- continue;
- /* 改为 int 了。Lin
- } else if (Field.TYPE_CMS.equals(f.getType())) {
- */ } else if (f.type == ObjC.FIELD_cmsType) {
- if (newValue == null ||
- /* 再改,IDEA:'CmsU.rdChgList(newValue.toString(), null).size() == 0' can be replaced with
- * 'CmsU.rdChgList(newValue.toString(), null).isEmpty()'。Lin
- CmsU.rdChgList(newValue.toString(), null).size() == 0) // NrService.getNrChangeList()。Lin
- */ CmsU.rdChgList(newValue.toString(), null).isEmpty()) // NrService.getNrChangeList()。Lin
- continue;
- }
- /* 改为 int 了。Lin
- if (Field.TYPE_CMS.equals(f.getType()) == false) {
- */ if (f.type != ObjC.FIELD_cmsType) {
- if (oldValue != null && oldValue.equals(newValue)) {
- continue;
- } else if (newValue != null && newValue.equals(oldValue)) {
- continue;
- }
- }
- /* 去掉,去掉 type(int),只用到 type = 1。Lin
- if (type == 1) {
- */
- String oValue = (oldValue == null)? null : oldValue.toString();
- String nValue = (newValue == null)? null : newValue.toString();
- result.put(f.name, Arrays.asList(oValue, nValue));
- /* 去掉,去掉 type(int),只用到 type = 1。Lin
- } else if (type == 2) {
- result.put(f.name, newValue);
- }
- */
- }
- /* 改,纳入 BpmServ.saveChgField( 里的形成 wdObject(Map<String, List<String>>) 的程序。Lin
- return result; // (Map) result。Lin
- */
- /// 来自 BpmServ.saveChgField( 里的形成 wdObject(Map<String, List<String>>) 的程序。Lin
- Map<String, List<String>> wdObject = new HashMap<>();
- /* 改。Lin
- for (Map.Entry<String, List<String>> entry : compareResult.entrySet()) {
- */ for (Entry<String, Object> entry : result.entrySet()) {
- String fieldName = entry.getKey();
- ObjFieldB f = vo.fieldMap.get(fieldName); // .getFieldsMap().。Lin
- if (f == null)
- continue;
- /* 改为 Boolean 了。Lin
- if ("true".equals(f.getEditable())) {
- */ if (f.editable) {
- /* 在这,Object 转回 List<String>。Lin
- wdObject.put(fieldName, entry.getValue());
- */
- //noinspection unchecked
- wdObject.put(fieldName, (List<String>) entry.getValue());
- }
- }
- return wdObject; // 增加,改为新的返回值。Lin
- ///
- }
- // 来自 HDDev_scgl:WJSqlcBuilder.(String)
- // insql(String prefix, SqlCommandImpl sqlc, Stream<?> stream, String sqlPrefix)。Lin
- // sqlPrefix 改为 inStr
- // 顺序调整为 sqlc、inStr、stream、prefix
- public static String makeInSql(SqlCmd sqlc, String inStr, Stream<?> stream, String prefix) {
- AtomicInteger ai = new AtomicInteger();
- return stream.map(o -> {
- int paramIndex = ai.incrementAndGet();
- String paramName = prefix + paramIndex;
- sqlc.wrStrParm(paramName, o); // .setString(。Lin
- return paramName;
- }).map(":"::concat).collect(Collectors.joining(",", inStr +"(", ")"));
- }
- }
|