Kenmegne
7 days ago 23a46b4be35277e06ec89f48730eeb694e686be8
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
/*
 * 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.fdxcommons.tools.database.queries.metadata;
 
import com.megatim.fdxcommons.model.enumeration.TypeDonnee;
import com.megatim.fdxcommons.model.integration.json.JsonStructure;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.naming.NamingException;
 
/**
 *
 * @author ASUS
 */
public class JsonFdxTableStructure {
 
    private final String referentielVersion;
    private final String codeTypeFichier;
    private final Connection connection;
 
    public JsonFdxTableStructure(String referentielVersion, String codeTypeFichier, Connection connection) {
        this.referentielVersion = referentielVersion;
        this.codeTypeFichier = codeTypeFichier;
        this.connection = connection;
    }
 
    public JsonStructure jsonStructure() throws NamingException, SQLException {
        JsonStructure jsonStructure = null;
        Long jsonStructureId = jsonStructureId();
 
        if (jsonStructureId != null) {
            String query = "SELECT * FROM JsonStructure j where j.id = ?";
 
            try ( PreparedStatement stmt = connection.prepareStatement(query)) {
                stmt.setLong(1, jsonStructureId);
                ResultSet resultSet = stmt.executeQuery();
 
                if (resultSet.next()) {
                    jsonStructure = new JsonStructure();
                    jsonStructure.setId(resultSet.getLong("id"));
                    jsonStructure.setLengthh(resultSet.getInt("lengthh"));
                    jsonStructure.setTypeDonnee(TypeDonnee.fromValeur(resultSet.getString("typedonnee")));
                    jsonStructure.setName(resultSet.getString("name"));
                    jsonStructure.setRequired(resultSet.getBoolean("required"));
                    jsonStructure.setCollection(resultSet.getBoolean("collection"));
                    jsonStructure.setFormatDate(resultSet.getString("formatdate"));
                    
                }
            }
        }
 
        return jsonStructure;
    }
 
    private Long jsonStructureId() throws SQLException {
        String query = "SELECT json_structure_id FROM typeFichierJson t WHERE t.type_fichier_code = ? AND t.referentiel_version = ?";
 
        try ( PreparedStatement stmt = connection.prepareStatement(query)) {
            stmt.setString(1, codeTypeFichier);
            stmt.setString(2, referentielVersion);
            ResultSet resultSet = stmt.executeQuery();
 
            if (resultSet.next()) {
                return resultSet.getLong(1);
 
            }
 
        }
        return null;
    }
}