Project

General

Profile

« Previous | Next » 

Revision 6299

handle null values from DB better when querying system metadata

View differences:

src/edu/ucsb/nceas/metacat/IdentifierManager.java
62 62
import edu.ucsb.nceas.metacat.database.DBConnectionPool;
63 63
import edu.ucsb.nceas.metacat.properties.PropertyService;
64 64
import edu.ucsb.nceas.metacat.shared.AccessException;
65
import edu.ucsb.nceas.metacat.shared.ServiceException;
65 66
import edu.ucsb.nceas.metacat.util.DocumentUtil;
67
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
66 68

  
67 69
/**
68 70
 * Manage the relationship between Metacat local identifiers (LocalIDs) that are
......
1469 1471
     * @param start
1470 1472
     * @param count
1471 1473
     * @return ObjectList
1474
     * @throws SQLException 
1475
     * @throws ServiceException 
1476
     * @throws PropertyNotFoundException 
1472 1477
     */
1473 1478
    public ObjectList querySystemMetadata(Date startTime, Date endTime, 
1474
            ObjectFormat objectFormat, boolean replicaStatus, int start, int count)
1479
            ObjectFormat objectFormat, boolean replicaStatus, int start, int count) throws SQLException, PropertyNotFoundException, ServiceException
1475 1480
    {
1476 1481
        ObjectList ol = new ObjectList();
1477 1482
        int total = 0;
1478 1483
        DBConnection dbConn = null;
1479 1484
        int serialNumber = -1;
1480 1485
        
1481
        try
1482
        {
1486
        try {
1483 1487
            String sql = "select guid, date_uploaded, rights_holder, checksum, " +
1484 1488
                "checksum_algorithm, origin_member_node, authoritive_member_node, " +
1485 1489
                "date_modified, submitter, object_format, size from systemmetadata";
......
1488 1492
            boolean f2 = false;
1489 1493
            boolean f3 = false;
1490 1494
            
1491
            if(startTime != null)
1492
            {
1495
            if (startTime != null) {
1493 1496
                sql += " where systemmetadata.date_modified > ?";
1494 1497
                f1 = true;
1495 1498
            }
1496 1499
            
1497
            if(endTime != null)
1498
            {
1499
                if(!f1)
1500
                {
1500
            if (endTime != null) {
1501
                if (!f1) {
1501 1502
                    sql += " where systemmetadata.date_modified < ?";
1502 1503
                }
1503
                else
1504
                {
1504
                else {
1505 1505
                    sql += " and systemmetadata.date_modified < ?";
1506 1506
                }
1507 1507
                f2 = true;
1508 1508
            }
1509 1509
            
1510
            if(objectFormat != null)
1511
            {
1512
                if(!f1 && !f2)
1513
                {
1510
            if (objectFormat != null) {
1511
                if (!f1 && !f2) {
1514 1512
                    sql += " where object_format = ?";
1515 1513
                }
1516
                else 
1517
                {
1514
                else {
1518 1515
                    sql += " and object_format = ?";
1519 1516
                }
1520 1517
                f3 = true;
1521 1518
            }
1522 1519
            
1523
            if(replicaStatus)
1524
            {
1520
            if (replicaStatus) {
1525 1521
                String currentNodeId = PropertyService.getInstance().getProperty("dataone.memberNodeId");
1526
                if(!f1 && !f2 && !f3)
1527
                {
1522
                if (!f1 && !f2 && !f3) {
1528 1523
                    sql += " where authoritive_member_node != '" + currentNodeId.trim() + "'";
1529 1524
                }
1530
                else
1531
                {
1525
                else {
1532 1526
                    sql += " and authoritive_member_node != '" + currentNodeId.trim() + "'";
1533 1527
                }
1534 1528
            }
......
1537 1531
            serialNumber = dbConn.getCheckOutSerialNumber();
1538 1532
            PreparedStatement stmt = dbConn.prepareStatement(sql);
1539 1533
            
1540
            if(f1 && f2 && f3)
1541
            {
1534
            if (f1 && f2 && f3) {
1542 1535
                stmt.setTimestamp(1, new Timestamp(startTime.getTime()));
1543 1536
                stmt.setTimestamp(2, new Timestamp(endTime.getTime()));
1544 1537
                stmt.setString(3, objectFormat.getFmtid().getValue());
1545 1538
            }
1546
            else if(f1 && f2 && !f3)
1547
            {
1539
            else if (f1 && f2 && !f3) {
1548 1540
                stmt.setTimestamp(1, new Timestamp(startTime.getTime()));
1549 1541
                stmt.setTimestamp(2, new Timestamp(endTime.getTime()));
1550 1542
            }
1551
            else if(f1 && !f2 && f3)
1552
            {
1543
            else if (f1 && !f2 && f3) {
1553 1544
                stmt.setTimestamp(1, new Timestamp(startTime.getTime()));
1554 1545
                stmt.setString(2, objectFormat.getFmtid().getValue());
1555 1546
            }
1556
            else if(f1 && !f2 && !f3)
1557
            {
1547
            else if (f1 && !f2 && !f3) {
1558 1548
                stmt.setTimestamp(1, new Timestamp(startTime.getTime()));
1559 1549
            }
1560
            else if(!f1 && f2 && f3)
1561
            {
1550
            else if (!f1 && f2 && f3) {
1562 1551
                stmt.setTimestamp(1, new Timestamp(endTime.getTime()));
1563 1552
                stmt.setString(2, objectFormat.getFmtid().getValue());
1564 1553
            }
1565
            else if(!f1 && !f2 && f3)
1566
            {
1554
            else if (!f1 && !f2 && f3) {
1567 1555
                stmt.setString(1, objectFormat.getFmtid().getValue());
1568 1556
            }
1569
            else if(!f1 && f2 && !f3)
1570
            {
1557
            else if (!f1 && f2 && !f3) {
1571 1558
                stmt.setTimestamp(1, new Timestamp(endTime.getTime()));
1572 1559
            }
1573 1560
            
1574 1561
            //logMetacat.debug("LISTOBJECTS QUERY: " + stmt.toString());
1575 1562
            
1576 1563
            ResultSet rs = stmt.executeQuery();
1577
            for(int i=0; i<start; i++)
1578
            {
1579
                if(rs.next())
1580
                {
1564
            for (int i=0; i<start; i++) {
1565
                if (rs.next()) {
1581 1566
                    total++;
1582
                }
1583
                else
1584
                {
1567
                } else {
1585 1568
                    break;
1586 1569
                }
1587 1570
            }
1588 1571
            
1589 1572
            int countIndex = 0;
1590 1573
                        
1591
            while(rs.next()) 
1592
            {
1574
            while (rs.next()) {
1593 1575
                total++;
1594
                if(countIndex >= count)
1595
                {
1596
                    break;
1576
                if (countIndex >= count) {
1577
                	// allow unlimited (negative number for count)
1578
                	if (count > 0) {
1579
                		break;
1580
                	}
1597 1581
                }
1582
                
1598 1583
                String guid = rs.getString(1);
1599 1584
                //logMetacat.debug("query found doc with guid " + guid);
1600 1585
                //Timestamp dateUploaded = rs.getTimestamp(2);
......
1608 1593
                String fmtidStr = rs.getString(10);
1609 1594
                String sz = rs.getString(11);
1610 1595
                long size = 0;
1611
                if(sz != null && !sz.trim().equals(""))
1612
                {
1596
                
1597
                if (sz != null && !sz.trim().equals("")) {
1613 1598
                    size = new Long(rs.getString(11)).longValue();
1614 1599
                }
1615 1600
                
......
1619 1604
                id.setValue(guid);
1620 1605
                oi.setIdentifier(id);
1621 1606
                
1622
                oi.setDateSysMetadataModified(new Date(dateModified.getTime()));
1607
                if (dateModified != null) {
1608
                	oi.setDateSysMetadataModified(new Date(dateModified.getTime()));
1609
                }
1623 1610
                
1624 1611
                Checksum cs = new Checksum();
1625 1612
                cs.setValue(checksum);
1626
                cs.setAlgorithm(ChecksumAlgorithm.valueOf(checksumAlgorithm));
1627
                //cs.setAlgorithm(ChecksumAlgorithm.convert(checksumAlgorithm));
1613
                try {
1614
                	cs.setAlgorithm(ChecksumAlgorithm.valueOf(checksumAlgorithm));
1615
                    //cs.setAlgorithm(ChecksumAlgorithm.convert(checksumAlgorithm));
1616
                } catch (Exception e) {
1617
					logMetacat.warn("could not parse checksum algorithm", e);
1618
				}
1628 1619
                oi.setChecksum(cs);
1629 1620
                
1630 1621
                try {
1631
                ObjectFormat oFormat = ObjectFormatCache.getInstance().getFormat(fmtidStr);
1632
                oi.setObjectFormat(oFormat);
1633

  
1634
                } catch (NotFound nfe) {
1635
                  oi.setObjectFormat(ObjectFormatCache.getInstance().getFormat("application/octet-stream"));
1636
	
1637
                }
1622
	                oi.setObjectFormat(ObjectFormatCache.getInstance().getFormat(fmtidStr));
1623
                } catch (Exception e) {
1624
                	logMetacat.warn("could not find object format: " + fmtidStr, e);
1625
				}
1638 1626
                                
1639 1627
                oi.setSize(size);
1640 1628
                
......
1642 1630
                countIndex++;
1643 1631
            }
1644 1632
            
1645
            while(rs.next())
1646
            { //expend the resultset to get the total count of possible rows
1633
            // expend the resultset to get the total count of possible rows
1634
            while (rs.next()) { 
1647 1635
                total++;
1648 1636
            }
1649 1637
        }
1650
        catch(Exception e)
1651
        {
1652
           e.printStackTrace();
1653
           logMetacat.error("Error querying system metadata: " + e.getMessage());
1654
        }
1655
        finally 
1656
        {
1638
        
1639
        finally {
1657 1640
            // Return database connection to the pool
1658 1641
            DBConnectionPool.returnDBConnection(dbConn, serialNumber);
1659 1642
        }

Also available in: Unified diff