View Javadoc
1   /*
2    * Copyright (c) 2002-2017, Mairie de Paris
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    *  1. Redistributions of source code must retain the above copyright notice
10   *     and the following disclaimer.
11   *
12   *  2. Redistributions in binary form must reproduce the above copyright notice
13   *     and the following disclaimer in the documentation and/or other materials
14   *     provided with the distribution.
15   *
16   *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17   *     contributors may be used to endorse or promote products derived from
18   *     this software without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   *
32   * License 1.0
33   */
34  package fr.paris.lutece.plugins.document.modules.export.web;
35  
36  import fr.paris.lutece.plugins.document.business.DocumentFilter;
37  import fr.paris.lutece.plugins.document.modules.export.dto.exportdocument.ExportUserDocumentDTO;
38  import fr.paris.lutece.plugins.document.modules.export.service.DocumentExportService;
39  import fr.paris.lutece.plugins.document.service.DocumentPlugin;
40  import fr.paris.lutece.portal.service.message.AdminMessage;
41  import fr.paris.lutece.portal.service.message.AdminMessageService;
42  import fr.paris.lutece.portal.service.plugin.PluginService;
43  import fr.paris.lutece.portal.service.spring.SpringContextService;
44  import fr.paris.lutece.portal.service.util.AppLogService;
45  import fr.paris.lutece.portal.service.util.AppPathService;
46  import fr.paris.lutece.portal.service.util.AppPropertiesService;
47  import fr.paris.lutece.portal.web.admin.PluginAdminPageJspBean;
48  
49  import java.io.IOException;
50  import java.io.OutputStream;
51  import java.io.StringWriter;
52  import java.util.List;
53  import java.util.Locale;
54  
55  import javax.servlet.http.HttpServletRequest;
56  import javax.servlet.http.HttpServletResponse;
57  
58  import au.com.bytecode.opencsv.CSVWriter;
59  
60  /**
61   * 
62   * The DocumentExport JspBean
63   * 
64   */
65  public class DocumentExportJspBean extends PluginAdminPageJspBean
66  {
67  
68  	// constants
69  	private static final char CONSTANT_SEPARATOR = ';';
70  
71  	private static final String CONSTANT_EXPORT_FILE_NAME_DEFAULT = "documents_export.csv";
72  
73  	private static final String[] CONSTANT_HEADER_CSV_FILE =
74  	{ "Identifiant", "Titre", "Type", "Etat", "Date de début de validité", "Date de fin de validité", "Date de création", "Date de modification", "Espace" };
75  
76  	// properties
77  	private static final String PROPERTY_EXPORT_FILE_NAME = "document-export.export_documents.file.name";
78  
79  	private static final String PROPERTY_EXPORT_FILE_ENCODING = "document-export.export_documents.file.encoding";
80  
81  	private static final String PROPERTY_PLUGIN_DISABLED = "module.document.export.error.plugin.disabled.message";
82  
83      private DocumentExportService _documentExportService = SpringContextService
84              .getBean( "document-export.exportService" );
85  
86      /**
87       * Effectue l'export de la liste des documents
88       * @param request la requete Http
89       * @param response la reponse
90       * @return The next URL to redirect to
91       */
92  	public String doExportDocumentsDataList( HttpServletRequest request, HttpServletResponse response )
93  	{
94          if ( !PluginService.isPluginEnable( DocumentPlugin.PLUGIN_NAME ) )
95  		{
96  			return AdminMessageService.getMessageUrl( request, PROPERTY_PLUGIN_DISABLED, AdminMessage.TYPE_ERROR );
97  		}
98  
99  		DocumentFilter filter = new DocumentFilter();
100 		/*
101 		 * Construction du filtre
102 		 */
103         filter.setLoadBinaries( false );
104 		Locale locale = getLocale();
105 
106 		List<ExportUserDocumentDTO> listExportResults = _documentExportService.getListDocumentByFilter( filter, locale );
107 
108 		try
109 		{
110 			StringWriter strWriter = new StringWriter();
111 			CSVWriter csvWriter = new CSVWriter( strWriter, CONSTANT_SEPARATOR );
112 			csvWriter.writeNext( CONSTANT_HEADER_CSV_FILE );
113 			for ( ExportUserDocumentDTO documentData : listExportResults )
114 			{
115 				csvWriter.writeNext( documentData.toTabString() );
116 			}
117 			String strEncoding = AppPropertiesService.getProperty( PROPERTY_EXPORT_FILE_ENCODING );
118 			byte[] byteFileOutPut = strWriter.toString().getBytes( strEncoding );
119 
120 			String strFileName = AppPropertiesService.getProperty( PROPERTY_EXPORT_FILE_NAME, CONSTANT_EXPORT_FILE_NAME_DEFAULT );
121 			response.setHeader( "Content-Disposition", "attachment; filename=\"" + strFileName + " \";" );
122 			response.setHeader( "Pragma", "public" );
123 			response.setHeader( "Expires", "0" );
124 			response.setHeader( "Cache-Control", "must-revalidate,post-check=0,pre-check=0" );
125 			response.setContentType( "enctype=multipart/form-data" );
126 
127 			OutputStream os = response.getOutputStream();
128 			os.write( byteFileOutPut );
129 			os.close();
130 
131 			csvWriter.close();
132 			strWriter.close();
133 
134 		}
135 		catch ( IOException e )
136 		{
137 			AppLogService.error( e.getMessage(), e );
138 		}
139 		catch ( Exception e )
140 		{
141 			AppLogService.error( e.getMessage(), e );
142 		}
143 
144 		return AppPathService.getBaseUrl( request );
145 
146 	}
147 
148 }