[Suiviobsmer-commits] r1092 - in trunk/wao-business/src/main: java/fr/ifremer/wao/io/csv2 java/fr/ifremer/wao/io/csv2/models java/fr/ifremer/wao/io/csv2/models/operations resources/i18n
Author: bleny Date: 2011-03-24 17:50:19 +0000 (Thu, 24 Mar 2011) New Revision: 1092 Log: i18n for import/export Modified: trunk/wao-business/src/main/java/fr/ifremer/wao/io/csv2/Common.java trunk/wao-business/src/main/java/fr/ifremer/wao/io/csv2/Import.java trunk/wao-business/src/main/java/fr/ifremer/wao/io/csv2/ModelBuilder.java trunk/wao-business/src/main/java/fr/ifremer/wao/io/csv2/models/ContactImportExportModel.java trunk/wao-business/src/main/java/fr/ifremer/wao/io/csv2/models/operations/UserParserFormatter.java trunk/wao-business/src/main/java/fr/ifremer/wao/io/csv2/models/operations/UsersParserFormatter.java trunk/wao-business/src/main/resources/i18n/wao-business_en_GB.properties trunk/wao-business/src/main/resources/i18n/wao-business_fr_FR.properties Modified: trunk/wao-business/src/main/java/fr/ifremer/wao/io/csv2/Common.java =================================================================== --- trunk/wao-business/src/main/java/fr/ifremer/wao/io/csv2/Common.java 2011-03-24 17:50:06 UTC (rev 1091) +++ trunk/wao-business/src/main/java/fr/ifremer/wao/io/csv2/Common.java 2011-03-24 17:50:19 UTC (rev 1092) @@ -120,12 +120,21 @@ @Override public String format(E value) { - return toStrings.get(value); + String valueAsString = toStrings.get(value); + if (valueAsString == null) { + throw new IllegalArgumentException(); + } + return valueAsString; } @Override - public E parse(String value) throws ParseException { - return fromString.get(value); + public E parse(String valueAsString) throws ParseException { + E value = fromString.get(valueAsString); + if (value == null) { + throw new IllegalArgumentException("Unaple to parse value '" + valueAsString + + "'. Possible values are " + fromString.keySet().toString()); + } + return value; } } Modified: trunk/wao-business/src/main/java/fr/ifremer/wao/io/csv2/Import.java =================================================================== --- trunk/wao-business/src/main/java/fr/ifremer/wao/io/csv2/Import.java 2011-03-24 17:50:06 UTC (rev 1091) +++ trunk/wao-business/src/main/java/fr/ifremer/wao/io/csv2/Import.java 2011-03-24 17:50:19 UTC (rev 1092) @@ -21,6 +21,7 @@ package fr.ifremer.wao.io.csv2; import com.csvreader.CsvReader; +import fr.ifremer.wao.WaoUtils; import fr.ifremer.wao.bean.ValidationResult; import fr.ifremer.wao.bean.ValidationResultImpl; import org.nuiton.util.StringUtil; @@ -35,9 +36,12 @@ import java.util.Collections; import java.util.HashSet; import java.util.Iterator; +import java.util.LinkedList; import java.util.List; import java.util.Set; +import static org.nuiton.i18n.I18n.n_; + public class Import<E> { private static final Logger log = LoggerFactory.getLogger(Import.class); @@ -125,10 +129,14 @@ if ( ! csvHeaders.isEmpty()) { csvValidationResult.setSuccess(false); - String validationMessage = String.format( - "Les champs %s ne sont pas reconnus. Les champs possibles sont %s", + List<String> validHeaderNames = new LinkedList<String>(); + for (ImportableColumn importableColumn : model.getColumnsForImport()) { + validHeaderNames.add(importableColumn.getHeaderName()); + } + String validationMessage = WaoUtils.translate( + n_("csv.import.error.unrecognizedHeaders"), StringUtil.join(csvHeaders, ", " ,true), - StringUtil.join(model.getColumnsForImport(), ", " ,true)); + StringUtil.join(validHeaderNames, ", " ,true)); csvValidationResult.setMessage(validationMessage); } @@ -143,8 +151,8 @@ if ( ! mandatoryHeadersNames.isEmpty()) { csvValidationResult.setSuccess(false); - String validationMessage = String.format( - "Les champs obligatoires %s sont manquants", + String validationMessage = WaoUtils.translate( + n_("csv.import.error.missingMandatoryHeaders"), StringUtil.join(mandatoryHeadersNames, ", " , true)); csvValidationResult.setMessage(validationMessage); } @@ -189,7 +197,7 @@ value = reader.get(field.getHeaderName()); } catch (IOException e) { reader.close(); - throw new RuntimeException("unable to read field " + field.getHeaderName() + " at line " + lineNumber, e); + throw new RuntimeException(WaoUtils.translate(n_("csv.import.error.unableToReadField"), field.getHeaderName(), lineNumber), e); } // contravariance ftw @@ -197,15 +205,14 @@ try { parsedValue = field.parseValue(value); } catch (Exception e) { - throw new RuntimeException("exception while parsing value '" + value + "' at column '" + - field.getHeaderName() + "' at line " + lineNumber, e); + throw new RuntimeException(WaoUtils.translate(n_("csv.import.error.unableToParseValue"), value, + field.getHeaderName(), lineNumber), e); } try { field.setValue(element, parsedValue); } catch (Exception e) { - throw new RuntimeException("exception while setting value '" + parsedValue + "' on '" + - element + "' at line " + lineNumber, e); + throw new RuntimeException(WaoUtils.translate(n_("csv.import.error.unableToSetValue"), parsedValue, element.toString(), lineNumber), e); } } @@ -215,7 +222,7 @@ hasNext = reader.readRecord(); } catch (IOException e) { reader.close(); - throw new RuntimeException("unable to read line " + lineNumber + 1, e); + throw new RuntimeException(WaoUtils.translate(n_("csv.import.error.unableToReadLine"), lineNumber + 1), e); } return lastElement; } @@ -238,7 +245,7 @@ } } catch (IOException e) { reader.close(); - throw new RuntimeException("error occurred while reading first line", e); + throw new RuntimeException(WaoUtils.translate(n_("csv.import.error.unableToReadLine"), 1), e); } } Modified: trunk/wao-business/src/main/java/fr/ifremer/wao/io/csv2/ModelBuilder.java =================================================================== --- trunk/wao-business/src/main/java/fr/ifremer/wao/io/csv2/ModelBuilder.java 2011-03-24 17:50:06 UTC (rev 1091) +++ trunk/wao-business/src/main/java/fr/ifremer/wao/io/csv2/ModelBuilder.java 2011-03-24 17:50:19 UTC (rev 1092) @@ -50,7 +50,6 @@ public <T> ExportableColumn<E, T> newColumnForExport(String headerName, ValueGetter<E, T> valueGetter, ValueFormatter<T> valueFormatter) { ExportableColumn<E, T> newColumn = new Column(headerName, valueGetter, valueFormatter); columnsForExport.add(newColumn); - columnsForExport.add(newColumn); return newColumn; } Modified: trunk/wao-business/src/main/java/fr/ifremer/wao/io/csv2/models/ContactImportExportModel.java =================================================================== --- trunk/wao-business/src/main/java/fr/ifremer/wao/io/csv2/models/ContactImportExportModel.java 2011-03-24 17:50:06 UTC (rev 1091) +++ trunk/wao-business/src/main/java/fr/ifremer/wao/io/csv2/models/ContactImportExportModel.java 2011-03-24 17:50:19 UTC (rev 1092) @@ -22,7 +22,6 @@ import fr.ifremer.wao.io.csv2.ValueParserFormatter; import fr.ifremer.wao.io.csv2.models.operations.UserParserFormatter; import fr.ifremer.wao.io.csv2.models.operations.UsersParserFormatter; -import org.apache.commons.collections.MapUtils; import org.apache.commons.lang.StringUtils; import java.text.ParseException; @@ -31,6 +30,8 @@ import java.util.List; import java.util.Map; +import static org.nuiton.i18n.I18n.n_; + public class ContactImportExportModel implements ImportModel<Contact>, ExportModel<Contact> { static class TerrestrialLocationParserFormatter implements ValueParserFormatter<TerrestrialLocation> { @@ -67,7 +68,7 @@ } } if (StringUtils.isEmpty(code)) { - throw new IllegalArgumentException("Il manque le code du lieu"); + throw new IllegalArgumentException(WaoUtils.translate(n_("wao.import.contact.failure.terrestrialLocationMissing"))); } LocationType locationType; try { @@ -76,12 +77,12 @@ throw new RuntimeException(e); } if (locationType == null) { - throw new IllegalArgumentException("Le code du lieu doit être renseigné"); + throw new IllegalArgumentException(WaoUtils.translate(n_("wao.import.contact.failure.locationTypeMissing"))); } TerrestrialLocation terrestrialLocation = indexedLocations.get(locationType).get(code); if (terrestrialLocation == null) { - throw new IllegalArgumentException("Il n'y a pas de lieu de type '" + locationType.toString() - + "' ayant pour code '" + code + "'"); + throw new IllegalArgumentException(WaoUtils.translate(n_("wao.import.contact.failure.wrongTerrestrialLocation"), + locationType.toString(), code)); } return terrestrialLocation; } @@ -104,11 +105,11 @@ public SampleRow parse(String sampleRowCode) throws ParseException { String trimmedCode = sampleRowCode.trim(); if (StringUtils.isEmpty(trimmedCode)) { - throw new IllegalArgumentException("Il manque le code de la ligne de plan associée"); + throw new IllegalArgumentException(WaoUtils.translate(n_("wao.import.contact.failure.sampleRowCodeMissing"))); } SampleRow sampleRow = indexedSampleRows.get(sampleRowCode); if (sampleRow == null) { - throw new IllegalArgumentException("Il n'y a pas de ligne du plan avec le code '" + sampleRowCode + "'"); + throw new IllegalArgumentException(WaoUtils.translate(n_("wao.import.contact.failure.wrongSampleRowCode"), sampleRowCode)); } return sampleRow; } @@ -131,13 +132,12 @@ public Boat parse(String value) throws ParseException { String trimmedValue = value.trim(); if (trimmedValue.isEmpty()) { - throw new IllegalArgumentException("Il faut préciser l'immatriculation du navire associé au contact"); + throw new IllegalArgumentException(WaoUtils.translate(n_("wao.import.contact.failure.boatMissing"))); } Integer key = Integer.valueOf(trimmedValue); Boat boat = indexedBoats.get(key); if (boat == null) { - throw new IllegalArgumentException("Il n'y a pas de navire avec l'immatriculation '" - + value + "'"); + throw new IllegalArgumentException(WaoUtils.translate(n_("wao.import.contact.failure.wrongBoat"), value)); } return boat; } @@ -199,17 +199,18 @@ modelBuilder.newColumnForImportExport("CONTACT_COMMENTAIRE_OBSERVATEUR", Contact.PROPERTY_COMMENT); modelBuilder.newColumnForImportExport("CONTACT_COMMENTAIRE_COORDINATEUR", Contact.PROPERTY_COMMENT_COORDINATOR); modelBuilder.newColumnForImportExport("CONTACT_COMMENTAIRE_PROGRAMME", Contact.PROPERTY_COMMENT_ADMIN); - modelBuilder.newColumnForImportExport("CONTACT_OBSERVATION_MAMMIFERE", Contact.PROPERTY_MAMMALS_OBSERVATION, Common.BOOLEAN); - modelBuilder.newColumnForImportExport("CONTACT_CAPTURE_ACCIDENTELLE", Contact.PROPERTY_MAMMALS_CAPTURE, Common.BOOLEAN); - modelBuilder.newColumnForImportExport("PLAN_CODE", Contact.PROPERTY_SAMPLE_ROW, new SampleRowParserFormatter(sampleRows)); modelBuilder.newColumnForImportExport("NAVIRE_IMMATRICULATION", Contact.PROPERTY_BOAT, new BoatParserFormatter(boats)); modelBuilder.newColumnForImportExport("CONTACT_VALIDATION_SOCIETE", Contact.PROPERTY_VALIDATION_COMPANY, Common.BOOLEAN); modelBuilder.newColumnForImportExport("CONTACT_VALIDATION_PROGRAMME", Contact.PROPERTY_VALIDATION_PROGRAM, Common.BOOLEAN); + modelBuilder.newColumnForImportExport("PLAN_CODE", Contact.PROPERTY_SAMPLE_ROW, new SampleRowParserFormatter(sampleRows)); + if (ObsProgram.OBSMER.equals(obsProgram)) { modelBuilder.newColumnForImportExport("CONTACT_QUALITE_DONNEE", "dataReliability", new Common.ToStringParserFormatter<DataReliability>(DataReliability.values())); + modelBuilder.newColumnForImportExport("CONTACT_OBSERVATION_MAMMIFERE", Contact.PROPERTY_MAMMALS_OBSERVATION, Common.BOOLEAN); + modelBuilder.newColumnForImportExport("CONTACT_CAPTURE_ACCIDENTELLE", Contact.PROPERTY_MAMMALS_CAPTURE, Common.BOOLEAN); } if (ObsProgram.OBSVENTE.equals(obsProgram)) { @@ -221,10 +222,10 @@ return contact.getTerrestrialLocation().getLocationType().toString(); } }); - modelBuilder.newColumnForImportExport("CONTACT_LIEU_CODE", Contact.PROPERTY_TERRESTRIAL_LOCATION, - new TerrestrialLocationParserFormatter(terrestrialLocations, locationTypeGetter)); + new TerrestrialLocationParserFormatter(terrestrialLocations, locationTypeGetter)); + modelBuilder.newIgnoredColumn("CONTACT_LIEU_DESCRIPTION"); modelBuilder.newColumnForExport("CONTACT_LIEU_DESCRIPTION", new ValueGetter<Contact, String>() { @Override public String get(Contact contact) throws Exception { Modified: trunk/wao-business/src/main/java/fr/ifremer/wao/io/csv2/models/operations/UserParserFormatter.java =================================================================== --- trunk/wao-business/src/main/java/fr/ifremer/wao/io/csv2/models/operations/UserParserFormatter.java 2011-03-24 17:50:06 UTC (rev 1091) +++ trunk/wao-business/src/main/java/fr/ifremer/wao/io/csv2/models/operations/UserParserFormatter.java 2011-03-24 17:50:19 UTC (rev 1092) @@ -9,6 +9,8 @@ import java.util.List; import java.util.Map; +import static org.nuiton.i18n.I18n.n_; + public class UserParserFormatter implements ValueParserFormatter<WaoUser> { /** all waoUsers in the database when import was started, indexed by logins */ @@ -28,7 +30,7 @@ public WaoUser parse(String login) throws ParseException { WaoUser user = indexedWaoUsers.get(login.trim()); if (user == null) { - throw new IllegalArgumentException("il n'y a pas d'utilisateur avec l'identifiant '" + login + "'"); + throw new IllegalArgumentException(WaoUtils.translate(n_("wao.import.failure.wrongUser"), login)); } return user; } Modified: trunk/wao-business/src/main/java/fr/ifremer/wao/io/csv2/models/operations/UsersParserFormatter.java =================================================================== --- trunk/wao-business/src/main/java/fr/ifremer/wao/io/csv2/models/operations/UsersParserFormatter.java 2011-03-24 17:50:06 UTC (rev 1091) +++ trunk/wao-business/src/main/java/fr/ifremer/wao/io/csv2/models/operations/UsersParserFormatter.java 2011-03-24 17:50:19 UTC (rev 1092) @@ -11,6 +11,8 @@ import java.util.Map; import java.util.Set; +import static org.nuiton.i18n.I18n.n_; + public class UsersParserFormatter implements ValueParserFormatter<List<WaoUser>> { protected static final String SEPARATOR = ","; @@ -37,7 +39,7 @@ String trimmedLogin = login.trim(); WaoUser user = indexedWaoUsers.get(trimmedLogin); if (user == null) { - throw new IllegalArgumentException("il n'y a pas d'utilisateur avec l'identifiant '" + login + "'"); + throw new IllegalArgumentException(WaoUtils.translate(n_("wao.import.failure.wrongUser"), login)); } users.add(user); } Modified: trunk/wao-business/src/main/resources/i18n/wao-business_en_GB.properties =================================================================== --- trunk/wao-business/src/main/resources/i18n/wao-business_en_GB.properties 2011-03-24 17:50:06 UTC (rev 1091) +++ trunk/wao-business/src/main/resources/i18n/wao-business_en_GB.properties 2011-03-24 17:50:19 UTC (rev 1092) @@ -38,6 +38,12 @@ UserRole.GUEST=Guest UserRole.OBSERVER=Observer UserRole.PROFESSIONAL=Professional +csv.import.error.missingMandatoryHeaders=The mandatory fields %s are missing +csv.import.error.unableToParseValue=Unable to parse value '%s' (column '%s', line %s) +csv.import.error.unableToReadField=Unable to read value of column '%s' at line %s +csv.import.error.unableToReadLine=Unable to read line %s +csv.import.error.unableToSetValue=Unable to set value '%s' (entity '%s', line %s) +csv.import.error.unrecognizedHeaders=Fields %s are not recognized. Accepted fields are %s. fr.ifremer.wao.entity.FishingGearDCF.=Not specified fr.ifremer.wao.entity.FishingGearDCF.DRB=Boat dredges fr.ifremer.wao.entity.FishingGearDCF.DRH=Hand dredges @@ -228,3 +234,11 @@ wao.error.serviceUser.getObservers= wao.error.serviceUser.getUserRolesByLogin= wao.error.serviceUser.getUsersByCompany= +wao.import.contact.failure.boatMissing=You need to precise the plate number of the boat associated to the contact +wao.import.contact.failure.locationTypeMissing=The type of the location must be filled +wao.import.contact.failure.sampleRowCodeMissing=The code of the sample row line is missing +wao.import.contact.failure.terrestrialLocationMissing=The code of the terrestrial location is missing +wao.import.contact.failure.wrongBoat=There is no boat with plate number '%s' +wao.import.contact.failure.wrongSampleRowCode=The is no sample row with code '%s' +wao.import.contact.failure.wrongTerrestrialLocation=There is no location of type '%s' with code '%s' +wao.import.failure.wrongUser=There is no user with login '%s' Modified: trunk/wao-business/src/main/resources/i18n/wao-business_fr_FR.properties =================================================================== --- trunk/wao-business/src/main/resources/i18n/wao-business_fr_FR.properties 2011-03-24 17:50:06 UTC (rev 1091) +++ trunk/wao-business/src/main/resources/i18n/wao-business_fr_FR.properties 2011-03-24 17:50:19 UTC (rev 1092) @@ -38,6 +38,12 @@ UserRole.GUEST=Invit\u00E9 UserRole.OBSERVER=Observateur UserRole.PROFESSIONAL=Professionnel +csv.import.error.missingMandatoryHeaders=Les champs obligatoires %s sont manquants +csv.import.error.unableToParseValue=Erreur lors de l'interpr\u00E9tation de la valeur '%s' (colonne '%s', ligne %s) +csv.import.error.unableToReadField=Impossible de lire la colonne '%s' \u00E0 la ligne %s +csv.import.error.unableToReadLine=Impossible de lire la ligne %s +csv.import.error.unableToSetValue=Impossible d'enregistrer la valeur '%s' (entit\u00E9 '%s', ligne %s) +csv.import.error.unrecognizedHeaders=Les champs %s ne sont pas reconnus. Les champs possibles sont %s. fr.ifremer.wao.entity.FishingGearDCF.=Non sp\u00E9cifi\u00E9 fr.ifremer.wao.entity.FishingGearDCF.DRB=Dragues remorqu\u00E9es par bateau fr.ifremer.wao.entity.FishingGearDCF.DRH=Dragues \u00E0 main @@ -228,3 +234,11 @@ wao.error.serviceUser.getObservers=Impossible de r\u00E9cup\u00E9rer la liste des observateurs wao.error.serviceUser.getUserRolesByLogin= wao.error.serviceUser.getUsersByCompany=Impossible de r\u00E9cup\u00E9rer la liste des utilisateurs de la soci\u00E9t\u00E9 %1$s +wao.import.contact.failure.boatMissing=Il faut pr\u00E9ciser l'immatriculation du navire associ\u00E9 au contact +wao.import.contact.failure.locationTypeMissing=Le type du lieu doit \u00EAtre renseign\u00E9 +wao.import.contact.failure.sampleRowCodeMissing=Il manque le code de la ligne de plan associ\u00E9e +wao.import.contact.failure.terrestrialLocationMissing=Il manque le code du lieu +wao.import.contact.failure.wrongBoat=Il n'y a pas de navire avec l'immatriculation '%s' +wao.import.contact.failure.wrongSampleRowCode=Il n'y a pas de ligne du plan avec le code '%s' +wao.import.contact.failure.wrongTerrestrialLocation=Il n'y a pas de lieu de type '%s' ayant pour code '%s' +wao.import.failure.wrongUser=Il n'y a pas d'utilisateur ayant pour identifiant '%s'
participants (1)
-
bleny@users.labs.libre-entreprise.org