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.util.ArchiveClientConstants;
37  import fr.paris.lutece.plugins.directory.business.Directory;
38  import fr.paris.lutece.plugins.directory.business.DirectoryHome;
39  import fr.paris.lutece.plugins.directory.business.Record;
40  import fr.paris.lutece.plugins.directory.business.RecordField;
41  import fr.paris.lutece.plugins.directory.business.RecordFieldFilter;
42  import fr.paris.lutece.plugins.directory.business.RecordFieldHome;
43  import fr.paris.lutece.plugins.directory.business.RecordHome;
44  import fr.paris.lutece.plugins.directory.modules.pdfproducer.service.DirectoryPDFProducerPlugin;
45  import fr.paris.lutece.plugins.directory.modules.pdfproducerarchive.business.zipbasket.ZipBasket;
46  import fr.paris.lutece.plugins.directory.modules.pdfproducerarchive.business.zipbasket.ZipBasketAction;
47  import fr.paris.lutece.plugins.directory.modules.pdfproducerarchive.business.zipbasket.ZipBasketActionHome;
48  import fr.paris.lutece.plugins.directory.modules.pdfproducerarchive.business.zipbasket.ZipBasketHome;
49  import fr.paris.lutece.plugins.directory.modules.pdfproducerarchive.utils.FilesUtils;
50  import fr.paris.lutece.plugins.directory.modules.pdfproducerarchive.utils.StatusZipEnum;
51  import fr.paris.lutece.plugins.directory.service.DirectoryPlugin;
52  import fr.paris.lutece.portal.business.user.AdminUser;
53  import fr.paris.lutece.portal.service.admin.AdminUserService;
54  import fr.paris.lutece.portal.service.plugin.Plugin;
55  import fr.paris.lutece.portal.service.plugin.PluginService;
56  import fr.paris.lutece.portal.service.rbac.RBACService;
57  import fr.paris.lutece.portal.service.spring.SpringContextService;
58  import fr.paris.lutece.portal.service.util.AppPropertiesService;
59  
60  import java.io.File;
61  import java.util.Date;
62  import java.util.List;
63  import java.util.Locale;
64  
65  import javax.servlet.http.HttpServletRequest;
66  
67  import org.apache.commons.lang.StringUtils;
68  
69  
70  /**
71   * Service to manage zipbasket *
72   */
73  public class DirectoryManageZipBasketService
74  {
75      public static final String ARCHIVE_STATE_INITIAL = "INIT";
76      public static final String ARCHIVE_STATE_USED = "USED";
77      public static final String ARCHIVE_STATE_ERROR = "ERROR";
78      public static final String ARCHIVE_STATE_FINAL = "FINAL";
79      public static final String PROPERTY_ZIP_NAME_REPOSITORY = "directory.zipbasket.name_zip_repository";
80      public static final String EXTENSION_FILE_ZIP = ".zip";
81      public static final String EXTENSION_FILE_PDF = ".pdf";
82  
83      /**
84       * this method builds different repository to stock files and generate a PDF and a zip file of this repository
85       * @param request request
86       * @param strName the name of the futur zip
87       * @param nIdAdminUser id of admin user
88       * @param plugin plugin
89       * @param nIdDirectory id of directory
90       * @param nIdRecord id of record
91       * @param listIdConfig list of id config
92       * @return true if the zip is correctly added in the database, false if the zip already exists
93       */
94      public boolean addZipBasket( HttpServletRequest request, String strName, int nIdAdminUser, Plugin plugin,
95          int nIdDirectory, int nIdRecord, List<Integer> listIdConfig )
96      {
97          return addZipBasket( AdminUserService.getAdminUser( request ), AdminUserService.getLocale( request ), strName,
98                  nIdAdminUser, plugin, nIdDirectory, nIdRecord, listIdConfig );
99      }
100 
101     /**
102      * this method builds different repository to stock files and generate a PDF
103      * and a zip file of this repository
104      * @param adminUser The adminUser
105      * @param Locale The locale
106      * @param strName the name of the futur zip
107      * @param nIdAdminUser id of admin user
108      * @param plugin plugin
109      * @param nIdDirectory id of directory
110      * @param nIdRecord id of record
111      * @param listIdConfig list of id config
112      * @return true if the zip is correctly added in the database, false if the
113      *         zip already exists
114      */
115     public boolean addZipBasket( AdminUser adminUser, Locale locale, String strName, int nIdAdminUser, Plugin plugin,
116             int nIdDirectory, int nIdRecord, List<Integer> listIdConfig )
117     {
118         if ( !ZipBasketHome.existsZipBasket( nIdAdminUser, plugin, nIdDirectory, nIdRecord ) )
119         {
120             int nArchiveItemKey = getZipService( ).doGeneratePDFAndZip( adminUser, locale, strName, nIdAdminUser,
121                     nIdDirectory, listIdConfig,
122                             nIdRecord );
123 
124             if ( nArchiveItemKey == -1 )
125             {
126                 return false;
127             }
128 
129             ZipBasketHome.addZipBasket( strName, nIdAdminUser, plugin, nIdDirectory, nIdRecord, nArchiveItemKey );
130 
131             return true;
132         }
133         else
134         {
135             return false;
136         }
137     }
138 
139     /**
140     * This method load all element in basket by id admin user for a specific directory
141     * @param plugin plugin
142     * @param nIdAdminUser id of admin user
143     * @param nIdDirectory id of directory
144     * @return list of ZipBasket
145     */
146     public List<ZipBasket> loadAllZipBasketByAdminUser( Plugin plugin, int nIdAdminUser, int nIdDirectory )
147     {
148         return ZipBasketHome.loadAllZipBasketByAdminUser( plugin, nIdAdminUser, nIdDirectory );
149     }
150 
151     /**
152      * This method load all element in basket by id admin user for a specific
153      * directory by order
154      * @param plugin plugin
155      * @param nIdAdminUser id of admin user
156      * @param nIdDirectory id of directory
157      * @return list of ZipBasket
158      */
159     public List<ZipBasket> loadAllZipBasketByAdminUserOrder( Plugin plugin, int nIdAdminUser, int nIdDirectory )
160     {
161         return ZipBasketHome.loadAllZipBasketByAdminUserOrder( plugin, nIdAdminUser, nIdDirectory );
162     }
163 
164     /**
165      * Find zip basket by date
166      * @param plugin the plugin
167      * @param dateExpiry the date expiry
168      * @return a list of {@link ZipBasket}
169      */
170     public List<ZipBasket> loadZipBasketByDate( Plugin plugin, Date dateExpiry )
171     {
172         return ZipBasketHome.loadZipBasketByDate( plugin, dateExpiry );
173     }
174 
175     /**
176      * This method delete a ZipBasket, its PDF and attachments after update status
177      * @param plugin plugin
178      * @param nIdZipBasket id of the zipbasket
179      * @param strIdRecord id of record
180      * @return true if the zipbasket can be deleted otherwise false
181      */
182     public boolean deleteZipBasket( Plugin plugin, int nIdZipBasket, String strIdRecord )
183     {
184         return deleteZipBasket( plugin, nIdZipBasket, strIdRecord, null );
185     }
186 
187     /**
188      * This method delete a ZipBasket, its PDF and attachments after update status
189      * @param plugin plugin
190      * @param nIdZipBasket id of the zipbasket
191      * @param strIdRecord id of record
192      * @param sbLog the log
193      * @return true if the zipbasket can be deleted otherwise false
194      */
195     public boolean deleteZipBasket( Plugin plugin, int nIdZipBasket, String strIdRecord, StringBuilder sbLog )
196     {
197         updateZipBasketStatus( sbLog );
198 
199         ZipBasket zipbasket = ZipBasketHome.loadZipBasket( plugin, nIdZipBasket );
200         int nArchiveItemKey = zipbasket.getArchiveItemKey(  );
201         int nIdAdminUser = zipbasket.getIdAdminUser(  );
202         int nIdDirectory = zipbasket.getIdDirectory(  );
203         String zipName = zipbasket.getZipName(  );
204 
205         if ( getZipService(  ).doDeleteZip( strIdRecord, nArchiveItemKey, nIdAdminUser, nIdDirectory, zipName ) )
206         {
207             ZipBasketHome.deleteZipBasket( plugin, nIdZipBasket );
208             log( sbLog, "\n\tDeleting : SUCCESS " );
209 
210             return true;
211         }
212         else
213         {
214             log( sbLog, "\n\tDeleting : FAILED " );
215 
216             return false;
217         }
218     }
219 
220     /**
221      * Delete all zip
222      * @param plugin the plugin
223      * @param nIdDirectory the id directory
224      * @param nIdAdminUser the id admin user
225      * @return true if the zip basket can be deleted, false otherwise
226      */
227     public boolean deleteAllZipBasket( Plugin plugin, int nIdDirectory, int nIdAdminUser )
228     {
229         updateZipBasketStatus(  );
230 
231         for ( ZipBasket zipBasket : loadAllZipBasketByAdminUser( plugin, nIdAdminUser, nIdDirectory ) )
232         {
233             if ( getZipService(  )
234                          .doDeleteZip( Integer.toString( zipBasket.getIdRecord(  ) ), zipBasket.getArchiveItemKey(  ),
235                         nIdAdminUser, nIdDirectory, zipBasket.getZipName(  ) ) )
236             {
237                 ZipBasketHome.deleteZipBasket( plugin, zipBasket.getIdZip(  ) );
238             }
239             else
240             {
241                 return false;
242             }
243         }
244 
245         return true;
246     }
247 
248     /**
249      * this method delete several zip basket
250      * @param plugin plugin
251      * @param listIdZipBasket list of id zip basket
252      */
253     public void deleteMultiZipBasket( Plugin plugin, List<Integer> listIdZipBasket )
254     {
255         ZipBasketHome.deleteMultiZipBasket( plugin, listIdZipBasket );
256     }
257 
258     /**
259      * Get the directory from a given id directory
260      * @param nIdDirectory the id directory
261      * @return the directory
262      */
263     public Directory getDirectory( int nIdDirectory )
264     {
265         Plugin pluginDirectory = PluginService.getPlugin( DirectoryPlugin.PLUGIN_NAME );
266 
267         return DirectoryHome.findByPrimaryKey( nIdDirectory, pluginDirectory );
268     }
269 
270     /**
271      * Get the record from a given id record
272      * @param nIdRecord the id record
273      * @return the record
274      */
275     public Record getRecord( int nIdRecord )
276     {
277         Plugin pluginDirectory = PluginService.getPlugin( DirectoryPlugin.PLUGIN_NAME );
278 
279         return RecordHome.findByPrimaryKey( nIdRecord, pluginDirectory );
280     }
281 
282     /**
283      * Get the list of record fields by a filter
284      * @param filter the filter
285      * @return a list of record fields
286      */
287     public List<RecordField> getRecordFields( RecordFieldFilter filter )
288     {
289         Plugin pluginDirectory = PluginService.getPlugin( DirectoryPlugin.PLUGIN_NAME );
290 
291         return RecordFieldHome.getRecordFieldList( filter, pluginDirectory );
292     }
293 
294     /**
295      * this method change zip file status to in progress and modify the date
296      * @param plugin plugin
297      * @param nIdZipBasket id of the zipbasket
298      */
299     private static void changeZipBasketStatusToInProgress( Plugin plugin, int nIdZipBasket )
300     {
301         ZipBasketHome.changeZipBasketStatus( plugin, nIdZipBasket, StatusZipEnum.IN_PROGRESS.getId(  ) );
302     }
303 
304     /**
305      * this method change zip file status to finished and modify the date
306      * @param plugin plugin
307      * @param nIdZipBasket id of the zipbasket
308      * @param strUrl url
309      */
310     private static void changeZipBasketStatusToFinished( Plugin plugin, int nIdZipBasket, String strUrl )
311     {
312         ZipBasketHome.changeZipBasketStatus( plugin, nIdZipBasket, StatusZipEnum.FINISHED.getId(  ) );
313         ZipBasketHome.changeZipBasketUrl( plugin, nIdZipBasket, strUrl );
314     }
315 
316     /**
317      * this method change zip file status to failed and modify the date
318      * @param plugin plugin
319      * @param nIdZipBasket id of the zipbasket
320      */
321     private static void changeZipBasketStatusToFailed( Plugin plugin, int nIdZipBasket )
322     {
323         ZipBasketHome.changeZipBasketStatus( plugin, nIdZipBasket, StatusZipEnum.FAILED.getId(  ) );
324     }
325 
326     /**
327      * modify the status of zipbasket according the information given by archiveClientService
328      */
329     public void updateZipBasketStatus(  )
330     {
331         updateZipBasketStatus( null );
332     }
333 
334     /**
335      * modify the status of zipbasket according the information given by archiveClientService
336      * @param sbLog the log
337      */
338     public void updateZipBasketStatus( StringBuilder sbLog )
339     {
340         Plugin plugin = PluginService.getPlugin( DirectoryPlugin.PLUGIN_NAME );
341         List<ZipBasket> listZipBasket = ZipBasketHome.loadAllZipBasket( plugin );
342 
343         for ( ZipBasket zipBasket : listZipBasket )
344         {
345             if ( StatusZipEnum.IN_PROGRESS.getId(  ).equals( zipBasket.getZipStatus(  ) ) ||
346                     StatusZipEnum.PENDING.getId(  ).equals( zipBasket.getZipStatus(  ) ) )
347             {
348                 log( sbLog,
349                     "\n- Updating zip status '" + zipBasket.getZipName(  ) + " (ID : " + zipBasket.getIdZip(  ) +
350                     ")' " );
351 
352                 String strArchiveStatus = getZipService(  ).getStatutZip( zipBasket.getArchiveItemKey(  ) );
353 
354                 log( sbLog, "\n\tArchive Status : '" + strArchiveStatus + "'" );
355 
356                 if ( ARCHIVE_STATE_USED.equals( strArchiveStatus ) )
357                 {
358                     log( sbLog, "\n\tChanging zip status to '" + StatusZipEnum.IN_PROGRESS + "'" );
359                     changeZipBasketStatusToInProgress( plugin, zipBasket.getIdZip(  ) );
360                 }
361 
362                 if ( ARCHIVE_STATE_ERROR.equals( strArchiveStatus ) )
363                 {
364                     log( sbLog, "\n\tChanging zip status to '" + StatusZipEnum.FAILED + "'" );
365                     changeZipBasketStatusToFailed( plugin, zipBasket.getIdZip(  ) );
366 
367                     String strPathFilesGenerate = FilesUtils.builNamePathBasket( zipBasket.getIdAdminUser(  ),
368                             zipBasket.getIdDirectory(  ) ) + File.separator + zipBasket.getZipName(  );
369                     FilesUtils.cleanTemporyZipDirectory( strPathFilesGenerate );
370                 }
371 
372                 if ( ARCHIVE_STATE_FINAL.equals( strArchiveStatus ) )
373                 {
374                     String strUrl = getZipService(  ).getUrlZip( zipBasket.getArchiveItemKey(  ) );
375 
376                     if ( StringUtils.isNotBlank( strUrl ) )
377                     {
378                         log( sbLog, "\n\tChanging zip status to '" + StatusZipEnum.FINISHED + "'" );
379                         changeZipBasketStatusToFinished( plugin, zipBasket.getIdZip(  ), strUrl );
380 
381                         String strPathFilesGenerate = FilesUtils.builNamePathBasket( zipBasket.getIdAdminUser(  ),
382                                 zipBasket.getIdDirectory(  ) ) + File.separator + zipBasket.getZipName(  );
383                         FilesUtils.cleanTemporyZipDirectory( strPathFilesGenerate );
384                     }
385                     else
386                     {
387                         log( sbLog, "\n\tChanging zip status to '" + StatusZipEnum.FAILED + "'" );
388                         changeZipBasketStatusToFailed( plugin, zipBasket.getIdZip(  ) );
389                     }
390                 }
391             }
392         }
393     }
394 
395     /**
396      * generate a zip file of all of zipbasket
397      * @param strName name of zip
398      * @param nIdAdminUser id of admin user
399      * @param plugin plugin
400      * @param nIdDirectory if of directory
401      */
402     public void exportAllZipFile( String strName, int nIdAdminUser, Plugin plugin, int nIdDirectory )
403     {
404         String strPathZipGenerate = FilesUtils.builNamePathBasket( nIdAdminUser, nIdDirectory );
405 
406         int nARchiveItemKey = getZipService(  )
407                                   .doBasicZip( strPathZipGenerate + File.separator +
408                 AppPropertiesService.getProperty( PROPERTY_ZIP_NAME_REPOSITORY ), strPathZipGenerate,
409                 strName + EXTENSION_FILE_ZIP, ArchiveClientConstants.ARCHIVE_TYPE_ZIP );
410         ZipBasketHome.addZipBasket( strName, nIdAdminUser, plugin, nIdDirectory, -1, nARchiveItemKey );
411     }
412 
413     /**
414      * get all actions for zipbasket by its status
415      * @param nState status of zipbasket
416      * @param locale the locale
417      * @param directory the directory
418      * @param user the user
419      * @param plugin plugin
420      * @return list of actions
421      */
422     public List<ZipBasketAction> selectActionsByZipBasketState( int nState, Locale locale, Directory directory,
423         AdminUser user, Plugin plugin )
424     {
425         List<ZipBasketAction> listActions = ZipBasketActionHome.selectActionsByZipBasketState( nState, locale, plugin );
426 
427         return (List<ZipBasketAction>) RBACService.getAuthorizedActionsCollection( listActions, directory, user );
428     }
429 
430     /**
431      * This SQL method check if the zip is already exists
432      * @param nIdAdminUser id of admin user
433      * @param plugin plugin
434      * @param nIdDirectory id directory
435      * @param nIdRecord id ercord
436      * @return true if the zip already exists
437      */
438     public boolean existsZipBasket( int nIdAdminUser, Plugin plugin, int nIdDirectory, int nIdRecord )
439     {
440         return ZipBasketHome.existsZipBasket( nIdAdminUser, plugin, nIdDirectory, nIdRecord );
441     }
442 
443     /**
444      * Method to get ZipService
445      * @return ZipService
446      */
447     private static ZipService getZipService(  )
448     {
449         return (ZipService) SpringContextService.getPluginBean( DirectoryPDFProducerPlugin.PLUGIN_NAME,
450             "directory-pdfproducer-archive.zipUtils" );
451     }
452 
453     /**
454      * Log the message
455      * @param sbLog the log
456      * @param strMessage the message to log
457      */
458     private void log( StringBuilder sbLog, String strMessage )
459     {
460         if ( sbLog != null )
461         {
462             sbLog.append( strMessage );
463         }
464     }
465 }