001/* 002 * Copyright 2017 Anyware Services 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.ametys.plugins.messagingconnector.dynamic; 017 018import java.util.Collections; 019import java.util.List; 020 021import org.apache.cocoon.xml.XMLUtils; 022import org.xml.sax.SAXException; 023 024import org.ametys.core.user.UserIdentity; 025import org.ametys.core.util.StringUtils; 026import org.ametys.plugins.linkdirectory.dynamic.DynamicInformationException; 027import org.ametys.plugins.messagingconnector.EmailMessage; 028import org.ametys.plugins.messagingconnector.MessagingConnectorException; 029import org.ametys.runtime.i18n.I18nizableText; 030 031/** 032 * Generates sax events for mail information 033 */ 034public class MailDynamicInformationGenerator extends AbstractMessagingConnectorDynamicInformationGenerator 035{ 036 @Override 037 protected void saxShortValue() throws SAXException, DynamicInformationException 038 { 039 try 040 { 041 UserIdentity currentUser = getCurrentUser(); 042 if (currentUser != null) 043 { 044 int unreadEmailCount = _messagingConnector.getUnreadEmailCount(currentUser); 045 XMLUtils.createElement(contentHandler, SHORT_VALUE, String.valueOf(unreadEmailCount)); 046 } 047 else 048 { 049 XMLUtils.createElement(contentHandler, SHORT_VALUE, "0"); 050 } 051 } 052 catch (MessagingConnectorException e) 053 { 054 throw e.toDynamicInformationException(); 055 } 056 } 057 058 @Override 059 protected void saxLongValue() throws SAXException, DynamicInformationException 060 { 061 try 062 { 063 UserIdentity currentUser = getCurrentUser(); 064 if (currentUser != null) 065 { 066 int unreadMailCount = _messagingConnector.getUnreadEmailCount(currentUser); 067 068 String i18nKey = unreadMailCount > 1 ? "PLUGINS_MESSAGINGCONNECTOR_LINKDIRECTORY_MAIL_DISPLAY_MULTIPLE" : "PLUGINS_MESSAGINGCONNECTOR_LINKDIRECTORY_MAIL_DISPLAY_SINGLE"; 069 I18nizableText longValue = new I18nizableText("plugin.messaging-connector", i18nKey, Collections.singletonList(String.valueOf(unreadMailCount))); 070 longValue.toSAX(contentHandler, LONG_VALUE); 071 } 072 else 073 { 074 I18nizableText longValue = new I18nizableText("plugin.messaging-connector", "PLUGINS_MESSAGINGCONNECTOR_LINKDIRECTORY_MAIL_NO_USER"); 075 longValue.toSAX(contentHandler, LONG_VALUE); 076 } 077 } 078 catch (MessagingConnectorException e) 079 { 080 throw e.toDynamicInformationException(); 081 } 082 } 083 084 @Override 085 protected void saxTooltips() throws SAXException, DynamicInformationException 086 { 087 try 088 { 089 UserIdentity currentUser = getCurrentUser(); 090 if (currentUser != null) 091 { 092 List<EmailMessage> unreadEmails = _messagingConnector.getUnreadEmails(currentUser, getMaxItems()); 093 for (EmailMessage email : unreadEmails) 094 { 095 saxItem(email.getSubject(), email.getSummary(), email.getSender()); 096 } 097 } 098 } 099 catch (MessagingConnectorException e) 100 { 101 throw e.toDynamicInformationException(); 102 } 103 } 104 105 @Override 106 protected String getSpanId() 107 { 108 return "dynamic-info-agenda-change-password" + StringUtils.generateKey(); 109 } 110}