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.business.export;
35  
36  import fr.paris.lutece.plugins.importexport.business.AbstractImportExportDAO;
37  import fr.paris.lutece.plugins.importexport.business.ColumnType;
38  import fr.paris.lutece.plugins.importexport.business.ImportExportElement;
39  import fr.paris.lutece.plugins.importexport.business.TableColumn;
40  import fr.paris.lutece.portal.service.plugin.Plugin;
41  import fr.paris.lutece.portal.service.util.AppException;
42  import fr.paris.lutece.portal.service.util.AppLogService;
43  import fr.paris.lutece.util.sql.DAOUtil;
44  
45  import java.sql.Date;
46  import java.sql.Timestamp;
47  import java.text.DateFormat;
48  import java.util.ArrayList;
49  import java.util.List;
50  import java.util.Locale;
51  
52  import org.apache.commons.codec.binary.Hex;
53  import org.apache.commons.lang3.StringUtils;
54  
55  /**
56   * DAO to export data from a database table
57   */
58  public class ExportDAO extends AbstractImportExportDAO
59  {
60      private static final String SQL_QUERY_SELECT = " SELECT ";
61      private static final String SQL_QUERY_FROM = " FROM ";
62  
63      private static final String CONSTANT_COMMA = ",";
64  
65      /**
66       * Get the list of rows of a table of the database.
67       * 
68       * @param strTableName
69       *            The name of the table to get data from
70       * @param listColumns
71       *            The list of columns to get
72       * @param plugin
73       *            The plugin to use the pool of
74       * @return The list of rows containing the requested columns of the given table
75       */
76      public List<RowExportData> getDataFromTable( String strTableName, List<String> listColumns, Plugin plugin )
77      {
78          List<TableColumn> listTableColumns = getTableColumns( listColumns, strTableName, plugin, Locale.getDefault( ) );
79          List<RowExportData> listRowExportData = new ArrayList<RowExportData>( );
80          DAOUtil daoUtil = new DAOUtil( getSqlSelect( strTableName, listColumns ), plugin );
81          try
82          {
83              daoUtil.executeQuery( );
84              while ( daoUtil.next( ) )
85              {
86                  int nIndex = 1;
87                  List<ImportExportElement> listElements = new ArrayList<ImportExportElement>( listTableColumns.size( ) );
88                  for ( TableColumn tableColumn : listTableColumns )
89                  {
90                      String strValue = getElementValue( daoUtil, tableColumn.getColumnType( ), nIndex++ );
91                      ImportExportElementort/business/ImportExportElement.html#ImportExportElement">ImportExportElement element = new ImportExportElement( );
92                      element.setColumnName( tableColumn.getColumnName( ) );
93                      element.setValue( strValue );
94                      listElements.add( element );
95                  }
96                  RowExportDataortexport/business/export/RowExportData.html#RowExportData">RowExportData rowData = new RowExportData( listElements );
97                  listRowExportData.add( rowData );
98              }
99          }
100         catch( AppException e )
101         {
102             AppLogService.error( e.getMessage( ), e );
103         }
104         finally
105         {
106             daoUtil.free( );
107         }
108         return listRowExportData;
109     }
110 
111     /**
112      * Get the SQL string to select data from the given table
113      * 
114      * @param strTableName
115      *            The name of the table
116      * @param listColumns
117      *            The list of columns to get
118      * @return the SQL SELECT string
119      */
120     private String getSqlSelect( String strTableName, List<String> listColumns )
121     {
122         StringBuilder sbSqlSelect = new StringBuilder( SQL_QUERY_SELECT );
123         boolean bFirst = true;
124         for ( String strColumn : listColumns )
125         {
126             if ( bFirst )
127             {
128                 bFirst = false;
129             }
130             else
131             {
132                 sbSqlSelect.append( CONSTANT_COMMA );
133             }
134             sbSqlSelect.append( strColumn );
135         }
136         sbSqlSelect.append( SQL_QUERY_FROM );
137         sbSqlSelect.append( strTableName );
138         return sbSqlSelect.toString( );
139     }
140 
141     /**
142      * Get the value of an element from a daoUtil.
143      * 
144      * @param daoUtil
145      *            The daoUtil to get the value from
146      * @param columnType
147      *            The column type of the element to get
148      * @param nIndex
149      *            The index of the element in the DAO.
150      * @return The string representation of the element, or an empty string if the value could not be retrieved
151      */
152     private String getElementValue( DAOUtil daoUtil, ColumnType columnType, int nIndex )
153     {
154         String strResult = StringUtils.EMPTY;
155         switch( columnType )
156         {
157             case TYPE_INT:
158                 int nValue = daoUtil.getInt( nIndex );
159                 strResult = Integer.toString( nValue );
160                 break;
161             case TYPE_STRING:
162                 strResult = daoUtil.getString( nIndex );
163                 break;
164             case TYPE_LONG:
165                 long lValue = daoUtil.getLong( nIndex );
166                 strResult = Long.toString( lValue );
167                 break;
168             case TYPE_TIMESTAMP:
169                 Timestamp timestamp = daoUtil.getTimestamp( nIndex );
170                 if ( timestamp != null )
171                 {
172                     strResult = Long.toString( timestamp.getTime( ) );
173                 }
174                 break;
175             case TYPE_DATE:
176                 Date date = daoUtil.getDate( nIndex );
177                 if ( date != null )
178                 {
179                     strResult = DateFormat.getDateInstance( ).format( date );
180                 }
181                 break;
182             case TYPE_BYTE:
183                 byte [ ] bytesValue = daoUtil.getBytes( nIndex );
184                 if ( bytesValue != null )
185                 {
186                     strResult = String.valueOf( Hex.encodeHex( bytesValue ) );
187                 }
188                 break;
189             case TYPE_DOUBLE:
190                 double dValue = daoUtil.getDouble( nIndex );
191                 strResult = Double.toString( dValue );
192                 break;
193             default:
194                 AppLogService.error( "Error : unknown column type !" );
195         }
196         return strResult;
197     }
198 
199 }