Search in sources :

Example 1 with SubscriptionItem

use of org.olat.core.commons.services.notifications.SubscriptionItem in project OpenOLAT by OpenOLAT.

the class NotificationsManagerImpl method processSubscribersByEmail.

private void processSubscribersByEmail(Identity ident) {
    if (ident.getStatus().compareTo(Identity.STATUS_VISIBLE_LIMIT) >= 0) {
        // send only to active user
        return;
    }
    String userInterval = getUserIntervalOrDefault(ident);
    if ("never".equals(userInterval)) {
        return;
    }
    long start = System.currentTimeMillis();
    Date compareDate = getCompareDateFromInterval(userInterval);
    Property p = propertyManager.findProperty(ident, null, null, null, LATEST_EMAIL_USER_PROP);
    if (p != null) {
        Date latestEmail = new Date(p.getLongValue());
        if (latestEmail.after(compareDate)) {
            // nothing to do
            return;
        }
    }
    Date defaultCompareDate = getDefaultCompareDate();
    List<Subscriber> subscribers = getSubscribers(ident);
    if (subscribers.isEmpty()) {
        return;
    }
    String langPrefs = null;
    if (ident.getUser() != null && ident.getUser().getPreferences() != null) {
        langPrefs = ident.getUser().getPreferences().getLanguage();
    }
    Locale locale = I18nManager.getInstance().getLocaleOrDefault(langPrefs);
    boolean veto = false;
    Subscriber latestSub = null;
    List<SubscriptionItem> items = new ArrayList<>();
    List<Subscriber> subsToUpdate = new ArrayList<>();
    for (Subscriber sub : subscribers) {
        Date latestEmail = sub.getLatestEmailed();
        SubscriptionItem subsitem = null;
        if (latestEmail == null || compareDate.after(latestEmail)) {
            // no notif. ever sent until now
            if (latestEmail == null) {
                latestEmail = defaultCompareDate;
            } else if (latestEmail.before(defaultCompareDate)) {
                // no notification older than a month
                latestEmail = defaultCompareDate;
            }
            subsitem = createSubscriptionItem(sub, locale, SubscriptionInfo.MIME_HTML, SubscriptionInfo.MIME_HTML, latestEmail);
        } else if (latestEmail != null && latestEmail.after(compareDate)) {
        // already send an email within the user's settings interval
        // veto = true;
        }
        if (subsitem != null) {
            items.add(subsitem);
            subsToUpdate.add(sub);
        }
        latestSub = sub;
    }
    Translator translator = Util.createPackageTranslator(NotificationSubscriptionController.class, locale);
    notifySubscribersByEmail(latestSub, items, subsToUpdate, translator, start, veto);
}
Also used : Locale(java.util.Locale) SubscriptionItem(org.olat.core.commons.services.notifications.SubscriptionItem) Subscriber(org.olat.core.commons.services.notifications.Subscriber) Translator(org.olat.core.gui.translator.Translator) ArrayList(java.util.ArrayList) Property(org.olat.properties.Property) Date(java.util.Date)

Example 2 with SubscriptionItem

use of org.olat.core.commons.services.notifications.SubscriptionItem in project OpenOLAT by OpenOLAT.

the class NotificationsManagerImpl method sendEmail.

private boolean sendEmail(Identity to, Translator translator, List<SubscriptionItem> subItems) {
    String title = translator.translate("rss.title", new String[] { NotificationHelper.getFormatedName(to) });
    StringBuilder htmlText = new StringBuilder();
    htmlText.append("<style>");
    htmlText.append(".o_m_sub h4 {margin: 0 0 10px 0;}");
    htmlText.append(".o_m_sub ul {padding: 0 0 5px 20px; margin: 0;}");
    htmlText.append(".o_m_sub ul li {padding: 0; margin: 1px 0;}");
    htmlText.append(".o_m_go {padding: 5px 0 0 0}");
    htmlText.append(".o_date {font-size: 90%; color: #888}");
    htmlText.append(".o_m_footer {background: #FAFAFA; border: 1px solid #eee; border-radius: 5px; padding: 0 0.5em 0.5em 0.5em; margin: 1em 0 1em 0;' class='o_m_h'}");
    htmlText.append("</style>");
    for (Iterator<SubscriptionItem> it_subs = subItems.iterator(); it_subs.hasNext(); ) {
        SubscriptionItem subitem = it_subs.next();
        // o_m_wrap class for overriding styles in master mail template
        htmlText.append("<div class='o_m_wrap'>");
        // add background here for gmail as they ignore classes.
        htmlText.append("<div class='o_m_sub' style='background: #FAFAFA; padding: 5px 5px; margin: 10px 0;'>");
        // 1: title
        htmlText.append(subitem.getTitle());
        htmlText.append("\n");
        // 2: content
        String desc = subitem.getDescription();
        if (StringHelper.containsNonWhitespace(desc)) {
            htmlText.append(desc);
        }
        // 3: goto-link
        String link = subitem.getLink();
        if (StringHelper.containsNonWhitespace(link)) {
            htmlText.append("<div class='o_m_go'><a href=\"").append(link).append("\">");
            SubscriptionInfo subscriptionInfo = subitem.getSubsInfo();
            if (subscriptionInfo != null) {
                String innerType = subscriptionInfo.getType();
                String typeName = NewControllerFactory.translateResourceableTypeName(innerType, translator.getLocale());
                String open = translator.translate("resource.open", new String[] { typeName });
                htmlText.append(open);
                htmlText.append(" &raquo;</a></div>");
            }
        }
        htmlText.append("\n");
        htmlText.append("</div></div>");
    }
    String basePath = Settings.getServerContextPathURI() + "/auth/HomeSite/" + to.getKey() + "/";
    htmlText.append("<div class='o_m_footer'>");
    htmlText.append(translator.translate("footer.notifications", new String[] { basePath + "mysettings/0", basePath += "notifications/0", basePath + "/tab/1" }));
    htmlText.append("</div>");
    MailerResult result = null;
    try {
        MailBundle bundle = new MailBundle();
        bundle.setToId(to);
        bundle.setContent(title, htmlText.toString());
        result = CoreSpringFactory.getImpl(MailManager.class).sendExternMessage(bundle, null, true);
    } catch (Exception e) {
    // FXOLAT-294 :: sending the mail will throw nullpointer exception if To-Identity has no
    // valid email-address!, catch it...
    }
    if (result == null || result.getReturnCode() > 0) {
        if (result != null)
            log.warn("Could not send email to identity " + to.getName() + ". (returncode=" + result.getReturnCode() + ", to=" + to + ")");
        else
            log.warn("Could not send email to identity " + to.getName() + ". (returncode = null) , to=" + to + ")");
        return false;
    } else {
        return true;
    }
}
Also used : SubscriptionItem(org.olat.core.commons.services.notifications.SubscriptionItem) MailerResult(org.olat.core.util.mail.MailerResult) SubscriptionInfo(org.olat.core.commons.services.notifications.SubscriptionInfo) NoSubscriptionInfo(org.olat.core.commons.services.notifications.model.NoSubscriptionInfo) MailBundle(org.olat.core.util.mail.MailBundle) AssertException(org.olat.core.logging.AssertException)

Example 3 with SubscriptionItem

use of org.olat.core.commons.services.notifications.SubscriptionItem in project OpenOLAT by OpenOLAT.

the class NotificationSubscriptionAndNewsFormatter method getSubscriptionItem.

public SubscriptionItem getSubscriptionItem(Subscriber sub) {
    SubscriptionInfo subsInfo = subsInfoMap.get(sub);
    NotificationsManager notiMgr = NotificationsManager.getInstance();
    SubscriptionItem subscrItem = notiMgr.createSubscriptionItem(subsInfo, sub, translator.getLocale(), SubscriptionInfo.MIME_HTML, SubscriptionInfo.MIME_HTML);
    return subscrItem;
}
Also used : SubscriptionItem(org.olat.core.commons.services.notifications.SubscriptionItem) NotificationsManager(org.olat.core.commons.services.notifications.NotificationsManager) SubscriptionInfo(org.olat.core.commons.services.notifications.SubscriptionInfo)

Example 4 with SubscriptionItem

use of org.olat.core.commons.services.notifications.SubscriptionItem in project OpenOLAT by OpenOLAT.

the class NotificationsManagerImpl method createSubscriptionItem.

@Override
public SubscriptionItem createSubscriptionItem(SubscriptionInfo subsInfo, Subscriber subscriber, Locale locale, String mimeTypeTitle, String mimeTypeContent) {
    Publisher pub = subscriber.getPublisher();
    String title = getFormatedTitle(subsInfo, subscriber, locale, mimeTypeTitle);
    String itemLink = null;
    if (subsInfo.getCustomUrl() != null) {
        itemLink = subsInfo.getCustomUrl();
    }
    if (itemLink == null && pub.getBusinessPath() != null) {
        itemLink = BusinessControlFactory.getInstance().getURLFromBusinessPathString(pub.getBusinessPath());
    }
    String description = subsInfo.getSpecificInfo(mimeTypeContent, locale);
    SubscriptionItem subscriptionItem = new SubscriptionItem(title, itemLink, description);
    subscriptionItem.setSubsInfo(subsInfo);
    return subscriptionItem;
}
Also used : SubscriptionItem(org.olat.core.commons.services.notifications.SubscriptionItem) Publisher(org.olat.core.commons.services.notifications.Publisher)

Example 5 with SubscriptionItem

use of org.olat.core.commons.services.notifications.SubscriptionItem in project openolat by klemens.

the class NotificationNewsController method event.

/**
 * @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<>();
        List<Subscriber> subsList = new ArrayList<>();
        for (Subscriber subscriber : subsInfoMap.keySet()) {
            subsList.add(subscriber);
            SubscriptionItem item = man.createSubscriptionItem(subscriber, getLocale(), SubscriptionInfo.MIME_HTML, SubscriptionInfo.MIME_HTML, compareDate);
            if (item != null) {
                infoList.add(item);
            }
        }
        if (man.sendMailToUserAndUpdateSubscriber(subscriberIdentity, infoList, getTranslator(), subsList)) {
            showInfo("email.ok");
        } else {
            showError("email.nok");
        }
    }
}
Also used : SubscriptionItem(org.olat.core.commons.services.notifications.SubscriptionItem) Subscriber(org.olat.core.commons.services.notifications.Subscriber) NotificationsManager(org.olat.core.commons.services.notifications.NotificationsManager) ArrayList(java.util.ArrayList)

Aggregations

SubscriptionItem (org.olat.core.commons.services.notifications.SubscriptionItem)12 SubscriptionInfo (org.olat.core.commons.services.notifications.SubscriptionInfo)6 ArrayList (java.util.ArrayList)4 Date (java.util.Date)4 NotificationsManager (org.olat.core.commons.services.notifications.NotificationsManager)4 Publisher (org.olat.core.commons.services.notifications.Publisher)4 Subscriber (org.olat.core.commons.services.notifications.Subscriber)4 NoSubscriptionInfo (org.olat.core.commons.services.notifications.model.NoSubscriptionInfo)4 AssertException (org.olat.core.logging.AssertException)4 Locale (java.util.Locale)2 NotificationsHandler (org.olat.core.commons.services.notifications.NotificationsHandler)2 Translator (org.olat.core.gui.translator.Translator)2 MailBundle (org.olat.core.util.mail.MailBundle)2 MailerResult (org.olat.core.util.mail.MailerResult)2 Property (org.olat.properties.Property)2