1287 |
cyprien |
1 |
package org.tela_botanica.client.composants.pagination;
|
|
|
2 |
|
|
|
3 |
import java.util.ArrayList;
|
|
|
4 |
import java.util.Date;
|
|
|
5 |
import java.util.List;
|
|
|
6 |
|
|
|
7 |
import org.tela_botanica.client.modeles.ValeurListe;
|
|
|
8 |
import org.tela_botanica.client.util.Debug;
|
|
|
9 |
|
|
|
10 |
import com.extjs.gxt.ui.client.data.BasePagingLoadConfig;
|
|
|
11 |
import com.extjs.gxt.ui.client.data.BasePagingLoadResult;
|
|
|
12 |
import com.extjs.gxt.ui.client.data.DataField;
|
|
|
13 |
import com.extjs.gxt.ui.client.data.DataReader;
|
|
|
14 |
import com.extjs.gxt.ui.client.data.JsonPagingLoadResultReader;
|
|
|
15 |
import com.extjs.gxt.ui.client.data.JsonReader;
|
|
|
16 |
import com.extjs.gxt.ui.client.data.ListLoadResult;
|
|
|
17 |
import com.extjs.gxt.ui.client.data.ModelData;
|
|
|
18 |
import com.extjs.gxt.ui.client.data.ModelType;
|
|
|
19 |
import com.extjs.gxt.ui.client.data.PagingLoadConfig;
|
|
|
20 |
import com.extjs.gxt.ui.client.data.PagingLoadResult;
|
|
|
21 |
import com.google.gwt.core.client.JavaScriptObject;
|
|
|
22 |
import com.google.gwt.i18n.client.DateTimeFormat;
|
|
|
23 |
import com.google.gwt.json.client.JSONArray;
|
|
|
24 |
import com.google.gwt.json.client.JSONNumber;
|
|
|
25 |
import com.google.gwt.json.client.JSONObject;
|
|
|
26 |
import com.google.gwt.json.client.JSONParser;
|
|
|
27 |
import com.google.gwt.json.client.JSONString;
|
|
|
28 |
import com.google.gwt.json.client.JSONValue;
|
|
|
29 |
|
|
|
30 |
public class TransformateurJSONaModelData<D> extends JsonPagingLoadResultReader<D> {
|
|
|
31 |
|
|
|
32 |
private ModelType modelType = null;
|
|
|
33 |
|
|
|
34 |
public TransformateurJSONaModelData(ModelType modelType) {
|
|
|
35 |
super(modelType);
|
|
|
36 |
this.modelType = modelType;
|
|
|
37 |
}
|
|
|
38 |
|
|
|
39 |
@SuppressWarnings("unchecked")
|
|
|
40 |
@Override
|
|
|
41 |
protected Object createReturnData(Object loadConfig, List<ModelData> records, int totalCount) {
|
|
|
42 |
ListLoadResult<?> result = (ListLoadResult<?>) super.createReturnData(loadConfig, records, totalCount);
|
|
|
43 |
|
|
|
44 |
if (result instanceof PagingLoadResult) {
|
|
|
45 |
PagingLoadResult<?> r = (PagingLoadResult<?>) result;
|
|
|
46 |
r.setTotalLength(totalCount);
|
|
|
47 |
|
|
|
48 |
if (loadConfig instanceof PagingLoadConfig) {
|
|
|
49 |
PagingLoadConfig config = (PagingLoadConfig) loadConfig;
|
|
|
50 |
r.setOffset(config.getOffset());
|
|
|
51 |
}
|
|
|
52 |
}
|
|
|
53 |
return result;
|
|
|
54 |
|
|
|
55 |
}
|
|
|
56 |
|
|
|
57 |
@Override
|
|
|
58 |
protected BasePagingLoadResult<ModelData> newLoadResult(Object loadConfig, List<ModelData> models) {
|
|
|
59 |
return new BasePagingLoadResult<ModelData>(models);
|
|
|
60 |
}
|
|
|
61 |
|
|
|
62 |
@SuppressWarnings("unchecked")
|
1468 |
jpm |
63 |
public D read(Object loadConfig, Object data) {
|
1287 |
cyprien |
64 |
JSONObject jsonRoot = null;
|
|
|
65 |
if (data instanceof JSONObject) {
|
|
|
66 |
jsonRoot = (JSONObject) data;
|
|
|
67 |
}
|
|
|
68 |
JSONArray root = (JSONArray) jsonRoot.get(modelType.getRoot());
|
|
|
69 |
int size = root.size();
|
|
|
70 |
ArrayList<ModelData> models = new ArrayList<ModelData>();
|
1468 |
jpm |
71 |
|
1287 |
cyprien |
72 |
for (int i = 0; i < size; i++) {
|
|
|
73 |
JSONObject obj = (JSONObject) root.get(i);
|
|
|
74 |
ModelData model = newModelInstance();
|
|
|
75 |
for (int j = 0; j < modelType.getFieldCount(); j++) {
|
|
|
76 |
DataField field = modelType.getField(j);
|
|
|
77 |
|
|
|
78 |
String name = field.getName();
|
|
|
79 |
Class type = field.getType();
|
|
|
80 |
String map = field.getMap() != null ? field.getMap() : field.getName();
|
|
|
81 |
JSONValue value = obj.get(map);
|
|
|
82 |
|
|
|
83 |
if (value == null) continue;
|
|
|
84 |
if (value.isArray() != null) {
|
|
|
85 |
// nothing
|
|
|
86 |
} else if (value.isBoolean() != null) {
|
|
|
87 |
model.set(name, value.isBoolean().booleanValue());
|
|
|
88 |
} else if (value.isNumber() != null) {
|
|
|
89 |
if (type != null) {
|
|
|
90 |
Double d = value.isNumber().doubleValue();
|
|
|
91 |
if (type.equals(Integer.class)) {
|
|
|
92 |
model.set(name, d.intValue());
|
|
|
93 |
} else if (type.equals(Long.class)) {
|
|
|
94 |
model.set(name, d.longValue());
|
|
|
95 |
} else if (type.equals(Float.class)) {
|
|
|
96 |
model.set(name, d.floatValue());
|
|
|
97 |
} else {
|
|
|
98 |
model.set(name, d);
|
|
|
99 |
}
|
|
|
100 |
} else {
|
|
|
101 |
model.set(name, value.isNumber().doubleValue());
|
|
|
102 |
}
|
|
|
103 |
} else if (value.isObject() != null) {
|
|
|
104 |
// nothing
|
|
|
105 |
} else if (value.isString() != null) {
|
|
|
106 |
String s = value.isString().stringValue();
|
|
|
107 |
if (type != null) {
|
|
|
108 |
if (type.equals(Date.class)) {
|
|
|
109 |
if ("timestamp".equals(field.getFormat())) {
|
|
|
110 |
Date d = new Date(Long.parseLong(s) * 1000);
|
|
|
111 |
model.set(name, d);
|
|
|
112 |
} else {
|
|
|
113 |
DateTimeFormat format = DateTimeFormat.getFormat(field.getFormat());
|
|
|
114 |
Date d = format.parse(s);
|
|
|
115 |
model.set(name, d);
|
|
|
116 |
}
|
|
|
117 |
}
|
|
|
118 |
} else {
|
|
|
119 |
model.set(name, s);
|
|
|
120 |
}
|
|
|
121 |
} else if (value.isNull() != null) {
|
|
|
122 |
model.set(name, null);
|
|
|
123 |
}
|
|
|
124 |
}
|
|
|
125 |
models.add(model);
|
|
|
126 |
}
|
|
|
127 |
int totalCount = models.size();
|
|
|
128 |
if (modelType.getTotalName() != null) {
|
|
|
129 |
totalCount = getTotalCount(jsonRoot);
|
|
|
130 |
}
|
1468 |
jpm |
131 |
|
1287 |
cyprien |
132 |
return (D) createReturnData(loadConfig, models, totalCount);
|
|
|
133 |
}
|
|
|
134 |
|
|
|
135 |
}
|