String lastName = request.getParameter(LAST_NAME_PARAM);
String summary = request.getParameter(SUMMARY_PARAM);
String actionType = request.getParameter(ACTION_TYPE_PARAM);
ModelFacade mf = (ModelFacade) context.getAttribute(MF_KEY);
String returnURL = null;
String path = request.getPathInfo();
if (path != null && path.equals("/login")) {
String fUrl = "/site.jsp";
String user_name = request.getParameter("user_name");
UserBean userBean = (UserBean) request.getSession(true).getAttribute("userBean");
if (userBean == null) {
userBean = new UserBean();
userBean.setModelFacade(mf);
request.getSession().setAttribute("userBean", userBean);
}
if (user_name != null && password != null) {
Person p = mf.login(user_name, password);
if (p != null) {
userBean.setDisplayMessage("Successfully logged in");
userBean.setLoggedInPerson(p);
}
} else {
userBean.setDisplayMessage("Log in failed for user_name = " + user_name +
" password = " + password);
}
fUrl = userBean.getForwardingUrl();
if (fUrl == null) {
fUrl = request.getContextPath() + "/event/list";
} else {
fUrl = request.getContextPath() + fUrl;
// We will do a redirect rather than a forward so that refreshing the browser
// does not cause issues
}
response.sendRedirect(fUrl);
return null;
}
if (actionType != null) {
if (actionType.equalsIgnoreCase("display_person")) {
String username = request.getParameter("user_name");
Person displayUser = mf.findPerson(username);
if (displayUser != null) {
//get posted events
Collection<SocialEvent> userEvents = mf.getPostedEvents(displayUser);
request.setAttribute("userEvents", userEvents);
//logger.finer("the size of the incoming friendships is "+ displayUser.getIncomingInvitations().size());
request.setAttribute("displayPerson", displayUser);
returnURL = "/site.jsp?page=personContent.jsp";
}
} else if (actionType.equalsIgnoreCase("display_friends")) {
String username = request.getParameter("user_name");
Person displayUser = mf.findPerson(username);
request.setAttribute("displayPerson", displayUser);
//addition here for pages
int index = WebappUtil.getIntProperty(request.getParameter("index"));
List<Person> list = (List)displayUser.getFriends();
if (list != null) {
List friendList = WebappUtil.getPagedList(list, index);
request.setAttribute("numPages", WebappUtil.getNumPages(list));
request.setAttribute("pageUrl", request.getContextPath() + "/person?user_name="+username+"&page=friends.jsp&actionType=display_friends");
request.setAttribute("itemList", friendList);
request.setAttribute("index", index);
}
returnURL = "/site.jsp?page=friends.jsp";
} else if (actionType.equalsIgnoreCase("add_person")) {
//should not go here - we are doing this via PersonRestAction
//this.addPerson(request, response);
returnURL = "/site.jsp?page=addPerson.jsp";
} else if (actionType.equalsIgnoreCase("edit_person")) {
request.setAttribute("isEditable", Boolean.TRUE);
returnURL = "/site.jsp?page=addPerson.jsp";
} else if (actionType.equalsIgnoreCase("display_myPostedEvents")) {
String username = request.getParameter("user_name");
Person displayUser = mf.findPerson(username);
if (displayUser != null) {
//get posted events
Collection<SocialEvent> myPostedEvents = mf.getPostedEvents(displayUser);
logger.finer("the size of myPostedEvents is " + myPostedEvents.size());
request.setAttribute("myPostedEvents", myPostedEvents);
}
returnURL = "/site.jsp?page=eventsPostedByUser.jsp";
} else if (actionType.equalsIgnoreCase("Search")) {
String query = request.getParameter("query");
//need to specify max results
Collection<Person> searchResults = mf.searchUsers(query, 0);
//have all of the users - now get the friends
Person requestor = mf.findPerson(userName);
UserBean userBean = (UserBean) request.getSession().getAttribute("userBean");
Person loggedInPerson = userBean.getLoggedInPerson();
String loggedInPersonUsername = loggedInPerson.getUserName();
Collection<Person> friends = loggedInPerson.getFriends();
logger.finer("the size of the loggedInPerson's friends " + loggedInPerson.getUserName() + " is " + friends.size());
//invitations
Collection<Invitation> invites = loggedInPerson.getIncomingInvitations();
logger.finer("the size of the invitations list is " + invites.size());
//iterate through and remove the friends from the list
//need to remove loggedInPerson too since you cannot have yourself as a friend
Iterator<Person> it = searchResults.iterator();
while (it.hasNext()) {
Person person = it.next();
Iterator<Person> friendIterator = friends.iterator();
Iterator<Invitation> invitationIter = invites.iterator();
while (friendIterator.hasNext()) {
String fUsername = friendIterator.next().getUserName();
if (fUsername.equalsIgnoreCase(person.getUserName()) || fUsername.equalsIgnoreCase(loggedInPerson.getUserName())) {
it.remove();
}
}
//determine whether they are received their invitation already
while (invitationIter.hasNext()) {
Invitation inv = invitationIter.next();
if (inv.getCandidate().getUserName().equalsIgnoreCase(person.getUserName())) {
person.setHasReceivedInvitation(true);
}
}
}
//logger.finer("after sorting, the size of the collection is " + searchResults.size());
//not in session request.getSession().setAttribute("searchResults", searchResults);
loggedInPerson.setNonFriendList(searchResults);
if (searchResults != null) {
request.setAttribute("searchResults", searchResults);
}
returnURL = "/site.jsp?page=searchUsers.jsp";
} else if (actionType.equalsIgnoreCase("addDeleteFriend")) {
String query = request.getParameter("query");
String friendUsername = request.getParameter("friend");
//this is returning null - SecurityHandler.getInstance().getLoggedInPerson(request);
UserBean userBean = (UserBean) request.getSession().getAttribute("userBean");
Person loggedInPerson = userBean.getLoggedInPerson();
Collection<Person> previousSearchResults = loggedInPerson.getNonFriendList();
///add or delete
String flag = request.getParameter("flag");
logger.finer("*** flag is " + flag);
if (flag.equals("add")) {
Person friend = mf.findPerson(friendUsername);
Invitation invitation = new Invitation(loggedInPerson, friend);
mf.addInvitation(loggedInPerson, invitation);
//iterate through and set new added friend's hasReceivedInvitation to true
Iterator<Person> searchIter = previousSearchResults.iterator();
while (searchIter.hasNext()) {
Person eachPerson = searchIter.next();
if (eachPerson.getUserName().equalsIgnoreCase(friendUsername)) {
eachPerson.setHasReceivedInvitation(true);
logger.finer("user " + eachPerson.getUserName() + " status is " + eachPerson.isHasReceivedInvitation());
}
}
} else if (flag.equals("delete")) {
Invitation inv = mf.findInvitation(loggedInPerson.getUserName(), friendUsername);
mf.deleteInvitation(loggedInPerson, inv);
//iterate through and set new added friend's hasReceivedInvitation to false
Iterator<Person> searchIter = previousSearchResults.iterator();
while (searchIter.hasNext()) {
Person eachPerson = searchIter.next();