r458 - in trunk: coser-business/src/main/java/fr/ifremer/coser coser-business/src/main/java/fr/ifremer/coser/services coser-web/src/main/java/fr/ifremer/coser/web/actions coser-web/src/main/webapp/WEB-INF/content
Author: chatellier Date: 2011-01-03 16:19:58 +0000 (Mon, 03 Jan 2011) New Revision: 458 Log: Fix des indicateurs en fonction des especes Modified: trunk/coser-business/src/main/java/fr/ifremer/coser/CoserConstants.java trunk/coser-business/src/main/java/fr/ifremer/coser/services/ImportService.java trunk/coser-business/src/main/java/fr/ifremer/coser/services/WebService.java trunk/coser-web/src/main/java/fr/ifremer/coser/web/actions/SelectIndicatorAction.java trunk/coser-web/src/main/webapp/WEB-INF/content/select-indicator.jsp trunk/coser-web/src/main/webapp/WEB-INF/content/select-species.jsp Modified: trunk/coser-business/src/main/java/fr/ifremer/coser/CoserConstants.java =================================================================== --- trunk/coser-business/src/main/java/fr/ifremer/coser/CoserConstants.java 2011-01-03 14:14:02 UTC (rev 457) +++ trunk/coser-business/src/main/java/fr/ifremer/coser/CoserConstants.java 2011-01-03 16:19:58 UTC (rev 458) @@ -41,6 +41,9 @@ /** CSV Separator. */ public static final char CSV_SEPARATOR_CHAR = ';'; + /** CSV Separator for result file (estcomind/estpopind). */ + public static final char CSV_RESULT_SEPARATOR_CHAR = '\t'; + /** CSV File encoding. */ public static final String CSV_FILE_ENCODING = "UTF-8"; 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 2011-01-03 14:14:02 UTC (rev 457) +++ trunk/coser-business/src/main/java/fr/ifremer/coser/services/ImportService.java 2011-01-03 16:19:58 UTC (rev 458) @@ -88,7 +88,7 @@ * @param project project * @param category category * @param file file to load - * @return project + * @return data storage with file content * @throws CoserBusinessException */ public DataStorage loadCSVFile(Project project, Category category, File file) throws CoserBusinessException { @@ -102,7 +102,7 @@ * @param category category * @param file file to load * @param progress progress monitor (can be null) - * @return project + * @return data storage with file content * @throws CoserBusinessException */ public DataStorage loadCSVFile(Project project, Category category, File file, ProgressMonitor progress) throws CoserBusinessException { @@ -118,7 +118,7 @@ * @param progress progress monitor (can be null) * @param originalLoading dans le cas d'un reload, la colonne "line" est a prendre en compte * pour les data (elle est absente dans les jeux de données originaux) - * @return project + * @return data storage with file content * @throws CoserBusinessException */ public DataStorage loadCSVFile(Project project, Category category, File file, ProgressMonitor progress, boolean originalLoading) throws CoserBusinessException { @@ -327,4 +327,49 @@ return content; } + + /** + * Charge un fichier de résultat (estcomind/estpopind). C'est a peu pret + * le meme code qu'au dessus, sauf que le storage est simplement retourné + * et que les séparateurs ne sont pas les même. + * + * @param file file to load + * @return data storage with file content + * @throws CoserBusinessException + */ + public DataStorage loadResultCSVFile(File file) throws CoserBusinessException { + + DataStorage content = new MemoryDataStorage(); + + CSVReader csvReader = null; + try { + + InputStream stream = new FileInputStream(file); + Reader reader = new BufferedReader(new InputStreamReader(stream, CoserConstants.CSV_FILE_ENCODING)); + csvReader = new CSVReader(reader, CoserConstants.CSV_RESULT_SEPARATOR_CHAR); + + // check header + String[] line = csvReader.readNext(); + if (line == null || line.length <= 1) { + throw new CoserBusinessException(_("Can't read file '%s'. Check CSV file separator", + file.getAbsolutePath())); + } + else { + content.add(line); + } + + while ((line = csvReader.readNext()) != null) { + if (line.length>1) { + content.add(line); + } + } + } 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/WebService.java =================================================================== --- trunk/coser-business/src/main/java/fr/ifremer/coser/services/WebService.java 2011-01-03 14:14:02 UTC (rev 457) +++ trunk/coser-business/src/main/java/fr/ifremer/coser/services/WebService.java 2011-01-03 16:19:58 UTC (rev 458) @@ -40,10 +40,12 @@ import fr.ifremer.coser.CoserBusinessConfig; import fr.ifremer.coser.CoserBusinessException; +import fr.ifremer.coser.CoserConstants; import fr.ifremer.coser.bean.Project; import fr.ifremer.coser.bean.RSufiResult; import fr.ifremer.coser.bean.Selection; import fr.ifremer.coser.data.Catch; +import fr.ifremer.coser.storage.DataStorage; /** * Service for web server. @@ -60,11 +62,14 @@ protected CoserBusinessConfig config; + protected ImportService importService; + protected ProjectService projectService; public WebService(CoserBusinessConfig config) { this.config = config; + importService = new ImportService(config); projectService = new ProjectService(config); } @@ -103,6 +108,7 @@ Map<String, String> result = new TreeMap<String, String>(); + // parcours des resultats disponibles File webDirectory = config.getWebServerDirectory(); File newDirectory = new File(webDirectory, "tutu"); File[] projectFiles = newDirectory.listFiles(); @@ -110,13 +116,14 @@ for (File projectFile : projectFiles) { if (projectFile.isDirectory()) { Project project = projectService.openProject(projectFile.getName(), newDirectory); - + for (Selection selection : project.getSelections().values()) { boolean resultFound = false; for (RSufiResult rsufiResult : selection.getRsufiResults()) { + + // extraction des especes pour le résultat demandé if (rsufiResult.getZone() != null && rsufiResult.getZone().equals(zone)) { - projectService.loadSelectionData(project, selection); Map<String, String> resultSpecies = getRsufiResultSpecies(project, selection, rsufiResult); result.putAll(resultSpecies); resultFound = true; @@ -139,13 +146,14 @@ * Recupere la liste de toutes les especes nom sci et nom off à partir * d'un resultat. * - * @param project - * @param selection - * @param rsufiResult - * @return + * @param project project + * @param selection selection + * @param rsufiResult rsufi result + * @return map with each species code/species name + * @throws CoserBusinessException */ protected Map<String, String> getRsufiResultSpecies(Project project, - Selection selection, RSufiResult rsufiResult) { + Selection selection, RSufiResult rsufiResult) throws CoserBusinessException { Map<String, String> result = new HashMap<String, String>(); // load reftax in memory @@ -163,61 +171,129 @@ } // get this selection data - // FIXME echatellier sould be result data, but there is no such data - Iterator<String[]> catchIterator = selection.getCatch().iterator(); - catchIterator.next(); // skip header - while (catchIterator.hasNext()) { - String[] tuple = catchIterator.next(); - - String specyCode = tuple[Catch.INDEX_SPECIES]; + + // un peu lourd mais reconstruit le path jusqu'au fichier estcomind + File webDirectory = config.getWebServerDirectory(); + File newDirectory = new File(webDirectory, "tutu"); + File projectDirectory = new File(newDirectory, project.getName()); + File selectionsDirectory = new File(projectDirectory, CoserConstants.STORAGE_SELECTION_DIRECTORY); + File selectionDirectory = new File(selectionsDirectory, selection.getName()); + File resultsDirectory = new File(selectionDirectory, CoserConstants.STORAGE_RESULTS_DIRECTORY); + File resultDirectory = new File(resultsDirectory, rsufiResult.getName()); + File estPopIndFile = new File(resultDirectory, rsufiResult.getEstPopIndName()); + + // Campagne Indicateur Liste Espece Strate Annee Estimation EcartType CV + DataStorage dataStorage = importService.loadResultCSVFile(estPopIndFile); + + Iterator<String[]> estPopIndIterator = dataStorage.iterator(); + estPopIndIterator.next(); // skip header + while (estPopIndIterator.hasNext()) { + String[] tuple = estPopIndIterator.next(); + + String specyCode = tuple[3]; String specyName = speciesNames.get(specyCode); - + if (StringUtils.isNotEmpty(specyName)) { result.put(specyCode, specyName); } } - + return result; } /** - * Retourne les indicateurs calculables. + * Retourne les indicateurs calculés. * * @param zone zone * @param species especes * @return la liste des indicateurs + * @throws CoserBusinessException */ - public Map<String, String> getIndicators(String zone, String species) { + public Map<String, String> getIndicators(String zone, String species) throws CoserBusinessException { Map<String, String> indicators = new TreeMap<String, String>(); - // populations - indicators.put("Abundance", "Abundance"); - indicators.put("lnN", "lnN"); - //indicators.put("Dbar", "Dbar"); - indicators.put("Wbar", "Wbar"); - indicators.put("Biomass", "Biomass"); - indicators.put("Lbar", "Lbar"); - //indicators.put("propMat", "propMat"); - indicators.put("L50", "L50"); - //indicators.put("sexRatio", "sexRatio"); - //indicators.put("Zmoy", "Zmoy"); + // parcours des resultats disponibles + File webDirectory = config.getWebServerDirectory(); + File newDirectory = new File(webDirectory, "tutu"); + File[] projectFiles = newDirectory.listFiles(); + if (projectFiles != null) { + for (File projectFile : projectFiles) { + if (projectFile.isDirectory()) { + Project project = projectService.openProject(projectFile.getName(), newDirectory); - // communauté - indicators.put("Delta", "Delta"); - indicators.put("Gtot", "Gtot"); - //indicators.put("Shannonmod", "Shannonmod"); - indicators.put("meanWbar", "meanWbar"); - indicators.put("NBWtot", "NBWtot"); - //indicators.put("propL", "propL"); - indicators.put("propLW", "propLW"); - indicators.put("meanQuant", "meanQuant"); - //indicators.put("biomSmall", "biomSmall"); - //indicators.put("biomBig", "biomBig"); + for (Selection selection : project.getSelections().values()) { + boolean resultFound = false; + + for (RSufiResult rsufiResult : selection.getRsufiResults()) { + + // extraction des especes pour le résultat demandé + if (rsufiResult.getZone() != null && rsufiResult.getZone().equals(zone)) { + // data loading not necessary here + Map<String, String> resultIndicators = getRsufiResultIndicators(project, selection, rsufiResult, species); + indicators.putAll(resultIndicators); + resultFound = true; + break; + } + } + + if (resultFound) { + break; + } + } + } + } + } return indicators; } /** + * Recupere la liste de toutes les especes nom sci et nom off à partir + * d'un resultat. + * + * @param project + * @param selection + * @param rsufiResult + * @param species to get indicator + * @return indicator for species + * @throws CoserBusinessException + */ + protected Map<String, String> getRsufiResultIndicators(Project project, + Selection selection, RSufiResult rsufiResult, String species) throws CoserBusinessException { + Map<String, String> result = new HashMap<String, String>(); + + // on peu lours mais reconstruit le path jusqu'au fichier estcomind + File webDirectory = config.getWebServerDirectory(); + File newDirectory = new File(webDirectory, "tutu"); + File projectDirectory = new File(newDirectory, project.getName()); + File selectionsDirectory = new File(projectDirectory, CoserConstants.STORAGE_SELECTION_DIRECTORY); + File selectionDirectory = new File(selectionsDirectory, selection.getName()); + File resultsDirectory = new File(selectionDirectory, CoserConstants.STORAGE_RESULTS_DIRECTORY); + File resultDirectory = new File(resultsDirectory, rsufiResult.getName()); + + // le fichier estcomind + File estPopIndFile = new File(resultDirectory, rsufiResult.getEstPopIndName()); + + // Campagne Indicateur Liste Espece Strate Annee Estimation EcartType CV + DataStorage dataStorage = importService.loadResultCSVFile(estPopIndFile); + + Iterator<String[]> estPopIndIterator = dataStorage.iterator(); + estPopIndIterator.next(); // skip header + while (estPopIndIterator.hasNext()) { + String[] tuple = estPopIndIterator.next(); + + String specyCode = tuple[3]; + if (specyCode.equals(species)) { + + String indicatorCode = tuple[1]; + result.put(indicatorCode, indicatorCode); + } + } + + return result; + } + + /** * Generer les données du graph et genere la liste des parametres pour * les generateurs de graph (eastwood ou jfreechart). * Modified: trunk/coser-web/src/main/java/fr/ifremer/coser/web/actions/SelectIndicatorAction.java =================================================================== --- trunk/coser-web/src/main/java/fr/ifremer/coser/web/actions/SelectIndicatorAction.java 2011-01-03 14:14:02 UTC (rev 457) +++ trunk/coser-web/src/main/java/fr/ifremer/coser/web/actions/SelectIndicatorAction.java 2011-01-03 16:19:58 UTC (rev 458) @@ -24,9 +24,14 @@ import java.util.Map; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + import com.opensymphony.xwork2.ActionSupport; +import fr.ifremer.coser.CoserBusinessException; import fr.ifremer.coser.services.WebService; +import fr.ifremer.coser.web.CoserWebException; import fr.ifremer.coser.web.ServiceFactory; /** @@ -43,6 +48,8 @@ /** serialVersionUID. */ private static final long serialVersionUID = 1663244944108703571L; + private static final Log log = LogFactory.getLog(SelectIndicatorAction.class); + protected String zone; protected String species; @@ -72,10 +79,18 @@ @Override public String execute() { + if (log.isInfoEnabled()) { + log.info(String.format("Looking for indicator for zone %s and species %s", zone, species)); + } + WebService webService = ServiceFactory.getWebService(); - - indicators = webService.getIndicators(zone, species); + try { + indicators = webService.getIndicators(zone, species); + } catch (CoserBusinessException ex) { + throw new CoserWebException("Can't get indicators", ex); + } + return SUCCESS; } } Modified: trunk/coser-web/src/main/webapp/WEB-INF/content/select-indicator.jsp =================================================================== --- trunk/coser-web/src/main/webapp/WEB-INF/content/select-indicator.jsp 2011-01-03 14:14:02 UTC (rev 457) +++ trunk/coser-web/src/main/webapp/WEB-INF/content/select-indicator.jsp 2011-01-03 16:19:58 UTC (rev 458) @@ -36,8 +36,8 @@ <s:form action="show-graph" method="get"> <s:select name="indicator" list="indicators" label="Select an indicator" /> - <s:hidden name="zone" value="zone" /> - <s:hidden name="species" value="species" /> + <s:hidden name="zone" property="zone"/> + <s:hidden name="species" property="species"/> <s:submit label="Suite"/> </s:form> </body> Modified: trunk/coser-web/src/main/webapp/WEB-INF/content/select-species.jsp =================================================================== --- trunk/coser-web/src/main/webapp/WEB-INF/content/select-species.jsp 2011-01-03 14:14:02 UTC (rev 457) +++ trunk/coser-web/src/main/webapp/WEB-INF/content/select-species.jsp 2011-01-03 16:19:58 UTC (rev 458) @@ -37,7 +37,7 @@ <s:form action="select-indicator" method="get"> <s:select name="species" list="species" label="Select a species" /> - <s:hidden name="zone" value="zone" /> + <s:hidden name="zone" property="zone"/> <s:submit label="Suite"/> </s:form> </body>
participants (1)
-
chatellier@users.labs.libre-entreprise.org