Esempio n. 1
0
  /** @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */
  protected void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    Logger logger = GenericUtilities.checkLoggedIn(request, response, false);
    String todo = request.getParameter("todo");
    if (todo != null && todo.equals("printPDF")) {
      // --- stampa PDF ---
      Document doc = new Document();
      File file = new File("filePDF_" + logger.getUser().getId() + ".pdf");
      FileOutputStream os = new FileOutputStream(file);
      String str = file.getAbsolutePath();
      Boolean printTeams = request.getParameter("hire") != null;
      Boolean printMatches = request.getParameter("match") != null;
      Boolean printChampResults = request.getParameter("champ") != null;
      try {

        // campionati a cui l'utente partecipa
        List<ChampionshipEntity> cl =
            MySQLConnection.getChampionshipOfUser(logger.getUser().getId());
        // apri il documento PDF
        PdfWriter.getInstance(doc, os);
        doc.open();
        doc.add(
            new Phrase(
                new Chunk(
                    "Fantaservlet: utente " + logger.getUser().getName(),
                    FontFactory.getFont(FontFactory.HELVETICA, 20, Font.BOLD))));
        // per ogni campionato
        for (Iterator<ChampionshipEntity> it = cl.iterator(); it.hasNext(); ) {
          ChampionshipEntity champ = it.next();
          printChampTitle(champ, doc);

          // --- stampa le rose di calciatori ---
          if (printTeams) {
            // prendi la lista delle squadre
            List<TeamEntity> teams = MySQLConnection.getTeamsOfChampionship(champ.getId());
            for (Iterator<TeamEntity> it2 = teams.iterator(); it2.hasNext(); ) {
              TeamEntity team = it2.next();
              printHiring(
                  GenericUtilities.getPlayersListByRule(
                      MySQLConnection.getHiring(team.getId()), 'D'),
                  GenericUtilities.getPlayersListByRule(
                      MySQLConnection.getHiring(team.getId()), 'C'),
                  GenericUtilities.getPlayersListByRule(
                      MySQLConnection.getHiring(team.getId()), 'A'),
                  GenericUtilities.getPlayersListByRule(
                      MySQLConnection.getHiring(team.getId()), 'P'),
                  team,
                  doc);
            }
          }

          if (printMatches || printChampResults) {
            // prendi la lista degli scontri
            List<Match> lm = GenericUtilities.getListOfMatches(champ.getId());
            if (lm.size() > 0) {
              // --- stampa le partite del campionato ---
              if (printMatches) {
                printMatches(lm, GenericUtilities.getNumOfTeams(lm), doc);
              }
              // --- stampa la classifica ---
              if (printChampResults) {
                printRanking(GenericUtilities.getRanking(lm), GenericUtilities.isConcluse(lm), doc);
              }
            }
          }
        }
      } catch (DocumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      } catch (SQLException sqle) {
        // TODO errore SQL

      } finally {
        // chiudi documento PDF
        doc.close();
      }
      // stampa il pdf
      doDownload(request, response, str, "filepdf");
    } else {
      // --- stamp pagina e form ---
      response.setContentType("text/html");
      PrintWriter out = response.getWriter();
      out.println(Style.pageHeader(TITLE));

      out.println("<form action=\"PrintPDF\" method=\"POST\">");
      out.println("<input type=\"checkbox\"/ name=\"hire\" checked>Dati squadre<br/>");
      out.println("<input type=\"checkbox\" name=\"match\"/ checked>Risultati partite<br/>");
      out.println(
          "<input type=\"checkbox\" name=\"champ\"/ checked>Classifiche dei campionati<br/><br/>");
      out.println(Style.hidden("todo", "printPDF"));
      out.println("<input type=\"submit\"/>");
      out.println("</form>");
      out.println(Style.pageFooter());
    }
  }
Esempio n. 2
0
  /**
   * metodo di stampa dei risultati delle partite di un campionato su pdf
   *
   * @param matches lista delle partite del campionato
   * @param numTeams numero delle squadre che vi partecipano
   * @param doc documento pdf su cui stampare
   * @throws DocumentException sollevata quando si verificano errori di stampa su pdf
   */
  private void printMatches(List<Match> matches, Integer numTeams, Document doc)
      throws DocumentException {
    // numero delle partite
    Integer matchPerDay = numTeams / 2;
    // tabella pdf
    PdfPTable table = new PdfPTable(7);
    // cella pdf
    PdfPCell cell;
    table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
    table.getDefaultCell().setVerticalAlignment(Element.ALIGN_MIDDLE);
    table.setWidthPercentage(100);
    table.setWidths(new float[] {1, 2, 2, 0.5f, 0.5f, 0.5f, 0.5f});
    // stampa i titoli della tabella
    cell = new PdfPCell(new Phrase("Giornata"));
    cell.setHorizontalAlignment(Element.ALIGN_CENTER);
    table.addCell(cell);
    cell.setPhrase(new Phrase("Partita"));
    cell.setColspan(2);
    table.addCell(cell);
    cell.setPhrase(new Phrase("Risultato"));
    table.addCell(cell);
    cell.setPhrase(new Phrase("Punti"));
    table.addCell(cell);

    Integer counter = 0;
    // per ogni partita
    for (Iterator<Match> it = matches.iterator();
        it.hasNext();
        counter = (counter + 1) % matchPerDay) {
      Match match = it.next();
      if (counter == 0) {
        // inserisci la data della giornata
        cell = new PdfPCell(new Phrase(match.getDay().getFormatDate()));
        cell.setRowspan(matchPerDay);
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);

        table.addCell(cell);
      }
      // stampa i nomi delle squadre
      table.addCell(match.getTeam1().getName());
      table.addCell(match.getTeam2().getName());

      // stampa i gol segnati
      if (match.getDay().isEvaluated()) {
        table.addCell(GenericUtilities.pointsToGol(match.getPointsTeam1()).toString());
        table.addCell(GenericUtilities.pointsToGol(match.getPointsTeam2()).toString());
        table.addCell(match.getPointsTeam1().toString());
        table.addCell(match.getPointsTeam2().toString());
      } else {
        table.addCell("NA");
        table.addCell("NA");
        table.addCell("NA");
        table.addCell("NA");
      }
    }

    // inserisci il titolo nel documento
    doc.add(
        new Phrase(
            new Chunk(
                "\nRisultati delle partite",
                FontFactory.getFont(FontFactory.HELVETICA, 16, Font.BOLD))));
    // inserisci la tabella nel documento
    doc.add(table);
  }