Author: bleny Date: 2014-02-28 16:24:50 +0100 (Fri, 28 Feb 2014) New Revision: 1700 Url: http://codelutin.com/projects/wao/repository/revisions/1700 Log: import BoatService import methods in a new class, will be merged in ReferentialService next commit Added: trunk/wao-services/src/main/java/fr/ifremer/wao/services/service/LegacyBoatsServiceMethodsToMergeInReferentialService.java Copied: trunk/wao-services/src/main/java/fr/ifremer/wao/services/service/LegacyBoatsServiceMethodsToMergeInReferentialService.java (from rev 1698, tags/wao-3.4.1/wao-business/src/main/java/fr/ifremer/wao/service/ServiceBoatImpl.java) =================================================================== --- trunk/wao-services/src/main/java/fr/ifremer/wao/services/service/LegacyBoatsServiceMethodsToMergeInReferentialService.java (rev 0) +++ trunk/wao-services/src/main/java/fr/ifremer/wao/services/service/LegacyBoatsServiceMethodsToMergeInReferentialService.java 2014-02-28 15:24:50 UTC (rev 1700) @@ -0,0 +1,202 @@ +/* + * #%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(); + + } + +} Property changes on: trunk/wao-services/src/main/java/fr/ifremer/wao/services/service/LegacyBoatsServiceMethodsToMergeInReferentialService.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:mergeinfo + /branches/wao-1.5.x/wao-business/src/main/java/fr/ifremer/wao/service/ServiceBoatImpl.java:679-733
participants (1)
-
bleny@users.forge.codelutin.com