1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
|
package question2.csv;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.net.URL;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import question2.bean.Auteur;
import question2.bean.Livre;
import question2.interfaces.AbstractBibliotheque;
public class BibliothequeCSV extends AbstractBibliotheque{
private String pathAuteursCSV;
private String pathLivresCSV;
private SimpleDateFormat sdf;
//Auteurs
private final String ID = "id";
private final String NOM = "nom";
private final String PRENOM = "prenom";
private final String DATE_NAISSANCE = "date_naissance";
private final String[] AUTEURS_TAB = {ID,NOM,PRENOM,DATE_NAISSANCE};
//Livres
private final String ISBN = "isbn";
private final String TITRE = "titre";
private final String AUTEURS = "auteurs";
private final String[] LIVRES_TAB = {ISBN,TITRE,AUTEURS};
public BibliothequeCSV(){
super();
}
@Override
public void initialise() throws Exception {
livres = new ArrayList<Livre>();
auteurs = new ArrayList<Auteur>();
Map<Integer,Auteur> mapAuteurs = initialiseAuteurs();
initialiseLivres(mapAuteurs);
}
private Map<Integer,Auteur> initialiseAuteurs() throws IOException,ParseException{
Map<Integer,Auteur> mReturn = new HashMap<Integer,Auteur>();
URL url = this.getClass().getResource(pathAuteursCSV);
try(FileReader fr = new FileReader(new File(url.getFile()))){
try(BufferedReader br = new BufferedReader(fr)){
int i = 0;
String ligne;
Map<Integer, String> mapIndex = null;
Map<String,String> map;
Auteur auteur;
while( (ligne = br.readLine()) != null){
if(i == 0){
mapIndex = readLigneOne(ligne, Arrays.asList(AUTEURS_TAB));
}else{
map = readLignes(ligne, mapIndex);
auteur = new Auteur();
auteur.setLivres(new ArrayList<Livre>());
auteur.setNom(map.get(NOM));
auteur.setPrenom(map.get(PRENOM));
auteur.setDateDeNaissance(sdf.parse(map.get(DATE_NAISSANCE)));
mReturn.put(Integer.parseInt(map.get(ID)), auteur);
auteurs.add(auteur);
}
i++;
}
}catch(IOException | ParseException e){
throw e;
}
}catch(IOException | ParseException e){
throw e;
}
return mReturn;
}
private void initialiseLivres(Map<Integer,Auteur> mapAuteurs)throws IOException{
URL url = this.getClass().getResource(pathLivresCSV);
try(FileReader fr = new FileReader(new File(url.getFile()))){
try(BufferedReader br = new BufferedReader(fr)){
int i = 0;
String ligne;
Map<Integer, String> mapIndex = null;
Map<String,String> map;
Livre livre;
Auteur auteur;
String[] auteurs;
Integer id;
while( (ligne = br.readLine()) != null){
if(i == 0){
mapIndex = readLigneOne(ligne, Arrays.asList(LIVRES_TAB));
}else{
map = readLignes(ligne, mapIndex);
livre= new Livre();
livre.setIsbn(map.get(ISBN));
livre.setTitre(map.get(TITRE));
livre.setAuteurs(new ArrayList<Auteur>());
auteurs = map.get(AUTEURS).split("/");
for(String a:auteurs){
id = Integer.parseInt(a);
auteur = mapAuteurs.get(id);
auteur.getLivres().add(livre);
livre.getAuteurs().add(auteur);
}
livres.add(livre);
}
i++;
}
}catch(IOException e){
throw e;
}
}catch(IOException e){
throw e;
}
}
private Map<Integer, String> readLigneOne(String ligne,List<String> list){
Map<Integer, String> mapReturn = new HashMap<Integer, String>();
String[] tab = ligne.split(",");
String a;
for(Integer index = 0; index < tab.length;index++){
a = tab[index];
if(list.contains(a))
mapReturn.put(index,a);
}
return mapReturn;
}
private Map<String, String> readLignes(String ligne, Map<Integer, String> mapIndex){
Map<String, String> mapReturn = new HashMap<String, String>();
String[] tab = ligne.split(",");
for(Integer i:mapIndex.keySet()){
mapReturn.put(mapIndex.get(i), tab[i]);
}
return mapReturn;
}
public void setPathAuteursCSV(String pathAuteursCSV) {
this.pathAuteursCSV = pathAuteursCSV;
}
public void setPathLivresCSV(String pathLivresCSV) {
this.pathLivresCSV = pathLivresCSV;
}
public void setSdf(SimpleDateFormat sdf) {
this.sdf = sdf;
}
} |
Partager