r2950 - in branches/pollen-1.2.5-1.2.x/pollen-ui/src: main/java/org/chorem/pollen/ui/pages/user main/java/org/chorem/pollen/ui/utils main/resources/org/chorem/pollen/ui/pages/user test/java/org/chorem/pollen/ui/utils
Author: echatellier Date: 2010-03-19 11:17:45 +0100 (Fri, 19 Mar 2010) New Revision: 2950 Log: #147 : CSV import fail on content type check Added: branches/pollen-1.2.5-1.2.x/pollen-ui/src/main/java/org/chorem/pollen/ui/utils/CSVParseException.java Modified: branches/pollen-1.2.5-1.2.x/pollen-ui/src/main/java/org/chorem/pollen/ui/pages/user/UserLists.java branches/pollen-1.2.5-1.2.x/pollen-ui/src/main/java/org/chorem/pollen/ui/utils/CSVAccountUtil.java branches/pollen-1.2.5-1.2.x/pollen-ui/src/main/resources/org/chorem/pollen/ui/pages/user/UserLists_en.properties branches/pollen-1.2.5-1.2.x/pollen-ui/src/main/resources/org/chorem/pollen/ui/pages/user/UserLists_fr.properties branches/pollen-1.2.5-1.2.x/pollen-ui/src/test/java/org/chorem/pollen/ui/utils/CSVAccountUtilTest.java Modified: branches/pollen-1.2.5-1.2.x/pollen-ui/src/main/java/org/chorem/pollen/ui/pages/user/UserLists.java =================================================================== --- branches/pollen-1.2.5-1.2.x/pollen-ui/src/main/java/org/chorem/pollen/ui/pages/user/UserLists.java 2010-03-16 10:22:18 UTC (rev 2949) +++ branches/pollen-1.2.5-1.2.x/pollen-ui/src/main/java/org/chorem/pollen/ui/pages/user/UserLists.java 2010-03-19 10:17:45 UTC (rev 2950) @@ -46,7 +46,9 @@ import org.chorem.pollen.ui.data.EvenOdd; import org.chorem.pollen.ui.data.Lien; import org.chorem.pollen.ui.utils.CSVAccountUtil; +import org.chorem.pollen.ui.utils.CSVParseException; import org.chorem.pollen.ui.utils.LDAPAccountUtil; +import org.slf4j.Logger; /** * Classe de la page d'affichage des listes de votants favorites de @@ -59,6 +61,9 @@ @IncludeStylesheet("context:css/lists.css") public class UserLists { + @Inject + private Logger log; + /** * Objet de session représentant l'utilisateur identifié. */ @@ -196,19 +201,36 @@ // Import CVS des comptes if (accountsFile != null) { - if (!accountsFile.getContentType().equals("text/csv")) { + /* + Ce test n'est pas sur, c'est à l'appreciation du client, par + exemple, chromium envoi 'application/octet-stream' + + if (!"text/csv".equals(accountsFile.getContentType())) { createListForm.recordError(messages.format("invalidCsv", accountsFile.getFileName())); + if (log.isWarnEnabled()) { + log.warn("Uploaded csv file content type : " + accountsFile.getContentType()); + } return this; + }*/ + try { + List<PollAccountDTO> accounts = CSVAccountUtil + .importList(accountsFile); + if (accounts.size() == 0) { + createListForm.recordError(messages.format("noAccountCsv", + accountsFile.getFileName())); + return this; + } + newList.getPollAccountDTOs().addAll(accounts); } - List<PollAccountDTO> accounts = CSVAccountUtil - .importList(accountsFile); - if (accounts.size() == 0) { - createListForm.recordError(messages.format("noAccountCsv", + catch (CSVParseException ex) { + if (log.isErrorEnabled()) { + log.error("Can't import uploaded file", ex); + } + createListForm.recordError(messages.format("invalidCsv", accountsFile.getFileName())); return this; } - newList.getPollAccountDTOs().addAll(accounts); } // Import LDAP des comptes Modified: branches/pollen-1.2.5-1.2.x/pollen-ui/src/main/java/org/chorem/pollen/ui/utils/CSVAccountUtil.java =================================================================== --- branches/pollen-1.2.5-1.2.x/pollen-ui/src/main/java/org/chorem/pollen/ui/utils/CSVAccountUtil.java 2010-03-16 10:22:18 UTC (rev 2949) +++ branches/pollen-1.2.5-1.2.x/pollen-ui/src/main/java/org/chorem/pollen/ui/utils/CSVAccountUtil.java 2010-03-19 10:17:45 UTC (rev 2950) @@ -18,9 +18,11 @@ import java.io.InputStreamReader; import java.io.Reader; +import java.text.ParseException; import java.util.Iterator; import java.util.List; +import org.apache.commons.io.IOUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.tapestry5.upload.services.UploadedFile; @@ -47,33 +49,39 @@ * * @param reader which contains the input stream * @return the new PollAccountDTO list from results imported + * + * @throws CSVParseException if file can't be read (not csv file) */ - public static List<PollAccountDTO> importList(Reader reader) { + public static List<PollAccountDTO> importList(Reader reader) throws CSVParseException { List<PollAccountDTO> accounts = null; - // Définition de la stratégie de mapping - ColumnPositionMappingStrategy<PollAccountDTO> strat = - new ColumnPositionMappingStrategy<PollAccountDTO>(); - String[] columns = new String[] { "votingId", "email", "weight" }; - strat.setType(PollAccountDTO.class); - strat.setColumnMapping(columns); - - // Parsing du fichier CSV - CsvToBean<PollAccountDTO> csv = new CsvToBean<PollAccountDTO>(); - accounts = csv.parse(strat, reader); - - // Suppression des comptes null - Iterator<PollAccountDTO> it = accounts.iterator(); - while (it.hasNext()) { - if ("".equals(it.next().getVotingId())) { - it.remove(); + try { + // Définition de la stratégie de mapping + ColumnPositionMappingStrategy<PollAccountDTO> strat = + new ColumnPositionMappingStrategy<PollAccountDTO>(); + String[] columns = new String[] { "votingId", "email", "weight" }; + strat.setType(PollAccountDTO.class); + strat.setColumnMapping(columns); + + // Parsing du fichier CSV + CsvToBean<PollAccountDTO> csv = new CsvToBean<PollAccountDTO>(); + accounts = csv.parse(strat, reader); + + // Suppression des comptes null + Iterator<PollAccountDTO> it = accounts.iterator(); + while (it.hasNext()) { + if ("".equals(it.next().getVotingId())) { + it.remove(); + } } + + if (log.isInfoEnabled()) { + log.info(accounts.size() + " comptes importés."); + } + } catch (Exception ex) { + throw new CSVParseException("Error during csv import", ex); } - if (log.isInfoEnabled()) { - log.info(accounts.size() + " comptes importés."); - } - return accounts; } @@ -83,14 +91,25 @@ * @param file le fichier CSV * @return a PollAccountDTO list * @see #importList(java.io.Reader) + * + * @throws CSVParseException if file can't be read (not csv file) */ - public static List<PollAccountDTO> importList(UploadedFile file) { + public static List<PollAccountDTO> importList(UploadedFile file) throws CSVParseException { if (log.isInfoEnabled()) { log.info("Import du fichier " + file.getFileName() + " (" + file.getContentType() + ")..."); } - InputStreamReader reader = new InputStreamReader(file.getStream()); - return importList(reader); + // make sure that reader is closed after parse + List<PollAccountDTO> result = null; + InputStreamReader reader = null; + try { + reader = new InputStreamReader(file.getStream()); + result = importList(reader); + } + finally { + IOUtils.closeQuietly(reader); + } + return result; } } Added: branches/pollen-1.2.5-1.2.x/pollen-ui/src/main/java/org/chorem/pollen/ui/utils/CSVParseException.java =================================================================== --- branches/pollen-1.2.5-1.2.x/pollen-ui/src/main/java/org/chorem/pollen/ui/utils/CSVParseException.java (rev 0) +++ branches/pollen-1.2.5-1.2.x/pollen-ui/src/main/java/org/chorem/pollen/ui/utils/CSVParseException.java 2010-03-19 10:17:45 UTC (rev 2950) @@ -0,0 +1,43 @@ +/* *##% Pollen + * Copyright (C) 2010 CodeLutin + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU 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 General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. ##%*/ + +package org.chorem.pollen.ui.utils; + +/** + * CVS excpetion thrown during bad file parsing. + * + * @author chatellier + * @version $Revision$ + * + * Last update : $Date$ + * By : $Author$ + */ +public class CSVParseException extends Exception { + + /** serialVersionUID. */ + private static final long serialVersionUID = 1717187553275691080L; + + /** + * Constructor with message and cause. + * + * @param message message + * @param cause cause + */ + public CSVParseException(String message, Throwable cause) { + super(message, cause); + } + +} Property changes on: branches/pollen-1.2.5-1.2.x/pollen-ui/src/main/java/org/chorem/pollen/ui/utils/CSVParseException.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Modified: branches/pollen-1.2.5-1.2.x/pollen-ui/src/main/resources/org/chorem/pollen/ui/pages/user/UserLists_en.properties =================================================================== --- branches/pollen-1.2.5-1.2.x/pollen-ui/src/main/resources/org/chorem/pollen/ui/pages/user/UserLists_en.properties 2010-03-16 10:22:18 UTC (rev 2949) +++ branches/pollen-1.2.5-1.2.x/pollen-ui/src/main/resources/org/chorem/pollen/ui/pages/user/UserLists_en.properties 2010-03-19 10:17:45 UTC (rev 2950) @@ -1,7 +1,7 @@ title=Your voting lists accountExists=%s already exists in this list. listExists=%s list already exists. -invalidCsv=%s is not a CSV file. +invalidCsv=%s is not a valid CSV file ! noAccountCsv=%s file contains no valid entry. noAccountLdap=%s URL can not obtain valid entries. noUser=You must be logged to access to your polls.\n Please fill the form below. Modified: branches/pollen-1.2.5-1.2.x/pollen-ui/src/main/resources/org/chorem/pollen/ui/pages/user/UserLists_fr.properties =================================================================== --- branches/pollen-1.2.5-1.2.x/pollen-ui/src/main/resources/org/chorem/pollen/ui/pages/user/UserLists_fr.properties 2010-03-16 10:22:18 UTC (rev 2949) +++ branches/pollen-1.2.5-1.2.x/pollen-ui/src/main/resources/org/chorem/pollen/ui/pages/user/UserLists_fr.properties 2010-03-19 10:17:45 UTC (rev 2950) @@ -1,7 +1,7 @@ title=Vos listes de votants accountExists=L'identifiant de vote %s existe d\u00E9j\u00E0 dans cette liste. listExists=La liste %s existe d\u00E9j\u00E0. -invalidCsv=Le fichier %s n'est pas un fichier CSV. +invalidCsv=Le fichier %s n'est pas un fichier CSV valide ! noAccountCsv=Le fichier %s ne contient aucune entr\u00E9e valide. noAccountLdap=L'URL %s ne permet pas d'obtenir d'entr\u00E9es valides. noUser=Vous devez \u00EAtre identifi\u00E9 pour pouvoir acc\u00E9der \u00E0 vos listes de votants.\n Veuillez remplir le formulaire ci-dessous. Modified: branches/pollen-1.2.5-1.2.x/pollen-ui/src/test/java/org/chorem/pollen/ui/utils/CSVAccountUtilTest.java =================================================================== --- branches/pollen-1.2.5-1.2.x/pollen-ui/src/test/java/org/chorem/pollen/ui/utils/CSVAccountUtilTest.java 2010-03-16 10:22:18 UTC (rev 2949) +++ branches/pollen-1.2.5-1.2.x/pollen-ui/src/test/java/org/chorem/pollen/ui/utils/CSVAccountUtilTest.java 2010-03-19 10:17:45 UTC (rev 2950) @@ -39,9 +39,10 @@ /** * Test of importList method, of class CSVAccountUtil. + * @throws CSVParseException */ @Test - public void testImportList_Reader() { + public void testImportList_Reader() throws CSVParseException { System.out.println("importList"); InputStream input = getClass().getResourceAsStream("/import.csv");
participants (1)
-
echatellier@users.chorem.org