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

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

Import medias files and cleanup

  • Property svn:executable set to *
File size: 9.0 KB
Line 
1/*
2 * Created on 15 avr. 2005
3 */
4package com.medias.megapoli.trade;
5
6import com.medias.ConversionException;
7import com.medias.DateTime;
8import com.medias.Nombre;
9import com.medias.database.objects.Capteur;
10import com.medias.database.objects.Jeu;
11import com.medias.database.objects.Parametre;
12import com.medias.database.objects.Plateforme;
13import com.medias.database.objects.RequeteNbvalsJeu;
14import com.medias.megapoli.struts.forms.MetadataForm;
15import com.medias.utils.hibernate.CommunicationBD;
16import com.medias.xml.metadata.Contenu;
17import com.medias.xml.metadata.Description;
18import com.medias.xml.metadata.ExactTime;
19import com.medias.xml.metadata.Integration;
20import com.medias.xml.metadata.Metadata;
21import com.medias.xml.metadata.Presentation;
22import org.hibernate.HibernateException;
23import org.jdom.JDOMException;
24import com.medias.megapoli.struts.exceptions.MetadataException;
25
26import java.io.IOException;
27import java.util.Set;
28
29
30/**
31 * @author pignot
32 *
33 *
34 */
35public class MetadataAdapter {
36       
37        private Metadata metadata;
38       
39        public MetadataAdapter() {
40                metadata = new Metadata();
41        }
42       
43        private Presentation getPresentation(Jeu jeu) {
44                Presentation presentation = new Presentation();
45                presentation.setNom(jeu.getJeuNom());
46                presentation.setCat(jeu.getCategorie().getCategorieNom());
47                presentation.setDest("SGBD");
48                if (jeu.getCommentaire() != null) {
49                    presentation.setDef(jeu.getCommentaire().getCommentaireTxt().replaceAll("\\n", "<br/>"));
50                }
51                return presentation;
52        }
53       
54        private Description getDescription (Jeu jeu, int nbFichiers, int nbMesures) {
55                Description description = new Description ();
56               
57                description.setOrganisme (jeu.getOrganisme ().getOrganismeNom ());
58                if (jeu.getPersonneByPiId () != null) {
59                        description.setPi (PersonneAdapter.dbPersonneToMetaContact (jeu.getPersonneByPiId ()));
60                }
61                description.setTech (PersonneAdapter.dbPersonneToMetaContact (jeu.getPersonneByContactId ()));
62                String dateD = DateTime.dateToString( jeu.getJeuDated() );
63                if (dateD != null) {
64                    ExactTime dateDeb = new ExactTime();
65                    dateDeb.setDate (dateD.substring (0,10));
66                    dateDeb.setHeure (dateD.substring (11));
67                    description.setDateDeb (dateDeb);
68                }
69                String dateF = DateTime.dateToString (jeu.getJeuDatef ());
70                if (dateF != null) {
71                    ExactTime dateFin = new ExactTime ();
72                    dateFin.setDate (dateF.substring (0,10));
73                    dateFin.setHeure (dateF.substring (11));
74                    description.setDateFin (dateFin);
75                }
76                if (jeu.getJeuLatmin () != null) {
77                    Float latmin = Nombre.latIntToFloat (jeu.getJeuLatmin ());
78                    description.setLat_min (Nombre.arrondir (latmin.doubleValue (), 2));
79                }
80                if (jeu.getJeuLatmax () != null) {
81                    Float latmax = Nombre.latIntToFloat (jeu.getJeuLatmax ());
82                    description.setLat_max (Nombre.arrondir (latmax.doubleValue (), 2));                                   
83                }
84                if (jeu.getJeuLonmin () != null) {
85                    Float lonmin = Nombre.latIntToFloat (jeu.getJeuLonmin ());
86                    description.setLon_min (Nombre.arrondir (lonmin.doubleValue (), 2));
87                }
88                if (jeu.getJeuLonmax () != null) {
89                    Float lonmax = Nombre.latIntToFloat (jeu.getJeuLonmax ());
90                    description.setLon_max (Nombre.arrondir (lonmax.doubleValue (), 2));
91                }
92                if (jeu.getJeuAltmin () != null) {
93                    Float altmin = Nombre.altIntToFloat (jeu.getJeuAltmin ());
94                    description.setAlt_min (Nombre.arrondir (altmin.doubleValue (), 2));
95                }
96                if (jeu.getJeuAltmax () != null) {
97                    Float altmax = Nombre.altIntToFloat (jeu.getJeuAltmax ());
98                    description.setAlt_max (Nombre.arrondir (altmax.doubleValue (), 2));
99                }
100               
101                description.setNbFichiers(nbFichiers);
102                description.setNbMesures(nbMesures);
103                description.setJeuId(jeu.getJeuId());
104                return description;
105        }
106       
107        private Integration getIntegration (Jeu jeu, int nbFichiers) {
108                Integration integration = new Integration ();
109                ExactTime       inser           = new ExactTime ();
110                String          date            = DateTime.dateToString (jeu.getJeuDateinser ());
111               
112                inser.setDate (date.substring (0,10));
113                inser.setHeure (date.substring (11));
114                ExactTime modif = new ExactTime ();
115                if (jeu.getJeuDatemodif () != null) {
116                    date = DateTime.dateToString (jeu.getJeuDatemodif ());
117                    modif.setDate (date.substring (0,10));
118                        modif.setHeure (date.substring (11));
119                } else {
120                    modif.setDate (inser.getDate ());
121                        modif.setHeure (inser.getHeure ());
122                }
123               
124                integration.setDateD (inser);
125                integration.setDateF (modif);
126                integration.setNbFiles (nbFichiers);
127                integration.setJeuId(jeu.getJeuId());
128                integration.setResp (PersonneAdapter.dbPersonneToMetaContact (jeu.getPersonneByIntegrateurId ()));
129               
130                return integration;
131        }
132       
133        public void loadMetadata (MetadataForm metadataForm, String pathMetadata, String idJeu) 
134        throws MetadataException {
135            String nomJeu = null;
136            String destination = null;
137                // Si pathMetadata est un chemin
138                if (pathMetadata != null && pathMetadata.indexOf ("/") != -1) {
139                    nomJeu = pathMetadata.substring (pathMetadata.lastIndexOf ("/") + 1, pathMetadata.lastIndexOf ("_"));
140                    // Lecture du fichier de métadonnées
141                    Metadata meta = null;
142            try {
143                meta = new Metadata(pathMetadata);
144            } catch (IOException e) {
145                e.printStackTrace();
146            } catch (JDOMException e) {
147                e.printStackTrace();
148            } catch (ConversionException e) {
149                e.printStackTrace();
150            }
151            destination = meta.getPres().getDest();
152                }
153                // si pathMetadata est un nom de jeu
154                else if (pathMetadata!=null) {
155                    nomJeu = pathMetadata;
156                }
157            if (destination == null || destination.equals("SGBD")) {
158                loadMetadataFromDatabase(metadataForm, nomJeu, idJeu);
159            } else {
160                loadMetadataFromXMLFile(pathMetadata, metadataForm);
161            }
162        }
163       
164        public void loadMetadataFromDatabase (MetadataForm metadataForm, String nomJeu, String idJeu) throws MetadataException {
165                try {
166                        CommunicationBD.open();
167                        Jeu jeu = null;
168                        if (nomJeu != null) {
169                            jeu = (Jeu)CommunicationBD.getObjectByColonne(Jeu.class, "jeuNom", nomJeu);
170                        } 
171                        else if (idJeu != null) {
172                            Integer id = new Integer (idJeu);
173                            jeu = (Jeu)CommunicationBD.getObjectById(Jeu.class, id);
174                        }
175                       
176                        if (jeu != null) {
177
178                                RequeteNbvalsJeu requeteNb = (RequeteNbvalsJeu)CommunicationBD.getObjectById(RequeteNbvalsJeu.class, jeu.getJeuId ());
179                                int nbFichiers = 0, nbMesures = 0;
180                                if (requeteNb != null) {
181                                    nbFichiers = requeteNb.getNbFichiers ();
182                                    nbMesures = requeteNb.getNbMesures ();
183                                }
184
185                                Presentation presentation       = getPresentation (jeu);
186                                Description description         = getDescription(jeu, nbFichiers, nbMesures);
187                                Integration integration         = getIntegration (jeu, nbFichiers);
188                                Contenu contenu                 = new Contenu ();
189
190                                // Les plateformes
191                                //plateformes = CommunicationBD.getList("select req.plateforme from RequetePlateformeJeu as req where jeu_id = "+jeu.getJeuId());
192                                Set<Plateforme> plateformes = jeu.getPlateformes();
193                                contenu.setPlateformes(PlateformeAdapter.dbPlateformesToMetaPlateformes(plateformes));
194
195                                // Les paramÚtres
196                                //parametres = CommunicationBD.getList("select req.parametre from RequeteParametreJeu as req where jeu_id = "+jeu.getJeuId());
197                                Set<Parametre> parametres = jeu.getParametres();
198                                contenu.setParams(ParametreAdapter.dbParametresToMetaParametres(parametres));
199                               
200                                // Les capteurs
201                                //capteurs = CommunicationBD.getList("select req.capteur from RequeteCapteurJeu as req where jeu_id = "+jeu.getJeuId ());
202                                Set<Capteur> capteurs = jeu.getCapteurs();
203                                contenu.setCapteurs(CapteurAdapter.dbCapteursToMetaCapteurs(capteurs));
204                               
205                                this.metadata.setPres (presentation);
206                                this.metadata.setDesc (description);
207                                this.metadata.setInteg (integration);
208                                this.metadata.setContenuPrevu (contenu);
209                        }
210                        else {
211                            throw new MetadataException ("Aucune métadonnée pour le jeu " + nomJeu);
212                        }
213
214                        CommunicationBD.close();
215                        metadataForm.setMetadata (metadata);                                   
216                }
217                catch (HibernateException e) {                 
218                        // Libération de la connection à la base de données
219                        try {
220                                CommunicationBD.close();
221                        } catch (HibernateException ex) {
222                               
223                        }
224                }
225        }
226
227        public void loadMetadataFromXMLFile (String file, MetadataForm metadataForm) {
228                try {
229                    metadata = new Metadata (file);
230                        metadataForm.setMetadata(metadata);
231                }
232                catch (ConversionException e) {
233                        e.printStackTrace();
234                }
235                catch (IOException e) {
236                    e.printStackTrace();
237                }
238                catch (JDOMException e) {
239                        e.printStackTrace();
240                }
241        }
242       
243        public void updateMetadataInDatabase (MetadataForm metadataForm, String nomJeu) {
244                // TODO - Faire les vérifications sur les contacts (1 seul PI et 1 seul contact technique)
245               
246//              List<?>  jeux        = null;
247//              Jeu   jeu         = null;
248               
249                try {
250                        CommunicationBD.open();
251                       
252        //              Jeu jeu = (Jeu)CommunicationBD.getObjectByColonne(Jeu.class, "jeuNom", nomJeu);
253                       
254                        CommunicationBD.close();
255                }
256                catch (HibernateException e) {
257                        // Libération de la connection à la base de données
258                        try {
259                                CommunicationBD.close();
260                        } catch (HibernateException ex) {
261                               
262                        }
263                }
264        }
265       
266        public void updateMetadataInXMLFile (String file, MetadataForm metadaForm) {
267               
268        }
269       
270}
Note: See TracBrowser for help on using the repository browser.