Project

General

Profile

« Previous | Next » 

Revision 9352

Update the packageId attribute in EML documents on update. Never display the local docid in the rendered EML.

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

  
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;
49 59
import javax.servlet.http.HttpServletRequest;
50 60

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

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

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

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

  
1572 1586
      return objectList;
1573 1587
  }
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();
1574 1621

  
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

  
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

  
1575 1662
  /**
1576 1663
   * Update a systemMetadata document
1577 1664
   * 
src/edu/ucsb/nceas/metacat/dataone/MNodeService.java
36 36
import java.io.Writer;
37 37
import java.math.BigInteger;
38 38
import java.net.URISyntaxException;
39
import java.nio.charset.Charset;
39 40
import java.security.NoSuchAlgorithmException;
40 41
import java.sql.SQLException;
41 42
import java.util.ArrayList;
......
454 455
                //String objectAsXML = "";
455 456
                try {
456 457
                    //objectAsXML = IOUtils.toString(object, "UTF-8");
457
                    // give the old pid so we can calculate the new local id 
458
                    localId = insertOrUpdateDocument(object, "UTF-8", pid, session, "update");
458
                	
459
                	InputStream editedObject = editScienceMetadata(session, object, pid, newPid);
460
                	
461
                    localId = insertOrUpdateDocument(editedObject, "UTF-8", pid, session, "update");
462
                	
459 463
                    // register the newPid and the generated localId
460 464
                    if (newPid != null) {
461 465
                        IdentifierManager.getInstance().createMapping(newPid.getValue(), localId);
462

  
463 466
                    }
464 467

  
465 468
                } catch (IOException e) {

Also available in: Unified diff