-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDatabaseHelper.java
355 lines (315 loc) · 12 KB
/
DatabaseHelper.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
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
package com.example.bilkentcompanion;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;
import com.example.chat.R;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
/**
* the ones in ** are specific generated/user entered name
* the ones not in ** are tags( child names ) int the database
* methods follow the patterns of get.../set.../initialise...
* get methods return the reference to data so get the values by DataSnaphot.getValue()
* <p>
* DataBase Structure
* Chat Requests
* *UID*
* *Sender/Receiver ID*
* *Sent or Received*
* Groups
* *Group Name*
* *Message ID*
* date
* message
* name
* time
* Users
* *User ID*
* name
* uid
* email
* question
* answer
* contacts
* LostPost
* *Post ID*
* name
* uid
* category
* tags
* date
* key
* uploader_name
* SellPost
* *Post ID*
* name
* uid
* category
* tags
* price
* key
* uploader_name
*/
public class DatabaseHelper {
//User
/**
* @return the current users's id
*/
public static String getUID() {
return FirebaseAuth.getInstance().getCurrentUser().getUid();
}
/**
* @param uid UserID to get info from
* @return name of the user with the specified key
*/
public static String getName(String uid) {
return FirebaseDatabase.getInstance().getReference().child("Users").child(uid)
.child("name").toString();
}
/**
* @param uid UserID to get info from
* @return email of the user with the specified key
*/
public static String getEmail(String uid) {
return FirebaseDatabase.getInstance().getReference().child("Users").child(uid)
.child("email").toString();
}
/**
* @param uid UserID to get info from
* @return Security question of the user with the specified key
*/
public static String getQuestion(String uid) {
return FirebaseDatabase.getInstance().getReference().child("Users").child(uid)
.child("question").toString();
}
/**
* @param uid UserID to get info from
* @return Answer to the security question of the user with the specified key
*/
public static String getAnswer(String uid) {
return FirebaseDatabase.getInstance().getReference().child("Users").child(uid)
.child("answer").toString();
}
//LostPost
/**
* @param infos String array of information to put in the database with the indexes representing;
* 0 - name
* 1 - uid
* 2 - category
* 3 - tags
* 4 - date
* 5 - uploader's name
* @return key of the post
*/
public static DatabaseReference initialiseLostPost(String[] infos) {
DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("LostPost")
.push();
HashMap<String, String> properties = new HashMap<>();
properties.put("name", infos[0]);
properties.put("uid", infos[1]);
properties.put("category", infos[2]);
properties.put("tags", infos[3]);
properties.put("date", infos[4]);
properties.put("key", ref.getKey());
properties.put("uploader_name", infos[5]);
ref.setValue(properties);
return ref;
}
/**
* @param postID key of the post to get info from
* @return The UserID of the user that created the post
*/
public static DatabaseReference getLostPostUID(String postID) {
return FirebaseDatabase.getInstance().getReference().child("LostPost")
.child(postID).child("uid");
}
/**
* @param postID key of the post to get info from
* @return Tags of the post in String array form
*/
public static DatabaseReference getLostPostTags(String postID) {
return FirebaseDatabase.getInstance().getReference().child("LostPost")
.child(postID).child("tags");
// return s.split("\\W+");
}
/**
* @param postID key of the post to get info from
* @return Date of the post in DD-MM-YYYY form
*/
public static DatabaseReference getLostPostDate(String postID) {
return FirebaseDatabase.getInstance().getReference().child("LostPost")
.child(postID).child("date");
}
/**
* @param postID key of the post to get info from
* @return Name of the post
*/
public static DatabaseReference getLostPostName(String postID) {
return FirebaseDatabase.getInstance().getReference().child("LostPost")
.child(postID).child("name");
}
/**
* @param postID key of the post to get info from
* @return Category of the post
*/
public static DatabaseReference getLostPostCategory(String postID) {
return FirebaseDatabase.getInstance().getReference().child("LostPost")
.child(postID).child("category");
}
/**
* @param postID ID of the post to change the values
* @param data New name of the post
*/
public static void setLostPostName(String postID, String data) {
FirebaseDatabase.getInstance().getReference().child("LostPost")
.child(postID).child("name").setValue(data);
}
/**
* @param postID ID of the post to change the values
* @param data New UID of the post
*/
public static void setLostPostUID(String postID, String data) {
FirebaseDatabase.getInstance().getReference().child("LostPost")
.child(postID).child("uid").setValue(data);
}
/**
* @param postID ID of the post to change the values
* @param data New category of the post
*/
public static void setLostPostCategory(String postID, String data) {
FirebaseDatabase.getInstance().getReference().child("LostPost")
.child(postID).child("category").setValue(data);
}
/**
* @param postID ID of the post to change the values
* @param data New tags of the post in the form of a String where each tag is separated with a space
*/
public static void setLostPostTags(String postID, String data) {
FirebaseDatabase.getInstance().getReference().child("LostPost")
.child(postID).child("tags").setValue(data);
}
/**
* @param postID ID of the post to change the values
* @param data New date of the post
*/
public static void setLostPostDate(String postID, String data) {
FirebaseDatabase.getInstance().getReference().child("LostPost")
.child(postID).child("date").setValue(data);
}
/**
* @param key key of the post to be deleted
*/
public static void deleteLostPost( String key ) {
DatabaseReference ref = FirebaseDatabase.getInstance().getReference( "LostPost" ).child(key);
ref.removeValue();
}
//SellPost
/**
* @param infos String array of information to put in the database with the indexes representing;
* 0 - name
* 1 - uid
* 2 - category
* 3 - tags
* 4 - date
* 5 - uploader's name
* @return key of the post
*/
public static DatabaseReference initialiseSellPost(String[] infos) {
DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("SellPost").push();
HashMap<String, String> properties = new HashMap<>();
properties.put("name", infos[0]);
properties.put("uid", infos[1]);
properties.put("category", infos[2]);
properties.put("tags", infos[3]);
properties.put("price", infos[4]);
properties.put("key", ref.getKey());
properties.put("uploader_name", infos[5]);
ref.setValue(properties);
return ref;
}
/**
* @param postID UserID to get info from
* @return UID of the creator of the post with the specified key
*/
public static DatabaseReference getSellPostUID(String postID) {
return FirebaseDatabase.getInstance().getReference().child("SellPost")
.child(postID).child("uid");
}
/**
* @param postID UserID to get info from
* @return Tags of the post with the specified key
*/
public static DatabaseReference getSellPostTags(String postID) {
return FirebaseDatabase.getInstance().getReference().child("SellPost")
.child(postID).child("tags");
//return s.split("\\W+");
}
/**
* @param postID UserID to get info from
* @return Name of the post with the specified key
*/
public static DatabaseReference getSellPostName(String postID) {
return FirebaseDatabase.getInstance().getReference().child("SellPost")
.child(postID).child("name");
}
/**
* @param postID UserID to get info from
* @return Category of the post with the specified key
*/
public static DatabaseReference getSellPostCategory(String postID) {
return FirebaseDatabase.getInstance().getReference().child("SellPost")
.child(postID).child("category");
}
/**
* @param postID ID of the post to change the values
* @param data New name of the post
*/
public static void setSellPostName(String postID, String data) {
FirebaseDatabase.getInstance().getReference().child("SellPost")
.child(postID).child("name").setValue(data);
}
/**
* @param postID ID of the post to change the values
* @param data New UID of the post
*/
public static void setSellPostUID(String postID, String data) {
FirebaseDatabase.getInstance().getReference().child("SellPost")
.child(postID).child("uid").setValue(data);
}
/**
* @param postID ID of the post to change the values
* @param data New category of the post
*/
public static void setSellPostCategory(String postID, String data) {
FirebaseDatabase.getInstance().getReference().child("SellPost")
.child(postID).child("category").setValue(data);
}
/**
* @param postID ID of the post to change the values
* @param data New tags of the post in the form of a String where each tag is separated with a space
*/
public static void setSellPostTags(String postID, String data) {
FirebaseDatabase.getInstance().getReference().child("SellPost")
.child(postID).child("tags").setValue(data);
}
/**
* @param postID ID of the post to change the values
* @param data New price of the post
*/
public static void setSellPostPrice(String postID, String data) {
FirebaseDatabase.getInstance().getReference().child("SellPost")
.child(postID).child("price").setValue(data);
}
/**
* @param key key of the post to be deleted
*/
public static void deleteSellPost( String key ) {
DatabaseReference ref = FirebaseDatabase.getInstance().getReference( "SellPost" ).child(key);
ref.removeValue();
}
}