/*
|
* 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.App;
|
import com.megatim.fdxconvert.forms.ModeleJsonEditFormController;
|
import com.megatim.fdxconvert.forms.SubObjectEditFormController;
|
import com.megatim.fdxconvert.model.StructureChampJson;
|
import com.megatim.fdxconvert.model.SubObject;
|
import com.megatim.fdxconvert.service.StructureChampJsonService;
|
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.collections.ObservableList;
|
import javafx.event.ActionEvent;
|
import javafx.scene.Node;
|
import javafx.scene.layout.Pane;
|
import javafx.stage.Stage;
|
|
/**
|
*
|
* @author ASUS
|
*/
|
public class SubObjectEditDialogController extends AbstractEditDialogController<SubObject> {
|
|
private SubObjectEditFormController subObjectEditFormController;
|
private ModeleJsonEditFormController modeleJsonEditFormController;
|
|
private List<StructureChampJson> structJsonToBeDeleted = new ArrayList<>();
|
private ObservableList<SubObject> subObjectsList;
|
|
@Override
|
public void initialize(URL url, ResourceBundle rb) {
|
|
subObjectEditFormController = new SubObjectEditFormController();
|
subObjectEditFormController.setCurrentSubObjectsListe(subObjectsList);
|
|
super.initialize(url, rb);
|
}
|
|
@Override
|
public String getTitle() {
|
return "Edition d'un sous-type";
|
}
|
|
@Override
|
public Pane getContentFormPane() throws IOException {
|
return ViewLoaderUtil.getPaneFromFxmlFile(
|
subObjectEditFormController.getClass().getResource("SubObjectEditForm.fxml"),
|
subObjectEditFormController
|
);
|
}
|
|
@Override
|
public Object getContentFormController() {
|
return subObjectEditFormController;
|
}
|
|
public void setSubObjectEditFormController(SubObjectEditFormController subObjectEditFormController) {
|
this.subObjectEditFormController = subObjectEditFormController;
|
}
|
|
public void setModeleJsonEditFormController(ModeleJsonEditFormController modeleJsonEditFormController) {
|
this.modeleJsonEditFormController = modeleJsonEditFormController;
|
}
|
|
public void setSubObjectsList(ObservableList<SubObject> subObjectsList) {
|
this.subObjectsList = subObjectsList;
|
}
|
|
@Override
|
public SubObject refreshObjectToUpdate(SubObject objectToUpdate) {
|
SubObject subObject = new SubObject();
|
|
subObject.setId(objectToUpdate.getId());
|
subObject.getListeStructureJson().addAll(objectToUpdate.getListeStructureJson());
|
subObject.setModeleJson(objectToUpdate.getModeleJson());
|
subObject.setSubObjectName(objectToUpdate.getSubObjectName());
|
subObject.setDateCreation(objectToUpdate.getDateCreation());
|
|
return subObject;
|
}
|
|
@Override
|
public void afterSave(ActionEvent event) {
|
SubObject subObject = getCurrentObject();
|
subObject.setListeStructureJson(new HashSet<>(subObjectEditFormController.getSubStructureJsonListe()));
|
}
|
|
@Override
|
protected void beforeBindFormFieldWithCurrentObject(SubObject bindObject, Object formController, TypeOperation typeOperation) {
|
|
if (formController instanceof SubObjectEditFormController) {
|
|
SubObjectEditFormController controller = (SubObjectEditFormController) formController;
|
|
controller.getDeleteFieldButton().setOnMouseClicked(event -> {
|
removeStructureJsonFromList(controller);
|
});
|
|
if (typeOperation.equals(TypeOperation.UPDATE) || typeOperation.equals(TypeOperation.VIEW)) {
|
|
//Filtrer afin de ne garder que les champs des sous-objets
|
controller.getSubStructureJsonListe().addAll(bindObject.getListeStructureJson()
|
.stream().filter(p -> p.getModeleJson() == null)
|
.collect(Collectors.toList()));
|
|
controller.getSubStructureJsonListe().forEach(s -> {
|
|
if (s.getTypeDonnee().equals(TypeDonnee.DATE) && s.getCodeDelimiteurDate() != null) {
|
s.setDelimiteurDate(App.SEPARATEURS_DATE.get(s.getCodeDelimiteurDate()));
|
}
|
});
|
}
|
|
if (typeOperation.equals(TypeOperation.VIEW)) {
|
controller.getSubObjectNameTextField().setDisable(true);
|
controller.getAddFieldButton().setDisable(true);
|
}
|
|
if (typeOperation.equals(TypeOperation.UPDATE) || typeOperation.equals(TypeOperation.ADD)) {
|
controller.getDeleteFieldButton().disableProperty().bind(controller.getSubObjectsFieldsTableView().getSelectionModel().selectedItemProperty().isNull());
|
controller.getUpdateFieldButton().disableProperty().bind(controller.getSubObjectsFieldsTableView().getSelectionModel().selectedItemProperty().isNull());
|
|
}
|
}
|
}
|
|
@Override
|
public boolean beforeSave(ActionEvent event) {
|
boolean proceed = super.beforeSave(event);
|
StringBuilder message = new StringBuilder();
|
|
if (getCurrentObject().getSubObjectName() == null || getCurrentObject().getSubObjectName().isEmpty()) {
|
|
message.append("Bien vouloir renseigner le nom de l'objet\n");
|
proceed = false;
|
|
} else {
|
getCurrentObject().setSubObjectName(getCurrentObject().getSubObjectName().trim());
|
StringBuilder subMessage = Utilities.validateName(getCurrentObject().getSubObjectName(), "Le nom de l'objet ");
|
|
if (subMessage.length() != 0) {
|
|
message.append(subMessage);
|
proceed = false;
|
}
|
}
|
|
if (subObjectEditFormController.getSubStructureJsonListe().isEmpty()) {
|
|
message.append("Bien vouloir renseigner la structure des champs de l'objet");
|
proceed = false;
|
|
}
|
|
String subMessage = "";
|
|
if (modeleJsonEditFormController != null) {
|
subMessage = verifyIfSubObjectAlreadyExist(modeleJsonEditFormController.getSubObjectListe());
|
}
|
|
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();
|
}
|
|
try {
|
List<StructureChampJson> liste = new ArrayList<>();
|
liste.addAll(structJsonToBeDeleted);
|
|
for (StructureChampJson s : liste) {
|
StructureChampJsonService.getInstance().delete(s);
|
structJsonToBeDeleted.remove(s);
|
}
|
} catch (Exception ex) {
|
proceed = false;
|
|
AlertMessageUtil.showAlertException(ex, "Une exception s'est produite pendant la supprression", "Erreur");
|
|
}
|
return proceed;
|
}
|
|
private String verifyIfSubObjectAlreadyExist(List<SubObject> observableList) {
|
String message = "";
|
|
List<SubObject> foundListe = observableList.stream().filter(p
|
-> {
|
boolean testLibelle = p.getSubObjectName().equalsIgnoreCase(getCurrentObject().getSubObjectName());
|
|
return (testLibelle && getTypeOperation().equals(TypeOperation.ADD))
|
|| (getTypeOperation().equals(TypeOperation.UPDATE) && testLibelle && !p.equals(getCurrentObject()));
|
|
}
|
).collect(Collectors.toList());
|
|
if (!foundListe.isEmpty()) {
|
message = "Le libellé \"" + getCurrentObject().getSubObjectName() + "\"" + " est déjà utilisé par un autre objet \n";
|
}
|
|
return message;
|
}
|
|
private void removeStructureJsonFromList(SubObjectEditFormController controller) {
|
Object selectedItem = controller.getSubObjectsFieldsTableView().getSelectionModel().getSelectedItem();
|
|
if (selectedItem != null) {
|
StructureChampJson structureChampJson = (StructureChampJson) controller.getSubObjectsFieldsTableView().getSelectionModel().getSelectedItem();
|
controller.getSubStructureJsonListe().remove(structureChampJson);
|
|
if (structureChampJson.getId() != null && structureChampJson.getId() != 0L && structureChampJson.getId() != 0) {
|
structJsonToBeDeleted.add(structureChampJson);
|
}
|
}
|
}
|
}
|