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
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
/*
 * 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.core.impl.helper.referentiel;
 
import com.megatim.fdxcommons.core.ifaces.helper.referentiel.TableDefinitionChecker;
import com.megatim.fdxcommons.model.integration.ColumnDefinition;
import com.megatim.fdxcommons.model.integration.TableDefinition;
import com.megatim.fdxcommons.model.referentiel.natureproduction.NatureProduction;
import com.megatim.fdxcommons.model.referentiel.natureproduction.NatureProductionFichier;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;
import javax.enterprise.context.Dependent;
 
/**
 *
 * @author ASUS
 */
@Dependent
public class TableDefinitionCheckerImpl implements TableDefinitionChecker {
 
    @Override
    public boolean isSame(TableDefinition previousRefTableDef, TableDefinition nextRefTableDef, List<NatureProductionFichier> natureProductionFichiers) {
        if (!previousRefTableDef.getCodeTypeFichier().equals(nextRefTableDef.getCodeTypeFichier())) {
            throw new IllegalArgumentException();
        }
        return isSameNatureProductionFichier(previousRefTableDef.getReferentielVersion(), nextRefTableDef.getReferentielVersion(), natureProductionFichiers)
                && isSame(previousRefTableDef, nextRefTableDef)
                && isSame(nextRefTableDef.getColumnDefinitions(), previousRefTableDef.getColumnDefinitions());
    }
 
    private boolean isSameNatureProductionFichier(String refVersion, String previousRef, List<NatureProductionFichier> natureProductionFichiers) {
        Optional<NatureProductionFichier> natureProdFichOpt = natureProductionFichiers.stream().filter(p -> p.getReferentiel().getVersion().equals(refVersion)).findFirst();
        Optional<NatureProductionFichier> previousNatureProdFichOpt = natureProductionFichiers.stream().filter(p -> p.getReferentiel().getVersion().equals(previousRef)).findFirst();
 
        if (natureProdFichOpt.isPresent() && previousNatureProdFichOpt.isPresent()) {
            return natureProdFichOpt.get().getNatureProduction().equals(previousNatureProdFichOpt.get().getNatureProduction());
        } else if (!natureProdFichOpt.isPresent() && !previousNatureProdFichOpt.isPresent()) {
            return true;
        } else {
            if (natureProdFichOpt.isPresent()) {
                return natureProdFichOpt.get().getNatureProduction().equals(NatureProduction.REFERENTIELLE);
            } else {
                return previousNatureProdFichOpt.get().getNatureProduction().equals(NatureProduction.REFERENTIELLE);
            }
        }
    }
 
    private boolean isSame(TableDefinition previousRefTableDef, TableDefinition nextRefTableDef) {
        return isSameColumnDelimiter(previousRefTableDef, nextRefTableDef)
                && isSameLineDelimiter(previousRefTableDef, nextRefTableDef)
                && isSameHeaderPresent(previousRefTableDef, nextRefTableDef);
    }
 
    private boolean isSameColumnDelimiter(TableDefinition previousRefTableDef, TableDefinition nextRefTableDef) {
        if (previousRefTableDef.getColumnDelimiter() != null && nextRefTableDef.getColumnDelimiter() != null) {
            return previousRefTableDef.getColumnDelimiter().equals(nextRefTableDef.getColumnDelimiter());
        } else {
            return previousRefTableDef.getColumnDelimiter() == null && nextRefTableDef.getColumnDelimiter() == null;
        }
    }
 
    private boolean isSameLineDelimiter(TableDefinition previousRefTableDef, TableDefinition nextRefTableDef) {
        if (previousRefTableDef.getLineDelimiter() != null && nextRefTableDef.getLineDelimiter() != null) {
            return previousRefTableDef.getLineDelimiter().equals(nextRefTableDef.getLineDelimiter());
        } else {
            return previousRefTableDef.getLineDelimiter() == null && nextRefTableDef.getLineDelimiter() == null;
        }
    }
 
    private boolean isSameHeaderPresent(TableDefinition previousRefTableDef, TableDefinition nextRefTableDef) {
        if (previousRefTableDef.getHeaderPresent() != null && nextRefTableDef.getHeaderPresent() != null) {
            return previousRefTableDef.getHeaderPresent().equals(nextRefTableDef.getHeaderPresent());
        } else {
            return previousRefTableDef.getHeaderPresent() == null && nextRefTableDef.getHeaderPresent() == null;
        }
    }
 
    private boolean isSame(List<ColumnDefinition> newRefColumnDef, List<ColumnDefinition> oldRefColumnDef) {
        boolean isSame = true;
 
        if (newRefColumnDef.size() != oldRefColumnDef.size()) {
            return false;
        }
        Map<String, ColumnDefinition> currentRefColDefToMap = newRefColumnDef.stream().collect(Collectors.toMap(ColumnDefinition::getName, Function.identity()));
 
        for (ColumnDefinition c : oldRefColumnDef) {
            ColumnDefinition oldColDef = currentRefColDefToMap.get(c.getName());
 
            if (oldColDef == null) {
                isSame = false;
                break;
            }
 
            isSame = isSameFormatDate(c, oldColDef)
                    && isSamePosition(c, oldColDef)
                    && isSameTypeDonnee(c, oldColDef)
                    && isSameTaille(c, oldColDef)
                    && isSameTaillePartieDecimale(c, oldColDef);
 
            if (!isSame) {
                break;
            }
        }
        return isSame;
    }
 
    private boolean isSameFormatDate(ColumnDefinition newColDef, ColumnDefinition oldColDef) {
        return (newColDef.getFormatDate() == null ? oldColDef.getFormatDate() == null : newColDef.getFormatDate().trim().equalsIgnoreCase(oldColDef.getFormatDate()));
    }
 
    private boolean isSamePosition(ColumnDefinition newColDef, ColumnDefinition oldColDef) {
        return newColDef.getPosition() == oldColDef.getPosition();
    }
 
    private boolean isSameTypeDonnee(ColumnDefinition newColDef, ColumnDefinition oldColDef) {
        return (newColDef.getTypeDonnee() == null ? oldColDef.getTypeDonnee() == null : newColDef.getTypeDonnee().trim().equalsIgnoreCase(oldColDef.getTypeDonnee()));
    }
 
    private boolean isSameTaille(ColumnDefinition newColDef, ColumnDefinition oldColDef) {
        return newColDef.getTaille() == oldColDef.getTaille();
    }
 
    private boolean isSameTaillePartieDecimale(ColumnDefinition newColDef, ColumnDefinition oldColDef) {
        return newColDef.getTaillePartieDecimale() == oldColDef.getTaillePartieDecimale();
    }
}