合并TestOrganization冲突
commit
72f8bca9aa
|
@ -0,0 +1,28 @@
|
|||
package weaver.xiao.commons.config.entity;
|
||||
|
||||
import lombok.Data;
|
||||
import weaver.xiao.commons.utils.annotation.SqlFieldMapping;
|
||||
|
||||
import java.io.InputStream;
|
||||
|
||||
/**
|
||||
* <h1>文件信息实体类</h1>
|
||||
*
|
||||
* @author XiaoBokang
|
||||
* @create 2022/8/8 10:24
|
||||
*/
|
||||
|
||||
@Data
|
||||
public class WeaverFile {
|
||||
|
||||
@SqlFieldMapping("imagefilename")
|
||||
String fileName;
|
||||
|
||||
@SqlFieldMapping("imagefileid")
|
||||
String imageFileId;
|
||||
|
||||
@SqlFieldMapping("docid")
|
||||
String docId;
|
||||
|
||||
InputStream inputStream;
|
||||
}
|
|
@ -1239,6 +1239,45 @@ public class DealWithMapping extends ToolUtil {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* <h2>请求结果赋值到配置信息中</h2>
|
||||
* @param responseMappingList 回写信息配置列表
|
||||
* @param requestRes 请求结果
|
||||
* @return 要插入的信息
|
||||
*/
|
||||
public Map<String, Map<String, Object>> dealResponse(List<ResponseMapping> responseMappingList, Map<String, Object> requestRes) {
|
||||
this.writeDebuggerLog("回写信息转换处理 responseMappingList==>" + responseMappingList + " requestRes==>" + requestRes);
|
||||
Map<String, Map<String, Object>> tableUpdateMap = new HashMap<>();
|
||||
if (responseMappingList != null && !responseMappingList.isEmpty()) {
|
||||
Map<String, Object> mainUpdateMap = new HashMap<>();
|
||||
List<ResponseMapping> detailResponseList = new ArrayList<>();
|
||||
for (ResponseMapping responseMapping : responseMappingList) {
|
||||
if (responseMapping.getMainOrDetail() == 0) {
|
||||
String resVal = Util.null2String(requestRes.get(responseMapping.getResponseFieldName()));
|
||||
mainUpdateMap.put(responseMapping.getWorkflowFieldName(), resVal);
|
||||
} else {
|
||||
detailResponseList.add(responseMapping);
|
||||
}
|
||||
}
|
||||
tableUpdateMap.put(mainTable, mainUpdateMap);
|
||||
Map<String, List<ResponseMapping>> groupMap = detailResponseList.stream().collect(Collectors.groupingBy(ResponseMapping::getDetailTableId));
|
||||
Set<Map.Entry<String, List<ResponseMapping>>> entries = groupMap.entrySet();
|
||||
for (Map.Entry<String, List<ResponseMapping>> entry : entries) {
|
||||
Map<String, Object> detailUpdateMap = new HashMap<>();
|
||||
String detailId = entry.getKey();
|
||||
List<ResponseMapping> responseMappings = entry.getValue();
|
||||
String detailTableName = mainTable + "_dt" + detailId;
|
||||
for (ResponseMapping responseMapping : responseMappings) {
|
||||
String resVal = Util.null2String(requestRes.get(responseMapping.getResponseFieldName()));
|
||||
detailUpdateMap.put(responseMapping.getWorkflowFieldName(), resVal);
|
||||
}
|
||||
tableUpdateMap.put(detailTableName, detailUpdateMap);
|
||||
}
|
||||
}
|
||||
this.writeDebuggerLog("回写信息tableUpdateMap==> " + tableUpdateMap);
|
||||
return tableUpdateMap;
|
||||
}
|
||||
|
||||
/**
|
||||
* 自定义时间格式化
|
||||
*
|
||||
|
|
|
@ -0,0 +1,25 @@
|
|||
package weaver.xiao.commons.core;
|
||||
|
||||
import weaver.hrm.User;
|
||||
import weaver.workflow.request.RequestManager;
|
||||
|
||||
/**
|
||||
* @ClassName BaseActionHandleFunction
|
||||
* @Author 肖博亢
|
||||
* @Date 2022/11/25 12:17
|
||||
* @Description <h1>流程提交类型处理方法</h1>
|
||||
**/
|
||||
@FunctionalInterface
|
||||
public interface BaseActionHandleFunction {
|
||||
/**
|
||||
* <h2>流程处理方法action</h2>
|
||||
*
|
||||
* @param requestId 流程请求ID
|
||||
* @param billTable 流程对应主表名称
|
||||
* @param workflowId 流程对应流程ID
|
||||
* @param user 当前节点操作者用户
|
||||
* @param requestManager 请求管理对象
|
||||
*/
|
||||
void handle(String requestId, String billTable, int workflowId,
|
||||
User user, RequestManager requestManager) throws Exception;
|
||||
}
|
|
@ -0,0 +1,276 @@
|
|||
package weaver.xiao.commons.core;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
import weaver.conn.RecordSetTrans;
|
||||
import weaver.hrm.User;
|
||||
import weaver.interfaces.workflow.action.Action;
|
||||
import weaver.soa.workflow.request.*;
|
||||
import weaver.workflow.request.RequestManager;
|
||||
import weaver.workflow.workflow.WorkflowBillComInfo;
|
||||
import weaver.workflow.workflow.WorkflowComInfo;
|
||||
import weaver.xiao.commons.utils.LogUtil;
|
||||
import weaver.xiao.commons.utils.VerifyUtil;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
|
||||
/**
|
||||
* @ClassName BaseAction
|
||||
* @Author 肖博亢
|
||||
* @Date 2022/11/25 12:09
|
||||
* @Description <h1>实现Action接口并具备一些基本功能</h1>
|
||||
**/
|
||||
public abstract class CusBaseAction implements Action {
|
||||
|
||||
/** 全局日志对象 */
|
||||
protected final Logger log = LogUtil.getLogger();
|
||||
|
||||
/** 全局requestInfo对象 */
|
||||
protected RequestInfo requestInfo;
|
||||
|
||||
/** 全局数据处理事务类 */
|
||||
protected RecordSetTrans recordSetTrans = new RecordSetTrans();
|
||||
|
||||
/** 流程提交方式处理方法集合 */
|
||||
private final Map<String, BaseActionHandleFunction> actionHandleMethod = new HashMap<>();
|
||||
|
||||
|
||||
/**
|
||||
* <h2>初始化流程默认的处理方法</h2>
|
||||
*/
|
||||
private void initHandleMethod() {
|
||||
//提交
|
||||
actionHandleMethod.put(ActionRunType.SUBMIT, this::doSubmit);
|
||||
//退回
|
||||
actionHandleMethod.put(ActionRunType.REJECT, this::doReject);
|
||||
//撤回
|
||||
actionHandleMethod.put(ActionRunType.WITHDRAW, this::doWithdraw);
|
||||
//强制收回
|
||||
actionHandleMethod.put(ActionRunType.DRAW_BACK, this::doDrawBack);
|
||||
}
|
||||
|
||||
@Override
|
||||
public final String execute(RequestInfo requestInfo) {
|
||||
this.requestInfo = requestInfo;
|
||||
RequestManager requestManager = requestInfo.getRequestManager();
|
||||
String billTable = requestManager.getBillTableName();
|
||||
String requestId = requestInfo.getRequestid();
|
||||
User user = requestInfo.getRequestManager().getUser();
|
||||
int workflowId = requestManager.getWorkflowid();
|
||||
//操作类型 submit - 提交 reject - 退回 等
|
||||
String src = requestManager.getSrc();
|
||||
if ("".equals(billTable)) {
|
||||
WorkflowComInfo workflowComInfo = new WorkflowComInfo();
|
||||
String formId = workflowComInfo.getFormId(String.valueOf(workflowId));
|
||||
WorkflowBillComInfo workflowBillComInfo = new WorkflowBillComInfo();
|
||||
billTable = workflowBillComInfo.getTablename(formId);
|
||||
}
|
||||
try {
|
||||
VerifyUtil.requireVerify(this);
|
||||
if (VerifyUtil.isBlank(src)) {
|
||||
src = "submit";
|
||||
}
|
||||
//初始化默认的流程处理方法
|
||||
initHandleMethod();
|
||||
//提供自定义注册处理方法
|
||||
registerHandler(actionHandleMethod);
|
||||
//获取流程对应的处理方法
|
||||
BaseActionHandleFunction cusBaseActionHandleFunction = actionHandleMethod.get(src);
|
||||
//默认没有直接成功不做拦截
|
||||
if (null == cusBaseActionHandleFunction) {
|
||||
return Action.SUCCESS;
|
||||
}
|
||||
cusBaseActionHandleFunction.handle(requestId, billTable, workflowId, user, requestManager);
|
||||
}catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
log.info("action执行异常 ==>"+LogUtil.getExceptionStr(e));
|
||||
String message = doExceptionDeal(e,requestId,workflowId,user,requestManager);
|
||||
requestInfo.getRequestManager().setMessageid("11111C " + requestId + " 22222");
|
||||
requestInfo.getRequestManager().setMessagecontent(message);
|
||||
return Action.FAILURE_AND_CONTINUE;
|
||||
}
|
||||
return Action.SUCCESS;
|
||||
}
|
||||
|
||||
/**
|
||||
* <h2>流程其他流转类型处理方法注册</h2>
|
||||
*
|
||||
* @param actionHandleMethod 处理方法对应map
|
||||
*/
|
||||
public void registerHandler(Map<String, BaseActionHandleFunction> actionHandleMethod) {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* <h2>action 提交流程业务处理方法</h2>
|
||||
* <p>具体业务逻辑实现
|
||||
* 全局存在log成员变量,用于日志的输出
|
||||
* Util.actionFailException(requestManager,"error msg"); 用于提示action执行失败
|
||||
* </p>
|
||||
*
|
||||
* @param requestId 流程请求ID
|
||||
* @param billTable 流程对应主表名称
|
||||
* @param workflowId 流程对应流程ID
|
||||
* @param user 当前节点操作者用户
|
||||
* @param requestManager 请求管理对象
|
||||
*/
|
||||
|
||||
public abstract void doSubmit(String requestId, String billTable, int workflowId,
|
||||
User user, RequestManager requestManager) throws Exception;
|
||||
|
||||
|
||||
/**
|
||||
* <h2>action 退回流程业务处理方法</h2>
|
||||
* <p>具体业务逻辑实现
|
||||
* 全局存在log成员变量,用于日志的输出
|
||||
* Util.actionFailException(requestManager,"error msg"); 用于提示action执行失败
|
||||
* </p>
|
||||
*
|
||||
* @param requestId 流程请求ID
|
||||
* @param billTable 流程对应主表名称
|
||||
* @param workflowId 流程对应流程ID
|
||||
* @param user 当前节点操作者用户
|
||||
* @param requestManager 请求管理对象
|
||||
*/
|
||||
public void doReject(String requestId, String billTable, int workflowId,
|
||||
User user, RequestManager requestManager) throws Exception{
|
||||
}
|
||||
|
||||
/**
|
||||
* <h2>action 撤回、撤回流程流程业务处理方法</h2>
|
||||
* <p>具体业务逻辑实现
|
||||
* 全局存在log成员变量,用于日志的输出
|
||||
* Util.actionFailException(requestManager,"error msg"); 用于提示action执行失败
|
||||
* </p>
|
||||
*
|
||||
* @param requestId 流程请求ID
|
||||
* @param billTable 流程对应主表名称
|
||||
* @param workflowId 流程对应流程ID
|
||||
* @param user 当前节点操作者用户
|
||||
* @param requestManager 请求管理对象
|
||||
*/
|
||||
public void doWithdraw(String requestId, String billTable, int workflowId,
|
||||
User user, RequestManager requestManager) throws Exception{
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* <h2>action 强制收回流程业务处理方法</h2>
|
||||
* <p>具体业务逻辑实现
|
||||
* 全局存在log成员变量,用于日志的输出
|
||||
* Util.actionFailException(requestManager,"error msg"); 用于提示action执行失败
|
||||
* </p>
|
||||
*
|
||||
* @param requestId 流程请求ID
|
||||
* @param billTable 流程对应主表名称
|
||||
* @param workflowId 流程对应流程ID
|
||||
* @param user 当前节点操作者用户
|
||||
* @param requestManager 请求管理对象
|
||||
*/
|
||||
public void doDrawBack(String requestId, String billTable, int workflowId,
|
||||
User user, RequestManager requestManager) throws Exception{
|
||||
}
|
||||
|
||||
/**
|
||||
* <h2>action 发生异常处理方法</h2>
|
||||
* @param e 异常信息
|
||||
* @param requestId 请求id
|
||||
* @param workflowId 流程id
|
||||
* @param user 用户信息
|
||||
* @param requestManager 请求管理对象
|
||||
* @return 提示信息
|
||||
*/
|
||||
public abstract String doExceptionDeal(Exception e,String requestId,int workflowId,User user,RequestManager requestManager);
|
||||
|
||||
/**
|
||||
* <h2>获取流程主表数据</h2>
|
||||
*
|
||||
* @return 流程主表数据
|
||||
*/
|
||||
protected Map<String, Object> getMainTableValue() {
|
||||
//获取主表数据
|
||||
Property[] propertyArr = requestInfo.getMainTableInfo().getProperty();
|
||||
if (null == propertyArr) {
|
||||
return Collections.emptyMap();
|
||||
}
|
||||
Map<String, Object> mainTable = new HashMap<>((int) Math.ceil(propertyArr.length * 1.4));
|
||||
for (Property property : propertyArr) {
|
||||
String fieldName = property.getName();
|
||||
String value = property.getValue();
|
||||
mainTable.put(fieldName, value);
|
||||
}
|
||||
return mainTable;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* <h2>获取所有明细数据</h2>
|
||||
*
|
||||
* @return 以明细表需要为键,以明细表数据为值的键值对明细数据信息
|
||||
*/
|
||||
protected Map<String, List<Map<String, String>>> getDetailTableValue() {
|
||||
DetailTable[] detailTableArr = requestInfo.getDetailTableInfo().getDetailTable();
|
||||
Map<String, List<Map<String, String>>> detailDataList = new HashMap<>((int) Math.ceil(detailTableArr.length * 1.4));
|
||||
for (DetailTable detailTable : detailTableArr) {
|
||||
List<Map<String, String>> detailData = getDetailValue(detailTable);
|
||||
detailDataList.put(detailTable.getId(), detailData);
|
||||
}
|
||||
return detailDataList;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* <h2>获取指定明细表的表数据</h2>
|
||||
*
|
||||
* @param detailNo 明细表编号
|
||||
* @return 明细数据
|
||||
*/
|
||||
protected List<Map<String, String>> getDetailTableValueByDetailNo(int detailNo) {
|
||||
DetailTable detailTable = requestInfo.getDetailTableInfo().getDetailTable(detailNo);
|
||||
return getDetailValue(detailTable);
|
||||
}
|
||||
|
||||
/**
|
||||
* <h2>根据明细表信息获取明细表数据</h2>
|
||||
*
|
||||
* @param detailTable 明细表对象
|
||||
* @return 明细表数据
|
||||
*/
|
||||
private List<Map<String, String>> getDetailValue(DetailTable detailTable) {
|
||||
Row[] rowArr = detailTable.getRow();
|
||||
List<Map<String, String>> detailData = new ArrayList<>(rowArr.length);
|
||||
for (Row row : rowArr) {
|
||||
Cell[] cellArr = row.getCell();
|
||||
Map<String, String> rowData = new HashMap<>((int) Math.ceil(cellArr.length * (1 + 0.4)));
|
||||
rowData.put("id", row.getId());
|
||||
for (Cell cell : cellArr) {
|
||||
String fieldName = cell.getName();
|
||||
String value = cell.getValue();
|
||||
rowData.put(fieldName, value);
|
||||
}
|
||||
detailData.add(rowData);
|
||||
}
|
||||
return detailData;
|
||||
}
|
||||
|
||||
|
||||
public static final class ActionRunType {
|
||||
/**
|
||||
* 退回
|
||||
*/
|
||||
public static final String REJECT = "reject";
|
||||
/**
|
||||
* 撤回
|
||||
*/
|
||||
public static final String WITHDRAW = "withdraw";
|
||||
/**
|
||||
* 强制收回
|
||||
*/
|
||||
public static final String DRAW_BACK = "drawBack";
|
||||
/**
|
||||
* 提交
|
||||
*/
|
||||
public static final String SUBMIT = "submit";
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
package weaver.xiao.commons.exception;
|
||||
|
||||
/**
|
||||
* @author XiaoBokang
|
||||
* @create 2022/7/28 14:47
|
||||
*/
|
||||
|
||||
public class VerifyException extends RuntimeException{
|
||||
public VerifyException(String message){
|
||||
super(message);
|
||||
}
|
||||
|
||||
public VerifyException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized Throwable fillInStackTrace() {
|
||||
return super.fillInStackTrace();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,292 @@
|
|||
package weaver.xiao.commons.utils;
|
||||
|
||||
import aiyh.utils.excention.CustomerException;
|
||||
import org.apache.log4j.Logger;
|
||||
import weaver.docs.docs.DocImageManager;
|
||||
import weaver.docs.webservices.DocInfo;
|
||||
import weaver.docs.webservices.DocServiceImpl;
|
||||
import weaver.file.ImageFileManager;
|
||||
import weaver.general.IOUtils;
|
||||
import weaver.hrm.User;
|
||||
import weaver.xiao.commons.config.entity.WeaverFile;
|
||||
|
||||
import java.io.*;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.Enumeration;
|
||||
import java.util.List;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipFile;
|
||||
import java.util.zip.ZipOutputStream;
|
||||
|
||||
|
||||
/**
|
||||
* @author XiaoBokang
|
||||
* @create 2022/7/28 16:05
|
||||
*/
|
||||
|
||||
public class DocImageFileUtil {
|
||||
|
||||
private static final Logger logger = LogUtil.getLogger();
|
||||
|
||||
public static final String DOC = "doc";
|
||||
public static final String XLS = "xls";
|
||||
public static final String PPTX = "pptx";
|
||||
public static final String PPT = "ppt";
|
||||
public static final String WPS = "wps";
|
||||
public static final String DOCX = "docx";
|
||||
public static final String XLSX = "xlsx";
|
||||
private static final int BUFFER_SIZE = 2 * 1024;
|
||||
|
||||
/**
|
||||
* <h2>zip解压</h2>
|
||||
* @param srcFile zip源文件
|
||||
* @param destDirPath 解压后的目标文件夹
|
||||
* @throws RuntimeException 解压失败会抛出运行时异常
|
||||
*/
|
||||
public static void unZip(File srcFile, String destDirPath) throws RuntimeException {
|
||||
long start = System.currentTimeMillis();
|
||||
// 判断源文件是否存在
|
||||
if (!srcFile.exists()) {
|
||||
throw new RuntimeException(srcFile.getPath() + "所指文件不存在");
|
||||
}
|
||||
// 开始解压
|
||||
ZipFile zipFile = null;
|
||||
try {
|
||||
zipFile = new ZipFile(srcFile, Charset.forName("GBK"));
|
||||
Enumeration<?> entries = zipFile.entries();
|
||||
while (entries.hasMoreElements()) {
|
||||
ZipEntry entry = (ZipEntry) entries.nextElement();
|
||||
System.out.println("解压" + entry.getName());
|
||||
// 如果是文件夹,就创建个文件夹
|
||||
if (entry.isDirectory()) {
|
||||
String dirPath = destDirPath + "/" + entry.getName();
|
||||
File dir = new File(dirPath);
|
||||
dir.mkdirs();
|
||||
} else {
|
||||
// 如果是文件,就先创建一个文件,然后用io流把内容copy过去
|
||||
File targetFile = new File(destDirPath + "/" + entry.getName());
|
||||
// 保证这个文件的父文件夹必须存在
|
||||
if(!targetFile.getParentFile().exists()){
|
||||
targetFile.getParentFile().mkdirs();
|
||||
}
|
||||
targetFile.createNewFile();
|
||||
// 将压缩文件内容写入到这个文件中
|
||||
InputStream is = zipFile.getInputStream(entry);
|
||||
FileOutputStream fos = new FileOutputStream(targetFile);
|
||||
int len;
|
||||
byte[] buf = new byte[BUFFER_SIZE];
|
||||
while ((len = is.read(buf)) != -1) {
|
||||
fos.write(buf, 0, len);
|
||||
}
|
||||
// 关流顺序,先打开的后关闭
|
||||
fos.close();
|
||||
is.close();
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.info("解压文件出现异常 ==>"+LogUtil.getExceptionStr(e));
|
||||
throw new RuntimeException("unzip error from ZipUtils", e);
|
||||
} finally {
|
||||
if(zipFile != null){
|
||||
try {
|
||||
zipFile.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* <h2>压缩成ZIP方法</h2>
|
||||
* @param srcFiles 需要压缩的文件列表
|
||||
* @param out 压缩文件输出流
|
||||
* @throws RuntimeException 压缩失败会抛出运行时异常
|
||||
*/
|
||||
public static void toZip(List<File> srcFiles , OutputStream out)throws Exception {
|
||||
long start = System.currentTimeMillis();
|
||||
ZipOutputStream zos = null ;
|
||||
try {
|
||||
zos = new ZipOutputStream(out);
|
||||
for (File srcFile : srcFiles) {
|
||||
byte[] buf = new byte[BUFFER_SIZE];
|
||||
zos.putNextEntry(new ZipEntry(srcFile.getName()));
|
||||
int len;
|
||||
FileInputStream in = new FileInputStream(srcFile);
|
||||
while ((len = in.read(buf)) != -1){
|
||||
zos.write(buf, 0, len);
|
||||
}
|
||||
zos.closeEntry();
|
||||
in.close();
|
||||
}
|
||||
long end = System.currentTimeMillis();
|
||||
System.out.println("压缩完成,耗时:" + (end - start) +" ms");
|
||||
} catch (Exception e) {
|
||||
logger.info("压缩文件出现异常 ==>"+LogUtil.getExceptionStr(e));
|
||||
throw new RuntimeException("zip error from ZipUtils",e);
|
||||
}finally{
|
||||
if(zos != null){
|
||||
try {
|
||||
zos.close();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 在指定目录下生成对应的压缩文件
|
||||
* @param fileList 文件集合列表
|
||||
* @param dirPath 目录
|
||||
* @param fileName 压缩文件名
|
||||
*/
|
||||
public static void toZipFileName(List<WeaverFile> fileList,String dirPath,String fileName) throws IOException {
|
||||
String filePath = dirPath + File.separator + fileName;
|
||||
File file = new File(filePath);
|
||||
if(!file.exists()){
|
||||
file.createNewFile();
|
||||
}
|
||||
OutputStream outputStream = Files.newOutputStream(file.toPath());
|
||||
toZipByWeaverFile(fileList,outputStream);
|
||||
}
|
||||
|
||||
/**
|
||||
* 将文件集合压缩
|
||||
* @param fileList 文件信息集合
|
||||
* @param outputStream 压缩文件输出流
|
||||
*/
|
||||
public static void toZipByWeaverFile(List<WeaverFile> fileList, OutputStream outputStream){
|
||||
ZipOutputStream zos = null ;
|
||||
try {
|
||||
zos = new ZipOutputStream(outputStream);
|
||||
for (WeaverFile weaverFile : fileList) {
|
||||
byte[] buf = new byte[BUFFER_SIZE];
|
||||
zos.putNextEntry(new ZipEntry(weaverFile.getFileName()));
|
||||
int len;
|
||||
InputStream inputStream = weaverFile.getInputStream();
|
||||
while ((len = inputStream.read(buf)) != -1){
|
||||
zos.write(buf, 0, len);
|
||||
}
|
||||
inputStream.close();
|
||||
zos.closeEntry();
|
||||
}
|
||||
zos.flush();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
throw new RuntimeException("zip error from ZipUtils",e);
|
||||
}finally{
|
||||
if(zos != null){
|
||||
try {
|
||||
zos.finish();
|
||||
zos.close();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* <h2>创建物理文件</h2>
|
||||
* @param content 文件流
|
||||
* @param fileName 文件名
|
||||
* @return 文件id
|
||||
*/
|
||||
public static int createFileByInputSteam(InputStream content, String fileName) {
|
||||
ImageFileManager imageFileManager = new ImageFileManager();
|
||||
int imgFileId = -1;
|
||||
try {
|
||||
ImageFileManager.class.getMethod("saveImageFileByInputStream", InputStream.class, String.class);
|
||||
imgFileId = imageFileManager.saveImageFileByInputStream(content, fileName);
|
||||
} catch (NoSuchMethodException e) {
|
||||
imageFileManager.setImagFileName(fileName);
|
||||
try {
|
||||
imageFileManager.setData(IOUtils.toBytes(content));
|
||||
} catch (Exception ex) {
|
||||
throw new CustomerException("创建文件失败,文件流转换失败", e);
|
||||
}
|
||||
imgFileId = imageFileManager.saveImageFile();
|
||||
} catch (Exception e) {
|
||||
throw new CustomerException("创建文件失败");
|
||||
}
|
||||
return imgFileId;
|
||||
}
|
||||
|
||||
/**
|
||||
* <h2>创建文件信息,文件权限继承文档目录权限</h2>
|
||||
*
|
||||
* @param fileName 文件名称
|
||||
* @param secCategory 文件目录
|
||||
* @param imageFileId 物理文件ID
|
||||
* @param userId 用户ID
|
||||
* @return 文档ID
|
||||
* @throws Exception 可能会出现的异常
|
||||
*/
|
||||
public static int createDocByImageFileId(String fileName, int secCategory, int imageFileId, int userId) throws Exception {
|
||||
DocInfo docInfo = new DocInfo();
|
||||
docInfo.setImagefileId(imageFileId);
|
||||
docInfo.setSeccategory(secCategory);
|
||||
docInfo.setDocSubject(fileName);
|
||||
docInfo.setDoccontent("");
|
||||
DocServiceImpl docService = new DocServiceImpl();
|
||||
int docId = docService.createDocByUser(docInfo, new User(userId));
|
||||
DocImageManager imgManger = new DocImageManager();
|
||||
imgManger.resetParameter();
|
||||
imgManger.setDocid(docId);
|
||||
imgManger.setImagefileid(imageFileId);
|
||||
imgManger.setImagefilename(fileName);
|
||||
imgManger.setIsextfile("1");
|
||||
String ext = fileName.substring(fileName.lastIndexOf(".") + 1);
|
||||
if (DOC.equalsIgnoreCase(ext)) {
|
||||
imgManger.setDocfiletype("3");
|
||||
} else if (XLS.equalsIgnoreCase(ext)) {
|
||||
imgManger.setDocfiletype("4");
|
||||
} else if (PPT.equalsIgnoreCase(ext)) {
|
||||
imgManger.setDocfiletype("5");
|
||||
} else if (WPS.equalsIgnoreCase(ext)) {
|
||||
imgManger.setDocfiletype("6");
|
||||
} else if (DOCX.equalsIgnoreCase(ext)) {
|
||||
imgManger.setDocfiletype("7");
|
||||
} else if (XLSX.equalsIgnoreCase(ext)) {
|
||||
imgManger.setDocfiletype("8");
|
||||
} else if (PPTX.equalsIgnoreCase(ext)) {
|
||||
imgManger.setDocfiletype("9");
|
||||
} else {
|
||||
imgManger.setDocfiletype("2");
|
||||
}
|
||||
imgManger.AddDocImageInfo();
|
||||
return docId;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 删除临时文件
|
||||
* @param tempFilePath 临时文件路径
|
||||
*/
|
||||
public static void deleteTempFile(String tempFilePath){
|
||||
logger.info("=========删除临时文件:"+tempFilePath+"================");
|
||||
boolean deleteFlag = true;
|
||||
int n = 0;
|
||||
while (deleteFlag) {
|
||||
try {
|
||||
n++;
|
||||
Files.delete(Paths.get(tempFilePath));
|
||||
deleteFlag = false;
|
||||
} catch (Exception e) {
|
||||
//设置线程沉睡500毫秒,等待jvm进行垃圾回收,将持有临时文件的流对象回收,确保临时文件能删除
|
||||
try {
|
||||
Thread.sleep(500);
|
||||
} catch (InterruptedException interruptedException) {
|
||||
deleteFlag = false;
|
||||
}
|
||||
if (n > 5) {
|
||||
deleteFlag = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,98 @@
|
|||
package weaver.xiao.commons.utils;
|
||||
|
||||
import org.apache.http.client.CredentialsProvider;
|
||||
import org.apache.http.client.config.RequestConfig;
|
||||
import org.apache.http.conn.ssl.NoopHostnameVerifier;
|
||||
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
import org.apache.http.impl.client.HttpClientBuilder;
|
||||
import org.apache.http.impl.client.HttpClients;
|
||||
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
|
||||
import org.apache.http.ssl.SSLContextBuilder;
|
||||
|
||||
import javax.net.ssl.HostnameVerifier;
|
||||
import javax.net.ssl.SSLContext;
|
||||
import java.security.KeyManagementException;
|
||||
import java.security.KeyStoreException;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
|
||||
|
||||
/**
|
||||
* @author XiaoBokang
|
||||
* @create 2021/12/7 14:19
|
||||
*/
|
||||
|
||||
public class HttpManager {
|
||||
|
||||
/**
|
||||
* 创建连接池管理对象
|
||||
*/
|
||||
|
||||
|
||||
private static final int CONNECT_TIMEOUT = 1000 * 60 * 3;
|
||||
private static final int CONNECTION_REQUEST_TIMEOUT = 1000 * 60 * 3;
|
||||
private static final int SOCKET_TIMEOUT = 10000 * 60 * 3;
|
||||
private static final int MAX_TOTAL = 500;
|
||||
private static final int MAX_PRE_ROUTE = 500;
|
||||
|
||||
/**
|
||||
* 设置请求配置
|
||||
*/
|
||||
static RequestConfig requestConfig = RequestConfig.custom()
|
||||
//网络请求的超时时间
|
||||
.setConnectTimeout(CONNECT_TIMEOUT)
|
||||
//连接池去获取连接的超时时间
|
||||
.setConnectionRequestTimeout(CONNECTION_REQUEST_TIMEOUT)
|
||||
//设置socket超时时间
|
||||
.setSocketTimeout(SOCKET_TIMEOUT)
|
||||
.build();
|
||||
static PoolingHttpClientConnectionManager manager = new PoolingHttpClientConnectionManager();
|
||||
static {
|
||||
// 配置最大的连接数
|
||||
manager.setMaxTotal(MAX_TOTAL);
|
||||
// 每个路由最大连接数(路由是根据host来管理的,大小不好控制)
|
||||
manager.setDefaultMaxPerRoute(MAX_PRE_ROUTE);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取连接对象 从连接池里面去获取,根据url创建对应的对象,http / https
|
||||
* @param url 请求地址
|
||||
* @return 连接对象
|
||||
*/
|
||||
public static CloseableHttpClient getHttpConnection(String url, CredentialsProvider credentialsProvider){
|
||||
if(url.trim().toUpperCase().startsWith("https".toUpperCase())){
|
||||
SSLContext sslContext;
|
||||
SSLConnectionSocketFactory sslsf = null;
|
||||
try {
|
||||
sslContext = new SSLContextBuilder().loadTrustMaterial(null, (x509Certificates, s) -> {
|
||||
// 绕过所有验证
|
||||
return true;
|
||||
}).build();
|
||||
HostnameVerifier hostnameVerifier = NoopHostnameVerifier.INSTANCE;
|
||||
sslsf = new SSLConnectionSocketFactory(sslContext,hostnameVerifier);
|
||||
} catch (NoSuchAlgorithmException | KeyStoreException | KeyManagementException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
HttpClientBuilder httpClientBuilder = HttpClients.custom()
|
||||
.setSSLSocketFactory(sslsf)
|
||||
.setConnectionManager(manager)
|
||||
.setConnectionManagerShared(true)
|
||||
.setDefaultRequestConfig(requestConfig);
|
||||
if(credentialsProvider != null){
|
||||
httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
|
||||
}
|
||||
return httpClientBuilder
|
||||
.build();
|
||||
}else{
|
||||
HttpClientBuilder httpClientBuilder = HttpClients.custom()
|
||||
.setConnectionManager(manager)
|
||||
.setDefaultRequestConfig(requestConfig)
|
||||
.setConnectionManagerShared(true);
|
||||
if(credentialsProvider != null){
|
||||
httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
|
||||
}
|
||||
return httpClientBuilder
|
||||
.build();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -10,9 +10,9 @@ import org.apache.http.entity.mime.HttpMultipartMode;
|
|||
import org.apache.http.entity.mime.MultipartEntityBuilder;
|
||||
import org.apache.http.impl.client.DefaultHttpClient;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
import weaver.wechat.request.HttpManager;
|
||||
import weaver.xiao.commons.exception.RequestException;
|
||||
import weaver.zwl.common.ToolUtil;
|
||||
import weaver.wechat.request.HttpManager;
|
||||
|
||||
import javax.ws.rs.core.MediaType;
|
||||
import java.io.IOException;
|
||||
|
|
|
@ -0,0 +1,221 @@
|
|||
package weaver.xiao.commons.utils;
|
||||
|
||||
import com.jcraft.jsch.*;
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.nio.file.Files;
|
||||
import java.util.Iterator;
|
||||
import java.util.Properties;
|
||||
import java.util.Vector;
|
||||
|
||||
/**
|
||||
* @author XiaoBokang
|
||||
* @create 2022/8/10 12:07
|
||||
*/
|
||||
|
||||
public class SFTPClientUtil {
|
||||
|
||||
private final Logger logger = LogUtil.getLogger();
|
||||
|
||||
/**
|
||||
* 连接sftp服务器
|
||||
* @param host 主机
|
||||
* @param port 端口
|
||||
* @param username 用户名
|
||||
* @param password 密码
|
||||
* @return
|
||||
*/
|
||||
public ChannelSftp connect(String host, int port, String username, String password) {
|
||||
ChannelSftp sftp = null;
|
||||
Channel channel=null;
|
||||
Session sshSession=null;
|
||||
try {
|
||||
JSch jsch = new JSch();
|
||||
jsch.getSession(username, host, port);
|
||||
sshSession = jsch.getSession(username, host, port);
|
||||
if(logger.isInfoEnabled()){
|
||||
logger.info("***************** Session created. **********************");
|
||||
logger.info("***************** stf host is "+host+"port is "+port+" username is "+username+" password is "+password+"**********************");
|
||||
}
|
||||
sshSession.setPassword(password);
|
||||
Properties sshConfig = new Properties();
|
||||
sshConfig.put("StrictHostKeyChecking", "no");
|
||||
sshSession.setConfig(sshConfig);
|
||||
sshSession.connect();
|
||||
if(logger.isInfoEnabled()){
|
||||
logger.info("***************** Session connected. **********************");
|
||||
logger.info("***************** Opening Channel. **********************");
|
||||
}
|
||||
channel = sshSession.openChannel("sftp");
|
||||
channel.connect();
|
||||
sftp = (ChannelSftp) channel;
|
||||
if(logger.isInfoEnabled()){
|
||||
logger.info("***************** Connected to " + host + ". **********************");
|
||||
}
|
||||
} catch (Throwable e) {
|
||||
if (channel!=null) {
|
||||
try {
|
||||
channel.disconnect();
|
||||
}catch(Throwable e1) {
|
||||
}
|
||||
}
|
||||
|
||||
if (sshSession!=null) {
|
||||
try {
|
||||
sshSession.disconnect();
|
||||
}catch(Throwable e1) {
|
||||
}
|
||||
}
|
||||
|
||||
logger.error(e.getMessage(), e);
|
||||
}
|
||||
return sftp;
|
||||
}
|
||||
|
||||
/**
|
||||
* 关闭连接
|
||||
* @param sftp
|
||||
*/
|
||||
public void disconnect(String host, ChannelSftp sftp){
|
||||
// 关闭连接
|
||||
try {
|
||||
if (null != sftp) {
|
||||
sftp.disconnect();
|
||||
if(logger.isInfoEnabled()){
|
||||
logger.info("***************** Closing Channel. **********************");
|
||||
}
|
||||
if (null != sftp.getSession()) {
|
||||
sftp.getSession().disconnect();
|
||||
if(logger.isInfoEnabled()){
|
||||
logger.info("***************** Session disconnect. **********************");
|
||||
}
|
||||
}
|
||||
}
|
||||
if (logger.isInfoEnabled()) {
|
||||
logger.info("**************** Disconnect to " + host + ". *******************");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断目录下是否存在文件或者文件夹
|
||||
* @param directory 目录
|
||||
* @param fileName 文件
|
||||
* @param sftp 连接
|
||||
* @return 是否存在
|
||||
*/
|
||||
public boolean isExist(String directory, String fileName, ChannelSftp sftp) {
|
||||
try {
|
||||
Vector<ChannelSftp.LsEntry> v = listFiles(directory, sftp);
|
||||
Iterator<ChannelSftp.LsEntry> it = v.iterator();
|
||||
while (it.hasNext()) {
|
||||
ChannelSftp.LsEntry entry = it.next();
|
||||
String flName = entry.getFilename();
|
||||
if (flName.equals(fileName)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
} catch (SftpException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传文件
|
||||
* @param directory 上传的目录
|
||||
* @param uploadFile 要上传的文件
|
||||
* @param sftp 连接
|
||||
*/
|
||||
public boolean upload(String directory, String uploadFile, ChannelSftp sftp) {
|
||||
boolean successFlg = true;
|
||||
try {
|
||||
sftp.cd(directory);
|
||||
File file = new File(uploadFile);
|
||||
sftp.put(Files.newInputStream(file.toPath()), file.getName());
|
||||
if(logger.isInfoEnabled()){
|
||||
logger.info("***************** Finished **********************");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
successFlg = false;
|
||||
logger.info(LogUtil.getExceptionStr(e));
|
||||
}
|
||||
return successFlg;
|
||||
}
|
||||
|
||||
/**
|
||||
* 下载文件
|
||||
* @param directory 下载目录
|
||||
* @param downloadFile 下载的文件
|
||||
* @param saveFile 存在本地的路径
|
||||
* @param sftp 连接
|
||||
*/
|
||||
public boolean download(String directory, String downloadFile, String saveFile, ChannelSftp sftp) {
|
||||
boolean successFlg = true;
|
||||
try {
|
||||
sftp.cd(directory);
|
||||
File file = new File(saveFile);
|
||||
if(!file.getParentFile().exists()){
|
||||
file.getParentFile().mkdirs();
|
||||
}
|
||||
sftp.get(downloadFile, new FileOutputStream(file));
|
||||
if(logger.isInfoEnabled()){
|
||||
logger.info("***************** Finished **********************");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
successFlg = false;
|
||||
logger.info(LogUtil.getExceptionStr(e));
|
||||
}
|
||||
return successFlg;
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除文件
|
||||
* @param directory 要删除文件所在目录
|
||||
* @param deleteFile 要删除的文件
|
||||
* @param sftp 连接
|
||||
*/
|
||||
public void delete(String directory, String deleteFile, ChannelSftp sftp) {
|
||||
try {
|
||||
sftp.cd(directory);
|
||||
sftp.rm(deleteFile);
|
||||
if(logger.isInfoEnabled()){
|
||||
logger.info("***************** Finished **********************");
|
||||
}
|
||||
if (null != sftp) {
|
||||
sftp.disconnect();
|
||||
if (null != sftp.getSession()) {
|
||||
sftp.getSession().disconnect();
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 列出目录下的文件
|
||||
* @param directory 要列出的目录
|
||||
* @param sftp 连接
|
||||
* @return
|
||||
* @throws SftpException
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public Vector<ChannelSftp.LsEntry> listFiles(String directory, ChannelSftp sftp) throws SftpException {
|
||||
return sftp.ls(directory);
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断本地路径是否存在,不存在就创建路径
|
||||
*/
|
||||
public void makeDirs(String localSavePath) {
|
||||
File localFile = new File(localSavePath);
|
||||
if (!localFile.exists()) {
|
||||
localFile.mkdirs();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -8,7 +8,6 @@ import weaver.general.TimeUtil;
|
|||
import weaver.general.Util;
|
||||
import weaver.xiao.commons.utils.annotation.SqlFieldMapping;
|
||||
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.*;
|
||||
|
||||
|
@ -59,6 +58,7 @@ public class SqlUtil {
|
|||
case 2:{
|
||||
valueText = this.getFieldVal(fieldType,value);
|
||||
}break;
|
||||
default:break;
|
||||
}
|
||||
field.set(res,valueText);
|
||||
}
|
||||
|
@ -78,18 +78,51 @@ public class SqlUtil {
|
|||
*/
|
||||
public Object getFieldVal(Class<?> fieldType,String tempValue){
|
||||
Object fieldVal = null;
|
||||
if(fieldType.equals(int.class)){
|
||||
if(fieldType.equals(Integer.class)){
|
||||
fieldVal = Util.getIntValue(tempValue);
|
||||
}else if(fieldType.equals(String.class)){
|
||||
fieldVal = Util.null2String(tempValue);
|
||||
}else if(fieldType.equals(boolean.class)){
|
||||
}else if(fieldType.equals(Boolean.class)){
|
||||
fieldVal = Boolean.parseBoolean(tempValue);
|
||||
}else if(fieldType.equals(Double.class)){
|
||||
fieldVal = Util.getDoubleValue(tempValue,0.0);
|
||||
}else {
|
||||
fieldVal = "";
|
||||
}
|
||||
return fieldVal;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据表名和条件获取结果集
|
||||
* @param tableName 表名
|
||||
* @param param 参数
|
||||
* @return 数据集合
|
||||
*/
|
||||
public RecordSet getTableMessage(String tableName, Map<String,Object> param){
|
||||
List<Object> paramList = new ArrayList<>();
|
||||
RecordSet recordSet = new RecordSet();
|
||||
Set<Map.Entry<String, Object>> entries = param.entrySet();
|
||||
StringBuilder whereBuilder = new StringBuilder();
|
||||
whereBuilder.append("select * from ").append(tableName);
|
||||
for (Map.Entry<String, Object> entry : entries) {
|
||||
if("cusSql".equals(entry.getKey())){
|
||||
whereBuilder.append(" and ").append(entry.getValue());
|
||||
continue;
|
||||
}
|
||||
whereBuilder.append(" and ")
|
||||
.append(entry.getKey())
|
||||
.append(" = ? ");
|
||||
paramList.add(entry.getValue());
|
||||
}
|
||||
String whereStr = whereBuilder.toString();
|
||||
if(!"".equals(whereStr)){
|
||||
whereStr = whereStr.replaceFirst(" and"," where");
|
||||
}
|
||||
logger.info("执行查询的sql>>>" + whereStr + " paramList>>>" + paramList);
|
||||
recordSet.executeQuery(whereStr,paramList);
|
||||
return recordSet;
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建更新语句
|
||||
* @param tableName 表名
|
||||
|
@ -120,6 +153,10 @@ public class SqlUtil {
|
|||
updateBuilder.append(tableName).append(" set ");
|
||||
Set<Map.Entry<String, Object>> updateEntries = updateParam.entrySet();
|
||||
for (Map.Entry<String, Object> updateEntry : updateEntries) {
|
||||
if("cusSql".equals(updateEntry.getKey())){
|
||||
updateBuilder.append(updateEntry.getValue());
|
||||
continue;
|
||||
}
|
||||
updateBuilder.append(updateEntry.getKey())
|
||||
.append(" = ?,");
|
||||
paramList.add(updateEntry.getValue());
|
||||
|
@ -127,6 +164,10 @@ public class SqlUtil {
|
|||
StringBuilder whereBuilder = new StringBuilder();
|
||||
Set<Map.Entry<String, Object>> whereEntries = whereParam.entrySet();
|
||||
for (Map.Entry<String, Object> whereEntry : whereEntries) {
|
||||
if("cusSql".equals(whereEntry.getKey())){
|
||||
whereBuilder.append(" and ").append(whereEntry.getValue());
|
||||
continue;
|
||||
}
|
||||
whereBuilder.append(" and ")
|
||||
.append(whereEntry.getKey())
|
||||
.append(" = ? ");
|
||||
|
@ -257,4 +298,22 @@ public class SqlUtil {
|
|||
return modeId;
|
||||
}
|
||||
|
||||
/**
|
||||
* 全角转半角
|
||||
* @param input
|
||||
* @return
|
||||
*/
|
||||
public String toDBC(String input) {
|
||||
char c[] = input.toCharArray();
|
||||
for (int i = 0; i < c.length; i++) {
|
||||
if (c[i] == '\u3000') {
|
||||
c[i] = ' ';
|
||||
} else if (c[i] > '\uFF00' && c[i] < '\uFF5F') {
|
||||
c[i] = (char) (c[i] - 65248);
|
||||
}
|
||||
}
|
||||
String returnString = new String(c);
|
||||
return returnString;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,328 @@
|
|||
package weaver.xiao.commons.utils;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.type.CollectionType;
|
||||
import org.apache.log4j.Logger;
|
||||
import org.jdom.Document;
|
||||
import org.jdom.input.SAXBuilder;
|
||||
import org.xml.sax.InputSource;
|
||||
import weaver.xiao.commons.exception.ValueDealException;
|
||||
import weaver.xiao.commons.exception.VerifyException;
|
||||
import weaver.xiao.commons.utils.annotation.JsonParse;
|
||||
import weaver.xiao.commons.utils.annotation.RequiredVerify;
|
||||
|
||||
import javax.xml.bind.JAXBContext;
|
||||
import javax.xml.bind.Marshaller;
|
||||
import javax.xml.bind.Unmarshaller;
|
||||
import java.io.InputStream;
|
||||
import java.io.StringReader;
|
||||
import java.io.StringWriter;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.ParameterizedType;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* @author XiaoBokang
|
||||
* @create 2022/7/28 12:38
|
||||
*/
|
||||
|
||||
public class VerifyUtil {
|
||||
|
||||
private static final Logger logger = LogUtil.getLogger();
|
||||
|
||||
public static <T> void requireVerify(T t) throws IllegalAccessException {
|
||||
Class<?> clazz = t.getClass();
|
||||
Field[] declaredFields = clazz.getDeclaredFields();
|
||||
for (Field declaredField : declaredFields) {
|
||||
declaredField.setAccessible(true);
|
||||
boolean annotationPresent = declaredField.isAnnotationPresent(RequiredVerify.class);
|
||||
if (annotationPresent) {
|
||||
Object value = declaredField.get(t);
|
||||
Class<?> type = declaredField.getType();
|
||||
boolean valueIsNull = isNull(type, value);
|
||||
RequiredVerify annotation = declaredField.getAnnotation(RequiredVerify.class);
|
||||
if (valueIsNull) {
|
||||
throw new VerifyException(annotation.value());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static <T> T xmlStrToObject(Class<T> clazz, String xmlStr) throws Exception {
|
||||
T t = null;
|
||||
JAXBContext context = JAXBContext.newInstance(clazz);
|
||||
// 进行将Xml转成对象的核心接口
|
||||
Unmarshaller unmarshaller = context.createUnmarshaller();
|
||||
StringReader sr = new StringReader(xmlStr);
|
||||
t = (T) unmarshaller.unmarshal(sr);
|
||||
dealJsonStr(clazz, t);
|
||||
return t;
|
||||
}
|
||||
|
||||
public static <T> T xmlStrToObject(Class<T> clazz, InputStream inputStream) throws Exception {
|
||||
T t = null;
|
||||
JAXBContext context = JAXBContext.newInstance(clazz);
|
||||
// 进行将Xml转成对象的核心接口
|
||||
Unmarshaller unmarshaller = context.createUnmarshaller();
|
||||
t = (T) unmarshaller.unmarshal(inputStream);
|
||||
dealJsonStr(clazz, t);
|
||||
return t;
|
||||
}
|
||||
|
||||
private static <T> T dealJsonStr(Class<T> clazz, T t) throws Exception {
|
||||
Field[] declaredFields = clazz.getDeclaredFields();
|
||||
for (Field declaredField : declaredFields) {
|
||||
declaredField.setAccessible(true);
|
||||
boolean annotationPresent = declaredField.isAnnotationPresent(JsonParse.class);
|
||||
if (annotationPresent) {
|
||||
JsonParse annotation = declaredField.getAnnotation(JsonParse.class);
|
||||
String sourceFieldName = annotation.value();
|
||||
Field sourceField = clazz.getDeclaredField(sourceFieldName);
|
||||
String sourceMethodName = "get" + sourceFieldName.substring(0, 1).toUpperCase() + sourceFieldName.substring(1);
|
||||
Method declaredMethod = clazz.getDeclaredMethod(sourceMethodName);
|
||||
Object sourceValue = declaredMethod.invoke(t);
|
||||
Class<?> sourceType = sourceField.getType();
|
||||
if (isNotNull(sourceType, sourceValue)) {
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
String targetFieldName = declaredField.getName();
|
||||
Class<?> targetType = declaredField.getType();
|
||||
String targetMethodName = "set" + targetFieldName.substring(0, 1).toUpperCase() + targetFieldName.substring(1);
|
||||
Method targetMethod = clazz.getDeclaredMethod(targetMethodName, targetType);
|
||||
if (targetType.equals(List.class)) {
|
||||
Type genericType = declaredField.getGenericType();
|
||||
ParameterizedType pt = (ParameterizedType) genericType;
|
||||
//得到泛型里的class类型对象
|
||||
Class<?> genericClazz = (Class<?>) pt.getActualTypeArguments()[0];
|
||||
CollectionType listType = objectMapper.getTypeFactory().constructCollectionType(ArrayList.class, genericClazz);
|
||||
List<Object> list = objectMapper.readValue(sourceValue.toString(), listType);
|
||||
targetMethod.invoke(t, list);
|
||||
} else {
|
||||
Object o = objectMapper.readValue(sourceValue.toString(), targetType);
|
||||
targetMethod.invoke(t, o);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return t;
|
||||
}
|
||||
|
||||
public static <T> String beanToXml(T t, String encoding) {
|
||||
String xmlStr = "";
|
||||
try {
|
||||
//1、创建上下文
|
||||
JAXBContext context = JAXBContext.newInstance(t.getClass());
|
||||
StringWriter writer = new StringWriter();
|
||||
//2、创建Marshaller 这个是用于转换成xml的
|
||||
Marshaller marshaller = context.createMarshaller();
|
||||
//3、设置marshaller的属性
|
||||
//JAXB_FORMATTED_OUTPUT:格式化
|
||||
//JAXB_ENCODING:编码格式
|
||||
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
|
||||
marshaller.setProperty(Marshaller.JAXB_ENCODING, encoding);
|
||||
//4、将实体类读取到writer中
|
||||
marshaller.marshal(t, writer);
|
||||
xmlStr = writer.toString();
|
||||
} catch (Exception e) {
|
||||
logger.info("java bean 转换 xml异常 ==>" + LogUtil.getExceptionStr(e));
|
||||
}
|
||||
return xmlStr;
|
||||
}
|
||||
|
||||
public static String mapToXml(Map<String, Object> map) {
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
stringBuilder.append("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>").append("\n").append("<root>").append("\n");
|
||||
String xmlBody = mapDeal(map);
|
||||
stringBuilder.append(xmlBody).append("</root>");
|
||||
return stringBuilder.toString();
|
||||
}
|
||||
|
||||
public static String mapDeal(Map<String, Object> map) {
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
Set<Map.Entry<String, Object>> entries = map.entrySet();
|
||||
for (Map.Entry<String, Object> entry : entries) {
|
||||
String key = entry.getKey();
|
||||
Object value = entry.getValue();
|
||||
stringBuilder.append("\t").append("<").append(key).append(">");
|
||||
if (value instanceof Map) {
|
||||
String s = mapDeal((Map<String, Object>) value);
|
||||
stringBuilder.append(s);
|
||||
} else if (value instanceof List) {
|
||||
String nodeName = "node";
|
||||
String s = listDeal((List<Object>) value, nodeName);
|
||||
stringBuilder.append(s);
|
||||
} else {
|
||||
stringBuilder.append(value);
|
||||
}
|
||||
stringBuilder.append("</").append(key).append(">").append("\n");
|
||||
}
|
||||
return stringBuilder.toString();
|
||||
}
|
||||
|
||||
public static String listDeal(List<Object> list, String nodeName) {
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
for (Object o : list) {
|
||||
stringBuilder.append("\t").append("<").append(nodeName).append(">");
|
||||
if (o instanceof Map) {
|
||||
String s = mapToXml((Map<String, Object>) o);
|
||||
stringBuilder.append(s);
|
||||
} else if (o instanceof List) {
|
||||
throw new ValueDealException("不支持嵌套list");
|
||||
} else {
|
||||
stringBuilder.append(o);
|
||||
}
|
||||
stringBuilder.append("</").append(nodeName).append(">").append("\n");
|
||||
}
|
||||
return stringBuilder.toString();
|
||||
}
|
||||
|
||||
|
||||
public static Document convertStringToDocument(String xmlStr) {
|
||||
SAXBuilder sb = new SAXBuilder();
|
||||
try {
|
||||
return sb.build(new InputSource(new StringReader(xmlStr)));
|
||||
} catch (Exception e) {
|
||||
logger.info("xml字符串解析异常 ==>" + LogUtil.getExceptionStr(e));
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static Document convertStringToDocument(InputStream inputStream) {
|
||||
SAXBuilder sb = new SAXBuilder();
|
||||
try {
|
||||
return sb.build(inputStream);
|
||||
} catch (Exception e) {
|
||||
logger.info("xml字符串解析异常 ==>" + LogUtil.getExceptionStr(e));
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
public static boolean isNull(Class<?> type, Object value) {
|
||||
if (type.equals(String.class)) {
|
||||
return isBlank(value);
|
||||
} else {
|
||||
return value == null;
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean isNotNull(Class<?> type, Object value) {
|
||||
return !isNull(type, value);
|
||||
}
|
||||
|
||||
|
||||
public static boolean isNotBlank(Object value) {
|
||||
return !isBlank(value);
|
||||
}
|
||||
|
||||
public static boolean isBlank(Object value) {
|
||||
if (value == null) {
|
||||
return true;
|
||||
}
|
||||
String cs = String.valueOf(value);
|
||||
int strLen;
|
||||
if ((strLen = cs.length()) == 0) {
|
||||
return true;
|
||||
}
|
||||
for (int i = 0; i < strLen; i++) {
|
||||
if (!Character.isWhitespace(cs.charAt(i))) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* <h2>驼峰转下划线</h2>
|
||||
* @param sourceMap 原参数
|
||||
* @return 转换后的map
|
||||
*/
|
||||
public static Map<String, Object> humpToUnderlineMap(Map<String, Object> sourceMap) {
|
||||
return paramConversion(sourceMap, 2);
|
||||
}
|
||||
|
||||
/**
|
||||
* <h2>下划线转驼峰</h2>
|
||||
* @param sourceMap 原参数
|
||||
* @return 转换后的map
|
||||
*/
|
||||
public static Map<String, Object> underlineToHumpMap(Map<String, Object> sourceMap) {
|
||||
return paramConversion(sourceMap, 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* <h2>参数转换</h2>
|
||||
* @param sourceMap 原参数信息
|
||||
* @param conversionType 转换类型 1:下划线转驼峰;2:驼峰转下划线
|
||||
* @return 转换后的map
|
||||
*/
|
||||
public static Map<String, Object> paramConversion(Map<String, Object> sourceMap, int conversionType) {
|
||||
if (sourceMap == null || sourceMap.isEmpty()) {
|
||||
return sourceMap;
|
||||
}
|
||||
Map<String, Object> targetMap = new HashMap<>();
|
||||
Set<Map.Entry<String, Object>> entries = sourceMap.entrySet();
|
||||
for (Map.Entry<String, Object> entry : entries) {
|
||||
String key = entry.getKey();
|
||||
Object value = entry.getValue();
|
||||
if (conversionType == 1) {
|
||||
key = toCamelCase(key);
|
||||
} else {
|
||||
key = toUnderlineCase(key);
|
||||
}
|
||||
targetMap.put(key, value);
|
||||
}
|
||||
return targetMap;
|
||||
}
|
||||
|
||||
/**
|
||||
* <h2>驼峰转下划线方法</h2>
|
||||
* @param sourceString 原字符串
|
||||
* @return 转换后字符串
|
||||
*/
|
||||
public static String toUnderlineCase(String sourceString) {
|
||||
if (VerifyUtil.isBlank(sourceString)) {
|
||||
return sourceString;
|
||||
}
|
||||
StringBuffer stringBuffer = new StringBuffer();
|
||||
char[] chars = sourceString.toCharArray();
|
||||
for (char aChar : chars) {
|
||||
if (aChar >= 65 && aChar <= 90) {
|
||||
stringBuffer.append("_").append(aChar += 32);
|
||||
} else {
|
||||
stringBuffer.append(aChar);
|
||||
}
|
||||
}
|
||||
return stringBuffer.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* <h2>下划线转驼峰</h2>
|
||||
* @param sourceString 原字符串
|
||||
* @return 转换后字符串
|
||||
*/
|
||||
public static String toCamelCase(String sourceString) {
|
||||
if (VerifyUtil.isBlank(sourceString)) {
|
||||
return sourceString;
|
||||
}
|
||||
StringBuffer stringBuffer = new StringBuffer();
|
||||
char[] chars = sourceString.toCharArray();
|
||||
boolean isConversion = false;
|
||||
for (char aChar : chars) {
|
||||
//判断是否为'_'
|
||||
if (aChar == 95) {
|
||||
isConversion = true;
|
||||
continue;
|
||||
}
|
||||
if (isConversion) {
|
||||
stringBuffer.append(aChar -= 32);
|
||||
isConversion = false;
|
||||
} else {
|
||||
stringBuffer.append(aChar);
|
||||
}
|
||||
}
|
||||
return stringBuffer.toString();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
package weaver.xiao.commons.utils.annotation;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* <h1>标记于字段上用于JSON转换</h1>
|
||||
*
|
||||
* @author XiaoBokang
|
||||
* @create 2022/8/5 15:13
|
||||
*/
|
||||
|
||||
@Target({ElementType.FIELD})
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface JsonParse {
|
||||
String value();
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
package weaver.xiao.commons.utils.annotation;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* @author XiaoBokang
|
||||
* @create 2022/7/28 12:11
|
||||
*/
|
||||
|
||||
@Target({ElementType.FIELD,ElementType.TYPE})
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface RequiredVerify {
|
||||
String value();
|
||||
}
|
|
@ -223,4 +223,8 @@ public class TestOrganization extends BaseTest {
|
|||
System.out.println(JSON.toJSONString(orgChartTree));
|
||||
}
|
||||
|
||||
<<<<<<< HEAD
|
||||
=======
|
||||
|
||||
>>>>>>> df1e8bb8c13ef138291cbf85631880cd9e5505c8
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue