Kenmegne
7 days ago b3d0580439b9a00c7eb918085de1694151066004
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/*
 * 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);
            }
        }
    }
}