/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package Metier;
import com.itextpdf.text.Paragraph;
import java.io.InputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Collections;
import org.apache.poi.ss.usermodel.Workbook;
public class Principale {
BibliothecaireService bs = MetierFactory.getBibliothecaireService();
LivreService ls = MetierFactory.getLivreService();
CategorieService cs = MetierFactory.getCategorieService();
FileService fs = MetierFactory.getFileService();
StructureBibliothequeService sbs = MetierFactory.getStructureService();
protected Principale(){
}
public void remplirStructure(StructureBibliotheque sb) {
ArrayList<Armoire> armoires = sb.getArmoires();
ArrayList<Livre> livres = ls.getAllOrderByOrigineAndCategorieAndTitre();
int numeroLivre = 0;
for (int a = 0; a < armoires.size(); a++) {
for (int b = 0; b < armoires.get(a).getEtageres().size(); b++) {
Etagere etagere = armoires.get(a).getEtageres().get(b);
int nbLivreMax = etagere.getNbLivreMax(), profondeurEnCours = 0;
int marge = (int) (nbLivreMax * 0.25);
while (profondeurEnCours < etagere.getProfondeur()) {
for (int i = 0; i < nbLivreMax - marge; i++) {
if (livres.size() > numeroLivre) {
Livre livre = livres.get(numeroLivre);
if (livre.getExemplaire() > 1) {
String s = "";
for(int l = 0; l < livre.getExemplaire(); l++){
s += armoires.get(a).getCharArmoire() + "-"
+ etagere.getNumero() + "-" + (i + 1) + "-"
+ (profondeurEnCours+1);
if((l+1) < livre.getExemplaire()){
s += " / ";
i++;
}
}
String r = s.split("/")[0];
livre.setPosition(s);
} else {
livre.setPosition(armoires.get(a).getCharArmoire() + "-"
+ etagere.getNumero() + "-" + (i + 1) + "-"
+ (profondeurEnCours+1));
}
numeroLivre++;
ls.update(livre);
}
}
profondeurEnCours++;
}
}
}
}
public Paragraph imprimerRecherche(ArrayList<Livre> livres, String criteres) {
return fs.imprimerRecherche(livres, criteres);
}
public ArrayList<Livre> rechercheLimit(int min, int max, ArrayList<Livre> livres) {
ArrayList<Livre> retour = new ArrayList();
int nbMax;
if (max > livres.size()) {
nbMax = livres.size();
} else {
nbMax = max;
}
for (int i = min; i < nbMax; i++) {
retour.add(livres.get(i));
}
return retour;
}
public int getNbLivreTotalBDD() {
int nb = 0;
ArrayList<Livre> livres = this.ls.getAll();
for (int i = 0; i < livres.size(); i++) {
nb += livres.get(i).getExemplaire();
}
return nb;
}
public void CreerSauvegardeExcel(Workbook wb) {
fs.creerSauvegardeExcel(wb);
}
public void lireSauvegardeExcel(InputStream is) {
fs.lireSauvegardeExcel(is);
}
public ArrayList<Livre> tri(ArrayList<Livre> livres, int choix, int croissantOrNot) {
for (int i = 0; i < livres.size(); i++) {
livres.get(i).setEtatDeTri(croissantOrNot);
}
switch (choix) {
case 0:
Collections.sort(livres, Livre.TITRE_COMPARATOR);
break;
case 1:
Collections.sort(livres, Livre.AUTEUR_COMPARATOR);
break;
case 2:
Collections.sort(livres, Livre.CATEGORIE_COMPARATOR);
break;
default:
System.err.println("Erreur Tri");
}
return livres;
}
public String hashageMd5(byte[] hash) {
StringBuilder hashString = new StringBuilder();
for (int i = 0; i < hash.length; i++) {
String hex = Integer.toHexString(hash[i]);
if (hex.length() == 1) {
hashString.append('0');
hashString.append(hex.charAt(hex.length() - 1));
} else {
hashString.append(hex.substring(hex.length() - 2));
}
}
return hashString.toString();
}
public boolean connexion(String ndc, String mdp) {
boolean etat = false;
try {
String mdpProposerHasher = hashageMd5(MessageDigest.getInstance("MD5").digest(mdp.getBytes()));
System.out.println(mdpProposerHasher);
ArrayList<Bibliothecaire> all = bs.getAll();
for (int i = 0; i < all.size(); i++) {
String mdpBDD = all.get(i).getMdp();
System.out.println(mdpBDD + " " + mdpProposerHasher);
if (ndc.equals(all.get(i).getNdc()) && mdpProposerHasher.equals(mdpBDD)) {
etat = true;
}
}
} catch (NoSuchAlgorithmException ex) {
System.out.println(ex);
}
return etat;
}
public ArrayList<Livre> recherche(String auteur, boolean auteurEtOuTitre, String titre, boolean titreEtOuCategorie, String nomCategorie, boolean onlyMulti) {
// POUR LES BOOLEANS, SI IL EST FALSE C'EST UN "ET" ET SI C'EST TRUE ALORS C'EST "OU"
Categorie categorie = cs.getByNom(nomCategorie);
ArrayList<Livre> livres = null;
if (auteur.equals("") && titre.equals("") && categorie == null) {
livres = ls.getAll();
} else if (!auteur.equals("") && titre.equals("") && categorie == null) {
livres = ls.getByAuteur(auteur);
} else if (auteur.equals("") && !titre.equals("") && categorie == null) {
livres = ls.getByTitle(titre);
} else if (auteur.equals("") && titre.equals("") && categorie != null) {
livres = ls.getByCategorie(categorie);
} else if (!auteur.equals("") && !titre.equals("") && categorie == null) {
livres = ls.getByAuteurEtTitre(auteur, titre, auteurEtOuTitre);
} else if (!auteur.equals("") && titre.equals("") && categorie != null) {
livres = ls.getByAuteurEtCategorie(auteur, categorie, titreEtOuCategorie);
} else if (auteur.equals("") && !titre.equals("") && categorie != null) {
livres = ls.getByTitreEtCategorie(titre, categorie, titreEtOuCategorie);
} else if (!auteur.equals("") && !titre.equals("") && categorie != null) {
livres = ls.getByAuteurEtTitreEtCategorie(auteur, titre, categorie, auteurEtOuTitre, titreEtOuCategorie);
}
if(onlyMulti && livres != null){
ArrayList<Livre> livresReturn = new ArrayList();
for(int i = 0; i < livres.size(); i++){
if(livres.get(i).getExemplaire()>1){
livresReturn.add(livres.get(i));
}
}
return livresReturn;
}
return livres;
}
}