-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathAddFriends.java
More file actions
145 lines (130 loc) · 4.62 KB
/
AddFriends.java
File metadata and controls
145 lines (130 loc) · 4.62 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
package example;
import java.util.ArrayList;
import java.util.List;
import example.models.User;
import org.apache.wicket.MarkupContainer;
import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.form.FormComponent;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.protocol.http.WebSession;
/**
* AddFriends has a query section for finding a user that may exist
* on the system, as well as an action for friending/defriending that
* user if they exist. A user cannot friend himself.
*/
public class AddFriends extends Base {
private String username;
private String query;
private Boolean found;
private Boolean act;
public AddFriends(final PageParameters parameters) {
super(parameters);
username = ((TwissSession) WebSession.get()).getUname();
query = parameters.getString("query");
if (query == null) {
query = "";
}
found = parameters.getAsBoolean("found");
act = parameters.getAsBoolean("act");
add(new FriendForm("friendfinder"));
WebMarkupContainer action = new WebMarkupContainer("action") {
@Override
public boolean isVisible() {
return (found != null) && (found) && (username != null) && (!query.equals(username));
}
};
add(new Label("flash", getFlashMsg()));
Form aff = new ActionFriendForm("actionfriend");
String actiontext = "";
List<String> friendUnames = getFriendUnames(username);
if (action.isVisible()) {
if (friendUnames.contains(query)) {
actiontext = "Remove Friend ";
}
else {
actiontext = "Add Friend ";
}
}
aff.add(new Label("actionname", actiontext));
//TODO : I really don't like not having it on the button itself. ;_;
action.add(aff);
add(action);
}
private String getFlashMsg() {
if (act != null) {
if (act) {
return "Friendship was established.";
}
if (!act) {
return "Friendship was broken.";
}
}
if (found != null) {
if (!found) {
return query + " is not here.";
}
if (found) {
if ((query != null) && (query.equals(username))) {
return "You attempt to befriend yourself and fail.";
}
else {
return query + " is here!";
}
}
}
//default nothing
return "";
}
private class FriendForm extends Form {
private String q;
public FriendForm(String id) {
super(id);
add(new TextField("q", new PropertyModel(this,"q")));
}
@Override
public void onSubmit() {
User test = getUserByUsername(q);
if (test == null) {
PageParameters p = new PageParameters();
p.put("query",q);
p.put("found",false);
setResponsePage(getPage().getClass(), p);
}
else {
PageParameters p = new PageParameters();
p.put("query",q);
p.put("found",true);
setResponsePage(getPage().getClass(), p);
}
}
}
//<p>Hooray, this page exists!</p>
//<p>There was nobody with username {{ q }}</p>
//<p>Enter a username above to see if they are on the site!</p>
private class ActionFriendForm extends Form {
public ActionFriendForm(String id) {
super(id);
}
@Override
public void onSubmit() {
List<String> friendUnames = getFriendUnames(username);
if (friendUnames.contains(query)) {
List<String> friendname = new ArrayList<String>();
friendname.add(query);
removeFriends(username, friendname);
setResponsePage(getPage().getClass(), new PageParameters("act=false"));
}
else {
List<String> friendname = new ArrayList<String>();
friendname.add(query);
addFriends(username, friendname);
setResponsePage(getPage().getClass(), new PageParameters("act=true"));
}
}
}
}