package org.lockss.plugin.clockss.eastview;

import org.lockss.daemon.PluginException;
import org.lockss.extractor.ArticleMetadataExtractor;
import org.lockss.extractor.ArticleMetadataExtractorFactory;
import org.lockss.extractor.BaseArticleMetadataExtractor;
import org.lockss.extractor.MetadataTarget;
import org.lockss.plugin.ArchivalUnit;
import org.lockss.plugin.ArticleFiles;
import org.lockss.plugin.ArticleIteratorFactory;
import org.lockss.plugin.SubTreeArticleIteratorBuilder;
import org.lockss.util.Logger;

import java.util.Iterator;
import java.util.regex.Pattern;

public class EastviewBookSourceXmlArticleIteratorFactory implements ArticleIteratorFactory, ArticleMetadataExtractorFactory {

    protected static Logger log = Logger.getLogger(EastviewBookSourceXmlArticleIteratorFactory.class);

    // Since they will deliver both Ebook and Journal content for the same year
    // The folder structure will look like this:
    //https://clockss-test.lockss.org/sourcefiles/eastview-released/2020/Eastview%20Ebook%20Content/
    //https://clockss-test.lockss.org/sourcefiles/eastview-released/2020/Eastview%20Journal%20Content/

    protected static final String ROOT_TEMPLATE = "\"%s%d\",base_url,year";
    private static final String PATTERN_TEMPLATE = "\"%s%d/([^/]+Ebook[^/]+)\",base_url,year";

    // The delivery does not have one-pdf-to-one-xml matching relationship,
    // All the article metadata is inside articles_xml. However they may update other xmls or even Excel/CVS
    // So we not use PDF count as # of articles as in other plugin
    protected static final Pattern XML_PATTERN = Pattern.compile("/(.*)\\.xml$");
    protected static final String XML_REPLACEMENT = "/$1.xml";


    @Override
    public Iterator<ArticleFiles> createArticleIterator(ArchivalUnit au,
                                                        MetadataTarget target)
            throws PluginException {
        SubTreeArticleIteratorBuilder builder = new SubTreeArticleIteratorBuilder(au);

        builder.setSpec(target,
                ROOT_TEMPLATE,
                PATTERN_TEMPLATE, Pattern.CASE_INSENSITIVE);

        builder.addAspect(XML_PATTERN,
                XML_REPLACEMENT,
                ArticleFiles.ROLE_ARTICLE_METADATA);

        return builder.getSubTreeArticleIterator();
    }


    @Override
    public ArticleMetadataExtractor createArticleMetadataExtractor(MetadataTarget target)
            throws PluginException {
        return new BaseArticleMetadataExtractor(ArticleFiles.ROLE_ARTICLE_METADATA);
    }

}

@@ -1283,21 +1283,18 @@
 /%5D+Ebook
-%5B%5E/%5D+
+.*
 )%5C%22,base
@@ -1441,61 +1441,8 @@
 xml.
- However they may update other xmls or even Excel/CVS
 %0A   

update the pattern a little