Skip to content

Commit 8b6a514

Browse files
committed
Added handling of group admins as well as kicking and leaving of groups with backend
1 parent 9a65602 commit 8b6a514

File tree

4 files changed

+71
-15
lines changed

4 files changed

+71
-15
lines changed

src/main/java/ie/tcd/pavel/JoinCreateGroupPage.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ public JoinCreateGroupPage()
7474
if(!database.groupExists(newGroupNameField.getValue())) {
7575
database.insertGroup(newGroupNameField.getValue(), newGroupPassField.getValue(),
7676
SecurityUtils.getUsername());
77+
database.makeAdmin(SecurityUtils.getUsername(), newGroupNameField.getValue());
7778
dialog1.close();
7879
debugLabel.setText("Group Created Successfully");
7980
System.out.printf("[DEBUG] Group Created: Name - %s | Password - %s%n", newGroupNameField.getValue(), newGroupPassField.getValue());

src/main/java/ie/tcd/pavel/MainPage.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
public class MainPage extends AppLayout implements BeforeEnterObserver {
3636

3737
private HashMap<Tab, Component> tabMap = new HashMap<>();
38-
private Image background = new Image("https://i.imgur.com/Ua18y2v.jpg", "Background");
38+
private Image background = new Image("https://i.imgur.com/rrlI3vE.jpg", "Background");
3939

4040

4141
public MainPage() {

src/main/java/ie/tcd/pavel/MongoDBOperations.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,15 @@ public void removeUserFromGroup(String user, String group)
325325
mongoTemplate.remove(groupToRemove);
326326
}
327327

328-
329-
328+
public void deleteGroupFinal(String name)
329+
{
330+
Query findAdmins = new Query(Criteria.where("groupName").is(name));
331+
List<GroupAdmin> admins = mongoTemplate.query(GroupAdmin.class).matching(findAdmins).all();
332+
for(GroupAdmin admin : admins) {
333+
mongoTemplate.remove(admin);
334+
}
335+
Query searchGroupPassword = new Query(Criteria.where("name").is(name));
336+
GroupPassword password = mongoTemplate.findOne(searchGroupPassword,GroupPassword.class);
337+
mongoTemplate.remove(password);
338+
}
330339
}

src/main/java/ie/tcd/pavel/MyGroupsPage.java

Lines changed: 58 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
public class MyGroupsPage extends VerticalLayout {
2121

2222
MongoDBOperations database;
23+
ArrayList<Details> groupDetails;
24+
ArrayList<Group> groups;
2325

2426
public MyGroupsPage()
2527
{
@@ -28,26 +30,71 @@ public MyGroupsPage()
2830
/**setHeightFull();
2931
setAlignItems(Alignment.CENTER);
3032
setJustifyContentMode(JustifyContentMode.CENTER);**/
33+
generateGroupDetails();
34+
}
3135

32-
ArrayList<Group> groups = (ArrayList<Group>) database.getGroupsByUser(SecurityUtils.getUsername());
33-
ArrayList<Details> groupDetails = new ArrayList<>();
34-
36+
private void generateGroupDetails() {
37+
groups = (ArrayList<Group>) database.getGroupsByUser(SecurityUtils.getUsername());
38+
groupDetails = new ArrayList<>();
3539
if(!groups.isEmpty()) {
3640
for (Group g : groups) {
3741
Details group = new Details();
38-
group.setSummaryText(g.getName());
42+
if(database.checkIfAdmin(SecurityUtils.getUsername(), g.getName())) {
43+
group.setSummaryText(g.getName() + " (Owned)");
44+
} else {
45+
group.setSummaryText(g.getName());
46+
}
3947
Button leaveButton = new Button("Leave");
48+
leaveButton.addClickListener(buttonClickEvent -> {
49+
database.removeUserFromGroup(SecurityUtils.getUsername(), g.getName());
50+
// Check if no users are left then run database.deleteGroupPassword
51+
if(database.getUsersByGroup(g.getName()).size() <= 0) {
52+
database.deleteGroupFinal(g.getName());
53+
}
54+
removeAll();
55+
generateGroupDetails();
56+
});
4057
ArrayList<User> users = (ArrayList<User>) database.getUsersByGroup(g.getName());
4158
if(!users.isEmpty()) {
4259
for (User u : users) {
4360
// Only show this version if the user owns the group
44-
Details userSubDetails = new Details();
45-
Button button = new Button("Kick");
46-
userSubDetails.setSummaryText(u.getLogin());
47-
userSubDetails.addContent(button);
48-
Label padding = new Label();
49-
padding.getStyle().set("width", "1em");
50-
group.addContent(new HorizontalLayout(padding, userSubDetails), leaveButton);
61+
if(database.checkIfAdmin(SecurityUtils.getUsername(), g.getName())) {
62+
Details userSubDetails = new Details();
63+
boolean isAdmin = database.checkIfAdmin(u.getLogin(), g.getName());
64+
if(isAdmin) {
65+
userSubDetails.setSummaryText(u.getLogin() + " (Admin)");
66+
} else {
67+
userSubDetails.setSummaryText(u.getLogin());
68+
}
69+
if(!isAdmin) {
70+
Label padding = new Label();
71+
padding.getStyle().set("width", "1em");
72+
Button kickButton = new Button("Kick");
73+
kickButton.setDisableOnClick(true);
74+
kickButton.addClickListener(event -> {
75+
database.removeUserFromGroup(u.getLogin(), g.getName());
76+
});
77+
userSubDetails.addContent(new HorizontalLayout(padding, kickButton));
78+
}
79+
Label padding = new Label();
80+
padding.getStyle().set("width", "1em");
81+
Label padding2 = new Label();
82+
padding2.getStyle().set("width", "1em");
83+
group.addContent(new HorizontalLayout(padding, userSubDetails), new HorizontalLayout(padding2, leaveButton));
84+
} else {
85+
Details userSubDetails = new Details();
86+
boolean isAdmin = database.checkIfAdmin(u.getLogin(), g.getName());
87+
if(isAdmin) {
88+
userSubDetails.setSummaryText(u.getLogin() + " (Admin)");
89+
} else {
90+
userSubDetails.setSummaryText(u.getLogin());
91+
}
92+
Label padding = new Label();
93+
padding.getStyle().set("width", "1em");
94+
Label padding2 = new Label();
95+
padding2.getStyle().set("width", "1em");
96+
group.addContent(new HorizontalLayout(padding, userSubDetails), new HorizontalLayout(padding2, leaveButton));
97+
}
5198
}
5299
}
53100
groupDetails.add(group);
@@ -58,6 +105,5 @@ public MyGroupsPage()
58105
} else {
59106
add(new H3("You are not a member of any groups!"));
60107
}
61-
62108
}
63109
}

0 commit comments

Comments
 (0)