r1102 - trunk/coser-business/src/main/java/fr/ifremer/coser/services
Author: echatellier Date: 2013-01-29 15:35:47 +0100 (Tue, 29 Jan 2013) New Revision: 1102 Url: http://forge.codelutin.com/projects/coser/repository/revisions/1102 Log: fixes #1928: G?\195?\169n?\195?\169ration d'un pdf par campagne Modified: trunk/coser-business/src/main/java/fr/ifremer/coser/services/WebService.java Modified: trunk/coser-business/src/main/java/fr/ifremer/coser/services/WebService.java =================================================================== --- trunk/coser-business/src/main/java/fr/ifremer/coser/services/WebService.java 2013-01-29 11:01:20 UTC (rev 1101) +++ trunk/coser-business/src/main/java/fr/ifremer/coser/services/WebService.java 2013-01-29 14:35:47 UTC (rev 1102) @@ -2533,18 +2533,17 @@ extractSource(zones, srcDir); } - // les cartes et des graphiques doivent se retroiver dans le pdf + // les cartes doivent se retrouver dans le pdf MultiKeyMap pdfMaps = null; if (types.contains(DataType.MAP)) { if (log.isDebugEnabled()) { log.debug("Extracting maps"); } - File mapDir = new File(subDir, "maps"); - pdfMaps = extractDataMap(zones, species, mapDir); + pdfMaps = extractDataMap(zones, species); } // les graphiques sont également dans le pdf - Map<String, Object[]> pdfCharts = null; + MultiKeyMap pdfCharts = null; if (CollectionUtils.isNotEmpty(comIndicators) || CollectionUtils.isNotEmpty(popIndicators)) { if (log.isDebugEnabled()) { log.debug("Extracting charts"); @@ -2554,8 +2553,7 @@ // generate pdf if necessary if (MapUtils.isNotEmpty(pdfMaps) || MapUtils.isNotEmpty(pdfCharts)) { - File pdfFile = new File(subDir, "Indicateurs_Ifremer.pdf"); - generateExtractPDF(pdfFile, pdfMaps, pdfCharts, locale); + generateExtractPDF(subDir, zones, pdfMaps, pdfCharts, locale); } // fichier de décharge en pdf @@ -2587,17 +2585,16 @@ } /** - * Extrait les cartes + * Extrait les cartes. * * @param zones zones (zoneid) * @param species species - * @param directory - * @return map file + * @return map file (zone/speciesname/mapfile) * @throws CoserBusinessException * @throws IOException * @since 1.4 */ - protected MultiKeyMap extractDataMap(Collection<String> zones, Collection<String> species, File directory) throws CoserBusinessException, IOException { + protected MultiKeyMap extractDataMap(Collection<String> zones, Collection<String> species) throws CoserBusinessException, IOException { MultiKeyMap mapForZoneAndSpecies = new MultiKeyMap(); @@ -2736,13 +2733,13 @@ * @param comIndicator comIndicator * @param popIndicators popIndicators * @param locale locale - * @return la liste des indicateurs + * @return la liste des indicateurs (zone, speciesname, [graphfile, graphdata]) * @throws CoserBusinessException */ - protected Map<String, Object[]> extractCharts(Collection<String> zones, Collection<String> species, + protected MultiKeyMap extractCharts(Collection<String> zones, Collection<String> species, Collection<String> comIndicators, Collection<String> popIndicators, Locale locale) throws CoserBusinessException { - Map<String, Object[]> chartFileAndDatas = new HashMap<String, Object[]>(); + MultiKeyMap chartFileAndDatas = new MultiKeyMap(); // parcours des resultats disponibles File projectsDirectory = config.getWebIndicatorsProjectsDirectory(); @@ -2775,7 +2772,8 @@ // load project (without data to get reftax data) Project project = projectService.openProject(projectFile.getName(), projectsDirectory); - String zoneDisplayName = getZoneFullName(rsufiResult.getZone()); + String zone = rsufiResult.getZone(); + String zoneDisplayName = getZoneFullName(zone); // le fichier contenant le code type espece (utile // pour les traductions des list d'indicateur) @@ -2785,15 +2783,20 @@ Map<String, Object[]> chartFileAndDataCom = publicationService.getRsufiResultComCharts(project, resultFile, rsufiResult, codeTypeEspecesFile, comIndicators, zoneDisplayName, getIndicatorsMap(), locale, 650, 430); - chartFileAndDatas.putAll(chartFileAndDataCom); + // put in multimap as zone,speciesname, data + for (Entry<String, Object[]> entry : chartFileAndDataCom.entrySet()) { + chartFileAndDatas.put(zone, entry.getKey(), entry.getValue()); + } } if (CollectionUtils.isNotEmpty(popIndicators)) { Map<String, Object[]> chartFileAndDataPop = publicationService.getRsufiResultPopCharts(project, resultFile, rsufiResult, species, popIndicators, zoneDisplayName, getIndicatorsMap(), locale, 650, 430); - - chartFileAndDatas.putAll(chartFileAndDataPop); + // put in multimap as zone,speciesname, data + for (Entry<String, Object[]> entry : chartFileAndDataPop.entrySet()) { + chartFileAndDatas.put(zone, entry.getKey(), entry.getValue()); + } } } } @@ -2812,99 +2815,91 @@ /** * Generate pdf file filled with maps and charts. * - * @param pdfFile pdf file to generate to + * @param directory directory to generate pdf to + * @param zones zones ids * @param pdfMaps pdf maps (can be {@code null}) * @param pdfCharts pdf charts (can be {@code null}) * @throws CoserBusinessException */ - protected void generateExtractPDF(File pdfFile, - MultiKeyMap pdfMaps, Map<String, Object[]> pdfCharts, Locale locale) throws CoserBusinessException { + protected void generateExtractPDF(File directory, List<String> zones, + MultiKeyMap pdfMaps, MultiKeyMap pdfCharts, Locale locale) throws CoserBusinessException { - Collection<File> toDelete = new ArrayList<File>(); - OutputStream os = null; + for (String zone : zones) { + Collection<File> toDelete = new ArrayList<File>(); + OutputStream os = null; - try { - StringBuilder htmlContent = new StringBuilder(); - htmlContent.append("<html><head>"); - htmlContent.append("<title>" + l_(locale, "coser.business.extract.extracttitle")+ "</title>"); - htmlContent.append("<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />"); - htmlContent.append("</head><body>"); - - if (pdfMaps != null) { - for (Entry<MultiKey, File> mapEntry : (Set<Entry<MultiKey, File>>)pdfMaps.entrySet()) { - String zoneId = (String)mapEntry.getKey().getKey(0); - String speciesName = (String)mapEntry.getKey().getKey(1); - File file = mapEntry.getValue(); - - String zoneName = getZoneFullName(zoneId); - htmlContent.append("<div style='page-break-after: always'>"); - htmlContent.append("<p>" + zoneName + " - " + speciesName + "</p>"); - htmlContent.append("<img src='file://" + file.getAbsolutePath() + "' />"); - htmlContent.append("</div>"); + try { + StringBuilder htmlContent = new StringBuilder(); + htmlContent.append("<html><head>"); + htmlContent.append("<title>" + l_(locale, "coser.business.extract.extracttitle")+ "</title>"); + htmlContent.append("<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />"); + htmlContent.append("</head><body>"); + + if (pdfMaps != null) { + for (Entry<MultiKey, File> mapEntry : (Set<Entry<MultiKey, File>>)pdfMaps.entrySet()) { + String zoneId = (String)mapEntry.getKey().getKey(0); + if (zoneId.equals(zone)) { + String speciesName = (String)mapEntry.getKey().getKey(1); + File file = mapEntry.getValue(); + + String zoneName = getZoneFullName(zoneId); + htmlContent.append("<div style='page-break-after: always'>"); + htmlContent.append("<p>" + zoneName + " - " + speciesName + "</p>"); + htmlContent.append("<img src='file://" + file.getAbsolutePath() + "' />"); + htmlContent.append("</div>"); + } + } } - } - - if (pdfCharts != null) { - for (Entry<String, Object[]> chartFileAndData : pdfCharts.entrySet()) { - //String key = chartFileAndData.getKey(); - File chartFile = (File)chartFileAndData.getValue()[0]; - String content = (String)chartFileAndData.getValue()[1]; - - htmlContent.append("<div style='page-break-after: always'>"); - htmlContent.append("<img src='file://" + chartFile.getAbsolutePath() + "' />"); - htmlContent.append("<br />"); - htmlContent.append(l_(locale, "coser.business.extract.extractdata") + " :"); - htmlContent.append("<pre>").append(content).append("</pre>"); - htmlContent.append("</div>"); - - // les graphiques ont été générés, a supprimer donc - // a ne surtout pas faire avec les cartes !!! - toDelete.add(chartFile); - } - } - htmlContent.append("</body></html>"); - - // get content as w3c document - Document document = CoserUtils.parseDocument(htmlContent.toString()); - - // render template output as pdf - os = new FileOutputStream(pdfFile); - - ITextRenderer renderer = new ITextRenderer(); - /* plus rapide, mais ne fonctionne pas, les images n'apparaissent pas - renderer.getSharedContext().setUserAgentCallback(new ITextUserAgent(renderer.getOutputDevice()) { - @Override - protected InputStream resolveAndOpenStream(String uri) { - String localuri = StringUtils.removeStart(uri, "file://"); - Object[] chartData = chartFileAndDatas.get(localuri); - InputStream is; - if (chartData != null) { - byte[] os = (byte[])chartData[0]; - is = new ByteArrayInputStream(os); - } else { - is = super.resolveAndOpenStream(uri); + if (pdfCharts != null) { + for (Entry<MultiKey, Object[]> chartFileAndData : (Set<Entry<MultiKey, Object[]>>)pdfCharts.entrySet()) { + String zoneId = (String)chartFileAndData.getKey().getKey(0); + if (zoneId.equals(zone)) { + File chartFile = (File)chartFileAndData.getValue()[0]; + String content = (String)chartFileAndData.getValue()[1]; + + htmlContent.append("<div style='page-break-after: always'>"); + htmlContent.append("<img src='file://" + chartFile.getAbsolutePath() + "' />"); + htmlContent.append("<br />"); + htmlContent.append(l_(locale, "coser.business.extract.extractdata") + " :"); + htmlContent.append("<pre>").append(content).append("</pre>"); + htmlContent.append("</div>"); + + // les graphiques ont été générés, a supprimer donc + // a ne surtout pas faire avec les cartes !!! + toDelete.add(chartFile); + } } - return is; } - });*/ - renderer.setDocument(document, null); - renderer.layout(); - renderer.createPDF(os); + + htmlContent.append("</body></html>"); + + // get content as w3c document + Document document = CoserUtils.parseDocument(htmlContent.toString()); + + // render template output as pdf + String zoneDisplay = getZoneFullName(zone); + File pdfFile = new File(directory, zoneDisplay + ".pdf"); + os = new FileOutputStream(pdfFile); + + ITextRenderer renderer = new ITextRenderer(); + renderer.setDocument(document, null); + renderer.layout(); + renderer.createPDF(os); + + os.close(); + } catch (IOException ex) { + throw new CoserBusinessException("Can't generate log pdf", ex); + } catch (DocumentException ex) { + throw new CoserBusinessException("Can't generate log pdf", ex); + } finally { + IOUtils.closeQuietly(os); - os.close(); - } catch (IOException ex) { - throw new CoserBusinessException("Can't generate log pdf", ex); - } catch (DocumentException ex) { - throw new CoserBusinessException("Can't generate log pdf", ex); - } finally { - IOUtils.closeQuietly(os); - - // delete file collection - for (File file : toDelete) { - file.delete(); + // delete file collection + for (File file : toDelete) { + file.delete(); + } } } - } }
participants (1)
-
echatellier@users.forge.codelutin.com