public ActionForward list(ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse resp) {
TimerEventListForm timerEventListForm = (TimerEventListForm) form;
UserManager userManager = new UserManager(locale, session);
Group groupToDisplay = getCurrentGroup(req);
if (Boolean.TRUE.equals(timerEventListForm.getAllGroups()) && UserManager.isUserInRole(webUser, UserManagerBase.ADMINISTRATOR)) {
groupToDisplay = null;
}
User userToDisplay = null;
Long selectedUserId = timerEventListForm.getUserId() == null ? ALL_ID : timerEventListForm.getUserId();
if (UserManager.isUserInRole(webUser, UserManagerBase.GROUP_ADMINISTRATOR)) {
User[] users;
if (groupToDisplay == null) {
users = userManager.getUsers(false);
} else {
users = userManager.getUsers(groupToDisplay, false); // all users with a username
}
if (users.length > MAX_USERLIST_LENGTH) {
users = new User[0];
}
// sort users by username
Arrays.sort(users, new Comparator<User>() {
public int compare(User o1, User o2) {
String n1 = o1.getUsername() != null ? o1.getUsername() : "";
String n2 = o2.getUsername() != null ? o2.getUsername() : "";
return n1.compareToIgnoreCase(n2);
}});
req.setAttribute("users", users);
} else {
selectedUserId = webUser.getId();
timerEventListForm.setUserId(selectedUserId);
req.setAttribute("users", new User[]{webUser});
}
if (!selectedUserId.equals(ALL_ID)) {
userToDisplay = userManager.getUserDetails(selectedUserId);
Hibernate.initialize(userToDisplay); // used to fetch
}
// create displayable list of timer events
SmsServiceDbManager smsServiceDbManager = new SmsServiceDbManager(locale, session);
TimerEvent[] timerEvents = smsServiceDbManager.getTimerEvents(null,null, 0, true, groupToDisplay,userToDisplay); // show also out-of-date timer events, but not processed entries (shouldn't occur anyhow)
TimerEventListForm.DisplayListEntry[] displayListEntries = new TimerEventListForm.DisplayListEntry[timerEvents.length];
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
CommandEntryManager commandEntryManager = new CommandEntryManager(locale,session);
MessageResources resources = getResources(req);
String noInteractionsString = resources.getMessage(locale,"smssvc.noInteractions");
int i = 0;
for (TimerEvent event : timerEvents) {
TimerEventListForm.DisplayListEntry displayListEntry = new TimerEventListForm.DisplayListEntry();
displayListEntry.setTimerEventId(event.getId());
displayListEntry.setGroupname(event.getGroup().getGroupname());
displayListEntry.setUsername( event.getOwningUser().getUsername()); // not-null in the database
displayListEntry.setPointInTime(dateFormat.format(event.getScheduledTime()));
// description shows first command entry; "..." if more are available; empty comments at the beginning are ignored
String description = noInteractionsString;
SortedSet<CommandEntry> sortedCommandEntries = CommandEntryManager.sortedCommandEntries(event.getCommandEntries());
Iterator<CommandEntry> iterator = sortedCommandEntries.iterator();
while (iterator.hasNext()){
CommandEntry commandEntry = iterator.next();
if (commandEntry instanceof CommentCommandEntry){
String commentDescription = ((CommentCommandEntry)commandEntry).getDescription();
if (commentDescription != null){
description = "(" + commentDescription + ")";
break;
}
} else {
description = commandEntryManager.createCommandEntryExtender(commandEntry).readableCommandDescription(req);
break;
}
}
if (iterator.hasNext()) {
description += "; ...";
}
displayListEntry.setDescription(description);
displayListEntry.setStatus(event.getProcessing() == 0 ? resources.getMessage(locale,"smssvc.scheduled") : resources.getMessage(locale,"smssvc.processing"));
if (event.getIntervalInMillis() >= TimerEventEnterOrEditForm.MILLIS_PER_WEEK) {
long weeks = event.getIntervalInMillis() / TimerEventEnterOrEditForm.MILLIS_PER_WEEK;
displayListEntry.setRepeat(resources.getMessage(locale,"smssvc.repeatEveryNWeeks", weeks +""));
} else if (event.getIntervalInMillis() >= TimerEventEnterOrEditForm.MILLIS_PER_DAY){
long days = event.getIntervalInMillis() / TimerEventEnterOrEditForm.MILLIS_PER_DAY;
displayListEntry.setRepeat(resources.getMessage(locale,"smssvc.repeatEveryNDays", days+""));
} else if (event.getIntervalInMillis() >= TimerEventEnterOrEditForm.MILLIS_PER_HOUR){
long hours = event.getIntervalInMillis() / TimerEventEnterOrEditForm.MILLIS_PER_HOUR;
displayListEntry.setRepeat(resources.getMessage(locale,"smssvc.repeatEveryNHours", hours+""));
} else if (event.getIntervalInMillis() >= TimerEventEnterOrEditForm.MILLIS_PER_MINUTE){
long minutes = event.getIntervalInMillis() / TimerEventEnterOrEditForm.MILLIS_PER_MINUTE;
displayListEntry.setRepeat(resources.getMessage(locale,"smssvc.repeatEveryNMinutes", minutes+""));
}
String applyTo;
if (event.getRequestingUser() != null){
if (event.getRequestingUser().getUsername() != null){
applyTo = event.getRequestingUser().getUsername();
} else {
applyTo = event.getRequestingUser().getMsisdn() + "";
}
} else if (event.getRequestingUserSet() != null){
applyTo = event.getRequestingUserSet().getName();
} else {
applyTo = event.getGroup().getGroupname();
}
displayListEntry.setApplyTo(applyTo);
displayListEntries[i++] = displayListEntry;
}
req.setAttribute("displayListEntries", displayListEntries);
req.getSession().setAttribute("groupId", groupToDisplay != null ? groupToDisplay.getId() : ALL_ID); // needed when creating entry
return mapping.findForward("list");
}