Skip to content

Commit

Permalink
changed made clearer
Browse files Browse the repository at this point in the history
  • Loading branch information
yusufsefasezer committed Feb 8, 2024
1 parent cd3a240 commit 6caf28f
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 11 deletions.
6 changes: 6 additions & 0 deletions src/main/java/com/yusufsezer/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.yusufsezer.util.JPAUtils;
import com.yusufsezer.util.JavaFXUtils;
import jakarta.persistence.EntityManager;
import java.util.List;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
Expand Down Expand Up @@ -56,7 +57,12 @@ public void init() throws Exception {
entityManager.persist(task);
}
}

entityManager.getTransaction().commit();

Category foundCategory = categoryService.find(1L);
List<Task> tasks = foundCategory.getTasks();
tasks.forEach(System.out::println);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ public interface DialogControllerBase<T> {
public abstract T edit(T entity);

public abstract void set(T entity);

}
25 changes: 19 additions & 6 deletions src/main/java/com/yusufsezer/controller/MainController.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@
import com.yusufsezer.controller.dialog.TaskDialogController;
import com.yusufsezer.model.Category;
import com.yusufsezer.model.Task;
import com.yusufsezer.service.CategoryService;
import com.yusufsezer.service.TaskService;
import com.yusufsezer.util.DummyDataUtils;
import com.yusufsezer.util.JPAUtils;
import java.net.URL;
import java.util.List;
import java.util.Optional;
import java.util.ResourceBundle;
import javafx.collections.FXCollections;
Expand Down Expand Up @@ -80,7 +83,8 @@ public void initialize(URL url, ResourceBundle rb) {
void onAddNewCategory(MouseEvent event) {
Optional<Category> result = CategoryDialogController.createAddCategoryDialog();
if (result.isPresent()) {
JPAUtils.getCategoryService().create(result.get());
CategoryService categoryService = JPAUtils.getCategoryService();
categoryService.create(result.get());
loadCategories();
}
}
Expand All @@ -91,7 +95,8 @@ void onKeyTaskReleased(KeyEvent event) {
if (isEnterPressed) {
Optional<Task> result = TaskDialogController.createAddTaskDialog(quickAddTextField.getText());
if (result.isPresent()) {
JPAUtils.getTaskService().create(result.get());
TaskService taskService = JPAUtils.getTaskService();
taskService.create(result.get());
}
quickAddTextField.clear();
}
Expand All @@ -115,7 +120,8 @@ void onMouseEditPressed(MouseEvent event) {
if (selectedTask != null) {
Optional<Task> result = TaskDialogController.createEditTaskDialog(selectedTask);
if (result.isPresent()) {
JPAUtils.getTaskService().edit(result.get());
TaskService taskService = JPAUtils.getTaskService();
taskService.edit(result.get());
}
}
}
Expand All @@ -125,7 +131,8 @@ void onMouseRemovePressed(MouseEvent event) {
Task selectedTask = taskListView.getSelectionModel().getSelectedItem();
if (selectedTask != null) {
selectedTask.setDeleted(true);
JPAUtils.getTaskService().edit(selectedTask);
TaskService taskService = JPAUtils.getTaskService();
taskService.edit(selectedTask);
taskListView.getItems().remove(selectedTask);
taskListView.getSelectionModel().selectFirst();
}
Expand All @@ -141,7 +148,11 @@ void onShowAbout(MouseEvent event) {
}

void showCategoryTasks(Category category) {
taskListView.getItems().setAll(JPAUtils.getCategoryService().find(category.getId()).getTasks());
CategoryService categoryService = JPAUtils.getCategoryService();
Long categoryId = category.getId();
Category foundCategory = categoryService.find(categoryId);
List<Task> categoryTasks = foundCategory.getTasks();
taskListView.getItems().setAll(categoryTasks);
}

void showTaskContent(Task selectedTask) {
Expand All @@ -152,7 +163,9 @@ void showTaskContent(Task selectedTask) {
}

void loadCategories() {
categoryListView.getItems().setAll(JPAUtils.getCategoryService().findAll());
CategoryService categoryService = JPAUtils.getCategoryService();
List<Category> categories = categoryService.findAll();
categoryListView.getItems().setAll(categories);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,5 @@ protected void updateItem(Task task, boolean empty) {
setGraphic(null);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ public static Optional<Category> createAddCategoryDialog() {
return dialog.showAndWait();
}

public static Optional<Category>
createEditCategoryDialog(Category category) {
public static Optional<Category> createEditCategoryDialog(Category category) {
String title = JavaFXUtils.getBundleMessage("dialog.categoryEditTitle");
String headerText = JavaFXUtils.getBundleMessage("dialog.categoryEditHeaderText");
String fxml = "category";
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/yusufsezer/model/Category.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public class Category extends BaseEntity implements Serializable {

private String color;
private String name;

@Lob
@Column(length = 1000)
private String description;
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/yusufsezer/model/Task.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public enum Status {

private Status status;
private String name;

@Lob
@Column(length = 1000)
private String description;
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/yusufsezer/util/DialogUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ public static <T extends BaseEntity> Dialog<T> createCustomDialog(String title,
Dialog<T> dialog = new Dialog<>();
dialog.setTitle(title);
dialog.setHeaderText(headerText);
dialog.getDialogPane().setContent(JavaFXUtils.<Node>loadCustomFXML(DIALOG_FOLDER, fxml, controller));
Node node = JavaFXUtils.<Node>loadCustomFXML(DIALOG_FOLDER, fxml, controller);
dialog.getDialogPane().setContent(node);
dialog.getDialogPane().getButtonTypes().addAll(ButtonType.OK, ButtonType.CLOSE);
return dialog;
}
Expand Down
12 changes: 10 additions & 2 deletions src/main/java/com/yusufsezer/util/JPAUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ public class JPAUtils {
private static final String PERSISTENCE_UNIT_NAME = "YSTodoFXPU";
private static EntityManagerFactory entityManagerFactory;
private static EntityManager entityManager;
private static CategoryService categoryService;
private static TaskService taskService;

static {
try {
Expand All @@ -31,11 +33,17 @@ public static EntityManager getEntityManager() {
}

public static CategoryService getCategoryService() {
return new CategoryService(getEntityManager());
if (categoryService == null) {
categoryService = new CategoryService(getEntityManager());
}
return categoryService;
}

public static TaskService getTaskService() {
return new TaskService(getEntityManager());
if (taskService == null) {
taskService = new TaskService(getEntityManager());
}
return taskService;
}

}

0 comments on commit 6caf28f

Please sign in to comment.