1 | /* |
---|
2 | * Created on 25 avr. 2005 |
---|
3 | */ |
---|
4 | package com.medias.megapoli.trade; |
---|
5 | |
---|
6 | import java.io.File; |
---|
7 | import java.util.ArrayList; |
---|
8 | import java.util.Collections; |
---|
9 | import java.util.Iterator; |
---|
10 | import java.util.Set; |
---|
11 | |
---|
12 | import org.hibernate.HibernateException; |
---|
13 | import com.medias.database.objects.Capteur; |
---|
14 | import com.medias.database.objects.Categorie; |
---|
15 | import com.medias.database.objects.Commentaire; |
---|
16 | import com.medias.database.objects.Fabriquant; |
---|
17 | import com.medias.database.objects.Jeu; |
---|
18 | import com.medias.database.objects.Parametre; |
---|
19 | import com.medias.database.objects.Plateforme; |
---|
20 | import com.medias.database.objects.RequetePlatLoc; |
---|
21 | import com.medias.database.objects.TypeCapteur; |
---|
22 | import com.medias.database.objects.TypePlateforme; |
---|
23 | import com.medias.megapoli.struts.forms.PlateformeForm; |
---|
24 | import com.medias.megapoli.utils.MegapoliInitialisation; |
---|
25 | import com.medias.Nombre; |
---|
26 | import com.medias.utils.hibernate.CommunicationBD; |
---|
27 | |
---|
28 | |
---|
29 | /** |
---|
30 | * @author pignot |
---|
31 | * |
---|
32 | * Classe permettant, au moyen de requêtes sur la base de données, |
---|
33 | * de charger la description d'une plate-forme |
---|
34 | */ |
---|
35 | public class PlateformeAdapter { |
---|
36 | |
---|
37 | private Plateforme plateforme; |
---|
38 | |
---|
39 | public PlateformeAdapter() { |
---|
40 | plateforme = null; |
---|
41 | } |
---|
42 | |
---|
43 | public void loadPlateformeFromDatabase (PlateformeForm platForm, String id) { |
---|
44 | try { |
---|
45 | // Traitement de l'identifiant transmis |
---|
46 | String path = ""; |
---|
47 | if (id.indexOf("_") != -1) { |
---|
48 | path = id.substring(id.indexOf("_")+1); |
---|
49 | id = id.substring(0,id.indexOf("_")); |
---|
50 | } |
---|
51 | CommunicationBD.open(); |
---|
52 | Plateforme plat = (Plateforme)CommunicationBD.getObjectById(Plateforme.class, new Integer(id)); |
---|
53 | // Recherche de la position de chaque plate-forme |
---|
54 | //List<?> locs = CommunicationBD.getList("from RequetePlatLoc as req where plateforme_id='"+id+"'"); |
---|
55 | Set<RequetePlatLoc> locs = plat.getRequetePlatLocs(); |
---|
56 | // Recherche des capteurs associés |
---|
57 | //List<?> capteurs = CommunicationBD.getList("select req.capteur from RequetePlateformeCapteur as req where plateforme_id='" + id + "'"); |
---|
58 | Set<Capteur> capts = plat.getCapteurs(); |
---|
59 | // Recherche des paramÚtres associés |
---|
60 | //List<?> parametres = CommunicationBD.getList("select req.parametre from RequetePlateformeParametre as req where plateforme_id='" + id + "'"); |
---|
61 | Set<Parametre> params = plat.getParametres(); |
---|
62 | // Recherche des jeux associés |
---|
63 | //List<?> jeux = CommunicationBD.getList("select req.jeu from RequetePlateformeJeu as req where plateforme_id='" + id + "'"); |
---|
64 | Set<Jeu> jeus = plat.getJeus(); |
---|
65 | plateforme = new Plateforme(); |
---|
66 | if (plat.getCommentaire() != null) { |
---|
67 | Commentaire commentaire = new Commentaire(); |
---|
68 | commentaire.setCommentaireTxt(plat.getCommentaire().getCommentaireTxt()); |
---|
69 | plateforme.setCommentaire(commentaire); |
---|
70 | } |
---|
71 | plateforme.setPlateformeId(plat.getPlateformeId()); |
---|
72 | plateforme.setPlateformeNom(plat.getPlateformeNom()); |
---|
73 | TypePlateforme typePlateforme = new TypePlateforme(); |
---|
74 | typePlateforme.setTypePlateformeNom(plat.getTypePlateforme().getTypePlateformeNom()); |
---|
75 | plateforme.setTypePlateforme(typePlateforme); |
---|
76 | Iterator<RequetePlatLoc> it_locs = locs.iterator(); |
---|
77 | if (it_locs.hasNext()) { |
---|
78 | RequetePlatLoc loc = it_locs.next(); |
---|
79 | platForm.setLatMin(String.valueOf(Nombre.arrondir(Nombre.latIntToFloat(new Integer(loc.getLatMin())).doubleValue(),4))); |
---|
80 | platForm.setLatMax(String.valueOf(Nombre.arrondir(Nombre.latIntToFloat(new Integer(loc.getLatMax())).doubleValue(),4))); |
---|
81 | platForm.setLonMin(String.valueOf(Nombre.arrondir(Nombre.latIntToFloat(new Integer(loc.getLonMin())).doubleValue(),4))); |
---|
82 | platForm.setLonMax(String.valueOf(Nombre.arrondir(Nombre.latIntToFloat(new Integer(loc.getLonMax())).doubleValue(),4))); |
---|
83 | platForm.setAltMin(String.valueOf(Nombre.arrondir(Nombre.altIntToFloat(new Integer(loc.getAltMin())).doubleValue(),4))); |
---|
84 | platForm.setAltMax(String.valueOf(Nombre.arrondir(Nombre.altIntToFloat(new Integer(loc.getAltMax())).doubleValue(),4))); |
---|
85 | if (loc.getHsMin() != null) { |
---|
86 | platForm.setHsMin(String.valueOf(Nombre.arrondir(Nombre.altIntToFloat(loc.getHsMin()).doubleValue(),4))); |
---|
87 | } |
---|
88 | else { |
---|
89 | platForm.setHsMin("-1000000"); |
---|
90 | } |
---|
91 | if (loc.getHsMax() != null) { |
---|
92 | platForm.setHsMax(String.valueOf(Nombre.arrondir(Nombre.altIntToFloat(loc.getHsMax()).doubleValue(),4))); |
---|
93 | } else { |
---|
94 | platForm.setHsMax("-1000000"); |
---|
95 | } |
---|
96 | } else { |
---|
97 | platForm.setLatMin(""); |
---|
98 | platForm.setLatMax(""); |
---|
99 | platForm.setLonMin(""); |
---|
100 | platForm.setLonMax(""); |
---|
101 | platForm.setAltMin(""); |
---|
102 | platForm.setAltMax(""); |
---|
103 | platForm.setHsMin(""); |
---|
104 | platForm.setHsMax(""); |
---|
105 | } |
---|
106 | ArrayList<Parametre> parametres = new ArrayList<Parametre>(); |
---|
107 | Parametre parametre; |
---|
108 | for (Parametre param: params) { |
---|
109 | parametre = new Parametre(); |
---|
110 | parametre.setParametreId(param.getParametreId()); |
---|
111 | parametre.setParametreCode(param.getParametreCode()); |
---|
112 | parametre.setParametreNom(param.getParametreNom()); |
---|
113 | parametres.add(parametre); |
---|
114 | } |
---|
115 | Collections.sort(parametres,new Parametre().new ComparatorNom()); |
---|
116 | ArrayList<Capteur> capteurs = new ArrayList<Capteur>(); |
---|
117 | Capteur capteur; |
---|
118 | TypeCapteur typeCapteur; |
---|
119 | Fabriquant fabriquant; |
---|
120 | for (Capteur capt : capts) { |
---|
121 | capteur = new Capteur(); |
---|
122 | capteur.setCapteurId(capt.getCapteurId()); |
---|
123 | capteur.setCapteurModele(capt.getCapteurModele()); |
---|
124 | capteur.setCapteurNumserie(capt.getCapteurNumserie()); |
---|
125 | typeCapteur = new TypeCapteur(); |
---|
126 | typeCapteur.setTypeCapteurNom(capt.getTypeCapteur().getTypeCapteurNom()); |
---|
127 | capteur.setTypeCapteur(typeCapteur); |
---|
128 | fabriquant = new Fabriquant(); |
---|
129 | fabriquant.setFabriquantNom(capt.getFabriquant().getFabriquantNom()); |
---|
130 | capteur.setFabriquant(fabriquant); |
---|
131 | capteurs.add(capteur); |
---|
132 | } |
---|
133 | Collections.sort(capteurs,new Capteur().new ComparatorNom()); |
---|
134 | ArrayList<Jeu> jeux = new ArrayList<Jeu>(); |
---|
135 | Jeu jeu; |
---|
136 | Categorie categorie; |
---|
137 | for (Jeu j : jeus) { |
---|
138 | jeu = new Jeu(); |
---|
139 | jeu.setJeuId(j.getJeuId()); |
---|
140 | jeu.setJeuNom(j.getJeuNom()); |
---|
141 | categorie = new Categorie(); |
---|
142 | categorie.setCategorieNom(j.getCategorie().getCategorieNom()); |
---|
143 | jeu.setCategorie(categorie); |
---|
144 | jeu.setNbFichiers(j.getFichiers().size()); |
---|
145 | jeux.add(jeu); |
---|
146 | } |
---|
147 | CommunicationBD.close(); |
---|
148 | platForm.setPlateforme(plateforme); |
---|
149 | platForm.setParametres(parametres); |
---|
150 | platForm.setCapteurs(capteurs); |
---|
151 | platForm.setJeux(jeux); |
---|
152 | platForm.setPath(path); |
---|
153 | // Images associées à la plate-forme |
---|
154 | ArrayList<String> images = chercherImages(jeux); |
---|
155 | platForm.setImages(images); |
---|
156 | } |
---|
157 | catch (HibernateException e) { |
---|
158 | System.out.println ("exception_PlateformeAdapter"); |
---|
159 | // Libération de la connection à la base de données |
---|
160 | try { |
---|
161 | CommunicationBD.close(); |
---|
162 | } catch (HibernateException ex) { |
---|
163 | |
---|
164 | } |
---|
165 | } |
---|
166 | } |
---|
167 | |
---|
168 | /** |
---|
169 | * Méthode permettant de rechercher les images associées à un ou plusieurs jeux |
---|
170 | */ |
---|
171 | private ArrayList<String> chercherImages(ArrayList<Jeu> jeux) { |
---|
172 | ArrayList<String> images = new ArrayList<String>(); |
---|
173 | String path = MegapoliInitialisation.pathImages; |
---|
174 | |
---|
175 | String nomJeu, nomCateg, cheminJeu, pathImg; |
---|
176 | File fJeu; |
---|
177 | String[] listeFic; |
---|
178 | for (Jeu jeu: jeux) { |
---|
179 | nomJeu = jeu.getJeuNom(); |
---|
180 | nomCateg = jeu.getCategorie().getCategorieNom(); |
---|
181 | cheminJeu = path + "/" + nomCateg +"/" + nomJeu; |
---|
182 | fJeu = new File(cheminJeu); |
---|
183 | if (fJeu.exists()) { |
---|
184 | cheminJeu += "/"; |
---|
185 | // On recherche les images |
---|
186 | fJeu = new File(cheminJeu); |
---|
187 | listeFic = fJeu.list(); |
---|
188 | for (int numFic = 0; numFic < listeFic.length; numFic++) { |
---|
189 | pathImg = cheminJeu+listeFic[numFic]; |
---|
190 | // pathImg = pathImg.replaceAll("/www", "http://medias.cnrs.fr"); |
---|
191 | images.add(pathImg); |
---|
192 | } |
---|
193 | } |
---|
194 | } |
---|
195 | if (images.size() == 0) { |
---|
196 | images.add(""); |
---|
197 | } |
---|
198 | return images; |
---|
199 | } |
---|
200 | |
---|
201 | public static com.medias.xml.metadata.Plateforme dbPlateformeToMetaPlateforme (Plateforme dbPlateforme) { |
---|
202 | com.medias.xml.metadata.Plateforme plateforme = new com.medias.xml.metadata.Plateforme(); |
---|
203 | |
---|
204 | plateforme.setNom (dbPlateforme.getPlateformeNom()); |
---|
205 | if (dbPlateforme.getCommentaire() != null) { |
---|
206 | plateforme.setDesc (dbPlateforme.getCommentaire().getCommentaireTxt()); |
---|
207 | } |
---|
208 | plateforme.setTypePlateforme(dbPlateforme.getTypePlateforme().getTypePlateformeNom()); |
---|
209 | plateforme.setIdPlateforme(String.valueOf(dbPlateforme.getPlateformeId())); |
---|
210 | //plateforme.setListe_images (dbPlateforme.); |
---|
211 | return plateforme; |
---|
212 | } |
---|
213 | |
---|
214 | |
---|
215 | public static ArrayList<com.medias.xml.metadata.Plateforme> dbPlateformesToMetaPlateformes (Set<Plateforme> dbPlateformes) { |
---|
216 | ArrayList<com.medias.xml.metadata.Plateforme> result = new ArrayList<com.medias.xml.metadata.Plateforme>(); |
---|
217 | |
---|
218 | for (Plateforme p : dbPlateformes) { |
---|
219 | result.add(dbPlateformeToMetaPlateforme (p)); |
---|
220 | } |
---|
221 | Collections.sort(result,new com.medias.xml.metadata.Plateforme().new ComparatorNom()); |
---|
222 | return result; |
---|
223 | } |
---|
224 | } |
---|