/* * 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.dynamicjsonparser.enums.TypeDonnee; import com.megatimfx.common.abstracts.AbstractEditDialogController; import com.megatimfx.common.utils.ViewLoaderUtil; import com.megatimfx.common.enums.TypeOperation; import com.megatimfx.components.dialogs.NotificationDialog; import com.megatimfx.components.dialogs.NotificationType; import com.megatim.fdxconvert.App; import com.megatim.fdxconvert.forms.ModeleJsonEditFormController; import com.megatim.fdxconvert.forms.StructureChampJsonEditFormController; import com.megatim.fdxconvert.forms.SubObjectEditFormController; import com.megatim.fdxconvert.model.StructureChampJson; import com.megatim.fdxconvert.model.SubObject; import com.megatim.fdxconvert.model.TypeFichier; import com.megatim.fdxconvert.util.Utilities; import java.io.IOException; import java.net.URL; import java.util.List; import java.util.ResourceBundle; import java.util.stream.Collectors; import javafx.collections.ObservableList; import javafx.event.ActionEvent; import javafx.scene.Node; import javafx.scene.layout.Pane; import javafx.stage.Stage; /** * * @author ASUS */ public class StructureChampJsonEditDialogController extends AbstractEditDialogController { private StructureChampJsonEditFormController structureChampJsonEditFormController; private SubObjectEditFormController subObjectEditFormController; private ObservableList subObjectsList; private ModeleJsonEditFormController modeleJsonEditFormController; private TypeFichier typeFichier; @Override public void initialize(URL url, ResourceBundle rb) { if (subObjectEditFormController != null) { structureChampJsonEditFormController = new StructureChampJsonEditFormController(); } super.initialize(url, rb); } @Override public String getTitle() { return "Edition d'un champ du fichier JSON"; } @Override public Pane getContentFormPane() throws IOException { return ViewLoaderUtil.getPaneFromFxmlFile( structureChampJsonEditFormController.getClass().getResource("StructureChampJsonEditForm.fxml"), structureChampJsonEditFormController ); } @Override public Object getContentFormController() { return structureChampJsonEditFormController; } @Override protected void afterBindFormFieldWithCurrentObject(StructureChampJson bindObject, Object formController, TypeOperation typeOperation) { structureChampJsonEditFormController.getTypeObjetComboBox().setItems(subObjectsList); } public void setStructureChampJsonEditFormController(StructureChampJsonEditFormController structureChampJsonEditFormController) { this.structureChampJsonEditFormController = structureChampJsonEditFormController; } public void setTypeFichier(TypeFichier typeFichier) { this.typeFichier = typeFichier; } @Override public StructureChampJson refreshObjectToUpdate(StructureChampJson objectToUpdate) { StructureChampJson struct = new StructureChampJson(); struct.setDelimiteurDate(objectToUpdate.getDelimiteurDate()); struct.setFormatDate(objectToUpdate.getFormatDate()); struct.setLibelle(objectToUpdate.getLibelle()); struct.setListe(objectToUpdate.isListe()); struct.setTypeOfSubObject(objectToUpdate.getTypeOfSubObject()); struct.setFieldOfSubObject(objectToUpdate.getFieldOfSubObject()); struct.setTypeDonnee(objectToUpdate.getTypeDonnee()); struct.setDateCreation(objectToUpdate.getDateCreation()); struct.setId(objectToUpdate.getId()); struct.setModeleJson(objectToUpdate.getModeleJson()); return struct; } @Override protected void beforeBindFormFieldWithCurrentObject(StructureChampJson bindObject, Object formController, TypeOperation typeOperation) { if (formController instanceof StructureChampJsonEditFormController) { if (typeOperation.equals(TypeOperation.UPDATE) || typeOperation.equals(TypeOperation.VIEW)) { if (bindObject.getTypeDonnee().equals(TypeDonnee.DATE) && bindObject.getCodeDelimiteurDate() != null && !bindObject.getCodeDelimiteurDate().isEmpty()) { bindObject.setDelimiteurDate(App.SEPARATEURS_DATE.get(bindObject.getCodeDelimiteurDate())); } } } } @Override public boolean beforeSave(ActionEvent event) { boolean proceed = super.beforeSave(event); StringBuilder message = new StringBuilder(""); StructureChampJson currentObject = getCurrentObject(); if (currentObject != null) { if (currentObject.getLibelle() == null || currentObject.getLibelle().isEmpty()) { message.append("Le Libellé est obligatoire\n"); proceed = false; } else { getCurrentObject().setLibelle(getCurrentObject().getLibelle().trim()); StringBuilder subMessage = Utilities.validateName(getCurrentObject().getLibelle(), "Le libellé du champ"); if (subMessage.length() != 0) { message.append(subMessage); proceed = false; } } if (currentObject.getTypeDonnee() == null) { message.append("Le type de données est obligatoire\n"); proceed = false; } else if (currentObject.getTypeDonnee().equals(TypeDonnee.DATE)) { if (currentObject.getFormatDate() == null) { message.append("Le format de la date est obligatoire\n"); proceed = false; } if (currentObject.getDelimiteurDate() == null) { message.append("Le séparateur de date est obligatoire\n"); proceed = false; } else { getCurrentObject().setCodeDelimiteurDate(getCurrentObject().getDelimiteurDate().getCode()); } } else if (currentObject.getTypeDonnee().equals(TypeDonnee.OBJET) && currentObject.getTypeOfSubObject() == null) { message.append("Le type de l'objet est obligatoire\n"); proceed = false; } if (!currentObject.getTypeDonnee().equals(TypeDonnee.DATE)) { getCurrentObject().setCodeDelimiteurDate(null); getCurrentObject().setFormatDate(null); } if(!currentObject.getTypeDonnee().equals(TypeDonnee.OBJET)) { getCurrentObject().setTypeOfSubObject(null); } String subMessage = ""; if (subObjectEditFormController != null) { subMessage = verifyIfCodeIsAlreadUsed(subObjectEditFormController.getSubStructureJsonListe()); } else { subMessage = verifyIfCodeIsAlreadUsed(modeleJsonEditFormController.getStructureChampAbstractTable().getElementObservableList()); } if (!subMessage.isEmpty()) { message.append(subMessage); proceed = false; } } if (!proceed) { Node source = (Node) event.getSource(); Stage parentStage = (Stage) source.getScene().getWindow(); NotificationDialog notificationDialog = new NotificationDialog(message.toString(), NotificationType.ERROR, parentStage); notificationDialog.showNotification(); } return proceed; } public void setSubObjectsList(ObservableList subObjectsList) { this.subObjectsList = subObjectsList; } public void setSubObjectEditFormController(SubObjectEditFormController subObjectEditFormController) { this.subObjectEditFormController = subObjectEditFormController; } public StructureChampJsonEditFormController getStructureChampJsonEditFormController() { return structureChampJsonEditFormController; } public void setModeleJsonEditFormController(ModeleJsonEditFormController modeleJsonEditFormController) { this.modeleJsonEditFormController = modeleJsonEditFormController; } private String verifyIfCodeIsAlreadUsed(List observableList) { String message = ""; List foundListe = observableList.stream().filter(p -> { boolean testLibelle = p.getLibelle().equalsIgnoreCase(getCurrentObject().getLibelle()); return (testLibelle && getTypeOperation().equals(TypeOperation.ADD)) || (getTypeOperation().equals(TypeOperation.UPDATE) && testLibelle && !p.equals(getCurrentObject())); } ).collect(Collectors.toList()); if (!foundListe.isEmpty()) { message = "Le libellé \"" + getCurrentObject().getLibelle() + "\"" + " est déjà utilisé par un autre champ \n"; } return message; } }