/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package Physique;
import Metier.Armoire;
import Metier.StructureBibliotheque;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
public class StructureBibliothequeServiceJDBCImpl implements StructureBibliothequeServiceJDBC {
ConnectionService conn;
ArmoireServiceJDBC as = PhysiqueFactory.getArmoireService();
protected StructureBibliothequeServiceJDBCImpl() {
try {
conn = ConnectionService.getInstance(PhysiqueFactory.getBase(), PhysiqueFactory.getHost(), PhysiqueFactory.getNomBase(), PhysiqueFactory.getDbDriver(), PhysiqueFactory.getNdc(), PhysiqueFactory.getMdp());
} catch (Exception ex) {
System.err.println("Erreur structure constructeur" + ex);
}
}
@Override
public void add(StructureBibliotheque structure) {
try {
Statement st = conn.getStatement();
st.executeUpdate("INSERT INTO structure(nom, idArmoire) VALUES ('" + structure.getNom() + "', '" + null + "')");
ResultSet rs = st.executeQuery("SELECT * FROM structure WHERE nom ='" + structure.getNom() + "'");
while (rs.next()) {
structure.setId(rs.getInt("id"));
}
rs.close();
} catch (Exception ex) {
System.err.println("Erreur structure add" + ex);
}
}
@Override
public void update(StructureBibliotheque structure) {
try {
Statement st = conn.getStatement();
String s = "";
for (int i = 0; i < structure.getArmoires().size(); i++) {
s += structure.getArmoires().get(i).getId() + ";";
}
st.executeUpdate("UPDATE structure SET nom='" + structure.getNom() + "', idArmoire='" + s + "' WHERE id ='" + structure.getId() + "'");
} catch (Exception ex) {
System.err.println("Erreur structure update" + ex);
}
}
@Override
public void remove(StructureBibliotheque structure) {
try {
Statement st = conn.getStatement();
st.executeUpdate("DELETE FROM structure WHERE id ='" + structure.getId() + "'");
} catch (Exception ex) {
System.err.println("Erreur structure remove" + ex);
}
}
@Override
public ArrayList<StructureBibliotheque> getAll() {
ArrayList<StructureBibliotheque> structures = new ArrayList();
try {
Statement st = conn.getStatement();
ResultSet rs = st.executeQuery("SELECT * FROM structure");
while (rs.next()) {
StructureBibliotheque structure = new StructureBibliotheque(rs.getString("nom"));
String s = rs.getString("idArmoire");
if (!s.equals("null") && !s.equals("")) {
ArrayList<Armoire> armoires = new ArrayList();
String[] split = s.split(";");
for (String split1 : split) {
armoires.add(as.getById(Integer.parseInt(split1)));
}
structure.setArmoires(armoires);
structure.setId(rs.getInt("id"));
}
structures.add(structure);
}
rs.close();
} catch (Exception ex) {
System.err.println("Erreur structure getall" + ex);
}
return structures;
}
@Override
public StructureBibliotheque getById(int id) {
StructureBibliotheque structure = null;
try {
Statement st = conn.getStatement();
ResultSet rs = st.executeQuery("SELECT * FROM structure WHERE id ='" + id + "'");
while (rs.next()) {
structure = new StructureBibliotheque(rs.getString("nom"));
structure.setId(id);
String s = rs.getString("idArmoire");
if (!s.equals("null") && !s.equals("")) {
ArrayList<Armoire> armoires = new ArrayList();
String[] split = s.split(";");
for (String split1 : split) {
armoires.add(as.getById(Integer.parseInt(split1)));
}
structure.setArmoires(armoires);
}
}
rs.close();
} catch (Exception ex) {
System.err.println("Erreur structure getbyid" + ex);
}
return structure;
}
@Override
public StructureBibliotheque getByNom(String nom) {
StructureBibliotheque structure = null;
try {
Statement st = conn.getStatement();
ResultSet rs = st.executeQuery("SELECT * FROM structure WHERE nom ='" + nom + "'");
while (rs.next()) {
structure = new StructureBibliotheque(rs.getString("nom"));
structure.setId(rs.getInt("id"));
String s = rs.getString("idArmoire");
if (!s.equals("null") && !s.equals("")) {
ArrayList<Armoire> armoires = new ArrayList();
String[] split = s.split(";");
for (String split1 : split) {
armoires.add(as.getById(Integer.parseInt(split1)));
}
structure.setArmoires(armoires);
}
}
rs.close();
} catch (Exception ex) {
System.err.println("Erreur structure getbynom" + ex);
}
return structure;
}
}