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.archive.service.archive;
35  
36  import fr.paris.lutece.plugins.archive.business.ArchiveItem;
37  import fr.paris.lutece.plugins.archive.business.ArchiveItemHome;
38  import fr.paris.lutece.plugins.archive.util.ArchiveConstants;
39  import fr.paris.lutece.plugins.archive.util.ArchiveUtil;
40  import fr.paris.lutece.portal.service.plugin.Plugin;
41  import fr.paris.lutece.portal.service.plugin.PluginService;
42  import fr.paris.lutece.portal.service.util.AppLogService;
43  
44  import java.util.Date;
45  
46  
47  /**
48   * implementation of @see IArchiveService
49   * @author merlinfe
50   *
51   */
52  public class ArchiveService implements IArchiveService
53  {
54      private Plugin _plugin;
55      private IGenerateArchiveServiceFactory _generateArchiveServiceFactory;
56  
57      /**
58       * Constructor
59       */
60      public ArchiveService(  )
61      {
62      }
63  
64      /**
65       * Initialize the Form service
66       * @return the plugin
67       */
68      public Plugin getPlugin(  )
69      {
70          if ( _plugin == null )
71          {
72              _plugin = PluginService.getPlugin( ArchivePlugin.PLUGIN_NAME );
73          }
74  
75          return _plugin;
76      }
77  
78      /**
79       * {@inheritDoc}
80       */
81      public int generateArchive( String strFolderToArchive, String strArchiveDestination, String strArchiveName,
82          String strArchiveType )
83      {
84          int nIdArchiveItem = -1;
85  
86          IGenerateArchiveService generateArchiveService = _generateArchiveServiceFactory.getGenerateArchiveService( strArchiveType );
87  
88          if ( generateArchiveService != null )
89          {
90              ArchiveItem archiveItem = new ArchiveItem(  );
91              archiveItem.setFolderToArchive( strFolderToArchive );
92              archiveItem.setArchiveDestination( strArchiveDestination );
93              archiveItem.setArchiveName( strArchiveName );
94              archiveItem.setArchiveType( strArchiveType );
95              archiveItem.setState( ArchiveConstants.ARCHIVE_STATE_INITIAL );
96              archiveItem.setArchiveMimeType( generateArchiveService.getMimeType(  ) );
97              nIdArchiveItem = ArchiveItemHome.create( archiveItem, getPlugin(  ) );
98              archiveItem.setIdArchiveItem( nIdArchiveItem );
99          }
100 
101         return nIdArchiveItem;
102     }
103 
104     /**
105      * {@inheritDoc}
106      */
107     public String informationArchive( int archiveItemKey )
108     {
109         String strState = ArchiveConstants.ARCHIVE_STATE_INITIAL;
110         ArchiveItem archiveItem = ArchiveItemHome.findByPrimaryKey( archiveItemKey, getPlugin(  ) );
111 
112         if ( archiveItem != null )
113         {
114             strState = archiveItem.getState(  );
115         }
116 
117         return strState;
118     }
119 
120     /**
121      * {@inheritDoc}
122      */
123     public void removeArchive( int archiveItemKey )
124     {
125         ArchiveItem archiveItem = ArchiveItemHome.findByPrimaryKey( archiveItemKey, getPlugin(  ) );
126 
127         if ( archiveItem != null )
128         {
129             try
130             {
131                 ArchiveUtil.deleteFile( archiveItem );
132             }
133             catch ( Exception e )
134             {
135                 AppLogService.error( e );
136             }
137 
138             ArchiveItemHome.delete( archiveItemKey, getPlugin(  ) );
139         }
140     }
141 
142     /**
143      * {@inheritDoc}
144      */
145     public void runGenerateArchive( StringBuilder sbLogs )
146     {
147         ArchiveItem archiveItem = null;
148         StringBuilder newArchiveItem;
149 
150         do
151         {
152             archiveItem = ArchiveItemHome.getNextArchiveItemQueue( getPlugin(  ) );
153 
154             if ( archiveItem != null )
155             {
156                 newArchiveItem = new StringBuilder(  );
157                 newArchiveItem.append( "New Archive Item " );
158                 newArchiveItem.append( ArchiveUtil.getFilePath( archiveItem ) );
159                 AppLogService.info( newArchiveItem.toString(  ) );
160                 sbLogs.append( " Start treatment " );
161                 sbLogs.append( new Date(  ).toString(  ) );
162                 sbLogs.append( newArchiveItem.toString(  ) );
163 
164                 IGenerateArchiveService generateArchiveService = _generateArchiveServiceFactory.getGenerateArchiveService( archiveItem.getArchiveType(  ) );
165 
166                 try
167                 {
168                     ArchiveItemHome.updateState( archiveItem.getIdArchiveItem(  ), ArchiveConstants.ARCHIVE_STATE_USED,
169                         getPlugin(  ) );
170                     generateArchiveService.archiveDirectory( archiveItem.getFolderToArchive(  ),
171                         archiveItem.getArchiveDestination(  ), archiveItem.getArchiveName(  ) );
172 
173                     sbLogs.append( " End treatment " );
174                     sbLogs.append( new Date(  ).toString(  ) );
175                     sbLogs.append( "\r\n" );
176 
177                     ArchiveItemHome.updateState( archiveItem.getIdArchiveItem(  ),
178                         ArchiveConstants.ARCHIVE_STATE_FINAL, getPlugin(  ) );
179                 }
180                 catch ( Exception e )
181                 {
182                     AppLogService.error( e );
183                     sbLogs.append( e );
184                     sbLogs.append( " End treatment " );
185                     sbLogs.append( new Date(  ).toString(  ) );
186                     sbLogs.append( "\r\n" );
187                     ArchiveItemHome.updateState( archiveItem.getIdArchiveItem(  ),
188                         ArchiveConstants.ARCHIVE_STATE_ERROR, getPlugin(  ) );
189                 }
190             }
191         }
192         while ( archiveItem != null );
193     }
194 
195     /**
196      * Setter of generaArchiveServiceFactory
197      * @param generaArchiveServiceFactory The generate archive service factory
198      */
199     public void setGenerateArchiveServiceFactory( IGenerateArchiveServiceFactory generaArchiveServiceFactory )
200     {
201         this._generateArchiveServiceFactory = generaArchiveServiceFactory;
202     }
203 }