Kenmegne
6 days ago 908e81dd173f380879d5fabeb5794d5b7a76df0e
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/*
 * 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;
    }
 
}