Subversion Repositories eFlore/Applications.coel

Rev

Rev 1369 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
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")
63
	  public D read(Object loadConfig, Object data) {
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>();
71
	    for (int i = 0; i < size; i++) {
72
	      JSONObject obj = (JSONObject) root.get(i);
73
	      ModelData model = newModelInstance();
74
	      for (int j = 0; j < modelType.getFieldCount(); j++) {
75
	        DataField field = modelType.getField(j);
76
 
77
	        String name = field.getName();
78
	        Class type = field.getType();
79
	        String map = field.getMap() != null ? field.getMap() : field.getName();
80
	        JSONValue value = obj.get(map);
81
 
82
	        if (value == null) continue;
83
	        if (value.isArray() != null) {
84
	          // nothing
85
	        } else if (value.isBoolean() != null) {
86
	          model.set(name, value.isBoolean().booleanValue());
87
	        } else if (value.isNumber() != null) {
88
	          if (type != null) {
89
	            Double d = value.isNumber().doubleValue();
90
	            if (type.equals(Integer.class)) {
91
	              model.set(name, d.intValue());
92
	            } else if (type.equals(Long.class)) {
93
	              model.set(name, d.longValue());
94
	            } else if (type.equals(Float.class)) {
95
	              model.set(name, d.floatValue());
96
	            } else {
97
	              model.set(name, d);
98
	            }
99
	          } else {
100
	            model.set(name, value.isNumber().doubleValue());
101
	          }
102
	        } else if (value.isObject() != null) {
103
	          // nothing
104
	        } else if (value.isString() != null) {
105
	          String s = value.isString().stringValue();
106
	          if (type != null) {
107
	            if (type.equals(Date.class)) {
108
	              if ("timestamp".equals(field.getFormat())) {
109
	                Date d = new Date(Long.parseLong(s) * 1000);
110
	                model.set(name, d);
111
	              } else {
112
	                DateTimeFormat format = DateTimeFormat.getFormat(field.getFormat());
113
	                Date d = format.parse(s);
114
	                model.set(name, d);
115
	              }
116
	            }
117
	          } else {
118
	            model.set(name, s);
119
	          }
120
	        } else if (value.isNull() != null) {
121
	          model.set(name, null);
122
	        }
123
	      }
124
	      models.add(model);
125
	    }
126
	    int totalCount = models.size();
127
	    if (modelType.getTotalName() != null) {
128
	      totalCount = getTotalCount(jsonRoot);
129
	    }
130
	    return (D) createReturnData(loadConfig, models, totalCount);
131
	  }
132
 
133
}