package net.carchrae.smartgwt.server;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import net.carchrae.smartgwt.shared.data.DataSourceDescriptor;
import net.carchrae.smartgwt.shared.data.UserAccount;
import com.google.appengine.repackaged.com.google.common.base.Service;
public class DataOperations {
private static DataOperations instance;
HashMap<String, Class<?>> classMap = new HashMap<String, Class<?>>();
Service service;
DataSourceServiceImpl dataSourceService = new DataSourceServiceImpl();
private HashMap<String, DataOperation> defaultDataOperations = new HashMap<String, DataOperation>();
private HashMap<String, HashMap<String, DataOperation>> dataSourceToDataOperatoinMap;
private HashMap<Class<?>, ObjectService> objectServiceMap = new HashMap<Class<?>, ObjectService>();
public DataOperations() {
dataSourceToDataOperatoinMap = new HashMap<String, HashMap<String, DataOperation>>();
for (Class<?> c : DataStoreService.getClasses()) {
classMap.put(c.getSimpleName().toLowerCase(), c);
}
// register some generic data handlers
defaultDataOperations.put("fetch", new DataOperation() {
@Override
public Response execute(HttpServletRequest req,
HttpServletResponse resp) {
ObjectService objectService = getObjectSevice(req, resp);
return objectService.fetch(getSubMap(req, "_criteria"));
}
});
defaultDataOperations.put("add", new DataOperation() {
@Override
public Response execute(HttpServletRequest req,
HttpServletResponse resp) {
ObjectService objectService = getObjectSevice(req, resp);
Map map = getValueMap(req);
return objectService.add(map);
}
});
defaultDataOperations.put("update", new DataOperation() {
@Override
public Response execute(HttpServletRequest req,
HttpServletResponse resp) {
ObjectService objectService = getObjectSevice(req, resp);
return objectService.update(getValueMap(req));
}
});
defaultDataOperations.put("remove", new DataOperation() {
@Override
public Response execute(HttpServletRequest req,
HttpServletResponse resp) {
ObjectService objectService = getObjectSevice(req, resp);
return objectService.remove(getValueMap(req));
}
});
}
public Map getSubMap(HttpServletRequest req, String mapName) {
Map map = new HashMap();
Object c = req.getParameterMap().get(mapName);
if (c != null)
map = (Map) c;
return map;
}
public void register(String dataSource, String opType,
DataOperation operation) {
HashMap<String, DataOperation> opMap = dataSourceToDataOperatoinMap
.get(dataSource);
if (opMap == null)
dataSourceToDataOperatoinMap.put(dataSource,
opMap = new HashMap<String, DataOperation>());
opMap.put(opType, operation);
}
public DataSourceDescriptor getDataSource(HttpServletRequest req,
HttpServletResponse resp) {
String dsName = req.getParameter("_dataSource");
return dataSourceService.getDataSource(dsName);
}
public DataOperation get(String dataSource, String operationType) {
HashMap<String, DataOperation> dataOperationMap = dataSourceToDataOperatoinMap
.get(dataSource);
DataOperation dataOperation = null;
if (dataOperationMap != null)
dataOperation = dataOperationMap.get(operationType);
if (dataOperation != null)
return dataOperation;
else
return defaultDataOperations.get(operationType);
}
/**
* TODO: fix the user authentication mechanism. I ripped it out for now
*
* @param req
* @param resp
* @return
*/
public UserAccount getUser(HttpServletRequest req, HttpServletResponse resp) {
HttpSession session = req.getSession(true);
UserAccount account = (UserAccount) session.getAttribute("user");
return account;
}
public ObjectService getObjectSevice(HttpServletRequest req,
HttpServletResponse resp) {
String dsName = req.getParameter("_dataSource").toLowerCase();
Class<?> clazz = classMap.get(dsName);
ObjectService objectService = getObjectService(clazz);
return objectService;
}
private ObjectService getObjectService(Class<?> clazz) {
ObjectService objectService = objectServiceMap.get(clazz);
if (objectService == null)
objectServiceMap.put(clazz, objectService = new ObjectService(
DataStoreService.getInstance().ofy(), clazz));
return objectService;
}
/**
* this extracts any matching parameters from the request and creates a map
* of them
*
* @param req
* @return
*/
public Map<String, String> getValueMap(HttpServletRequest req) {
String dataSourceName = req.getParameter("_dataSource");
DataSourceDescriptor dataSource = dataSourceService
.getDataSource(dataSourceName);
Map<String, String> map = new HashMap<String, String>();
for (String field : dataSource.getFields().keySet()) {
if (req.getParameterMap().containsKey(field)) {
String value = req.getParameter(field);
map.put(field, value);
}
}
return map;
}
public static DataOperations getInstance() {
if (instance == null)
instance = new DataOperations();
return instance;
}
}