1 package com.daffodilwoods.daffodildb.server.serversystem.deeprecordcopy; 2 3 import java.util.*; 4 5 import com.daffodilwoods.daffodildb.server.datadictionarysystem.*; 6 import com.daffodilwoods.database.general.*; 7 import com.daffodilwoods.database.resource.*; 8 public class HierarchyCreator{ 9 10 QualifiedIdentifier tableName; 11 _DataDictionary dataDictionary; 12 ArrayList tables = new ArrayList(); 13 TableCopy parentTableCopy; 14 String [] tablesToBeIncluded; 15 boolean performChecking ; 16 int level = 1; 17 18 public HierarchyCreator(QualifiedIdentifier tableName0,_DataDictionary dataDictionary0 , String [] tableNames , boolean performChecking0 ) { 19 tableName = tableName0; 20 dataDictionary = dataDictionary0; 21 tablesToBeIncluded = tableNames; 22 performChecking = performChecking0; 23 } 24 25 public synchronized void createHierarchy() throws DException{ 26 parentTableCopy = new TableCopy(tableName); 27 parentTableCopy.level = 1; 28 tables.add(parentTableCopy); 29 if( performChecking ) 30 if( tablesToBeIncluded == null || tablesToBeIncluded.length == 0 ) 31 return; 32 for (int i = 0; i < tables.size(); i++) { 33 initialise((TableCopy)tables.get(i)); 34 } 35 } 36 37 38 public synchronized TableCopy getRootNode(){ 39 return parentTableCopy; 40 } 41 42 43 public void initialise(TableCopy tableCopy1) throws DException{ 44 tableCopy1.setTableInfo(getTableInformation(tableCopy1)); 45 int columnCount = dataDictionary.getColumnCharacteristics(tableCopy1.tablename,true).getColumnCount(); 46 _DDSConstraintOperations dds= dataDictionary.getDDSConstraintsOperation(); 47 _ReferentialConstraint[] referencedConstraints = dds.getReferencedConstraintCharacteristics(tableCopy1.tablename,false). 48 getReferencedConstraintsForDelete(); 49 _ReferentialConstraint[] referencingConstraints = dds.getReferencingConstraintCharacteristics(tableCopy1.tablename,false). 50 getReferencingConstraints(); 51 if(referencingConstraints!=null){ 52 tableCopy1.tableInf.setReferencingconstraints(referencingConstraints); 53 tableCopy1.tableInf .setReferencingColumns(getReferencingColumns(referencingConstraints,columnCount)); 54 checkForOffHierarchyParents(tableCopy1,referencingConstraints); 55 } 56 if(referencedConstraints == null){ 57 return; 58 } 59 tableCopy1.tableInf.setReferencedColumns(getReferencedColumns(referencedConstraints,columnCount)); 60 61 for (int i = 0; i < referencedConstraints.length; i++) { 62 if( referencedConstraints[i].getDelete_Rule() == 7 ){ 63 QualifiedIdentifier referencingTable = referencedConstraints[i].getReferencingTable(); 64 if( performChecking ){ 65 if( copyToMake(referencingTable.getName()) ){ 66 TableCopy childTable = getTableCopy(referencingTable); 67 tableCopy1.addChild(childTable,referencedConstraints[i]); 68 childTable.addParent(tableCopy1); 69 } 70 }else{ 71 TableCopy childTable = getTableCopy(referencingTable); 72 tableCopy1.addChild(childTable,referencedConstraints[i]); 73 childTable.addParent(tableCopy1); 74 } 75 } 76 } 77 } 78 79 80 public TableCopy getTableCopy(QualifiedIdentifier tableName){ 81 for (int i = 0; i < tables.size(); i++) { 82 if(tableName.equals(((TableCopy)tables.get(i)).tablename)) 83 return (TableCopy)tables.get(i); 84 } 85 TableCopy tb = new TableCopy(tableName); 86 tables.add(tb); 87 return tb; 88 } 89 90 public TableCopy getTableCopyForOffHierarchy(QualifiedIdentifier tableName){ 91 for (int i = 0; i < tables.size(); i++) { 92 if(tableName.equals(((TableCopy)tables.get(i)).tablename)) 93 return (TableCopy)tables.get(i); 94 } 95 TableCopy tb = new TableCopy(tableName); 96 return tb; 97 } 98 99 100 public TableInformation getTableInformation(TableCopy tableCopy) throws DException{ 101 _DDSConstraintOperations ddsOperations = dataDictionary.getDDSConstraintsOperation(); 102 int columnCount = dataDictionary.getColumnCharacteristics(tableCopy.tablename,true).getColumnCount(); 103 _PrimaryAndUniqueConstraintCharacteristics puc = ddsOperations.getPrimaryAndUniqueConstraintCharacteristics(tableCopy.tablename,false); 104 _ColumnCharacteristics cc = dataDictionary.getColumnCharacteristics( tableCopy.tablename , true ); 105 TableInformation tableInf = new TableInformation(tableCopy,dataDictionary.getColumnCharacteristics(tableCopy.tablename,true)); 106 tableInf.setColumnTypes(); 107 tableInf.setPrimaryColumns( cc.getPrimaryConditionColumns() ); 108 _UniqueConstraint[] uniqueConstraints = puc.getUniqueConstraints(); 109 if(uniqueConstraints!=null) 110 tableInf.setUniqueColumns(getUniqueColumns(uniqueConstraints,columnCount)); 111 return tableInf; 112 } 113 114 public void checkForOffHierarchyParents(TableCopy tableCopy1 ,_ReferentialConstraint[] referencingConstraints)throws DException{ 115 if(tableCopy1.parents.size()!=referencingConstraints.length) 116 for (int i = 0; i < referencingConstraints.length; i++) { 117 TableCopy parent = getTableCopyForOffHierarchy(referencingConstraints[i].getReferencedTable()); 118 if( parent.tableInf == null ) 119 parent.setTableInfo(getTableInformation(parent)); 120 tableCopy1.addParent(parent); 121 } 122 } 123 124 public int[] getReferencedColumns(_ReferentialConstraint[] referencedConstraints,int columnCount ) throws DException{ 125 boolean[] referencedC = new boolean[columnCount]; 126 int count = 0; 127 for (int i = 0; i < referencedConstraints.length; i++) { 128 int[] referencedCol = referencedConstraints[i].getReferencedColumns(); 129 for (int j = 0; j < referencedCol.length; j++) { 130 if(!referencedC[referencedCol[j]]){ 131 referencedC[referencedCol[j]] = true; 132 count++; 133 } 134 } 135 } 136 int[] referencedcolumns = new int[count]; 137 int j =0; 138 for (int i = 0; i < referencedC.length && j < count; i++) 139 if(referencedC[i]) 140 referencedcolumns[j++] =i ; 141 return referencedcolumns; 142 } 143 144 public int[] getReferencingColumns(_ReferentialConstraint[] referencingConstraints,int columnCount ) throws DException{ 145 boolean[] referencingC = new boolean[columnCount]; 146 int count = 0; 147 for (int i = 0; i < referencingConstraints.length; i++) { 148 int[] referencingCol = referencingConstraints[i].getReferencingColumns(); 149 for (int j = 0; j < referencingCol.length; j++) { 150 if(!referencingC[referencingCol[j]]){ 151 referencingC[referencingCol[j]] = true; 152 count++; 153 } 154 } 155 } 156 int[] referencingcolumns = new int[count]; 157 int j =0; 158 for (int i = 0; i < referencingC.length && j < count; i++) 159 if(referencingC[i]) 160 referencingcolumns[j++] =i ; 161 return referencingcolumns; 162 } 163 164 public int[] getUniqueColumns(_UniqueConstraint[] uniqueConstraints,int columnCount ) throws DException{ 165 boolean[] uniqueC = new boolean[columnCount]; 166 int count = 0; 167 for (int i = 0; i < uniqueConstraints.length; i++) { 168 int[] uniqueCol = uniqueConstraints[i].getColumns(); 169 for (int j = 0; j < uniqueCol.length; j++) { 170 if(!uniqueC[uniqueCol[j]]){ 171 uniqueC[uniqueCol[j]] = true; 172 count++; 173 } 174 } 175 } 176 int[] uniquecolumns = new int[count]; 177 int j =0; 178 for (int i = 0; i < uniqueC.length && j < count; i++) 179 if(uniqueC[i]) 180 uniquecolumns[j++] =i ; 181 return uniquecolumns; 182 } 183 184 private boolean copyToMake( String tName ) { 185 for (int i = 0; i < tablesToBeIncluded.length; i++) 186 if( tName.equalsIgnoreCase(tablesToBeIncluded[i] )) 187 return true; 188 return false; 189 } 190 } 191 | Popular Tags |