<commit_before>package org.openregistry.service;

import org.openregistry.core.service.*;
import org.openregistry.core.service.identifier.IdentifierAssigner;
import org.openregistry.core.service.identifier.IdentifierGenerator;
import org.openregistry.core.service.reconciliation.ReconciliationResult;
import org.openregistry.core.service.reconciliation.Reconciler;
import org.openregistry.core.domain.Person;
import org.openregistry.core.domain.Role;
import org.openregistry.core.domain.Name;
import org.openregistry.core.domain.sor.SorPerson;
import org.openregistry.core.domain.sor.PersonSearch;
import org.openregistry.core.repository.PersonRepository;
import org.openregistry.service.validator.RoleValidator;
import org.openregistry.service.validator.PersonValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.javalid.core.AnnotationValidator;
import org.javalid.core.ValidationMessage;

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

/**
 * Default implementation of the {@link PersonService}.
 *
 * @author <NAME>
 * @author <NAME>
 * @since 1.0.0
 */
@Service
public class DefaultPersonService implements PersonService {

    @Autowired(required = true)
    private PersonRepository personRepository;

    @Autowired(required = true)
    private AnnotationValidator annotationValidator;

    @Autowired(required=true)
    private RoleValidator roleValidator;

    @Autowired(required=true)
    private PersonValidator personValidator;

    @Autowired(required=true)
    private Reconciler reconciler;

    @Autowired(required=true)
    private ObjectFactory<Person> personObjectFactory;

    @Autowired(required=false)
    private List<IdentifierAssigner> identifierAssigners = new ArrayList<IdentifierAssigner>();

    @Autowired(required=true)
    private IdentifierGenerator identifierGenerator;

    public Person findPersonById(final Long id) {
        return this.personRepository.findByInternalId(id);
    }

    @Transactional
    public ServiceExecutionResult validateAndSaveRoleForPerson(final Person person, final Role role) {
        final String serviceName = "PersonService.validateAndSaveRoleForPerson";
        // final Errors errors = new DataBinder(role, "role").getBindingResult();

        final List<ValidationError> validationErrors = validateAndConvert(role);
        // this.roleValidator.validate(role, errors);

        if (!validationErrors.isEmpty()) {
            return new DefaultServiceExecutionResult(serviceName, role, validationErrors);
        }

        this.personRepository.savePerson(person);
        return new DefaultServiceExecutionResult(serviceName, role);
    }

    @Transactional
    public ServiceExecutionResult validateAndSavePerson(final Person person) {
        final String serviceName = "PersonService.validateAndSaveRoleForPerson";
        // final Errors errors = new DataBinder(person, "person").getBindingResult();

        final List<ValidationError> validationErrors = validateAndConvert(person);

        //this.personValidator.validate(person, errors);

        if (!validationErrors.isEmpty()) {
            return new DefaultServiceExecutionResult(serviceName, person, validationErrors);
        }

        // TODO Call to reconciliation code and save code.
        this.personRepository.savePerson(person);
        return new DefaultServiceExecutionResult(serviceName, person);
    }

    @Transactional
    public ServiceExecutionResult addPerson(final PersonSearch personSearch, final ReconciliationResult oldReconciliationResult) {
        final List<ValidationError> validationErrors = validateAndConvert(personSearch);
        final String serviceName = "PersonService.addPerson";

        if (!validationErrors.isEmpty()) {
            return new DefaultServiceExecutionResult(serviceName, personSearch, validationErrors);
        }


        if (oldReconciliationResult != null) {
            final ReconciliationResult result = this.reconciler.reconcile(personSearch);

            if (result.getReconciliationType() == ReconciliationResult.ReconciliationType.NONE) {
                return new DefaultServiceExecutionResult(serviceName, magic(personSearch));
            }

            return new DefaultServiceExecutionResult(serviceName, personSearch, result);
        }

        return new DefaultServiceExecutionResult(serviceName, magic(personSearch));
    }

    protected List<ValidationError> validateAndConvert(final Object object) {
        final List<ValidationMessage> validationMessages = this.annotationValidator.validateObject(object);
        return convertToValidationErrors(validationMessages);
    }

    protected List<ValidationError> convertToValidationErrors(final List<ValidationMessage> messages) {
        final List<ValidationError> validationErrors = new ArrayList<ValidationError>();

        for (final ValidationMessage message : messages) {
            validationErrors.add(new JaValidValidationError(message));
        }

        return validationErrors;
    }

    protected Person magic(final PersonSearch personSearch) {
        final SorPerson sorPerson = personSearch.getPerson();
<commit_msg>NOJIRA<commit_after>package org.openregistry.service;

import org.openregistry.core.service.*;
import org.openregistry.core.service.identifier.IdentifierAssigner;
import org.openregistry.core.service.identifier.IdentifierGenerator;
import org.openregistry.core.service.reconciliation.ReconciliationResult;
import org.openregistry.core.service.reconciliation.Reconciler;
import org.openregistry.core.domain.Person;
import org.openregistry.core.domain.Role;
import org.openregistry.core.domain.Name;
import org.openregistry.core.domain.sor.SorPerson;
import org.openregistry.core.domain.sor.PersonSearch;
import org.openregistry.core.repository.PersonRepository;
import org.openregistry.service.validator.RoleValidator;
import org.openregistry.service.validator.PersonValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.javalid.core.AnnotationValidator;
import org.javalid.core.ValidationMessage;

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

/**
 * Default implementation of the {@link PersonService}.
 *
 * @author <NAME>
 * @author <NAME>
 * @since 1.0.0
 */
@Service
public class DefaultPersonService implements PersonService {

    @Autowired(required = true)
    private PersonRepository personRepository;

    @Autowired(required = true)
    private AnnotationValidator annotationValidator;

    @Autowired(required=true)
    private RoleValidator roleValidator;

    @Autowired(required=true)
    private PersonValidator personValidator;

    @Autowired(required=true)
    private Reconciler reconciler;

    @Autowired(required=true)
    private ObjectFactory<Person> personObjectFactory;

    @Autowired(required=false)
    private List<IdentifierAssigner> identifierAssigners = new ArrayList<IdentifierAssigner>();

    @Autowired(required=true)
    private IdentifierGenerator identifierGenerator;

    public Person findPersonById(final Long id) {
        return this.personRepository.findByInternalId(id);
    }

    @Transactional
    public ServiceExecutionResult validateAndSaveRoleForPerson(final Person person, final Role role) {
        final String serviceName = "PersonService.validateAndSaveRoleForPerson";
        // final Errors errors = new DataBinder(role, "role").getBindingResult();

        final List<ValidationError> validationErrors = validateAndConvert(role);
        // this.roleValidator.validate(role, errors);

        if (!validationErrors.isEmpty()) {
            return new DefaultServiceExecutionResult(serviceName, role, validationErrors);
        }

        this.personRepository.savePerson(person);
        return new DefaultServiceExecutionResult(serviceName, role);
    }

    @Transactional
    public ServiceExecutionResult validateAndSavePerson(final Person person) {
        final String serviceName = "PersonService.validateAndSaveRoleForPerson";
        // final Errors errors = new DataBinder(person, "person").getBindingResult();

        final List<ValidationError> validationErrors = validateAndConvert(person);

        //this.personValidator.validate(person, errors);

        if (!validationErrors.isEmpty()) {
            return new DefaultServiceExecutionResult(serviceName, person, validationErrors);
        }

        // TODO Call to reconciliation code and save code.
        this.personRepository.savePerson(person);
        return new DefaultServiceExecutionResult(serviceName, person);
    }

    @Transactional
    public ServiceExecutionResult addPerson(final PersonSearch personSearch, final ReconciliationResult oldReconciliationResult) {
        final List<ValidationError> validationErrors = validateAndConvert(personSearch);
        final String serviceName = "PersonService.addPerson";

        if (!validationErrors.isEmpty()) {
            return new DefaultServiceExecutionResult(serviceName, personSearch, validationErrors);
        }


        if (oldReconciliationResult != null) {
            final ReconciliationResult result = this.reconciler.reconcile(personSearch);

            if (result.getReconciliationType() == ReconciliationResult.ReconciliationType.NONE) {
                return new DefaultServiceExecutionResult(serviceName, magic(personSearch));
            }

            return new DefaultServiceExecutionResult(serviceName, personSearch, result);
        }

        return new DefaultServiceExecutionResult(serviceName, magic(personSearch));
    }

    /**
     * Validates the object using the JaValid annotation framework and then converts the errors into the OpenRegistry API for errors.
     *
     * @param object the object to check
     * @return the list of validation errors.  CANNOT be NULL.  Can be empty.
     */
    protected List<ValidationError> validateAndConvert(final Object object) {
        final List<ValidationMessage> validationMessages = this.annotationValidator.validateObject(object, null, "", true, -1);
        return convertToValidationErrors(validationMessages);
    }

    /**
     * Converts the validation errors from JaValid into the OpenRegistry API validation errors.
     *
     * @param messages the JaValid messages to convert.
     * @return the list of validation errors.  CANNOT be null.  CAN be empty.
     */
    protected List<ValidationError> convertToValidationErrors(final List<ValidationMessage> messages) {
        final List<ValidationError> validationErrors = new ArrayList<ValidationError>();

        for (final ValidationMessage message : messages) {
            validationErrors.add(new JaValidValidationError(message));
        }

        return validationErrors;
    }

    /**
     * Current workflow for converting an SorPerson into the actual Person.
     * 
     * @param personSearch the original search criteria.
     * @return the newly saved Person.
     */
    protected Person magic(final PersonSearch personSearch) {
        final SorPerson sorPerson = personSearch.getPerson();

{'fraction_non_alphanumeric': 0.08164689462665736, 'fraction_numerical': 0.0006106071179344033, 'mean_word_length': 4.18543645409317, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 31, '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': '4674613', 'n_tokens_mistral': 2779, 'n_tokens_neox': 2743, 'n_words': 638}