/* * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html */ package org.opendaylight.controller.md.sal.dom.xsql; import java.io.Serializable; import java.sql.SQLException; import java.util.Collection; import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set; /** * @author Sharon Aicler(saichler@gmail.com) **/ public class XSQLBluePrintNode implements Serializable { private static final long serialVersionUID = 1L; private Class myInterface = null; private String myInterfaceString = null; private Set relations = new HashSet<>(); private Set inheritingNodes = new HashSet<>(); private Set children = new HashSet<>(); private XSQLBluePrintNode parent = null; private int level = -1; private transient Set parentHierarchySet = null; private String myInterfaceName = null; private Set columns = new HashSet<>(); private Map origNameToColumn = new HashMap<>(); private transient Object[] odlSchemaNodes = null; private boolean module = false; private String bluePrintTableName = null; private String odlTableName = null; private String origName = null; public void mergeAugmentation(XSQLBluePrintNode aug) { this.relations.addAll(aug.relations); this.inheritingNodes.addAll(aug.inheritingNodes); this.children.addAll(aug.children); this.columns.addAll(aug.columns); this.origNameToColumn.putAll(aug.origNameToColumn); if (aug.odlSchemaNodes != null) { for (Object sn : aug.odlSchemaNodes) { addToSchemaNodes(sn); } } } public XSQLBluePrintNode(String name, String _origName, int _level) { this.level = _level; this.odlTableName = name; this.bluePrintTableName = name; this.origName = _origName; } public XSQLBluePrintNode(Class _myInterface, int _level) { this.myInterface = _myInterface; this.myInterfaceString = _myInterface.getName(); this.myInterfaceName = myInterface.getSimpleName(); this.level = _level; } public XSQLBluePrintNode(Object _odlNode, int _level, XSQLBluePrintNode _parent) { addToSchemaNodes(_odlNode); this.level = _level; this.module = XSQLODLUtils.isModule(_odlNode); this.parent = _parent; this.bluePrintTableName = XSQLODLUtils.getBluePrintName(_odlNode); this.odlTableName = XSQLODLUtils .getODLNodeName(getFirstFromSchemaNodes()); } private void addToSchemaNodes(Object schemaObject) { if (this.odlSchemaNodes == null) this.odlSchemaNodes = new Object[1]; else { Object[] temp = new Object[this.odlSchemaNodes.length + 1]; System.arraycopy(this.odlSchemaNodes, 0, temp, 0, this.odlSchemaNodes.length); this.odlSchemaNodes = temp; } this.odlSchemaNodes[this.odlSchemaNodes.length - 1] = schemaObject; } public Object getFirstFromSchemaNodes() { if (this.odlSchemaNodes == null) { return null; } return this.odlSchemaNodes[0]; } public String getOrigName() { return this.origName; } public String getBluePrintNodeName() { return this.bluePrintTableName; } public boolean isModule() { return this.module; } public Set getChildren() { return this.children; } public String getODLTableName() { if (this.odlTableName == null) { this.odlTableName = XSQLODLUtils .getODLNodeName(getFirstFromSchemaNodes()); } return this.odlTableName; } public void addChild(XSQLBluePrintNode ch) { this.children.add(ch); } public boolean isModelChild(Class p) { if (this.relations.size() == 0) { return false; } for (XSQLBluePrintRelation parentRelation : this.relations) { if (parentRelation.getParent().getInterface().equals(p)) { return true; } } for (XSQLBluePrintRelation dtr : this.relations) { XSQLBluePrintNode parent = dtr.getParent(); if (!parent.getInterface().equals(this.getInterface()) && !parent.getInterface().isAssignableFrom( this.getInterface()) && this.getInterface().isAssignableFrom( parent.getInterface()) && parent.isModelChild(p)) { return true; } } return false; } public Set getRelations() { return this.relations; } public String getClassName() { return this.myInterfaceString; } public void addInheritingNode(XSQLBluePrintNode node) { this.inheritingNodes.add(node); } public Set getInheritingNodes() { return this.inheritingNodes; } public void addColumn(Object node, String tableName) { XSQLColumn c = new XSQLColumn(node, getBluePrintNodeName(), this); this.columns.add(c); } public XSQLColumn addColumn(String name, String tableName, String origName, String origTableName) { XSQLColumn c = new XSQLColumn(name, tableName, origName, origTableName); this.columns.add(c); this.origNameToColumn.put(origName, c); return c; } public void addColumn(String methodName) { if (methodName.startsWith("get")) { methodName = methodName.substring(3); } else if (methodName.startsWith("is")) { methodName = methodName.substring(2); } XSQLColumn c = new XSQLColumn(methodName, myInterfaceName, null); this.columns.add(c); } public Collection getColumns() { return this.columns; } public XSQLColumn findColumnByName(String name) throws SQLException { XSQLColumn exactMatch = null; XSQLColumn indexOfMatch = null; XSQLColumn exactLowercaseMatch = null; XSQLColumn indexOfLowerCaseMatch = null; for (XSQLColumn col : columns) { if (col.getName().equals(name)) { exactMatch = col; } if (col.getName().indexOf(name) != -1) { indexOfMatch = col; } if (col.getName().toLowerCase().equals(name.toLowerCase())) { exactLowercaseMatch = col; } if (col.getName().toLowerCase().indexOf(name.toLowerCase()) != -1) { indexOfLowerCaseMatch = col; } } if (exactMatch != null) { return exactMatch; } if (exactLowercaseMatch != null) { return exactLowercaseMatch; } if (indexOfMatch != null) { return indexOfMatch; } if (indexOfLowerCaseMatch != null) { return indexOfLowerCaseMatch; } throw new SQLException("Unknown field name '" + name + "'"); } public void addParent(XSQLBluePrintNode parent, String property) { try { if (property.equals("ContainingTPs")) { return; } // Method m = parent.getInterface().getMethod("get"+property, null); // if(!m.getDeclaringClass().equals(parent.getInterface())) // return; XSQLBluePrintRelation rel = new XSQLBluePrintRelation(parent, property, myInterface); relations.add(rel); } catch (Exception err) { err.printStackTrace(); } } public XSQLBluePrintNode getParent() { return this.parent; } public Set getClonedParents() { Set result = new HashSet<>(); result.addAll(this.relations); return result; } public String toString() { if (myInterfaceName != null) { return myInterfaceName; } if (this.odlSchemaNodes != null) { return getBluePrintNodeName(); } if (odlTableName != null) { return odlTableName; } return "Unknown"; } public Class getInterface() { return this.myInterface; } public int getLevel() { return this.level; } @Override public boolean equals(Object obj) { XSQLBluePrintNode other = (XSQLBluePrintNode) obj; if (this.odlSchemaNodes != null) { return getBluePrintNodeName().equals(other.getBluePrintNodeName()); } else if (this.odlTableName == null && other.odlTableName != null) { return false; } if (this.odlTableName != null && other.odlTableName == null) { return false; } else { return this.odlTableName.equals(other.odlTableName); } } @Override public int hashCode() { if (myInterfaceString != null) { return myInterfaceString.hashCode(); } else if (this.odlSchemaNodes != null) { return bluePrintTableName.hashCode(); } return 0; } }