2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.controller.md.sal.dom.xsql;
10 import java.io.Serializable;
11 import java.sql.SQLException;
12 import java.util.Collection;
13 import java.util.HashMap;
14 import java.util.HashSet;
19 * @author Sharon Aicler(saichler@gmail.com)
21 public class XSQLBluePrintNode implements Serializable {
23 private static final long serialVersionUID = 1L;
24 private Class<?> myInterface = null;
25 private String myInterfaceString = null;
26 private Set<XSQLBluePrintRelation> relations = new HashSet<XSQLBluePrintRelation>();
27 private Set<XSQLBluePrintNode> inheritingNodes = new HashSet<XSQLBluePrintNode>();
28 private Set<XSQLBluePrintNode> children = new HashSet<XSQLBluePrintNode>();
29 private XSQLBluePrintNode parent = null;
31 private int level = -1;
32 private transient Set<String> parentHierarchySet = null;
33 private String myInterfaceName = null;
34 private Set<XSQLColumn> columns = new HashSet<XSQLColumn>();
35 private Map<String, XSQLColumn> origNameToColumn = new HashMap<String, XSQLColumn>();
37 private transient Object[] odlSchemaNodes = null;
38 private boolean module = false;
39 private String bluePrintTableName = null;
40 private String odlTableName = null;
41 private String origName = null;
43 public void mergeAugmentation(XSQLBluePrintNode aug) {
44 this.relations.addAll(aug.relations);
45 this.inheritingNodes.addAll(aug.inheritingNodes);
46 this.children.addAll(aug.children);
47 this.columns.addAll(aug.columns);
48 this.origNameToColumn.putAll(aug.origNameToColumn);
49 if (aug.odlSchemaNodes != null) {
50 for (Object sn : aug.odlSchemaNodes) {
56 public XSQLBluePrintNode(String name, String _origName, int _level) {
58 this.odlTableName = name;
59 this.bluePrintTableName = name;
60 this.origName = _origName;
63 public XSQLBluePrintNode(Class<?> _myInterface, int _level) {
64 this.myInterface = _myInterface;
65 this.myInterfaceString = _myInterface.getName();
66 this.myInterfaceName = myInterface.getSimpleName();
70 public XSQLBluePrintNode(Object _odlNode, int _level,
71 XSQLBluePrintNode _parent) {
72 addToSchemaNodes(_odlNode);
74 this.module = XSQLODLUtils.isModule(_odlNode);
75 this.parent = _parent;
76 this.bluePrintTableName = XSQLODLUtils.getBluePrintName(_odlNode);
77 this.odlTableName = XSQLODLUtils
78 .getODLNodeName(getFirstFromSchemaNodes());
81 private void addToSchemaNodes(Object schemaObject) {
82 if (this.odlSchemaNodes == null)
83 this.odlSchemaNodes = new Object[1];
85 Object[] temp = new Object[this.odlSchemaNodes.length + 1];
86 System.arraycopy(this.odlSchemaNodes, 0, temp, 0,
87 this.odlSchemaNodes.length);
88 this.odlSchemaNodes = temp;
90 this.odlSchemaNodes[this.odlSchemaNodes.length - 1] = schemaObject;
93 public Object getFirstFromSchemaNodes() {
94 if (this.odlSchemaNodes == null) {
97 return this.odlSchemaNodes[0];
100 public String getOrigName() {
101 return this.origName;
104 public String getBluePrintNodeName() {
105 return this.bluePrintTableName;
108 public boolean isModule() {
112 public Set<XSQLBluePrintNode> getChildren() {
113 return this.children;
116 public String getODLTableName() {
117 if (this.odlTableName == null) {
118 this.odlTableName = XSQLODLUtils
119 .getODLNodeName(getFirstFromSchemaNodes());
121 return this.odlTableName;
124 public void addChild(XSQLBluePrintNode ch) {
125 this.children.add(ch);
128 public boolean isModelChild(Class<?> p) {
129 if (this.relations.size() == 0) {
132 for (XSQLBluePrintRelation parentRelation : this.relations) {
133 if (parentRelation.getParent().getInterface().equals(p)) {
137 for (XSQLBluePrintRelation dtr : this.relations) {
138 XSQLBluePrintNode parent = dtr.getParent();
139 if (!parent.getInterface().equals(this.getInterface())
140 && !parent.getInterface().isAssignableFrom(
142 && this.getInterface().isAssignableFrom(
143 parent.getInterface()) && parent.isModelChild(p)) {
150 public Set<XSQLBluePrintRelation> getRelations() {
151 return this.relations;
154 public String getClassName() {
155 return this.myInterfaceString;
158 public void addInheritingNode(XSQLBluePrintNode node) {
159 this.inheritingNodes.add(node);
162 public Set<XSQLBluePrintNode> getInheritingNodes() {
163 return this.inheritingNodes;
166 public void addColumn(Object node, String tableName) {
167 XSQLColumn c = new XSQLColumn(node, getBluePrintNodeName(), this);
171 public XSQLColumn addColumn(String name, String tableName, String origName,
172 String origTableName) {
173 XSQLColumn c = new XSQLColumn(name, tableName, origName, origTableName);
175 this.origNameToColumn.put(origName, c);
179 public void addColumn(String methodName) {
180 if (methodName.startsWith("get")) {
181 methodName = methodName.substring(3);
182 } else if (methodName.startsWith("is")) {
183 methodName = methodName.substring(2);
185 XSQLColumn c = new XSQLColumn(methodName, myInterfaceName, null);
189 public Collection<XSQLColumn> getColumns() {
193 public XSQLColumn findColumnByName(String name) throws SQLException {
195 XSQLColumn exactMatch = null;
196 XSQLColumn indexOfMatch = null;
197 XSQLColumn exactLowercaseMatch = null;
198 XSQLColumn indexOfLowerCaseMatch = null;
200 for (XSQLColumn col : columns) {
201 if (col.getName().equals(name)) {
204 if (col.getName().indexOf(name) != -1) {
207 if (col.getName().toLowerCase().equals(name.toLowerCase())) {
208 exactLowercaseMatch = col;
210 if (col.getName().toLowerCase().indexOf(name.toLowerCase()) != -1) {
211 indexOfLowerCaseMatch = col;
215 if (exactMatch != null) {
218 if (exactLowercaseMatch != null) {
219 return exactLowercaseMatch;
221 if (indexOfMatch != null) {
224 if (indexOfLowerCaseMatch != null) {
225 return indexOfLowerCaseMatch;
228 throw new SQLException("Unknown field name '" + name + "'");
231 public void addParent(XSQLBluePrintNode parent, String property) {
233 if (property.equals("ContainingTPs")) {
236 // Method m = parent.getInterface().getMethod("get"+property, null);
237 // if(!m.getDeclaringClass().equals(parent.getInterface()))
239 XSQLBluePrintRelation rel = new XSQLBluePrintRelation(parent,
240 property, myInterface);
242 } catch (Exception err) {
243 err.printStackTrace();
247 public XSQLBluePrintNode getParent() {
251 public Set<XSQLBluePrintRelation> getClonedParents() {
252 Set<XSQLBluePrintRelation> result = new HashSet<XSQLBluePrintRelation>();
253 result.addAll(this.relations);
257 public String toString() {
258 if (myInterfaceName != null) {
259 return myInterfaceName;
261 if (this.odlSchemaNodes != null) {
262 return getBluePrintNodeName();
264 if (odlTableName != null) {
270 public Class<?> getInterface() {
271 return this.myInterface;
274 public int getLevel() {
279 public boolean equals(Object obj) {
280 XSQLBluePrintNode other = (XSQLBluePrintNode) obj;
281 if (this.odlSchemaNodes != null) {
282 return getBluePrintNodeName().equals(other.getBluePrintNodeName());
283 } else if (this.odlTableName == null && other.odlTableName != null) {
286 if (this.odlTableName != null && other.odlTableName == null) {
289 return this.odlTableName.equals(other.odlTableName);
294 public int hashCode() {
295 if (myInterfaceString != null) {
296 return myInterfaceString.hashCode();
297 } else if (this.odlSchemaNodes != null) {
298 return bluePrintTableName.hashCode();