r2120 - isis-fish/trunk/src/main/java/fr/ifremer/isisfish/datastore
Author: bpoussin Date: 2009-04-18 10:25:47 +0000 (Sat, 18 Apr 2009) New Revision: 2120 Modified: isis-fish/trunk/src/main/java/fr/ifremer/isisfish/datastore/ResultStorage.java Log: correction bug de TopiaContext. On utilisait certaine fois le context de simulation alors que le SimulationStorage n'etait pas celui de la simulation. Un test a ete ajoute pour eviter cette erreur Modified: isis-fish/trunk/src/main/java/fr/ifremer/isisfish/datastore/ResultStorage.java =================================================================== --- isis-fish/trunk/src/main/java/fr/ifremer/isisfish/datastore/ResultStorage.java 2009-04-17 12:26:08 UTC (rev 2119) +++ isis-fish/trunk/src/main/java/fr/ifremer/isisfish/datastore/ResultStorage.java 2009-04-18 10:25:47 UTC (rev 2120) @@ -84,11 +84,11 @@ /** to use log facility, just put in your code: log.info(\"...\"); */ static private Log log = LogFactory.getLog(ResultStorage.class); - + protected SimulationStorage simulation = null; // transient protected HashMap<String, MatrixND> globalMatrix = new HashMap<String, MatrixND>(); transient protected ReferenceMap cacheContext = new ReferenceMap(ReferenceMap.HARD, ReferenceMap.WEAK); - + /** cache to maintains some result. key: String(date + ':' + name), value: matrix * TODO: cache will be more efficient if it keep at min the number of result by year */ transient protected ReferenceMap cache = new ReferenceMap(ReferenceMap.HARD, ReferenceMap.SOFT); @@ -105,7 +105,7 @@ public ResultStorage(SimulationStorage simulation) { this.simulation = simulation; } - + protected void putInCache(Date date, String name, MatrixND mat, TopiaContext context) { String key = date + ":" + name; putInCache(key, mat, context); @@ -142,8 +142,13 @@ if (availableResult == null) { availableResult = new HashSet<String>(); try { - TopiaContext tx = SimulationContext.get().getDbResult(); - boolean mustClose = false; + TopiaContext tx = null; + boolean mustClose = false; + + if (simulation == SimulationContext.get().getSimulationStorage()) { + tx = SimulationContext.get().getDbResult(); + } + if (tx == null) { // not in simulation, create transaction tx = simulation.getStorage().beginTransaction(); @@ -165,7 +170,7 @@ } return availableResult; } - + /** * Verifie si un resultat est disponible pour une date donnée * @param date @@ -186,7 +191,7 @@ String key = date.getDate() + ":" + name; getAvailableResult().add(key); } - + /** * Permet de savoir si lorsque l'on ajoutera ce resultat, il sera * sauvé ou non. @@ -195,7 +200,7 @@ name = name.trim(); if (enabledResult == null) { enabledResult = new HashSet<String>(); - + Collection<String> resultEnabled = simulation.getParameter().getResultEnabled(); enabledResult.addAll(resultEnabled); List<String> exportNames = simulation.getParameter().getExportNames(); @@ -259,7 +264,7 @@ public void addResult(Date date, String name, MatrixND mat) throws IsisFishException{ addResult(false, date, name, mat); } - + public void addResult(boolean force, Date date, String name, Population pop, MatrixND mat) throws IsisFishException{ if (force || isEnabled(name)) { doAddResult(date, name + " " + pop, mat); @@ -271,11 +276,15 @@ doAddResult(date, name, mat); } } - + protected void doAddResult(Date date, String name, MatrixND mat) throws IsisFishException{ try { - TopiaContext tx = SimulationContext.get().getDbResult(); - boolean mustClose = false; + TopiaContext tx = null; + boolean mustClose = false; + + if (simulation == SimulationContext.get().getSimulationStorage()) { + tx = SimulationContext.get().getDbResult(); + } if (tx == null) { // not in simulation, create transaction tx = simulation.getStorage().beginTransaction(); @@ -319,8 +328,12 @@ public void addActiveRule(Date date, Rule rule) throws IsisFishException { try { - TopiaContext tx = SimulationContext.get().getDbResult(); - boolean mustClose = false; + TopiaContext tx = null; + boolean mustClose = false; + + if (simulation == SimulationContext.get().getSimulationStorage()) { + tx = SimulationContext.get().getDbResult(); + } if (tx == null) { // not in simulation, create transaction tx = simulation.getStorage().beginTransaction(); @@ -338,9 +351,9 @@ } } catch (TopiaException eee) { throw new IsisFishException("Can't add result", eee); - } + } } - + /** * Retourne la liste de tous les résultats. Si le résultat est categorisé * par une population alors le nom de la population est automatiquement @@ -348,19 +361,23 @@ */ @SuppressWarnings("unchecked") public List<String> getResultName(){ - + List<String> result = null; try { - TopiaContext tx = SimulationContext.get().getDbResult(); - boolean mustClose = false; + TopiaContext tx = null; + boolean mustClose = false; + + if (simulation == SimulationContext.get().getSimulationStorage()) { + tx = SimulationContext.get().getDbResult(); + } if (tx == null) { // not in simulation, create transaction tx = simulation.getStorage().beginTransaction(); mustClose = true; } ResultDAO resultPS = IsisFishDAOHelper.getResultDAO(tx); - - + + result = (List<String>)resultPS.getContext().find( "Select distinct name from fr.ifremer.isisfish.entities.Result order by name"); if (mustClose) { @@ -393,8 +410,12 @@ MatrixND mat = getInCache(date, name); if (mat == null && isAvailableResult(date, name)) { try { - TopiaContext tx = SimulationContext.get().getDbResult(); - boolean mustClose = false; + TopiaContext tx = null; + boolean mustClose = false; + + if (simulation == SimulationContext.get().getSimulationStorage()) { + tx = SimulationContext.get().getDbResult(); + } if (tx == null) { // not in simulation, create transaction tx = simulation.getStorage().beginTransaction(); @@ -413,11 +434,11 @@ if (log.isWarnEnabled()) { log.warn("Can't return matrix '" + name + "' for date " + date, eee); } - } + } } return mat; } - + /** * Retourne la matrice stocke pour un pas de temps * @param date le pas de temps que l'on souhaite @@ -435,12 +456,12 @@ if (result != null) { mat = result.getMatrix(); putInCache(date, name, mat, tx); - } + } } catch (Exception eee) { if (log.isWarnEnabled()) { log.warn("Can't return matrix '" + name + "' for date " + date, eee); } - } + } } return mat; } @@ -462,8 +483,12 @@ public MatrixND getMatrix(String name){ MatrixND resultMat = null; try { - TopiaContext tx = SimulationContext.get().getDbResult(); - boolean mustClose = false; + TopiaContext tx = null; + boolean mustClose = false; + + if (simulation == SimulationContext.get().getSimulationStorage()) { + tx = SimulationContext.get().getDbResult(); + } if (tx == null) { // not in simulation, create transaction tx = simulation.getStorage().beginTransaction(); @@ -485,7 +510,7 @@ } return resultMat; } - + /** * Retourne une matrice contenant tous les pas de temps. * @param name le nom des resultats dont on veut une matrice globale. @@ -557,7 +582,7 @@ log.trace("Ajout de la semantics: "+ Arrays.asList(mattmp.getSemantics())); } - + for(int s=0; s<mattmp.getNbDim(); s++){ sem[s+1].addAll(mattmp.getSemantics(s)); } @@ -598,7 +623,7 @@ * @return */ public Date getLastDate() { - int monthNumber = simulation.getParameter().getNumberOfYear() * Month.NUMBER_OF_MONTH; + int monthNumber = simulation.getParameter().getNumberOfYear() * Month.NUMBER_OF_MONTH; Date result = new Date(monthNumber - 1); // -1 because date begin at 0 return result; } @@ -678,4 +703,3 @@ // } } // ResultStorage -
participants (1)
-
bpoussin@users.labs.libre-entreprise.org