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.environment.ObjectModelHelper; 022import org.apache.cocoon.environment.Request; 023import org.apache.cocoon.xml.XMLUtils; 024import org.xml.sax.SAXException; 025 026import org.ametys.core.user.UserIdentity; 027import org.ametys.core.util.StringUtils; 028import org.ametys.plugins.linkdirectory.dynamic.DynamicInformationException; 029import org.ametys.plugins.messagingconnector.CalendarEvent; 030import org.ametys.plugins.messagingconnector.MessagingConnectorException; 031import org.ametys.runtime.i18n.I18nizableText; 032 033/** 034 * Generates sax events for calendar information 035 */ 036public class CalendarDynamicInformationGenerator extends AbstractMessagingConnectorDynamicInformationGenerator 037{ 038 private static final int __DEFAULT_MAX_DAYS = 7; 039 040 @Override 041 protected void saxShortValue() throws SAXException, DynamicInformationException 042 { 043 try 044 { 045 UserIdentity currentUser = getCurrentUser(); 046 if (currentUser != null) 047 { 048 int eventsCount = _messagingConnector.getEventsCount(currentUser, getMaxDays()); 049 XMLUtils.createElement(contentHandler, SHORT_VALUE, String.valueOf(eventsCount)); 050 } 051 else 052 { 053 XMLUtils.createElement(contentHandler, SHORT_VALUE, "0"); 054 } 055 } 056 catch (MessagingConnectorException e) 057 { 058 throw e.toDynamicInformationException(); 059 } 060 } 061 062 @Override 063 protected void saxLongValue() throws SAXException, DynamicInformationException 064 { 065 try 066 { 067 UserIdentity currentUser = getCurrentUser(); 068 if (currentUser != null) 069 { 070 int eventsCount = _messagingConnector.getEventsCount(currentUser, getMaxDays()); 071 072 String i18nKey = eventsCount > 1 ? "PLUGINS_MESSAGINGCONNECTOR_LINKDIRECTORY_CALENDAR_DISPLAY_MULTIPLE" : "PLUGINS_MESSAGINGCONNECTOR_LINKDIRECTORY_CALENDAR_DISPLAY_SINGLE"; 073 074 I18nizableText longValue = new I18nizableText("plugin.messaging-connector", i18nKey, Collections.singletonList(String.valueOf(eventsCount))); 075 longValue.toSAX(contentHandler, LONG_VALUE); 076 } 077 else 078 { 079 I18nizableText longValue = new I18nizableText("plugin.messaging-connector", "PLUGINS_MESSAGINGCONNECTOR_LINKDIRECTORY_CALENDAR_NO_USER"); 080 longValue.toSAX(contentHandler, LONG_VALUE); 081 } 082 } 083 catch (MessagingConnectorException e) 084 { 085 throw e.toDynamicInformationException(); 086 } 087 } 088 089 @Override 090 protected void saxTooltips() throws SAXException, DynamicInformationException 091 { 092 try 093 { 094 UserIdentity currentUser = getCurrentUser(); 095 if (currentUser != null) 096 { 097 List<CalendarEvent> events = _messagingConnector.getEvents(currentUser, getMaxDays(), getMaxItems()); 098 099 for (CalendarEvent calendarEvent : events) 100 { 101 saxItem(calendarEvent.getStartDate(), calendarEvent.getSubject(), calendarEvent.getLocation()); 102 } 103 } 104 } 105 catch (MessagingConnectorException e) 106 { 107 throw e.toDynamicInformationException(); 108 } 109 } 110 111 /** 112 * Get the max days for search 113 * @return the max number of day 114 */ 115 protected int getMaxDays() 116 { 117 Request request = ObjectModelHelper.getRequest(objectModel); 118 try 119 { 120 return Integer.parseInt(request.getParameter("maxDays")); 121 } 122 catch (NumberFormatException e) 123 { 124 return __DEFAULT_MAX_DAYS; 125 } 126 } 127 128 @Override 129 protected String getSpanId() 130 { 131 return "dynamic-info-calendar-change-password-" + StringUtils.generateKey(); 132 } 133}