<commit_before>package de.setsoftware.reviewtool.connectors.file;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Properties;
import java.util.Set;

import de.setsoftware.reviewtool.base.ReviewtoolException;
import de.setsoftware.reviewtool.model.EndTransition;
import de.setsoftware.reviewtool.model.IReviewPersistence;
import de.setsoftware.reviewtool.model.ITicketData;
import de.setsoftware.reviewtool.model.TicketInfo;

/**
 * A simple review persistence layer based on a shared directory.
 */
public class FilePersistence implements IReviewPersistence {

    private static final String REVIEW_DATA_TXT = "reviewData.txt";
    private static final String REVIEW_HISTORY_TXT = "reviewHistory.txt";
    private static final String STATE_PREFIX = "state.";
    private static final String IN_IMPLEMENTATION = "inImplementation";
    private static final String REJECTED = "rejected";
    private static final String IN_REVIEW = "inReview";
    private static final String READY_FOR_REVIEW = "readyForReview";
    private static final String DONE = "done";

    /**
     * The directory containing the data for a ticket.
     */
    private final class TicketDir implements ITicketData {

        private final File ticketDir;

        public TicketDir(File file) {
            this.ticketDir = file;
        }

        @Override
        public String getReviewData() {
            final Path reviewDataFile = this.ticketDir.toPath().resolve(REVIEW_DATA_TXT);
            if (!reviewDataFile.toFile().exists()) {
                return "";
            }
            try {
                return new String(Files.readAllBytes(reviewDataFile), "UTF-8");
            } catch (final IOException e) {
                throw new ReviewtoolException(e);
            }
        }

        @Override
        public String getReviewerForRound(int number) {
            final List<String> lines = this.readReviewHistory();
            if (number > lines.size() || number <= 0) {
                return FilePersistence.this.defaultReviewer;
            } else {
                return lines.get(number - 1);
            }
        }

        @Override
        public int getCurrentRound() {
            return this.readReviewHistory().size();
        }

        private List<String> readReviewHistory() {<commit_msg>FilePersistence automatically creates dummy ticket data if not existing<commit_after>package de.setsoftware.reviewtool.connectors.file;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Properties;
import java.util.Set;

import de.setsoftware.reviewtool.base.ReviewtoolException;
import de.setsoftware.reviewtool.model.EndTransition;
import de.setsoftware.reviewtool.model.IReviewPersistence;
import de.setsoftware.reviewtool.model.ITicketData;
import de.setsoftware.reviewtool.model.TicketInfo;

/**
 * A simple review persistence layer based on a shared directory.
 */
public class FilePersistence implements IReviewPersistence {

    private static final String REVIEW_DATA_TXT = "reviewData.txt";
    private static final String REVIEW_HISTORY_TXT = "reviewHistory.txt";
    private static final String STATE_PREFIX = "state.";
    private static final String IN_IMPLEMENTATION = "inImplementation";
    private static final String REJECTED = "rejected";
    private static final String IN_REVIEW = "inReview";
    private static final String READY_FOR_REVIEW = "readyForReview";
    private static final String DONE = "done";

    /**
     * The directory containing the data for a ticket.
     */
    private final class TicketDir implements ITicketData {

        private final File ticketDir;

        public TicketDir(File file) {
            this.ticketDir = file;
            if (!this.ticketDir.exists()) {
                try {
                    this.createDummyDir();
                } catch (final IOException e) {
                    throw new ReviewtoolException(e);
                }
            }
        }

        private void createDummyDir() throws IOException {
            this.ticketDir.mkdir();
            Files.write(this.ticketDir.toPath().resolve("ticket.properties"),
                    ("description=Testticket " + (int) (Math.random() * 1000) + "\ncomponent=Main").getBytes());
            Files.createFile(this.ticketDir.toPath().resolve("state.readyForReview"));
        }

        @Override
        public String getReviewData() {
            final Path reviewDataFile = this.ticketDir.toPath().resolve(REVIEW_DATA_TXT);
            if (!reviewDataFile.toFile().exists()) {
                return "";
            }
            try {
                return new String(Files.readAllBytes(reviewDataFile), "UTF-8");
            } catch (final IOException e) {
                throw new ReviewtoolException(e);
            }
        }

        @Override
        public String getReviewerForRound(int number) {
            final List<String> lines = this.readReviewHistory();
            if (number > lines.size() || number <= 0) {
                return FilePersistence.this.defaultReviewer;
            } else {
                return lines.get(number - 1);
            }
        }

        @Override
        public int getCurrentRound() {
            return this.readReviewHistory().size();
        }

        private List<String> readReviewHistory() {
{'fraction_non_alphanumeric': 0.09341317365269461, 'fraction_numerical': 0.001710863986313088, 'mean_word_length': 2.6041923551171395, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '4632617', 'n_tokens_mistral': 1583, 'n_tokens_neox': 1512, 'n_words': 384}