package com.adaptrex.core.view;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import com.adaptrex.core.ext.Association;
import com.adaptrex.core.ext.ExtConfig;
import com.adaptrex.core.ext.Filter;
import com.adaptrex.core.ext.ModelDefinition;
import com.adaptrex.core.ext.ModelInstance;
import com.adaptrex.core.ext.RestProxy;
import com.adaptrex.core.ext.Sorter;
import com.adaptrex.core.ext.StoreDefinition;
import com.adaptrex.core.persistence.api.ORMStoreData;
import com.adaptrex.core.utilities.Inflector;
import com.adaptrex.core.utilities.StringUtilities;
public class StoreComponent {
private HttpServletRequest request;
private Boolean clearOnPageLoad;
private Boolean clearRemovedOnLoad;
private Boolean autoSync;
private Boolean autoLoad;
private Boolean remoteGroup;
private Boolean remoteSort;
private Boolean remoteFilter;
private Boolean sortOnFilter;
private Boolean buffered;
private Integer trailingBufferZone;
private Integer leadingBufferZone;
private Integer purgePageCount;
private List<Sorter> groupers = new ArrayList<Sorter>();
private List<Sorter> sorters = new ArrayList<Sorter>();
private Boolean inline = false;
private Integer start;
private Integer pageSize;
private String where;
private Map<String, Object> params = new HashMap<String,Object>();
private List<Filter> filters = new ArrayList<Filter>();
private String rest;
private ExtConfig extConfig;
private String entityClassName;
private String factoryName;
private String includes;
private String excludes;
private String associations;
public StoreComponent(HttpServletRequest request, String entityClassName, String factoryName) {
this.request = request;
this.entityClassName = entityClassName;
this.factoryName = factoryName;
this.extConfig = new ExtConfig(entityClassName, factoryName);
if (request.getAttribute("adaptrex_touch") != null) {
this.extConfig.setTouch(true);
}
};
public String toString() {
ExtConfig config = this.extConfig;
/*
* The first thing a store needs is a model
*/
ModelComponent modelComponent = new ModelComponent(request, entityClassName, factoryName);
modelComponent.setNamespace(config.getNamespace());
if (config.getModelName() != null) modelComponent.setModelName(config.getModelName());
if (this.includes != null) modelComponent.setInclude(this.includes);
if (this.excludes != null) modelComponent.setExclude(this.excludes);
if (this.associations != null) modelComponent.setAssociations(this.associations);
if (this.rest != null) modelComponent.setRest(this.rest);
String modelName = config.getModelName();
String fullModelName = config.getNamespace() + ".model." + modelName;
String storeName = StringUtilities.pluralize(modelName);
String fullStoreName = config.getNamespace() + ".store." + storeName;
/*
* Create the store definition
*/
StoreDefinition storeDefinition = new StoreDefinition();
storeDefinition.setModelName(fullModelName);
storeDefinition.setClearOnPageLoad(this.clearOnPageLoad);
storeDefinition.setClearRemovedOnLoad(this.clearRemovedOnLoad);
storeDefinition.setAutoSync(this.autoSync);
storeDefinition.setAutoLoad(this.autoLoad);
storeDefinition.setRemoteGroup(this.remoteGroup);
storeDefinition.setRemoteSort(this.remoteSort);
storeDefinition.setRemoteFilter(this.remoteFilter);
storeDefinition.setSortOnFilter(this.sortOnFilter);
storeDefinition.setBuffered(this.buffered);
storeDefinition.setTrailingBufferZone(this.trailingBufferZone);
storeDefinition.setLeadingBufferZone(this.leadingBufferZone);
storeDefinition.setPurgePageCount(this.purgePageCount);
storeDefinition.setGroupers(this.groupers);
storeDefinition.setSorters(this.sorters);
storeDefinition.setPageSize(this.pageSize);
storeDefinition.setFilters(this.filters);
if (this.inline) {
ORMStoreData storeData = config.getORMPersistenceManager().getStoreData(config);
storeData.setFilters(this.filters);
storeData.setGroupers(this.groupers);
storeData.setLimit(this.pageSize);
storeData.setParams(this.params);
storeData.setSorters(this.sorters);
storeData.setStart(this.start);
storeData.setWhere(this.where);
storeDefinition.setData(storeData.getData());
}
String storeOutput;
if (request.getAttribute("adaptrex_touch") == null) {
storeOutput = "<script type='text/javascript'>\n" +
"Ext.define('" + fullStoreName + "', " +
StringUtilities.json(storeDefinition) + ");\n" +
"</script>";
} else {
storeOutput = "<script type='text/javascript'>\n" +
"Ext.define('" + fullStoreName + "', {extend:'Ext.data.Store', config:" +
StringUtilities.json(storeDefinition) + "});\n" +
"</script>";
}
return modelComponent.toString() + storeOutput;
}
public StoreComponent setClearOnPageLoad(Boolean clearOnPageLoad) {
this.clearOnPageLoad = clearOnPageLoad;
return this;
}
public StoreComponent setClearRemovedOnLoad(Boolean clearRemovedOnLoad) {
this.clearRemovedOnLoad = clearRemovedOnLoad;
return this;
}
public StoreComponent setAutoSync(Boolean autoSync) {
this.autoSync = autoSync;
return this;
}
public StoreComponent setAutoLoad(Boolean autoLoad) {
this.autoLoad = autoLoad;
return this;
}
public StoreComponent setRemoteGroup(Boolean remoteGroup) {
this.remoteGroup = remoteGroup;
return this;
}
public StoreComponent setRemoteSort(Boolean remoteSort) {
this.autoLoad = remoteSort;
return this;
}
public StoreComponent setRemoteFilter(Boolean remoteFilter) {
this.remoteFilter = remoteFilter;
return this;
}
public StoreComponent setSortOnFilter(Boolean sortOnFilter) {
this.remoteFilter = sortOnFilter;
return this;
}
public StoreComponent setBuffered(Boolean buffered) {
this.buffered = buffered;
return this;
}
public StoreComponent setTrailingBufferZone(Integer trailingBufferZone) {
this.trailingBufferZone = trailingBufferZone;
return this;
}
public StoreComponent setLeadingBufferZone(Integer leadingBufferZone) {
this.leadingBufferZone = leadingBufferZone;
return this;
}
public StoreComponent setPurgePageCount(Integer purgePageCount) {
this.purgePageCount = purgePageCount;
return this;
}
public StoreComponent addGrouper(String property, String direction, String root) {
groupers.add(new Sorter(property, direction, root));
return this;
}
public StoreComponent addSorter(String property, String direction, String root) {
sorters.add(new Sorter(property, direction, root));
return this;
}
public StoreComponent setInline(Boolean inline) {
this.inline = inline;
return this;
}
public StoreComponent setStart(Integer start) {
this.start = start;
return this;
}
public StoreComponent setPageSize(Integer pageSize) {
this.pageSize = pageSize;
return this;
}
public StoreComponent setWhere(String where) {
this.where = where;
return this;
}
public StoreComponent addFilter(String property, String value) {
filters.add(new Filter(property, value));
return this;
}
public StoreComponent addParam(String property, String value) {
params.put(property, value);
return this;
}
public StoreComponent setRest(String rest) {
this.rest = rest;
return this;
}
public StoreComponent setNamespace(String namespace) {
this.extConfig.setNamespace(namespace);
return this;
}
public StoreComponent setInclude(String includes) {
this.extConfig.setIncludes(includes);
this.includes = includes;
return this;
}
public StoreComponent setExclude(String excludes) {
this.extConfig.setExcludes(excludes);
this.excludes = excludes;
return this;
}
public StoreComponent setAssociations(String associations) {
this.extConfig.setAssociations(associations);
this.associations = associations;
return this;
}
public StoreComponent setModelName(String modelName) {
this.extConfig.setModelName(modelName);
return this;
}
}