1 package com.daffodilwoods.daffodildb.server.sql99.ddl.revoke; 2 3 7 import java.util.*; 8 import com.daffodilwoods.daffodildb.server.serversystem.*; 9 import com.daffodilwoods.daffodildb.server.sql99.ddl.descriptors.*; 10 import com.daffodilwoods.daffodildb.server.sql99.dql.iterator.*; 11 import com.daffodilwoods.database.resource.*; 12 13 public class PrivilegeDependencyGraph { 14 HashMap graph; 15 private ArrayList unfinishedList; 16 CalculateDD calculateDD; 17 18 public PrivilegeDependencyGraph(_ServerSession serverSession) throws DException { 19 graph = new HashMap(); 20 unfinishedList = new ArrayList(); 21 calculateDD = new CalculateDD(serverSession); 22 } 23 24 public void addVertex(PrivilegeDescriptor pd) { 25 if (notInGraph(pd)) { 26 graph.put(pd, null); 27 } else { 28 doOtherThings(); 29 } 30 } 31 32 public void addVertex(PrivilegeDescriptor pd, PDCharacterstics characterstic) { 33 if (notInGraph(pd)) { 34 graph.put(pd, characterstic); 35 } else { 36 doOtherThings(); 37 } 38 } 39 40 public void updateVertex(PrivilegeDescriptor pd, PDCharacterstics characterstic) { 41 if (!graph.containsKey(pd)) { 42 graph.put(pd, characterstic); 43 return; 44 } 45 } 46 47 private boolean notInGraph(PrivilegeDescriptor pd) { 48 return!graph.containsKey(pd); 49 } 50 51 public void initializeGraph(_ServerSession currentSession, ArrayList identifiedList, boolean withHierarchyOption) throws DException { 52 unfinishedList.addAll(identifiedList); 53 ArrayList roleList = calculateDD.getRoleNames(currentSession); 54 for (int i = 0; i < unfinishedList.size(); i++) { 55 PrivilegeDescriptor pd = (PrivilegeDescriptor) unfinishedList.get(i); 56 Object [] obj = calculateDD.getDependentAndAncestor(currentSession, pd, roleList); 57 58 HashMap dependents = (HashMap) obj[0]; 59 HashMap ancestors = (HashMap) obj[1]; 60 int pdType = obj[2] == null ? 0 : ( (Integer ) obj[2]).intValue(); 61 62 if (ancestors != null) { 63 Iterator iter = ancestors.keySet().iterator(); 64 while (iter.hasNext()) { 65 PrivilegeDescriptor temppd = (PrivilegeDescriptor) iter.next(); 66 if (!graph.containsKey(temppd)) { 67 unfinishedList.add(temppd); 68 } 69 } 70 } 71 72 if (dependents != null) { 73 Iterator iter = dependents.keySet().iterator(); 74 while (iter.hasNext()) { 75 PrivilegeDescriptor temppd = (PrivilegeDescriptor) iter.next(); 76 if (!graph.containsKey(temppd)) { 77 unfinishedList.add(temppd); 78 } 79 } 80 } 81 PDCharacterstics characterstic = new PDCharacterstics(); 82 characterstic.setPDType(pdType); 83 characterstic.setAncestor(ancestors); 84 characterstic.setDependents(dependents); 85 addVertex(pd, characterstic); 86 } 87 markIdentifiedPrivilegeDescriptor(identifiedList); 88 markModifiedPrivilegeDescriptor(currentSession); 89 markIndependentNode(); 90 markAbandonedPrivilegeDescriptor(currentSession, withHierarchyOption); 91 } 92 93 private void markIdentifiedPrivilegeDescriptor(ArrayList identifiedList) { 94 for (int i = 0, size = identifiedList.size(); i < size; i++) { 95 PrivilegeDescriptor pd = (PrivilegeDescriptor) identifiedList.get(i); 96 PDCharacterstics characterstic = (PDCharacterstics) graph.get(pd); 97 characterstic.setStatus(_PDCharacterstics.indentified); 98 PDCharacterstics characterstic1 = (PDCharacterstics) graph.get(pd); 99 } 100 } 101 102 private void markModifiedPrivilegeDescriptor(_ServerSession serverSession) throws DException { 103 if (graph != null) { 104 Iterator iter = graph.entrySet().iterator(); 105 while (iter.hasNext()) { 106 Map.Entry entry = (Map.Entry) iter.next(); 107 PrivilegeDescriptor pd = (PrivilegeDescriptor) entry.getKey(); 108 PDCharacterstics characterstic = (PDCharacterstics) entry.getValue(); 109 if (isModifiedPrivilegeDescriptorPointAnB(serverSession, pd, characterstic.getAncestor())) { 110 } 111 } 112 } 113 } 114 115 private boolean isModifiedPrivilegeDescriptorPointAnB(_ServerSession serverSession, PrivilegeDescriptor pd, HashMap anc) throws DException { 116 if (pd.is_grantable.equalsIgnoreCase(SqlSchemaConstants.YES)) { 117 if (anc == null) { 118 return false; 119 } else { 120 Iterator iter = anc.keySet().iterator(); 121 while (iter.hasNext()) { 122 PrivilegeDescriptor temppd = (PrivilegeDescriptor) iter.next(); 123 PDCharacterstics characterstic = (PDCharacterstics) graph.get(temppd); 124 if (characterstic.getStatus() == _PDCharacterstics.indentified) { 125 return isModifiedPrivilegeDescriptorPointC(serverSession, pd); 126 } else 127 if (isModifiedPrivilegeDescriptorPointAnB(serverSession, temppd, characterstic.getAncestor())) { 128 return isModifiedPrivilegeDescriptorPointC(serverSession, pd); 129 } 130 } 131 } 132 } 133 return false; 134 } 135 136 private boolean isModifiedPrivilegeDescriptorPointC(_ServerSession serverSession, PrivilegeDescriptor pd) throws DException { 137 if (pd instanceof ColumnPrivilegeDescriptor) { 138 ColumnPrivilegeDescriptor temppd = (ColumnPrivilegeDescriptor) pd; 139 if (temppd.privilege_type.equalsIgnoreCase("SELECT") || temppd.privilege_type.equalsIgnoreCase("REFERENCES")) { 140 _Iterator iter = SqlSchemaConstants.getIterator(serverSession, QueryGetter.checkViews, new Object [] {pd.object_name, pd.object_schema, pd.object_catalog}); 141 if (iter.first()) { 142 return false; 143 } 144 } 145 } 146 HashMap dep = getDependents(pd); 147 return isModifiedPrivilegeDescriptorPointD(serverSession, pd); 148 } 149 150 private boolean isModifiedPrivilegeDescriptorPointD(_ServerSession serverSession, PrivilegeDescriptor pd) throws DException { 151 if (pd.getGrantor().equalsIgnoreCase("_SYSTEM") && pd.is_grantable.equalsIgnoreCase("YES")) { 152 return true; 153 } else { 154 return false; 155 } 156 } 157 158 private void markIndependentNode() throws DException { 159 if (graph != null) { 160 Iterator iter = graph.entrySet().iterator(); 161 while (iter.hasNext()) { 162 Map.Entry entry = (Map.Entry) iter.next(); 163 PrivilegeDescriptor pd = (PrivilegeDescriptor) entry.getKey(); 164 PDCharacterstics charac = (PDCharacterstics) entry.getValue(); 165 if (charac.getAncestor() == null) { 166 charac.setIndependent(true); 167 } 168 } 169 } 170 } 171 172 private void markAbandonedPrivilegeDescriptor(_ServerSession serverSession, boolean withHierarchyOption) throws DException { 173 if (graph != null) { 174 Iterator iter = graph.entrySet().iterator(); 175 while (iter.hasNext()) { 176 Map.Entry et = (Map.Entry) iter.next(); 177 PrivilegeDescriptor pd = (PrivilegeDescriptor) et.getKey(); PDCharacterstics charac = (PDCharacterstics) et.getValue(); if (!withHierarchyOption || (charac.getPDType() == _PDCharacterstics.tableprivilegedescriptor && ( (TablePrivilegesDescriptor) pd).with_hierarchy.equalsIgnoreCase("YES"))) { 180 HashMap pathElement = new HashMap(); 181 if (!charac.isIndependent() && charac.getStatus() != _PDCharacterstics.indentified && charac.getStatus() != _PDCharacterstics.modified && isPathFromIndependentConidtionSatisfying(pd, false, pathElement)) { 182 charac.setStatus(_PDCharacterstics.abandoned); 183 } 184 } 185 186 199 } 200 } 201 } 202 203 public boolean isPathFromIndependentConidtionSatisfying(PrivilegeDescriptor pd, boolean condition, HashMap pathElement) throws DException { 204 pathElement.put(pd, null); 205 HashMap anc = getAncestors(pd); 206 boolean flag = false; 207 if (anc != null) { 208 Iterator iter = anc.keySet().iterator(); 209 while (iter.hasNext()) { 210 PrivilegeDescriptor temppd = (PrivilegeDescriptor) iter.next(); 211 if (pathElement.containsKey(temppd)) { 212 return false; 213 } 214 PDCharacterstics charac = (PDCharacterstics) graph.get(temppd); 215 if (charac.getStatus() == _PDCharacterstics.indentified || charac.getStatus() == _PDCharacterstics.modified || isUnsupportedArc(pd, temppd)) { 216 if (charac.isIndependent()) { 217 return true; 218 } 219 return isPathFromIndependentConidtionSatisfying(temppd, true, pathElement); 220 } else { 221 if (charac.isIndependent() && !condition) { 222 return false; 223 } 224 return isPathFromIndependentConidtionSatisfying(temppd, false, pathElement); 225 } 226 } 227 } 228 return true; 229 } 230 231 private boolean isUnsupportedArc(PrivilegeDescriptor firstpd, PrivilegeDescriptor secondpd) { 232 return false; 233 } 234 235 private void doOtherThings() { 236 } 237 238 private ArrayList getRoleNames(_ServerSession serverSession) throws DException { 239 return calculateDD.getRoleNames(serverSession); 240 } 241 242 public HashMap getDependents(PrivilegeDescriptor pd) throws DException { 243 if (graph == null) { 244 return null; 245 } 246 PDCharacterstics temp = (PDCharacterstics) graph.get(pd); 247 if (temp == null) { 248 return null; 249 } 250 return temp.getDependents(); 251 } 252 253 public HashMap getAncestors(PrivilegeDescriptor pd) throws DException { 254 if (graph == null) { 255 return null; 256 } 257 PDCharacterstics temp = (PDCharacterstics) graph.get(pd); 258 if (temp == null) { 259 return null; 260 } 261 return temp.getAncestor(); 262 } 263 264 public int getStatus(PrivilegeDescriptor pd) throws DException { 265 PDCharacterstics temp = (PDCharacterstics) graph.get(pd); 266 if (temp == null) { 267 throw new DException("DSE5047", null); 268 } 269 return temp.getStatus(); 270 } 271 272 273 public ArrayList getAllIdentifiedPrivilegeDescriptors() throws DException { 274 if (graph != null) { 275 ArrayList temp = new ArrayList(); 276 Iterator iter = graph.entrySet().iterator(); 277 while (iter.hasNext()) { 278 Map.Entry entry = (Map.Entry) iter.next(); 279 PrivilegeDescriptor pd = (PrivilegeDescriptor) entry.getKey(); 280 PDCharacterstics characterstic = (PDCharacterstics) entry.getValue(); 281 if (getStatus(characterstic) == _PDCharacterstics.indentified) { 282 temp.add(pd); 283 } 284 } 285 return temp.size() == 0 ? null : temp; 286 } 287 return null; 288 } 289 290 public ArrayList getAllModifiedPrivilegeDescriptors() throws DException { 291 if (graph != null) { 292 ArrayList temp = new ArrayList(); 293 Iterator iter = graph.entrySet().iterator(); 294 while (iter.hasNext()) { 295 Map.Entry entry = (Map.Entry) iter.next(); 296 PrivilegeDescriptor pd = (PrivilegeDescriptor) entry.getKey(); 297 PDCharacterstics characterstic = (PDCharacterstics) entry.getValue(); 298 if (getStatus(characterstic) == _PDCharacterstics.modified) { 299 temp.add(pd); 300 } 301 } 302 return temp.size() == 0 ? null : temp; 303 } 304 return null; 305 } 306 307 public ArrayList getAllAbandonedPrivilegeDescriptors() throws DException { 308 if (graph != null) { 309 ArrayList temp = new ArrayList(); 310 Iterator iter = graph.entrySet().iterator(); 311 while (iter.hasNext()) { 312 Map.Entry entry = (Map.Entry) iter.next(); 313 PrivilegeDescriptor pd = (PrivilegeDescriptor) entry.getKey(); 314 PDCharacterstics characterstic = (PDCharacterstics) entry.getValue(); 315 if (getStatus(characterstic) == _PDCharacterstics.abandoned) { 316 temp.add(pd); 317 } 318 } 319 return temp.size() == 0 ? null : temp; 320 } 321 return null; 322 } 323 324 public void printGraph() throws DException { 325 if (graph != null) { 326 Iterator iter = graph.keySet().iterator(); 327 int i = 0; 328 while (iter.hasNext()) { 329 PrivilegeDescriptor pd = (PrivilegeDescriptor) iter.next(); 330 print_privilege_descriptor(pd, "[" + i + "] Entry "); 331 PDCharacterstics ch = (PDCharacterstics) graph.get(pd); 332 HashMap anc = ch.getAncestor(); 333 if (anc == null) { 334 ; } else { 336 Iterator t = anc.keySet().iterator(); 337 int j = 0; 338 while (t.hasNext()) { 339 PrivilegeDescriptor temppd = (PrivilegeDescriptor) t.next(); 340 print_privilege_descriptor(temppd, "\tAncestor [" + j + "] "); 341 j++; 342 } 343 } 344 HashMap dep = ch.getDependents(); 345 if (dep == null) { 346 ; } else { 348 Iterator t = dep.keySet().iterator(); 349 int j = 0; 350 while (t.hasNext()) { 351 PrivilegeDescriptor temppd = (PrivilegeDescriptor) t.next(); 352 print_privilege_descriptor(temppd, "\tDependent [" + j + "] "); 353 j++; 354 } 355 } 356 i++; 357 } 358 } else { 359 ; } 361 } 362 363 public void print_modified_privilege_descriptor() throws DException { 364 int i = 0; 365 if (graph != null) { 366 Iterator iter = graph.entrySet().iterator(); 367 while (iter.hasNext()) { 368 Map.Entry entry = (Map.Entry) iter.next(); 369 PrivilegeDescriptor pd = (PrivilegeDescriptor) entry.getKey(); 370 PDCharacterstics characterstic = (PDCharacterstics) entry.getValue(); 371 if (getStatus(characterstic) == _PDCharacterstics.modified) { 372 print_privilege_descriptor(pd, "\t[" + ++i + "] "); 373 } 374 } 375 } 376 if (i == 0) { 377 ; } 379 } 380 381 public void print_identified_privilege_descriptor() throws DException { 382 int i = 0; 383 if (graph != null) { 384 Iterator iter = graph.entrySet().iterator(); 385 while (iter.hasNext()) { 386 Map.Entry entry = (Map.Entry) iter.next(); 387 PrivilegeDescriptor pd = (PrivilegeDescriptor) entry.getKey(); 388 PDCharacterstics characterstic = (PDCharacterstics) entry.getValue(); 389 if (getStatus(characterstic) == _PDCharacterstics.indentified) { 390 print_privilege_descriptor(pd, "\t[" + ++i + "] "); 391 } 392 } 393 } 394 if (i == 0) { 395 ; } 397 } 398 399 public void print_abandoned_privilege_descriptor() throws DException { 400 int i = 0; 401 if (graph != null) { 402 Iterator iter = graph.entrySet().iterator(); 403 while (iter.hasNext()) { 404 Map.Entry entry = (Map.Entry) iter.next(); 405 PrivilegeDescriptor pd = (PrivilegeDescriptor) entry.getKey(); 406 PDCharacterstics characterstic = (PDCharacterstics) entry.getValue(); 407 if (getStatus(characterstic) == _PDCharacterstics.abandoned) { 408 print_privilege_descriptor(pd, "\t[" + ++i + "] "); 409 } 410 } 411 } 412 if (i == 0) { 413 ; } 415 } 416 417 public void print_independent_nodes() throws DException { 418 int i = 0; 419 if (graph != null) { 420 Iterator iter = graph.entrySet().iterator(); 421 while (iter.hasNext()) { 422 Map.Entry entry = (Map.Entry) iter.next(); 423 PrivilegeDescriptor pd = (PrivilegeDescriptor) entry.getKey(); 424 PDCharacterstics characterstic = (PDCharacterstics) entry.getValue(); 425 if (isIndenpendentNode(characterstic)) { 426 print_privilege_descriptor(pd, "\t[" + ++i + "] "); 427 } 428 } 429 } 430 if (i == 0) { 431 ; } 433 } 434 435 public void print_privilege_descriptor(PrivilegeDescriptor pd, String extra) { 436 if (pd instanceof TablePrivilegesDescriptor) { 437 ; } else if (pd instanceof ColumnPrivilegeDescriptor) { 439 ; } else if (pd instanceof UsagePrivilegesDescriptor) { 441 ; } else if (pd instanceof RoutinePrivilegeDescriptor) { 443 ; } 445 } 446 447 public boolean isIndenpendentNode(PDCharacterstics temp) throws DException { 448 if (temp == null) { 449 throw new DException("DSE5047", null); 450 } 451 return temp.isIndependent(); 452 } 453 454 public int getStatus(PDCharacterstics temp) throws DException { 455 if (temp == null) { 456 throw new DException("DSE5047", null); 457 } 458 return temp.getStatus(); 459 } 460 461 public int getPDType(PDCharacterstics temp) throws DException { 462 if (temp == null) { 463 throw new DException("DSE5047", null); 464 } 465 return temp.getPDType(); 466 } 467 } 468 | Popular Tags |