/**
* OLAT - Online Learning and Training<br>
* http://www.olat.org
* <p>
* Licensed under the Apache License, Version 2.0 (the "License"); <br>
* you may not use this file except in compliance with the License.<br>
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing,<br>
* software distributed under the License is distributed on an "AS IS" BASIS, <br>
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. <br>
* See the License for the specific language governing permissions and <br>
* limitations under the License.
* <p>
* Copyright (c) frentix GmbH<br>
* http://www.frentix.com<br>
* <p>
*/
package org.olat.notifications;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import org.olat.core.gui.UserRequest;
import org.olat.core.gui.components.Component;
import org.olat.core.gui.components.link.Link;
import org.olat.core.gui.components.link.LinkFactory;
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.id.Identity;
import org.olat.core.util.notifications.NotificationHelper;
import org.olat.core.util.notifications.NotificationsManager;
import org.olat.core.util.notifications.Subscriber;
import org.olat.core.util.notifications.SubscriptionInfo;
import org.olat.core.util.notifications.SubscriptionItem;
/**
* Description:<br>
* This controller shows the list of the news generated by the users
* subscriptions. The news interval can be changed by the user by setting an
* appropriate date. To manage the users subscription the
* NotificationSubscriptionController can be used.
*
* <P>
* Initial Date: 22.12.2009 <br>
*
* @author gnaegi
*/
class NotificationNewsController extends BasicController {
private VelocityContainer newsVC;
private Date compareDate;
private Identity subscriberIdentity;
private DateChooserController dateChooserCtr;
private Link emailLink;
private Map<Subscriber, SubscriptionInfo> subsInfoMap;
/**
* Constructor
*
* @param subscriberIdentity The identity which news are displayed
* @param ureq
* @param wControl
* @param newsSinceDate The lower date boundary to collect the news or NULL to
* use the user defined notification interval
*/
NotificationNewsController(Identity subscriberIdentity, UserRequest ureq, WindowControl wControl, Date newsSinceDate) {
super(ureq, wControl);
this.subscriberIdentity = subscriberIdentity;
if (newsSinceDate == null) {
NotificationsManager man = NotificationsManager.getInstance();
compareDate = man.getCompareDateFromInterval(man.getUserIntervalOrDefault(ureq.getIdentity()));
} else {
compareDate = newsSinceDate;
}
// Main view is a velocity container
newsVC = createVelocityContainer("notificationsNews");
// Fetch data from DB and update datamodel
updateNewsDataModel();
// Add date chooser
dateChooserCtr = new DateChooserController(ureq, getWindowControl(), new Date());
listenTo(dateChooserCtr);
newsVC.put("dateChosserCtr", dateChooserCtr.getInitialComponent());
// Add email link
emailLink = LinkFactory.createButton("emailLink", newsVC, this);
//
putInitialPanel(newsVC);
}
/**
* Update the new data model and refresh the GUI
*/
void updateNewsDataModel() {
NotificationsManager man = NotificationsManager.getInstance();
List<Subscriber> subs = man.getSubscribers(subscriberIdentity);
newsVC.contextPut("subs", subs);
subsInfoMap = NotificationHelper.getSubscriptionMap(getIdentity(), getLocale(), true, compareDate);
NotificationSubscriptionAndNewsFormatter subsFormatter = new NotificationSubscriptionAndNewsFormatter(getTranslator(), subsInfoMap);
newsVC.contextPut("subsFormatter", subsFormatter);
}
/**
* @see org.olat.core.gui.control.DefaultController#event(org.olat.core.gui.UserRequest,
* org.olat.core.gui.control.Controller, org.olat.core.gui.control.Event)
*/
protected void event(UserRequest ureq, Controller source, Event event) {
if (source == dateChooserCtr) {
if (event == Event.CHANGED_EVENT) {
compareDate = dateChooserCtr.getChoosenDate();
updateNewsDataModel();
}
}
}
/**
* @see org.olat.core.gui.control.DefaultController#event(org.olat.core.gui.UserRequest,
* org.olat.core.gui.components.Component,
* org.olat.core.gui.control.Event)
*/
@Override
protected void event(UserRequest ureq, Component source, Event event) {
if (source == emailLink) {
// send email to user with the currently visible date
NotificationsManager man = NotificationsManager.getInstance();
List<SubscriptionItem> infoList = new ArrayList<SubscriptionItem>();
List<Subscriber> subsList = new ArrayList<Subscriber>();
for (Subscriber subscriber : subsInfoMap.keySet()) {
subsList.add(subscriber);
SubscriptionItem item = man.createSubscriptionItem(subscriber, getLocale(), SubscriptionInfo.MIME_PLAIN, SubscriptionInfo.MIME_PLAIN, compareDate);
if(item != null) {
infoList.add(item);
}
}
if (man.sendMailToUserAndUpdateSubscriber(subscriberIdentity, infoList, getTranslator(), subsList)) {
showInfo("email.ok");
} else {
showError("email.nok");
}
}
}
/**
* @see org.olat.core.gui.control.DefaultController#doDispose()
*/
@Override
protected void doDispose() {
// child controllers disposed by basic controller
}
}