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 50033a981808ce7d029339c4ba82b859347bdc7c Author: Tony CHEMIT <chemit@codelutin.com> Date: Sun Feb 15 20:35:56 2015 +0100 improve archive object --- .../genericformat/GenericFormatArchive.java | 216 +++++++++++++++++---- 1 file changed, 175 insertions(+), 41 deletions(-) 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 index cb201ac..a80111f 100644 --- 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 @@ -1,7 +1,11 @@ package fr.ifremer.tutti.service.genericformat; import com.google.common.base.Joiner; +import com.google.common.base.Preconditions; import fr.ifremer.tutti.persistence.ProgressionModel; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.commons.vfs2.FileObject; import org.nuiton.jaxx.application.ApplicationBusinessException; import org.nuiton.jaxx.application.ApplicationIOUtil; import org.nuiton.jaxx.application.ApplicationTechnicalException; @@ -12,6 +16,7 @@ import java.io.InputStream; import java.nio.file.Files; import java.nio.file.Path; import java.util.ArrayList; +import java.util.EnumSet; import java.util.List; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; @@ -26,6 +31,9 @@ import static org.nuiton.i18n.I18n.t; */ public class GenericFormatArchive { + /** Logger. */ + private static final Log log = LogFactory.getLog(GenericFormatArchive.class); + private static enum ArchiveMode { IMPORT, EXPORT @@ -44,7 +52,7 @@ public class GenericFormatArchive { DATA_OPERATION(true, "operation.csv"), DATA_PARAMETER(true, "parameter.csv"), DATA_CATCH(true, "catch.csv"), - DATA_SPECIES(true, "species.csv"), + DATA_SPECIES(false, "species.csv"), DATA_MARINE_LITTER(true, "marineLitter.csv"), DATA_ACCIDENTAL_CATCH(true, "accidentalCatch.csv"), DATA_INDIVIDUAL_OBSERVATION(true, "individualObservation.csv"); @@ -66,11 +74,6 @@ public class GenericFormatArchive { return filename; } - public ZipEntry getzipentry(ZipFile zipFile) { - ZipEntry entry = zipFile.getEntry(getFilename()); - return entry; - } - } private final File archiveFile; @@ -79,6 +82,8 @@ public class GenericFormatArchive { private final ArchiveMode archiveMode; + private final String prefixPath; + public static GenericFormatArchive forImport(File archiveFile, File tempDirectory) { try { @@ -107,6 +112,13 @@ public class GenericFormatArchive { } + public static GenericFormatArchive forExportFromWorkingDirectory(File archiveFile, File workingDirectory) { + + GenericFormatArchive archive = new GenericFormatArchive(ArchiveMode.EXPORT, archiveFile, workingDirectory.toPath()); + return archive; + + } + public Path getWorkingDirectoryPath() { return workingDirectory; } @@ -191,39 +203,6 @@ public class GenericFormatArchive { 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) { @@ -242,22 +221,33 @@ public class GenericFormatArchive { Path file = workingDirectory.resolve(filename); - if (isImport() && Files.notExists(file)) { + if (isImport() && Files.notExists(file) && !missingPaths.contains(archiveFilePath)) { // Explode from archive try (ZipFile zipFile = new ZipFile(archiveFile)) { - ZipEntry entry = archiveFilePath.getzipentry(zipFile); + String zipEntryPath = getZipEntryPath(archiveFilePath); + ZipEntry entry = zipFile.getEntry(zipEntryPath); + + Preconditions.checkState(!(archiveFilePath.isMandatory() && entry == null), "Must have entry " + filename); + if (entry != null) { + + if (log.isInfoEnabled()) { + log.info("Explode zip entry to " + file.toFile().getName()); + } + 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; @@ -272,10 +262,154 @@ public class GenericFormatArchive { return ArchiveMode.EXPORT == archiveMode; } + protected final EnumSet<ArchiveFilePath> missingPaths; protected GenericFormatArchive(ArchiveMode archiveMode, File archiveFile, Path workingDirectory) { + this.archiveFile = archiveFile; this.workingDirectory = workingDirectory; this.archiveMode = archiveMode; + + if (log.isInfoEnabled()) { + log.info("Archive zip file: " + archiveFile); + log.info("Archive working directory: " + workingDirectory); + log.info("Archive mode: " + archiveMode); + } + if (isImport()) { + this.prefixPath = extractPrefixPath(); + if (log.isInfoEnabled()) { + log.info("Zip entries prefix path: " + prefixPath); + } + + checkArchiveLayout(); + + missingPaths = getMissingPaths(); + + } else { + this.prefixPath = null; + this.missingPaths = null; + } + + } + + protected String extractPrefixPath() { + + FileObject fileObject = ApplicationIOUtil.resolveFile( + "zip:" + archiveFile.getAbsolutePath(), + t("tutti.service.persistence.getArchive.error", archiveFile)); + + FileObject[] children = ApplicationIOUtil.getChildren( + fileObject, + t("tutti.service.persistence.openArchive.error", fileObject)); + + fileObject = children[0]; + + String prefix = fileObject.getName().getBaseName() + "/"; + return prefix; + + } + + protected String getZipEntryPath(ArchiveFilePath archiveFilePath) { + + String path = prefixPath + archiveFilePath.getFilename(); + return path; + } + + protected void checkArchiveLayout() { + + List<String> errors = new ArrayList<>(); + + try (ZipFile zipFile = new ZipFile(archiveFile)) { + + for (ArchiveFilePath archiveFilePath : ArchiveFilePath.values()) { + + if (archiveFilePath.isMandatory()) { + String zipEntryPath = getZipEntryPath(archiveFilePath); + + if (log.isDebugEnabled()) { + log.debug("Check mandatory entry " + zipEntryPath + " exists."); + } + + ZipEntry zipEntry = zipFile.getEntry(zipEntryPath); + + if (zipEntry == null) { + + if (log.isErrorEnabled()) { + log.error("Mandatory entry " + zipEntryPath + " not found."); + } + errors.add(t("tutti.genericformat.importError.missArchiveFile", archiveFilePath.getFilename())); + + } else { + + if (log.isInfoEnabled()) { + log.info("Mandatory entry " + zipEntryPath + " found."); + } + } + } + + } + + } 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); + + } + + } + + + protected EnumSet<ArchiveFilePath> getMissingPaths() { + + EnumSet<ArchiveFilePath> result = EnumSet.noneOf(ArchiveFilePath.class); + + try (ZipFile zipFile = new ZipFile(archiveFile)) { + + for (ArchiveFilePath archiveFilePath : ArchiveFilePath.values()) { + + if (!archiveFilePath.isMandatory()) { + + String zipEntryPath = getZipEntryPath(archiveFilePath); + + if (log.isDebugEnabled()) { + log.debug("Check optional entry " + zipEntryPath + " exists."); + } + + ZipEntry zipEntry = zipFile.getEntry(zipEntryPath); + + if (zipEntry == null) { + + if (log.isInfoEnabled()) { + log.info("Optional entry " + zipEntryPath + " not found."); + } + + result.add(archiveFilePath); + + } else { + + if (log.isInfoEnabled()) { + log.info("Optional entry " + zipEntryPath + " found."); + } + + } + + } + + } + + } catch (IOException e) { + + throw new ApplicationTechnicalException("Could not open zip file: " + archiveFile, e); + } + + return result; + } + } -- To stop receiving notification emails like this one, please contact codelutin.com SCM administrator <admin+scm@codelutin.com>.