package org.lockss.plugin.clockss.innovativepublication;

import org.apache.commons.io.FileUtils;
import org.lockss.daemon.PluginException;
import org.lockss.daemon.ShouldNotHappenException;
import org.lockss.extractor.*;
import org.lockss.plugin.ArchivalUnit;
import org.lockss.plugin.CachedUrl;
import org.lockss.plugin.clockss.JatsPublishingSchemaHelper;
import org.lockss.plugin.clockss.SourceXmlMetadataExtractorFactory;
import org.lockss.plugin.clockss.SourceXmlMetadataExtractorTest;
import org.lockss.plugin.clockss.SourceXmlSchemaHelper;
import org.lockss.util.Constants;
import org.lockss.util.Logger;
import org.w3c.dom.Document;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

public class TestInnovativePublicationJatsXmlXPathMetadataExtractor extends SourceXmlMetadataExtractorTest {

    private static final Logger log = Logger.getLogger(TestInnovativePublicationJatsXmlXPathMetadataExtractor.class);

    private static String BaseUrl = "http://source.host.org/sourcefiles/innovativepublication/";
    private static String Directory = "2019_04";

    private static String getXmlFileContent(String fname) {
        String xmlContent = "";
        try {
            String currentDirectory = System.getProperty("user.dir");
            String pathname = currentDirectory +
                    "/plugins/test/src/org/lockss/plugin/clockss/innovativepublication/" + fname;
            xmlContent = FileUtils.readFileToString(new File(pathname), Constants.DEFAULT_ENCODING);


        } catch(IOException e) {
            log.error(e.getMessage(), e);
        }
        return xmlContent;
    }

    public void testExtractArticleXmlSchema() throws Exception {

        String fname = "sample_jats_metadata.xml";
        String journalXml = getXmlFileContent(fname);

        String xml_url = BaseUrl + Directory + "/" + fname;

        FileMetadataExtractor me = new MyInnovativePublicationJatsXmlXPathMetadataExtractory().createFileMetadataExtractor(MetadataTarget.Any(), "text/xml");

        FileMetadataListExtractor mle =
                new FileMetadataListExtractor(me);
        List<ArticleMetadata> mdlist = extractFromContent(xml_url, "text/xml", journalXml, mle);
        assertNotEmpty(mdlist);
        ArticleMetadata md = mdlist.get(0);
        assertNotNull(md);
        assertEquals("2019", md.get(MetadataField.FIELD_DATE));
        assertEquals("2394-6792", md.get(MetadataField.FIELD_ISSN));
        assertEquals("6", md.get(MetadataField.FIELD_VOLUME));
        assertEquals("4", md.get(MetadataField.FIELD_ISSUE));
        assertEquals("Butale, Pradip", md.get(MetadataField.FIELD_AUTHOR));
        assertEquals("Lipid profile in arthritides", md.get(MetadataField.FIELD_ARTICLE_TITLE));

    }

    private class MyInnovativePublicationJatsXmlXPathMetadataExtractory extends SourceXmlMetadataExtractorFactory {

        private  SourceXmlSchemaHelper InnovativeJatsXmlHelper = null;

        @Override
        public FileMetadataExtractor createFileMetadataExtractor(MetadataTarget target,
                                                                 String contentType)
                throws PluginException {
            return new InnovativePublicationMetadataExtractor();
        }

        public class InnovativePublicationMetadataExtractor extends SourceXmlMetadataExtractor {

            @Override
            protected SourceXmlSchemaHelper setUpSchema(CachedUrl cu) {
                throw new ShouldNotHappenException("This version of the schema setup cannot be used for this plugin");
            }

            @Override
            protected SourceXmlSchemaHelper setUpSchema(CachedUrl cu, Document doc) {

                if (InnovativeJatsXmlHelper == null) {
                    InnovativeJatsXmlHelper = new JatsPublishingSchemaHelper();
                }
                return InnovativeJatsXmlHelper;
            }

            @Override
            protected List<String> getFilenamesAssociatedWithRecord(SourceXmlSchemaHelper helper, CachedUrl cu,
                                                                    ArticleMetadata oneAM) {

                String pdfPath = "";
                String url_string = cu.getUrl();
                List<String> returnList = new ArrayList<String>();
                //XML and PDF are located inside the same directory in most cases
                //http://content5.lockss.org/sourcefiles/innovativepublication-released/2019/IJCA/2019/volume%206/issue%204/IJCA%206-4-478-480.pdf
                //http://content5.lockss.org/sourcefiles/innovativepublication-released/2019/IJCA/2019/volume%206/issue%204/IJCA%206-4-481-487.xml
                if (url_string.indexOf(".xml") > -1) {
                    pdfPath = url_string.replace(".xml", ".pdf");
                    ArchivalUnit B_au = cu.getArchivalUnit();
                    CachedUrl fileCu;
                    fileCu = B_au.makeCachedUrl(pdfPath);
                    log.debug3("Check for existence of " + pdfPath);
                    if(fileCu != null && (fileCu.hasContent())) {
                        log.debug3("pdfPath is " + pdfPath);
                        returnList.add(pdfPath);
                    } else {
                        log.debug3("no matching PDF found, use xml file instead " + pdfPath);
                        returnList.add(url_string);
                    }
                }
                return returnList;
            }

            @Override
            protected void postCookProcess(SourceXmlSchemaHelper schemaHelper,
                                           CachedUrl cu, ArticleMetadata thisAM) {

                //If we didn't get a valid date value, use the copyright year if it's there
                if (thisAM.get(MetadataField.FIELD_DATE) == null) {
                    if (thisAM.getRaw(JatsPublishingSchemaHelper.JATS_date) != null) {
                        thisAM.put(MetadataField.FIELD_DATE, thisAM.getRaw(JatsPublishingSchemaHelper.JATS_date));
                    } else {// last chance
                        thisAM.put(MetadataField.FIELD_DATE, thisAM.getRaw(JatsPublishingSchemaHelper.JATS_edate));
                    }
                }
            }
        }
    }
}


@@ -55,48 +55,8 @@
 n;%0A%0A
-import org.apache.commons.io.FileUtils;%0A
 impo
@@ -523,25 +523,22 @@
 ss.util.
-Constants
+IOUtil
 ;%0Aimport
@@ -558,24 +558,59 @@
 til.Logger;%0A
+import org.lockss.util.StringUtil;%0A
 import org.w
@@ -629,30 +629,8 @@
 ent;
-%0A%0Aimport java.io.File;
 %0Aimp
@@ -650,24 +650,52 @@
 OException;%0A
+import java.io.InputStream;%0A
 import java.
@@ -736,40 +736,8 @@
 ist;
-%0Aimport java.util.regex.Pattern;
 %0A%0Apu
@@ -1118,31 +1118,24 @@
     private 
-static 
 String getXm
@@ -1207,132 +1207,53 @@
     
-try %7B%0A            String currentDirectory = System.getProperty(%22user.dir%22);%0A            String pathname = currentDirectory +
+InputStream file_input = null;%0A%0A        try %7B
 %0A   
@@ -1265,92 +1265,47 @@
     
-        %22/plugins/test/src/org/lockss/plugin/clockss/innovativepublication/%22 + 
+file_input = getResourceAsStream(
 fname
+)
 ;%0A  
@@ -1331,85 +1331,89 @@
 t = 
-FileUtils.readFileToString(new File(pathname), Constants.DEFAULT_ENCODING);%0A%0A
+StringUtil.fromInputStream(file_input);%0A            IOUtil.safeClose(file_input);
 %0A   
@@ -1424,16 +1424,17 @@
  %7D catch
+ 
 (IOExcep
@@ -1481,24 +1481,86 @@
 sage(), e);%0A
+        %7D finally %7B%0A            IOUtil.safeClose(file_input);%0A
         %7D%0A  

update test to use getResourceAsStream function