package it.abaco.dla.report; import java.io.FileOutputStream; import java.sql.ResultSet; import java.util.Enumeration; import java.util.Hashtable; import java.util.Vector; import org.apache.log4j.Logger; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.util.CellReference; import it.abaco.dla.report.util.XlsUtil; import it.abaco.dla.tables.DatabaseAccess; import it.abaco.dla.tables.TbBLCentriCosto; import it.abaco.dla.tables.TbBLInvoiceDetails; import it.abaco.dla.tables.TbBLPriceListStandard; import it.abaco.dla.tables.TbBLUsageDetails; import it.abaco.sc.Util; import it.abaco.sc.email.Report; public class BLSintesi_VerReport { static Logger logger = Logger.getLogger(BLSintesi_VerReport.class.getName()); BLSintesi_XlsStyles styles; Hashtable invoiceDetails = new Hashtable(); Hashtable priceStandard = new Hashtable(); Hashtable reqName_row = new Hashtable(); Hashtable intestazione_col = new Hashtable(); public void createReport(String cliente, String userBL, String meseRif, String outPath) { logger.info("Start Report creation..."); // String cliente = "Mediobanca Spa"; // String userBL = "30241168"; // String meseRif = "201510"; try { TbBLUsageDetails details = new TbBLUsageDetails(); String queryBillable = "WHERE utenza = '" + userBL + "' AND meseRiferimento = '" + meseRif + "' " + "UNION " + "SELECT DISTINCT RTRIM(BILLABLE_PRODUCT) + ' Maint.ce' AS BILLABLE_PRODUCT " + "FROM BLUsageDetails " + "WHERE utenza = '" + userBL + "' AND meseRiferimento = '" + meseRif + "' AND CTRB_MAIN = 'Y' " + "ORDER BY BILLABLE_PRODUCT"; logger.info("Individuo i billable product: SELECT DISTINCT BILLABLE_PRODUCT FROM BLUsageDetails " + queryBillable); Vector billableRow = details.retrieveDistinct(queryBillable, "BILLABLE_PRODUCT"); logger.info("Load billable product..."); String queryReq = "WHERE utenza = '" + userBL + "' AND meseRiferimento = '" + meseRif + "' ORDER BY req_name"; logger.info("Individuo le richieste: SELECT DISTINCT req_name FROM BLUsageDetails " + queryReq); Vector reqColl = details.retrieveDistinct(queryReq, "req_name"); logger.info("Load request name..."); TbBLInvoiceDetails tbID = new TbBLInvoiceDetails(); Vector vID = tbID.retrieveAll("WHERE utenza = '" + userBL + "' AND startDate <= '" + meseRif + "01' AND endDate >= '" + meseRif + "01'"); for (int i=0; i vPL = tbPL.retrieveAll(); for (int i=0; i rowIndex = new Hashtable(); Hashtable colIndex = new Hashtable(); /* * POI ha un bug per il quale se delle celle contengono delle formule che si riferiscono ad altri fogli _E_ viene modificato * l'ordine dei fogli, la cella con le formule punta ad altri fogli. * https://bz.apache.org/bugzilla/show_bug.cgi?id=58746 * A questo punto, creo fin da subito i fogli nell'ordine corretto e inserisco successivamente i valori. */ HSSFSheet centriDiCostoSheet = workbook.createSheet("CentriDiCosto_" + userBL + "_" + meseRif); HSSFSheet summarySheet = workbook.createSheet("Summary_" + userBL + "_" + meseRif); HSSFSheet initChargeSheet = workbook.createSheet("InitCharge"); HSSFSheet multiRequestSheet = workbook.createSheet("MultiRequest"); // imposto il foglio MULTIPLE REQUEST logger.info("Creo il foglio 'MultiRequest'"); details_sheetTemplate(multiRequestSheet, billableRow, reqColl, rowIndex, colIndex); String query = "SELECT req_name, BILLABLE_PRODUCT, COUNT(*) AS tot " + "FROM BLUsageDetails " + "WHERE (utenza = '" + userBL + "') AND (meseRiferimento = '" + meseRif + "') AND (INIT_CHARGE = 'N') AND (CTRB_MH = 'Y') " + "GROUP BY req_name, BILLABLE_PRODUCT " + "ORDER BY req_name, BILLABLE_PRODUCT"; details_sheetValues(query, multiRequestSheet, reqColl, rowIndex, colIndex); // Crea il foglio INITIAL CHARGE logger.info("Creo il foglio 'InitCharge'"); details_sheetTemplate (initChargeSheet, billableRow, reqColl, rowIndex, colIndex); query = "SELECT req_name, BILLABLE_PRODUCT, COUNT(*) AS tot " + "FROM BLUsageDetails " + "WHERE (utenza = '" + userBL + "') AND (meseRiferimento = '" + meseRif + "') AND (INIT_CHARGE = 'Y') AND (CTRB_MH = 'N') AND (CTRB_MAIN = 'N') " + "GROUP BY req_name, BILLABLE_PRODUCT " + "UNION " + "SELECT req_name, RTRIM(BILLABLE_PRODUCT) + ' Maint.ce' AS BILLABLE_PRODUCT, COUNT(*) AS tot " + "FROM BLUsageDetails " + "WHERE (utenza = '" + userBL + "') AND (meseRiferimento = '" + meseRif + "') AND (INIT_CHARGE = 'Y') AND (CTRB_MH = 'N') AND (CTRB_MAIN = 'Y') " + "GROUP BY req_name, BILLABLE_PRODUCT " + "ORDER BY req_name, BILLABLE_PRODUCT"; details_sheetValues(query, initChargeSheet, reqColl, rowIndex, colIndex); // Crea il foglio di SINTESI logger.info("Creo il foglio di Sintesi"); summary_sheetTemplate (summarySheet, billableRow, reqColl, cliente, userBL, meseRif); // crea il foglio CENTRI DI COSTO centriDiCosto_template(centriDiCostoSheet, summarySheet, cliente, userBL, meseRif); centriDiCosto_values(centriDiCostoSheet, cliente, userBL, meseRif); // nomeFile[u] = getConfig().getChild("nomeFile").replaceAll("USER", blUser[u]).replaceAll(dateFormat, df.format(dataRiferimento.getTime())); FileOutputStream report = new FileOutputStream(outPath+"/MB_Report_" + userBL + "_" + meseRif + ".xls"); workbook.write(report); report.close(); logger.info("Creazione report terminata con successo!"); } catch (Exception e) { logger.error("Eccezione: ", e); Report.sendReport("ErroreGenerico", "Sintesi Report", "InternalReport", "Eccezione", Util.exceptionToString(e)); } // Invio per email il report // sendEmail(nomeFile, dataRiferimento); } private void details_sheetTemplate(HSSFSheet sheet, Vector billableRow, Vector reqColl, Hashtable rowIndex, Hashtable colIndex) { try { int colIndex_costoUnitario = reqColl.size() + 6; // Crea nuovo foglio nel documento Excel (InitCharge) int r = 0; for (; r reqColl, Hashtable rowIndex, Hashtable colIndex) { try { DatabaseAccess db = new DatabaseAccess(); ResultSet rs = null; boolean isConnected = false; try { isConnected = db.connect(); logger.info("Load " + sheet.getSheetName() + " details: " + query); rs = db.executeQuery(query); logger.info(sheet.getSheetName() + " details loaded..."); Hashtable billebleProduct_valorizzate = new Hashtable(); while (rs.next()) { String colonna = rs.getString("req_name").trim(); String riga = rs.getString("BILLABLE_PRODUCT").trim(); billebleProduct_valorizzate.put(riga, riga); if (rowIndex.get(riga)!=null && colIndex.get(colonna)!= null) XlsUtil.addCell(sheet, rowIndex.get(riga), colIndex.get(colonna), rs.getInt("tot"), styles.formatoRigaTab_int); else logger.info("Verificare: " + riga + "= " + rowIndex.get(riga) + " - " + colonna + "= " + colIndex.get(colonna)); } Enumeration keys = rowIndex.keys(); String billP = null; while (keys.hasMoreElements()) { billP = keys.nextElement(); HSSFCell deltaCell = sheet.getRow(rowIndex.get(billP)).getCell(reqColl.size()+4); double delta = 0; if (deltaCell != null) delta = deltaCell.getNumericCellValue(); if (!billebleProduct_valorizzate.containsKey(billP) && delta == 0) sheet.getRow(rowIndex.get(billP)).setZeroHeight(true); } } catch (Exception e) { logger.error("Eccezione: ", e); Report.sendReport("ErroreGenerico", "Sintesi Report", "InternalReport", "Eccezione", Util.exceptionToString(e)); } finally { if (rs != null) rs.close(); if (isConnected && db != null) db.disconnect(); } } catch (Exception e) { logger.error("Eccezione: ", e); Report.sendReport("ErroreGenerico", "Sintesi Report", "InternalReport", "Eccezione", Util.exceptionToString(e)); } } private void summary_sheetTemplate (HSSFSheet sheet, Vector billableRow, Vector reqColl, String cliente, String user, String meseRif) { try { // Titolo XlsUtil.addMergedCell(sheet, 0, 0, 1, 10, cliente, styles.formatoIntestazioneP); XlsUtil.addMergedCell(sheet, 1, 1, 1, 10, "Analisi del BBG DL Per Security Verification Report", styles.formatoTitolo); logger.info("meseRif "+meseRif); XlsUtil.addMergedCell(sheet, 2, 2, 1, 10, "Account BBG DL nr: " + user + " - " + meseRif.substring(4) + "/" + meseRif.substring(0, 4), styles.formatoTitolo); int rigaInizioTabulato = 4; // Testata principale XlsUtil.addMergedCell(sheet, rigaInizioTabulato, rigaInizioTabulato, 1, 4, "ITEM RICHIESTI", styles.formatoIntestazioneP); XlsUtil.addMergedCell(sheet, rigaInizioTabulato, rigaInizioTabulato, 5, 10, "ATTRIBUZIONE COSTI", styles.formatoIntestazioneP); // Testata secondaria XlsUtil.addMergedCell(sheet, rigaInizioTabulato+1, rigaInizioTabulato+1, 1, 2, "INITIAL", styles.formatoIntestazioneS); XlsUtil.addMergedCell(sheet, rigaInizioTabulato+1, rigaInizioTabulato+1, 3, 4, "MULTI", styles.formatoIntestazioneS); XlsUtil.addMergedCell(sheet, rigaInizioTabulato+1, rigaInizioTabulato+1, 5, 6, "INITIAL", styles.formatoIntestazioneS); XlsUtil.addMergedCell(sheet, rigaInizioTabulato+1, rigaInizioTabulato+1, 7, 8, "MULTI", styles.formatoIntestazioneS); XlsUtil.addMergedCell(sheet, rigaInizioTabulato+1, rigaInizioTabulato+1, 9, 10, "TOTALE", styles.formatoIntestazioneS); // Testata XlsUtil.addCell(sheet, rigaInizioTabulato+2, 1, "Numero", styles.formatoIntestazione); XlsUtil.addCell(sheet, rigaInizioTabulato+2, 2, "% su tot", styles.formatoIntestazione); XlsUtil.addCell(sheet, rigaInizioTabulato+2, 3, "Numero", styles.formatoIntestazione); XlsUtil.addCell(sheet, rigaInizioTabulato+2, 4, "% su tot", styles.formatoIntestazione); sheet.setColumnWidth(5, 15*256); XlsUtil.addCell(sheet, rigaInizioTabulato+2, 5, "Costo ($/mese)", styles.formatoIntestazione); XlsUtil.addCell(sheet, rigaInizioTabulato+2, 6, "% su tot", styles.formatoIntestazione); sheet.setColumnWidth(7, 15*256); XlsUtil.addCell(sheet, rigaInizioTabulato+2, 7, "Costo ($/mese)", styles.formatoIntestazione); XlsUtil.addCell(sheet, rigaInizioTabulato+2, 8, "% su tot", styles.formatoIntestazione); sheet.setColumnWidth(9, 15*256); XlsUtil.addCell(sheet, rigaInizioTabulato+2, 9, "Costo ($/mese)", styles.formatoIntestazione); XlsUtil.addCell(sheet, rigaInizioTabulato+2, 10, "% su tot", styles.formatoIntestazione); // Intestazione di riga sheet.setColumnWidth(0, 20*256); int rowIndex_totale = rigaInizioTabulato + reqColl.size() + 4; for (int r=0; r vCentriCosto = centriCosto.retrieveAll("WHERE userBL = '" + user + "' ORDER BY centroCosto"); XlsUtil.addMergedCell(sheet, 4, 4, c, c+vCentriCosto.size()-1, "Ripatizione % del total billing legata all'usage effettivo", styles.formatoIntestazioneS); for (int i=0; i