@SuppressWarnings("unchecked")
public ActionForward select(ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse resp) {
req.getSession().setAttribute("formActionPath", req.getParameter("formActionPath"));
// get destination application (and thus group) or at least the group from session
Group destinationGroup;
Application destinationApplication = null;
Object destinationApplicationIdFromSession = req.getSession().getAttribute("destinationApplicationId");
if (destinationApplicationIdFromSession != null){
if (!(destinationApplicationIdFromSession instanceof Long)){
throw new BugException("invalid destinationApplicationId parameter: " + destinationApplicationIdFromSession);
}
ApplicationManager applicationManager = new ApplicationManager(locale,session);
destinationApplication = applicationManager.getApplication((Long)destinationApplicationIdFromSession);
if (destinationApplication == null){
throw new BugException("invalid destination application id: " + destinationApplicationIdFromSession);
}
destinationGroup = destinationApplication.getGroup();
} else {
Object destinationGroupIdFromSession = req.getSession().getAttribute("destinationGroupId");
if (!(destinationGroupIdFromSession instanceof Long)){
throw new BugException("invalid destinationGroupId parameter: " + destinationGroupIdFromSession);
}
GroupManager groupManager = new GroupManager(locale,session);
destinationGroup = groupManager.getGroup((Long)destinationGroupIdFromSession);
if (destinationGroup == null){
throw new BugException("invalid destination group id: " + destinationGroupIdFromSession);
}
}
// get mappings from session
Object mappingSetFromSession = req.getSession().getAttribute("mappingSet");
if (!(mappingSetFromSession instanceof MappingSet)) {
throw new BugException("invalid mappingSet parameter: " + mappingSetFromSession);
}
MappingSet mappingSet = (MappingSet) mappingSetFromSession;
mappingSet.reload(locale, session);
// prepare mapping form and request attributes
List<User> sourceUsers = new LinkedList<User>();
List<User> proposedUsers = new LinkedList<User>();
List<Long> proposedUserIds = new LinkedList<Long>();
List<UserSet> sourceUserSets = new LinkedList<UserSet>();
List<Long> proposedUserSetIds = new LinkedList<Long>();
List<Sender> sourceSenders = new LinkedList<Sender>();
List<Long> proposedSenderIds = new LinkedList<Long>();
List<Fragment> sourceFragments = new LinkedList<Fragment>();
List<Long> proposedContentIds = new LinkedList<Long>();
List<Long> proposedFragmentIds = new LinkedList<Long>();
List<Long> proposedChannelIds = new LinkedList<Long>();
List<Blog> sourceBlogs = new LinkedList<Blog>();
List<Long> proposedBlogIds = new LinkedList<Long>();
for (Mapping entryMapping : mappingSet) {
Entry sourceEntry = entryMapping.getSourceEntry();
if (sourceEntry.getUser() != null){
sourceUsers.add(sourceEntry.getUser());
Entry destinationEntry = entryMapping.getDestinationEntry();
if (destinationEntry != null){
proposedUsers.add(destinationEntry.getUser());
proposedUserIds.add(destinationEntry.getUser().getId());
} else {
proposedUserIds.add(-1L);
}
} else if (sourceEntry.getUserSet() != null){
sourceUserSets.add(sourceEntry.getUserSet());
Entry destinationEntry = entryMapping.getDestinationEntry();
if (destinationEntry != null){
proposedUserSetIds.add(destinationEntry.getUserSet().getId());
} else {
proposedUserSetIds.add(-1L);
}
} else if (sourceEntry.getSender() != null){
sourceSenders.add(sourceEntry.getSender());
Entry destinationEntry = entryMapping.getDestinationEntry();
if (destinationEntry != null){
proposedSenderIds.add(destinationEntry.getSender().getId());
} else {
proposedSenderIds.add(-1L);
}
} else if (sourceEntry.getFragment() != null){
sourceFragments.add(sourceEntry.getFragment());
Entry destinationEntry = entryMapping.getDestinationEntry();
if (destinationEntry != null){
proposedFragmentIds.add(destinationEntry.getFragment().getId());
} else {
proposedFragmentIds.add(-1L);
}
} else if (sourceEntry.getBlog() != null) {
sourceBlogs.add(sourceEntry.getBlog());
Entry destinationEntry = entryMapping.getDestinationEntry();
if (destinationEntry != null) {
proposedBlogIds.add(destinationEntry.getBlog().getId());
} else {
proposedBlogIds.add(-1L);
}
} // ignore group entry
}
req.setAttribute("sourceUsers", sourceUsers);
req.setAttribute("proposedUserIds",proposedUserIds);
req.setAttribute("sourceUserSets", sourceUserSets);
req.setAttribute("proposedUserSetIds",proposedUserSetIds);
req.setAttribute("sourceSenders", sourceSenders);
req.setAttribute("proposedSenderIds",proposedSenderIds);
req.setAttribute("proposedContentIds",proposedContentIds);
req.setAttribute("sourceFragments", sourceFragments);
req.setAttribute("proposedFragmentIds",proposedFragmentIds);
req.setAttribute("proposedChannelIds",proposedChannelIds);
req.setAttribute("sourceBlogs", sourceBlogs);
req.setAttribute("proposedBlogIds",proposedBlogIds);
// selection content; proposed mappings have to be in the lists independently of the maximum number of entries
UserManager userManager = new UserManager(locale,session);
SortedSet<SelectableUser> selectableUsers = new TreeSet<SelectableUser>();
for (User user : proposedUsers) {
selectableUsers.add(new SelectableUser(user.getId(),userManager.selectionDescription(user)));
}
User[] users = userManager.getUsers(destinationGroup, true);
for (User user : users) {
if (selectableUsers.size() < MAX_NOF_SELECTION_ENTRIES && user.getUsername() != null){ // only propose users with user names
selectableUsers.add(new SelectableUser(user.getId(),userManager.selectionDescription(user)));
}
}
req.setAttribute("selectableUsers", selectableUsers);
UserSetManager userSetManager = new UserSetManager(locale,session);
req.setAttribute("selectableUserSets", userSetManager.getUserSets(destinationGroup));
Set<Sender> senders = destinationGroup.getSmsSenders();
for (Sender sender : senders) { // fill cache
sender.getMsisdn();
sender.getGatewayProvider();
sender.getDescription();
}