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.glpi.dynamic;
017
018import java.util.Collections;
019import java.util.List;
020
021import org.apache.avalon.framework.service.ServiceException;
022import org.apache.avalon.framework.service.ServiceManager;
023import org.apache.cocoon.xml.XMLUtils;
024import org.xml.sax.SAXException;
025
026import org.ametys.core.user.UserIdentity;
027import org.ametys.plugins.glpi.GlpiTicket;
028import org.ametys.plugins.glpi.TicketGlpiManager;
029import org.ametys.plugins.linkdirectory.dynamic.AbstractInternalDynamicInformationGenerator;
030import org.ametys.runtime.i18n.I18nizableText;
031
032/**
033 *  Generates sax events for GLPI tickets information 
034 */
035public class TicketsDynamicInformationGenerator extends AbstractInternalDynamicInformationGenerator
036{
037    /** The ticket glpi component */
038    private TicketGlpiManager _ticketGlpiManager;
039    
040    @Override
041    public void service(ServiceManager serviceManager) throws ServiceException
042    {
043        super.service(serviceManager);
044        _ticketGlpiManager = (TicketGlpiManager) serviceManager.lookup(TicketGlpiManager.ROLE);
045    }
046
047    @Override
048    protected void saxShortValue() throws SAXException
049    {
050        try
051        {
052            UserIdentity currentUser = getCurrentUser();
053            int count = _ticketGlpiManager.getCountOpenTickets(currentUser);
054            if (count != -1)
055            {
056                XMLUtils.createElement(contentHandler, SHORT_VALUE, String.valueOf(count));
057            }
058        }
059        catch (Exception e)
060        {
061            throw new SAXException("Unable to sax short value for GLPI tickets", e);
062        }
063    }
064
065    @Override
066    protected void saxLongValue() throws SAXException
067    {
068        try
069        {
070            int openCount = _ticketGlpiManager.getCountOpenTickets(getCurrentUser());
071            if (openCount != -1)
072            {
073                I18nizableText longValue = new I18nizableText("plugin.glpi", "PLUGINS_GLPI_DYNAMIC_INFORMATION_NB_TICKETS", Collections.singletonList(String.valueOf(openCount)));
074                longValue.toSAX(contentHandler, LONG_VALUE);
075            }
076        }
077        catch (Exception e)
078        {
079            throw new SAXException("Unable to retrieve the number of open tickets", e);
080        }
081    }
082
083    @Override
084    protected void saxTooltips() throws SAXException
085    {
086        try
087        {
088            List<GlpiTicket> openTickets = _ticketGlpiManager.getOpenTickets(getCurrentUser());
089            int maxItems = getMaxItems();
090            
091            for (int i = 0; (maxItems == -1 || i < maxItems) && i < openTickets.size(); i++)
092            {
093                GlpiTicket glpiTicket = openTickets.get(i);
094                
095                int id = glpiTicket.getId();
096                String title = glpiTicket.getTitle();
097                int status = glpiTicket.getStatusId();
098                I18nizableText statusLabel = _ticketGlpiManager.getGlpiStatusLabel(status);
099                
100                XMLUtils.startElement(contentHandler, ITEM);
101                XMLUtils.createElement(contentHandler, ITEM_TITLE, String.valueOf(id));
102                XMLUtils.createElement(contentHandler, ITEM_SUMMARY, title != null ? title : "");
103                statusLabel.toSAX(contentHandler, ITEM_FOOTER);
104                XMLUtils.endElement(contentHandler, ITEM);
105            }
106        }
107        catch (Exception e)
108        {
109            throw new SAXException("Unable to retrieve open tickets information", e);
110        }
111    }
112
113}