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.linkdirectory.dynamic; 017 018import java.io.IOException; 019import java.util.Date; 020 021import org.apache.avalon.framework.service.ServiceException; 022import org.apache.avalon.framework.service.ServiceManager; 023import org.apache.cocoon.ProcessingException; 024import org.apache.cocoon.environment.ObjectModelHelper; 025import org.apache.cocoon.environment.Request; 026import org.apache.cocoon.generation.ServiceableGenerator; 027import org.apache.cocoon.xml.AttributesImpl; 028import org.apache.cocoon.xml.XMLUtils; 029import org.xml.sax.SAXException; 030 031import org.ametys.core.user.CurrentUserProvider; 032import org.ametys.core.user.UserIdentity; 033import org.ametys.core.util.DateUtils; 034import org.ametys.plugins.linkdirectory.dynamic.DynamicInformationException.ExceptionType; 035import org.ametys.runtime.i18n.I18nizableText; 036 037/** 038 * Abstract generator for saxing dynamic information. 039 */ 040public abstract class AbstractInternalDynamicInformationGenerator extends ServiceableGenerator 041{ 042 /** Constant for root tag */ 043 public static final String DYNAMIC_INFORMATION = "dynamic-information"; 044 /** Constant for short value tag */ 045 public static final String SHORT_VALUE = "short-value"; 046 /** Constant for long value tag */ 047 public static final String LONG_VALUE = "long-value"; 048 /** Constant for tooltip tag */ 049 public static final String TOOLTIP = "tooltip"; 050 /** Constant for item tag */ 051 public static final String ITEM = "item"; 052 /** Constant for item's title tag */ 053 public static final String ITEM_TITLE = "title"; 054 /** Constant for item's summary tag */ 055 public static final String ITEM_SUMMARY = "summary"; 056 /** Constant for item's footer tag */ 057 public static final String ITEM_FOOTER = "footer"; 058 /** Constant for item's error message tag */ 059 public static final String ERROR = "error"; 060 /** Constant for item's error type tag */ 061 public static final String ERROR_ATTRIBUTE_TYPE = "type"; 062 063 /** The current user provider **/ 064 protected CurrentUserProvider _currentUserProvider; 065 066 @Override 067 public void service(ServiceManager serviceManager) throws ServiceException 068 { 069 super.service(serviceManager); 070 _currentUserProvider = (CurrentUserProvider) serviceManager.lookup(CurrentUserProvider.ROLE); 071 } 072 073 public void generate() throws IOException, SAXException, ProcessingException 074 { 075 contentHandler.startDocument(); 076 077 XMLUtils.startElement(contentHandler, DYNAMIC_INFORMATION); 078 079 try 080 { 081 saxShortValue(); 082 saxLongValue(); 083 084 XMLUtils.startElement(contentHandler, TOOLTIP); 085 saxTooltips(); 086 XMLUtils.endElement(contentHandler, TOOLTIP); 087 } 088 catch (DynamicInformationException e) 089 { 090 getLogger().error("Unable to sax dynamic information", e); 091 saxError(e); 092 } 093 094 XMLUtils.endElement(contentHandler, DYNAMIC_INFORMATION); 095 096 contentHandler.endDocument(); 097 } 098 099 /** 100 * Get the current connected user 101 * @return A user identity or <code>null</code> if there is no connected user 102 */ 103 protected UserIdentity getCurrentUser() 104 { 105 return _currentUserProvider.getUser(); 106 } 107 108 /** 109 * Get the current language 110 * @return the current language 111 */ 112 protected String getCurrentLanguage() 113 { 114 Request request = ObjectModelHelper.getRequest(objectModel); 115 return request.getParameter("lang"); 116 } 117 118 /** 119 * Get the number of requested max items 120 * @return the number of max items. -1 if there is no limitations 121 */ 122 protected int getMaxItems() 123 { 124 Request request = ObjectModelHelper.getRequest(objectModel); 125 try 126 { 127 return Integer.parseInt(request.getParameter("maxItems")); 128 } 129 catch (NumberFormatException e) 130 { 131 return -1; 132 } 133 } 134 135 /** 136 * SAX the error 137 * @param e The thrown exception 138 * @throws SAXException If a SAXException error occurs 139 */ 140 protected void saxError(DynamicInformationException e) throws SAXException 141 { 142 getLogger().error("Unable to sax dynamic information", e); 143 ExceptionType type = e.getType(); 144 AttributesImpl attr = new AttributesImpl(); 145 attr.addCDATAAttribute(ERROR_ATTRIBUTE_TYPE, type.name()); 146 XMLUtils.startElement(contentHandler, ERROR, attr); 147 I18nizableText errorMessage = new I18nizableText("plugin.link-directory", "PLUGINS_LINKDIRECTORY_DYNAMIC_INFO_PROVIDER_ERROR_" + type.name().toUpperCase()); 148 errorMessage.toSAX(contentHandler); 149 XMLUtils.endElement(contentHandler, ERROR); 150 } 151 152 /** 153 * Create a short value node. 154 * 155 * @throws SAXException If a SAXException error occurs 156 */ 157 protected abstract void saxShortValue() throws SAXException; 158 159 /** 160 * Create a long value node. 161 * 162 * @throws SAXException If a error occurs while saxing 163 */ 164 protected abstract void saxLongValue() throws SAXException; 165 166 /** 167 * Create a tooltip node. 168 * 169 * @throws SAXException If a error occurs while saxing 170 */ 171 protected abstract void saxTooltips() throws SAXException; 172 173 /** 174 * Create an item node. 175 * 176 * @param title The title of item. 177 * @param summary The summary of item. 178 * @param footer The footer of item. 179 * @throws SAXException If a error occurs while saxing 180 */ 181 protected void saxItem(String title, String summary, String footer) throws SAXException 182 { 183 XMLUtils.startElement(contentHandler, ITEM); 184 XMLUtils.createElement(contentHandler, ITEM_TITLE, title == null ? "" : title); 185 XMLUtils.createElement(contentHandler, ITEM_SUMMARY, summary == null ? "" : summary); 186 XMLUtils.createElement(contentHandler, ITEM_FOOTER, footer == null ? "" : footer); 187 XMLUtils.endElement(contentHandler, ITEM); 188 } 189 190 /** 191 * Create an item node when the title is a {@link Date} 192 * @param title The title of item as Date. 193 * @param summary The summary of item. 194 * @param footer The footer of item. 195 * @throws SAXException If a error occurs while saxing 196 */ 197 protected void saxItem(Date title, String summary, String footer) throws SAXException 198 { 199 XMLUtils.startElement(contentHandler, ITEM); 200 AttributesImpl attr = new AttributesImpl(); 201 attr.addCDATAAttribute("type", "date"); 202 XMLUtils.createElement(contentHandler, ITEM_TITLE, attr, DateUtils.dateToString(title)); 203 XMLUtils.createElement(contentHandler, ITEM_SUMMARY, summary == null ? "" : summary); 204 XMLUtils.createElement(contentHandler, ITEM_FOOTER, footer == null ? "" : footer); 205 XMLUtils.endElement(contentHandler, ITEM); 206 } 207 208}