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