<commit_before>package task.timer.view;

import java.sql.Date;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Calendar;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

import javax.swing.Timer;

import javafx.application.Platform;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.DatePicker;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.control.cell.TextFieldTableCell;
import javafx.util.StringConverter;
import task.timer.model.AbstractEntity;
import task.timer.model.MEFactory;
import task.timer.model.ManageEntity;
import task.timer.model.Project;
import task.timer.model.Record;
import task.timer.model.User;

public class MainEmployeerController {
	@FXML private Label loggedUserName;
	
	@FXML private TableView<Record> recordTable;
	@FXML private TableColumn<Record, String> dateColumn;
	@FXML private TableColumn<Record, String> projectNameColumn;
	@FXML private TableColumn<Record, String> descriptionColumn;
	@FXML private TableColumn<Record, String> startTimeColumn;
	@FXML private TableColumn<Record, String> stopTimeColumn;
	
	@FXML private Button startStopTime;
	@FXML private Label measuredTimeHoursLabel;
	@FXML private Label measuredTimeMinutesLabel;
	@FXML private Label measuredTimeSecondsLabel;
	@FXML private ChoiceBox projectChoice;
	@FXML private TextField descriptionName;
	@FXML private DatePicker date;
	@FXML private Label dataLabel;
	
	private final String patternDate = "yyyy-MM-dd";
	private final String patternTime = "HH:mm:ss";
	private LocalDate currentDate;
	
	private LocalTime timeStart;
	private LocalTime timeStop;
	private Timer time;
	private long passageOfTimeSeconds;		
	private long timeHour;								
	private long timeMinutes;
	private long timeSeconds;
	private long rest;	

	private int indexOfCurrentProject;
	Record newRecord;
	
	ObservableList<Project> listOfProjects = FXCollections.observableArrayList();
	ObservableList<String> listOfProjectsName = FXCollections.observableArrayList();
	
	private final ObservableList<Record> dataRecords = 
			FXCollections.observableArrayList();
	
	@FXML private void initialize(){
		loggedUserName.setText(
				LoginWindowController.loggedUser.getFirstName() 
				+ " " 
				+ LoginWindowController.loggedUser.getLastName());
	
	
		setDatePicker();
		
		currentDate = date.getValue();
		dataLabel.setText(currentDate.toString());
	
		dateColumn.setCellValueFactory(cellData ->
			cellData.getValue().getDateProperty());
		projectNameColumn.setCellValueFactory(cellData ->
			cellData.getValue().getProjectName());			
		descriptionColumn.setCellValueFactory(cellData ->
			cellData.getValue().getDescriptionProperty());			
		descriptionColumn.setCellFactory(TextFieldTableCell.forTableColumn()); // włącza edytowanie pola	
		startTimeColumn.setCellValueFactory(cellData ->
			cellData.getValue().getTimeStartProperty());	
		stopTimeColumn.setCellValueFactory(cellData ->
			cellData.getValue().getTimeStopProperty());		

		readProjectsFromDataBase();
		projectChoice.setItems(listOfProjectsName);
    
		readAndShowRecordsFromDataBase();
	}
	
	@FXML private void timing() throws ClassNotFoundException{
		indexOfCurrentProject = projectChoice.getSelectionModel().getSelectedIndex();
		
		// zareaguj gdy jest wybrany projekt
		if (indexOfCurrentProject>-1){	
			if (startStopTime.getText().equals("START")){
				int currentPositionInTableView;
				disableElements();						
				timeStart = LocalTime.now();
				startTimeMeaserement();			
				addNewRecordToDataBase();	
				
				currentPositionInTableView = recordTable.getItems().size();
				readAndShowRecordsFromDataBase();		
				recordTable.getSelectionModel().select(currentPositionInTableView); // ustaw podswietlenie na ostatni wiersz
			}				
			else {
				enableElements();
				timeStop = LocalTime.now().withNano(0);
			      				
				stopTimeMeaserement();
				recordTable.getSelectionModel().getSelectedItem().setTimeStop(timeStop);				
				DAO.MMRecord.update(recordTable.getSelectionModel().getSelectedItem());
				
				recordTable.refresh();
				descriptionName.clear();
			}
		}
	}


	@FXML private void readAndShowRecordsFromDataBase(){
	
		List<Record> records = DAO.MMRecord.listRecords(LoginWindowController.loggedUser, null, currentDate);	
		
		if (records.size() > 0){
			dataRecords.clear();			
			dataRecords.addAll(records);					
			recordTable.setItems(dataRecords);	
		}
	}
	
	// umożliwia edycję pola w TableView i zapis do bazy danych po zatwierdzeniu przez ENTER
	@FXML private void onEditDescription(TableColumn.CellEditEvent<Record, String> descriptionEditEvent) throws ClassNotFoundException{	
		recordTable.getSelectionModel().getSelectedItem().setDescription(descriptionEditEvent.getNewValue());
		DAO.MMRecord.update(recordTable.getSelectionModel().getSelectedItem());		
	}

	private void disableElements(){
		startStopTime.setText("STOP");	
		projectChoice.setDisable(true);
		descriptionName.setDisable(true);
		date.setDisable(true);	
		recordTable.setDisable(true);
		measuredTimeHoursLabel.setText("0");	
		measuredTimeMinutesLabel.setText("0");
		measuredTimeSecondsLabel.setText("0");
	}
	
	private void enableElements(){<commit_msg>add access to edit time on exist Records by employeer<commit_after>package task.timer.view;

import java.sql.Date;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.time.temporal.ChronoUnit;
import java.util.Calendar;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

import javax.swing.Timer;

import javafx.application.Platform;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.DatePicker;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.control.cell.TextFieldTableCell;
import javafx.util.StringConverter;
import task.timer.model.AbstractEntity;
import task.timer.model.MEFactory;
import task.timer.model.ManageEntity;
import task.timer.model.Project;
import task.timer.model.Record;
import task.timer.model.User;

public class MainEmployeerController {
	@FXML private Label loggedUserName;
	
	@FXML private TableView<Record> recordTable;
	@FXML private TableColumn<Record, String> dateColumn;
	@FXML private TableColumn<Record, String> projectNameColumn;
	@FXML private TableColumn<Record, String> descriptionColumn;
	@FXML private TableColumn<Record, String> startTimeColumn;
	@FXML private TableColumn<Record, String> stopTimeColumn;
	
	@FXML private Button startStopTime;
	@FXML private Label measuredTimeHoursLabel;
	@FXML private Label measuredTimeMinutesLabel;
	@FXML private Label measuredTimeSecondsLabel;
	@FXML private ChoiceBox projectChoice;
	@FXML private TextField descriptionName;
	@FXML private DatePicker date;
	@FXML private Label dataLabel;
	
	private final String patternDate = "yyyy-MM-dd";
	private final String patternTime = "HH:mm:ss";
	private LocalDate currentDate;
	
	private LocalTime timeStart;
	private LocalTime timeStop;
	private Timer time;
	private long passageOfTimeSeconds;		
	private long timeHour;								
	private long timeMinutes;
	private long timeSeconds;
	private long rest;	

	private int indexOfCurrentProject;
	Record newRecord;
	
	ObservableList<Project> listOfProjects = FXCollections.observableArrayList();
	ObservableList<String> listOfProjectsName = FXCollections.observableArrayList();
	
	private final ObservableList<Record> dataRecords = 
			FXCollections.observableArrayList();
	
	@FXML private void initialize(){
		loggedUserName.setText(
				LoginWindowController.loggedUser.getFirstName() 
				+ " " 
				+ LoginWindowController.loggedUser.getLastName());
	
	
		setDatePicker();
		
		currentDate = date.getValue();
		dataLabel.setText(currentDate.toString());
	
		dateColumn.setCellValueFactory(cellData ->
			cellData.getValue().getDateProperty());
		projectNameColumn.setCellValueFactory(cellData ->
			cellData.getValue().getProjectName());			
		descriptionColumn.setCellValueFactory(cellData ->
			cellData.getValue().getDescriptionProperty());			
		descriptionColumn.setCellFactory(TextFieldTableCell.forTableColumn()); // włącza edytowanie pola	
		startTimeColumn.setCellValueFactory(cellData ->
			cellData.getValue().getTimeStartProperty());	
		startTimeColumn.setCellFactory(TextFieldTableCell.forTableColumn()); // włącza edytowanie pola		
		stopTimeColumn.setCellValueFactory(cellData ->
			cellData.getValue().getTimeStopProperty());		
		stopTimeColumn.setCellFactory(TextFieldTableCell.forTableColumn()); // włącza edytowanie pola
		readProjectsFromDataBase();
		projectChoice.setItems(listOfProjectsName);    
		readAndShowRecordsFromDataBase();
	}
	
	@FXML private void timing() throws ClassNotFoundException{
		indexOfCurrentProject = projectChoice.getSelectionModel().getSelectedIndex();
		
		// zareaguj gdy jest wybrany projekt
		if (indexOfCurrentProject>-1){	
			if (startStopTime.getText().equals("START")){
				int currentPositionInTableView;
				disableElements();						
				timeStart = LocalTime.now();
				startTimeMeaserement();			
				addNewRecordToDataBase();	
				currentPositionInTableView = recordTable.getItems().size();
				readAndShowRecordsFromDataBase();		
				recordTable.getSelectionModel().select(currentPositionInTableView); // ustaw podswietlenie na ostatni wiersz
			}				
			else {
				enableElements();
				timeStop = LocalTime.now().withNano(0);			      				
				stopTimeMeaserement();
				recordTable.getSelectionModel().getSelectedItem().setTimeStop(timeStop);				
				DAO.MMRecord.update(recordTable.getSelectionModel().getSelectedItem());				
				recordTable.refresh();
				descriptionName.clear();
			}
		}
	}


	@FXML private void readAndShowRecordsFromDataBase(){	
		List<Record> records = DAO.MMRecord.listRecords(LoginWindowController.loggedUser, null, currentDate);		
		if (records.size() > 0){
			dataRecords.clear();			
			dataRecords.addAll(records);					
			recordTable.setItems(dataRecords);	
		}
	}
	
	// umożliwia edycję pola w TableView i zapis do bazy danych po zatwierdzeniu przez ENTER
	@FXML private void onEditDescription(TableColumn.CellEditEvent<Record, String> descriptionEditEvent) throws ClassNotFoundException{	
		recordTable.getSelectionModel().getSelectedItem().setDescription(descriptionEditEvent.getNewValue());
		DAO.MMRecord.update(recordTable.getSelectionModel().getSelectedItem());		
	}
	
	// umożliwia edycję startu pracy w TableView i zapis do bazy danych po zatwierdzeniu przez ENTER
	@FXML private void onEditStartTime(TableColumn.CellEditEvent<Record, String> timeStartEditEvent) throws ClassNotFoundException{	
		LocalTime time;
		try{
			time = java.time.LocalTime.parse(timeStartEditEvent.getNewValue());			
			recordTable.getSelectionModel()
			.getSelectedItem()
			.setTimeStart(time);
		DAO.MMRecord.update(recordTable.getSelectionModel().getSelectedItem());
		}
		catch (DateTimeParseException e){
			time = java.time.LocalTime.parse(timeStartEditEvent.getOldValue());
			System.out.println(time);
			recordTable.getSelectionModel()
			.getSelectedItem()
			.setTimeStart(time);
			recordTable.refresh();
		}		
	}
	
	// umożliwia edycję konca pracy w TableView i zapis do bazy danych po zatwierdzeniu przez ENTER
	@FXML private void onEditStopTime(TableColumn.CellEditEvent<Record, String> timeStopEditEvent) throws ClassNotFoundException{	
		LocalTime time;
		try{
			time = java.time.LocalTime.parse(timeStopEditEvent.getNewValue());	
			recordTable.getSelectionModel().getSelectedItem().setTimeStop(time);
			DAO.MMRecord.update(recordTable.getSelectionModel().getSelectedItem());
		}
		catch (DateTimeParseException e){
			time = java.time.LocalTime.parse(timeStopEditEvent.getOldValue());
			System.out.println(time);
			recordTable.getSelectionModel().getSelectedItem().setTimeStop(time);		
		}
		recordTable.refresh();
	}

	private void disableElements(){
		startStopTime.setText("STOP");	
		projectChoice.setDisable(true);
		descriptionName.setDisable(true);
		date.setDisable(true);	
		recordTable.setDisable(true);
		measuredTimeHoursLabel.setText("0");	
		measuredTimeMinutesLabel.setText("0");
		measuredTimeSecondsLabel.setText("0");
	}
	
	private void enableElements(){
{'fraction_non_alphanumeric': 0.0994365768235115, 'fraction_numerical': 0.0009136592051164915, 'mean_word_length': 5.5380786460925835, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '2403059', 'n_tokens_mistral': 4280, 'n_tokens_neox': 3829, 'n_words': 722}