|
@@ -1,17 +1,26 @@
|
|
|
package com.persagy.filemove.service;
|
|
|
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.persagy.filemove.app.SagaFileMoveApp;
|
|
|
import com.persagy.filemove.dto.SagaFileMoveDTO;
|
|
|
+import com.persagy.filemove.dto.TransErrDTO;
|
|
|
import com.persagy.filemove.util.HttpTools;
|
|
|
+import com.persagy.filemove.util.JsonTools;
|
|
|
|
|
|
import java.util.HashMap;
|
|
|
+import java.util.HashSet;
|
|
|
import java.util.Map;
|
|
|
+import java.util.Set;
|
|
|
|
|
|
public class SagaFileMoveService {
|
|
|
private SagaFileMoveDTO dto;
|
|
|
private static final String resp_success = "success";
|
|
|
private static final String testKey = "persagyAndSagaTest2020121130";
|
|
|
private static final String url_pj_get = "/mng/project/query";
|
|
|
- private static final String url_source_get = "BASE/common/TYPE_get?systemId=SYSID&key=KEY";
|
|
|
+ private static final String url_obj_query = "/object/subset_query?projectId=PJID&secret=SEC";
|
|
|
+ private static final String url_source_get = "/common/TYPE_get?systemId=SYSID&key=";
|
|
|
+ private static final String url_source_send = "/common/TYPE_upload?systemId=SYSID&secret=SEC&overwrite=true&key=";
|
|
|
|
|
|
public SagaFileMoveService(SagaFileMoveDTO dto) {
|
|
|
this.dto = dto;
|
|
@@ -35,6 +44,205 @@ public class SagaFileMoveService {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
+ * 资源转移逻辑
|
|
|
+ */
|
|
|
+ public void resourceMove(SagaFileMoveApp app) {
|
|
|
+ final String tipPrefix = "文件迁移:";
|
|
|
+ Map<String, String> obj_key = requestSourceKey(app);
|
|
|
+ if(!obj_key.isEmpty()) {
|
|
|
+ int totalNum = obj_key.size();
|
|
|
+
|
|
|
+ // 文件不存在对象id集合
|
|
|
+ Set<TransErrDTO> noFileSet = new HashSet<>();
|
|
|
+ // 资源key为null的对象id集合
|
|
|
+ Set<TransErrDTO> nullKeySet = new HashSet<>();
|
|
|
+ // 文件转移失败的对象id集合
|
|
|
+ Set<TransErrDTO> moveErrorSet = new HashSet<>();
|
|
|
+
|
|
|
+ int handleIndex = 1;
|
|
|
+
|
|
|
+ // 默认使用image_get下载文件
|
|
|
+ String getApiType = "image";
|
|
|
+ if("file".equals(dto.imgFromApiType.getValue())) {
|
|
|
+ getApiType = "file";
|
|
|
+ }
|
|
|
+
|
|
|
+ // 默认使用file_upload上传文件
|
|
|
+ String toImgApi = "file";
|
|
|
+ if("image".equals(dto.imgToApiType.getValue())) {
|
|
|
+ toImgApi = "image";
|
|
|
+ }
|
|
|
+
|
|
|
+ String getUrl = dto.imgFromUrl.getValue() + url_source_get;
|
|
|
+ getUrl = getUrl.replace("TYPE", getApiType)
|
|
|
+ .replace("SYSID", dto.imgFromSysId.getValue());
|
|
|
+
|
|
|
+ for(String objId : obj_key.keySet()) {
|
|
|
+ app.showTipsInfo(tipPrefix + handleIndex + " / " + totalNum);
|
|
|
+
|
|
|
+ fileMove(objId, obj_key.get(objId), getUrl, toImgApi, noFileSet, nullKeySet, moveErrorSet);
|
|
|
+
|
|
|
+ handleIndex ++;
|
|
|
+ }
|
|
|
+
|
|
|
+ String tips = app.getTips();
|
|
|
+ int noFileNum = noFileSet.size(), nullKeyNum = nullKeySet.size(), moveErrNum = moveErrorSet.size();
|
|
|
+ int errorNum = noFileNum + nullKeyNum + moveErrNum;
|
|
|
+ if(errorNum > 0) {
|
|
|
+ tips = tips + " 执行结束,转移失败数:" + (totalNum - errorNum) + "。详情见日志文件。";
|
|
|
+ app.showTipsError(tips);
|
|
|
+
|
|
|
+ StringBuilder sb = null;
|
|
|
+
|
|
|
+ if(noFileNum > 0) {
|
|
|
+ sb = new StringBuilder("对象关联的文件不存在的数量:" + noFileNum + "。详情:\n");
|
|
|
+ for(TransErrDTO item : noFileSet) {
|
|
|
+ sb.append(JsonTools.beanToJson(item) + "\n");
|
|
|
+ }
|
|
|
+ app.writeToLogFile(sb.toString());
|
|
|
+ }
|
|
|
+
|
|
|
+ if(nullKeyNum > 0) {
|
|
|
+ sb = new StringBuilder("对象关联文件key为空的数量:" + nullKeyNum + "。详情:\n");
|
|
|
+ for(TransErrDTO item : nullKeySet) {
|
|
|
+ sb.append(JsonTools.beanToJson(item) + "\n");
|
|
|
+ }
|
|
|
+ app.writeToLogFile(sb.toString());
|
|
|
+ }
|
|
|
+
|
|
|
+ if(moveErrNum > 0) {
|
|
|
+ sb = new StringBuilder("文件迁移出错的数量:" + moveErrNum + "。详情:\n");
|
|
|
+ for(TransErrDTO item : moveErrorSet) {
|
|
|
+ sb.append(JsonTools.beanToJson(item) + "\n");
|
|
|
+ }
|
|
|
+ app.writeToLogFile(sb.toString());
|
|
|
+ }
|
|
|
+ }else {
|
|
|
+ app.showTipsInfo(tips + " 执行结束,转移成功数:" + totalNum);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 在两个文件服务器之间转移文件
|
|
|
+ * @param objId 对象id
|
|
|
+ * @param key 文件资源的key
|
|
|
+ * @param getUrl 获取资源的url
|
|
|
+ * @param toImgApi 上传文件使用的接口类型
|
|
|
+ * @param noFileSet 文件不存在对象id集合
|
|
|
+ * @param nullKeySet 资源key为null的对象id集合
|
|
|
+ * @param moveErrorSet 文件转移失败的对象id集合
|
|
|
+ */
|
|
|
+ private void fileMove(String objId, String key, String getUrl, String toImgApi, Set<TransErrDTO> noFileSet, Set<TransErrDTO> nullKeySet, Set<TransErrDTO> moveErrorSet) {
|
|
|
+ if(key == null || key.trim().length() == 0) {
|
|
|
+ // 文件资源key值为空
|
|
|
+ nullKeySet.add(new TransErrDTO(objId, key, "", "", ""));
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ getUrl = getUrl + key;
|
|
|
+
|
|
|
+ if(key.endsWith(".jsonz")) {
|
|
|
+ // 对于.jsonz文件,从file_get接口获取
|
|
|
+ getUrl = getUrl.replace("image_get","file_get");
|
|
|
+ }
|
|
|
+
|
|
|
+ String sendUrl = dto.imgToUrl.getValue() + url_source_send;
|
|
|
+ sendUrl = sendUrl.replace("TYPE", toImgApi)
|
|
|
+ .replace("SYSID", dto.imgToSysId.getValue())
|
|
|
+ .replace("SEC", dto.imgToSecret.getValue())
|
|
|
+ + key;
|
|
|
+
|
|
|
+
|
|
|
+ try {
|
|
|
+ byte[] byteData = HttpTools.httpGetFile(getUrl);
|
|
|
+ if(byteData != null) {
|
|
|
+ if(byteData.length == 17) {
|
|
|
+ // 文件不存在
|
|
|
+ noFileSet.add(new TransErrDTO(objId, key, "not exist", getUrl, ""));
|
|
|
+ }else {
|
|
|
+ String uploadResult = HttpTools.httpPostRequest(sendUrl, byteData);
|
|
|
+ if(null == uploadResult || !uploadResult.contains(resp_success)) {
|
|
|
+ // 文件转移失败
|
|
|
+ moveErrorSet.add(new TransErrDTO(objId, key, "downloadSuccess uploadError", getUrl, sendUrl));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }catch (Exception e){
|
|
|
+ // 文件转移报错
|
|
|
+ moveErrorSet.add(new TransErrDTO(objId, key, "serviceError:" + e.getMessage(), getUrl, sendUrl));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 从数据平台获取对象关联的资源key
|
|
|
+ * @param app
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private Map<String, String> requestSourceKey(SagaFileMoveApp app) {
|
|
|
+ Map<String, String> obj_key = new HashMap<>();
|
|
|
+
|
|
|
+ app.showTipsInfo("查询数据平台对象,获取文件资源key...");
|
|
|
+
|
|
|
+ String reqUrl = dto.dpf.getValue() + url_obj_query;
|
|
|
+ reqUrl = reqUrl.replace("PJID", dto.pjId.getValue()).replace("SEC", dto.pjSecret.getValue());
|
|
|
+
|
|
|
+ JSONObject paramObj = new JSONObject();
|
|
|
+
|
|
|
+ JSONObject criteria = new JSONObject();
|
|
|
+ criteria.put("type", JSONArray.parse("["+dto.objType.getValue()+"]"));
|
|
|
+
|
|
|
+ JSONObject returnInfos = new JSONObject();
|
|
|
+ returnInfos.put("returnInfos", JSONArray.parse("["+dto.objInfoCode.getValue()+"]"));
|
|
|
+
|
|
|
+ paramObj.put("customInfo", true);
|
|
|
+ paramObj.put("criteria", criteria);
|
|
|
+ paramObj.put("returnInfos", returnInfos);
|
|
|
+
|
|
|
+ String resp = null;
|
|
|
+ try{
|
|
|
+ resp = HttpTools.httpPostJson(reqUrl, paramObj);
|
|
|
+ app.showTipsInfo("查询数据平台对象完成。");
|
|
|
+ }catch (Exception e){
|
|
|
+ app.showTipsError("请求数据平台出错:" + e.getMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ if(null != resp) {
|
|
|
+ app.showTipsInfo("查询结果转为Json对象...");
|
|
|
+
|
|
|
+ JSONObject respJson = null;
|
|
|
+ try {
|
|
|
+ respJson = JsonTools.strToJsonObj(resp);
|
|
|
+ }catch (Exception e) {
|
|
|
+ app.showTipsError("String转Json对象失败:" + e.getMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ if(respJson != null && resp_success.equals(respJson.getString("Result"))) {
|
|
|
+ JSONArray content = respJson.getJSONArray("Content");
|
|
|
+ if(null != content && !content.isEmpty()) {
|
|
|
+ String objId = null, sourceKey = null;
|
|
|
+
|
|
|
+ for(int i=0; i<content.size(); i++) {
|
|
|
+ objId = content.getJSONObject(i).getString("id");
|
|
|
+ JSONObject infoJson = content.getJSONObject(i).getJSONObject("infos");
|
|
|
+
|
|
|
+ if(null != infoJson) {
|
|
|
+ sourceKey = infoJson.getString(dto.objInfoCode.getValue());
|
|
|
+ }
|
|
|
+ obj_key.put(objId, sourceKey);
|
|
|
+ }
|
|
|
+ }else {
|
|
|
+ app.showTipsWarn("结束。查询到数据平台对象数量为0。");
|
|
|
+ }
|
|
|
+ }else {
|
|
|
+ app.showTipsError("请求数据平台数据出错:" + respJson.getString("ResultMsg"));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return obj_key;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
* 校验文件服务From是否可访问
|
|
|
* @return
|
|
|
*/
|