<commit_before>package com.github.neunkasulle.chronocommand.control;

import com.github.neunkasulle.chronocommand.model.*;


import java.io.File;
import java.time.LocalDateTime;
import java.time.Month;
import java.time.temporal.ChronoUnit;
import java.util.LinkedList;
import java.util.List;
import java.time.LocalDate;

/**
 * Created by Janze on 18.01.2016.
 * Handles TimeSheet manipulation
 */
public class TimeSheetControl extends Control {

    private static TimeSheetControl ourInstance = new TimeSheetControl();

    private TimeSheetControl() {

    }

    /**
     * Gets the one Instance of the TimeSheetControl.
     * @return The one TimeSheetControl instance.
     */
    public static TimeSheetControl getInstance() {
        return ourInstance;
    }

    /**
     * creates a new TimeRecord. And a new Time Sheet when there is no time sheet this month
     * @param user The user which the time record belongs to
     */
    public TimeRecord newTimeRecord(User user) throws ChronoCommandException {
        TimeSheetDAO timeSheetDAO = TimeSheetDAO.getInstance();
        TimeSheet timeSheet = timeSheetDAO.getTimeSheet(LocalDate.now().getMonth(), LocalDate.now().getYear(), user);

        if(timeSheet == null){  //No Time sheet yet, we need to build a new one
            timeSheet = new TimeSheet(user, LocalDate.now().getMonth(), LocalDate.now().getYear());
            timeSheetDAO.saveTimeSheet(timeSheet);
        }

        TimeRecord timeRecord = new TimeRecord(LocalDateTime.now(), null, null, null, timeSheet);
        timeSheetDAO.saveTimeRecord(timeRecord);

        return timeRecord;
    }

    /**
     * creates a new TimeRecord. And a new Time Sheet when there is no time sheet this month
     * @param category the category of the work performed in this time.
     * @param description description of the work performed.
     * @param user The user which the time record belongs to
     * @throws ChronoCommandException When there is something wrong with e.g. the category
     */
    public TimeRecord newTimeRecord(Category category, String description, User user) throws ChronoCommandException {
        TimeSheetDAO timeSheetDAO = TimeSheetDAO.getInstance();
        TimeSheet timeSheet = timeSheetDAO.getTimeSheet(LocalDate.now().getMonth(), LocalDate.now().getYear(), user);


        if(timeSheet == null) {  //No Time sheet yet, we need to build a new one
            timeSheet = new TimeSheet(user, LocalDate.now().getMonth(), LocalDate.now().getYear());
            timeSheetDAO.saveTimeSheet(timeSheet);
        }


        TimeRecord timeRecord = new TimeRecord(LocalDateTime.now(), null, category, description, timeSheet);
        timeSheetDAO.saveTimeRecord(timeRecord);

        return timeRecord;
    }

    /**
     * Closes a time record. Cant be invoked if newTimeRecord was called without category and description
     * @param user The user which the time record belongs to
     * @throws ChronoCommandException ChronoCommandException When there is something wrong with e.g. the category
     */
    public TimeRecord closeTimeRecord(User user) throws ChronoCommandException {
        TimeSheetDAO timeSheetDAO = TimeSheetDAO.getInstance();
        TimeRecord timeRecord = timeSheetDAO.getLatestTimeRecord(user);
        if(timeRecord.getCategory() == null) {
            throw new ChronoCommandException(Reason.MISSINGCATEGORY);
        }

        timeRecord.setEnd(LocalDateTime.now());
        timeSheetDAO.saveTimeRecord(timeRecord);

        return timeRecord;
    }

    /**
     * Closes a time record. Cant be invoked if newTimeRecord was called with category and description
     * @param category the category of the work performed in this time.
     * @param description description of the work performed.
     * @param user The user which the time record belongs to
     * @throws ChronoCommandException When there is something wrong with e.g. the category
     */
    public TimeRecord closeTimeRecord(Category category, String description, User user) throws ChronoCommandException {
        TimeSheetDAO timeSheetDAO = TimeSheetDAO.getInstance();
        TimeRecord timeRecord = timeSheetDAO.getLatestTimeRecord(user);

        if (category == null && timeRecord.getCategory() == null) {
            throw new ChronoCommandException(Reason.MISSINGCATEGORY);
        }

        timeRecord.setCategory(category);
        timeRecord.setDescription(description);
        timeRecord.setEnd(LocalDateTime.now());
        TimeSheetDAO.getInstance().saveTimeRecord(timeRecord);

        return timeRecord;
    }

    public TimeRecord getLatestTimeRecord(User user) {
        return TimeSheetDAO.getInstance().getLatestTimeRecord(user);
    }

    /**
     * Adds a whole time record to the timeSheet
     * @param beginn Start of work
     * @param end end of work
     * @param category the category of the work performed in this time.
     * @param description description of the work performed.
     * @param user The user which the time record belongs to
     * @throws ChronoCommandException When there is something wrong with e.g. the category
     */
    public void addTimeToSheet(LocalDateTime beginn, LocalDateTime end, Category category, String description, User user)
    throws  ChronoCommandException {
        TimeSheetDAO timeSheetDAO = TimeSheetDAO.getInstance();
        TimeSheet timeSheet = timeSheetDAO.getTimeSheet(beginn.getMonth(), beginn.getYear(), user);
        if(category == null) {
            throw new ChronoCommandException(Reason.MISSINGCATEGORY);
        }

        if (timeSheet == null) {  //No Time sheet yet, we need to build a new one
            timeSheet = new TimeSheet(user, beginn.getMonth(), beginn.getYear());
            timeSheetDAO.saveTimeSheet(timeSheet);
        }

        if (timeSheet.getState() != TimeSheetState.UNLOCKED) {
            throw new ChronoCommandException(Reason.TIMESHEETLOCKED);
        }

        timeSheetDAO.saveTimeRecord(new TimeRecord(beginn, end, category, description, timeSheet));
    }


    /**
     * Collects all time Sheets from user which are supervised by a specific user
     * @param month the month from which the time sheets will be collected
     * @param year the year from which the time sheets will be collected
     * @param user the supervising user
     * @return A list of time sheets<commit_msg>add checks before editing time sheet in control<commit_after>package com.github.neunkasulle.chronocommand.control;

import com.github.neunkasulle.chronocommand.model.*;


import java.io.File;
import java.time.LocalDateTime;
import java.time.Month;
import java.time.temporal.ChronoUnit;
import java.util.LinkedList;
import java.util.List;
import java.time.LocalDate;

/**
 * Created by Janze on 18.01.2016.
 * Handles TimeSheet manipulation
 */
public class TimeSheetControl extends Control {

    private static TimeSheetControl ourInstance = new TimeSheetControl();

    private TimeSheetControl() {

    }

    /**
     * Gets the one Instance of the TimeSheetControl.
     * @return The one TimeSheetControl instance.
     */
    public static TimeSheetControl getInstance() {
        return ourInstance;
    }

    /**
     * creates a new TimeRecord. And a new Time Sheet when there is no time sheet this month
     * @param user The user which the time record belongs to
     */
    public TimeRecord newTimeRecord(User user) throws ChronoCommandException {
        TimeSheetDAO timeSheetDAO = TimeSheetDAO.getInstance();
        TimeSheet timeSheet = timeSheetDAO.getTimeSheet(LocalDate.now().getMonth(), LocalDate.now().getYear(), user);

        if (timeSheet.getState() != TimeSheetState.UNLOCKED) {
            throw new ChronoCommandException(Reason.TIMESHEETLOCKED);
        }

        if(timeSheet == null){  //No Time sheet yet, we need to build a new one
            timeSheet = new TimeSheet(user, LocalDate.now().getMonth(), LocalDate.now().getYear());
            timeSheetDAO.saveTimeSheet(timeSheet);
        }

        TimeRecord timeRecord = new TimeRecord(LocalDateTime.now(), null, null, null, timeSheet);
        timeSheetDAO.saveTimeRecord(timeRecord);

        return timeRecord;
    }

    /**
     * creates a new TimeRecord. And a new Time Sheet when there is no time sheet this month
     * @param category the category of the work performed in this time.
     * @param description description of the work performed.
     * @param user The user which the time record belongs to
     * @throws ChronoCommandException When there is something wrong with e.g. the category
     */
    public TimeRecord newTimeRecord(Category category, String description, User user) throws ChronoCommandException {
        TimeSheetDAO timeSheetDAO = TimeSheetDAO.getInstance();
        TimeSheet timeSheet = timeSheetDAO.getTimeSheet(LocalDate.now().getMonth(), LocalDate.now().getYear(), user);

        if (timeSheet.getState() != TimeSheetState.UNLOCKED) {
            throw new ChronoCommandException(Reason.TIMESHEETLOCKED);
        }

        if(timeSheet == null) {  //No Time sheet yet, we need to build a new one
            timeSheet = new TimeSheet(user, LocalDate.now().getMonth(), LocalDate.now().getYear());
            timeSheetDAO.saveTimeSheet(timeSheet);
        }


        TimeRecord timeRecord = new TimeRecord(LocalDateTime.now(), null, category, description, timeSheet);
        timeSheetDAO.saveTimeRecord(timeRecord);

        return timeRecord;
    }

    /**
     * Closes a time record. Cant be invoked if newTimeRecord was called without category and description
     * @param user The user which the time record belongs to
     * @throws ChronoCommandException ChronoCommandException When there is something wrong with e.g. the category
     */
    public TimeRecord closeTimeRecord(User user) throws ChronoCommandException {
        TimeSheetDAO timeSheetDAO = TimeSheetDAO.getInstance();
        TimeRecord timeRecord = timeSheetDAO.getLatestTimeRecord(user);

        if (timeRecord.getTimeSheet().getState() != TimeSheetState.UNLOCKED) {
            throw new ChronoCommandException(Reason.TIMESHEETLOCKED);
        }

        if(timeRecord.getCategory() == null) {
            throw new ChronoCommandException(Reason.MISSINGCATEGORY);
        }

        timeRecord.setEnd(LocalDateTime.now());
        timeSheetDAO.saveTimeRecord(timeRecord);

        return timeRecord;
    }

    /**
     * Closes a time record. Cant be invoked if newTimeRecord was called with category and description
     * @param category the category of the work performed in this time.
     * @param description description of the work performed.
     * @param user The user which the time record belongs to
     * @throws ChronoCommandException When there is something wrong with e.g. the category
     */
    public TimeRecord closeTimeRecord(Category category, String description, User user) throws ChronoCommandException {
        TimeSheetDAO timeSheetDAO = TimeSheetDAO.getInstance();
        TimeRecord timeRecord = timeSheetDAO.getLatestTimeRecord(user);

        if (timeRecord.getTimeSheet().getState() != TimeSheetState.UNLOCKED) {
            throw new ChronoCommandException(Reason.TIMESHEETLOCKED);
        }

        if (category == null && timeRecord.getCategory() == null) {
            throw new ChronoCommandException(Reason.MISSINGCATEGORY);
        }

        timeRecord.setCategory(category);
        timeRecord.setDescription(description);
        timeRecord.setEnd(LocalDateTime.now());
        TimeSheetDAO.getInstance().saveTimeRecord(timeRecord);

        return timeRecord;
    }

    public TimeRecord getLatestTimeRecord(User user) {
        return TimeSheetDAO.getInstance().getLatestTimeRecord(user);
    }

    /**
     * Adds a whole time record to the timeSheet
     * @param beginn Start of work
     * @param end end of work
     * @param category the category of the work performed in this time.
     * @param description description of the work performed.
     * @param user The user which the time record belongs to
     * @throws ChronoCommandException When there is something wrong with e.g. the category
     */
    public void addTimeToSheet(LocalDateTime beginn, LocalDateTime end, Category category, String description, User user)
    throws  ChronoCommandException {
        TimeSheetDAO timeSheetDAO = TimeSheetDAO.getInstance();
        TimeSheet timeSheet = timeSheetDAO.getTimeSheet(beginn.getMonth(), beginn.getYear(), user);

        if (timeSheet.getState() != TimeSheetState.UNLOCKED) {
            throw new ChronoCommandException(Reason.TIMESHEETLOCKED);
        }

        if(category == null) {
            throw new ChronoCommandException(Reason.MISSINGCATEGORY);
        }

        if (timeSheet == null) {  //No Time sheet yet, we need to build a new one
            timeSheet = new TimeSheet(user, beginn.getMonth(), beginn.getYear());
            timeSheetDAO.saveTimeSheet(timeSheet);
        }

        timeSheetDAO.saveTimeRecord(new TimeRecord(beginn, end, category, description, timeSheet));
    }


    /**
     * Collects all time Sheets from user which are supervised by a specific user
     * @param month the month from which the time sheets will be collected
     * @param year the year from which the time sheets will be collected
     * @param user the supervising user
     * @return A list of time sheets
{'fraction_non_alphanumeric': 0.08063430323883611, 'fraction_numerical': 0.0011967985638417233, 'mean_word_length': 3.130367624343528, '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': '6724402', 'n_tokens_mistral': 3537, 'n_tokens_neox': 3443, 'n_words': 1118}