<commit_before>package com.paulograbin.persistence;

import com.google.gson.Gson;
import com.paulograbin.domain.EntityNotFoundException;
import com.paulograbin.domain.notes.Note;
import com.paulograbin.domain.notes.NotesRepository;
import org.springframework.stereotype.Component;

import java.io.*;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;


@Component
public class FileSystemNotesRepository implements NotesRepository {

    private final String DIRECTORY_NAME = "/OEditor_uploads/";
    private final String FILE_EXTENSION = ".txt";

    private final File repository;
    private boolean ready;
    private int lastId;

    private Gson converter;


    public FileSystemNotesRepository() {
        converter = new Gson();

        String basePath = System.getProperty("user.home");
        repository = new File(basePath + DIRECTORY_NAME);

        FileSystemRepositoryCreator repoCreator = new FileSystemRepositoryCreator(repository);

        if(repoCreator.isRepositoryReady()) {
            ready = true;
        } else {
            repoCreator.create();
        }
    }

    @Override
    public void save(Note entity) {
        if(entity.getId() == null)
            entity.setId(getNextId());

        writeContentToFile(entity);
    }

    private void writeContentToFile(Note entity) {
        String filePath = makeNewNoteFileName(entity.getId());
        File newNoteFile = new File(filePath);

        try {
            if(!newNoteFile.exists())
                newNoteFile.createNewFile();

            FileWriter fw = new FileWriter(newNoteFile);
            fw.write(converter.toJson(entity));
            fw.flush();
            fw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private String makeNewNoteFileName(Integer noteId) {
        Path path = Paths.get(repository.getPath(), String.valueOf(noteId));
        return path.toString() + FILE_EXTENSION;
    }

    private Integer getNextId() {<commit_msg>Refactoring.<commit_after>package com.paulograbin.persistence;

import com.google.gson.Gson;
import com.paulograbin.domain.EntityNotFoundException;
import com.paulograbin.domain.notes.Note;
import com.paulograbin.domain.notes.NotesRepository;
import org.springframework.stereotype.Component;

import java.io.*;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;


@Component
public class FileSystemNotesRepository implements NotesRepository {

    private final String DIRECTORY_NAME = "/OEditor_uploads/";
    private final String FILE_EXTENSION = ".txt";

    private final File repository;
    private boolean ready;
    private int lastId;

    private Gson converter;


    public FileSystemNotesRepository() {
        converter = new Gson();

        String basePath = System.getProperty("user.home");
        repository = new File(basePath + DIRECTORY_NAME);

        FileSystemRepositoryCreator repoCreator = new FileSystemRepositoryCreator(repository);

        if(repoCreator.isRepositoryReady()) {
            ready = true;
        } else {
            repoCreator.create();
        }
    }

    @Override
    public void save(Note entity) {
        if(entity.getId() == null)
            entity.setId(getNextId());

        writeContentToFile(entity);
    }

    private void writeContentToFile(Note entity) {
        String filePath = makeNewNoteFileName(entity.getId());
        File newNoteFile = new File(filePath);

        try {
            createFileIfNotExists(newNoteFile);

            FileWriter fw = new FileWriter(newNoteFile);
            fw.write(converter.toJson(entity));
            fw.flush();
            fw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void createFileIfNotExists(File newNoteFile) throws IOException {
        if(!newNoteFile.exists())
            newNoteFile.createNewFile();
    }

    private String makeNewNoteFileName(Integer noteId) {
        Path path = Paths.get(repository.getPath(), String.valueOf(noteId));
        return path.toString() + FILE_EXTENSION;
    }

    private Integer getNextId() {
{'fraction_non_alphanumeric': 0.09769585253456221, 'fraction_numerical': 0.0, 'mean_word_length': 2.889784946236559, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '5327158', 'n_tokens_mistral': 1226, 'n_tokens_neox': 1171, 'n_words': 255}