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;
35  
36  import fr.paris.lutece.plugins.importexport.business.importdata.ImportResult;
37  import fr.paris.lutece.portal.service.plugin.Plugin;
38  import fr.paris.lutece.portal.service.util.AppLogService;
39  
40  import java.util.Locale;
41  
42  /**
43   * Service that allow to import data. This service implements the Runnable interface.
44   */
45  public class RunnableImportService implements Runnable
46  {
47      public static final int STATUS_QUEUED = 0;
48      public static final int STATUS_WORKING = 1;
49      public static final int STATUS_FINISHED = 2;
50  
51      private IImportSource _importSource;
52      private String _strTableName;
53      private Plugin _plugin;
54      private Locale _locale;
55      private boolean _bUpdateExistingRows;
56      private boolean _bStopOnErrors;
57      private boolean _bEmptyTable;
58      private int _nStatus = STATUS_QUEUED;
59      private volatile ImportResult _importResult;
60  
61      /**
62       * Service to asynchronously import data
63       * 
64       * @param importSource
65       *            The data source to get data from
66       * @param strTableName
67       *            The name of the table
68       * @param plugin
69       *            The plugin associated with the pool the table of the database is in.
70       * @param locale
71       *            The locale to display messages in
72       * @param bUpdateExistingRows
73       *            Indicates whether existing rows should be updated (true) or ignored (false)
74       * @param bStopOnErrors
75       *            True to stop when an error occurred, false to skip the item and continue
76       * @param bEmptyTable
77       *            True to empty the table before importing data, false otherwise
78       */
79      public RunnableImportService( IImportSource importSource, String strTableName, Plugin plugin, Locale locale, boolean bUpdateExistingRows,
80              boolean bStopOnErrors, boolean bEmptyTable )
81      {
82          this._importSource = importSource;
83          this._strTableName = strTableName;
84          this._plugin = plugin;
85          this._bUpdateExistingRows = bUpdateExistingRows;
86          this._bStopOnErrors = bStopOnErrors;
87          this._bEmptyTable = bEmptyTable;
88          this._locale = locale;
89      }
90  
91      /**
92       * {@inheritDoc}
93       */
94      @Override
95      public void run( )
96      {
97          try
98          {
99              _nStatus = STATUS_WORKING;
100             _importResult = ImportManager.doProcessImport( _importSource, _strTableName, _bUpdateExistingRows, _bStopOnErrors, _bEmptyTable, _plugin, _locale );
101         }
102         catch( Exception e )
103         {
104             AppLogService.error( e.getMessage( ), e );
105         }
106         finally
107         {
108             _nStatus = STATUS_FINISHED;
109         }
110     }
111 
112     /**
113      * Get the status of the service.
114      * 
115      * @return {@link #STATUS_QUEUED} if the serviced has not been started, {@link #STATUS_WORKING} if it is executing, or {@link #STATUS_FINISHED} if its
116      *         execution has ended.
117      */
118     public int getServiceStatus( )
119     {
120         return _nStatus;
121     }
122 
123     /**
124      * Get the result of the importation of this service
125      * 
126      * @return The result of the importation of this service, or null if the import is not complete or if an error occurred
127      */
128     public ImportResult getImportResult( )
129     {
130         return _importResult;
131     }
132 }