/*
|
* 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.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.forms.ModeleJsonEditFormController;
|
import com.megatim.fdxconvert.forms.StructureChampJsonEditFormController;
|
import com.megatim.fdxconvert.model.ModeleJson;
|
import com.megatim.fdxconvert.model.StructureChampJson;
|
import com.megatim.fdxconvert.model.SubObject;
|
import com.megatim.fdxconvert.service.SubObjectService;
|
import com.megatim.fdxconvert.util.Utilities;
|
import java.io.IOException;
|
import java.net.URL;
|
import java.util.ArrayList;
|
import java.util.HashSet;
|
import java.util.List;
|
import java.util.ResourceBundle;
|
import java.util.stream.Collectors;
|
import javafx.event.ActionEvent;
|
import javafx.event.Event;
|
import javafx.scene.Node;
|
import javafx.scene.layout.Pane;
|
import javafx.stage.Stage;
|
|
/**
|
*
|
* @author ASUS
|
*/
|
public class ModeleJsonEditDialogController extends AbstractEditDialogController<ModeleJson> {
|
|
private ModeleJsonEditFormController modeleJsonEditFormController;
|
|
private StructureChampJsonEditDialogController structureChampJsonEditDialogController;
|
|
private List<StructureChampJson> structJsonToBeDeleted = new ArrayList<>();
|
private final List<SubObject> subObjectToBeDeleted = new ArrayList<>();
|
|
@Override
|
public void initialize(URL url, ResourceBundle rb) {
|
modeleJsonEditFormController = new ModeleJsonEditFormController();
|
|
structureChampJsonEditDialogController = new StructureChampJsonEditDialogController();
|
structureChampJsonEditDialogController.setStructureChampJsonEditFormController(new StructureChampJsonEditFormController());
|
structureChampJsonEditDialogController.setModeleJsonEditFormController(modeleJsonEditFormController);
|
|
structureChampJsonEditDialogController.setSubObjectsList(modeleJsonEditFormController.getSubObjectListe());
|
|
super.initialize(url, rb);
|
}
|
|
@Override
|
public String getTitle() {
|
return "Structure d'un fichier JSON";
|
}
|
|
@Override
|
public Pane getContentFormPane() throws IOException {
|
return ViewLoaderUtil.getPaneFromFxmlFile(
|
modeleJsonEditFormController.getClass().getResource("ModeleJsonEditForm.fxml"),
|
modeleJsonEditFormController
|
);
|
}
|
|
@Override
|
public Object getContentFormController() {
|
return modeleJsonEditFormController;
|
}
|
|
@Override
|
public ModeleJson refreshObjectToUpdate(ModeleJson objectToUpdate) {
|
ModeleJson modeleJson = new ModeleJson();
|
|
modeleJson.setId(objectToUpdate.getId());
|
modeleJson.setObjectName(objectToUpdate.getObjectName());
|
modeleJson.setTypeFichier(objectToUpdate.getTypeFichier());
|
modeleJson.getListeStructureJson().addAll(objectToUpdate.getListeStructureJson());
|
modeleJson.setDateCreation(objectToUpdate.getDateCreation());
|
modeleJson.getSubObjects().addAll(objectToUpdate.getSubObjects());
|
|
if (objectToUpdate.getSubObjects() != null) {
|
modeleJson.getSubObjects().addAll(objectToUpdate.getSubObjects());
|
}
|
|
return modeleJson;
|
}
|
|
@Override
|
protected void beforeBindFormFieldWithCurrentObject(ModeleJson bindObject, Object formController, TypeOperation typeOperation) {
|
|
if (formController instanceof ModeleJsonEditFormController) {
|
ModeleJsonEditFormController controller = (ModeleJsonEditFormController) formController;
|
|
controller.getSubTypeDeleteButton().setOnAction(event -> {
|
removeSubObjectFromList(controller, event);
|
});
|
|
if (typeOperation.equals(TypeOperation.UPDATE) || typeOperation.equals(TypeOperation.VIEW)) {
|
controller.getSubObjectListe().addAll(bindObject.getSubObjects());
|
}
|
|
if (typeOperation.equals(TypeOperation.VIEW)) {
|
|
controller.getSubTypeAddButton().setDisable(true);
|
controller.getSubTypeUpdateButton().setDisable(true);
|
controller.getObjectNameTextField().setDisable(true);
|
}
|
|
if (typeOperation.equals(TypeOperation.ADD) || typeOperation.equals(TypeOperation.UPDATE)) {
|
|
controller.getSubTypeDeleteButton().disableProperty().bind(controller.getSubObjetsTableView().getSelectionModel().selectedItemProperty().isNull());
|
controller.getSubTypeUpdateButton().disableProperty().bind(controller.getSubObjetsTableView().getSelectionModel().selectedItemProperty().isNull());
|
}
|
|
}
|
|
}
|
|
@Override
|
protected void afterBindFormFieldWithCurrentObject(ModeleJson bindObject, Object formController, TypeOperation typeOperation) {
|
if (formController instanceof ModeleJsonEditFormController) {
|
|
ModeleJsonEditFormController controller = (ModeleJsonEditFormController) formController;
|
|
controller.getStructureChampAbstractTable().setController(structureChampJsonEditDialogController);
|
|
switch (typeOperation) {
|
|
case ADD:
|
controller.getTypeFichierAbstractSelectItem().selectedElementProperty().addListener((observable, oldValue, newValue) -> {
|
|
if (newValue != null) {
|
structureChampJsonEditDialogController.setTypeFichier(newValue);
|
}
|
|
});
|
|
break;
|
|
case UPDATE:
|
|
structureChampJsonEditDialogController.setTypeFichier(controller.getTypeFichierAbstractSelectItem().getSelectedElement());
|
|
break;
|
|
}
|
}
|
}
|
|
@Override
|
public boolean beforeSave(ActionEvent event) {
|
boolean proceed = super.beforeSave(event);
|
ModeleJson modeleJson = getCurrentObject();
|
StringBuilder message = new StringBuilder();
|
|
if (modeleJson.getListeStructureJson().isEmpty()) {
|
|
message.append("Bien vouloir remplir la structure des champs\n");
|
proceed = false;
|
|
}
|
|
if (modeleJson.getTypeFichier() == null) {
|
|
message.append("Le type du fichier est obligatoire\n");
|
proceed = false;
|
}
|
|
if (modeleJson.getObjectName() == null || modeleJson.getObjectName().isEmpty()) {
|
|
message.append("Le nom de l'objet à définir est obligatoire");
|
proceed = false;
|
|
} else {
|
getCurrentObject().setObjectName(getCurrentObject().getObjectName().trim());
|
StringBuilder subMessage = Utilities.validateName(getCurrentObject().getObjectName(), "Le nom de l'objet ");
|
|
if (subMessage.length() != 0) {
|
|
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();
|
} else {
|
|
getCurrentObject().setSubObjects(new HashSet<>(modeleJsonEditFormController.getSubObjectListe()));
|
|
getCurrentObject().getSubObjects().forEach(s -> {
|
|
s.setModeleJson(getCurrentObject());
|
|
s.getListeStructureJson().forEach(ss -> {
|
ss.setFieldOfSubObject(s);
|
});
|
});
|
|
}
|
|
try {
|
List<SubObject> liste = new ArrayList<>();
|
liste.addAll(subObjectToBeDeleted);
|
|
for (SubObject s : liste) {
|
SubObjectService.getInstance().delete(s);
|
subObjectToBeDeleted.remove(s);
|
}
|
} catch (Exception ex) {
|
proceed = false;
|
|
AlertMessageUtil.showAlertException(ex, "Une exception s'est produite pendant la supprression", "Erreur");
|
|
}
|
return proceed;
|
}
|
|
public StructureChampJsonEditDialogController getStructureChampJsonEditDialogController() {
|
return structureChampJsonEditDialogController;
|
}
|
|
private void removeSubObjectFromList(ModeleJsonEditFormController controller, Event event) {
|
Object selectedItem = controller.getSubObjetsTableView().getSelectionModel().getSelectedItem();
|
|
if (selectedItem != null) {
|
SubObject subObject = (SubObject) selectedItem;
|
|
if (!findIfSubObjectIsUsed(subObject, controller)) {
|
|
controller.getSubObjectListe().remove(subObject);
|
|
if (subObject.getId() != null && subObject.getId() != 0L && subObject.getId() != 0) {
|
subObjectToBeDeleted.add(subObject);
|
}
|
|
} else {
|
Node source = (Node) event.getSource();
|
Stage parentStage = (Stage) source.getScene().getWindow();
|
NotificationDialog notificationDialog = new NotificationDialog("Echec Suppression : L'objet à supprimer est utilisé dans la déjfinition d'un objet ou d'un champ", NotificationType.ERROR, parentStage);
|
notificationDialog.showNotification();
|
}
|
|
}
|
}
|
|
private boolean findIfSubObjectIsUsed(final SubObject subObject, ModeleJsonEditFormController controller) {
|
|
boolean found;
|
|
//Retirer le subObject concerné de la liste de recherche
|
List<SubObject> subObjectsListe = controller.getSubObjectListe().stream()
|
.filter(p -> {
|
|
if (subObject.getId() == null || subObject.getId() == 0 || subObject.getId() == 0L) {
|
return !p.getDateCreation().equals(subObject.getDateCreation());
|
} else {
|
return !p.getId().equals(subObject.getId());
|
}
|
|
}).collect(Collectors.toList());
|
List<SubObject> subObjectUsingSubObject = new ArrayList<>();
|
|
//Vérifier un sous-objet utilise le sous-objet à supprimer
|
subObjectsListe.forEach(s -> {
|
List<StructureChampJson> structUsingSubObject = s.getListeStructureJson().stream().filter(p -> p.getTypeDonnee().equals(TypeDonnee.OBJET) && p.getTypeOfSubObject().getId().equals(subObject.getId()))
|
.collect(Collectors.toList());
|
|
if (!structUsingSubObject.isEmpty()) {
|
subObjectUsingSubObject.add(s);
|
}
|
});
|
|
//Si aucun sous-objet n'utilise le sous-objet à supprimer
|
if (subObjectUsingSubObject.isEmpty()) {
|
|
List<StructureChampJson> structList = controller.getStructureChampAbstractTable().getElementObservableList()
|
.stream().filter(p -> p.getTypeDonnee().equals(TypeDonnee.OBJET))
|
.collect(Collectors.toList());
|
|
if (!structList.isEmpty()) {
|
|
List<StructureChampJson> structUsingSubObject = structList.stream()
|
.filter(p -> {
|
|
if (p.getTypeDonnee().equals(TypeDonnee.OBJET)) {
|
|
if (subObject.getId() == null || subObject.getId() == 0 || subObject.getId() == 0L) {
|
return !p.getDateCreation().equals(subObject.getDateCreation());
|
|
} else {
|
return !p.getId().equals(subObject.getId());
|
}
|
} else {
|
return false;
|
}
|
|
}
|
)
|
.collect(Collectors.toList());
|
|
found = !structUsingSubObject.isEmpty();
|
|
} else {
|
found = false;
|
}
|
} else {
|
found = false;
|
}
|
return found;
|
}
|
|
}
|