Author: chatellier Date: 2009-06-20 19:03:07 +0000 (Sat, 20 Jun 2009) New Revision: 2413 Modified: isis-fish/trunk/src/main/java/fr/ifremer/isisfish/simulator/SimulationParameter.java Log: Add method to reload rule parameters from current topia context. Modified: isis-fish/trunk/src/main/java/fr/ifremer/isisfish/simulator/SimulationParameter.java =================================================================== --- isis-fish/trunk/src/main/java/fr/ifremer/isisfish/simulator/SimulationParameter.java 2009-06-19 16:59:20 UTC (rev 2412) +++ isis-fish/trunk/src/main/java/fr/ifremer/isisfish/simulator/SimulationParameter.java 2009-06-20 19:03:07 UTC (rev 2413) @@ -20,6 +20,8 @@ import static org.codelutin.i18n.I18n._; import java.io.File; +import java.lang.reflect.Field; +import java.lang.reflect.Modifier; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; @@ -552,6 +554,66 @@ } /** + * Reload parameters du to context change. + * + * ie : in simulators when rollbacking transaction + * + * Actually : reload rules + * + * @throws TopiaException + */ + public void reloadContextParameters() throws TopiaException { + + // On verifie tout d'abord que l'on ai pas dans une simulation + // si on y es, on utilise le context static non null du thread local + // Resoud les lazy exceptions des parametres des regles + boolean mustClose = false; + TopiaContext tx = SimulationContext.get().getDB(); + + if (tx == null) { + // not in simulation, create transaction + tx = getRegion().getStorage().beginTransaction(); + mustClose = true; + } + + // reload rules + if (log.isDebugEnabled()) { + log.debug("Reloading rules"); + } + for (Rule rule : rules) { + try { + for (Field field : rule.getClass().getFields()) { + // le champ ne doit pas être privé + if (Modifier.isPublic(field.getModifiers())) { + // si le type est topia entity (ou sous classe) + if (TopiaEntity.class.isAssignableFrom(field.getType())) { + TopiaEntity entity = (TopiaEntity)field.get(rule); + // reloading + TopiaEntity newEntity = tx.findByTopiaId(entity.getTopiaId()); + field.set(rule, newEntity); + } + } + } + } catch (IllegalArgumentException e) { + if (log.isErrorEnabled()) { + log.error("Can't refresh rule field", e); + } + } catch (IllegalAccessException e) { + if (log.isErrorEnabled()) { + log.error("Can't access rule field", e); + } + } + } + + // si la transaction a été ouverte (pas dans une simulation) + // on la referme + if (mustClose) { + tx.commitTransaction(); + tx.closeContext(); + } + } + + /** * @return Returns the region. */ public RegionStorage getRegion() {
participants (1)
-
chatellier@users.labs.libre-entreprise.org