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.plugins.linkdirectory.dynamic.DynamicInformationException; 031import org.ametys.plugins.linkdirectory.dynamic.DynamicInformationException.ExceptionType; 032import org.ametys.runtime.i18n.I18nizableText; 033 034/** 035 * Generates sax events for GLPI tickets information 036 */ 037public class TicketsDynamicInformationGenerator extends AbstractInternalDynamicInformationGenerator 038{ 039 /** The ticket glpi component */ 040 private TicketGlpiManager _ticketGlpiManager; 041 042 @Override 043 public void service(ServiceManager serviceManager) throws ServiceException 044 { 045 super.service(serviceManager); 046 _ticketGlpiManager = (TicketGlpiManager) serviceManager.lookup(TicketGlpiManager.ROLE); 047 } 048 049 @Override 050 protected void saxShortValue() throws SAXException, DynamicInformationException 051 { 052 try 053 { 054 UserIdentity currentUser = getCurrentUser(); 055 056 if (currentUser == null) 057 { 058 throw new DynamicInformationException("Unable to retrieve open GLPI tickets, user is not connected", ExceptionType.UNAUTHORIZED); 059 } 060 else 061 { 062 int count = _ticketGlpiManager.getCountOpenTickets(currentUser); 063 if (count != -1) 064 { 065 XMLUtils.createElement(contentHandler, SHORT_VALUE, String.valueOf(count)); 066 } 067 else 068 { 069 throw new DynamicInformationException("Unable to retrieve open GLPI tickets", ExceptionType.UNKNOWN); 070 } 071 } 072 } 073 catch (SAXException | DynamicInformationException e) 074 { 075 throw e; 076 } 077 catch (Exception e) 078 { 079 throw new DynamicInformationException("Unable to sax short value for GLPI tickets", e); 080 } 081 } 082 083 @Override 084 protected void saxLongValue() throws SAXException, DynamicInformationException 085 { 086 try 087 { 088 UserIdentity currentUser = getCurrentUser(); 089 090 if (currentUser == null) 091 { 092 throw new DynamicInformationException("Unable to retrieve open GLPI tickets, user is not connected", ExceptionType.UNAUTHORIZED); 093 } 094 else 095 { 096 int openCount = _ticketGlpiManager.getCountOpenTickets(currentUser); 097 if (openCount != -1) 098 { 099 I18nizableText longValue = new I18nizableText("plugin.glpi", "PLUGINS_GLPI_DYNAMIC_INFORMATION_NB_TICKETS", Collections.singletonList(String.valueOf(openCount))); 100 longValue.toSAX(contentHandler, LONG_VALUE); 101 } 102 else 103 { 104 throw new DynamicInformationException("Unable to retrieve open GLPI tickets", ExceptionType.UNKNOWN); 105 } 106 } 107 } 108 catch (SAXException | DynamicInformationException e) 109 { 110 throw e; 111 } 112 catch (Exception e) 113 { 114 throw new DynamicInformationException("Unable to retrieve the number of open GLPI tickets", e); 115 } 116 } 117 118 @Override 119 protected void saxTooltips() throws SAXException, DynamicInformationException 120 { 121 try 122 { 123 UserIdentity currentUser = getCurrentUser(); 124 125 if (currentUser == null) 126 { 127 throw new DynamicInformationException("Unable to retrieve open GLPI tickets, user is not connected", ExceptionType.UNAUTHORIZED); 128 } 129 else 130 { 131 List<GlpiTicket> openTickets = _ticketGlpiManager.getOpenTickets(currentUser); 132 int maxItems = getMaxItems(); 133 134 for (int i = 0; (maxItems == -1 || i < maxItems) && i < openTickets.size(); i++) 135 { 136 GlpiTicket glpiTicket = openTickets.get(i); 137 138 int id = glpiTicket.getId(); 139 String title = glpiTicket.getTitle(); 140 int status = glpiTicket.getStatusId(); 141 I18nizableText statusLabel = _ticketGlpiManager.getGlpiStatusLabel(status); 142 143 XMLUtils.startElement(contentHandler, ITEM); 144 XMLUtils.createElement(contentHandler, ITEM_TITLE, String.valueOf(id)); 145 XMLUtils.createElement(contentHandler, ITEM_SUMMARY, title != null ? title : ""); 146 statusLabel.toSAX(contentHandler, ITEM_FOOTER); 147 XMLUtils.endElement(contentHandler, ITEM); 148 } 149 } 150 } 151 catch (SAXException | DynamicInformationException e) 152 { 153 throw e; 154 } 155 catch (Exception e) 156 { 157 throw new DynamicInformationException("Unable to retrieve open GLPI tickets information", e); 158 } 159 } 160}