r285 - in trunk: coser-business/src/main/java/fr/ifremer/coser coser-business/src/main/java/fr/ifremer/coser/bean coser-business/src/main/java/fr/ifremer/coser/services coser-business/src/test/java/fr/ifremer/coser coser-business/src/test/resources coser-ui/src/main/java/fr/ifremer/coser/ui
Author: chatellier Date: 2010-11-26 10:28:18 +0000 (Fri, 26 Nov 2010) New Revision: 285 Log: Conservation et utilisation des noms originaux des fichiers utilisateur Added: trunk/coser-business/src/test/java/fr/ifremer/coser/CoserUtilsTest.java Modified: trunk/coser-business/src/main/java/fr/ifremer/coser/CoserConstants.java trunk/coser-business/src/main/java/fr/ifremer/coser/CoserUtils.java trunk/coser-business/src/main/java/fr/ifremer/coser/bean/Project.java trunk/coser-business/src/main/java/fr/ifremer/coser/services/ProjectService.java trunk/coser-business/src/test/resources/log4j.properties trunk/coser-ui/src/main/java/fr/ifremer/coser/ui/CoserFrameHandler.java Modified: trunk/coser-business/src/main/java/fr/ifremer/coser/CoserConstants.java =================================================================== --- trunk/coser-business/src/main/java/fr/ifremer/coser/CoserConstants.java 2010-11-26 09:36:00 UTC (rev 284) +++ trunk/coser-business/src/main/java/fr/ifremer/coser/CoserConstants.java 2010-11-26 10:28:18 UTC (rev 285) @@ -68,17 +68,14 @@ /** Suffix des nom de fichiers data apres selection. */ public static final String STORAGE_SELECTION_SUFFIX = "_se"; - /** Extension des fichiers CSV. */ - public static final String STORAGE_CSV_EXTENSION = ".csv"; - /** Categories des données manipulées. */ public static enum Category { - CATCH(n_("coser.business.category.catch"), "catch"), - STRATA(n_("coser.business.category.strata"), "strata"), - HAUL(n_("coser.business.category.haul"), "haul"), - LENGTH(n_("coser.business.category.length"), "length"), - REFTAX_SPECIES(n_("coser.business.category.reftax.species"), "reftaxSpecies", false), - TYPE_ESPECES(n_("coser.business.category.typeEspece"), "codeTypeEspeces", false); + CATCH(n_("coser.business.category.catch"), "catch.csv", true), + STRATA(n_("coser.business.category.strata"), "strata.csv", true), + HAUL(n_("coser.business.category.haul"), "haul.csv", true), + LENGTH(n_("coser.business.category.length"), "length.csv", true), + REFTAX_SPECIES(n_("coser.business.category.reftax.species"), "reftaxSpecies.csv", false), + TYPE_ESPECES(n_("coser.business.category.typeEspece"), "codeTypeEspeces.csv", false); protected String translationKey; protected String storageFileName; @@ -92,15 +89,23 @@ this.dataCategory = dataCategory; } - private Category(String translationKey, String storageFileName) { - this(translationKey, storageFileName, true); - } - public String getTranslationKey() { return translationKey; } - + + /** + * Nom de stockage interne a coser. + * + * N'est pas utilisé pour les categorie de données (nom original utilisé). + * + * @return le nom de stockage (pour les fichier de référence) + */ public String getStorageFileName() { + + if (dataCategory) { + throw new IllegalStateException("Can't use storageFileName for data category !"); + } + return storageFileName; } Modified: trunk/coser-business/src/main/java/fr/ifremer/coser/CoserUtils.java =================================================================== --- trunk/coser-business/src/main/java/fr/ifremer/coser/CoserUtils.java 2010-11-26 09:36:00 UTC (rev 284) +++ trunk/coser-business/src/main/java/fr/ifremer/coser/CoserUtils.java 2010-11-26 10:28:18 UTC (rev 285) @@ -28,6 +28,8 @@ import java.util.ArrayList; import java.util.Iterator; import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import org.apache.commons.lang.StringUtils; import org.nuiton.util.StringUtil; @@ -45,6 +47,9 @@ public static final String BRACKET_STRING_SEPARATOR = ";"; + /** Pattern pour ajouter les suffix (probleme de replace sur le 'dernier' .) */ + public static Pattern FILENAME_SUFFIX_PATTERN = Pattern.compile("^(.+)(\\.[^\\.]+)$"); + /** * Converti une collection de string en une string * delimité par les () pour pouvoir être facilement relu par @@ -86,4 +91,28 @@ } return listArgs; } + + /** + * Dans un nom de fichier, ajoute un suffix dans le nom du fichier + * juste avant l'extension. + * + * Exemple : + * captures.csv > captures_co.csv (ajout du suffix _co) + * captures > captures_co (si pas d'extension) + * + * @param str nom du fichier + * @param suffix suffix a ajouter + * @return nouveau nom du fichier + */ + public static String addSuffixBeforeExtension(String str, String suffix) { + String result = null; + Matcher matcher = FILENAME_SUFFIX_PATTERN.matcher(str); + if (matcher.matches()) { + result = matcher.group(1) + suffix + matcher.group(2); + } + else { + result = str + suffix; + } + return result; + } } Modified: trunk/coser-business/src/main/java/fr/ifremer/coser/bean/Project.java =================================================================== --- trunk/coser-business/src/main/java/fr/ifremer/coser/bean/Project.java 2010-11-26 09:36:00 UTC (rev 284) +++ trunk/coser-business/src/main/java/fr/ifremer/coser/bean/Project.java 2010-11-26 10:28:18 UTC (rev 285) @@ -75,6 +75,19 @@ /** Utilisé seulement lors de la création du projet (sinon, non valorisé). */ protected String strataFile; + /** Nom original du fichier de captures. */ + protected String catchFileName; + + /** Nom original du fichier de traits. */ + protected String haulFileName; + + /** Nom original du fichier de tailles. */ + protected String lengthFileName; + + /** Nom original du fichier de strates. */ + protected String strataFileName; + + /** Commentaire du projet. */ protected String comment; protected Control control; @@ -147,6 +160,38 @@ getPropertyChangeSupport().firePropertyChange("strataFile", oldValue, strataFile); } + public String getCatchFileName() { + return catchFileName; + } + + public void setCatchFileName(String catchFileName) { + this.catchFileName = catchFileName; + } + + public String getHaulFileName() { + return haulFileName; + } + + public void setHaulFileName(String haulFileName) { + this.haulFileName = haulFileName; + } + + public String getLengthFileName() { + return lengthFileName; + } + + public void setLengthFileName(String lengthFileName) { + this.lengthFileName = lengthFileName; + } + + public String getStrataFileName() { + return strataFileName; + } + + public void setStrataFileName(String strataFileName) { + this.strataFileName = strataFileName; + } + public String getComment() { return comment; } @@ -216,6 +261,18 @@ if (author != null) { props.setProperty("project.author", author); } + if (catchFileName != null) { + props.setProperty("project.catchFileName", catchFileName); + } + if (lengthFileName != null) { + props.setProperty("project.lengthFileName", lengthFileName); + } + if (haulFileName != null) { + props.setProperty("project.haulFileName", haulFileName); + } + if (strataFileName != null) { + props.setProperty("project.strataFileName", strataFileName); + } if (comment != null) { props.setProperty("project.comment", comment); } @@ -226,6 +283,18 @@ if (props.containsKey("project.author")) { setAuthor(props.getProperty("project.author")); } + if (props.containsKey("project.catchFileName")) { + setCatchFileName(props.getProperty("project.catchFileName")); + } + if (props.containsKey("project.lengthFileName")) { + setLengthFileName(props.getProperty("project.lengthFileName")); + } + if (props.containsKey("project.haulFileName")) { + setHaulFileName(props.getProperty("project.haulFileName")); + } + if (props.containsKey("project.strataFileName")) { + setStrataFileName(props.getProperty("project.strataFileName")); + } if (props.containsKey("control.comment")) { setComment(props.getProperty("control.comment")); } Modified: trunk/coser-business/src/main/java/fr/ifremer/coser/services/ProjectService.java =================================================================== --- trunk/coser-business/src/main/java/fr/ifremer/coser/services/ProjectService.java 2010-11-26 09:36:00 UTC (rev 284) +++ trunk/coser-business/src/main/java/fr/ifremer/coser/services/ProjectService.java 2010-11-26 10:28:18 UTC (rev 285) @@ -187,19 +187,19 @@ Category category = categoryAndFile.getKey(); File dataFile = categoryAndFile.getValue(); try { - + // les fichiers de donnees sont stockes dans un repertoire // "original" if (category.isDataCategory()) { - File storageDataFile = new File(originalDirectory, - category.getStorageFileName() + CoserConstants.STORAGE_CSV_EXTENSION); + String storageFileName = getDataStorageFileName(project, category, null); + File storageDataFile = new File(originalDirectory, storageFileName); FileUtils.copyFile(dataFile, storageDataFile); } // les fichiers autres (reftax) sont stockes a la base else { File storageDataFile = new File(projectDirectory, - category.getStorageFileName() + CoserConstants.STORAGE_CSV_EXTENSION); + category.getStorageFileName()); FileUtils.copyFile(dataFile, storageDataFile); } } catch (IOException ex) { @@ -240,6 +240,42 @@ } /** + * Retourne le nom de stockage d'un fichier de données suivant la categories + * de fichier demandé. Les fichiers sont stockés avec leurs noms originaux + * (sauf les fichiers de réference) donc il n'est pas fixe. + * + * @param project project (containing originals file names) + * @param category category to get file name + * @param suffix suffix to add into file name + * @return storage file name + */ + protected String getDataStorageFileName(Project project, Category category, String suffix) { + + String result = null; + + switch (category) { + case CATCH: + result = project.getCatchFileName(); + break; + case HAUL: + result = project.getHaulFileName(); + break; + case LENGTH: + result = project.getLengthFileName(); + break; + case STRATA: + result = project.getStrataFileName(); + break; + } + + if (suffix != null) { + result = CoserUtils.addSuffixBeforeExtension(result, suffix); + } + + return result; + } + + /** * Open project without loading data. * * Just load non category data (reftax). @@ -263,8 +299,7 @@ // load only additional files if (!category.isDataCategory()) { File inputFile = new File(projectDirectory, - category.getStorageFileName() + - CoserConstants.STORAGE_CSV_EXTENSION); + category.getStorageFileName()); if (inputFile.isFile()) { DataStorage dataStorage = importService.loadCSVFile(project, category, inputFile, false); @@ -432,8 +467,8 @@ // seulement les category de données ici if (category.isDataCategory()) { - File inputFile = new File(controlDirectory, category.getStorageFileName() + - CoserConstants.STORAGE_CONTROL_SUFFIX + CoserConstants.STORAGE_CSV_EXTENSION); + String storageFileName = getDataStorageFileName(project, category, CoserConstants.STORAGE_CONTROL_SUFFIX); + File inputFile = new File(controlDirectory, storageFileName); if (inputFile.isFile()) { DataStorage dataStorage = importService.loadCSVFile(project, category, inputFile, false); @@ -447,8 +482,8 @@ } // load deleted file if exists - inputFile = new File(controlDirectory, category.getStorageFileName() + - CoserConstants.STORAGE_DELECTED_SUFFIX + CoserConstants.STORAGE_CSV_EXTENSION); + storageFileName = getDataStorageFileName(project, category, CoserConstants.STORAGE_DELECTED_SUFFIX); + inputFile = new File(controlDirectory, storageFileName); DataStorage dataStorage = null; if (inputFile.isFile()) { dataStorage = importService.loadCSVFile(project, category, inputFile, false); @@ -469,8 +504,8 @@ for (Category category : Category.values()) { if (category.isDataCategory()) { - File storageDataFile = new File(originalDirectory, - category.getStorageFileName() + CoserConstants.STORAGE_CSV_EXTENSION); + String storageFileName = getDataStorageFileName(project, category, null); + File storageDataFile = new File(originalDirectory, storageFileName); // main data if (storageDataFile.isFile()) { @@ -545,8 +580,8 @@ // seulement les category de données ici if (category.isDataCategory()) { - File inputFile = new File(selectionDirectory, category.getStorageFileName() + - CoserConstants.STORAGE_SELECTION_SUFFIX + CoserConstants.STORAGE_CSV_EXTENSION); + String storageFileName = getDataStorageFileName(project, category, CoserConstants.STORAGE_SELECTION_SUFFIX); + File inputFile = new File(selectionDirectory, storageFileName); if (inputFile.isFile()) { DataStorage dataStorage = importService.loadCSVFile(project, category, inputFile, false); @@ -605,10 +640,8 @@ if (category.isDataCategory()) { // save main content - File controlFile = new File(controlDirectory, - category.getStorageFileName() + - CoserConstants.STORAGE_CONTROL_SUFFIX + - CoserConstants.STORAGE_CSV_EXTENSION); + String storageFileName = getDataStorageFileName(project, category, CoserConstants.STORAGE_CONTROL_SUFFIX); + File controlFile = new File(controlDirectory, storageFileName); if (log.isDebugEnabled()) { log.debug("Saving control file : " + controlFile); } @@ -619,10 +652,8 @@ DataStorage contentDeleted = getProjectContent(project, control, category, true); // if more content than header if (contentDeleted.size() > 1) { - File deletedDataFile = new File(controlDirectory, - category.getStorageFileName() + - CoserConstants.STORAGE_DELECTED_SUFFIX + - CoserConstants.STORAGE_CSV_EXTENSION); + String deletedFileName = getDataStorageFileName(project, category, CoserConstants.STORAGE_DELECTED_SUFFIX); + File deletedDataFile = new File(controlDirectory, deletedFileName); importService.storeData(project, contentDeleted, deletedDataFile); } } @@ -758,10 +789,8 @@ for (Category category : Category.values()) { if (category.isDataCategory()) { - File dataFile = new File(selectionDirectory, - category.getStorageFileName() + - CoserConstants.STORAGE_SELECTION_SUFFIX + - CoserConstants.STORAGE_CSV_EXTENSION); + String storageFileName = getDataStorageFileName(project, category, CoserConstants.STORAGE_SELECTION_SUFFIX); + File dataFile = new File(selectionDirectory, storageFileName); if (log.isDebugEnabled()) { log.debug("Saving selection file : " + dataFile); } Added: trunk/coser-business/src/test/java/fr/ifremer/coser/CoserUtilsTest.java =================================================================== --- trunk/coser-business/src/test/java/fr/ifremer/coser/CoserUtilsTest.java (rev 0) +++ trunk/coser-business/src/test/java/fr/ifremer/coser/CoserUtilsTest.java 2010-11-26 10:28:18 UTC (rev 285) @@ -0,0 +1,51 @@ +/* + * #%L + * + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2010 Ifremer, Codelutin, Chatellier Eric + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser 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 Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ + +package fr.ifremer.coser; + +import org.junit.Assert; +import org.junit.Test; + +/** + * Coser utils tests. + * + * @author chatellier + * @version $Revision$ + * + * Last update : $Date$ + * By : $Author$ + */ +public class CoserUtilsTest { + + /** + * Test l'ajout de suffix dans les noms de fichier. + */ + @Test + public void testAddSuffixBeforeExtension() { + Assert.assertEquals("captures_co.csv", CoserUtils.addSuffixBeforeExtension("captures.csv", CoserConstants.STORAGE_CONTROL_SUFFIX)); + Assert.assertEquals("cap.tu.res_se.csv", CoserUtils.addSuffixBeforeExtension("cap.tu.res.csv", CoserConstants.STORAGE_SELECTION_SUFFIX)); + Assert.assertEquals("captures_co", CoserUtils.addSuffixBeforeExtension("captures", CoserConstants.STORAGE_CONTROL_SUFFIX)); + } +} Property changes on: trunk/coser-business/src/test/java/fr/ifremer/coser/CoserUtilsTest.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Modified: trunk/coser-business/src/test/resources/log4j.properties =================================================================== --- trunk/coser-business/src/test/resources/log4j.properties 2010-11-26 09:36:00 UTC (rev 284) +++ trunk/coser-business/src/test/resources/log4j.properties 2010-11-26 10:28:18 UTC (rev 285) @@ -32,5 +32,5 @@ log4j.appender.stdout.layout.ConversionPattern=%d{HH:mm:ss} %-5p %c:%L - %m%n # Categories -log4j.category.fr.ifremer.coser=DEBUG +#log4j.category.fr.ifremer.coser=DEBUG #log4j.category.com.opensymphony.xwork2=DEBUG Modified: trunk/coser-ui/src/main/java/fr/ifremer/coser/ui/CoserFrameHandler.java =================================================================== --- trunk/coser-ui/src/main/java/fr/ifremer/coser/ui/CoserFrameHandler.java 2010-11-26 09:36:00 UTC (rev 284) +++ trunk/coser-ui/src/main/java/fr/ifremer/coser/ui/CoserFrameHandler.java 2010-11-26 10:28:18 UTC (rev 285) @@ -239,6 +239,12 @@ File reftaxSpeciesFile = new File(reftaxSpeciesPath); File codeTypeEspeceFile = new File(codeTypeEspecePath); + // remember original file names + project.setCatchFileName(capturesFile.getName()); + project.setLengthFileName(taillesFile.getName()); + project.setHaulFileName(traitsFile.getName()); + project.setStrataFileName(stratesFile.getName()); + // check some files existences if (!reftaxSpeciesFile.canRead()) { JOptionPane.showMessageDialog(projectView, _("coser.ui.project.createProjectMissingReftax"),
participants (1)
-
chatellier@users.labs.libre-entreprise.org