public class ResultTranslator {
private Object convert(Object obj) {
if (obj instanceof FactHandle && !(obj instanceof DisconnectedFactHandle)) {
return new DisconnectedFactHandle(((FactHandle) obj).toExternalForm());
}
if (obj instanceof NativeQueryResults) {
NativeQueryResults nativeQueryResults = (NativeQueryResults) obj;
List<Declaration> declrs = new ArrayList<Declaration>();
HashMap<String, Integer> identifiers = new HashMap<String, Integer>( );
for ( String identifier : nativeQueryResults.getIdentifiers() ) {
// we don't want to marshall the query parameters
Declaration declr = nativeQueryResults.getDeclarations().get( identifier );
ObjectType objectType = declr.getPattern().getObjectType();
if ( objectType instanceof ClassObjectType &&
((ClassObjectType) objectType).getClassType() == DroolsQuery.class ) {
continue;
}
declrs.add(declr);
identifiers.put(identifier, declrs.size() - 1);
}
ArrayList<ArrayList<Object>> results = new ArrayList<ArrayList<Object>>( nativeQueryResults.size() );
ArrayList<ArrayList<org.drools.runtime.rule.FactHandle>> factHandles = new ArrayList<ArrayList<org.drools.runtime.rule.FactHandle>> ( nativeQueryResults.size() );
for (QueryResultsRow row : nativeQueryResults) {
ArrayList<Object> objectList = new ArrayList<Object>();
ArrayList<org.drools.runtime.rule.FactHandle> factHandleList = new ArrayList<org.drools.runtime.rule.FactHandle>();
for (int i = 0; i < declrs.size(); i++) {
objectList.add(convert(row.get(declrs.get(i).getIdentifier())));
factHandleList.add(new DisconnectedFactHandle(row.getFactHandle(declrs.get(i).getIdentifier()).toExternalForm()));
}
factHandles.add(factHandleList);
results.add(objectList);
}