/* * #%L * IsisFish data * %% * Copyright (C) 2016 Ifremer, Code Lutin, Chatellier Eric * %% * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as * published by the Free Software Foundation, either version 2 of the * License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public * License along with this program. If not, see * . * #L% */ package scripts; import java.io.IOException; import java.util.*; import org.nuiton.topia.TopiaContext; import fr.ifremer.isisfish.IsisFishDAOHelper; import fr.ifremer.isisfish.datastore.RegionStorage; import fr.ifremer.isisfish.entities.*; import fr.ifremer.isisfish.types.Month; import org.nuiton.util.FileUtil; import java.io.FileReader; import org.nuiton.math.matrix.*; import java.io.File; import java.io.Writer; import java.io.FileWriter; /** * Ce script sert à modifier une region de facon informatique car il peut être très * couteux en temps de cliquer à de nombreuses reprises dans l'interface graphique. * * @author Eric Chatellier */ public class FillTargetFactors { public void run() throws Exception { // ouverture d'une region par son nom RegionStorage region = RegionStorage.getRegion("CERES_PEL"); // ouverture d'une transaction pour effectuer toutes les modifications TopiaContext tx = region.getStorage().beginTransaction(); // utilisation du contexte dans d'autre methodes fillpossibleMetiersParam(tx); fillStdFactors(tx); createTargetSpecies(tx); updateMetier(tx); // commit : valide les transactions en base de données // roolback : annule les modifications en base de données (permet de tester le script) tx.commitTransaction(); //tx.rollbackTransaction(); // close region tx.closeContext(); } protected void fillpossibleMetiersParam(TopiaContext tx) { SetOfVesselsDAO sovDAO = IsisFishDAOHelper.getSetOfVesselsDAO(tx); List sovs = sovDAO.findAll(); for(SetOfVessels sov : sovs){ Collection strMet = sov.getPossibleMetiers(); for (EffortDescription ed : strMet) { ed.setFishingOperation(1); ed.setGearsNumberPerOperation(1); } } } /** * Modifie les standardisation factors * * @param tx transaction */ protected void fillStdFactors(TopiaContext tx) { GearDAO gearDAO = IsisFishDAOHelper.getGearDAO(tx); List gears = gearDAO.findAll(); for(Gear ge : gears){ ge.setStandardisationFactor(1); } } /** * cree les target species manquantes * * @param tx transaction */ protected void createTargetSpecies(TopiaContext tx) throws IOException { TargetSpeciesDAO tsDAO = IsisFishDAOHelper.getTargetSpeciesDAO(tx); MetierDAO metierDAO = IsisFishDAOHelper.getMetierDAO(tx); List metiers = metierDAO.findAll(); SpeciesDAO speciesDAO = IsisFishDAOHelper.getSpeciesDAO(tx); List species = speciesDAO.findAll(); for (Metier metier : metiers) { List msis = metier.getMetierSeasonInfo(); for (MetierSeasonInfo msi : msis) { for(Species sp :species){ if(msi.getSpeciesTargetSpecies(sp)==null){ TargetSpecies ts = tsDAO.create(); String form = "return 0;"; ts.getTargetFactorEquation().setContent(form); ts.setSpecies(sp) ; msi.addSpeciesTargetSpecies(ts) ; } } } } } /** * Modifie les target factors * * @param tx transaction */ protected void updateMetier(TopiaContext tx) throws IOException { MetierDAO metierDAO = IsisFishDAOHelper.getMetierDAO(tx); // load target factor table File fileTargetFactors = new File("INPUT/TargetFactor_Semantics.csv"); MatrixND matrixTargetFactors = MatrixFactory.getInstance().create(fileTargetFactors); List metiers = metierDAO.findAll(); for (Metier metier : metiers) { List msis = metier.getMetierSeasonInfo(); for (MetierSeasonInfo msi : msis) { int mo = msi.getFirstMonth().getMonthNumber(); Collection stsList = msi.getSpeciesTargetSpecies(); for (TargetSpecies ts : stsList) { double tf = matrixTargetFactors.getValue(metier,mo,ts); String eq = "return "+ tf + ";"; ts.getTargetFactorEquation().setContent(eq); } } } } /** * Point d'entrée du script. */ public static void main(String[] args) throws Exception { FillTargetFactors script = new FillTargetFactors(); script.run(); System.out.println("Done: " + new Date()); } }