Subversion Repositories eFlore/Applications.coel

Rev

Rev 1442 | Blame | Compare with Previous | Last modification | View Log | RSS feed

package org.tela_botanica.client.composants.pagination;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.tela_botanica.client.modeles.ValeurListe;
import org.tela_botanica.client.util.Debug;

import com.extjs.gxt.ui.client.data.BasePagingLoadConfig;
import com.extjs.gxt.ui.client.data.BasePagingLoadResult;
import com.extjs.gxt.ui.client.data.DataField;
import com.extjs.gxt.ui.client.data.DataReader;
import com.extjs.gxt.ui.client.data.JsonPagingLoadResultReader;
import com.extjs.gxt.ui.client.data.JsonReader;
import com.extjs.gxt.ui.client.data.ListLoadResult;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.data.ModelType;
import com.extjs.gxt.ui.client.data.PagingLoadConfig;
import com.extjs.gxt.ui.client.data.PagingLoadResult;
import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONNumber;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONParser;
import com.google.gwt.json.client.JSONString;
import com.google.gwt.json.client.JSONValue;

public class TransformateurJSONaModelData<D> extends JsonPagingLoadResultReader<D> {

        private ModelType modelType = null;
        
        // HashMap<'nom du champ virtuel', 'nom du champ à binder'>
        private HashMap<String, String> virtualFields = null;
        
        public TransformateurJSONaModelData(ModelType modelType, HashMap<String, String> virtualFields) {
                super(modelType);
                this.modelType = modelType;
                this.virtualFields = virtualFields;
        }

          @SuppressWarnings("unchecked")
          @Override
          protected Object createReturnData(Object loadConfig, List<ModelData> records, int totalCount) {
            ListLoadResult<?> result = (ListLoadResult<?>) super.createReturnData(loadConfig, records, totalCount);

            if (result instanceof PagingLoadResult) {
              PagingLoadResult<?> r = (PagingLoadResult<?>) result;
              r.setTotalLength(totalCount);

              if (loadConfig instanceof PagingLoadConfig) {
                PagingLoadConfig config = (PagingLoadConfig) loadConfig;
                r.setOffset(config.getOffset());
              }
            }
            return result;

          }

          @Override
          protected BasePagingLoadResult<ModelData> newLoadResult(Object loadConfig, List<ModelData> models) {
            return new BasePagingLoadResult<ModelData>(models);
          }

          @SuppressWarnings("unchecked")
          public D read(Object loadConfig, Object data) {                 
            JSONObject jsonRoot = null;
            if (data instanceof JSONObject) {
              jsonRoot = (JSONObject) data;
            }
            JSONArray root = (JSONArray) jsonRoot.get(modelType.getRoot());
            int size = root.size();
            ArrayList<ModelData> models = new ArrayList<ModelData>();
            
            for (int i = 0; i < size; i++) {            
              JSONObject obj = (JSONObject) root.get(i);
              ModelData model = newModelInstance();
              for (int j = 0; j < modelType.getFieldCount(); j++) {
                DataField field = modelType.getField(j);
                
                String name = field.getName();
                Class type = field.getType();
                String map = field.getMap() != null ? field.getMap() : field.getName();
                JSONValue value = obj.get(map);
                
                if (value == null) continue;
                if (value.isArray() != null) {
                  // nothing
                } else if (value.isBoolean() != null) {
                  model.set(name, value.isBoolean().booleanValue());
                } else if (value.isNumber() != null) {
                  if (type != null) {
                    Double d = value.isNumber().doubleValue();
                    if (type.equals(Integer.class)) {
                      model.set(name, d.intValue());
                    } else if (type.equals(Long.class)) {
                      model.set(name, d.longValue());
                    } else if (type.equals(Float.class)) {
                      model.set(name, d.floatValue());
                    } else {
                      model.set(name, d);
                    }
                  } else {
                    model.set(name, value.isNumber().doubleValue());
                  }
                } else if (value.isObject() != null) {
                  // nothing
                } else if (value.isString() != null) {
                  String s = value.isString().stringValue();              
                  if (type != null) {
                    if (type.equals(Date.class)) {
                      if ("timestamp".equals(field.getFormat())) {
                        Date d = new Date(Long.parseLong(s) * 1000);
                        model.set(name, d);
                      } else {
                        DateTimeFormat format = DateTimeFormat.getFormat(field.getFormat());
                        Date d = format.parse(s);
                        model.set(name, d);
                      }
                    }
                  } else {
                    model.set(name, s);
                  }
                } else if (value.isNull() != null) {
                  model.set(name, null);
                }
                
                    if (virtualFields != null) {
                                Set<String> cles = virtualFields.keySet();
                                Iterator<String> it = cles.iterator();
                                while(it.hasNext()) {
                                        String vField = it.next();
                                        // si il y a un champ à binder
                                        if (virtualFields.get(vField) != null) {
                                                model.set(vField, model.get(virtualFields.get(vField)));
                                        }
                                        // sinon affecter la propriété à 'chaîne vide'
                                        else {
                                                model.set(virtualFields.get(vField), "");
                                        }
                                }
                    }
              }
              models.add(model);
            }
            int totalCount = models.size();
            if (modelType.getTotalName() != null) {
              totalCount = getTotalCount(jsonRoot);
            }
            
            return (D) createReturnData(loadConfig, models, totalCount);
          }       

}