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.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
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
67
68
69
70
71
72
73
74
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
113
114
115
116
117
118
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
143
144
145
146
147
148
149
150
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 }