View Javadoc
1   /*
2    * Copyright (c) 2002-2013, 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.exportfile.service;
35  
36  import fr.paris.lutece.plugins.directory.business.PhysicalFile;
37  import fr.paris.lutece.plugins.directory.business.PhysicalFileHome;
38  import fr.paris.lutece.plugins.directory.business.Record;
39  import fr.paris.lutece.plugins.directory.modules.exportfile.business.File;
40  import fr.paris.lutece.plugins.directory.modules.exportfile.business.FileHome;
41  import fr.paris.lutece.plugins.directory.modules.exportfile.business.FileName;
42  import fr.paris.lutece.plugins.directory.modules.exportfile.business.FileNameHome;
43  import fr.paris.lutece.plugins.directory.modules.exportfile.business.MappingEntry;
44  import fr.paris.lutece.plugins.directory.modules.exportfile.business.MappingEntryHome;
45  import fr.paris.lutece.plugins.directory.service.DirectoryPlugin;
46  import fr.paris.lutece.portal.service.daemon.Daemon;
47  import fr.paris.lutece.portal.service.plugin.Plugin;
48  import fr.paris.lutece.portal.service.plugin.PluginService;
49  import fr.paris.lutece.portal.service.util.AppLogService;
50  import fr.paris.lutece.portal.service.util.AppPropertiesService;
51  
52  import java.io.ByteArrayInputStream;
53  import java.io.FileNotFoundException;
54  import java.io.FileOutputStream;
55  import java.io.IOException;
56  import java.text.SimpleDateFormat;
57  import java.util.Collection;
58  
59  import org.apache.commons.compress.utils.IOUtils;
60  import org.apache.commons.lang.StringUtils;
61  
62  /**
63   * ExportPVNETDaemon
64   */
65  public class ExportFileDaemon extends Daemon
66  {
67      private DirectoryService _directorySrvc = DirectoryService.getService( );
68      public final Plugin _pluginDirectory = PluginService.getPlugin( DirectoryPlugin.PLUGIN_NAME );
69      private String PROPERTY_IMAGE_TITLE_DATE_FORMAT = AppPropertiesService.getProperty( "module.directory.exportfile.date.format", "YYYY-MM-DD hh:mm:ss" );
70  
71      /**
72       * {@inheritDoc}
73       */
74      @Override
75      public void run( )
76      {
77  
78          Collection<MappingEntry> listMappingEntry = MappingEntryHome.getMappingEntrysList( );
79  
80          for ( MappingEntry ent : listMappingEntry )
81          {
82  
83              if ( ent.getIsActive( ) )
84              {
85                  for ( Record record : _directorySrvc.getListRecord( ent.getIdDirectory( ), ent.getIdEntry( ) ) )
86                  {
87                      try
88                      {
89  
90                          doProcessAction( ent.getIdEntry( ), record.getIdRecord( ), ent.getIdDirectory( ), ent.getPath( ), ent.getId( ) );
91  
92                      }
93                      catch( Exception e )
94                      {
95  
96                          AppLogService.error( "Error de creation du fichier", e );
97                      }
98                  }
99              }
100 
101         }
102 
103     }
104 
105     /**
106      * Create file in directory
107      * 
108      * @param nIdEntry
109      * @param idRecord
110      * @param idDirectory
111      */
112     private void doProcessAction( int nIdEntry, int nIdRecord, int nIdDirectory, String strPath, int nId )
113     {
114         File fileSaved = null;
115         PhysicalFile phFile = null;
116 
117         fr.paris.lutece.plugins.directory.business.File file = _directorySrvc.getRecordFieldValue( nIdEntry, nIdRecord, nIdDirectory );
118         if ( file != null )
119         {
120             fileSaved = FileHome.findFile( file.getIdFile( ), nIdDirectory );
121             phFile = PhysicalFileHome.findByPrimaryKey( file.getPhysicalFile( ).getIdPhysicalFile( ), _pluginDirectory );
122         }
123         if ( file != null && phFile != null && ( fileSaved == null || !fileSaved.getIsCreated( ) ) )
124         {
125 
126             byte [ ] bytes = phFile.getValue( );
127             ByteArrayInputStream in = new ByteArrayInputStream( bytes );
128             FileOutputStream out;
129             String fileName = getFileName( nIdEntry, nIdRecord, nIdDirectory, nId );
130             if ( fileName == null )
131             {
132                 fileName = file.getTitle( );
133             }
134             else
135                 if ( file.getExtension( ) != null )
136                 {
137                     fileName = fileName.concat( "." + file.getExtension( ) );
138                 }
139 
140             try
141             {
142 
143                 out = new FileOutputStream( new java.io.File( strPath + "/" + fileName.trim( ) ) );
144                 IOUtils.copy( in, out );
145                 IOUtils.closeQuietly( in );
146                 IOUtils.closeQuietly( out );
147 
148             }
149             catch( FileNotFoundException e )
150             {
151                 File fileExport = new File( );
152                 fileExport.setIdDirectory( nIdDirectory );
153                 fileExport.setIsCreated( false );
154                 fileExport.setIdFile( file.getIdFile( ) );
155                 AppLogService.error( "Error de creation du fichier", e );
156             }
157             catch( IOException e )
158             {
159                 File fileExport = new File( );
160                 fileExport.setIdDirectory( nIdDirectory );
161                 fileExport.setIsCreated( false );
162                 fileExport.setIdFile( file.getIdFile( ) );
163                 AppLogService.error( "Error de creation du fichier", e );
164             }
165 
166             File fileExport = new File( );
167             fileExport.setIdDirectory( nIdDirectory );
168             fileExport.setIsCreated( true );
169             fileExport.setIdFile( file.getIdFile( ) );
170 
171             FileHome.create( fileExport );
172         }
173     }
174 
175     private String getFileName( int nIdEntry, int nIdRecord, int nIdDirectory, int nId )
176     {
177 
178         Collection<FileName> fileNameList = FileNameHome.getFilesList( nId );
179         String fileName = StringUtils.EMPTY;
180         SimpleDateFormat dt = new SimpleDateFormat( PROPERTY_IMAGE_TITLE_DATE_FORMAT );
181 
182         for ( FileName fname : fileNameList )
183         {
184 
185             String tmpFileName = _directorySrvc.getRecordFieldStringValue( Integer.parseInt( fname.getAttribute( ) ), nIdRecord, nIdDirectory );
186             if ( fname.getAttribute( ).equals( DirectoryService.DATE_CREATION_RECORD_CODE ) )
187             {
188 
189                 fileName = fileName.concat( dt.format( _directorySrvc.getDateCreation( nIdRecord, nIdDirectory ) ) ).concat( "_" );
190 
191             }
192             else
193                 if ( tmpFileName.length( ) > fname.getNumberChar( ) )
194                 {
195 
196                     fileName = fileName.concat( tmpFileName.substring( 0, fname.getNumberChar( ) ).concat( "_" ) );
197 
198                 }
199                 else
200                 {
201 
202                     fileName = fileName.concat( tmpFileName ).concat( "_" );
203 
204                 }
205 
206         }
207         if ( !fileName.isEmpty( ) )
208         {
209             return fileName.substring( 0, fileName.length( ) - 1 );
210 
211         }
212         return null;
213     }
214 }