/*
|
* 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.typefichier.validator.Validator;
|
import com.megatim.typefichier.validator.model.ConfigStreamValidator;
|
import com.megatimfx.common.abstracts.AbstractEditDialogController;
|
import com.megatimfx.common.utils.ViewLoaderUtil;
|
import com.megatimfx.components.customdialogs.AlertMessageUtil;
|
import com.megatimfx.components.customdialogs.LoadinIndicatorDialogUtil;
|
import com.megatimfx.components.dialogs.NotificationDialog;
|
import com.megatimfx.components.dialogs.NotificationType;
|
import com.megatim.fdxconvert.App;
|
import com.megatim.fdxconvert.dao.ValidateurDAO;
|
import com.megatim.fdxconvert.exceptions.ConfigException;
|
import com.megatim.fdxconvert.exceptions.ValidatorException;
|
import com.megatim.fdxconvert.forms.TxtFileToValidateEditFormController;
|
import com.megatim.fdxconvert.model.Configuration;
|
import com.megatim.fdxconvert.pojo.TxtFileToValidate;
|
import com.megatim.fdxconvert.model.Validateur;
|
import com.megatim.fdxconvert.service.ConfigurationService;
|
import com.megatim.fdxconvert.util.Utilities;
|
import java.io.File;
|
import java.io.FileNotFoundException;
|
import java.io.IOException;
|
import java.net.URL;
|
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.IOUtils;
|
|
/**
|
*
|
* @author STEPHANIE
|
*/
|
public class TxtFileToValidateEditDialogController extends AbstractEditDialogController<TxtFileToValidate> {
|
|
private TxtFileToValidateEditFormController txtFileToValidateEditFormController;
|
|
@Override
|
public String getTitle() {
|
getEditButton().setText("Vérifier");
|
return "Vérification format de fichier Fdx";
|
}
|
|
@Override
|
public Pane getContentFormPane() throws IOException {
|
return ViewLoaderUtil.getPaneFromFxmlFile(txtFileToValidateEditFormController.getClass().getResource("TxtFileToValidateEditForm.fxml"), txtFileToValidateEditFormController);
|
|
}
|
|
@Override
|
public Object getContentFormController() {
|
return txtFileToValidateEditFormController;
|
}
|
|
@Override
|
public void initialize(URL url, ResourceBundle rb) {
|
txtFileToValidateEditFormController = new TxtFileToValidateEditFormController();
|
super.initialize(url, rb);
|
}
|
|
@Override
|
public void afterSave(ActionEvent event) {
|
|
Configuration config = ConfigurationService.getInstance().getCurrentConfig();
|
|
Task<Boolean> task = new Task<Boolean>() {
|
|
@Override
|
protected Boolean call() throws Exception {
|
|
TxtFileToValidate val = getCurrentObject();
|
|
return validate(val, config);
|
|
}
|
};
|
|
task.setOnRunning(e -> LoadinIndicatorDialogUtil.getLoadingIndicatorDialog().show());
|
|
task.setOnSucceeded(e -> {
|
|
LoadinIndicatorDialogUtil.getLoadingIndicatorDialog().hide();
|
|
Node source = (Node) event.getSource();
|
|
Stage parentStage = (Stage) source.getScene().getWindow();
|
|
boolean valid = task.getValue();
|
|
if (valid) {
|
|
NotificationDialog notificationDialog = new NotificationDialog(
|
"Vérification réussie. Fichier correcte",
|
NotificationType.SUCCESS,
|
parentStage
|
);
|
|
notificationDialog.showNotification();
|
|
} else {
|
NotificationDialog notificationDialog = new NotificationDialog(
|
"Echec de la vérification. Le fichier contient des erreurs, veuillez consulter le fichier d'erreur pour en savoir plus",
|
NotificationType.ERROR,
|
parentStage
|
);
|
|
notificationDialog.showNotification();
|
|
}
|
|
});
|
|
task.setOnFailed(e -> {
|
|
LoadinIndicatorDialogUtil.getLoadingIndicatorDialog().hide();
|
|
Throwable th = task.getException();
|
|
if (th instanceof ConfigException || th instanceof ValidatorException) {
|
|
Node source = (Node) event.getSource();
|
|
Stage parentStage = (Stage) source.getScene().getWindow();
|
|
NotificationDialog notificationDialog = new NotificationDialog(th.getMessage(), NotificationType.WARNING, parentStage);
|
|
notificationDialog.showNotification();
|
|
} else {
|
|
AlertMessageUtil.showAlertException(th, "Une exception s'est produite pendant la validation du fichier", "Erreur");
|
|
}
|
|
});
|
|
new Thread(task).start();
|
|
}
|
|
private boolean validate(TxtFileToValidate val, Configuration config) throws Exception {
|
|
if (config == null) {
|
throw new ConfigException("Validation impossible, les repertoires ne sont pas configurés. Bien vouloir les configurer avant avant d'effectuer cette action");
|
}
|
|
String codeTypeFichier = val.getTypeFichier().getCode();
|
|
Optional<Validateur> validateur = ValidateurDAO.findByCodeTypeFichier(codeTypeFichier);
|
|
if (!validateur.isPresent()) {
|
throw new ValidatorException("Validation impossible. Le type fichier " + codeTypeFichier + " n'a pas de validateur enregistré");
|
}
|
|
File file = new File(val.getTxtFilePath());
|
|
if (!file.exists()) {
|
throw new FileNotFoundException("Le fichier " + file.getAbsolutePath() + " est introuvable");
|
}
|
|
byte[] targetArray = IOUtils.toByteArray(App.class.getClass().getResourceAsStream("/predicatelogic-engine.xml"));
|
|
ConfigStreamValidator configValidator = new ConfigStreamValidator(targetArray, validateur.get().getContent(), config.getErrorDir(), config.getOutputDir(), file,file);
|
|
return new Validator().validate(configValidator, Boolean.FALSE, Boolean.FALSE,Utilities.getNbThreads());
|
}
|
}
|