Merge "Do not override managed versions"
[controller.git] / opendaylight / md-sal / sal-dom-xsql / src / main / java / org / opendaylight / controller / md / sal / dom / xsql / XSQLBluePrintNode.java
1 package org.opendaylight.controller.md.sal.dom.xsql;
2
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;
8 import java.util.Map;
9 import java.util.Set;
10
11 public class XSQLBluePrintNode implements Serializable {
12
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;
20
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>();
26
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;
32
33     public XSQLBluePrintNode(String name, String _origName, int _level) {
34         this.level = _level;
35         this.odlTableName = name;
36         this.bluePrintTableName = name;
37         this.origName = _origName;
38     }
39
40     public XSQLBluePrintNode(Class<?> _myInterface, int _level) {
41         this.myInterface = _myInterface;
42         this.myInterfaceString = _myInterface.getName();
43         this.myInterfaceName = myInterface.getSimpleName();
44         this.level = _level;
45     }
46
47     public XSQLBluePrintNode(Object _odlNode, int _level,
48             XSQLBluePrintNode _parent) {
49         this.odlNode = _odlNode;
50         this.level = _level;
51         this.module = XSQLODLUtils.isModule(_odlNode);
52         this.parent = _parent;
53         this.bluePrintTableName = XSQLODLUtils.getBluePrintName(_odlNode);
54         this.odlTableName = XSQLODLUtils.getODLNodeName(this.odlNode);
55     }
56
57     public String getOrigName() {
58         return this.origName;
59     }
60
61     public String getBluePrintNodeName() {
62         return this.bluePrintTableName;
63     }
64
65     public boolean isModule() {
66         return this.module;
67     }
68
69     public Set<XSQLBluePrintNode> getChildren() {
70         return this.children;
71     }
72
73     public String getODLTableName() {
74         if (this.odlTableName == null) {
75             this.odlTableName = XSQLODLUtils.getODLNodeName(this.odlNode);
76         }
77         return this.odlTableName;
78     }
79
80     public Object getODLNode() {
81         return this.odlNode;
82     }
83
84     public void AddChild(XSQLBluePrintNode ch) {
85         this.children.add(ch);
86     }
87
88     public boolean isModelChild(Class<?> p) {
89         if (this.relations.size() == 0) {
90             return false;
91         }
92         for (XSQLBluePrintRelation parentRelation : this.relations) {
93             if (parentRelation.getParent().getInterface().equals(p)) {
94                 return true;
95             }
96         }
97         for (XSQLBluePrintRelation dtr : this.relations) {
98             XSQLBluePrintNode parent = dtr.getParent();
99             if (!parent.getInterface().equals(this.getInterface())
100                     && !parent.getInterface().isAssignableFrom(
101                             this.getInterface())
102                     && this.getInterface().isAssignableFrom(
103                             parent.getInterface()) && parent.isModelChild(p)) {
104                 return true;
105             }
106         }
107         return false;
108     }
109
110     public Set<XSQLBluePrintRelation> getRelations() {
111         return this.relations;
112     }
113
114     public String getClassName() {
115         return this.myInterfaceString;
116     }
117
118     public void addInheritingNode(XSQLBluePrintNode node) {
119         this.inheritingNodes.add(node);
120     }
121
122     public Set<XSQLBluePrintNode> getInheritingNodes() {
123         return this.inheritingNodes;
124     }
125
126     public void addColumn(Object node, String tableName) {
127         XSQLColumn c = new XSQLColumn(node, getBluePrintNodeName(), this);
128         this.columns.add(c);
129     }
130
131     public XSQLColumn addColumn(String name, String tableName, String origName,
132             String origTableName) {
133         XSQLColumn c = new XSQLColumn(name, tableName, origName, origTableName);
134         this.columns.add(c);
135         this.origNameToColumn.put(origName, c);
136         return c;
137     }
138
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);
144         }
145         XSQLColumn c = new XSQLColumn(methodName, myInterfaceName, null);
146         this.columns.add(c);
147     }
148
149     public Collection<XSQLColumn> getColumns() {
150         return this.columns;
151     }
152
153     public XSQLColumn findColumnByName(String name) throws SQLException {
154
155         XSQLColumn exactMatch = null;
156         XSQLColumn indexOfMatch = null;
157         XSQLColumn exactLowercaseMatch = null;
158         XSQLColumn indexOfLowerCaseMatch = null;
159
160         for (XSQLColumn col : columns) {
161             if (col.getName().equals(name)) {
162                 exactMatch = col;
163             }
164             if (col.getName().indexOf(name) != -1) {
165                 indexOfMatch = col;
166             }
167             if (col.getName().toLowerCase().equals(name.toLowerCase())) {
168                 exactLowercaseMatch = col;
169             }
170             if (col.getName().toLowerCase().indexOf(name.toLowerCase()) != -1) {
171                 indexOfLowerCaseMatch = col;
172             }
173         }
174
175         if (exactMatch != null) {
176             return exactMatch;
177         }
178         if (exactLowercaseMatch != null) {
179             return exactLowercaseMatch;
180         }
181         if (indexOfMatch != null) {
182             return indexOfMatch;
183         }
184         if (indexOfLowerCaseMatch != null) {
185             return indexOfLowerCaseMatch;
186         }
187
188         throw new SQLException("Unknown field name '" + name + "'");
189     }
190
191     public void addParent(XSQLBluePrintNode parent, String property) {
192         try {
193             if (property.equals("ContainingTPs")) {
194                 return;
195             }
196             // Method m = parent.getInterface().getMethod("get"+property, null);
197             // if(!m.getDeclaringClass().equals(parent.getInterface()))
198             // return;
199             XSQLBluePrintRelation rel = new XSQLBluePrintRelation(parent,
200                     property, myInterface);
201             relations.add(rel);
202         } catch (Exception err) {
203             err.printStackTrace();
204         }
205     }
206
207     public XSQLBluePrintNode getParent() {
208         return this.parent;
209     }
210
211     public Set<XSQLBluePrintRelation> getClonedParents() {
212         Set<XSQLBluePrintRelation> result = new HashSet<XSQLBluePrintRelation>();
213         result.addAll(this.relations);
214         return result;
215     }
216
217     public String toString() {
218         if (myInterfaceName != null) {
219             return myInterfaceName;
220         }
221         if (odlNode != null) {
222             return getBluePrintNodeName();
223         }
224         if (odlTableName != null) {
225             return odlTableName;
226         }
227         return "Unknown";
228     }
229
230     public Class<?> getInterface() {
231         return this.myInterface;
232     }
233
234     public int getLevel() {
235         return this.level;
236     }
237
238     @Override
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)
244             return false;
245         if (this.odlTableName != null && other.odlTableName == null)
246             return false;
247         else
248             return this.odlTableName.equals(other.odlTableName);
249     }
250
251     @Override
252     public int hashCode() {
253         if (myInterfaceString != null) {
254             return myInterfaceString.hashCode();
255         } else if (odlNode != null) {
256             return bluePrintTableName.hashCode();
257         }
258         return 0;
259     }
260
261 }