AdmEntityTransferUtil.java 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. package com.persagy.proxy.adm.utils;
  2. import cn.hutool.core.collection.CollUtil;
  3. import cn.hutool.core.util.ArrayUtil;
  4. import cn.hutool.core.util.StrUtil;
  5. import com.fasterxml.jackson.databind.JsonNode;
  6. import com.fasterxml.jackson.databind.ObjectMapper;
  7. import com.fasterxml.jackson.databind.node.ArrayNode;
  8. import com.fasterxml.jackson.databind.node.ObjectNode;
  9. import com.persagy.dmp.basic.utils.JsonNodeUtils;
  10. import com.persagy.dmp.common.exception.BusinessException;
  11. import com.persagy.dmp.common.helper.SpringHelper;
  12. import java.io.IOException;
  13. import java.util.Iterator;
  14. import java.util.List;
  15. import java.util.Set;
  16. /**
  17. * ADM通用查询条件
  18. * @author Charlie Yu
  19. * @date 2021-08-16
  20. */
  21. public class AdmEntityTransferUtil {
  22. /**
  23. * 转换为数据中台对象
  24. * @param objList
  25. * @return
  26. */
  27. public static ArrayNode toDmpMultiEntity(List objList) {
  28. ArrayNode arrayNode = JsonNodeUtils.toArrayNode(objList, "infos", null);
  29. if(arrayNode == null) {
  30. return arrayNode;
  31. }
  32. // outline特殊处理
  33. for(int i = 0;i < arrayNode.size();i++) {
  34. ObjectNode jsonNode = (ObjectNode) arrayNode.get(i);
  35. JsonNode outLine = jsonNode.get("outline");
  36. if(outLine != null && !outLine.isTextual()) {
  37. jsonNode.remove("outline");
  38. jsonNode.put("outline", outLine.toString());
  39. }
  40. JsonNode nullNode = jsonNode.get("nullList");
  41. if(nullNode == null || !nullNode.isArray()) {
  42. continue;
  43. }
  44. ArrayNode nullList = (ArrayNode) nullNode;
  45. ArrayNode removeNode = jsonNode.putArray("$remove");
  46. for(int k=0; k < nullList.size(); k++){
  47. String node = nullList.get(k).textValue();
  48. if(StrUtil.isBlank(node)) {
  49. continue;
  50. }
  51. if(node.startsWith("infos.")){
  52. node = node.replace("infos.","");
  53. }
  54. jsonNode.remove(node);
  55. removeNode.add(node);
  56. }
  57. jsonNode.remove("nullList");
  58. }
  59. return arrayNode;
  60. }
  61. /**
  62. * 转换为数据中台对象
  63. * @param obj
  64. * @return
  65. */
  66. public static JsonNode toDmpSingleEntity(Object obj) {
  67. return JsonNodeUtils.toObjectNode(obj, "infos", null);
  68. }
  69. /**
  70. * 转换为数据中心对象
  71. * @param array 数据中台对象
  72. * @param clazz 数据中心对象class
  73. * @param <T>
  74. * @return
  75. */
  76. public static <T> List<T> toAdmMultiEntity(List array, List<String> projection, Class<T> clazz) {
  77. ArrayNode dataNode = JsonNodeUtils.toArrayNode(array, "infos", null);
  78. return toAdmMultiEntity(dataNode, projection, clazz);
  79. }
  80. public static <T> List<T> toAdmMultiEntityExtra(List array, List<String> projection, Class<T> clazz) {
  81. ArrayNode dataNode = JsonNodeUtils.toArrayNode(array, null, null);
  82. return toAdmMultiEntityExtra(dataNode, projection, clazz);
  83. }
  84. /**
  85. * 转换为数据中心对象
  86. * @param array
  87. * @param clazz
  88. * @param <T>
  89. * @return
  90. */
  91. public static <T> List<T> toAdmMultiEntity(ArrayNode array, List<String> projection, Class<T> clazz) {
  92. if(ArrayUtil.isEmpty(array)) {
  93. return null;
  94. }
  95. // 转换属性名
  96. for(int i = 0, j = array.size();i < j;i++) {
  97. JsonNode node = array.get(i);
  98. if(node != null && node.isObject()) {
  99. resetAdmProperty((ObjectNode) node, projection);
  100. }
  101. }
  102. return JsonNodeUtils.toEntity(array, clazz, "infos");
  103. /*try {
  104. return JSONEntityUtil.toEntity(array, clazz, "infos");
  105. } catch (IOException e) {
  106. e.printStackTrace();
  107. }
  108. return null;*/
  109. }
  110. public static <T> List<T> toAdmMultiEntityExtra(ArrayNode array, List<String> projection, Class<T> clazz) {
  111. if(ArrayUtil.isEmpty(array)) {
  112. return null;
  113. }
  114. // 转换属性名
  115. for(int i = 0, j = array.size();i < j;i++) {
  116. JsonNode node = array.get(i);
  117. if(node != null && node.isObject()) {
  118. resetAdmProperty((ObjectNode) node, projection);
  119. }
  120. }
  121. try {
  122. return JSONEntityUtil.toEntity(array, clazz, "infos");
  123. } catch (IOException e) {
  124. e.printStackTrace();
  125. }
  126. return null;
  127. }
  128. /**
  129. * 转换为数据中心对象
  130. * @param node
  131. * @param clazz
  132. * @param <T>
  133. * @return
  134. */
  135. public static <T> T toAdmSingleEntity(ObjectNode node, List<String> projection, Class<T> clazz) {
  136. resetAdmProperty(node, projection);
  137. return JsonNodeUtils.toEntity(node, clazz, "infos");
  138. }
  139. /**
  140. * 处理dmp转adm对象的属性名
  141. * @param node
  142. */
  143. private static void resetAdmProperty(ObjectNode node, List<String> projection) {
  144. // 转换属性名 TODO 目前只有创建时间和修改时间需要转换,后续有其他时通过建立映射在此转换即可
  145. copyProperty(node, "creationTime", "createTime");
  146. copyProperty(node, "modifiedTime", "lastUpdate");
  147. copyProperty(node, "objType", "objectType");
  148. // outline特殊处理
  149. JsonNode outLine = node.get("outline");
  150. if(outLine != null && outLine.isTextual()) {
  151. node.remove("outline");
  152. ArrayNode arrayNode = toSingleEntityQuietly(outLine.textValue(), ArrayNode.class);
  153. node.set("outline", arrayNode);
  154. }
  155. removeProperty(node, projection);
  156. }
  157. /**
  158. * 转换为指定对象
  159. * @param jsonStr
  160. * @param clazz
  161. * @param <T>
  162. * @return
  163. */
  164. public static <T> T toSingleEntityQuietly(String jsonStr, Class<T> clazz) {
  165. try {
  166. return SpringHelper.getBean(ObjectMapper.class).readValue(jsonStr, clazz);
  167. } catch (IOException e) {
  168. throw new BusinessException("outline格式解析失败");
  169. }
  170. }
  171. /**
  172. * 拷贝属性
  173. * @param dmpData 数据中台对象
  174. * @param srcProp 数据中台属性名
  175. * @param desProp 对应的数据中心属性名
  176. */
  177. private static void copyProperty(ObjectNode dmpData, String srcProp, String desProp) {
  178. if(dmpData == null) {
  179. return;
  180. }
  181. JsonNode value = dmpData.get(srcProp);
  182. if(value == null || value.isNull()) {
  183. return;
  184. }
  185. dmpData.set(desProp, value);
  186. dmpData.remove(srcProp);
  187. }
  188. /**
  189. * 删除掉数据中心不需要的属性
  190. * @param dmpData
  191. */
  192. private static void removeProperty(ObjectNode dmpData, List<String> projection){
  193. if(CollUtil.isNotEmpty(projection)) {
  194. // 转换为Set,提供遍历效率
  195. Set<String> fieldSet = CollUtil.newHashSet(projection);
  196. if(fieldSet.contains("localID")){//兼容adm前端数据
  197. fieldSet.add("localId");
  198. }
  199. Iterator<String> fields = dmpData.fieldNames();
  200. while (fields.hasNext()) {
  201. String field = fields.next();
  202. // 返回结果中包含的不处理
  203. if(fieldSet.contains(field)) {
  204. continue;
  205. }
  206. // 去掉不包含的属性
  207. fields.remove();
  208. }
  209. } else {
  210. // 返回全部时,去掉不需要属性
  211. List<String> removeList = CollUtil.newArrayList("creator","valid", "createApp", "virtualCodes", "updateApp", "grouping", "modifier");
  212. for(String removeField:removeList) {
  213. dmpData.remove(removeField);
  214. }
  215. }
  216. }
  217. }