-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDataWriter.java
134 lines (103 loc) · 3.98 KB
/
DataWriter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
//@author Logan Praylow
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
public class DataWriter extends DataConstants {
public static void saveUsers() {
UserList users = UserList.getInstance();
ArrayList<User> userList = users.getUserList();
JSONArray jsonUsers = new JSONArray();
//creating all the json objects
for(int i=0; i< userList.size(); i++) {
jsonUsers.add(getUserJSON(userList.get(i)));
}
//Write JSON file
try (FileWriter file = new FileWriter(USER_FILE_NAME)) {
file.write(jsonUsers.toJSONString());
file.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
public static JSONObject getUserJSON(User user) {
JSONObject userDetails = new JSONObject();
userDetails.put(USER_ID, user.getId().toString());
userDetails.put(USER_USER_NAME, user.getUserName());
userDetails.put(USER_FIRST_NAME, user.getFirstName());
userDetails.put(USER_LAST_NAME, user.getLastName());
userDetails.put(USER_PASSWORD, user.getPassword());
userDetails.put(USER_EMAIL, user.getEmail());
userDetails.put(USER_PRIVACY, user.getPrivacyLevel());
return userDetails;
}
public static void saveProjects() {
ProjectList projects = ProjectList.getInstance();
ArrayList<Project> projectList = projects.getProjectList();
JSONArray jsonProjects = new JSONArray();
//creating all the json objects
for(int i=0; i< projectList.size(); i++) {
jsonProjects.add(getProjectJSON(projectList.get(i)));
}
//Write JSON file
try (FileWriter file = new FileWriter(PROJECT_FILE_NAME)) {
file.write(jsonProjects.toJSONString());
file.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
public static JSONObject getProjectJSON(Project project) {
JSONObject projectDetails = new JSONObject();
projectDetails.put(PROJECT_ID, project.getId().toString());
projectDetails.put(PROJECT_NAME, project.getProjectName());
projectDetails.put(PROJECT_DESCRIPTION, project.getProjectDesc());
projectDetails.put(PROJECT_AUTHOR, project.getProjectAuthor());
//projectDetails.put(PROJECT_TASK, project.getTasks());
return projectDetails;
}
public static void saveTasks() {
TaskList task = TaskList.getInstance();
ArrayList<Task> taskList = task.geTasksList();
JSONArray jsonTask = new JSONArray();
//creating all the json objects
for(int i=0; i< taskList.size(); i++) {
jsonTask.add(getTaskJSON(taskList.get(i)));
}
//Write JSON file
try (FileWriter file = new FileWriter(TASK_FILE_NAME)) {
file.write(jsonTask.toJSONString());
file.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
public static JSONObject getTaskJSON(Task task) {
JSONArray jsonComments = new JSONArray();
JSONObject taskDetails = new JSONObject();
ArrayList<Comment> comments = task.getComments();
taskDetails.put(TASK_ID, task.getId().toString());
taskDetails.put(TASK_NAME, task.getTaskName());
taskDetails.put(TASK_DESC, task.getTaskDes());
taskDetails.put(TASK_PRIO, task.getTaskPrio());
taskDetails.put(TASK_CATE, task.getCategory().toString());
taskDetails.put(TASK_PRIV, task.getTaskPrivacyString());
taskDetails.put(TASK_INPROGRESS, task.getInProgressString());
taskDetails.put(TASK_COLOR, task.getColor());
for(int i=0; i< comments.size(); i++) {
JSONObject commentJSON = getCommentJSON(comments.get(i));
jsonComments.add(commentJSON);
}
taskDetails.put(TASK_THREAD, jsonComments);
return taskDetails;
}
public static JSONObject getCommentJSON(Comment comment) {
JSONObject CommentDetail = new JSONObject();
//System.out.println(comment.getAuthor()+" "+comment.getText()+" "+comment.GetDate());
CommentDetail.put(COMMENT_AUTH, comment.getAuthor());
CommentDetail.put(COMMENT, comment.getText());
CommentDetail.put(COMMENT_DATE, comment.GetDate());
return CommentDetail;
}
}