/**
*
* BPS Bildungsportal Sachsen GmbH<br>
* Bahnhofstrasse 6<br>
* 09111 Chemnitz<br>
* Germany<br>
*
* Copyright (c) 2005-2009 by BPS Bildungsportal Sachsen GmbH<br>
* http://www.bps-system.de<br>
*
* All rights reserved.
*/
package de.bps.course.nodes.den;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.BitSet;
import java.util.List;
import org.olat.admin.user.UserSearchController;
import org.olat.basesecurity.events.MultiIdentityChosenEvent;
import org.olat.basesecurity.events.SingleIdentityChosenEvent;
import org.olat.commons.calendar.model.KalendarEvent;
import org.olat.core.gui.UserRequest;
import org.olat.core.gui.components.Component;
import org.olat.core.gui.components.form.flexible.impl.components.SimpleText;
import org.olat.core.gui.components.table.Table;
import org.olat.core.gui.components.table.TableController;
import org.olat.core.gui.components.table.TableEvent;
import org.olat.core.gui.components.table.TableMultiSelectEvent;
import org.olat.core.gui.components.velocity.VelocityContainer;
import org.olat.core.gui.control.Controller;
import org.olat.core.gui.control.Event;
import org.olat.core.gui.control.WindowControl;
import org.olat.core.gui.control.controller.BasicController;
import org.olat.core.gui.control.generic.closablewrapper.CloseableModalController;
import org.olat.core.id.Identity;
import org.olat.core.id.OLATResourceable;
import org.olat.core.util.Util;
import org.olat.core.util.mail.ContactList;
import org.olat.core.util.mail.ContactMessage;
import org.olat.core.util.mail.MailHelper;
import org.olat.core.util.mail.MailNotificationEditController;
import org.olat.core.util.mail.MailTemplate;
import org.olat.core.util.mail.MailerResult;
import org.olat.core.util.mail.MailerWithTemplate;
import org.olat.modules.co.ContactFormController;
import de.bps.course.nodes.DENCourseNode;
public class DENManageParticipantsController extends BasicController {
private static final String VELOCITY_ROOT = Util.getPackageVelocityRoot(DENManageParticipantsController.class);
private DENCourseNode courseNode;
private OLATResourceable ores;
private DENStatus status;
//objects for list of participants view
private List<KalendarEvent> dateList;
private DENListTableDataModel listTableData;
private TableController tableListParticipants;
//objects for manage participants view
private VelocityContainer participantsVC;
private DENParticipantsTableDataModel participantsTableData;
private TableController tableManageParticipants;
private DENManageParticipantsForm formManageParticipants;
private UserSearchController userSearchCntrl;
private CloseableModalController userSearchCMC;
private KalendarEvent selectedEvent;
//mail notification
private MailNotificationEditController addedNotificationCtr, removedNotificationCtr;
private ContactFormController contactCtr;
private CloseableModalController notificationCmc;
private List<Identity> added = new ArrayList<Identity>();
private List<Identity> removed = new ArrayList<Identity>();
private DENManager denManager;
public DENManageParticipantsController(UserRequest ureq, WindowControl wControl, OLATResourceable ores, DENCourseNode courseNode) {
super(ureq, wControl);
this.ores = ores;
this.courseNode = courseNode;
this.denManager = DENManager.getInstance();
//prepare list of enrolled participants
dateList = denManager.getDENEvents(ores.getResourceableId(), courseNode.getIdent());
listTableData = new DENListTableDataModel(dateList);
tableListParticipants = denManager.createListParticipantsTable(ureq, wControl, getTranslator(), this, listTableData);
putInitialPanel(tableListParticipants.getInitialComponent());
}
@Override
protected void doDispose() {
if(tableListParticipants != null) {
tableListParticipants.dispose();
tableListParticipants = null;
}
if(tableManageParticipants != null) {
tableManageParticipants.dispose();
tableManageParticipants = null;
}
if(formManageParticipants != null) {
formManageParticipants.dispose();
formManageParticipants = null;
}
}
@Override
public void event(UserRequest ureq, Controller source, Event event) {
if(source == tableListParticipants) {
if(event.getCommand().equals(Table.COMMANDLINK_ROWACTION_CLICKED)) {
TableEvent tableEvent = (TableEvent)event;
//open window for choosen date to manage the enrolled users or manually add
if(tableEvent.getActionId().equals(DENListTableDataModel.CHANGE_ACTION)) {
selectedEvent = (KalendarEvent)listTableData.getObject(tableEvent.getRowId());
List<Identity> participants = denManager.getEventParticipants(selectedEvent);
participantsTableData = new DENParticipantsTableDataModel(participants);
tableManageParticipants = denManager.createParticipantsTable(ureq, getWindowControl(), getTranslator(), this, participantsTableData);
formManageParticipants = new DENManageParticipantsForm(ureq, getWindowControl());
formManageParticipants.addControllerListener(this);
participantsVC = new VelocityContainer("participants.manage", VELOCITY_ROOT + "/participants.html", getTranslator(), this);
SimpleText dateTitle = new SimpleText("dateTitle", translate("dates.table.subject") + ": " + selectedEvent.getSubject());
DateFormat df = new SimpleDateFormat();
SimpleText dateTimeframe = new SimpleText("dateTimeframe", translate("dates.table.date") + ": " + df.format(selectedEvent.getBegin()) + " - " + df.format(selectedEvent.getEnd()));
participantsVC.put("dateTitle", dateTitle);
participantsVC.put("dateTimeframe", dateTimeframe);
participantsVC.put("participantsTable", tableManageParticipants.getInitialComponent());
participantsVC.put("addParticipants", formManageParticipants.getInitialComponent());
CloseableModalController manageParticipantsModalCntrl = new CloseableModalController(getWindowControl(), "close", participantsVC, true, translate("dates.table.participant.manage"));
manageParticipantsModalCntrl.activate();
}
} else {
TableMultiSelectEvent tmse = (TableMultiSelectEvent)event;
BitSet selection = tmse.getSelection();
//delete all users from the selected dates
if(tmse.getAction().equals(DENListTableDataModel.DELETE_ACTION) && selection.cardinality() > 0) {
removed = denManager.getSelectedEventParticipants(dateList, selection);
dateList = denManager.deleteParticipants(ores, courseNode, denManager.getSelectedEventIDs(dateList, selection));
listTableData.setObjects(dateList);
//send notification mail
createRemovedNotificationMail(ureq, dateList.get(0).getSubject());
} else if(tmse.getAction().equals(DENListTableDataModel.MAIL_ACTION) && selection.cardinality() > 0) {
//send email to all users from the selected dates
List<Identity> participants = denManager.getSelectedEventParticipants(dateList, selection);
createParticipantsMail(ureq, participants);
} else if(selection.cardinality() == 0) {
getWindowControl().setWarning(translate("participants.message.empty"));
}
}
} else if(source == formManageParticipants && event == DENManageParticipantsForm.ADD_PARTICIPANTS) {
//open user search controller to manually add users in date
userSearchCntrl = new UserSearchController(ureq, getWindowControl(), true, true, true);
userSearchCntrl.addControllerListener(this);
userSearchCMC = new CloseableModalController(getWindowControl(), "close", userSearchCntrl.getInitialComponent());
userSearchCMC.activate();
} else if(source == userSearchCntrl) {
if(event == Event.CANCELLED_EVENT) {
userSearchCMC.deactivate();
} else {
List<Identity> toAdd = null;
added = new ArrayList<Identity>();
if (event instanceof SingleIdentityChosenEvent) {
SingleIdentityChosenEvent singleEvent = (SingleIdentityChosenEvent) event;
Identity choosenIdentity = singleEvent.getChosenIdentity();
toAdd = new ArrayList<Identity>();
toAdd.add(choosenIdentity);
} else if (event instanceof MultiIdentityChosenEvent) {
MultiIdentityChosenEvent multiEvent = (MultiIdentityChosenEvent) event;
toAdd = multiEvent.getChosenIdentities();
}
boolean showMessage = false;
if(toAdd != null && toAdd.size() > 0) {
for( Identity identity : toAdd ) {
status = denManager.doEnroll(identity, selectedEvent, ores, courseNode, true);
if(!status.isEnrolled() && status.getErrorMessage().equals(DENStatus.ERROR_ALREADY_ENROLLED))
showMessage = true;
else
added.add(identity);
}
if(showMessage)
getWindowControl().setWarning(translate("enrollment.warning.manual"));
refreshTables();
}
userSearchCMC.deactivate();
if(added.size() > 0) {
//write notification mail
createAddedNotificationMail(ureq, dateList.get(0).getSubject());
}
}
userSearchCntrl.dispose();
userSearchCntrl = null;
} else if(source == tableManageParticipants) {
if(event.getCommand().equals(Table.COMMANDLINK_ROWACTION_CLICKED)) {
TableEvent tableEvent = (TableEvent)event;
//delete single user from event
if(tableEvent.getActionId().equals(DENParticipantsTableDataModel.REMOVE_ACTION)) {
Identity identity = participantsTableData.getEntryAt(tableEvent.getRowId());
status = denManager.cancelEnroll(identity, selectedEvent, ores, courseNode);
if(!status.isCancelled()) showError();
//send notification mail
else {
removed.clear();
removed.add(identity);
createRemovedNotificationMail(ureq, dateList.get(0).getSubject());
}
refreshTables();
//write email to single user
} else if(tableEvent.getActionId().equals(DENParticipantsTableDataModel.MAIL_ACTION)) {
List<Identity> participants = new ArrayList<Identity>();
participants.add(participantsTableData.getEntryAt(tableEvent.getRowId()));
createParticipantsMail(ureq, participants);
}
}
} else if(source == addedNotificationCtr && event == Event.DONE_EVENT) {
if(addedNotificationCtr.getMailTemplate() != null) {
List<Identity> ccIdentities = new ArrayList<Identity>();
if(addedNotificationCtr.getMailTemplate().getCpfrom()) {
ccIdentities.add(ureq.getIdentity());
} else {
ccIdentities = null;
}
MailerResult mailerResult = MailerWithTemplate.getInstance().sendMailAsSeparateMails(added, null, null, addedNotificationCtr.getMailTemplate(), ureq.getIdentity());
MailHelper.printErrorsAndWarnings(mailerResult, getWindowControl(), ureq.getLocale());
}
notificationCmc.deactivate();
addedNotificationCtr = null;
notificationCmc = null;
added.clear();
} else if(source == removedNotificationCtr && event == Event.DONE_EVENT) {
if(removedNotificationCtr.getMailTemplate() != null) {
List<Identity> ccIdentities = new ArrayList<Identity>();
if(addedNotificationCtr.getMailTemplate().getCpfrom()) {
ccIdentities.add(ureq.getIdentity());
} else {
ccIdentities = null;
}
MailerResult mailerResult = MailerWithTemplate.getInstance().sendMailAsSeparateMails(added, null, null, addedNotificationCtr.getMailTemplate(), ureq.getIdentity());
MailHelper.printErrorsAndWarnings(mailerResult, getWindowControl(), ureq.getLocale());
}
notificationCmc.deactivate();
removedNotificationCtr = null;
notificationCmc = null;
removed.clear();
} else if(source == contactCtr) {
notificationCmc.deactivate();
contactCtr.dispose();
contactCtr = null;
notificationCmc.dispose();
notificationCmc = null;
}
}
@Override
public void event(UserRequest ureq, Component source, Event event) {
// nothing to do
}
private void showError() {
String message = status.getErrorMessage();
if(DENStatus.ERROR_ALREADY_ENROLLED.equals(message)) {
getWindowControl().setError("");
} else if(DENStatus.ERROR_NOT_ENROLLED.equals(message)) {
getWindowControl().setError("");
} else if(DENStatus.ERROR_PERSISTING.equals(message)) {
getWindowControl().setError("");
} else if(DENStatus.ERROR_GENERAL.equals(message)) {
getWindowControl().setError("");
}
}
private void refreshTables() {
//set all table datas new to refresh view
List<Identity> participants = denManager.getEventParticipants(selectedEvent);
participantsTableData.setEntries((participants));
tableManageParticipants.setTableDataModel(participantsTableData);
listTableData.setObjects(dateList);
tableListParticipants.setTableDataModel(listTableData);
}
private void createAddedNotificationMail(UserRequest ureq, String subjectStr) {
MailTemplate mailTempl = denManager.getAddedMailTemplate(ureq, subjectStr, getTranslator());
addedNotificationCtr = new MailNotificationEditController(getWindowControl(), ureq, mailTempl, false);
addedNotificationCtr.addControllerListener(this);
VelocityContainer sendNotificationVC = new VelocityContainer("sendnotification", VELOCITY_ROOT + "/sendnotification.html", getTranslator(), this);
sendNotificationVC.put("notificationForm", addedNotificationCtr.getInitialComponent());
notificationCmc = new CloseableModalController(getWindowControl(), "close", sendNotificationVC);
notificationCmc.activate();
}
private void createRemovedNotificationMail(UserRequest ureq, String subjectStr) {
MailTemplate mailTempl = denManager.getRemovedMailTemplate(ureq, subjectStr, getTranslator());
addedNotificationCtr = new MailNotificationEditController(getWindowControl(), ureq, mailTempl, false);
addedNotificationCtr.addControllerListener(this);
VelocityContainer sendNotificationVC = new VelocityContainer("sendnotification", VELOCITY_ROOT + "/sendnotification.html", getTranslator(), this);
sendNotificationVC.put("notificationForm", addedNotificationCtr.getInitialComponent());
notificationCmc = new CloseableModalController(getWindowControl(), "close", sendNotificationVC);
notificationCmc.activate();
}
private void createParticipantsMail(UserRequest ureq, List<Identity> participants) {
VelocityContainer sendMessageVC = new VelocityContainer("sendmessage", VELOCITY_ROOT + "/sendmessage.html", getTranslator(), this);
ContactMessage cmsg = new ContactMessage(ureq.getIdentity());
ContactList contactList = null;
if(participants.size() == 1) {
contactList = new ContactList(participants.get(0).getName());
} else {
contactList = new ContactList(translate("participants.message.to"));
}
contactList.addAllIdentites(participants);
cmsg.addEmailTo(contactList);
contactCtr = new ContactFormController(ureq, getWindowControl(), false, false, false, false, cmsg);
contactCtr.addControllerListener(this);
sendMessageVC.contextPut("title", translate("participants.message"));
sendMessageVC.put("contactForm", contactCtr.getInitialComponent());
notificationCmc = new CloseableModalController(getWindowControl(), "close", sendMessageVC);
notificationCmc.activate();
}
}