1
1
package data_access ;
2
2
3
- import interface_adapters . IRetrieveList ;
4
- import interface_adapters . user_registration_interface_adapters . UserExists ;
5
- import entities . user_entities .* ;
6
- import entities . user_entities . UserFactory ;
7
- import use_cases . user_login_use_case . UserCreator ;
8
- import interface_adapters .UserRetriever ;
3
+ import entities . userEntities . User ;
4
+ import entities . userEntities . UserFactory ;
5
+ import interface_adapters . Chat . ConvHistGateway ;
6
+ import interface_adapters . Chat . MsgSenderGateway ;
7
+ import interface_adapters . Chat . UserChatGateway ;
8
+ import interface_adapters .User .* ;
9
9
10
10
import java .io .*;
11
11
import java .util .ArrayList ;
12
12
import java .util .List ;
13
- public class UserDatabase implements UserExists , UserRetriever , UserCreator , IRetrieveList {
13
+ public class UserDatabase implements UserExists , UserRetriever , UserCreator , IRetrieveList , UserModificationGateway ,
14
+ ConvHistGateway , MsgSenderGateway , UserChatGateway {
14
15
File accounts ;
15
16
List <User > accountList ;
16
17
public UserDatabase (){
@@ -38,6 +39,16 @@ public boolean UserExists(String username, String email) {
38
39
return false ;
39
40
}
40
41
42
+ @ Override
43
+ public boolean UserExists (String username ) {
44
+ for (User user : this .accountList ){
45
+ if (user .getUsername ().equals (username )){
46
+ return true ;
47
+ }
48
+ }
49
+ return false ;
50
+ }
51
+
41
52
// Creates a new user with a username and password, and an email address
42
53
// The order is username, password, email address, verification status, status
43
54
//
@@ -62,9 +73,9 @@ public void createUser(String username, String password, String email, String ty
62
73
// To be edited to get user from the array format rather than the serialized format.
63
74
public User getUser (String username ) {
64
75
User ans = null ;
65
- for (int i = 0 ; i < (this .getList () .size ()); i ++) {
66
- if (this .getList () .get (i ).getUsername ().equals (username )) {
67
- ans = this .getList () .get (i );
76
+ for (int i = 0 ; i < (this .accountList .size ()); i ++) {
77
+ if (this .accountList .get (i ).getUsername ().equals (username )) {
78
+ ans = this .accountList .get (i );
68
79
}
69
80
}
70
81
return ans ;
@@ -78,11 +89,88 @@ public List<User> getList() {
78
89
ObjectInputStream in = new ObjectInputStream (fileIn )) {
79
90
80
91
users = (ArrayList <User >) in .readObject ();
92
+ /*
93
+ while(true){
94
+ try{
95
+ entities.userEntities.User user = (entities.userEntities.User) in.readObject();
96
+ users.add(user);}
97
+ catch(EOFException e){
98
+ break;
99
+ }*/
81
100
return users ;
82
101
}catch (EOFException e ){
83
102
return users ;
84
103
} catch (IOException | ClassNotFoundException ex ) {
85
104
throw new RuntimeException (ex );
86
105
}
87
106
}
107
+
108
+ @ Override
109
+ public void modifyUser (String oldUsername , User modified ){
110
+ // swap in modified user to accountList
111
+ this .accountList .remove (this .getUser (oldUsername ));
112
+ this .accountList .add (modified );
113
+
114
+ // overwrite the serialized file
115
+ try (FileOutputStream fileOut = new FileOutputStream (accounts )){
116
+ try (ObjectOutputStream out = new ObjectOutputStream (fileOut )){
117
+ out .writeObject (this .accountList );
118
+ out .close ();
119
+ fileOut .close ();
120
+ }catch (Exception e ){
121
+ System .out .println ("Error" );
122
+ }
123
+ }catch (Exception e ){
124
+ System .out .println ("Error" );
125
+ }
126
+ }
127
+
128
+
129
+ // Below two methods are used by conversation history-related interactors
130
+ // (Commented as objects are not found)
131
+ // /**
132
+ // * Pushes a new message to a chat's conversation history (in memory not persisting storage)
133
+ // * @param dsRequestModel input data containing user ID, chat ID, message content
134
+ // */
135
+ // public void saveMessage(MsgSenderDsRequestModel dsRequestModel) {
136
+ // String userID = dsRequestModel.getUserID();
137
+ // String chatID = dsRequestModel.getChatID();
138
+ // Message message = dsRequestModel.getMessage();
139
+ //
140
+ // // Find chat under specified entities.userEntities.User
141
+ // Chat chat = this.getUser(userID).getChat(chatID);
142
+ //
143
+ // chat.addMessage(message);
144
+ // }
145
+ //
146
+ // /**
147
+ // * Gets a chat's conversation history (from memory not persisting storage)
148
+ // * @param dsRequestModel input data containing user ID, chat ID
149
+ // * @return a chat's conversation history
150
+ // */
151
+ // public ArrayList<Message> getConversationHistory(ConvHistDsRequestModel dsRequestModel) {
152
+ // String userID = dsRequestModel.getUserID();
153
+ // String chatID = dsRequestModel.getChatID();
154
+ //
155
+ // // Find chat under specified entities.userEntities.User
156
+ // Chat chat = this.getUser(userID).getChat(chatID);
157
+ //
158
+ // return Chat.getConversationHistory();
159
+ // }
160
+
161
+
162
+
163
+
164
+ // This method will get a user's chats; this will be used by AppScreenLoader to display chats and could
165
+ // also be used by ChatInteractor (Chat entity is undefined here and at the moment user doesn't have chats)
166
+ // @Override
167
+ // public ArrayList<Chat> getUserChats(String username) {
168
+ // for (entities.userEntities.User user: accountList){
169
+ // if (user.getUsername().equals(username)){
170
+ // return user.getChats();
171
+ // }
172
+ // }
173
+ // throw new RuntimeException("Invalid username: user does not exist");
174
+ // }
175
+
88
176
}
0 commit comments