View Javadoc
1   /*
2    * Copyright (c) 2002-2014, 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.directory.modules.pdfproducerarchive.service;
35  
36  import fr.paris.lutece.plugins.archiveclient.service.archive.IArchiveClientService;
37  import fr.paris.lutece.plugins.archiveclient.service.util.ArchiveClientConstants;
38  import fr.paris.lutece.plugins.archiveclient.service.util.ArchiveClientException;
39  import fr.paris.lutece.plugins.directory.modules.pdfproducer.utils.PDFUtils;
40  import fr.paris.lutece.plugins.directory.modules.pdfproducerarchive.utils.FilesUtils;
41  import fr.paris.lutece.plugins.directory.utils.DirectoryUtils;
42  import fr.paris.lutece.portal.business.user.AdminUser;
43  import fr.paris.lutece.portal.service.admin.AdminUserService;
44  import fr.paris.lutece.portal.service.util.AppLogService;
45  import fr.paris.lutece.portal.service.util.AppPropertiesService;
46  import fr.paris.lutece.util.string.StringUtil;
47  
48  import java.io.File;
49  import java.io.FileNotFoundException;
50  import java.io.FileOutputStream;
51  import java.util.List;
52  import java.util.Locale;
53  
54  import javax.servlet.http.HttpServletRequest;
55  
56  import org.apache.commons.io.IOUtils;
57  import org.apache.commons.lang.StringUtils;
58  
59  
60  /**
61   * ZipUtils
62   */
63  public final class ZipService
64  {
65      public static final String ARCHIVE_STATE_INITIAL = "INIT";
66      public static final String ARCHIVE_STATE_USED = "USED";
67      public static final String ARCHIVE_STATE_ERROR = "ERROR";
68      public static final String ARCHIVE_STATE_FINAL = "FINAL";
69      public static final String PROPERTY_ZIP_NAME_REPOSITORY = "directory.zipbasket.name_zip_repository";
70      public static final String EXTENSION_FILE_ZIP = ".zip";
71      public static final String EXTENSION_FILE_PDF = ".pdf";
72      private IArchiveClientService _archiveClientService;
73  
74      /**
75      * Constructor
76      */
77      private ZipService(  )
78      {
79      }
80  
81      /**
82       * Method to generate zip
83       * @param request request
84       * @param strName directory name
85       * @param nIdKeyUser id key user
86       * @param nIdDirectory id directory
87       * @param listIdEntryConfig config to build pdf
88       * @param nIdRecord the id record
89       * @return the archive id
90       */
91      public int doGeneratePDFAndZip( HttpServletRequest request, String strName, int nIdKeyUser, int nIdDirectory,
92          List<Integer> listIdEntryConfig, int nIdRecord )
93      {
94          return doGeneratePDFAndZip( AdminUserService.getAdminUser( request ), AdminUserService.getLocale( request ),
95                  strName, nIdKeyUser, nIdDirectory, listIdEntryConfig, nIdRecord );
96      }
97  
98      /**
99       * Method to generate zip
100      * @param adminUser The adminUser
101      * @param locale The locale
102      * @param strName directory name
103      * @param nIdKeyUser id key user
104      * @param nIdDirectory id directory
105      * @param listIdEntryConfig config to build pdf
106      * @param nIdRecord the id record
107      * @return the archive id
108      */
109     public int doGeneratePDFAndZip( AdminUser adminUser, Locale locale, String strName, int nIdKeyUser,
110             int nIdDirectory, List<Integer> listIdEntryConfig, int nIdRecord )
111     {
112         String strDirectoryName = strName;
113 
114         if ( StringUtils.isNotEmpty( strName ) )
115         {
116             strDirectoryName = StringUtil.replaceAccent( strName ).replaceAll( "\\W", "_" );
117         }
118 
119         String strPathFilesGenerate = FilesUtils.builNamePathBasket( nIdKeyUser, nIdDirectory ) + File.separator +
120             strDirectoryName;
121         String strPathZipGenerate = FilesUtils.builNamePathBasket( nIdKeyUser, nIdDirectory ) + File.separator +
122             AppPropertiesService.getProperty( PROPERTY_ZIP_NAME_REPOSITORY );
123 
124         FilesUtils.createTemporyZipDirectory( strPathFilesGenerate );
125         FilesUtils.createTemporyZipDirectory( strPathZipGenerate );
126 
127         FileOutputStream os = null;
128 
129         try
130         {
131             os = new FileOutputStream( new File( strPathFilesGenerate + "/" + strDirectoryName + EXTENSION_FILE_PDF ) );
132             PDFUtils.doCreateDocumentPDF( adminUser, locale, strDirectoryName, os, nIdRecord, listIdEntryConfig, true );
133         }
134         catch ( FileNotFoundException e )
135         {
136             AppLogService.error( e );
137         }
138         finally
139         {
140             IOUtils.closeQuietly( os );
141         }
142 
143         FilesUtils.getAllFilesRecorded( adminUser, strPathFilesGenerate, listIdEntryConfig, nIdRecord );
144 
145         int nArchiveItemKey;
146 
147         try
148         {
149             nArchiveItemKey = _archiveClientService.generateArchive( strPathFilesGenerate, strPathZipGenerate,
150                     strDirectoryName + EXTENSION_FILE_ZIP, ArchiveClientConstants.ARCHIVE_TYPE_ZIP );
151         }
152         catch ( Exception e )
153         {
154             AppLogService.error( e );
155 
156             return -1;
157         }
158 
159         return nArchiveItemKey;
160     }
161 
162     /**
163      * Method to delete zip
164      * @param strIdRecord id record
165      * @param nArchiveItemKey id archive item
166      * @param nIdKeyUser id user key
167      * @param nIdDirectory id directory
168      * @param zipName zip name
169      * @return true if the zip is deleted otherwise false
170      */
171     public boolean doDeleteZip( String strIdRecord, int nArchiveItemKey, int nIdKeyUser, int nIdDirectory,
172         String zipName )
173     {
174         String strArchiveStatus = _archiveClientService.informationArchive( nArchiveItemKey );
175 
176         if ( !ARCHIVE_STATE_USED.equals( strArchiveStatus ) )
177         {
178             String strPathFilesGenerate = FilesUtils.builNamePathBasket( nIdKeyUser, nIdDirectory ) + File.separator +
179                 zipName;
180 
181             if ( DirectoryUtils.convertStringToInt( strIdRecord ) != DirectoryUtils.CONSTANT_ID_NULL )
182             {
183                 // Delete the temp folder in case it still exists
184                 FilesUtils.cleanTemporyZipDirectory( strPathFilesGenerate );
185             }
186 
187             try
188             {
189                 // Archive client must handle the deletion of the files, not directory-pdfproducer-archive
190                 _archiveClientService.removeArchive( nArchiveItemKey );
191             }
192             catch ( ArchiveClientException e )
193             {
194                 AppLogService.error( e.getMessage(  ), e );
195 
196                 return false;
197             }
198 
199             return true;
200         }
201         else
202         {
203             return false;
204         }
205     }
206 
207     /**
208      * Method to do zip
209      * @param strFolderToArchive path to the folder to archive
210      * @param strArchiveDestination path to the destination folder which will store the archive
211      * @param strArchiveName the name of the archive
212      * @param strArchiveType the archive type(zip,..)
213      * @return the archive id
214      */
215     public int doBasicZip( String strFolderToArchive, String strArchiveDestination, String strArchiveName,
216         String strArchiveType )
217     {
218         return _archiveClientService.generateArchive( strFolderToArchive, strArchiveDestination, strArchiveName,
219             strArchiveType );
220     }
221 
222     /**
223      * Method to get the zip status
224      * @param nArchiveItemKey id of archive item
225      * @return the zip status
226      */
227     public String getStatutZip( int nArchiveItemKey )
228     {
229         return _archiveClientService.informationArchive( nArchiveItemKey );
230     }
231 
232     /**
233      * Method to get the url zip
234      * @param nArchiveItemKey id of archive item
235      * @return the url zip
236      */
237     public String getUrlZip( int nArchiveItemKey )
238     {
239         return _archiveClientService.getDownloadUrl( nArchiveItemKey );
240     }
241 
242     /**
243     * Method to set _archiveClientService
244     * @param archiveClientService archiveClientService
245     */
246     public void setArchiveClientService( IArchiveClientService archiveClientService )
247     {
248         _archiveClientService = archiveClientService;
249     }
250 }