Skip to content

Commit 363bc15

Browse files
committed
App is migrated from on Prem storage to the MySql Database
Signed-off-by: cankush625 <[email protected]>
1 parent 935b9e2 commit 363bc15

File tree

9 files changed

+150
-82
lines changed

9 files changed

+150
-82
lines changed

TodoList.iml

+1
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,6 @@
1616
<SOURCES />
1717
</library>
1818
</orderEntry>
19+
<orderEntry type="library" name="mysql-connector-java-8.0.17" level="project" />
1920
</component>
2021
</module>

TodoListItems.txt

-11
This file was deleted.
Binary file not shown.
Binary file not shown.

src/com/cankush/todolist/Controller.java

+31-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.cankush.todolist;
22

3+
import com.cankush.todolist.database.MysqlConnect;
34
import com.cankush.todolist.datamodel.TodoData;
45
import com.cankush.todolist.datamodel.TodoItem;
56
import javafx.application.Platform;
@@ -19,6 +20,7 @@
1920
import javafx.util.Callback;
2021

2122
import java.io.IOException;
23+
import java.sql.SQLException;
2224
import java.time.LocalDate;
2325
import java.time.format.DateTimeFormatter;
2426
import java.util.Comparator;
@@ -55,17 +57,32 @@ public void initialize() {
5557
// initializing context menu list
5658
listContextMenu = new ContextMenu();
5759
MenuItem deleteMenuItem = new MenuItem("Delete");
60+
// Adding menu to edit the todoItem
61+
MenuItem editMenuItem = new MenuItem("Edit");
5862
// Code for deleting the selected item
5963
deleteMenuItem.setOnAction(new EventHandler<ActionEvent>() {
6064
@Override
6165
public void handle(ActionEvent event) {
6266
TodoItem item = todoListView.getSelectionModel().getSelectedItem();
63-
deleteItem(item);
67+
try {
68+
deleteItem(item);
69+
} catch (SQLException e) {
70+
System.out.println(e.getMessage());
71+
}
72+
}
73+
});
74+
// Code for editing the selected item
75+
editMenuItem.setOnAction(new EventHandler<ActionEvent>() {
76+
@Override
77+
public void handle(ActionEvent event) {
78+
TodoItem item = todoListView.getSelectionModel().getSelectedItem();
79+
editItem(item);
6480
}
6581
});
6682
// Adding delete option to the context menu
6783
listContextMenu.getItems().addAll(deleteMenuItem);
68-
84+
// Adding edit option to the context menu
85+
listContextMenu.getItems().addAll(editMenuItem);
6986
// Adding change listener to select the item that is changed
7087
// Whenever the change is occur this will automatically trigger using change listener
7188
todoListView.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<TodoItem>() {
@@ -222,7 +239,7 @@ public void handleClickListView() {
222239
* @Param keyEvent accepts the key event
223240
*/
224241
@FXML
225-
public void handleKeyPressed(KeyEvent keyEvent) {
242+
public void handleKeyPressed(KeyEvent keyEvent) throws SQLException {
226243
TodoItem selectedItem = todoListView.getSelectionModel().getSelectedItem();
227244
// Checking if the item is get selected
228245
if (selectedItem != null) {
@@ -237,18 +254,27 @@ public void handleKeyPressed(KeyEvent keyEvent) {
237254
/**
238255
* Method to delete the selected item
239256
*/
240-
public void deleteItem(TodoItem item) {
257+
public void deleteItem(TodoItem item) throws SQLException {
241258
Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
242259
alert.setTitle("Delete Todo Item");
243260
alert.setHeaderText("Delete item: " + item.getTitle());
244261
alert.setContentText("Are you sure? Press OK to confirm and cancel to back");
245262
Optional<ButtonType> result = alert.showAndWait();
246263

247264
if (result.isPresent() && (result.get() == ButtonType.OK)) {
248-
TodoData.getInstance().deleteTodoItem(item);
265+
String sql = "delete from taskstack.todo where title = " + "'" + item.getTitle() + "'";
266+
MysqlConnect.mysqlDelete(sql);
267+
TodoData.getInstance().displayTodoItem();
249268
}
250269
}
251270

271+
/**
272+
* Method to edit selected item
273+
*/
274+
public void editItem(TodoItem item) {
275+
showNewItemDialog();
276+
}
277+
252278
/**
253279
* Method to handle the filter button on main window
254280
*/

src/com/cankush/todolist/DialogController.java

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
package com.cankush.todolist;
22

3-
import com.cankush.todolist.datamodel.TodoData;
3+
import com.cankush.todolist.database.MysqlConnect;
44
import com.cankush.todolist.datamodel.TodoItem;
55
import javafx.fxml.FXML;
66
import javafx.scene.control.DatePicker;
77
import javafx.scene.control.TextArea;
88
import javafx.scene.control.TextField;
99

1010
import java.time.LocalDate;
11+
import java.time.format.DateTimeFormatter;
1112

1213
public class DialogController {
1314
@FXML
@@ -24,9 +25,14 @@ public TodoItem processResults() {
2425
String details = detailsArea.getText().trim();
2526
LocalDate deadlineValue = deadlinePicker.getValue();
2627

28+
String deadline = deadlineValue.toString();
2729
TodoItem newItem = new TodoItem(title, details, deadlineValue);
30+
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); // Formatting the date
31+
LocalDate date = LocalDate.parse(deadline, formatter);
32+
2833
// Adding newItem to the database/datafile
29-
TodoData.getInstance().addTodoItem(newItem);
34+
String sql = "insert into taskstack.todo" + " values('" + title + "'" + "," + "'" + details + "'" + "," + "'" + date + "')";
35+
MysqlConnect.mysqlUpdate(sql);
3036
return newItem;
3137
}
3238
}

src/com/cankush/todolist/Main.java

+10-10
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
import javafx.scene.Scene;
88
import javafx.stage.Stage;
99

10-
import java.io.IOException;
11-
1210
public class Main extends Application {
1311

1412
@Override
@@ -27,21 +25,23 @@ public static void main(String[] args) {
2725

2826
@Override
2927
public void stop() throws Exception {
30-
try {
31-
// Storing the items to the file/database
32-
TodoData.getInstance().storeTodoItems();
33-
} catch (IOException e) {
34-
System.out.println(e.getMessage());
35-
}
28+
// try {
29+
// // Storing the items to the file/database
30+
//// TodoData.getInstance().storeTodoItems();
31+
// TodoData.getInstance().addNewItem();
32+
// } catch (Exception e) {
33+
// System.out.println(e.getMessage());
34+
// }
3635
}
3736

3837
// Every time the application starts this will load the list of todoItems
3938
@Override
4039
public void init() throws Exception {
4140
try {
4241
// Loading the items from the file/database at the starting of the application
43-
TodoData.getInstance().loadTodoItems();
44-
} catch (IOException e) {
42+
// TodoData.getInstance().loadTodoItems();
43+
TodoData.getInstance().displayTodoItem();
44+
} catch (Exception e) {
4545
System.out.println(e.getMessage());
4646
}
4747
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.cankush.todolist.database;
2+
3+
import com.cankush.todolist.datamodel.TodoItem;
4+
import javafx.collections.ObservableList;
5+
6+
import java.sql.*;
7+
import java.time.format.DateTimeFormatter;
8+
9+
public class MysqlConnect {
10+
private static ObservableList<TodoItem> todoItems;
11+
private static DateTimeFormatter formatter;
12+
13+
public static void mysqlUpdate(String sql) {
14+
System.out.println("Inserting values in mysql database table!");
15+
16+
Connection conn = null;
17+
String url = "jdbc:mysql://localhost:3306/";
18+
String db = "taskstack";
19+
try {
20+
conn = DriverManager.getConnection(url + db, "cankush", "ankush");
21+
try {
22+
Statement st = conn.createStatement();
23+
System.out.println(sql);
24+
int val = st.executeUpdate(sql);
25+
System.out.println("1 row affected + return value: " + val);
26+
} catch(SQLException s) {
27+
System.out.println("SQL statement is not executable! Error is: " + s.getMessage());
28+
}
29+
} catch(Exception e) {
30+
e.printStackTrace();
31+
}
32+
}
33+
34+
public static void mysqlDelete(String sql) {
35+
System.out.println("Deleting values from mysql database table!");
36+
37+
Connection conn = null;
38+
String url = "jdbc:mysql://localhost:3306/";
39+
String db = "taskstack";
40+
try {
41+
conn = DriverManager.getConnection(url + db, "cankush", "ankush");
42+
try {
43+
Statement st = conn.createStatement();
44+
System.out.println(sql);
45+
boolean val = st.execute(sql);
46+
System.out.println("1 row affected + return value: " + val);
47+
} catch(SQLException s) {
48+
System.out.println("SQL statement is not executable! Error is: " + s.getMessage());
49+
}
50+
} catch(Exception e) {
51+
e.printStackTrace();
52+
}
53+
}
54+
55+
public static ResultSet mysqlDisplay() {
56+
Connection conn = null;
57+
Statement st;
58+
String url = "jdbc:mysql://localhost:3306/";
59+
String db = "taskstack";
60+
try {
61+
//Open a connection
62+
conn = DriverManager.getConnection(url + db, "cankush", "ankush");
63+
try {
64+
st = conn.createStatement();
65+
String sql = "select * from taskstack.todo";
66+
System.out.println(sql);
67+
return st.executeQuery(sql);
68+
69+
} catch(SQLException s) {
70+
System.out.println("SQL statement is not executable! Error is: " + s.getMessage());
71+
}
72+
} catch(Exception e) {
73+
e.printStackTrace();
74+
}
75+
return null;
76+
}
77+
}

src/com/cankush/todolist/datamodel/TodoData.java

+23-54
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
package com.cankush.todolist.datamodel;
22

3+
import com.cankush.todolist.database.MysqlConnect;
34
import javafx.collections.FXCollections;
45
import javafx.collections.ObservableList;
6+
import javafx.fxml.FXML;
7+
import javafx.scene.control.DatePicker;
8+
import javafx.scene.control.TextArea;
9+
import javafx.scene.control.TextField;
510

6-
import java.io.BufferedReader;
7-
import java.io.BufferedWriter;
8-
import java.io.IOException;
9-
import java.nio.file.Files;
10-
import java.nio.file.Path;
11-
import java.nio.file.Paths;
11+
import java.sql.ResultSet;
12+
import java.sql.SQLException;
1213
import java.time.LocalDate;
1314
import java.time.format.DateTimeFormatter;
14-
import java.util.Iterator;
1515

1616
public class TodoData {
1717
// Creating the instance of the class so that making the class Singleton and later declaring constructor as private
@@ -21,14 +21,21 @@ public class TodoData {
2121
private ObservableList<TodoItem> todoItems;
2222
private DateTimeFormatter formatter;
2323

24+
@FXML
25+
private TextField titleField;
26+
@FXML
27+
private TextArea detailsArea;
28+
@FXML
29+
private DatePicker deadLinePicker;
30+
2431
// Getting the instance
2532
public static TodoData getInstance() {
2633
return instance;
2734
}
2835

2936
// Creating a private constructor so that the another objects of this class are not allowed to create any more
3037
private TodoData () {
31-
formatter = DateTimeFormatter.ofPattern("dd-MM-yyy"); // Formatting the date
38+
formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); // Formatting the date
3239
}
3340

3441
// Adding getter for TodoItems
@@ -43,58 +50,20 @@ public void addTodoItem(TodoItem todoItem) {
4350

4451
// Loading TodoItems
4552
// Taking the item from the user and add it to the todoItems list
46-
public void loadTodoItems() throws IOException {
53+
public void displayTodoItem() throws SQLException {
4754
todoItems = FXCollections.observableArrayList();
48-
Path path = Paths.get(filename); // Path of the file
49-
BufferedReader br = Files.newBufferedReader(path); // Reading from the file
50-
51-
String input;
52-
try {
53-
while ((input = br.readLine()) != null) {
54-
// Separating the input by \t. The first element is title, second element is details, and the third element is date
55-
String[] itemPieces = input.split("\t");
56-
57-
String title = itemPieces[0];
58-
String details = itemPieces[1];
59-
String dateString = itemPieces[2];
55+
ResultSet val = MysqlConnect.mysqlDisplay();
56+
while (val.next()) {
57+
System.out.println("Inside val");
58+
String title = val.getString("title");
59+
String details = val.getString("details");
60+
String deadline = val.getString("deadline");
6061

61-
LocalDate date = LocalDate.parse(dateString, formatter);
62+
LocalDate date = LocalDate.parse(deadline, formatter);
6263
TodoItem todoItem = new TodoItem(title, details, date);
6364
// Adding todoItem from database/file to the todoItems ObservableList
6465
todoItems.add(todoItem);
6566
}
66-
} finally {
67-
if (br != null) {
68-
br.close();
69-
}
70-
}
71-
}
72-
73-
// Storing the TodoItem
74-
public void storeTodoItems() throws IOException {
75-
Path path = Paths.get(filename);
76-
BufferedWriter bw = Files.newBufferedWriter(path);
77-
try {
78-
// Initializing iterator
79-
Iterator<TodoItem> iter = todoItems.iterator();
80-
while(iter.hasNext()) {
81-
TodoItem item = iter.next();
82-
bw.write(String.format("%s\t%s\t%s\t", // Separating the fields by tab space
83-
item.getTitle(),
84-
item.getDetails(),
85-
item.getDeadline().format(formatter))); // Using date formatter
86-
bw.newLine();
87-
}
88-
} finally {
89-
if (bw != null) {
90-
bw.close();
91-
}
92-
}
93-
}
94-
95-
// Removing the item / Deleting the item
96-
public void deleteTodoItem(TodoItem item) {
97-
todoItems.remove(item);
9867
}
9968
}
10069

0 commit comments

Comments
 (0)