Package org.jibeframework.core.data.model

Source Code of org.jibeframework.core.data.model.GridDataModel

package org.jibeframework.core.data.model;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.JsonProcessingException;
import org.codehaus.jackson.map.JsonSerializer;
import org.codehaus.jackson.map.SerializerProvider;
import org.codehaus.jackson.map.annotate.JsonSerialize;
import org.jibeframework.core.Context;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;

@JsonSerialize(using = GridDataModel.Serializer.class)
public class GridDataModel {
  private String modelId;
  private List<?> data;

  public GridDataModel(String modelId, List<?> data) {
    this.modelId = modelId;
    this.data = data;
  }

  @SuppressWarnings("unchecked")
  private List<Object> getModel() {
    Map<String, Object> model = Context.getCurrentContext().getModel(modelId);
    ExpressionParser parser = new SpelExpressionParser();
    List<Object> d = new ArrayList<Object>();
    for (Object val : data) {
      Map<String, Object> m = new HashMap<String, Object>();
      for (Map.Entry<String, Object> entry : model.entrySet()) {
        Map<String, Object> modelEntry = (Map<String, Object>) entry.getValue();
        String name = entry.getKey();
        String expr = (String) (modelEntry.containsKey("jread") ? modelEntry.get("jread") : modelEntry
            .get("jbind"));
        if (expr != null) {
          Object value = parser.parseExpression(expr).getValue(val);
          m.put(name, value);
        }
      }
      d.add(m);
    }
    return d;
  }

  public static class Serializer extends JsonSerializer<GridDataModel> {

    @Override
    public void serialize(GridDataModel value, JsonGenerator jgen, SerializerProvider provider) throws IOException,
        JsonProcessingException {
      jgen.writeObject(value.getModel());

    }

  }

}
TOP

Related Classes of org.jibeframework.core.data.model.GridDataModel

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.