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
241
242
243
244
/*
 * 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<StructureChampJson> {
 
    private StructureChampJsonEditFormController structureChampJsonEditFormController;
 
    private SubObjectEditFormController subObjectEditFormController;
 
    private ObservableList<SubObject> 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<SubObject> 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<StructureChampJson> observableList) {
        String message = "";
 
        List<StructureChampJson> 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;
    }
 
}