Revision 1413
Added by Jing Tao almost 22 years ago
src/edu/ucsb/nceas/metacat/EmlSAXHandler.java | ||
---|---|---|
60 | 60 |
private AccessRule accessRule = null; |
61 | 61 |
private Hashtable topLevelAccessControlMap = new Hashtable(); |
62 | 62 |
private Hashtable additionalAccessControlMap = new Hashtable(); |
63 |
|
|
64 |
|
|
63 |
private Vector describesId = new Vector(); // store the ids in |
|
64 |
//additionalmetadata/describes |
|
65 |
|
|
65 | 66 |
// Constant |
66 | 67 |
private static final String DESCRIBES = "describes"; |
67 | 68 |
private static final String ADDITIONALMETADATA = "additionalMetadata"; |
68 | 69 |
private static final String ORDER = "order"; |
69 | 70 |
private static final String ID ="id"; |
70 |
|
|
71 |
/** Construct an instance of the handler class |
|
71 |
private static final String REFERENCES = "references"; |
|
72 |
|
|
73 |
/** Construct an instance of the handler class |
|
72 | 74 |
* |
73 | 75 |
* @param conn the JDBC connection to which information is written |
74 | 76 |
* @param action - "INSERT" or "UPDATE" |
... | ... | |
285 | 287 |
} |
286 | 288 |
|
287 | 289 |
|
288 |
// Set up a access rule |
|
290 |
// Set up a access rule for allow
|
|
289 | 291 |
if (parentNode.getTagName() != null && |
290 | 292 |
(parentNode.getTagName()).equals(ACCESS) && localName.equals(ALLOW)) |
291 | 293 |
{ |
... | ... | |
296 | 298 |
accessRule.setPermissionType(ALLOW); |
297 | 299 |
|
298 | 300 |
} |
299 |
|
|
300 |
// set up an access rule |
|
301 |
if (parentNode.getTagName() != null |
|
301 |
// set up an access rule for den |
|
302 |
else if (parentNode.getTagName() != null |
|
302 | 303 |
&& (parentNode.getTagName()).equals(ACCESS) && localName.equals(DENY)) |
303 | 304 |
{ |
304 | 305 |
accessRule = new AccessRule(); |
305 | 306 |
//set permission type "allow" |
306 | 307 |
accessRule.setPermissionType(DENY); |
307 | 308 |
} |
308 |
|
|
309 |
|
|
309 |
|
|
310 | 310 |
// Add the node to the stack, so that any text data can be |
311 | 311 |
// added as it is encountered |
312 | 312 |
nodeStack.push(currentNode); |
... | ... | |
365 | 365 |
} |
366 | 366 |
accessRule.setPermission(permission); |
367 | 367 |
} |
368 |
|
|
368 |
// put additionalmetadata/describes into vector |
|
369 |
else if (currentTag.equals(DESCRIBES)) |
|
370 |
{ |
|
371 |
data = (textBuffer.toString()).trim(); |
|
372 |
describesId.add(data); |
|
373 |
} |
|
374 |
else if (currentTag.equals(REFERENCES) && processTopLeverAccess) |
|
375 |
{ |
|
376 |
// get reference |
|
377 |
data = (textBuffer.toString()).trim(); |
|
378 |
// insert docid and reference id into toplevlerAccess |
|
379 |
topLevelAccessControlMap.put(docid, data); |
|
380 |
|
|
381 |
} |
|
382 |
else if (currentTag.equals(REFERENCES) && processAdditionalAccess) |
|
383 |
{ |
|
384 |
// get reference |
|
385 |
data = (textBuffer.toString()).trim(); |
|
386 |
additionalAccessControlMap.put(docid, data); |
|
387 |
} |
|
369 | 388 |
// write text to db |
370 | 389 |
MetaCatUtil.debugMessage("Write text into DB in End Element", 50); |
371 | 390 |
writeTextForDBSAXNode(textBuffer, currentNode); |
... | ... | |
402 | 421 |
else if (processAdditionalAccess) |
403 | 422 |
{ |
404 | 423 |
// for additional control |
424 |
// put everything in describes value and access object into hash |
|
425 |
for ( int i=0; i<describesId.size(); i++) |
|
426 |
{ |
|
427 |
String subId = (String)describesId.elementAt(i); |
|
428 |
if (subId != null) |
|
429 |
{ |
|
430 |
additionalAccessControlMap.put(subId, newAccessObject); |
|
431 |
} |
|
432 |
} |
|
405 | 433 |
} |
406 | 434 |
|
407 | 435 |
//reset access section object |
408 | 436 |
accessObject = null; |
409 | 437 |
} |
438 |
else if ((currentNode.getTagName()).equals(ADDITIONALMETADATA)) |
|
439 |
{ |
|
440 |
//reset describesId |
|
441 |
describesId = new Vector(); |
|
442 |
} |
|
410 | 443 |
} |
411 | 444 |
|
412 | 445 |
/** SAX Handler that receives notification of end of the document */ |
... | ... | |
429 | 462 |
e.getMessage()); |
430 | 463 |
} |
431 | 464 |
} |
432 |
/* The method to write access rule into db. */ |
|
465 |
|
|
466 |
/* The method to write all access rule intodb */ |
|
433 | 467 |
private void writeAccessRuleToDB() throws SAXException |
434 | 468 |
{ |
469 |
//write top leve access rule |
|
470 |
writeTopLevelAccessRuleToDB(); |
|
471 |
//write additional access rule |
|
472 |
// writeAddtionalAccessRuleToDB(); |
|
473 |
}//writeAccessRuleToDB |
|
474 |
|
|
475 |
/* The method to write top level access rule into db. */ |
|
476 |
private void writeTopLevelAccessRuleToDB() throws SAXException |
|
477 |
{ |
|
435 | 478 |
|
436 | 479 |
// for top document level |
437 |
AccessSection accessSection = (AccessSection) |
|
438 |
topLevelAccessControlMap.get(docid); |
|
439 |
// if accessSection is null stop it |
|
440 |
if( accessSection == null) |
|
480 |
Object accessSection = topLevelAccessControlMap.get(docid); |
|
481 |
// if accessSection is not null and isnot reference |
|
482 |
if( accessSection != null && (accessSection instanceof AccessSection) ) |
|
441 | 483 |
{ |
442 |
return; |
|
484 |
String subSectionId = null; |
|
485 |
boolean top = true; |
|
486 |
AccessSection accessObj = (AccessSection)accessSection; |
|
487 |
writeGivenAccessRuleIntoDB(accessObj, top, subSectionId); |
|
488 |
}//if |
|
489 |
else |
|
490 |
{ |
|
491 |
// this is a reference |
|
443 | 492 |
} |
444 |
String permOrder = accessSection.getPermissionOrder(); |
|
445 |
String sql = "INSERT INTO xml_access (docid, principal_name, permission, "+ |
|
446 |
"perm_type, perm_order, accessfileid) VALUES " + |
|
447 |
" (?, ?, ?, ?, ?, ?)"; |
|
448 |
PreparedStatement pstmt = null; |
|
493 |
|
|
494 |
}//writeTopLevelAccessRuletoDB |
|
449 | 495 |
|
450 |
try |
|
496 |
/* The method to write addtional access rule into db. */ |
|
497 |
private void writeAddtionalAccessRuleToDB() throws SAXException |
|
498 |
{ |
|
499 |
|
|
500 |
PreparedStatement pstmt = null; |
|
501 |
// additional access rule |
|
502 |
Enumeration en = additionalAccessControlMap.keys(); |
|
503 |
boolean topLevel =false; |
|
504 |
|
|
505 |
while(en.hasMoreElements()) |
|
451 | 506 |
{ |
507 |
try |
|
508 |
{ |
|
509 |
// Get subsection id |
|
510 |
String subSectionId = (String)en.nextElement(); |
|
511 |
|
|
512 |
if (subSectionId == null) |
|
513 |
{ |
|
514 |
// if id is null ignore the following lines |
|
515 |
continue; |
|
516 |
} |
|
517 |
// Get AccessSection Object |
|
518 |
Object accessSection = additionalAccessControlMap.get(subSectionId); |
|
519 |
if (accessSection == null) |
|
520 |
{ |
|
521 |
// if accesssection is null ignore the following lines |
|
522 |
continue; |
|
523 |
} |
|
524 |
else if ( accessSection instanceof AccessSection ) |
|
525 |
{ |
|
526 |
// it is not reference |
|
527 |
AccessSection accessObj = (AccessSection)accessSection; |
|
528 |
writeGivenAccessRuleIntoDB(accessObj, topLevel, subSectionId); |
|
529 |
} |
|
530 |
else |
|
531 |
{ |
|
532 |
//this is reference |
|
533 |
} |
|
534 |
}//try |
|
535 |
catch (Exception e) |
|
536 |
{ |
|
537 |
MetaCatUtil.debugMessage("error in EmlSAXHandler.writeAddtionalAccess" |
|
538 |
+ ": "+e.getMessage(), 30); |
|
539 |
} |
|
540 |
}//while |
|
541 |
}//writeAccessRuletoDB |
|
542 |
|
|
543 |
/* Write a gaven access rule into db*/ |
|
544 |
private void writeGivenAccessRuleIntoDB(AccessSection accessSection, |
|
545 |
boolean topLevel, String subSectionId) |
|
546 |
throws SAXException |
|
547 |
{ |
|
548 |
if (accessSection == null) |
|
549 |
{ |
|
550 |
throw new SAXException("The access object is null"); |
|
551 |
} |
|
452 | 552 |
|
453 |
pstmt = connection.prepareStatement(sql); |
|
454 |
// Increase DBConnection usage count |
|
455 |
connection.increaseUsageCount(1); |
|
456 |
// Bind the values to the query |
|
457 |
pstmt.setString(1, docid); |
|
458 |
pstmt.setString(6, docid); |
|
459 |
pstmt.setString(5, permOrder); |
|
460 |
|
|
461 |
Vector accessRules = accessSection.getAccessRules(); |
|
462 |
// go through every rule |
|
463 |
for (int i=0; i<accessRules.size(); i++) |
|
553 |
String permOrder = accessSection.getPermissionOrder(); |
|
554 |
String sql = null; |
|
555 |
PreparedStatement pstmt = null; |
|
556 |
if (topLevel) |
|
464 | 557 |
{ |
465 |
AccessRule rule = (AccessRule)accessRules.elementAt(i); |
|
466 |
String permType = rule.getPermissionType(); |
|
467 |
int permission = rule.getPermission(); |
|
468 |
pstmt.setInt(3, permission); |
|
469 |
pstmt.setString(4, permType); |
|
470 |
// go through every principle in rule |
|
471 |
Vector nameVector = rule.getPrincipal(); |
|
472 |
for ( int j = 0; j < nameVector.size(); j++ ) |
|
473 |
{ |
|
474 |
String prName = (String)nameVector.elementAt(j); |
|
475 |
pstmt.setString(2, prName); |
|
476 |
pstmt.execute(); |
|
477 |
}//for |
|
478 |
}//for |
|
479 |
pstmt.close(); |
|
480 |
}//try |
|
481 |
catch (SQLException e) |
|
482 |
{ |
|
483 |
throw new |
|
484 |
SAXException("EMLSAXHandler.writeAccessRuletoDB(): " + e.getMessage()); |
|
485 |
}//catch |
|
486 |
finally |
|
487 |
{ |
|
488 |
try |
|
558 |
sql = "INSERT INTO xml_access (docid, principal_name, permission, "+ |
|
559 |
"perm_type, perm_order, accessfileid) VALUES " + |
|
560 |
" (?, ?, ?, ?, ?, ?)"; |
|
561 |
} |
|
562 |
else |
|
489 | 563 |
{ |
490 |
pstmt.close(); |
|
564 |
sql ="INSERT INTO xml_access (docid, principal_name, "+ |
|
565 |
"permission, perm_type, perm_order, accessfileid, sectionid) VALUES" |
|
566 |
+" (?, ?, ?, ?, ?, ?, ?)"; |
|
491 | 567 |
} |
492 |
catch(SQLException ee)
|
|
568 |
try
|
|
493 | 569 |
{ |
570 |
|
|
571 |
pstmt = connection.prepareStatement(sql); |
|
572 |
// Increase DBConnection usage count |
|
573 |
connection.increaseUsageCount(1); |
|
574 |
// Bind the values to the query |
|
575 |
pstmt.setString(1, docid); |
|
576 |
MetaCatUtil.debugMessage("Docid in accesstable: "+ docid); |
|
577 |
pstmt.setString(6, docid); |
|
578 |
MetaCatUtil.debugMessage("Accessfileid in accesstable: "+ docid); |
|
579 |
pstmt.setString(5, permOrder); |
|
580 |
MetaCatUtil.debugMessage("PermOder in accesstable: "+ permOrder); |
|
581 |
// if it is not top level, set subsection id |
|
582 |
if (!topLevel) |
|
583 |
{ |
|
584 |
pstmt.setString(7, subSectionId); |
|
585 |
MetaCatUtil.debugMessage("SubSectionId in accesstable: "+ |
|
586 |
subSectionId); |
|
587 |
} |
|
588 |
|
|
589 |
Vector accessRules = accessSection.getAccessRules(); |
|
590 |
// go through every rule |
|
591 |
for (int i=0; i<accessRules.size(); i++) |
|
592 |
{ |
|
593 |
AccessRule rule = (AccessRule)accessRules.elementAt(i); |
|
594 |
String permType = rule.getPermissionType(); |
|
595 |
int permission = rule.getPermission(); |
|
596 |
pstmt.setInt(3, permission); |
|
597 |
MetaCatUtil.debugMessage("permission in accesstable: "+ permission); |
|
598 |
pstmt.setString(4, permType); |
|
599 |
MetaCatUtil.debugMessage("Permtype in accesstable: "+ permType); |
|
600 |
// go through every principle in rule |
|
601 |
Vector nameVector = rule.getPrincipal(); |
|
602 |
for ( int j = 0; j < nameVector.size(); j++ ) |
|
603 |
{ |
|
604 |
String prName = (String)nameVector.elementAt(j); |
|
605 |
pstmt.setString(2, prName); |
|
606 |
MetaCatUtil.debugMessage("Principal in accesstable: "+prName); |
|
607 |
pstmt.execute(); |
|
608 |
}//for |
|
609 |
}//for |
|
610 |
pstmt.close(); |
|
611 |
}//try |
|
612 |
catch (SQLException e) |
|
613 |
{ |
|
494 | 614 |
throw new |
495 |
SAXException("EMLSAXHandler.writeAccessRuletoDB(): " + ee.getMessage()); |
|
496 |
} |
|
497 |
}//finally |
|
498 |
}//writeAccessRuletoDB |
|
615 |
SAXException("EMLSAXHandler.writeAccessRuletoDB(): " + e.getMessage()); |
|
616 |
}//catch |
|
617 |
finally |
|
618 |
{ |
|
619 |
try |
|
620 |
{ |
|
621 |
pstmt.close(); |
|
622 |
} |
|
623 |
catch(SQLException ee) |
|
624 |
{ |
|
625 |
throw new |
|
626 |
SAXException("EMLSAXHandler.writeAccessRuletoDB(): " + |
|
627 |
ee.getMessage()); |
|
628 |
} |
|
629 |
}//finally |
|
499 | 630 |
|
631 |
} |
|
500 | 632 |
|
501 | 633 |
} |
Also available in: Unified diff
Add code to handle additional access data and reference.