/*
 * Axelor Business Solutions
 *
 * Copyright (C) 2019 Axelor (<http://axelor.com>).
 *
 * This program is free software: you can redistribute it and/or  modify
 * it under the terms of the GNU Affero General Public License, version 3,
 * as published by the Free Software Foundation.
 *
 * 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 Affero 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/>.
 */
package com.axelor.apps.base.service;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;

import org.apache.commons.io.FileUtils;
import org.apache.tika.detect.AutoDetectReader;
import org.apache.tika.exception.TikaException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.axelor.apps.base.exceptions.IExceptionMessage;
import com.axelor.dms.db.DMSFile;
import com.axelor.dms.db.repo.DMSFileRepository;
import com.axelor.exception.AxelorException;
import com.axelor.exception.db.repo.TraceBackRepository;
import com.axelor.i18n.I18n;
import com.axelor.meta.MetaFiles;
import com.axelor.meta.db.MetaFile;
import com.google.inject.Inject;
import com.google.inject.persist.Transactional;

public class DMSImportWizardServiceImpl implements DMSImportWizardService {

  @Inject private DMSFileRepository dmsFileRepo;

  @Inject private MetaFiles metaFiles;

  private Logger LOG = LoggerFactory.getLogger(getClass());

  @Override
  public void importDMS(MetaFile metaFile) throws AxelorException {
    ZipInputStream zipInputStream = null;
    try {
      File file = MetaFiles.getPath(metaFile).toFile();
      ZipFile zipfile = new ZipFile(file);
      zipInputStream = validateZip(file);
      createDmsTree(zipInputStream, zipfile);
      throw new AxelorException(
          TraceBackRepository.CATEGORY_CONFIGURATION_ERROR,
          I18n.get(IExceptionMessage.DMS_IMPORT_PROCESS_SUCCESS_MESSAGE));
    } catch (IOException e) {
      throw new AxelorException(
          TraceBackRepository.CATEGORY_CONFIGURATION_ERROR,
          I18n.get(IExceptionMessage.DMS_IMPORT_FILE_PROCESS_ERROR));
    } finally {
      try {
        if (zipInputStream != null) {
          zipInputStream.close();
        }
      } catch (IOException e) {
      }
    }
  }

  private ZipInputStream validateZip(File file) throws AxelorException {
    try {
      AutoDetectReader autoDetectReader = new AutoDetectReader(new FileInputStream(file));
      ZipInputStream zis =
          new ZipInputStream(new FileInputStream(file), autoDetectReader.getCharset());
      autoDetectReader.close();
      return zis;
    } catch (IOException | TikaException e) {
      throw new AxelorException(
          TraceBackRepository.CATEGORY_CONFIGURATION_ERROR,
          I18n.get(IExceptionMessage.DMS_IMPORT_INVALID_ZIP_ERROR));
    }
  }

  @Transactional
  public void createDmsTree(ZipInputStream zipInputStream, ZipFile zipfile) throws IOException {
    ZipEntry zipEntry = zipInputStream.getNextEntry();
    Map<String, DMSFile> dmsMap = new HashMap<>();
    while (zipEntry != null) {
      String fileName = getFileName(zipEntry);
      DMSFile dmsFile = new DMSFile();
      dmsFile.setFileName(fileName);
      String parentName = getParentName(zipEntry, fileName);
      dmsFile.setParent(dmsMap.get(parentName));
      LOG.debug("Praent file: {}", dmsFile.getParent());
      if (zipEntry.isDirectory()) {
        dmsFile.setIsDirectory(true);
        dmsFile = dmsFileRepo.save(dmsFile);
        dmsMap.put(zipEntry.getName(), dmsFile);
        LOG.debug("DMS Directory created: {}", dmsFile.getFileName());
      } else {
        String fileType = fileName.substring(fileName.indexOf(".") + 1);
        try {
          File tempDir = File.createTempFile("", "");
          File file = new File(tempDir,fileName);
          InputStream is = zipfile.getInputStream(zipEntry);
          FileUtils.copyInputStreamToFile(is, file);
          MetaFiles.checkType(file);
          MetaFile metaFile = metaFiles.upload(zipInputStream, fileName);
          dmsFile.setMetaFile(metaFile);
          dmsFile = dmsFileRepo.save(dmsFile);
          LOG.debug("DMS File created: {}", dmsFile.getFileName());
        } catch (IllegalArgumentException e) {
          LOG.debug("File type is not allowed : {}", fileType);
        }
      }
      zipEntry = zipInputStream.getNextEntry();
    }
  }

  private String getFileName(ZipEntry zipEntry) {
    String entryName = zipEntry.getName();
    String[] names = entryName.split(File.separator);
    return names[names.length - 1];
  }

  private String getParentName(ZipEntry zipEntry, String fileName) {
    String entryName = zipEntry.getName();
    return entryName.substring(0, entryName.lastIndexOf(File.separator + fileName) + 1);
  }
}

@@ -731,16 +731,439 @@
 rvice;%0A%0A
+import com.axelor.apps.base.exceptions.IExceptionMessage;%0Aimport com.axelor.dms.db.DMSFile;%0Aimport com.axelor.dms.db.repo.DMSFileRepository;%0Aimport com.axelor.exception.AxelorException;%0Aimport com.axelor.exception.db.repo.TraceBackRepository;%0Aimport com.axelor.i18n.I18n;%0Aimport com.axelor.meta.MetaFiles;%0Aimport com.axelor.meta.db.MetaFile;%0Aimport com.google.inject.Inject;%0Aimport com.google.inject.persist.Transactional;%0A
 import j
@@ -1171,24 +1171,24 @@
 va.io.File;%0A
-
 import java.
@@ -1409,17 +1409,16 @@
 Stream;%0A
-%0A
 import o
@@ -1566,24 +1566,24 @@
 f4j.Logger;%0A
+
 import org.s
@@ -1607,432 +1607,8 @@
 y;%0A%0A
-import com.axelor.apps.base.exceptions.IExceptionMessage;%0Aimport com.axelor.dms.db.DMSFile;%0Aimport com.axelor.dms.db.repo.DMSFileRepository;%0Aimport com.axelor.exception.AxelorException;%0Aimport com.axelor.exception.db.repo.TraceBackRepository;%0Aimport com.axelor.i18n.I18n;%0Aimport com.axelor.meta.MetaFiles;%0Aimport com.axelor.meta.db.MetaFile;%0Aimport com.google.inject.Inject;%0Aimport com.google.inject.persist.Transactional;%0A%0A
 publ
@@ -4205,16 +4205,17 @@
 tempDir,
+ 
 fileName

Apply spotless java google code format