<commit_before>package it.gov.aifa.invoice_processor.runner.impl;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.stream.Collectors;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import it.gov.aifa.invoice_processor.entity.invoice.Invoice;
import it.gov.aifa.invoice_processor.mapping.invoice1_1.Invoice1_1;
import it.gov.aifa.invoice_processor.runner.JsonToDbUploader;
import it.gov.aifa.invoice_processor.service.InvoiceMappingToEntityConverter;
import it.gov.aifa.invoice_processor.service.MappingObjectFactory;
import it.gov.aifa.invoice_processor.service.persistence.InvoiceRepository;

@Service
public class JsonToDbUploaderImpl extends AbstractFilesystemCrawler implements JsonToDbUploader{

	private static final Logger log = LoggerFactory.getLogger(JsonToDbUploaderImpl.class);
	
	private InvoiceMappingToEntityConverter<Invoice1_1, Invoice> invoiceMappingToEntityConverter;
	private InvoiceRepository invoiceRepository;
	private MappingObjectFactory<Invoice1_1> invoiceMappingFactory;
	
	public JsonToDbUploaderImpl(
			InvoiceMappingToEntityConverter<Invoice1_1, Invoice> invoiceMappingToEntityConverter,
			InvoiceRepository invoiceRepository,
			MappingObjectFactory<Invoice1_1> invoiceMappingFactory) {
		this.invoiceMappingToEntityConverter = invoiceMappingToEntityConverter;
		this.invoiceRepository = invoiceRepository;
		this.invoiceMappingFactory = invoiceMappingFactory;
	}

	@Override
	protected void actionOnFile(final Path file) {
		String filePath = file.toUri().getPath();
		StringBuilder message = new StringBuilder();
		String fileContents;
		try {
			fileContents = Files.lines(file).collect(Collectors.joining());
		} catch (IOException e) {
			message.append("Cannot load the contents of ");
			message.append(filePath);
			log.error(message.toString(), e);
			return;
		}
		Invoice1_1 invoiceMapping;
		try {
			invoiceMapping = invoiceMappingFactory.buildFromJson(fileContents, Invoice1_1.class);
		} catch (Exception e) {
			message.append("Error creating a mapping object for ");
			message.append(filePath);
			log.error(message.toString(), e);
			return;
		}
		
		String invoiceId;
		
		try {
			invoiceId = invoiceMapping.getId();
		} catch (NullPointerException e) {
			message.append(e.getMessage());
			message.append("Error while searching the id of ");
			message.append(filePath);
			log.error(message.toString());
			return;
		}
		
		if(!invoiceRepository.exists(invoiceId)){
			message.append("Processing ");
			message.append(filePath);
			Invoice invoice = invoiceMappingToEntityConverter.convert(invoiceMapping);
			invoiceRepository.save(invoice);
		}else{
			message.append("Skipping ");
			message.append(filePath);
			message.append(" because it was already processed");
		}
		log.info(message.toString());
	}
}
<commit_msg>Do not crash if there is an exception while creating entities<commit_after>package it.gov.aifa.invoice_processor.runner.impl;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.stream.Collectors;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import it.gov.aifa.invoice_processor.entity.invoice.Invoice;
import it.gov.aifa.invoice_processor.mapping.invoice1_1.Invoice1_1;
import it.gov.aifa.invoice_processor.runner.JsonToDbUploader;
import it.gov.aifa.invoice_processor.service.InvoiceMappingToEntityConverter;
import it.gov.aifa.invoice_processor.service.MappingObjectFactory;
import it.gov.aifa.invoice_processor.service.persistence.InvoiceRepository;

@Service
public class JsonToDbUploaderImpl extends AbstractFilesystemCrawler implements JsonToDbUploader{

	private static final Logger log = LoggerFactory.getLogger(JsonToDbUploaderImpl.class);

	private InvoiceMappingToEntityConverter<Invoice1_1, Invoice> invoiceMappingToEntityConverter;
	private InvoiceRepository invoiceRepository;
	private MappingObjectFactory<Invoice1_1> invoiceMappingFactory;

	public JsonToDbUploaderImpl(
			InvoiceMappingToEntityConverter<Invoice1_1, Invoice> invoiceMappingToEntityConverter,
			InvoiceRepository invoiceRepository,
			MappingObjectFactory<Invoice1_1> invoiceMappingFactory) {
		this.invoiceMappingToEntityConverter = invoiceMappingToEntityConverter;
		this.invoiceRepository = invoiceRepository;
		this.invoiceMappingFactory = invoiceMappingFactory;
	}

	@Override
	protected void actionOnFile(final Path file) {
		String filePath = file.toUri().getPath();
		log.info("Processing {}", filePath);
		
		String fileContents;
		try {
			fileContents = Files.lines(file).collect(Collectors.joining());
		} catch (IOException e) {
			StringBuilder message = new StringBuilder();
			message.append("Cannot load the contents of ");
			message.append(filePath);
			log.error(message.toString(), e);
			return;
		}
		
		Invoice1_1 invoiceMapping;
		try {
			invoiceMapping = invoiceMappingFactory.buildFromJson(fileContents, Invoice1_1.class);
		} catch (Exception e) {
			StringBuilder message = new StringBuilder();
			message.append("Error creating a mapping object for ");
			message.append(filePath);
			log.error(message.toString(), e);
			return;
		}

		String invoiceId;
		try {
			invoiceId = invoiceMapping.getId();
		} catch (NullPointerException e) {
			StringBuilder message = new StringBuilder();
			message.append("Error while searching the id of ");
			message.append(filePath);
			log.error(message.toString(), e);
			return;
		}

		Invoice invoice;
		try {
			invoice = invoiceMappingToEntityConverter.convert(invoiceMapping);
		} catch (NullPointerException e) {
			StringBuilder message = new StringBuilder();
			message.append("Error while converting mapping object to entity. Path: ");
			message.append(filePath);
			log.error(message.toString(), e);
			return;
		}
		if(!invoiceRepository.exists(invoiceId)){
			try {
				invoiceRepository.save(invoice);
			} catch (Exception e) {
				StringBuilder message = new StringBuilder();
				message.append("Error while converting mapping object to entity. Path: ");
				message.append(filePath);
				log.error(message.toString(), e);
				return;
			}
			log.info("Completed processing of: {}", filePath);
		}else{
			log.info("Skipping {} because it was already processed", filePath);
		}
	}
}

{'fraction_non_alphanumeric': 0.0994805603651818, 'fraction_numerical': 0.005666614198016685, 'mean_word_length': 5.510245901639344, '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': '1700552', 'n_tokens_mistral': 2078, 'n_tokens_neox': 1838, 'n_words': 363}