/*
|
* 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();
|
}
|
}
|