Author: fdesbois Date: 2012-08-20 16:10:34 +0200 (Mon, 20 Aug 2012) New Revision: 444 Url: http://forge.codelutin.com/repositories/revision/sammoa/444 Log: some little improvements Added: trunk/sammoa-application/src/main/java/fr/ulr/sammoa/application/io/input/map/DbfImport.java Removed: trunk/sammoa-application/src/main/java/fr/ulr/sammoa/application/io/output/map/DbfImport.java Modified: trunk/sammoa-application/src/main/java/fr/ulr/sammoa/application/SammoaService.java trunk/sammoa-application/src/main/java/fr/ulr/sammoa/application/io/input/map/ImportMapService.java trunk/sammoa-application/src/main/java/fr/ulr/sammoa/application/io/input/map/ShpImporter.java trunk/sammoa-application/src/test/java/fr/ulr/sammoa/application/output/map/ExportMapServiceTest.java Modified: trunk/sammoa-application/src/main/java/fr/ulr/sammoa/application/SammoaService.java =================================================================== --- trunk/sammoa-application/src/main/java/fr/ulr/sammoa/application/SammoaService.java 2012-08-20 13:45:15 UTC (rev 443) +++ trunk/sammoa-application/src/main/java/fr/ulr/sammoa/application/SammoaService.java 2012-08-20 14:10:34 UTC (rev 444) @@ -26,7 +26,7 @@ import java.io.Closeable; /** - * Contract for any saommoa service. + * Contract for any sammoa service. * <p/> * This contract just allow to inject the {@link SammoaContext} in your service. * <p/> Copied: trunk/sammoa-application/src/main/java/fr/ulr/sammoa/application/io/input/map/DbfImport.java (from rev 443, trunk/sammoa-application/src/main/java/fr/ulr/sammoa/application/io/output/map/DbfImport.java) =================================================================== --- trunk/sammoa-application/src/main/java/fr/ulr/sammoa/application/io/input/map/DbfImport.java (rev 0) +++ trunk/sammoa-application/src/main/java/fr/ulr/sammoa/application/io/input/map/DbfImport.java 2012-08-20 14:10:34 UTC (rev 444) @@ -0,0 +1,129 @@ +package fr.ulr.sammoa.application.io.input.map; +/* + * #%L + * SAMMOA :: UI Swing + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2012 UMS 3462, Code Lutin + * %% + * 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 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 General Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/gpl-3.0.html>. + * #L% + */ + +import com.bbn.openmap.dataAccess.shape.DbfTableModel; +import com.google.common.base.Throwables; +import com.google.common.collect.Lists; +import org.nuiton.util.csv.Import; +import org.nuiton.util.csv.ImportModel; +import org.nuiton.util.csv.ImportRuntimeException; +import org.nuiton.util.csv.ImportableColumn; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; +import java.io.Reader; +import java.net.URL; +import java.util.Iterator; +import java.util.List; + +import static org.nuiton.i18n.I18n._; + +/** + * Created: 25/07/12 + * + * @author fdesbois <desbois@codelutin.com> + */ +public class DbfImport<E> extends Import<E> { + + private static final Logger logger = LoggerFactory.getLogger(DbfImport.class); + + protected DbfTableModel source; + + protected Iterator<List<Object>> records; + + protected List<Object> record; + + public DbfImport(ImportModel<E> model, URL fileUrl) { + // Use dummy reader for superclass Import that manage by default csv files + super(model, new Reader() { + + @Override + public int read(char[] cbuf, int off, int len) throws IOException { + return -1; + } + + @Override + public void close() throws IOException { + } + }); + + try { + // FIXME-fdesbois-2012-07-26 : there is an issue with encoding, don't know how to customize it + this.source = DbfTableModel.read(fileUrl); + + } catch (Exception e) { + throw Throwables.propagate(e); + } + this.records = source.getRecords(); + } + + @Override + protected <T> String readValue(ImportableColumn<E, T> field, int lineNumber) { + try { + + int columnNumber = source.getColumnIndexForName(field.getHeaderName()); + + String value = String.valueOf(record.get(columnNumber)); + + return value; + + } catch (Exception e) { + throw new ImportRuntimeException( + _("sammoa.dbf.import.error.unableToReadField", + field.getHeaderName(), lineNumber), e); + } + } + + @Override + public void close() { + // nothing to do + } + + @Override + protected boolean readRow() throws ImportRuntimeException { + boolean result; + if (records.hasNext()) { + record = records.next(); + if (logger.isTraceEnabled()) { + logger.trace("Read record {}", record); + } + result = true; + + } else { + result = false; + } + return result; + } + + @Override + protected String[] getHeaders() throws ImportRuntimeException { + List<String> list = Lists.newArrayList(); + for (int index = 0; index < source.getColumnCount(); index++) { + list.add(source.getColumnName(index)); + } + return list.toArray(new String[list.size()]); + } +} Property changes on: trunk/sammoa-application/src/main/java/fr/ulr/sammoa/application/io/input/map/DbfImport.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Modified: trunk/sammoa-application/src/main/java/fr/ulr/sammoa/application/io/input/map/ImportMapService.java =================================================================== --- trunk/sammoa-application/src/main/java/fr/ulr/sammoa/application/io/input/map/ImportMapService.java 2012-08-20 13:45:15 UTC (rev 443) +++ trunk/sammoa-application/src/main/java/fr/ulr/sammoa/application/io/input/map/ImportMapService.java 2012-08-20 14:10:34 UTC (rev 444) @@ -82,66 +82,16 @@ Maps.uniqueIndex(strates, Strates.toCode()); int result = 0; - int rowIndex = 0; + int index = 0; for (Transect transect : transects) { - // This is the link with the source shape file that contains graphic elements - int graphicIndex = rowIndex; + boolean isNew = importTransect(transectDAO, transect, strateMap, index); - rowIndex++; - - String strateCode = Strates.getCode(transect.getStrate()); - - // We check the strate, it must exist - // We can't do that in TransectImportModel during parsing because - // we need two params : sectorNumber and strateType to create the - // strateCode and check the existence. It must be done after parsing - Strate existStrate = strateMap.get(strateCode); - if (existStrate == null) { - throw new ImportRuntimeException(String.format( - "Unable to read line %1$d : The strate %2$s doesn't exist", - rowIndex, - strateCode) - ); + if (isNew) { + result++; } - Transect existTransect = transectDAO.findByNaturalId(transect.getName(), existStrate); - - // If the existing strate is null, create it otherwise ignore - if (existTransect == null) { - - Transect newTransect = transectDAO.createByNaturalId(transect.getName(), existStrate); - newTransect.setLength(transect.getLength()); - newTransect.setStartX(transect.getStartX()); - newTransect.setStartY(transect.getStartY()); - newTransect.setEndX(transect.getEndX()); - newTransect.setEndY(transect.getEndY()); - newTransect.setNbTimes(transect.getNbTimes()); - newTransect.setGraphicIndex(graphicIndex); - - result++; - - if (logger.isDebugEnabled()) { - logger.debug("Create new transect {}" + - "(graphicIndex = {})", - newTransect.getName(), - graphicIndex - ); - } - - } else { - - existTransect.setGraphicIndex(graphicIndex); - - if (logger.isWarnEnabled()) { - logger.warn("The transect {} already " + - "exists and will be ignored " + - "(graphicIndex = {})", - existTransect.getName(), - graphicIndex - ); - } - } + index++; } transaction.commitTransaction(); @@ -180,61 +130,142 @@ int result = 0; for (Strate strate : strates) { - StrateType strateType = strate.getStrateType(); - Sector sector = strate.getSector(); - int sectorNumber = sector.getSectorNumber(); + boolean isNew = importStrate(strateDAO, sectorDAO, strate, sectorMap, campaign); - Strate existStrate; + if (isNew) { + result++; + } + } - // Create sector if necessary - Sector existSector = sectorMap.get(sectorNumber); - if (existSector == null) { + transaction.commitTransaction(); - existSector = sectorDAO.createByNaturalId(sectorNumber, campaign); - existSector.setName(sector.getName()); - sectorMap.put(sectorNumber, existSector); + return result; + } catch (TopiaException e) { + throw new TopiaRuntimeException(e); - existStrate = null; + } finally { + endTransaction(transaction); + } + } - } else { + protected boolean importTransect(TransectDAO dao, + Transect transect, + Map<String, Strate> strates, + int rowIndex) throws TopiaException { - // Find existing strate only if sector is not created - existStrate = strateDAO.findByNaturalId(strateType, existSector); - } + boolean result = false; - // If the existing strate is null, create it otherwise ignore - if (existStrate == null) { + // This is the link with the source shape file that contains graphic elements + int graphicIndex = rowIndex; - Strate newStrate = strateDAO.createByNaturalId(strateType, existSector); - newStrate.setCode(Strates.getCode(newStrate)); - newStrate.setName(strate.getName()); + String strateCode = Strates.getCode(transect.getStrate()); - result++; + // We check the strate, it must exists + // We can't do that in TransectImportModel during parsing because + // we need two params : sectorNumber and strateType to create the + // strateCode and check the existence. It must be done after parsing + Strate existStrate = strates.get(strateCode); + if (existStrate == null) { + throw new ImportRuntimeException(String.format( + "Unable to read line %1$d : The strate %2$s doesn't exist", + rowIndex + 1, + strateCode) + ); + } - if (logger.isDebugEnabled()) { - logger.debug("Create new strate {}", - newStrate.getCode() - ); - } + Transect existTransect = dao.findByNaturalId(transect.getName(), existStrate); - } else { + // If the existing strate is null, create it otherwise ignore + if (existTransect == null) { - if (logger.isWarnEnabled()) { - logger.warn("The strate {} already " + - "exists and will be ignored", - existStrate.getCode()); - } - } + Transect newTransect = dao.createByNaturalId(transect.getName(), existStrate); + newTransect.setLength(transect.getLength()); + newTransect.setStartX(transect.getStartX()); + newTransect.setStartY(transect.getStartY()); + newTransect.setEndX(transect.getEndX()); + newTransect.setEndY(transect.getEndY()); + newTransect.setNbTimes(transect.getNbTimes()); + newTransect.setGraphicIndex(graphicIndex); + + result = true; + + if (logger.isDebugEnabled()) { + logger.debug("Create new transect {}" + + "(graphicIndex = {})", + newTransect.getName(), + graphicIndex + ); } - transaction.commitTransaction(); + } else { - return result; - } catch (TopiaException e) { - throw new TopiaRuntimeException(e); + existTransect.setGraphicIndex(graphicIndex); - } finally { - endTransaction(transaction); + if (logger.isWarnEnabled()) { + logger.warn("The transect {} already " + + "exists and will be ignored " + + "(graphicIndex = {})", + existTransect.getName(), + graphicIndex + ); + } } + return result; } + + protected boolean importStrate(StrateDAO strateDAO, + SectorDAO sectorDAO, + Strate strate, + Map<Integer, Sector> sectors, + Campaign campaign) throws TopiaException { + + boolean result = false; + + StrateType strateType = strate.getStrateType(); + Sector sector = strate.getSector(); + int sectorNumber = sector.getSectorNumber(); + + Strate existStrate; + + // Create sector if necessary + Sector existSector = sectors.get(sectorNumber); + if (existSector == null) { + + existSector = sectorDAO.createByNaturalId(sectorNumber, campaign); + existSector.setName(sector.getName()); + sectors.put(sectorNumber, existSector); + + existStrate = null; + + } else { + + // Find existing strate only if sector is not created + existStrate = strateDAO.findByNaturalId(strateType, existSector); + } + + // If the existing strate is null, create it otherwise ignore + if (existStrate == null) { + + Strate newStrate = strateDAO.createByNaturalId(strateType, existSector); + newStrate.setCode(Strates.getCode(newStrate)); + newStrate.setName(strate.getName()); + + result = true; + + if (logger.isDebugEnabled()) { + logger.debug("Create new strate {}", + newStrate.getCode() + ); + } + + } else { + + if (logger.isWarnEnabled()) { + logger.warn("The strate {} already " + + "exists and will be ignored", + existStrate.getCode()); + } + } + return result; + } } Modified: trunk/sammoa-application/src/main/java/fr/ulr/sammoa/application/io/input/map/ShpImporter.java =================================================================== --- trunk/sammoa-application/src/main/java/fr/ulr/sammoa/application/io/input/map/ShpImporter.java 2012-08-20 13:45:15 UTC (rev 443) +++ trunk/sammoa-application/src/main/java/fr/ulr/sammoa/application/io/input/map/ShpImporter.java 2012-08-20 14:10:34 UTC (rev 444) @@ -25,7 +25,6 @@ import com.google.common.io.Files; import fr.ulr.sammoa.application.io.CampaignStorage; -import fr.ulr.sammoa.application.io.output.map.DbfImport; import org.apache.commons.io.FileUtils; import org.nuiton.util.csv.ImportModel; import org.slf4j.Logger; Deleted: trunk/sammoa-application/src/main/java/fr/ulr/sammoa/application/io/output/map/DbfImport.java =================================================================== --- trunk/sammoa-application/src/main/java/fr/ulr/sammoa/application/io/output/map/DbfImport.java 2012-08-20 13:45:15 UTC (rev 443) +++ trunk/sammoa-application/src/main/java/fr/ulr/sammoa/application/io/output/map/DbfImport.java 2012-08-20 14:10:34 UTC (rev 444) @@ -1,129 +0,0 @@ -package fr.ulr.sammoa.application.io.output.map; -/* - * #%L - * SAMMOA :: UI Swing - * $Id$ - * $HeadURL$ - * %% - * Copyright (C) 2012 UMS 3462, Code Lutin - * %% - * 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 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 General Public - * License along with this program. If not, see - * <http://www.gnu.org/licenses/gpl-3.0.html>. - * #L% - */ - -import com.bbn.openmap.dataAccess.shape.DbfTableModel; -import com.google.common.base.Throwables; -import com.google.common.collect.Lists; -import org.nuiton.util.csv.Import; -import org.nuiton.util.csv.ImportModel; -import org.nuiton.util.csv.ImportRuntimeException; -import org.nuiton.util.csv.ImportableColumn; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.io.IOException; -import java.io.Reader; -import java.net.URL; -import java.util.Iterator; -import java.util.List; - -import static org.nuiton.i18n.I18n._; - -/** - * Created: 25/07/12 - * - * @author fdesbois <desbois@codelutin.com> - */ -public class DbfImport<E> extends Import<E> { - - private static final Logger logger = LoggerFactory.getLogger(DbfImport.class); - - protected DbfTableModel source; - - protected Iterator<List<Object>> records; - - protected List<Object> record; - - public DbfImport(ImportModel<E> model, URL fileUrl) { - // Use dummy reader for superclass Import that manage by default csv files - super(model, new Reader() { - - @Override - public int read(char[] cbuf, int off, int len) throws IOException { - return -1; - } - - @Override - public void close() throws IOException { - } - }); - - try { - // FIXME-fdesbois-2012-07-26 : there is an issue with encoding, don't know how to customize it - this.source = DbfTableModel.read(fileUrl); - - } catch (Exception e) { - throw Throwables.propagate(e); - } - this.records = source.getRecords(); - } - - @Override - protected <T> String readValue(ImportableColumn<E, T> field, int lineNumber) { - try { - - int columnNumber = source.getColumnIndexForName(field.getHeaderName()); - - String value = String.valueOf(record.get(columnNumber)); - - return value; - - } catch (Exception e) { - throw new ImportRuntimeException( - _("sammoa.dbf.import.error.unableToReadField", - field.getHeaderName(), lineNumber), e); - } - } - - @Override - public void close() { - // nothing to do - } - - @Override - protected boolean readRow() throws ImportRuntimeException { - boolean result; - if (records.hasNext()) { - record = records.next(); - if (logger.isTraceEnabled()) { - logger.trace("Read record {}", record); - } - result = true; - - } else { - result = false; - } - return result; - } - - @Override - protected String[] getHeaders() throws ImportRuntimeException { - List<String> list = Lists.newArrayList(); - for (int index = 0; index < source.getColumnCount(); index++) { - list.add(source.getColumnName(index)); - } - return list.toArray(new String[list.size()]); - } -} Modified: trunk/sammoa-application/src/test/java/fr/ulr/sammoa/application/output/map/ExportMapServiceTest.java =================================================================== --- trunk/sammoa-application/src/test/java/fr/ulr/sammoa/application/output/map/ExportMapServiceTest.java 2012-08-20 13:45:15 UTC (rev 443) +++ trunk/sammoa-application/src/test/java/fr/ulr/sammoa/application/output/map/ExportMapServiceTest.java 2012-08-20 14:10:34 UTC (rev 444) @@ -27,8 +27,8 @@ import fr.ulr.sammoa.application.FlightService; import fr.ulr.sammoa.application.SammoaConfig; import fr.ulr.sammoa.application.SammoaContext; +import fr.ulr.sammoa.application.io.input.map.DbfImport; import fr.ulr.sammoa.application.io.input.map.DoubleToIntegerValueParser; -import fr.ulr.sammoa.application.io.output.map.DbfImport; import fr.ulr.sammoa.application.io.output.map.DbfTableModelBuilder; import fr.ulr.sammoa.application.io.output.map.ExportMapModel; import fr.ulr.sammoa.application.io.output.map.ExportMapService;