1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package fr.paris.lutece.plugins.calendar.web;
35
36 import fr.paris.lutece.plugins.calendar.business.CalendarFilter;
37 import fr.paris.lutece.plugins.calendar.business.stylesheet.CalendarStyleSheetHome;
38 import fr.paris.lutece.plugins.calendar.service.XMLUtils;
39 import fr.paris.lutece.portal.business.stylesheet.StyleSheet;
40 import fr.paris.lutece.portal.business.user.AdminUser;
41 import fr.paris.lutece.portal.service.admin.AdminUserService;
42 import fr.paris.lutece.portal.service.i18n.I18nService;
43 import fr.paris.lutece.portal.service.message.SiteMessage;
44 import fr.paris.lutece.portal.service.message.SiteMessageException;
45 import fr.paris.lutece.portal.service.message.SiteMessageService;
46 import fr.paris.lutece.portal.service.plugin.Plugin;
47 import fr.paris.lutece.portal.service.plugin.PluginService;
48 import fr.paris.lutece.portal.service.util.AppException;
49 import fr.paris.lutece.portal.service.util.AppPropertiesService;
50 import fr.paris.lutece.portal.web.constants.Messages;
51 import fr.paris.lutece.portal.web.constants.Parameters;
52 import fr.paris.lutece.portal.web.stylesheet.StyleSheetJspBean;
53
54 import java.io.ByteArrayInputStream;
55 import java.io.IOException;
56 import java.io.OutputStream;
57
58 import javax.servlet.ServletContext;
59 import javax.servlet.http.HttpServletRequest;
60 import javax.servlet.http.HttpServletResponse;
61 import javax.servlet.http.HttpSession;
62
63
64
65
66
67
68 public class CalendarDownloadFile
69 {
70
71 private static final String PROPERTY_FILE_EXPORT_NAME = "agenda";
72 private static final String PROPERTY_RSS_FILE_NAME = "rss";
73 private static final String PROPERTY_CALENDAR_RSS_FILE_ID = "calendar.rss.xls.id";
74
75
76 private static final String PROPERTY_ERROR_MESSAGE = "calendar.message.errorDownloadFile";
77
78
79
80
81
82
83
84
85 public void downloadCalendarToFile( HttpServletRequest request, HttpServletResponse response )
86 throws SiteMessageException
87 {
88 byte[] result = null;
89 String strAgenda = request.getParameter( Constants.PARAM_AGENDA );
90 String strStyleSheetId = request.getParameter( Constants.PARAMETER_STYLESHEET_ID );
91 String strRss = request.getParameter( Constants.PARAMETER_ACTION );
92 String strXml = Constants.EMPTY_STRING;
93 String strEventSort = request.getParameter( Constants.PARAMETER_SORT_EVENTS );
94 String[] arrayCategory = request.getParameterValues( Constants.PARAMETER_CATEGORY );
95 String[] arrayCalendar = request.getParameterValues( Constants.PARAMETER_CALENDAR_ID );
96 int[] arrayCategoryIds = null;
97 int[] arrayCalendarIds = null;
98
99
100 if ( ( strAgenda != null ) && strAgenda.equals( "none" ) )
101 {
102 SiteMessageService.setMessage( request, Messages.MANDATORY_FIELDS, SiteMessage.TYPE_STOP );
103 }
104
105
106 if ( arrayCategory != null )
107 {
108 if ( arrayCategory.length != 0 )
109 {
110 arrayCategoryIds = new int[arrayCategory.length];
111 int i = 0;
112
113 for ( String strIdCategory : arrayCategory )
114 {
115 if ( strIdCategory != null )
116 {
117 arrayCategoryIds[i++] = Integer.parseInt( strIdCategory );
118 }
119 }
120 }
121 }
122
123
124 if ( arrayCalendar != null )
125 {
126 if ( !arrayCalendar[0].equals( Constants.PROPERTY_CALENDAR_NONE ) )
127 {
128 arrayCalendarIds = new int[arrayCalendar.length];
129
130 int i = 0;
131
132 for ( String strId : arrayCalendar )
133 {
134 if ( strId != null )
135 {
136 arrayCalendarIds[i++] = Integer.parseInt( strId );
137 }
138 }
139 }
140 }
141
142 CalendarFilter filter = new CalendarFilter( );
143
144 if ( strRss != null )
145 {
146 filter.setCategoriesId( arrayCategoryIds );
147 filter.setCalendarIds( arrayCalendarIds );
148 filter.setSortEvents( ( strEventSort != null ) ? Integer.parseInt( strEventSort ) : ( -1 ) );
149 strXml = XMLUtils.getRssXml( filter );
150 }
151 else
152 {
153 strXml = XMLUtils.getAgendaXml( strAgenda, request );
154 }
155
156 try
157 {
158 String strFileName = Constants.EMPTY_STRING;
159 ByteArrayInputStream baSource = null;
160
161 Plugin plugin = PluginService.getPlugin( Constants.PLUGIN_NAME );
162
163 if ( strStyleSheetId != null )
164 {
165 int nStyleSheetId = Integer.parseInt( strStyleSheetId );
166 StyleSheet stylesheet = CalendarStyleSheetHome.findByPrimaryKey( nStyleSheetId, plugin );
167 String strAgendaExtension = CalendarStyleSheetHome.getExtension( nStyleSheetId, plugin );
168 baSource = new ByteArrayInputStream( stylesheet.getSource( ) );
169 strFileName = PROPERTY_FILE_EXPORT_NAME + "." + strAgendaExtension;
170 response.setHeader( "Content-disposition", "attachement; filename=\"" + strFileName + "\"" );
171 }
172 else if ( strRss != null )
173 {
174 int nStyleSheetId = AppPropertiesService.getPropertyInt( PROPERTY_CALENDAR_RSS_FILE_ID, 1 );
175 StyleSheet stylesheet = CalendarStyleSheetHome.findByPrimaryKey( nStyleSheetId, plugin );
176 String strAgendaExtension = CalendarStyleSheetHome.getExtension( nStyleSheetId, plugin );
177 baSource = new ByteArrayInputStream( stylesheet.getSource( ) );
178 strFileName = PROPERTY_RSS_FILE_NAME + "." + strAgendaExtension;
179 response.setHeader( "Content-disposition", "inline; filename=\"" + strFileName + "\"" );
180 }
181
182 if ( baSource != null )
183 {
184 HttpSession session = request.getSession( );
185 ServletContext context = session.getServletContext( );
186 String strMimetype = context.getMimeType( strFileName );
187 response.setContentType( ( strMimetype != null ) ? strMimetype : "application/octet-stream" );
188 result = XMLUtils.transformXMLToXSL( strXml, baSource, null, null );
189 }
190
191 OutputStream os = response.getOutputStream( );
192 os.write( result );
193 os.flush( );
194 os.close( );
195 }
196 catch ( IOException e )
197 {
198 throw new AppException( I18nService.getLocalizedString( PROPERTY_ERROR_MESSAGE, request.getLocale( ) ) );
199 }
200 catch ( Exception e )
201 {
202 throw new AppException( I18nService.getLocalizedString( PROPERTY_ERROR_MESSAGE, request.getLocale( ) ) );
203 }
204 }
205
206
207
208
209
210
211
212 public void downloadXSLFileFromDatabase( HttpServletRequest request, HttpServletResponse response )
213 {
214 AdminUser user = AdminUserService.getAdminUser( request );
215
216 if ( ( user != null ) && ( user.checkRight( StyleSheetJspBean.RIGHT_MANAGE_STYLESHEET ) ) )
217 {
218 String strStyleSheetId = request.getParameter( Parameters.STYLESHEET_ID );
219
220 try
221 {
222 if ( strStyleSheetId != null )
223 {
224 int nStyleSheetId = Integer.parseInt( strStyleSheetId );
225
226 Plugin plugin = PluginService.getPlugin( Constants.PLUGIN_NAME );
227
228 StyleSheet stylesheet = CalendarStyleSheetHome.findByPrimaryKey( nStyleSheetId, plugin );
229
230 HttpSession session = request.getSession( );
231 ServletContext context = session.getServletContext( );
232 String strMimetype = context.getMimeType( stylesheet.getFile( ) );
233 response.setContentType( ( strMimetype != null ) ? strMimetype : "application/octet-stream" );
234 response.setHeader( "Content-Disposition", "attachement; filename=\"" + stylesheet.getFile( )
235 + "\"" );
236
237 OutputStream out = response.getOutputStream( );
238 out.write( stylesheet.getSource( ) );
239 out.flush( );
240 out.close( );
241 }
242 }
243 catch ( IOException e )
244 {
245 throw new AppException( I18nService.getLocalizedString( PROPERTY_ERROR_MESSAGE, request.getLocale( ) ) );
246 }
247 }
248 }
249 }