1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
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
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
107
108
109
110
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 }