Author: chatellier Date: 2009-03-17 15:41:44 +0000 (Tue, 17 Mar 2009) New Revision: 1985 Modified: isis-fish/trunk/src/main/java/fr/ifremer/isisfish/entities/PopulationImpl.java Log: Fix bug were a population cannot be delete. Seams to be resaved by species Modified: isis-fish/trunk/src/main/java/fr/ifremer/isisfish/entities/PopulationImpl.java =================================================================== --- isis-fish/trunk/src/main/java/fr/ifremer/isisfish/entities/PopulationImpl.java 2009-03-17 15:11:59 UTC (rev 1984) +++ isis-fish/trunk/src/main/java/fr/ifremer/isisfish/entities/PopulationImpl.java 2009-03-17 15:41:44 UTC (rev 1985) @@ -1,5 +1,5 @@ /* *##% - * Copyright (C) 2006 + * Copyright (C) 2006 - 2009 * Code Lutin, Cédric Pineau, Benjamin Poussin * * This program is free software; you can redistribute it and/or @@ -17,18 +17,6 @@ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *##%*/ -/* * - * PopulationImpl.java - * - * Created: 11 janv. 2006 20:16:27 - * - * @author poussin - * @version $Revision$ - * - * Last update: $Date$ - * by : $Author$ - */ - package fr.ifremer.isisfish.entities; import static org.codelutin.i18n.I18n._; @@ -57,30 +45,47 @@ import fr.ifremer.isisfish.equation.PopulationReproductionEquation; import fr.ifremer.isisfish.types.Month; - /** + * PopulationImpl. + * + * Created: 11 janv. 2006 20:16:27 + * * @author poussin + * @version $Revision$ * + * Last update: $Date$ + * by : $Author$ */ - public class PopulationImpl extends PopulationAbstract { + /** Logger for this class. */ + private static final Log log = LogFactory.getLog(PopulationImpl.class); + + /** serialVersionUID */ + private static final long serialVersionUID = 1L; + /** - * Logger for this class + * Overwrite delete. + * @throws TopiaException */ - private static final Log log = LogFactory.getLog(PopulationImpl.class); + @Override + public void delete() throws TopiaException { + // chatellier 20090317 fix hibernate exeception + // object will be resaved ... + // FIXME should be fixed on topia maybe ? + this.getSpecies().getPopulation().remove(this); - /** */ - private static final long serialVersionUID = 1L; + super.delete(); + } - /* (non-Javadoc) + /* * @see fr.ifremer.isisfish.entities.PopulationAbstract#toString() */ @Override public String toString() { return getName(); } - + /** * override to change name of equation * @see fr.ifremer.isisfish.entities.PopulationAbstract#setName(java.lang.String) @@ -90,7 +95,7 @@ super.setName(value); updateEquationName(); } - + private void updateEquationName() { Equation eq = null; eq = getGrowth(); @@ -118,21 +123,22 @@ eq.setName(getName()); } } - + public void setEquationReproductionEquation(String content) { try { Equation eq = getReproductionEquation(); - + if (eq == null) { - EquationDAO dao = IsisFishDAOHelper.getEquationDAO(getTopiaContext()); + EquationDAO dao = IsisFishDAOHelper + .getEquationDAO(getTopiaContext()); // create equation - eq = dao.create(); + eq = dao.create(); eq.setCategory("Reproduction"); eq.setJavaInterface(PopulationReproductionEquation.class); eq.setLanguage(Language.JAVA); setReproductionEquation(eq); } - + eq.setName(getName()); // Fire @@ -143,25 +149,28 @@ eq.update(); fireOnPostWrite("content", _oldValue, content); - + } catch (TopiaException eee) { - throw new IsisFishRuntimeException(_("isisfish.error.change.equation"), eee); + throw new IsisFishRuntimeException( + _("isisfish.error.change.equation"), eee); } } + public void setEquationGrowth(String content) { try { Equation eq = getGrowth(); - + if (eq == null) { - EquationDAO dao = IsisFishDAOHelper.getEquationDAO(getTopiaContext()); + EquationDAO dao = IsisFishDAOHelper + .getEquationDAO(getTopiaContext()); // create Growth equation - eq = dao.create(); + eq = dao.create(); eq.setCategory("Growth"); eq.setJavaInterface(PopulationGrowth.class); eq.setLanguage(Language.JAVA); setGrowth(eq); } - + eq.setName(getName()); // Fire @@ -174,23 +183,26 @@ fireOnPostWrite("content", _oldValue, content); } catch (TopiaException eee) { - throw new IsisFishRuntimeException(_("isisfish.change.equation"), eee); + throw new IsisFishRuntimeException(_("isisfish.change.equation"), + eee); } } + public void setEquationGrowthReverse(String content) { try { Equation eq = getGrowthReverse(); - + if (eq == null) { - EquationDAO dao = IsisFishDAOHelper.getEquationDAO(getTopiaContext()); + EquationDAO dao = IsisFishDAOHelper + .getEquationDAO(getTopiaContext()); // create equation - eq = dao.create(); + eq = dao.create(); eq.setCategory("GrowthReverse"); eq.setJavaInterface(PopulationGrowthReverse.class); eq.setLanguage(Language.JAVA); setGrowthReverse(eq); } - + eq.setName(getName()); // Fire @@ -203,23 +215,26 @@ fireOnPostWrite("content", _oldValue, content); } catch (TopiaException eee) { - throw new IsisFishRuntimeException(_("isisfish.error.change.equation"), eee); + throw new IsisFishRuntimeException( + _("isisfish.error.change.equation"), eee); } } + public void setEquationNaturalDeathRate(String content) { try { Equation eq = getNaturalDeathRate(); - + if (eq == null) { - EquationDAO dao = IsisFishDAOHelper.getEquationDAO(getTopiaContext()); + EquationDAO dao = IsisFishDAOHelper + .getEquationDAO(getTopiaContext()); // create equation - eq = dao.create(); + eq = dao.create(); eq.setCategory("NaturalDeathRate"); eq.setJavaInterface(PopulationNaturalDeathRate.class); eq.setLanguage(Language.JAVA); setNaturalDeathRate(eq); } - + eq.setName(getName()); // Fire @@ -232,23 +247,26 @@ fireOnPostWrite("content", _oldValue, content); } catch (TopiaException eee) { - throw new IsisFishRuntimeException(_("isisfish.error.change.equation"), eee); + throw new IsisFishRuntimeException( + _("isisfish.error.change.equation"), eee); } } + public void setEquationMeanWeight(String content) { try { Equation eq = getMeanWeight(); - + if (eq == null) { - EquationDAO dao = IsisFishDAOHelper.getEquationDAO(getTopiaContext()); + EquationDAO dao = IsisFishDAOHelper + .getEquationDAO(getTopiaContext()); // create equation - eq = dao.create(); + eq = dao.create(); eq.setCategory("MeanWeight"); eq.setJavaInterface(PopulationMeanWeight.class); eq.setLanguage(Language.JAVA); setMeanWeight(eq); } - + eq.setName(getName()); // Fire @@ -261,23 +279,26 @@ fireOnPostWrite("content", _oldValue, content); } catch (TopiaException eee) { - throw new IsisFishRuntimeException(_("isisfish.error.change.equation"), eee); + throw new IsisFishRuntimeException( + _("isisfish.error.change.equation"), eee); } } + public void setEquationPrice(String content) { try { Equation eq = getPrice(); - + if (eq == null) { - EquationDAO dao = IsisFishDAOHelper.getEquationDAO(getTopiaContext()); + EquationDAO dao = IsisFishDAOHelper + .getEquationDAO(getTopiaContext()); // create equation - eq = dao.create(); + eq = dao.create(); eq.setCategory("Price"); eq.setJavaInterface(PopulationPrice.class); eq.setLanguage(Language.JAVA); setPrice(eq); } - + eq.setName(getName()); // Fire @@ -290,13 +311,15 @@ fireOnPostWrite("content", _oldValue, content); } catch (TopiaException eee) { - throw new IsisFishRuntimeException(_("isisfish.error.change.equation"), eee); + throw new IsisFishRuntimeException( + _("isisfish.error.change.equation"), eee); } } - - /* (non-Javadoc) + + /* * @see fr.ifremer.isisfish.entities.Population#getNaturalDeathBirth() */ + @Override public double getNaturalDeathBirth(Zone zone) { double result = 0; try { @@ -312,36 +335,38 @@ } return result; } - + /** * return Matrix [groups x zones] of Natural death rate * @return */ + @Override public MatrixND getNaturalDeathRateMatrix() { List<PopulationGroup> groups = getPopulationGroup(); List<Zone> zones = getPopulationZone(); - + MatrixND result = MatrixFactory.getInstance().create( - "Natural death rate", - new List[]{groups, zones}, - new String[]{"Groups", "Zones"}); - - for (MatrixIterator i=result.iterator(); i.hasNext();) { + "Natural death rate", new List[] { groups, zones }, + new String[] { "Groups", "Zones" }); + + for (MatrixIterator i = result.iterator(); i.hasNext();) { i.next(); - PopulationGroup group = (PopulationGroup)i.getSemanticsCoordinates()[0]; - Zone zone = (Zone)i.getSemanticsCoordinates()[1]; - + PopulationGroup group = (PopulationGroup) i + .getSemanticsCoordinates()[0]; + Zone zone = (Zone) i.getSemanticsCoordinates()[1]; + double value = group.getNaturalDeathRate(zone); i.setValue(value); } - + return result; } - + + @Override public PopulationSeasonInfo getPopulationSeasonInfo(Month month) { Collection<PopulationSeasonInfo> all = getPopulationSeasonInfo(); PopulationSeasonInfo result = null; - for(PopulationSeasonInfo psi : all) { + for (PopulationSeasonInfo psi : all) { if (psi.containsMonth(month)) { result = psi; break; @@ -349,13 +374,14 @@ } return result; } - + /** * Compute age of group with length * @param length length of group * @param group group * @return age in month */ + @Override public double getAge(double length, PopulationGroup group) { double result = 0; try { @@ -363,7 +389,7 @@ result = growth.evaluate("length", length, "group", group); } catch (Exception eee) { if (log.isWarnEnabled()) { - log.warn("Error in growth equation"); + log.warn("Error in get age"); } if (log.isDebugEnabled()) { log.debug("StackTrace", eee); @@ -371,13 +397,14 @@ } return result; } - + /** * Compute length of group with age * @param age age of group in month * @param group group * @return length of group */ + @Override public double getLength(double age, PopulationGroup group) { double result = 0; try { @@ -385,7 +412,7 @@ result = growth.evaluate("age", age, "group", group); } catch (Exception eee) { if (log.isWarnEnabled()) { - log.warn("Error in growth equation"); + log.warn("Error in get length"); } if (log.isDebugEnabled()) { log.debug("StackTrace", eee); @@ -393,47 +420,47 @@ } return result; } - -// /* (non-Javadoc) -// * @see fr.ifremer.isisfish.entities.PopulationAbstract#addPopulationSeasonInfo(fr.ifremer.isisfish.entities.PopulationSeasonInfo) -// */ -// @Override -// public void addPopulationSeasonInfo(PopulationSeasonInfo populationSeasonInfo) { -// super.addPopulationSeasonInfo(populationSeasonInfo); -// changeCapturability(); -// } -// -// /* (non-Javadoc) -// * @see fr.ifremer.isisfish.entities.PopulationAbstract#addPopulationGroup(fr.ifremer.isisfish.entities.PopulationGroup) -// */ -// @Override -// public void addPopulationGroup(PopulationGroup populationGroup) { -// super.addPopulationGroup(populationGroup); -// changeCapturability(); -// } -// protected void changeCapturability() { -// MatrixND mat = null; -// if (sizePopulationGroup() == 0 || sizePopulationSeasonInfo() == 0) { -// log.warn("Population doesn't have population group or population season info"); -// } else { -// List [] sems = new List[]{ -// getPopulationGroup(), -// getPopulationSeasonInfo(), -// }; -// mat = MatrixFactory.getInstance().create(_("isisfish.population.capturability"), sems, -// new String[]{_("isisfish.population.group"), _("isisfish.population.season")}); -// -// -// MatrixND oldMat = getCapturability(); -// if (oldMat != null) { -// mat.pasteSemantics(oldMat); -// } -// } -// setCapturability(mat); -// } - - /* (non-Javadoc) + // /* (non-Javadoc) + // * @see fr.ifremer.isisfish.entities.PopulationAbstract#addPopulationSeasonInfo(fr.ifremer.isisfish.entities.PopulationSeasonInfo) + // */ + // @Override + // public void addPopulationSeasonInfo(PopulationSeasonInfo populationSeasonInfo) { + // super.addPopulationSeasonInfo(populationSeasonInfo); + // changeCapturability(); + // } + // + // /* (non-Javadoc) + // * @see fr.ifremer.isisfish.entities.PopulationAbstract#addPopulationGroup(fr.ifremer.isisfish.entities.PopulationGroup) + // */ + // @Override + // public void addPopulationGroup(PopulationGroup populationGroup) { + // super.addPopulationGroup(populationGroup); + // changeCapturability(); + // } + + // protected void changeCapturability() { + // MatrixND mat = null; + // if (sizePopulationGroup() == 0 || sizePopulationSeasonInfo() == 0) { + // log.warn("Population doesn't have population group or population season info"); + // } else { + // List [] sems = new List[]{ + // getPopulationGroup(), + // getPopulationSeasonInfo(), + // }; + // mat = MatrixFactory.getInstance().create(_("isisfish.population.capturability"), sems, + // new String[]{_("isisfish.population.group"), _("isisfish.population.season")}); + // + // + // MatrixND oldMat = getCapturability(); + // if (oldMat != null) { + // mat.pasteSemantics(oldMat); + // } + // } + // setCapturability(mat); + // } + + /* * @see fr.ifremer.isisfish.entities.PopulationAbstract#getCapturability() */ @Override @@ -441,24 +468,31 @@ MatrixND mat = super.getCapturability(); // check the validity if (sizePopulationGroup() == 0 || sizePopulationSeasonInfo() == 0) { - log.warn("Population doesn't have population group or population season info"); + log + .warn("Population doesn't have population group or population season info"); return null; } - List [] sems = new List[]{ - getPopulationGroup(), - getPopulationSeasonInfo(), - }; + List[] sems = new List[] { getPopulationGroup(), + getPopulationSeasonInfo(), }; if (mat == null) { log.debug("Capturability is null, create new matrix"); - mat = MatrixFactory.getInstance().create(n_("isisfish.population.capturability"), sems, - new String[]{n_("isisfish.population.group"), n_("isisfish.population.season")}); + mat = MatrixFactory.getInstance().create( + n_("isisfish.population.capturability"), + sems, + new String[] { n_("isisfish.population.group"), + n_("isisfish.population.season") }); // we don't call setCapturability because is better to create a valid // matrix when capturability is null instead check validity and // create new one and paste the old - } else if (!Arrays.equals(mat.getSemantics(), sems)){ - log.debug("Capturability has changed, create new matrix and copy old: " + mat.getSemantics() + " " + sems); - MatrixND newmat = MatrixFactory.getInstance().create(n_("isisfish.population.capturability"), sems, - new String[]{n_("isisfish.population.group"), n_("isisfish.population.season")}); + } else if (!Arrays.equals(mat.getSemantics(), sems)) { + log + .debug("Capturability has changed, create new matrix and copy old: " + + mat.getSemantics() + " " + sems); + MatrixND newmat = MatrixFactory.getInstance().create( + n_("isisfish.population.capturability"), + sems, + new String[] { n_("isisfish.population.group"), + n_("isisfish.population.season") }); newmat.pasteSemantics(mat); mat = newmat; // perhaps call setCapturability, but if possible wait the user @@ -467,35 +501,39 @@ return mat; } - /* (non-Javadoc) + /* * @see fr.ifremer.isisfish.entities.PopulationAbstract#getMappingZoneReproZoneRecru() */ @Override public MatrixND getMappingZoneReproZoneRecru() { if (sizeReproductionZone() == 0 || sizeRecruitmentZone() == 0) { - log.warn("Population doesn't have zone reproduction or zone recruitment"); - return null; + log + .warn("Population doesn't have zone reproduction or zone recruitment"); + return null; } MatrixND mat = super.getMappingZoneReproZoneRecru(); - List [] sems = new List[]{ - getReproductionZone(), - getRecruitmentZone(), - }; + List[] sems = new List[] { getReproductionZone(), getRecruitmentZone(), }; if (mat == null) { - mat = MatrixFactory.getInstance().create(n_("isisfish.population.mappingZoneReproZoneRecru"), sems, - new String[]{n_("isisfish.population.reproduction"), n_("isisfish.population.recruitment")}); + mat = MatrixFactory.getInstance().create( + n_("isisfish.population.mappingZoneReproZoneRecru"), + sems, + new String[] { n_("isisfish.population.reproduction"), + n_("isisfish.population.recruitment") }); // we don't call setMappingZoneReproZoneRecru because is better to create a valid // matrix when MappingZoneReproZoneRecru is null instead check validity and // create new one and paste the old } else if (!Arrays.equals(mat.getSemantics(), sems)) { - MatrixND newmat = MatrixFactory.getInstance().create(n_("isisfish.population.mappingZoneReproZoneRecru"), sems, - new String[]{n_("isisfish.population.reproduction"), n_("isisfish.population.recruitment")}); + MatrixND newmat = MatrixFactory.getInstance().create( + n_("isisfish.population.mappingZoneReproZoneRecru"), + sems, + new String[] { n_("isisfish.population.reproduction"), + n_("isisfish.population.recruitment") }); newmat.paste(mat); mat = newmat; } return mat; } - + /** * convertie une matrice N 2D en une matrice N 1D strucutre pour les * calcules. @@ -509,15 +547,16 @@ MatrixND result = MatrixFactory.getInstance().create( n_("isisfish.population.matrixAbundance1D"), - new int[]{1, maxX * maxY}); - - for(int x=0; x<maxX; x++){ - for(int y=0; y<maxY; y++){ - result.setValue(0, x*maxY+y, N.getValue(x, y)); + new int[] { 1, maxX * maxY }); + + for (int x = 0; x < maxX; x++) { + for (int y = 0; y < maxY; y++) { + result.setValue(0, x * maxY + y, N.getValue(x, y)); } } return result; } + /** * a partir d'une matrice N structure selon l'ecriture matricielle * en classe puis en zone, construit une matrice 2D en Classe(X) @@ -532,25 +571,24 @@ MatrixND result = MatrixFactory.getInstance().create( n_("isisfish.population.matrixAbundance"), - new List[]{groups, zones}, - new String[]{n_("isisfish.population.groups"), n_("isisfish.population.zones")}); + new List[] { groups, zones }, + new String[] { n_("isisfish.population.groups"), + n_("isisfish.population.zones") }); try { int c = 0; int z = 0; - for(MatrixIterator mi=N.iterator(); mi.next();){ - result.setValue(c,z, mi.getValue()); - if (z+1 == nbZone){ + for (MatrixIterator mi = N.iterator(); mi.next();) { + result.setValue(c, z, mi.getValue()); + if (z + 1 == nbZone) { c++; } - z = (z+1)%nbZone; + z = (z + 1) % nbZone; } - } catch(MatrixException eee) { + } catch (MatrixException eee) { log.warn("MatrixException", eee); } return result; - } - + } + } - -