/*
* 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.RestOptions;
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 AdaptrexSession adaptrexSession;
private DataConfig config;
private Integer totalCount;
private static Logger log = LoggerFactory.getLogger(Store.class);
public Store(AdaptrexSession adaptrexSession, DataConfig config) {
this.adaptrexSession = adaptrexSession;
this.config = config;
}
/*
* Expose this store's config
*/
public DataConfig getConfig() {
return config;
}
/**
* ********************************************************************************
*
* Store
* Output
*/
public ModelDefinition getModelDefinition() {
return new ModelDefinition(this.config);
}
public StoreDefinition getStoreDefinition() {
return new StoreDefinition(this);
}
public List<Map<String, Object>> getData() {
List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();
try {
/*
* Get List of entities
*/
List<Object> entityList = adaptrexSession.getPersistence().getEntityList(adaptrexSession, this);
/*
* Convert entities into ModelInstance objects
*/
for (Object entity : entityList) {
ModelInstance modelInstance = new ModelInstance(this.adaptrexSession, this.config, entity);
data.add(modelInstance.getData());
}
this.adaptrexSession.close();
} catch (Exception e) {
log.warn("Error", e);
}
return data;
}
/* ************************************************************************************
*
* Read Store
*/
public static Store read(String entityName, RestOptions restOptions) {
return read(entityName, restOptions, null);
}
public static Store read(String entityName, RestOptions restOptions, String factoryName) {
AdaptrexPersistence persistence =
Adaptrex.getAdaptrex().getPersistenceManager().getPersistence(factoryName);
AdaptrexSession session = new AdaptrexSession(persistence);
Class<?> clazz = persistence.getEntityClass(entityName);
DataConfig config = new DataConfig(clazz, persistence);
config.applyRestOptions(restOptions);
return new Store(session, config);
}
/**
* *************************************************************
*
* Additional
* info
* required
* for
* Ext
* responses
*
*/
public Integer getTotalCount() {
return totalCount;
}
public void setTotalCount(Integer totalCount) {
this.totalCount = totalCount;
}
}