package org.lockss.plugin.clockss.scientificvideoprotocols;

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 ScientificVideoProtocolsRisArticleIteratorFactory implements ArticleIteratorFactory, ArticleMetadataExtractorFactory {

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

    public static final Pattern RIS_PATTERN = Pattern.compile("/([^/]+)\\.ris$", Pattern.CASE_INSENSITIVE);
    public static final String RIS_REPLACEMENT = "/$1.ris";

    public static final Pattern PDF_PATTERN = Pattern.compile("/([^/]+)\\.pdf$", Pattern.CASE_INSENSITIVE);
    public static final String PDF_REPLACEMENT = "/$1.pdf";

    protected static final String PATTERN_TEMPLATE = "\"^%s[^/]+/(.*)\\.(?:ris|pdf)$\",base_url";
    //
    // The source content structure looks like this:
    // <root_location>/<dir>/<possible subdirectories>/<STUFF> where STUFF is a series of files:
    // <name>.pdf, <name>.ris. <name>.ris is used to extract metadata
    @Override
    public Iterator<ArticleFiles> createArticleIterator(ArchivalUnit au,
                                                        MetadataTarget target)
            throws PluginException {
        SubTreeArticleIteratorBuilder builder = new SubTreeArticleIteratorBuilder(au);

        // no need to limit to ROOT_TEMPLATE
        builder.setSpec(builder.newSpec()
                .setTarget(target)
                .setPatternTemplate(PATTERN_TEMPLATE, Pattern.CASE_INSENSITIVE));

        builder.addAspect(RIS_PATTERN,
                RIS_REPLACEMENT,
                ArticleFiles.ROLE_ARTICLE_METADATA);

        builder.addAspect(PDF_PATTERN,
                PDF_REPLACEMENT,
                ArticleFiles.ROLE_FULL_TEXT_PDF);

        builder.setFullTextFromRoles(ArticleFiles.ROLE_FULL_TEXT_PDF);

        return builder.getSubTreeArticleIterator();
    }

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



@@ -2016,19 +2016,19 @@
 dAspect(
-RIS
+PDF
 _PATTERN
@@ -2045,19 +2045,19 @@
         
-RIS
+PDF
 _REPLACE
@@ -2096,32 +2096,29 @@
 es.ROLE_
-ARTICLE_METADATA
+FULL_TEXT_PDF
 );%0A%0A    
@@ -2131,35 +2131,35 @@
 ilder.addAspect(
-PDF
+RIS
 _PATTERN,%0A      
@@ -2160,35 +2160,35 @@
                 
-PDF
+RIS
 _REPLACEMENT,%0A  
@@ -2219,32 +2219,43 @@
 es.ROLE_
-FULL_TEXT_PDF);%0A
+ARTICLE_METADATA);%0A        
 %0A       

make article iterator and metadata extractor work as expected