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();
List<String> userNames = Arrays.asList(uiForm.getUserName().trim().split("\\s*,\\s*"));
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
for (int i0 = 0; i0 < userNames.size() - 1; i0++)
{
String user0 = userNames.get(i0);
if (user0 == null || user0.trim().length() == 0)
continue;
for (int i1 = i0 + 1; i1 < userNames.size(); i1++)
if (user0.equals(userNames.get(i1)))
{
uiApp.addMessage(new ApplicationMessage("UIGroupMembershipForm.msg.duplicate-user",
new String[]{user0}));
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)
{
uiApp.addMessage(new ApplicationMessage("UIGroupMembershipForm.msg.user-not-exist",
new String[]{listNotExist}));
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();
}