-
Notifications
You must be signed in to change notification settings - Fork 0
/
DatabaseService.java
2125 lines (2028 loc) · 73 KB
/
DatabaseService.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
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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2013 UniCoPA
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package unicopa.copa.server.database;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.jdbc.ScriptRunner;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import unicopa.copa.base.UserData;
import unicopa.copa.base.ServerStatusNote;
import unicopa.copa.base.UserEventSettings;
import unicopa.copa.base.UserRole;
import unicopa.copa.base.UserSettings;
import unicopa.copa.base.event.CategoryNode;
import unicopa.copa.base.event.CategoryNodeImpl;
import unicopa.copa.base.event.Event;
import unicopa.copa.base.event.EventGroup;
import unicopa.copa.base.event.SingleEvent;
import unicopa.copa.base.event.SingleEventUpdate;
import unicopa.copa.server.GeneralUserPermission;
import unicopa.copa.server.database.data.db.DBCategoryNode;
import unicopa.copa.server.database.data.db.DBSingleEventUpdate;
import unicopa.copa.server.database.data.db.DBUserData;
import unicopa.copa.server.database.data.persistence.CategoryMapper;
import unicopa.copa.server.database.data.persistence.EventGroupMapper;
import unicopa.copa.server.database.data.persistence.EventMapper;
import unicopa.copa.server.database.data.persistence.PersonMapper;
import unicopa.copa.server.database.data.persistence.PrivilegeMapper;
import unicopa.copa.server.database.data.persistence.ServerStatusMapper;
import unicopa.copa.server.database.data.persistence.SingleEventMapper;
import unicopa.copa.server.database.data.persistence.SingleEventUpdateMapper;
import unicopa.copa.server.database.data.persistence.UserSettingMapper;
import unicopa.copa.server.database.util.DatabaseUtil;
import unicopa.copa.server.module.eventimport.model.EventGroupImport;
import unicopa.copa.server.module.eventimport.model.EventImport;
import unicopa.copa.server.module.eventimport.model.EventImportContainer;
/**
* The database service provides an interface to the database. It allows to
* obtain objects from and write objects to the database.
*
* @author Felix Wiemuth, David Knacker
*/
public class DatabaseService {
private static final String RESOURCE_SQL_INITDB = "/sql/initializeDB.sql";
private static final String RESOURCE_MYBATIS_CONFIG = "mybatis-config.xml";
private final File database;
private SqlSessionFactory sqlSessionFactory; // The session factory used to
// obtain SQL sessions in
// service methods
/**
* Create a new database service. Note: The database must already be
* initialized by calling static method 'initDB()'.
*
* @param database
* @param username
* @param password
* @throws IOException
*/
public DatabaseService(File database, String username, String password)
throws IOException {
this.database = database;
Properties properties = new Properties();
properties.setProperty("username", username);
properties.setProperty("password", password);
properties.setProperty("url",
DatabaseUtil.protocol + database.getCanonicalPath());
InputStream inputStream = Resources
.getResourceAsStream(RESOURCE_MYBATIS_CONFIG);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream,
properties);
}
public DatabaseService(File database) throws IOException {
this(database, "", "");
}
/**
* Get all event groups that match a given category and a search string.
*
* @param categoryNodeID
* the ID of the category node in the category tree whose subtree
* must contain a category node of the event group
* @param searchTerm
* the exact string the name of the event group must contain
* @return a List of EventGroups
* @throws ObjectNotFoundException
* is thrown if the given CategoryNode or one of the childNodes
* in CategeoryNode is not existend in the databse
*/
public List<EventGroup> getEventGroups(int categoryNodeID, String searchTerm)
throws ObjectNotFoundException {
List<Integer> nodeList = getAllChildNodes(categoryNodeID);
if (categoryNodeID != 0) {
nodeList.add(categoryNodeID);
}
try (SqlSession session = sqlSessionFactory.openSession()) {
EventGroupMapper mapper = session.getMapper(EventGroupMapper.class);
List<EventGroup> list = mapper.getEventGroups(nodeList, "%"
+ searchTerm + "%");
return list;
}
}
/**
* Get all events of an event group that match a given category.
*
* @param eventGroupID
* the ID of the event group to get the events from
* @param categoryNodeID
* the ID of the category node in the category tree whose subtree
* must contain a category node of the event
* @return a list of Events
* @throws ObjectNotFoundException
* is thrown if the given eventGroup, or category does not
* exists in the database
*/
public List<Event> getEvents(int eventGroupID, int categoryNodeID)
throws ObjectNotFoundException {
checkEventGroup(eventGroupID);
List<Integer> nodeList = getAllChildNodes(categoryNodeID);
if (categoryNodeID != 0) {
nodeList.add(categoryNodeID);
}
try (SqlSession session = sqlSessionFactory.openSession()) {
EventMapper mapper = session.getMapper(EventMapper.class);
List<Event> list = mapper.getEvents(eventGroupID, nodeList);
return list;
}
}
/**
* Get the eventGroup by its ID.
*
* @param eventGroupID
* the ID of the eventGroup.
* @return the EventGroup
* @throws ObjectNotFoundException
* is thrown if the given eventgroup does not exists in the
* database
*/
public EventGroup getEventGroup(int eventGroupID)
throws ObjectNotFoundException {
checkEventGroup(eventGroupID);
try (SqlSession session = sqlSessionFactory.openSession()) {
EventGroupMapper mapper = session.getMapper(EventGroupMapper.class);
EventGroup eGroup = mapper.getEventGroup(eventGroupID);
return eGroup;
}
}
/**
* Get the event by its ID.
*
* @param eventID
* the ID of the event.
* @return the event
* @throws ObjectNotFoundException
* is thrown if the given event does not exist in the database
*/
public Event getEvent(int eventID) throws ObjectNotFoundException {
try (SqlSession session = sqlSessionFactory.openSession()) {
EventMapper mapper = session.getMapper(EventMapper.class);
Event e = mapper.getEvent(eventID);
if (e == null)
throw new ObjectNotFoundException("There is no Event with ID="
+ eventID + "in the database");
return e;
}
}
/**
* Get the SingleEventUpdates of SingleEvents that belong to an event the
* user subscribed and that occured since the given date
*
* @param since
* the data from when to return updates
* @param userID
* the ID of the user
* @return the list of the singleEventUpdates
* @throws ObjectNotFoundException
* is thrown if the given user does not exist in the database
* @throws IncorrectObjectException
* is thrown if the given date is null
*/
public List<SingleEventUpdate> getSubscribedSingleEventUpdates(int userID,
Date since) throws ObjectNotFoundException,
IncorrectObjectException {
checkNull(since, "given Date");
checkUser(userID);
Set<Integer> subscribedEvents = getUserSettings(userID)
.getSubscriptions();
List<SingleEventUpdate> singleEventUpdateList = new ArrayList<>();
for (int eventID : subscribedEvents) {
singleEventUpdateList.addAll(getSingleEventUpdates(eventID, since));
}
return singleEventUpdateList;
}
/**
* Get the SingleEventUpdates that belong to the specified event and that
* occured since the given date
*
* @param eventID
* the ID of the event where to get updates from
* @param since
* the data from when to return updates
* @return the list of SingleEventUpdates
* @throws ObjectNotFoundException
* is thrown if the given event does not exist in the database
* @throws IncorrectObjectException
* is thrown if the given date is null
*/
public List<SingleEventUpdate> getSingleEventUpdates(int eventID, Date since)
throws ObjectNotFoundException, IncorrectObjectException {
checkNull(since, "given Date");
checkEvent(eventID);
try (SqlSession session = sqlSessionFactory.openSession()) {
SingleEventUpdateMapper mapper = session
.getMapper(SingleEventUpdateMapper.class);
List<DBSingleEventUpdate> dbSingleEventUpdates = mapper
.getDBSingleEventUpdates(eventID, since.getTime());
List<SingleEventUpdate> singleEventUpdates = new ArrayList<>();
for (DBSingleEventUpdate dbSingleEvent : dbSingleEventUpdates) {
// the event is not cancelled
if (dbSingleEvent.getUpdatedSingleEvent() != 0) {
singleEventUpdates.add(new SingleEventUpdate(
getSingleEvent(dbSingleEvent
.getUpdatedSingleEvent()), dbSingleEvent
.getOldSingleEventID(), new Date(
dbSingleEvent.getUpdateDate()),
dbSingleEvent.getCreatorName(), dbSingleEvent
.getComment()));
} else {// the event is cancelled, the new singleEvent is now
// null
singleEventUpdates.add(new SingleEventUpdate(null,
dbSingleEvent.getOldSingleEventID(), new Date(
dbSingleEvent.getUpdateDate()),
dbSingleEvent.getCreatorName(), dbSingleEvent
.getComment()));
}
}
return singleEventUpdates;
}
}
/**
* Checks if there is a Event with ID = eventID in the database, if not a
* ObjectNotFound Exception is thrown, else true is returned
*
* @param eventID
* @return true if the given event exists in the database, false if not
* @throws ObjectNotFoundException
* is thrown if the given event does not exist in the database
*/
private boolean eventExists(int eventID) throws ObjectNotFoundException {
try (SqlSession session = sqlSessionFactory.openSession()) {
EventMapper mapper = session.getMapper(EventMapper.class);
int a = mapper.eventExists(eventID);
if (a == 0)
return false;
return true;
}
}
/**
* Get a list of user-IDs of users that are subscribers for the event.
*
* @param eventID
* the event ID for the event the users should have subscribed
* to.
* @return a list of user-IDs of the subscribers
* @throws ObjectNotFoundException
* is thrown if the given event does not exist in the database
*/
public List<Integer> getSubscribedUserIDs(int eventID)
throws ObjectNotFoundException {
checkEvent(eventID);
try (SqlSession session = sqlSessionFactory.openSession()) {
PersonMapper mapper = session.getMapper(PersonMapper.class);
List<Integer> iDList = mapper.getSubscribedUserIDs(eventID);
return iDList;
}
}
/**
* Get the ID of a user.
*
* @param userName
* the user name of the user
* @return the userID
* @throws ObjectNotFoundException
* is thrown if the given user does not exist in the database
*/
public int getUserID(String userName) throws ObjectNotFoundException {
try (SqlSession session = sqlSessionFactory.openSession()) {
PersonMapper mapper = session.getMapper(PersonMapper.class);
Integer userID = mapper.getUserID(userName);
if (userID == null)
throw new ObjectNotFoundException("There is no user with name="
+ userName + " in the database");
return userID;
}
}
/**
* Get the ID of a user by his email address.
*
* @param email
* the email address of the user
* @return the userID
* @throws ObjectNotFoundException
* is thrown if the given email does not match to a entry in the
* database
*/
public int getUserIDByEmail(String email) throws ObjectNotFoundException {
if (!emailExsists(email))
throw new ObjectNotFoundException("There is no entry with email="
+ email + " in the database");
try (SqlSession session = sqlSessionFactory.openSession()) {
PersonMapper mapper = session.getMapper(PersonMapper.class);
int userID = mapper.getUserIDByEmail(email);
return userID;
}
}
/**
* Get the email address of the given user.
*
* @param userID
* the user-ID
* @return the E-Mail address as String
* @throws ObjectNotFoundException
* is thrown if the given user does not exist in the database
*/
public String getEmailAddress(int userID) throws ObjectNotFoundException {
checkUser(userID);
try (SqlSession session = sqlSessionFactory.openSession()) {
PersonMapper mapper = session.getMapper(PersonMapper.class);
String email = mapper.getEmailAddress(userID);
return email;
}
}
/**
* Get the user settings of the given user.
*
* @param userID
* the user-ID
* @return the UserSettings for the user
* @throws ObjectNotFoundException
* is thrown if the given user does not exist in the database
*/
public UserSettings getUserSettings(int userID)
throws ObjectNotFoundException {
checkUser(userID);
try (SqlSession session = sqlSessionFactory.openSession()) {
UserSettingMapper mapper = session
.getMapper(UserSettingMapper.class);
HashSet<String> uGCMKeys = mapper.getUserGCMKey(userID);
Boolean eMailNoty = mapper.getEmailNotification(userID);
String language = mapper.getLanguage(userID);
List<Map<String, Integer>> listEventColor = mapper
.getEventColors(userID);
Map<Integer, UserEventSettings> eventSettings = new HashMap<>();
for (Map<String, Integer> map : listEventColor) {
if (map.get("EVENTID") != 0) {
eventSettings.put(
map.get("EVENTID"),
new UserEventSettings(String.valueOf(map
.get("COLOR"))));
}
}
return new UserSettings(uGCMKeys, eMailNoty, language,
eventSettings);
}
}
/**
* Get the SingleEvent.
*
* @param id
* the ID of the SingleEvent
* @returns the singleEvent
* @throws ObjectNotFoundException
* is thrown if the given singleEvent does not exist in the
* database
*/
public SingleEvent getSingleEvent(int id) throws ObjectNotFoundException {
try (SqlSession session = sqlSessionFactory.openSession()) {
SingleEventMapper mapper = session
.getMapper(SingleEventMapper.class);
SingleEvent sEH = mapper.getSingleEvent(id);
if (sEH == null)
throw new ObjectNotFoundException(
"There is no SingleEvent with ID=" + id
+ " in the database");
return sEH;
}
}
/**
* Get all (still valid) SingleEvents for an Event where the scheduled date
* is past the given date.
*
* @param eventID
* the ID of the event where to get the current SingleEvents from
* @param since
* the date since when SingleEvents should be returned
* @return the list of singleEvents
* @throws ObjectNotFoundException
* is thrown if the given event does not exist in the database
* @throws IncorrectObjectException
* is thrown if the given date is null
*/
public List<SingleEvent> getCurrentSingleEvents(int eventID, Date since)
throws ObjectNotFoundException, IncorrectObjectException {
checkNull(since, "given Date");
checkEvent(eventID);
try (SqlSession session = sqlSessionFactory.openSession()) {
SingleEventMapper mapper = session
.getMapper(SingleEventMapper.class);
List<SingleEvent> singleEventList = mapper.getCurrentSingleEvent(
eventID, since.getTime());
return singleEventList;
}
}
/**
*
* Get the names of rightholders for an event.
*
* @param eventID
* the ID of the event
* @param appointedByUserID
* the ID of the user that appointed the rightholders to be
* returned, '-1' means all users
* @return the list of the rightholder names
* @throws ObjectNotFoundException
* is thrown if the given event or the given user does not exist
* in the database
*/
public List<String> getRightholders(int eventID, int appointedByUserID)
throws ObjectNotFoundException {
checkEvent(eventID);
if (appointedByUserID != -1)
checkUser(appointedByUserID);
try (SqlSession session = sqlSessionFactory.openSession()) {
PrivilegeMapper mapper = session.getMapper(PrivilegeMapper.class);
List<String> privList = mapper.getPrivileged(eventID,
appointedByUserID, 1);
return privList;
}
}
/**
* Get the names of all rightholders for an event.
*
* @param eventID
* the ID of the event
* @return the list of the rightholder names
* @throws ObjectNotFoundException
* is thrown if the given event does not exist in the database
*/
public List<String> getRightholders(int eventID)
throws ObjectNotFoundException {
return getRightholders(eventID, -1);
}
/**
* Get all events where a user holds higher roles. The map returned maps
* from RIGHTHOLDER, DEPUTY and OWNER to the IDs of the events where the
* user hold this role.
*
* @param userID
* the ID of the user
* @return the map with the userRole and the associated List of userIDs
* @throws ObjectNotFoundException
* is thrown if the given user does not exist in the database
*/
public Map<UserRole, List<Integer>> getUsersPriviligedEvents(int userID)
throws ObjectNotFoundException {
checkUser(userID);
try (SqlSession session = sqlSessionFactory.openSession()) {
PrivilegeMapper mapper = session.getMapper(PrivilegeMapper.class);
List<Map<String, Integer>> userPrivList = mapper
.getUsersPriviligedEvents(userID);
List<Integer> ownerEvents = new ArrayList<>();
List<Integer> deputyEvents = new ArrayList<>();
List<Integer> rightholderEvents = new ArrayList<>();
for (Map<String, Integer> result : userPrivList) {
switch (result.get("KINDOFPRIVILEGE")) {
case (1):
rightholderEvents.add(result.get("EVENTID"));
break;
case (2):
deputyEvents.add(result.get("EVENTID"));
break;
case (3):
ownerEvents.add(result.get("EVENTID"));
break;
}
}
Map<UserRole, List<Integer>> userPrivMap = new HashMap<>();
userPrivMap.put(UserRole.OWNER, ownerEvents);
userPrivMap.put(UserRole.DEPUTY, deputyEvents);
userPrivMap.put(UserRole.RIGHTHOLDER, rightholderEvents);
return userPrivMap;
}
}
/**
* Get all users a user gave higher roles to. The map returned maps from
* DEPUTY and OWNER to the user data of the users who got this role.
*
* @param userID
* the ID of the user who gave the roles
* @return the map with the userRole and the associated List of userIDs
* @throws ObjectNotFoundException
* is thrown if the given user does not exist in the database
*/
public Map<UserRole, List<UserData>> getUsersAppointedUsers(int userID)
throws ObjectNotFoundException {
try (SqlSession session = sqlSessionFactory.openSession()) {
PrivilegeMapper mapper = session.getMapper(PrivilegeMapper.class);
List<DBUserData> dbUserDataList = mapper
.getUsersAppointedUsers(userID);
List<UserData> ownerList = new ArrayList<>();
List<UserData> deputyList = new ArrayList<>();
for (DBUserData user : dbUserDataList) {
switch (user.getKindOfPrivilege()) {
case (2):
deputyList.add(new UserData(user.getFirstname() + " "
+ user.getFamilyname(), user.getEmail()));
break;
case (3):
ownerList.add(new UserData(user.getFirstname() + " "
+ user.getFamilyname(), user.getEmail()));
break;
}
}
Map<UserRole, List<UserData>> usersAppointedUsers = new HashMap<>();
usersAppointedUsers.put(UserRole.DEPUTY, deputyList);
usersAppointedUsers.put(UserRole.OWNER, ownerList);
return usersAppointedUsers;
}
}
/**
* Check whether a user is appointed by another user with a special role at
* a specific event.
*
* @param userID
* the ID of the user who to check to be appointed
* @param appointedByUserID
* the ID of the user who appointed
* @param eventID
* the ID of the event
* @param role
* the role the appointed user should have
* @return true if the user granted the privilege to the user for the given
* event, else false
* @throws IncorrectObjectException
* is thrown if the given userRole is invalid
* @throws ObjectNotFoundException
* is thrown if the given user does not exist in the database
*/
public boolean isAppointedBy(int userID, int appointedByUserID,
int eventID, UserRole role) throws ObjectNotFoundException,
IncorrectObjectException {
checkEvent(eventID);
checkUser(appointedByUserID);
checkUser(userID);
try (SqlSession session = sqlSessionFactory.openSession()) {
PrivilegeMapper mapper = session.getMapper(PrivilegeMapper.class);
int kindOfPrivilege = 0;
switch (role) {
case DEPUTY:
kindOfPrivilege = 2;
break;
case OWNER:
kindOfPrivilege = 3;
break;
case RIGHTHOLDER:
kindOfPrivilege = 1;
break;
default:
throw new IncorrectObjectException(
"isAppointedBy not defined for UserRole = " + role);
}
Integer rows = mapper.isAppointedBy(userID, appointedByUserID,
eventID, kindOfPrivilege);
if (rows == 0)
return false;
return true;
}
}
/**
* Get the child nodes of the node categoryID
*
* @param categoryID
* the ID of the node
* @return the categoryID list of the child nodes
* @throws ObjectNotFoundException
* is thrown if the given category does not exist in the
* database
*/
private List<Integer> getChildNodeIDs(int categoryID)
throws ObjectNotFoundException {
try (SqlSession session = sqlSessionFactory.openSession()) {
CategoryMapper mapper = session.getMapper(CategoryMapper.class);
List<Integer> nodeList = mapper.getChildNodeIDs(categoryID);
if (nodeList == null)
throw new ObjectNotFoundException(
"There is no Category with ID=" + categoryID
+ " in the database");
return nodeList;
}
}
/**
* Uses getChildNodes recursive to get all leaves that are below the node
* categoryID
*
* @param categoryID
* the ID of the node
* @return a category id list of all children of the given category (childs,
* gradchildren,...)
* @throws ObjectNotFoundException
* is thrown if the given category does not exist in the
* database
*/
private List<Integer> getAllChildNodes(int categoryID)
throws ObjectNotFoundException {
List<Integer> nodeList = new ArrayList<>();
nodeList.clear();
if (getChildNodeIDs(categoryID).isEmpty()) {
return nodeList;
} else {
nodeList = getChildNodeIDs(categoryID);
for (int i = 0; i < nodeList.size(); i++) {
nodeList.addAll(getAllChildNodes(nodeList.get(i)));
}
return nodeList;
}
}
/**
* Get the role a user holds in general. This will be either UserRole.USER
* or UserRole.ADMINISTRATOR.
*
* @param userID
* the ID of the user
* @return the role the user holds
* @throws ObjectNotFoundException
* is thrown if the given user does not exist in the database
*/
public UserRole getUserRole(int userID) throws ObjectNotFoundException {
return getUsersRoleForEvent(userID, 0);
}
/**
* Get the role a user holds for a specific event. The roles will be checked
* and returned in the following order: UserRole.ADMINISTRATOR if the user
* holds this role in general, UserRole.RIGHTHOLER, UserRole.DEPUTY,
* UserRole.OWNER if the user holds the role for the specified event
* UserRole.User otherwise.
*
* @param userID
* the ID of the user
* @param eventID
* the ID of the event
* @return the role the user holds for the specified event
* @throws ObjectNotFoundException
* is thrown if the given user or the event does not exist in
* the database
*/
public UserRole getUsersRoleForEvent(int userID, int eventID)
throws ObjectNotFoundException {
checkEvent(eventID);
checkUser(userID);
try (SqlSession session = sqlSessionFactory.openSession()) {
PersonMapper mapper = session.getMapper(PersonMapper.class);
Map<String, Integer> result = mapper.isAdmin(userID);
if (result == null) {
Integer priv = mapper.getPrivilege(userID, eventID);
if (priv == null)
return UserRole.USER;
switch (priv) {
case 1:
return UserRole.RIGHTHOLDER;
case 2:
return UserRole.DEPUTY;
case 3:
return UserRole.OWNER;
}
} else
return UserRole.ADMINISTRATOR;
}
return null;
}
/**
* gives the given user the admin privilege
*
* @param userID
* @throws ObjectNotFoundException
* is thrown if the given user does not exist in the database
*/
public void addAdministrator(int userID) throws ObjectNotFoundException {
checkUser(userID);
try (SqlSession session = sqlSessionFactory.openSession()) {
PrivilegeMapper mapper = session.getMapper(PrivilegeMapper.class);
mapper.insertAdmin(userID, new Date().getTime());
session.commit();
}
}
/**
* removes the admin privilege of the given User
*
* @param userID
* @throws ObjectNotFoundException
* is thrown if the given user does not exist in the database
*/
public void removeAdministrator(int userID) throws ObjectNotFoundException {
checkUser(userID);
try (SqlSession session = sqlSessionFactory.openSession()) {
PrivilegeMapper mapper = session.getMapper(PrivilegeMapper.class);
mapper.deleteAdmin(userID);
session.commit();
}
}
/**
* Set the role of a user for a specific event.
*
* @param userID
* the ID of the user
* @param evenID
* the ID of the event where to set the role for the user
* @param role
* the role to set
* @param gavePrivilegeID
* the ID of the user that permitted the privilege
* @throws IncorrectObjectException
* is thrown if the given userRole is invalid
* @throws ObjectNotFoundException
* is thrown if one of the given users or the event does not
* exist in the database
*/
public void setUserRoleForEvent(int userID, int eventID, UserRole role,
int gavePrivilegeID) throws IncorrectObjectException,
ObjectNotFoundException {
int kindOfPrivilege = 0;
switch (role) {
case DEPUTY:
kindOfPrivilege = 2;
break;
case OWNER:
kindOfPrivilege = 3;
break;
case RIGHTHOLDER:
kindOfPrivilege = 1;
break;
case USER:
break;
default:
throw new IncorrectObjectException(
"setUserRoleForEvent not defined for UserRole = " + role);
}
if (hasPrivfor(userID, eventID) != 0)
removePrivilege(userID, eventID);
if (role != UserRole.USER)
insertPrivilege(userID, eventID, kindOfPrivilege, gavePrivilegeID,
new Date());
}
/**
* Get the names of deputies for an event.
*
* @param eventID
* the ID of the event
* @param appointedByUserID
* the ID of the user that appointed the deputies to be returned,
* '-1' means all users
* @return the list of user names
* @throws ObjectNotFoundException
* is thrown if the given user or the event does not exist in
* the database
*/
public List<String> getDeputies(int eventID, int appointedByUserID)
throws ObjectNotFoundException {
checkEvent(eventID);
if (appointedByUserID != -1)
checkUser(appointedByUserID);
try (SqlSession session = sqlSessionFactory.openSession()) {
PrivilegeMapper mapper = session.getMapper(PrivilegeMapper.class);
List<String> privList = mapper.getPrivileged(eventID,
appointedByUserID, 2);
return privList;
}
}
/**
* Get the names of all deputies for an event.
*
* @param eventID
* the ID of the event
* @return the list of user names
* @throws ObjectNotFoundException
* is thrown if the given event does not exist in the database
*/
public List<String> getDeputies(int eventID) throws ObjectNotFoundException {
return getDeputies(eventID, -1);
}
/**
* Get the names of all owners for an event.
*
* @param eventID
* the ID of the event
* @return the list of user names
* @throws ObjectNotFoundException
* is thrown if the given event does not exist in the database
*/
public List<String> getOwners(int eventID) throws ObjectNotFoundException {
checkEvent(eventID);
try (SqlSession session = sqlSessionFactory.openSession()) {
PrivilegeMapper mapper = session.getMapper(PrivilegeMapper.class);
List<String> privList = mapper.getPrivileged(eventID, -1, 3);
return privList;
}
}
/**
* Returns the list of userIDs of all users that have the given familyName
* and have a generalUserPermission that is higher or equal to the given one
*
* @param familyName
* @param generalUserPermission
* @return
* @throws IncorrectObjectException
* is thrown if the given gerneralUserPermission is not valid
*/
public List<Integer> getUserByFamilyNameWithPermission(String familyName,
GeneralUserPermission generalUserPermission)
throws IncorrectObjectException {
int permission = 0;
switch (generalUserPermission) {
case POSSIBLE_OWNER:
permission = 1;
break;
case NONE:
permission = 0;
break;
default:
throw new IncorrectObjectException(generalUserPermission
+ " is no valid GeneralUserPermission");
}
try (SqlSession session = sqlSessionFactory.openSession()) {
PersonMapper mapper = session.getMapper(PersonMapper.class);
List<Integer> permList = mapper.getUserByFamilyNameWithPermission(
familyName, permission);
return permList;
}
}
/**
* Get the categoryNodeImpl with ID=categoryID.
*
* @param categoryID
* the ID of the category
* @return the categoryNodeImpl
* @throws ObjectNotFoundException
* is thrown if the given category does not exist in the
* database
*/
public CategoryNodeImpl getCategoryTree(int categoryID)
throws ObjectNotFoundException {
CategoryNodeImpl catTree = null;
try (SqlSession session = sqlSessionFactory.openSession()) {
CategoryMapper mapper = session.getMapper(CategoryMapper.class);
DBCategoryNode dBCategoryNode;
if (mapper.getChildNodeIDs(categoryID).isEmpty()) {
dBCategoryNode = mapper.getDBCategoryNodeLeaf(categoryID);
} else {
dBCategoryNode = mapper.getDBCategoryNode(categoryID);
}
if (dBCategoryNode == null)
throw new ObjectNotFoundException(
"There is no Category with ID=" + categoryID
+ " in the database");
catTree = new CategoryNodeImpl(dBCategoryNode.getId(),
dBCategoryNode.getName());
for (int child : dBCategoryNode.getChildren()) {
catTree.addChildNode(getCategoryTree(child));
}
}
return catTree;
}
/**
* Update the UserSettings of the User with ID = userID
*
* @param userSetting
* the new UserSettings
* @param userID
* the userID
* @throws ObjectNotFoundException
* is thrown if the given user does not exist in the database
* @throws IncorrectObjectException
* is thrown if the given userSettings is null, the colorCode in
* the given UserSettings is invalid or the gCMKey in the given
* UserSettings is invalid
* @throws ObjectAlreadyExsistsException
* is thrown if there already is another user with the given
* gCMKey in the database
*/
public void updateUserSetting(UserSettings userSetting, int userID)
throws ObjectNotFoundException, IncorrectObjectException,
ObjectAlreadyExsistsException {
checkNull(userSetting, "given UserSettings");
checkUser(userID);
if (userSetting.getGCMKeys() != null) {
for (String gcmKey : userSetting.getGCMKeys()) {
checkNull(gcmKey, "given gcmKey");
checkString(gcmKey, 300);
}
}
deleteAllGCMKeys(userID);
if (userSetting.getGCMKeys() != null) {
for (String gcmKey : userSetting.getGCMKeys()) {
if (existsGCMKey(gcmKey))
throw new ObjectAlreadyExsistsException(
"There is already an GCMKey with value=" + gcmKey
+ " in the database");
}
}
try (SqlSession session = sqlSessionFactory.openSession()) {
UserSettingMapper mapper = session
.getMapper(UserSettingMapper.class);
if (userSetting.getGCMKeys() != null
&& !userSetting.getGCMKeys().isEmpty()) {
mapper.insertGCMKeys(userSetting.getGCMKeys(), userID);
}
mapper.updatePerson(userSetting.getLanguage(),
userSetting.isEmailNotificationEnabled(), userID);
mapper.deleteAllSubscriptions(userID);
for (int eventID : userSetting.getSubscriptions()) {
if (eventID != 0) {
if (userSetting.getEventSettings(eventID) != null) {
if (userSetting.getEventSettings(eventID)
.getColorCode() == null) {
userSetting.getEventSettings(eventID).setColorCode(
"000000");
}
checkColor(userSetting.getEventSettings(eventID)
.getColorCode());
mapper.insertSubscription(eventID, userSetting
.getEventSettings(eventID).getColorCode(),