Merge "BUG-2878: Add the XML declaration to every outgoing NETCONF message."
[controller.git] / opendaylight / md-sal / sal-dom-xsql / src / main / java / org / opendaylight / controller / md / sal / dom / xsql / jdbc / JDBCResultSet.java
index 37eacd5595975404a03cbf0390e12994d65bbe8c..6689908204e19e9aced43dccbb28d2dd5a8f2c07 100644 (file)
@@ -41,6 +41,9 @@ import org.opendaylight.controller.md.sal.dom.xsql.XSQLODLUtils;
 public class JDBCResultSet implements Serializable, ResultSet,
         ResultSetMetaData {
     private static final long serialVersionUID = -7450200738431047057L;
+    private static final ClassLoader CLASS_LOADER = JDBCResultSet.class.getClassLoader();
+    private static final Class<?>[] PROXY_INTERFACES = new Class[] { ResultSet.class };
+    private static int nextID = 0;
 
     private String sql = null;
     private List<XSQLBluePrintNode> tablesInQuery = new ArrayList<XSQLBluePrintNode>();
@@ -50,7 +53,6 @@ public class JDBCResultSet implements Serializable, ResultSet,
     private transient Map<String, Object> currentRecord = null;
     private boolean finished = false;
     private int id = 0;
-    private static Integer nextID = new Integer(0);
     public int numberOfTasks = 0;
     private Map<String, Map<XSQLColumn, List<XSQLCriteria>>> criteria = new ConcurrentHashMap<String, Map<XSQLColumn, List<XSQLCriteria>>>();
     private Exception err = null;
@@ -58,7 +60,7 @@ public class JDBCResultSet implements Serializable, ResultSet,
     private transient Map<String,JDBCResultSet> subQueries = new HashMap<String,JDBCResultSet>();
 
     public ResultSet getProxy() {
-         return (ResultSet) Proxy.newProxyInstance(this.getClass().getClassLoader(), new Class[] {ResultSet.class }, new JDBCProxy(this));
+         return (ResultSet) Proxy.newProxyInstance(CLASS_LOADER, PROXY_INTERFACES, new JDBCProxy(this));
     }
 
     public void setSQL(String _sql) {
@@ -66,16 +68,18 @@ public class JDBCResultSet implements Serializable, ResultSet,
     }
 
     public JDBCResultSet addSubQuery(String _sql,String logicalName) {
-        if(subQueries == null)
+        if (subQueries == null) {
             subQueries = new HashMap<String,JDBCResultSet>();
+        }
         JDBCResultSet rs = new JDBCResultSet(_sql);
         this.subQueries.put(logicalName,rs);
         return rs;
     }
 
     public Map<String,JDBCResultSet> getSubQueries() {
-        if(this.subQueries==null)
+        if (this.subQueries==null) {
             this.subQueries = new HashMap<>();
+        }
         return this.subQueries;
     }
 
@@ -381,13 +385,15 @@ public class JDBCResultSet implements Serializable, ResultSet,
         for (Object child : children) {
 
             String odlNodeName = XSQLODLUtils.getNodeIdentiofier(child);
-            if (odlNodeName == null)
+            if (odlNodeName == null) {
                 continue;
+            }
 
             XSQLBluePrintNode eNodes[] = bluePrint
                     .getBluePrintNodeByODLTableName(odlNodeName);
-            if (eNodes == null)
+            if (eNodes == null) {
                 continue;
+            }
 
             boolean match = false;
             for (XSQLBluePrintNode enode : eNodes) {
@@ -397,8 +403,9 @@ public class JDBCResultSet implements Serializable, ResultSet,
                 }
             }
 
-            if (!match)
+            if (!match) {
                 continue;
+            }
 
             if (child.getClass().getName().endsWith("ImmutableContainerNode")) {
                 result.add(child);
@@ -418,20 +425,20 @@ public class JDBCResultSet implements Serializable, ResultSet,
         return result;
     }
 
-    public List<Record> addRecords(Object element, XSQLBluePrintNode node,
-            boolean root, String tableName, XSQLBluePrint bluePrint) {
-
+    public List<Record> addRecords(Object element, XSQLBluePrintNode node,boolean root, String tableName, XSQLBluePrint bluePrint) {
         List<Record> result = new LinkedList<Record>();
+        //In case this is a sibling to the requested table, the elenment type
+        //won't be in the path of the leaf node
+        if(node==null){
+            return result;
+        }
         String nodeID = XSQLODLUtils.getNodeIdentiofier(element);
         if (node.getODLTableName().equals(nodeID)) {
-            XSQLBluePrintNode bluePrintNode = bluePrint
-                    .getBluePrintNodeByODLTableName(nodeID)[0];
+            XSQLBluePrintNode bluePrintNode = bluePrint.getBluePrintNodeByODLTableName(nodeID)[0];
             Record rec = new Record();
             rec.element = element;
-            XSQLBluePrintNode bpn = this.tablesInQueryMap.get(bluePrintNode
-                    .getBluePrintNodeName());
-            if (this.criteria.containsKey(bluePrintNode.getBluePrintNodeName())
-                    || bpn != null) {
+            XSQLBluePrintNode bpn = this.tablesInQueryMap.get(bluePrintNode.getBluePrintNodeName());
+            if (this.criteria.containsKey(bluePrintNode.getBluePrintNodeName()) || bpn != null) {
                 Map<String, Object> allKeyValues = collectColumnValues(element, bpn);
                 if (!(isObjectFitCriteria(allKeyValues,
                         bpn.getBluePrintNodeName()) == 1)) {
@@ -448,8 +455,7 @@ public class JDBCResultSet implements Serializable, ResultSet,
         }
 
         XSQLBluePrintNode parent = node.getParent();
-        List<Record> subRecords = addRecords(element, parent, false, tableName,
-                bluePrint);
+        List<Record> subRecords = addRecords(element, parent, false, tableName,bluePrint);
         for (Record subRec : subRecords) {
             List<Object> subO = getChildren(subRec.element, tableName,
                     bluePrint);
@@ -466,8 +472,9 @@ public class JDBCResultSet implements Serializable, ResultSet,
                     for (XSQLBluePrintNode eNode : eNodes) {
                         bpn = this.tablesInQueryMap.get(eNode
                                 .getBluePrintNodeName());
-                        if (bpn != null)
+                        if (bpn != null) {
                             break;
+                        }
                     }
                     boolean isObjectInCriteria = true;
                     if (bpn != null) {
@@ -482,8 +489,9 @@ public class JDBCResultSet implements Serializable, ResultSet,
 
                     if (isObjectInCriteria) {
                         if (root) {
-                            if (!rec.data.isEmpty())
+                            if (!rec.data.isEmpty()) {
                                 addRecord(rec.data);
+                            }
                         } else {
                             result.add(rec);
                         }