Project

General

Profile

« Previous | Next » 

Revision 6016

use access control dao for setting access in EML parser. send additional xml_access info in replication request

View differences:

Eml210SAXHandler.java
46 46
import org.xml.sax.Attributes;
47 47
import org.xml.sax.SAXException;
48 48

  
49
import edu.ucsb.nceas.metacat.accesscontrol.AccessControlForSingleFile;
49 50
import edu.ucsb.nceas.metacat.accesscontrol.AccessControlInterface;
50 51
import edu.ucsb.nceas.metacat.accesscontrol.AccessRule;
51 52
import edu.ucsb.nceas.metacat.accesscontrol.AccessSection;
53
import edu.ucsb.nceas.metacat.accesscontrol.XMLAccessDAO;
52 54
import edu.ucsb.nceas.metacat.database.DBConnection;
53 55
import edu.ucsb.nceas.metacat.database.DBConnectionPool;
54 56
import edu.ucsb.nceas.metacat.properties.PropertyService;
......
1425 1427
			throw new SAXException("The access object is null");
1426 1428
		}
1427 1429

  
1430
		String currentDocId = null;
1431
		String accessFileId = docid;
1428 1432
		String permOrder = accessSection.getPermissionOrder();
1429
		String sql = null;
1430
		PreparedStatement pstmt = null;
1431 1433
		if (topLevel) {
1432
			sql = "INSERT INTO xml_access (docid, principal_name, permission, "
1433
					+ "perm_type, perm_order, accessfileid) VALUES "
1434
					+ " (?, ?, ?, ?, ?, ?)";
1434
			currentDocId = docid;
1435 1435
		} else {
1436
			sql = "INSERT INTO xml_access (docid,principal_name, "
1437
					+ "permission, perm_type, perm_order, accessfileid, subtreeid"
1438
					+ ") VALUES" + " (?, ?, ?, ?, ?, ?, ?)";
1436
			currentDocId = accessSection.getDataFileName();
1437
			// for subtree should specify the
1438
			if (subSectionId == null) {
1439
				throw new SAXException("The subsection is null");
1440
			}
1439 1441
		}
1440 1442
		try {
1441 1443

  
1442
			pstmt = connection.prepareStatement(sql);
1443
			// Increase DBConnection usage count
1444
			connection.increaseUsageCount(1);
1445
			// Bind the values to the query
1446
			pstmt.setString(6, docid);
1447
			logMetacat.debug("Accessfileid in accesstable: " + docid);
1448
			pstmt.setString(5, permOrder);
1449
			logMetacat.debug("PermOder in accesstable: " + permOrder);
1450
			// if it is not top level, set subsection id
1451
			if (topLevel) {
1452
				pstmt.setString(1, docid);
1453
				logMetacat.debug("Docid in accesstable: " + docid);
1454
			}
1455
			if (!topLevel) {
1456
				pstmt.setString(1, accessSection.getDataFileName());
1457
				logMetacat.debug("Docid in accesstable: " + inlineDataFileName);
1458

  
1459
				// for subtree should specify the
1460
				if (subSectionId == null) {
1461
					throw new SAXException("The subsection is null");
1462
				}
1463

  
1464
				pstmt.setString(7, subSectionId);
1465
				logMetacat.debug("SubSectionId in accesstable: " + subSectionId);
1466
			}
1467

  
1444
			AccessControlForSingleFile acfsf = new AccessControlForSingleFile(currentDocId);
1468 1445
			Vector<AccessRule> accessRules = accessSection.getAccessRules();
1469 1446
			// go through every rule
1470 1447
			for (int i = 0; i < accessRules.size(); i++) {
1471 1448
				AccessRule rule = accessRules.elementAt(i);
1472 1449
				String permType = rule.getPermissionType();
1473 1450
				int permission = rule.getPermission();
1474
				pstmt.setInt(3, permission);
1475 1451
				logMetacat.debug("permission in accesstable: " + permission);
1476
				pstmt.setString(4, permType);
1477 1452
				logMetacat.debug("Permtype in accesstable: " + permType);
1478 1453
				// go through every principle in rule
1479 1454
				Vector<String> nameVector = rule.getPrincipal();
1480 1455
				for (int j = 0; j < nameVector.size(); j++) {
1481 1456
					String prName = nameVector.elementAt(j);
1482
					pstmt.setString(2, prName);
1483 1457
					logMetacat.debug("Principal in accesstable: " + prName);
1484
					logMetacat.debug("running sql: " + pstmt.toString());
1485
					pstmt.execute();
1458
					XMLAccessDAO dao = new XMLAccessDAO();
1459
					dao.setAccessFileId(accessFileId);
1460
					dao.setDocId(currentDocId);
1461
					dao.setPermission(new Long(permission));
1462
					dao.setPermOrder(permOrder);
1463
					dao.setPermType(permType);
1464
					dao.setPrincipalName(prName);
1465
					dao.setSubTreeId(subSectionId);
1466
					// insert if it does not exist
1467
					if (!acfsf.accessControlExists(dao)) {
1468
						acfsf.insertPermissions(dao);
1469
					}
1470
					
1486 1471
				}// for
1487 1472
			}// for
1488
			pstmt.close();
1489 1473
		}// try
1490
		catch (SQLException e) {
1474
		catch (Exception e) {
1491 1475
			throw new SAXException("EMLSAXHandler.writeAccessRuletoDB(): "
1492 1476
					+ e.getMessage());
1493 1477
		}// catch
1494
		finally {
1495
			try {
1496
				pstmt.close();
1497
			} catch (SQLException ee) {
1498
				throw new SAXException("EMLSAXHandler.writeAccessRuletoDB(): "
1499
						+ ee.getMessage());
1500
			}
1501
		}// finally
1502 1478

  
1503 1479
	}// writeGivenAccessRuleIntoDB
1504 1480

  
1505
	/* Write a gaven access rule into db */
1506
	private void writeAccessRuleForRelatedDataFileIntoDB(AccessSection accessSection,
1507
			String dataId) throws SAXException {
1508
		if (accessSection == null) {
1509
			throw new SAXException("The access object is null");
1510
		}
1511
		// get rid of rev from dataId
1512
		// dataId = MetacatUtil.getDocIdFromString(dataId);
1513
		String permOrder = accessSection.getPermissionOrder();
1514
		String sql = null;
1515
		PreparedStatement pstmt = null;
1516
		sql = "INSERT INTO xml_access (docid, principal_name, permission, "
1517
				+ "perm_type, perm_order, accessfileid) VALUES " + " (?, ?, ?, ?, ?, ?)";
1518

  
1519
		try {
1520

  
1521
			pstmt = connection.prepareStatement(sql);
1522
			// Increase DBConnection usage count
1523
			connection.increaseUsageCount(1);
1524
			// Bind the values to the query
1525
			pstmt.setString(1, dataId);
1526
			logMetacat.debug("Docid in accesstable: " + docid);
1527
			pstmt.setString(6, docid);
1528
			logMetacat.debug("Accessfileid in accesstable: " + docid);
1529
			pstmt.setString(5, permOrder);
1530
			logMetacat.debug("PermOder in accesstable: " + permOrder);
1531
			// if it is not top level, set subsection id
1532

  
1533
			Vector<AccessRule> accessRules = accessSection.getAccessRules();
1534
			// go through every rule
1535
			for (int i = 0; i < accessRules.size(); i++) {
1536
				AccessRule rule = accessRules.elementAt(i);
1537
				String permType = rule.getPermissionType();
1538
				int permission = rule.getPermission();
1539
				pstmt.setInt(3, permission);
1540
				logMetacat.debug("permission in accesstable: " + permission);
1541
				pstmt.setString(4, permType);
1542
				logMetacat.debug("Permtype in accesstable: " + permType);
1543
				// go through every principle in rule
1544
				Vector<String> nameVector = rule.getPrincipal();
1545
				for (int j = 0; j < nameVector.size(); j++) {
1546
					String prName = nameVector.elementAt(j);
1547
					pstmt.setString(2, prName);
1548
					logMetacat.debug("Principal in accesstable: " + prName);
1549
					logMetacat.debug("running sql: " + pstmt.toString());
1550
					pstmt.execute();
1551
				}// for
1552
			}// for
1553
			pstmt.close();
1554
		}// try
1555
		catch (SQLException e) {
1556
			throw new SAXException("EMLSAXHandler.writeAccessRuletoDB(): "
1557
					+ e.getMessage());
1558
		}// catch
1559
		finally {
1560
			try {
1561
				pstmt.close();
1562
			} catch (SQLException ee) {
1563
				throw new SAXException("EMLSAXHandler.writeAccessRuletoDB(): "
1564
						+ ee.getMessage());
1565
			}
1566
		}// finally
1567

  
1568
	}// writeAccessRuleForRalatedDataFileIntoDB
1569

  
1570 1481
	/* Delete from db all permission for resources related to @aclid if any. */
1571 1482
	private void deletePermissionsInAccessTable(String aclid) throws SAXException {
1572 1483
		Statement stmt = null;

Also available in: Unified diff