Author: chatellier Date: 2010-12-15 17:10:28 +0000 (Wed, 15 Dec 2010) New Revision: 420 Log: Affichage des donn?\195?\169es d'origine dans les rapports Modified: trunk/coser-business/src/main/java/fr/ifremer/coser/command/ModifyFieldCommand.java trunk/coser-business/src/main/java/fr/ifremer/coser/services/ImportService.java trunk/coser-business/src/main/java/fr/ifremer/coser/services/ProjectService.java trunk/coser-business/src/main/java/fr/ifremer/coser/services/PublicationService.java trunk/coser-business/src/test/java/fr/ifremer/coser/services/PublicationServiceTest.java Modified: trunk/coser-business/src/main/java/fr/ifremer/coser/command/ModifyFieldCommand.java =================================================================== --- trunk/coser-business/src/main/java/fr/ifremer/coser/command/ModifyFieldCommand.java 2010-12-15 16:18:22 UTC (rev 419) +++ trunk/coser-business/src/main/java/fr/ifremer/coser/command/ModifyFieldCommand.java 2010-12-15 17:10:28 UTC (rev 420) @@ -265,7 +265,8 @@ int index = ArrayUtils.indexOf(headers, fieldName); // 0 = header - String realFieldName = dataStorage.get(0)[index]; + // +1 = les données contiennent le champs 'line' en plus + String realFieldName = dataStorage.get(0)[index + 1]; return realFieldName; } } Modified: trunk/coser-business/src/main/java/fr/ifremer/coser/services/ImportService.java =================================================================== --- trunk/coser-business/src/main/java/fr/ifremer/coser/services/ImportService.java 2010-12-15 16:18:22 UTC (rev 419) +++ trunk/coser-business/src/main/java/fr/ifremer/coser/services/ImportService.java 2010-12-15 17:10:28 UTC (rev 420) @@ -38,7 +38,10 @@ import java.io.Reader; import java.io.Writer; import java.util.Arrays; +import java.util.Collection; +import java.util.HashMap; import java.util.Iterator; +import java.util.Map; import org.apache.commons.io.IOUtils; import org.apache.commons.lang.StringUtils; @@ -121,11 +124,10 @@ DataStorage content = new MemoryDataStorage(); - Reader reader = null; CSVReader csvReader = null; try { - reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), CoserConstants.CSV_FILE_ENCODING)); + Reader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), CoserConstants.CSV_FILE_ENCODING)); if (progress != null) { reader = new ProgressReader(reader, progress); } @@ -165,7 +167,6 @@ } finally { IOUtils.closeQuietly(csvReader); - IOUtils.closeQuietly(reader); } return content; @@ -267,10 +268,9 @@ public void storeData(Project project, DataStorage content, File file) throws CoserBusinessException { // save content - Writer writer = null; CSVWriter csvWriter = null; try { - writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), CoserConstants.CSV_FILE_ENCODING)); + Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), CoserConstants.CSV_FILE_ENCODING)); csvWriter = new CSVWriter(writer, CoserConstants.CSV_SEPARATOR_CHAR); Iterator<String[]> itContent = content.iterator(); @@ -282,7 +282,47 @@ } finally { IOUtils.closeQuietly(csvWriter); - IOUtils.closeQuietly(writer); } } + + /** + * Lit le fichier demandé et sauve dans une map les lignes demandées. + * + * @param file file to read + * @param lines lines to save content + * @return saved content + * @throws CoserBusinessException + */ + public Map<String, String[]> getOriginalFileContent(File file, Collection<String> lines) throws CoserBusinessException { + Map<String, String[]> content = new HashMap<String, String[]>(); + + CSVReader csvReader = null; + try { + + Reader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), CoserConstants.CSV_FILE_ENCODING)); + csvReader = new CSVReader(reader, CoserConstants.CSV_SEPARATOR_CHAR); + + // skip header + String[] line = csvReader.readNext(); + + // pour alimenter la line index (si non reloading) + // dans coser, la numérotation des lignes commence à 1 + int lineIndex = 1; + while ((line = csvReader.readNext()) != null) { + String stringLine = String.valueOf(lineIndex); + if (lines.contains(stringLine)) { + content.put(stringLine, line); + } + lineIndex++; + } + } + catch (IOException ex) { + throw new CoserBusinessException("Can't read file", ex); + } + finally { + IOUtils.closeQuietly(csvReader); + } + + return content; + } } 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-12-15 16:18:22 UTC (rev 419) +++ trunk/coser-business/src/main/java/fr/ifremer/coser/services/ProjectService.java 2010-12-15 17:10:28 UTC (rev 420) @@ -1409,7 +1409,7 @@ break; case HAUL: currentBeanData = new Haul(); - newBeanData = new Catch(); + newBeanData = new Haul(); lineIndex = control.getHaul().indexOf(lineNumber); currentBeanData.setData(control.getHaul().get(lineIndex)); break; @@ -2526,4 +2526,35 @@ IOUtils.closeQuietly(out); } } + + /** + * Lit tout le fichier associé à la categorie demandé, mais ne retourne + * que les données correspondant au numéro de lignes demandée. + * + * Le contenu retourné ne contient pas le numéro de ligne. + * + * @param project project + * @param category category + * @param lines lines to get content + * @return content for lines + * @throws CoserBusinessException + */ + public Map<String, String[]> getOriginalContent(Project project, Category category, Collection<String> lines) throws CoserBusinessException { + Map<String, String[]> contents = null; + + // ne lit pas un fichier entier pour ne rien chercher + if (CollectionUtils.isEmpty(lines)) { + return new HashMap<String, String[]>(); + } + + File projectsDirectory = config.getProjectsDirectory(); + File projectDirectory = new File(projectsDirectory, project.getName()); + File originalDirectory = new File(projectDirectory, CoserConstants.STORAGE_ORIGINAL_DIRECTORY); + String storageFileName = getDataStorageFileName(project, category, null); + File dataFile = new File(originalDirectory, storageFileName); + + contents = importService.getOriginalFileContent(dataFile, lines); + + return contents; + } } Modified: trunk/coser-business/src/main/java/fr/ifremer/coser/services/PublicationService.java =================================================================== --- trunk/coser-business/src/main/java/fr/ifremer/coser/services/PublicationService.java 2010-12-15 16:18:22 UTC (rev 419) +++ trunk/coser-business/src/main/java/fr/ifremer/coser/services/PublicationService.java 2010-12-15 17:10:28 UTC (rev 420) @@ -35,6 +35,7 @@ import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; +import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Map; @@ -43,6 +44,7 @@ import org.apache.commons.collections.CollectionUtils; import org.apache.commons.io.IOUtils; +import org.apache.commons.lang.ArrayUtils; import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -68,6 +70,7 @@ import fr.ifremer.coser.bean.Control; import fr.ifremer.coser.bean.Project; import fr.ifremer.coser.bean.Selection; +import fr.ifremer.coser.command.CategoryLineCommand; import fr.ifremer.coser.command.Command; import fr.ifremer.coser.control.ControlError; import fr.ifremer.coser.control.ControlErrorGroup; @@ -90,8 +93,11 @@ protected CoserBusinessConfig config; + protected ProjectService projectService; + public PublicationService(CoserBusinessConfig config) { this.config = config; + projectService = new ProjectService(config); } /** @@ -422,13 +428,13 @@ * Extrait les logs des modifications faites sur un control au format html. * * @param project project - * @param container data container + * @param control data container * @return extractedFile * @throws CoserBusinessException * * @see AbstractDataContainer#getHistoryCommand() */ - public File extractControlLogAsHTML(Project project, Control container) throws CoserBusinessException { + public File extractControlLogAsHTML(Project project, Control control) throws CoserBusinessException { File exportHtmlFile = null; PrintStream out = null; try { @@ -448,12 +454,8 @@ out.println("<h1 style='text-align:center'>" + _("coser.business.publication.controllogexporttitle", project.getName()) + "</h1>"); // partie specific : commandes - out.println("<h2>" + _("coser.business.publication.datamodification") + "</h2>"); - out.println("<ol>"); - for (Command command : container.getHistoryCommand()) { - out.println("<li>" + command.getLogString(container) + "</li>"); - } - out.println("</ol>"); + extractHistoryCommandReport(project, control, out); + out.println("</body></html>"); } catch (IOException ex) { @@ -497,18 +499,14 @@ // partie resumé de selection out.println("<h2>" + _("coser.business.publication.selectionchoices") + "</h2>"); out.println("<ul>"); - out.println("<li>" + _("coser.business.common.years") + " : " + StringUtils.join(selection.getSelectedYears(), " ,") + "</li>"); - out.println("<li>" + _("coser.business.common.strata") + " : " + StringUtils.join(selection.getSelectedStrata(), " ,") + "</li>"); - out.println("<li>" + _("coser.business.common.species") + " : " + StringUtils.join(selection.getSelectedSpecies(), " ,") + "</li>"); + out.println("<li>" + _("coser.business.common.years") + " : " + StringUtils.join(selection.getSelectedYears(), ", ") + "</li>"); + out.println("<li>" + _("coser.business.common.strata") + " : " + StringUtils.join(selection.getSelectedStrata(), ", ") + "</li>"); + out.println("<li>" + _("coser.business.common.species") + " : " + StringUtils.join(selection.getSelectedSpecies(), ", ") + "</li>"); out.println("</ul>"); // partie specific : commandes - out.println("<h2>" + _("coser.business.publication.datamodification") + "</h2>"); - out.println("<ol>"); - for (Command command : selection.getHistoryCommand()) { - out.println("<li>" + command.getLogString(selection) + "</li>"); - } - out.println("</ol>"); + extractHistoryCommandReport(project, selection, out); + out.println("</body></html>"); } catch (IOException ex) { @@ -519,4 +517,90 @@ } return exportHtmlFile; } + + /** + * Partie commune aux export qui effectue en 2 passe la recuperation + * des lignes d'erreur, la recuperation des données correspondant à + * ces lignes dans les fichiers originaux et la sortie des erreurs. + * + * @param project project + * @param container data container + * @param out output stream + * @throws CoserBusinessException + */ + protected void extractHistoryCommandReport(Project project, AbstractDataContainer container, PrintStream out) throws CoserBusinessException { + + // first get lines index to get content + Set<String> catchLines = new HashSet<String>(); + Set<String> lengthLines = new HashSet<String>(); + Set<String> haulLines = new HashSet<String>(); + Set<String> strataLines = new HashSet<String>(); + + for (Command command : container.getHistoryCommand()) { + Category category = null; + String line = null; + if (command instanceof CategoryLineCommand) { + category = ((CategoryLineCommand)command).getCategory(); + line = ((CategoryLineCommand)command).getLineNumber(); + } + if (category != null && line != null) { + switch (category) { + case CATCH: + catchLines.add(line); + break; + case LENGTH: + lengthLines.add(line); + break; + case HAUL: + haulLines.add(line); + break; + case STRATA: + strataLines.add(line); + break; + } + } + } + + // second get content for lines + Map<String, String[]> catchContent = projectService.getOriginalContent(project, Category.CATCH, catchLines); + Map<String, String[]> lengthContent = projectService.getOriginalContent(project, Category.LENGTH, lengthLines); + Map<String, String[]> haulContent = projectService.getOriginalContent(project, Category.HAUL, haulLines); + Map<String, String[]> strataContent = projectService.getOriginalContent(project, Category.STRATA, strataLines); + + // third, generate html report + out.println("<h2>" + _("coser.business.publication.datamodification") + "</h2>"); + out.println("<ol>"); + for (Command command : container.getHistoryCommand()) { + Category category = null; + String line = null; + if (command instanceof CategoryLineCommand) { + category = ((CategoryLineCommand)command).getCategory(); + line = ((CategoryLineCommand)command).getLineNumber(); + } + if (category != null && line != null) { + String[] data = null; + switch (category) { + case CATCH: + data = catchContent.get(line); + break; + case LENGTH: + data = lengthContent.get(line); + break; + case HAUL: + data = haulContent.get(line); + break; + case STRATA: + data = strataContent.get(line); + break; + } + out.println("<li>" + command.getLogString(container) + "<br/>"); + out.println("<tt style='color:#5F5A59'>" + StringUtils.join(data, ", ") + "</tt><br /></li>"); + } + else { + out.println("<li>" + command.getLogString(container) + "</li>"); + } + } + out.println("</ol>"); + } + } Modified: trunk/coser-business/src/test/java/fr/ifremer/coser/services/PublicationServiceTest.java =================================================================== --- trunk/coser-business/src/test/java/fr/ifremer/coser/services/PublicationServiceTest.java 2010-12-15 16:18:22 UTC (rev 419) +++ trunk/coser-business/src/test/java/fr/ifremer/coser/services/PublicationServiceTest.java 2010-12-15 17:10:28 UTC (rev 420) @@ -177,7 +177,7 @@ Assert.assertTrue(fileContent.indexOf("from \"251.86\" to \"392.98\"") > 0); // clean all - //htmlExport.delete(); + htmlExport.delete(); } /**
participants (1)
-
chatellier@users.labs.libre-entreprise.org