package net.carchrae.smartgwt.client.datasource;
import java.util.Map;
import net.carchrae.smartgwt.shared.data.DataSourceDescriptor;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.smartgwt.client.data.DSRequest;
import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.data.DataSourceField;
import com.smartgwt.client.data.OperationBinding;
import com.smartgwt.client.data.RestDataSource;
import com.smartgwt.client.types.DSDataFormat;
import com.smartgwt.client.types.DSOperationType;
/**
* TODO: in theory, this could/should come back from the DataSourceDescriptor.
* At the moment, the server just sends back fields
*
* @author tom
*
*/
public class DataSourceLoader {
private final static DataSourceServiceAsync service = GWT
.create(DataSourceService.class);
public static DataSource get(String className) {
DataSource dataSource = new DataSource();
// XJSONDataSource dataSource = new XJSONDataSource();
String baseUrl = Window.Location.getProtocol() + "//"
+ Window.Location.getHost();
dataSource.setDataURL(baseUrl + "/datasource/"
+ className.toLowerCase());
return dataSource;
}
public static void load(String className,
AsyncCallback<DataSourceDescriptor> callback) {
service.getDataSource(className, callback);
}
public static void loadDataSource(String className,
final AsyncCallback<DataSource> callback) {
AsyncCallback<DataSourceDescriptor> callbackDSD = new AsyncCallback<DataSourceDescriptor>() {
@Override
public void onSuccess(DataSourceDescriptor result) {
String s = "";
RestDataSource dataSource = new RestDataSource() {
@Override
protected Object transformRequest(DSRequest dsRequest) {
return super.transformRequest(dsRequest);
}
};
dataSource.setID(result.get("name"));
for (String param : result.keySet()) {
dataSource.setAttribute(param, result.get(param), false);
}
String baseUrl = Window.Location.getProtocol() + "//"
+ Window.Location.getHost();
String fetchURL = ((baseUrl + "/api/fetch/" + result
.get("name")));
String addURL = (baseUrl + "/api/add/" + result.get("name"));
String updateURL = (baseUrl + "/api/update/" + result
.get("name"));
String removeURL = (baseUrl + "/api/remove/" + result
.get("name"));
OperationBinding remove = new OperationBinding(
DSOperationType.REMOVE, removeURL);
OperationBinding update = new OperationBinding(
DSOperationType.UPDATE, updateURL);
OperationBinding add = new OperationBinding(
DSOperationType.ADD, addURL);
OperationBinding fetch = new OperationBinding(
DSOperationType.FETCH, fetchURL);
fetch.setDataFormat(DSDataFormat.JSON);
add.setDataFormat(DSDataFormat.JSON);
update.setDataFormat(DSDataFormat.JSON);
remove.setDataFormat(DSDataFormat.JSON);
dataSource.setOperationBindings(fetch, add, update, remove);
for (String field : result.getFields().keySet()) {
DataSourceField f = new DataSourceField();
f.setName(field);
s += "<br> " + field + " ";
Map<String, String> fieldMap = result.getFields()
.get(field);
for (String param : fieldMap.keySet()) {
String value = fieldMap.get(param);
s += param + "=" + value + " ";
f.setAttribute(param, value);
}
dataSource.addField(f);
}
callback.onSuccess(dataSource);
}
@Override
public void onFailure(Throwable caught) {
callback.onFailure(caught);
}
};
load(className, callbackDSD);
}
}