/*
 * $Id:$
 */

/*

 Copyright (c) 2000-2018 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.bioone;

import org.lockss.config.TdbAu;
import org.lockss.daemon.PluginException;
import org.lockss.daemon.ShouldNotHappenException;
import org.lockss.extractor.ArticleMetadata;
import org.lockss.extractor.FileMetadataExtractor;
import org.lockss.extractor.MetadataField;
import org.lockss.extractor.MetadataTarget;
import org.lockss.plugin.CachedUrl;
import org.lockss.plugin.clockss.JatsPublishingSchemaHelper;
import org.lockss.plugin.clockss.PubMedSchemaHelper;
import org.lockss.plugin.clockss.SourceXmlMetadataExtractorFactory;
import org.lockss.plugin.clockss.SourceXmlSchemaHelper;
import org.lockss.util.Logger;
import org.w3c.dom.Document;

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

public class BioOneMetadataExtractorFactory extends SourceXmlMetadataExtractorFactory {
	static Logger log = Logger.getLogger(BioOneMetadataExtractorFactory.class);

	private static SourceXmlSchemaHelper JatsPublishingHelper = null;

	@Override
	public FileMetadataExtractor createFileMetadataExtractor(MetadataTarget target,
															 String contentType)
			throws PluginException {
		return new JatsPublishingSourceXmlMetadataExtractor();
	}

	public class JatsPublishingSourceXmlMetadataExtractor extends SourceXmlMetadataExtractor {

		/*
		 * This setUpSchema shouldn't be called directly
		 * but for safety, just use the CU to figure out which schema to use.
		 *
		 */
		@Override
		protected SourceXmlSchemaHelper setUpSchema(CachedUrl cu) {
			throw new ShouldNotHappenException("This version of the schema setup cannot be used for this plugin");
		}

		@Override
		protected SourceXmlSchemaHelper setUpSchema(CachedUrl cu, Document xmlDoc) {
			String url = cu.getUrl();
			log.debug3("Setup Jats schema helper for url " + url);
			if (JatsPublishingHelper == null) {
				JatsPublishingHelper = new JatsPublishingSchemaHelper();
			}
			return JatsPublishingHelper;
		}

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

			List<String> returnList = new ArrayList<String>();

			String publisherName = "BioOne";

			TdbAu tdbau = cu.getArchivalUnit().getTdbAu();
			if (tdbau != null) {
				publisherName =  tdbau.getPublisherName();
			}

			oneAM.put(MetadataField.FIELD_PUBLISHER, publisherName);

			return returnList;
		}
		*/
		
		@Override
		protected void postCookProcess(SourceXmlSchemaHelper schemaHelper,
									   CachedUrl cu, ArticleMetadata thisAM) {

			//If we didn't get a valid date value, use the copyright year if it's there
			if (thisAM.get(MetadataField.FIELD_DATE) == null) {
				if (thisAM.getRaw(JatsPublishingSchemaHelper.JATS_date) != null) {
					thisAM.put(MetadataField.FIELD_DATE, thisAM.getRaw(JatsPublishingSchemaHelper.JATS_date));
				} else {// last chance
					thisAM.put(MetadataField.FIELD_DATE, thisAM.getRaw(JatsPublishingSchemaHelper.JATS_edate));
				}
			}
		}

	}
}

@@ -3314,481 +3314,8 @@
 %0A%09%09%7D
-%0A%0A%09%09/*%0A%09%09@Override%0A%09%09protected List%3CString%3E getFilenamesAssociatedWithRecord(SourceXmlSchemaHelper helper, CachedUrl cu,%0A%09%09%09%09%09%09%09%09%09%09%09%09%09%09%09%09ArticleMetadata oneAM) %7B%0A%0A%09%09%09List%3CString%3E returnList = new ArrayList%3CString%3E();%0A%0A%09%09%09String publisherName = %22BioOne%22;%0A%0A%09%09%09TdbAu tdbau = cu.getArchivalUnit().getTdbAu();%0A%09%09%09if (tdbau != null) %7B%0A%09%09%09%09publisherName =  tdbau.getPublisherName();%0A%09%09%09%7D%0A%0A%09%09%09oneAM.put(MetadataField.FIELD_PUBLISHER, publisherName);%0A%0A%09%09%09return returnList;%0A%09%09%7D%0A%09%09*/
 %0A%09%09%0A
@@ -3883,16 +3883,245 @@
 %09%09%7D%0A%09%09%09%7D
+%0A%0A%09%09%09String publisherName = %22BioOne%22;%0A%0A%09%09%09TdbAu tdbau = cu.getArchivalUnit().getTdbAu();%0A%09%09%09if (tdbau != null) %7B%0A%09%09%09%09publisherName =  tdbau.getPublisherName();%0A%09%09%09%7D%0A%09%09%09%0A%09%09%09thisAM.put(MetadataField.FIELD_PUBLISHER, publisherName);
 %0A%09%09%7D%0A%0A%09%7D

Use publisher name in tdb file