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.importexport.service.importdata.daemon;
35
36 import fr.paris.lutece.plugins.importexport.business.importdata.ImportMessage;
37 import fr.paris.lutece.plugins.importexport.business.importdata.ImportResult;
38 import fr.paris.lutece.plugins.importexport.service.importdata.IImportSource;
39 import fr.paris.lutece.plugins.importexport.service.importdata.ImportManager;
40 import fr.paris.lutece.portal.service.daemon.Daemon;
41 import fr.paris.lutece.portal.service.util.AppLogService;
42 import fr.paris.lutece.portal.service.util.AppPropertiesService;
43
44 import java.io.File;
45 import java.util.Locale;
46
47 import org.apache.commons.lang3.StringUtils;
48
49
50
51
52 public class ImportDaemon extends Daemon
53 {
54 private static final String PROPERTY_DATABASE_TABLES = "importexport.database.importableTableNames";
55 private static final String PROPERTY_DAEMON_SOURCE_FOLDER_PATH = "importexport.daemon.importDaemon.sourceFolderPath";
56 private static final String PROPERTY_DAEMON_UPDATE_EXISTING_ROWS = "importexport.daemon.importDaemon.updateExistingRows";
57 private static final String PROPERTY_DAEMON_STOP_ON_ERRORS = "importexport.daemon.importDaemon.stopOnErrors";
58 private static final String PROPERTY_DAEMON_EMPTY_TABLE_BEFORE_IMPORT = "importexport.daemon.importDaemon.emptyTableBeforeImporting";
59
60 private static final String CONSTANT_SLASH = "/";
61 private static final String CONSTANT_SEMICOLON = ";";
62 private static final String CONSTANT_POINT = ".";
63
64
65
66
67 @Override
68 public void run( )
69 {
70 String strSourcePath = AppPropertiesService.getProperty( PROPERTY_DAEMON_SOURCE_FOLDER_PATH );
71 File directory = new File( strSourcePath );
72 if ( directory.isDirectory( ) && directory.canRead( ) )
73 {
74 if ( !strSourcePath.endsWith( CONSTANT_SLASH ) )
75 {
76 strSourcePath = strSourcePath + CONSTANT_SLASH;
77 }
78 String [ ] strFiles = directory.list( );
79 if ( strFiles != null && strFiles.length > 0 )
80 {
81 String strDatabaseTables = AppPropertiesService.getProperty( PROPERTY_DATABASE_TABLES );
82 boolean bUpdateExistingRows = Boolean.parseBoolean( AppPropertiesService.getProperty( PROPERTY_DAEMON_UPDATE_EXISTING_ROWS ) );
83 boolean bStopOnErrors = Boolean.parseBoolean( AppPropertiesService.getProperty( PROPERTY_DAEMON_STOP_ON_ERRORS ) );
84 boolean bEmptyTable = Boolean.parseBoolean( AppPropertiesService.getProperty( PROPERTY_DAEMON_EMPTY_TABLE_BEFORE_IMPORT ) );
85 for ( String strFileName : strFiles )
86 {
87 File file = new File( strSourcePath + strFileName );
88
89 String strTableName;
90 if ( StringUtils.contains( strFileName, CONSTANT_POINT ) )
91 {
92 strTableName = strFileName.substring( 0, strFileName.lastIndexOf( CONSTANT_POINT ) );
93 }
94 else
95 {
96 strTableName = strFileName;
97 }
98 if ( file.exists( ) && !file.isDirectory( ) )
99 {
100 boolean bAuthorizedTable = false;
101 for ( String strDatabaseTable : strDatabaseTables.split( CONSTANT_SEMICOLON ) )
102 {
103 if ( StringUtils.equals( strDatabaseTable, strTableName ) )
104 {
105 bAuthorizedTable = true;
106 break;
107 }
108 }
109 if ( bAuthorizedTable )
110 {
111 IImportSource importSource = ImportManager.getImportSource( file );
112 if ( importSource != null )
113 {
114 ImportResult result = ImportManager.doProcessImport( importSource, strTableName, bUpdateExistingRows, bStopOnErrors,
115 bEmptyTable, null, Locale.getDefault( ) );
116 importSource.close( );
117
118 String strLog = result.getCreatedElements( ) + " element(s) created, " + result.getUpdatedElements( )
119 + " element(s) updated and " + result.getIgnoredElements( ) + " element(s) ignored.";
120 AppLogService.info( "ImportDaemon : " + strLog );
121 setLastRunLogs( strLog );
122 if ( AppLogService.isDebugEnabled( ) && result.getListImportMessage( ) != null )
123 {
124 for ( ImportMessage message : result.getListImportMessage( ) )
125 {
126 AppLogService.debug( message.toString( ) );
127 }
128 }
129 if ( result.getCreatedElements( ) > 0 || result.getUpdatedElements( ) > 0 || result.getIgnoredElements( ) > 0 )
130 {
131 file.delete( );
132 }
133 }
134 }
135 }
136 }
137 }
138 }
139 else
140 {
141 setLastRunLogs( "The specified source folder '" + strSourcePath + "' does not exist or can not be read." );
142 }
143 }
144
145 }