From ad75047ec22bdb71b1d72a820ad794abb7268bf0 Mon Sep 17 00:00:00 2001 From: jan Date: Wed, 20 May 2020 11:33:27 +0200 Subject: [PATCH 1/4] #11 Import [IT, TA, Kj] --- config.json | 6 +-- .../java/asd_morning_9/note/JsonParser.java | 45 +++++++++++++++++++ .../note/ui/Dashboard/DashboardView.java | 20 ++++++++- .../asd_morning_9/note/JsonParserTest.java | 15 +++++++ test | 0 test.json | 1 + 6 files changed, 81 insertions(+), 6 deletions(-) create mode 100644 test create mode 100644 test.json diff --git a/config.json b/config.json index b6c4fcc..5c89078 100644 --- a/config.json +++ b/config.json @@ -1,5 +1 @@ -<<<<<<< HEAD -{"Notes":[{"pinned":false,"id":0,"completed":false,"title":"qwer","content":"aesr","tags":"aer"}]} -======= -{"Notes":[{"pinned":false,"id":0,"completed":false,"title":"new","content":"ball","tags":"new"}]} ->>>>>>> 893128a0d6802d838eba18cbfbadcccfb87846a9 +{"Notes":[{"pinned":false,"id":0,"completed":false,"title":"test","content":"test123","tags":"test1"},{"pinned":false,"id":1,"completed":false,"title":"test","content":"test123","tags":"test"},{"pinned":false,"id":2,"completed":false,"title":"test","content":"test12132","tags":"test12"}]} \ No newline at end of file diff --git a/src/main/java/asd_morning_9/note/JsonParser.java b/src/main/java/asd_morning_9/note/JsonParser.java index 6193f26..7365912 100644 --- a/src/main/java/asd_morning_9/note/JsonParser.java +++ b/src/main/java/asd_morning_9/note/JsonParser.java @@ -278,6 +278,51 @@ public boolean ReadNotes(String file) } } + public void ImportNotes(String file) + { + System.out.println(file); + try + { + // parsing file "JSONExample.json" + Object obj = new JSONParser().parse(new FileReader(file)); + + // typecasting obj to JSONObject + JSONObject jo = (JSONObject) obj; + + // getting notes + JSONArray ja = (JSONArray) jo.get("Notes"); + + if (notes_ != null) + + notes_.clear(); + + Iterator itr = ja.iterator(); + while (itr.hasNext()) + { + + JSONObject item = (JSONObject) itr.next(); + + String id_string = JSONValue.toJSONString(item.get("id")); + int id = Integer.parseInt(id_string); + + + + String title = item.get("title").toString(); + String content = item.get("content").toString(); + + String tags = item.get("tags").toString(); + Boolean pinned = Boolean.parseBoolean(item.get("pinned").toString()); + boolean completed = Boolean.parseBoolean(item.get("completed").toString()); + + notes_.add(new Note(id, title, content, tags, completed, pinned)); + } + } + catch (Exception e) + { + System.out.println("[ERROR IN READ NOTES] " + e.getMessage()); + } + } + // Delete note with specific id public void DeleteNote(int id) { diff --git a/src/main/java/asd_morning_9/note/ui/Dashboard/DashboardView.java b/src/main/java/asd_morning_9/note/ui/Dashboard/DashboardView.java index 348e873..162c5a8 100644 --- a/src/main/java/asd_morning_9/note/ui/Dashboard/DashboardView.java +++ b/src/main/java/asd_morning_9/note/ui/Dashboard/DashboardView.java @@ -258,9 +258,27 @@ public DashboardView() { notification.open(); })); - add(ui); + Div import_note_cont = new Div(); + + TextField file = new TextField("Import path"); + import_note_cont.add(file); + + add(import_note_cont); + + add(new Button("Import", event -> { + parser.ImportNotes(file.getValue()); + parser.SaveNotes(); + //TextField id = new TextField("id"); + //parser.SaveNotes(); + Notification notification = new Notification( + "Note was imported successfully!", 2000, + Notification.Position.MIDDLE); + notification.open(); + + })); + add(ui); } } diff --git a/src/test/java/asd_morning_9/note/JsonParserTest.java b/src/test/java/asd_morning_9/note/JsonParserTest.java index 97065b4..16478f0 100644 --- a/src/test/java/asd_morning_9/note/JsonParserTest.java +++ b/src/test/java/asd_morning_9/note/JsonParserTest.java @@ -194,6 +194,21 @@ public void ExportNotesTest() deleteTestFile(); } + @Test + public void ImportNotesTest() + { + createTestFile(); + + JsonParser parser; + ArrayList notes_; + parser = new JsonParser(test_file); + boolean result = parser.ReadNotes(test_file); + notes_ = parser.getNotesList(); + assertTrue(result); + assertEquals(expected_arr_size, notes_.size()); + + deleteTestFile(); + } @Test public void GetNewIdTest() diff --git a/test b/test new file mode 100644 index 0000000..e69de29 diff --git a/test.json b/test.json new file mode 100644 index 0000000..5c89078 --- /dev/null +++ b/test.json @@ -0,0 +1 @@ +{"Notes":[{"pinned":false,"id":0,"completed":false,"title":"test","content":"test123","tags":"test1"},{"pinned":false,"id":1,"completed":false,"title":"test","content":"test123","tags":"test"},{"pinned":false,"id":2,"completed":false,"title":"test","content":"test12132","tags":"test12"}]} \ No newline at end of file From fd4d0456d79afbed0a2d0b7c98ebfde6f52ef312 Mon Sep 17 00:00:00 2001 From: zzoorrii <47061680+zzoorrii@users.noreply.github.com> Date: Wed, 20 May 2020 11:59:33 +0200 Subject: [PATCH 2/4] #8 NOTE_008 [ZZ, HT] --- src/main/java/asd_morning_9/note/Note.java | 30 ++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/src/main/java/asd_morning_9/note/Note.java b/src/main/java/asd_morning_9/note/Note.java index 1215228..05285e0 100644 --- a/src/main/java/asd_morning_9/note/Note.java +++ b/src/main/java/asd_morning_9/note/Note.java @@ -1,5 +1,9 @@ package asd_morning_9.note; +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.Date; + public class Note { private int id; @@ -8,6 +12,8 @@ public class Note private String tags; private boolean completed; private boolean pinned; + private Date date_when_completed; + public Note(int id, String title, String content, String tags) { @@ -34,6 +40,7 @@ public Note(int id, String title, String content, Boolean completed) this.content = content; this.tags = ""; this.completed = completed; + if (completed) this.date_when_completed = new Date(); } public Note(int id, String title, String content, String tags, Boolean completed) @@ -43,11 +50,10 @@ public Note(int id, String title, String content, String tags, Boolean completed this.content = content; this.tags = tags; this.completed = completed; + if (completed) this.date_when_completed = new Date(); } - - public Note(int id, String title, String content, String tags, Boolean completed, Boolean pinned) { this.id = id; @@ -55,9 +61,22 @@ public Note(int id, String title, String content, String tags, Boolean completed this.content = content; this.tags = tags; this.completed = completed; + if (completed) this.date_when_completed = new Date(); this.pinned = pinned; } + public Note(int id, String title, String content, String tags, Boolean completed, Boolean pinned, Date date_when_completed) + { + this.id = id; + this.title = title; + this.content = content; + this.tags = tags; + this.completed = completed; + if (completed) this.date_when_completed = date_when_completed; + this.pinned = pinned; + } + + /*public Note(String value, String value1) { //hab die erste Instanz entfernt }*/ @@ -114,5 +133,12 @@ public void setCompleted(boolean completed) public boolean getPinned(){return this.pinned;} + public Date getDate_when_completed() { + return date_when_completed; + } + public void setDate_when_completed(Date date_when_completed) + { + this.date_when_completed = date_when_completed; + } } From 88e77b6c65fd12f877819b40b9a0d646ad091345 Mon Sep 17 00:00:00 2001 From: zzoorrii <47061680+zzoorrii@users.noreply.github.com> Date: Wed, 20 May 2020 12:05:25 +0200 Subject: [PATCH 3/4] #8 NOTE_008 [ZZ, GJ] --- .../java/asd_morning_9/note/JsonParser.java | 24 ++++++++++++++----- 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/src/main/java/asd_morning_9/note/JsonParser.java b/src/main/java/asd_morning_9/note/JsonParser.java index 7365912..3e7386c 100644 --- a/src/main/java/asd_morning_9/note/JsonParser.java +++ b/src/main/java/asd_morning_9/note/JsonParser.java @@ -135,7 +135,7 @@ public void EditNote(Note oldNote, Note newNote) } } - public void SaveNotes() + public void SaveNotes() { JSONObject obj = new JSONObject(); @@ -148,10 +148,13 @@ public void SaveNotes() item_obj.put("title", item.getTitle()); item_obj.put("content", item.getContent()); item_obj.put("tags", item.getTags()); - item_obj.put("completed", item.isCompleted()); item_obj.put("pinned", item.getPinned()); list.add(item_obj); + + if (item.getDate_when_completed() != null) + item_obj.put("date_when_completed", item.getDate_when_completed()); + } obj.put("Notes", list); @@ -184,10 +187,13 @@ public void SaveNotes(String path) item_obj.put("title", item.getTitle()); item_obj.put("content", item.getContent()); item_obj.put("tags", item.getTags()); - item_obj.put("completed", item.isCompleted()); item_obj.put("pinned", item.getPinned()); list.add(item_obj); + + if (item.getDate_when_completed() != null) + item_obj.put("date_when_completed", item.getDate_when_completed()); + } obj.put("Notes", list); @@ -233,8 +239,6 @@ public void ReadNotes() String id_string = JSONValue.toJSONString(item.get("id")); int id = Integer.parseInt(id_string); - - String title = item.get("title").toString(); String content = item.get("content").toString(); @@ -242,7 +246,15 @@ public void ReadNotes() Boolean pinned = Boolean.parseBoolean(item.get("pinned").toString()); boolean completed = Boolean.parseBoolean(item.get("completed").toString()); - notes_.add(new Note(id, title, content, tags, completed, pinned)); + if (item.get("date_when_completed") != null) { + String str_date_when_completed = JSONValue.toJSONString(item.get("date_when_completed")); + + DateFormat dateFormat = new SimpleDateFormat("yyyy-mm-dd"); + Date date_when_completed = dateFormat.parse(str_date_when_completed); + notes_.add(new Note(id, title, content, tags, completed, pinned, date_when_completed)); + } + else notes_.add(new Note(id, title, content, tags, completed, pinned)); + } } catch (Exception e) From ac6dff27184e7ce38096804a392376ec4aa32a3d Mon Sep 17 00:00:00 2001 From: zzoorrii <47061680+zzoorrii@users.noreply.github.com> Date: Wed, 20 May 2020 12:07:43 +0200 Subject: [PATCH 4/4] #8 NOTE_008 [ZZ, GJ] --- src/test/java/asd_morning_9/note/JsonParserTest.java | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/test/java/asd_morning_9/note/JsonParserTest.java b/src/test/java/asd_morning_9/note/JsonParserTest.java index 16478f0..01af8a7 100644 --- a/src/test/java/asd_morning_9/note/JsonParserTest.java +++ b/src/test/java/asd_morning_9/note/JsonParserTest.java @@ -309,6 +309,7 @@ public void TagsTest() } @Test + @Test public void MarkAsCompletedTest() { createTestFile(); @@ -332,7 +333,13 @@ public void MarkAsCompletedTest() assertEquals(true, notes_.get(2).getCompleted()); assertEquals(true, notes_.get(3).getCompleted()); + String pattern = "yyyy-MM-dd"; + SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern); + String date_when_completed = simpleDateFormat.format(notes_.get(1).getDate_when_completed()); + String str_today_day = simpleDateFormat.format(new Date()); + + assertEquals(str_today_day, date_when_completed); deleteTestFile(); }