Skip to content

Commit 89b5d28

Browse files
committed
Refactor some stuff
1 parent a7a4a22 commit 89b5d28

File tree

8 files changed

+159
-146
lines changed

8 files changed

+159
-146
lines changed

backend/src/main/java/timetable/users/UserData.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ public final class UserData {
1313
private static final MessageDigest hasher = getHashAlgorithm();
1414

1515
@Id
16-
public transient String id;
16+
public String id;
1717
public final ClassData[] classes;
18-
public final transient LocalDateTime creationDate;
19-
public final transient String password;
18+
public final LocalDateTime creationDate;
19+
public final String password;
2020

2121
@PersistenceConstructor
2222
public UserData(String id, ClassData[] classes, LocalDateTime creationDate, String password) {

backend/src/main/java/timetable/users/UsersController.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,16 @@ public UsersController(UsersRepository users) {
1414
}
1515

1616
@GetMapping
17-
public ResponseEntity<UserData> get(@RequestParam String id) {
17+
public ResponseEntity<ClassData[]> getClasses(@RequestParam String id) {
1818
var user = users.findById(id);
1919

20-
return user.map(k -> new ResponseEntity<>(k, HttpStatus.OK))
20+
return user.map(k -> new ResponseEntity<>(k.classes, HttpStatus.OK))
2121
.orElseGet(() -> new ResponseEntity<>(HttpStatus.BAD_REQUEST));
2222
}
2323

2424
@PostMapping
25-
public ResponseEntity<String> createOrUpdate(@RequestParam(required = false) String id, @RequestBody UserData newUserData) {
26-
if(id == null) {
25+
public ResponseEntity<String> createOrUpdate(@RequestParam String id, @RequestBody UserData newUserData) {
26+
if(id.equals("null")) {
2727
return new ResponseEntity<>(users.insert(newUserData).id, HttpStatus.OK);
2828
}
2929

desktop/pom.xml

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,14 @@
2121
<version>4.1.1</version>
2222
</dependency>
2323
<dependency>
24-
<groupId>org.eclipse</groupId>
25-
<artifactId>yasson</artifactId>
26-
<version>2.0.1</version>
24+
<groupId>com.fasterxml.jackson.core</groupId>
25+
<artifactId>jackson-databind</artifactId>
26+
<version>2.13.2.1</version>
27+
</dependency>
28+
<dependency>
29+
<groupId>com.fasterxml.jackson.datatype</groupId>
30+
<artifactId>jackson-datatype-jsr310</artifactId>
31+
<version>2.13.2</version>
2732
</dependency>
2833
<dependency>
2934
<groupId>com.google.zxing</groupId>

desktop/src/main/java/module-info.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,14 @@
33
requires java.sql;
44
requires java.net.http;
55

6-
requires jakarta.json;
7-
requires jakarta.json.bind;
6+
requires com.fasterxml.jackson.core;
7+
requires com.fasterxml.jackson.databind;
8+
requires com.fasterxml.jackson.datatype.jsr310;
9+
810
requires poi;
911
requires poi.ooxml;
1012
requires com.google.zxing;
1113
requires com.google.zxing.javase;
14+
15+
opens timetable to com.fasterxml.jackson.databind;
1216
}

desktop/src/main/java/timetable/ClassButton.java

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package timetable;
22

3-
import jakarta.json.*;
3+
import com.fasterxml.jackson.annotation.*;
44
import java.awt.*;
55
import java.awt.Font;
66
import java.awt.event.*;
@@ -22,9 +22,17 @@ public final class ClassButton extends MouseAdapter {
2222
public final String type;
2323
public final String room;
2424
public boolean unImportant;
25-
public final JButton button;
25+
public final transient JButton button;
26+
27+
@JsonCreator
28+
public ClassButton(@JsonProperty("day") String day,
29+
@JsonProperty("name") String name,
30+
@JsonProperty("type") String type,
31+
@JsonProperty("startTime") LocalTime startTime,
32+
@JsonProperty("endTime") LocalTime endTime,
33+
@JsonProperty("room") String room,
34+
@JsonProperty("unImportant") boolean unImportant) {
2635

27-
public ClassButton(String day, String name, String type, LocalTime startTime, LocalTime endTime, String room, boolean unImportant) {
2836
this.day = day;
2937
this.name = name;
3038
this.type = type;
@@ -51,18 +59,11 @@ public static ClassButton fromEditorTable(JTable table, boolean unImportant) {
5159
return new ClassButton((String) table.getValueAt(1, 1),
5260
(String) table.getValueAt(0, 1),
5361
(String) table.getValueAt(2, 1),
54-
LocalTime.parse((String) table.getValueAt(3, 1), DateTimeFormatter.ISO_LOCAL_TIME),
55-
LocalTime.parse((String) table.getValueAt(4, 1), DateTimeFormatter.ISO_LOCAL_TIME),
62+
LocalTime.parse((String) table.getValueAt(3, 1)),
63+
LocalTime.parse((String) table.getValueAt(4, 1)),
5664
(String) table.getValueAt(5, 1), unImportant);
5765
}
5866

59-
public static ClassButton fromJson(JsonObject object) {
60-
return new ClassButton(object.getString("day"), object.getString("name"), object.getString("type"),
61-
LocalTime.parse(object.getString("startTime"), DateTimeFormatter.ISO_LOCAL_TIME),
62-
LocalTime.parse(object.getString("endTime"), DateTimeFormatter.ISO_LOCAL_TIME),
63-
object.getString("room"), object.getBoolean("unImportant"));
64-
}
65-
6667
public static ClassButton fromTimetableExcel(Row classRow, DateTimeFormatter format) {
6768
var beginDate = LocalDateTime.parse(classRow.getCell(0).getStringCellValue(), format);
6869
var endDate = LocalDateTime.parse(classRow.getCell(1).getStringCellValue(), format);
@@ -83,7 +84,7 @@ public static ClassButton fromTimetableExcel(Row classRow, DateTimeFormatter for
8384
public void mousePressed(MouseEvent event) {
8485
if(event.getButton() == MouseEvent.BUTTON3) {
8586
var panel = new JPanel(null);
86-
panel.add(Components.newClassToolButton(0, PopupGuis.editIcon, e -> PopupGuis.showEditorForOldClass(day, this)));
87+
panel.add(Components.newClassToolButton(0, PopupGuis.editIcon, e -> PopupGuis.showEditorForOldClass(this)));
8788
panel.add(Components.newClassToolButton(32, PopupGuis.deleteIcon, e -> onDeleteButtonPressed()));
8889
panel.add(Components.newClassToolButton(64, unImportant ? PopupGuis.unIgnore : PopupGuis.ignoreIcon, e -> onImportantButtonPressed(panel)));
8990

@@ -100,32 +101,27 @@ public void mousePressed(MouseEvent event) {
100101
}
101102

102103
private void onImportantButtonPressed(JPanel panel) {
103-
Settings.classes.get(day).stream()
104-
.filter(this::equals)
105-
.findFirst()
106-
.ifPresent(butt -> {
107-
butt.unImportant = !butt.unImportant;
108-
Main.updateClassesGui();
109-
((JDialog)panel.getTopLevelAncestor()).dispose();
110-
});
104+
unImportant = !unImportant;
105+
106+
Main.updateClassesGui();
107+
((JDialog)panel.getTopLevelAncestor()).dispose();
111108
}
112109

113110
private void onDeleteButtonPressed() {
114111
if(JOptionPane.showConfirmDialog(Main.classesPanel, "Tényleg Törlöd?", "Törlés Megerősítés", JOptionPane.YES_NO_OPTION) == 0) {
115-
Settings.classes.get(day).removeIf(this::equals);
112+
Settings.classes.remove(this);
116113
Main.updateClassesGui();
117114
}
118115
}
119116

120117
@Override
121118
public boolean equals(Object obj) {
122-
if(obj == this) {
123-
return true;
124-
}
125-
if(obj instanceof ClassButton) {
126-
var button = (ClassButton) obj;
127-
return name.equals(button.name) && day.equals(button.day) && type.equals(button.type) && startTime.equals(button.startTime) && endTime.equals(button.endTime) && room.equals(button.room);
128-
}
129-
return false;
119+
return obj == this ||
120+
(obj instanceof ClassButton button && name.equals(button.name) &&
121+
day.equals(button.day) &&
122+
type.equals(button.type) &&
123+
startTime.equals(button.startTime) &&
124+
endTime.equals(button.endTime) &&
125+
room.equals(button.room));
130126
}
131127
}

desktop/src/main/java/timetable/Main.java

Lines changed: 21 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package timetable;
22

3-
import jakarta.json.*;
43
import java.awt.*;
54
import java.awt.Color;
65
import java.awt.TrayIcon.*;
@@ -9,9 +8,7 @@
98
import java.io.*;
109
import java.net.*;
1110
import java.net.http.*;
12-
import java.net.http.HttpRequest.*;
1311
import java.net.http.HttpResponse.*;
14-
import java.nio.charset.*;
1512
import java.time.*;
1613
import java.time.format.*;
1714
import java.util.*;
@@ -29,7 +26,7 @@
2926
public final class Main {
3027
private static final ArrayList<ClassButton> classButtons = new ArrayList<>();
3128
private static final JPanel mainPanel = new JPanel(new BorderLayout());
32-
private static final String BACKEND_URL = "https://timetable-backend.herokuapp.com/timetable";
29+
private static final String BACKEND_URL = "http://localhost:8080/timetable";
3330
private static final HttpClient client = HttpClient.newHttpClient();
3431
private static final JLabel dateLabel = new JLabel("\0");
3532
private static final TrayIcon tray = new TrayIcon(Components.trayIcon.getScaledInstance(16, 16, Image.SCALE_SMOOTH), "Órarend");
@@ -151,14 +148,10 @@ private static void exportToCloud(@SuppressWarnings("unused") ActionEvent event)
151148

152149
if(userPwInput != null) {
153150
Consumer<JDialog> exportFunction = dialog -> {
154-
var classesArray = Settings.createClassesArray();
155-
var objectToSend = Json.createObjectBuilder()
156-
.add("classes", classesArray)
157-
.add("password", userPwInput)
158-
.build();
151+
var objectToSend = Map.of("classes", Settings.classes, "password", userPwInput);
159152

160-
var request = HttpRequest.newBuilder(URI.create(BACKEND_URL + (Settings.cloudID != null ? ("?id=" + Settings.cloudID) : "")))
161-
.POST(BodyPublishers.ofString(Settings.json.toJson(objectToSend)));
153+
var request = HttpRequest.newBuilder(URI.create(BACKEND_URL + (!Settings.cloudID.equals(Settings.NULL_CLOUD_ID) ? ("?id=" + Settings.cloudID) : "")))
154+
.POST(Settings.publisherOf(objectToSend));
162155

163156
var response = sendRequest(request, BodyHandlers.ofString());
164157
var responseStatusCode = response.statusCode();
@@ -168,7 +161,7 @@ private static void exportToCloud(@SuppressWarnings("unused") ActionEvent event)
168161
if(responseStatusCode == 401) {
169162
JOptionPane.showMessageDialog(mainPanel, "Sikertelen mentés! Hibás jelszó!");
170163
}else if(responseStatusCode == 400) {
171-
Settings.cloudID = null;
164+
Settings.cloudID = Settings.NULL_CLOUD_ID;
172165
JOptionPane.showMessageDialog(mainPanel, "Sikertelen mentés! Nem található ilyen azonosítójú órarend...\nAz eddigi felhő azonosító törlésre került!");
173166
}else if(responseStatusCode == 200) {
174167
var optionalReceivedCloudID = response.body();
@@ -198,10 +191,10 @@ private static void importFromExcel(@SuppressWarnings("unused") ActionEvent even
198191
var classesSheet = book.getSheetAt(0);
199192
var format = DateTimeFormatter.ofPattern("uuuu.MM.dd. [HH:][H:]mm:ss");
200193

201-
Settings.updateClassesData(StreamSupport.stream(classesSheet.spliterator(), false)
194+
Settings.classes = StreamSupport.stream(classesSheet.spliterator(), false)
202195
.skip(1) // Header
203196
.map(row -> ClassButton.fromTimetableExcel(row, format))
204-
.collect(Collectors.groupingBy(k -> k.day)));
197+
.collect(Collectors.toCollection(ArrayList::new));
205198
updateClassesGui();
206199
dialog.setVisible(false);
207200
}catch (FileNotFoundException e) {
@@ -221,13 +214,10 @@ private static void importFromCloud(@SuppressWarnings("unused") ActionEvent even
221214

222215
if(userIDInput != null && !userIDInput.isBlank()) {
223216
Consumer<JDialog> importFunction = dialog -> {
224-
JsonObject response = sendRequest(HttpRequest.newBuilder(URI.create(BACKEND_URL + "?id=" + userIDInput)), ofObject()).body();
217+
var responseClasses = sendRequest(HttpRequest.newBuilder(URI.create(BACKEND_URL + "?id=" + userIDInput)), Settings.of(Settings.CLASSES_TYPEREF)).body();
225218

226-
if(response != null) {
227-
Settings.updateClassesData(Settings.getArraySetting("classes", response).stream()
228-
.map(JsonValue::asJsonObject)
229-
.map(ClassButton::fromJson)
230-
.collect(Collectors.groupingBy(k -> k.day)));
219+
if(responseClasses != null) {
220+
Settings.classes = responseClasses;
231221

232222
dialog.setVisible(false);
233223
Settings.cloudID = userIDInput;
@@ -251,11 +241,6 @@ private static<T> HttpResponse<T> sendRequest(HttpRequest.Builder request, BodyH
251241
}
252242
}
253243

254-
private static BodyHandler<JsonObject> ofObject() {
255-
return info -> BodySubscribers.mapping(BodySubscribers.ofString(StandardCharsets.UTF_8),
256-
data -> info.statusCode() != 200 ? null : Settings.json.fromJson(data, JsonObject.class));
257-
}
258-
259244

260245
private static void showTransferDialog(String text, Consumer<JDialog> fun) {
261246
var jop = new JOptionPane(text, JOptionPane.PLAIN_MESSAGE, JOptionPane.DEFAULT_OPTION, null, new Object[0]);
@@ -277,15 +262,17 @@ public static void updateClassesGui() {
277262
Components.handleNightMode(classesPanel, nowTime);
278263
Components.handleNightMode(dateLabel, nowTime);
279264

280-
Settings.classes.forEach((day, classesPerDay) -> {
281-
var yPosition = new int[] { -40 };
282-
var xPosition = 20 + indexOf(day, days) * 200;
283-
var isToday = day.equals(today);
284-
285-
classesPerDay.stream()
286-
.sorted(ClassButton.timeBasedOrder)
287-
.forEach(clazz -> positionAndAddButtonToPanel(nowTime, yPosition, xPosition, isToday, clazz));
288-
});
265+
Settings.classes.stream()
266+
.collect(Collectors.groupingBy(k -> k.day))
267+
.forEach((day, classesPerDay) -> {
268+
var yPosition = new int[] { -40 };
269+
var xPosition = 20 + indexOf(day, days) * 200;
270+
var isToday = day.equals(today);
271+
272+
classesPerDay.stream()
273+
.sorted(ClassButton.timeBasedOrder)
274+
.forEach(clazz -> positionAndAddButtonToPanel(nowTime, yPosition, xPosition, isToday, clazz));
275+
});
289276

290277
updateCurrentClass(nowDate);
291278
classesPanel.repaint();

desktop/src/main/java/timetable/PopupGuis.java

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import java.nio.file.*;
1111
import java.time.*;
1212
import java.time.format.*;
13-
import java.util.List;
1413
import java.util.function.*;
1514
import java.util.stream.*;
1615
import javax.swing.*;
@@ -31,23 +30,21 @@ public static void showEditorForNewClass(ClassButton dataButton) {
3130
showClassEditorDialog(frame -> {
3231
if(editorTable.getCellEditor() != null) editorTable.getCellEditor().stopCellEditing();
3332

34-
Settings.classes.get(editorTable.getValueAt(1, 1))
35-
.add(ClassButton.fromEditorTable(editorTable, dataButton.unImportant));
33+
Settings.classes.add(ClassButton.fromEditorTable(editorTable, dataButton.unImportant));
3634

3735
Main.updateClassesGui();
3836
frame.dispose();
3937
}, editorTable);
4038
}
4139

42-
public static void showEditorForOldClass(String day, ClassButton dataButton) {
40+
public static void showEditorForOldClass(ClassButton dataButton) {
4341
var editorTable = Components.createClassEditorTable(dataButton);
4442

4543
showClassEditorDialog(frame -> {
4644
if(editorTable.getCellEditor() != null) editorTable.getCellEditor().stopCellEditing();
4745

48-
Settings.classes.get(day).remove(dataButton);
49-
Settings.classes.get(editorTable.getValueAt(1, 1))
50-
.add(ClassButton.fromEditorTable(editorTable, dataButton.unImportant));
46+
Settings.classes.remove(dataButton);
47+
Settings.classes.add(ClassButton.fromEditorTable(editorTable, dataButton.unImportant));
5148

5249
Main.updateClassesGui();
5350
frame.dispose();
@@ -105,7 +102,7 @@ public static void showSettingsGui(@SuppressWarnings("unused") ActionEvent event
105102
Components.addSettingButton(minutesBeforeClassNoteBox, 550, "Óra előtti értesítés előtti idő percekben", scrollPanel, now);
106103

107104
Components.addSettingsSection("Felhő", 590, now, scrollPanel);
108-
Components.addSettingInfoLabel(640, "Felhő Azonosító: " + (Settings.cloudID == null ? "nincs" : Settings.cloudID), scrollPanel, now);
105+
Components.addSettingInfoLabel(640, "Felhő Azonosító: " + (Settings.cloudID.equals(Settings.NULL_CLOUD_ID) ? "nincs" : Settings.cloudID), scrollPanel, now);
109106

110107
Components.addSettingsSection("Egyéb", 700, now, scrollPanel);
111108
Components.addSettingButton(popupCheckBox, 750, "Üzenetek Bekapcsolva", scrollPanel, now);
@@ -171,7 +168,7 @@ public static void showSettingsGui(@SuppressWarnings("unused") ActionEvent event
171168
}
172169

173170
private static ImageIcon generateQRCodeImage() {
174-
if(Settings.cloudID == null) {
171+
if(Settings.cloudID.equals(Settings.NULL_CLOUD_ID)) {
175172
return new ImageIcon(new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB));
176173
}
177174

@@ -201,7 +198,7 @@ private static ImageIcon generateQRCodeImage() {
201198

202199
private static void handleClassReset(JPanel scrollPanel) {
203200
if(JOptionPane.showConfirmDialog(scrollPanel, "Biztos törlöd az összes órát?", "Órarend", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
204-
Settings.classes.values().forEach(List::clear);
201+
Settings.classes.clear();
205202
Main.updateClassesGui();
206203
}
207204
}

0 commit comments

Comments
 (0)