/*
 * $Id$
 */

/*

 Copyright (c) 2000-2017 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.clockss.gigascience;

import org.lockss.daemon.PluginException;
import org.lockss.extractor.ArticleMetadata;
import org.lockss.extractor.FileMetadataExtractor;
import org.lockss.extractor.MetadataTarget;
import org.lockss.plugin.CachedUrl;
import org.lockss.plugin.clockss.CrossRefSchemaHelper;
import org.lockss.plugin.clockss.SourceXmlMetadataExtractorFactory;
import org.lockss.plugin.clockss.SourceXmlSchemaHelper;
import org.lockss.util.Logger;

import java.util.ArrayList;
import java.util.List;


public class GigaScienceXmlMetadataExtractorFactory extends SourceXmlMetadataExtractorFactory {
  private static final Logger log = Logger.getLogger(GigaScienceXmlMetadataExtractorFactory.class);
  
  private static SourceXmlSchemaHelper CrossRefHelper = null;
  
  @Override
  public FileMetadataExtractor createFileMetadataExtractor(MetadataTarget target,
      String contentType)
          throws PluginException {
    return new SciPostXmlMetadataExtractor();
  }

  public class SciPostXmlMetadataExtractor extends SourceXmlMetadataExtractor {

    @Override
    protected SourceXmlSchemaHelper setUpSchema(CachedUrl cu) {
      // Once you have it, just keep returning the same one. It won't change.
      if (CrossRefHelper != null) {
        return CrossRefHelper;
      }
      CrossRefHelper = new CrossRefSchemaHelper();
      return CrossRefHelper;
    }

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

      // XML: SciPostPhys_1_1_003_Crossref.xml
      // PDF: SciPostPhys_1_1_003.pdf
      String url_string = cu.getUrl();
      String pdfName = url_string.substring(0,url_string.length() - 13) + ".pdf";
      log.debug3("pdfName is " + pdfName);
      List<String> returnList = new ArrayList<String>();
      returnList.add(pdfName);
      return returnList;
    }
    
    @Override
    protected void postCookProcess(SourceXmlSchemaHelper schemaHelper, 
        CachedUrl cu, ArticleMetadata thisAM) {
      log.debug("in SciPost postcook");
    }

  }
}

@@ -1433,16 +1433,48 @@
 ience;%0A%0A
+import org.lockss.config.TdbAu;%0A
 import o
@@ -1507,16 +1507,16 @@
 eption;%0A
-
 import o
@@ -1603,16 +1603,59 @@
 ractor;%0A
+import org.lockss.extractor.MetadataField;%0A
 import o
@@ -1726,16 +1726,16 @@
 hedUrl;%0A
-
 import o
@@ -1773,32 +1773,93 @@
 efSchemaHelper;%0A
+import org.lockss.plugin.clockss.JatsPublishingSchemaHelper;%0A
 import org.locks
@@ -3081,93 +3081,8 @@
  %7B%0A%0A
-      // XML: SciPostPhys_1_1_003_Crossref.xml%0A      // PDF: SciPostPhys_1_1_003.pdf%0A
     
@@ -3112,24 +3112,24 @@
 u.getUrl();%0A
+
       String
@@ -3360,21 +3360,17 @@
 ;%0A    %7D%0A
-    
 %0A
+
     @Ove
@@ -3445,18 +3445,44 @@
 aHelper,
- 
 %0A
+                           
         
@@ -3525,45 +3525,258 @@
 ) %7B%0A
+%0A
       
-log.debug(%22in SciPost postcook%22
+String publisherName = %22Giga Science%22;%0A%0A      TdbAu tdbau = cu.getArchivalUnit().getTdbAu();%0A      if (tdbau != null) %7B%0A          publisherName =  tdbau.getPublisherName();%0A      %7D%0A%0A      thisAM.put(MetadataField.FIELD_PUBLISHER, publisherName
 );%0A 
@@ -3780,15 +3780,14 @@
 ;%0A    %7D%0A
-%0A
   %7D%0A%7D%0A

Read publisher name from tdb file