Author: tchemit Date: 2011-11-27 11:06:07 +0100 (Sun, 27 Nov 2011) New Revision: 134 Url: http://forge.codelutin.com/repositories/revision/echobase/134 Log: - ajout type d'import de donn?\195?\169es (enum ImportDataMode) - mise en place squelette import historique Added: trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/ImportDataMode.java trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/ImportHistoricalConfiguration.java trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/ImportHistoricalService.java Modified: trunk/echobase-services/src/main/java/fr/ifremer/echobase/InputFile.java trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/ImportDataConfiguration.java trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/ImportDataService.java trunk/echobase-services/src/main/resources/i18n/echobase-services_fr_FR.properties Modified: trunk/echobase-services/src/main/java/fr/ifremer/echobase/InputFile.java =================================================================== --- trunk/echobase-services/src/main/java/fr/ifremer/echobase/InputFile.java 2011-11-26 11:02:53 UTC (rev 133) +++ trunk/echobase-services/src/main/java/fr/ifremer/echobase/InputFile.java 2011-11-27 10:06:07 UTC (rev 134) @@ -48,7 +48,11 @@ /** Label of the import. */ protected String label; - public InputFile(String label) { + public static InputFile newFile(String label) { + return new InputFile(label); + } + + protected InputFile(String label) { this.label = label; } Modified: trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/ImportDataConfiguration.java =================================================================== --- trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/ImportDataConfiguration.java 2011-11-26 11:02:53 UTC (rev 133) +++ trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/ImportDataConfiguration.java 2011-11-27 10:06:07 UTC (rev 134) @@ -25,7 +25,10 @@ import fr.ifremer.echobase.InputFile; import fr.ifremer.echobase.entities.references.Mission; +import org.apache.commons.io.FileUtils; +import java.io.File; +import java.io.IOException; import java.io.Serializable; import java.util.Locale; @@ -44,6 +47,10 @@ /** Mission used for the import (may not be in db if needed to be created). */ protected Mission mission; + protected ImportDataMode importDataMode; + + protected File workingDirectory; + protected InputFile acousticFile; protected InputFile captureFile; @@ -61,13 +68,26 @@ protected float progression; public ImportDataConfiguration(Locale locale) { - acousticFile = new InputFile(l_(locale, "echobase.common.acousticImport")); - captureFile = new InputFile(l_(locale, "echobase.common.captureImport")); - lectureAgeGenFile = new InputFile(l_(locale, "echobase.common.lectureAgeGenImport")); - eventsFile = new InputFile(l_(locale, "echobase.common.eventsImport")); - typeEchoSpeciesFile = new InputFile(l_(locale, "echobase.common.typeEchoSpeciesImport")); + acousticFile = InputFile.newFile( + l_(locale, "echobase.common.acousticImport")); + captureFile = InputFile.newFile( + l_(locale, "echobase.common.captureImport")); + lectureAgeGenFile = InputFile.newFile( + l_(locale, "echobase.common.lectureAgeGenImport")); + eventsFile = InputFile.newFile( + l_(locale, "echobase.common.eventsImport")); + typeEchoSpeciesFile = InputFile.newFile( + l_(locale, "echobase.common.typeEchoSpeciesImport")); } + public ImportDataMode getImportDataMode() { + return importDataMode; + } + + public File getWorkingDirectory() { + return workingDirectory; + } + public Mission getMission() { return mission; } @@ -100,6 +120,14 @@ return progression; } + public void setImportDataMode(ImportDataMode importDataMode) { + this.importDataMode = importDataMode; + } + + public void setWorkingDirectory(File workingDirectory) { + this.workingDirectory = workingDirectory; + } + public void setMission(Mission mission) { this.mission = mission; } @@ -114,21 +142,19 @@ public void computeSteps() { nbSteps = 0; - if (acousticFile.getFile() != null) { - nbSteps++; + switch (importDataMode) { + case ALL: + nbSteps = 5; + case ACOUSTIC: + nbSteps = 1; + default: } - if (captureFile.getFile() != null) { - nbSteps++; - } - if (lectureAgeGenFile.getFile() != null) { - nbSteps++; - } - if (eventsFile.getFile() != null) { - nbSteps++; - } - if (typeEchoSpeciesFile.getFile() != null) { - nbSteps++; - } stepIncrement = 100f / nbSteps; } + + public void destroy() throws IOException { + if (workingDirectory != null) { + FileUtils.deleteDirectory(workingDirectory); + } + } } Added: trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/ImportDataMode.java =================================================================== --- trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/ImportDataMode.java (rev 0) +++ trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/ImportDataMode.java 2011-11-27 10:06:07 UTC (rev 134) @@ -0,0 +1,50 @@ +/* + * #%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 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 { + + /** Import only acoustic file. */ + ACOUSTIC(n_("echobase.common.importDataMode.acoustic")), + /** Import all files. */ + ALL(n_("echobase.common.importDataMode.all")); + + private final String i18nKey; + + ImportDataMode(String i18nKey) { + this.i18nKey = i18nKey; + } + + public String getI18nKey() { + return i18nKey; + } +} Property changes on: trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/ImportDataMode.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Modified: trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/ImportDataService.java =================================================================== --- trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/ImportDataService.java 2011-11-26 11:02:53 UTC (rev 133) +++ trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/ImportDataService.java 2011-11-27 10:06:07 UTC (rev 134) @@ -60,6 +60,29 @@ Map<InputFile, CsvImportResult> result = Maps.newHashMap(); + + ImportDataMode importDataMode = configuration.getImportDataMode(); + switch (importDataMode) { + case ACOUSTIC: + doImportAcoustic(configuration, result); + break; + case ALL: + doImportAll(configuration, result); + break; + default: + throw new IllegalStateException( + "Can not know what to do with importDataMode " + + importDataMode); + + } + + + return result; + } + + protected void doImportAcoustic(ImportDataConfiguration configuration, + Map<InputFile, CsvImportResult> result) { + Mission mission = configuration.getMission(); // compute nb step to treate (means nb imports to do) @@ -83,7 +106,35 @@ deleteFile(acousticFile); } } + } + protected void doImportAll(ImportDataConfiguration configuration, + Map<InputFile, CsvImportResult> result) { + + Mission mission = configuration.getMission(); + + // compute nb step to treate (means nb imports to do) + configuration.computeSteps(); + + if (log.isInfoEnabled()) { + log.info("Starts data import with mission " + + mission.getMissionName()); + } + + InputFile acousticFile = configuration.getAcousticFile(); + if (acousticFile.getFile() != null) { + + // do acoustic import + try { + CsvImportResult importResult = + importAcoustic(mission, acousticFile); + configuration.incrementsProgression(); + result.put(acousticFile, importResult); + } finally { + deleteFile(acousticFile); + } + } + InputFile captureFile = configuration.getCaptureFile(); if (captureFile.getFile() != null) { @@ -139,8 +190,6 @@ deleteFile(typeEchoSpeciesFile); } } - - return result; } protected void deleteFile(InputFile input) { Added: trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/ImportHistoricalConfiguration.java =================================================================== --- trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/ImportHistoricalConfiguration.java (rev 0) +++ trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/ImportHistoricalConfiguration.java 2011-11-27 10:06:07 UTC (rev 134) @@ -0,0 +1,102 @@ +/* + * #%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.InputFile; +import org.apache.commons.io.FileUtils; + +import java.io.File; +import java.io.IOException; +import java.io.Serializable; +import java.util.Locale; + +import static org.nuiton.i18n.I18n.l_; + +/** + * Configuration of an historical import. + * + * @author tchemit <chemit@codelutin.com> + * @since 0.2 + */ +public class ImportHistoricalConfiguration implements Serializable { + + private static final long serialVersionUID = 1L; + + protected File workingDirectory; + + + protected InputFile input; + + protected int nbSteps; + + private float stepIncrement; + + protected float progression; + + public ImportHistoricalConfiguration(Locale locale) { + input = InputFile.newFile( + l_(locale, "echobase.common.importHistoricalFile")); + } + + public File getWorkingDirectory() { + return workingDirectory; + } + + public InputFile getInput() { + return input; + } + + public int getNbSteps() { + return nbSteps; + } + + public float getProgression() { + return progression; + } + + public void setWorkingDirectory(File workingDirectory) { + this.workingDirectory = workingDirectory; + } + + public void setProgression(float progression) { + this.progression = progression; + } + + public void incrementsProgression() { + setProgression(progression + stepIncrement); + } + + public void computeSteps() { + nbSteps = 0; + + stepIncrement = 100f / nbSteps; + } + + public void destroy() throws IOException { + if (workingDirectory != null) { + FileUtils.deleteDirectory(workingDirectory); + } + } + +} \ No newline at end of file Property changes on: trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/ImportHistoricalConfiguration.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Added: trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/ImportHistoricalService.java =================================================================== --- trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/ImportHistoricalService.java (rev 0) +++ trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/ImportHistoricalService.java 2011-11-27 10:06:07 UTC (rev 134) @@ -0,0 +1,94 @@ +/* + * #%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.EchoBaseTechnicalException; +import fr.ifremer.echobase.InputFile; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import java.io.File; + +/** + * To import historical datas (from access database). + * + * @author tchemit <chemit@codelutin.com> + * @since 0.2 + */ +public class ImportHistoricalService extends EchoBaseServiceSupport { + + /** Logger. */ + private static final Log log = + LogFactory.getLog(ImportHistoricalService.class); + + public CsvImportResult startImport(ImportHistoricalConfiguration configuration) { + + CsvImportResult result; + + InputFile input = configuration.getInput(); + + if (log.isInfoEnabled()) { + log.info("Starts historical import with file " + + input.getFileName()); + } + + try { + + result = doImport(input); + + } finally { + deleteFile(input); + } + return result; + } + + protected CsvImportResult doImport(InputFile input) { + + CsvImportResult result = new CsvImportResult(); + prepareImport(); + return result; + } + + protected void deleteFile(InputFile input) { + File file = input.getFile(); + + if (log.isInfoEnabled()) { + log.info("Will delete import file " + file); + } + boolean wasDel = file.delete(); + if (!wasDel) { + throw new EchoBaseTechnicalException("Could not delete file " + file); + } + } + + + protected void prepareImport() { + try { + Thread.sleep(2000); + } catch (InterruptedException e) { + // ignore + } + } + +} Property changes on: trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/ImportHistoricalService.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Modified: trunk/echobase-services/src/main/resources/i18n/echobase-services_fr_FR.properties =================================================================== --- trunk/echobase-services/src/main/resources/i18n/echobase-services_fr_FR.properties 2011-11-26 11:02:53 UTC (rev 133) +++ trunk/echobase-services/src/main/resources/i18n/echobase-services_fr_FR.properties 2011-11-27 10:06:07 UTC (rev 134) @@ -1,5 +1,8 @@ echobase.common.acousticImport=Import Acoustique echobase.common.captureImport=Import Pêcherie echobase.common.eventsImport=Import Evènements +echobase.common.importDataMode.acoustic=Import Acoustic +echobase.common.importDataMode.all=Import globale +echobase.common.importHistoricalFile=Fichier MS-ACCESS de données historiques echobase.common.lectureAgeGenImport=Import Lecture Agen Gen echobase.common.typeEchoSpeciesImport=Import Type EchoSpecies
participants (1)
-
tchemit@users.forge.codelutin.com