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.suggest.web.action;
35
36 import fr.paris.lutece.plugins.suggest.business.CommentSubmit;
37 import fr.paris.lutece.plugins.suggest.business.Suggest;
38 import fr.paris.lutece.plugins.suggest.business.SuggestHome;
39 import fr.paris.lutece.plugins.suggest.business.SuggestSubmit;
40 import fr.paris.lutece.plugins.suggest.business.EntryFilter;
41 import fr.paris.lutece.plugins.suggest.business.EntryHome;
42 import fr.paris.lutece.plugins.suggest.business.ExportFormat;
43 import fr.paris.lutece.plugins.suggest.business.ExportFormatHome;
44 import fr.paris.lutece.plugins.suggest.business.SubmitFilter;
45 import fr.paris.lutece.plugins.suggest.service.CommentSubmitService;
46 import fr.paris.lutece.plugins.suggest.service.SuggestSubmitService;
47 import fr.paris.lutece.plugins.suggest.service.SuggestPlugin;
48 import fr.paris.lutece.plugins.suggest.service.SuggestResourceIdService;
49 import fr.paris.lutece.plugins.suggest.utils.SuggestUtils;
50 import fr.paris.lutece.portal.business.user.AdminUser;
51 import fr.paris.lutece.portal.service.admin.AccessDeniedException;
52 import fr.paris.lutece.portal.service.html.XmlTransformerService;
53 import fr.paris.lutece.portal.service.i18n.I18nService;
54 import fr.paris.lutece.portal.service.message.AdminMessage;
55 import fr.paris.lutece.portal.service.message.AdminMessageService;
56 import fr.paris.lutece.portal.service.plugin.Plugin;
57 import fr.paris.lutece.portal.service.plugin.PluginService;
58 import fr.paris.lutece.portal.service.rbac.RBACService;
59 import fr.paris.lutece.portal.service.util.AppLogService;
60 import fr.paris.lutece.portal.service.util.AppPropertiesService;
61 import fr.paris.lutece.portal.web.pluginaction.AbstractPluginAction;
62 import fr.paris.lutece.portal.web.pluginaction.DefaultPluginActionResult;
63 import fr.paris.lutece.portal.web.pluginaction.IPluginActionResult;
64 import fr.paris.lutece.util.UniqueIDGenerator;
65 import fr.paris.lutece.util.filesystem.FileSystemUtil;
66 import fr.paris.lutece.util.xml.XmlUtil;
67
68 import java.io.IOException;
69 import java.io.OutputStream;
70
71 import java.util.List;
72 import java.util.Map;
73
74 import javax.servlet.http.HttpServletRequest;
75 import javax.servlet.http.HttpServletResponse;
76
77
78
79
80
81
82 public class ExportSuggestSubmitAction extends AbstractPluginAction<SuggestAdminSearchFields> implements ISuggestAction
83 {
84 private static final String ACTION_NAME = "Export Suggest XSL";
85 private static final String MESSAGE_YOU_ARE_NOT_ALLOWED_TO_DOWLOAD_THIS_FILE = "suggest.message.youAreNotAllowedToDownloadFile";
86 private static final String MESSAGE_YOU_MUST_SELECT_EXPORT_FORMAT = "suggest.message.youMustSelectExportFormat";
87 private static final String MESSAGE_ERROR_DURING_DOWNLOAD_FILE = "suggest.message.errorDuringDownloadFile";
88 private static final String MARK_EXPORT_FORMAT_REF_LIST = "export_format_list";
89 private static final String PARAMETER_BUTTON_EXPORT_ALL = "export_search_all";
90 private static final String PARAMETER_BUTTON_EXPORT_SEARCH = "export_search_result";
91 private static final String PARAMETER_ID_SUGGEST = "id_suggest";
92 private static final String XSL_UNIQUE_PREFIX_ID = UniqueIDGenerator.getNewId( ) + "suggest-";
93 private static final String PARAMETER_ID_EXPORT_FORMAT = "id_export_format";
94 private static final String EXPORT_CSV_EXT = "csv";
95 private static final String DEAFULT_ENCODING = "UTF-8";
96 private static final String CONSTANT_MIME_TYPE_CSV = "application/csv";
97 private static final String CONSTANT_MIME_TYPE_OCTETSTREAM = "application/octet-stream";
98
99
100 private static final String PROPERTY_EXPORT_ENCODING_CSV = "suggest.exportFileEncoding.csv";
101 private static final String PROPERTY_EXPORT_ENCODING_XML = "suggest.exportFileEncoding.xml";
102
103
104
105
106 @Override
107 public void fillModel( HttpServletRequest request, AdminUser adminUser, Map<String, Object> model )
108 {
109 model.put( MARK_EXPORT_FORMAT_REF_LIST, ExportFormatHome.getListExport( getPlugin( ) ) );
110 }
111
112
113
114
115 @Override
116 public String getName( )
117 {
118 return ACTION_NAME;
119 }
120
121
122
123
124 @Override
125 public String getButtonTemplate( )
126 {
127 return null;
128 }
129
130
131
132
133 @Override
134 public boolean isInvoked( HttpServletRequest request )
135 {
136 return ( request.getParameter( PARAMETER_BUTTON_EXPORT_SEARCH ) != null ) || ( request.getParameter( PARAMETER_BUTTON_EXPORT_ALL ) != null );
137 }
138
139
140
141
142 @Override
143 public IPluginActionResult process( HttpServletRequest request, HttpServletResponse response, AdminUser adminUser, SuggestAdminSearchFields searchFields )
144 throws AccessDeniedException
145 {
146 Plugin plugin = getPlugin( );
147 IPluginActionResult result = new DefaultPluginActionResult( );
148
149 String strIdExportFormat = request.getParameter( PARAMETER_ID_EXPORT_FORMAT );
150 String strIdSuggest = request.getParameter( PARAMETER_ID_SUGGEST );
151 int nIdExportFormat = SuggestUtils.getIntegerParameter( strIdExportFormat );
152 int nIdSuggest = SuggestUtils.getIntegerParameter( strIdSuggest );
153
154 Suggest suggest = SuggestHome.findByPrimaryKey( nIdSuggest, plugin );
155
156 if ( ( suggest == null )
157 || !RBACService.isAuthorized( Suggest.RESOURCE_TYPE, Integer.toString( suggest.getIdSuggest( ) ),
158 SuggestResourceIdService.PERMISSION_MANAGE_SUGGEST_SUBMIT, adminUser ) )
159 {
160 throw new AccessDeniedException( I18nService.getLocalizedString( MESSAGE_YOU_ARE_NOT_ALLOWED_TO_DOWLOAD_THIS_FILE, request.getLocale( ) ) );
161 }
162
163 ExportFormat exportFormat;
164 exportFormat = ExportFormatHome.findByPrimaryKey( nIdExportFormat, plugin );
165
166 if ( exportFormat == null )
167 {
168 result.setRedirect( AdminMessageService.getMessageUrl( request, MESSAGE_YOU_MUST_SELECT_EXPORT_FORMAT, AdminMessage.TYPE_STOP ) );
169 }
170 else
171 {
172 EntryFilterst/business/EntryFilter.html#EntryFilter">EntryFilter entryfilter = new EntryFilter( );
173 entryfilter.setIdSuggest( suggest.getIdSuggest( ) );
174
175 suggest.setEntries( EntryHome.getEntryList( entryfilter, plugin ) );
176
177 SubmitFilter filter = SuggestUtils.getSuggestSubmitFilter( searchFields );
178 List<Integer> listIdSuggestSubmit = SuggestSubmitService.getService( ).getSuggestSubmitListId( filter, plugin );
179
180 StringBuffer strBufferListSuggestSubmitXml = new StringBuffer( );
181 SuggestSubmit suggestSubmit = null;
182 List<CommentSubmit> listCommentSubmit;
183
184 filter = new SubmitFilter( );
185
186 for ( Integer nIdSuggestSubmit : listIdSuggestSubmit )
187 {
188 suggestSubmit = SuggestSubmitService.getService( ).findByPrimaryKey( nIdSuggestSubmit, false, true, plugin );
189 filter.setIdSuggestSubmit( nIdSuggestSubmit );
190 listCommentSubmit = CommentSubmitService.getService( ).getCommentSubmitList( filter, plugin );
191 suggestSubmit.setComments( listCommentSubmit );
192 suggestSubmit.setSuggest( suggest );
193 strBufferListSuggestSubmitXml.append( suggestSubmit.getXml( request, adminUser.getLocale( ) ) );
194 }
195
196 String strXmlSource = XmlUtil.getXmlHeader( ) + suggest.getXml( request, strBufferListSuggestSubmitXml, adminUser.getLocale( ) );
197 XmlTransformerService xmlTransformerService = new XmlTransformerService( );
198
199 String strFileOutPut = xmlTransformerService.transformBySourceWithXslCache( strXmlSource, exportFormat.getXsl( ), XSL_UNIQUE_PREFIX_ID
200 + nIdExportFormat, null, null );
201
202 String strFormatExtension = exportFormat.getExtension( ).trim( );
203 String strFileName = suggest.getTitle( ) + "." + strFormatExtension;
204 boolean isExporTypeCSV = ( ( strFormatExtension != null ) && strFormatExtension.equals( EXPORT_CSV_EXT ) );
205
206 String strEncoding = isExporTypeCSV ? AppPropertiesService.getProperty( PROPERTY_EXPORT_ENCODING_CSV, DEAFULT_ENCODING ) : AppPropertiesService
207 .getProperty( PROPERTY_EXPORT_ENCODING_XML, DEAFULT_ENCODING );
208 String strResponseContentType = null;
209
210 if ( isExporTypeCSV )
211 {
212 strResponseContentType = CONSTANT_MIME_TYPE_CSV;
213 }
214 else
215 {
216 String strMimeType = FileSystemUtil.getMIMEType( strFileName );
217 strResponseContentType = ( strMimeType != null ) ? strMimeType : CONSTANT_MIME_TYPE_OCTETSTREAM;
218 }
219
220 SuggestUtils.addHeaderResponse( request, response, strFileName );
221 response.setContentType( strResponseContentType );
222 response.setCharacterEncoding( strEncoding );
223
224 try
225 {
226 byte [ ] byteFileOutPut = strFileOutPut.getBytes( strEncoding );
227 response.setContentLength( (int) byteFileOutPut.length );
228
229 OutputStream os = response.getOutputStream( );
230 os.write( byteFileOutPut );
231 os.close( );
232 }
233 catch( IOException e )
234 {
235 AppLogService.error( e.getMessage( ), e );
236
237 result.setRedirect( AdminMessageService.getMessageUrl( request, MESSAGE_ERROR_DURING_DOWNLOAD_FILE, AdminMessage.TYPE_STOP ) );
238 }
239 }
240
241 result.setNoop( true );
242
243 return result;
244 }
245
246
247
248
249
250
251 private Plugin getPlugin( )
252 {
253 return PluginService.getPlugin( SuggestPlugin.PLUGIN_NAME );
254 }
255 }