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