Author: tchemit Date: 2013-04-05 16:34:01 +0200 (Fri, 05 Apr 2013) New Revision: 741 Url: http://forge.codelutin.com/projects/tutti/repository/revisions/741 Log: refs #1875: [IMP/EXP] - Export des donn?\195?\169es de campagne : format g?\195?\169n?\195?\169rique (debut) Added: trunk/tutti-service/src/main/java/fr/ifremer/tutti/service/export/ trunk/tutti-service/src/main/java/fr/ifremer/tutti/service/export/SurveyExportModel.java trunk/tutti-service/src/main/java/fr/ifremer/tutti/service/export/SurveyExportRow.java trunk/tutti-service/src/main/java/fr/ifremer/tutti/service/export/TuttiExportService.java Modified: trunk/tutti-service/src/main/java/fr/ifremer/tutti/service/TuttiCsvUtil.java Modified: trunk/tutti-service/src/main/java/fr/ifremer/tutti/service/TuttiCsvUtil.java =================================================================== --- trunk/tutti-service/src/main/java/fr/ifremer/tutti/service/TuttiCsvUtil.java 2013-04-05 14:19:05 UTC (rev 740) +++ trunk/tutti-service/src/main/java/fr/ifremer/tutti/service/TuttiCsvUtil.java 2013-04-05 14:34:01 UTC (rev 741) @@ -31,6 +31,7 @@ import org.nuiton.util.csv.ext.AbstractImportExportModel; import java.text.ParseException; +import java.util.Date; import java.util.Map; import java.util.Set; @@ -40,7 +41,7 @@ * @author tchemit <chemit@codelutin.com> * @since 1.0 */ -public class TuttiCsvUtil { +public class TuttiCsvUtil extends Common { public static abstract class AbstractTuttiImportExportModel<M> extends AbstractImportExportModel<M> { @@ -179,6 +180,15 @@ } } + public static final ValueParserFormatter<Date> YEAR = + new DateValue("yyyy"); + + public static final ValueParserFormatter<Date> DAY_EMPTY_TIME = + new DateValue("dd/MM/yyyy 00:00:00"); + + public static final ValueParserFormatter<Date> TIME = + new DateValue("HH:mm:ss"); + protected TuttiCsvUtil() { // no instance } Added: trunk/tutti-service/src/main/java/fr/ifremer/tutti/service/export/SurveyExportModel.java =================================================================== --- trunk/tutti-service/src/main/java/fr/ifremer/tutti/service/export/SurveyExportModel.java (rev 0) +++ trunk/tutti-service/src/main/java/fr/ifremer/tutti/service/export/SurveyExportModel.java 2013-04-05 14:34:01 UTC (rev 741) @@ -0,0 +1,65 @@ +package fr.ifremer.tutti.service.export; + +/* + * #%L + * Tutti :: Service + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2012 - 2013 Ifremer + * %% + * 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 fr.ifremer.tutti.persistence.entities.data.Cruise; +import fr.ifremer.tutti.service.TuttiCsvUtil; + +/** + * Model of a survey export. + * + * @author tchemit <chemit@codelutin.com> + * @since 1.3 + */ +public class SurveyExportModel extends TuttiCsvUtil.AbstractTuttiImportExportModel<SurveyExportRow> { + + public SurveyExportModel(char separator) { + super(separator); + + newColumnForExport("Annee", Cruise.PROPERTY_BEGIN_DATE, TuttiCsvUtil.YEAR); + newColumnForExport("Serie", Cruise.PROPERTY_PROGRAM); + newColumnForExport("Serie_Partielle", Cruise.PROPERTY_SURVEY_PART); + newColumnForExport("Navire", Cruise.PROPERTY_VESSEL); +//FIXME newColumnForExport("Pays", Cruise.PROPERTY_VESSEL); + newColumnForExport("Zone_Etude", "programeZoneName"); + newColumnForExport("Id_Sismer", Cruise.PROPERTY_NAME); + newColumnForExport("Date_Deb_Campagne", Cruise.PROPERTY_BEGIN_DATE, TuttiCsvUtil.DAY_EMPTY_TIME); + newColumnForExport("Heure_Deb_Campagne", Cruise.PROPERTY_BEGIN_DATE, TuttiCsvUtil.TIME); + newColumnForExport("Port_Deb_Campagne", Cruise.PROPERTY_DEPARTURE_LOCATION); + newColumnForExport("Date_Fin_Campagne", Cruise.PROPERTY_END_DATE, TuttiCsvUtil.DAY_EMPTY_TIME); + newColumnForExport("Heure_Fin_Campagne", Cruise.PROPERTY_END_DATE, TuttiCsvUtil.TIME); + newColumnForExport("Port_Fin_Campagne", Cruise.PROPERTY_RETURN_LOCATION); +//FIXME newColumnForExport("Responsable_Serie", Cruise.PROPERTY_HEAD_OF_MISSION); + newColumnForExport("Chef_Mission", Cruise.PROPERTY_HEAD_OF_MISSION); + newColumnForExport("Resp_Salle_Tri", Cruise.PROPERTY_HEAD_OF_SORT_ROOM); + newColumnForExport("Commentaire", Cruise.PROPERTY_COMMENT); + } + + @Override + public SurveyExportRow newEmptyInstance() { + return new SurveyExportRow(); + } + +} \ No newline at end of file Property changes on: trunk/tutti-service/src/main/java/fr/ifremer/tutti/service/export/SurveyExportModel.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Added: trunk/tutti-service/src/main/java/fr/ifremer/tutti/service/export/SurveyExportRow.java =================================================================== --- trunk/tutti-service/src/main/java/fr/ifremer/tutti/service/export/SurveyExportRow.java (rev 0) +++ trunk/tutti-service/src/main/java/fr/ifremer/tutti/service/export/SurveyExportRow.java 2013-04-05 14:34:01 UTC (rev 741) @@ -0,0 +1,103 @@ +package fr.ifremer.tutti.service.export; + +/* + * #%L + * Tutti :: Service + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2012 - 2013 Ifremer + * %% + * 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 fr.ifremer.tutti.persistence.entities.data.Cruise; +import fr.ifremer.tutti.persistence.entities.referential.Person; + +import java.io.Serializable; +import java.util.Date; +import java.util.List; + +/** + * A row in a survey export. + * + * @author tchemit <chemit@codelutin.com> + * @since 1.3 + */ +public class SurveyExportRow implements Serializable { + + private static final long serialVersionUID = 1L; + + protected Cruise cruise; + + public SurveyExportRow(Cruise cruise) { + this.cruise = cruise; + } + + public Date getBeginDate() { + return cruise.getBeginDate(); + } + + public String getProgram() { + return cruise.getProgram().getName(); + } + + public String getSurveyPart() { + return cruise.getSurveyPart(); + } + + public String getProgramZone() { + return cruise.getProgram().getZone().getName(); + } + + public String getVessel() { + return cruise.getVessel().getName(); + } + + public String getCountry() { + //FIXME + return null; + } + + public String getName() { + //FIXME + return cruise.getName(); + } + + public String getDepartureLocation() { + return cruise.getDepartureLocation().getName(); + } + + public Date getEndDate() { + return cruise.getEndDate(); + } + + public String getReturnLocation() { + return cruise.getReturnLocation().getName(); + } + + public String getComment() { + return cruise.getComment(); + } + + public List<Person> getHeadOfMission() { + return cruise.getHeadOfMission(); + } + + public List<Person> getHeadOfSortRoom() { + return cruise.getHeadOfSortRoom(); + } +} Property changes on: trunk/tutti-service/src/main/java/fr/ifremer/tutti/service/export/SurveyExportRow.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Added: trunk/tutti-service/src/main/java/fr/ifremer/tutti/service/export/TuttiExportService.java =================================================================== --- trunk/tutti-service/src/main/java/fr/ifremer/tutti/service/export/TuttiExportService.java (rev 0) +++ trunk/tutti-service/src/main/java/fr/ifremer/tutti/service/export/TuttiExportService.java 2013-04-05 14:34:01 UTC (rev 741) @@ -0,0 +1,252 @@ +package fr.ifremer.tutti.service.export; + +/* + * #%L + * Tutti :: Service + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2012 - 2013 Ifremer + * %% + * 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.google.common.base.Charsets; +import com.google.common.base.Preconditions; +import com.google.common.collect.Lists; +import com.google.common.io.Files; +import fr.ifremer.tutti.TuttiIOUtil; +import fr.ifremer.tutti.TuttiTechnicalException; +import fr.ifremer.tutti.persistence.entities.data.Cruise; +import fr.ifremer.tutti.persistence.entities.data.FishingOperation; +import fr.ifremer.tutti.service.AbstractTuttiService; +import fr.ifremer.tutti.service.PersistenceService; +import fr.ifremer.tutti.service.TuttiServiceContext; +import org.apache.commons.io.IOUtils; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.nuiton.util.csv.Export; +import org.nuiton.util.csv.ExportModel; +import org.nuiton.util.csv.ext.RepeatableExport; + +import java.io.BufferedWriter; +import java.io.Closeable; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.Writer; +import java.util.Collections; +import java.util.List; + +/** + * To export data in the generic format. + * <p/> + * See http://forge.codelutin.com/issues/1875. + * + * @author tchemit <chemit@codelutin.com> + * @since 1.3 + */ +public class TuttiExportService extends AbstractTuttiService { + + private static final Log log = + LogFactory.getLog(TuttiExportService.class); + + protected PersistenceService persistenceService; + + @Override + public void setServiceContext(TuttiServiceContext context) { + super.setServiceContext(context); + persistenceService = getService(PersistenceService.class); + } + + public void exportCruise(String cruiseId, File exportFile) { + + Preconditions.checkNotNull(cruiseId); + Preconditions.checkNotNull(exportFile); + + Cruise cruise = persistenceService.getCruise(cruiseId); + Preconditions.checkNotNull(cruise); + + File basedir = context.getConfig().newTempFile("exportCruise-" + cruiseId); + + File surveyFile = new File(basedir, "survey.csv"); + exportSurvey(surveyFile, cruise); + + File parameterFile = new File(basedir, "parameter.csv"); + File operationFile = new File(basedir, "operation.csv"); + File catchesFile = new File(basedir, "catch.csv"); + + SurveyExportModel operationExportModel = null; + SurveyExportModel parameterExportModel = null; + SurveyExportModel catchExportModel = null; + + OperationWriter operationWriter = null; + + try { + operationWriter = new OperationWriter( + operationFile, + parameterFile, + catchesFile, + operationExportModel, + parameterExportModel, + catchExportModel); + + List<FishingOperation> operations = + persistenceService.getAllFishingOperation(cruiseId); + + for (FishingOperation operation : operations) { + exportOperation(operationWriter, cruise, operation); + } + operationWriter.close(); + } catch (Exception e) { + throw new TuttiTechnicalException( + "Could not export operations", e); + } finally { + IOUtils.closeQuietly(operationWriter); + } + TuttiIOUtil.zip(basedir, exportFile, "Could not create export zip"); + } + + protected void exportSurvey(File file, Cruise cruise) { + + if (log.isInfoEnabled()) { + log.info("Will export survey to file: " + file); + } + + SurveyExportModel csvModel = + new SurveyExportModel(context.getConfig().getCsvSeparator()); + + SurveyExportRow row = new SurveyExportRow(cruise); + List<SurveyExportRow> rows = Lists.newArrayList((row)); + + BufferedWriter writer = null; + try { + writer = Files.newWriter(file, Charsets.UTF_8); + + Export export = Export.newExport(csvModel, rows); + export.write(writer); + writer.close(); + } catch (Exception e) { + throw new TuttiTechnicalException( + "Could not export survey to file: " + file, e); + } finally { + IOUtils.closeQuietly(writer); + } + } + + protected void exportOperation(OperationWriter operationWriter, + Cruise cruise, + FishingOperation operation) throws Exception { + + if (log.isInfoEnabled()) { + log.info("Will export operation: " + operation.getId()); + } + + operationWriter.exportOperation(cruise, operation); + operationWriter.exportParameter(cruise, operation); + operationWriter.exportCatch(cruise, operation); + } + + + class OperationWriter implements Closeable { + + final BufferedWriter operationWriter; + + final BufferedWriter parameterWriter; + + final BufferedWriter catchWriter; + + final TuttiRepeatableExport<SurveyExportRow> operationExporter; + + final TuttiRepeatableExport<SurveyExportRow> parameterExporter; + + final TuttiRepeatableExport<SurveyExportRow> catchExporter; + + OperationWriter(File operationFile, + File parameterFile, + File catchesFile, + SurveyExportModel operationExportModel, + SurveyExportModel parameterExportModel, + SurveyExportModel catchExportModel) { + + try { + + operationWriter = Files.newWriter(operationFile, Charsets.UTF_8); + parameterWriter = Files.newWriter(parameterFile, Charsets.UTF_8); + catchWriter = Files.newWriter(catchesFile, Charsets.UTF_8); + } catch (FileNotFoundException e) { + throw new TuttiTechnicalException(e); + } + operationExporter = new TuttiRepeatableExport<SurveyExportRow>(operationExportModel); + parameterExporter = new TuttiRepeatableExport<SurveyExportRow>(parameterExportModel); + catchExporter = new TuttiRepeatableExport<SurveyExportRow>(catchExportModel); + } + + void exportOperation(Cruise cruise, FishingOperation operation) throws Exception { + + SurveyExportRow row = new SurveyExportRow(cruise); + List<SurveyExportRow> rows = Lists.newArrayList((row)); + operationExporter.write(rows, operationWriter); + } + + void exportParameter(Cruise cruise, FishingOperation operation) throws Exception { + + SurveyExportRow row = new SurveyExportRow(cruise); + List<SurveyExportRow> rows = Lists.newArrayList((row)); + parameterExporter.write(rows, parameterWriter); + } + + void exportCatch(Cruise cruise, FishingOperation operation) throws Exception { + SurveyExportRow row = new SurveyExportRow(cruise); + List<SurveyExportRow> rows = Lists.newArrayList((row)); + catchExporter.write(rows, catchWriter); + } + + @Override + public void close() throws IOException { + try { + if (operationWriter != null) { + operationWriter.close(); + } + } finally { + + try { + if (parameterWriter != null) { + parameterWriter.close(); + } + } catch (IOException e) { + + if (catchWriter != null) { + catchWriter.close(); + } + } + } + } + } + + class TuttiRepeatableExport<E> extends RepeatableExport<E> { + + public TuttiRepeatableExport(ExportModel<E> model) { + super(model, Collections.<E>emptyList(), true); + } + + public void write(Iterable<E> data, Writer writer) throws Exception { + this.data = data; + write(writer); + } + } + +} Property changes on: trunk/tutti-service/src/main/java/fr/ifremer/tutti/service/export/TuttiExportService.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native