source: ether_megapoli/trunk/service/implementation/com/medias/megapoli/trade/ValeurAdapter.java @ 152

Last change on this file since 152 was 152, checked in by vmipsl, 13 years ago

Import medias files and cleanup

File size: 8.3 KB
Line 
1/*
2 * Created on 25 avr. 2005
3 */
4package com.medias.megapoli.trade;
5
6import java.util.Date;
7import java.util.List;
8
9import org.hibernate.HibernateException;
10import com.medias.megapoli.struts.forms.DatasForm;
11import com.medias.DateTime;
12import com.medias.Nombre;
13import com.medias.utils.hibernate.CommunicationBD;
14
15
16/**
17 * @author pignot
18 *
19 * Classe permettant, au moyen de requêtes sur la base de données,
20 * de charger la description d'une valeur
21 */
22public class ValeurAdapter {
23   
24    public ValeurAdapter () {
25    }
26
27    public void loadCountFromDatabase(DatasForm dForm) {
28        try {
29                int nbVal = 0;
30                String listeJeux = "", listePlats = "", listeParams = "", listeCapts = "";
31                if ((dForm.getSelectCapts() != null && dForm.getSelectCapts().length != 0)
32                                || (dForm.getSelectParams() != null && dForm.getSelectParams().length != 0)
33                                || (dForm.getSelectPlats() != null && dForm.getSelectPlats().length != 0)
34                                || (dForm.getSelectJeux() != null && dForm.getSelectJeux().length != 0)) {
35                    String dateMin = DateTime.getJourInc(dForm.getDateDeb(), DateTime.formatDateStandard, -1);
36                    String dateMax = DateTime.getJourInc(dForm.getDateFin(), DateTime.formatDateStandard, 1);
37                    Integer latMin = Nombre.latFloatToInt(Float.valueOf(dForm.getLatMin()));
38                    Integer latMax = Nombre.latFloatToInt(Float.valueOf(dForm.getLatMax()));
39                    Integer lonMin = Nombre.latFloatToInt(Float.valueOf(dForm.getLonMin()));
40                    Integer lonMax = Nombre.latFloatToInt(Float.valueOf(dForm.getLonMax()));
41                    String sqlDeb = "select count(v.valeurVal)";
42                    String sql = " from Valeur v";
43                    sql += " join v.mesure m  join m.localisation l";
44                    sql += " where m.mesureDate > '"+dateMin+"' and m.mesureDate < '"+dateMax+"'";
45                    sql += " and l.localisationLat >= "+latMin+" and l.localisationLat <= "+latMax;
46                    sql += " and l.localisationLon >= "+lonMin+" and l.localisationLon <= "+lonMax;
47                    String sqlBilan = " from Bilan b";
48                    sqlBilan += " where b.bilanDated <= '"+dateMax+"' and b.bilanDatef >= '"+dateMin+"'";
49                    sqlBilan += " and b.bilanLatmin <= "+latMax+" and b.bilanLatmax >= "+latMin;
50                    sqlBilan += " and b.bilanLonmin <= "+lonMax+" and b.bilanLonmax >= "+lonMin;
51                    if (dForm.getSelectCapts() != null && dForm.getSelectCapts().length != 0) {
52                        sql += " and (";
53                        sqlBilan += " and (";
54                        for (int num = 0; num < dForm.getNbSelectedCapts(); num++) {
55                                sql += (num == 0 ? "": " or ")+"v.capteur.capteurId = "+dForm.getSelectCapts()[num];
56                                sqlBilan += (num == 0 ? "": " or ")+"b.capteur.capteurId = "+dForm.getSelectCapts()[num];
57//                              listeCapts += (num == 0 ? "": ",")+dForm.getSelectCapts()[num];
58                        }
59                        sql += ")";
60                        sqlBilan += ")";
61                    }
62                    if (dForm.getSelectParams() != null && dForm.getSelectParams().length != 0) {
63                        sql += " and (";
64                        sqlBilan += " and (";
65                        for (int num = 0; num < dForm.getNbSelectedParams(); num++) {
66                                sql += (num == 0 ? "": " or ")+"v.parametre.parametreId = "+dForm.getSelectParams()[num];
67                                sqlBilan += (num == 0 ? "": " or ")+"b.parametre.parametreId = "+dForm.getSelectParams()[num];
68//                              listeParams += (num == 0 ? "": ",")+dForm.getSelectParams()[num];
69                        }
70                        sql += ")";
71                        sqlBilan += ")";
72                    }
73                    if (dForm.getSelectPlats() != null && dForm.getSelectPlats().length != 0) {
74                        sql += " and (";
75                        sqlBilan += " and (";
76                        for (int num = 0; num < dForm.getNbSelectedPlats(); num++) {
77                                sql += (num == 0 ? "": " or ")+"m.plateforme.plateformeId = "+dForm.getSelectPlats()[num];
78                                sqlBilan += (num == 0 ? "": " or ")+"b.plateforme.plateformeId = "+dForm.getSelectPlats()[num];
79//                              listePlats += (num == 0 ? "": ",")+dForm.getSelectPlats()[num];
80                        }
81                        sql += ")";
82                        sqlBilan += ")";
83                    }
84                        if (dForm.getSelectJeux() != null && dForm.getSelectJeux().length != 0) {
85                        sql += " and (";
86                        sqlBilan += " and (";
87                        for (int num = 0; num < dForm.getNbSelectedJeux(); num++) {
88                                sql += (num == 0 ? "": " or ")+"m.fichier.jeu.jeuId = "+dForm.getSelectJeux()[num];
89                                sqlBilan += (num == 0 ? "": " or ")+"b.jeu.jeuId = "+dForm.getSelectJeux()[num];
90//                              listeJeux += (num == 0 ? "": ",")+dForm.getSelectJeux()[num];
91                        }
92                        sql += ")";
93                        sqlBilan += ")";
94                    }
95                        String sqlParams = "select distinct b.parametre.parametreId";
96                        String sqlCapts = "select distinct b.capteur.capteurId";
97                        String sqlPlats = "select distinct b.plateforme.plateformeId";
98                        String sqlJeux = "select distinct b.jeu.jeuId";
99                        String sqlDatesLocs = "select min(b.bilanDated), max(b.bilanDatef), min(b.bilanLatmin), max(b.bilanLatmax), min(b.bilanLonmin), max(b.bilanLonmax)";
100                        CommunicationBD.open();
101                    List<?> res = CommunicationBD.getList(sqlDeb+sql);
102                    List<?> resParams = CommunicationBD.getList(sqlParams+sqlBilan);
103                    List<?> resCapts = CommunicationBD.getList(sqlCapts+sqlBilan);
104                    List<?> resPlats = CommunicationBD.getList(sqlPlats+sqlBilan);
105                    List<?> resJeux = CommunicationBD.getList(sqlJeux+sqlBilan);
106                    List<?> resDatesLocs = CommunicationBD.getList(sqlDatesLocs+sqlBilan);
107                                CommunicationBD.close();
108                                nbVal = ((Long)res.get(0)).intValue();
109                                String[] selJeux = new String[resJeux.size()];
110                                String[] selParams = new String[resParams.size()];
111                                String[] selCapts = new String[resCapts.size()];
112                                String[] selPlats = new String[resPlats.size()];
113                                Object[] selDatesLocs = (Object[])resDatesLocs.get(0);
114                                for (int num = 0; num < resJeux.size(); num++) {
115                                        selJeux[num] = resJeux.get(num).toString();
116                        listeJeux += (num == 0 ? "": ",")+resJeux.get(num);
117                                }
118                                for (int num = 0; num < resCapts.size(); num++) {
119                                        selCapts[num] = resCapts.get(num).toString();
120                        listeCapts += (num == 0 ? "": ",")+resCapts.get(num);
121                                }
122                                for (int num = 0; num < resPlats.size(); num++) {
123                                        selPlats[num] = resPlats.get(num).toString();
124                        listePlats += (num == 0 ? "": ",")+resPlats.get(num);
125                                }
126                                for (int num = 0; num < resParams.size(); num++) {
127                                        selParams[num] = resParams.get(num).toString();
128                        listeParams += (num == 0 ? "": ",")+resParams.get(num);
129                                }
130                               
131                                dForm.setSelectCapts(selCapts);
132                                dForm.setSelectJeux(selJeux);
133                                dForm.setSelectParams(selParams);
134                                dForm.setSelectPlats(selPlats);
135                                String dateD = DateTime.dateToString((Date)selDatesLocs[0],DateTime.formatDateStandard);
136                                String dateF = DateTime.dateToString((Date)selDatesLocs[1],DateTime.formatDateStandard);
137                                if (dateD != null && DateTime.plusGrand(dateD, dForm.getDateDeb())) dForm.setDateDeb(dateD);
138                                if (dateF != null && DateTime.plusGrand(dForm.getDateFin(), dateF)) dForm.setDateFin(dateF);
139                                Integer latD = (Integer)selDatesLocs[2];
140                                Integer latF = (Integer)selDatesLocs[3];
141                                Integer lonD = (Integer)selDatesLocs[4];
142                                Integer lonF = (Integer)selDatesLocs[5];
143                                if (latD != null && latMin.intValue() < latD.intValue()) dForm.setLatMin(Nombre.latIntToFloat(latD).toString());
144                                if (latF != null && latF.intValue() < latMax.intValue()) dForm.setLatMax(Nombre.latIntToFloat(latF).toString());
145                                if (lonD != null && lonMin.intValue() < lonD.intValue()) dForm.setLonMin(Nombre.latIntToFloat(lonD).toString());
146                                if (lonF != null && lonF.intValue() < lonMax.intValue()) dForm.setLonMax(Nombre.latIntToFloat(lonF).toString());
147                }
148                        dForm.setNbValeurs(nbVal);
149                        dForm.setListeJeux(listeJeux);
150                        dForm.setListeCapts(listeCapts);
151                        dForm.setListeParams(listeParams);
152                        dForm.setListePlats(listePlats);
153        } catch (HibernateException e) {
154            System.out.println("exception_MesureAdapter");
155            // Libération de la connection à la base de données
156            try {
157               CommunicationBD.close();
158            } catch (HibernateException ex) {
159                ex.printStackTrace();
160            }
161        }
162    }
163}
Note: See TracBrowser for help on using the repository browser.