1 /*
2 * Copyright (c) 2002-2017, Mairie de 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.adminquery.business;
35
36 import fr.paris.lutece.portal.service.plugin.Plugin;
37 import fr.paris.lutece.portal.service.util.AppException;
38 import fr.paris.lutece.util.date.DateUtil;
39 import fr.paris.lutece.util.sql.DAOUtil;
40
41 import java.sql.ResultSetMetaData;
42 import java.sql.SQLException;
43
44 import java.util.ArrayList;
45 import java.util.List;
46
47
48 /**
49 * This class provides Data Access methods for AdminQuery DAO objects
50 */
51 public final class AdminQueryDAO implements IAdminqueryDAO
52 {
53 private static final String STR_SQL_ERROR = "SQL Error executing command : ";
54
55 /**
56 * Loads the list ColumnNames
57 * @param strRequest The Request String
58 * @param plugin the Plugin
59 * @return a list of String
60 */
61 public List<String> selectColumnNames( String strRequest, Plugin plugin )
62 {
63 List<String> listNames = new ArrayList<String>( );
64 DAOUtil daoUtil = new DAOUtil( strRequest, plugin );
65 daoUtil.executeQuery( );
66
67 for ( int i = 1; i <= getColumnCount( daoUtil ); i++ )
68 {
69 String columnName = getColumnName( i, daoUtil );
70 listNames.add( columnName );
71 }
72
73 daoUtil.free( );
74
75 return listNames;
76 }
77
78 /**
79 * Return a list (all rows) of list (row of the request's table) of String (cells)
80 * @param strRequest The Request String
81 * @return a List of the Columns' values
82 */
83 public List<List<String>> selectRows( String strRequest, Plugin plugin )
84 {
85 List<List<String>> listRows = new ArrayList<List<String>>( );
86 DAOUtil daoUtil = new DAOUtil( strRequest, plugin );
87 daoUtil.executeQuery( );
88
89 while ( daoUtil.next( ) )
90 {
91 String strValue = null;
92 List<String> listLine = new ArrayList<String>( );
93
94 for ( int i = 1; i <= getColumnCount( daoUtil ); i++ )
95 {
96 switch ( getColumnType( i, daoUtil ) )
97 {
98 case java.sql.Types.CHAR:
99 case java.sql.Types.VARCHAR:
100 case java.sql.Types.LONGVARCHAR:
101 strValue = daoUtil.getString( getColumnName( i, daoUtil ) );
102
103 break;
104
105 case java.sql.Types.INTEGER:
106 case java.sql.Types.BIGINT:
107 case java.sql.Types.SMALLINT:
108 strValue = "" + daoUtil.getInt( getColumnName( i, daoUtil ) );
109
110 break;
111
112 case java.sql.Types.TIMESTAMP:
113 strValue = DateUtil.getDateString( daoUtil.getTimestamp( getColumnName( i, daoUtil ) ) );
114
115 break;
116
117 case java.sql.Types.DATE:
118 strValue = DateUtil.getDateString( daoUtil.getDate( getColumnName( i, daoUtil ) ) );
119
120 break;
121
122 default:
123 break;
124 }
125
126 listLine.add( strValue );
127 }
128
129 listRows.add( listLine );
130 }
131
132 daoUtil.free( );
133
134 return listRows;
135 }
136
137 /**
138 * Returns the number of columns
139 *
140 * @param daoUtil the DAOUtil object
141 * @return int
142 */
143 private int getColumnCount( DAOUtil daoUtil )
144 {
145 try
146 {
147 ResultSetMetaData rsmd = daoUtil.getResultSet( ).getMetaData( );
148
149 return rsmd.getColumnCount( );
150 }
151 catch ( SQLException e )
152 {
153 daoUtil.free( );
154 throw new AppException( STR_SQL_ERROR + e.toString( ) );
155 }
156 }
157
158 /**
159 * Returns the name of the column
160 *
161 * @param int nColumn, the index of the column
162 * @param daoUtil the DAOUtil object
163 * @return String, the name of the column
164 */
165 private String getColumnName( int nColumn, DAOUtil daoUtil )
166 {
167 try
168 {
169 ResultSetMetaData rsmd = daoUtil.getResultSet( ).getMetaData( );
170
171 return rsmd.getColumnName( nColumn );
172 }
173 catch ( SQLException e )
174 {
175 daoUtil.free( );
176 throw new AppException( STR_SQL_ERROR + e.toString( ) );
177 }
178 }
179
180 /**
181 * Returns the type of the column's data
182 *
183 * @param int nColumn, the index of the column
184 * @param daoUtil the DAOUtil object
185 * @return int
186 */
187 private int getColumnType( int nColumn, DAOUtil daoUtil )
188 {
189 try
190 {
191 ResultSetMetaData rsmd = daoUtil.getResultSet( ).getMetaData( );
192
193 return rsmd.getColumnType( nColumn );
194 }
195 catch ( SQLException e )
196 {
197 daoUtil.free( );
198 throw new AppException( STR_SQL_ERROR + e.toString( ) );
199 }
200 }
201 }