This is an automated email from the git hooks/post-receive script. New commit to branch develop in repository tutti. See http://git.codelutin.com/tutti.git commit 78a89c71bbec3d708938460948ae7cfdd6d4e68c Author: Tony CHEMIT <chemit@codelutin.com> Date: Wed Feb 11 17:59:33 2015 +0100 introduce GenericFormatArchive object --- .../genericformat/GenericFormatArchive.java | 282 +++++++++++++++++++++ 1 file changed, 282 insertions(+) diff --git a/tutti-service/src/main/java/fr/ifremer/tutti/service/genericformat/GenericFormatArchive.java b/tutti-service/src/main/java/fr/ifremer/tutti/service/genericformat/GenericFormatArchive.java new file mode 100644 index 0000000..0382bcd --- /dev/null +++ b/tutti-service/src/main/java/fr/ifremer/tutti/service/genericformat/GenericFormatArchive.java @@ -0,0 +1,282 @@ +package fr.ifremer.tutti.service.genericformat; + +import com.google.common.base.Joiner; +import fr.ifremer.tutti.persistence.ProgressionModel; +import org.nuiton.jaxx.application.ApplicationBusinessException; +import org.nuiton.jaxx.application.ApplicationIOUtil; +import org.nuiton.jaxx.application.ApplicationTechnicalException; + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.List; +import java.util.zip.ZipEntry; +import java.util.zip.ZipFile; + +import static org.nuiton.i18n.I18n.t; + +/** + * Created on 2/11/15. + * + * @author Tony Chemit - chemit@codelutin.com + * @since 3.14 + */ +public class GenericFormatArchive { + + private static enum ArchiveMode { + IMPORT, + EXPORT + } + + private static enum ArchiveFilePath { + + PROTOCOL(false, "protocol.tuttiProtocol"), + REFERENTIAL_GEAR(false, "temporaryGears.csv"), + REFERENTIAL_PERSON(false, "temporaryPersons.csv"), + REFERENTIAL_SPECIES(false, "temporarySpecies.csv"), + REFERENTIAL_VESSEL(false, "temporaryVessels.csv"), + SAMPLE_CATEGORY(true, "sampleCategory.csv"), + DATA_SURVEY(true, "survey.csv"), + DATA_GEAR_CARACTERISTIC(true, "gearCaracteristics.csv"), + DATA_OPERATION(true, "operation.csv"), + DATA_PARAMETER(true, "parameter.csv"), + DATA_CATCH(true, "catch.csv"), + DATA_SPECIES(true, "species.csv"), + DATA_MARINE_LITTER(true, "marineLitter.csv"), + DATA_ACCIDENTAL_CATCH(true, "accidentalCatch.csv"), + DATA_INDIVIDUAL_OBSERVATION(true, "individualObservation.csv"); + + private final boolean mandatory; + + private final String filename; + + ArchiveFilePath(boolean mandatory, String filename) { + this.mandatory = mandatory; + this.filename = filename; + } + + public boolean isMandatory() { + return mandatory; + } + + public String getFilename() { + return filename; + } + + public ZipEntry getzipentry(ZipFile zipFile) { + ZipEntry entry = zipFile.getEntry(getFilename()); + return entry; + } + + } + + private final File archiveFile; + + private final Path workingDirectory; + + private final ArchiveMode archiveMode; + + public static GenericFormatArchive forImport(File archiveFile, File tempDirectory) { + + try { + + Path workingDirectory = Files.createTempDirectory(tempDirectory.toPath(), "genericImport"); + Files.createDirectory(workingDirectory); + GenericFormatArchive archive = new GenericFormatArchive(ArchiveMode.IMPORT, archiveFile, workingDirectory); + return archive; + + } catch (IOException e) { + throw new ApplicationTechnicalException("Could not create generic format import archive", e); + } + + } + + public static GenericFormatArchive forExport(File archiveFile, File tempDirectory) { + + try { + + Path workingDirectory = Files.createTempDirectory(tempDirectory.toPath(), "genericExport"); + GenericFormatArchive archive = new GenericFormatArchive(ArchiveMode.EXPORT, archiveFile, workingDirectory); + return archive; + + } catch (IOException e) { + throw new ApplicationTechnicalException("Could not create generic format export archive", e); + } + + } + + public Path getWorkingDirectoryPath() { + return workingDirectory; + } + + public Path getSampleCategoryModelPath() { + return getPath(ArchiveFilePath.SAMPLE_CATEGORY); + } + + public boolean isProtocolExists() { + return Files.exists(getProtocolPath()); + } + + public boolean isTemporaryReferentialGearsPathExists() { + return Files.exists(getTemporaryReferentialGearsPath()); + } + + public boolean isTemporaryReferentialPersonsPathExists() { + return Files.exists(getTemporaryReferentialPersonsPath()); + } + + public boolean isTemporaryReferentialSpeciesPathExists() { + return Files.exists(getTemporaryReferentialSpeciesPath()); + } + + public boolean isTemporaryReferentialVesselsPathExists() { + return Files.exists(getTemporaryReferentialVesselsPath()); + } + + public Path getProtocolPath() { + return getPath(ArchiveFilePath.PROTOCOL); + } + + public Path getTemporaryReferentialGearsPath() { + return getPath(ArchiveFilePath.REFERENTIAL_GEAR); + } + + public Path getTemporaryReferentialPersonsPath() { + return getPath(ArchiveFilePath.REFERENTIAL_PERSON); + } + + public Path getTemporaryReferentialSpeciesPath() { + return getPath(ArchiveFilePath.REFERENTIAL_SPECIES); + } + + public Path getTemporaryReferentialVesselsPath() { + return getPath(ArchiveFilePath.REFERENTIAL_VESSEL); + } + + public Path getSurveyPath() { + return getPath(ArchiveFilePath.DATA_SURVEY); + } + + public Path getGearCaracteristicsPath() { + return getPath(ArchiveFilePath.DATA_ACCIDENTAL_CATCH); + } + + public Path getOperationPath() { + return getPath(ArchiveFilePath.DATA_OPERATION); + } + + public Path getIndividualObservationPath() { + return getPath(ArchiveFilePath.DATA_INDIVIDUAL_OBSERVATION); + } + + public Path getSpeciesPath() { + return getPath(ArchiveFilePath.DATA_SPECIES); + } + + public Path getCatchPath() { + return getPath(ArchiveFilePath.DATA_CATCH); + } + + public Path getAccidentalCatchPath() { + return getPath(ArchiveFilePath.DATA_ACCIDENTAL_CATCH); + } + + public Path getParameterPath() { + return getPath(ArchiveFilePath.DATA_PARAMETER); + } + + public Path getMarineLitterPath() { + return getPath(ArchiveFilePath.DATA_MARINE_LITTER); + } + + public void checkArchiveLayout() { + + List<String> errors = new ArrayList<>(); + + try (ZipFile zipFile = new ZipFile(archiveFile)) { + + for (ArchiveFilePath archiveFilePath : ArchiveFilePath.values()) { + + if (archiveFilePath.isMandatory()) { + + ZipEntry zipEntry = archiveFilePath.getzipentry(zipFile); + + if (zipEntry == null) { + errors.add(t("tutti.genericformat.importError.missArchiveFile", archiveFilePath.getFilename())); + } + + } + } + } catch (IOException e) { + + throw new ApplicationTechnicalException("Could not open zip file: " + archiveFile, e); + } + + if (errors.isEmpty()) { + + String message = t("tutti.genericFormat.importError.archiveNotSane", Joiner.on("\n").join(errors)); + + throw new ApplicationBusinessException(message); + + } + + } + + public void createZip(ProgressionModel progressionModel) { + + if (progressionModel != null) { + + progressionModel.increments(t("tutti.service.genericExport.buildZip", archiveFile)); + + } + + ApplicationIOUtil.zip(workingDirectory.toFile(), archiveFile, t("tutti.service.genericExport.zip.error", archiveFile)); + + } + + protected Path getPath(ArchiveFilePath archiveFilePath) { + + String filename = archiveFilePath.getFilename(); + + Path file = workingDirectory.resolve(filename); + + if (isImport() && Files.notExists(file)) { + + // Explode from archive + + try (ZipFile zipFile = new ZipFile(archiveFile)) { + + ZipEntry entry = archiveFilePath.getzipentry(zipFile); + if (entry != null) { + try (InputStream inputStream = zipFile.getInputStream(entry)) { + Files.copy(inputStream, file); + } + } + + } catch (IOException e) { + throw new ApplicationTechnicalException("Could not open zip file: " + archiveFile, e); + } + } + + return file; + + } + + protected boolean isImport() { + return ArchiveMode.IMPORT == archiveMode; + } + + protected boolean isExport() { + return ArchiveMode.EXPORT == archiveMode; + } + + + protected GenericFormatArchive(ArchiveMode archiveMode, File archiveFile, Path workingDirectory) { + this.archiveFile = archiveFile; + this.workingDirectory = workingDirectory; + this.archiveMode = archiveMode; + } +} -- To stop receiving notification emails like this one, please contact codelutin.com SCM administrator <admin+scm@codelutin.com>.