Author: bleny Date: 2014-02-28 16:26:57 +0100 (Fri, 28 Feb 2014) New Revision: 1701 Url: http://codelutin.com/projects/wao/repository/revisions/1701 Log: refs #4482 delete LegacyBoatsServiceMethodsToMergeInReferentialService and integrate content in ReferentialService Removed: trunk/wao-services/src/main/java/fr/ifremer/wao/services/service/LegacyBoatsServiceMethodsToMergeInReferentialService.java Modified: trunk/wao-services/src/main/java/fr/ifremer/wao/services/service/ReferentialService.java Deleted: trunk/wao-services/src/main/java/fr/ifremer/wao/services/service/LegacyBoatsServiceMethodsToMergeInReferentialService.java =================================================================== --- trunk/wao-services/src/main/java/fr/ifremer/wao/services/service/LegacyBoatsServiceMethodsToMergeInReferentialService.java 2014-02-28 15:24:50 UTC (rev 1700) +++ trunk/wao-services/src/main/java/fr/ifremer/wao/services/service/LegacyBoatsServiceMethodsToMergeInReferentialService.java 2014-02-28 15:26:57 UTC (rev 1701) @@ -1,202 +0,0 @@ -/* - * #%L - * Wao :: Business - * - * $Id$ - * $HeadURL$ - * %% - * Copyright (C) 2009 - 2010 Ifremer - * %% - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 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 Affero General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * #L% - */ - -package fr.ifremer.wao.services.service; - -import com.google.common.collect.Maps; -import fr.ifremer.wao.entity.Boat; -import fr.ifremer.wao.entity.BoatGroup; -import fr.ifremer.wao.entity.BoatGroupTopiaDao; -import fr.ifremer.wao.entity.BoatGroups; -import fr.ifremer.wao.entity.BoatTopiaDao; -import fr.ifremer.wao.entity.Fleet; -import fr.ifremer.wao.entity.FleetTopiaDao; -import fr.ifremer.wao.entity.Fleets; -import fr.ifremer.wao.entity.LocationType; -import fr.ifremer.wao.entity.ShipOwner; -import fr.ifremer.wao.entity.ShipOwnerTopiaDao; -import fr.ifremer.wao.entity.ShipOwners; -import fr.ifremer.wao.entity.TerrestrialLocation; -import fr.ifremer.wao.entity.TerrestrialLocationTopiaDao; -import fr.ifremer.wao.services.service.csv.BoatGroupImportModel; -import fr.ifremer.wao.services.service.csv.BoatImportExportModel; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.nuiton.csv.Import; -import org.nuiton.csv.ImportRuntimeException; -import org.nuiton.i18n.I18n; - -import java.io.InputStream; -import java.util.List; -import java.util.Map; - - -public class LegacyBoatsServiceMethodsToMergeInReferentialService extends WaoServiceSupport { - - private static final Log log = LogFactory.getLog(LegacyBoatsServiceMethodsToMergeInReferentialService.class); - - /** - * Import boats from CSV file, given data will be merged with data already - * in the DB. All shipowner or district code mentioned will be added to - * the database on the fly - */ - public void importBoats(InputStream input) throws ImportErrorException { - - // Before importing, we need to retrieve all ports that may be mentioned - // in the CSV file - TerrestrialLocationTopiaDao terrestrialLocationDao = - getPersistenceContext().getTerrestrialLocationDao(); - List<TerrestrialLocation> ports = terrestrialLocationDao.forLocationTypeEquals(LocationType.PORT).findAll(); - List<TerrestrialLocation> districts = terrestrialLocationDao.forLocationTypeEquals(LocationType.DISTRICT).findAll(); - - // Building the model, providing ports - BoatImportExportModel model = new BoatImportExportModel(ports, districts); - Import<Boat> boatImport = Import.newImport(model, input); - - // Getting some DAOs - BoatTopiaDao boatDao = getPersistenceContext().getBoatDao(); - ShipOwnerTopiaDao shipOwnerDao = getPersistenceContext().getShipOwnerDao(); - FleetTopiaDao fleetDao = getPersistenceContext().getFleetDao(); - - // load data that will be linked to the boat - Map<String, ShipOwner> shipOwners = - Maps.uniqueIndex( - shipOwnerDao.findAll(), - ShipOwners.getCode()); - Map<String, Fleet> fleets = - Maps.uniqueIndex( - fleetDao.findAll(), - Fleets.getCode()); - - // now iterating over the lines of the CSV - try { - for (Boat boat : boatImport) { - - // first each boat read, merge with data already in database or add - // all new boats encountered. New district codes or shipowners may be - // added on the fly too. - - // first, find and reuse existing ship owner - String shipOwnerCode = boat.getShipOwner().getCode(); - ShipOwner shipOwner = shipOwners.get(shipOwnerCode); - if (shipOwner == null) { - shipOwner = shipOwnerDao.create(boat.getShipOwner()); - shipOwners.put(shipOwnerCode, shipOwner); - } - boat.setShipOwner(shipOwner); - - // secondly, find and reuse fleet - String fleetCode = boat.getFleet().getCode(); - if (fleetCode == null) { - boat.setFleet(null); - } else { - Fleet fleet = fleets.get(fleetCode); - if (fleet == null) { - fleet = fleetDao.create(boat.getFleet()); - fleets.put(fleetCode, fleet); - } - boat.setFleet(fleet); - } - - // now boat object is ready - Boat existingBoat = boatDao.forImmatriculationEquals(boat.getImmatriculation()).findUniqueOrNull(); - if (existingBoat == null) { - boatDao.create(boat); - } else { - // copy new values in found entity and save changes - existingBoat.setName(boat.getName()); - existingBoat.setBoatLength(boat.getBoatLength()); - existingBoat.setBuildYear(boat.getBuildYear()); - existingBoat.setActive(boat.isActive()); - existingBoat.setShipOwner(boat.getShipOwner()); - existingBoat.setDistrict(boat.getDistrict()); - existingBoat.setPortOfRegistry(boat.getPortOfRegistry()); - existingBoat.setFleet(boat.getFleet()); - boatDao.update(existingBoat); - } - - } - } catch (ImportRuntimeException e) { - throw new ImportErrorException(e); - } - - getReferentialService().updateReferentialMeta(Boat.class.getName()); - - commit(); - - } - - public void importBoatGroups(InputStream input) throws ImportErrorException { - // Building the model, providing ports - BoatGroupImportModel model = new BoatGroupImportModel(); - Import<Boat> boatImport = Import.newImport(model, input); - - // Getting some DAOs - BoatTopiaDao boatDao = getPersistenceContext().getBoatDao(); - BoatGroupTopiaDao boatGroupDAO = getPersistenceContext().getBoatGroupDao(); - - Map<String, BoatGroup> boatGroups = - Maps.uniqueIndex( - boatGroupDAO.findAll(), - BoatGroups.getCode()); - - // now iterating over the lines of the CSV - try { - for (Boat boat : boatImport) { - - // fourth, the group - String boatGroupCode = boat.getBoatGroup().getCode(); - if (boatGroupCode == null) { - boat.setBoatGroup(null); - } else { - BoatGroup boatGroup = boatGroups.get(boatGroupCode); - if (boatGroup == null) { - boatGroup = boatGroupDAO.create(boat.getBoatGroup()); - boatGroups.put(boatGroupCode, boatGroup); - } - boat.setBoatGroup(boatGroup); - } - - // now boat object is ready - Boat existingBoat = boatDao.forImmatriculationEquals(boat.getImmatriculation()).findUniqueOrNull(); - if (existingBoat == null) { - throw new ImportRuntimeException(I18n.t("wao.import.contact.failure.wrongBoat", String.valueOf(boat.getImmatriculation()))); - } else { - // copy new values in found entity and save changes - existingBoat.setBoatGroup(boat.getBoatGroup()); - boatDao.update(existingBoat); - } - - } - } catch (ImportRuntimeException e) { - throw new ImportErrorException(e); - } - - getReferentialService().updateReferentialMeta(Boat.class.getName()); - - commit(); - - } - -} Modified: trunk/wao-services/src/main/java/fr/ifremer/wao/services/service/ReferentialService.java =================================================================== --- trunk/wao-services/src/main/java/fr/ifremer/wao/services/service/ReferentialService.java 2014-02-28 15:24:50 UTC (rev 1700) +++ trunk/wao-services/src/main/java/fr/ifremer/wao/services/service/ReferentialService.java 2014-02-28 15:26:57 UTC (rev 1701) @@ -26,9 +26,13 @@ import com.google.common.base.Preconditions; import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Maps; import fr.ifremer.wao.WaoTechnicalException; import fr.ifremer.wao.entity.Boat; import fr.ifremer.wao.entity.BoatGroup; +import fr.ifremer.wao.entity.BoatGroupTopiaDao; +import fr.ifremer.wao.entity.BoatGroups; +import fr.ifremer.wao.entity.BoatTopiaDao; import fr.ifremer.wao.entity.ContactState; import fr.ifremer.wao.entity.ContactStateMotif; import fr.ifremer.wao.entity.ContactStateMotifTopiaDao; @@ -37,6 +41,9 @@ import fr.ifremer.wao.entity.FishingGearDCFTopiaDao; import fr.ifremer.wao.entity.FishingZone; import fr.ifremer.wao.entity.FishingZoneTopiaDao; +import fr.ifremer.wao.entity.Fleet; +import fr.ifremer.wao.entity.FleetTopiaDao; +import fr.ifremer.wao.entity.Fleets; import fr.ifremer.wao.entity.LocationType; import fr.ifremer.wao.entity.ObsDebCode; import fr.ifremer.wao.entity.ObsDebCodeDetails; @@ -50,6 +57,9 @@ import fr.ifremer.wao.entity.ReferentialMetaTopiaDao; import fr.ifremer.wao.entity.SampleRow; import fr.ifremer.wao.entity.SampleRowTopiaDao; +import fr.ifremer.wao.entity.ShipOwner; +import fr.ifremer.wao.entity.ShipOwnerTopiaDao; +import fr.ifremer.wao.entity.ShipOwners; import fr.ifremer.wao.entity.TargetSpeciesDCF; import fr.ifremer.wao.entity.TargetSpeciesDCFTopiaDao; import fr.ifremer.wao.entity.TerrestrialDivision; @@ -58,6 +68,8 @@ import fr.ifremer.wao.entity.TerrestrialLocation; import fr.ifremer.wao.entity.TerrestrialLocationImpl; import fr.ifremer.wao.entity.TerrestrialLocationTopiaDao; +import fr.ifremer.wao.services.service.csv.BoatGroupImportModel; +import fr.ifremer.wao.services.service.csv.BoatImportExportModel; import fr.ifremer.wao.services.service.csv.ContactStateMotivesImportModel; import fr.ifremer.wao.services.service.csv.FishingZoneImportModel; import fr.ifremer.wao.services.service.csv.RawObsDebCode; @@ -821,4 +833,147 @@ return all; } + /** + * Import boats from CSV file, given data will be merged with data already + * in the DB. All shipowner or district code mentioned will be added to + * the database on the fly + */ + public void importBoats(InputStream input) throws ImportErrorException { + + // Before importing, we need to retrieve all ports that may be mentioned + // in the CSV file + TerrestrialLocationTopiaDao terrestrialLocationDao = + getPersistenceContext().getTerrestrialLocationDao(); + List<TerrestrialLocation> ports = terrestrialLocationDao.forLocationTypeEquals(LocationType.PORT).findAll(); + List<TerrestrialLocation> districts = terrestrialLocationDao.forLocationTypeEquals(LocationType.DISTRICT).findAll(); + + // Building the model, providing ports + BoatImportExportModel model = new BoatImportExportModel(ports, districts); + Import<Boat> boatImport = Import.newImport(model, input); + + // Getting some DAOs + BoatTopiaDao boatDao = getPersistenceContext().getBoatDao(); + ShipOwnerTopiaDao shipOwnerDao = getPersistenceContext().getShipOwnerDao(); + FleetTopiaDao fleetDao = getPersistenceContext().getFleetDao(); + + // load data that will be linked to the boat + Map<String, ShipOwner> shipOwners = + Maps.uniqueIndex( + shipOwnerDao.findAll(), + ShipOwners.getCode()); + Map<String, Fleet> fleets = + Maps.uniqueIndex( + fleetDao.findAll(), + Fleets.getCode()); + + // now iterating over the lines of the CSV + try { + for (Boat boat : boatImport) { + + // first each boat read, merge with data already in database or add + // all new boats encountered. New district codes or shipowners may be + // added on the fly too. + + // first, find and reuse existing ship owner + String shipOwnerCode = boat.getShipOwner().getCode(); + ShipOwner shipOwner = shipOwners.get(shipOwnerCode); + if (shipOwner == null) { + shipOwner = shipOwnerDao.create(boat.getShipOwner()); + shipOwners.put(shipOwnerCode, shipOwner); + } + boat.setShipOwner(shipOwner); + + // secondly, find and reuse fleet + String fleetCode = boat.getFleet().getCode(); + if (fleetCode == null) { + boat.setFleet(null); + } else { + Fleet fleet = fleets.get(fleetCode); + if (fleet == null) { + fleet = fleetDao.create(boat.getFleet()); + fleets.put(fleetCode, fleet); + } + boat.setFleet(fleet); + } + + // now boat object is ready + Boat existingBoat = boatDao.forImmatriculationEquals(boat.getImmatriculation()).findUniqueOrNull(); + if (existingBoat == null) { + boatDao.create(boat); + } else { + // copy new values in found entity and save changes + existingBoat.setName(boat.getName()); + existingBoat.setBoatLength(boat.getBoatLength()); + existingBoat.setBuildYear(boat.getBuildYear()); + existingBoat.setActive(boat.isActive()); + existingBoat.setShipOwner(boat.getShipOwner()); + existingBoat.setDistrict(boat.getDistrict()); + existingBoat.setPortOfRegistry(boat.getPortOfRegistry()); + existingBoat.setFleet(boat.getFleet()); + boatDao.update(existingBoat); + } + + } + } catch (ImportRuntimeException e) { + throw new ImportErrorException(e); + } + + updateReferentialMeta(Boat.class.getName()); + + commit(); + + } + + public void importBoatGroups(InputStream input) throws ImportErrorException { + // Building the model, providing ports + BoatGroupImportModel model = new BoatGroupImportModel(); + Import<Boat> boatImport = Import.newImport(model, input); + + // Getting some DAOs + BoatTopiaDao boatDao = getPersistenceContext().getBoatDao(); + BoatGroupTopiaDao boatGroupDAO = getPersistenceContext().getBoatGroupDao(); + + Map<String, BoatGroup> boatGroups = + Maps.uniqueIndex( + boatGroupDAO.findAll(), + BoatGroups.getCode()); + + // now iterating over the lines of the CSV + try { + for (Boat boat : boatImport) { + + // fourth, the group + String boatGroupCode = boat.getBoatGroup().getCode(); + if (boatGroupCode == null) { + boat.setBoatGroup(null); + } else { + BoatGroup boatGroup = boatGroups.get(boatGroupCode); + if (boatGroup == null) { + boatGroup = boatGroupDAO.create(boat.getBoatGroup()); + boatGroups.put(boatGroupCode, boatGroup); + } + boat.setBoatGroup(boatGroup); + } + + // now boat object is ready + Boat existingBoat = boatDao.forImmatriculationEquals(boat.getImmatriculation()).findUniqueOrNull(); + if (existingBoat == null) { + throw new ImportRuntimeException(I18n.t("wao.import.contact.failure.wrongBoat", String.valueOf(boat.getImmatriculation()))); + } else { + // copy new values in found entity and save changes + existingBoat.setBoatGroup(boat.getBoatGroup()); + boatDao.update(existingBoat); + } + + } + } catch (ImportRuntimeException e) { + throw new ImportErrorException(e); + } + + updateReferentialMeta(Boat.class.getName()); + + commit(); + + } + }