Search in sources :

Example 1 with QBFactory

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;
}
Also used : QBFactory(com.servoy.j2db.querybuilder.impl.QBFactory) JSONObject(org.json.JSONObject) IVisitor(com.servoy.j2db.util.visitor.IVisitor) JSONException(org.json.JSONException) VisitOnceDelegateVisitor(com.servoy.j2db.util.visitor.VisitOnceDelegateVisitor) JSONObject(org.json.JSONObject) QueryTable1(com.servoy.j2db.query.QueryTable1) UnmarshallException(org.jabsorb.serializer.UnmarshallException) QuerySelect(com.servoy.j2db.query.QuerySelect)

Aggregations

QuerySelect (com.servoy.j2db.query.QuerySelect)1 QueryTable1 (com.servoy.j2db.query.QueryTable1)1 QBFactory (com.servoy.j2db.querybuilder.impl.QBFactory)1 IVisitor (com.servoy.j2db.util.visitor.IVisitor)1 VisitOnceDelegateVisitor (com.servoy.j2db.util.visitor.VisitOnceDelegateVisitor)1 UnmarshallException (org.jabsorb.serializer.UnmarshallException)1 JSONException (org.json.JSONException)1 JSONObject (org.json.JSONObject)1