Project

General

Profile

« Previous | Next » 

Revision 5977

These are fairly significant changes to MetacatHandler.handleInsertOrUpdateAction() that add in support for creation or update of GUIDs and SystemMetadata. Upon insertion or update of DataPackages from non-DataONE aware clients (such as Morpho), the identifier table is updated by creating a GUID, and the systemmetadata table is updated with fields after the EML document is parsed for distribution information and entity typing. System Metadata documents are also generated and inserted into Metacat. The list of data entities is iterated over and System Metadata is generated for each data file as well.

View differences:

src/edu/ucsb/nceas/metacat/MetacatHandler.java
1562 1562
    public String handleInsertOrUpdateAction(String ipAddress,
1563 1563
            HttpServletResponse response, PrintWriter out, Hashtable<String, String[]> params,
1564 1564
            String user, String[] groups) {
1565
        Logger logMetacat = Logger.getLogger(MetaCatServlet.class);
1565
        Logger logMetacat = Logger.getLogger(MetacatHandler.class);
1566 1566
        DBConnection dbConn = null;
1567 1567
        int serialNumber = -1;
1568 1568
        String output = "";
......
1573 1573
        }
1574 1574
        
1575 1575
        if(params.get("docid") == null){
1576
            String msg = "<?xml version=\"1.0\"?>" +
1577
                "<error>" +
1578
                "Docid not specified" +
1579
                "</error>";
1576
            String msg = this.PROLOG +
1577
                         this.ERROR  +
1578
                         "Docid not specified" +
1579
                         this.ERRORCLOSE;
1580 1580
            if(out != null)
1581 1581
            {
1582 1582
                out.println(msg);
1583
                logMetacat.error("MetaCatServlet.handleInsertOrUpdateAction - Docid not specified");
1583
                logMetacat.error("MetacatHandler.handleInsertOrUpdateAction - Docid not specified");
1584 1584
            }
1585 1585
            return msg; 
1586 1586
        }
1587 1587
        
1588 1588
        try {
1589 1589
            if (!AuthUtil.canInsertOrUpdate(user, groups)) {
1590
                String msg = "<?xml version=\"1.0\"?>" +
1591
                "<error>" +
1592
                "User '" + user + "' not allowed to insert and update" +
1593
                "</error>";
1590
                String msg = this.PROLOG +
1591
                             this.ERROR +
1592
                             "User '" + 
1593
                             user + 
1594
                             "' not allowed to insert and update" +
1595
                             this.ERRORCLOSE;
1594 1596
                if(out != null)
1595 1597
                {
1596 1598
                  out.println(msg);
......
1607 1609
        }
1608 1610
        
1609 1611
        try {
1610
            // Get the document indicated
1611
            logMetacat.debug("MetaCatServlet.handleInsertOrUpdateAction - params: " + params.toString());
1612
            
1613
            String[] doctext = params.get("doctext");
1614
            String pub = null;
1615
            if (params.containsKey("public")) {
1616
                pub = params.get("public")[0];
1617
            }
1618
            
1619
            StringReader dtd = null;
1620
            if (params.containsKey("dtdtext")) {
1621
                String[] dtdtext = params.get("dtdtext");
1622
                try {
1623
                    if (!dtdtext[0].equals("")) {
1624
                        dtd = new StringReader(dtdtext[0]);
1612
          // Get the document indicated
1613
          logMetacat.debug("MetacatHandler.handleInsertOrUpdateAction - params: " + params.toString());
1614
          
1615
          String[] doctext = params.get("doctext");
1616
          String pub = null;
1617
          if (params.containsKey("public")) {
1618
              pub = params.get("public")[0];
1619
          }
1620
          
1621
          StringReader dtd = null;
1622
          if (params.containsKey("dtdtext")) {
1623
              String[] dtdtext = params.get("dtdtext");
1624
              try {
1625
                  if (!dtdtext[0].equals("")) {
1626
                      dtd = new StringReader(dtdtext[0]);
1627
                  }
1628
              } catch (NullPointerException npe) {
1629
              }
1630
          }
1631
          
1632
          if(doctext == null){
1633
              String msg = this.PROLOG +
1634
                           this.ERROR +
1635
                           "Document text not submitted." +
1636
                           this.ERRORCLOSE;
1637
              if(out != null)
1638
              {
1639
                out.println(msg);
1640
              }
1641
              
1642
              // TODO: this should really throw an exception
1643
              return msg;
1644
          }
1645
          
1646
          logMetacat.debug("MetacatHandler.handleInsertOrUpdateAction - the xml document in metacat servlet (before parsing):\n" + doctext[0]);
1647
          StringReader xmlReader = new StringReader(doctext[0]);
1648
          boolean validate = false;
1649
          DocumentImplWrapper documentWrapper = null;
1650
          try {
1651
            // look inside XML Document for <!DOCTYPE ... PUBLIC/SYSTEM ...
1652
            // >
1653
            // in order to decide whether to use validation parser
1654
            validate = needDTDValidation(xmlReader);
1655
            if (validate) {
1656
                // set a dtd base validation parser
1657
                String rule = DocumentImpl.DTD;
1658
                documentWrapper = new DocumentImplWrapper(rule, validate);
1659
            } else {
1660
                
1661
                String namespace = XMLSchemaService.findDocumentNamespace(xmlReader);
1662
                
1663
                if (namespace != null) {
1664
                    if (namespace.compareTo(DocumentImpl.EML2_0_0NAMESPACE) == 0
1665
                            || namespace.compareTo(
1666
                            DocumentImpl.EML2_0_1NAMESPACE) == 0) {
1667
                        // set eml2 base     validation parser
1668
                        String rule = DocumentImpl.EML200;
1669
                        // using emlparser to check id validation
1670
                        @SuppressWarnings("unused")
1671
                        EMLParser parser = new EMLParser(doctext[0]);
1672
                        documentWrapper = new DocumentImplWrapper(rule, true);
1673
                    } else if (
1674
                    		namespace.compareTo(DocumentImpl.EML2_1_0NAMESPACE) == 0
1675
                    		|| namespace.compareTo(DocumentImpl.EML2_1_1NAMESPACE) == 0) {
1676
                        // set eml2 base validation parser
1677
                        String rule = DocumentImpl.EML210;
1678
                        // using emlparser to check id validation
1679
                        @SuppressWarnings("unused")
1680
                        EMLParser parser = new EMLParser(doctext[0]);
1681
                        documentWrapper = new DocumentImplWrapper(rule, true);
1682
                    } else {
1683
                        // set schema base validation parser
1684
                        String rule = DocumentImpl.SCHEMA;
1685
                        documentWrapper = new DocumentImplWrapper(rule, true);
1625 1686
                    }
1626
                } catch (NullPointerException npe) {
1687
                } else {
1688
                    documentWrapper = new DocumentImplWrapper("", false);
1627 1689
                }
1628 1690
            }
1629 1691
            
1630
            if(doctext == null){
1631
                String msg = "<?xml version=\"1.0\"?>" +
1632
                "<error>" +
1633
                "Document text not submitted." +
1634
                "</error>";
1635
                if(out != null)
1636
                {
1637
                  out.println(msg);
1638
                }
1692
            String[] action = params.get("action");
1693
            String[] docid = params.get("docid");
1694
            String newdocid = null;
1695
            
1696
            String doAction = null;
1697
            if (action[0].equals("insert") || action[0].equals("insertmultipart")) {
1698
                doAction = "INSERT";
1639 1699
                
1640
                // TODO: this should really throw an exception
1641
                return msg;
1700
            } else if (action[0].equals("update")) {
1701
                doAction = "UPDATE";
1702
                
1642 1703
            }
1643 1704
            
1644
            logMetacat.debug("MetaCatServlet.handleInsertOrUpdateAction - the xml document in metacat servlet (before parsing):\n" + doctext[0]);
1645
            StringReader xmlReader = new StringReader(doctext[0]);
1646
            boolean validate = false;
1647
            DocumentImplWrapper documentWrapper = null;
1648 1705
            try {
1649
                // look inside XML Document for <!DOCTYPE ... PUBLIC/SYSTEM ...
1650
                // >
1651
                // in order to decide whether to use validation parser
1652
                validate = needDTDValidation(xmlReader);
1653
                if (validate) {
1654
                    // set a dtd base validation parser
1655
                    String rule = DocumentImpl.DTD;
1656
                    documentWrapper = new DocumentImplWrapper(rule, validate);
1657
                } else {
1658
                    
1659
                    String namespace = XMLSchemaService.findDocumentNamespace(xmlReader);
1660
                    
1661
                    if (namespace != null) {
1662
                        if (namespace.compareTo(DocumentImpl.EML2_0_0NAMESPACE) == 0
1663
                                || namespace.compareTo(
1664
                                DocumentImpl.EML2_0_1NAMESPACE) == 0) {
1665
                            // set eml2 base     validation parser
1666
                            String rule = DocumentImpl.EML200;
1667
                            // using emlparser to check id validation
1668
                            @SuppressWarnings("unused")
1669
                            EMLParser parser = new EMLParser(doctext[0]);
1670
                            documentWrapper = new DocumentImplWrapper(rule, true);
1671
                        } else if (
1672
                        		namespace.compareTo(DocumentImpl.EML2_1_0NAMESPACE) == 0
1673
                        		|| namespace.compareTo(DocumentImpl.EML2_1_1NAMESPACE) == 0) {
1674
                            // set eml2 base validation parser
1675
                            String rule = DocumentImpl.EML210;
1676
                            // using emlparser to check id validation
1677
                            @SuppressWarnings("unused")
1678
                            EMLParser parser = new EMLParser(doctext[0]);
1679
                            documentWrapper = new DocumentImplWrapper(rule, true);
1680
                        } else {
1681
                            // set schema base validation parser
1682
                            String rule = DocumentImpl.SCHEMA;
1683
                            documentWrapper = new DocumentImplWrapper(rule, true);
1684
                        }
1685
                    } else {
1686
                        documentWrapper = new DocumentImplWrapper("", false);
1687
                    }
1688
                }
1706
              // get a connection from the pool
1707
              dbConn = DBConnectionPool
1708
                      .getDBConnection("Metacathandler.handleInsertOrUpdateAction");
1709
              serialNumber = dbConn.getCheckOutSerialNumber();
1710
              
1711
              // write the document to the database and disk
1712
              String accNumber = docid[0];
1713
              logMetacat.debug("MetaCatServlet.handleInsertOrUpdateAction - " + 
1714
                doAction + " " + accNumber + "...");
1715
              
1716
              if (accNumber == null || accNumber.equals("")) {
1717
                  logMetacat.warn("MetacatHandler.handleInsertOrUpdateAction - writing with null acnumber");
1718
                  newdocid = documentWrapper.write(dbConn, doctext[0], pub, dtd,
1719
                          doAction, null, user, groups);
1720
                  EventLog.getInstance().log(ipAddress, user, "", action[0]);
1721
              
1722
              } else {
1723
                  newdocid = documentWrapper.write(dbConn, doctext[0], pub, dtd,
1724
                          doAction, accNumber, user, groups);
1725
            
1726
                  EventLog.getInstance().log(ipAddress, user, accNumber, action[0]);
1727
              
1728
              }
1729
              
1730
              // Check for existing system metadata for the docid. Update
1731
              // or create it as necessary, but only for non-DataONE
1732
              // calls to handleInsertOrUpdateAction()
1733
              if ( !(ipAddress.contains("/d1")) ) {
1689 1734
                
1690
                String[] action = params.get("action");
1691
                String[] docid = params.get("docid");
1692
                String newdocid = null;
1735
                SystemMetadata sysMeta = new SystemMetadata();  
1736
                IdentifierManager im = IdentifierManager.getInstance();
1737
                Hashtable<String, Object> docInfo = 
1738
                  im.getDocumentInfo(newdocid);
1739
                int rev = im.getLatestRevForLocalId(newdocid);
1740
                Identifier identifier = new Identifier();
1741
                String sysMetaLocalId = "";
1693 1742
                
1694
                String doAction = null;
1695
                if (action[0].equals("insert") || action[0].equals("insertmultipart")) {
1696
                    doAction = "INSERT";
1697
                } else if (action[0].equals("update")) {
1698
                    doAction = "UPDATE";
1699
                }
1700
                
1743
                // it's possible that system metadata exists although
1744
                // older clients don't support it. Try updates first.
1701 1745
                try {
1702
                    // get a connection from the pool
1703
                    dbConn = DBConnectionPool
1704
                            .getDBConnection("MetaCatServlet.handleInsertOrUpdateAction");
1705
                    serialNumber = dbConn.getCheckOutSerialNumber();
1746
                  // handle updates
1747
                  identifier.setValue(im.getGUID(newdocid, rev));
1748
                  sysMeta = getSystemMetadata(identifier, user, groups);
1749
                  sysMetaLocalId = insertOrUpdateSystemMetadata(
1750
                    sysMeta, "update", user, groups);
1751
                  
1752
                } catch ( PropertyNotFoundException pnfe) {
1753
                  logMetacat.error("There was a problem getting the server " +
1754
                    "url properties: " + pnfe.getMessage());
1755
                  
1756
                } catch ( McdbDocNotFoundException mnfe) {
1757
                  
1758
                  // handle inserts
1759
                  try {
1760
                    sysMeta = createSystemMetadata(newdocid, user, groups);
1761
                    sysMetaLocalId = insertOrUpdateSystemMetadata(
1762
                      sysMeta, "insert", user, groups);
1706 1763
                    
1707
                    // write the document to the database and disk
1708
                    String accNumber = docid[0];
1709
                    logMetacat.debug("MetaCatServlet.handleInsertOrUpdateAction - " + doAction + " "
1710
                            + accNumber + "...");
1711
                    if (accNumber == null || accNumber.equals("")) {
1712
                        logMetacat.warn("MetaCatServlet.handleInsertOrUpdateAction - writing with null acnumber");
1713
                        newdocid = documentWrapper.write(dbConn, doctext[0], pub, dtd,
1714
                                doAction, null, user, groups);
1715
                        EventLog.getInstance().log(ipAddress, user, "", action[0]);
1716
                    } else {
1717
                        newdocid = documentWrapper.write(dbConn, doctext[0], pub, dtd,
1718
                                doAction, accNumber, user, groups);
1719

  
1720
                        EventLog.getInstance().log(ipAddress, user, accNumber, action[0]);
1721
                    }
1722
                } finally {
1723
                    // Return db connection
1724
                    DBConnectionPool.returnDBConnection(dbConn, serialNumber);
1764
                  } catch ( McdbDocNotFoundException dnfe ) {
1765
                    logMetacat.debug(
1766
                      "There was a problem finding the localId " +
1767
                      newdocid + "The error was: " + dnfe.getMessage());
1768
                    throw dnfe;
1769
            
1770
                  } catch ( AccessionNumberException ane ) {
1771
            
1772
                    logMetacat.debug(
1773
                      "There was a problem creating the accession number " +
1774
                      "for " + newdocid + ". The error was: " + ane.getMessage());
1775
                    throw ane;
1776
                    
1777
                  } // end try()
1778
                                        
1725 1779
                }
1726 1780
                
1727
                // set content type and other response header fields first
1728
                //response.setContentType("text/xml");
1729
                output += "<?xml version=\"1.0\"?>";
1730
                output += "<success>";
1731
                output += "<docid>" + newdocid + "</docid>";
1732
                output += "</success>";
1733
                
1734
            } catch (NullPointerException npe) {
1735
                //response.setContentType("text/xml");
1736
                output += "<?xml version=\"1.0\"?>";
1737
                output += "<error>";
1738
                output += npe.getMessage();
1739
                output += "</error>";
1740
                logMetacat.warn("MetaCatServlet.handleInsertOrUpdateAction - Null pointer error when writing eml document to the database: " + npe.getMessage());
1741
                npe.printStackTrace();
1781
              } // end if()
1782
              
1783
              
1784
            } finally {
1785
                // Return db connection
1786
                DBConnectionPool.returnDBConnection(dbConn, serialNumber);
1742 1787
            }
1788
            
1789
            // set content type and other response header fields first
1790
            //response.setContentType("text/xml");
1791
            output += this.PROLOG;
1792
            output += this.SUCCESS;
1793
            output += "<docid>" + newdocid + "</docid>";
1794
            output += this.SUCCESSCLOSE;
1795
            
1796
          } catch (NullPointerException npe) {
1797
              //response.setContentType("text/xml");
1798
              output += this.PROLOG;
1799
              output += this.ERROR;
1800
              output += npe.getMessage();
1801
              output += this.ERRORCLOSE;
1802
              logMetacat.warn("MetaCatServlet.handleInsertOrUpdateAction - Null pointer error when writing eml document to the database: " + npe.getMessage());
1803
              npe.printStackTrace();
1804
          }
1743 1805
        } catch (Exception e) {
1744 1806
            //response.setContentType("text/xml");
1745
            output += "<?xml version=\"1.0\"?>";
1746
            output += "<error>";
1807
            output += this.PROLOG;
1808
            output += this.ERROR;
1747 1809
            output += e.getMessage();
1748
            output += "</error>";
1810
            output += this.ERRORCLOSE;
1749 1811
            logMetacat.warn("MetaCatServlet.handleInsertOrUpdateAction - General error when writing eml document to the database: " + e.getMessage());
1750 1812
            e.printStackTrace();
1751 1813
        }

Also available in: Unified diff