- Timestamp:
- 10/09/12 14:50:26 (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
ether_statistics/service/implementation/com/ether/EtherServiceImpl.java
r569 r572 1 1 package com.ether; 2 2 3 import com.ether.dao.JeuDAO;4 import com.ether.dao.McoDAO;5 import com.ether.dao.MeasureDAO;6 import com.ether.dao.ParameterDAO;7 import com.ether.dao.PersonDAO;8 import com.ether.dao.PlateformDAO;9 import com.ether.dao.UserDAO;10 import com.ether.dao.ValueDAO;11 import com.ether.dao.simulation.AttributeDAO;12 import com.ether.dao.simulation.DimensionDAO;13 import com.ether.dao.simulation.SimulationDAO;14 import com.ether.dao.simulation.VariableDAO;15 import com.ether.mco.Mco;16 import com.ether.mco.McoFilter;17 import com.ether.simulation.Attribute;18 import com.ether.simulation.Dimension;19 import com.ether.simulation.Simulation;20 import com.ether.simulation.SimulationFilter;21 import com.ether.simulation.Variable;22 import com.ether.user.User;23 import com.ether.user.UserFilter;24 import com.ether.user.UserState;25 import com.medias.annuaire.Personne;26 import com.medias.database.objects.Jeu;27 import com.medias.database.objects.Parametre;28 import com.medias.database.objects.Plateforme;29 3 import org.apache.commons.logging.Log; 30 4 import org.apache.commons.logging.LogFactory; 31 import org.jetbrains.annotations.NotNull;32 import org.jetbrains.annotations.Nullable;33 import org.springframework.beans.factory.annotation.Required;34 import org.springframework.transaction.annotation.Transactional;35 36 import javax.xml.bind.JAXBContext;37 import javax.xml.bind.JAXBException;38 import javax.xml.bind.Marshaller;39 import javax.xml.bind.PropertyException;40 import javax.xml.bind.Unmarshaller;41 import java.io.BufferedReader;42 import java.io.File;43 import java.io.IOException;44 import java.io.InputStreamReader;45 import java.util.ArrayList;46 import java.util.Collections;47 import java.util.Date;48 import java.util.HashSet;49 import java.util.List;50 import java.util.Set;51 5 52 6 /** … … 57 11 implements EtherService 58 12 { 59 @Nullable 60 @Transactional(readOnly = true) 61 public List<Plateforme> getAllPlateforms() 62 throws ServiceException 63 { 64 try 65 { 66 return _plateformDAO.getAllPlateforms(); 67 } 68 catch( PersistenceException e ) 69 { 70 throw new ServiceException( ServiceException.ServiceCode.PLATEFORM_NOT_FOUND, e ); 71 } 72 } 73 74 @Nullable 75 @Transactional(readOnly = true) 76 public Plateforme getPlateformById( @Nullable final Integer plateformId ) 77 throws ServiceException 78 { 79 if( null == plateformId ) 80 return null; 81 try 82 { 83 return _plateformDAO.getPlateformById( plateformId ); 84 } 85 catch( PersistenceException e ) 86 { 87 throw new ServiceException( ServiceException.ServiceCode.PLATEFORM_NOT_FOUND, e ); 88 } 89 } 90 91 @Nullable 92 @Transactional(readOnly = true) 93 public Parametre getParameterById( @Nullable final Integer parameterId ) 94 throws ServiceException 95 { 96 if( null == parameterId ) 97 return null; 98 try 99 { 100 return _parameterDAO.getParameterById( parameterId ); 101 } 102 catch( PersistenceException e ) 103 { 104 throw new ServiceException( ServiceException.ServiceCode.PARAMETER_NOT_FOUND, e ); 105 } 106 } 107 108 @NotNull 109 @Transactional(readOnly = true) 110 public List<Parametre> getParametersByPlateformId( @NotNull final Integer plateformId ) 111 throws ServiceException 112 { 113 try 114 { 115 return _parameterDAO.getParametersByPlateformId( plateformId ); 116 } 117 catch( PersistenceException e ) 118 { 119 throw new ServiceException( ServiceException.ServiceCode.PARAMETER_NOT_FOUND, e ); 120 } 121 } 122 123 @NotNull 124 @Transactional(readOnly = true) 125 public <T1, T2, T3> Data<T1[], T2[], T3[]> getListsByPlateformByParameterByPeriodForTimeSerie( @NotNull final Integer plateformId, @NotNull final Integer parameterId, @Nullable final Date dateBegin, @Nullable final Date dateEnd ) 126 throws ServiceException 127 { 128 try 129 { 130 return _valueDAO.getListsByPlateformByParameterByPeriodForTimeSerie( plateformId, parameterId, dateBegin, dateEnd ); 131 } 132 catch( PersistenceException e ) 133 { 134 throw new ServiceException( ServiceException.ServiceCode.VALUE_NOT_FOUND, e ); 135 } 136 } 137 138 @NotNull 139 @Transactional(readOnly = true) 140 public <T1, T2, T3> Data<List<T1>, List<T2>, List<T3>> getListsByPlateformByParameterByPeriodFor2D( @NotNull final Integer plateformId, @NotNull final Integer parameterId, @Nullable final Date dateBegin, @Nullable final Date dateEnd ) 141 throws ServiceException 142 { 143 try 144 { 145 return _valueDAO.getListsByPlateformByParameterByPeriodFor2D( plateformId, parameterId, dateBegin, dateEnd ); 146 } 147 catch( PersistenceException e ) 148 { 149 throw new ServiceException( ServiceException.ServiceCode.VALUE_NOT_FOUND, e ); 150 } 151 } 152 153 @NotNull 154 @Transactional(readOnly = true) 155 public <T1, T2, T3> Data<T1[], T2[], T3[]> getArraysByPlateformByParameterByPeriodFor2D( @NotNull final Integer plateformId, @NotNull final Integer parameterId, @Nullable final Date dateBegin, @Nullable final Date dateEnd ) 156 throws ServiceException 157 { 158 try 159 { 160 return _valueDAO.getArraysByPlateformByParameterByPeriodFor2D( plateformId, parameterId, dateBegin, dateEnd ); 161 } 162 catch( PersistenceException e ) 163 { 164 throw new ServiceException( ServiceException.ServiceCode.VALUE_NOT_FOUND, e ); 165 } 166 } 167 168 @NotNull 169 @Transactional(readOnly = true) 170 public Integer getNumberValuesByPlateformByParameterByPeriod( @NotNull final List<Pair<Integer, Integer>> pairs, @Nullable final Date beginDate, @Nullable final Date endDate ) 171 throws ServiceException 172 { 173 try 174 { 175 Integer number = 0; 176 for( final Pair pair : pairs ) 177 number += _valueDAO.getNumberValuesByPlateformByParameterByPeriod( (Integer) pair.getFirstValue(), (Integer) pair.getSecondValue(), beginDate, endDate ); 178 179 return number; 180 } 181 catch( PersistenceException e ) 182 { 183 throw new ServiceException( ServiceException.ServiceCode.VALUE_NOT_FOUND, e ); 184 } 185 } 186 187 @Nullable 188 @Transactional(readOnly = true) 189 public Date getFirstDate() 190 throws ServiceException 191 { 192 try 193 { 194 return _measureDAO.getFirstDate(); 195 } 196 catch( PersistenceException e ) 197 { 198 throw new ServiceException( ServiceException.ServiceCode.DATE_NOT_FOUND, e ); 199 } 200 } 201 202 @Nullable 203 @Transactional(readOnly = true) 204 public Date getLastDate() 205 throws ServiceException 206 { 207 try 208 { 209 return _measureDAO.getLastDate(); 210 } 211 catch( PersistenceException e ) 212 { 213 throw new ServiceException( ServiceException.ServiceCode.DATE_NOT_FOUND, e ); 214 } 215 } 216 217 @Nullable 218 @Transactional(readOnly = true) 219 public User getUserById( @NotNull final Integer userId ) 220 throws ServiceException 221 { 222 try 223 { 224 return _userDAO.selectByPrimaryKey( userId ); 225 } 226 catch( PersistenceException e ) 227 { 228 throw new ServiceException( ServiceException.ServiceCode.USER_NOT_FOUND, e ); 229 } 230 } 231 232 @Nullable 233 @Transactional(readOnly = true) 234 public User getUserByEmail( @NotNull final String userEmail ) 235 throws ServiceException 236 { 237 try 238 { 239 return _userDAO.getUserByEmail( userEmail ); 240 } 241 catch( PersistenceException e ) 242 { 243 throw new ServiceException( ServiceException.ServiceCode.USER_NOT_FOUND, e ); 244 } 245 } 246 247 @NotNull 248 @Transactional(rollbackFor = Exception.class) 249 public Integer createUser( @NotNull final User user ) 250 throws ServiceException 251 { 252 try 253 { 254 return _userDAO.insert( user ); 255 } 256 catch( PersistenceException e ) 257 { 258 throw new ServiceException( ServiceException.ServiceCode.PERSISTENCE, e ); 259 } 260 } 261 262 @NotNull 263 @Transactional(readOnly = true) 264 public List<User> getAllUsers() 265 throws ServiceException 266 { 267 try 268 { 269 return _userDAO.selectAll(); 270 } 271 catch( PersistenceException e ) 272 { 273 throw new ServiceException( ServiceException.ServiceCode.USER_NOT_FOUND, e ); 274 } 275 } 276 277 @NotNull 278 @Transactional(readOnly = true) 279 public List<User> getAllUsersByNameOrder() 280 throws ServiceException 281 { 282 try 283 { 284 return _userDAO.getAllUsersByNameOrder(); 285 } 286 catch( PersistenceException e ) 287 { 288 throw new ServiceException( ServiceException.ServiceCode.USER_NOT_FOUND, e ); 289 } 290 } 291 292 @NotNull 293 @Transactional(readOnly = true) 294 public List<User> getUsersByState( @NotNull final UserState userState ) 295 throws ServiceException 296 { 297 try 298 { 299 return _userDAO.getUsersByState( userState ); 300 } 301 catch( PersistenceException e ) 302 { 303 throw new ServiceException( ServiceException.ServiceCode.USER_NOT_FOUND, e ); 304 } 305 } 306 307 @Transactional(rollbackFor = Exception.class) 308 public void removeUserById( @NotNull final Integer userId ) 309 throws ServiceException 310 { 311 try 312 { 313 _userDAO.deleteByPrimaryKey( userId ); 314 } 315 catch( PersistenceException e ) 316 { 317 throw new ServiceException( ServiceException.ServiceCode.PERSISTENCE, e ); 318 } 319 } 320 321 @Transactional(rollbackFor = Exception.class) 322 public void updateUser( @NotNull final User user ) 323 throws ServiceException 324 { 325 try 326 { 327 _userDAO.update( user ); 328 } 329 catch( PersistenceException e ) 330 { 331 throw new ServiceException( ServiceException.ServiceCode.PERSISTENCE, e ); 332 } 333 } 334 335 @Transactional(rollbackFor = Exception.class) 336 public void acceptOrRefuseUser( @NotNull final Integer userId, final boolean isAccepted ) 337 throws ServiceException 338 { 339 try 340 { 341 final User user = _userDAO.selectByPrimaryKey( userId ); 342 if( isAccepted ) 343 user.setState( UserState.ACCEPTED ); 344 else 345 user.setState( UserState.REFUSED ); 346 _userDAO.update( user ); 347 } 348 catch( PersistenceException e ) 349 { 350 throw new ServiceException( ServiceException.ServiceCode.PERSISTENCE, e ); 351 } 352 } 353 354 @Nullable 355 @Transactional(readOnly = true) 356 public Personne getPeopleByEmail( @NotNull final String email ) 357 throws ServiceException 358 { 359 try 360 { 361 return _personDAO.getPersonByEmail( email ); 362 } 363 catch( PersistenceException e ) 364 { 365 throw new ServiceException( ServiceException.ServiceCode.PERSON_NOT_FOUND, e ); 366 } 367 } 368 369 @Transactional(readOnly = true) 370 public PaginatedResult<User> searchUsers( @NotNull final UserFilter filter ) 371 throws ServiceException 372 { 373 try 374 { 375 return _userDAO.search( filter ); 376 } 377 catch( PersistenceException e ) 378 { 379 throw new ServiceException( ServiceException.ServiceCode.USER_NOT_FOUND, e ); 380 } 381 } 382 383 @Transactional(readOnly = true) 384 public PaginatedResult<Mco> searchMcos( @NotNull final McoFilter filter ) 385 throws ServiceException 386 { 387 try 388 { 389 return _mcoDAO.search( filter ); 390 } 391 catch( PersistenceException e ) 392 { 393 throw new ServiceException( ServiceException.ServiceCode.REQUEST_NOT_FOUND, e ); 394 } 395 } 396 397 @Nullable 398 @Transactional(readOnly = true) 399 public Mco getMcoByCode( @NotNull final String code ) 400 throws ServiceException 401 { 402 try 403 { 404 return _mcoDAO.getMcoByCode( code ); 405 } 406 catch( PersistenceException e ) 407 { 408 throw new ServiceException( ServiceException.ServiceCode.REQUEST_NOT_FOUND, e ); 409 } 410 } 411 412 @NotNull 413 @Transactional(rollbackFor = Exception.class) 414 public Integer createMco( @NotNull final Mco mco ) 415 throws ServiceException 416 { 417 try 418 { 419 return _mcoDAO.insert( mco ); 420 } 421 catch( PersistenceException e ) 422 { 423 throw new ServiceException( ServiceException.ServiceCode.PERSISTENCE, e ); 424 } 425 } 426 427 @Transactional(rollbackFor = Exception.class) 428 public void removeMcoById( @NotNull final Integer mcoId ) 429 throws ServiceException 430 { 431 try 432 { 433 _mcoDAO.deleteByPrimaryKey( mcoId ); 434 } 435 catch( PersistenceException e ) 436 { 437 throw new ServiceException( ServiceException.ServiceCode.PERSISTENCE, e ); 438 } 439 } 440 441 @Transactional(rollbackFor = Exception.class) 442 public void updateMco( @NotNull final Mco mco ) 443 throws ServiceException 444 { 445 try 446 { 447 _mcoDAO.update( mco ); 448 } 449 catch( PersistenceException e ) 450 { 451 throw new ServiceException( ServiceException.ServiceCode.PERSISTENCE, e ); 452 } 453 } 454 455 @Nullable 456 @Transactional(readOnly = true) 457 public Mco getMcoById( @NotNull final Integer requestId ) 458 throws ServiceException 459 { 460 try 461 { 462 return _mcoDAO.selectByPrimaryKey( requestId ); 463 } 464 catch( PersistenceException e ) 465 { 466 throw new ServiceException( ServiceException.ServiceCode.REQUEST_NOT_FOUND, e ); 467 } 468 } 469 470 /** 471 * This method manage the sub-menus for the list of parameters (to avoid multiple "Particle Concentration" by example) 472 * The fullParameters must be ordered with parameterName ascendant 473 * 474 * @param fullParameters 475 * @return 476 */ 477 @NotNull 478 public List<List<Parametre>> manageMenusForParameterList( @NotNull final List<Parametre> fullParameters ) 479 { 480 final List<List<Parametre>> parameterListWithMenu = new ArrayList<List<Parametre>>(); 481 482 int i = 0; 483 while( i < fullParameters.size() ) 484 { 485 final Parametre fullParameter = fullParameters.get( i ); 486 final int firstIndex = fullParameters.indexOf( fullParameter ); 487 final int lastIndex = fullParameters.lastIndexOf( fullParameter ); 488 final List<Parametre> parameters = fullParameters.subList( firstIndex, lastIndex + 1 ); 489 parameterListWithMenu.add( parameters ); 490 i += parameters.size(); 491 } 492 493 return parameterListWithMenu; 494 } 495 496 @Nullable 497 @Transactional(readOnly = true) 498 public List<Parametre> getAllParametersOrderByCategoryByName() 499 throws ServiceException 500 { 501 try 502 { 503 return _parameterDAO.getAllParametersOrderByCategoryByName(); 504 } 505 catch( PersistenceException e ) 506 { 507 throw new ServiceException( ServiceException.ServiceCode.PARAMETER_NOT_FOUND, e ); 508 } 509 } 510 511 @Nullable 512 @Transactional(readOnly = true) 513 public List<Date> getAllMeasureDateByPlateformId( @NotNull final Integer plateformId ) 514 throws ServiceException 515 { 516 try 517 { 518 return _measureDAO.getAllMeasureDateByPlateformId( plateformId ); 519 } 520 catch( PersistenceException e ) 521 { 522 throw new ServiceException( ServiceException.ServiceCode.DATE_NOT_FOUND, e ); 523 } 524 } 525 526 @Nullable 527 @Transactional(readOnly = true) 528 public PaginatedResult<Jeu> searchJeux( @NotNull final JeuFilter filter ) 529 throws ServiceException 530 { 531 try 532 { 533 return _jeuDAO.search( filter ); 534 } 535 catch( PersistenceException e ) 536 { 537 throw new ServiceException( ServiceException.ServiceCode.JEU_NOT_FOUND, e ); 538 } 539 } 540 541 @Transactional(rollbackFor = Exception.class) 542 public void removeJeuAndDataByName( @NotNull final String datasetName ) 543 throws ServiceException 544 { 545 try 546 { 547 _jeuDAO.removeJeuAndDataByName( datasetName ); 548 } 549 catch( PersistenceException e ) 550 { 551 throw new ServiceException( ServiceException.ServiceCode.PERSISTENCE, e ); 552 } 553 } 554 555 @Transactional(rollbackFor = Exception.class) 556 public void cleanLocalisationAndFlagTables() 557 throws ServiceException 558 { 559 try 560 { 561 _jeuDAO.cleanLocalisationAndFlagTables(); 562 } 563 catch( PersistenceException e ) 564 { 565 throw new ServiceException( ServiceException.ServiceCode.PERSISTENCE, e ); 566 } 567 } 568 569 @Nullable 570 @Transactional(readOnly = true) 571 public Jeu getJeuById( @NotNull final Integer jeuId ) 572 throws ServiceException 573 { 574 try 575 { 576 return _jeuDAO.selectByPrimaryKey( jeuId ); 577 } 578 catch( PersistenceException e ) 579 { 580 throw new ServiceException( ServiceException.ServiceCode.JEU_NOT_FOUND, e ); 581 } 582 } 583 584 @Nullable 585 @Transactional(readOnly = true) 586 public String getDatasetNameById( @NotNull final Integer jeuId ) 587 throws ServiceException 588 { 589 try 590 { 591 return _jeuDAO.getDatasetNameById( jeuId ); 592 } 593 catch( PersistenceException e ) 594 { 595 throw new ServiceException( ServiceException.ServiceCode.JEU_NOT_FOUND, e ); 596 } 597 } 598 599 @Nullable 600 public PaginatedResult<Dimension> searchDimensions( @NotNull final SimulationFilter filter ) 601 throws ServiceException 602 { 603 try 604 { 605 return _dimensionDAO.search( filter ); 606 } 607 catch( PersistenceException e ) 608 { 609 throw new ServiceException( ServiceException.ServiceCode.DIMENSION_NOT_FOUND, e ); 610 } 611 } 612 613 @Nullable 614 public PaginatedResult<Attribute> searchAttributes( @NotNull final SimulationFilter filter ) 615 throws ServiceException 616 { 617 try 618 { 619 return _attributeDAO.search( filter ); 620 } 621 catch( PersistenceException e ) 622 { 623 throw new ServiceException( ServiceException.ServiceCode.ATTRIBUTE_NOT_FOUND, e ); 624 } 625 } 626 627 @Nullable 628 public PaginatedResult<Variable> searchVariables( @NotNull final SimulationFilter filter ) 629 throws ServiceException 630 { 631 try 632 { 633 return _variableDAO.search( filter ); 634 } 635 catch( PersistenceException e ) 636 { 637 throw new ServiceException( ServiceException.ServiceCode.VARIABLE_NOT_FOUND, e ); 638 } 639 } 640 641 @Nullable 642 public PaginatedResult<Simulation> searchSimulations( @NotNull final SimulationFilter filter ) 643 throws ServiceException 644 { 645 try 646 { 647 return _simulationDAO.search( filter ); 648 } 649 catch( PersistenceException e ) 650 { 651 throw new ServiceException( ServiceException.ServiceCode.SIMULATION_NOT_FOUND, e ); 652 } 653 } 654 655 /** 656 * This method create a directory for all ".nc" files in the simulation directory 657 * And create for each a "header_init.xml" file which contents the attributes, variables, dimensions, etc... for the simulation 658 * The second line of the "header_init.xml" file is changed by "xmlnsHeader" because of the term "xmlns" which is not ok for the "unmarshall" method 659 * The real file to work becomes "header.xml" 660 * 661 * @param simulationPath 662 */ 663 public void launchNCDumpInSimulationDirectory( @NotNull final String simulationPath ) 664 throws ServiceException 665 { 666 final File simulationsDirectory = new File( simulationPath ); 667 for( final String fileName : simulationsDirectory.list() ) 668 { 669 if( fileName.endsWith( FORMAT_FILE ) ) 670 { 671 // Create directory and move simulation file into 672 final String shortFileName = fileName.substring( 0, fileName.lastIndexOf( '.' ) ); 673 final String directoryName = simulationPath + File.separatorChar + shortFileName; 674 final File directory = new File( directoryName ); 675 final File file = new File( simulationPath + File.separatorChar + fileName ); 676 if( !directory.exists() ) 677 { 678 directory.mkdir(); 679 file.renameTo( new File( directoryName + File.separatorChar + fileName ) ); 680 try 681 { 682 final Runtime runtime = Runtime.getRuntime(); 683 final String headerInit = directoryName + File.separatorChar + "header_init.xml"; 684 final String[] argsNcdump = {"/bin/sh", "-c", "/usr/bin/ncdump -h -x " + directoryName + File.separatorChar + fileName + " > " + headerInit}; 685 runtime.exec( argsNcdump ); 686 EtherHelper.waitForFileWithMax( new File( headerInit ), 5000 ); 687 final String[] argsSed = {"/bin/sh", "-c", "sed s/xmlns/xmlnsHeader/ " + directoryName + File.separatorChar + "header_init.xml > " + directoryName + File.separatorChar + FILE_HEADER_NAME}; 688 runtime.exec( argsSed ); 689 } 690 catch( IOException e ) 691 { 692 throw new ServiceException( ServiceException.ServiceCode.ERROR_PROCESS_SIMULATION_NCDUMP, e ); 693 } 694 catch( InterruptedException e ) 695 { 696 throw new ServiceException( ServiceException.ServiceCode.ERROR_PROCESS_SIMULATION_NCDUMP_WAIT, e ); 697 } 698 } 699 } 700 } 701 } 702 703 /** 704 * This method insert the header and the dates (variable "Times") in database 705 * The dates are saved in database as a unique String (use to split when we need to have the real values) 706 * The new variables are separatly created in the table .... 707 * The metadata are saved in the table .... 708 * 709 * @param simulationPath 710 * @param directoryName 711 */ 712 @Transactional(rollbackFor = Exception.class) 713 public void insertHeader( @NotNull final String simulationPath, @NotNull final String directoryName ) 714 throws ServiceException 715 { 716 try 717 { 718 final String fileName = simulationPath + File.separatorChar + directoryName + File.separatorChar + FILE_HEADER_NAME; 719 720 final JAXBContext context = JAXBContext.newInstance( Simulation.class ); 721 final Marshaller marshaller = context.createMarshaller(); 722 marshaller.setProperty( Marshaller.JAXB_FORMATTED_OUTPUT, true ); 723 final Unmarshaller uMarshaller = context.createUnmarshaller(); 724 final Simulation simulation = (Simulation) uMarshaller.unmarshal( new File( fileName ) ); 725 726 // Extract dates for the simulation 727 final Runtime runtime = Runtime.getRuntime(); 728 final String[] argsNcdump = {"/bin/sh", "-c", "ncdump -v Times " + simulationPath + File.separatorChar + directoryName + File.separatorChar + directoryName + FORMAT_FILE + " | sed -e '1,/data:/d' -e '1,/Times/d' -e '$d'"}; 729 final Process exec = runtime.exec( argsNcdump ); 730 731 final BufferedReader bufferedReader = new BufferedReader( new InputStreamReader( exec.getInputStream() ) ); 732 final StringBuffer stringBuffer = new StringBuffer(); 733 String line; 734 while( ( line = bufferedReader.readLine() ) != null ) 735 stringBuffer.append( line ); 736 737 final String dates = stringBuffer.toString().replaceAll( "\"", "" ).replaceAll( " ", "" ).replaceAll( ";", "" ); 738 simulation.setDates( dates ); 739 740 final Integer simulationId = insertSimulation( simulation ); 741 } 742 catch( PropertyException e ) 743 { 744 throw new ServiceException( ServiceException.ServiceCode.ERROR_PROPERTY, e ); 745 } 746 catch( JAXBException e ) 747 { 748 throw new ServiceException( ServiceException.ServiceCode.ERROR_JAXB, e ); 749 } 750 catch( IOException e ) 751 { 752 throw new ServiceException( ServiceException.ServiceCode.ERROR_PROCESS_SIMULATION_NCDUMP, e ); 753 } 754 } 755 756 /** 757 * This method extract data (parameters, latitudes, longitutes) from the file corresponding to the simulation for the megapoliPlot 758 * We use ncks to extract only data for a geographic area and a date 759 * http://linux.die.net/man/1/ncks 760 * Example to see easily : ncks -H -d Time,0 -d bottom_top,0 -v APINEN out.20090701_20090705_01_SMPV4_complet.nc 761 * 762 * @param modelId 763 * @param variableName 764 * @param dateNumber : the number of the date among all the available dates 765 * @param level 766 * @param writeInTemporaryFiles : use to debug and see the result of the process 767 * @return 768 * @throws IOException 769 * @throws ServiceException 770 */ 771 @Nullable 772 @Transactional(readOnly = true) 773 public List<Double> extractValuesByModelByVariableByDateByLevelFromFile( @NotNull final Integer modelId, @NotNull final String variableName, @NotNull final Integer dateNumber, @NotNull final Integer level, final boolean writeInTemporaryFiles ) 774 throws ServiceException 775 { 776 final String filePath = getLocationByModelId( modelId ); 777 final String resultFile = filePath + '_' + variableName; 778 779 try 780 { 781 final Runtime runtime = Runtime.getRuntime(); 782 // TODO : voir pour la précision -s "%f\n" 783 final String processString = "ncks -s \"%16.30f\\n\" -d Time," + dateNumber + " -d bottom_top," + level + " -v " + variableName + ' ' + filePath + " | sed '/^" + variableName + "/d'"; 784 final String[] argsNcdump = {"/bin/sh", "-c", processString}; 785 final Process exec = runtime.exec( argsNcdump ); 786 787 if( writeInTemporaryFiles ) 788 runtime.exec( new String[]{"/bin/sh", "-c", processString + " > " + resultFile} ); 789 790 // TODO : voir avec java 7 pour utiliser le Files.readAllLines() et obtenir directement une List<>, http://adiguba.developpez.com/tutoriels/java/7/ 791 final BufferedReader bufferedReader = new BufferedReader( new InputStreamReader( exec.getInputStream() ) ); 792 final List<Double> valuesList = new ArrayList<Double>(); 793 String line; 794 while( ( line = bufferedReader.readLine() ) != null ) 795 if( !line.isEmpty() ) 796 valuesList.add( Double.valueOf( line ) ); 797 798 return valuesList; 799 } 800 catch( IOException e ) 801 { 802 if( new File( resultFile ).exists() ) 803 new File( resultFile ).delete(); 804 805 throw new ServiceException( ServiceException.ServiceCode.ERROR_PROCESS_SIMULATION_NCDUMP, e ); 806 } 807 } 808 809 @Nullable 810 @Transactional(readOnly = true) 811 public Dimension getDimensionById( @NotNull final Integer dimensionId ) 812 throws ServiceException 813 { 814 try 815 { 816 return _dimensionDAO.selectByPrimaryKey( dimensionId ); 817 } 818 catch( PersistenceException e ) 819 { 820 throw new ServiceException( ServiceException.ServiceCode.DIMENSION_NOT_FOUND, e ); 821 } 822 } 823 824 @Transactional(rollbackFor = Exception.class) 825 public void removeDimension( @NotNull final Integer dimensionId ) 826 throws ServiceException 827 { 828 try 829 { 830 _dimensionDAO.deleteByPrimaryKey( dimensionId ); 831 } 832 catch( PersistenceException e ) 833 { 834 throw new ServiceException( ServiceException.ServiceCode.PERSISTENCE, e ); 835 } 836 } 837 838 @Nullable 839 @Transactional(readOnly = true) 840 public Attribute getAttributeById( @NotNull final Integer attributeId ) 841 throws ServiceException 842 { 843 try 844 { 845 return _attributeDAO.selectByPrimaryKey( attributeId ); 846 } 847 catch( PersistenceException e ) 848 { 849 throw new ServiceException( ServiceException.ServiceCode.ATTRIBUTE_NOT_FOUND, e ); 850 } 851 } 852 853 @Transactional(rollbackFor = Exception.class) 854 public void removeAttribute( @NotNull final Integer attributeId ) 855 throws ServiceException 856 { 857 try 858 { 859 _attributeDAO.deleteByPrimaryKey( attributeId ); 860 } 861 catch( PersistenceException e ) 862 { 863 throw new ServiceException( ServiceException.ServiceCode.PERSISTENCE, e ); 864 } 865 } 866 867 @Nullable 868 @Transactional(readOnly = true) 869 public Variable getVariableById( @NotNull final Integer variableId ) 870 throws ServiceException 871 { 872 try 873 { 874 return _variableDAO.selectByPrimaryKey( variableId ); 875 } 876 catch( PersistenceException e ) 877 { 878 throw new ServiceException( ServiceException.ServiceCode.VARIABLE_NOT_FOUND, e ); 879 } 880 } 881 882 @Transactional(rollbackFor = Exception.class) 883 public void removeVariable( @NotNull final Integer variableId ) 884 throws ServiceException 885 { 886 try 887 { 888 _variableDAO.deleteByPrimaryKey( variableId ); 889 } 890 catch( PersistenceException e ) 891 { 892 throw new ServiceException( ServiceException.ServiceCode.PERSISTENCE, e ); 893 } 894 } 895 896 @Nullable 897 @Transactional(readOnly = true) 898 public Simulation getSimulationById( @NotNull final Integer simulationId ) 899 throws ServiceException 900 { 901 try 902 { 903 return _simulationDAO.selectByPrimaryKey( simulationId ); 904 } 905 catch( PersistenceException e ) 906 { 907 throw new ServiceException( ServiceException.ServiceCode.SIMULATION_NOT_FOUND, e ); 908 } 909 } 910 911 @Transactional(rollbackFor = Exception.class) 912 public void removeSimulation( @NotNull final Integer simulationId ) 913 throws ServiceException 914 { 915 try 916 { 917 _simulationDAO.deleteByPrimaryKey( simulationId ); 918 } 919 catch( PersistenceException e ) 920 { 921 throw new ServiceException( ServiceException.ServiceCode.PERSISTENCE, e ); 922 } 923 } 924 925 @Nullable 926 @Transactional(readOnly = true) 927 public List<Simulation> getAllSimulations() 928 throws ServiceException 929 { 930 try 931 { 932 return _simulationDAO.selectAll(); 933 } 934 catch( PersistenceException e ) 935 { 936 throw new ServiceException( ServiceException.ServiceCode.SIMULATION_NOT_FOUND, e ); 937 } 938 } 939 940 @Nullable 941 @Transactional(readOnly = true) 942 public List<List<String>> getAllModels() 943 throws ServiceException 944 { 945 try 946 { 947 return _simulationDAO.getAllModels(); 948 } 949 catch( PersistenceException e ) 950 { 951 throw new ServiceException( ServiceException.ServiceCode.MODEL_NOT_FOUND, e ); 952 } 953 } 954 955 @Nullable 956 @Transactional(readOnly = true) 957 public List<Variable> getVariablesByModelId( @NotNull final Integer modelId ) 958 throws ServiceException 959 { 960 try 961 { 962 return _variableDAO.getVariablesByModelId( modelId ); 963 } 964 catch( PersistenceException e ) 965 { 966 throw new ServiceException( ServiceException.ServiceCode.VARIABLE_NOT_FOUND, e ); 967 } 968 } 969 970 @Nullable 971 @Transactional(readOnly = true) 972 public String getLocationByModelId( @NotNull final Integer modelId ) 973 throws ServiceException 974 { 975 try 976 { 977 return _simulationDAO.getLocationByModelId( modelId ); 978 } 979 catch( PersistenceException e ) 980 { 981 throw new ServiceException( ServiceException.ServiceCode.SIMULATION_NOT_FOUND, e ); 982 } 983 } 984 985 @Nullable 986 @Transactional(readOnly = true) 987 public List<Plateforme> getPlateformsByParameter( @NotNull final String parameterCode ) 988 throws ServiceException 989 { 990 try 991 { 992 return _plateformDAO.getPlateformsByParameter( parameterCode ); 993 } 994 catch( PersistenceException e ) 995 { 996 throw new ServiceException( ServiceException.ServiceCode.PLATEFORM_NOT_FOUND, e ); 997 } 998 } 999 1000 @Nullable 1001 @Transactional(readOnly = true) 1002 public Integer getParameterByCode( @NotNull final String parameterCode ) 1003 throws ServiceException 1004 { 1005 try 1006 { 1007 return _parameterDAO.getParameterByCode( parameterCode ); 1008 } 1009 catch( PersistenceException e ) 1010 { 1011 throw new ServiceException( ServiceException.ServiceCode.PARAMETER_NOT_FOUND, e ); 1012 } 1013 } 1014 1015 /** 1016 * This method reduce the geographic map of the simulated data to the geographic map of the real data 1017 * We extract only the simulated data which are in the geographic map of the real data 1018 * 1019 * @param allParameterValues 1020 * @param allLatitudeValues 1021 * @param allLongitudeValues 1022 * @param realData 1023 * @return 1024 */ 1025 @NotNull 1026 public Data makeZoom( @NotNull final List<Double> allParameterValues, @NotNull final List<Double> allLatitudeValues, @NotNull final List<Double> allLongitudeValues, @Nullable final Data realData ) 1027 { 1028 if( null == realData ) 1029 return new Data( allParameterValues, allLatitudeValues, allLongitudeValues ); 1030 else 1031 { 1032 final Double minRealLat = Collections.min( (List<Double>) realData.getSecondArray() ); 1033 final Double maxRealLat = Collections.max( (List<Double>) realData.getSecondArray() ); 1034 final Double minRealLon = Collections.min( (List<Double>) realData.getThirdArray() ); 1035 final Double maxRealLon = Collections.max( (List<Double>) realData.getThirdArray() ); 1036 1037 final Integer indexMinLat = getNearestIndex( allLatitudeValues, minRealLat, true ); 1038 final Integer indexMaxLat = getNearestIndex( allLatitudeValues, maxRealLat, false ); 1039 final Integer indexMinLon = getNearestIndex( allLongitudeValues, minRealLon, true ); 1040 final Integer indexMaxLon = getNearestIndex( allLongitudeValues, maxRealLon, false ); 1041 1042 final Double borneMinLat = allLatitudeValues.get( indexMinLat ); 1043 final Double borneMaxLat = allLatitudeValues.get( indexMaxLat ); 1044 1045 final Double borneMinLon = allLongitudeValues.get( indexMinLon ); 1046 final Double borneMaxLon = allLongitudeValues.get( indexMaxLon ); 1047 1048 final List<Double> parameterValues = new ArrayList<Double>(); 1049 final List<Double> latitudeValues = new ArrayList<Double>(); 1050 final List<Double> longitudeValues = new ArrayList<Double>(); 1051 for( int i = 0; i < allParameterValues.size(); i++ ) 1052 { 1053 final Double latitude = allLatitudeValues.get( i ); 1054 final Double longitude = allLongitudeValues.get( i ); 1055 if( borneMinLat <= latitude && latitude <= borneMaxLat && borneMinLon <= longitude && longitude <= borneMaxLon ) 1056 { 1057 latitudeValues.add( latitude ); 1058 longitudeValues.add( longitude ); 1059 parameterValues.add( allParameterValues.get( i ) ); 1060 } 1061 } 1062 1063 return new Data( parameterValues, latitudeValues, longitudeValues ); 1064 } 1065 } 1066 1067 /** 1068 * This method flip the list 1069 * We need this operation because the ploting is lon0[lat0,lat1,lat2], lon1[lat0,lat1,lat2]... and the values extract from the file are given as lat0[lon0,lon1], lat1[lon0, lon1], lat2[lon0,lon1]... 1070 * 1071 * @param list 1072 * @param sizeLatitude 1073 * @param sizeLongitude 1074 * @return 1075 */ 1076 @NotNull 1077 public List<Double> flipList( @NotNull final List<Double> list, @NotNull final Integer sizeLatitude, @NotNull final Integer sizeLongitude ) 1078 { 1079 final List<Double> flipedList = new ArrayList<Double>( list.size() ); 1080 for( int lon = 0; lon < sizeLongitude; lon++ ) 1081 for( int lat = 0; lat < sizeLatitude; lat++ ) 1082 { 1083 flipedList.add( list.get( lon + lat * sizeLongitude ) ); 1084 } 1085 return flipedList; 1086 } 1087 1088 @Nullable 1089 private Integer getNearestIndex( @NotNull final List<Double> list, @NotNull final Double value, final boolean isForMinimum ) 1090 { 1091 boolean findValue = false; 1092 int i = 0; 1093 while( !findValue && i < list.size() ) 1094 { 1095 if( value > list.get( i ) ) 1096 i++; 1097 else 1098 findValue = true; 1099 } 1100 if( findValue && i > 1 ) 1101 return isForMinimum ? i - 1 : i; 1102 else 1103 return 0; 1104 } 1105 1106 /** 1107 * This method insert in database the new dimensions, attributes, variables and simulation 1108 * We use HashSet to avoid doublon ( no new insertion for a parameter already in DB ) 1109 * 1110 * @param simulation 1111 * @return 1112 * @throws ServiceException 1113 */ 1114 private Integer insertSimulation( final Simulation simulation ) 1115 throws ServiceException 1116 { 1117 try 1118 { 1119 // Insert dimensions 1120 final Set<Dimension> dimensionsInDB = new HashSet<Dimension>( _dimensionDAO.selectAll() ); 1121 final Set<Dimension> dimensionsInSimulation = simulation.getDimension(); 1122 final Set<Dimension> dimensionsInDBAndSimulation = new HashSet<Dimension>( dimensionsInDB ); 1123 dimensionsInDBAndSimulation.retainAll( dimensionsInSimulation ); 1124 _dimensionDAO.updateAll( dimensionsInDBAndSimulation ); 1125 final Set<Dimension> dimensionsToAdd = new HashSet<Dimension>( dimensionsInSimulation ); 1126 dimensionsToAdd.removeAll( dimensionsInDB ); 1127 _dimensionDAO.insertAll( dimensionsToAdd ); 1128 dimensionsInDBAndSimulation.addAll( dimensionsToAdd ); 1129 simulation.setDimension( dimensionsInDBAndSimulation ); 1130 1131 // Insert Attributes 1132 final Set<Attribute> attributesInDB = new HashSet<Attribute>( _attributeDAO.selectAll() ); 1133 final Set<Attribute> attributesInSimulation = simulation.getAttribute(); 1134 final Set<Attribute> attributesInDBAndSimulation = new HashSet<Attribute>( attributesInDB ); 1135 attributesInDBAndSimulation.retainAll( attributesInSimulation ); 1136 _attributeDAO.updateAll( attributesInDBAndSimulation ); 1137 final Set<Attribute> attributesToAdd = new HashSet<Attribute>( attributesInSimulation ); 1138 attributesToAdd.removeAll( attributesInDB ); 1139 _attributeDAO.insertAll( attributesToAdd ); 1140 attributesInDBAndSimulation.addAll( attributesToAdd ); 1141 simulation.setAttribute( attributesInDBAndSimulation ); 1142 1143 // Insert variables 1144 final Set<Variable> variablesInDB = new HashSet<Variable>( _variableDAO.selectAll() ); 1145 final Set<Variable> variablesInSimulation = simulation.getVariable(); 1146 final Set<Variable> variablesInDBAndSimulation = new HashSet<Variable>( variablesInDB ); 1147 variablesInDBAndSimulation.retainAll( variablesInSimulation ); 1148 _variableDAO.updateAll( variablesInDBAndSimulation ); 1149 final Set<Variable> variablesToAdd = new HashSet<Variable>( variablesInSimulation ); 1150 variablesToAdd.removeAll( variablesInDB ); 1151 _variableDAO.insertAll( variablesToAdd ); 1152 variablesInDBAndSimulation.addAll( variablesToAdd ); 1153 simulation.setVariable( variablesInDBAndSimulation ); 1154 1155 // Insert header 1156 final Simulation simulationInDB = _simulationDAO.getSimulationByLocation( simulation.getLocation() ); 1157 if( null != simulationInDB ) 1158 { 1159 simulationInDB.setXmlnsHeader( simulation.getXmlnsHeader() ); 1160 simulationInDB.setDimension( simulation.getDimension() ); 1161 simulationInDB.setAttribute( simulation.getAttribute() ); 1162 simulationInDB.setVariable( simulation.getVariable() ); 1163 _simulationDAO.update( simulationInDB ); 1164 return simulationInDB.getId(); 1165 } 1166 else 1167 return _simulationDAO.insert( simulation ); 1168 } 1169 catch( PersistenceException e ) 1170 { 1171 throw new ServiceException( ServiceException.ServiceCode.PERSISTENCE, e ); 1172 } 1173 } 1174 1175 @Required 1176 public void setPlateformDAO( final PlateformDAO plateformDAO ) 1177 { 1178 _plateformDAO = plateformDAO; 1179 } 1180 1181 @Required 1182 public void setParameterDAO( final ParameterDAO parameterDAO ) 1183 { 1184 _parameterDAO = parameterDAO; 1185 } 1186 1187 @Required 1188 public void setValueDAO( final ValueDAO valueDAO ) 1189 { 1190 _valueDAO = valueDAO; 1191 } 1192 1193 @Required 1194 public void setMeasureDAO( final MeasureDAO measureDAO ) 1195 { 1196 _measureDAO = measureDAO; 1197 } 1198 1199 @Required 1200 public void setUserDAO( final UserDAO userDAO ) 1201 { 1202 _userDAO = userDAO; 1203 } 1204 1205 @Required 1206 public void setPersonDAO( final PersonDAO personDAO ) 1207 { 1208 _personDAO = personDAO; 1209 } 1210 1211 @Required 1212 public void setMcoDAO( final McoDAO mcoDAO ) 1213 { 1214 _mcoDAO = mcoDAO; 1215 } 1216 1217 @Required 1218 public void setJeuDAO( final JeuDAO jeuDAO ) 1219 { 1220 _jeuDAO = jeuDAO; 1221 } 1222 1223 @Required 1224 public void setDimensionDAO( final DimensionDAO dimensionDAO ) 1225 { 1226 _dimensionDAO = dimensionDAO; 1227 } 1228 1229 @Required 1230 public void setAttributeDAO( final AttributeDAO attributeDAO ) 1231 { 1232 _attributeDAO = attributeDAO; 1233 } 1234 1235 @Required 1236 public void setVariableDAO( final VariableDAO variableDAO ) 1237 { 1238 _variableDAO = variableDAO; 1239 } 1240 1241 @Required 1242 public void setSimulationDAO( final SimulationDAO simulationDAO ) 1243 { 1244 _simulationDAO = simulationDAO; 1245 } 13 // @Nullable 14 // @Transactional(readOnly = true) 15 // public List<Plateforme> getAllPlateforms() 16 // throws ServiceException 17 // { 18 // try 19 // { 20 // return _plateformDAO.getAllPlateforms(); 21 // } 22 // catch( PersistenceException e ) 23 // { 24 // throw new ServiceException( ServiceException.ServiceCode.PLATEFORM_NOT_FOUND, e ); 25 // } 26 // } 27 // 28 // @NotNull 29 // @Transactional(rollbackFor = Exception.class) 30 // public Integer createUser( @NotNull final User user ) 31 // throws ServiceException 32 // { 33 // try 34 // { 35 // return _userDAO.insert( user ); 36 // } 37 // catch( PersistenceException e ) 38 // { 39 // throw new ServiceException( ServiceException.ServiceCode.PERSISTENCE, e ); 40 // } 41 // } 1246 42 1247 43 private static final Log LOGGER = LogFactory.getLog( EtherServiceImpl.class ); 1248 1249 private static String FILE_HEADER_NAME = "header.xml";1250 private static String FORMAT_FILE = ".nc";1251 1252 private PlateformDAO _plateformDAO;1253 private ParameterDAO _parameterDAO;1254 private ValueDAO _valueDAO;1255 private MeasureDAO _measureDAO;1256 private UserDAO _userDAO;1257 private PersonDAO _personDAO;1258 private McoDAO _mcoDAO;1259 private JeuDAO _jeuDAO;1260 private DimensionDAO _dimensionDAO;1261 private AttributeDAO _attributeDAO;1262 private VariableDAO _variableDAO;1263 private SimulationDAO _simulationDAO;1264 44 }
Note: See TracChangeset
for help on using the changeset viewer.