/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package Physique; import Metier.Categorie; import Metier.Livre; import com.itextpdf.text.Element; import com.itextpdf.text.Font; import com.itextpdf.text.Paragraph; import com.itextpdf.text.Phrase; import com.itextpdf.text.pdf.PdfPCell; import com.itextpdf.text.pdf.PdfPTable; import java.io.InputStream; import java.util.ArrayList; import java.util.Date; import java.util.Iterator; import java.util.ResourceBundle; import java.util.regex.Matcher; import java.util.regex.Pattern; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.poifs.filesystem.POIFSFileSystem; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; public class FileServicePhysiqueImpl implements FileServicePhysique { CategorieServiceJDBC cs; LivreServiceJDBC ls; private static final Font catFont = new Font(Font.FontFamily.TIMES_ROMAN, 18, Font.BOLD); private static final Font smallBold = new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.BOLD); protected FileServicePhysiqueImpl(){ } @Override public Paragraph imprimerRecherche(ArrayList<Livre> livres, String criteres) { Paragraph preface = new Paragraph(); try { addEmptyLine(preface, 1); preface.add(new Paragraph("Recherche imprimée", catFont)); addEmptyLine(preface, 1); preface.add(new Paragraph(criteres)); addEmptyLine(preface, 1); preface.add(new Paragraph("Effectuer par : " + System.getProperty("user.name") + " le " + new Date(), smallBold)); addEmptyLine(preface, 1); PdfPTable table = new PdfPTable(5); String[] s = new String[5]; s[0] = "Titre"; s[1] = "Auteur"; s[2] = "Catégorie"; s[3] = "Origine"; s[4] = "Position"; for (int i = 0; i < s.length; i++) { PdfPCell c1 = new PdfPCell(new Phrase(s[i])); c1.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(c1); if(i >1){ table.setHeaderRows(1); } } for (int i = 0; i < livres.size(); i++) { table.addCell(livres.get(i).getTitre()); table.addCell(livres.get(i).getAuteur()); table.addCell(livres.get(i).getCatégorie().getNomCategorie()); table.addCell(livres.get(i).getOrigine()); table.addCell(livres.get(i).getPosition()); } preface.add(table); } catch (Exception e) { System.out.println(e); } return preface; } public String noAccents(String s) { String r = s.toUpperCase(); return r.toLowerCase(); } @Override public void lireSauvegardeExcel(InputStream is) { try { cs = PhysiqueFactory.getCategorieServiceJDBC(); ls = PhysiqueFactory.getLivreServiceJDBC(); ArrayList<Categorie> categories = new ArrayList(); POIFSFileSystem fs = new POIFSFileSystem(is); HSSFWorkbook wb = new HSSFWorkbook(fs); Cell cell; boolean firstLine, rowCategorie = false, effectuer; int numeroCell = 0; Categorie categorie; String auteur = ""; String titre = ""; String position = ""; String volume = ""; String exemplaire = ""; String année = ""; String prix = ""; String origine; Categorie defaultCategorie = new Categorie("default"); cs.add(defaultCategorie); for (int y = 0; y < wb.getNumberOfSheets(); y++) { System.err.println("nb feuille : " + wb.getNumberOfSheets()); effectuer = false; firstLine = true; categorie = null; HSSFSheet feuille = wb.getSheetAt(y); origine = feuille.getSheetName(); Iterator<Row> iterator = feuille.iterator(); while (iterator.hasNext()) { Row next = iterator.next(); if (effectuer) { firstLine = false; } if (!firstLine) { for (int i = next.getFirstCellNum(); i < next.getLastCellNum(); i++) { cell = next.getCell(i); if (cell != null) { switch (numeroCell) { case 0: if (cell.getCellType() == 1) { rowCategorie = true; } else { rowCategorie = false; } break; case 1: if (rowCategorie) { } else { if (y != 3) { auteur = cell.getStringCellValue(); auteur = auteur.trim(); auteur = noAccents(auteur); } else { } } break; case 2: if (rowCategorie) { categorie = new Categorie(cell.getStringCellValue()); } else if (cell.getCellType() == 1) { titre = cell.getStringCellValue(); titre = titre.trim(); titre = noAccents(titre); } else { titre = cell.getNumericCellValue() + ""; titre = titre.trim(); titre = noAccents(titre); } break; case 3: if (rowCategorie) { } else if (cell.getCellType() == 1) { position = cell.getStringCellValue(); position = position.trim(); } else { position = cell.getNumericCellValue() + ""; position = position.trim(); } break; case 4: if (rowCategorie) { } else { if (cell.getCellType() == 1) { volume = cell.getStringCellValue(); volume = volume.trim(); } else if (cell.getCellType() == 0) { volume = cell.getNumericCellValue() + ""; volume = volume.trim(); } } break; case 5: if (rowCategorie) { } else { if (cell.getCellType() == 1) { exemplaire = cell.getStringCellValue(); exemplaire = exemplaire.trim(); } else if (cell.getCellType() == 0) { exemplaire = cell.getNumericCellValue() + ""; exemplaire = exemplaire.trim(); } else if (cell.getCellType() == 2) { exemplaire = cell.getCellFormula() + ""; exemplaire = exemplaire.trim(); } } break; case 6: if (rowCategorie) { } else { if (cell.getCellType() == 1) { année = cell.getStringCellValue(); année = année.trim(); } else if (cell.getCellType() == 0) { année = cell.getNumericCellValue() + ""; année = année.trim(); } } break; case 7: if (rowCategorie) { } else { if (cell.getCellType() == 1) { prix = cell.getStringCellValue(); prix = prix.trim(); } else if (cell.getCellType() == 0) { prix = cell.getNumericCellValue() + ""; prix = prix.trim(); } } break; } } numeroCell++; } numeroCell = 0; if (rowCategorie) { } else { String[] exemplaireSplit = new String[10], volumeSplit = new String[10]; int i = 0, nbVolume = 1, z; if (volume != null) { volumeSplit = volume.split("-"); nbVolume = volumeSplit.length; } if (exemplaire != null) { Pattern p = Pattern.compile("[0-9]"); Matcher m = p.matcher(exemplaire); while (m.find()) { String tempo = m.group(); exemplaireSplit[i] = tempo; i++; } } for (z = 0; z < nbVolume; z++) { System.err.println("Ajout du livre : " + titre + " auteur : " + auteur + " position : " + position + " et exemplaire : " + exemplaireSplit[z]); Livre livre = new Livre(auteur, titre, position, volumeSplit[z], Integer.parseInt(exemplaireSplit[z]), année, prix, origine); if (categorie == null) { livre.setCatégorie(defaultCategorie); } else { livre.setCatégorie(categorie); } ls.add(livre); } auteur = ""; titre = ""; position = ""; volume = ""; exemplaire = ""; année = ""; prix = ""; } if (!categories.contains(categorie) && categorie != null) { categories.add(categorie); cs.add(categorie); } } effectuer = true; } } } catch (Exception ex) { System.err.println("Erreur Lire Sauvegarde Excel tier physique " + ex); } } @Override public void creerSauvegardeExcel(Workbook wb) { cs = PhysiqueFactory.getCategorieServiceJDBC(); ls = PhysiqueFactory.getLivreServiceJDBC(); int numeroLigne = 1; ArrayList<Categorie> categories = cs.getAll(); ArrayList<String> origines = ls.getAllOrigineLivre(); String[] tab = new String[8]; tab[0] = "Nr. Current"; tab[1] = "Autorul"; tab[2] = "Titlul"; tab[3] = "Pozitia"; tab[4] = "Nr Volume"; tab[5] = "Bucati"; tab[6] = "Editia"; tab[7] = "Pret"; try { org.apache.poi.ss.usermodel.Font font = wb.createFont(); font.setBoldweight(org.apache.poi.ss.usermodel.Font.BOLDWEIGHT_BOLD); CellStyle style = wb.createCellStyle(); style.setAlignment(CellStyle.ALIGN_CENTER); style.setFont(font); for (int i = 0; i < origines.size(); i++) { wb.createSheet(origines.get(i)); Sheet feuille = wb.getSheetAt(i); Row rowTitle = feuille.createRow(0); for (int y = 0; y < 8; y++) { Cell cellTitle = rowTitle.createCell(y); cellTitle.setCellValue(tab[y]); cellTitle.setCellStyle(style); } for (int numeroCategorie = 0; numeroCategorie < categories.size(); numeroCategorie++) { ArrayList<Livre> livres = ls.getLivreByOrigineEtCategorie(origines.get(i), categories.get(numeroCategorie)); int numeroLivre = 0; ArrayList<String> rassemblerVolume = rassemblerVolume(livres); while (numeroLivre < livres.size()) { Row row = feuille.createRow(numeroLigne); if (numeroLivre == 0) { if (!categories.get(numeroCategorie).getNomCategorie().equals("default")) { Cell cellC = row.createCell(0); cellC.setCellValue("C"); cellC.setCellStyle(style); Cell cellCat = row.createCell(2); cellCat.setCellValue(categories.get(numeroCategorie).getNomCategorie()); cellCat.setCellStyle(style); numeroLigne++; row = feuille.createRow(numeroLigne); } } row.createCell(0).setCellValue(numeroLivre); row.createCell(1).setCellValue(livres.get(numeroLivre).getAuteur()); row.createCell(2).setCellValue(livres.get(numeroLivre).getTitre()); row.createCell(3).setCellValue(livres.get(numeroLivre).getPosition()); row.createCell(4).setCellValue(livres.get(numeroLivre).getVolume()); row.createCell(5).setCellValue(rassemblerVolume.get(numeroLivre)); row.createCell(6).setCellValue(livres.get(numeroLivre).getAnnee()); row.createCell(7).setCellValue(livres.get(numeroLivre).getPrix()); numeroLivre++; numeroLigne++; } } numeroLigne = 1; for (int y = 0; y < tab.length; y++) { feuille.autoSizeColumn(y); } } } catch (Exception ex) { System.err.println("Erreur Creer Sauvegarde Excel tier physique " + ex); } } private static void addEmptyLine(Paragraph paragraph, int number) { for (int i = 0; i < number; i++) { paragraph.add(new Paragraph(" ")); } } public ArrayList<String> rassemblerVolume(ArrayList<Livre> livres) { int etat = -1; ArrayList< String> exemplaires = new ArrayList(); String exemplaire = null; for (int z = 0; z < livres.size(); z++) { Livre l1 = livres.get(z); if (z != livres.size() - 1) { Livre l2 = livres.get(z + 1); if (l1.getAnnee().equals(l2.getAnnee()) && l1.getAuteur().equals(l2.getAuteur()) && l1.getOrigine().equals(l2.getOrigine()) && l1.getTitre().equals(l2.getTitre())) { if (etat == -1) { etat = 0; } else { etat = 1; } String volume1 = l1.getVolume(); int exemplaire1 = l1.getExemplaire(); String volume2 = l2.getVolume(); int exemplaire2 = l2.getExemplaire(); l1.setVolume(volume1 + "-" + volume2); switch (etat) { case 0: exemplaire = "(" + exemplaire1 + ")-(" + exemplaire2 + ")"; break; case 1: exemplaire = exemplaire + "-(" + exemplaire2 + ")"; break; } livres.remove(z + 1); z--; } else { if (etat == -1) { exemplaires.add(l1.getExemplaire() + ""); } else { exemplaires.add(exemplaire); etat = -1; } } } if (z == livres.size() - 1) { exemplaires.add(l1.getExemplaire() + ""); } } return exemplaires; } @Override public void lireFichierPropriete(String url) { ResourceBundle rb = ResourceBundle.getBundle(url); String base = rb.getString("base"); String host = rb.getString("host"); String nomBase = rb.getString("nomBase"); String dbDriver = rb.getString("dbDriver"); String dbUser = rb.getString("dbUser"); String dbPasswd = rb.getString("dbPasswd"); PhysiqueFactory.setBase(base); PhysiqueFactory.setDbDriver(dbDriver); PhysiqueFactory.setHost(host); PhysiqueFactory.setMdp(dbPasswd); PhysiqueFactory.setNdc(dbUser); PhysiqueFactory.setNomBase(nomBase); } }