/*
|
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
|
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
|
*/
|
package com.megatim.fdxconvert.controller;
|
|
import com.megatim.module.compression.impl.JavaZipZip4Impl;
|
import com.megatim.module.encryption.impl.AESImpl;
|
import com.megatimfx.common.abstracts.AbstractEditDialogController;
|
import com.megatimfx.common.enums.TypeOperation;
|
import com.megatimfx.common.utils.ViewLoaderUtil;
|
import com.megatimfx.components.customdialogs.AlertMessageUtil;
|
import com.megatimfx.components.dialogs.NotificationDialog;
|
import com.megatimfx.components.dialogs.NotificationType;
|
import com.megatim.fdxconvert.dao.ValidateurDAO;
|
import com.megatim.fdxconvert.exceptions.ConfigException;
|
import com.megatim.fdxconvert.forms.ValidateurEditFormController;
|
import com.megatim.fdxconvert.model.Configuration;
|
import com.megatim.fdxconvert.model.TypeFichier;
|
import com.megatim.fdxconvert.model.Validateur;
|
import com.megatim.fdxconvert.service.ConfigurationService;
|
import com.megatim.fdxconvert.util.ParserUtils;
|
import java.io.File;
|
import java.io.IOException;
|
import java.net.URL;
|
import java.util.Arrays;
|
import java.util.List;
|
import java.util.Optional;
|
import java.util.ResourceBundle;
|
import javafx.concurrent.Task;
|
import javafx.event.ActionEvent;
|
import javafx.scene.Node;
|
import javafx.scene.layout.Pane;
|
import javafx.stage.Stage;
|
import org.apache.commons.io.FileUtils;
|
|
/**
|
*
|
* @author STEPHANIE
|
*/
|
public class ValidateurEditDialogController extends AbstractEditDialogController<Validateur> {
|
|
private ValidateurEditFormController validateurEditFormController;
|
private final String UNZIP_DIR = "UNZIP_DIR";
|
Boolean result;
|
|
@Override
|
public String getTitle() {
|
getEditButton().setText("Importer");
|
return "Importer le validateur d'un type fichier";
|
}
|
|
@Override
|
public Pane getContentFormPane() throws IOException {
|
return ViewLoaderUtil.getPaneFromFxmlFile(validateurEditFormController.getClass().getResource("ValidateurEditForm.fxml"), validateurEditFormController);
|
|
}
|
|
@Override
|
public Object getContentFormController() {
|
return validateurEditFormController;
|
}
|
|
@Override
|
public void initialize(URL url, ResourceBundle rb) {
|
validateurEditFormController = new ValidateurEditFormController();
|
super.initialize(url, rb);
|
}
|
|
@Override
|
public String getConfirmationMessage() {
|
return "Validateur du typefichier " + getCurrentObject().getCodeTypeFichier() + " importé avec succès !";
|
}
|
|
@Override
|
public boolean beforeSave(ActionEvent event) {
|
result = false;
|
boolean proceed = super.beforeSave(event);
|
|
if (validateurEditFormController.getProtegeCheckBox().isSelected()) {
|
proceed = proceed && validateurEditFormController.getKeyConfirmationField() != null
|
&& validateurEditFormController.getKeyToDecryptField() != null;
|
}
|
|
if (proceed && getTypeOperation().equals(TypeOperation.ADD)) {
|
Node source = (Node) event.getSource();
|
Stage parentStage = (Stage) source.getScene().getWindow();
|
|
Validateur validateur = getCurrentObject();
|
Configuration config = ConfigurationService.getInstance().getCurrentConfig();
|
File decryptedFile = null;
|
|
try {
|
if (validateur.isProtege()) {
|
Optional<Validateur> exist = ValidateurDAO.findByCodeTypeFichier(validateur.getCodeTypeFichier());
|
|
if (!exist.isPresent()) {
|
File fileToUnzip = new File(validateur.getFilePath());
|
unzip(fileToUnzip, config);
|
decryptedFile = decrypt(validateur, config, fileToUnzip);
|
validateur.setFilePath(decryptedFile.getAbsolutePath());
|
setContent(validateur);
|
|
decryptedFile.deleteOnExit();
|
result = true;
|
} else {
|
NotificationDialog notificationDialog = new NotificationDialog(
|
"Validateur du typefichier " + validateur.getCodeTypeFichier() + " déjà enregistré. Bien vouloir supprimer l'existant avant d'effectuer cette action",
|
NotificationType.WARNING, parentStage);
|
notificationDialog.showNotification();
|
result = false;
|
}
|
} else {
|
setContent(validateur);
|
result = true;
|
}
|
} catch (Exception th) {
|
|
if (th instanceof javax.crypto.BadPaddingException || th instanceof java.security.spec.InvalidKeySpecException
|
|| th instanceof java.security.InvalidKeyException) {
|
|
decryptedFile.deleteOnExit();
|
|
NotificationDialog notificationDialog = new NotificationDialog(
|
"Echec décryptage : La clé fournie n'est pas la bonne ",
|
NotificationType.ERROR,
|
parentStage
|
);
|
notificationDialog.showNotification();
|
} else if (th instanceof ConfigException) {
|
NotificationDialog notificationDialog = new NotificationDialog(
|
th.getMessage(), NotificationType.ERROR, parentStage);
|
notificationDialog.showNotification();
|
} else {
|
AlertMessageUtil.showAlertException(th, "Une exception s'est produite pendant le décryptage du fichier", "Erreur");
|
}
|
} finally {
|
if (config != null) {
|
File unzipDir = new File(config.getValidatorsDir(), UNZIP_DIR);
|
|
if (unzipDir.exists() && unzipDir.isDirectory()) {
|
List<File> liste = Arrays.asList(unzipDir.listFiles());
|
liste.forEach(f -> f.deleteOnExit());
|
}
|
}
|
}
|
}
|
|
return result;
|
}
|
|
private void unzip(File fileToUnzip, Configuration config) throws Exception {
|
JavaZipZip4Impl zip4Impl = new JavaZipZip4Impl();
|
|
if (config == null) {
|
throw new ConfigException("Bien vouloir configurer les répertoires avant avant d'effectuer cette action");
|
}
|
|
File unzipDir = new File(config.getValidatorsDir(), UNZIP_DIR);
|
|
if (!unzipDir.exists()) {
|
unzipDir.mkdir();
|
}
|
|
Arrays.asList(unzipDir.listFiles()).forEach(f -> f.deleteOnExit());
|
zip4Impl.unZipFile(fileToUnzip, unzipDir);
|
}
|
|
private File decrypt(Validateur val, Configuration config, File fileToUnzip) throws Exception {
|
AESImpl aes = new AESImpl();
|
File unzipDir = new File(config.getValidatorsDir(), UNZIP_DIR);
|
String fileName = fileNameWithoutExtension(fileToUnzip);
|
File inputFile = new File(unzipDir.getAbsolutePath(), fileName + ".xml");
|
File outputFile = new File(config.getValidatorsDir(), inputFile.getName());
|
aes.decryptFile(val.getKeyToDecrypt(), val.getKeyLength(), inputFile.getAbsolutePath(), outputFile.getAbsolutePath());
|
|
inputFile.deleteOnExit();
|
|
return outputFile;
|
}
|
|
private void setContent(Validateur validateur) throws Exception {
|
TypeFichier typeFichier = getCurrentObject().getTypeFichier();
|
getCurrentObject().setConvertDataBeforeValidation(ParserUtils.isConvertDataBeforeValidation(new File(validateur.getFilePath())));
|
byte[] content = FileUtils.readFileToByteArray(new File(validateur.getFilePath()));
|
getCurrentObject().setContent(content);
|
getCurrentObject().setCodeTypeFichier(typeFichier.getCode());
|
}
|
|
private String fileNameWithoutExtension(File file) {
|
String name = file.getName();
|
int index = name.lastIndexOf(".");
|
String fileName = name.substring(0, index);
|
|
return fileName;
|
}
|
|
@Override
|
public String getConfirmationDialogMessage() {
|
return "Le validateur du typefichier " + getCurrentObject().getCodeTypeFichier() + " a été importé avec succès.";
|
}
|
}
|