/*
 */
package org.dspace.curate;

import java.io.IOException;
import java.sql.SQLException;
import java.util.Map;
import org.apache.log4j.Logger;
import org.dspace.content.DSpaceObject;
import org.dspace.content.Item;
import org.dspace.doi.CDLDataCiteService;
import org.dspace.doi.DryadDOIRegistrationHelper;
import org.jdom.input.SAXBuilder;
import org.apache.xerces.parsers.SAXParser;
import org.dspace.content.DCValue;
import org.dspace.core.ConfigurationManager;
import org.jdom.Document;
import org.jdom.JDOMException;


/**
 * Generates a report of Dryad items with invalid DataCite XML, resulting in
 * failed DOI Registration.
 * @author Dan Leehr <dan.leehr@nescent.org>
 */
public class DataCiteXMLValidator extends AbstractCurationTask {
    private static final Logger log = Logger.getLogger(DataCiteXMLValidator.class);
    private static final String HEADERS = "\"DOI\",\"Status\",\"Result\",\"Detail\"";
    private static SAXBuilder builder = new SAXBuilder("org.apache.xerces.parsers.SAXParser", true);
    private void report(Item item, Integer status, String result, String detail) {
        //TODO: Do something with the result.
        // DOI, status code, result, detail
        DCValue[] dcdoi = item.getMetadata("dc.identifier");
        String doi = "";
        if (dcdoi.length > 0) {
            doi = dcdoi[0].value;
        }

        String message = String.format("\"%s\",%d,\"%s\",\"%s\"",
                doi,
                status,
                result,
                detail);
        report(message);
    }

    @Override
    public int perform(DSpaceObject dso) throws IOException {
        builder.setFeature("http://apache.org/xml/features/validation/schema", true);
        // This is stored locally to keep the validation quick.
        // If we change the datacite schema we must change this as well.
        String xsdFilePath = ConfigurationManager.getProperty("dspace.dir") + "/config/external-schema/datacite-kernel-2.2/metadata.xsd";
        builder.setProperty("http://apache.org/xml/properties/schema/external-schemaLocation",
                "http://datacite.org/schema/kernel-2.2 file://" + xsdFilePath);
        report(HEADERS);
        distribute(dso);
        return Curator.CURATE_SUCCESS;
    }

    /**
     * Check an individual item and report its XML validation status
     * @param item
     * @throws SQLException
     * @throws IOException
     */
    @Override
    protected void performItem(Item item) throws SQLException, IOException {
        // Get the item
        // Determine if it should be registered
        Boolean shouldCheck = (
                item.isArchived() ||
                DryadDOIRegistrationHelper.isDataPackageInPublicationBlackout(item)
                );
        if(shouldCheck == false) {
            report(item, Curator.CURATE_SKIP, "not archived or in blackout",null);
            return;
        }
        // crosswalk it to get xml
        Map<String,String> metadata = CDLDataCiteService.createMetadataListXML(item);
        // get doi and target?
        String dataCiteXmlMetadata = metadata.get(CDLDataCiteService.DATACITE);
        // validate xml against datacite schema
        // from http://www.jdom.org/docs/faq.html#a0370
        try {
            Document doc = builder.build(dataCiteXmlMetadata);
            report(item, Curator.CURATE_SUCCESS, "Validated", null);
        } catch (JDOMException ex) {
            // There was an exception validating
            String cause = ex.getCause().getMessage();
            report(item, Curator.CURATE_FAIL, "Failed", cause);
        }
    }

}

@@ -43,27 +43,92 @@
 java.io.
-IOException
+ByteArrayInputStream;%0Aimport java.io.IOException;%0Aimport java.io.InputStream
 ;%0Aimport
@@ -3308,16 +3308,110 @@
   try %7B%0A
+            InputStream in = new ByteArrayInputStream(dataCiteXmlMetadata.getBytes(%22UTF-8%22));%0A
         
@@ -3443,35 +3443,18 @@
 r.build(
-dataCiteXmlMetadata
+in
 );%0A     

Read document from an inputstream