Package com.adaptrex.core.view

Source Code of com.adaptrex.core.view.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.view;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.adaptrex.core.ext.Association;
import com.adaptrex.core.ext.ExtConfig;
import com.adaptrex.core.ext.ModelDefinition;
import com.adaptrex.core.ext.ModelInstance;
import com.adaptrex.core.ext.RestProxy;
import com.adaptrex.core.utilities.StringUtilities;

public class ModelComponent {

  private HttpServletRequest request;
 
  private ExtConfig extConfig;
 
  private Object entityId;
  private String entityKey;
  private Object entityValue;
 
  RestProxy restProxy;
 
  public ModelComponent(HttpServletRequest request, String entityClassName) {
    this(request, entityClassName, null);
  }
 
  public ModelComponent(HttpServletRequest request, String entityClassName, String factoryName) {
    this.request = request;
    this.extConfig = new ExtConfig(entityClassName, factoryName);
   
    if (request.getAttribute("adaptrex_touch") != null) {
      this.extConfig.setTouch((Boolean) request.getAttribute("adaptrex_touch"));
    }
   
    if (request.getAttribute("adaptrex_namespace") != null) {
      this.setNamespace((String) request.getAttribute("adaptrex_namespace"));
    }
  };
 
  public String toString() {
    List<String> modelDefinitionStrings = new ArrayList<String>();
   
    String namespace = extConfig.getNamespace();
    String modelName = extConfig.getModelName();
    String fullModelName = namespace +  ".model." + modelName;
   
    /*
     * Get the model definition
     */
    ModelDefinition modelDefinition = new ModelDefinition(this.extConfig);
   
    /*
     * Add the rest API to the model
     */
    modelDefinition.setProxy(this.restProxy);
   
    if (request.getAttribute("adaptrex_touch") == null) {
      modelDefinitionStrings.add("Ext.define('" + fullModelName + "', " +
          StringUtilities.json(modelDefinition) + ")");
    } else {
      modelDefinitionStrings.add("Ext.define('" + fullModelName + "', {extend:'Ext.data.Model', config:" +
          StringUtilities.json(modelDefinition) + "})");
    }
   
   
    /*
     * Get the model's association definitions
     */
    getAllAssociations(modelDefinition);
    for (Association association : associations) {
      ModelDefinition assocModelDef = association.getModelDefinition();
      if (assocModelDef != null) {
        if (request.getAttribute("adaptrex_touch") == null) {
          modelDefinitionStrings.add("Ext.define('" + assocModelDef.getModelName() + "', " +
              StringUtilities.json(assocModelDef) + ")");
        } else {
          modelDefinitionStrings.add("Ext.define('" + assocModelDef.getModelName() + "', {extend:'Ext.data.Model', config:" +
              StringUtilities.json(assocModelDef) + "})");       
        }
      }
    }
   
    /*
     * Are we creating an instance?
     */
    String modelInstanceString = "";
    if (this.entityId != null || entityKey != null) {
      ModelInstance modelInstance = this.entityId != null
          ? new ModelInstance(extConfig, this.entityId)
          : new ModelInstance(extConfig, this.entityKey, this.entityValue);
     
      modelInstanceString = "Ext.ns('" + namespace + ".instance');" + "\n" +
          namespace + ".instance." + modelName + "=Ext.create('" +
          fullModelName + "', " + StringUtilities.json(modelInstance.getData()) + ");\n";
    }
   
   
    return "<script type='text/javascript'>\n" +
      StringUtilities.join(modelDefinitionStrings, ";\n") + "\n" +
      modelInstanceString + ";\n</script>\n";
  }
 
  private List<Association> associations = new ArrayList<Association>();
  private void getAllAssociations(ModelDefinition baseModelDefinition) {
    for (Association assoc : baseModelDefinition.getAssociations()) {
      associations.add(assoc);
      if (assoc.getModelDefinition() != null) {
        getAllAssociations(assoc.getModelDefinition());
      }
    }
  }
 
  public ModelComponent setNamespace(String namespace) {
    this.extConfig.setNamespace(namespace);
    return this;
  }
 
  public ModelComponent setRest(String rest) {
    if (rest != null) {
      String classRestPath = this.extConfig.getEntityClass().getSimpleName().toLowerCase();
      if (this.extConfig.getFactoryName() != null) {
        classRestPath = this.extConfig.getFactoryName() + "/" + classRestPath;
      }
     
      String restPath = this.request.getContextPath() + "/rest/" + (rest.equals("true")
          ? classRestPath
          : rest);
      this.restProxy = new RestProxy(restPath, extConfig);     
    }
    return this;
  }
 
  public ModelComponent setInclude(String includes) {
    this.extConfig.setIncludes(includes);
    return this;
  }
 
  public ModelComponent setExclude(String excludes) {
    this.extConfig.setExcludes(excludes);
    return this;
  }
 
  public ModelComponent setAssociations(String associations) {
    this.extConfig.setAssociations(associations);
    return this;
  }

  public ModelComponent setModelName(String modelName) {
    this.extConfig.setModelName(modelName);
    return this;
  }
 
 
  public ModelComponent setEntityId(String entityId) {
    this.entityId = entityId;
    return this;
  }

 
  public ModelComponent setEntityKey(String entityKey) {
    this.entityKey = entityKey;
    return this;
  }

  public ModelComponent setEntityValue(String entityValue) {
    this.entityValue = entityValue;
    return this;
  }
}
TOP

Related Classes of com.adaptrex.core.view.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.