Package com.adaptrex.core.ext.jsf

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

/*
* Copyright 2012 Adaptrex, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*    http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.adaptrex.core.ext.jsf;

import com.adaptrex.core.Adaptrex;
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.DataConfig;
import com.adaptrex.core.ext.data.Model;
import com.adaptrex.core.ext.data.ModelDefinition;
import com.adaptrex.core.ext.data.RestProxy;
import com.adaptrex.core.ext.data.Store;
import com.adaptrex.core.persistence.AdaptrexPersistence;
import com.adaptrex.core.persistence.AdaptrexSession;
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 factoryName = param(a, "factory");
  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;
      }
  }

  /*
   * Get the config
   */
  AdaptrexPersistence persistence =
    Adaptrex.getAdaptrex().getPersistenceManager().getPersistence(factoryName);
  Class<?> clazz = persistence.getEntityClass(className);
  DataConfig config = new DataConfig(clazz, persistence);


  /*
   * Create the model
   */
  AdaptrexSession session = new AdaptrexSession(persistence);
  Model model = new Model(session, config);

 
  /*
   * Set the model name
   */
  String modelName = ns + ".model." + (name != null ? name : className);
  config.setModelName(name);


  if (include != null) {
      config.include(include);
  }
  if (exclude != null) {
      config.exclude(exclude);
  }

  /*
   * Add associations
   */
  if (associations != null) {
      config.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/" +
      (rest.equals("true") ? clazz.getSimpleName().toLowerCase() : rest);
      config.setProxy(new RestProxy(restPath, config));
  }


  ModelDefinition modelDef = model.getModelDefinition();
  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.