Repository navigation
Expand file tree
/
Copy pathRequestUtils.java
More file actions
133 lines (121 loc) · 3.96 KB
/
Copy pathRequestUtils.java
File metadata and controls
133 lines (121 loc) · 3.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package com.wcode.util;
import com.alibaba.fastjson.JSONObject;
import com.wcode.annotations.Alias;
import org.apache.commons.lang.ArrayUtils;
import weaver.soa.workflow.request.*;
import java.lang.reflect.Field;
import java.util.*;
public class RequestUtils {
private static final int DEFAULT_SIZE = 0;
public static Map<String, String> convertToMap(RequestInfo request, String... fieldMarks) {
if (ArrayUtils.isEmpty(fieldMarks)) return new HashMap<>(DEFAULT_SIZE);
Set<String> set = new HashSet<>(Arrays.asList(fieldMarks));
return convertToMap(request, set);
}
public static List<Map<String, String>> convertToMap(DetailTable detailTable, String... fieldMarks) {
if (ArrayUtils.isEmpty(fieldMarks)) return new ArrayList<>(DEFAULT_SIZE);
Set<String> set = new HashSet<>(Arrays.asList(fieldMarks));
return convertToMap(detailTable, set);
}
public static List<Map<String, String>> convertToMap(DetailTable detailTable, Collection<String> set) {
Row[] rows = detailTable.getRow();
List<Map<String, String>> list = new ArrayList<>(detailTable.getRowCount());
Map<String, String> map;
int size = set.size();
for (Row row : rows) {
map = new HashMap<>(size);
for (Cell cell : row.getCell()) {
String name = cell.getName();
String val = cell.getValue();
if (set.contains(name)) {
map.put(name, val);
}
}
list.add(map);
}
return list;
}
public static Map<String, String> convertToMap(RequestInfo request, Collection<String> set) {
Property[] properties = request.getMainTableInfo().getProperty();
int len = set.size();
Map<String, String> map = new HashMap<>(len);
for (Property property : properties) {
if (len == map.size()) break;
String name = property.getName();
String val = property.getValue();
if (set.contains(name)) {
map.put(name, val);
}
}
return map;
}
public static <T> List<T> convert(DetailTable detailTable, Class<T> clazz) {
Map<String, String> fieldMap = getFieldMap(clazz);
if (fieldMap.size() == 0) {
return new ArrayList<>(DEFAULT_SIZE);
} else {
Row[] rows = detailTable.getRow();
List<T> list = new ArrayList<>(rows.length);
for (Row row : rows) {
T object = convertToObject(fieldMap, row, clazz);
list.add(object);
}
return list;
}
}
public static <T> T convert(RequestInfo request, Class<T> clazz) {
Map<String, String> fieldMap = getFieldMap(clazz);
JSONObject json = convertJson(fieldMap, request.getMainTableInfo().getProperty());
return json.toJavaObject(clazz);
}
private static <T> T convertToObject(Map<String, String> fieldMap, Row row, Class<T> clazz) {
Cell[] cells = row.getCell();
int count = 0;
JSONObject json = new JSONObject(fieldMap.size());
for (Cell cell : cells) {
if (fieldMap.size() == count) break;
String name = cell.getName();
String val = cell.getValue();
if (fieldMap.containsKey(name)) {
json.put(fieldMap.get(name), val);
count ++;
}
}
return json.toJavaObject(clazz);
}
private static JSONObject convertJson(Map<String, String> fieldMap, Property[] properties) {
JSONObject json = new JSONObject(fieldMap.size());
if (fieldMap.size() == 0) {
return json;
} else {
int count = 0;
for (Property property : properties) {
if (fieldMap.size() == count) break;
String name = property.getName();
String val = property.getValue();
if (fieldMap.containsKey(name)) {
json.put(fieldMap.get(name), val);
count ++;
}
}
return json;
}
}
private static Map<String, String> getFieldMap(Class clazz) {
Field[] fields = clazz.getDeclaredFields();
Map<String, String> fieldMap = new HashMap<>(fields.length);
for (Field field : fields) {
Alias annotation = field.getAnnotation(Alias.class);
String fieldName = field.getName();
if (annotation == null) {
fieldMap.put(fieldName, fieldName);
} else {
String[] aliasArr = annotation.value();
for (String alias : aliasArr) {
fieldMap.put(alias, fieldName);
}
}
}
return fieldMap;
}
}