use of com.servoy.j2db.querybuilder.impl.QBFactory in project servoy-client by Servoy.
the class QueryBuilderSerializer method unmarshall.
public Object unmarshall(SerializerState state, Class clazz, Object o) throws UnmarshallException {
final QBFactory queryBuilderFactory = queryBuilderFactoryProvider.getQueryBuilderFactory();
if (queryBuilderFactory == null) {
throw new UnmarshallException("QueryBuilderSerializer needs query builder factory");
}
JSONObject jso = (JSONObject) o;
String xml;
String dataSource;
String alias;
try {
// required
xml = jso.getString("query");
// required
dataSource = jso.getString("datasource");
// optional
alias = jso.optString("alias", null);
} catch (JSONException e) {
throw new UnmarshallException("Could not get the query in QueryBuilderSerializer", e);
}
if (jso.has("javaClass")) {
try {
clazz = Class.forName(jso.getString("javaClass"));
} catch (ClassNotFoundException e) {
throw new UnmarshallException(e.getMessage(), e);
} catch (JSONException e) {
throw new UnmarshallException("Could not find javaClass", e);
}
}
Object returnValue = null;
if (QBSelect.class.equals(clazz)) {
final String queryDataSource = dataSource;
QuerySelect query = (QuerySelect) getXstream().fromXML(xml);
// If the xml was from before adding dataSource to QueryTable, the query will contain QueryTable1 objects, these need to be replaced
// with QueryTable with resolved dataSource.
query.acceptVisitor(new VisitOnceDelegateVisitor(new IVisitor() {
public Object visit(Object obj) {
if (obj instanceof QueryTable1) {
return ((QueryTable1) obj).addDataSource(queryBuilderFactory.resolveDataSource(queryDataSource, ((QueryTable1) obj).getName()));
}
return obj;
}
}));
returnValue = queryBuilderFactory.createSelect(dataSource, alias, query);
}
if (returnValue == null) {
throw new UnmarshallException("invalid class " + clazz);
}
state.setSerialized(o, returnValue);
return returnValue;
}
Aggregations