source: tapas/service/implementation/com/ether/TapasServiceImpl.java @ 817

Last change on this file since 817 was 817, checked in by rboipsl, 9 years ago

changement logo
champs locality/lieu desactives

File size: 46.3 KB
RevLine 
[816]1    package com.ether;
[376]2
[816]3    import com.ether.dao.ObservatoryDAO;
4    import com.ether.dao.TapasRequestDAO;
5    import com.ether.tapas.Los;
6    import com.ether.tapas.Location;
7    import com.ether.tapas.Observation;
8    import com.ether.tapas.Observatory;
9    import com.ether.tapas.Preference;
10    import com.ether.tapas.Request;
11    import com.ether.tapas.Tapas;
12    import com.ether.tapas.TapasRequest;
13    import com.ether.user.User;
14    import org.apache.commons.logging.Log;
15    import org.apache.commons.logging.LogFactory;
16    import org.jdom.Attribute;
17    import org.jdom.Document;
18    import org.jdom.Element;
19    import org.jdom.output.Format;
20    import org.jdom.output.XMLOutputter;
21    import org.jetbrains.annotations.NotNull;
22    import org.jetbrains.annotations.Nullable;
23    import org.springframework.beans.factory.annotation.Required;
24    import org.springframework.transaction.annotation.Transactional;
[376]25
[816]26    import java.io.FileOutputStream;
27    import java.io.IOException;
28    import java.text.ParseException;
29    import java.text.SimpleDateFormat;
30    import java.util.ArrayList;
31    import java.util.Calendar;
32    import java.util.Date;
33    import java.util.Iterator;
34    import java.util.List;
35    import java.util.Properties;
36    import java.util.StringTokenizer;
[376]37
[816]38    //import java.sql.Date;
[509]39
[816]40    /**
41     * @author rboipsl
42     * @date 2 mars 2012
43     */
44    public class TapasServiceImpl
45            extends BackofficeServiceImpl
46            implements TapasService
47    {
48        @Transactional(readOnly = true)
49        public void createXMLRequest( @NotNull final Tapas fTapas, @NotNull final Properties prop )
50                throws ServiceException
51        {
52            final String servicePath = prop.getProperty( "service_path" );
53            //final String srcPath = prop.getProperty( "src_path" );
54            final String dataPath = prop.getProperty( "data_path" );
[389]55
[816]56            //final String fichier = servicePath + "/" + "request.xml";
57            final Element tapas = new Element( "tapas" );
58            final List<Element> listRequest = new ArrayList<Element>( fTapas.getRequests().size() );
[389]59
[816]60            //On crée un nouveau Document JDOM basé sur la racine que l'on vient de créer
61            final Document document = new Document( tapas );
[389]62
[816]63            //recuperation valeur id requete (bdd table tapas_request)
64            fTapas.setId( "Ether_TAPAS_" + String.valueOf( getNumRequest() ) );
[389]65
[816]66            final Attribute id = new Attribute( "Id", fTapas.getId() );
67            tapas.setAttribute( id );
[444]68
[816]69            //a terme devient une boucle
70            final List<Request> requests = fTapas.getRequests();
71            Request xRequest, nRequest;
[389]72
[816]73            String iname;
[389]74
[816]75            Iterator<Request> iter = requests.iterator();
[504]76
[816]77            int iii = requests.size();
78            int inr = 0;
79            int indice = 1;
80            //Request xRequest = iter.next();
81            String oneTransmission;
[389]82
[816]83            while ( indice <= iii )
84            {
85               nRequest = iter.next();
86               nRequest.setValid( 1 );
[504]87
[816]88               oneTransmission = nRequest.getPreference().getOneTransmission().getSecondValue();
[389]89
[816]90               if( oneTransmission.equalsIgnoreCase( "NO" ) )
91                {
92                    nRequest.setValid( -1 );
93                    if( nRequest.getPreference().getH2oExtinction().getSecondValue().equalsIgnoreCase( "Yes" ) )
94                    {
95                        requests.add( addNewRequest( nRequest, (iii+inr+1), "H2O") );
96                        inr = inr + 1;
97                        iter = requests.iterator();
98                        /*for ( int i=1 ; i <= (iii+inr) ; i++ )
99                        {
100                            nRequest = iter.next();
101                        }*/
102                    }
[504]103
[411]104
[816]105                    if( nRequest.getPreference().getCh4Extinction().getSecondValue().equalsIgnoreCase( "Yes" ) )
106                    {
107                        requests.add( addNewRequest( nRequest, (iii+inr+1), "CH4" ) );
108                        inr = inr + 1;
109                        iter = requests.iterator();
110                        /*for ( int i=1 ; i <= (iii+inr) ; i++ )
111                        {
112                            nRequest = iter.next();
113                        }*/
114                    }
[440]115
[466]116
[816]117                    if( nRequest.getPreference().getCo2Extinction().getSecondValue().equalsIgnoreCase( "Yes" ) )
118                    {
119                        requests.add( addNewRequest( nRequest, (iii+inr+1), "CO2" ) );
120                        inr = inr + 1;
121                        iter = requests.iterator();
122                        /*for ( int i=1 ; i <= (iii+inr) ; i++ )
123                        {
124                            nRequest = iter.next();
125                        }*/
126                    }
[466]127
[507]128
[816]129                    if( nRequest.getPreference().getN2oExtinction().getSecondValue().equalsIgnoreCase( "Yes" ) )
130                    {
131                        requests.add( addNewRequest( nRequest, (iii+inr+1), "N2O" ) );
132                        inr = inr + 1;
133                        iter = requests.iterator();
134                        /*for ( int i=1 ; i <= (iii+inr) ; i++ )
135                        {
136                            nRequest = iter.next();
137                        }*/
138                    }
[507]139
[466]140
[816]141                    if( nRequest.getPreference().getO2Extinction().getSecondValue().equalsIgnoreCase( "Yes" ) )
142                    {
143                        requests.add( addNewRequest( nRequest, (iii+inr+1), "O2" ) );
144                        iter = requests.iterator();
145                        inr = inr + 1;
146                        iter = requests.iterator();
147                        /*for ( int i=1 ; i <= (iii+inr) ; i++ )
148                        {
149                            nRequest = iter.next();
150                        }*/
151                    }
[767]152
153
[816]154                    if( nRequest.getPreference().getO3Extinction().getSecondValue().equalsIgnoreCase( "Yes" ) )
155                    {
156                        requests.add( addNewRequest( nRequest, (iii+inr+1), "O3" ) );
157                        iter = requests.iterator();
158                        inr = inr + 1;
159                        iter = requests.iterator();
160                        /*for ( int i=1 ; i <= (iii+inr) ; i++ )
161                        {
162                            nRequest = iter.next();
163                        }*/
164                    }
165                }
[779]166
[816]167                iter = requests.iterator();
168                indice=indice+1;
169                for (int i = 1 ; i < indice ; i++)
170                {
171                    nRequest = iter.next();
172                }
173            }
[440]174
175
[816]176            indice=1;
[529]177
[816]178            ////
179            for( final Request iRequest : requests )
180            {
181                if (iRequest.getValid() != -1) {
[529]182
[816]183                    final Element request = new Element( "request" );
184                    // Id de la request
185                    //final Attribute idR = new Attribute( "Id", (Long) iRequest.getId().toString() );
186                    final Attribute idR = new Attribute( "Id", String.valueOf(indice) );
187                    request.setAttribute( idR );
[529]188
[816]189                    // PREFERENCES
190                    final Element preferences = new Element( "preferences" );
[466]191
192
[816]193                    //format
194                    final Element format = new Element( "format" );
195                    //final Attribute validF = new Attribute( "valid", iRequest.getPreference().getFormat().getFirstValue() );
196                    final Attribute validF = new Attribute( "valid", "VO,ASCII,FITS,NETCDF" );
197                    format.setAttribute( validF );
198                    format.setText( iRequest.getPreference().getFormat().getSecondValue() );
199                    preferences.addContent( format );
200
201
202                    //rayleighExtinction
203                    final Element rayleighExtinction = new Element( "rayleigh_extinction" );
204                    //final Attribute validR = new Attribute( "valid", iRequest.getPreference().getRayleighExtinction().getFirstValue() );
205                    Attribute validR = new Attribute( "valid", "YES,NO" );
206                    rayleighExtinction.setAttribute( validR );
207                    rayleighExtinction.setText( iRequest.getPreference().getRayleighExtinction().getSecondValue() );
208                    preferences.addContent( rayleighExtinction );
209
210                    //h2o
211                    final Element h2oe = new Element( "h2o_extinction" );
212                    validR = new Attribute( "valid", "YES,NO" );
213                    h2oe.setAttribute( validR );
214                    h2oe.setText( iRequest.getPreference().getH2oExtinction().getSecondValue() );
215                    preferences.addContent( h2oe );
216
217
218                    //o3
219                    final Element o3e = new Element( "o3_extinction" );
220                    validR = new Attribute( "valid", "YES,NO" );
221                    o3e.setAttribute( validR );
222                    o3e.setText( iRequest.getPreference().getO3Extinction().getSecondValue() );
223                    preferences.addContent( o3e );
224
225                    //o2
226                    final Element o2e = new Element( "o2_extinction" );
227                    validR = new Attribute( "valid", "YES,NO" );
228                    o2e.setAttribute( validR );
229                    o2e.setText( iRequest.getPreference().getO2Extinction().getSecondValue() );
230                    preferences.addContent( o2e );
231
232
233                    //co2
234                    final Element co2e = new Element( "co2_extinction" );
235                    validR = new Attribute( "valid", "YES,NO" );
236                    co2e.setAttribute( validR );
237                    co2e.setText( iRequest.getPreference().getCo2Extinction().getSecondValue() );
238                    preferences.addContent( co2e );
239
240                    //ch4
241                    final Element ch4e = new Element( "ch4_extinction" );
242                    validR = new Attribute( "valid", "YES,NO" );
243                    ch4e.setAttribute( validR );
244                    ch4e.setText( iRequest.getPreference().getCh4Extinction().getSecondValue() );
245                    preferences.addContent( ch4e );
246
247                    //no2
248                    final Element n2oe = new Element( "n2o_extinction" );
249                    validR = new Attribute( "valid", "YES,NO" );
250                    n2oe.setAttribute( validR );
251                    n2oe.setText( iRequest.getPreference().getN2oExtinction().getSecondValue() );
252                    preferences.addContent( n2oe );
253
254
255                    request.addContent( preferences );
256
257                    // OBSERVATORIES
258                    final Element observation = new Element( "observation" );
259
260                    // date mesure
261                    final Element mdate = new Element( "date" );
262                    final Calendar calendar = Calendar.getInstance();
263
264                    calendar.setTimeInMillis( Long.valueOf( iRequest.getObservation().getDate() ) );
265                    final String dateFormatFinal=DateHelper.formatDate( calendar.getTime(), DateHelper.DATE_UTC );
266
267                    mdate.setText( dateFormatFinal );
268
269                    //mdate.setText( iRequest.getObservation().getDate() );
270                    observation.addContent( mdate );
271
272                    //observatoire ou lieu libre en indiquant alt,lat,lon
273                    final Element observatory = new Element( "observatory" );
274                    observation.addContent( observatory );
275
276                    //recuperation des coordonnées de l'observatoire
277                    final Observatory obs;
278                    try
279                    {
280                        //obs = _observatoryDAO.selectByPrimaryKey( iRequest.getObservation().getObservatory().getId() );
281                        obs = _observatoryDAO.getObservatoryByName( iRequest.getObservation().getObservatory().getName() );
282                    }
283                    catch( PersistenceException e )
284                    {
285                        throw new ServiceException( ServiceException.ServiceCode.OBSERVATORY_NOT_FOUND, e );
286                    }
287
288                    ////// a tester ////
289
290                    final Element name = new Element( "name" );
291
292                    final Element longitude = new Element( "longitude" );
293                    validR = new Attribute( "min", "-180" );
294                    longitude.setAttribute( validR );
295                    validR = new Attribute( "max", "180" );
296                    longitude.setAttribute( validR );
297
298                    final Element latitude = new Element( "latitude" );
299                    validR = new Attribute( "min", "-90" );
300                    latitude.setAttribute( validR );
301                    validR = new Attribute( "max", "90" );
302                    latitude.setAttribute( validR );
303
304                    final Element altitude = new Element( "altitude" );
305                    validR = new Attribute( "min", "0" );
306                    altitude.setAttribute( validR );
307                    validR = new Attribute( "max", "10000" );
308                    altitude.setAttribute( validR );
309
310                    String shortName="";
311
312                     if (null == obs)
313                    {
314                        //lieu libre
315                        name.setText(iRequest.getObservation().getLocation().getName());
316                        altitude.setText( iRequest.getObservation().getLocation().getAltitude().toString() );
317                        latitude.setText( iRequest.getObservation().getLocation().getLatitude().toString() );
318                        longitude.setText( iRequest.getObservation().getLocation().getLongitude().toString() );
319                        shortName = iRequest.getObservation().getLocation().getName();
320                    }
321                    else {
322                        //observatoire connu de la bdd tapas
323                        name.setText( obs.getName() );
324                        longitude.setText( obs.getLongitude().toString() );
325                        latitude.setText( obs.getLatitude().toString() );
326                        altitude.setText( obs.getAltitude().toString() );
327                        shortName = obs.getShortName().toString();
328                    }
329
330
331                    observatory.addContent( name );
332                    observatory.addContent( latitude );
333                    observatory.addContent( longitude );
334                    observatory.addContent( altitude );
335
336
337                    //los           addN
338                    final Element los = new Element( "los" );
339                    observation.addContent( los );
340
341                    Los temp = iRequest.getObservation().getLos();
342                    if ( !temp.getRaJ2000().equals( "" )) {
343                        final Element ra_j2000 = new Element( "ra_j2000" );
344                        ra_j2000.setText( iRequest.getObservation().getLos().getRaJ2000() );
345                        los.addContent( ra_j2000 );
346                    }
347
348                    if ( !temp.getDecJ2000().equals( "" )) {
349                        final Element dec_j2000 = new Element( "dec_j2000" );
350                        dec_j2000.setText( iRequest.getObservation().getLos().getDecJ2000() );
351                        los.addContent( dec_j2000 );
352                    }
353
354
355                    if ( !temp.getZenithAngle().equals("") ) {
356                        final Element zenith_angle = new Element( "zenith_angle" );
357
358                        Attribute a1 = new Attribute( "min", "0" );
359                        Attribute a2 = new Attribute( "max", "90" );
360
361                        zenith_angle.setAttribute( a1 );
362                        zenith_angle.setAttribute( a2 );
363                        zenith_angle.setText( temp.getZenithAngle() );
364
365                        los.addContent( zenith_angle );
366                    }
367
368
369                    //instrument
370                    final Element instrument = new Element( "instrument" );
371                    observation.addContent( instrument );
372
373                    final Element spectral_choice = new Element( "spectral_choice" );
374
375
376                    final String[] tabMess = iRequest.getObservation().getInstrument().getSpectralChoice().getFirstValue().split( "," );
377
378                    String chValid = "";
379                    int i;
380
381                    for( i = 0; i <= tabMess.length - 1; i++ ) //final String mess : tabMess )
382                    {
383                        if( i == tabMess.length - 1 )
384                            chValid += EtherHelper.getMessage( "label.spectralUnit." + tabMess[i], null );
385                        else chValid += EtherHelper.getMessage( "label.spectralUnit." + tabMess[i], null ) + ",";
386                    }
387
388
389                    Attribute a3 = new Attribute( "valid", chValid );
390
391                    spectral_choice.setAttribute( a3 );
392                    final String message = EtherHelper.getMessage( "label.spectralUnit." + iRequest.getObservation().getInstrument().getSpectralChoice().getSecondValue(), null );
393                    spectral_choice.setText( message );
394                    instrument.addContent( spectral_choice );
395
396                    final Element spectral_range = new Element( "spectral_range" );
397                    spectral_range.setText( iRequest.getObservation().getInstrument().getSpectralRange() );
398                    instrument.addContent( spectral_range );
399
400
401                    //
402                    final Element ilsfChoice = new Element( "ilsf_choice" );
403                    validR = new Attribute( "valid", iRequest.getObservation().getInstrument().getIlsfChoice().getFirstValue() );
404                    ilsfChoice.setAttribute( validR );
405                    //selectInstrumentalFunctions
406                    ilsfChoice.setText( String.valueOf( iRequest.getObservation().getInstrument().getIlsfChoice().getSecondValue() ) );
407                    instrument.addContent( ilsfChoice );
408
409
410                    final Element resolvingPower = new Element( "resolving_power" );
411                    validR = new Attribute( "min", "0" );
412                    resolvingPower.setAttribute( validR );
413                    resolvingPower.setText( String.valueOf( iRequest.getObservation().getInstrument().getResolvingPower().getSecondValue() ) );
414                    instrument.addContent( resolvingPower );
415
416                    final Element samplingRatio = new Element( "sampling_ratio" );
417                    validR = new Attribute( "min", "0" );
418                    samplingRatio.setAttribute( validR );
419                    samplingRatio.setText( String.valueOf( iRequest.getObservation().getInstrument().getSamplingRatio().getSecondValue() ) );
420                    instrument.addContent( samplingRatio );
421
422                    //berv correction
423                    //final Element bervCorr = new Element( "berv_correction" );
424                    final Element bervCorr = new Element( "apply_berv" );
425                    validR = new Attribute( "valid", "YES,NO" );
426                    bervCorr.setAttribute( validR );
427                    bervCorr.setText( iRequest.getPreference().getBervCorrection().getSecondValue() );
428                    instrument.addContent( bervCorr );
429
430
431                    request.addContent( observation );
432
433                    //atmosphere
434                    final Element atmosphere = new Element( "atmosphere" );
435                    request.addContent( atmosphere );
436
437                    //reference
438                    final Element reference = new Element( "reference" );
439                    validR = new Attribute( "valid", "0,1,2,3,4,5,6" );
440                    reference.setAttribute( validR );
441                    reference.setText( String.valueOf( iRequest.getAtmosphere().getReference().getSecondValue() ) );
442                    atmosphere.addContent( reference );
443
444
445                    String fileArletty = "", fileECMWF = "";
446
447
448                    fileArletty = dataPath + "/" + createFileName( dateFormatFinal , "arletty", shortName );
449                    fileECMWF = dataPath + "/" + createFileName( dateFormatFinal, "ecmwf", shortName );
450
451                    final Element arlettyFile = new Element( "arletty_file" );
452                    arlettyFile.setText( fileArletty );
453                    atmosphere.addContent( arlettyFile );
454
455                    final Element ecmwfFile = new Element( "ecmwf_file" );
456                    ecmwfFile.setText( fileECMWF );
457                    atmosphere.addContent( ecmwfFile );
458
459                    listRequest.add( request );
460
461                    indice=indice+1;
462                }
[466]463            }
[816]464
465
466
467            //////////
468            /////////
469            /*for( final Request iRequest : requests )
[466]470            {
[816]471                final Element request = new Element( "request" );
472                // Id de la request
473                final Attribute idR = new Attribute( "Id", iRequest.getId().toString() );
474                request.setAttribute( idR );
[466]475
[816]476                // PREFERENCES
477                final Element preferences = new Element( "preferences" );
[466]478
479
[816]480                //format
481                final Element format = new Element( "format" );
482                //final Attribute validF = new Attribute( "valid", iRequest.getPreference().getFormat().getFirstValue() );
483                final Attribute validF = new Attribute( "valid", "VO,ASCII,FITS,NETCDF" );
484                format.setAttribute( validF );
485                format.setText( iRequest.getPreference().getFormat().getSecondValue() );
486                preferences.addContent( format );
[466]487
488
[816]489                //rayleighExtinction
490                final Element rayleighExtinction = new Element( "rayleigh_extinction" );
491                //final Attribute validR = new Attribute( "valid", iRequest.getPreference().getRayleighExtinction().getFirstValue() );
492                Attribute validR = new Attribute( "valid", "YES,NO" );
493                rayleighExtinction.setAttribute( validR );
494                rayleighExtinction.setText( iRequest.getPreference().getRayleighExtinction().getSecondValue() );
495                preferences.addContent( rayleighExtinction );
[466]496
[816]497                //h2o
498                final Element h2oe = new Element( "h2o_extinction" );
499                validR = new Attribute( "valid", "YES,NO" );
500                h2oe.setAttribute( validR );
501                h2oe.setText( iRequest.getPreference().getH2oExtinction().getSecondValue() );
502                preferences.addContent( h2oe );
[466]503
504
[816]505                //o3
506                final Element o3e = new Element( "o3_extinction" );
507                validR = new Attribute( "valid", "YES,NO" );
508                o3e.setAttribute( validR );
509                o3e.setText( iRequest.getPreference().getO3Extinction().getSecondValue() );
510                preferences.addContent( o3e );
[466]511
[816]512                //o2
513                final Element o2e = new Element( "o2_extinction" );
514                validR = new Attribute( "valid", "YES,NO" );
515                o2e.setAttribute( validR );
516                o2e.setText( iRequest.getPreference().getO2Extinction().getSecondValue() );
517                preferences.addContent( o2e );
[466]518
519
[816]520                //co2
521                final Element co2e = new Element( "co2_extinction" );
522                validR = new Attribute( "valid", "YES,NO" );
523                co2e.setAttribute( validR );
524                co2e.setText( iRequest.getPreference().getCo2Extinction().getSecondValue() );
525                preferences.addContent( co2e );
[466]526
[816]527                //ch4
528                final Element ch4e = new Element( "ch4_extinction" );
529                validR = new Attribute( "valid", "YES,NO" );
530                ch4e.setAttribute( validR );
531                ch4e.setText( iRequest.getPreference().getCh4Extinction().getSecondValue() );
532                preferences.addContent( ch4e );
[466]533
[816]534                //no2
535                final Element n2oe = new Element( "n2o_extinction" );
536                validR = new Attribute( "valid", "YES,NO" );
537                n2oe.setAttribute( validR );
538                n2oe.setText( iRequest.getPreference().getN2oExtinction().getSecondValue() );
539                preferences.addContent( n2oe );
[466]540
541
[816]542                request.addContent( preferences );
[466]543
[816]544                // OBSERVATORIES
545                final Element observation = new Element( "observation" );
[466]546
[816]547                // date mesure
548                final Element mdate = new Element( "date" );
549                final Calendar calendar = Calendar.getInstance();
[470]550
[816]551                calendar.setTimeInMillis( Long.valueOf( iRequest.getObservation().getDate() ) );
552                final String dateFormatFinal=DateHelper.formatDate( calendar.getTime(), DateHelper.DATE_UTC );
[470]553
[816]554                mdate.setText( dateFormatFinal );
[470]555
[816]556    //            mdate.setText( iRequest.getObservation().getDate() );
557                observation.addContent( mdate );
[470]558
[816]559                //observatoire ou lieu libre en indiquant alt,lat,lon
560                final Element observatory = new Element( "observatory" );
561                observation.addContent( observatory );
[470]562
[816]563                //recuperation des coordonnées de l'observatoire
564                final Observatory obs;
565                try
566                {
567                    //obs = _observatoryDAO.selectByPrimaryKey( iRequest.getObservation().getObservatory().getId() );
568                    obs = _observatoryDAO.getObservatoryByName( iRequest.getObservation().getObservatory().getName() );
569                }
570                catch( PersistenceException e )
571                {
572                    throw new ServiceException( ServiceException.ServiceCode.OBSERVATORY_NOT_FOUND, e );
573                }
[470]574
[816]575                ////// a tester ////
[470]576
[816]577                final Element name = new Element( "name" );
[466]578
[816]579                final Element longitude = new Element( "longitude" );
580                validR = new Attribute( "min", "-180" );
581                longitude.setAttribute( validR );
582                validR = new Attribute( "max", "180" );
583                longitude.setAttribute( validR );
[466]584
[816]585                final Element latitude = new Element( "latitude" );
586                validR = new Attribute( "min", "-90" );
587                latitude.setAttribute( validR );
588                validR = new Attribute( "max", "90" );
589                latitude.setAttribute( validR );
[466]590
[816]591                final Element altitude = new Element( "altitude" );
592                validR = new Attribute( "min", "0" );
593                altitude.setAttribute( validR );
594                validR = new Attribute( "max", "10000" );
595                altitude.setAttribute( validR );
[470]596
[816]597                String shortName="";
[470]598
[816]599                if (null == obs)
600                {
601                    //lieu libre
602                    name.setText(iRequest.getObservation().getLocation().getName());
603                    altitude.setText( iRequest.getObservation().getLocation().getAltitude().toString() );
604                    latitude.setText( iRequest.getObservation().getLocation().getLatitude().toString() );
605                    longitude.setText( iRequest.getObservation().getLocation().getLongitude().toString() );
606                    shortName = iRequest.getObservation().getLocation().getName();
607                }
608                else {
609                    //observatoire connu de la bdd tapas
610                    name.setText( obs.getName() );
611                    longitude.setText( obs.getLongitude().toString() );
612                    latitude.setText( obs.getLatitude().toString() );
613                    altitude.setText( obs.getAltitude().toString() );
614                    shortName = obs.getShortName().toString();
615                }
[470]616
617
[816]618                observatory.addContent( name );
619                observatory.addContent( latitude );
620                observatory.addContent( longitude );
621                observatory.addContent( altitude );
[779]622
623
[816]624                //los           addN
625                final Element los = new Element( "los" );
626                observation.addContent( los );
[466]627
[816]628                Los temp = iRequest.getObservation().getLos();
629                if ( !temp.getRaJ2000().equals( "" )) {
630                    final Element ra_j2000 = new Element( "ra_j2000" );
631                    ra_j2000.setText( iRequest.getObservation().getLos().getRaJ2000() );
632                    los.addContent( ra_j2000 );
633                }
[470]634
[816]635                if ( !temp.getDecJ2000().equals( "" )) {
636                    final Element dec_j2000 = new Element( "dec_j2000" );
637                    dec_j2000.setText( iRequest.getObservation().getLos().getDecJ2000() );
638                    los.addContent( dec_j2000 );
639                }
[470]640
641
[816]642                if ( !temp.getZenithAngle().equals("") ) {
643                    final Element zenith_angle = new Element( "zenith_angle" );
[470]644
[816]645                    Attribute a1 = new Attribute( "min", "0" );
646                    Attribute a2 = new Attribute( "max", "90" );
[470]647
[816]648                    zenith_angle.setAttribute( a1 );
649                    zenith_angle.setAttribute( a2 );
650                    zenith_angle.setText( temp.getZenithAngle() );
[504]651
[816]652                    los.addContent( zenith_angle );
653                }
[529]654
[470]655
[816]656                //instrument
657                final Element instrument = new Element( "instrument" );
658                observation.addContent( instrument );
[470]659
[816]660                final Element spectral_choice = new Element( "spectral_choice" );
[470]661
[504]662
[816]663                final String[] tabMess = iRequest.getObservation().getInstrument().getSpectralChoice().getFirstValue().split( "," );
[490]664
[816]665                String chValid = "";
666                int i;
[507]667
[816]668                for( i = 0; i <= tabMess.length - 1; i++ ) //final String mess : tabMess )
669                {
670                    if( i == tabMess.length - 1 )
671                        chValid += EtherHelper.getMessage( "label.spectralUnit." + tabMess[i], null );
672                    else chValid += EtherHelper.getMessage( "label.spectralUnit." + tabMess[i], null ) + ",";
673                }
[440]674
[509]675
[816]676                Attribute a3 = new Attribute( "valid", chValid );
[444]677
[816]678                spectral_choice.setAttribute( a3 );
679                final String message = EtherHelper.getMessage( "label.spectralUnit." + iRequest.getObservation().getInstrument().getSpectralChoice().getSecondValue(), null );
680                spectral_choice.setText( message );
681                instrument.addContent( spectral_choice );
[507]682
[816]683                final Element spectral_range = new Element( "spectral_range" );
684                spectral_range.setText( iRequest.getObservation().getInstrument().getSpectralRange() );
685                instrument.addContent( spectral_range );
[507]686
687
[816]688                //
689                final Element ilsfChoice = new Element( "ilsf_choice" );
690                validR = new Attribute( "valid", iRequest.getObservation().getInstrument().getIlsfChoice().getFirstValue() );
691                ilsfChoice.setAttribute( validR );
692                //selectInstrumentalFunctions
693                ilsfChoice.setText( String.valueOf( iRequest.getObservation().getInstrument().getIlsfChoice().getSecondValue() ) );
694                instrument.addContent( ilsfChoice );
[507]695
696
[816]697                final Element resolvingPower = new Element( "resolving_power" );
698                validR = new Attribute( "min", "0" );
699                resolvingPower.setAttribute( validR );
700                resolvingPower.setText( String.valueOf( iRequest.getObservation().getInstrument().getResolvingPower().getSecondValue() ) );
701                instrument.addContent( resolvingPower );
[507]702
[816]703                final Element samplingRatio = new Element( "sampling_ratio" );
704                validR = new Attribute( "min", "0" );
705                samplingRatio.setAttribute( validR );
706                samplingRatio.setText( String.valueOf( iRequest.getObservation().getInstrument().getSamplingRatio().getSecondValue() ) );
707                instrument.addContent( samplingRatio );
[507]708
[816]709                //berv correction
710                //final Element bervCorr = new Element( "berv_correction" );
711                final Element bervCorr = new Element( "apply_berv" );
712                validR = new Attribute( "valid", "YES,NO" );
713                bervCorr.setAttribute( validR );
714                bervCorr.setText( iRequest.getPreference().getBervCorrection().getSecondValue() );
715                instrument.addContent( bervCorr );
[509]716
[507]717
[816]718                request.addContent( observation );
[507]719
[816]720                //atmosphere
721                final Element atmosphere = new Element( "atmosphere" );
722                request.addContent( atmosphere );
[504]723
[816]724                //reference
725                final Element reference = new Element( "reference" );
726                validR = new Attribute( "valid", "0,1,2,3,4,5,6" );
727                reference.setAttribute( validR );
728                reference.setText( String.valueOf( iRequest.getAtmosphere().getReference().getSecondValue() ) );
729                atmosphere.addContent( reference );
[480]730
731
[816]732                String fileArletty = "", fileECMWF = "";
[490]733
[507]734
[816]735                fileArletty = dataPath + "/" + createFileName( dateFormatFinal , "arletty", shortName );
736                fileECMWF = dataPath + "/" + createFileName( dateFormatFinal, "ecmwf", shortName );
[509]737
[816]738                final Element arlettyFile = new Element( "arletty_file" );
739                arlettyFile.setText( fileArletty );
740                atmosphere.addContent( arlettyFile );
[509]741
[816]742                final Element ecmwfFile = new Element( "ecmwf_file" );
743                ecmwfFile.setText( fileECMWF );
744                atmosphere.addContent( ecmwfFile );
[507]745
[816]746                listRequest.add( request );
747            }*/
[507]748
749
[509]750
[507]751
[816]752            /////
753            ///////
[507]754
[504]755
[490]756
[816]757            tapas.addContent( listRequest );
758
759            String commandePath = servicePath + "/" + fTapas.getId();
760            createXMLFile( document, commandePath, prop );
[509]761        }
[816]762
763        @Transactional(readOnly = true)
764        public void createUserFtpDir( @NotNull final User user, @NotNull final Properties prop )
765                throws ServiceException
[509]766        {
[816]767            final String usersPath = prop.getProperty( "users_path" );
768            String commande = "mkdir -p " + usersPath + "/" + user.getEmail();
[509]769
[816]770            try
771            {
772                EtherHelper.execProcess( commande );
773            }
774            catch( FormattedException e )
775            {
776            }
[509]777
[816]778            EtherHelper.pause( 4000 );
[509]779
[816]780            //commande = "chown -R "+prop.getProperty( "chown_user" )+" "+usersPath+"/"+user.getEmail();
781            commande = "chmod 777 -R " + usersPath + "/" + user.getEmail();
782
783            try
784            {
785                EtherHelper.execProcess( commande );
786            }
787            catch( FormattedException e )
788            {
789            }
790
791            EtherHelper.pause( 4000 );
[411]792        }
[816]793
794        public void createXMLFile( final Document document, final String commandePath, final Properties prop )
[480]795        {
[816]796            try
797            {
798                //creation repertoire commande
799                try
800                {
801                    EtherHelper.execProcess( "mkdir -p " + commandePath );
802                }
803                catch( FormattedException e )
804                {
805                }
[411]806
[816]807                EtherHelper.pause( 5000 );
[504]808
[389]809
[816]810                /*try
811                {
812                    EtherHelper.execProcess( "chown "+prop.getProperty( "chown_user" )+" "+commandePath);
813                }
814                catch( FormattedException e )
815                {
[470]816
[816]817                } */
[470]818
[816]819                final String fichier = commandePath + "/" + "request.xml";
[470]820
[816]821                //On utilise ici un affichage classique avec getPrettyFormat()
822                final XMLOutputter sortie = new XMLOutputter( Format.getPrettyFormat() );
823                //Remarquez qu'il suffit simplement de créer une instance de FileOutputStream
824                //avec en argument le nom du fichier pour effectuer la sérialisation.
825                sortie.output( document, new FileOutputStream( fichier ) );
[470]826
[816]827                /*try
828                {
829                    EtherHelper.execProcess( "chmod 755 "+commandePath+"/"+"request.xml" );
830                }
831                catch( FormattedException e )
832                {
[470]833
[816]834                }
[470]835
[816]836                EtherHelper.pause(7000);
837                */
838            }
839            catch( IOException ignored )
840            {
841            }
842        }
[470]843
[816]844        // liste tous observatoires
845        @Nullable
846        @Transactional(readOnly = true)
847        public List<Observatory> getAllObservatories()
848                throws ServiceException
849        {
850            try
851            {
852                return _observatoryDAO.getAllObservatories();
853            }
854            catch( PersistenceException e )
855            {
856                throw new ServiceException( ServiceException.ServiceCode.OBSERVATORY_NOT_FOUND, e );
857            }
858        }
[470]859
[816]860        // recuperation infos observatoire avec l'id
861        @Nullable
862        @Transactional(readOnly = true)
863        public Observatory getObservatoryById( @NotNull final long id )
864                throws ServiceException
[524]865        {
[816]866            try
867            {
868                return _observatoryDAO.getObservatoryById( id );
869            }
870            catch( PersistenceException e )
871            {
872                throw new ServiceException( ServiceException.ServiceCode.OBSERVATORY_NOT_FOUND, e );
873            }
[504]874        }
[816]875
876        // recuperation infos observatoire avec le nom
877        @Nullable
878        @Transactional(readOnly = true)
879        public Observatory getObservatoryByName( @NotNull final String name )
880                throws ServiceException
[524]881        {
[816]882            try
883            {
884                return _observatoryDAO.getObservatoryByName( name );
885            }
886            catch( PersistenceException e )
887            {
888                throw new ServiceException( ServiceException.ServiceCode.OBSERVATORY_NOT_FOUND, e );
889            }
890        }
[470]891
[816]892        // recuperation infos observatoire avec le nom
893        @Nullable
894        @Transactional(readOnly = true)
895        public Long getNumRequest()
896                throws ServiceException
897        {
898            try
899            {
900                return _tapasRequestDAO.getNumRequest() + 1;
901            }
902            catch( PersistenceException e )
903            {
904                throw new ServiceException( ServiceException.ServiceCode.REQUEST_NOT_FOUND, e );
905            }
[504]906        }
[470]907
[816]908        /*   public String findMeasureHour( String date )
909    {
910    String heure;
911    int val;
[470]912
[816]913
914    val = Math.abs( Integer.parseInt( heure ) );
915
916    return heure;
917    }                    */
918
919        public String createFileName( String date, String type, String nomCourt )
[524]920        {
[816]921            String fileName = "";
[470]922
[816]923            String annee = date.substring( 0, 4 );
924            //String mois = String.valueOf( Integer.parseInt(date.substring(5,7))-1);
925            String mois = date.substring( 5, 7 );
926            String heure = date.substring( 11, 13 );
927            String jour = date.substring( 8, 10 );
928            String ind = "";
[470]929
[816]930            Calendar cal = Calendar.getInstance();
[470]931
[816]932            try
933            {
934                Date dd = new SimpleDateFormat( "dd/MM/yyyy HH:mm:ss" ).parse( jour + "/" + mois + "/" + annee + " " + heure + ":00:00" );
935                cal.setTime( dd );
936            }
937            catch( ParseException e )
938            {
939            }
[470]940
[816]941            int iheure = cal.get( Calendar.HOUR_OF_DAY );// + 2;
[504]942
[816]943            if( iheure < 3 ) ind = "00";
944            else if( iheure >= 21 )
945            {
946                ind = "00";
947                cal.add( Calendar.DAY_OF_MONTH, +1 );
948            }
949            else if( Math.abs( 6 - iheure ) < 3 || iheure == 3 ) ind = "06";
950            else if( Math.abs( 12 - iheure ) < 3 || iheure == 9 ) ind = "12";
951            else if( Math.abs( 18 - iheure ) < 3 || iheure == 15 ) ind = "18";
[504]952
[816]953            //+1 sur le mois pour recuperer le numero correct du mois considere
954            if( cal.get( Calendar.MONTH ) + 1 < 10 ) mois = "0" + String.valueOf( cal.get( Calendar.MONTH ) + 1 );
955            else mois = "" + String.valueOf( cal.get( Calendar.MONTH ) + 1 );
[470]956
[816]957            if( cal.get( Calendar.DAY_OF_MONTH ) < 10 ) jour = "0" + cal.get( Calendar.DAY_OF_MONTH );
958            else jour = "" + cal.get( Calendar.DAY_OF_MONTH );
[470]959
[816]960            annee = "" + cal.get( Calendar.YEAR );
[767]961
[816]962            //fileName = "/" + annee + "/" + mois + "/" + jour + "/" + nomCourt + "_" + annee + mois + jour + ind;
963            fileName="/"+type+"/" + nomCourt + "_" + annee + mois + jour + ind;
964
965            if( type == "arletty" ) fileName = fileName + ".arl";
966            else if( type == "ecmwf" ) fileName = fileName + "_qo3.txt";
967
968            return fileName;
969        }
970
971        public boolean execAppelQSub( final String repCommande, User user, Properties prop )
972                throws ServiceException
[389]973        {
[816]974            String commande = "";
975            String commandePath = prop.getProperty( "service_path" ) + "/" + repCommande + "/";
976
977            //copie run qsub dans repertoire commande
978            commande = "cp " + prop.getProperty( "src_path" ) + "/" + prop.getProperty( "process_name" ) + " " + commandePath;
979
[490]980            try
981            {
[816]982                EtherHelper.execProcess( commande );
[490]983            }
984            catch( FormattedException e )
985            {
[816]986            }
[490]987
[816]988            EtherHelper.pause( 7000 );
989
990            //final String fichier = commandePath+"commande.txt";
991
992            /*commande="chown -R www:www "+commandePath;
993
994            try
995            {
996                EtherHelper.execProcess( commande );
[490]997            }
[816]998            catch( FormattedException e )
999            {
[490]1000
[816]1001            }*/
[504]1002
[816]1003            EtherHelper.pause( 7000 );
[509]1004
[816]1005            commande = "chmod 777 -R " + commandePath;
1006
1007            try
[509]1008            {
[816]1009                EtherHelper.execProcess( commande );
[509]1010            }
1011            catch( FormattedException e )
1012            {
[816]1013            }
[509]1014
[816]1015            EtherHelper.pause( 7000 );
1016
1017            //execution qsub pour la commande en cours
1018            commande = commandePath + "/" + prop.getProperty( "process_name" ) + " -f request.xml" + " -m " + user.getEmail();
1019
1020            /*PrintWriter out  = null;
1021            try
1022            {
1023                out = new PrintWriter(new FileWriter(fichier));
1024                out.println(commande);
1025                out.close();
1026            }
1027            catch( IOException e )
1028            {
1029                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
[509]1030            } */
1031
[490]1032
[816]1033            /*commande = commandePath+"/"+prop.getProperty( "process_name" );
1034            commande = "cd "+commandePath;*/
[504]1035
[816]1036            try
[507]1037            {
[816]1038                EtherHelper.execProcess( commande );
[507]1039            }
1040            catch( FormattedException e )
1041            {
[816]1042                throw new ServiceException( ServiceException.ServiceCode.ERROR_PROCESS, e );
[507]1043            }
1044
[816]1045            EtherHelper.pause( 7000 );
[507]1046
[816]1047            return true;
[389]1048        }
[816]1049
1050        @NotNull
1051        @Transactional(rollbackFor = Exception.class)
1052        public Long insertTapasRequest( @NotNull final User user )
1053                throws ServiceException
[389]1054        {
[816]1055            try
1056            {
1057                String dd = "1";
1058
1059                final TapasRequest ntp = new TapasRequest( user.getFirstName(), user.getLastName(), dd, _tapasRequestDAO.getNumRequest() + 1 );
1060                return _tapasRequestDAO.insert( ntp );
1061            }
1062            catch( PersistenceException e )
1063            {
1064                throw new ServiceException( ServiceException.ServiceCode.PERSISTENCE, e );
1065            }
[389]1066        }
1067
[816]1068        @Transactional(readOnly = true)
1069        public void updateRequestGasTrace(Request iRequest, Pair<String, String> oldValuesCO2, Pair<String, String> oldValuesO2, Pair<String, String> oldValuesO3, Pair<String, String> oldValuesCH4, Pair<String, String> oldValuesN2O, Pair<String, String> oldValuesH20)
1070                throws ServiceException
[376]1071        {
[816]1072            iRequest.getPreference().setCo2Extinction(oldValuesCO2);
1073            iRequest.getPreference().setO2Extinction(oldValuesO2);
1074            iRequest.getPreference().setO3Extinction(oldValuesO3);
1075            iRequest.getPreference().setCh4Extinction(oldValuesCH4);
1076            iRequest.getPreference().setN2oExtinction(oldValuesN2O);
1077            iRequest.getPreference().setH2oExtinction(oldValuesH20);
[376]1078        }
1079
[816]1080        @Transactional(readOnly = true)
1081        public Request addNewRequest( @NotNull Request iRequest, @NotNull int id, String specie)
1082                throws ServiceException
[447]1083        {
[816]1084            Request cpRequest = new Request();
1085            cpRequest.setId( new Long(id) );
[376]1086
[816]1087            cpRequest.setPreference( new Preference() );
[447]1088
[816]1089            cpRequest.getPreference().setCo2Extinction( new Pair<String, String>("YES,NO","NO") );
1090            cpRequest.getPreference().setO2Extinction( new Pair<String, String>( "YES,NO", "NO" ) );
1091            cpRequest.getPreference().setO3Extinction( new Pair<String, String>( "YES,NO", "NO" ) );
1092            cpRequest.getPreference().setCh4Extinction( new Pair<String, String>( "YES,NO", "NO" ) );
1093            cpRequest.getPreference().setN2oExtinction( new Pair<String, String>( "YES,NO", "NO" ) );
1094            cpRequest.getPreference().setH2oExtinction( new Pair<String, String>( "YES,NO", "NO" ) );
[480]1095
1096
[816]1097            cpRequest.getPreference().setBervCorrection( iRequest.getPreference().getBervCorrection() );
1098            cpRequest.getPreference().setFormat( iRequest.getPreference().getFormat() );
1099            cpRequest.getPreference().setRayleighExtinction( iRequest.getPreference().getRayleighExtinction() );
[480]1100
1101
[816]1102            cpRequest.setAtmosphere( iRequest.getAtmosphere() );
1103            cpRequest.setObservation( iRequest.getObservation() );
1104
1105            if ( specie.equalsIgnoreCase("CO2") ) cpRequest.getPreference().setCo2Extinction( new Pair<String, String>( "YES,NO","YES" ) );
1106            if ( specie.equalsIgnoreCase("O2") ) cpRequest.getPreference().setO2Extinction( new Pair<String, String>( "YES,NO","YES" ) );
1107            if ( specie.equalsIgnoreCase("O3") ) cpRequest.getPreference().setO3Extinction( new Pair<String, String>( "YES,NO","YES" ) );
1108            if ( specie.equalsIgnoreCase("CH4") ) cpRequest.getPreference().setCh4Extinction( new Pair<String, String>( "YES,NO","YES" ) );
1109            if ( specie.equalsIgnoreCase("N2O") ) cpRequest.getPreference().setN2oExtinction( new Pair<String, String>( "YES,NO","YES" ) );
1110            if ( specie.equalsIgnoreCase("H2O") ) {
1111                cpRequest.getPreference().setH2oExtinction( new Pair<String, String>( "YES,NO","YES" ) );
1112            }
1113
1114            return cpRequest;
[447]1115        }
[816]1116
1117        @Required
1118        public void setObservatoryDAO( final ObservatoryDAO observatoryDAO )
[447]1119        {
[816]1120            _observatoryDAO = observatoryDAO;
[447]1121        }
1122
[816]1123        @Required
1124        public void setTapasRequestDAO( final TapasRequestDAO tapasRequestDAO )
[779]1125        {
[816]1126            _tapasRequestDAO = tapasRequestDAO;
[779]1127        }
1128
[816]1129        private static final Log LOGGER = LogFactory.getLog( TapasServiceImpl.class );
[779]1130
[816]1131        private ObservatoryDAO _observatoryDAO;
[376]1132
[816]1133        private TapasRequestDAO _tapasRequestDAO;
[447]1134    }
Note: See TracBrowser for help on using the repository browser.