ModelKit.java
5.53 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/**
* Copyright 2015-2025 FLY的狐狸(email:jflyfox@sina.com qq:369191470).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package com.jflyfox.util;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import com.jfinal.log.Log;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.DbKit;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.Table;
import com.jfinal.plugin.activerecord.TableMapping;
public class ModelKit {
protected final static Log logger = Log.getLog(ModelKit.class);
@SuppressWarnings({ "rawtypes", "unchecked" })
public static Record toRecord(Model model) {
Record record = new Record();
Set<Entry<String, Object>> attrs = model._getAttrsEntrySet();
for (Entry<String, Object> entry : attrs) {
record.set(entry.getKey(), entry.getValue());
}
return record;
}
@SuppressWarnings("rawtypes")
public static Model set(Model model, Object... attrsAndValues) {
int length = attrsAndValues.length;
if (!(length % 2 == 0)) {
throw new RuntimeException("attrsAndValues length must be even number");
}
for (int i = 0; i < length; i = i + 2) {
Object attr = attrsAndValues[i];
if (!(attr instanceof String)) {
throw new RuntimeException("the odd number of attrsAndValues must be String");
}
model.set((String) attr, attrsAndValues[i + 1]);
}
return model;
}
@SuppressWarnings({ "rawtypes", "unchecked" })
public static Map<String, Object> toMap(Model model) {
Map<String, Object> map = new HashMap<String, Object>();
Set<Entry<String, Object>> attrs = model._getAttrsEntrySet();
for (Entry<String, Object> entry : attrs) {
map.put(entry.getKey(), entry.getValue());
}
return map;
}
public static Model<?> fromBean(Class<? extends Model<?>> clazz, Object bean) {
Model<?> model = null;
try {
model = clazz.newInstance();
} catch (Exception e) {
logger.error(e.getMessage(), e);
return model;
}
// TODO bean to model
return model;
}
@SuppressWarnings("rawtypes")
public static int[] batchSave(List<? extends Model> data) {
return batchSave(data, data.size());
}
@SuppressWarnings({ "rawtypes", "unchecked" })
public static int[] batchSave(List<? extends Model> data, int batchSize) {
Model model = data.get(0);
Map<String, Object> attrs = (Map<String, Object>) ReflectionUtils.getFieldValue(model, "attrs");
Class<? extends Model> modelClass = model.getClass();
Table table = TableMapping.me().getTable(modelClass);
StringBuilder sql = new StringBuilder();
List<Object> paras = new ArrayList<Object>();
DbKit.getConfig().getDialect().forModelSave(table, attrs, sql, paras);
Object[][] batchPara = new Object[data.size()][attrs.size()];
for (int i = 0; i < data.size(); i++) {
int j = 0;
for (String key : attrs.keySet()) {
batchPara[i][j++] = data.get(i).get(key);
}
}
return Db.batch(sql.toString(), batchPara, batchSize);
}
public static int hashCode(Model<?> model) {
final int prime = 31;
int result = 1;
Table tableinfo = TableMapping.me().getTable(model.getClass());
Set<Entry<String, Object>> attrsEntrySet = model._getAttrsEntrySet();
for (Entry<String, Object> entry : attrsEntrySet) {
String key = entry.getKey();
Object value = entry.getValue();
Class<?> clazz = tableinfo.getColumnType(key);
if (clazz == Integer.class) {
result = prime * result + (Integer) value;
} else if (clazz == Short.class) {
result = prime * result + (Short) value;
} else if (clazz == Long.class) {
result = prime * result + (int) ((Long) value ^ ((Long) value >>> 32));
} else if (clazz == Float.class) {
result = prime * result + Float.floatToIntBits((Float) value);
} else if (clazz == Double.class) {
long temp = Double.doubleToLongBits((Double) value);
result = prime * result + (int) (temp ^ (temp >>> 32));
} else if (clazz == Boolean.class) {
result = prime * result + ((Boolean) value ? 1231 : 1237);
} else if (clazz == Model.class) {
result = hashCode((Model<?>) value);
} else {
result = prime * result + ((value == null) ? 0 : value.hashCode());
}
}
return result;
}
public static boolean equals(Model<?> model, Object obj) {
if (model == obj)
return true;
if (obj == null)
return false;
if (model.getClass() != obj.getClass())
return false;
Model<?> other = (Model<?>) obj;
Table table = TableMapping.me().getTable(model.getClass());
Set<Entry<String, Object>> attrsEntrySet = model._getAttrsEntrySet();
for (Entry<String, Object> entry : attrsEntrySet) {
String key = entry.getKey();
Object value = entry.getValue();
Class<?> clazz = table.getColumnType(key);
if (clazz == Float.class) {
} else if (clazz == Double.class) {
} else if (clazz == Model.class) {
} else {
if (value == null) {
if (other.get(key) != null)
return false;
} else if (!value.equals(other.get(key)))
return false;
}
}
return true;
}
}