Author: tchemit Date: 2012-03-10 01:53:51 +0100 (Sat, 10 Mar 2012) New Revision: 345 Url: http://forge.codelutin.com/repositories/revision/echobase/345 Log: move import tests as IT Added: trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/exceptions/MissionNameAlreadyExistException.java trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/importdata/ trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/importdata/AbstractImportDataService.java trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/importdata/AcousticDataImportService.java trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/importdata/CatchesDataImportService.java trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/importdata/CommonDataImportService.java trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/importdata/ImportDataMode.java trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/importdata/OperationImportService.java trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/importdata/ResultsImportService.java trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/importdata/ trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/importdata/AbstractImportDataServiceIT.java trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/importdata/AcousticDataImportServiceIT.java trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/importdata/CatchesDataImportServiceIT.java trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/importdata/CommonData2ImportServiceIT.java trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/importdata/CommonDataImportServiceIT.java trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/importdata/OperationImportServiceIT.java trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/importdata/ResultsImportServiceIT.java Removed: trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/AbstractImportDataService.java trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/AcousticDataImportService.java trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/CatchesDataImportService.java trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/CommonDataImportService.java trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/ImportDataMode.java trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/MissionNameAlreadyExistException.java trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/OperationImportService.java trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/ResultsImportService.java trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/AcousticDataImportServiceTest.java trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/CatchesDataImportServiceTest.java trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/CommonDataImportService2Test.java trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/CommonDataImportServiceTest.java trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/ImportDataConstants.java trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/OperationImportServiceTest.java trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/ResultsImportServiceTest.java Modified: trunk/echobase-services/pom.xml trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/EchoBaseTestServiceSupport.java trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/EmbeddedApplicationServiceTest.java trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/ExportQueryServiceTest.java Modified: trunk/echobase-services/pom.xml =================================================================== --- trunk/echobase-services/pom.xml 2012-03-09 23:52:25 UTC (rev 344) +++ trunk/echobase-services/pom.xml 2012-03-10 00:53:51 UTC (rev 345) @@ -66,11 +66,6 @@ </dependency> <dependency> - <groupId>commons-beanutils</groupId> - <artifactId>commons-beanutils</artifactId> - </dependency> - - <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> </dependency> @@ -204,4 +199,42 @@ </build> + <profiles> + + <profile> + <id>import-data-profile</id> + <activation> + <property> + <name>performRelease</name> + <value>true</value> + </property> + </activation> + + <build> + <defaultGoal>integration-test</defaultGoal> + <plugins> + <plugin> + <artifactId>maven-surefire-plugin</artifactId> + <executions> + <execution> + <id>import-data-tests</id> + <phase>integration-test</phase> + <goals> + <goal>test</goal> + </goals> + </execution> + </executions> + <configuration> + <includes> + <include>**/*ImportServiceIT.java</include> + </includes> + </configuration> + + </plugin> + </plugins> + </build> + + </profile> + </profiles> + </project> Deleted: trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/AbstractImportDataService.java =================================================================== --- trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/AbstractImportDataService.java 2012-03-09 23:52:25 UTC (rev 344) +++ trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/AbstractImportDataService.java 2012-03-10 00:53:51 UTC (rev 345) @@ -1,226 +0,0 @@ -/* - * #%L - * EchoBase :: Services - * - * $Id$ - * $HeadURL$ - * %% - * Copyright (C) 2011 - 2012 Ifremer, Codelutin - * %% - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * #L% - */ -package fr.ifremer.echobase.services; - -import com.google.common.base.Charsets; -import com.google.common.base.Function; -import com.google.common.collect.Maps; -import fr.ifremer.echobase.EchoBaseTechnicalException; -import fr.ifremer.echobase.InputFile; -import fr.ifremer.echobase.entities.EchoBaseEntityEnum; -import fr.ifremer.echobase.entities.EchoBaseUser; -import fr.ifremer.echobase.entities.EntityModificationLog; -import fr.ifremer.echobase.entities.references.SexCategory; -import fr.ifremer.echobase.entities.references.SizeCategory; -import fr.ifremer.echobase.entities.references.Species; -import fr.ifremer.echobase.entities.references.SpeciesCategory; -import fr.ifremer.echobase.entities.references.SpeciesCategoryDAO; -import fr.ifremer.echobase.services.configurations.AbstractImportConfiguration; -import fr.ifremer.echobase.services.csv.CsvFileImportResult; -import org.nuiton.topia.TopiaException; -import org.nuiton.topia.persistence.TopiaDAO; -import org.nuiton.topia.persistence.TopiaEntity; - -import java.io.BufferedReader; -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.IOException; -import java.io.InputStreamReader; -import java.io.Reader; -import java.util.List; -import java.util.Map; -import java.util.Set; - -/** - * Abstract data import service. - * - * @author tchemit <chemit@codelutin.com> - * @since 0.3 - */ -public abstract class AbstractImportDataService<M extends AbstractImportConfiguration> extends EchoBaseServiceSupport { - - public final List<CsvFileImportResult> doImport(M configuration, - EchoBaseUser user) throws ImportException { - - - try { - List<CsvFileImportResult> result = startImport(configuration, user); - - // do commit - commitTransaction("Could not execute import " + getImportLabel()); - return result; - } catch (ImportException e) { - throw e; - } catch (Exception e) { - throw new ImportException("Could not do import " + e.getMessage(), e); - } - } - - protected abstract String getImportLabel(); - - protected abstract List<CsvFileImportResult> startImport(M configuration, - EchoBaseUser user) throws ImportException; - - protected void addResultAndLog(List<CsvFileImportResult> result, - InputFile inputFile, - CsvFileImportResult importResult, - EchoBaseUser user) { - - Set<EchoBaseEntityEnum> entityTypes = importResult.getEntityTypes(); - if (!entityTypes.isEmpty()) { - - result.add(importResult); - - String importType = getImportLabel(); - - StringBuilder description = new StringBuilder(); - description.append("Depuis Fichier ").append(inputFile.getFileName()); - TopiaDAO<EntityModificationLog> dao = - getDAO(EntityModificationLog.class); - - for (EchoBaseEntityEnum entityType : entityTypes) { - int numberCreated = importResult.getNumberCreated(entityType); - int numberUpdated = importResult.getNumberUpdated(entityType); - if (numberCreated > 0) { - description.append(" [ nombre de création : ").append(numberCreated).append("]"); - } - if (numberUpdated > 0) { - description.append(" [ nombre de mises à jour : ").append(numberUpdated).append("]"); - } - - create(dao, - EntityModificationLog.PROPERTY_ENTITY_TYPE, entityType.name(), - EntityModificationLog.PROPERTY_ENTITY_ID, importType, - EntityModificationLog.PROPERTY_MODIFICATION_USER, user.getEmail(), - EntityModificationLog.PROPERTY_MODIFICATION_DATE, newDate(), - EntityModificationLog.PROPERTY_MODIFICATION_TEXT, description.toString() - ); - } - - } - } - - protected Reader getInputFileReader(InputFile inputFile) throws EchoBaseTechnicalException { - try { - Reader reader = new BufferedReader(new InputStreamReader( - new FileInputStream(inputFile.getFile()), Charsets.UTF_8)); - return reader; - } catch (FileNotFoundException e) { - throw new EchoBaseTechnicalException( - "Could not find import file " + inputFile.getFile(), e); - } - } - - protected void closeReader(Reader reader, InputFile inputFile) { - try { - reader.close(); - } catch (IOException e) { - throw new EchoBaseTechnicalException( - "Could not close reader on file " + - inputFile.getFile(), e); - } - } - - protected <E extends TopiaEntity, K> Map<K, E> getEntitiesMap( - Class<E> entityType, - Function<E, K> function) { - List<E> allVoyages = getEntities(entityType); - Map<K, E> voyageMap = Maps.uniqueIndex( - allVoyages, function); - return voyageMap; - } - - protected <E extends TopiaEntity> E newInstance(TopiaDAO<E> dao) { - try { - E result = dao.newInstance(); - return result; - } catch (TopiaException e) { - throw new EchoBaseTechnicalException("Could not create new instance of entity", e); - } - } - - protected <E extends TopiaEntity> E create(TopiaDAO<E> dao) { - try { - E result = dao.create(); - return result; - } catch (TopiaException e) { - throw new EchoBaseTechnicalException("Could not create entity", e); - } - } - - protected <E extends TopiaEntity> E create(TopiaDAO<E> dao, E entity) { - try { - E result = dao.create(entity); - return result; - } catch (TopiaException e) { - throw new EchoBaseTechnicalException("Could not create entity", e); - } - } - - protected <E extends TopiaEntity> E create(TopiaDAO<E> dao, - Object... properties) { - try { - E result = dao.create(properties); - return result; - } catch (TopiaException e) { - throw new EchoBaseTechnicalException("Could not create entity", e); - } - } - - protected <E extends TopiaEntity> E findByProperties(TopiaDAO<E> dao, - String propertyName, - Object value, - Object... others) { - try { - E result = dao.findByProperties(propertyName, value, others); - return result; - } catch (TopiaException e) { - throw new EchoBaseTechnicalException("Could not obtain query db", e); - } - } - - protected SpeciesCategory getSpeciesCategory(SpeciesCategoryDAO speciesCategoryDAO, - Species species, - SizeCategory sizeCategory, - SexCategory sexCategory) { - SpeciesCategory category = findByProperties( - speciesCategoryDAO, - SpeciesCategory.PROPERTY_SPECIES, species, - SpeciesCategory.PROPERTY_SIZE_CATEGORY, sizeCategory, - SpeciesCategory.PROPERTY_SEX_CATEGORY, sexCategory - ); - - if (category == null) { - - // create it - - category = create(speciesCategoryDAO); - category.setSpecies(species); - category.setSizeCategory(sizeCategory); - category.setSexCategory(sexCategory); - } - - return category; - } -} Deleted: trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/AcousticDataImportService.java =================================================================== --- trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/AcousticDataImportService.java 2012-03-09 23:52:25 UTC (rev 344) +++ trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/AcousticDataImportService.java 2012-03-10 00:53:51 UTC (rev 345) @@ -1,572 +0,0 @@ -/* - * #%L - * EchoBase :: Services - * - * $Id$ - * $HeadURL$ - * %% - * Copyright (C) 2011 - 2012 Ifremer, Codelutin - * %% - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * #L% - */ -package fr.ifremer.echobase.services; - -import com.google.common.base.Preconditions; -import com.google.common.collect.Lists; -import fr.ifremer.echobase.EchoBaseFunctions; -import fr.ifremer.echobase.EchoBasePredicates; -import fr.ifremer.echobase.InputFile; -import fr.ifremer.echobase.entities.EchoBaseEntityEnum; -import fr.ifremer.echobase.entities.EchoBaseUser; -import fr.ifremer.echobase.entities.data.Cell; -import fr.ifremer.echobase.entities.data.CellDAO; -import fr.ifremer.echobase.entities.data.Data; -import fr.ifremer.echobase.entities.data.DataAcquisition; -import fr.ifremer.echobase.entities.data.DataAcquisitionDAO; -import fr.ifremer.echobase.entities.data.DataDAO; -import fr.ifremer.echobase.entities.data.DataProcessing; -import fr.ifremer.echobase.entities.data.DataProcessingDAO; -import fr.ifremer.echobase.entities.data.Transect; -import fr.ifremer.echobase.entities.data.Transit; -import fr.ifremer.echobase.entities.data.Voyage; -import fr.ifremer.echobase.entities.references.AcousticInstrument; -import fr.ifremer.echobase.entities.references.CellType; -import fr.ifremer.echobase.entities.references.DataMetadata; -import fr.ifremer.echobase.entities.references.Vessel; -import fr.ifremer.echobase.services.configurations.AcousticDataImportConfiguration; -import fr.ifremer.echobase.services.csv.AcousticDataImportModel; -import fr.ifremer.echobase.services.csv.AcousticDataImportModelRow; -import fr.ifremer.echobase.services.csv.CsvFileImportResult; -import fr.ifremer.echobase.services.csv.EchoBaseImport; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.nuiton.util.csv.Import; -import org.nuiton.util.csv.ImportRuntimeException; - -import java.io.Reader; -import java.text.DateFormat; -import java.text.SimpleDateFormat; -import java.util.Date; -import java.util.List; -import java.util.Map; - -import static org.nuiton.i18n.I18n.l_; - -/** - * Service to launch a "acoustic data" import. - * - * @author tchemit <chemit@codelutin.com> - * @since 0.3 - */ -public class AcousticDataImportService extends AbstractImportDataService<AcousticDataImportConfiguration> { - - /** Logger. */ - private static final Log log = - LogFactory.getLog(AcousticDataImportService.class); - - @Override - protected String getImportLabel() { - return l_(getLocale(), ImportDataMode.AccousticData.getI18nKey()); - } - - @Override - protected List<CsvFileImportResult> startImport( - AcousticDataImportConfiguration configuration, - EchoBaseUser user) throws ImportException { - - List<CsvFileImportResult> result = Lists.newLinkedList(); - - // get selected voyage - Voyage voyage = getEntityById(Voyage.class, - configuration.getVoyageId()); - - // get selected vessel - Vessel vessel = getEntityById(Vessel.class, - configuration.getVesselId()); - - CellType esduCellType = getEntityByProperty( - CellType.class, CellType.PROPERTY_ID, "Esdu"); - Preconditions.checkNotNull(esduCellType); - - CellType elementaryCellType = getEntityByProperty( - CellType.class, CellType.PROPERTY_ID, "Elementary"); - Preconditions.checkNotNull(elementaryCellType); - - InputFile inputFile = configuration.getMoviesFile(); - - CsvFileImportResult importResult = importMoviesFile(voyage, - vessel, - esduCellType, - elementaryCellType, - configuration, - inputFile); - - addResultAndLog(result, inputFile, importResult, user); - - return result; - } - - private final DateFormat cellDateFormat = - new SimpleDateFormat("yyyy-MM-dd hh:mm:ss'Z'"); - - private CsvFileImportResult importMoviesFile(Voyage voyage, - Vessel vessel, - CellType esduCellType, - CellType elementaryCellType, - AcousticDataImportConfiguration configuration, - InputFile inputFile - ) throws ImportException { - - CsvFileImportResult importResult = new CsvFileImportResult( - inputFile.getFileName()); - - Map<String, AcousticInstrument> instrumentsById = - getEntitiesMap(AcousticInstrument.class, - EchoBaseFunctions.ACOUSTIC_INSTRUMENT_ID); - - Map<String, DataMetadata> dataMetadatasByName = getEntitiesMap( - DataMetadata.class, EchoBaseFunctions.DATA_METADATA_NAME); - - AcousticDataImportModel csvModel = new AcousticDataImportModel( - getCsvSeparator(), instrumentsById); - - DataAcquisitionDAO dataAcquisitionDAO = - getDAO(DataAcquisition.class, DataAcquisitionDAO.class); - DataProcessingDAO dataProcessingDAO = - getDAO(DataProcessing.class, DataProcessingDAO.class); - CellDAO cellDAO = getDAO(Cell.class, CellDAO.class); - DataDAO dataDAO = getDAO(Data.class, DataDAO.class); - - boolean addDataAcquisition = configuration.isAddDataAcquisition(); - - String suffix = - configuration.getCellPositionReference().getMetadataNameSuffix(); - - Reader reader = getInputFileReader(inputFile); - try { - Import<AcousticDataImportModelRow> importer = - EchoBaseImport.newImport(csvModel, reader); - DataAcquisition dataAcquisition = null; - DataProcessing dataProcessing = null; - Cell esduCell = null; - Cell elementaryCell; - for (AcousticDataImportModelRow row : importer) { - - Date startDate = row.getCellDateStart(); - - // get transect to use - Transit transit = voyage.getTransit(startDate); - if (transit == null) { - - // can not find correct transit - throw new ImportException( - l_(getLocale(), "echobase.importError.transit.notfound", - voyage.getName(), - startDate)); - } - - Transect transect = transit.getTransect(vessel); - if (transect == null) { - - // can not find correct transect - throw new ImportException( - l_(getLocale(), "echobase.importError.transect.notfound", - voyage.getName(), - startDate, - vessel.getName())); - } - - // Build the row acoustic instrument -// String sndname = row.getSndName(); -// String channelName = row.getChannelName(); -// String instrumentId = sndname; - AcousticInstrument instrument = row.getAcousticInstrument(); -// instrumentsById.get(instrumentId); - boolean isME70 = - EchoBasePredicates.IS_ACOUSTIC_INSTRUMENT_ME70.apply(instrument); - if (dataAcquisition == null || - !instrument.equals(dataAcquisition.getAcousticInstrument())) { - - // need to use another data acquisition - - String softwareVersion = - getSoftwareVersion(configuration, isME70); - - String soundSpeedCalculations = - getSoundSpeedCalculations(configuration, isME70); - - if (!addDataAcquisition && - !transect.isDataAcquisitionEmpty()) { - - // try to obtain an existing data acquisiton - - for (DataAcquisition acquisition : transect.getDataAcquisition()) { - - if (instrument.equals(acquisition.getAcousticInstrument())) { - - // found a matching data acquisiotn from his acoustic instrument - dataAcquisition = acquisition; - break; - } - } - } - - if (dataAcquisition == null) { - - // need to create the data acquisition - - // create dataAcquisiton - dataAcquisition = createDataAcquisition( - configuration, - instrument, - softwareVersion, - soundSpeedCalculations, - row, - dataAcquisitionDAO - ); - - importResult.incrementsNumberCreated(EchoBaseEntityEnum.DataAcquisition); - - // add it to transect - transect.addDataAcquisition(dataAcquisition); - } - - // create data processing - dataProcessing = createDataProcessing( - configuration, - softwareVersion, - soundSpeedCalculations, - row, - dataProcessingDAO - ); - - importResult.incrementsNumberCreated(EchoBaseEntityEnum.DataProcessing); - - // add it to data acquisition - dataAcquisition.addDataProcessing(dataProcessing); - - // must start a fresh new esdu cell - esduCell = null; - } - - int cellType = row.getCellType(); - - String esduCellId = cellDateFormat.format(startDate); - - if (cellType == 4) { - - if (log.isDebugEnabled()) { - log.debug("Esdu cell (" + esduCellId + ")"); - } - // this is a esdu cell row - - // create esdu cell - esduCell = create(cellDAO, - Cell.PROPERTY_CELL_TYPE, esduCellType, - Cell.PROPERTY_NAME, esduCellId - ); - - importResult.incrementsNumberCreated(EchoBaseEntityEnum.Cell); - - // add it to data processing - dataProcessing.addCell(esduCell); - - // create esdu cell data - createEsduCellData(suffix, - esduCell, - dataMetadatasByName, - row, - dataDAO, - importResult); - } else { - - // this is a elementary cell row - - int cellNum = row.getCellNum(); - - String elementaryCellId = esduCellId + "_" + cellNum; - - if (log.isDebugEnabled()) { - log.debug("elementary cell (" + elementaryCellId + ")"); - } - if (esduCell == null) { - throw new ImportException( - l_(getLocale(), "echobase.importError.esduCell.notfound", - esduCellId, - voyage.getName(), - elementaryCellId - )); - } - - // create the elementary cell - - elementaryCell = create(cellDAO, - Cell.PROPERTY_CELL_TYPE, elementaryCellType, - Cell.PROPERTY_NAME, elementaryCellId - ); - - importResult.incrementsNumberCreated(EchoBaseEntityEnum.Cell); - - // add it to esdu cell - esduCell.addChilds(elementaryCell); - - - // create datas of the elementary cell - createElementaryCellData(elementaryCell, - dataMetadatasByName, - row, - dataDAO, - importResult); - } - } - - return importResult; - } catch (ImportRuntimeException e) { - throw new ImportException(getLocale(), inputFile, e); - } finally { - closeReader(reader, inputFile); - } - } - - protected void createEsduCellData(String suffix, - Cell cell, - Map<String, DataMetadata> dataMetadatas, - AcousticDataImportModelRow row, - DataDAO dao, - CsvFileImportResult importResult) { - - String dataValue; - - // create Latitude data - dataValue = row.getEsduCellDataLatitude(); - createCellData(dao, cell, dataMetadatas, "Latitude" + suffix, - dataValue, importResult); - - // create Longitude data - dataValue = row.getEsduCellDataLongitude(); - createCellData(dao, cell, dataMetadatas, "Longitude" + suffix, - dataValue, importResult); - - // create Depth data - dataValue = row.getEsduCellDataDepth(); - createCellData(dao, cell, dataMetadatas, "DepthRefSurface" + suffix, - dataValue, importResult); - - // create Time data - dataValue = cellDateFormat.format(row.getCellDateStart()); - createCellData(dao, cell, dataMetadatas, "Time" + suffix, - dataValue, importResult); - - // create AcousticDensity data - dataValue = row.getEsduCellDataAcousticDensity(); - createCellData(dao, cell, dataMetadatas, "sa", dataValue, importResult); - } - - private void createElementaryCellData(Cell cell, - Map<String, DataMetadata> dataMetadatas, - AcousticDataImportModelRow row, - DataDAO dao, - CsvFileImportResult importResult) { - - String dataValue; - - int cellType = row.getCellType(); - - String startMeta; - String endMeta; - - if (cellType == 0) { - - // surface - - startMeta = "DepthRefSurfaceStart"; - endMeta = "DepthRefSurfaceEnd"; - - } else { - - // bottom - - startMeta = "DepthRefBottomStart"; - endMeta = "DepthRefBottomEnd"; - } - - // create depth start data - dataValue = row.getCellDepthStart(); - createCellData(dao, cell, dataMetadatas, startMeta, dataValue, importResult); - - // create depth end data - dataValue = row.getCellDepthEnd(); - createCellData(dao, cell, dataMetadatas, endMeta, dataValue, importResult); - - // create acoustic density data - dataValue = row.getCellSa(); - createCellData(dao, cell, dataMetadatas, "sa", dataValue, importResult); - - } - - private DataAcquisition createDataAcquisition(AcousticDataImportConfiguration configuration, - AcousticInstrument instrument, - String softwareVersion, - String soundSpeedCalculations, - AcousticDataImportModelRow row, - DataAcquisitionDAO dataAcquisitionDAO) { - - String transceiverAcquisitionAbsorptionDescription = - configuration.getTransceiverAcquisitionAbsorptionDescription(); - String loggedDataFormat = configuration.getLoggedDataFormat(); - String loggedDataDatatype = configuration.getLoggedDataDatatype(); - String pingDutyCycle = configuration.getPingDutyCycle(); - String echosounderSoundSpeed = configuration.getEchosounderSoundSpeed(); - - DataAcquisition dataAcquisition = create(dataAcquisitionDAO); - - // fill from manual configuration - dataAcquisition.setAcousticInstrument(instrument); - dataAcquisition.setTransceiverAcquisitionAbsorptionDescription(transceiverAcquisitionAbsorptionDescription); - dataAcquisition.setAcquisitionSoftwareVersion(softwareVersion); - dataAcquisition.setLoggedDataFormat(loggedDataFormat); - dataAcquisition.setLoggedDataDatatype(loggedDataDatatype); - dataAcquisition.setPingDutyCycle(pingDutyCycle); - dataAcquisition.setEchosounderSoundSpeed(echosounderSoundSpeed); - dataAcquisition.setSoundSpeedCalculations(soundSpeedCalculations); - - // fill from csv file - Float transceiverAcquisitionAbsorption = - row.getTransceiverAcquisitionAbsorption(); - Float transducerAcquisitionBeamAngleAthwartship = - row.getTransducerAcquisitionBeamAngleAthwartship(); - Float transducerAcquisitionBeamAngleAlongship = - row.getTransducerAcquisitionBeamAngleAlongship(); - Float transducerAcquisitionPsi = - row.getTransducerAcquisitionPsi(); - Float transceiverAcquisitionPower = - row.getTransceiverAcquisitionPower(); - Float transceiverAcquisitionPulseLength = - row.getTransceiverAcquisitionPulseLength(); - Float transceiverAcquisitionGain = - row.getTransceiverAcquisitionGain(); - Float transceiverAcquisitionSacorrection = - row.getTransceiverAcquisitionSacorrection(); - - dataAcquisition.setTransceiverAcquisitionAbsorption(transceiverAcquisitionAbsorption); - dataAcquisition.setTransducerAcquisitionBeamAngleAthwartship(transducerAcquisitionBeamAngleAthwartship); - dataAcquisition.setTransducerAcquisitionBeamAngleAlongship(transducerAcquisitionBeamAngleAlongship); - dataAcquisition.setTransducerAcquisitionPsi(transducerAcquisitionPsi); - dataAcquisition.setTransceiverAcquisitionPower(transceiverAcquisitionPower); - dataAcquisition.setTransceiverAcquisitionPulseLength(transceiverAcquisitionPulseLength); - dataAcquisition.setTransceiverAcquisitionGain(transceiverAcquisitionGain); - dataAcquisition.setTransceiverAcquisitionSacorrection(transceiverAcquisitionSacorrection); - - return dataAcquisition; - } - - private DataProcessing createDataProcessing(AcousticDataImportConfiguration configuration, - String softwareVersion, - String soundSpeedCalculations, - AcousticDataImportModelRow row, - DataProcessingDAO dataProcessingDAO) { - - String transceiverAcquisitionAbsorptionDescription = - configuration.getTransceiverAcquisitionAbsorptionDescription(); - String echosounderSoundSpeed = configuration.getEchosounderSoundSpeed(); - String processingTemplate = configuration.getProcessingTemplate(); - String processingDescription = configuration.getProcessingDescription(); - Float sounderConstant = configuration.getSounderConstant(); - float digitThreshold = configuration.getDigitThreshold(); - String acousticDensityUnit = configuration.getAcousticDensityUnit(); - String notes = configuration.getNotes(); - - DataProcessing dataProcessing = create(dataProcessingDAO); - - // fill from manual configuration - dataProcessing.setProcessingTemplate(processingTemplate); - dataProcessing.setProcessingDescription(processingDescription); - dataProcessing.setSounderConstant(sounderConstant); - dataProcessing.setDigitThreshold(digitThreshold); - dataProcessing.setAcousticDensityUnit(acousticDensityUnit); - dataProcessing.setNotes(notes); - dataProcessing.setProcessingSoftwareVersion(softwareVersion); - dataProcessing.setTransceiverProcessingAbsorptionDescription(transceiverAcquisitionAbsorptionDescription); - dataProcessing.setEchosounderSoundSpeed(echosounderSoundSpeed); - dataProcessing.setSoundSpeedCalculations(soundSpeedCalculations); - - // fill from csv file - - Float transceiverAcquisitionAbsorption = - row.getTransceiverAcquisitionAbsorption(); - Float transducerAcquisitionBeamAngleAthwartship = - row.getTransducerAcquisitionBeamAngleAthwartship(); - Float transducerAcquisitionBeamAngleAlongship = - row.getTransducerAcquisitionBeamAngleAlongship(); - Float transducerAcquisitionPsi = - row.getTransducerAcquisitionPsi(); - - Float transceiverAcquisitionGain = - row.getTransceiverAcquisitionGain(); - Float transceiverAcquisitionSacorrection = - row.getTransceiverAcquisitionSacorrection(); - Float eIThreshold = row.getEiThreshold(); - - dataProcessing.setEiThreshold(eIThreshold); - dataProcessing.setTransceiverProcessingSacorrection(transceiverAcquisitionSacorrection); - dataProcessing.setTransceiverProcessingAbsorption(transceiverAcquisitionAbsorption); - dataProcessing.setTransceiverProcessingGain(transceiverAcquisitionGain); - dataProcessing.setTransducerProcessingPsi(transducerAcquisitionPsi); - dataProcessing.setTransducerProcessingBeamAngleAthwartship(transducerAcquisitionBeamAngleAthwartship); - dataProcessing.setTransducerProcessingBeamAngleAlongship(transducerAcquisitionBeamAngleAlongship); - - - return dataProcessing; - } - - private void createCellData(DataDAO dao, - Cell cell, - Map<String, DataMetadata> dataMetadatasByName, - String metadataName, - String dataValue, - CsvFileImportResult importResult) { - - DataMetadata dataMetaData = dataMetadatasByName.get(metadataName); - Data data = create(dao); - data.setDataMetadata(dataMetaData); - data.setDataValue(dataValue); - cell.addData(data); - - importResult.incrementsNumberCreated(EchoBaseEntityEnum.Data); - } - - private String getSoftwareVersion(AcousticDataImportConfiguration configuration, - boolean isME70) { - String result; - if (isME70) { - result = configuration.getAcquisitionSoftwareVersionME70(); - } else { - result = configuration.getAcquisitionSoftwareVersionER60(); - } - return result; - } - - private String getSoundSpeedCalculations(AcousticDataImportConfiguration configuration, - boolean isME70) { - String result; - if (isME70) { - result = configuration.getSoundSpeedCalculationsME70(); - } else { - result = configuration.getSoundSpeedCalculationsER60(); - } - return result; - } - -} Deleted: trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/CatchesDataImportService.java =================================================================== --- trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/CatchesDataImportService.java 2012-03-09 23:52:25 UTC (rev 344) +++ trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/CatchesDataImportService.java 2012-03-10 00:53:51 UTC (rev 345) @@ -1,481 +0,0 @@ -/* - * #%L - * EchoBase :: Services - * - * $Id$ - * $HeadURL$ - * %% - * Copyright (C) 2011 - 2012 Ifremer, Codelutin - * %% - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * #L% - */ -package fr.ifremer.echobase.services; - -import com.google.common.base.Preconditions; -import com.google.common.collect.Lists; -import com.google.common.collect.Maps; -import fr.ifremer.echobase.EchoBaseFunctions; -import fr.ifremer.echobase.EchoBasePredicates; -import fr.ifremer.echobase.InputFile; -import fr.ifremer.echobase.entities.EchoBaseEntityEnum; -import fr.ifremer.echobase.entities.EchoBaseUser; -import fr.ifremer.echobase.entities.data.Operation; -import fr.ifremer.echobase.entities.data.Sample; -import fr.ifremer.echobase.entities.data.SampleDAO; -import fr.ifremer.echobase.entities.data.SampleData; -import fr.ifremer.echobase.entities.data.SampleDataDAO; -import fr.ifremer.echobase.entities.data.Voyage; -import fr.ifremer.echobase.entities.references.SampleDataType; -import fr.ifremer.echobase.entities.references.SampleDataTypeDAO; -import fr.ifremer.echobase.entities.references.SampleType; -import fr.ifremer.echobase.entities.references.SampleTypeDAO; -import fr.ifremer.echobase.entities.references.SexCategory; -import fr.ifremer.echobase.entities.references.SizeCategory; -import fr.ifremer.echobase.entities.references.Species; -import fr.ifremer.echobase.entities.references.SpeciesCategory; -import fr.ifremer.echobase.entities.references.SpeciesCategoryDAO; -import fr.ifremer.echobase.entities.references.Vessel; -import fr.ifremer.echobase.services.configurations.CatchesDataImportConfiguration; -import fr.ifremer.echobase.services.csv.BiometrySampleImportModel; -import fr.ifremer.echobase.services.csv.BiometrySampleImportModelRow; -import fr.ifremer.echobase.services.csv.CsvFileImportResult; -import fr.ifremer.echobase.services.csv.EchoBaseImport; -import fr.ifremer.echobase.services.csv.SubSampleImportModel; -import fr.ifremer.echobase.services.csv.SubSampleImportModelRow; -import fr.ifremer.echobase.services.csv.TotalSampleImportModel; -import fr.ifremer.echobase.services.csv.TotalSampleImportModelRow; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.nuiton.util.csv.Import; -import org.nuiton.util.csv.ImportRuntimeException; - -import java.io.Reader; -import java.util.Collection; -import java.util.List; -import java.util.Locale; -import java.util.Map; - -import static org.nuiton.i18n.I18n.l_; - -/** - * Service to launch a "catches sample data" import. - * - * @author tchemit <chemit@codelutin.com> - * @since 0.3 - */ -public class CatchesDataImportService extends AbstractImportDataService<CatchesDataImportConfiguration> { - - /** Logger. */ - private static final Log log = - LogFactory.getLog(CatchesDataImportService.class); - - @Override - protected String getImportLabel() { - return l_(getLocale(), ImportDataMode.CatchesData.getI18nKey()); - } - - @Override - protected List<CsvFileImportResult> startImport(CatchesDataImportConfiguration configuration, - EchoBaseUser user) throws ImportException { - - List<CsvFileImportResult> result = Lists.newLinkedList(); - - Voyage voyage = getEntityById(Voyage.class, - configuration.getVoyageId()); - - // get all operation for this voyage and this vessel - Collection<Operation> operations = voyage.getAllOperations(); - - // split them by operation Id - Map<String, Operation> operationMap = Maps.uniqueIndex( - operations, EchoBaseFunctions.OPERATION_ID); - - Map<String, Species> speciesMap = getEntitiesMap( - Species.class, - EchoBaseFunctions.SPECIES_BARACOUDA_CODE); - - Map<String, SizeCategory> sizeCategoryMap = getEntitiesMap( - SizeCategory.class, - EchoBaseFunctions.SIZE_CATEGORY_NAME); - - Map<String, SexCategory> sexCategoryMap = getEntitiesMap( - SexCategory.class, - EchoBaseFunctions.SEX_CATEGORY_NAME); - - if (configuration.getTotalSampleFile().hasFile()) { - CsvFileImportResult totalSampleResult = importTotalSampleFile( - configuration.getTotalSampleFile(), - operationMap, - speciesMap, - sizeCategoryMap); - - addResultAndLog(result, configuration.getTotalSampleFile(), - totalSampleResult, user); - } - - if (configuration.getSubSampleFile().hasFile()) { - CsvFileImportResult subSampleResult = importSubSampleFile( - configuration.getSubSampleFile(), - operationMap, - speciesMap, - sizeCategoryMap, - sexCategoryMap); - - addResultAndLog(result, configuration.getSubSampleFile(), - subSampleResult, user); - } - - if (configuration.getBiometrySampleFile().hasFile()) { - CsvFileImportResult biometrySampleResult = importBiometrySampleFile( - configuration.getBiometrySampleFile(), - operationMap, - speciesMap); - - - addResultAndLog(result, configuration.getBiometrySampleFile(), - biometrySampleResult, user); - } - return result; - } - - private CsvFileImportResult importTotalSampleFile(InputFile inputFile, - Map<String, Operation> operationMap, - Map<String, Species> speciesMap, - Map<String, SizeCategory> sizeCategoryMap) throws ImportException { - - if (log.isInfoEnabled()) { - log.info("Starts import of totalSample from file " + - inputFile.getFileName()); - } - - CsvFileImportResult importResult = new CsvFileImportResult( - inputFile.getFileName()); - - TotalSampleImportModel csvModel = new TotalSampleImportModel(getCsvSeparator(), - operationMap, - speciesMap, - sizeCategoryMap - ); - - SampleDAO sampleDAO = getDAO(Sample.class, SampleDAO.class); - SampleDataDAO sampleDataDAO = getDAO(SampleData.class, SampleDataDAO.class); - SampleTypeDAO sampleTypeDAO = getDAO(SampleType.class, SampleTypeDAO.class); - SampleDataTypeDAO sampleDataTypeDAO = getDAO(SampleDataType.class, SampleDataTypeDAO.class); - SpeciesCategoryDAO speciesCategoryDAO = getDAO(SpeciesCategory.class, SpeciesCategoryDAO.class); - - SampleType sampleTypeTotal = findByProperties(sampleTypeDAO, SampleType.PROPERTY_NAME, "Total"); - Preconditions.checkNotNull(sampleTypeTotal); - SampleType sampleTypeUnsorted = findByProperties(sampleTypeDAO, SampleType.PROPERTY_NAME, "Unsorted"); - Preconditions.checkNotNull(sampleTypeUnsorted); - SampleType sampleTypeSorted = findByProperties(sampleTypeDAO, SampleType.PROPERTY_NAME, "Sorted"); - Preconditions.checkNotNull(sampleTypeSorted); - - SampleDataType sampleDataTypeMeanLength = findByProperties(sampleDataTypeDAO, SampleDataType.PROPERTY_NAME, "MeanLengthcm"); - Preconditions.checkNotNull(sampleDataTypeMeanLength); - SampleDataType sampleDataTypeMeanWeight = findByProperties(sampleDataTypeDAO, SampleDataType.PROPERTY_NAME, "MeanWeightg"); - Preconditions.checkNotNull(sampleDataTypeMeanWeight); - SampleDataType sampleDataTypeNoPerKg = findByProperties(sampleDataTypeDAO, SampleDataType.PROPERTY_NAME, "NoPerKg"); - Preconditions.checkNotNull(sampleDataTypeNoPerKg); - - Reader reader = getInputFileReader(inputFile); - Locale locale = getLocale(); - try { - Import<TotalSampleImportModelRow> importer = - EchoBaseImport.newImport(csvModel, reader); - - for (TotalSampleImportModelRow row : importer) { - - Operation operation = row.getOperation(); - - Species species = row.getSpecies(); - SizeCategory sizeCategory = row.getSizeCategory(); - - SpeciesCategory category = getSpeciesCategory( - speciesCategoryDAO, - species, - sizeCategory, - null - ); - - SampleType sampleType; - - if (EchoBasePredicates.IS_HORS_VRAC.apply(sizeCategory)) { - - // hors vrac case - sampleType = sampleTypeUnsorted; - - } else { - // none hors vrac case - sampleType = sampleTypeTotal; - } - - Sample sample = operation.getSample(category, sampleType); - - if (sample != null) { - - // can not have twice same sample - throw new ImportException( - l_(locale, "echobase.importError.duplicate.sample", - operation.getId(), - sampleType.getName(), - species.getBaracoudaCode(), - sizeCategory.getName())); - } - - if (sample == null) { - - // must create it - - sample = row.getSample(); - sample.setSpeciesCategory(category); - - sample.setSampleType(sampleType); - - sample = addSample(sampleDAO, - operation, - sample, - importResult); - } else { - sample = sample; - } - - // create datas - - if (row.getMeanLength() != null) { - - //create meanLength data - addSampleData(sampleDataDAO, - sampleDataTypeMeanLength, - null, - row.getMeanLength(), - sample, - importResult); - } - - if (row.getMeanWeight() != null) { - - //create meanWeight data - addSampleData(sampleDataDAO, - sampleDataTypeMeanWeight, - null, - row.getMeanWeight(), - sample, - importResult); - } - - if (row.getNoPerKg() != null) { - - //create noPerKg data - addSampleData(sampleDataDAO, - sampleDataTypeNoPerKg, - null, - row.getNoPerKg(), - sample, - importResult); - } - - Sample createdSortedSample = newInstance(sampleDAO); - createdSortedSample.setSampleType(sampleTypeSorted); - createdSortedSample.setSampleWeight(row.getSortedWeight()); - - // create sorted sample - addSample(sampleDAO, - operation, - createdSortedSample, - importResult); - - - } - - return importResult; - } catch (ImportRuntimeException e) { - throw new ImportException(locale, inputFile, e); - } finally { - closeReader(reader, inputFile); - } - } - - private CsvFileImportResult importSubSampleFile(InputFile inputFile, - Map<String, Operation> operationMap, - Map<String, Species> speciesMap, - Map<String, SizeCategory> sizeCategoryMap, - Map<String, SexCategory> sexCategoryMap - ) throws ImportException { - - if (log.isInfoEnabled()) { - log.info("Starts import of subSample from file " + - inputFile.getFileName()); - } - - CsvFileImportResult importResult = new CsvFileImportResult( - inputFile.getFileName()); - - SubSampleImportModel csvModel = new SubSampleImportModel(getCsvSeparator(), - operationMap, - speciesMap, - sizeCategoryMap, - sexCategoryMap); - - SampleDAO sampleDAO = getDAO(Sample.class, SampleDAO.class); - SampleDataDAO sampleDataDAO = getDAO(SampleData.class, SampleDataDAO.class); - SampleTypeDAO sampleTypeDAO = getDAO(SampleType.class, SampleTypeDAO.class); - SampleDataTypeDAO sampleDataTypeDAO = getDAO(SampleDataType.class, SampleDataTypeDAO.class); - SpeciesCategoryDAO speciesCategoryDAO = getDAO(SpeciesCategory.class, SpeciesCategoryDAO.class); - - SampleType sampleTypeSubsample = findByProperties(sampleTypeDAO, SampleType.PROPERTY_NAME, "Subsample"); - Preconditions.checkNotNull(sampleTypeSubsample); - - SampleDataType sampleDataTypeNumberAtLength = findByProperties(sampleDataTypeDAO, SampleDataType.PROPERTY_NAME, "NumberAtLength"); - Preconditions.checkNotNull(sampleDataTypeNumberAtLength); - SampleDataType sampleDataTypeWeightAtLength = findByProperties(sampleDataTypeDAO, SampleDataType.PROPERTY_NAME, "WeightAtLengthkg"); - Preconditions.checkNotNull(sampleDataTypeWeightAtLength); - - Reader reader = getInputFileReader(inputFile); - try { - Import<SubSampleImportModelRow> importer = - EchoBaseImport.newImport(csvModel, reader); - - for (SubSampleImportModelRow row : importer) { - - Operation operation = row.getOperation(); - - Species species = row.getSpecies(); - SizeCategory sizeCategory = row.getSizeCategory(); - SexCategory sexCategory = row.getSexCategory(); - - SpeciesCategory category = getSpeciesCategory( - speciesCategoryDAO, - species, - sizeCategory, - sexCategory - ); - - // find the sample with this category - Sample sample = operation.getSample(category, - sampleTypeSubsample); - - if (sample == null) { - - // must create it - sample = row.getSample(); - - sample.setSpeciesCategory(category); - sample.setSampleType(sampleTypeSubsample); - - sample = addSample(sampleDAO, - operation, - sample, - importResult); - } - - //create numberAtLength data - addSampleData(sampleDataDAO, - sampleDataTypeNumberAtLength, - "" + row.getLengthClass(), - row.getNumberAtLength(), - sample, - importResult); - - if (row.getWeightAtLength() != null) { - - //create weightAtLength data - addSampleData(sampleDataDAO, - sampleDataTypeWeightAtLength, - "" + row.getLengthClass(), - row.getWeightAtLength(), - sample, - importResult); - } - } - - return importResult; - } catch (ImportRuntimeException e) { - throw new ImportException(getLocale(), inputFile, e); - } finally { - closeReader(reader, inputFile); - } - } - - private CsvFileImportResult importBiometrySampleFile(InputFile inputFile, - Map<String, Operation> operationMap, - Map<String, Species> speciesMap - ) throws ImportException { - - if (log.isInfoEnabled()) { - log.info("Starts import of biometrySample from file " + - inputFile.getFileName()); - } - - CsvFileImportResult importResult = new CsvFileImportResult( - inputFile.getFileName()); - - BiometrySampleImportModel csvModel = - new BiometrySampleImportModel(getCsvSeparator()); - - SampleDAO sampleDAO = getDAO(Sample.class, SampleDAO.class); - SampleDataDAO sampleDataDAO = getDAO(SampleData.class, SampleDataDAO.class); - SampleTypeDAO sampleTypeDAO = getDAO(SampleType.class, SampleTypeDAO.class); - - SampleType sampleTypeIndividual = findByProperties(sampleTypeDAO, SampleType.PROPERTY_NAME, "Individual"); - Preconditions.checkNotNull(sampleTypeIndividual); - - Reader reader = getInputFileReader(inputFile); - try { - Import<BiometrySampleImportModelRow> importer = - EchoBaseImport.newImport(csvModel, reader); - - for (BiometrySampleImportModelRow row : importer) { - - //TODO - - } - - return importResult; - } catch (ImportRuntimeException e) { - throw new ImportException(getLocale(), inputFile, e); - } finally { - closeReader(reader, inputFile); - } - } - - private Sample addSample(SampleDAO dao, - Operation operation, - Sample sample, - CsvFileImportResult importResult) { - Preconditions.checkNotNull(operation); - Preconditions.checkNotNull(sample); - Sample result = create(dao, sample); - operation.addSample(result); - importResult.incrementsNumberCreated(EchoBaseEntityEnum.Sample); - return result; - } - - private SampleData addSampleData(SampleDataDAO dao, - SampleDataType sampleDataType, - String label, - float value, - Sample sample, - CsvFileImportResult importResult) { - SampleData sampleData = create(dao); - sampleData.setSampleDataType(sampleDataType); - sampleData.setDataValue(value); - sampleData.setDataLabel(label); - sample.addSampleData(sampleData); - - importResult.incrementsNumberCreated(EchoBaseEntityEnum.SampleData); - return sampleData; - } - -} Deleted: trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/CommonDataImportService.java =================================================================== --- trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/CommonDataImportService.java 2012-03-09 23:52:25 UTC (rev 344) +++ trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/CommonDataImportService.java 2012-03-10 00:53:51 UTC (rev 345) @@ -1,327 +0,0 @@ -/* - * #%L - * EchoBase :: Services - * - * $Id$ - * $HeadURL$ - * %% - * Copyright (C) 2011 - 2012 Ifremer, Codelutin - * %% - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * #L% - */ -package fr.ifremer.echobase.services; - -import com.google.common.base.Preconditions; -import com.google.common.collect.Lists; -import com.google.common.collect.Maps; -import fr.ifremer.echobase.EchoBaseFunctions; -import fr.ifremer.echobase.EchoBaseTechnicalException; -import fr.ifremer.echobase.InputFile; -import fr.ifremer.echobase.entities.EchoBaseEntityEnum; -import fr.ifremer.echobase.entities.EchoBaseUser; -import fr.ifremer.echobase.entities.data.Transect; -import fr.ifremer.echobase.entities.data.TransectDAO; -import fr.ifremer.echobase.entities.data.Transit; -import fr.ifremer.echobase.entities.data.TransitDAO; -import fr.ifremer.echobase.entities.data.Voyage; -import fr.ifremer.echobase.entities.data.VoyageDAO; -import fr.ifremer.echobase.entities.references.AreaOfOperation; -import fr.ifremer.echobase.entities.references.Mission; -import fr.ifremer.echobase.entities.references.MissionDAO; -import fr.ifremer.echobase.entities.references.Vessel; -import fr.ifremer.echobase.services.configurations.CommonDataImportConfiguration; -import fr.ifremer.echobase.services.csv.CsvFileImportResult; -import fr.ifremer.echobase.services.csv.EchoBaseImport; -import fr.ifremer.echobase.services.csv.TransectImportModel; -import fr.ifremer.echobase.services.csv.TransectImportModelRow; -import fr.ifremer.echobase.services.csv.TransitImportModel; -import fr.ifremer.echobase.services.csv.TransitImportModelRow; -import fr.ifremer.echobase.services.csv.VoyageImportModel; -import org.nuiton.topia.TopiaException; -import org.nuiton.util.csv.Import; -import org.nuiton.util.csv.ImportModel; -import org.nuiton.util.csv.ImportRuntimeException; - -import java.io.Reader; -import java.util.Arrays; -import java.util.Date; -import java.util.List; -import java.util.Locale; -import java.util.Map; - -import static org.nuiton.i18n.I18n.l_; - -/** - * Service to launch a "common data" import. - * - * @author tchemit <chemit@codelutin.com> - * @since 0.3 - */ -public class CommonDataImportService extends AbstractImportDataService<CommonDataImportConfiguration> { - - @Override - protected String getImportLabel() { - return l_(getLocale(), ImportDataMode.CommonData.getI18nKey()); - } - - @Override - protected List<CsvFileImportResult> startImport( - CommonDataImportConfiguration configuration, - EchoBaseUser user) throws ImportException { - - List<CsvFileImportResult> result = Lists.newLinkedList(); - - // get usable vessels - Map<String, Vessel> vesselMap = getEntitiesMap( - Vessel.class, EchoBaseFunctions.VESSEL_NAME); - - InputFile transectFile = configuration.getTransectFile(); - - switch (configuration.getImportMode()) { - - case ALL: { - - InputFile voyageFile = configuration.getVoyageFile(); - InputFile transitFile = configuration.getTransitFile(); - - CsvFileImportResult voyageResult = importVoyageFile( - voyageFile, configuration); - - CsvFileImportResult transitResult = importTransitFile( - transitFile, configuration); - - CsvFileImportResult transectResult = importTransectFile( - transectFile, vesselMap, configuration); - - addResultAndLog(result, voyageFile, voyageResult, user); - addResultAndLog(result, transitFile, transitResult, user); - addResultAndLog(result, transectFile, transectResult, user); - } - break; - case TRANSECT: { - - CsvFileImportResult transectResult = importTransectFile( - transectFile, vesselMap, configuration); - - addResultAndLog(result, transectFile, transectResult, user); - } - break; - } - return result; - } - - - public Mission createMission(Mission mission) throws MissionNameAlreadyExistException { - - Preconditions.checkNotNull(mission); - - try { - MissionDAO dao = getDAO(Mission.class, MissionDAO.class); - - // check mission name is unique - boolean exists = dao.existByProperties(Mission.PROPERTY_NAME, - mission.getName()); - - if (exists) { - throw new MissionNameAlreadyExistException(); - } - Mission result = create(dao, mission); - commitTransaction("Could not create mission " + - mission.getName()); - return result; - } catch (TopiaException eee) { - throw new EchoBaseTechnicalException(eee); - } - } - - protected CsvFileImportResult importVoyageFile( - InputFile inputFile, - CommonDataImportConfiguration configuration) throws ImportException { - - Mission mission = getEntityById(Mission.class, - configuration.getMissionId()); - AreaOfOperation areaOfOperation = - getEntityById(AreaOfOperation.class, - configuration.getAreaOfOperationId()); - - CsvFileImportResult importResult = new CsvFileImportResult( - inputFile.getFileName()); - - String voyageDescription = configuration.getVoyageDescription(); - String datum = configuration.getDatum(); - - VoyageDAO dao = getDAO(Voyage.class, VoyageDAO.class); - - ImportModel<Voyage> csvModel = - new VoyageImportModel(getCsvSeparator()); - - Reader reader = getInputFileReader(inputFile); - Import<Voyage> importer = EchoBaseImport.newImport(csvModel, reader); - Voyage newVoyage = null; - Locale locale = getLocale(); - try { - for (Voyage voyage : importer) { - - voyage.setMission(mission); - voyage.setAreaOfOperation(areaOfOperation); - voyage.setDescription(voyageDescription); - voyage.setDatum(datum); - - Voyage createdVoyage = create(dao, voyage); - if (newVoyage == null) { - newVoyage = createdVoyage; - } else { - // this means a voyage file with more than one row not possible... - - throw new ImportException( - l_(locale, "echobase.importError.can.only.import.one.voyage.atime")); - } - - importResult.incrementsNumberCreated(EchoBaseEntityEnum.Voyage); - } - - if (newVoyage == null) { - throw new ImportException( - l_(locale, "echobase.importError.no.voyage.imported")); - } - - // push back to id of the voyage in configuration for next imports - configuration.setVoyageId(newVoyage.getTopiaId()); - - return importResult; - } catch (ImportRuntimeException e) { - throw new ImportException(locale, inputFile, e); - } finally { - closeReader(reader, inputFile); - } - } - - protected CsvFileImportResult importTransitFile( - InputFile inputFile, - CommonDataImportConfiguration configuration) throws ImportException { - - CsvFileImportResult importResult = new CsvFileImportResult( - inputFile.getFileName()); - - // get voyage - Voyage voyage = getEntityById(Voyage.class, - configuration.getVoyageId()); - - // restrict voyage to use to this voyage - Map<String, Voyage> voyageMap = Maps.uniqueIndex( - Arrays.asList(voyage), EchoBaseFunctions.VOYAGE_NAME); - - String relatedActivity = configuration.getTransitRelatedActivity(); - - TransitImportModel csvModel = - new TransitImportModel(getCsvSeparator(), voyageMap); - - TransitDAO dao = getDAO(Transit.class, TransitDAO.class); - - Reader reader = getInputFileReader(inputFile); - try { - Import<TransitImportModelRow> importer = - EchoBaseImport.newImport(csvModel, reader); - for (TransitImportModelRow row : importer) { - - Transit transit = row.getTransit(); - - transit.setRelatedActivity(relatedActivity); - - Transit createdTransit = create(dao, transit); - - voyage.addTransit(createdTransit); - - importResult.incrementsNumberCreated(EchoBaseEntityEnum.Transit); - } - return importResult; - } catch (ImportRuntimeException e) { - throw new ImportException(getLocale(), inputFile, e); - } finally { - closeReader(reader, inputFile); - } - } - - private CsvFileImportResult importTransectFile( - InputFile inputFile, - Map<String, Vessel> vesselMap, - CommonDataImportConfiguration configuration) throws ImportException { - - CsvFileImportResult importResult = new CsvFileImportResult( - inputFile.getFileName()); - - // get voyage - Voyage voyage = getEntityById(Voyage.class, - configuration.getVoyageId()); - - // restrict voyage to use to this voyage - Map<String, Voyage> voyageMap = Maps.uniqueIndex( - Arrays.asList(voyage), EchoBaseFunctions.VOYAGE_NAME); - - String datum = configuration.getDatum(); - String license = configuration.getTransectLicence(); - String geospatialVerticalPositive = - configuration.getTransectGeospatialVerticalPositive(); - String binUnitsPingAxis = configuration.getTransectBinUnitsPingAxis(); - - TransectImportModel csvModel = - new TransectImportModel(getCsvSeparator(), - voyageMap, - vesselMap); - - TransectDAO dao = getDAO(Transect.class, TransectDAO.class); - - Reader reader = getInputFileReader(inputFile); - try { - Import<TransectImportModelRow> importer = - EchoBaseImport.newImport(csvModel, reader); - for (TransectImportModelRow row : importer) { - - Transect transect = row.getTransect(); - - Date timeCoverageStart = transect.getTimeCoverageStart(); - Date timeCoverageEnd = transect.getTimeCoverageEnd(); - - Transit transit = voyage.getTransit(timeCoverageStart, - timeCoverageEnd); - if (transit == null) { - throw new ImportException( - l_(getLocale(), "echobase.importError.no.transit.between.date", - voyage.getName(), timeCoverageStart, timeCoverageEnd)); - } - - transect.setDatum(datum); - transect.setLicence(license); - transect.setGeospatialVerticalPositive( - geospatialVerticalPositive); - transect.setBinUnitsPingAxis(binUnitsPingAxis); - - Transect createdTransect = create(dao, transect); - - transit.addTransect(createdTransect); - importResult.incrementsNumberCreated(EchoBaseEntityEnum.Transect); - } - - return importResult; - - } catch (ImportRuntimeException e) { - throw new ImportException(getLocale(), inputFile, e); - } finally { - closeReader(reader, inputFile); - } - } - - -} Deleted: trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/ImportDataMode.java =================================================================== --- trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/ImportDataMode.java 2012-03-09 23:52:25 UTC (rev 344) +++ trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/ImportDataMode.java 2012-03-10 00:53:51 UTC (rev 345) @@ -1,63 +0,0 @@ -/* - * #%L - * EchoBase :: Services - * - * $Id$ - * $HeadURL$ - * %% - * Copyright (C) 2011 Ifremer, Codelutin - * %% - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * #L% - */ -package fr.ifremer.echobase.services; - -import fr.ifremer.echobase.I18nAble; - -import static org.nuiton.i18n.I18n.n_; - -/** - * To define the mode of import data (says files to be imported). - * - * @author tchemit <chemit@codelutin.com> - * @since 0.2 - */ -public enum ImportDataMode implements I18nAble { - - /** Import Voyage / Transit / Transect. */ - CommonData(n_("echobase.common.importDataMode.commonData")), - - /** Import operations common data. */ - Operation(n_("echobase.common.importDataMode.operation")), - - /** Import catches data (unsorted, total, but no individual ones). */ - CatchesData(n_("echobase.common.importDataMode.catchesData")), - - /** Import accoustic data (Cells ESDU and Elementary). */ - AccousticData(n_("echobase.common.importDataMode.accousticData")), - - /** Import results. */ - Results(n_("echobase.common.importDataMode.results")); - - private final String i18nKey; - - ImportDataMode(String i18nKey) { - this.i18nKey = i18nKey; - } - - @Override - public String getI18nKey() { - return i18nKey; - } -} Deleted: trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/MissionNameAlreadyExistException.java =================================================================== --- trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/MissionNameAlreadyExistException.java 2012-03-09 23:52:25 UTC (rev 344) +++ trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/MissionNameAlreadyExistException.java 2012-03-10 00:53:51 UTC (rev 345) @@ -1,34 +0,0 @@ -/* - * #%L - * EchoBase :: Services - * - * $Id$ - * $HeadURL$ - * %% - * Copyright (C) 2011 - 2012 Ifremer, Codelutin - * %% - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * #L% - */ -package fr.ifremer.echobase.services; - -/** - * TODO - * - * @author tchemit <chemit@codelutin.com> - * @since 0.3 - */ -public class MissionNameAlreadyExistException extends Exception { - private static final long serialVersionUID = 1L; -} Deleted: trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/OperationImportService.java =================================================================== --- trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/OperationImportService.java 2012-03-09 23:52:25 UTC (rev 344) +++ trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/OperationImportService.java 2012-03-10 00:53:51 UTC (rev 345) @@ -1,302 +0,0 @@ -/* - * #%L - * EchoBase :: Services - * - * $Id$ - * $HeadURL$ - * %% - * Copyright (C) 2011 - 2012 Ifremer, Codelutin - * %% - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * #L% - */ -package fr.ifremer.echobase.services; - -import com.google.common.collect.Lists; -import com.google.common.collect.Maps; -import fr.ifremer.echobase.EchoBaseFunctions; -import fr.ifremer.echobase.InputFile; -import fr.ifremer.echobase.entities.EchoBaseEntityEnum; -import fr.ifremer.echobase.entities.EchoBaseUser; -import fr.ifremer.echobase.entities.data.GearMetadataValue; -import fr.ifremer.echobase.entities.data.GearMetadataValueDAO; -import fr.ifremer.echobase.entities.data.Operation; -import fr.ifremer.echobase.entities.data.OperationDAO; -import fr.ifremer.echobase.entities.data.OperationMetadataValue; -import fr.ifremer.echobase.entities.data.OperationMetadataValueDAO; -import fr.ifremer.echobase.entities.data.Transect; -import fr.ifremer.echobase.entities.data.Transit; -import fr.ifremer.echobase.entities.data.Voyage; -import fr.ifremer.echobase.entities.references.DepthStratum; -import fr.ifremer.echobase.entities.references.Gear; -import fr.ifremer.echobase.entities.references.GearMetadata; -import fr.ifremer.echobase.entities.references.OperationMetadata; -import fr.ifremer.echobase.entities.references.Vessel; -import fr.ifremer.echobase.services.configurations.OperationImportConfiguration; -import fr.ifremer.echobase.services.csv.CsvFileImportResult; -import fr.ifremer.echobase.services.csv.EchoBaseImport; -import fr.ifremer.echobase.services.csv.GearMetadataValueImportModel; -import fr.ifremer.echobase.services.csv.GearMetadataValueImportModelRow; -import fr.ifremer.echobase.services.csv.OperationImportModel; -import fr.ifremer.echobase.services.csv.OperationImportModelRow; -import fr.ifremer.echobase.services.csv.OperationMetadataValueImportModel; -import fr.ifremer.echobase.services.csv.OperationMetadataValueImportModelRow; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.nuiton.util.csv.Import; -import org.nuiton.util.csv.ImportRuntimeException; - -import java.io.Reader; -import java.util.Collection; -import java.util.Date; -import java.util.List; -import java.util.Map; - -import static org.nuiton.i18n.I18n.l_; - -/** - * Service to launch a "operations" import. - * - * @author tchemit <chemit@codelutin.com> - * @since 0.3 - */ -public class OperationImportService extends AbstractImportDataService<OperationImportConfiguration> { - - /** Logger. */ - private static final Log log = - LogFactory.getLog(OperationImportService.class); - - @Override - protected String getImportLabel() { - return l_(getLocale(), ImportDataMode.Operation.getI18nKey()); - } - - @Override - protected List<CsvFileImportResult> startImport( - OperationImportConfiguration configuration, - EchoBaseUser user) throws ImportException { - - List<CsvFileImportResult> result = Lists.newLinkedList(); - - // get selected voyage - Voyage voyage = getEntityById(Voyage.class, - configuration.getVoyageId()); - - // get usable vessels from the voyage by their name - Map<String, Vessel> vesselMap = Maps.uniqueIndex( - voyage.getAllVessels(), EchoBaseFunctions.VESSEL_NAME); - - // get usable gears by their casino gear name - Map<String, Gear> gearMap = getEntitiesMap( - Gear.class, EchoBaseFunctions.GEAR_CASINO_GEAR_NAME); - - InputFile operationFile = configuration.getOperationFile(); - CsvFileImportResult nbOperations = importOperationFile( - operationFile, - voyage, - vesselMap, - gearMap); - - // get all operation for this voyage and this vessel - Collection<Operation> operations = voyage.getAllOperations(); - - // split them by operation Id - Map<String, Operation> operationMap = Maps.uniqueIndex( - operations, EchoBaseFunctions.OPERATION_ID); - - InputFile operationMetadataFile = configuration.getOperationMetadataFile(); - CsvFileImportResult nbOperationMetas = importOperationMetadataFile( - operationMetadataFile, - vesselMap, - operationMap); - - InputFile gearMetadataFile = configuration.getGearMetadataFile(); - CsvFileImportResult nbGearMetas = importGearMetadataFile( - gearMetadataFile, - vesselMap, - gearMap, - operationMap); - - addResultAndLog(result, operationFile, nbOperations, user); - - addResultAndLog(result, operationMetadataFile, nbOperationMetas, user); - - addResultAndLog(result, gearMetadataFile, nbGearMetas, user); - - return result; - } - - protected CsvFileImportResult importOperationFile( - InputFile inputFile, - Voyage voyage, - Map<String, Vessel> vesselMap, - Map<String, Gear> gearMap) throws ImportException { - - if (log.isInfoEnabled()) { - log.info("Starts import of operation from file " + - inputFile.getFileName()); - } - - CsvFileImportResult importResult = new CsvFileImportResult( - inputFile.getFileName()); - - Map<String, DepthStratum> depthStratumMap = getEntitiesMap( - DepthStratum.class, EchoBaseFunctions.DEPTH_STRATUM_ID); - - OperationImportModel csvModel = - new OperationImportModel(getCsvSeparator(), - vesselMap, - gearMap, - depthStratumMap); - - OperationDAO dao = getDAO(Operation.class, OperationDAO.class); - - Reader reader = getInputFileReader(inputFile); - try { - Import<OperationImportModelRow> importer = - EchoBaseImport.newImport(csvModel, reader); - - for (OperationImportModelRow row : importer) { - - Vessel vessel = row.getVessel(); - - Operation operation = row.getOperation(); - - Date startTime = operation.getGearShootingStartTime(); - Date endTime = operation.getGearShootingEndTime(); - - Transit transit = voyage.getTransit(startTime, endTime); - - Transect transect = transit.getTransect(vessel); - - Operation createdOperation = create(dao, operation); - - transect.addOperation(createdOperation); - - importResult.incrementsNumberCreated(EchoBaseEntityEnum.Operation); - } - - return importResult; - } catch (ImportRuntimeException e) { - throw new ImportException(getLocale(), inputFile, e); - } finally { - closeReader(reader, inputFile); - } - } - - protected CsvFileImportResult importOperationMetadataFile( - InputFile inputFile, - Map<String, Vessel> vesselMap, - Map<String, Operation> operationMap) throws ImportException { - - if (log.isInfoEnabled()) { - log.info("Starts import of operation metadata values from file " + - inputFile.getFileName()); - } - - CsvFileImportResult importResult = new CsvFileImportResult( - inputFile.getFileName()); - - Map<String, OperationMetadata> operationMetadatasByName = - getEntitiesMap(OperationMetadata.class, - EchoBaseFunctions.OPERATION_METADATA_NAME); - - OperationMetadataValueImportModel csvModel = - new OperationMetadataValueImportModel(getCsvSeparator(), - vesselMap, - operationMetadatasByName, - operationMap); - - OperationMetadataValueDAO dao = getDAO( - OperationMetadataValue.class, - OperationMetadataValueDAO.class); - - Reader reader = getInputFileReader(inputFile); - try { - Import<OperationMetadataValueImportModelRow> importer = - EchoBaseImport.newImport(csvModel, reader); - - for (OperationMetadataValueImportModelRow row : importer) { - - Operation operation = row.getOperation(); - - OperationMetadataValue operationMetadataValueToCreate = - row.getOperationMetadataValue(); - OperationMetadataValue operationMetadataValue = create( - dao, operationMetadataValueToCreate); - - operation.addOperationMetadataValue(operationMetadataValue); - importResult.incrementsNumberCreated(EchoBaseEntityEnum.OperationMetadataValue); - } - return importResult; - } catch (ImportRuntimeException e) { - throw new ImportException(getLocale(), inputFile, e); - } finally { - closeReader(reader, inputFile); - } - } - - protected CsvFileImportResult importGearMetadataFile( - InputFile inputFile, - Map<String, Vessel> vesselMap, - Map<String, Gear> gearMap, - Map<String, Operation> operationMap) throws ImportException { - - if (log.isInfoEnabled()) { - log.info("Starts import of gear metadata values from file " + - inputFile.getFileName()); - } - - CsvFileImportResult importResult = new CsvFileImportResult( - inputFile.getFileName()); - - Map<String, GearMetadata> gearMetadatasByType = - getEntitiesMap(GearMetadata.class, - EchoBaseFunctions.GEAR_METADATA_NAME); - - GearMetadataValueImportModel csvModel = - new GearMetadataValueImportModel(getCsvSeparator(), - vesselMap, - gearMetadatasByType, - gearMap, - operationMap); - - GearMetadataValueDAO dao = getDAO(GearMetadataValue.class, - GearMetadataValueDAO.class); - - Reader reader = getInputFileReader(inputFile); - try { - Import<GearMetadataValueImportModelRow> importer = - EchoBaseImport.newImport(csvModel, reader); - - for (GearMetadataValueImportModelRow row : importer) { - - Operation operation = row.getOperation(); - - GearMetadataValue gearMetadataValuetoCreate = - row.getGearMetadataValue(); - GearMetadataValue gearMetadataValue = create( - dao, gearMetadataValuetoCreate); - - operation.addGearMetadataValue(gearMetadataValue); - importResult.incrementsNumberCreated(EchoBaseEntityEnum.GearMetadataValue); - } - return importResult; - } catch (ImportRuntimeException e) { - throw new ImportException(getLocale(), inputFile, e); - } finally { - closeReader(reader, inputFile); - } - } -} Deleted: trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/ResultsImportService.java =================================================================== --- trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/ResultsImportService.java 2012-03-09 23:52:25 UTC (rev 344) +++ trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/ResultsImportService.java 2012-03-10 00:53:51 UTC (rev 345) @@ -1,456 +0,0 @@ -/* - * #%L - * EchoBase :: Services - * - * $Id$ - * $HeadURL$ - * %% - * Copyright (C) 2011 - 2012 Ifremer, Codelutin - * %% - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * #L% - */ -package fr.ifremer.echobase.services; - -import com.google.common.base.Predicate; -import com.google.common.collect.Iterables; -import com.google.common.collect.Lists; -import com.google.common.collect.Maps; -import fr.ifremer.echobase.EchoBaseFunctions; -import fr.ifremer.echobase.EchoBasePredicates; -import fr.ifremer.echobase.InputFile; -import fr.ifremer.echobase.entities.EchoBaseEntityEnum; -import fr.ifremer.echobase.entities.EchoBaseUser; -import fr.ifremer.echobase.entities.data.Cell; -import fr.ifremer.echobase.entities.data.CellDAO; -import fr.ifremer.echobase.entities.data.Voyage; -import fr.ifremer.echobase.entities.references.DepthStratum; -import fr.ifremer.echobase.entities.data.Echotype; -import fr.ifremer.echobase.entities.data.LengthAgeKey; -import fr.ifremer.echobase.entities.data.LengthWeightKey; -import fr.ifremer.echobase.entities.data.LengthWeightKeyDAO; -import fr.ifremer.echobase.entities.references.SizeCategory; -import fr.ifremer.echobase.entities.references.Species; -import fr.ifremer.echobase.entities.references.SpeciesCategory; -import fr.ifremer.echobase.entities.references.SpeciesCategoryDAO; -import fr.ifremer.echobase.entities.references.Strata; -import fr.ifremer.echobase.services.configurations.ResultsImportConfiguration; -import fr.ifremer.echobase.services.configurations.ResultsImportMode; -import fr.ifremer.echobase.services.csv.CsvFileImportResult; -import fr.ifremer.echobase.services.csv.EchoBaseImport; -import fr.ifremer.echobase.services.csv.EchotypeImportModel; -import fr.ifremer.echobase.services.csv.EchotypeImportModelRow; -import fr.ifremer.echobase.services.csv.LengthAgeKeyImportModel; -import fr.ifremer.echobase.services.csv.LengthAgeKeyImportModelRow; -import fr.ifremer.echobase.services.csv.LengthWeightKeyImportModel; -import fr.ifremer.echobase.services.csv.LengthWeightKeyImportModelRow; -import fr.ifremer.echobase.services.csv.RegionCellAssociationImportModel; -import fr.ifremer.echobase.services.csv.RegionCellAssociationImportModelRow; -import fr.ifremer.echobase.services.csv.RegionCellImportModel; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.nuiton.topia.persistence.TopiaDAO; -import org.nuiton.util.csv.Import; -import org.nuiton.util.csv.ImportRuntimeException; - -import java.io.Reader; -import java.util.Arrays; -import java.util.Collection; -import java.util.List; -import java.util.Map; - -import static org.nuiton.i18n.I18n.l_; - -/** - * Service to launch a "results" import. - * - * @author tchemit <chemit@codelutin.com> - * @since 0.3 - */ -public class ResultsImportService extends AbstractImportDataService<ResultsImportConfiguration> { - - /** Logger. */ - private static final Log log = - LogFactory.getLog(ResultsImportService.class); - - @Override - protected String getImportLabel() { - return l_(getLocale(), ImportDataMode.Results.getI18nKey()); - } - - @Override - public List<CsvFileImportResult> startImport(ResultsImportConfiguration configuration, - EchoBaseUser user) throws ImportException { - - List<CsvFileImportResult> result = Lists.newLinkedList(); - - ResultsImportMode importMode = configuration.getImportMode(); - - switch (importMode) { - case VOYAGE: - - // get selected voyage - Voyage voyage = getEntityById(Voyage.class, - configuration.getVoyageId()); - - // authorize only the selected voyage to be imported - Map<String, Voyage> voyageMap = Maps.uniqueIndex(Arrays.asList( - voyage), EchoBaseFunctions.VOYAGE_NAME); - - Map<String, Species> speciesMap = getEntitiesMap( - Species.class, - EchoBaseFunctions.SPECIES_BARACOUDA_CODE); - - InputFile inputFile = configuration.getLengthAgeKeyFile(); - - if (inputFile.hasFile()) { - - CsvFileImportResult importResult = importLenthAgeKey( - inputFile, - voyageMap, - speciesMap - ); - addResultAndLog(result, inputFile, importResult, user); - } - - inputFile = configuration.getLengthWeightKeyFile(); - if (inputFile.hasFile()) { - - CsvFileImportResult importResult = importLenghtWeightKey( - inputFile, - voyageMap, - speciesMap - ); - addResultAndLog(result, inputFile, importResult, user); - } - - inputFile = configuration.getEchotypeFile(); - if (inputFile.hasFile()) { - - CsvFileImportResult importResult = importEchotypeFile( - inputFile, - voyageMap, - speciesMap - ); - addResultAndLog(result, inputFile, importResult, user); - } - break; - - case REGION: - - if (configuration.getRegionsFile().hasFile()) { - CsvFileImportResult importResult = importRegionsFile( - configuration.getRegionsFile()); - - addResultAndLog(result, configuration.getRegionsFile(), - importResult, user); - } - if (configuration.getAssociationFile().hasFile()) { - CsvFileImportResult importResult = importRegionAssociationFile( - configuration.getAssociationFile()); - - addResultAndLog(result, configuration.getAssociationFile(), - importResult, user); - } - break; - - case ACOUSTIC: - if (configuration.getRegionsFile().hasFile()) { - CsvFileImportResult importResult = importRegionsFile( - configuration.getRegionsFile()); - - addResultAndLog(result, configuration.getRegionsFile(), - importResult, user); - } - if (configuration.getAssociationFile().hasFile()) { - CsvFileImportResult importResult = importRegionAssociationFile( - configuration.getAssociationFile()); - - addResultAndLog(result, configuration.getAssociationFile(), - importResult, user); - } - break; - } - return result; - } - - protected CsvFileImportResult importRegionsFile(InputFile inputFile) throws ImportException { - - if (log.isInfoEnabled()) { - log.info("Starts import of region from file " + - inputFile.getFileName()); - } - - CsvFileImportResult importResult = new CsvFileImportResult( - inputFile.getFileName()); - - RegionCellImportModel csvModel = new RegionCellImportModel( - serviceContext.getConfiguration().getCsvSeparator()); - - CellDAO dao = getDAO(Cell.class, CellDAO.class); - - Reader reader = getInputFileReader(inputFile); - try { - Import<Cell> importer = - EchoBaseImport.newImport(csvModel, reader); - - for (Cell row : importer) { - - //TODO - - importResult.incrementsNumberCreated(EchoBaseEntityEnum.Cell); - } - return importResult; - } catch (ImportRuntimeException e) { - throw new ImportException(getLocale(), inputFile, e); - } finally { - closeReader(reader, inputFile); - } - } - - protected CsvFileImportResult importRegionAssociationFile(InputFile inputFile) throws ImportException { - RegionCellAssociationImportModel csvModel = new RegionCellAssociationImportModel( - serviceContext.getConfiguration().getCsvSeparator()); - - if (log.isInfoEnabled()) { - log.info("Starts import of region association from file " + - inputFile.getFileName()); - } - - CsvFileImportResult importResult = new CsvFileImportResult( - inputFile.getFileName()); - - CellDAO dao = getDAO(Cell.class, CellDAO.class); - - Reader reader = getInputFileReader(inputFile); - try { - Import<RegionCellAssociationImportModelRow> importer = - EchoBaseImport.newImport(csvModel, reader); - - for (RegionCellAssociationImportModelRow row : importer) { - - //TODO - - importResult.incrementsNumberUpdated(EchoBaseEntityEnum.Cell); - } - - return importResult; - } catch (ImportRuntimeException e) { - throw new ImportException(getLocale(), inputFile, e); - } finally { - closeReader(reader, inputFile); - } - } - - protected CsvFileImportResult importLenghtWeightKey(InputFile inputFile, - Map<String, Voyage> voyageMap, - Map<String, Species> speciesMap) throws ImportException { - - if (log.isInfoEnabled()) { - log.info("Starts import of lenthWeightKey from file " + - inputFile.getFileName()); - } - - CsvFileImportResult importResult = new CsvFileImportResult( - inputFile.getFileName()); - - Map<String, SizeCategory> sizeCategoryMap = getEntitiesMap( - SizeCategory.class, - EchoBaseFunctions.SIZE_CATEGORY_NAME); - - LengthWeightKeyImportModel csvModel = new LengthWeightKeyImportModel( - serviceContext.getConfiguration().getCsvSeparator(), - voyageMap, - sizeCategoryMap, - speciesMap); - - LengthWeightKeyDAO dao = getDAO(LengthWeightKey.class, - LengthWeightKeyDAO.class); - - SpeciesCategoryDAO speciesCategoryDAO = - getDAO(SpeciesCategory.class, SpeciesCategoryDAO.class); - - Reader reader = getInputFileReader(inputFile); - try { - Import<LengthWeightKeyImportModelRow> importer = - EchoBaseImport.newImport(csvModel, reader); - - for (LengthWeightKeyImportModelRow row : importer) { - - Voyage voyage = row.getVoyage(); - - LengthWeightKey lengthWeightKeyToCreate = - row.getLengthWeightKey(); - LengthWeightKey lengthWeightKey = create( - dao, lengthWeightKeyToCreate); - - //TODO should we import it ? - //lengthWeightKey.setMetadata(row.getMetadata()); - - Species species = row.getSpecies(); - SizeCategory sizeCategory = row.getSizeCategory(); - - // find speciesCategory - SpeciesCategory speciesCategory = getSpeciesCategory( - speciesCategoryDAO, - species, sizeCategory, - null - ); - - lengthWeightKey.setSpeciesCategory(speciesCategory); - - // attach it to voyage - voyage.addLengthWeightKey(lengthWeightKey); - - importResult.incrementsNumberCreated(EchoBaseEntityEnum.LengthWeightKey); - } - - return importResult; - } catch (ImportRuntimeException e) { - throw new ImportException(getLocale(), inputFile, e); - } finally { - closeReader(reader, inputFile); - } - } - - protected CsvFileImportResult importLenthAgeKey(InputFile inputFile, - Map<String, Voyage> voyageMap, - Map<String, Species> speciesMap) throws ImportException { - - if (log.isInfoEnabled()) { - log.info("Starts import of lenthAgeKey from file " + - inputFile.getFileName()); - } - - CsvFileImportResult importResult = new CsvFileImportResult( - inputFile.getFileName()); - - Map<String, Strata> strataMap = getEntitiesMap( - Strata.class, EchoBaseFunctions.STRATA_BY_NAME); - - LengthAgeKeyImportModel csvModel = new LengthAgeKeyImportModel( - serviceContext.getConfiguration().getCsvSeparator(), - voyageMap, - strataMap, - speciesMap); - - TopiaDAO<LengthAgeKey> dao = getDAO(LengthAgeKey.class); - - Reader reader = getInputFileReader(inputFile); - try { - Import<LengthAgeKeyImportModelRow> importer = - EchoBaseImport.newImport(csvModel, reader); - - for (LengthAgeKeyImportModelRow row : importer) { - - Voyage voyage = row.getVoyage(); - - LengthAgeKey lengthAgeKeyToCreate = row.getLengthAgeKey(); - LengthAgeKey lengthAgeKey = create(dao, lengthAgeKeyToCreate); - - // attach it to voyage - voyage.addLengthAgeKey(lengthAgeKey); - - importResult.incrementsNumberCreated(EchoBaseEntityEnum.LengthAgeKey); - } - return importResult; - } catch (ImportRuntimeException e) { - throw new ImportException(getLocale(), inputFile, e); - } finally { - closeReader(reader, inputFile); - } - } - - private CsvFileImportResult importEchotypeFile(InputFile inputFile, - Map<String, Voyage> voyageMap, - Map<String, Species> speciesMap) throws ImportException { - - if (log.isInfoEnabled()) { - log.info("Starts import of echotype from file " + - inputFile.getFileName()); - } - - CsvFileImportResult importResult = new CsvFileImportResult( - inputFile.getFileName()); - - Map<String, DepthStratum> depthStratumMap = getEntitiesMap( - DepthStratum.class, - EchoBaseFunctions.DEPTH_STRATUM_ID); - - EchotypeImportModel csvModel = new EchotypeImportModel( - serviceContext.getConfiguration().getCsvSeparator(), - voyageMap, - depthStratumMap, - speciesMap); - - TopiaDAO<Echotype> dao = getDAO(Echotype.class); - - Reader reader = getInputFileReader(inputFile); - try { - Import<EchotypeImportModelRow> importer = - EchoBaseImport.newImport(csvModel, reader); - - for (EchotypeImportModelRow row : importer) { - - Voyage voyage = row.getVoyage(); - - Echotype rowEchotype = row.getEchotype(); - - String echotypeName = rowEchotype.getName(); - - // check if there is a echotype for the voyage and this name - Echotype echotype = null; - - if (!voyage.isEchotypeEmpty()) { - - Collection<Echotype> echotypes = voyage.getEchotype(); - Predicate<Echotype> predicate = - EchoBasePredicates.newEchotypeByNamePredicate( - echotypeName); - echotype = Iterables.find(echotypes, predicate, null); - - } - - if (echotype == null) { - - // creates it - echotype = create(dao, rowEchotype); - - // attach it to voyage - voyage.addEchotype(echotype); - - importResult.incrementsNumberCreated(EchoBaseEntityEnum.Echotype); - - } else { - - Species species = rowEchotype.getSpecies().iterator().next(); - Species existingSpecie = - echotype.getSpeciesByTopiaId(species.getTopiaId()); - - if (existingSpecie == null) { - - // add this species - echotype.addSpecies(species); - } - } - } - - return importResult; - } catch (ImportRuntimeException e) { - throw new ImportException(getLocale(), inputFile, e); - } finally { - closeReader(reader, inputFile); - } - } -} Copied: trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/exceptions/MissionNameAlreadyExistException.java (from rev 334, trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/MissionNameAlreadyExistException.java) =================================================================== --- trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/exceptions/MissionNameAlreadyExistException.java (rev 0) +++ trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/exceptions/MissionNameAlreadyExistException.java 2012-03-10 00:53:51 UTC (rev 345) @@ -0,0 +1,34 @@ +/* + * #%L + * EchoBase :: Services + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2011 - 2012 Ifremer, Codelutin + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * #L% + */ +package fr.ifremer.echobase.services.exceptions; + +/** + * TODO + * + * @author tchemit <chemit@codelutin.com> + * @since 0.3 + */ +public class MissionNameAlreadyExistException extends Exception { + private static final long serialVersionUID = 1L; +} Property changes on: trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/exceptions/MissionNameAlreadyExistException.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Copied: trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/importdata/AbstractImportDataService.java (from rev 334, trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/AbstractImportDataService.java) =================================================================== --- trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/importdata/AbstractImportDataService.java (rev 0) +++ trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/importdata/AbstractImportDataService.java 2012-03-10 00:53:51 UTC (rev 345) @@ -0,0 +1,228 @@ +/* + * #%L + * EchoBase :: Services + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2011 - 2012 Ifremer, Codelutin + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * #L% + */ +package fr.ifremer.echobase.services.importdata; + +import com.google.common.base.Charsets; +import com.google.common.base.Function; +import com.google.common.collect.Maps; +import fr.ifremer.echobase.EchoBaseTechnicalException; +import fr.ifremer.echobase.InputFile; +import fr.ifremer.echobase.entities.EchoBaseEntityEnum; +import fr.ifremer.echobase.entities.EchoBaseUser; +import fr.ifremer.echobase.entities.EntityModificationLog; +import fr.ifremer.echobase.entities.references.SexCategory; +import fr.ifremer.echobase.entities.references.SizeCategory; +import fr.ifremer.echobase.entities.references.Species; +import fr.ifremer.echobase.entities.references.SpeciesCategory; +import fr.ifremer.echobase.entities.references.SpeciesCategoryDAO; +import fr.ifremer.echobase.services.EchoBaseServiceSupport; +import fr.ifremer.echobase.services.ImportException; +import fr.ifremer.echobase.services.configurations.AbstractImportConfiguration; +import fr.ifremer.echobase.services.csv.CsvFileImportResult; +import org.nuiton.topia.TopiaException; +import org.nuiton.topia.persistence.TopiaDAO; +import org.nuiton.topia.persistence.TopiaEntity; + +import java.io.BufferedReader; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.Reader; +import java.util.List; +import java.util.Map; +import java.util.Set; + +/** + * Abstract data import service. + * + * @author tchemit <chemit@codelutin.com> + * @since 0.3 + */ +public abstract class AbstractImportDataService<M extends AbstractImportConfiguration> extends EchoBaseServiceSupport { + + public final List<CsvFileImportResult> doImport(M configuration, + EchoBaseUser user) throws ImportException { + + + try { + List<CsvFileImportResult> result = startImport(configuration, user); + + // do commit + commitTransaction("Could not execute import " + getImportLabel()); + return result; + } catch (ImportException e) { + throw e; + } catch (Exception e) { + throw new ImportException("Could not do import " + e.getMessage(), e); + } + } + + protected abstract String getImportLabel(); + + protected abstract List<CsvFileImportResult> startImport(M configuration, + EchoBaseUser user) throws ImportException; + + protected void addResultAndLog(List<CsvFileImportResult> result, + InputFile inputFile, + CsvFileImportResult importResult, + EchoBaseUser user) { + + Set<EchoBaseEntityEnum> entityTypes = importResult.getEntityTypes(); + if (!entityTypes.isEmpty()) { + + result.add(importResult); + + String importType = getImportLabel(); + + StringBuilder description = new StringBuilder(); + description.append("Depuis Fichier ").append(inputFile.getFileName()); + TopiaDAO<EntityModificationLog> dao = + getDAO(EntityModificationLog.class); + + for (EchoBaseEntityEnum entityType : entityTypes) { + int numberCreated = importResult.getNumberCreated(entityType); + int numberUpdated = importResult.getNumberUpdated(entityType); + if (numberCreated > 0) { + description.append(" [ nombre de création : ").append(numberCreated).append("]"); + } + if (numberUpdated > 0) { + description.append(" [ nombre de mises à jour : ").append(numberUpdated).append("]"); + } + + create(dao, + EntityModificationLog.PROPERTY_ENTITY_TYPE, entityType.name(), + EntityModificationLog.PROPERTY_ENTITY_ID, importType, + EntityModificationLog.PROPERTY_MODIFICATION_USER, user.getEmail(), + EntityModificationLog.PROPERTY_MODIFICATION_DATE, newDate(), + EntityModificationLog.PROPERTY_MODIFICATION_TEXT, description.toString() + ); + } + + } + } + + protected Reader getInputFileReader(InputFile inputFile) throws EchoBaseTechnicalException { + try { + Reader reader = new BufferedReader(new InputStreamReader( + new FileInputStream(inputFile.getFile()), Charsets.UTF_8)); + return reader; + } catch (FileNotFoundException e) { + throw new EchoBaseTechnicalException( + "Could not find import file " + inputFile.getFile(), e); + } + } + + protected void closeReader(Reader reader, InputFile inputFile) { + try { + reader.close(); + } catch (IOException e) { + throw new EchoBaseTechnicalException( + "Could not close reader on file " + + inputFile.getFile(), e); + } + } + + protected <E extends TopiaEntity, K> Map<K, E> getEntitiesMap( + Class<E> entityType, + Function<E, K> function) { + List<E> allVoyages = getEntities(entityType); + Map<K, E> voyageMap = Maps.uniqueIndex( + allVoyages, function); + return voyageMap; + } + + protected <E extends TopiaEntity> E newInstance(TopiaDAO<E> dao) { + try { + E result = dao.newInstance(); + return result; + } catch (TopiaException e) { + throw new EchoBaseTechnicalException("Could not create new instance of entity", e); + } + } + + protected <E extends TopiaEntity> E create(TopiaDAO<E> dao) { + try { + E result = dao.create(); + return result; + } catch (TopiaException e) { + throw new EchoBaseTechnicalException("Could not create entity", e); + } + } + + protected <E extends TopiaEntity> E create(TopiaDAO<E> dao, E entity) { + try { + E result = dao.create(entity); + return result; + } catch (TopiaException e) { + throw new EchoBaseTechnicalException("Could not create entity", e); + } + } + + protected <E extends TopiaEntity> E create(TopiaDAO<E> dao, + Object... properties) { + try { + E result = dao.create(properties); + return result; + } catch (TopiaException e) { + throw new EchoBaseTechnicalException("Could not create entity", e); + } + } + + protected <E extends TopiaEntity> E findByProperties(TopiaDAO<E> dao, + String propertyName, + Object value, + Object... others) { + try { + E result = dao.findByProperties(propertyName, value, others); + return result; + } catch (TopiaException e) { + throw new EchoBaseTechnicalException("Could not obtain query db", e); + } + } + + protected SpeciesCategory getSpeciesCategory(SpeciesCategoryDAO speciesCategoryDAO, + Species species, + SizeCategory sizeCategory, + SexCategory sexCategory) { + SpeciesCategory category = findByProperties( + speciesCategoryDAO, + SpeciesCategory.PROPERTY_SPECIES, species, + SpeciesCategory.PROPERTY_SIZE_CATEGORY, sizeCategory, + SpeciesCategory.PROPERTY_SEX_CATEGORY, sexCategory + ); + + if (category == null) { + + // create it + + category = create(speciesCategoryDAO); + category.setSpecies(species); + category.setSizeCategory(sizeCategory); + category.setSexCategory(sexCategory); + } + + return category; + } +} Property changes on: trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/importdata/AbstractImportDataService.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Copied: trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/importdata/AcousticDataImportService.java (from rev 334, trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/AcousticDataImportService.java) =================================================================== --- trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/importdata/AcousticDataImportService.java (rev 0) +++ trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/importdata/AcousticDataImportService.java 2012-03-10 00:53:51 UTC (rev 345) @@ -0,0 +1,573 @@ +/* + * #%L + * EchoBase :: Services + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2011 - 2012 Ifremer, Codelutin + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * #L% + */ +package fr.ifremer.echobase.services.importdata; + +import com.google.common.base.Preconditions; +import com.google.common.collect.Lists; +import fr.ifremer.echobase.EchoBaseFunctions; +import fr.ifremer.echobase.EchoBasePredicates; +import fr.ifremer.echobase.InputFile; +import fr.ifremer.echobase.entities.EchoBaseEntityEnum; +import fr.ifremer.echobase.entities.EchoBaseUser; +import fr.ifremer.echobase.entities.data.Cell; +import fr.ifremer.echobase.entities.data.CellDAO; +import fr.ifremer.echobase.entities.data.Data; +import fr.ifremer.echobase.entities.data.DataAcquisition; +import fr.ifremer.echobase.entities.data.DataAcquisitionDAO; +import fr.ifremer.echobase.entities.data.DataDAO; +import fr.ifremer.echobase.entities.data.DataProcessing; +import fr.ifremer.echobase.entities.data.DataProcessingDAO; +import fr.ifremer.echobase.entities.data.Transect; +import fr.ifremer.echobase.entities.data.Transit; +import fr.ifremer.echobase.entities.data.Voyage; +import fr.ifremer.echobase.entities.references.AcousticInstrument; +import fr.ifremer.echobase.entities.references.CellType; +import fr.ifremer.echobase.entities.references.DataMetadata; +import fr.ifremer.echobase.entities.references.Vessel; +import fr.ifremer.echobase.services.ImportException; +import fr.ifremer.echobase.services.configurations.AcousticDataImportConfiguration; +import fr.ifremer.echobase.services.csv.AcousticDataImportModel; +import fr.ifremer.echobase.services.csv.AcousticDataImportModelRow; +import fr.ifremer.echobase.services.csv.CsvFileImportResult; +import fr.ifremer.echobase.services.csv.EchoBaseImport; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.nuiton.util.csv.Import; +import org.nuiton.util.csv.ImportRuntimeException; + +import java.io.Reader; +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.List; +import java.util.Map; + +import static org.nuiton.i18n.I18n.l_; + +/** + * Service to launch a "acoustic data" import. + * + * @author tchemit <chemit@codelutin.com> + * @since 0.3 + */ +public class AcousticDataImportService extends AbstractImportDataService<AcousticDataImportConfiguration> { + + /** Logger. */ + private static final Log log = + LogFactory.getLog(AcousticDataImportService.class); + + @Override + protected String getImportLabel() { + return l_(getLocale(), ImportDataMode.AccousticData.getI18nKey()); + } + + @Override + protected List<CsvFileImportResult> startImport( + AcousticDataImportConfiguration configuration, + EchoBaseUser user) throws ImportException { + + List<CsvFileImportResult> result = Lists.newLinkedList(); + + // get selected voyage + Voyage voyage = getEntityById(Voyage.class, + configuration.getVoyageId()); + + // get selected vessel + Vessel vessel = getEntityById(Vessel.class, + configuration.getVesselId()); + + CellType esduCellType = getEntityByProperty( + CellType.class, CellType.PROPERTY_ID, "Esdu"); + Preconditions.checkNotNull(esduCellType); + + CellType elementaryCellType = getEntityByProperty( + CellType.class, CellType.PROPERTY_ID, "Elementary"); + Preconditions.checkNotNull(elementaryCellType); + + InputFile inputFile = configuration.getMoviesFile(); + + CsvFileImportResult importResult = importMoviesFile(voyage, + vessel, + esduCellType, + elementaryCellType, + configuration, + inputFile); + + addResultAndLog(result, inputFile, importResult, user); + + return result; + } + + private final DateFormat cellDateFormat = + new SimpleDateFormat("yyyy-MM-dd hh:mm:ss'Z'"); + + private CsvFileImportResult importMoviesFile(Voyage voyage, + Vessel vessel, + CellType esduCellType, + CellType elementaryCellType, + AcousticDataImportConfiguration configuration, + InputFile inputFile + ) throws ImportException { + + CsvFileImportResult importResult = new CsvFileImportResult( + inputFile.getFileName()); + + Map<String, AcousticInstrument> instrumentsById = + getEntitiesMap(AcousticInstrument.class, + EchoBaseFunctions.ACOUSTIC_INSTRUMENT_ID); + + Map<String, DataMetadata> dataMetadatasByName = getEntitiesMap( + DataMetadata.class, EchoBaseFunctions.DATA_METADATA_NAME); + + AcousticDataImportModel csvModel = new AcousticDataImportModel( + getCsvSeparator(), instrumentsById); + + DataAcquisitionDAO dataAcquisitionDAO = + getDAO(DataAcquisition.class, DataAcquisitionDAO.class); + DataProcessingDAO dataProcessingDAO = + getDAO(DataProcessing.class, DataProcessingDAO.class); + CellDAO cellDAO = getDAO(Cell.class, CellDAO.class); + DataDAO dataDAO = getDAO(Data.class, DataDAO.class); + + boolean addDataAcquisition = configuration.isAddDataAcquisition(); + + String suffix = + configuration.getCellPositionReference().getMetadataNameSuffix(); + + Reader reader = getInputFileReader(inputFile); + try { + Import<AcousticDataImportModelRow> importer = + EchoBaseImport.newImport(csvModel, reader); + DataAcquisition dataAcquisition = null; + DataProcessing dataProcessing = null; + Cell esduCell = null; + Cell elementaryCell; + for (AcousticDataImportModelRow row : importer) { + + Date startDate = row.getCellDateStart(); + + // get transect to use + Transit transit = voyage.getTransit(startDate); + if (transit == null) { + + // can not find correct transit + throw new ImportException( + l_(getLocale(), "echobase.importError.transit.notfound", + voyage.getName(), + startDate)); + } + + Transect transect = transit.getTransect(vessel); + if (transect == null) { + + // can not find correct transect + throw new ImportException( + l_(getLocale(), "echobase.importError.transect.notfound", + voyage.getName(), + startDate, + vessel.getName())); + } + + // Build the row acoustic instrument +// String sndname = row.getSndName(); +// String channelName = row.getChannelName(); +// String instrumentId = sndname; + AcousticInstrument instrument = row.getAcousticInstrument(); +// instrumentsById.get(instrumentId); + boolean isME70 = + EchoBasePredicates.IS_ACOUSTIC_INSTRUMENT_ME70.apply(instrument); + if (dataAcquisition == null || + !instrument.equals(dataAcquisition.getAcousticInstrument())) { + + // need to use another data acquisition + + String softwareVersion = + getSoftwareVersion(configuration, isME70); + + String soundSpeedCalculations = + getSoundSpeedCalculations(configuration, isME70); + + if (!addDataAcquisition && + !transect.isDataAcquisitionEmpty()) { + + // try to obtain an existing data acquisiton + + for (DataAcquisition acquisition : transect.getDataAcquisition()) { + + if (instrument.equals(acquisition.getAcousticInstrument())) { + + // found a matching data acquisiotn from his acoustic instrument + dataAcquisition = acquisition; + break; + } + } + } + + if (dataAcquisition == null) { + + // need to create the data acquisition + + // create dataAcquisiton + dataAcquisition = createDataAcquisition( + configuration, + instrument, + softwareVersion, + soundSpeedCalculations, + row, + dataAcquisitionDAO + ); + + importResult.incrementsNumberCreated(EchoBaseEntityEnum.DataAcquisition); + + // add it to transect + transect.addDataAcquisition(dataAcquisition); + } + + // create data processing + dataProcessing = createDataProcessing( + configuration, + softwareVersion, + soundSpeedCalculations, + row, + dataProcessingDAO + ); + + importResult.incrementsNumberCreated(EchoBaseEntityEnum.DataProcessing); + + // add it to data acquisition + dataAcquisition.addDataProcessing(dataProcessing); + + // must start a fresh new esdu cell + esduCell = null; + } + + int cellType = row.getCellType(); + + String esduCellId = cellDateFormat.format(startDate); + + if (cellType == 4) { + + if (log.isDebugEnabled()) { + log.debug("Esdu cell (" + esduCellId + ")"); + } + // this is a esdu cell row + + // create esdu cell + esduCell = create(cellDAO, + Cell.PROPERTY_CELL_TYPE, esduCellType, + Cell.PROPERTY_NAME, esduCellId + ); + + importResult.incrementsNumberCreated(EchoBaseEntityEnum.Cell); + + // add it to data processing + dataProcessing.addCell(esduCell); + + // create esdu cell data + createEsduCellData(suffix, + esduCell, + dataMetadatasByName, + row, + dataDAO, + importResult); + } else { + + // this is a elementary cell row + + int cellNum = row.getCellNum(); + + String elementaryCellId = esduCellId + "_" + cellNum; + + if (log.isDebugEnabled()) { + log.debug("elementary cell (" + elementaryCellId + ")"); + } + if (esduCell == null) { + throw new ImportException( + l_(getLocale(), "echobase.importError.esduCell.notfound", + esduCellId, + voyage.getName(), + elementaryCellId + )); + } + + // create the elementary cell + + elementaryCell = create(cellDAO, + Cell.PROPERTY_CELL_TYPE, elementaryCellType, + Cell.PROPERTY_NAME, elementaryCellId + ); + + importResult.incrementsNumberCreated(EchoBaseEntityEnum.Cell); + + // add it to esdu cell + esduCell.addChilds(elementaryCell); + + + // create datas of the elementary cell + createElementaryCellData(elementaryCell, + dataMetadatasByName, + row, + dataDAO, + importResult); + } + } + + return importResult; + } catch (ImportRuntimeException e) { + throw new ImportException(getLocale(), inputFile, e); + } finally { + closeReader(reader, inputFile); + } + } + + protected void createEsduCellData(String suffix, + Cell cell, + Map<String, DataMetadata> dataMetadatas, + AcousticDataImportModelRow row, + DataDAO dao, + CsvFileImportResult importResult) { + + String dataValue; + + // create Latitude data + dataValue = row.getEsduCellDataLatitude(); + createCellData(dao, cell, dataMetadatas, "Latitude" + suffix, + dataValue, importResult); + + // create Longitude data + dataValue = row.getEsduCellDataLongitude(); + createCellData(dao, cell, dataMetadatas, "Longitude" + suffix, + dataValue, importResult); + + // create Depth data + dataValue = row.getEsduCellDataDepth(); + createCellData(dao, cell, dataMetadatas, "DepthRefSurface" + suffix, + dataValue, importResult); + + // create Time data + dataValue = cellDateFormat.format(row.getCellDateStart()); + createCellData(dao, cell, dataMetadatas, "Time" + suffix, + dataValue, importResult); + + // create AcousticDensity data + dataValue = row.getEsduCellDataAcousticDensity(); + createCellData(dao, cell, dataMetadatas, "sa", dataValue, importResult); + } + + private void createElementaryCellData(Cell cell, + Map<String, DataMetadata> dataMetadatas, + AcousticDataImportModelRow row, + DataDAO dao, + CsvFileImportResult importResult) { + + String dataValue; + + int cellType = row.getCellType(); + + String startMeta; + String endMeta; + + if (cellType == 0) { + + // surface + + startMeta = "DepthRefSurfaceStart"; + endMeta = "DepthRefSurfaceEnd"; + + } else { + + // bottom + + startMeta = "DepthRefBottomStart"; + endMeta = "DepthRefBottomEnd"; + } + + // create depth start data + dataValue = row.getCellDepthStart(); + createCellData(dao, cell, dataMetadatas, startMeta, dataValue, importResult); + + // create depth end data + dataValue = row.getCellDepthEnd(); + createCellData(dao, cell, dataMetadatas, endMeta, dataValue, importResult); + + // create acoustic density data + dataValue = row.getCellSa(); + createCellData(dao, cell, dataMetadatas, "sa", dataValue, importResult); + + } + + private DataAcquisition createDataAcquisition(AcousticDataImportConfiguration configuration, + AcousticInstrument instrument, + String softwareVersion, + String soundSpeedCalculations, + AcousticDataImportModelRow row, + DataAcquisitionDAO dataAcquisitionDAO) { + + String transceiverAcquisitionAbsorptionDescription = + configuration.getTransceiverAcquisitionAbsorptionDescription(); + String loggedDataFormat = configuration.getLoggedDataFormat(); + String loggedDataDatatype = configuration.getLoggedDataDatatype(); + String pingDutyCycle = configuration.getPingDutyCycle(); + String echosounderSoundSpeed = configuration.getEchosounderSoundSpeed(); + + DataAcquisition dataAcquisition = create(dataAcquisitionDAO); + + // fill from manual configuration + dataAcquisition.setAcousticInstrument(instrument); + dataAcquisition.setTransceiverAcquisitionAbsorptionDescription(transceiverAcquisitionAbsorptionDescription); + dataAcquisition.setAcquisitionSoftwareVersion(softwareVersion); + dataAcquisition.setLoggedDataFormat(loggedDataFormat); + dataAcquisition.setLoggedDataDatatype(loggedDataDatatype); + dataAcquisition.setPingDutyCycle(pingDutyCycle); + dataAcquisition.setEchosounderSoundSpeed(echosounderSoundSpeed); + dataAcquisition.setSoundSpeedCalculations(soundSpeedCalculations); + + // fill from csv file + Float transceiverAcquisitionAbsorption = + row.getTransceiverAcquisitionAbsorption(); + Float transducerAcquisitionBeamAngleAthwartship = + row.getTransducerAcquisitionBeamAngleAthwartship(); + Float transducerAcquisitionBeamAngleAlongship = + row.getTransducerAcquisitionBeamAngleAlongship(); + Float transducerAcquisitionPsi = + row.getTransducerAcquisitionPsi(); + Float transceiverAcquisitionPower = + row.getTransceiverAcquisitionPower(); + Float transceiverAcquisitionPulseLength = + row.getTransceiverAcquisitionPulseLength(); + Float transceiverAcquisitionGain = + row.getTransceiverAcquisitionGain(); + Float transceiverAcquisitionSacorrection = + row.getTransceiverAcquisitionSacorrection(); + + dataAcquisition.setTransceiverAcquisitionAbsorption(transceiverAcquisitionAbsorption); + dataAcquisition.setTransducerAcquisitionBeamAngleAthwartship(transducerAcquisitionBeamAngleAthwartship); + dataAcquisition.setTransducerAcquisitionBeamAngleAlongship(transducerAcquisitionBeamAngleAlongship); + dataAcquisition.setTransducerAcquisitionPsi(transducerAcquisitionPsi); + dataAcquisition.setTransceiverAcquisitionPower(transceiverAcquisitionPower); + dataAcquisition.setTransceiverAcquisitionPulseLength(transceiverAcquisitionPulseLength); + dataAcquisition.setTransceiverAcquisitionGain(transceiverAcquisitionGain); + dataAcquisition.setTransceiverAcquisitionSacorrection(transceiverAcquisitionSacorrection); + + return dataAcquisition; + } + + private DataProcessing createDataProcessing(AcousticDataImportConfiguration configuration, + String softwareVersion, + String soundSpeedCalculations, + AcousticDataImportModelRow row, + DataProcessingDAO dataProcessingDAO) { + + String transceiverAcquisitionAbsorptionDescription = + configuration.getTransceiverAcquisitionAbsorptionDescription(); + String echosounderSoundSpeed = configuration.getEchosounderSoundSpeed(); + String processingTemplate = configuration.getProcessingTemplate(); + String processingDescription = configuration.getProcessingDescription(); + Float sounderConstant = configuration.getSounderConstant(); + float digitThreshold = configuration.getDigitThreshold(); + String acousticDensityUnit = configuration.getAcousticDensityUnit(); + String notes = configuration.getNotes(); + + DataProcessing dataProcessing = create(dataProcessingDAO); + + // fill from manual configuration + dataProcessing.setProcessingTemplate(processingTemplate); + dataProcessing.setProcessingDescription(processingDescription); + dataProcessing.setSounderConstant(sounderConstant); + dataProcessing.setDigitThreshold(digitThreshold); + dataProcessing.setAcousticDensityUnit(acousticDensityUnit); + dataProcessing.setNotes(notes); + dataProcessing.setProcessingSoftwareVersion(softwareVersion); + dataProcessing.setTransceiverProcessingAbsorptionDescription(transceiverAcquisitionAbsorptionDescription); + dataProcessing.setEchosounderSoundSpeed(echosounderSoundSpeed); + dataProcessing.setSoundSpeedCalculations(soundSpeedCalculations); + + // fill from csv file + + Float transceiverAcquisitionAbsorption = + row.getTransceiverAcquisitionAbsorption(); + Float transducerAcquisitionBeamAngleAthwartship = + row.getTransducerAcquisitionBeamAngleAthwartship(); + Float transducerAcquisitionBeamAngleAlongship = + row.getTransducerAcquisitionBeamAngleAlongship(); + Float transducerAcquisitionPsi = + row.getTransducerAcquisitionPsi(); + + Float transceiverAcquisitionGain = + row.getTransceiverAcquisitionGain(); + Float transceiverAcquisitionSacorrection = + row.getTransceiverAcquisitionSacorrection(); + Float eIThreshold = row.getEiThreshold(); + + dataProcessing.setEiThreshold(eIThreshold); + dataProcessing.setTransceiverProcessingSacorrection(transceiverAcquisitionSacorrection); + dataProcessing.setTransceiverProcessingAbsorption(transceiverAcquisitionAbsorption); + dataProcessing.setTransceiverProcessingGain(transceiverAcquisitionGain); + dataProcessing.setTransducerProcessingPsi(transducerAcquisitionPsi); + dataProcessing.setTransducerProcessingBeamAngleAthwartship(transducerAcquisitionBeamAngleAthwartship); + dataProcessing.setTransducerProcessingBeamAngleAlongship(transducerAcquisitionBeamAngleAlongship); + + + return dataProcessing; + } + + private void createCellData(DataDAO dao, + Cell cell, + Map<String, DataMetadata> dataMetadatasByName, + String metadataName, + String dataValue, + CsvFileImportResult importResult) { + + DataMetadata dataMetaData = dataMetadatasByName.get(metadataName); + Data data = create(dao); + data.setDataMetadata(dataMetaData); + data.setDataValue(dataValue); + cell.addData(data); + + importResult.incrementsNumberCreated(EchoBaseEntityEnum.Data); + } + + private String getSoftwareVersion(AcousticDataImportConfiguration configuration, + boolean isME70) { + String result; + if (isME70) { + result = configuration.getAcquisitionSoftwareVersionME70(); + } else { + result = configuration.getAcquisitionSoftwareVersionER60(); + } + return result; + } + + private String getSoundSpeedCalculations(AcousticDataImportConfiguration configuration, + boolean isME70) { + String result; + if (isME70) { + result = configuration.getSoundSpeedCalculationsME70(); + } else { + result = configuration.getSoundSpeedCalculationsER60(); + } + return result; + } + +} Property changes on: trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/importdata/AcousticDataImportService.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Copied: trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/importdata/CatchesDataImportService.java (from rev 334, trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/CatchesDataImportService.java) =================================================================== --- trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/importdata/CatchesDataImportService.java (rev 0) +++ trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/importdata/CatchesDataImportService.java 2012-03-10 00:53:51 UTC (rev 345) @@ -0,0 +1,481 @@ +/* + * #%L + * EchoBase :: Services + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2011 - 2012 Ifremer, Codelutin + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * #L% + */ +package fr.ifremer.echobase.services.importdata; + +import com.google.common.base.Preconditions; +import com.google.common.collect.Lists; +import com.google.common.collect.Maps; +import fr.ifremer.echobase.EchoBaseFunctions; +import fr.ifremer.echobase.EchoBasePredicates; +import fr.ifremer.echobase.InputFile; +import fr.ifremer.echobase.entities.EchoBaseEntityEnum; +import fr.ifremer.echobase.entities.EchoBaseUser; +import fr.ifremer.echobase.entities.data.Operation; +import fr.ifremer.echobase.entities.data.Sample; +import fr.ifremer.echobase.entities.data.SampleDAO; +import fr.ifremer.echobase.entities.data.SampleData; +import fr.ifremer.echobase.entities.data.SampleDataDAO; +import fr.ifremer.echobase.entities.data.Voyage; +import fr.ifremer.echobase.entities.references.SampleDataType; +import fr.ifremer.echobase.entities.references.SampleDataTypeDAO; +import fr.ifremer.echobase.entities.references.SampleType; +import fr.ifremer.echobase.entities.references.SampleTypeDAO; +import fr.ifremer.echobase.entities.references.SexCategory; +import fr.ifremer.echobase.entities.references.SizeCategory; +import fr.ifremer.echobase.entities.references.Species; +import fr.ifremer.echobase.entities.references.SpeciesCategory; +import fr.ifremer.echobase.entities.references.SpeciesCategoryDAO; +import fr.ifremer.echobase.services.ImportException; +import fr.ifremer.echobase.services.configurations.CatchesDataImportConfiguration; +import fr.ifremer.echobase.services.csv.BiometrySampleImportModel; +import fr.ifremer.echobase.services.csv.BiometrySampleImportModelRow; +import fr.ifremer.echobase.services.csv.CsvFileImportResult; +import fr.ifremer.echobase.services.csv.EchoBaseImport; +import fr.ifremer.echobase.services.csv.SubSampleImportModel; +import fr.ifremer.echobase.services.csv.SubSampleImportModelRow; +import fr.ifremer.echobase.services.csv.TotalSampleImportModel; +import fr.ifremer.echobase.services.csv.TotalSampleImportModelRow; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.nuiton.util.csv.Import; +import org.nuiton.util.csv.ImportRuntimeException; + +import java.io.Reader; +import java.util.Collection; +import java.util.List; +import java.util.Locale; +import java.util.Map; + +import static org.nuiton.i18n.I18n.l_; + +/** + * Service to launch a "catches sample data" import. + * + * @author tchemit <chemit@codelutin.com> + * @since 0.3 + */ +public class CatchesDataImportService extends AbstractImportDataService<CatchesDataImportConfiguration> { + + /** Logger. */ + private static final Log log = + LogFactory.getLog(CatchesDataImportService.class); + + @Override + protected String getImportLabel() { + return l_(getLocale(), ImportDataMode.CatchesData.getI18nKey()); + } + + @Override + protected List<CsvFileImportResult> startImport(CatchesDataImportConfiguration configuration, + EchoBaseUser user) throws ImportException { + + List<CsvFileImportResult> result = Lists.newLinkedList(); + + Voyage voyage = getEntityById(Voyage.class, + configuration.getVoyageId()); + + // get all operation for this voyage and this vessel + Collection<Operation> operations = voyage.getAllOperations(); + + // split them by operation Id + Map<String, Operation> operationMap = Maps.uniqueIndex( + operations, EchoBaseFunctions.OPERATION_ID); + + Map<String, Species> speciesMap = getEntitiesMap( + Species.class, + EchoBaseFunctions.SPECIES_BARACOUDA_CODE); + + Map<String, SizeCategory> sizeCategoryMap = getEntitiesMap( + SizeCategory.class, + EchoBaseFunctions.SIZE_CATEGORY_NAME); + + Map<String, SexCategory> sexCategoryMap = getEntitiesMap( + SexCategory.class, + EchoBaseFunctions.SEX_CATEGORY_NAME); + + if (configuration.getTotalSampleFile().hasFile()) { + CsvFileImportResult totalSampleResult = importTotalSampleFile( + configuration.getTotalSampleFile(), + operationMap, + speciesMap, + sizeCategoryMap); + + addResultAndLog(result, configuration.getTotalSampleFile(), + totalSampleResult, user); + } + + if (configuration.getSubSampleFile().hasFile()) { + CsvFileImportResult subSampleResult = importSubSampleFile( + configuration.getSubSampleFile(), + operationMap, + speciesMap, + sizeCategoryMap, + sexCategoryMap); + + addResultAndLog(result, configuration.getSubSampleFile(), + subSampleResult, user); + } + + if (configuration.getBiometrySampleFile().hasFile()) { + CsvFileImportResult biometrySampleResult = importBiometrySampleFile( + configuration.getBiometrySampleFile(), + operationMap, + speciesMap); + + + addResultAndLog(result, configuration.getBiometrySampleFile(), + biometrySampleResult, user); + } + return result; + } + + private CsvFileImportResult importTotalSampleFile(InputFile inputFile, + Map<String, Operation> operationMap, + Map<String, Species> speciesMap, + Map<String, SizeCategory> sizeCategoryMap) throws ImportException { + + if (log.isInfoEnabled()) { + log.info("Starts import of totalSample from file " + + inputFile.getFileName()); + } + + CsvFileImportResult importResult = new CsvFileImportResult( + inputFile.getFileName()); + + TotalSampleImportModel csvModel = new TotalSampleImportModel(getCsvSeparator(), + operationMap, + speciesMap, + sizeCategoryMap + ); + + SampleDAO sampleDAO = getDAO(Sample.class, SampleDAO.class); + SampleDataDAO sampleDataDAO = getDAO(SampleData.class, SampleDataDAO.class); + SampleTypeDAO sampleTypeDAO = getDAO(SampleType.class, SampleTypeDAO.class); + SampleDataTypeDAO sampleDataTypeDAO = getDAO(SampleDataType.class, SampleDataTypeDAO.class); + SpeciesCategoryDAO speciesCategoryDAO = getDAO(SpeciesCategory.class, SpeciesCategoryDAO.class); + + SampleType sampleTypeTotal = findByProperties(sampleTypeDAO, SampleType.PROPERTY_NAME, "Total"); + Preconditions.checkNotNull(sampleTypeTotal); + SampleType sampleTypeUnsorted = findByProperties(sampleTypeDAO, SampleType.PROPERTY_NAME, "Unsorted"); + Preconditions.checkNotNull(sampleTypeUnsorted); + SampleType sampleTypeSorted = findByProperties(sampleTypeDAO, SampleType.PROPERTY_NAME, "Sorted"); + Preconditions.checkNotNull(sampleTypeSorted); + + SampleDataType sampleDataTypeMeanLength = findByProperties(sampleDataTypeDAO, SampleDataType.PROPERTY_NAME, "MeanLengthcm"); + Preconditions.checkNotNull(sampleDataTypeMeanLength); + SampleDataType sampleDataTypeMeanWeight = findByProperties(sampleDataTypeDAO, SampleDataType.PROPERTY_NAME, "MeanWeightg"); + Preconditions.checkNotNull(sampleDataTypeMeanWeight); + SampleDataType sampleDataTypeNoPerKg = findByProperties(sampleDataTypeDAO, SampleDataType.PROPERTY_NAME, "NoPerKg"); + Preconditions.checkNotNull(sampleDataTypeNoPerKg); + + Reader reader = getInputFileReader(inputFile); + Locale locale = getLocale(); + try { + Import<TotalSampleImportModelRow> importer = + EchoBaseImport.newImport(csvModel, reader); + + for (TotalSampleImportModelRow row : importer) { + + Operation operation = row.getOperation(); + + Species species = row.getSpecies(); + SizeCategory sizeCategory = row.getSizeCategory(); + + SpeciesCategory category = getSpeciesCategory( + speciesCategoryDAO, + species, + sizeCategory, + null + ); + + SampleType sampleType; + + if (EchoBasePredicates.IS_HORS_VRAC.apply(sizeCategory)) { + + // hors vrac case + sampleType = sampleTypeUnsorted; + + } else { + // none hors vrac case + sampleType = sampleTypeTotal; + } + + Sample sample = operation.getSample(category, sampleType); + + if (sample != null) { + + // can not have twice same sample + throw new ImportException( + l_(locale, "echobase.importError.duplicate.sample", + operation.getId(), + sampleType.getName(), + species.getBaracoudaCode(), + sizeCategory.getName())); + } + + if (sample == null) { + + // must create it + + sample = row.getSample(); + sample.setSpeciesCategory(category); + + sample.setSampleType(sampleType); + + sample = addSample(sampleDAO, + operation, + sample, + importResult); + } else { + sample = sample; + } + + // create datas + + if (row.getMeanLength() != null) { + + //create meanLength data + addSampleData(sampleDataDAO, + sampleDataTypeMeanLength, + null, + row.getMeanLength(), + sample, + importResult); + } + + if (row.getMeanWeight() != null) { + + //create meanWeight data + addSampleData(sampleDataDAO, + sampleDataTypeMeanWeight, + null, + row.getMeanWeight(), + sample, + importResult); + } + + if (row.getNoPerKg() != null) { + + //create noPerKg data + addSampleData(sampleDataDAO, + sampleDataTypeNoPerKg, + null, + row.getNoPerKg(), + sample, + importResult); + } + + Sample createdSortedSample = newInstance(sampleDAO); + createdSortedSample.setSampleType(sampleTypeSorted); + createdSortedSample.setSampleWeight(row.getSortedWeight()); + + // create sorted sample + addSample(sampleDAO, + operation, + createdSortedSample, + importResult); + + + } + + return importResult; + } catch (ImportRuntimeException e) { + throw new ImportException(locale, inputFile, e); + } finally { + closeReader(reader, inputFile); + } + } + + private CsvFileImportResult importSubSampleFile(InputFile inputFile, + Map<String, Operation> operationMap, + Map<String, Species> speciesMap, + Map<String, SizeCategory> sizeCategoryMap, + Map<String, SexCategory> sexCategoryMap + ) throws ImportException { + + if (log.isInfoEnabled()) { + log.info("Starts import of subSample from file " + + inputFile.getFileName()); + } + + CsvFileImportResult importResult = new CsvFileImportResult( + inputFile.getFileName()); + + SubSampleImportModel csvModel = new SubSampleImportModel(getCsvSeparator(), + operationMap, + speciesMap, + sizeCategoryMap, + sexCategoryMap); + + SampleDAO sampleDAO = getDAO(Sample.class, SampleDAO.class); + SampleDataDAO sampleDataDAO = getDAO(SampleData.class, SampleDataDAO.class); + SampleTypeDAO sampleTypeDAO = getDAO(SampleType.class, SampleTypeDAO.class); + SampleDataTypeDAO sampleDataTypeDAO = getDAO(SampleDataType.class, SampleDataTypeDAO.class); + SpeciesCategoryDAO speciesCategoryDAO = getDAO(SpeciesCategory.class, SpeciesCategoryDAO.class); + + SampleType sampleTypeSubsample = findByProperties(sampleTypeDAO, SampleType.PROPERTY_NAME, "Subsample"); + Preconditions.checkNotNull(sampleTypeSubsample); + + SampleDataType sampleDataTypeNumberAtLength = findByProperties(sampleDataTypeDAO, SampleDataType.PROPERTY_NAME, "NumberAtLength"); + Preconditions.checkNotNull(sampleDataTypeNumberAtLength); + SampleDataType sampleDataTypeWeightAtLength = findByProperties(sampleDataTypeDAO, SampleDataType.PROPERTY_NAME, "WeightAtLengthkg"); + Preconditions.checkNotNull(sampleDataTypeWeightAtLength); + + Reader reader = getInputFileReader(inputFile); + try { + Import<SubSampleImportModelRow> importer = + EchoBaseImport.newImport(csvModel, reader); + + for (SubSampleImportModelRow row : importer) { + + Operation operation = row.getOperation(); + + Species species = row.getSpecies(); + SizeCategory sizeCategory = row.getSizeCategory(); + SexCategory sexCategory = row.getSexCategory(); + + SpeciesCategory category = getSpeciesCategory( + speciesCategoryDAO, + species, + sizeCategory, + sexCategory + ); + + // find the sample with this category + Sample sample = operation.getSample(category, + sampleTypeSubsample); + + if (sample == null) { + + // must create it + sample = row.getSample(); + + sample.setSpeciesCategory(category); + sample.setSampleType(sampleTypeSubsample); + + sample = addSample(sampleDAO, + operation, + sample, + importResult); + } + + //create numberAtLength data + addSampleData(sampleDataDAO, + sampleDataTypeNumberAtLength, + "" + row.getLengthClass(), + row.getNumberAtLength(), + sample, + importResult); + + if (row.getWeightAtLength() != null) { + + //create weightAtLength data + addSampleData(sampleDataDAO, + sampleDataTypeWeightAtLength, + "" + row.getLengthClass(), + row.getWeightAtLength(), + sample, + importResult); + } + } + + return importResult; + } catch (ImportRuntimeException e) { + throw new ImportException(getLocale(), inputFile, e); + } finally { + closeReader(reader, inputFile); + } + } + + private CsvFileImportResult importBiometrySampleFile(InputFile inputFile, + Map<String, Operation> operationMap, + Map<String, Species> speciesMap + ) throws ImportException { + + if (log.isInfoEnabled()) { + log.info("Starts import of biometrySample from file " + + inputFile.getFileName()); + } + + CsvFileImportResult importResult = new CsvFileImportResult( + inputFile.getFileName()); + + BiometrySampleImportModel csvModel = + new BiometrySampleImportModel(getCsvSeparator()); + + SampleDAO sampleDAO = getDAO(Sample.class, SampleDAO.class); + SampleDataDAO sampleDataDAO = getDAO(SampleData.class, SampleDataDAO.class); + SampleTypeDAO sampleTypeDAO = getDAO(SampleType.class, SampleTypeDAO.class); + + SampleType sampleTypeIndividual = findByProperties(sampleTypeDAO, SampleType.PROPERTY_NAME, "Individual"); + Preconditions.checkNotNull(sampleTypeIndividual); + + Reader reader = getInputFileReader(inputFile); + try { + Import<BiometrySampleImportModelRow> importer = + EchoBaseImport.newImport(csvModel, reader); + + for (BiometrySampleImportModelRow row : importer) { + + //TODO + + } + + return importResult; + } catch (ImportRuntimeException e) { + throw new ImportException(getLocale(), inputFile, e); + } finally { + closeReader(reader, inputFile); + } + } + + private Sample addSample(SampleDAO dao, + Operation operation, + Sample sample, + CsvFileImportResult importResult) { + Preconditions.checkNotNull(operation); + Preconditions.checkNotNull(sample); + Sample result = create(dao, sample); + operation.addSample(result); + importResult.incrementsNumberCreated(EchoBaseEntityEnum.Sample); + return result; + } + + private SampleData addSampleData(SampleDataDAO dao, + SampleDataType sampleDataType, + String label, + float value, + Sample sample, + CsvFileImportResult importResult) { + SampleData sampleData = create(dao); + sampleData.setSampleDataType(sampleDataType); + sampleData.setDataValue(value); + sampleData.setDataLabel(label); + sample.addSampleData(sampleData); + + importResult.incrementsNumberCreated(EchoBaseEntityEnum.SampleData); + return sampleData; + } + +} Property changes on: trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/importdata/CatchesDataImportService.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Copied: trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/importdata/CommonDataImportService.java (from rev 334, trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/CommonDataImportService.java) =================================================================== --- trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/importdata/CommonDataImportService.java (rev 0) +++ trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/importdata/CommonDataImportService.java 2012-03-10 00:53:51 UTC (rev 345) @@ -0,0 +1,329 @@ +/* + * #%L + * EchoBase :: Services + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2011 - 2012 Ifremer, Codelutin + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * #L% + */ +package fr.ifremer.echobase.services.importdata; + +import com.google.common.base.Preconditions; +import com.google.common.collect.Lists; +import com.google.common.collect.Maps; +import fr.ifremer.echobase.EchoBaseFunctions; +import fr.ifremer.echobase.EchoBaseTechnicalException; +import fr.ifremer.echobase.InputFile; +import fr.ifremer.echobase.entities.EchoBaseEntityEnum; +import fr.ifremer.echobase.entities.EchoBaseUser; +import fr.ifremer.echobase.entities.data.Transect; +import fr.ifremer.echobase.entities.data.TransectDAO; +import fr.ifremer.echobase.entities.data.Transit; +import fr.ifremer.echobase.entities.data.TransitDAO; +import fr.ifremer.echobase.entities.data.Voyage; +import fr.ifremer.echobase.entities.data.VoyageDAO; +import fr.ifremer.echobase.entities.references.AreaOfOperation; +import fr.ifremer.echobase.entities.references.Mission; +import fr.ifremer.echobase.entities.references.MissionDAO; +import fr.ifremer.echobase.entities.references.Vessel; +import fr.ifremer.echobase.services.ImportException; +import fr.ifremer.echobase.services.exceptions.MissionNameAlreadyExistException; +import fr.ifremer.echobase.services.configurations.CommonDataImportConfiguration; +import fr.ifremer.echobase.services.csv.CsvFileImportResult; +import fr.ifremer.echobase.services.csv.EchoBaseImport; +import fr.ifremer.echobase.services.csv.TransectImportModel; +import fr.ifremer.echobase.services.csv.TransectImportModelRow; +import fr.ifremer.echobase.services.csv.TransitImportModel; +import fr.ifremer.echobase.services.csv.TransitImportModelRow; +import fr.ifremer.echobase.services.csv.VoyageImportModel; +import org.nuiton.topia.TopiaException; +import org.nuiton.util.csv.Import; +import org.nuiton.util.csv.ImportModel; +import org.nuiton.util.csv.ImportRuntimeException; + +import java.io.Reader; +import java.util.Arrays; +import java.util.Date; +import java.util.List; +import java.util.Locale; +import java.util.Map; + +import static org.nuiton.i18n.I18n.l_; + +/** + * Service to launch a "common data" import. + * + * @author tchemit <chemit@codelutin.com> + * @since 0.3 + */ +public class CommonDataImportService extends AbstractImportDataService<CommonDataImportConfiguration> { + + @Override + protected String getImportLabel() { + return l_(getLocale(), ImportDataMode.CommonData.getI18nKey()); + } + + @Override + protected List<CsvFileImportResult> startImport( + CommonDataImportConfiguration configuration, + EchoBaseUser user) throws ImportException { + + List<CsvFileImportResult> result = Lists.newLinkedList(); + + // get usable vessels + Map<String, Vessel> vesselMap = getEntitiesMap( + Vessel.class, EchoBaseFunctions.VESSEL_NAME); + + InputFile transectFile = configuration.getTransectFile(); + + switch (configuration.getImportMode()) { + + case ALL: { + + InputFile voyageFile = configuration.getVoyageFile(); + InputFile transitFile = configuration.getTransitFile(); + + CsvFileImportResult voyageResult = importVoyageFile( + voyageFile, configuration); + + CsvFileImportResult transitResult = importTransitFile( + transitFile, configuration); + + CsvFileImportResult transectResult = importTransectFile( + transectFile, vesselMap, configuration); + + addResultAndLog(result, voyageFile, voyageResult, user); + addResultAndLog(result, transitFile, transitResult, user); + addResultAndLog(result, transectFile, transectResult, user); + } + break; + case TRANSECT: { + + CsvFileImportResult transectResult = importTransectFile( + transectFile, vesselMap, configuration); + + addResultAndLog(result, transectFile, transectResult, user); + } + break; + } + return result; + } + + + public Mission createMission(Mission mission) throws MissionNameAlreadyExistException { + + Preconditions.checkNotNull(mission); + + try { + MissionDAO dao = getDAO(Mission.class, MissionDAO.class); + + // check mission name is unique + boolean exists = dao.existByProperties(Mission.PROPERTY_NAME, + mission.getName()); + + if (exists) { + throw new MissionNameAlreadyExistException(); + } + Mission result = create(dao, mission); + commitTransaction("Could not create mission " + + mission.getName()); + return result; + } catch (TopiaException eee) { + throw new EchoBaseTechnicalException(eee); + } + } + + protected CsvFileImportResult importVoyageFile( + InputFile inputFile, + CommonDataImportConfiguration configuration) throws ImportException { + + Mission mission = getEntityById(Mission.class, + configuration.getMissionId()); + AreaOfOperation areaOfOperation = + getEntityById(AreaOfOperation.class, + configuration.getAreaOfOperationId()); + + CsvFileImportResult importResult = new CsvFileImportResult( + inputFile.getFileName()); + + String voyageDescription = configuration.getVoyageDescription(); + String datum = configuration.getDatum(); + + VoyageDAO dao = getDAO(Voyage.class, VoyageDAO.class); + + ImportModel<Voyage> csvModel = + new VoyageImportModel(getCsvSeparator()); + + Reader reader = getInputFileReader(inputFile); + Import<Voyage> importer = EchoBaseImport.newImport(csvModel, reader); + Voyage newVoyage = null; + Locale locale = getLocale(); + try { + for (Voyage voyage : importer) { + + voyage.setMission(mission); + voyage.setAreaOfOperation(areaOfOperation); + voyage.setDescription(voyageDescription); + voyage.setDatum(datum); + + Voyage createdVoyage = create(dao, voyage); + if (newVoyage == null) { + newVoyage = createdVoyage; + } else { + // this means a voyage file with more than one row not possible... + + throw new ImportException( + l_(locale, "echobase.importError.can.only.import.one.voyage.atime")); + } + + importResult.incrementsNumberCreated(EchoBaseEntityEnum.Voyage); + } + + if (newVoyage == null) { + throw new ImportException( + l_(locale, "echobase.importError.no.voyage.imported")); + } + + // push back to id of the voyage in configuration for next imports + configuration.setVoyageId(newVoyage.getTopiaId()); + + return importResult; + } catch (ImportRuntimeException e) { + throw new ImportException(locale, inputFile, e); + } finally { + closeReader(reader, inputFile); + } + } + + protected CsvFileImportResult importTransitFile( + InputFile inputFile, + CommonDataImportConfiguration configuration) throws ImportException { + + CsvFileImportResult importResult = new CsvFileImportResult( + inputFile.getFileName()); + + // get voyage + Voyage voyage = getEntityById(Voyage.class, + configuration.getVoyageId()); + + // restrict voyage to use to this voyage + Map<String, Voyage> voyageMap = Maps.uniqueIndex( + Arrays.asList(voyage), EchoBaseFunctions.VOYAGE_NAME); + + String relatedActivity = configuration.getTransitRelatedActivity(); + + TransitImportModel csvModel = + new TransitImportModel(getCsvSeparator(), voyageMap); + + TransitDAO dao = getDAO(Transit.class, TransitDAO.class); + + Reader reader = getInputFileReader(inputFile); + try { + Import<TransitImportModelRow> importer = + EchoBaseImport.newImport(csvModel, reader); + for (TransitImportModelRow row : importer) { + + Transit transit = row.getTransit(); + + transit.setRelatedActivity(relatedActivity); + + Transit createdTransit = create(dao, transit); + + voyage.addTransit(createdTransit); + + importResult.incrementsNumberCreated(EchoBaseEntityEnum.Transit); + } + return importResult; + } catch (ImportRuntimeException e) { + throw new ImportException(getLocale(), inputFile, e); + } finally { + closeReader(reader, inputFile); + } + } + + private CsvFileImportResult importTransectFile( + InputFile inputFile, + Map<String, Vessel> vesselMap, + CommonDataImportConfiguration configuration) throws ImportException { + + CsvFileImportResult importResult = new CsvFileImportResult( + inputFile.getFileName()); + + // get voyage + Voyage voyage = getEntityById(Voyage.class, + configuration.getVoyageId()); + + // restrict voyage to use to this voyage + Map<String, Voyage> voyageMap = Maps.uniqueIndex( + Arrays.asList(voyage), EchoBaseFunctions.VOYAGE_NAME); + + String datum = configuration.getDatum(); + String license = configuration.getTransectLicence(); + String geospatialVerticalPositive = + configuration.getTransectGeospatialVerticalPositive(); + String binUnitsPingAxis = configuration.getTransectBinUnitsPingAxis(); + + TransectImportModel csvModel = + new TransectImportModel(getCsvSeparator(), + voyageMap, + vesselMap); + + TransectDAO dao = getDAO(Transect.class, TransectDAO.class); + + Reader reader = getInputFileReader(inputFile); + try { + Import<TransectImportModelRow> importer = + EchoBaseImport.newImport(csvModel, reader); + for (TransectImportModelRow row : importer) { + + Transect transect = row.getTransect(); + + Date timeCoverageStart = transect.getTimeCoverageStart(); + Date timeCoverageEnd = transect.getTimeCoverageEnd(); + + Transit transit = voyage.getTransit(timeCoverageStart, + timeCoverageEnd); + if (transit == null) { + throw new ImportException( + l_(getLocale(), "echobase.importError.no.transit.between.date", + voyage.getName(), timeCoverageStart, timeCoverageEnd)); + } + + transect.setDatum(datum); + transect.setLicence(license); + transect.setGeospatialVerticalPositive( + geospatialVerticalPositive); + transect.setBinUnitsPingAxis(binUnitsPingAxis); + + Transect createdTransect = create(dao, transect); + + transit.addTransect(createdTransect); + importResult.incrementsNumberCreated(EchoBaseEntityEnum.Transect); + } + + return importResult; + + } catch (ImportRuntimeException e) { + throw new ImportException(getLocale(), inputFile, e); + } finally { + closeReader(reader, inputFile); + } + } + + +} Property changes on: trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/importdata/CommonDataImportService.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Copied: trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/importdata/ImportDataMode.java (from rev 334, trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/ImportDataMode.java) =================================================================== --- trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/importdata/ImportDataMode.java (rev 0) +++ trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/importdata/ImportDataMode.java 2012-03-10 00:53:51 UTC (rev 345) @@ -0,0 +1,63 @@ +/* + * #%L + * EchoBase :: Services + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2011 Ifremer, Codelutin + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * #L% + */ +package fr.ifremer.echobase.services.importdata; + +import fr.ifremer.echobase.I18nAble; + +import static org.nuiton.i18n.I18n.n_; + +/** + * To define the mode of import data (says files to be imported). + * + * @author tchemit <chemit@codelutin.com> + * @since 0.2 + */ +public enum ImportDataMode implements I18nAble { + + /** Import Voyage / Transit / Transect. */ + CommonData(n_("echobase.common.importDataMode.commonData")), + + /** Import operations common data. */ + Operation(n_("echobase.common.importDataMode.operation")), + + /** Import catches data (unsorted, total, but no individual ones). */ + CatchesData(n_("echobase.common.importDataMode.catchesData")), + + /** Import accoustic data (Cells ESDU and Elementary). */ + AccousticData(n_("echobase.common.importDataMode.accousticData")), + + /** Import results. */ + Results(n_("echobase.common.importDataMode.results")); + + private final String i18nKey; + + ImportDataMode(String i18nKey) { + this.i18nKey = i18nKey; + } + + @Override + public String getI18nKey() { + return i18nKey; + } +} Property changes on: trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/importdata/ImportDataMode.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Copied: trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/importdata/OperationImportService.java (from rev 334, trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/OperationImportService.java) =================================================================== --- trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/importdata/OperationImportService.java (rev 0) +++ trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/importdata/OperationImportService.java 2012-03-10 00:53:51 UTC (rev 345) @@ -0,0 +1,303 @@ +/* + * #%L + * EchoBase :: Services + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2011 - 2012 Ifremer, Codelutin + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * #L% + */ +package fr.ifremer.echobase.services.importdata; + +import com.google.common.collect.Lists; +import com.google.common.collect.Maps; +import fr.ifremer.echobase.EchoBaseFunctions; +import fr.ifremer.echobase.InputFile; +import fr.ifremer.echobase.entities.EchoBaseEntityEnum; +import fr.ifremer.echobase.entities.EchoBaseUser; +import fr.ifremer.echobase.entities.data.GearMetadataValue; +import fr.ifremer.echobase.entities.data.GearMetadataValueDAO; +import fr.ifremer.echobase.entities.data.Operation; +import fr.ifremer.echobase.entities.data.OperationDAO; +import fr.ifremer.echobase.entities.data.OperationMetadataValue; +import fr.ifremer.echobase.entities.data.OperationMetadataValueDAO; +import fr.ifremer.echobase.entities.data.Transect; +import fr.ifremer.echobase.entities.data.Transit; +import fr.ifremer.echobase.entities.data.Voyage; +import fr.ifremer.echobase.entities.references.DepthStratum; +import fr.ifremer.echobase.entities.references.Gear; +import fr.ifremer.echobase.entities.references.GearMetadata; +import fr.ifremer.echobase.entities.references.OperationMetadata; +import fr.ifremer.echobase.entities.references.Vessel; +import fr.ifremer.echobase.services.ImportException; +import fr.ifremer.echobase.services.configurations.OperationImportConfiguration; +import fr.ifremer.echobase.services.csv.CsvFileImportResult; +import fr.ifremer.echobase.services.csv.EchoBaseImport; +import fr.ifremer.echobase.services.csv.GearMetadataValueImportModel; +import fr.ifremer.echobase.services.csv.GearMetadataValueImportModelRow; +import fr.ifremer.echobase.services.csv.OperationImportModel; +import fr.ifremer.echobase.services.csv.OperationImportModelRow; +import fr.ifremer.echobase.services.csv.OperationMetadataValueImportModel; +import fr.ifremer.echobase.services.csv.OperationMetadataValueImportModelRow; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.nuiton.util.csv.Import; +import org.nuiton.util.csv.ImportRuntimeException; + +import java.io.Reader; +import java.util.Collection; +import java.util.Date; +import java.util.List; +import java.util.Map; + +import static org.nuiton.i18n.I18n.l_; + +/** + * Service to launch a "operations" import. + * + * @author tchemit <chemit@codelutin.com> + * @since 0.3 + */ +public class OperationImportService extends AbstractImportDataService<OperationImportConfiguration> { + + /** Logger. */ + private static final Log log = + LogFactory.getLog(OperationImportService.class); + + @Override + protected String getImportLabel() { + return l_(getLocale(), ImportDataMode.Operation.getI18nKey()); + } + + @Override + protected List<CsvFileImportResult> startImport( + OperationImportConfiguration configuration, + EchoBaseUser user) throws ImportException { + + List<CsvFileImportResult> result = Lists.newLinkedList(); + + // get selected voyage + Voyage voyage = getEntityById(Voyage.class, + configuration.getVoyageId()); + + // get usable vessels from the voyage by their name + Map<String, Vessel> vesselMap = Maps.uniqueIndex( + voyage.getAllVessels(), EchoBaseFunctions.VESSEL_NAME); + + // get usable gears by their casino gear name + Map<String, Gear> gearMap = getEntitiesMap( + Gear.class, EchoBaseFunctions.GEAR_CASINO_GEAR_NAME); + + InputFile operationFile = configuration.getOperationFile(); + CsvFileImportResult nbOperations = importOperationFile( + operationFile, + voyage, + vesselMap, + gearMap); + + // get all operation for this voyage and this vessel + Collection<Operation> operations = voyage.getAllOperations(); + + // split them by operation Id + Map<String, Operation> operationMap = Maps.uniqueIndex( + operations, EchoBaseFunctions.OPERATION_ID); + + InputFile operationMetadataFile = configuration.getOperationMetadataFile(); + CsvFileImportResult nbOperationMetas = importOperationMetadataFile( + operationMetadataFile, + vesselMap, + operationMap); + + InputFile gearMetadataFile = configuration.getGearMetadataFile(); + CsvFileImportResult nbGearMetas = importGearMetadataFile( + gearMetadataFile, + vesselMap, + gearMap, + operationMap); + + addResultAndLog(result, operationFile, nbOperations, user); + + addResultAndLog(result, operationMetadataFile, nbOperationMetas, user); + + addResultAndLog(result, gearMetadataFile, nbGearMetas, user); + + return result; + } + + protected CsvFileImportResult importOperationFile( + InputFile inputFile, + Voyage voyage, + Map<String, Vessel> vesselMap, + Map<String, Gear> gearMap) throws ImportException { + + if (log.isInfoEnabled()) { + log.info("Starts import of operation from file " + + inputFile.getFileName()); + } + + CsvFileImportResult importResult = new CsvFileImportResult( + inputFile.getFileName()); + + Map<String, DepthStratum> depthStratumMap = getEntitiesMap( + DepthStratum.class, EchoBaseFunctions.DEPTH_STRATUM_ID); + + OperationImportModel csvModel = + new OperationImportModel(getCsvSeparator(), + vesselMap, + gearMap, + depthStratumMap); + + OperationDAO dao = getDAO(Operation.class, OperationDAO.class); + + Reader reader = getInputFileReader(inputFile); + try { + Import<OperationImportModelRow> importer = + EchoBaseImport.newImport(csvModel, reader); + + for (OperationImportModelRow row : importer) { + + Vessel vessel = row.getVessel(); + + Operation operation = row.getOperation(); + + Date startTime = operation.getGearShootingStartTime(); + Date endTime = operation.getGearShootingEndTime(); + + Transit transit = voyage.getTransit(startTime, endTime); + + Transect transect = transit.getTransect(vessel); + + Operation createdOperation = create(dao, operation); + + transect.addOperation(createdOperation); + + importResult.incrementsNumberCreated(EchoBaseEntityEnum.Operation); + } + + return importResult; + } catch (ImportRuntimeException e) { + throw new ImportException(getLocale(), inputFile, e); + } finally { + closeReader(reader, inputFile); + } + } + + protected CsvFileImportResult importOperationMetadataFile( + InputFile inputFile, + Map<String, Vessel> vesselMap, + Map<String, Operation> operationMap) throws ImportException { + + if (log.isInfoEnabled()) { + log.info("Starts import of operation metadata values from file " + + inputFile.getFileName()); + } + + CsvFileImportResult importResult = new CsvFileImportResult( + inputFile.getFileName()); + + Map<String, OperationMetadata> operationMetadatasByName = + getEntitiesMap(OperationMetadata.class, + EchoBaseFunctions.OPERATION_METADATA_NAME); + + OperationMetadataValueImportModel csvModel = + new OperationMetadataValueImportModel(getCsvSeparator(), + vesselMap, + operationMetadatasByName, + operationMap); + + OperationMetadataValueDAO dao = getDAO( + OperationMetadataValue.class, + OperationMetadataValueDAO.class); + + Reader reader = getInputFileReader(inputFile); + try { + Import<OperationMetadataValueImportModelRow> importer = + EchoBaseImport.newImport(csvModel, reader); + + for (OperationMetadataValueImportModelRow row : importer) { + + Operation operation = row.getOperation(); + + OperationMetadataValue operationMetadataValueToCreate = + row.getOperationMetadataValue(); + OperationMetadataValue operationMetadataValue = create( + dao, operationMetadataValueToCreate); + + operation.addOperationMetadataValue(operationMetadataValue); + importResult.incrementsNumberCreated(EchoBaseEntityEnum.OperationMetadataValue); + } + return importResult; + } catch (ImportRuntimeException e) { + throw new ImportException(getLocale(), inputFile, e); + } finally { + closeReader(reader, inputFile); + } + } + + protected CsvFileImportResult importGearMetadataFile( + InputFile inputFile, + Map<String, Vessel> vesselMap, + Map<String, Gear> gearMap, + Map<String, Operation> operationMap) throws ImportException { + + if (log.isInfoEnabled()) { + log.info("Starts import of gear metadata values from file " + + inputFile.getFileName()); + } + + CsvFileImportResult importResult = new CsvFileImportResult( + inputFile.getFileName()); + + Map<String, GearMetadata> gearMetadatasByType = + getEntitiesMap(GearMetadata.class, + EchoBaseFunctions.GEAR_METADATA_NAME); + + GearMetadataValueImportModel csvModel = + new GearMetadataValueImportModel(getCsvSeparator(), + vesselMap, + gearMetadatasByType, + gearMap, + operationMap); + + GearMetadataValueDAO dao = getDAO(GearMetadataValue.class, + GearMetadataValueDAO.class); + + Reader reader = getInputFileReader(inputFile); + try { + Import<GearMetadataValueImportModelRow> importer = + EchoBaseImport.newImport(csvModel, reader); + + for (GearMetadataValueImportModelRow row : importer) { + + Operation operation = row.getOperation(); + + GearMetadataValue gearMetadataValuetoCreate = + row.getGearMetadataValue(); + GearMetadataValue gearMetadataValue = create( + dao, gearMetadataValuetoCreate); + + operation.addGearMetadataValue(gearMetadataValue); + importResult.incrementsNumberCreated(EchoBaseEntityEnum.GearMetadataValue); + } + return importResult; + } catch (ImportRuntimeException e) { + throw new ImportException(getLocale(), inputFile, e); + } finally { + closeReader(reader, inputFile); + } + } +} Property changes on: trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/importdata/OperationImportService.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Copied: trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/importdata/ResultsImportService.java (from rev 336, trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/ResultsImportService.java) =================================================================== --- trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/importdata/ResultsImportService.java (rev 0) +++ trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/importdata/ResultsImportService.java 2012-03-10 00:53:51 UTC (rev 345) @@ -0,0 +1,457 @@ +/* + * #%L + * EchoBase :: Services + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2011 - 2012 Ifremer, Codelutin + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * #L% + */ +package fr.ifremer.echobase.services.importdata; + +import com.google.common.base.Predicate; +import com.google.common.collect.Iterables; +import com.google.common.collect.Lists; +import com.google.common.collect.Maps; +import fr.ifremer.echobase.EchoBaseFunctions; +import fr.ifremer.echobase.EchoBasePredicates; +import fr.ifremer.echobase.InputFile; +import fr.ifremer.echobase.entities.EchoBaseEntityEnum; +import fr.ifremer.echobase.entities.EchoBaseUser; +import fr.ifremer.echobase.entities.data.Cell; +import fr.ifremer.echobase.entities.data.CellDAO; +import fr.ifremer.echobase.entities.data.Voyage; +import fr.ifremer.echobase.entities.references.DepthStratum; +import fr.ifremer.echobase.entities.data.Echotype; +import fr.ifremer.echobase.entities.data.LengthAgeKey; +import fr.ifremer.echobase.entities.data.LengthWeightKey; +import fr.ifremer.echobase.entities.data.LengthWeightKeyDAO; +import fr.ifremer.echobase.entities.references.SizeCategory; +import fr.ifremer.echobase.entities.references.Species; +import fr.ifremer.echobase.entities.references.SpeciesCategory; +import fr.ifremer.echobase.entities.references.SpeciesCategoryDAO; +import fr.ifremer.echobase.entities.references.Strata; +import fr.ifremer.echobase.services.ImportException; +import fr.ifremer.echobase.services.configurations.ResultsImportConfiguration; +import fr.ifremer.echobase.services.configurations.ResultsImportMode; +import fr.ifremer.echobase.services.csv.CsvFileImportResult; +import fr.ifremer.echobase.services.csv.EchoBaseImport; +import fr.ifremer.echobase.services.csv.EchotypeImportModel; +import fr.ifremer.echobase.services.csv.EchotypeImportModelRow; +import fr.ifremer.echobase.services.csv.LengthAgeKeyImportModel; +import fr.ifremer.echobase.services.csv.LengthAgeKeyImportModelRow; +import fr.ifremer.echobase.services.csv.LengthWeightKeyImportModel; +import fr.ifremer.echobase.services.csv.LengthWeightKeyImportModelRow; +import fr.ifremer.echobase.services.csv.RegionCellAssociationImportModel; +import fr.ifremer.echobase.services.csv.RegionCellAssociationImportModelRow; +import fr.ifremer.echobase.services.csv.RegionCellImportModel; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.nuiton.topia.persistence.TopiaDAO; +import org.nuiton.util.csv.Import; +import org.nuiton.util.csv.ImportRuntimeException; + +import java.io.Reader; +import java.util.Arrays; +import java.util.Collection; +import java.util.List; +import java.util.Map; + +import static org.nuiton.i18n.I18n.l_; + +/** + * Service to launch a "results" import. + * + * @author tchemit <chemit@codelutin.com> + * @since 0.3 + */ +public class ResultsImportService extends AbstractImportDataService<ResultsImportConfiguration> { + + /** Logger. */ + private static final Log log = + LogFactory.getLog(ResultsImportService.class); + + @Override + protected String getImportLabel() { + return l_(getLocale(), ImportDataMode.Results.getI18nKey()); + } + + @Override + public List<CsvFileImportResult> startImport(ResultsImportConfiguration configuration, + EchoBaseUser user) throws ImportException { + + List<CsvFileImportResult> result = Lists.newLinkedList(); + + ResultsImportMode importMode = configuration.getImportMode(); + + switch (importMode) { + case VOYAGE: + + // get selected voyage + Voyage voyage = getEntityById(Voyage.class, + configuration.getVoyageId()); + + // authorize only the selected voyage to be imported + Map<String, Voyage> voyageMap = Maps.uniqueIndex(Arrays.asList( + voyage), EchoBaseFunctions.VOYAGE_NAME); + + Map<String, Species> speciesMap = getEntitiesMap( + Species.class, + EchoBaseFunctions.SPECIES_BARACOUDA_CODE); + + InputFile inputFile = configuration.getLengthAgeKeyFile(); + + if (inputFile.hasFile()) { + + CsvFileImportResult importResult = importLenthAgeKey( + inputFile, + voyageMap, + speciesMap + ); + addResultAndLog(result, inputFile, importResult, user); + } + + inputFile = configuration.getLengthWeightKeyFile(); + if (inputFile.hasFile()) { + + CsvFileImportResult importResult = importLenghtWeightKey( + inputFile, + voyageMap, + speciesMap + ); + addResultAndLog(result, inputFile, importResult, user); + } + + inputFile = configuration.getEchotypeFile(); + if (inputFile.hasFile()) { + + CsvFileImportResult importResult = importEchotypeFile( + inputFile, + voyageMap, + speciesMap + ); + addResultAndLog(result, inputFile, importResult, user); + } + break; + + case REGION: + + if (configuration.getRegionsFile().hasFile()) { + CsvFileImportResult importResult = importRegionsFile( + configuration.getRegionsFile()); + + addResultAndLog(result, configuration.getRegionsFile(), + importResult, user); + } + if (configuration.getAssociationFile().hasFile()) { + CsvFileImportResult importResult = importRegionAssociationFile( + configuration.getAssociationFile()); + + addResultAndLog(result, configuration.getAssociationFile(), + importResult, user); + } + break; + + case ACOUSTIC: + if (configuration.getRegionsFile().hasFile()) { + CsvFileImportResult importResult = importRegionsFile( + configuration.getRegionsFile()); + + addResultAndLog(result, configuration.getRegionsFile(), + importResult, user); + } + if (configuration.getAssociationFile().hasFile()) { + CsvFileImportResult importResult = importRegionAssociationFile( + configuration.getAssociationFile()); + + addResultAndLog(result, configuration.getAssociationFile(), + importResult, user); + } + break; + } + return result; + } + + protected CsvFileImportResult importRegionsFile(InputFile inputFile) throws ImportException { + + if (log.isInfoEnabled()) { + log.info("Starts import of region from file " + + inputFile.getFileName()); + } + + CsvFileImportResult importResult = new CsvFileImportResult( + inputFile.getFileName()); + + RegionCellImportModel csvModel = new RegionCellImportModel( + serviceContext.getConfiguration().getCsvSeparator()); + + CellDAO dao = getDAO(Cell.class, CellDAO.class); + + Reader reader = getInputFileReader(inputFile); + try { + Import<Cell> importer = + EchoBaseImport.newImport(csvModel, reader); + + for (Cell row : importer) { + + //TODO + + importResult.incrementsNumberCreated(EchoBaseEntityEnum.Cell); + } + return importResult; + } catch (ImportRuntimeException e) { + throw new ImportException(getLocale(), inputFile, e); + } finally { + closeReader(reader, inputFile); + } + } + + protected CsvFileImportResult importRegionAssociationFile(InputFile inputFile) throws ImportException { + RegionCellAssociationImportModel csvModel = new RegionCellAssociationImportModel( + serviceContext.getConfiguration().getCsvSeparator()); + + if (log.isInfoEnabled()) { + log.info("Starts import of region association from file " + + inputFile.getFileName()); + } + + CsvFileImportResult importResult = new CsvFileImportResult( + inputFile.getFileName()); + + CellDAO dao = getDAO(Cell.class, CellDAO.class); + + Reader reader = getInputFileReader(inputFile); + try { + Import<RegionCellAssociationImportModelRow> importer = + EchoBaseImport.newImport(csvModel, reader); + + for (RegionCellAssociationImportModelRow row : importer) { + + //TODO + + importResult.incrementsNumberUpdated(EchoBaseEntityEnum.Cell); + } + + return importResult; + } catch (ImportRuntimeException e) { + throw new ImportException(getLocale(), inputFile, e); + } finally { + closeReader(reader, inputFile); + } + } + + protected CsvFileImportResult importLenghtWeightKey(InputFile inputFile, + Map<String, Voyage> voyageMap, + Map<String, Species> speciesMap) throws ImportException { + + if (log.isInfoEnabled()) { + log.info("Starts import of lenthWeightKey from file " + + inputFile.getFileName()); + } + + CsvFileImportResult importResult = new CsvFileImportResult( + inputFile.getFileName()); + + Map<String, SizeCategory> sizeCategoryMap = getEntitiesMap( + SizeCategory.class, + EchoBaseFunctions.SIZE_CATEGORY_NAME); + + LengthWeightKeyImportModel csvModel = new LengthWeightKeyImportModel( + serviceContext.getConfiguration().getCsvSeparator(), + voyageMap, + sizeCategoryMap, + speciesMap); + + LengthWeightKeyDAO dao = getDAO(LengthWeightKey.class, + LengthWeightKeyDAO.class); + + SpeciesCategoryDAO speciesCategoryDAO = + getDAO(SpeciesCategory.class, SpeciesCategoryDAO.class); + + Reader reader = getInputFileReader(inputFile); + try { + Import<LengthWeightKeyImportModelRow> importer = + EchoBaseImport.newImport(csvModel, reader); + + for (LengthWeightKeyImportModelRow row : importer) { + + Voyage voyage = row.getVoyage(); + + LengthWeightKey lengthWeightKeyToCreate = + row.getLengthWeightKey(); + LengthWeightKey lengthWeightKey = create( + dao, lengthWeightKeyToCreate); + + //TODO should we import it ? + //lengthWeightKey.setMetadata(row.getMetadata()); + + Species species = row.getSpecies(); + SizeCategory sizeCategory = row.getSizeCategory(); + + // find speciesCategory + SpeciesCategory speciesCategory = getSpeciesCategory( + speciesCategoryDAO, + species, sizeCategory, + null + ); + + lengthWeightKey.setSpeciesCategory(speciesCategory); + + // attach it to voyage + voyage.addLengthWeightKey(lengthWeightKey); + + importResult.incrementsNumberCreated(EchoBaseEntityEnum.LengthWeightKey); + } + + return importResult; + } catch (ImportRuntimeException e) { + throw new ImportException(getLocale(), inputFile, e); + } finally { + closeReader(reader, inputFile); + } + } + + protected CsvFileImportResult importLenthAgeKey(InputFile inputFile, + Map<String, Voyage> voyageMap, + Map<String, Species> speciesMap) throws ImportException { + + if (log.isInfoEnabled()) { + log.info("Starts import of lenthAgeKey from file " + + inputFile.getFileName()); + } + + CsvFileImportResult importResult = new CsvFileImportResult( + inputFile.getFileName()); + + Map<String, Strata> strataMap = getEntitiesMap( + Strata.class, EchoBaseFunctions.STRATA_BY_NAME); + + LengthAgeKeyImportModel csvModel = new LengthAgeKeyImportModel( + serviceContext.getConfiguration().getCsvSeparator(), + voyageMap, + strataMap, + speciesMap); + + TopiaDAO<LengthAgeKey> dao = getDAO(LengthAgeKey.class); + + Reader reader = getInputFileReader(inputFile); + try { + Import<LengthAgeKeyImportModelRow> importer = + EchoBaseImport.newImport(csvModel, reader); + + for (LengthAgeKeyImportModelRow row : importer) { + + Voyage voyage = row.getVoyage(); + + LengthAgeKey lengthAgeKeyToCreate = row.getLengthAgeKey(); + LengthAgeKey lengthAgeKey = create(dao, lengthAgeKeyToCreate); + + // attach it to voyage + voyage.addLengthAgeKey(lengthAgeKey); + + importResult.incrementsNumberCreated(EchoBaseEntityEnum.LengthAgeKey); + } + return importResult; + } catch (ImportRuntimeException e) { + throw new ImportException(getLocale(), inputFile, e); + } finally { + closeReader(reader, inputFile); + } + } + + private CsvFileImportResult importEchotypeFile(InputFile inputFile, + Map<String, Voyage> voyageMap, + Map<String, Species> speciesMap) throws ImportException { + + if (log.isInfoEnabled()) { + log.info("Starts import of echotype from file " + + inputFile.getFileName()); + } + + CsvFileImportResult importResult = new CsvFileImportResult( + inputFile.getFileName()); + + Map<String, DepthStratum> depthStratumMap = getEntitiesMap( + DepthStratum.class, + EchoBaseFunctions.DEPTH_STRATUM_ID); + + EchotypeImportModel csvModel = new EchotypeImportModel( + serviceContext.getConfiguration().getCsvSeparator(), + voyageMap, + depthStratumMap, + speciesMap); + + TopiaDAO<Echotype> dao = getDAO(Echotype.class); + + Reader reader = getInputFileReader(inputFile); + try { + Import<EchotypeImportModelRow> importer = + EchoBaseImport.newImport(csvModel, reader); + + for (EchotypeImportModelRow row : importer) { + + Voyage voyage = row.getVoyage(); + + Echotype rowEchotype = row.getEchotype(); + + String echotypeName = rowEchotype.getName(); + + // check if there is a echotype for the voyage and this name + Echotype echotype = null; + + if (!voyage.isEchotypeEmpty()) { + + Collection<Echotype> echotypes = voyage.getEchotype(); + Predicate<Echotype> predicate = + EchoBasePredicates.newEchotypeByNamePredicate( + echotypeName); + echotype = Iterables.find(echotypes, predicate, null); + + } + + if (echotype == null) { + + // creates it + echotype = create(dao, rowEchotype); + + // attach it to voyage + voyage.addEchotype(echotype); + + importResult.incrementsNumberCreated(EchoBaseEntityEnum.Echotype); + + } else { + + Species species = rowEchotype.getSpecies().iterator().next(); + Species existingSpecie = + echotype.getSpeciesByTopiaId(species.getTopiaId()); + + if (existingSpecie == null) { + + // add this species + echotype.addSpecies(species); + } + } + } + + return importResult; + } catch (ImportRuntimeException e) { + throw new ImportException(getLocale(), inputFile, e); + } finally { + closeReader(reader, inputFile); + } + } +} Property changes on: trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/importdata/ResultsImportService.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Deleted: trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/AcousticDataImportServiceTest.java =================================================================== --- trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/AcousticDataImportServiceTest.java 2012-03-09 23:52:25 UTC (rev 344) +++ trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/AcousticDataImportServiceTest.java 2012-03-10 00:53:51 UTC (rev 345) @@ -1,119 +0,0 @@ -/* - * #%L - * EchoBase :: Services - * - * $Id$ - * $HeadURL$ - * %% - * Copyright (C) 2011 - 2012 Ifremer, Codelutin - * %% - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * #L% - */ -package fr.ifremer.echobase.services; - -import fr.ifremer.echobase.entities.data.Cell; -import fr.ifremer.echobase.entities.data.Data; -import fr.ifremer.echobase.entities.data.DataAcquisition; -import fr.ifremer.echobase.entities.data.DataProcessing; -import fr.ifremer.echobase.entities.references.AcousticInstrument; -import fr.ifremer.echobase.services.configurations.AcousticDataImportConfiguration; -import fr.ifremer.echobase.services.configurations.CellPositionReference; -import fr.ifremer.echobase.services.csv.CsvFileImportResult; -import org.junit.Assert; -import org.junit.Ignore; -import org.junit.Test; - -import java.util.List; - -/** - * Tests {@link AcousticDataImportService}. - * - * @author tchemit <chemit@codelutin.com> - * @since 0.3 - */ -public class AcousticDataImportServiceTest extends EchoBaseTestServiceSupport { - - protected FakeEchoBaseServiceContext initContext() { - return new FakeEchoBaseServiceContext( - ImportDataConstants.IMPORT_DATA_ECHOBASE_COMMON_DATA_RESULT); - } - - protected String[] getImportPath(String filename) { - return new String[]{"/import-data", "acousticData", filename}; - } - - @Ignore - @Test - public void doImport() throws Exception { - - assertImportCommonData(); - - assertNoEntities(DataAcquisition.class, - DataProcessing.class, - Cell.class, - Data.class); - - AcousticDataImportConfiguration conf = - new AcousticDataImportConfiguration(getLocale()); - - conf.setVoyageId(getVoyageId()); - conf.setVesselId(ImportDataConstants.VESSEL_ID); - conf.setAcousticDensityUnit("acousticDensityUnit"); - conf.setAcquisitionSoftwareVersionER60("acquisitionSoftwareVersionER60"); - conf.setAcquisitionSoftwareVersionME70("acquisitionSoftwareVersionME70"); - conf.setAddDataAcquisition(false); - conf.setTransceiverAcquisitionAbsorptionDescription("transceiverAcquisitionAbsorptionDescription"); - conf.setCellPositionReference(CellPositionReference.START); - conf.setDigitThreshold(1.5f); - conf.setEchosounderSoundSpeed("echosounderSoundSpeed"); - conf.setLoggedDataDatatype("loggedDataDatatype"); - conf.setLoggedDataFormat("loggedDataFormat"); - conf.setNotes("notes"); - conf.setPingDutyCycle("pingDutyCycle"); - conf.setProcessingDescription("processingDescription"); - conf.setProcessingTemplate("processingTemplate"); - conf.setSounderConstant(1.0f); - conf.setSoundSpeedCalculationsER60("soundSpeedCalculationsER60"); - conf.setSoundSpeedCalculationsME70("soundSpeedCalculationsME70"); - conf.setTransceiverAcquisitionAbsorptionDescription("transceiverAcquisitionAbsorptionDescription"); - - prepareInputFile(conf.getMoviesFile(), getImportPath("movies.csv")); - - AcousticDataImportService service = - getService(AcousticDataImportService.class); - - //FIXME : remove this when db will have such data - service.create(getDAO(AcousticInstrument.class), AcousticInstrument.PROPERTY_ID, "ES18-11"); - service.create(getDAO(AcousticInstrument.class), AcousticInstrument.PROPERTY_ID, "ES38B"); - service.create(getDAO(AcousticInstrument.class), AcousticInstrument.PROPERTY_ID, "ES70-7C"); - service.create(getDAO(AcousticInstrument.class), AcousticInstrument.PROPERTY_ID, "ES120-7"); - service.create(getDAO(AcousticInstrument.class), AcousticInstrument.PROPERTY_ID, "ES200-7C"); - service.create(getDAO(AcousticInstrument.class), AcousticInstrument.PROPERTY_ID, "ME70"); - - List<CsvFileImportResult> result = - service.doImport(conf, createFakeUser()); - - Assert.assertNotNull(result); - Assert.assertEquals(1, result.size()); - CsvFileImportResult importResult = result.get(0); - assertCsvImportResult(importResult, DataAcquisition.class, ImportDataConstants.NB_DATA_ACQUISITION); - assertCsvImportResult(importResult, DataProcessing.class, ImportDataConstants.NB_DATA_PROCESSING); - assertCsvImportResult(importResult, Cell.class, ImportDataConstants.NB_CELL); - assertCsvImportResult(importResult, Data.class, ImportDataConstants.NB_DATA); - - assertImportAcousticDatas(); - } - -} Deleted: trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/CatchesDataImportServiceTest.java =================================================================== --- trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/CatchesDataImportServiceTest.java 2012-03-09 23:52:25 UTC (rev 344) +++ trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/CatchesDataImportServiceTest.java 2012-03-10 00:53:51 UTC (rev 345) @@ -1,187 +0,0 @@ -/* - * #%L - * EchoBase :: Services - * - * $Id$ - * $HeadURL$ - * %% - * Copyright (C) 2011 - 2012 Ifremer, Codelutin - * %% - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * #L% - */ -package fr.ifremer.echobase.services; - -import fr.ifremer.echobase.entities.data.Sample; -import fr.ifremer.echobase.entities.data.SampleData; -import fr.ifremer.echobase.entities.references.Species; -import fr.ifremer.echobase.services.configurations.CatchesDataImportConfiguration; -import fr.ifremer.echobase.services.csv.CsvFileImportResult; -import org.junit.Assert; -import org.junit.Ignore; -import org.junit.Test; - -import java.util.Iterator; -import java.util.List; - -/** - * Tests {@link CatchesDataImportService}. - * - * @author tchemit <chemit@codelutin.com> - * @since 0.3 - */ -public class CatchesDataImportServiceTest extends EchoBaseTestServiceSupport { - - protected FakeEchoBaseServiceContext initContext() { - return new FakeEchoBaseServiceContext( - ImportDataConstants.IMPORT_DATA_ECHOBASE_OPERATION_RESULT); - } - - protected String[] getImportPath(String filename) { - return new String[]{"/import-data", "catchesData", filename}; - } - - @Test - public void doImportAllSamples() throws Exception { - - assertImportCommonData(); - assertImportOperations(); - assertNoEntities(Sample.class, SampleData.class); - - CatchesDataImportConfiguration conf = - new CatchesDataImportConfiguration(getLocale()); - - conf.setVoyageId(getVoyageId()); - - prepareInputFile(conf.getTotalSampleFile(), - getImportPath("totalsample.csv")); - prepareInputFile(conf.getSubSampleFile(), - getImportPath("subsample.csv")); -// prepareInputFile(conf.getBiometrySampleFile(), -// getImportPath("biometrysample.csv")); - - CatchesDataImportService service = - getService(CatchesDataImportService.class); - - List<CsvFileImportResult> result = service.doImport(conf, - createFakeUser()); - - Assert.assertNotNull(result); -// Assert.assertEquals(3, result.size()); - Assert.assertEquals(2, result.size()); - Iterator<CsvFileImportResult> iterator = result.iterator(); - CsvFileImportResult importResult; - - importResult = iterator.next(); - assertCsvImportResult(importResult, Sample.class, ImportDataConstants.NB_SAMPLE_TOTAL, 0, ImportDataConstants.NB_SAMPLE); - assertCsvImportResult(importResult, SampleData.class, ImportDataConstants.NB_SAMPLE_DATA_TOTAL, 0, ImportDataConstants.NB_SAMPLE_DATA); - importResult = iterator.next(); - assertCsvImportResult(importResult, Sample.class, ImportDataConstants.NB_SAMPLE_UNSORTED, 0, ImportDataConstants.NB_SAMPLE); - assertCsvImportResult(importResult, SampleData.class, ImportDataConstants.NB_SAMPLE_DATA_UNSORTED, 0, ImportDataConstants.NB_SAMPLE_DATA); -// importResult = iterator.next(); -// assertCsvImportResult(importResult, Sample.class, NB_SAMPLE_BIOMETRY, 0, NB_SAMPLE); -// assertCsvImportResult(importResult, SampleData.class, NB_SAMPLE_DATA_BIOMETRY, 0, NB_SAMPLE_DATA); - - assertImportSampleDatas(); - } - - @Ignore - @Test - public void doImportTotalSample() throws Exception { - - assertImportCommonData(); - assertImportOperations(); - assertNoEntities(Sample.class, SampleData.class); - - CatchesDataImportConfiguration conf = - new CatchesDataImportConfiguration(getLocale()); - - conf.setVoyageId(getVoyageId()); - - prepareInputFile(conf.getTotalSampleFile(), - getImportPath("totalsample.csv")); - - CatchesDataImportService service = - getService(CatchesDataImportService.class); - - List<CsvFileImportResult> result = service.doImport(conf, - createFakeUser()); - - Assert.assertNotNull(result); - Assert.assertEquals(1, result.size()); - CsvFileImportResult importResult = result.get(0); - assertCsvImportResult(importResult, Sample.class, ImportDataConstants.NB_SAMPLE_TOTAL); - assertCsvImportResult(importResult, SampleData.class, ImportDataConstants.NB_SAMPLE_DATA_TOTAL); - } - - @Ignore - @Test - public void doImportSubSample() throws Exception { - - assertImportCommonData(); - assertImportOperations(); - assertNoEntities(Sample.class, SampleData.class); - - CatchesDataImportConfiguration conf = - new CatchesDataImportConfiguration(getLocale()); - - conf.setVoyageId(getVoyageId()); - - prepareInputFile(conf.getSubSampleFile(), - getImportPath("subsample.csv")); - - CatchesDataImportService service = - getService(CatchesDataImportService.class); - - List<CsvFileImportResult> result = service.doImport(conf, - createFakeUser()); - - Assert.assertNotNull(result); - Assert.assertEquals(1, result.size()); - Iterator<CsvFileImportResult> iterator = result.iterator(); - CsvFileImportResult importResult = iterator.next(); - assertCsvImportResult(importResult, Sample.class, ImportDataConstants.NB_SAMPLE_UNSORTED); - assertCsvImportResult(importResult, SampleData.class, ImportDataConstants.NB_SAMPLE_DATA_UNSORTED); - } - - @Ignore - @Test - public void doImportBiometrySample() throws Exception { - - assertImportCommonData(); - assertImportOperations(); - assertNoEntities(Sample.class, SampleData.class); - - CatchesDataImportConfiguration conf = - new CatchesDataImportConfiguration(getLocale()); - - conf.setVoyageId(getVoyageId()); - - prepareInputFile(conf.getBiometrySampleFile(), - getImportPath("biometrysample.csv")); - - CatchesDataImportService service = - getService(CatchesDataImportService.class); - List<CsvFileImportResult> result = service.doImport(conf, - createFakeUser()); - - Assert.assertNotNull(result); - Assert.assertEquals(1, result.size()); - Iterator<CsvFileImportResult> iterator = result.iterator(); - CsvFileImportResult importResult = iterator.next(); - assertCsvImportResult(importResult, Sample.class, ImportDataConstants.NB_SAMPLE_BIOMETRY); - assertCsvImportResult(importResult, SampleData.class, ImportDataConstants.NB_SAMPLE_DATA_BIOMETRY); - } - -} Deleted: trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/CommonDataImportService2Test.java =================================================================== --- trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/CommonDataImportService2Test.java 2012-03-09 23:52:25 UTC (rev 344) +++ trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/CommonDataImportService2Test.java 2012-03-10 00:53:51 UTC (rev 345) @@ -1,92 +0,0 @@ -/* - * #%L - * EchoBase :: Services - * - * $Id$ - * $HeadURL$ - * %% - * Copyright (C) 2011 - 2012 Ifremer, Codelutin - * %% - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * #L% - */ -package fr.ifremer.echobase.services; - -import fr.ifremer.echobase.entities.data.Transect; -import fr.ifremer.echobase.entities.data.Transit; -import fr.ifremer.echobase.services.configurations.CommonDataImportConfiguration; -import fr.ifremer.echobase.services.configurations.CommonDataImportMode; -import fr.ifremer.echobase.services.csv.CsvFileImportResult; -import org.junit.Assert; -import org.junit.Test; - -import java.util.Iterator; -import java.util.List; - -/** - * Tests {@link CommonDataImportService} with {@link CommonDataImportMode#TRANSECT} mode. - * - * @author tchemit <chemit@codelutin.com> - * @since 0.3 - */ -public class CommonDataImportService2Test extends EchoBaseTestServiceSupport { - - protected FakeEchoBaseServiceContext initContext() { - return new FakeEchoBaseServiceContext( - ImportDataConstants.IMPORT_DATA_ECHOBASE_COMMON_DATA_RESULT); - } - - @Test - public void doImport() throws Exception { - - // import with mode transect - - assertImportCommonData(); - - CommonDataImportConfiguration conf = - new CommonDataImportConfiguration(getLocale()); - - conf.setAreaOfOperationId(ImportDataConstants.AREA_OF_OPERATION_ID); - conf.setDatum("datum"); - conf.setVoyageId(getVoyageId()); - conf.setTransectBinUnitsPingAxis("transectBinUnitsPingAxis"); - conf.setTransectGeospatialVerticalPositive("transectGeospatialVerticalPositive"); - conf.setTransectLicence("transectLicence"); - - prepareInputFile(conf.getTransectFile(), getImportPath("transect.csv")); - - conf.setImportMode(CommonDataImportMode.TRANSECT); - - CommonDataImportService service = - getService(CommonDataImportService.class); - - // let's delete every transects - for (Transit transit : getEntities(Transit.class)) { - transit.clearTransect(); - } - List<CsvFileImportResult> result = service.doImport(conf, createFakeUser()); - Assert.assertNotNull(result); - Assert.assertEquals(1, result.size()); - Iterator<CsvFileImportResult> iterator = result.iterator(); - assertCsvImportResult(iterator.next(), Transect.class, - ImportDataConstants.NB_TRANSECT); - - assertImportCommonData(); - } - - protected String[] getImportPath(String filename) { - return new String[]{"/import-data", "commonData", filename}; - } - -} Deleted: trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/CommonDataImportServiceTest.java =================================================================== --- trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/CommonDataImportServiceTest.java 2012-03-09 23:52:25 UTC (rev 344) +++ trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/CommonDataImportServiceTest.java 2012-03-10 00:53:51 UTC (rev 345) @@ -1,99 +0,0 @@ -/* - * #%L - * EchoBase :: Services - * - * $Id$ - * $HeadURL$ - * %% - * Copyright (C) 2011 - 2012 Ifremer, Codelutin - * %% - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * #L% - */ -package fr.ifremer.echobase.services; - -import fr.ifremer.echobase.entities.data.Transect; -import fr.ifremer.echobase.entities.data.Transit; -import fr.ifremer.echobase.entities.data.Voyage; -import fr.ifremer.echobase.services.configurations.CommonDataImportConfiguration; -import fr.ifremer.echobase.services.configurations.CommonDataImportMode; -import fr.ifremer.echobase.services.csv.CsvFileImportResult; -import org.junit.Assert; -import org.junit.Test; - -import java.util.Iterator; -import java.util.List; - -/** - * Tests {@link CommonDataImportService} with {@link CommonDataImportMode#ALL} mode. - * - * @author tchemit <chemit@codelutin.com> - * @since 0.3 - */ -public class CommonDataImportServiceTest extends EchoBaseTestServiceSupport { - - @Override - protected FakeEchoBaseServiceContext initContext() { - return new FakeEchoBaseServiceContext( - ImportDataConstants.IMPORT_DATA_ECHOBASE_NO_DATA); - } - - @Test - public void doImport() throws Exception { - - // no data in db - - assertNoEntities(Voyage.class, Transit.class, Transect.class); - - // import with mode all (voyage / transit / transect) - - CommonDataImportConfiguration conf = - new CommonDataImportConfiguration(getLocale()); - - conf.setAreaOfOperationId(ImportDataConstants.AREA_OF_OPERATION_ID); - conf.setDatum("datum"); - conf.setMissionId(ImportDataConstants.MISSION_ID); - conf.setTransectBinUnitsPingAxis("transectBinUnitsPingAxis"); - conf.setTransectGeospatialVerticalPositive("transectGeospatialVerticalPositive"); - conf.setTransectLicence("transectLicence"); - conf.setTransitRelatedActivity("transitRelatedActivity"); - conf.setVoyageDescription("voyageDescription"); - - prepareInputFile(conf.getVoyageFile(), getImportPath("voyage.csv")); - prepareInputFile(conf.getTransitFile(), getImportPath("transit.csv")); - prepareInputFile(conf.getTransectFile(), getImportPath("transect.csv")); - - conf.setImportMode(CommonDataImportMode.ALL); - - CommonDataImportService service = - getService(CommonDataImportService.class); - - List<CsvFileImportResult> result = - service.doImport(conf, createFakeUser()); - - Assert.assertNotNull(result); - Assert.assertEquals(3, result.size()); - Iterator<CsvFileImportResult> iterator = result.iterator(); - assertCsvImportResult(iterator.next(), Voyage.class, ImportDataConstants.NB_VOYAGE); - assertCsvImportResult(iterator.next(), Transit.class, ImportDataConstants.NB_TRANSIT); - assertCsvImportResult(iterator.next(), Transect.class, ImportDataConstants.NB_TRANSECT); - - assertImportCommonData(); - } - - protected String[] getImportPath(String filename) { - return new String[]{"/import-data", "commonData", filename}; - } - -} Modified: trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/EchoBaseTestServiceSupport.java =================================================================== --- trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/EchoBaseTestServiceSupport.java 2012-03-09 23:52:25 UTC (rev 344) +++ trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/EchoBaseTestServiceSupport.java 2012-03-10 00:53:51 UTC (rev 345) @@ -28,17 +28,6 @@ import fr.ifremer.echobase.entities.EchoBaseEntityEnum; import fr.ifremer.echobase.entities.EchoBaseUser; import fr.ifremer.echobase.entities.EchoBaseUserImpl; -import fr.ifremer.echobase.entities.data.Cell; -import fr.ifremer.echobase.entities.data.Data; -import fr.ifremer.echobase.entities.data.DataAcquisition; -import fr.ifremer.echobase.entities.data.DataProcessing; -import fr.ifremer.echobase.entities.data.GearMetadataValue; -import fr.ifremer.echobase.entities.data.Operation; -import fr.ifremer.echobase.entities.data.OperationMetadataValue; -import fr.ifremer.echobase.entities.data.Sample; -import fr.ifremer.echobase.entities.data.SampleData; -import fr.ifremer.echobase.entities.data.Transect; -import fr.ifremer.echobase.entities.data.Transit; import fr.ifremer.echobase.entities.data.Voyage; import fr.ifremer.echobase.services.csv.CsvFileImportResult; import org.apache.commons.io.FileUtils; @@ -148,30 +137,6 @@ } } - public void assertImportCommonData() throws TopiaException { - assertNbEntities(Voyage.class, ImportDataConstants.NB_VOYAGE); - assertNbEntities(Transit.class, ImportDataConstants.NB_TRANSIT); - assertNbEntities(Transect.class, ImportDataConstants.NB_TRANSECT); - } - - public void assertImportOperations() throws TopiaException { - assertNbEntities(Operation.class, ImportDataConstants.NB_OPERATION); - assertNbEntities(OperationMetadataValue.class, ImportDataConstants.NB_OPERATION_METADATAVALUE); - assertNbEntities(GearMetadataValue.class, ImportDataConstants.NB_GEAR_METADATAVALUE); - } - - public void assertImportSampleDatas() throws TopiaException { - assertNbEntities(Sample.class, ImportDataConstants.NB_SAMPLE); - assertNbEntities(SampleData.class, ImportDataConstants.NB_SAMPLE_DATA); - } - - public void assertImportAcousticDatas() throws TopiaException { - assertNbEntities(DataAcquisition.class, ImportDataConstants.NB_DATA_ACQUISITION); - assertNbEntities(DataProcessing.class, ImportDataConstants.NB_DATA_PROCESSING); - assertNbEntities(Cell.class, ImportDataConstants.NB_CELL); - assertNbEntities(Data.class, ImportDataConstants.NB_DATA); - } - protected String getVoyageId() { Voyage voyage = getEntities(Voyage.class).get(0); Preconditions.checkNotNull(voyage); Modified: trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/EmbeddedApplicationServiceTest.java =================================================================== --- trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/EmbeddedApplicationServiceTest.java 2012-03-09 23:52:25 UTC (rev 344) +++ trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/EmbeddedApplicationServiceTest.java 2012-03-10 00:53:51 UTC (rev 345) @@ -27,6 +27,7 @@ import fr.ifremer.echobase.entities.data.Voyage; import fr.ifremer.echobase.entities.data.VoyageDAO; import fr.ifremer.echobase.services.configurations.EmbeddedApplicationConfiguration; +import fr.ifremer.echobase.services.importdata.AbstractImportDataServiceIT; import org.junit.Assert; import org.junit.Test; import org.nuiton.topia.TopiaContext; @@ -45,7 +46,7 @@ protected FakeEchoBaseServiceContext initContext() { return new FakeEchoBaseServiceContext( - ImportDataConstants.IMPORT_DATA_ECHOBASE_OPERATION_RESULT); + AbstractImportDataServiceIT.IMPORT_DATA_ECHOBASE_OPERATION_RESULT); } @Test Modified: trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/ExportQueryServiceTest.java =================================================================== --- trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/ExportQueryServiceTest.java 2012-03-09 23:52:25 UTC (rev 344) +++ trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/ExportQueryServiceTest.java 2012-03-10 00:53:51 UTC (rev 345) @@ -34,7 +34,7 @@ * @author tchemit <chemit@codelutin.com> * @since 0.3 */ -public class ExportQueryServiceTest extends EchoBaseTestServiceSupport{ +public class ExportQueryServiceTest extends EchoBaseTestServiceSupport { @Override protected FakeEchoBaseServiceContext initContext() { return new FakeEchoBaseServiceContext(null); @@ -60,9 +60,9 @@ processLibreOfficeSqlQuery("select \"t\".name FROM \"t\" WHERE (select \"s\".name FROM \"s\" WHERE rien2);", "select t.name FROM t WHERE (select s.name FROM s WHERE rien2);"); } - + protected void processLibreOfficeSqlQuery(String libreOfficeQuery, - String expected) { + String expected) { String actual = service.processLibreOfficeSqlQuery(libreOfficeQuery); Assert.assertEquals(expected, actual); Deleted: trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/ImportDataConstants.java =================================================================== --- trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/ImportDataConstants.java 2012-03-09 23:52:25 UTC (rev 344) +++ trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/ImportDataConstants.java 2012-03-10 00:53:51 UTC (rev 345) @@ -1,90 +0,0 @@ -/* - * #%L - * EchoBase :: Services - * - * $Id$ - * $HeadURL$ - * %% - * Copyright (C) 2011 - 2012 Ifremer, Codelutin - * %% - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * #L% - */ -package fr.ifremer.echobase.services; - -/** - * constants for import data tests. - * - * @author tchemit <chemit@codelutin.com> - * @since 0.3 - */ -public class ImportDataConstants { - - protected ImportDataConstants() { - } - - public static final String IMPORT_DATA_ECHOBASE_NO_DATA = "/import-data/echobase-0.3-nodata.h2.db.gz"; - - public static final String IMPORT_DATA_ECHOBASE_COMMON_DATA_RESULT = "/import-data/echobase-0.3-commonData-result.h2.db.gz"; - - public static final String IMPORT_DATA_ECHOBASE_OPERATION_RESULT = "/import-data/echobase-0.3-operation-result.h2.db.gz"; - - public static final String VESSEL_ID = "fr.ifremer.echobase.entities.references.Vessel#1323196672049#0.9790502711645855"; - - public static final String MISSION_ID = "fr.ifremer.echobase.entities.references.Mission#1323127544274#0.7939481378378231"; - - public static final String AREA_OF_OPERATION_ID = "fr.ifremer.echobase.entities.references.AreaOfOperation#1323128474277#0.011341599655098622"; - - public static final int NB_VOYAGE = 1; - - public static final int NB_TRANSIT = 3; - - public static final int NB_TRANSECT = 251; - - public static final int NB_OPERATION = 131; - - public static final int NB_OPERATION_METADATAVALUE = 393; - - public static final int NB_GEAR_METADATAVALUE = 917; - - public static final int NB_SAMPLE_TOTAL = 1232; - - public static final int NB_SAMPLE_DATA_TOTAL = 1834; - - public static final int NB_SAMPLE_UNSORTED = 605; - - public static final int NB_SAMPLE_DATA_UNSORTED = 8057; - - public static final int NB_SAMPLE_BIOMETRY = 0; - - public static final int NB_SAMPLE_DATA_BIOMETRY = 0; - - public static final int NB_SAMPLE = NB_SAMPLE_TOTAL + NB_SAMPLE_UNSORTED + NB_SAMPLE_BIOMETRY; - - public static final int NB_SAMPLE_DATA = NB_SAMPLE_DATA_TOTAL + NB_SAMPLE_DATA_UNSORTED + NB_SAMPLE_DATA_BIOMETRY; - - public static final int NB_DATA_ACQUISITION = 61; - - public static final int NB_DATA_PROCESSING = 183; - - public static final int NB_CELL = 427; - - public static final int NB_DATA = 427; - - public static final int NB_LENGTH_AGE_KEY = 404; - - public static final int NB_LENGTH_WEIGHT_KEY = 7; - - public static final int NB_ECHOTYPE = 8; -} Deleted: trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/OperationImportServiceTest.java =================================================================== --- trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/OperationImportServiceTest.java 2012-03-09 23:52:25 UTC (rev 344) +++ trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/OperationImportServiceTest.java 2012-03-10 00:53:51 UTC (rev 345) @@ -1,91 +0,0 @@ -/* - * #%L - * EchoBase :: Services - * - * $Id$ - * $HeadURL$ - * %% - * Copyright (C) 2011 - 2012 Ifremer, Codelutin - * %% - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * #L% - */ -package fr.ifremer.echobase.services; - -import fr.ifremer.echobase.entities.data.GearMetadataValue; -import fr.ifremer.echobase.entities.data.Operation; -import fr.ifremer.echobase.entities.data.OperationMetadataValue; -import fr.ifremer.echobase.entities.references.Gear; -import fr.ifremer.echobase.entities.references.GearMetadata; -import fr.ifremer.echobase.services.configurations.OperationImportConfiguration; -import fr.ifremer.echobase.services.csv.CsvFileImportResult; -import org.junit.Assert; -import org.junit.Test; - -import java.util.Iterator; -import java.util.List; - -/** - * Tests {@link OperationImportService}. - * - * @author tchemit <chemit@codelutin.com> - * @since 0.3 - */ -public class OperationImportServiceTest extends EchoBaseTestServiceSupport { - - protected FakeEchoBaseServiceContext initContext() { - return new FakeEchoBaseServiceContext( - ImportDataConstants.IMPORT_DATA_ECHOBASE_COMMON_DATA_RESULT); - } - - protected String[] getImportPath(String filename) { - return new String[]{"/import-data", "operation", filename}; - } - - @Test - public void doImport() throws Exception { - - assertImportCommonData(); - - assertNoEntities(Operation.class, - OperationMetadataValue.class, - GearMetadataValue.class); - - OperationImportConfiguration conf = - new OperationImportConfiguration(getLocale()); - - conf.setVoyageId(getVoyageId()); - - prepareInputFile(conf.getOperationFile(), - getImportPath("operation.csv")); - prepareInputFile(conf.getOperationMetadataFile(), - getImportPath("operationmetadatavalue.csv")); - prepareInputFile(conf.getGearMetadataFile(), - getImportPath("gearmetadatavalue.csv")); - - OperationImportService service = - getService(OperationImportService.class); - - List<CsvFileImportResult> result = service.doImport(conf, createFakeUser()); - - Assert.assertNotNull(result); - Assert.assertEquals(3, result.size()); - Iterator<CsvFileImportResult> iterator = result.iterator(); - assertCsvImportResult(iterator.next(), Operation.class, ImportDataConstants.NB_OPERATION); - assertCsvImportResult(iterator.next(), OperationMetadataValue.class, ImportDataConstants.NB_OPERATION_METADATAVALUE); - assertCsvImportResult(iterator.next(), GearMetadataValue.class, ImportDataConstants.NB_GEAR_METADATAVALUE); - - assertImportOperations(); - } -} Deleted: trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/ResultsImportServiceTest.java =================================================================== --- trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/ResultsImportServiceTest.java 2012-03-09 23:52:25 UTC (rev 344) +++ trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/ResultsImportServiceTest.java 2012-03-10 00:53:51 UTC (rev 345) @@ -1,134 +0,0 @@ -/* - * #%L - * EchoBase :: Services - * - * $Id$ - * $HeadURL$ - * %% - * Copyright (C) 2011 - 2012 Ifremer, Codelutin - * %% - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * #L% - */ -package fr.ifremer.echobase.services; - -import fr.ifremer.echobase.entities.data.Echotype; -import fr.ifremer.echobase.entities.data.LengthAgeKey; -import fr.ifremer.echobase.entities.data.LengthWeightKey; -import fr.ifremer.echobase.services.configurations.ResultsImportConfiguration; -import fr.ifremer.echobase.services.configurations.ResultsImportMode; -import fr.ifremer.echobase.services.csv.CsvFileImportResult; -import org.junit.Assert; -import org.junit.Test; - -import java.util.List; - -/** - * Tests {@link ResultsImportService}. - * - * @author tchemit <chemit@codelutin.com> - * @since 0.3 - */ -public class ResultsImportServiceTest extends EchoBaseTestServiceSupport { - - protected FakeEchoBaseServiceContext initContext() { - return new FakeEchoBaseServiceContext( - ImportDataConstants.IMPORT_DATA_ECHOBASE_COMMON_DATA_RESULT); - } - - protected String[] getImportPath(String filename) { - return new String[]{"/import-data", "result", filename}; - } - - @Test - public void doImportLengthAgeKey() throws Exception { - - assertImportCommonData(); - assertNoEntities(LengthAgeKey.class); - - ResultsImportConfiguration conf = - new ResultsImportConfiguration(getLocale()); - - conf.setVoyageId(getVoyageId()); - conf.setImportMode(ResultsImportMode.VOYAGE); - - prepareInputFile(conf.getLengthAgeKeyFile(), - getImportPath("lengthAgeKey.csv")); - - ResultsImportService service = - getService(ResultsImportService.class); - - List<CsvFileImportResult> result = - service.doImport(conf, createFakeUser()); - - Assert.assertNotNull(result); - Assert.assertEquals(1, result.size()); - assertCsvImportResult(result.get(0), LengthAgeKey.class, - ImportDataConstants.NB_LENGTH_AGE_KEY); - } - - @Test - public void doImportLengthWeightKey() throws Exception { - - assertImportCommonData(); - assertNoEntities(LengthWeightKey.class); - - ResultsImportConfiguration conf = - new ResultsImportConfiguration(getLocale()); - - conf.setVoyageId(getVoyageId()); - conf.setImportMode(ResultsImportMode.VOYAGE); - - prepareInputFile(conf.getLengthWeightKeyFile(), - getImportPath("lengthWeightKey.csv")); - - ResultsImportService service = - getService(ResultsImportService.class); - - List<CsvFileImportResult> result = - service.doImport(conf, createFakeUser()); - - Assert.assertNotNull(result); - Assert.assertEquals(1, result.size()); - assertCsvImportResult(result.get(0), LengthWeightKey.class, - ImportDataConstants.NB_LENGTH_WEIGHT_KEY); - } - - @Test - public void doImportEchotype() throws Exception { - - assertImportCommonData(); - assertNoEntities(Echotype.class); - - ResultsImportConfiguration conf = - new ResultsImportConfiguration(getLocale()); - - conf.setVoyageId(getVoyageId()); - conf.setImportMode(ResultsImportMode.VOYAGE); - - prepareInputFile(conf.getEchotypeFile(), getImportPath("echotype.csv")); - - ResultsImportService service = - getService(ResultsImportService.class); - - List<CsvFileImportResult> result = - service.doImport(conf, createFakeUser()); - - Assert.assertNotNull(result); - Assert.assertEquals(1, result.size()); - assertCsvImportResult(result.get(0), Echotype.class, - ImportDataConstants.NB_ECHOTYPE); - } - -} Added: trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/importdata/AbstractImportDataServiceIT.java =================================================================== --- trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/importdata/AbstractImportDataServiceIT.java (rev 0) +++ trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/importdata/AbstractImportDataServiceIT.java 2012-03-10 00:53:51 UTC (rev 345) @@ -0,0 +1,126 @@ +/* + * #%L + * EchoBase :: Services + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2011 - 2012 Ifremer, Codelutin + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * #L% + */ +package fr.ifremer.echobase.services.importdata; + +import fr.ifremer.echobase.entities.data.Cell; +import fr.ifremer.echobase.entities.data.Data; +import fr.ifremer.echobase.entities.data.DataAcquisition; +import fr.ifremer.echobase.entities.data.DataProcessing; +import fr.ifremer.echobase.entities.data.GearMetadataValue; +import fr.ifremer.echobase.entities.data.Operation; +import fr.ifremer.echobase.entities.data.OperationMetadataValue; +import fr.ifremer.echobase.entities.data.Sample; +import fr.ifremer.echobase.entities.data.SampleData; +import fr.ifremer.echobase.entities.data.Transect; +import fr.ifremer.echobase.entities.data.Transit; +import fr.ifremer.echobase.entities.data.Voyage; +import fr.ifremer.echobase.services.EchoBaseTestServiceSupport; +import org.nuiton.topia.TopiaException; + +/** + * Abstrac import data test. + * + * @author tchemit <chemit@codelutin.com> + * @since 0.3 + */ +public abstract class AbstractImportDataServiceIT extends EchoBaseTestServiceSupport { + + public static final String IMPORT_DATA_ECHOBASE_NO_DATA = "/import-data/echobase-0.3-nodata.h2.db.gz"; + + public static final String IMPORT_DATA_ECHOBASE_COMMON_DATA_RESULT = "/import-data/echobase-0.3-commonData-result.h2.db.gz"; + + public static final String IMPORT_DATA_ECHOBASE_OPERATION_RESULT = "/import-data/echobase-0.3-operation-result.h2.db.gz"; + + public static final String VESSEL_ID = "fr.ifremer.echobase.entities.references.Vessel#1323196672049#0.9790502711645855"; + + public static final String MISSION_ID = "fr.ifremer.echobase.entities.references.Mission#1323127544274#0.7939481378378231"; + + public static final String AREA_OF_OPERATION_ID = "fr.ifremer.echobase.entities.references.AreaOfOperation#1323128474277#0.011341599655098622"; + + public static final int NB_VOYAGE = 1; + + public static final int NB_TRANSIT = 3; + + public static final int NB_TRANSECT = 251; + + public static final int NB_OPERATION = 131; + + public static final int NB_OPERATION_METADATAVALUE = 393; + + public static final int NB_GEAR_METADATAVALUE = 917; + + public static final int NB_SAMPLE_TOTAL = 1232; + + public static final int NB_SAMPLE_DATA_TOTAL = 1834; + + public static final int NB_SAMPLE_UNSORTED = 605; + + public static final int NB_SAMPLE_DATA_UNSORTED = 8057; + + public static final int NB_SAMPLE_BIOMETRY = 0; + + public static final int NB_SAMPLE_DATA_BIOMETRY = 0; + + public static final int NB_SAMPLE = NB_SAMPLE_TOTAL + NB_SAMPLE_UNSORTED + NB_SAMPLE_BIOMETRY; + + public static final int NB_SAMPLE_DATA = NB_SAMPLE_DATA_TOTAL + NB_SAMPLE_DATA_UNSORTED + NB_SAMPLE_DATA_BIOMETRY; + + public static final int NB_DATA_ACQUISITION = 61; + + public static final int NB_DATA_PROCESSING = 183; + + public static final int NB_CELL = 427; + + public static final int NB_DATA = 427; + + public static final int NB_LENGTH_AGE_KEY = 404; + + public static final int NB_LENGTH_WEIGHT_KEY = 7; + + public static final int NB_ECHOTYPE = 8; + + public void assertImportCommonData() throws TopiaException { + assertNbEntities(Voyage.class, NB_VOYAGE); + assertNbEntities(Transit.class, NB_TRANSIT); + assertNbEntities(Transect.class, NB_TRANSECT); + } + + public void assertImportOperations() throws TopiaException { + assertNbEntities(Operation.class, NB_OPERATION); + assertNbEntities(OperationMetadataValue.class, NB_OPERATION_METADATAVALUE); + assertNbEntities(GearMetadataValue.class, NB_GEAR_METADATAVALUE); + } + + public void assertImportSampleDatas() throws TopiaException { + assertNbEntities(Sample.class, NB_SAMPLE); + assertNbEntities(SampleData.class, NB_SAMPLE_DATA); + } + + public void assertImportAcousticDatas() throws TopiaException { + assertNbEntities(DataAcquisition.class, NB_DATA_ACQUISITION); + assertNbEntities(DataProcessing.class, NB_DATA_PROCESSING); + assertNbEntities(Cell.class, NB_CELL); + assertNbEntities(Data.class, NB_DATA); + } +} Copied: trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/importdata/AcousticDataImportServiceIT.java (from rev 334, trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/AcousticDataImportServiceTest.java) =================================================================== --- trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/importdata/AcousticDataImportServiceIT.java (rev 0) +++ trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/importdata/AcousticDataImportServiceIT.java 2012-03-10 00:53:51 UTC (rev 345) @@ -0,0 +1,120 @@ +/* + * #%L + * EchoBase :: Services + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2011 - 2012 Ifremer, Codelutin + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * #L% + */ +package fr.ifremer.echobase.services.importdata; + +import fr.ifremer.echobase.entities.data.Cell; +import fr.ifremer.echobase.entities.data.Data; +import fr.ifremer.echobase.entities.data.DataAcquisition; +import fr.ifremer.echobase.entities.data.DataProcessing; +import fr.ifremer.echobase.entities.references.AcousticInstrument; +import fr.ifremer.echobase.services.FakeEchoBaseServiceContext; +import fr.ifremer.echobase.services.configurations.AcousticDataImportConfiguration; +import fr.ifremer.echobase.services.configurations.CellPositionReference; +import fr.ifremer.echobase.services.csv.CsvFileImportResult; +import org.junit.Assert; +import org.junit.Ignore; +import org.junit.Test; + +import java.util.List; + +/** + * Tests {@link AcousticDataImportService}. + * + * @author tchemit <chemit@codelutin.com> + * @since 0.3 + */ +public class AcousticDataImportServiceIT extends AbstractImportDataServiceIT { + + protected FakeEchoBaseServiceContext initContext() { + return new FakeEchoBaseServiceContext( + IMPORT_DATA_ECHOBASE_COMMON_DATA_RESULT); + } + + protected String[] getImportPath(String filename) { + return new String[]{"/import-data", "acousticData", filename}; + } + + @Ignore + @Test + public void doImport() throws Exception { + + assertImportCommonData(); + + assertNoEntities(DataAcquisition.class, + DataProcessing.class, + Cell.class, + Data.class); + + AcousticDataImportConfiguration conf = + new AcousticDataImportConfiguration(getLocale()); + + conf.setVoyageId(getVoyageId()); + conf.setVesselId(VESSEL_ID); + conf.setAcousticDensityUnit("acousticDensityUnit"); + conf.setAcquisitionSoftwareVersionER60("acquisitionSoftwareVersionER60"); + conf.setAcquisitionSoftwareVersionME70("acquisitionSoftwareVersionME70"); + conf.setAddDataAcquisition(false); + conf.setTransceiverAcquisitionAbsorptionDescription("transceiverAcquisitionAbsorptionDescription"); + conf.setCellPositionReference(CellPositionReference.START); + conf.setDigitThreshold(1.5f); + conf.setEchosounderSoundSpeed("echosounderSoundSpeed"); + conf.setLoggedDataDatatype("loggedDataDatatype"); + conf.setLoggedDataFormat("loggedDataFormat"); + conf.setNotes("notes"); + conf.setPingDutyCycle("pingDutyCycle"); + conf.setProcessingDescription("processingDescription"); + conf.setProcessingTemplate("processingTemplate"); + conf.setSounderConstant(1.0f); + conf.setSoundSpeedCalculationsER60("soundSpeedCalculationsER60"); + conf.setSoundSpeedCalculationsME70("soundSpeedCalculationsME70"); + conf.setTransceiverAcquisitionAbsorptionDescription("transceiverAcquisitionAbsorptionDescription"); + + prepareInputFile(conf.getMoviesFile(), getImportPath("movies.csv")); + + AcousticDataImportService service = + getService(AcousticDataImportService.class); + + //FIXME : remove this when db will have such data + service.create(getDAO(AcousticInstrument.class), AcousticInstrument.PROPERTY_ID, "ES18-11"); + service.create(getDAO(AcousticInstrument.class), AcousticInstrument.PROPERTY_ID, "ES38B"); + service.create(getDAO(AcousticInstrument.class), AcousticInstrument.PROPERTY_ID, "ES70-7C"); + service.create(getDAO(AcousticInstrument.class), AcousticInstrument.PROPERTY_ID, "ES120-7"); + service.create(getDAO(AcousticInstrument.class), AcousticInstrument.PROPERTY_ID, "ES200-7C"); + service.create(getDAO(AcousticInstrument.class), AcousticInstrument.PROPERTY_ID, "ME70"); + + List<CsvFileImportResult> result = + service.doImport(conf, createFakeUser()); + + Assert.assertNotNull(result); + Assert.assertEquals(1, result.size()); + CsvFileImportResult importResult = result.get(0); + assertCsvImportResult(importResult, DataAcquisition.class, NB_DATA_ACQUISITION); + assertCsvImportResult(importResult, DataProcessing.class, NB_DATA_PROCESSING); + assertCsvImportResult(importResult, Cell.class, NB_CELL); + assertCsvImportResult(importResult, Data.class, NB_DATA); + + assertImportAcousticDatas(); + } + +} Property changes on: trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/importdata/AcousticDataImportServiceIT.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Copied: trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/importdata/CatchesDataImportServiceIT.java (from rev 338, trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/CatchesDataImportServiceTest.java) =================================================================== --- trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/importdata/CatchesDataImportServiceIT.java (rev 0) +++ trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/importdata/CatchesDataImportServiceIT.java 2012-03-10 00:53:51 UTC (rev 345) @@ -0,0 +1,187 @@ +/* + * #%L + * EchoBase :: Services + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2011 - 2012 Ifremer, Codelutin + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * #L% + */ +package fr.ifremer.echobase.services.importdata; + +import fr.ifremer.echobase.entities.data.Sample; +import fr.ifremer.echobase.entities.data.SampleData; +import fr.ifremer.echobase.services.FakeEchoBaseServiceContext; +import fr.ifremer.echobase.services.configurations.CatchesDataImportConfiguration; +import fr.ifremer.echobase.services.csv.CsvFileImportResult; +import org.junit.Assert; +import org.junit.Ignore; +import org.junit.Test; + +import java.util.Iterator; +import java.util.List; + +/** + * Tests {@link CatchesDataImportService}. + * + * @author tchemit <chemit@codelutin.com> + * @since 0.3 + */ +public class CatchesDataImportServiceIT extends AbstractImportDataServiceIT { + + protected FakeEchoBaseServiceContext initContext() { + return new FakeEchoBaseServiceContext( + IMPORT_DATA_ECHOBASE_OPERATION_RESULT); + } + + protected String[] getImportPath(String filename) { + return new String[]{"/import-data", "catchesData", filename}; + } + + @Test + public void doImportAllSamples() throws Exception { + + assertImportCommonData(); + assertImportOperations(); + assertNoEntities(Sample.class, SampleData.class); + + CatchesDataImportConfiguration conf = + new CatchesDataImportConfiguration(getLocale()); + + conf.setVoyageId(getVoyageId()); + + prepareInputFile(conf.getTotalSampleFile(), + getImportPath("totalsample.csv")); + prepareInputFile(conf.getSubSampleFile(), + getImportPath("subsample.csv")); +// prepareInputFile(conf.getBiometrySampleFile(), +// getImportPath("biometrysample.csv")); + + CatchesDataImportService service = + getService(CatchesDataImportService.class); + + List<CsvFileImportResult> result = service.doImport(conf, + createFakeUser()); + + Assert.assertNotNull(result); +// Assert.assertEquals(3, result.size()); + Assert.assertEquals(2, result.size()); + Iterator<CsvFileImportResult> iterator = result.iterator(); + CsvFileImportResult importResult; + + importResult = iterator.next(); + assertCsvImportResult(importResult, Sample.class, NB_SAMPLE_TOTAL, 0, NB_SAMPLE); + assertCsvImportResult(importResult, SampleData.class, NB_SAMPLE_DATA_TOTAL, 0, NB_SAMPLE_DATA); + importResult = iterator.next(); + assertCsvImportResult(importResult, Sample.class, NB_SAMPLE_UNSORTED, 0, NB_SAMPLE); + assertCsvImportResult(importResult, SampleData.class, NB_SAMPLE_DATA_UNSORTED, 0, NB_SAMPLE_DATA); +// importResult = iterator.next(); +// assertCsvImportResult(importResult, Sample.class, NB_SAMPLE_BIOMETRY, 0, NB_SAMPLE); +// assertCsvImportResult(importResult, SampleData.class, NB_SAMPLE_DATA_BIOMETRY, 0, NB_SAMPLE_DATA); + + assertImportSampleDatas(); + } + + @Ignore + @Test + public void doImportTotalSample() throws Exception { + + assertImportCommonData(); + assertImportOperations(); + assertNoEntities(Sample.class, SampleData.class); + + CatchesDataImportConfiguration conf = + new CatchesDataImportConfiguration(getLocale()); + + conf.setVoyageId(getVoyageId()); + + prepareInputFile(conf.getTotalSampleFile(), + getImportPath("totalsample.csv")); + + CatchesDataImportService service = + getService(CatchesDataImportService.class); + + List<CsvFileImportResult> result = service.doImport(conf, + createFakeUser()); + + Assert.assertNotNull(result); + Assert.assertEquals(1, result.size()); + CsvFileImportResult importResult = result.get(0); + assertCsvImportResult(importResult, Sample.class, NB_SAMPLE_TOTAL); + assertCsvImportResult(importResult, SampleData.class, NB_SAMPLE_DATA_TOTAL); + } + + @Ignore + @Test + public void doImportSubSample() throws Exception { + + assertImportCommonData(); + assertImportOperations(); + assertNoEntities(Sample.class, SampleData.class); + + CatchesDataImportConfiguration conf = + new CatchesDataImportConfiguration(getLocale()); + + conf.setVoyageId(getVoyageId()); + + prepareInputFile(conf.getSubSampleFile(), + getImportPath("subsample.csv")); + + CatchesDataImportService service = + getService(CatchesDataImportService.class); + + List<CsvFileImportResult> result = service.doImport(conf, + createFakeUser()); + + Assert.assertNotNull(result); + Assert.assertEquals(1, result.size()); + Iterator<CsvFileImportResult> iterator = result.iterator(); + CsvFileImportResult importResult = iterator.next(); + assertCsvImportResult(importResult, Sample.class, NB_SAMPLE_UNSORTED); + assertCsvImportResult(importResult, SampleData.class, NB_SAMPLE_DATA_UNSORTED); + } + + @Ignore + @Test + public void doImportBiometrySample() throws Exception { + + assertImportCommonData(); + assertImportOperations(); + assertNoEntities(Sample.class, SampleData.class); + + CatchesDataImportConfiguration conf = + new CatchesDataImportConfiguration(getLocale()); + + conf.setVoyageId(getVoyageId()); + + prepareInputFile(conf.getBiometrySampleFile(), + getImportPath("biometrysample.csv")); + + CatchesDataImportService service = + getService(CatchesDataImportService.class); + List<CsvFileImportResult> result = service.doImport(conf, + createFakeUser()); + + Assert.assertNotNull(result); + Assert.assertEquals(1, result.size()); + Iterator<CsvFileImportResult> iterator = result.iterator(); + CsvFileImportResult importResult = iterator.next(); + assertCsvImportResult(importResult, Sample.class, NB_SAMPLE_BIOMETRY); + assertCsvImportResult(importResult, SampleData.class, NB_SAMPLE_DATA_BIOMETRY); + } + +} Property changes on: trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/importdata/CatchesDataImportServiceIT.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Copied: trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/importdata/CommonData2ImportServiceIT.java (from rev 334, trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/CommonDataImportService2Test.java) =================================================================== --- trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/importdata/CommonData2ImportServiceIT.java (rev 0) +++ trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/importdata/CommonData2ImportServiceIT.java 2012-03-10 00:53:51 UTC (rev 345) @@ -0,0 +1,93 @@ +/* + * #%L + * EchoBase :: Services + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2011 - 2012 Ifremer, Codelutin + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * #L% + */ +package fr.ifremer.echobase.services.importdata; + +import fr.ifremer.echobase.entities.data.Transect; +import fr.ifremer.echobase.entities.data.Transit; +import fr.ifremer.echobase.services.FakeEchoBaseServiceContext; +import fr.ifremer.echobase.services.configurations.CommonDataImportConfiguration; +import fr.ifremer.echobase.services.configurations.CommonDataImportMode; +import fr.ifremer.echobase.services.csv.CsvFileImportResult; +import org.junit.Assert; +import org.junit.Test; + +import java.util.Iterator; +import java.util.List; + +/** + * Tests {@link CommonDataImportService} with {@link CommonDataImportMode#TRANSECT} mode. + * + * @author tchemit <chemit@codelutin.com> + * @since 0.3 + */ +public class CommonData2ImportServiceIT extends AbstractImportDataServiceIT { + + protected FakeEchoBaseServiceContext initContext() { + return new FakeEchoBaseServiceContext( + IMPORT_DATA_ECHOBASE_COMMON_DATA_RESULT); + } + + @Test + public void doImport() throws Exception { + + // import with mode transect + + assertImportCommonData(); + + CommonDataImportConfiguration conf = + new CommonDataImportConfiguration(getLocale()); + + conf.setAreaOfOperationId(AREA_OF_OPERATION_ID); + conf.setDatum("datum"); + conf.setVoyageId(getVoyageId()); + conf.setTransectBinUnitsPingAxis("transectBinUnitsPingAxis"); + conf.setTransectGeospatialVerticalPositive("transectGeospatialVerticalPositive"); + conf.setTransectLicence("transectLicence"); + + prepareInputFile(conf.getTransectFile(), getImportPath("transect.csv")); + + conf.setImportMode(CommonDataImportMode.TRANSECT); + + CommonDataImportService service = + getService(CommonDataImportService.class); + + // let's delete every transects + for (Transit transit : getEntities(Transit.class)) { + transit.clearTransect(); + } + List<CsvFileImportResult> result = service.doImport(conf, createFakeUser()); + Assert.assertNotNull(result); + Assert.assertEquals(1, result.size()); + Iterator<CsvFileImportResult> iterator = result.iterator(); + assertCsvImportResult(iterator.next(), Transect.class, + NB_TRANSECT); + + assertImportCommonData(); + } + + protected String[] getImportPath(String filename) { + return new String[]{"/import-data", "commonData", filename}; + } + +} Property changes on: trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/importdata/CommonData2ImportServiceIT.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Copied: trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/importdata/CommonDataImportServiceIT.java (from rev 334, trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/CommonDataImportServiceTest.java) =================================================================== --- trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/importdata/CommonDataImportServiceIT.java (rev 0) +++ trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/importdata/CommonDataImportServiceIT.java 2012-03-10 00:53:51 UTC (rev 345) @@ -0,0 +1,100 @@ +/* + * #%L + * EchoBase :: Services + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2011 - 2012 Ifremer, Codelutin + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * #L% + */ +package fr.ifremer.echobase.services.importdata; + +import fr.ifremer.echobase.entities.data.Transect; +import fr.ifremer.echobase.entities.data.Transit; +import fr.ifremer.echobase.entities.data.Voyage; +import fr.ifremer.echobase.services.FakeEchoBaseServiceContext; +import fr.ifremer.echobase.services.configurations.CommonDataImportConfiguration; +import fr.ifremer.echobase.services.configurations.CommonDataImportMode; +import fr.ifremer.echobase.services.csv.CsvFileImportResult; +import org.junit.Assert; +import org.junit.Test; + +import java.util.Iterator; +import java.util.List; + +/** + * Tests {@link CommonDataImportService} with {@link CommonDataImportMode#ALL} mode. + * + * @author tchemit <chemit@codelutin.com> + * @since 0.3 + */ +public class CommonDataImportServiceIT extends AbstractImportDataServiceIT { + + @Override + protected FakeEchoBaseServiceContext initContext() { + return new FakeEchoBaseServiceContext( + IMPORT_DATA_ECHOBASE_NO_DATA); + } + + @Test + public void doImport() throws Exception { + + // no data in db + + assertNoEntities(Voyage.class, Transit.class, Transect.class); + + // import with mode all (voyage / transit / transect) + + CommonDataImportConfiguration conf = + new CommonDataImportConfiguration(getLocale()); + + conf.setAreaOfOperationId(AREA_OF_OPERATION_ID); + conf.setDatum("datum"); + conf.setMissionId(MISSION_ID); + conf.setTransectBinUnitsPingAxis("transectBinUnitsPingAxis"); + conf.setTransectGeospatialVerticalPositive("transectGeospatialVerticalPositive"); + conf.setTransectLicence("transectLicence"); + conf.setTransitRelatedActivity("transitRelatedActivity"); + conf.setVoyageDescription("voyageDescription"); + + prepareInputFile(conf.getVoyageFile(), getImportPath("voyage.csv")); + prepareInputFile(conf.getTransitFile(), getImportPath("transit.csv")); + prepareInputFile(conf.getTransectFile(), getImportPath("transect.csv")); + + conf.setImportMode(CommonDataImportMode.ALL); + + CommonDataImportService service = + getService(CommonDataImportService.class); + + List<CsvFileImportResult> result = + service.doImport(conf, createFakeUser()); + + Assert.assertNotNull(result); + Assert.assertEquals(3, result.size()); + Iterator<CsvFileImportResult> iterator = result.iterator(); + assertCsvImportResult(iterator.next(), Voyage.class, NB_VOYAGE); + assertCsvImportResult(iterator.next(), Transit.class, NB_TRANSIT); + assertCsvImportResult(iterator.next(), Transect.class, NB_TRANSECT); + + assertImportCommonData(); + } + + protected String[] getImportPath(String filename) { + return new String[]{"/import-data", "commonData", filename}; + } + +} Property changes on: trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/importdata/CommonDataImportServiceIT.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Copied: trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/importdata/OperationImportServiceIT.java (from rev 338, trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/OperationImportServiceTest.java) =================================================================== --- trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/importdata/OperationImportServiceIT.java (rev 0) +++ trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/importdata/OperationImportServiceIT.java 2012-03-10 00:53:51 UTC (rev 345) @@ -0,0 +1,90 @@ +/* + * #%L + * EchoBase :: Services + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2011 - 2012 Ifremer, Codelutin + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * #L% + */ +package fr.ifremer.echobase.services.importdata; + +import fr.ifremer.echobase.entities.data.GearMetadataValue; +import fr.ifremer.echobase.entities.data.Operation; +import fr.ifremer.echobase.entities.data.OperationMetadataValue; +import fr.ifremer.echobase.services.FakeEchoBaseServiceContext; +import fr.ifremer.echobase.services.configurations.OperationImportConfiguration; +import fr.ifremer.echobase.services.csv.CsvFileImportResult; +import org.junit.Assert; +import org.junit.Test; + +import java.util.Iterator; +import java.util.List; + +/** + * Tests {@link OperationImportService}. + * + * @author tchemit <chemit@codelutin.com> + * @since 0.3 + */ +public class OperationImportServiceIT extends AbstractImportDataServiceIT { + + protected FakeEchoBaseServiceContext initContext() { + return new FakeEchoBaseServiceContext( + IMPORT_DATA_ECHOBASE_COMMON_DATA_RESULT); + } + + protected String[] getImportPath(String filename) { + return new String[]{"/import-data", "operation", filename}; + } + + @Test + public void doImport() throws Exception { + + assertImportCommonData(); + + assertNoEntities(Operation.class, + OperationMetadataValue.class, + GearMetadataValue.class); + + OperationImportConfiguration conf = + new OperationImportConfiguration(getLocale()); + + conf.setVoyageId(getVoyageId()); + + prepareInputFile(conf.getOperationFile(), + getImportPath("operation.csv")); + prepareInputFile(conf.getOperationMetadataFile(), + getImportPath("operationmetadatavalue.csv")); + prepareInputFile(conf.getGearMetadataFile(), + getImportPath("gearmetadatavalue.csv")); + + OperationImportService service = + getService(OperationImportService.class); + + List<CsvFileImportResult> result = service.doImport(conf, createFakeUser()); + + Assert.assertNotNull(result); + Assert.assertEquals(3, result.size()); + Iterator<CsvFileImportResult> iterator = result.iterator(); + assertCsvImportResult(iterator.next(), Operation.class, NB_OPERATION); + assertCsvImportResult(iterator.next(), OperationMetadataValue.class, NB_OPERATION_METADATAVALUE); + assertCsvImportResult(iterator.next(), GearMetadataValue.class, NB_GEAR_METADATAVALUE); + + assertImportOperations(); + } +} Property changes on: trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/importdata/OperationImportServiceIT.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Copied: trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/importdata/ResultsImportServiceIT.java (from rev 336, trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/ResultsImportServiceTest.java) =================================================================== --- trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/importdata/ResultsImportServiceIT.java (rev 0) +++ trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/importdata/ResultsImportServiceIT.java 2012-03-10 00:53:51 UTC (rev 345) @@ -0,0 +1,135 @@ +/* + * #%L + * EchoBase :: Services + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2011 - 2012 Ifremer, Codelutin + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * #L% + */ +package fr.ifremer.echobase.services.importdata; + +import fr.ifremer.echobase.entities.data.Echotype; +import fr.ifremer.echobase.entities.data.LengthAgeKey; +import fr.ifremer.echobase.entities.data.LengthWeightKey; +import fr.ifremer.echobase.services.FakeEchoBaseServiceContext; +import fr.ifremer.echobase.services.configurations.ResultsImportConfiguration; +import fr.ifremer.echobase.services.configurations.ResultsImportMode; +import fr.ifremer.echobase.services.csv.CsvFileImportResult; +import org.junit.Assert; +import org.junit.Test; + +import java.util.List; + +/** + * Tests {@link ResultsImportService}. + * + * @author tchemit <chemit@codelutin.com> + * @since 0.3 + */ +public class ResultsImportServiceIT extends AbstractImportDataServiceIT { + + protected FakeEchoBaseServiceContext initContext() { + return new FakeEchoBaseServiceContext( + IMPORT_DATA_ECHOBASE_COMMON_DATA_RESULT); + } + + protected String[] getImportPath(String filename) { + return new String[]{"/import-data", "result", filename}; + } + + @Test + public void doImportLengthAgeKey() throws Exception { + + assertImportCommonData(); + assertNoEntities(LengthAgeKey.class); + + ResultsImportConfiguration conf = + new ResultsImportConfiguration(getLocale()); + + conf.setVoyageId(getVoyageId()); + conf.setImportMode(ResultsImportMode.VOYAGE); + + prepareInputFile(conf.getLengthAgeKeyFile(), + getImportPath("lengthAgeKey.csv")); + + ResultsImportService service = + getService(ResultsImportService.class); + + List<CsvFileImportResult> result = + service.doImport(conf, createFakeUser()); + + Assert.assertNotNull(result); + Assert.assertEquals(1, result.size()); + assertCsvImportResult(result.get(0), LengthAgeKey.class, + NB_LENGTH_AGE_KEY); + } + + @Test + public void doImportLengthWeightKey() throws Exception { + + assertImportCommonData(); + assertNoEntities(LengthWeightKey.class); + + ResultsImportConfiguration conf = + new ResultsImportConfiguration(getLocale()); + + conf.setVoyageId(getVoyageId()); + conf.setImportMode(ResultsImportMode.VOYAGE); + + prepareInputFile(conf.getLengthWeightKeyFile(), + getImportPath("lengthWeightKey.csv")); + + ResultsImportService service = + getService(ResultsImportService.class); + + List<CsvFileImportResult> result = + service.doImport(conf, createFakeUser()); + + Assert.assertNotNull(result); + Assert.assertEquals(1, result.size()); + assertCsvImportResult(result.get(0), LengthWeightKey.class, + NB_LENGTH_WEIGHT_KEY); + } + + @Test + public void doImportEchotype() throws Exception { + + assertImportCommonData(); + assertNoEntities(Echotype.class); + + ResultsImportConfiguration conf = + new ResultsImportConfiguration(getLocale()); + + conf.setVoyageId(getVoyageId()); + conf.setImportMode(ResultsImportMode.VOYAGE); + + prepareInputFile(conf.getEchotypeFile(), getImportPath("echotype.csv")); + + ResultsImportService service = + getService(ResultsImportService.class); + + List<CsvFileImportResult> result = + service.doImport(conf, createFakeUser()); + + Assert.assertNotNull(result); + Assert.assertEquals(1, result.size()); + assertCsvImportResult(result.get(0), Echotype.class, + NB_ECHOTYPE); + } + +} Property changes on: trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/importdata/ResultsImportServiceIT.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native