public static class SaveActionListener extends EventListener<UIGroupMembershipForm> {
public void execute(Event<UIGroupMembershipForm> event) throws Exception {
UIGroupMembershipForm uiForm = event.getSource();
UIUserInGroup userInGroup = uiForm.getParent();
OrganizationService service = uiForm.getApplicationComponent(OrganizationService.class);
MembershipHandler memberShipHandler = service.getMembershipHandler();
UIApplication uiApp = event.getRequestContext().getUIApplication();
Group group = userInGroup.getSelectedGroup();
MembershipType membershipType = service.getMembershipTypeHandler().findMembershipType(uiForm.getMembership());
if (group == null) {
uiApp.addMessage(new ApplicationMessage("UIGroupMembershipForm.msg.group-not-select", null));
return;
}
// add new
List<String> userNames = Arrays.asList(uiForm.getUserName().trim().split("\\s*,\\s*"));
if (new HashSet<String>(userNames).size() != userNames.size()) {
uiApp.addMessage(new ApplicationMessage("UIGroupMembershipForm.msg.duplicate-user", null));
return;
}
// check user
boolean check = false;
String listNotExist = null;
for (String username : userNames) {
if (username == null || username.trim().length() == 0)
continue;
User user = service.getUserHandler().findUserByName(username);
if (user == null) {
check = true;
if (listNotExist == null)
listNotExist = username;
else
listNotExist += ", " + username;
}
}
if (check) {
ApplicationMessage msg = new ApplicationMessage("UIGroupMembershipForm.msg.user-not-exist",
new String[] { listNotExist });
msg.setArgsLocalized(false);
uiApp.addMessage(msg);
return;
}
// check membership
String listUserMembership = null;
for (String username : userNames) {
if (username == null || username.trim().length() == 0)
continue;
Membership membership = memberShipHandler.findMembershipByUserGroupAndType(username, group.getId(),
membershipType.getName());
if (membership != null) {
check = true;
if (listUserMembership == null)
listUserMembership = username;
else
listUserMembership += ", " + username;
}
}
if (check) {
uiApp.addMessage(new ApplicationMessage("UIGroupMembershipForm.msg.membership-exist", new String[] {
listUserMembership, group.getGroupName() }));
return;
}
for (String username : userNames) {
if (username == null || username.trim().length() == 0)
continue;
User user = service.getUserHandler().findUserByName(username);
memberShipHandler.linkMembership(user, group, membershipType, true);
}
userInGroup.refresh();
uiForm.reset();
}