1 package org.opendaylight.controller.md.sal.dom.xsql;
3 import java.io.Serializable;
4 import java.sql.SQLException;
5 import java.util.Collection;
6 import java.util.HashMap;
7 import java.util.HashSet;
11 public class XSQLBluePrintNode implements Serializable {
13 private static final long serialVersionUID = 1L;
14 private Class<?> myInterface = null;
15 private String myInterfaceString = null;
16 private Set<XSQLBluePrintRelation> relations = new HashSet<XSQLBluePrintRelation>();
17 private Set<XSQLBluePrintNode> inheritingNodes = new HashSet<XSQLBluePrintNode>();
18 private Set<XSQLBluePrintNode> children = new HashSet<XSQLBluePrintNode>();
19 private XSQLBluePrintNode parent = null;
21 private int level = -1;
22 private transient Set<String> parentHierarchySet = null;
23 private String myInterfaceName = null;
24 private Set<XSQLColumn> columns = new HashSet<XSQLColumn>();
25 private Map<String, XSQLColumn> origNameToColumn = new HashMap<String, XSQLColumn>();
27 private transient Object odlNode = null;
28 private boolean module = false;
29 private String bluePrintTableName = null;
30 private String odlTableName = null;
31 private String origName = null;
33 public XSQLBluePrintNode(String name, String _origName, int _level) {
35 this.odlTableName = name;
36 this.bluePrintTableName = name;
37 this.origName = _origName;
40 public XSQLBluePrintNode(Class<?> _myInterface, int _level) {
41 this.myInterface = _myInterface;
42 this.myInterfaceString = _myInterface.getName();
43 this.myInterfaceName = myInterface.getSimpleName();
47 public XSQLBluePrintNode(Object _odlNode, int _level,
48 XSQLBluePrintNode _parent) {
49 this.odlNode = _odlNode;
51 this.module = XSQLODLUtils.isModule(_odlNode);
52 this.parent = _parent;
53 this.bluePrintTableName = XSQLODLUtils.getBluePrintName(_odlNode);
54 this.odlTableName = XSQLODLUtils.getODLNodeName(this.odlNode);
57 public String getOrigName() {
61 public String getBluePrintNodeName() {
62 return this.bluePrintTableName;
65 public boolean isModule() {
69 public Set<XSQLBluePrintNode> getChildren() {
73 public String getODLTableName() {
74 if (this.odlTableName == null) {
75 this.odlTableName = XSQLODLUtils.getODLNodeName(this.odlNode);
77 return this.odlTableName;
80 public Object getODLNode() {
84 public void AddChild(XSQLBluePrintNode ch) {
85 this.children.add(ch);
88 public boolean isModelChild(Class<?> p) {
89 if (this.relations.size() == 0) {
92 for (XSQLBluePrintRelation parentRelation : this.relations) {
93 if (parentRelation.getParent().getInterface().equals(p)) {
97 for (XSQLBluePrintRelation dtr : this.relations) {
98 XSQLBluePrintNode parent = dtr.getParent();
99 if (!parent.getInterface().equals(this.getInterface())
100 && !parent.getInterface().isAssignableFrom(
102 && this.getInterface().isAssignableFrom(
103 parent.getInterface()) && parent.isModelChild(p)) {
110 public Set<XSQLBluePrintRelation> getRelations() {
111 return this.relations;
114 public String getClassName() {
115 return this.myInterfaceString;
118 public void addInheritingNode(XSQLBluePrintNode node) {
119 this.inheritingNodes.add(node);
122 public Set<XSQLBluePrintNode> getInheritingNodes() {
123 return this.inheritingNodes;
126 public void addColumn(Object node, String tableName) {
127 XSQLColumn c = new XSQLColumn(node, getBluePrintNodeName(), this);
131 public XSQLColumn addColumn(String name, String tableName, String origName,
132 String origTableName) {
133 XSQLColumn c = new XSQLColumn(name, tableName, origName, origTableName);
135 this.origNameToColumn.put(origName, c);
139 public void addColumn(String methodName) {
140 if (methodName.startsWith("get")) {
141 methodName = methodName.substring(3);
142 } else if (methodName.startsWith("is")) {
143 methodName = methodName.substring(2);
145 XSQLColumn c = new XSQLColumn(methodName, myInterfaceName, null);
149 public Collection<XSQLColumn> getColumns() {
153 public XSQLColumn findColumnByName(String name) throws SQLException {
155 XSQLColumn exactMatch = null;
156 XSQLColumn indexOfMatch = null;
157 XSQLColumn exactLowercaseMatch = null;
158 XSQLColumn indexOfLowerCaseMatch = null;
160 for (XSQLColumn col : columns) {
161 if (col.getName().equals(name)) {
164 if (col.getName().indexOf(name) != -1) {
167 if (col.getName().toLowerCase().equals(name.toLowerCase())) {
168 exactLowercaseMatch = col;
170 if (col.getName().toLowerCase().indexOf(name.toLowerCase()) != -1) {
171 indexOfLowerCaseMatch = col;
175 if (exactMatch != null) {
178 if (exactLowercaseMatch != null) {
179 return exactLowercaseMatch;
181 if (indexOfMatch != null) {
184 if (indexOfLowerCaseMatch != null) {
185 return indexOfLowerCaseMatch;
188 throw new SQLException("Unknown field name '" + name + "'");
191 public void addParent(XSQLBluePrintNode parent, String property) {
193 if (property.equals("ContainingTPs")) {
196 // Method m = parent.getInterface().getMethod("get"+property, null);
197 // if(!m.getDeclaringClass().equals(parent.getInterface()))
199 XSQLBluePrintRelation rel = new XSQLBluePrintRelation(parent,
200 property, myInterface);
202 } catch (Exception err) {
203 err.printStackTrace();
207 public XSQLBluePrintNode getParent() {
211 public Set<XSQLBluePrintRelation> getClonedParents() {
212 Set<XSQLBluePrintRelation> result = new HashSet<XSQLBluePrintRelation>();
213 result.addAll(this.relations);
217 public String toString() {
218 if (myInterfaceName != null) {
219 return myInterfaceName;
221 if (odlNode != null) {
222 return getBluePrintNodeName();
224 if (odlTableName != null) {
230 public Class<?> getInterface() {
231 return this.myInterface;
234 public int getLevel() {
239 public boolean equals(Object obj) {
240 XSQLBluePrintNode other = (XSQLBluePrintNode) obj;
241 if (odlNode != null) {
242 return getBluePrintNodeName().equals(other.getBluePrintNodeName());
243 } else if (this.odlTableName == null && other.odlTableName != null)
245 if (this.odlTableName != null && other.odlTableName == null)
248 return this.odlTableName.equals(other.odlTableName);
252 public int hashCode() {
253 if (myInterfaceString != null) {
254 return myInterfaceString.hashCode();
255 } else if (odlNode != null) {
256 return bluePrintTableName.hashCode();