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;
|
}
|
}
|