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
/*
 * 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.util;
 
import com.megatim.fdxconvert.model.JsonStructure;
import com.megatim.fdxconvert.model.TypeFichierJson;
import com.megatim.fdxconvert.model.jaxb.TypeFichierJsonsList;
import java.io.File;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
 
/**
 *
 * @author ASUS
 */
public class TypeFichierJsonMarshaller {
 
    public static void marshall(List<TypeFichierJson> typeFichierJsons, String destinationDir) throws JAXBException {
 
        if (typeFichierJsons == null) {
            typeFichierJsons = new ArrayList<>();
        }
        JAXBContext context = JAXBContext.newInstance(TypeFichierJsonsList.class);
        Marshaller marshaller = context.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
 
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
        String date = formatter.format(LocalDateTime.now());
        marshaller.marshal(new TypeFichierJsonsList(typeFichierJsons), new File(destinationDir, "typeFichierJsons" + date + ".xml"));
    }
 
    public static void marshall(String destinationDir, TypeFichierJson typeFichierJson) throws JAXBException {
 
        if (typeFichierJson == null) {
            return;
        }
        JAXBContext context = JAXBContext.newInstance(TypeFichierJson.class);
        Marshaller marshaller = context.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
 
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
        String date = formatter.format(LocalDateTime.now());
 
        marshaller.marshal(typeFichierJson, new File(destinationDir, typeFichierJson.getTypeFichier().getCode() + date + ".xml"));
    }
 
    public static TypeFichierJsonsList unMarshall(String filePath) throws JAXBException {
        JAXBContext context = JAXBContext.newInstance(TypeFichierJsonsList.class);
        Unmarshaller unmarshaller = context.createUnmarshaller();
 
        TypeFichierJsonsList typeFichierJsonsList = (TypeFichierJsonsList) unmarshaller.unmarshal(new File(filePath));
        typeFichierJsonsList.getTypeFichierJson().forEach(t -> setParent(t.getJsonStructure()));
 
        return typeFichierJsonsList;
    }
 
    public static TypeFichierJson unMarshallOne(String filePath) throws JAXBException {
        JAXBContext context = JAXBContext.newInstance(TypeFichierJson.class);
        Unmarshaller unmarshaller = context.createUnmarshaller();
 
        TypeFichierJson typeFichierJson = (TypeFichierJson) unmarshaller.unmarshal(new File(filePath));
        setParent(typeFichierJson.getJsonStructure());
 
        return typeFichierJson;
    }
 
    private static void setParent(JsonStructure jsonStruct) {
        jsonStruct.getFields().forEach(f -> {
            f.setParent(jsonStruct);
            setParent(f);
        });
    }
}