<commit_before>package com.freiheit.fuava.simplebatch.fsjobs.importer;

import java.io.File;

import com.freiheit.fuava.simplebatch.fetch.FetchedItem;
import com.freiheit.fuava.simplebatch.processor.AbstractSingleItemProcessor;
import com.freiheit.fuava.simplebatch.result.Result;

public class FileMovingPersistence<D> extends AbstractSingleItemProcessor<FetchedItem<ControlFile>, D, D> {

    private final FileMover fileMover = new FileMover();
    private final String processingDir;
    private final String archivedDir;
    private final String failedDir;

    public FileMovingPersistence( final String processingDir, final String archivedDir, final String failedDir ) {
        super();
        this.processingDir = processingDir;
        this.archivedDir = archivedDir;
        this.failedDir = failedDir;
    }

    @Override
    public Result<FetchedItem<ControlFile>, D> processItem( final Result<FetchedItem<ControlFile>, D> r ) {
        final FetchedItem<ControlFile> input = r.getInput();
        final ControlFile controlFile = input.getValue();
        try {
            if ( r.isFailed() ) {
                moveBoth( controlFile, failedDir );
            } else {
                moveBoth( controlFile, archivedDir );
            }
            return Result.success( input, r.getOutput() );
        } catch ( final Throwable e ) {
            return Result.<FetchedItem<ControlFile>, D> builder( r ).failed( e );
        }

    }

    private void moveBoth( final ControlFile input, final String targetDirName ) throws FailedToMoveFileException {
        final File dir = new File( processingDir );
        final File targetDir = new File( targetDirName );
        if ( !targetDir.exists() ) {
            if ( !targetDir.mkdirs() ) {
                throw new FailedToMoveFileException( "could not create directory " + processingDir );
            }
        }
        fileMover.moveFile( new File( dir, input.getFileName() ), targetDirName );<commit_msg>do not suppress fail information<commit_after>package com.freiheit.fuava.simplebatch.fsjobs.importer;

import java.io.File;

import com.freiheit.fuava.simplebatch.fetch.FetchedItem;
import com.freiheit.fuava.simplebatch.processor.AbstractSingleItemProcessor;
import com.freiheit.fuava.simplebatch.result.Result;

public class FileMovingPersistence<D> extends AbstractSingleItemProcessor<FetchedItem<ControlFile>, D, D> {

    private final FileMover fileMover = new FileMover();
    private final String processingDir;
    private final String archivedDir;
    private final String failedDir;

    public FileMovingPersistence( final String processingDir, final String archivedDir, final String failedDir ) {
        super();
        this.processingDir = processingDir;
        this.archivedDir = archivedDir;
        this.failedDir = failedDir;
    }

    @Override
    public Result<FetchedItem<ControlFile>, D> processItem( final Result<FetchedItem<ControlFile>, D> r ) {
        final FetchedItem<ControlFile> input = r.getInput();
        final ControlFile controlFile = input.getValue();
        try {
            if ( r.isFailed() ) {
                moveBoth( controlFile, failedDir );
                // even though this operation was successful, we must not say success if the original item failed!
                return Result.<FetchedItem<ControlFile>, D> builder( r ).failed();

            } else {
                moveBoth( controlFile, archivedDir );
                return Result.success( input, r.getOutput() );
            }

        } catch ( final Throwable e ) {
            return Result.<FetchedItem<ControlFile>, D> builder( r ).failed( e );
        }

    }

    private void moveBoth( final ControlFile input, final String targetDirName ) throws FailedToMoveFileException {
        final File dir = new File( processingDir );
        final File targetDir = new File( targetDirName );
        if ( !targetDir.exists() ) {
            if ( !targetDir.mkdirs() ) {
                throw new FailedToMoveFileException( "could not create directory " + processingDir );
            }
        }
        fileMover.moveFile( new File( dir, input.getFileName() ), targetDirName );
{'fraction_non_alphanumeric': 0.08962377186676253, 'fraction_numerical': 0.0, 'mean_word_length': 2.8223443223443225, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, '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': '4268869', 'n_tokens_mistral': 1140, 'n_tokens_neox': 1100, 'n_words': 298}