예제 #1
0
  /**
   * Metodo utilzado, luego de montar un Pyck, que debe guardarse en el Excel.
   *
   * @throws Exception
   */
  public void guardarApuesta(CurrentPOddsPortal partidoOP, PartidoPyckioBO partidoPIO, PyckBO pyck)
      throws XLSException {
    File f = new File(PATH_FILE_RENDIMIENTO);
    HSSFWorkbook workbook = null;
    HSSFSheet sheet = null;

    // --------------------->>>>>>>>>>>>>
    // Si el archivo existe, leer el contenido
    if (f.exists()) {

      FileInputStream fileInputStream;
      try {
        fileInputStream = new FileInputStream(PATH_FILE_RENDIMIENTO);
      } catch (FileNotFoundException e) {
        e.printStackTrace();
        throw new XLSException("No fue posible guardar Apuesta. Archivo no existe");
      }

      try {
        workbook = new HSSFWorkbook(fileInputStream);
      } catch (IOException e) {
        e.printStackTrace();
        throw new XLSException();
      }

      // Mover los pycks existentes hacia abajo
      sheet = workbook.getSheet(PESTANA);

      HSSFRow row = sheet.getRow(1);
      HSSFCell celdag2 = row.getCell(convert('g'));

      // Buscar en F2, la cantidad total de Pycks
      Integer cantidadPycks = ((Double) celdag2.getNumericCellValue()).intValue();

      HSSFCell celdaa2 = row.getCell(convert('a'));
      HSSFCell celdab2 = row.getCell(convert('b'));
      HSSFCell celdac2 = row.getCell(convert('c'));

      sheet.shiftRows(4, 4 + cantidadPycks, 1);

      // Agregar el nuevo Pyck
      row = sheet.createRow(4);

      // Agregar el nuevo Pyck
      Object[] datos =
          new Object[] {
            "",
            "",
            pyck.getStake(),
            pyck.getOdds(),
            pyck.getPyck().getAbreviatura(),
            "PDF",
            partidoOP.getFecha(),
            partidoOP.getCountry(),
            partidoOP.getEquipos(),
            partidoPIO.getEquipoLocal() + " - " + partidoPIO.getEquipoVisitante(),
            partidoOP.getLeague(),
            "Soccer",
            new Integer(1)
          };
      llenarCeldas(row, datos);

      cantidadPycks = cantidadPycks + 1;

      double ganancias = 0.0;
      double perdidas = 0.0;
      HSSFRow rowPycks;
      for (int i = 4; i < 4 + cantidadPycks; i++) {
        rowPycks = sheet.getRow(i);

        try {
          perdidas = perdidas + rowPycks.getCell(convert('a')).getNumericCellValue();
          ganancias = ganancias + rowPycks.getCell(convert('b')).getNumericCellValue();
        } catch (java.lang.IllegalStateException ex) {
          perdidas = perdidas + 0.0;
          ganancias = ganancias + 0.0;
        }
      }

      celdag2.setCellValue(cantidadPycks);
      celdaa2.setCellValue(perdidas);
      celdab2.setCellValue(ganancias);
      celdac2.setCellValue(ganancias + perdidas);

    } else {
      // --------------------->>>>>>>>>>>>>
      // Si el archivo no existe, crearlo
      workbook = new HSSFWorkbook();
      sheet = workbook.createSheet(PESTANA);

      int numberRow = 0;
      Row row = sheet.createRow(numberRow++);

      Object[] datos =
          new Object[] {
            "Perdidas",
            "Ganancias",
            "Total",
            null,
            null,
            null,
            "TotalPycks",
            null,
            "Estado ::",
            "(PDF = PorDefinir;",
            " FIN = Finalizado;",
            " SUS = Suspendido)"
          };

      llenarCeldas(row, datos);

      row = sheet.createRow(numberRow++);

      // Se agregan las formulas para el calculo del rendimiento
      Cell cellA = row.createCell(convert('a'));
      cellA.setCellValue(0.0);

      Cell cellB = row.createCell(convert('b'));
      cellB.setCellValue(0.0);

      Cell cellC = row.createCell(convert('c'));
      cellC.setCellValue(0.0);

      Cell cellG = row.createCell(convert('g'));
      cellG.setCellValue(1.0);

      // Dejar linea en Blanco
      row = sheet.createRow(numberRow++);

      // Agregar linea para titulos
      row = sheet.createRow(numberRow++);

      datos =
          new Object[] {
            "Lose", "Win", "Stake", "Odds",
            "Pick", "Estado", "Date", "Country",
            "MatchOP", "MatchPIO", "Liga", "Sport"
          };

      llenarCeldas(row, datos);

      // Agregar el nuevo Pyck
      row = sheet.createRow(numberRow++);
      datos =
          new Object[] {
            "",
            "",
            pyck.getStake(),
            pyck.getOdds(),
            pyck.getPyck().getAbreviatura(),
            "PDF",
            partidoOP.getFecha(),
            partidoOP.getCountry(),
            partidoOP.getEquipos(),
            partidoPIO.getEquipoLocal() + " - " + partidoPIO.getEquipoVisitante(),
            partidoOP.getLeague(),
            "Soccer",
            new Integer(1)
          };
      llenarCeldas(row, datos);
    } // fin if - else Archivo Existe

    // Pone cada Columna AutoSize
    for (int i = 1; i <= 12; i++) {
      sheet.autoSizeColumn(i);
    }

    agregarStaticsObjs(pyck);

    FileOutputStream out;
    try {
      out = new FileOutputStream(new File(PATH_FILE_RENDIMIENTO));
      workbook.write(out);
      out.close();
      System.out.println("Excel written successfully..");

    } catch (FileNotFoundException e) {
      e.printStackTrace();
      throw new XLSException();
    } catch (IOException e) {
      e.printStackTrace();
      throw new XLSException();
    }
  }