/*
 * $Id: GeorgThiemeVerlagArticleIteratorFactory.java,v 1.7 2014-02-28 20:08:13 etenbrink Exp $
 */

/*

Copyright (c) 2000-2014 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.georgthiemeverlag;

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

import org.lockss.daemon.*;
import org.lockss.extractor.*;
import org.lockss.plugin.*;
import org.lockss.util.Logger;


public class GeorgThiemeVerlagArticleIteratorFactory
    implements ArticleIteratorFactory,
               ArticleMetadataExtractorFactory {

  protected static Logger log = Logger.getLogger(GeorgThiemeVerlagArticleIteratorFactory.class);
  
  protected static final String ROOT_TEMPLATE =
      "\"%s\", base_url";
  
  protected static final String PATTERN_TEMPLATE =
      "\"^%sejournals/(?:html|pdf)/10[.][0-9a-z]{4,6}/[^/?&.]+(?:[.]pdf|[?]issue=[^&]+)?$\"," +
      " base_url";
  
  // various aspects of an article
  // https://www.thieme-connect.de/ejournals/html/10.1055/s-0029-1214947
  // https://www.thieme-connect.de/ejournals/pdf/10.1055/s-0029-1214947.pdf
  // https://www.thieme-connect.de/ejournals/abstract/10.1055/s-0029-1214947
  
  // Identify groups in the pattern "/(html|pdf)(<doi>)(.pdf)?
  protected static final Pattern HTML_PATTERN = Pattern.compile(
      "/html/([^/]+/[^/?&]+)$",
      Pattern.CASE_INSENSITIVE);
  
  protected static final Pattern PDF_PATTERN = Pattern.compile(
      "/pdf/([^/]+/[^/?&.]+)[.]pdf$",
      Pattern.CASE_INSENSITIVE);
  
  // how to change from one form (aspect) of article to another
  protected static final String HTML_REPLACEMENT = "/html/$1";
  protected static final String PDF_REPLACEMENT = "/pdf/$1.pdf";
  protected static final String ABSTRACT_REPLACEMENT = "/abstract/$1";
  protected static final String RIS_REPLACEMENT = "/ris/$1/BIB";
  
  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);
    
    // set up fulltext, pdf, abstract to be aspects that will trigger an ArticleFiles
    // NOTE - for the moment this also means it is considered a FULL_TEXT_CU
    // until this fulltext concept is deprecated
    builder.addAspect(
        HTML_PATTERN, HTML_REPLACEMENT,
        ArticleFiles.ROLE_FULL_TEXT_HTML);
    
    builder.addAspect(
        PDF_PATTERN, PDF_REPLACEMENT,
        ArticleFiles.ROLE_FULL_TEXT_PDF);
    
    builder.addAspect(
        ABSTRACT_REPLACEMENT,
        ArticleFiles.ROLE_ABSTRACT);
    
    builder.addAspect(
        RIS_REPLACEMENT,
        ArticleFiles.ROLE_CITATION_RIS);
    
    // add metadata role from abstract, html, or pdf (NOTE: pdf metadata gets DOI from filename)
    builder.setRoleFromOtherRoles(ArticleFiles.ROLE_ARTICLE_METADATA, Arrays.asList(
        ArticleFiles.ROLE_ABSTRACT,
        ArticleFiles.ROLE_FULL_TEXT_HTML,
        ArticleFiles.ROLE_FULL_TEXT_PDF));
    
    // The order in which we want to define full_text_cu.
    // First one that exists will get the job
    builder.setFullTextFromRoles(
        ArticleFiles.ROLE_FULL_TEXT_PDF,
        ArticleFiles.ROLE_FULL_TEXT_HTML);
    
    return builder.getSubTreeArticleIterator();
  }
  
  public ArticleMetadataExtractor createArticleMetadataExtractor(MetadataTarget target)
      throws PluginException {
    return new BaseArticleMetadataExtractor(ArticleFiles.ROLE_ARTICLE_METADATA);
  }
  
}

@@ -57,29 +57,29 @@
 v 1.
-7
+8
  2014-0
-2-2
+4-1
 8 20:
-08:13
+26:22
  ete
@@ -3683,20 +3683,19 @@
         
-HTML
+PDF
 _PATTERN
@@ -3696,20 +3696,19 @@
 ATTERN, 
-HTML
+PDF
 _REPLACE
@@ -3741,36 +3741,35 @@
 .ROLE_FULL_TEXT_
-HTML
+PDF
 );%0A    %0A    buil
@@ -3783,35 +3783,36 @@
 Aspect(%0A        
-PDF
+HTML
 _PATTERN, PDF_RE
@@ -3797,35 +3797,36 @@
   HTML_PATTERN, 
-PDF
+HTML
 _REPLACEMENT,%0A  
@@ -3851,35 +3851,36 @@
 .ROLE_FULL_TEXT_
-PDF
+HTML
 );%0A    %0A    buil
@@ -4372,235 +4372,8 @@
    %0A
-    // The order in which we want to define full_text_cu.%0A    // First one that exists will get the job%0A    builder.setFullTextFromRoles(%0A        ArticleFiles.ROLE_FULL_TEXT_PDF,%0A        ArticleFiles.ROLE_FULL_TEXT_HTML);%0A    %0A
     

Remove setFullTextFromRoles call and reorder the aspect definition to accomplish 'correct' order with precedence to PDF