/*
* 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.data;
import com.adaptrex.core.Adaptrex;
import com.adaptrex.core.ext.rest.ExtConfig;
import com.adaptrex.core.persistence.AdaptrexPersistence;
import com.adaptrex.core.persistence.AdaptrexSession;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/*
* Store... wraps StoreData and StoreDefinition
*/
public class Store {
private static AdaptrexPersistence persistence = Adaptrex.getPersistence();
private Config config;
private Integer totalCount;
private static Logger log = LoggerFactory.getLogger(Store.class);
/*
* Create a store based on an entity name without a namespace. This should only be
* used to retrieve store data and not create an entire Ext store definition.
*/
public Store (String entityName) {
this(persistence.getEntityClass(entityName));
}
/*
* Create a store based on a class without a namespace. This should only be
* used to retrieve store data and not create an entire Ext store definition.
*/
public Store (Class<?> clazz) {
this(clazz, null);
}
/*
* Create a store based on an entity name and include a namespace. This should be
* used when generate a full Ext store definition and the ability to load data
* inline or via the rest API
*/
public Store (String entityName, String namespace) {
this(persistence.getEntityClass(entityName), namespace);
}
/*
* Create a store based on a class and include a namespace. This should be
* used when generate a full Ext store definition and the ability to load data
* inline or via the rest API
*/
public Store (Class<?> clazz, String namespace) {
this.config = new Config();
if (namespace != null) this.config.setNamespace(namespace);
this.config.setClazz(clazz);
this.config.setModelName(clazz.getSimpleName());
}
/*
* Create a store with ext options applied. This is generally used
* inside the rest API.
*/
public Store (String entityName, ExtConfig extConfig) {
this(entityName);
this.config.applyExtConfig(extConfig);
}
/*
* Allow users to get the store config at any point
*/
public Config getConfig() {
return config;
}
/**********************************************************************************
*
* Store Output
*
* Store instances are created to ultimately retrieve a store
* definition, a model defitnion and data.
*/
public ModelDefinition getModelDefinition() {
// if (this.modelDefinition == null) {
// this.modelDefinition = new ModelDefinition(this.config);
// }
return new ModelDefinition(this.config);
}
public StoreDefinition getStoreDefinition() {
return new StoreDefinition(this);
}
public List<Map<String,Object>> getData() {
/*
* Get List of entities
*/
AdaptrexSession session = new AdaptrexSession();
List<Object> entityList = session.getEntityList(this);
/*
* Convert entities into ModelInstance objects
*/
List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();
for (Object entity : entityList) {
ModelInstance modelInstance = new ModelInstance(this.config, entity);
data.add(modelInstance.getData());
}
session.close();
return data;
}
/**********************************************************************************
*
* Store Configuration
*
* In general, these configuration methods allow users to interact directly with
* the store to configure the Config object related to this store. The Config
* object contains options shared between all store and model components.
*/
/*
* General store options
*/
public Store start(Integer start) {
this.config.setStart(start);
return this;
}
public Store pageSize(Integer pageSize) {
this.config.setPageSize(pageSize);
return this;
}
/*
* Store definition only options
*/
public Store inline() {
this.config.setInline(true);
return this;
}
public Store autoSync() {
this.config.setAutoSync(true);
return this;
}
public Store autoLoad() {
this.config.setAutoLoad(true);
return this;
}
public Store clearOnPageLoad() {
this.config.setClearOnPageLoad(true);
return this;
}
public Store clearRemovedOnLoad() {
this.config.setClearRemovedOnLoad(true);
return this;
}
public Store buffered() {
this.config.setBuffered(true);
return this;
}
public Store leadingBufferZone(Integer leadingBufferZone) {
this.config.setLeadingBufferZone(leadingBufferZone);
return this;
}
public Store trailingBufferZone(Integer trailingBufferZone) {
this.config.setTrailingBufferZone(trailingBufferZone);
return this;
}
public Store purgePageCount(Integer purgePageCount) {
this.config.setPurgePageCount(purgePageCount);
return this;
}
public Store proxy(String restPath) {
this.config.setProxy(new RestProxy(restPath, config));
return this;
}
/*
* Sorting and grouping configurations
*/
public Store remoteGroup() {
this.config.setRemoteGroup(true);
return this;
}
public Store remoteSort() {
this.config.setRemoteSort(true);
return this;
}
public Store remoteFilter(Boolean remoteFilter) {
this.config.setRemoteFilter(remoteFilter);
return this;
}
public Store sortOnFilter(Boolean sortOnFilter) {
this.config.setSortOnFilter(sortOnFilter);
return this;
}
/*
* Grouping
*/
public Store group(String property) {
this.config.group(property, "asc", null);
return this;
}
public Store group(String property, String direction) {
this.config.group(property, direction, null);
return this;
}
public Store group(String property, String direction, String root) {
this.config.group(property, direction, root);
return this;
}
/*
* Sorting
*/
public Store sort(String property) {
this.config.sort(property, "asc", null);
return this;
}
public Store sort(String property, String direction) {
this.config.sort(property, direction, null);
return this;
}
public Store sort(String property, String direction, String root) {
this.config.sort(property, direction, root);
return this;
}
/*
* Response Customization
*/
public Store where(String where) {
this.config.setWhere(where);
return this;
}
public Store filter(String property, String value) {
this.config.filter(property, value);
return this;
}
public Store param(String key, Object value) {
this.config.param(key, value);
return this;
}
public Store include(String items) {
this.config.include(items);
return this;
}
public Store exclude(String items) {
this.config.exclude(items);
return this;
}
public Store associations(String association) {
this.config.associations(association);
return this;
}
public Store modelName(String modelName) {
this.config.setModelName(modelName);
return this;
}
/***************************************************************
*
* Additional info required for Ext responses
*
*/
public Integer getTotalCount() {
return totalCount;
}
public void setTotalCount(Integer totalCount) {
this.totalCount = totalCount;
}
}