Skip to content

Commit

Permalink
Merge pull request #5 from sw20-tug/develop
Browse files Browse the repository at this point in the history
Update fork
  • Loading branch information
magi92 authored May 20, 2020
2 parents df09d97 + a41618a commit 82f050b
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 14 deletions.
6 changes: 1 addition & 5 deletions config.json
Original file line number Diff line number Diff line change
@@ -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"}]}
69 changes: 63 additions & 6 deletions src/main/java/asd_morning_9/note/JsonParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public void EditNote(Note oldNote, Note newNote)
}
}

public void SaveNotes()
public void SaveNotes()
{
JSONObject obj = new JSONObject();

Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -233,16 +239,22 @@ 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();

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));
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)
Expand Down Expand Up @@ -278,6 +290,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)
{
Expand Down
30 changes: 28 additions & 2 deletions src/main/java/asd_morning_9/note/Note.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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)
{
Expand All @@ -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)
Expand All @@ -43,21 +50,33 @@ 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;
this.title = title;
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
}*/

Expand Down Expand Up @@ -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;
}
}
20 changes: 19 additions & 1 deletion src/main/java/asd_morning_9/note/ui/Dashboard/DashboardView.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);

}
}
22 changes: 22 additions & 0 deletions src/test/java/asd_morning_9/note/JsonParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,21 @@ public void ExportNotesTest()

deleteTestFile();
}
@Test
public void ImportNotesTest()
{
createTestFile();

JsonParser parser;
ArrayList<Note> 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()
Expand Down Expand Up @@ -314,6 +329,7 @@ public void TagsTest()
}

@Test
@Test
public void MarkAsCompletedTest()
{
createTestFile();
Expand All @@ -337,7 +353,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();
}

Expand Down
Empty file added test
Empty file.
1 change: 1 addition & 0 deletions test.json
Original file line number Diff line number Diff line change
@@ -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"}]}

0 comments on commit 82f050b

Please sign in to comment.