View Javadoc
1   /*
2    * Copyright (c) 2002-2021, City of 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.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   * Daemon to import data
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       * {@inheritDoc}
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                      // The file name is the name of the database table plus an extension
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                                 // If we extracted some data from the file, we remove it
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 }