/* * 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.forms; import com.megatimfx.common.abstracts.AbstractEditDialogController; import com.megatimfx.common.abstracts.AbstractMainDialogController; import com.megatimfx.common.pojo.SearchCriteria; import com.megatimfx.common.service.GenericCrudService; import com.megatimfx.common.utils.ViewLoaderUtil; import com.megatimfx.components.customdialogs.LoadinIndicatorDialogUtil; import com.megatimfx.components.dialogs.NotificationDialog; import com.megatimfx.components.dialogs.NotificationType; import com.megatim.fdxconvert.controller.ValidateurEditDialogController; import com.megatim.fdxconvert.controller.search.ValidateurSearchFormController; import com.megatim.fdxconvert.controller.table.ValidateurTable; import com.megatim.fdxconvert.exceptions.ConfigException; import com.megatim.fdxconvert.model.Configuration; import com.megatim.fdxconvert.model.Validateur; import com.megatim.fdxconvert.service.ConfigurationService; import com.megatim.fdxconvert.service.ValidateurService; import java.io.File; import java.io.IOException; import java.net.URL; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.ResourceBundle; import java.util.logging.Level; import java.util.logging.Logger; import javafx.concurrent.Task; import javafx.event.Event; import javafx.fxml.FXML; import javafx.scene.Node; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.Label; import javafx.scene.control.TableColumn; import javafx.scene.input.MouseEvent; import javafx.scene.layout.Pane; import javafx.stage.Stage; import org.apache.commons.io.FileUtils; /** * * @author STEPHANIE */ public class ValidateurListFormController extends AbstractMainDialogController { private ValidateurSearchFormController validateurSearchFormController; private double dx, dy; @FXML private Button btnPrintValidator; @FXML private Label btnPrintValidatorLabel; @Override public void initialize(URL url, ResourceBundle rb) { validateurSearchFormController = new ValidateurSearchFormController(); setPrintButtonRemoved(true); setEditButtonRemoved(true); btnPrintValidator.setOnAction(e -> { exportValidatorFile(e); }); btnPrintValidatorLabel.setOnMouseClicked(e -> { exportValidatorFile(e); }); super.initialize(url, rb); } @Override public GenericCrudService getGenericCrudService() { return ValidateurService.getInstance(); } @Override public String getTitle() { return "Importations des formats de validation"; } @Override public Class getElementClazz() { return Validateur.class; } @Override public List getElementTableColumns() { return Arrays.asList(ValidateurTable.typeFichierColumn(), ValidateurTable.dataConversionColumn(), ValidateurTable.dateCreationColumn(), ValidateurTable.keyLengthColumn()); } @Override public AbstractEditDialogController getAbstractEditDialogController() { return new ValidateurEditDialogController(); } @Override public List getPermanentSearchCriterias() { List liste = new ArrayList<>(); return liste; } @Override public Object getSearchFormController() { return validateurSearchFormController; } @Override public void resetFormFields(Object searchController, Object advancedSearchController) { if (searchController instanceof ValidateurSearchFormController) { ValidateurSearchFormController controller = (ValidateurSearchFormController) searchController; controller.getTypeFichierAbstractSelectionItem().setOldElement(null); } } @Override public Pane getSearchFormPane() throws IOException { return ViewLoaderUtil.getPaneFromFxmlFile(validateurSearchFormController.getClass().getResource("ValidateurSearchForm.fxml"), validateurSearchFormController); } @FXML public void titleBarMousePressed(MouseEvent event) { Scene scene = ((Node) event.getSource()).getScene(); Stage stage = (Stage) scene.getWindow(); dx = stage.getX() - event.getScreenX(); dy = stage.getY() - event.getScreenY(); } @FXML public void titleBarMouseDragged(MouseEvent event) { Scene scene = ((Node) event.getSource()).getScene(); Stage stage = (Stage) scene.getWindow(); stage.setX(event.getScreenX() + dx); stage.setY(event.getScreenY() + dy); } private void exportValidatorFile(Event event) { Node node = (Node) event.getSource(); Stage parentStage = (Stage) node.getScene().getWindow(); Validateur validateur = getElementTable().getSelectionModel().getSelectedItem(); DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyyMMdd-HHmmss"); try { Configuration config = ConfigurationService.getInstance().getCurrentConfig(); if (config == null) { throw new ConfigException("Répertoires non configurés"); } if (validateur != null) { exportOneFile(validateur.getCodeTypeFichier(), config.getValidatorsDir(), dtf, validateur.getContent()); NotificationDialog notificationDialog = new NotificationDialog( "Succès de l'exportation, consulter le répertoire '" + config.getValidatorsDir() + "' pour voir le fichier validateur du typefichier " + validateur.getCodeTypeFichier(), NotificationType.SUCCESS, parentStage ); notificationDialog.showNotification(); } else { List validateurs = ValidateurService.getInstance().getAll(); Task task = new Task() { @Override protected Void call() throws Exception { for (Validateur v : validateurs) { exportOneFile(v.getCodeTypeFichier(), config.getValidatorsDir(), dtf, v.getContent()); } return null; } }; task.setOnRunning(e -> LoadinIndicatorDialogUtil.getLoadingIndicatorDialog().show()); task.setOnSucceeded(e -> { String message = ""; if (validateurs.isEmpty()) { message = "Aucun validateur à exporter"; } else { message = "Tous les validateurs ont été exporté dans le répertoire " + config.getValidatorsDir(); } LoadinIndicatorDialogUtil.getLoadingIndicatorDialog().hide(); NotificationDialog notificationDialog = new NotificationDialog( message, NotificationType.INFO, parentStage ); notificationDialog.showNotification(); }); task.setOnFailed(e -> { LoadinIndicatorDialogUtil.getLoadingIndicatorDialog().hide(); NotificationDialog notificationDialog = new NotificationDialog( "Echec de l'exportation, une erreur est survenue ", NotificationType.ERROR, parentStage ); notificationDialog.showNotification(); }); Thread thread1 = new Thread(task); thread1.setDaemon(true); thread1.start(); } } catch (ConfigException ex) { NotificationDialog notificationDialog = new NotificationDialog( "Echec de l'exportation, Veuillez configurer les répertoires avant d'exécuter cette opération", NotificationType.WARNING, parentStage ); notificationDialog.showNotification(); } catch (Exception ex) { NotificationDialog notificationDialog = new NotificationDialog( "Echec de l'exportation, une erreur est survenue ", NotificationType.ERROR, parentStage ); notificationDialog.showNotification(); Logger.getLogger(ValidateurListFormController.class.getName()).log(Level.SEVERE, null, ex); } } private void exportOneFile(String codeTypeFichier, String validatorsDir, DateTimeFormatter dtf, byte[] content) throws IOException { File file = new File(validatorsDir, "Fdx-Validateur" + codeTypeFichier + "-" + dtf.format(LocalDateTime.now()) + ".xml"); FileUtils.writeByteArrayToFile(file, content, false); } }