All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.adaptrex.core.view.ModelComponent Maven / Gradle / Ivy

There is a newer version: 1.0-Alpha3
Show newest version
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, String factoryName) {
		this.request = request;
		this.extConfig = new ExtConfig(entityClassName, factoryName);
		
		if (request.getAttribute("adaptrex_touch") != null) {
			this.extConfig.setTouch(true);
		}
	};
	
	public String toString() {
		List modelDefinitionStrings = new ArrayList();
		
		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 "\n";
	}
	
	private List associations = new ArrayList();
	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;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy