/*
 * $Id:$
 */

/*

Copyright (c) 2019 Board of Trustees of Leland Stanford Jr. University,
all rights reserved.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
STANFORD UNIVERSITY BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Except as contained in this notice, the name of Stanford University shall not
be used in advertising or otherwise to promote the sale, use or other dealings
in this Software without prior written authorization from Stanford University.

*/

package org.lockss.plugin.pensoft.oai;

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

import org.lockss.daemon.*;
import org.lockss.extractor.ArticleMetadataExtractor;
import org.lockss.extractor.ArticleMetadataExtractorFactory;
import org.lockss.extractor.BaseArticleMetadataExtractor;
import org.lockss.extractor.MetadataTarget;
import org.lockss.plugin.*;
import org.lockss.util.Logger;
import org.lockss.util.UrlUtil;

public class PensoftOaiArticleIteratorFactory
implements ArticleIteratorFactory,
           ArticleMetadataExtractorFactory {

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

  protected static final Pattern ABSTRACT_PATTERN = Pattern.compile(
      "article(?:/|s[.]php[?]id=)([0-9]+)/?$",
      Pattern.CASE_INSENSITIVE);

  //Some are full text HTML and some are abstracts
  //Abstract = http://zookeys.pensoft.net/articles.php?id=1929
  //Full Text = http://bdj.pensoft.net/articles.php?id=995
  protected static final String ABSTRACT_0_REPLACEMENT = "articles.php?id=$1";
  protected static final String ABSTRACT_1_REPLACEMENT = "article/$1";
  //http://zookeys.pensoft.net/lib/ajax_srv/article_elements_srv.php?action=download_pdf&item_id=1929
  //protected static final String PDF_REPLACEMENT 
  protected static final String PDF_0_REPLACEMENT = "lib/ajax_srv/article_elements_srv.php?action=download_pdf&item_id=$1";
  protected static final String PDF_1_REPLACEMENT = "article/$1/download/pdf/";
  
  @Override
  public Iterator<ArticleFiles> createArticleIterator(ArchivalUnit au, MetadataTarget target) 
      throws PluginException {
    SubTreeArticleIteratorBuilder builder = new SubTreeArticleIteratorBuilder(au);

    String strip_base = UrlUtil.stripProtocol(au.getConfiguration().get(ConfigParamDescr.BASE_URL.getKey()));
    // params from tdb file corresponding to AU
    final String ROOT_TEMPLATE0 = "\"https://" + strip_base + "\"";
    final String ROOT_TEMPLATE1 = "\"http://" + strip_base + "\"";

    final String PATTERN_TEMPLATE =
        "\"^https?://" + strip_base + "article(/|s[.]php[?]id=)[0-9]+/?$\"";

    builder.setSpec(target,
        Arrays.asList(ROOT_TEMPLATE0, ROOT_TEMPLATE1),
        PATTERN_TEMPLATE, Pattern.CASE_INSENSITIVE);

    builder.addAspect(ABSTRACT_PATTERN,
        Arrays.asList(ABSTRACT_0_REPLACEMENT, ABSTRACT_1_REPLACEMENT),
        ArticleFiles.ROLE_ABSTRACT, 
        ArticleFiles.ROLE_ARTICLE_METADATA);

    builder.addAspect(
        Arrays.asList(PDF_1_REPLACEMENT, PDF_0_REPLACEMENT),
        ArticleFiles.ROLE_FULL_TEXT_PDF);

    builder.setFullTextFromRoles(
        ArticleFiles.ROLE_FULL_TEXT_PDF,
        ArticleFiles.ROLE_ABSTRACT);

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

}

@@ -2192,24 +2192,163 @@
 NSENSITIVE);
+%0A  protected static final Pattern PDF_PATTERN = Pattern.compile(%0A      %22article/(%5B0-9%5D+)/download/pdf/?$%22,%0A      Pattern.CASE_INSENSITIVE);
 %0A%0A  //Some a
@@ -2766,59 +2766,8 @@
 929%0A
-  //protected static final String PDF_REPLACEMENT %0A
   pr
@@ -3305,192 +3305,8 @@
 ));%0A
-    // params from tdb file corresponding to AU%0A    final String ROOT_TEMPLATE0 = %22%5C%22https://%22 + strip_base + %22%5C%22%22;%0A    final String ROOT_TEMPLATE1 = %22%5C%22http://%22 + strip_base + %22%5C%22%22;%0A%0A
     
@@ -3384,16 +3384,18 @@
 article(
+?:
 /%7Cs%5B.%5Dph
@@ -3406,22 +3406,40 @@
 id=)
+(
 %5B0-9%5D+
+)(/download/pdf)?
 /?$%5C%22%22;%0A
 %0A   
@@ -3434,17 +3434,16 @@
 /?$%5C%22%22;%0A
-%0A
     buil
@@ -3458,79 +3458,97 @@
 pec(
-target,%0A        Arrays.asList(ROOT_TEMPLATE0, ROOT_TEMPLATE1),%0A        
+new SubTreeArticleIterator.Spec()%0A        .setTarget(target)%0A        .setPatternTemplate(
 PATT
@@ -3582,24 +3582,25 @@
 INSENSITIVE)
+)
 ;%0A%0A    build
@@ -3806,16 +3806,28 @@
 dAspect(
+PDF_PATTERN,
 %0A       

remove ROOT_TEMPLATE