-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathData.java
More file actions
459 lines (446 loc) · 23.9 KB
/
Data.java
File metadata and controls
459 lines (446 loc) · 23.9 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import org.json.simple.parser.JSONParser;
import org.json.JSONArray;
import org.json.JSONObject;
import org.json.simple.*;
import org.json.simple.parser.ParseException;
public class Data {
public static void writeData() {
// Creating some sample data
// Convert User ARRAY to JSON
//=============user=================================
JSONArray UserJson = new JSONArray();
for (User user : Main.vec) {
JSONObject userjson = new JSONObject();
userjson.put("User ID", user.getUserID());
userjson.put("username", user.getUserName());
userjson.put("name", user.getName());
userjson.put("email", user.getEmail());
userjson.put("password", user.getPassword());
userjson.put("birth date", user.getBirthdateString());
userjson.put("phone number", user.getPhoneNumber());
userjson.put("gender", user.getGender());
UserJson.put(userjson);
}
// Writing JSON to a file
try (FileWriter file = new FileWriter("user.json",false)) {
file.write(UserJson.toString(4)); // Use toString(int) for indentation
System.out.println("JSON file created successfully.");
} catch (IOException e) {
System.out.println("JSON file not work.");
}
//==========posts===============================
JSONArray postsArray = new JSONArray();
for (User user:Main.vec) {
for (Post post : user.getAllPosts()) {
JSONObject postJson = new JSONObject();
postJson.put("User ID", user.getUserID());
postJson.put("Post ID", user.getAllPosts().indexOf(post));
postJson.put("content", post.getContent());
postJson.put("time stamp", post.getTimestamp());
postJson.put("Number of reacts", post.getReacts());
postJson.put("Number of comments", post.getNumberOfComments());
JSONArray taggedusers = new JSONArray();
for (User tagged: post.getTaggedUsers()) {
JSONObject taggedfile = new JSONObject();
taggedfile.put("Username",tagged.getUserName());
taggedusers.put(taggedfile);
}
postJson.put("tagged users",taggedusers);
postsArray.put(postJson);
}
}
// Writing JSON to a file
try (FileWriter file = new FileWriter("Posts.json",false)) {
file.write(postsArray.toString(4)); // Use toString(int) for indentation
System.out.println("JSON file created successfully.");
} catch (IOException e) {
System.out.println("JSON file not work.");
}
//===================comments========================
JSONArray commentsArray = new JSONArray();
for (User user:Main.vec) {
for (Post post: user.getAllPosts()) {
for (Comment comment : post.getAllComments()) {
JSONObject commentJson = new JSONObject();
commentJson.put("User ID", user.getUserID());
int postID= user.getAllPosts().indexOf(post);
commentJson.put("Post ID",postID);
commentJson.put("comment ID", user.getAllPosts().get(postID).getAllComments().indexOf(comment));
commentJson.put("comment reacts", comment.getReacts());
commentJson.put("comment content", comment.getContent());
commentJson.put("commenter", comment.getAuthor().getUserName());
commentsArray.put(commentJson);
}
}
}
// Writing JSON to a file
try (FileWriter file = new FileWriter("comments.json",false)) {
file.write(commentsArray.toString(4)); // Use toString(int) for indentation
System.out.println("JSON file created successfully.");
} catch (IOException e) {
System.out.println("JSON file not work.");
}
//======================replays======================
JSONArray replaysArray = new JSONArray();
for (User user:Main.vec) {
for (Post post: user.getAllPosts()) {
for (Comment comment : post.getAllComments()) {
for (Reply replay : comment.getUserReplies()) {
JSONObject replayjson = new JSONObject();
replayjson.put("User ID", user.getUserID());
int postID= user.getAllPosts().indexOf(post);
replayjson.put("Post ID", postID);
int commentID=user.getAllPosts().get(postID).getAllComments().indexOf(comment);
replayjson.put("Comment id", commentID);
replayjson.put("reply id",user.getAllPosts().get(postID).getAllComments().get(commentID).getUserReplies().indexOf(replay) );
replayjson.put("reply content", replay.getContent());
replayjson.put("replier", replay.getAuthor().getUserName());
replaysArray.put(replayjson);
}
}
}
}
// Writing JSON to a file
try (FileWriter file = new FileWriter("replays.json",false)) {
file.write(replaysArray.toString(4)); // Use toString(int) for indentation
System.out.println("JSON file created successfully.");
} catch (IOException e) {
System.out.println("JSON file not work.");
}
//===================================messenger=====================
JSONArray conversationsArray = new JSONArray();
for (User user:Main.vec) {
if(user.getMessenger()!=null) {
for (Conversation conversation : user.getMessenger().getConversations()) {
JSONObject conversationJson = new JSONObject();
conversationJson.put("User ID", user.getUserID());
conversationJson.put("Messanger ID", user.getMessenger().getMessengerid());
conversationJson.put("conversationID", conversation.getConversation_id());
conversationJson.put("conversation time", conversation.getTime());
conversationJson.put("conversation status", conversation.getStatus());
JSONArray partecipants = new JSONArray();
for (User partcipant : conversation.getParticipants()) {
JSONObject paticipant_id = new JSONObject();
paticipant_id.put("Username", partcipant.getUserName());
}
conversationJson.put("partcipent", partecipants);
conversationsArray.put(conversationJson);
}
}
}
try (FileWriter file = new FileWriter("messanger.json",false)) {
file.write(conversationsArray.toString(4)); // Use toString(int) for indentation
System.out.println("JSON file created successfully.");
} catch (IOException e) {
System.out.println("JSON file not work.");
}
//============================
JSONArray MessageArray = new JSONArray();
for (User user:Main.vec) {
if(user.getMessenger()!=null) {
for (Conversation conversation : user.getMessenger().getConversations()) {
for (Messages Message : conversation.getMessages()) {
JSONObject messagejson = new JSONObject();
messagejson.put("user id", user.getUserID());
int messengerid=user.getMessenger().getMessengerid();
messagejson.put("messenger id", messengerid);
messagejson.put("Conversation id",user.getMessenger().getConversations().indexOf(conversation));
messagejson.put("message id", Message.getId());
messagejson.put("message content", Message.getContent());
messagejson.put("message sender_id", Message.getSender_ID());
messagejson.put("message status", Message.getStatus());
messagejson.put("message timestamp", Message.getTimestamp());
messagejson.put("message reacts", Message.getReacts());
}
}
}
}
try (FileWriter file = new FileWriter("messages.json",false)) {
file.write(MessageArray.toString(4)); // Use toString(int) for indentation
System.out.println("JSON file created successfully.");
} catch (IOException e) {
System.out.println("JSON file not work.");
}
//=================================
JSONArray replayArray = new JSONArray();
for (User user:Main.vec) {
if(user.getMessenger()!=null) {
for (Conversation conversation : user.getMessenger().getConversations()) {
for (Messages Message : conversation.getMessages()) {
for (Reply messagereply : Message.getMessageReplies()) {
JSONObject messageReplayjson = new JSONObject();
messageReplayjson.put("user id", user.getUserID());
int messengerid=user.getMessenger().getMessengerid();
messageReplayjson.put("Messenger", messengerid);
int conversationid=user.getMessenger().getConversations().indexOf(conversation);
messageReplayjson.put("conversation id", conversationid);
int messageID=user.getMessenger().getConversations().get(conversationid).getMessages().indexOf(Message);
messageReplayjson.put("Message id", messageID);
messageReplayjson.put("reacts", messagereply.getReacts());
messageReplayjson.put("replay id",user.getMessenger().getConversations().get(conversationid).getMessages().get(messageID).getMessageReplies().indexOf(messagereply) );
messageReplayjson.put("replay content", messagereply.getContent());
messageReplayjson.put("replayer", messagereply.getAuthor().getUserName());
replayArray.put(messagereply);
}
}
}
}}
try (FileWriter file = new FileWriter("messageReplay.json",false)) {
file.write(replaysArray.toString(4)); // Use toString(int) for indentation
System.out.println("JSON file created successfully.");
} catch (IOException e) {
System.out.println("JSON file not work.");
}
//================================================
JSONArray FriendshipArray = new JSONArray();
if (Main.Friendships!=null){
for (FriendShip friend : Main.Friendships) {
JSONObject Friendjson = new JSONObject();
Friendjson.put("User1", friend.getUser1().getUserName());
Friendjson.put("User2", friend.getUser2().getUserName());
Friendjson.put("role1", friend.getFriend1_Role());
Friendjson.put("role2", friend.getFriend2_Role());
Friendjson.put("status", friend.getFriendship_status());
Friendjson.put("Timestamp", friend.getFriendsSince());
}
}
try (FileWriter file = new FileWriter("friendship.json",false)) {
file.write(FriendshipArray.toString(4));// Use toString(int) for indentation
System.out.println("JSON file created successfully.");
} catch (IOException e) {
System.out.println("JSON file not work.");
}
}
public static void readData() {
//===============users===================================
try {
// Read JSON file content as a String
String jsonString = new String(Files.readAllBytes(Paths.get("user.json")));
// Parse the JSON string into a JSONArray
JSONArray jsonArray = new JSONArray(jsonString);
// Iterate through the JSONArray
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
// Access object properties
int Id = jsonObject.getInt("User ID");
String username = jsonObject.getString("username");
String name = jsonObject.getString("name");
String email = jsonObject.getString("email");
String password = jsonObject.getString("password");
String birth_date = jsonObject.getString("birth date");
Date dateOfBirth=new SimpleDateFormat("yyyy-MM-dd").parse(birth_date);
Long phonenumber=jsonObject.getLong("phone number");
String gender = jsonObject.getString("gender");
User user=new User(username,password);
user.setName(name);
user.setPhoneNumber(phonenumber);
user.setEmail(email);
user.setBirthdate(dateOfBirth);
if(gender.equals("female")){
user.setGender(User.GenderOptions.FEMALE);
}else{
user.setGender(User.GenderOptions.MALE);
}
Main.vec.add(user);
}
} catch (Exception e) {
e.printStackTrace();
}
//======================================posts========================================
try {
// Read JSON file content as a String
String jsonString = new String(Files.readAllBytes(Paths.get("Posts.json")));
// Parse the JSON string into a JSONArray
JSONArray jsonArray = new JSONArray(jsonString);
// Iterate through the JSONArray
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
// Access object properties
Post post=new Post();
User user=Main.vec.get( jsonObject.getInt("User ID"));
post.setAuthor(user);
post.setPostId( jsonObject.getInt("Post ID"));
post.setContent(jsonObject.getString("content"));
post.setCntReacts( jsonObject.getInt("Number of reacts"));
post.setNumberOfComments( jsonObject.getInt("Number of comments"));
JSONArray r=jsonObject.getJSONArray("tagged users");
/* if(!r.isEmpty()){
for (Object obj:r) {
String str = (String) obj;
post.addtaggedUsers(Feed.GetUserData(str));
}
}*/
user.addApost(post);
}
} catch (Exception e) {
e.printStackTrace();
}
//==============================================comments===============
try {
// Read JSON file content as a String
String jsonString = new String(Files.readAllBytes(Paths.get("comments.json")));
// Parse the JSON string into a JSONArray
JSONArray jsonArray = new JSONArray(jsonString);
// Iterate through the JSONArray
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
String content=jsonObject.getString("comment content");
Comment comment=new Comment(content);
User user=Main.vec.get( jsonObject.getInt("User ID"));
comment.setAuthor(user);
int post_id= jsonObject.getInt("Post ID");
comment.setCommentId( jsonObject.getInt("comment ID"));
String time = jsonObject.getString("time stamp");
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
Date parsedDate = dateFormat.parse(time);
comment.setTimestamp(new Timestamp(parsedDate.getTime()));
comment.setCntReacts( jsonObject.getInt("comment reacts"));
comment.setAuthor(Feed.GetUserData(jsonObject.getString("commenter")));
user.getAllPosts().get(post_id).add_comment(comment);
}
} catch (Exception e) {
e.printStackTrace();
}
//================================================replays=================
try {
// Read JSON file content as a String
String jsonString = new String(Files.readAllBytes(Paths.get("replays.json")));
// Parse the JSON string into a JSONArray
JSONArray jsonArray = new JSONArray(jsonString);
// Iterate through the JSONArray
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
String content=jsonObject.getString("reply content");
Reply reply=new Reply(content);
User user=Main.vec.get( jsonObject.getInt("User ID"));
reply.setAuthor(user);
int post_id= jsonObject.getInt("Post ID");
int commentId= jsonObject.getInt("Comment id");
reply.setReplyId( jsonObject.getInt("reply id"));
reply.setAuthor(Feed.GetUserData(jsonObject.getString("replier")));
user.getAllPosts().get(post_id).getAllComments().get(commentId).addReply(reply);
}
} catch (Exception e) {
e.printStackTrace();
}
//==================================messenger=============
try {
// Read JSON file content as a String
String jsonString = new String(Files.readAllBytes(Paths.get("messanger.json")));
// Parse the JSON string into a JSONArray
JSONArray jsonArray = new JSONArray(jsonString);
// Iterate through the JSONArray
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
// Access object properties
int user_Id = jsonObject.getInt("User ID");
int Messangerid = jsonObject.getInt("Messanger ID");
int conversationid = jsonObject.getInt("conversationID");
String time = jsonObject.getString("conversation time");
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
Date parsedDate = dateFormat.parse(time);
String stat = jsonObject.getString("conversation status");
ArrayList <User> part=new ArrayList<>();
for (Object obj:jsonObject.getJSONArray("partcipent")) {
String str = (String) obj;
part.add(Feed.GetUserData(str));
}
Main.vec.get(user_Id).setMessenger(Main.vec.get(user_Id));
Conversation con=new Conversation(part);
con.setStatus(stat);
con.setTimestamp(new Timestamp(parsedDate.getTime()));
Main.vec.get(user_Id).getMessenger().addconversations(con);
}
} catch (Exception e) {
e.printStackTrace();
}
//====================================messages===========
try {
// Read JSON file content as a String
String jsonString = new String(Files.readAllBytes(Paths.get("messages.json")));
// Parse the JSON string into a JSONArray
JSONArray jsonArray = new JSONArray(jsonString);
// Iterate through the JSONArray
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
// Access object properties
int user_Id = jsonObject.getInt("User ID");
int Messangerid = jsonObject.getInt("messenger id");
int conversationid = jsonObject.getInt("Conversation id");
int messageId = jsonObject.getInt("message id");
String content=jsonObject.getString("message content");
int senderID = jsonObject.getInt("message sender_id");
String stat = jsonObject.getString("message status");
String time = jsonObject.getString("message timestamp");
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
Date parsedDate = dateFormat.parse(time);
int reacts = jsonObject.getInt("message reacts");
Messages m=new Messages(senderID,content);
m.setTimestamp(new Timestamp(parsedDate.getTime()));
m.setStatus(stat);
m.setCntReacts(reacts);
Main.vec.get(user_Id).getMessenger().getConversations().get(conversationid).getMessages().add(m);
}
} catch (Exception e) {
e.printStackTrace();
}
//===============================================message reply
try {
// Read JSON file content as a String
String jsonString = new String(Files.readAllBytes(Paths.get("messageReplay.json")));
// Parse the JSON string into a JSONArray
JSONArray jsonArray = new JSONArray(jsonString);
// Iterate through the JSONArray
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
// Access object properties
int user_Id = jsonObject.getInt("user id");
int Messangerid = jsonObject.getInt("Messenger");
int conversationid = jsonObject.getInt("conversation id");
int messageId = jsonObject.getInt("Message id");
int replyId = jsonObject.getInt("replay id");
int reacts = jsonObject.getInt("reacts");
String content=jsonObject.getString("replay content");
String username = jsonObject.getString("replayer");
Reply r=new Reply(content);
r.setCntReacts(reacts);
r.setAuthor(Feed.GetUserData(username));
Main.vec.get(user_Id).getMessenger().getConversations().get(conversationid).getMessages().get(messageId).addreplay(r);
}
} catch (Exception e) {
e.printStackTrace();
}
//=====================================friendship=======
try {
// Read JSON file content as a String
String jsonString = new String(Files.readAllBytes(Paths.get("friendship.json")));
// Parse the JSON string into a JSONArray
JSONArray jsonArray = new JSONArray(jsonString);
// Iterate through the JSONArray
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
// Access object properties
String user1 = jsonObject.getString("User1");
String user2 = jsonObject.getString("User2");
int role1=jsonObject.getInt("role1");
int role2=jsonObject.getInt("role2");
String status = jsonObject.getString("status");
String time = jsonObject.getString("conversation time");
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
Date parsedDate = dateFormat.parse(time);
FriendShip f=new FriendShip(Feed.GetUserData(user1),Feed.GetUserData(user2),role1,role2,status,new Timestamp(parsedDate.getTime()));
Main.Friendships.add(f);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}