<commit_before>package org.emonocot.job.checklist;

import java.lang.reflect.InvocationTargetException;
import java.util.Set;

import javax.validation.ConstraintViolation;
import javax.validation.Path;
import javax.validation.Validator;
import javax.validation.constraints.Max;
import javax.validation.constraints.Size;

import org.apache.commons.beanutils.BeanUtils;
import org.emonocot.api.SourceService;
import org.emonocot.model.common.Annotation;
import org.emonocot.model.common.AnnotationType;
import org.emonocot.model.reference.Reference;
import org.emonocot.model.source.Source;
import org.emonocot.model.taxon.Taxon;
import org.openarchives.pmh.Record;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.core.ExitStatus;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.core.StepExecutionListener;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.core.convert.ConversionException;

/**
 *
 * @author ben
 *
 */
public class OaiPmhRecordValidator implements
        ItemProcessor<Taxon, Taxon>, StepExecutionListener {
    /**
     *
     */
    private Validator validator;

    /**
    *
    */
    private Logger logger
        = LoggerFactory.getLogger(OaiPmhRecordValidator.class);

    /**
     *
     */
    private String sourceName;

    /**
     *
     */
    private SourceService sourceService;

    /**
     *
     */
    private Source source;

    /**
     *
     */
    private StepExecution stepExecution;

    /**
     *
     * @return the source
     */
    private Source getSource() {
        if (source == null) {
            source = sourceService.load(sourceName);
        }
        return source;
    }

    /**
     * @param sourceService Set the source service;
     */
    public final void setSourceService(SourceService sourceService) {
        this.sourceService = sourceService;
    }

    /**
     *
     * @param newSourceName Set the name of the source
     */
    public final void setSourceName(final String newSourceName) {
        this.sourceName = newSourceName;
    }
    /**
     *
     * @param validator Set the validator
     */
    public final void setValidator(Validator validator) {
        this.validator = validator;
    }

    /**
     * @param taxon Set the taxon
     * @return a taxon
     * @throws Exception if there is a problem
     */
    public final Taxon process(final Taxon taxon) throws Exception {
        Set<ConstraintViolation<Taxon>> violations = validator.validate(taxon);
        if (violations.isEmpty()) {
            return taxon;
        } else {
            for (ConstraintViolation<Taxon> violation : violations) {
                Object o = violation.getLeafBean();
                if (o.getClass().equals(Taxon.class)) {
                    ((Taxon) o).getAnnotations().add(
                            addAnnotation("Taxon", violation));
                } else if (o.getClass().equals(Reference.class)) {
                    ((Reference) o).getAnnotations().add(
                            addAnnotation("Reference", violation));
                }

            }
            return taxon;
        }
    }

    /**<commit_msg>Added more logging to harvester to diagnose problem<commit_after>package org.emonocot.job.checklist;

import java.lang.reflect.InvocationTargetException;
import java.util.Set;

import javax.validation.ConstraintViolation;
import javax.validation.Path;
import javax.validation.Validator;
import javax.validation.constraints.Max;
import javax.validation.constraints.Size;

import org.apache.commons.beanutils.BeanUtils;
import org.emonocot.api.SourceService;
import org.emonocot.model.common.Annotation;
import org.emonocot.model.common.AnnotationType;
import org.emonocot.model.reference.Reference;
import org.emonocot.model.source.Source;
import org.emonocot.model.taxon.Taxon;
import org.openarchives.pmh.Record;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.core.ExitStatus;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.core.StepExecutionListener;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.core.convert.ConversionException;

/**
 *
 * @author ben
 *
 */
public class OaiPmhRecordValidator implements
        ItemProcessor<Taxon, Taxon>, StepExecutionListener {
	
    /**
     *
     */
    private Validator validator;

    /**
    *
    */
    private Logger logger
        = LoggerFactory.getLogger(OaiPmhRecordValidator.class);

    /**
     *
     */
    private String sourceName;

    /**
     *
     */
    private SourceService sourceService;

    /**
     *
     */
    private Source source;

    /**
     *
     */
    private StepExecution stepExecution;

    /**
     *
     * @return the source
     */
    private Source getSource() {
        if (source == null) {
            source = sourceService.load(sourceName);
        }
        return source;
    }

    /**
     * @param sourceService Set the source service;
     */
    public final void setSourceService(SourceService sourceService) {
        this.sourceService = sourceService;
    }

    /**
     *
     * @param newSourceName Set the name of the source
     */
    public final void setSourceName(final String newSourceName) {
        this.sourceName = newSourceName;
    }
    /**
     *
     * @param validator Set the validator
     */
    public final void setValidator(Validator validator) {
        this.validator = validator;
    }

    /**
     * @param taxon Set the taxon
     * @return a taxon
     * @throws Exception if there is a problem
     */
    public final Taxon process(final Taxon taxon) throws Exception {
        Set<ConstraintViolation<Taxon>> violations = validator.validate(taxon);
        if (violations.isEmpty()) {
        	logger.debug(taxon.getName() + " is valid");
        	if(taxon.getAuthorship() != null) {
        		logger.debug(taxon.getAuthorship() + " (" + taxon.getAuthorship().length() + ") is valid");
        	}
            return taxon;
        } else {
        	logger.debug(taxon.getName() + " is not valid");
        	if(taxon.getAuthorship() != null) {
        		logger.debug(taxon.getAuthorship() + " (" + taxon.getAuthorship().length() + ") is valid?");
        	}
            for (ConstraintViolation<org.emonocot.job.taxonmatch.Taxon> violation : violations) {
            	logger.debug(violation.getMessage());
                Object o = violation.getLeafBean();
                if (o.getClass().equals(Taxon.class)) {
                    ((Taxon) o).getAnnotations().add(
                            addAnnotation("Taxon", violation));
                } else if (o.getClass().equals(Reference.class)) {
                    ((Reference) o).getAnnotations().add(
                            addAnnotation("Reference", violation));
                }

            }
            return taxon;
        }
    }

    /**
{'fraction_non_alphanumeric': 0.10891938250428816, 'fraction_numerical': 0.0005717552887364208, 'mean_word_length': 2.3850991775520076, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7063311', 'n_tokens_mistral': 1969, 'n_tokens_neox': 1933, 'n_words': 389}