package com.vci.web.util;
|
|
import com.alibaba.fastjson.JSONObject;
|
import com.alibaba.fastjson.serializer.SerializerFeature;
|
import com.vci.client.bof.ClientBusinessObject;
|
import com.vci.client.bof.ClientLinkObject;
|
import com.vci.client.common.objects.UserEntityObject;
|
import com.vci.common.exception.VciException;
|
import com.vci.corba.common.PLException;
|
import com.vci.corba.common.data.UserEntityInfo;
|
import com.vci.corba.omd.data.AttributeValue;
|
import com.vci.corba.omd.data.BusinessObject;
|
import com.vci.corba.omd.data.LinkObject;
|
import com.vci.starter.revision.bo.TreeWrapperOptions;
|
import com.vci.starter.web.annotation.*;
|
import com.vci.starter.web.constant.QueryOptionConstant;
|
import com.vci.starter.web.enumpck.BooleanEnum;
|
import com.vci.starter.web.enumpck.VciFieldTypeEnum;
|
import com.vci.starter.web.exception.VciBaseException;
|
import com.vci.starter.web.pagemodel.SessionInfo;
|
import com.vci.starter.web.pagemodel.Tree;
|
import com.vci.starter.web.toolmodel.DateConverter;
|
import com.vci.starter.web.util.*;
|
import com.vci.starter.web.wrapper.VciQueryWrapperForDO;
|
import com.vci.web.pageModel.*;
|
import com.vci.web.service.impl.WebLoServiceImpl;
|
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
|
import net.sf.jsqlparser.statement.select.PlainSelect;
|
import net.sf.jsqlparser.statement.select.SelectItem;
|
import org.apache.commons.lang3.StringUtils;
|
import org.slf4j.Logger;
|
import org.slf4j.LoggerFactory;
|
import org.springframework.stereotype.Component;
|
import org.springframework.util.CollectionUtils;
|
|
import javax.servlet.http.HttpServletRequest;
|
import java.beans.IntrospectionException;
|
import java.beans.PropertyDescriptor;
|
import java.lang.reflect.*;
|
import java.math.BigDecimal;
|
import java.net.InetAddress;
|
import java.net.NetworkInterface;
|
import java.sql.Timestamp;
|
import java.util.*;
|
import java.util.function.Function;
|
import java.util.stream.Collectors;
|
|
/**
|
* 通用工具类
|
* @author weidy
|
* @date 2021-2-13
|
*/
|
@Component
|
public class WebUtil extends VciBaseUtil {
|
|
/**
|
* 全部标记,常用语树形展示时
|
*/
|
public static final String ALL = "${all}";
|
|
/**
|
* 是否持久化
|
*/
|
private static ThreadLocal<String> needPersistenceInThread = new ThreadLocal<>();
|
/**
|
* 日志对象
|
*/
|
private static Logger logger = LoggerFactory.getLogger(WebUtil.class);
|
|
/**
|
* 获取错误信息
|
* @param e 异常对象
|
* @return 错误信息
|
*/
|
public static String getErrorMsg(Throwable e){
|
String errContent = "";
|
try {
|
if (e == null) {
|
return errContent;
|
}
|
if (e instanceof PLException ) {
|
// String error_code = ((VCIError) e).error_code;
|
// String[] error_message = ((VCIError) e).error_message;
|
// return MessageUtils.get(error_code, error_message);
|
return null;
|
} else if(e instanceof VciBaseException || e.getClass().getSuperclass().equals(VciBaseException.class)){
|
return LangBaseUtil.getErrorMsg(e);
|
}else if (e instanceof VciException){
|
String error_code = ((VciException) e).getCode();
|
Object[] error_message = ((VciException) e).getObjArray();
|
return MessageUtils.get(error_code, error_message);
|
}else if (e.getCause() != null
|
&& !e.getCause().toString().equals("")) {
|
errContent = e.getCause().toString();
|
} else if (e.getLocalizedMessage() != null
|
&& !e.getLocalizedMessage().equals("")) {
|
errContent = e.getLocalizedMessage();
|
} else if (e.getMessage() != null && !e.getMessage().equals("")) {
|
errContent = e.getMessage();
|
} else {
|
errContent = e.toString();
|
}
|
} catch (Exception e1) {
|
return LangBaseUtil.getErrorMsg(e1);
|
}
|
return errContent;
|
}
|
|
/**
|
* 获取当前用户的用户名
|
* @return 用户名
|
*/
|
public static String getCurrentUserId( ){
|
SessionInfo s = getCurrentUserSessionInfoNotException();
|
if(s !=null){
|
return s.getUserId();
|
}else{
|
return "";
|
}
|
}
|
|
|
/**
|
* 获取当前用户的主键
|
* @return 主键
|
*/
|
public static String getCurrentUserOid( ){
|
SessionInfo s = getCurrentUserSessionInfoNotException();
|
if(s !=null){
|
return s.getUserId();
|
}else{
|
return "";
|
}
|
}
|
|
/**
|
* json字符串转为对象
|
* @param jsonString json的字符串
|
* @param beanClass bean的类
|
* @return bean
|
*/
|
public static <T> T jsonString2JavaBean(String jsonString,Class<T> beanClass){
|
return (T)JSONObject.parseObject(jsonString, beanClass);
|
}
|
|
/**
|
* 从json字符串中获取第一个对象
|
* @param jsonString json的字符串
|
* @param beanClass Bean的类
|
* @return 从列表里获取第一个对象
|
*/
|
public static <T> T getFirstObjectFromJson(String jsonString,Class<T> beanClass){
|
if(!isNull(jsonString)) {
|
return JSONObject.parseArray(jsonString, beanClass).get(0);
|
} else {
|
return null;
|
}
|
}
|
|
/**
|
* 获取sessionInfo对象
|
* @return 获取当前用户的信息
|
*/
|
public static SessionInfo getSessionInfo(){
|
try{
|
return getCurrentUserSessionInfoNotException();
|
}catch(Exception e){
|
return null;
|
}
|
}
|
|
/**
|
* 获取当前线程中的用户对象
|
* @return 当前用户信息
|
* @throws VciBaseException 没有登录会抛出异常
|
*/
|
public static SessionInfo getCurrentUserSessionInfo() throws VciBaseException{
|
SessionInfo si= getCurrentUserSessionInfoNotException();
|
if(si==null){
|
throw new VciBaseException("noLogin",new String[]{"没有当前用户信息"});
|
}
|
return si;
|
}
|
|
/**
|
* 获取当前线程中的用户对象
|
* @return 用户对象,但是不抛出异常
|
*/
|
public static SessionInfo getCurrentUserSessionInfoNotException() {
|
return WebThreadLocalUtil.getCurrentUserSessionInfoInThread();
|
}
|
|
/**
|
* 设置用户的会话信息对象
|
* @param sessionInfo 会话信息
|
*/
|
public static synchronized void setSessionInfo(SessionInfo sessionInfo){
|
WebThreadLocalUtil.setCurrentUserSessionInfoInThread(sessionInfo);
|
}
|
|
/**
|
* 是否设置了corba需要使用的上下文信息,只针对webService的
|
*/
|
private static volatile boolean isSetContext = false;
|
|
/**
|
* 获取IP地址,通过request
|
* @param request 请求的对象
|
* @return ip地址
|
*/
|
public static String getClientInfo(HttpServletRequest request){
|
String ip = request.getHeader("X-Forwarded-For");
|
if (StringUtils.isBlank(ip) || "unknown".equalsIgnoreCase(ip)) {
|
ip = request.getHeader("Proxy-Client-IP");
|
}
|
if (StringUtils.isBlank(ip) || "unknown".equalsIgnoreCase(ip)) {
|
ip = request.getHeader("WL-Proxy-Client-IP");
|
}
|
if (StringUtils.isBlank(ip) || "unknown".equalsIgnoreCase(ip)) {
|
ip = request.getHeader("HTTP_CLIENT_IP");
|
}
|
if (StringUtils.isBlank(ip) || "unknown".equalsIgnoreCase(ip)) {
|
ip = request.getHeader("HTTP_X_FORWARDED_FOR");
|
}
|
if (StringUtils.isBlank(ip) || "unknown".equalsIgnoreCase(ip)) {
|
ip = request.getRemoteAddr();
|
}
|
if (StringUtils.isBlank(ip) || ip.indexOf("0:0:0:0:0:0:0:1") >-1) {//0:0:0:0:0:0:0:1是本机在访问
|
ip = "127.0.0.1";
|
}
|
return ip;
|
}
|
|
/**
|
* 获取corba要的用户对象
|
* @param model 当前模块
|
* @return 用户对象
|
*/
|
public static UserEntityInfo getUserEntityInfo(String model){
|
SessionInfo si = WebUtil.getCurrentUserSessionInfo();
|
UserEntityInfo ueo = new UserEntityInfo();
|
// ueo.ip = si.getIp();
|
ueo.modules = model;
|
ueo.userName = si.getUserId();
|
return ueo;
|
}
|
|
/**
|
* 获取corba要的用户对象
|
* @param model 模块
|
* @return 用户对象
|
*/
|
public static UserEntityObject getUserEntityObject(String model) {
|
UserEntityObject entityObject = new UserEntityObject();
|
SessionInfo si = WebUtil.getCurrentUserSessionInfo();
|
// entityObject.setIp(si.getIp());
|
entityObject.setModules(model);
|
entityObject.setUserName(si.getUserId());
|
return entityObject;
|
}
|
|
|
/**
|
* 是否持久化
|
* @return 默认为true
|
*/
|
public static boolean isPersistence(){
|
String persistence =needPersistenceInThread.get();
|
if(StringUtils.isNotBlank(persistence) && BooleanEnum.FASLE.getValue().equalsIgnoreCase(persistence)){
|
return false;
|
}else{
|
return true;
|
}
|
}
|
|
/**
|
* 设置是否持久化
|
* @param isPersistence 是否持久化
|
*/
|
public static void setPersistence(boolean isPersistence){
|
if(!isPersistence){
|
needPersistenceInThread.set("false");
|
}else{
|
needPersistenceInThread.set("");
|
}
|
}
|
|
|
/**
|
* 将对象转换为字符串出来
|
* @return json字符串
|
*/
|
public static String getJSONStringWithDateFormat(Object obj){
|
return JSONObject.toJSONStringWithDateFormat(obj, VciDateUtil.DateTimeMillFormat, SerializerFeature.WriteDateUseDateFormat);
|
}
|
|
/**
|
* 最新转换为map
|
* @param o 对象
|
* @return map
|
*/
|
public static Map<String,Object> objectToMap(Object o){
|
Map<String,Object> map = new HashMap<String,Object>();
|
if(o!=null) {
|
String jsonString = JSONObject.toJSONStringWithDateFormat(o, VciDateUtil.DateTimeMillFormat, SerializerFeature.WriteDateUseDateFormat);
|
if(StringUtils.isNotBlank(jsonString)) {
|
JSONObject jsonObject = JSONObject.parseObject(jsonString);
|
if(jsonObject!=null){
|
for(String key : jsonObject.keySet()){
|
map.put(key,jsonObject.get(key));
|
}
|
}
|
}
|
}
|
return map;
|
}
|
|
/**
|
* 对象转换为map
|
* @param o 对象
|
* @return map
|
*/
|
public static Map<String,String> objectToMapString(Object o){
|
Map<String,String> map = new HashMap<String,String>();
|
if(o!=null) {
|
String jsonString = JSONObject.toJSONStringWithDateFormat(o, VciDateUtil.DateTimeMillFormat, SerializerFeature.WriteDateUseDateFormat);
|
if(StringUtils.isNotBlank(jsonString)) {
|
JSONObject jsonObject = JSONObject.parseObject(jsonString);
|
if(jsonObject!=null){
|
for(String key : jsonObject.keySet()){
|
map.put(key,jsonObject.getString(key));
|
}
|
}
|
}
|
}
|
return map;
|
}
|
|
/**
|
* 判断某个属性是否为空
|
* @param obj 对象
|
* @param f 字段
|
* @return true是不空
|
*/
|
public static boolean isNotNullForField(Object obj,Field f){
|
if(!"serialVersionUID".equalsIgnoreCase(f.getName()) &&!"DEFAULT_INITIAL_CAPACITY".equalsIgnoreCase(f.getName())&&null!=obj && !WebUtil.isNullOrNullString(obj.toString())) {
|
return true;
|
} else {
|
return false;
|
}
|
}
|
|
/**
|
* 数组转换为字符串
|
* @param array 数组
|
* @return 字符串
|
*/
|
public static String array2String(String[] array) {
|
if(null == array || array.length == 0) {
|
return "";
|
}else{
|
return Arrays.stream(array).collect(Collectors.joining(","));
|
}
|
}
|
|
/**
|
* 对象转换为字符串
|
* @param obj 对象
|
* @return 字符串
|
*/
|
public static String getString(Object obj){
|
if(obj == null) {
|
return "";
|
}
|
if(obj instanceof Date) {
|
return VciDateUtil.date2Str((Date) obj, VciDateUtil.DateTimeFormat);
|
}
|
return String.valueOf(obj);
|
}
|
|
/**
|
* 拷贝map里的值
|
* @param source 源
|
* @param target 目标
|
* @param copyField 拷贝的属性
|
*/
|
public static void copyValueForMap(Map source,Map target,String[] copyField){
|
Map<String,String> copyFieldMap = new HashMap<String,String>();
|
for(String field : copyField) {
|
copyFieldMap.put(field, field);
|
}
|
copyValueForMap(source,target,copyFieldMap);
|
}
|
/**
|
* 为map拷贝值
|
* @param source 源
|
* @param target 目标
|
* @param copyField key为目标对象里的字段,
|
*/
|
public static void copyValueForMap(Map source,Map target,Map<String,String> copyField){
|
try{
|
Iterator<String> it = copyField.keySet().iterator();
|
while(it.hasNext()){
|
String field = it.next();
|
target.put(field, source.get(copyField.get(field)));
|
}
|
}catch(Exception e){
|
if(logger.isErrorEnabled()){
|
logger.error("拷贝值到map",e);
|
}
|
}
|
}
|
|
/**
|
* 将字符串转换为时间对象
|
* @param text 字符串
|
* @return 时间对象
|
*/
|
public static Date readText2Date(String text){
|
DateConverter dateConverter = new DateConverter();
|
dateConverter.setAsText(text);
|
return dateConverter.getValue();
|
}
|
|
/**
|
* 从Map里获取double类型
|
* @param field 字段名称
|
* @param record map
|
* @return 属性
|
*/
|
public static Double getDoubleFromMap(String field,
|
Map<String, Object> record) {
|
if(WebUtil.isNullOrNullString(field) || record == null || !record.containsKey(field)) {
|
return null;
|
}else{
|
Object v = record.get(field);
|
if(v instanceof BigDecimal){
|
return ((BigDecimal)v).doubleValue();
|
}else if(v instanceof Double){
|
return ((Double)v).doubleValue();
|
}else{
|
return WebUtil.getDouble((String)v);
|
}
|
}
|
}
|
|
/**
|
* 获取不是空值的映射,且key是小写
|
* @param map 映射
|
* @return 去除空值的
|
*/
|
public static Map getNotNullMap(Map map){
|
if(map == null){
|
return new HashMap();
|
}
|
Iterator it = map.keySet().iterator();
|
Map unNullMap = new HashMap();
|
while(it.hasNext()){
|
Object key = it.next();
|
String newKey = key.toString().toLowerCase();
|
Object value = map.get(key);
|
if(value !=null){
|
if(value instanceof String && WebUtil.isNotNull(value.toString())){
|
unNullMap.put(newKey, value);
|
}else if(!(value instanceof String)){
|
unNullMap.put(newKey, value);
|
}
|
}
|
}
|
return unNullMap;
|
}
|
|
/**
|
* 获取参照的属性
|
* @param c 对象
|
* @return 参照属性
|
*/
|
public static Map<String,String> getReferAttrName(Class c){
|
Map<String,String> fieldMap = new HashMap<String, String>();
|
List<Field> allField = getAllFieldForObj(c);
|
if(allField!=null&&allField.size()>0){
|
for(Field field : allField){
|
if (field.isAnnotationPresent(Transient.class)){
|
//有参照或者枚举
|
String referColumn = ((Transient)field.getAnnotation(Transient.class)).referColumn();
|
if(isNotNull(referColumn)){
|
fieldMap.put(referColumn.toLowerCase(), field.getName());
|
}
|
}
|
}
|
}
|
return fieldMap;
|
}
|
|
/**
|
* 获取参照的业务类型
|
* @param c 对象
|
* @return 参照的业务类型
|
*/
|
public static List<String> getReferBoAttrName(Class c){
|
return getReferBoAttrName(c,false);
|
}
|
|
/**
|
* 获取参照的业务类型
|
* @param c 对象
|
* @param isNotHasEnum 是否包含枚举
|
* @return 参照的业务类型
|
*/
|
public static List<String> getReferBoAttrName(Class c,boolean isNotHasEnum){
|
List<String> fieldMap = new ArrayList<String>();
|
List<Field> allField = getAllFieldForObj(c);
|
if(allField!=null&&allField.size()>0){
|
for(Field field : allField){
|
if (field.isAnnotationPresent(Transient.class)){
|
//有参照或者枚举
|
String referColumn = ((Transient)field.getAnnotation(Transient.class)).referColumn();
|
if(isNotNull(referColumn) &&
|
(!isNotHasEnum || (isNotHasEnum && referColumn.indexOf(".")>-1))){
|
fieldMap.add(referColumn.toLowerCase());
|
}
|
}
|
}
|
}
|
return fieldMap;
|
}
|
|
/**
|
* 获取对象与业务类型里的属性映射
|
* @param c 对象所属类
|
* @return Map<String,String> 业务类型的字段:对象上的属性
|
*/
|
public static Map<String/*业务类型中的字段*/,String/*对象上的属性*/> getFieldNameMap(Class c){
|
Map<String,String> fieldMap = new HashMap<String, String>();
|
List<Field> allField = getAllFieldForObj(c);
|
if(allField!=null&&allField.size()>0){
|
for(Field field : allField){
|
if(!field.getName().equals("serialVersionUID")){
|
if(!field.isAnnotationPresent(Transient.class) ){
|
String clientBoAttrName = getCboAttrNameFromField(field,c);
|
fieldMap.put(clientBoAttrName, field.getName());
|
if(field.isAnnotationPresent(VciUseEnum.class)){
|
//这个是枚举,需要
|
VciUseEnum vciUseEnum = field.getAnnotation(VciUseEnum.class);
|
fieldMap.put(vciUseEnum.value() + "_" + clientBoAttrName,vciUseEnum.showTextField());
|
}
|
}else if (field.isAnnotationPresent(Transient.class)){
|
//说明不是持久化的属性,但是从平台中查询出来后可能得需要显示
|
String referColumn = ((Transient)field.getAnnotation(Transient.class)).referColumn();
|
if(isNotNull(referColumn)){
|
fieldMap.put(referColumn, field.getName());
|
}
|
}else{
|
//子表的不能这样取
|
}
|
}
|
}
|
}
|
if(!CollectionUtils.isEmpty(fieldMap)){
|
//看看有没有null
|
Map<String,String> fieldMapNotNull = new HashMap<>();
|
fieldMap.forEach((key,value)->{
|
if(value!=null){
|
fieldMapNotNull.put(key,value);
|
}
|
});
|
return fieldMapNotNull;
|
}
|
return fieldMap;
|
}
|
|
/**
|
* 获取集合的元素类型
|
* @param field 属性
|
* @return 元素类型, 不是集合的时候返回Null
|
*/
|
public static Class getCollectionElementClass(Field field) throws VciBaseException{
|
Class fieldClass = null;
|
if(field == null){
|
return null;
|
}
|
if(field.getType().isAssignableFrom(List.class) ||
|
field.getType().isAssignableFrom(Set.class) ||
|
field.getType().isAssignableFrom(Vector.class)){
|
Type fc = field.getGenericType();
|
if(fc instanceof ParameterizedType){
|
ParameterizedType pt = (ParameterizedType)fc;
|
fieldClass= (Class)pt.getActualTypeArguments()[0];
|
}
|
}
|
return fieldClass;
|
}
|
|
/**
|
* 获取字段在业务类型中的名称
|
* @param field 字段
|
* @param c 业务类型
|
* @return 业务类型中的名称
|
*/
|
public static String getCboAttrNameFromField(Field field,Class c){
|
String clientBoAttrName = field.getName().toLowerCase();
|
boolean isFindColumn = false;
|
if(field.isAnnotationPresent(Column.class)){
|
String name = ((Column)field.getAnnotation(Column.class)).name();
|
if(isNotNull(name)){
|
clientBoAttrName = name.toLowerCase();
|
}
|
isFindColumn = true;
|
}
|
if(!isFindColumn){
|
//找一下Get方法上..set方法上肯定是不支持的,
|
Method getMethod = getGetmethod(c, field);
|
if(getMethod!=null && !getMethod.isAnnotationPresent(Transient.class)){
|
if(getMethod.isAnnotationPresent(Column.class)){
|
String name = ((Column)getMethod.getAnnotation(Column.class)).name();
|
if(isNotNull(name)){
|
clientBoAttrName = name.toLowerCase();
|
}
|
}
|
}else if(getMethod!=null && getMethod.isAnnotationPresent(Transient.class)){
|
//说明不是持久化的属性,但是从平台中查询出来后可能得需要显示
|
String referColumn = ((Transient)field.getAnnotation(Transient.class)).referColumn();
|
if(isNotNull(referColumn)){
|
clientBoAttrName = referColumn;
|
}
|
}
|
}
|
return VciQueryWrapperForDO.BASE_MODEL_COMPATIBILITY_MAP.getOrDefault(clientBoAttrName.toLowerCase(Locale.ROOT),clientBoAttrName);
|
}
|
|
/**
|
* 获取对象中的所有属性,包括其继承的属性
|
* @param c 对象
|
* @return 所有的属性
|
*/
|
public static List<Field> getAllFieldForObj(Class c){
|
List<Field> allField = new ArrayList<Field>();
|
for(Class<?> classz = c ; classz != Object.class ; classz = classz.getSuperclass() ){
|
Field[] thisClassField = classz.getDeclaredFields();
|
for(Field field : thisClassField){
|
if(!field.getName().equals("serialVersionUID")){
|
if(!allField.contains(field)){
|
allField.add(field);
|
}
|
}
|
}
|
}
|
return allField;
|
}
|
|
/**
|
* 获取对象的主键字段
|
* @param c 对象所属的类
|
* @return 主键的对象
|
*/
|
public static Field getPkFieldForObj(Class c){
|
List<Field> allField = getAllFieldForObj(c);
|
if(allField!=null&&allField.size()>0){
|
for(Field field : allField){
|
if(field.isAnnotationPresent(Id.class)){
|
return field;
|
}
|
}
|
//如果没找到,那就找oid
|
for(Field field : allField){
|
if(field.getName().toLowerCase().equalsIgnoreCase("oid")){
|
return field;
|
}
|
}
|
}
|
return null;
|
}
|
|
/**
|
* 获取ts的字段
|
* @param c 对象所属的类
|
* @return 时间戳的对象
|
*/
|
public static Field getTsField(Class c){
|
List<Field> allField = getAllFieldForObj(c);
|
if(allField!=null&&allField.size()>0){
|
for(Field field : allField){
|
if(field.getName().equals("ts")){
|
return field;
|
}
|
}
|
}
|
return null;
|
}
|
|
/**
|
* 根据名称获取字段
|
* @param field 字段的名称
|
* @param obj 对象
|
* @return 字段对象
|
*/
|
public static Field getFieldForObject(String field,Object obj){
|
if(obj == null){
|
return null;
|
}
|
return getFieldForObject(field,obj.getClass());
|
}
|
|
/**
|
* 根据名称获取字段
|
* @param fieldName 字段的名称
|
* @param c 对象类型
|
* @return 字段对象
|
*/
|
public static Field getFieldForObject(String fieldName,Class c){
|
List<Field> allField = getAllFieldForObj(c);
|
if(allField!=null&&allField.size()>0){
|
for(Field field : allField){
|
if(field.getName().toLowerCase().equalsIgnoreCase(fieldName.toLowerCase())){
|
return field;
|
}
|
}
|
}
|
return null;
|
}
|
|
/**
|
* 获取连接类型的主键信息
|
* @param s 字符串
|
* @return 连接类型
|
*/
|
public static LinkTypeInfoVO getLinkTypeAnnotation(String s){
|
if(s.indexOf("\"") > -1
|
&& s.indexOf(":") > -1
|
&& s.indexOf("}") > -1){
|
return JSONObject.parseObject(s, LinkTypeInfoVO.class);
|
}
|
return null;
|
}
|
|
/**
|
* 获取字段的setter
|
* @param c 对象类
|
* @param field 字段
|
* @return 方法
|
*/
|
public static Method getSetmethod(Class c,Field field){
|
return getSetmethod(c,field.getName());
|
}
|
|
/**
|
* 获取字段的setter
|
* @param c 对象类型
|
* @param fieldName 字段名称
|
* @return 方法
|
*/
|
public static Method getSetmethod(Class c,String fieldName){
|
if(c!=null&&isNotNull(fieldName)){
|
try {
|
PropertyDescriptor pd = new PropertyDescriptor(fieldName, c);
|
return pd.getWriteMethod();
|
} catch (SecurityException e) {
|
} catch (IntrospectionException e) {
|
}
|
}
|
return null;
|
}
|
|
/**
|
* 获取字段的getter
|
* @param c 对象类型
|
* @param fieldName 字段名称
|
* @return 方法
|
*/
|
public static Method getGetmethod(Class c,String fieldName){
|
if(c!=null&&isNotNull(fieldName)){
|
try {
|
PropertyDescriptor pd = new PropertyDescriptor(fieldName, c);
|
return pd.getReadMethod();
|
} catch (SecurityException e) {
|
} catch (IntrospectionException e) {
|
}
|
}
|
return null;
|
}
|
|
/**
|
* 获取字段的getter
|
* @param c 对象类型
|
* @param field 字段
|
* @return 方法
|
*/
|
public static Method getGetmethod(Class c,Field field){
|
return getGetmethod(c,field.getName());
|
}
|
|
/**
|
* 获取对象转换字符串
|
* @param obj 对象
|
* @return 字符串
|
*/
|
public static String getStringValueFromObject(Object obj){
|
if(obj == null){
|
return "";
|
}else{
|
if(obj instanceof Integer || obj instanceof Float || obj instanceof Long || obj instanceof Double){
|
return String.valueOf(obj);
|
}else if(obj instanceof Date){
|
return VciDateUtil.date2Str((Date)obj, VciDateUtil.DateTimeMillFormat);
|
}else{
|
return obj.toString();
|
}
|
}
|
}
|
|
/**
|
* 将平台返回的属性值赋值到对象上
|
* @param fieldName 属性名
|
* @param obj 对象
|
* @param value 属性值
|
*/
|
public static void setValueForFieldFromCbo(String fieldName,Object obj,String value){
|
try{
|
Field field = getFieldForObject(fieldName,obj);
|
if(field!=null){
|
field.setAccessible(true);
|
Method setMethod = getSetmethod(field.getDeclaringClass(), fieldName);
|
if(setMethod ==null) {
|
logger.error(obj.getClass().getName() + "中属性" + fieldName + "没有setter方法");
|
}
|
Class type = field.getType();
|
//从平台读取到的值不会为null,为空时为"";所以不处理空值
|
if(type.equals(int.class) || type.equals(Integer.class)){
|
int valueObj = getInt(value);
|
if(setMethod !=null) {
|
setMethod.invoke(obj,valueObj);
|
}else{
|
field.set(obj,valueObj);
|
}
|
}else if(type.equals(float.class) || type.equals(Float.class)){
|
float valueObj = getFloat(value);
|
if(setMethod !=null) {
|
setMethod.invoke(obj, valueObj);
|
}else{
|
field.set(obj,valueObj);
|
}
|
}else if(type.equals(long.class) || type.equals(Long.class)){
|
long valueObj = getLong(value);
|
if(setMethod !=null) {
|
setMethod.invoke(obj, valueObj);
|
}else{
|
field.set(obj,valueObj);
|
}
|
}else if(type.equals(Double.class) || type.equals(double.class)){
|
double valueObj = getDouble(value);
|
if(setMethod !=null) {
|
setMethod.invoke(obj, valueObj);
|
}else{
|
field.set(obj,valueObj);
|
}
|
//从平台中查询出来就不用处理精度了,因为平台会自行处理
|
}else if(type.equals(Short.class) || type.equals(short.class)){
|
short valueObject = getShort(value);
|
if(setMethod !=null) {
|
setMethod.invoke(obj, valueObject);
|
}else{
|
field.set(obj,valueObject);
|
}
|
}else if(type.equals(Date.class)){
|
//平台实际存储的都是Timestamp,不论是日期,日期时间,都是。如果仅是时间,需要用字符串来存储。
|
Date d = null;
|
if(StringUtils.isNotBlank(value)){
|
try {
|
if (value.indexOf("-") > -1 && value.indexOf(".") > -1 && value.indexOf(" ") > -1 && value.substring(value.lastIndexOf(".") + 1).length() == 9) {
|
//2013-4-19.14.5. 45. 734000000 这种格式,这个在使用SQL语句直接查询出时间字段的时候就会显示成这样
|
String ymd = value.substring(0, value.indexOf("."));
|
value = value.substring(value.indexOf(".") + 1);
|
if (value.indexOf(".") > -1) {
|
String hms = value.substring(0, value.lastIndexOf("."));
|
String nano = value.substring(value.lastIndexOf(".") + 1).trim();
|
hms = hms.replace(".", ":").replace(" ", "");
|
Date tempDate = VciDateUtil.str2Date(ymd + " " + hms, "yyyy-M-d h:m:s");
|
if (tempDate != null) {
|
Timestamp ts = Timestamp.valueOf(VciDateUtil.date2Str(tempDate, VciDateUtil.DateTimeFormat) + "." + nano);
|
d = ts;
|
}
|
}
|
} else if (value.matches("\\d+\\.?\\d*")) {
|
//还有坑爹的是给的long型的字符串
|
d = new Date();
|
d.setTime(getLong(value));
|
} else {
|
if(field.isAnnotationPresent(VciFieldType.class)) {
|
VciFieldType vciFieldType = field.getAnnotation(VciFieldType.class);
|
if(VciFieldTypeEnum.VTDate.equals(vciFieldType.value())) {
|
d = VciDateUtil.str2Date(value, VciDateUtil.DateFormat);
|
}else if(VciFieldTypeEnum.VTDateTime.equals(vciFieldType.value())){
|
d = VciDateUtil.str2Date(value, VciDateUtil.DateTimeFormat);
|
}else {
|
if(value.indexOf("-") > -1 &&value.length() == 10 ) {
|
d = VciDateUtil.str2Date(value, VciDateUtil.DateFormat);
|
}else {
|
d = VciDateUtil.str2Date(value, VciDateUtil.DateTimeMillFormat);
|
}
|
}
|
}else {
|
if(value.indexOf("-") > -1 &&value.length() == 10 ) {
|
d = VciDateUtil.str2Date(value, VciDateUtil.DateFormat);
|
}else {
|
d = VciDateUtil.str2Date(value, VciDateUtil.DateTimeMillFormat);
|
}
|
}
|
}
|
} catch (Exception e) {
|
try {
|
d = VciDateUtil.str2Date(value, VciDateUtil.DateTimeMillFormat);
|
} catch (Exception e1) {
|
|
}
|
}
|
}
|
try {
|
if(d != null ){
|
if(setMethod !=null) {
|
setMethod.invoke(obj, d);
|
}else{
|
field.set(obj,d);
|
}
|
}
|
} catch (Exception e) {
|
}
|
}else if(type.equals(String.class)){
|
if(setMethod != null) {
|
setMethod.invoke(obj, value);
|
}else{
|
field.set(obj,value);
|
}
|
}else{
|
logger.info("WebUtil.setValueForFieldForCbo:不支持的类型" + type.toString());
|
}
|
}
|
}catch (Exception e) {
|
if(logger.isErrorEnabled()){
|
logger.error("转换字符串出错",e);
|
}
|
}
|
}
|
|
/**
|
* 从对象上获取值,并转换为平台可以存储的内容,需要提前判断字段是否属于平台的字段
|
* @param field 属性对象
|
* @param obj 要设置值的对象
|
* @return 值
|
*/
|
public static String getValueFromFieldForCbo(Field field ,Object obj){
|
Object value = getValueFromField(field.getName(),obj);
|
if(value!=null){
|
try {
|
Class type = field.getType();
|
if(type.equals(int.class) || type.equals(Integer.class)){
|
return String.valueOf(((Integer)value).intValue());
|
}else if(type.equals(long.class) || type.equals(Long.class)){
|
return String.valueOf(((Long)value).longValue());
|
}else if(type.equals(float.class) || type.equals(Float.class)){
|
return String.valueOf(((Float)value).floatValue());
|
}else if(type.equals(short.class) || type.equals(Short.class)){
|
return String.valueOf(((Short)value).shortValue());
|
}else if(type.equals(Double.class) || type.equals(double.class)){
|
return String.valueOf(((Double)value).doubleValue());
|
//精度由平台底层处理
|
}else if(type.equals(Date.class)){
|
//平台实际存储的都是Timestamp,不论是日期,日期时间,都是。如果仅是时间,需要用字符串来存储。java保留到毫秒,而oracle是保留到微秒的
|
String dataFormat = "";
|
if(field.isAnnotationPresent(VciFieldType.class)){
|
if(VciFieldTypeEnum.VTDate.name().equals((field.getAnnotation(VciFieldType.class).value()).name().toLowerCase())){
|
dataFormat = VciDateUtil.DateFormat;
|
}
|
}
|
if(StringUtils.isBlank(dataFormat)){
|
dataFormat = VciDateUtil.DateTimeMillFormat;
|
}
|
return VciDateUtil.date2Str((Date)value, dataFormat);
|
}else if(type.equals(Timestamp.class)){
|
return VciDateUtil.date2Str((Timestamp)value, VciDateUtil.DateTimeMillFormat);
|
}else if(type.equals(String.class)){
|
return value.toString();
|
}else{
|
logger.info("WebUtil.setValueForFieldForCbo:不支持的类型" + type.toString());
|
}
|
} catch (SecurityException e) {
|
if(logger.isErrorEnabled()){
|
logger.error("WebUtil.setValueForFieldForCbo",e);
|
}
|
} catch (IllegalArgumentException e) {
|
if(logger.isErrorEnabled()){
|
logger.error("WebUtil.setValueForFieldForCbo",e);
|
}
|
}
|
return null;
|
}else{
|
return null;//返回给平台的话,不能有null对象
|
}
|
}
|
|
/**
|
* 为对象赋值,不判断类型,属性是什么类型的,value就必须是什么类型
|
* @param fieldName 属性名称
|
* @param targetObject 对象
|
* @param value 属性值
|
*/
|
public static void setValueToField(String fieldName,Object targetObject,Object value){
|
if(isNotNull(fieldName)){
|
Method setMethod = getSetmethod(targetObject.getClass(), fieldName);
|
try {
|
if(setMethod != null){
|
setMethod.invoke(targetObject, value);
|
}else{
|
Field field = getFieldForObject(fieldName, targetObject);
|
if(field !=null){
|
field.setAccessible(true);
|
field.set(targetObject, value);
|
}
|
}
|
} catch (IllegalArgumentException e) {
|
if(logger.isErrorEnabled()){
|
logger.error("WebUtil.setValueToField",e);
|
}
|
} catch (IllegalAccessException e) {
|
if(logger.isErrorEnabled()){
|
logger.error("WebUtil.setValueToField",e);
|
}
|
} catch (InvocationTargetException e) {
|
if(logger.isErrorEnabled()){
|
logger.error("WebUtil.setValueToField",e);
|
}
|
}
|
}
|
}
|
|
/**
|
* 从对象上获取属性的值
|
* @param fieldName 属性名
|
* @param sourceObject 对象
|
* @return 值
|
*/
|
public static Object getValueFromField(String fieldName,Object sourceObject){
|
if(isNotNull(fieldName)){
|
try {
|
Method getMethod = getGetmethod(sourceObject.getClass(), fieldName);
|
if(getMethod !=null){
|
return getMethod.invoke(sourceObject);
|
}else{
|
//说明没有设置getter,比如BO和LO对象这种
|
Field field = getFieldForObject(fieldName, sourceObject);
|
if(field !=null){
|
field.setAccessible(true);
|
return field.get(sourceObject);
|
}
|
}
|
} catch (SecurityException e) {
|
if(logger.isErrorEnabled()){
|
logger.error("WebUtil.getValueFromField",e);
|
}
|
} catch (IllegalAccessException e) {
|
if(logger.isErrorEnabled()){
|
logger.error("WebUtil.getValueFromField",e);
|
}
|
} catch (IllegalArgumentException e) {
|
if(logger.isErrorEnabled()){
|
logger.error("WebUtil.getValueFromField",e);
|
}
|
} catch (InvocationTargetException e) {
|
if(logger.isErrorEnabled()){
|
logger.error("WebUtil.getValueFromField",e);
|
}
|
}
|
}
|
return null;
|
}
|
|
/**
|
* 拷贝业务数据 到 对象的属性
|
* @param cbo 业务数据
|
* @param obj 对象
|
*/
|
public static void copyValueToObjectFromCbos(com.vci.client.bof.ClientBusinessObject cbo,Object obj){
|
if(cbo!=null){
|
copyValueToObjectFromBos(cbo.getBusinessObject(),obj);
|
}
|
}
|
|
/**
|
* 拷贝业务数据 到 对象的属性
|
* @param cbo 业务数据
|
* @param obj 对象
|
* @param fieldAttrMap 属性映射
|
*/
|
public static void copyValueToObjectFromCbos(com.vci.client.bof.ClientBusinessObject cbo,Object obj,Map<String,String> fieldAttrMap){
|
if(cbo!=null){
|
copyValueToObjectFromBos(cbo.getBusinessObject(),obj,fieldAttrMap);
|
}
|
}
|
|
/**
|
* 根据对象获取其业务类型的名称
|
* @param c 数据对象所属的类
|
* @return 业务类型
|
*/
|
public static String getBtmTypeByObject(Class c){
|
String btmType = c.getSimpleName().toLowerCase();
|
if(btmType.endsWith("DO")) {
|
btmType = btmType.substring(0,btmType.length()-2);
|
}
|
if(c.isAnnotationPresent(VciBtmType.class)) {
|
VciBtmType tableAnnotation = (VciBtmType) c.getAnnotation(VciBtmType.class);
|
String tempBtmType = tableAnnotation.name();
|
if (WebUtil.isNotNull(tempBtmType)) {
|
btmType = tempBtmType.toLowerCase();
|
}
|
}
|
return btmType;
|
}
|
|
/**
|
* 获取连接类型的名称
|
* @param c 连接类型所属的类
|
* @return 业务类型
|
*/
|
public static String getLinkTypeByObject(Class c){
|
String btmType = c.getSimpleName().toLowerCase();
|
if(btmType.endsWith("DO")) {
|
btmType = btmType.substring(0,btmType.length()-2);
|
}
|
if(c.isAnnotationPresent(VciLinkType.class)){
|
VciLinkType tableAnnotation = (VciLinkType) c.getAnnotation(VciLinkType.class);
|
String tempBtmType = tableAnnotation.name();
|
if (WebUtil.isNotNull(tempBtmType)) {
|
btmType = tempBtmType.toLowerCase();
|
}
|
}
|
return btmType;
|
}
|
|
|
/**
|
* 拷贝业务数据 到 对象的属性
|
* @param bo 业务数据
|
* @param obj 对象
|
*/
|
public static void copyValueToObjectFromBos(com.vci.corba.omd.data.BusinessObject bo ,Object obj){
|
copyValueToObjectFromBos(bo,obj,null);
|
}
|
|
/**
|
* 拷贝业务数据 到 对象的属性
|
* @param bo 业务数据
|
* @param obj 对象
|
* @param fieldAttrMap 属性映射
|
*/
|
public static void copyValueToObjectFromBos(com.vci.corba.omd.data.BusinessObject bo , Object obj, Map<String,String> fieldAttrMap){
|
if(bo!=null && obj != null){
|
//先把所有的字段映射找到
|
if(fieldAttrMap == null){
|
fieldAttrMap = getFieldNameMap(obj.getClass());
|
}
|
com.vci.corba.omd.data.AttributeValue[] newAList = bo.newAttrValList;
|
com.vci.corba.omd.data.AttributeValue[] hisAList = bo.hisAttrValList;
|
//要先把BO对象上的值拷贝过去
|
List<Field> boFields = getAllFieldForObj(bo.getClass());
|
|
if(boFields!=null){
|
for(Field field :boFields){
|
if(fieldAttrMap.containsKey(field.getName().toLowerCase())){
|
//说明这个就是在BO对象上的
|
Object value = getValueFromField(field.getName(), bo);
|
if(value !=null){
|
setValueForFieldFromCbo(fieldAttrMap.get(field.getName().toLowerCase()), obj, getStringValueFromObject(value));
|
}
|
}
|
}
|
}
|
if(newAList!=null&&newAList.length>0){
|
for(int i = 0 ; i < newAList.length;i++){
|
com.vci.corba.omd.data.AttributeValue av = newAList[i];
|
String attrName = av.attrName.toLowerCase();
|
if(fieldAttrMap.containsKey(attrName)) {
|
String fieldName = fieldAttrMap.get(attrName);
|
setValueForFieldFromCbo(fieldName, obj, av.attrVal);
|
}else{
|
fieldAttrMap.forEach((cboField,fieldName)->{
|
if(fieldName!=null && fieldName.equalsIgnoreCase(attrName) && cboField.contains(".")){
|
//参照的时候
|
setValueForFieldFromCbo(fieldName, obj, av.attrVal);
|
return;
|
}
|
});
|
}
|
}
|
}
|
if(hisAList!=null&&hisAList.length>0){
|
for(int i = 0 ; i < hisAList.length;i++){
|
com.vci.corba.omd.data.AttributeValue av = hisAList[i];
|
String attrName = av.attrName.toLowerCase();
|
String fieldName = fieldAttrMap.get(attrName);
|
if(fieldName!=null){
|
setValueForFieldFromCbo(fieldName, obj, av.attrVal);
|
}else{
|
fieldAttrMap.forEach((cboField,field)->{
|
if(field!=null && field.equalsIgnoreCase(attrName) && cboField.contains(".")){
|
//参照的时候
|
setValueForFieldFromCbo(field, obj, av.attrVal);
|
return;
|
}
|
});
|
}
|
}
|
}
|
}
|
}
|
|
/**
|
* 拷贝业务类型到map
|
* @param cbo 业务数据
|
* @param map map
|
*/
|
public static void copyValueToMapFromCbos(com.vci.client.bof.ClientBusinessObject cbo,Map<String,String> map){
|
if(cbo!=null){
|
copyValueToMapFromBos(cbo.getBusinessObject(),map);
|
}
|
}
|
|
/**
|
* 拷贝业务类型到map
|
* @param bo 业务数据
|
* @param map map
|
*/
|
public static void copyValueToMapFromBos(com.vci.corba.omd.data.BusinessObject bo, Map<String,String> map){
|
if(bo!=null ){
|
//先把所有的字段映射找到
|
com.vci.corba.omd.data.AttributeValue[] newAList = bo.newAttrValList;
|
com.vci.corba.omd.data.AttributeValue[] hisAList = bo.hisAttrValList;
|
if(hisAList!=null&&hisAList.length>0){//
|
for(int i = 0 ; i < hisAList.length;i++){
|
com.vci.corba.omd.data.AttributeValue av = hisAList[i];
|
String attrName = av.attrName.toLowerCase();
|
map.put(attrName, av.attrVal);
|
}
|
}
|
if(newAList!=null&&newAList.length>0){//NEW的优先级高些
|
for(int i = 0 ; i < newAList.length;i++){
|
com.vci.corba.omd.data.AttributeValue av = newAList[i];
|
String attrName = av.attrName.toLowerCase();
|
map.put(attrName, av.attrVal);
|
}
|
}
|
}
|
}
|
|
/**
|
* 拷贝链接类型的值到map
|
* @param clo 链接类型
|
* @param map map
|
*/
|
public static void copyValueToMapFromClos(com.vci.client.bof.ClientLinkObject clo,Map<String,String> map){
|
if(clo!=null){
|
copyValueToMapFromLos(clo.getLinkObject(),map);
|
}
|
}
|
|
/**
|
* 拷贝链接类型的值到map
|
* @param lo 链接类型
|
* @param map map
|
*/
|
public static void copyValueToMapFromLos(com.vci.corba.omd.data.LinkObject lo,Map<String,String> map){
|
if(lo!=null){
|
WebLoServiceImpl.LO_BASE_FIELD_MAP.forEach((field,attr)->{
|
map.put(attr.toLowerCase(),getValueFromFieldForCbo(getFieldForObject(field,lo),lo));
|
});
|
//先把所有的字段映射找到
|
com.vci.corba.omd.data.AttributeValue[] newAList = lo.newAttrValList;
|
com.vci.corba.omd.data.AttributeValue[] hisAList = lo.hisAttrValList;
|
if(hisAList!=null&&hisAList.length>0){
|
for(int i = 0 ; i < hisAList.length;i++){
|
com.vci.corba.omd.data.AttributeValue av = hisAList[i];
|
String attrName = av.attrName.toLowerCase();
|
map.put(attrName, av.attrVal);
|
}
|
}
|
if(newAList!=null&&newAList.length>0){
|
for(int i = 0 ; i < newAList.length;i++){
|
com.vci.corba.omd.data.AttributeValue av = newAList[i];
|
String attrName = av.attrName.toLowerCase();
|
map.put(attrName, av.attrVal);
|
}
|
}
|
}
|
}
|
/**
|
* 拷贝链接类型的值到对象
|
* @param clo 链接类型
|
* @param obj 对象
|
* @param fieldAttrMap 属性map
|
*/
|
public static void copyValueToObjectFromClos(ClientLinkObject clo,Object obj,Map<String,String> fieldAttrMap){
|
if(clo!=null){
|
copyValueToObjectFromLos(clo.getLinkObject(),obj,fieldAttrMap);
|
}
|
}
|
|
/**
|
* 拷贝链接类型的值到对象
|
* @param lo 链接类型
|
* @param obj 对象
|
*/
|
public static void copyValueToObjectFromLos(LinkObject lo ,Object obj){
|
copyValueToObjectFromLos(lo,obj,null);
|
}
|
|
/**
|
* 拷贝链接类型的值到对象
|
* @param lo 链接类型
|
* @param obj 对象
|
* @param fieldAttrMap 属性map
|
*/
|
public static void copyValueToObjectFromLos(LinkObject lo , Object obj, Map<String,String> fieldAttrMap){
|
if(lo!=null && obj != null){
|
//先把所有的字段映射找到
|
if(fieldAttrMap == null){
|
fieldAttrMap = getFieldNameMap(obj.getClass());
|
}
|
AttributeValue[] newAList = lo.newAttrValList;
|
AttributeValue[] hisAList = lo.hisAttrValList;
|
if(newAList!=null&&newAList.length>0){
|
for(int i = 0 ; i < newAList.length;i++){
|
AttributeValue av = newAList[i];
|
String attrName = av.attrName.toLowerCase();
|
String fieldName = fieldAttrMap.get(attrName);
|
setValueForFieldFromCbo(fieldName, obj, av.attrVal);//依然使用CBO的处理方式
|
}
|
}
|
if(hisAList!=null&&hisAList.length>0){
|
for(int i = 0 ; i < hisAList.length;i++){
|
AttributeValue av = hisAList[i];
|
String attrName = av.attrName.toLowerCase();
|
String fieldName = fieldAttrMap.get(attrName);
|
if(fieldName!=null){
|
setValueForFieldFromCbo(fieldName, obj, av.attrVal);
|
}
|
}
|
}
|
}
|
}
|
/**
|
* 拷贝链接类型的值到map
|
* @param cbo 链接类型
|
* @param map 对象
|
*/
|
public static void copyValueToCboFromMap(com.vci.client.bof.ClientBusinessObject cbo,Map<String,String> map) throws VciBaseException{
|
if(map!=null){
|
Iterator<String> it = map.keySet().iterator();
|
while(it.hasNext()){
|
String key = it.next();
|
String value = map.get(key);
|
if(value == null){
|
value = "";
|
}
|
try {
|
if(isDefaultField(key)){
|
setValueToField(key, cbo, value);
|
}
|
cbo.setAttributeValueWithNoCheck(key.toLowerCase(), value);
|
} catch (PLException e) {
|
throw WebUtil.getVciBaseException(e);
|
}
|
}
|
}
|
}
|
|
/**
|
* 拷贝链接类型的值到map
|
* @param clo 链接类型
|
* @param map 对象
|
*/
|
public static void copyValueToCloFromMap(ClientLinkObject clo,Map<String,String> map) throws VciBaseException{
|
if(map!=null){
|
Iterator<String> it = map.keySet().iterator();
|
while(it.hasNext()){
|
String key = it.next();
|
String value = map.get(key);
|
if(value == null){
|
value = "";
|
}
|
try {
|
if(isDefaultField(key)){
|
setValueToField(key, clo, value);
|
}else {
|
clo.setAttributeValue(key.toLowerCase(), value);
|
}
|
} catch (PLException e) {
|
throw WebUtil.getVciBaseException(e);
|
}
|
}
|
}
|
}
|
|
/**
|
* 拷贝业务对象
|
* @param bo 业务对象
|
* @param obj 数据对象
|
* @throws VciBaseException 拷贝出错的时候会抛出异常
|
*/
|
public static void copyValueToBoFromObj(BusinessObject bo,Object obj) throws VciBaseException{
|
ClientBusinessObject cbo = new ClientBusinessObject();
|
cbo.setBusinessObject(bo);
|
copyValueToCboFromObj(cbo,obj);
|
bo = cbo.getBusinessObject();
|
}
|
|
/**
|
* 拷贝业务对象
|
* @param cbo 业务对象
|
* @param obj 数据对象
|
* @throws VciBaseException 拷贝出错的时候会抛出异常
|
*/
|
public static void copyValueToCboFromObj(ClientBusinessObject cbo ,Object obj) throws VciBaseException{
|
Map<String,String> fieldMap = getFieldNameMap(obj.getClass());//先查找所有需要持久化到平台的属性,不需要持久化的就不用赋值了
|
Iterator<String> it = fieldMap.keySet().iterator();
|
while(it.hasNext()){
|
String attrName = it.next();
|
String fieldName = fieldMap.get(attrName);
|
if(isNormalAttr(attrName)){//不是参照的属性才能赋值
|
Field thisField = getFieldForObject(fieldName, obj);
|
String value = getValueFromFieldForCbo(thisField, obj);
|
if(value==null){
|
value = "";
|
}
|
try {
|
if(isDefaultField(fieldName)){
|
setValueToField(fieldName, cbo, value);
|
}
|
cbo.setAttributeValue(attrName.toLowerCase(), value);
|
} catch (PLException e) {
|
throw WebUtil.getVciBaseException(e);
|
}
|
}
|
}
|
}
|
|
/**
|
* 是否默认的属性
|
* @param fieldNames 属性的名字
|
* @return true为默认
|
*/
|
public static boolean isDefaultField(String fieldNames){
|
if(VciQueryWrapperForDO.BASIC_FIELD_MAP.containsKey(fieldNames) || VciQueryWrapperForDO.LIFECYCLE_MANAGE_FIELD_MAP.containsKey(fieldNames)
|
|| VciQueryWrapperForDO.REVISION_MANAGE_FIELD_MAP.containsKey(fieldNames) ) {
|
return true;
|
}
|
return false;
|
}
|
|
/**
|
* 是否默认的属性
|
* @param fieldNames 属性的名字
|
* @return true为默认
|
*/
|
public static boolean isDefaultLinkField(String fieldNames){
|
if(VciQueryWrapperForDO.LINK_TYPE_FIELD_MAP.containsKey(fieldNames) ) {
|
return true;
|
}
|
return false;
|
}
|
|
/**
|
* 是否为普通的属性
|
* @param attrName 属性的名字
|
* @return true
|
*/
|
public static boolean isNormalAttr(String attrName){
|
attrName = attrName.toLowerCase();
|
if(attrName.indexOf(".")<0 && attrName.indexOf("_")<0 && !attrName.equalsIgnoreCase("lcstatustext")){
|
return true;
|
}else{
|
return false;
|
}
|
}
|
|
|
/**
|
* 将whereSql里的内容转化到查询map里
|
* @param whereSql sql转换为map
|
* @return map
|
*/
|
public static Map<String,String> whereSql2Map(
|
String whereSql) {
|
Map<String,String> map = new HashMap<String, String>();
|
if(isNotNull(whereSql)){
|
String[] selects = whereSql.split("and");
|
if(selects!=null&&selects.length>0){
|
for(String s : selects){
|
s = s.trim();
|
map.put(s.substring(0,s.indexOf(" ")).trim(), s.substring(s.indexOf(" ") +1).trim());
|
}
|
}
|
}
|
return map;
|
}
|
|
/**
|
* 转换异常
|
* @param e 异常对象
|
* @return base异常对象
|
*/
|
// public static VciBaseException getVciBaseException(PLException e){
|
//// return new VciBaseException(e.error_code, e.error_message);
|
// return null;
|
// }
|
|
/**
|
* 转换异常
|
* @param e 异常对象
|
* @return base异常对象
|
*/
|
public static VciBaseException getVciBaseException(PLException e){
|
return new VciBaseException(e.code, new java.lang.String[]{e.getMessage()});
|
// return null;
|
}
|
|
/**
|
* 转换异常
|
* @param e 异常对象
|
* @return base异常对象
|
*/
|
public static VciBaseException getVciBaseException(VciException e){
|
String[] errorMsgs = new String[0];
|
if(e != null && e.getObjArray() !=null ){
|
errorMsgs = new String[e.getObjArray().length];
|
for(int i = 0 ; i < errorMsgs.length ; i ++){
|
errorMsgs[i] = e.getObjArray()[i].toString();
|
}
|
}
|
return new VciBaseException(e == null ?"com.vci.web.base.unknowerror":e.getCode(), errorMsgs);
|
}
|
|
/**
|
* 本机的ip
|
*/
|
private static String localIp = null;
|
|
/**
|
* 获取本机地址,不是客户端电脑的ip,是当前服务所在的ip
|
* @return
|
*/
|
public static String getLocalIp(){
|
if(localIp == null){
|
try {
|
InetAddress inetAddress = getLocalHostLANAddress();
|
if (inetAddress == null) {
|
localIp = "127.0.0.1";
|
} else {
|
localIp = inetAddress.getHostAddress();
|
}
|
}catch (Exception e){
|
localIp = "127.0.0.1";
|
}
|
}
|
return localIp;
|
}
|
|
/**
|
* 从网络接口上获取ip地址
|
* @return ip地址
|
*/
|
private static InetAddress getLocalHostLANAddress(){
|
try {
|
InetAddress candidateAddress = null;
|
// 遍历所有的网络接口
|
for (Enumeration ifaces = NetworkInterface.getNetworkInterfaces(); ifaces.hasMoreElements(); ) {
|
NetworkInterface iface = (NetworkInterface) ifaces.nextElement();
|
// 在所有的接口下再遍历IP
|
for (Enumeration inetAddrs = iface.getInetAddresses(); inetAddrs.hasMoreElements(); ) {
|
InetAddress inetAddr = (InetAddress) inetAddrs.nextElement();
|
if (!inetAddr.isLoopbackAddress()) {// 排除loopback类型地址
|
if (inetAddr.isSiteLocalAddress()) {
|
// 如果是site-local地址,就是它了
|
return inetAddr;
|
} else if (candidateAddress == null) {
|
// site-local类型的地址未被发现,先记录候选地址
|
candidateAddress = inetAddr;
|
}
|
}
|
}
|
}
|
if (candidateAddress != null) {
|
return candidateAddress;
|
}
|
// 如果没有发现 non-loopback地址.只能用最次选的方案
|
InetAddress jdkSuppliedAddress = InetAddress.getLocalHost();
|
return jdkSuppliedAddress;
|
} catch (Exception e) {
|
if(logger.isErrorEnabled()){
|
logger.error("获取本机ip",e);
|
}
|
}
|
return null;
|
}
|
|
/**
|
* oracle in 查询不能超过1000,转换一下集合
|
* 由于SQL语句1000个可能很长,超过oracle10g,所以牺牲性能分配为500个数组
|
* @param list 需要转换的列表内容
|
* @return 分组后的list
|
*/
|
public static <T> Collection<Collection<T>> switchCollectionForOracleIn(Collection<T> list) {
|
return switchCollectionForOracleIn(list,500);
|
}
|
|
/**
|
* 转换集合的大小,这个用在feign调用的时候,不要在sql查询的时候使用
|
* @param collection 需要转换的列表内容
|
* @param preSize 每个分组的大小
|
* @return 分组后的list
|
*/
|
public static <T> Collection<Collection<T>> switchCollectionForOracleIn(Collection<T> collection,int preSize) {
|
Collection<Collection<T>> listHasList = new ArrayList<Collection<T>>();
|
if(collection == null){
|
return listHasList;
|
}
|
List<T> newList = new ArrayList<T>();
|
for(Object obj : collection){
|
//为了让list还可以添加内容,因为使用sublist后,list不能再Add了
|
newList.add((T)obj);
|
}
|
int muti = 1;
|
if(newList.size() >preSize){
|
int balance = newList.size()%preSize;
|
muti = (newList.size() - balance)/preSize + (balance == 0?0:1);
|
}
|
for(int i = 0 ; i < muti; i ++){
|
int start = i*preSize;
|
int end = start + preSize;
|
if(i == muti-1 || end >newList.size() ){
|
end = newList.size();
|
}
|
List subList = newList.subList(start,end);
|
listHasList.add(subList);
|
}
|
return listHasList;
|
}
|
|
/**
|
* 构建主键的查询条件
|
* @param oid 主键
|
* @return 查询条件
|
*/
|
public static Map<String,String> getOidQuery(String oid) {
|
Map<String,String> conditionMap = new HashMap<>();
|
if(oid.contains(",")){
|
conditionMap.put("oid",QueryOptionConstant.IN + "(" + VciBaseUtil.toInSql(VciBaseUtil.str2List(oid).toArray(new String[0])) + ")");
|
}else {
|
conditionMap.put("oid", oid.trim());
|
}
|
return conditionMap;
|
}
|
|
/**
|
* 将数据对象转换为树形
|
* @param doList 数据对象
|
* @param wrapperOptions 封装的信息
|
* @return 树列表
|
*/
|
public static <T,R> List<Tree> cboList2Trees(List<? extends com.vci.client.bof.ClientBusinessObject> doList, TreeWrapperOptions wrapperOptions, Function<T,R> f){
|
if(CollectionUtils.isEmpty(doList)){
|
return new ArrayList<>();
|
}
|
List<Tree> allTree = new ArrayList<Tree>();
|
List<Tree> children = new ArrayList<Tree>();
|
for (int i = 0 ; i < doList.size();i++) {
|
com.vci.client.bof.ClientBusinessObject cbo = doList.get(i);
|
Tree tree =new Tree();
|
List<String> oidFieldNames = VciBaseUtil.str2List(wrapperOptions.getOidFieldName());
|
List<String> oidValues = new LinkedList<>();
|
oidFieldNames.stream().forEach( s->{
|
oidValues.add(cbo.getAttributeValue(s));
|
});
|
tree.setOid(oidValues.stream().collect(Collectors.joining(wrapperOptions.getOidValueSep())));
|
if(f !=null){
|
tree.setText((String)f.apply((T) cbo));
|
}else{
|
List<String> textFieldNames = VciBaseUtil.str2List(wrapperOptions.getTextFieldName());
|
List<String> textValues = new LinkedList<>();
|
textFieldNames.stream().forEach( s->{
|
textValues.add(cbo.getAttributeValue(s));
|
});
|
tree.setText(textValues.stream().collect(Collectors.joining(wrapperOptions.getTextValueSep())));
|
}
|
if(StringUtils.isNotBlank(wrapperOptions.getParentFieldName())){
|
tree.setParentId(cbo.getAttributeValue(wrapperOptions.getParentFieldName()));
|
}
|
if(wrapperOptions.isAllAttributes()) {
|
try {
|
Map<String,String> map = new HashMap<>();
|
copyValueToMapFromCbos(cbo,map);
|
tree.setAttributes(map);
|
} catch (Exception e) {
|
//这里不做处理
|
if (logger.isErrorEnabled()) {
|
logger.error("把对象转换为map时出现了错误,但是不影响树的展示,对业务可能有影响");
|
}
|
}
|
}
|
if(wrapperOptions.isMultipleSelect() || wrapperOptions.isShowCheckBox()){
|
tree.setShowCheckbox(true);
|
}
|
if(wrapperOptions.getParentOid() == null){
|
wrapperOptions.setParentOid("");
|
}
|
tree.setIndex(String.valueOf(i));
|
if(StringUtils.isBlank(tree.getParentId())
|
|| (StringUtils.isNotBlank(wrapperOptions.getParentOid()) && wrapperOptions.getParentOid().equalsIgnoreCase(tree.getParentId()))){
|
allTree.add(tree);
|
}else {
|
children.add(tree);
|
}
|
}
|
|
if(allTree.size()<=0){
|
//说明全是子,我们需要从子里获取
|
if(!CollectionUtils.isEmpty(children)){
|
Map<String, String> oidParentMap = children.stream().collect(Collectors.toMap(s -> s.getOid(), t -> t.getParentId() == null?"":t.getParentId(), (o1, o2) -> o1));
|
List<String> rootOids = oidParentMap.entrySet().stream().filter(s -> !oidParentMap.containsKey(s.getValue())).map(s -> s.getKey()).collect(Collectors.toList());
|
List<Tree> newChildren = new ArrayList<>();
|
allTree = children.stream().filter(s->rootOids.contains(s.getOid())).collect(Collectors.toList());
|
newChildren = children.stream().filter(s->!rootOids.contains(s.getOid())).collect(Collectors.toList());
|
children = newChildren;
|
}
|
}
|
new Tree().findChild(allTree,children);
|
return allTree;
|
}
|
|
public static com.vci.client.bof.ClientBusinessObject arryAndSqlToClientBusinessObject(String sqlStr,String[] valueArry) {
|
com.vci.client.bof.ClientBusinessObject clientBusinessObject = new com.vci.client.bof.ClientBusinessObject();
|
try {
|
PlainSelect select = (PlainSelect) CCJSqlParserUtil.parse(sqlStr);
|
com.vci.corba.omd.data.AttributeValue[] attributeValues = new com.vci.corba.omd.data.AttributeValue[valueArry.length];
|
for (int i = 0; i < select.getSelectItems().size(); i++) {
|
SelectItem selectItem = select.getSelectItem(i);
|
com.vci.corba.omd.data.AttributeValue attributeValue = new com.vci.corba.omd.data.AttributeValue();
|
attributeValue.attrName = selectItem.getAlias().getName();
|
attributeValue.attrVal = valueArry[i];
|
attributeValues[i] = attributeValue;
|
}
|
clientBusinessObject.getBusinessObject().newAttrValList= attributeValues;
|
}catch (Exception e){
|
logger.error("数据解析错误",e);
|
return null;
|
}
|
return clientBusinessObject;
|
}
|
}
|