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
package com.megatim.fdxconvert.viewmodel;
 
import com.megatim.dynamicjsonparser.enums.TypeDonnee;
import com.megatimfx.components.customdialogs.AlertMessageUtil;
import com.megatim.fdxconvert.model.JsonStructure;
import com.megatim.fdxconvert.model.TypeFichier;
import com.megatim.fdxconvert.model.TypeFichierJson;
import com.megatim.fdxconvert.service.TypeFichierJsonService;
import com.megatim.fdxconvert.service.TypeFichierService;
import com.megatim.fdxconvert.views.jsonstructure.NestedJsonStructure;
import javafx.beans.property.*;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.concurrent.Task;
 
import java.util.Set;
import java.util.stream.Collectors;
 
public class JsonStructureFormViewModel {
 
    private final ObservableList<TypeFichier> typeFichiers = FXCollections.observableArrayList();
    private final BooleanProperty loading = new SimpleBooleanProperty(false);
    private final ObjectProperty<TypeFichier> selectedTypeFichier;
    private final ObjectProperty<TypeFichierJson> result = new SimpleObjectProperty<>(null);
    private final TypeFichierJson typeFichierJson;
 
    public JsonStructureFormViewModel(TypeFichierJson typeFichierJson) {
        this.typeFichierJson = typeFichierJson != null ? typeFichierJson : new TypeFichierJson();
        selectedTypeFichier = new SimpleObjectProperty<>(typeFichierJson != null ? typeFichierJson.getTypeFichier() : null);
    }
 
    public void init() {
        loadTypeFichiersFromDB();
    }
 
    private void loadTypeFichiersFromDB() {
        Task<Set<TypeFichier>> task = new Task() {
            @Override
            protected Object call() throws Exception {
                return TypeFichierService.getInstance().getAll();
            }
        };
        task.setOnRunning(e -> loading.set(true));
        task.setOnSucceeded(e -> {
            loading.set(false);
            typeFichiers.clear();
            typeFichiers.setAll(task.getValue());
        });
        task.setOnFailed(e -> {
            loading.set(false);
            AlertMessageUtil.showAlertException(task.getException(), "Une exception s'est produite pendant le traitement", "Erreur");
        });
        Thread thread = new Thread(task);
        thread.setDaemon(true);
        thread.start();
    }
 
    public void createTypeFichierJSON(NestedJsonStructure nestedJsonStructure) {
        Task<TypeFichierJson> task = new Task() {
            @Override
            protected TypeFichierJson call() throws Exception {
                typeFichierJson.setJsonStructure(jsonStructure(nestedJsonStructure, null));
                if(typeFichierJson.getId() == null) {
                    typeFichierJson.setTypeFichier(selectedTypeFichier.get());
                    return TypeFichierJsonService.getInstance().add(typeFichierJson);
                }
                return TypeFichierJsonService.getInstance().edit(typeFichierJson);
            }
        };
        task.setOnRunning(e -> loading.set(true));
        task.setOnSucceeded(e -> {
            loading.set(false);
            result.set(typeFichierJson);
        });
        task.setOnFailed(e -> {
            loading.set(false);
            AlertMessageUtil.showAlertException(task.getException(), "Une exception s'est produite pendant le traitement", "Erreur");
        });
        Thread thread = new Thread(task);
        thread.setDaemon(true);
        thread.start();
    }
 
    private JsonStructure jsonStructure(NestedJsonStructure nestedJsonStructure, JsonStructure parent) {
        JsonStructure jsonStructure = new JsonStructure();
        jsonStructure.setName(nestedJsonStructure.name());
        jsonStructure.setFormatDate(nestedJsonStructure.format());
        jsonStructure.setTypeDonnee(nestedJsonStructure.typeDonnee());
        jsonStructure.setLengthh(nestedJsonStructure.taille());
        jsonStructure.setRequired(nestedJsonStructure.isRequired());
        jsonStructure.setCollection(nestedJsonStructure.isCollection());
 
        if (nestedJsonStructure.typeDonnee().equals(TypeDonnee.DATE)) {
            jsonStructure.setCodeDelimiteurDate(nestedJsonStructure.delimiteur().getCode());
        }
 
        jsonStructure.setParent(parent);
 
        jsonStructure.setFields(nestedJsonStructure.children().stream().map(nJ -> jsonStructure(nJ, jsonStructure)).collect(Collectors.toList()));
 
        return jsonStructure;
    }
 
    public ObjectProperty<TypeFichierJson> resultProperty() {
        return result;
    }
 
    public ObjectProperty<TypeFichier> selectedTypeFichierProperty() {
        return selectedTypeFichier;
    }
 
    public ObservableList<TypeFichier> getTypeFichiers() {
        return typeFichiers;
    }
}