001/* 002 * Copyright 2022 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.hyperplanning.dynamic; 017 018import java.time.ZoneId; 019import java.time.format.FormatStyle; 020import java.util.List; 021import java.util.Map; 022 023import org.apache.avalon.framework.service.ServiceException; 024import org.apache.avalon.framework.service.ServiceManager; 025import org.apache.cocoon.xml.XMLUtils; 026import org.apache.commons.lang3.StringUtils; 027import org.xml.sax.SAXException; 028 029import org.ametys.core.user.UserIdentity; 030import org.ametys.plugins.hyperplanning.HyperplanningManager; 031import org.ametys.plugins.hyperplanning.HyperplanningManager.CancelledLesson; 032import org.ametys.plugins.linkdirectory.dynamic.AbstractInternalDynamicInformationGenerator; 033import org.ametys.plugins.linkdirectory.dynamic.DynamicInformationException; 034import org.ametys.plugins.linkdirectory.dynamic.DynamicInformationException.ExceptionType; 035import org.ametys.runtime.i18n.I18nizableDateTime; 036import org.ametys.runtime.i18n.I18nizableText; 037 038/** 039 * Generate sax events for hyperplanning cancelled lessons informations 040 */ 041public class CancelledLessonsDynamicInformationGenerator extends AbstractInternalDynamicInformationGenerator 042{ 043 private HyperplanningManager _hyperplanningManager; 044 045 @Override 046 public void service(ServiceManager serviceManager) throws ServiceException 047 { 048 super.service(serviceManager); 049 _hyperplanningManager = (HyperplanningManager) serviceManager.lookup(HyperplanningManager.ROLE); 050 } 051 052 @Override 053 protected void saxShortValue() throws SAXException, DynamicInformationException 054 { 055 UserIdentity currentUser = getCurrentUser(); 056 057 if (currentUser == null) 058 { 059 throw new DynamicInformationException("Unable to retrieve hyperplanning data, user is not connected", ExceptionType.UNAUTHORIZED); 060 } 061 else 062 { 063 List<CancelledLesson> cancelledLessons = _getLessons(currentUser); 064 XMLUtils.createElement(contentHandler, SHORT_VALUE, String.valueOf(cancelledLessons.size())); 065 } 066 } 067 068 @Override 069 protected void saxLongValue() throws SAXException, DynamicInformationException 070 { 071 UserIdentity currentUser = getCurrentUser(); 072 073 if (currentUser == null) 074 { 075 throw new DynamicInformationException("Unable to retrieve hyperplanning data, user is not connected", ExceptionType.UNAUTHORIZED); 076 } 077 else 078 { 079 List<CancelledLesson> cancelledLessons = _getLessons(currentUser); 080 int count = cancelledLessons.size(); 081 if (count == 0) 082 { 083 I18nizableText longValue = new I18nizableText("plugin.hyperplanning", "PLUGIN_HYPERPLANNING_DYNAMIC_INFORMATION_CANCELLED_LESSONS_NO_ELEMENT"); 084 longValue.toSAX(contentHandler, LONG_VALUE); 085 } 086 else if (count == 1) 087 { 088 I18nizableText longValue = new I18nizableText("plugin.hyperplanning", "PLUGIN_HYPERPLANNING_DYNAMIC_INFORMATION_CANCELLED_LESSONS_NB_ELEMENT", Map.of("count", new I18nizableText(Integer.toString(count)))); 089 longValue.toSAX(contentHandler, LONG_VALUE); 090 } 091 else 092 { 093 I18nizableText longValue = new I18nizableText("plugin.hyperplanning", "PLUGIN_HYPERPLANNING_DYNAMIC_INFORMATION_CANCELLED_LESSONS_NB_ELEMENTS", Map.of("count", new I18nizableText(Integer.toString(count)))); 094 longValue.toSAX(contentHandler, LONG_VALUE); 095 } 096 } 097 } 098 099 @Override 100 protected void saxTooltips() throws SAXException, DynamicInformationException 101 { 102 UserIdentity currentUser = getCurrentUser(); 103 104 if (currentUser == null) 105 { 106 throw new DynamicInformationException("Unable to retrieve hyperplanning data, user is not connected", ExceptionType.UNAUTHORIZED); 107 } 108 else 109 { 110 List<CancelledLesson> cancelledLessons = _getLessons(currentUser); 111 112 int maxItems = getMaxItems(); 113 cancelledLessons.stream() 114 .limit(maxItems != -1 ? maxItems : cancelledLessons.size()) 115 .forEach(lesson -> { 116 try 117 { 118 XMLUtils.startElement(contentHandler, ITEM); 119 120 XMLUtils.createElement(contentHandler, ITEM_TITLE, lesson.code() + " " + (StringUtils.isNotBlank(lesson.fullLabel()) ? lesson.fullLabel() : lesson.label())); 121 122 I18nizableText summary = new I18nizableText( 123 "plugin.hyperplanning", 124 "PLUGIN_HYPERPLANNING_DYNAMIC_INFORMATION_CANCELLED_LESSONS_SUMMARY", 125 Map.of( 126 // Hyperplanning don't use time zone. So we use system default. 127 "date", new I18nizableDateTime(lesson.lessonDate().atZone(ZoneId.systemDefault()), ZoneId.systemDefault(), FormatStyle.MEDIUM)) 128 ); 129 summary.toSAX(contentHandler, ITEM_SUMMARY); 130 XMLUtils.createElement(contentHandler, ITEM_FOOTER, lesson.cancelRationale() + " " + lesson.cancelComment()); 131 132 XMLUtils.endElement(contentHandler, ITEM); 133 } 134 catch (SAXException e) 135 { 136 throw new RuntimeException(e); 137 } 138 }); 139 } 140 } 141 142 private List<CancelledLesson> _getLessons(UserIdentity currentUser) 143 { 144 try 145 { 146 return _hyperplanningManager.getUpcomingCancelledLessons(currentUser); 147 } 148 catch (Exception e) 149 { 150 throw new DynamicInformationException("An error occured while retrieving data from hyperplanning", e); 151 } 152 } 153}