Package com.adaptrex.core.ext.jsf

Source Code of com.adaptrex.core.ext.jsf.ModelComponent

package com.adaptrex.core.ext.jsf;

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

import javax.faces.component.FacesComponent;
import javax.faces.context.FacesContext;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.adaptrex.core.ext.data.Association;
import com.adaptrex.core.ext.data.Model;
import com.adaptrex.core.ext.data.ModelDefinition;
import com.adaptrex.core.utilities.StringUtilities;

@FacesComponent("ext.data.Model")
public class ModelComponent extends ExtComponentBase {

  private Logger log = LoggerFactory.getLogger(ModelComponent.class);
 
  @Override
  public String getFamily() {
    return "ext.data.model";
  }
 
    @Override
    public void encodeBegin(FacesContext context) throws IOException {
      Map<String,Object> a = getAttributes();
     
      /*
       * Definition parameters
       */
      String ns           = param(a,"ns");
      String className      = param(a,"table");
      String name          = param(a,"name");
      String rest          = param(a,"rest");     

      String include        = param(a, "include");
      String exclude        = param(a, "exclude");

      String associations = param(a, "associations");
     
      /*
       * Get or set the namespace for this store
       */
      if (ns != null) {
        write("Ext.ns(\"" + ns + "\")");
      } else {
        ns = (String) getAttribute("ns");
        if (ns == null) {
          log.warn("Error creating model: Namespace has not been set");
          write ("<!-- Error creating model: Namespace has not been set -->\n");
          return;
        }
      }
     
      /*
       * Create the model
       */
      Model model = new Model(className, ns);
      if (name != null) model.modelName(name);
     
      if (include != null) model.include(include);
      if (exclude != null) model.exclude(exclude);
     
      /*
       * Add associations
       */
      if (associations != null) model.associations(associations);
     
      /*
       * The proxy needs additinoal information from the model.  We shouldn't create
       * the proxy until the store has been fully configured
       */
      if (rest != null) {
        String restPath = getRequest().getContextPath() + "/rest/";
        restPath += (rest.equals("true") ? model.getConfig().getClazz().getSimpleName().toLowerCase() : rest);
        model.proxy(restPath);
      }
     
     
      ModelDefinition modelDef = model.getModelDefinition();
      String modelName = ns + ".model." + model.getConfig().getModelName();
      String output =  "Ext.define(\"" + modelName + "\"," + StringUtilities.json(modelDef) + ");\n";

        for (Association assoc : getAllAssociations(modelDef)) {
          ModelDefinition associatedModel = assoc.getAssociatedModelDefinition();
          if (associatedModel == null) continue;
         
          output += "Ext.define(\"" + assoc.getModelName() + "\"," + StringUtilities.json(associatedModel) + ");\n";
        }
       
        writeScript(output);
    }
   
   
    /*
     * Each of our associations need to have a model automatically generated
     */
    private List<Association> getAllAssociations(ModelDefinition baseModelDefinition) {
      List<Association> allAssociations = new ArrayList<Association>();
      for (Association assoc : baseModelDefinition.getAssociations()) {
        allAssociations.add(assoc);
       
        List<Association> thisAssociations = assoc.getAssociatedModelDefinition().getAssociations();
        if (thisAssociations.size() > 0) allAssociations.addAll(thisAssociations);
      }
      return allAssociations;
    }
   
    private String param(Map<String,Object> a, String p) {
      return (String) a.get(p);
    }
}
TOP

Related Classes of com.adaptrex.core.ext.jsf.ModelComponent

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.