Project

General

Profile

« Previous | Next » 

Revision 9387

Move the editScienceMetadata method to the MNodeService class - only update packageId in EML on publish()

View differences:

src/edu/ucsb/nceas/metacat/dataone/D1NodeService.java
23 23

  
24 24
package edu.ucsb.nceas.metacat.dataone;
25 25

  
26
import java.io.ByteArrayInputStream;
27 26
import java.io.ByteArrayOutputStream;
28 27
import java.io.File;
29 28
import java.io.FileNotFoundException;
......
32 31
import java.io.InputStream;
33 32
import java.io.OutputStream;
34 33
import java.io.OutputStreamWriter;
35
import java.io.StringReader;
36
import java.io.UnsupportedEncodingException;
37 34
import java.io.Writer;
38 35
import java.math.BigInteger;
39
import java.nio.charset.Charset;
40 36
import java.sql.PreparedStatement;
41 37
import java.sql.ResultSet;
42 38
import java.sql.SQLException;
......
50 46
import java.util.Vector;
51 47
import java.util.concurrent.locks.Lock;
52 48

  
53
import javax.xml.transform.Result;
54
import javax.xml.transform.Source;
55
import javax.xml.transform.TransformerException;
56
import javax.xml.transform.TransformerFactory;
57
import javax.xml.transform.dom.DOMSource;
58
import javax.xml.transform.stream.StreamResult;
59 49
import javax.servlet.http.HttpServletRequest;
60 50

  
61 51
import org.apache.commons.io.IOUtils;
......
96 86
import org.dataone.service.types.v1.util.AuthUtils;
97 87
import org.dataone.service.types.v1.util.ChecksumUtil;
98 88
import org.dataone.service.util.Constants;
99
import org.w3c.dom.Document;
100 89

  
101 90
import edu.ucsb.nceas.metacat.AccessionNumber;
102 91
import edu.ucsb.nceas.metacat.AccessionNumberException;
......
116 105
import edu.ucsb.nceas.metacat.replication.ForceReplicationHandler;
117 106
import edu.ucsb.nceas.metacat.util.SkinUtil;
118 107
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
119
import edu.ucsb.nceas.utilities.XMLUtilities;
120 108

  
121 109
public abstract class D1NodeService {
122 110
    
123 111
  public static final String DELETEDMESSAGE = "The object with the PID has been deleted from the node.";
124
  
125
  private static String XPATH_EML_ID = "/eml:eml/@packageId";
126
  
112
    
127 113
  private static Logger logMetacat = Logger.getLogger(D1NodeService.class);
128 114

  
129 115
  /** For logging the operations */
......
1585 1571

  
1586 1572
      return objectList;
1587 1573
  }
1588
  
1589
  /**
1590
   * Update a science metadata document with its new Identifier  
1591
   * 
1592
   * @param session - the Session object containing the credentials for the Subject
1593
   * @param object - the InputStream for the XML object to be edited
1594
   * @param pid - the Identifier of the XML object to be updated
1595
   * @param newPid = the new Identifier to give to the modified XML doc
1596
   * 
1597
   * @return newObject - The InputStream for the modified XML object
1598
   * 
1599
   * @throws ServiceFailure
1600
   * @throws IOException
1601
   * @throws UnsupportedEncodingException
1602
   * @throws InvalidToken
1603
   * @throws NotAuthorized
1604
   * @throws NotFound
1605
   * @throws NotImplemented
1606
   */
1607
  public InputStream editScienceMetadata(Session session, InputStream object, Identifier pid, Identifier newPid)
1608
  	throws ServiceFailure, IOException, UnsupportedEncodingException, InvalidToken, NotAuthorized, NotFound, NotImplemented {
1609
    
1610
	logMetacat.debug("D1NodeService.editScienceMetadata() called.");
1611
	
1612
	 InputStream newObject = null;
1613
	
1614
    try{   	
1615
    	//Get the root node of the XML document
1616
    	byte[] xmlBytes  = IOUtils.toByteArray(object);
1617
        String xmlStr = new String(xmlBytes, "UTF-8");
1618
        
1619
    	Document doc = XMLUtilities.getXMLReaderAsDOMDocument(new StringReader(xmlStr));
1620
	    org.w3c.dom.Node docNode = doc.getDocumentElement();
1621 1574

  
1622
	    //Get the system metadata for this object
1623
	    SystemMetadata sysMeta = null;
1624
	    try{
1625
	    	sysMeta = getSystemMetadata(session, pid);
1626
	    } catch(NotAuthorized e){
1627
	    	throw new ServiceFailure("1030", "D1NodeService.editScienceMetadata(): " + 
1628
	    			"This session is not authorized to access the system metadata for " +
1629
	    			pid.getValue() + " : " + e.getMessage());
1630
	    } catch(NotFound e){
1631
	    	throw new ServiceFailure("1030", "D1NodeService.editScienceMetadata(): " + 
1632
	    			"Could not find the system metadata for " +
1633
	    			pid.getValue() + " : " + e.getMessage());
1634
	    }
1635
	    
1636
	    //Get the formatId
1637
        ObjectFormatIdentifier objFormatId = sysMeta.getFormatId();
1638
        String formatId = objFormatId.getValue();
1639
        
1640
    	//For all EML formats
1641
        if(formatId.indexOf("eml") == 0){
1642
        	//Update or add the id attribute
1643
    	    XMLUtilities.addAttributeNodeToDOMTree(docNode, XPATH_EML_ID, newPid.getValue());
1644
        }
1645 1575

  
1646
        //The modified object InputStream
1647
	    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
1648
	    Source xmlSource = new DOMSource(docNode);
1649
	    Result outputTarget = new StreamResult(outputStream);
1650
	    TransformerFactory.newInstance().newTransformer().transform(xmlSource, outputTarget);
1651
	    newObject = new ByteArrayInputStream(outputStream.toByteArray());
1652
	    
1653
    } catch(TransformerException e) {
1654
    	throw new ServiceFailure("1030", "D1NodeService.editScienceMetadata(): " +
1655
                "Could not update the ID in the XML document for " +
1656
                "pid " + pid.getValue() +" : " + e.getMessage());
1657
    }
1658
    
1659
    return newObject;
1660
  }
1661

  
1662 1576
  /**
1663 1577
   * Update a systemMetadata document
1664 1578
   * 
......
1819 1733
                  throw new InvalidRequest("4869", "Can't find the pid "+pid.getValue()+" for archive.");
1820 1734
              }
1821 1735
          }
1822
      
1823
      } else if((currentSysmeta.getArchived() != null && currentSysmeta.getArchived() == true) && (sysmeta.getArchived() == null || sysmeta.getArchived() == false)) {
1824
          throw new InvalidRequest("4869", "The pid "+pid.getValue()+" has been archived and it can't be set archive false again.");
1825 1736
      } else {
1826 1737
          logMetacat.debug("D1Node.update - regularly update the system metadata of the pid "+pid.getValue());
1827 1738
          updateSystemMetadataWithoutLock(sysmeta, needUpdateModificationDate);
src/edu/ucsb/nceas/metacat/dataone/MNodeService.java
32 32
import java.io.InputStream;
33 33
import java.io.InputStreamReader;
34 34
import java.io.OutputStreamWriter;
35
import java.io.StringReader;
35 36
import java.io.UnsupportedEncodingException;
36 37
import java.io.Writer;
37 38
import java.math.BigInteger;
......
53 54
import java.util.Vector;
54 55

  
55 56
import javax.servlet.http.HttpServletRequest;
57
import javax.xml.transform.Result;
58
import javax.xml.transform.Source;
59
import javax.xml.transform.TransformerException;
60
import javax.xml.transform.TransformerFactory;
61
import javax.xml.transform.dom.DOMSource;
62
import javax.xml.transform.stream.StreamResult;
56 63

  
57 64
import org.apache.commons.io.IOUtils;
58 65
import org.apache.log4j.Logger;
......
129 136
import org.ecoinformatics.datamanager.parser.Entity;
130 137
import org.ecoinformatics.datamanager.parser.generic.DataPackageParserInterface;
131 138
import org.ecoinformatics.datamanager.parser.generic.Eml200DataPackageParser;
139
import org.w3c.dom.Document;
132 140

  
133 141
import edu.ucsb.nceas.ezid.EZIDException;
134 142
import edu.ucsb.nceas.metacat.DBQuery;
......
191 199
	public static final String UUID_SCHEME = "UUID";
192 200
	public static final String DOI_SCHEME = "DOI";
193 201
	private static final String UUID_PREFIX = "urn:uuid:";
202
	
203
	private static String XPATH_EML_ID = "/eml:eml/@packageId";
194 204

  
195 205
	/* the logger instance */
196 206
    private Logger logMetacat = null;
......
457 467
                try {
458 468
                    //objectAsXML = IOUtils.toString(object, "UTF-8");
459 469
                	
460
                	//InputStream editedObject = editScienceMetadata(session, object, pid, newPid);
461
                    //localId = insertOrUpdateDocument(editedObject, "UTF-8", pid, session, "update");
462 470
                    localId = insertOrUpdateDocument(object, "UTF-8", pid, session, "update");
463 471
                    
464 472
                    // register the newPid and the generated localId
......
1845 1853
	 * @throws UnsupportedType 
1846 1854
	 * @throws IdentifierNotUnique 
1847 1855
	 */
1848
	public Identifier publish(Session session, Identifier originalIdentifier) throws InvalidToken, ServiceFailure, NotAuthorized, NotImplemented, InvalidRequest, NotFound, IdentifierNotUnique, UnsupportedType, InsufficientResources, InvalidSystemMetadata {
1856
	public Identifier publish(Session session, Identifier originalIdentifier) throws InvalidToken, 
1857
	ServiceFailure, NotAuthorized, NotImplemented, InvalidRequest, NotFound, IdentifierNotUnique, 
1858
	UnsupportedType, InsufficientResources, InvalidSystemMetadata, IOException {
1849 1859
		
1850 1860
	    String serviceFailureCode = "1030";
1851 1861
	    Identifier sid = getPIDForSID(originalIdentifier, serviceFailureCode);
......
1879 1889
		// ensure it is publicly readable
1880 1890
		sysmeta = makePublicIfNot(sysmeta, originalIdentifier);
1881 1891
		
1882
		// get the bytes
1883
		InputStream inputStream = this.get(session, originalIdentifier);
1892
		//Get the bytes
1893
		InputStream inputStream = null;		
1894
		boolean isScienceMetadata = isScienceMetadata(sysmeta);
1895
		//If it's a science metadata doc, we want to update the packageId first
1896
		if(isScienceMetadata){
1897
			InputStream originalObject = this.get(session, originalIdentifier);
1898
			
1899
			//Edit the science metadata with the new package Id (EML)
1900
			inputStream = editScienceMetadata(session, originalObject, originalIdentifier, newIdentifier);
1901
		}
1902
		else{
1903
			inputStream = this.get(session, originalIdentifier);
1904
		}
1884 1905
		
1885 1906
		// update the object
1886 1907
		this.update(session, originalIdentifier, inputStream, newIdentifier, sysmeta);
......
2034 2055
	}
2035 2056
	
2036 2057
	/**
2058
	   * Update a science metadata document with its new Identifier  
2059
	   * 
2060
	   * @param session - the Session object containing the credentials for the Subject
2061
	   * @param object - the InputStream for the XML object to be edited
2062
	   * @param pid - the Identifier of the XML object to be updated
2063
	   * @param newPid = the new Identifier to give to the modified XML doc
2064
	   * 
2065
	   * @return newObject - The InputStream for the modified XML object
2066
	   * 
2067
	   * @throws ServiceFailure
2068
	   * @throws IOException
2069
	   * @throws UnsupportedEncodingException
2070
	   * @throws InvalidToken
2071
	   * @throws NotAuthorized
2072
	   * @throws NotFound
2073
	   * @throws NotImplemented
2074
	   */
2075
	  public InputStream editScienceMetadata(Session session, InputStream object, Identifier pid, Identifier newPid)
2076
	  	throws ServiceFailure, IOException, UnsupportedEncodingException, InvalidToken, NotAuthorized, NotFound, NotImplemented {
2077
	    
2078
		logMetacat.debug("D1NodeService.editScienceMetadata() called.");
2079
		
2080
		 InputStream newObject = null;
2081
		
2082
	    try{   	
2083
	    	//Get the root node of the XML document
2084
	    	byte[] xmlBytes  = IOUtils.toByteArray(object);
2085
	        String xmlStr = new String(xmlBytes, "UTF-8");
2086
	        
2087
	    	Document doc = XMLUtilities.getXMLReaderAsDOMDocument(new StringReader(xmlStr));
2088
		    org.w3c.dom.Node docNode = doc.getDocumentElement();
2089

  
2090
		    //Get the system metadata for this object
2091
		    SystemMetadata sysMeta = null;
2092
		    try{
2093
		    	sysMeta = getSystemMetadata(session, pid);
2094
		    } catch(NotAuthorized e){
2095
		    	throw new ServiceFailure("1030", "D1NodeService.editScienceMetadata(): " + 
2096
		    			"This session is not authorized to access the system metadata for " +
2097
		    			pid.getValue() + " : " + e.getMessage());
2098
		    } catch(NotFound e){
2099
		    	throw new ServiceFailure("1030", "D1NodeService.editScienceMetadata(): " + 
2100
		    			"Could not find the system metadata for " +
2101
		    			pid.getValue() + " : " + e.getMessage());
2102
		    }
2103
		    
2104
		    //Get the formatId
2105
	        ObjectFormatIdentifier objFormatId = sysMeta.getFormatId();
2106
	        String formatId = objFormatId.getValue();
2107
	        
2108
	    	//For all EML formats
2109
	        if(formatId.indexOf("eml") == 0){
2110
	        	//Update or add the id attribute
2111
	    	    XMLUtilities.addAttributeNodeToDOMTree(docNode, XPATH_EML_ID, newPid.getValue());
2112
	        }
2113

  
2114
	        //The modified object InputStream
2115
		    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
2116
		    Source xmlSource = new DOMSource(docNode);
2117
		    Result outputTarget = new StreamResult(outputStream);
2118
		    TransformerFactory.newInstance().newTransformer().transform(xmlSource, outputTarget);
2119
		    newObject = new ByteArrayInputStream(outputStream.toByteArray());
2120
		    
2121
	    } catch(TransformerException e) {
2122
	    	throw new ServiceFailure("1030", "MNNodeService.editScienceMetadata(): " +
2123
	                "Could not update the ID in the XML document for " +
2124
	                "pid " + pid.getValue() +" : " + e.getMessage());
2125
	    } catch(IOException e){
2126
	    	throw new ServiceFailure("1030", "MNNodeService.editScienceMetadata(): " +
2127
	                "Could not update the ID in the XML document for " +
2128
	                "pid " + pid.getValue() +" : " + e.getMessage());
2129
	    }
2130
	    
2131
	    return newObject;
2132
	  }
2133
	
2134
	/**
2037 2135
	 * Determines if we already have registered an ORE map for this package
2038 2136
	 * NOTE: uses a solr query to locate OREs for the object
2039 2137
	 * @param guid of the EML/packaging object

Also available in: Unified diff