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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
| package com.dotbase.jasper.model.peer;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import com.dotbase.jasper.configuration.Configuration;
import com.dotbase.jasper.model.Database;
import com.dotbase.jasper.model.Report;
import com.dotbase.jasper.model.ReportParameter;
/**
* Classe d'accès aux données de la base.
* @author lrobert
*
*/
public class ReportsPeer
{
public static final String REPORT_TABLE = Configuration.getParameter( "database.report.tablename" );
public static final String REPORT_ID_FIELD = Configuration.getParameter( "database.report.fieldname.id" );
public static final String REPORT_PATH_FIELD = Configuration.getParameter( "database.report.fieldname.path" );
public static final String REPORT_PDFPATH_FIELD = Configuration.getParameter( "database.report.fieldname.pdfpath" );
public static final String REPORT_CONSERVER_FIELD = Configuration.getParameter( "database.report.fieldname.conserver" );
public static final String REPORT_DATECREATION_FIELD = Configuration.getParameter( "database.report.fieldname.creationdate" );
public static final String REPORT_DATEGENERATION_FIELD = Configuration.getParameter( "database.report.fieldname.generationdate" );
public static final String REPORT_GENERATIONSTATUS_FIELD = Configuration.getParameter( "database.report.fieldname.generationstatus" );
public static final String REPORT_GENERATIONERROR_FIELD = Configuration.getParameter( "database.report.fieldname.generationerror" );
public static final String REPORT_PRET_POUR_GENERATION = Configuration.getParameter( "database.report.fieldname.generationready" );
public static Report getReportById( int printId ) throws SQLException
{
//construire la requête
String select = "SELECT *";
String from = "FROM " +ReportsPeer.REPORT_TABLE;
String where = "WHERE " +REPORT_TABLE+ "." +REPORT_ID_FIELD+ " = ?";
String sql = select+ " " +from+ " " +where;
PreparedStatement statement = Database.getConnection().prepareStatement( sql );
statement.setInt(1, printId);
//exécuter la requête
ResultSet rs = Database.executeSelect( statement );
if ( !rs.next() )
{
return null;
}
//créer l'objet Report
Report report = ReportsPeer.hydrate(rs.getInt(REPORT_ID_FIELD), rs.getString(REPORT_PATH_FIELD), rs.getString(REPORT_PDFPATH_FIELD), rs.getBoolean(REPORT_CONSERVER_FIELD), rs.getDate(REPORT_DATEGENERATION_FIELD), rs.getDate(REPORT_DATECREATION_FIELD), rs.getString(REPORT_GENERATIONSTATUS_FIELD), rs.getString(REPORT_GENERATIONERROR_FIELD), rs.getBoolean(REPORT_PRET_POUR_GENERATION) );
return report;
}
/**
* Enregistre le rapport.
* Si le rapport n'a pas d'id, fait un insert. Sinon, fait un update.
* @param report
* @return
*/
public static Report saveReport( Report report ) throws SQLException
{
boolean doUpdate = false; //pour savoir si on doit faire un insert ou un update
if ( report.getId() > 0 )
doUpdate = true;
String sql = "";
PreparedStatement statement = null;
//déterminer si il faut faire un update ou un insert
if ( doUpdate ) //UPDATE
{
String update = "update " +REPORT_TABLE;
String set = "SET ";
set += REPORT_PATH_FIELD+ " = ?";
set += ", " +REPORT_PDFPATH_FIELD+ " = ?";
set += ", " +REPORT_CONSERVER_FIELD+ " = ?";
set += ", " +REPORT_DATEGENERATION_FIELD+ " = ?";
set += ", " +REPORT_DATECREATION_FIELD+ " = ?";
set += ", " +REPORT_GENERATIONSTATUS_FIELD+ " = ?";
set += ", " +REPORT_GENERATIONERROR_FIELD+ " = ?";
String where = "WHERE " +REPORT_ID_FIELD+ " = ?";
sql = update+ " " +set+ " " +where;
statement = Database.getConnection().prepareStatement( sql );
}
else //INSERT
{
String insert = "INSERT INTO " +REPORT_TABLE;
insert += " (";
insert += REPORT_PATH_FIELD;
insert += ", " +REPORT_PDFPATH_FIELD;
insert += ", " +REPORT_CONSERVER_FIELD;
insert += ", " +REPORT_DATEGENERATION_FIELD;
insert += ", " +REPORT_DATECREATION_FIELD;
insert += ", " +REPORT_GENERATIONSTATUS_FIELD;
insert += ", " +REPORT_GENERATIONERROR_FIELD;
insert += ")";
String values = "VALUES (?, ?, ?, ?, ?, ?, ?)";
sql = insert+ " " +values;
statement = Database.getConnection().prepareStatement( sql );
}
statement.setString(1, report.getFilePath());
statement.setString(2, report.getPdfFilePath());
statement.setBoolean(3, report.isConserver());
if ( report.getDateGeneration() != null )
statement.setDate( 4, new java.sql.Date(report.getDateGeneration().getTime()) );
else
statement.setDate( 4, null );
if ( report.getDateCreation() != null )
statement.setDate( 5, new java.sql.Date(report.getDateCreation().getTime()) );
else
statement.setDate( 5, null );
statement.setString(6, report.getReportStatus());
statement.setString(7, report.getGenerationError());
if ( doUpdate ) statement.setInt(8, report.getId());
//FIXME il faut récupérer l'id nouvellement inséré. Comment faire ? Risque de conflit (accès concurents). Pour l'instant on ne récupère pas le nouvel id, de toute façon on n'est pas sensé faire des inserts depuis ici...
//exécuter la requête
//TODO faire qqch pour vérifier si le nombre lignes modifiées semble correct
int affectedRows = Database.executeUpdate( statement );
//XXX prov
if ( !doUpdate ) //INSERT
{
//récupérer le nouvel ID
PreparedStatement st = Database.getConnection().prepareStatement( "SELECT MAX(print_id) FROM t_print" );
ResultSet rs = st.executeQuery();
while( rs.next() )
{
report.setId(rs.getInt(1));
}
}
//traiter aussi les objets associés
//commencer par supprimer dans la base tous les paramètres du rapport
ReportsPeer.removeAllParametersFromDB(report);
//enregistrer les paramètres
for (ReportParameter param : report.getParameters())
{
ReportsParamsPeer.saveReportParameter( param );
}
return report;
}
/**
* Efface de la base de données tous les paramètres du rapport.
* @param report
* @return
* @throws SQLException
*/
public static void removeAllParametersFromDB( Report report ) throws SQLException
{
//construire la requête
String sql = "DELETE FROM " +ReportsParamsPeer.REPORT_PARAM_TABLE+ " WHERE " +ReportsParamsPeer.REPORT_PARAM_PRINTID_FIELD+ " = ?";
PreparedStatement statement = Database.getConnection().prepareStatement(sql);
statement.setInt(1, report.getId());
//exécuter la requête
Database.executeUpdate(statement);
//supprimer les id dans les paramètres
for (ReportParameter param : report.getParameters())
{
param.setId(-1);
}
}
/**
* récupère tous les enregistrements de la table t_print
* @return
*/
public static Collection<Report> getAllReports() throws SQLException
{
//construire la requête
String select = "SELECT *";
String from = "FROM " +ReportsPeer.REPORT_TABLE;
String sql = select+ " " +from;
PreparedStatement statement = Database.getConnection().prepareStatement( sql );
//exécuter la requête
ResultSet rs = Database.executeSelect( statement );
//créer la collection
ArrayList<Report> reports = new ArrayList<Report>();
//remplir la collection
while( rs.next() )
{
Report report = ReportsPeer.hydrate(rs.getInt(REPORT_ID_FIELD), rs.getString(REPORT_PATH_FIELD), rs.getString(REPORT_PDFPATH_FIELD), rs.getBoolean(REPORT_CONSERVER_FIELD), rs.getDate(REPORT_DATEGENERATION_FIELD), rs.getDate(REPORT_DATECREATION_FIELD), rs.getString(REPORT_GENERATIONSTATUS_FIELD), rs.getString(REPORT_GENERATIONERROR_FIELD), rs.getBoolean(REPORT_PRET_POUR_GENERATION) );
reports.add( report );
}
return reports;
}
/**
* @return tous les rapports dont la print_generation_date est nulle
*/
public static Collection<Report> getNotYetCreatedReports() throws SQLException
{
//construire la requête
String select = "SELECT *";
String from = "FROM " +ReportsPeer.REPORT_TABLE;
String where = "WHERE " +REPORT_DATEGENERATION_FIELD+ " is null";
String sql = select+ " " +from+ " " +where;
PreparedStatement statement = Database.getConnection().prepareStatement( sql );
//exécuter la requête
ResultSet rs = Database.executeSelect( statement );
//créer la collection
ArrayList<Report> reports = new ArrayList<Report>();
//remplir la collection
while( rs.next() )
{
Report report = ReportsPeer.hydrate(rs.getInt(REPORT_ID_FIELD), rs.getString(REPORT_PATH_FIELD), rs.getString(REPORT_PDFPATH_FIELD), rs.getBoolean(REPORT_CONSERVER_FIELD), rs.getDate(REPORT_DATEGENERATION_FIELD), rs.getDate(REPORT_DATECREATION_FIELD), rs.getString(REPORT_GENERATIONSTATUS_FIELD), rs.getString(REPORT_GENERATIONERROR_FIELD), rs.getBoolean(REPORT_PRET_POUR_GENERATION) );
reports.add( report );
}
return reports;
}
/**
* Rapports à générer
* @return tous les rapports dont la print_generation_date est nulle et dont pret_pour_generation est vrai
*/
public static Collection<Report> getPendingReports() throws SQLException
{
//construire la requête
String select = "SELECT *";
String from = "FROM " +ReportsPeer.REPORT_TABLE;
String where = "WHERE ";
where += REPORT_DATEGENERATION_FIELD+ " is null "; //pas de generation_date
where += "AND" +REPORT_PRET_POUR_GENERATION+ " = true"; //pret_pour_generation = true
String sql = select+ " " +from+ " " +where;
PreparedStatement statement = Database.getConnection().prepareStatement( sql );
//exécuter la requête
ResultSet rs = Database.executeSelect( statement );
//créer la collection
ArrayList<Report> reports = new ArrayList<Report>();
//remplir la collection
while( rs.next() )
{
Report report = ReportsPeer.hydrate(rs.getInt(REPORT_ID_FIELD), rs.getString(REPORT_PATH_FIELD), rs.getString(REPORT_PDFPATH_FIELD), rs.getBoolean(REPORT_CONSERVER_FIELD), rs.getDate(REPORT_DATEGENERATION_FIELD), rs.getDate(REPORT_DATECREATION_FIELD), rs.getString(REPORT_GENERATIONSTATUS_FIELD), rs.getString(REPORT_GENERATIONERROR_FIELD), rs.getBoolean(REPORT_PRET_POUR_GENERATION) );
reports.add( report );
}
return reports;
}
/**
* @return tous les rapports dont print_generation_status est "KO"
*/
public static Collection<Report> getFailedReports()
{
return null;
}
private static Report hydrate( int id, String path, String pdfPath, boolean conserver, Date dateGeneration, Date dateCreation, String reportStatus, String generationError, boolean pretPourGeneration ) throws SQLException
{
Report report = new Report();
report.setId( id );
report.setFilePath( path );
report.setPdfFilePath( pdfPath );
report.setDateGeneration(dateGeneration);
report.setDateCreation(dateCreation);
report.setConserver(conserver);
report.setReportStatus( reportStatus );
report.setGenerationError( generationError );
report.setPretPourGeneration( pretPourGeneration );
report.setParameters( ReportsParamsPeer.getParametersForReport(report) );
return report;
}
}
class ReportsParamsPeer
{
public static final String REPORT_PARAM_TABLE = Configuration.getParameter( "database.reportparam.tablename" );
public static final String REPORT_PARAM_ID_FIELD = Configuration.getParameter( "database.reportparam.fieldname.id" );
public static final String REPORT_PARAM_PRINTID_FIELD = Configuration.getParameter( "database.reportparam.fieldname.printid" );
public static final String REPORT_PARAM_NOM_FIELD = Configuration.getParameter( "database.reportparam.fieldname.nom" );
public static final String REPORT_PARAM_TYPE_FIELD = Configuration.getParameter( "database.reportparam.fieldname.type" );
public static final String REPORT_PARAM_VALEUR_FIELD = Configuration.getParameter( "database.reportparam.fieldname.valeur" );
public static final String REPORT_PARAM_POUR_JASPER = Configuration.getParameter( "database.reportparam.fieldname.pourjasper" );
public static final String REPORT_PARAM_NUM_FIELD = Configuration.getParameter( "database.reportparam.fieldname.num" );
/**
* Retourne les paramètres du rapport en ignorant ceux qui ne sont pas pour Jasper (print_param_pour_jasper = false)
* @param report
* @return
* @throws SQLException
*/
public static Collection<ReportParameter> getParametersForReport( Report report ) throws SQLException
{
//construire la requête
String select = "SELECT *";
String from = "FROM " +ReportsParamsPeer.REPORT_PARAM_TABLE;
String where = "WHERE ";
where += ReportsParamsPeer.REPORT_PARAM_TABLE+ "." +ReportsParamsPeer.REPORT_PARAM_PRINTID_FIELD+ " = ? ";
//where += "AND " +ReportsParamsPeer.REPORT_PARAM_TABLE+ "." +ReportsParamsPeer.REPORT_PARAM_POUR_JASPER+ " = true";
String sql = select+ " " +from+ " " +where;
PreparedStatement statement = Database.getConnection().prepareStatement( sql );
statement.setInt(1, report.getId());
//exécuter la requête
ResultSet resultSet = Database.executeSelect( statement );
//créer la collection
ArrayList<ReportParameter> parameters = new ArrayList<ReportParameter>();
//remplir la collection
while( resultSet.next() )
{
ReportParameter reportParameter = ReportsParamsPeer.hydrate(report, resultSet.getInt(ReportsParamsPeer.REPORT_PARAM_ID_FIELD), resultSet.getString(ReportsParamsPeer.REPORT_PARAM_NOM_FIELD), resultSet.getString(ReportsParamsPeer.REPORT_PARAM_TYPE_FIELD), resultSet.getString(ReportsParamsPeer.REPORT_PARAM_VALEUR_FIELD), resultSet.getBoolean(ReportsParamsPeer.REPORT_PARAM_POUR_JASPER), resultSet.getInt(ReportsParamsPeer.REPORT_PARAM_NUM_FIELD) );
parameters.add( reportParameter );
}
return parameters;
}
public static ReportParameter saveReportParameter( ReportParameter reportParameter ) throws SQLException
{
boolean doUpdate = false; //pour savoir si on doit faire un insert ou un update
if ( reportParameter.getId() > 0 )
doUpdate = true;
String sql = "";
PreparedStatement statement = null;
//déterminer si il faut faire un update ou un insert
if ( doUpdate ) //UPDATE
{
String update = "update " +REPORT_PARAM_TABLE;
String set = "SET ";
set += REPORT_PARAM_PRINTID_FIELD+ " = ?";
set += ", " +REPORT_PARAM_NOM_FIELD+ " = ?";
set += ", " +REPORT_PARAM_TYPE_FIELD+ " = ?";
set += ", " +REPORT_PARAM_VALEUR_FIELD+ " = ?";
set += ", " +REPORT_PARAM_POUR_JASPER+ " = ?";
set += ", " +REPORT_PARAM_NUM_FIELD+ " = ?";
String where = "WHERE " +REPORT_PARAM_ID_FIELD+ " = ?";
sql = update+ " " +set+ " " +where;
statement = Database.getConnection().prepareStatement( sql );
}
else //INSERT
{
String insert = "INSERT INTO " +REPORT_PARAM_TABLE;
insert += " (";
insert += REPORT_PARAM_PRINTID_FIELD;
insert += ", " +REPORT_PARAM_NOM_FIELD;
insert += ", " +REPORT_PARAM_TYPE_FIELD;
insert += ", " +REPORT_PARAM_VALEUR_FIELD;
insert += ", " +REPORT_PARAM_POUR_JASPER;
insert += ", " +REPORT_PARAM_NUM_FIELD;
insert += ")";
String values = "VALUES (?, ?, ?, ?, ?, ?)";
sql = insert+ " " +values;
statement = Database.getConnection().prepareStatement( sql );
}
statement.setInt(1, reportParameter.getReport().getId());
statement.setString(2, reportParameter.getNom());
statement.setString(3, reportParameter.getType());
statement.setString(4, reportParameter.getValeur());
statement.setBoolean(5, reportParameter.isPourJasper());
statement.setInt(6, reportParameter.getNum());
if ( doUpdate ) statement.setInt(7, reportParameter.getId());
//FIXME il faut récupérer l'id nouvellement inséré. Comment faire ? Risque de conflit (accès concurents). Pour l'instant on ne récupère pas le nouvel id, de toute façon on n'est pas sensé faire des inserts depuis ici...
//exécuter la requête
//TODO faire qqch pour vérifier si le nombre lignes modifiées semble correct
int affectedRows = Database.executeUpdate( statement );
//XXX prov
if ( !doUpdate ) //INSERT
{
//récupérer le nouvel ID
PreparedStatement st = Database.getConnection().prepareStatement( "SELECT MAX(print_id) FROM t_print" );
ResultSet rs = st.executeQuery();
while( rs.next() )
{
reportParameter.setId(rs.getInt(1));
}
}
return reportParameter;
}
private static ReportParameter hydrate( Report report, int id, String nom, String type, String valeur, boolean pourJasper, int num )
{
//créer l'objet
ReportParameter reportParam = new ReportParameter();
reportParam.setId( id );
reportParam.setReport( report );
reportParam.setNom(nom);
reportParam.setType(type);
reportParam.setValeur(valeur);
reportParam.setPourJasper(pourJasper);
reportParam.setNum(num);
return reportParam;
}
/*public static void deleteReportParameter( ReportParameter reportParameter )
{
//Vérifier que la paramètre a bien un id
if ( !(reportParameter.getId() > 0) )
return;
//construire la requête
String delete = "DELETE FROM " +REPORT_PARAM_TABLE+ " WHERE " +
}*/
} |
Partager