Skip to content

Commit

Permalink
wip: fileIdCard refactor controle
Browse files Browse the repository at this point in the history
  • Loading branch information
lmanelphe committed Nov 7, 2023
1 parent cf01506 commit 13e397b
Show file tree
Hide file tree
Showing 10 changed files with 182 additions and 152 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,43 +8,34 @@
import fr.insee.arc.utils.exception.ArcExceptionMessage;

public enum TypeNormage {
RELATION("relation"),
CARTESIAN("cartesian"),
UNICITE("unicité"),
PARTITION("partition"),
INDEPENDANCE("independance"),
BLOC_INDEPENDANCE("bloc_independance"),
DELETION("deletion"),
DUPLICATION("duplication")
;

RELATION("relation"), CARTESIAN("cartesian"), UNICITE("unicité"), PARTITION("partition"),
INDEPENDANCE("independance"), BLOC_INDEPENDANCE("bloc_independance"), DELETION("deletion"),
DUPLICATION("duplication");

private String nom;

private TypeNormage(String nom) {
this.nom = nom;
}

public String getNom() {
return nom;
}

public void setNom(String nom) {
this.nom = nom;
}


public static TypeNormage getEnum(String code) throws ArcException {

List<TypeNormage> filtered = Arrays.asList(TypeNormage.values()).stream().filter(t -> t.getNom().equals(code)).collect(Collectors.toList());

if (filtered.isEmpty())
{
throw new ArcException(ArcExceptionMessage.NORMAGE_TYPE_NOT_FOUND, code);
}

return filtered.get(0);
}



private TypeNormage(String nom) {
this.nom = nom;
}

public String getNom() {
return nom;
}

public void setNom(String nom) {
this.nom = nom;
}

public static TypeNormage getEnum(String code) throws ArcException {

List<TypeNormage> filtered = Arrays.asList(TypeNormage.values()).stream().filter(t -> t.getNom().equals(code))
.collect(Collectors.toList());

if (filtered.isEmpty()) {
throw new ArcException(ArcExceptionMessage.NORMAGE_TYPE_NOT_FOUND, code);
}

return filtered.get(0);
}

}
Original file line number Diff line number Diff line change
@@ -1,34 +1,46 @@
package fr.insee.arc.core.service.p4controle.bo;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import fr.insee.arc.utils.exception.ArcException;
import fr.insee.arc.utils.exception.ArcExceptionMessage;

/**
* code des types de controle proposés par ARC
* code des types de controle proposés par ARC
*
* @author FY2QEQ
*
*/
public enum ControleTypeCode {
NUM, DATE, ALPHANUM, CARDINALITE, CONDITION, REGEXP, ENUM_BRUTE, ENUM_TABLE;

public static ControleTypeCode getEnum(String code) {
switch (code) {
case "NUM":
return NUM;
case "DATE":
return DATE;
case "ALPHANUM":
return ALPHANUM;
case "CARDINALITE":
return CARDINALITE;
case "CONDITION":
return CONDITION;
case "REGEXP":
return REGEXP;
case "ENUM_BRUTE":
return ENUM_BRUTE;
case "ENUM_TABLE":
return ENUM_TABLE;
default:
return null;
NUM("NUM"), DATE("DATE"), ALPHANUM("ALPHANUM"), CARDINALITE("CARDINALITE"), CONDITION("CONDITION"),
REGEXP("REGEXP"), ENUM_BRUTE("ENUM_BRUTE"), ENUM_TABLE("ENUM_TABLE");

private String nom;

private ControleTypeCode(String nom) {
this.nom = nom;
}

public String getNom() {
return nom;
}

public void setNom(String nom) {
this.nom = nom;
}

public static ControleTypeCode getEnum(String code) throws ArcException {

List<ControleTypeCode> filtered = Arrays.asList(ControleTypeCode.values()).stream()
.filter(t -> t.getNom().equals(code)).collect(Collectors.toList());

if (filtered.isEmpty()) {
throw new ArcException(ArcExceptionMessage.CONTROLE_TYPE_NOT_FOUND, code);
}

return filtered.get(0);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public class RegleControle {
private String condition;

private String preAction; // aka prétraitement SQL
private int idRegle;

private int xsdOrdre;
private String xsdLabelFils;
Expand All @@ -20,7 +21,7 @@ public class RegleControle {
private String traitementLignesErreur; // aka error_row_processing

public RegleControle(ControleTypeCode typeControle, String rubriquePere, String rubriqueFils, String borneInf,
String borneSup, String condition, String preAction, int xsdOrdre, String xsdLabelFils, String xsdRole,
String borneSup, String condition, String preAction, int idRegle, int xsdOrdre, String xsdLabelFils, String xsdRole,
String seuilBloquant, String traitementLignesErreur) {
super();
this.typeControle = typeControle;
Expand All @@ -30,13 +31,18 @@ public RegleControle(ControleTypeCode typeControle, String rubriquePere, String
this.borneSup = borneSup;
this.condition = condition;
this.preAction = preAction;
this.idRegle = idRegle;
this.xsdOrdre = xsdOrdre;
this.xsdLabelFils = xsdLabelFils;
this.xsdRole = xsdRole;
this.seuilBloquant = seuilBloquant;
this.traitementLignesErreur = traitementLignesErreur;
}

public RegleControle() {
super();
}

public ControleTypeCode getTypeControle() {
return typeControle;
}
Expand Down Expand Up @@ -93,6 +99,14 @@ public void setPreAction(String preAction) {
this.preAction = preAction;
}

public int getIdRegle() {
return idRegle;
}

public void setIdRegle(int idRegle) {
this.idRegle = idRegle;
}

public int getXsdOrdre() {
return xsdOrdre;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Component;

import fr.insee.arc.core.service.global.bo.RegleControleEntity;
import fr.insee.arc.core.service.p4controle.bo.ControleMarkCode;
import fr.insee.arc.core.service.p4controle.bo.ControleXsdCode;
import fr.insee.arc.core.service.p4controle.bo.RegleControle;
import fr.insee.arc.core.service.p4controle.bo.XsdDate;
import fr.insee.arc.core.util.StaticLoggerDispatcher;
import fr.insee.arc.utils.utils.FormatSQL;
Expand Down Expand Up @@ -168,12 +168,12 @@ private String insertBloc(String blockingThreshold, String errorRowProcessing, S
* @param reg
* @return
*/
public String ctlIsNumeric(RegleControleEntity reg) {
public String ctlIsNumeric(RegleControle reg) {
String cond = conditionLongueur(reg);
String requete = "WITH ctl AS ( SELECT id FROM " + this.tableTempData + " "
+ " WHERE ({2} ~ '^-?\\d*(\\.\\d+)?$') IS FALSE " + cond + ") "
+ insertBloc(reg.getBlockingThreshold(), reg.getErrorRowProcessing(), reg.getIdRegle());
requete = getRequete(requete, TABLE_TEMP_MARK, reg.getIdRegle(), reg.getRubriquePere());
+ insertBloc(reg.getSeuilBloquant(), reg.getTraitementLignesErreur(), Integer.toString(reg.getIdRegle()));
requete = getRequete(requete, TABLE_TEMP_MARK, Integer.toString(reg.getIdRegle()), reg.getRubriquePere());
return requete;
}

Expand All @@ -185,7 +185,7 @@ public String ctlIsNumeric(RegleControleEntity reg) {
*
* @param reg la règle à appliquer
*/
public String ctlIsDate(RegleControleEntity reg) {
public String ctlIsDate(RegleControle reg) {
StringBuilder reqBuilder = new StringBuilder("WITH ctl AS (SELECT id FROM " + this.tableTempData + " ");
String requete = "";
if (reg.getCondition().equalsIgnoreCase(ControleXsdCode.XSD_DATE_NAME)
Expand All @@ -202,28 +202,28 @@ public String ctlIsDate(RegleControleEntity reg) {
reqBuilder.append(") THEN false ");
reqBuilder.append("\n WHEN {2} is null THEN false ");
reqBuilder.append("\n ELSE true END) ");
reqBuilder.append(insertBloc(reg.getBlockingThreshold(), reg.getErrorRowProcessing(), reg.getIdRegle()));
requete = getRequete(reqBuilder.toString(), TABLE_TEMP_MARK, reg.getIdRegle(), reg.getRubriquePere());
reqBuilder.append(insertBloc(reg.getSeuilBloquant(), reg.getTraitementLignesErreur(), Integer.toString(reg.getIdRegle())));
requete = getRequete(reqBuilder.toString(), TABLE_TEMP_MARK, Integer.toString(reg.getIdRegle()), reg.getRubriquePere());
} else {
reqBuilder.append("\n WHERE CASE WHEN arc.isdate({2},'{3}') THEN false");
reqBuilder.append("\n WHEN {2} is null THEN false ");
reqBuilder.append("\n ELSE true END) ");
reqBuilder.append(insertBloc(reg.getBlockingThreshold(), reg.getErrorRowProcessing(), reg.getIdRegle()));
requete = getRequete(reqBuilder.toString(), TABLE_TEMP_MARK, reg.getIdRegle(), reg.getRubriquePere(),
reqBuilder.append(insertBloc(reg.getSeuilBloquant(), reg.getTraitementLignesErreur(), Integer.toString(reg.getIdRegle())));
requete = getRequete(reqBuilder.toString(), TABLE_TEMP_MARK, Integer.toString(reg.getIdRegle()), reg.getRubriquePere(),
reg.getCondition());
}
return requete;
}

public String ctlIsAlphanum(RegleControleEntity reg) {
public String ctlIsAlphanum(RegleControle reg) {
String cond = conditionLongueur(reg);
String requete = "WITH ctl AS (SELECT id FROM " + this.tableTempData + " WHERE false " + cond + ")"
+ insertBloc(reg.getBlockingThreshold(), reg.getErrorRowProcessing(), reg.getIdRegle());
requete = getRequete(requete, TABLE_TEMP_MARK, reg.getIdRegle());
+ insertBloc(reg.getSeuilBloquant(), reg.getTraitementLignesErreur(), Integer.toString(reg.getIdRegle()));
requete = getRequete(requete, TABLE_TEMP_MARK, Integer.toString(reg.getIdRegle()));
return requete;
}

public String ctlCardinalite(RegleControleEntity reg, List<String> listRubriqueExpr,
public String ctlCardinalite(RegleControle reg, List<String> listRubriqueExpr,
List<String> ListRubriqueTable) {

// on retire le pere et le fils de la liste d'expr
Expand Down Expand Up @@ -306,9 +306,9 @@ public String ctlCardinalite(RegleControleEntity reg, List<String> listRubriqueE
requete.append(" SELECT a.id FROM " + this.tableTempData + " a ");
requete.append(" INNER JOIN trav ON row(a.{2})::text collate \"C\"=row(trav.{2})::text collate \"C\" ");
requete.append(" ) ");
requete.append(insertBloc(reg.getBlockingThreshold(), reg.getErrorRowProcessing(), reg.getIdRegle()));
requete.append(insertBloc(reg.getSeuilBloquant(), reg.getTraitementLignesErreur(), Integer.toString(reg.getIdRegle())));

return getRequete(requete.toString(), TABLE_TEMP_MARK, reg.getIdRegle(), reg.getRubriquePere(),
return getRequete(requete.toString(), TABLE_TEMP_MARK, Integer.toString(reg.getIdRegle()), reg.getRubriquePere(),
reg.getRubriqueFils());
}

Expand All @@ -320,14 +320,14 @@ public String ctlCardinalite(RegleControleEntity reg, List<String> listRubriqueE
* @param mapRubrique
* @return
*/
public String ctlCondition(RegleControleEntity reg, Map<String, RegleControleEntity> mapRubrique) {
public String ctlCondition(RegleControle reg, Map<String, RegleControle> mapRubrique) {
String filtre = writeFiltre(mapRubrique);
String cond = rewriteCondition(mapRubrique, reg.getCondition());
String requete = "WITH ctl AS ( select id from (SELECT id, " + " CASE WHEN " + filtre + " THEN CASE WHEN "
+ cond + " THEN FALSE ELSE TRUE END ELSE FALSE END as condition_a_tester " + " FROM "
+ this.tableTempData + " " + ") ww where condition_a_tester ) "
+ insertBloc(reg.getBlockingThreshold(), reg.getErrorRowProcessing(), reg.getIdRegle());
requete = getRequete(requete, TABLE_TEMP_MARK, reg.getIdRegle());
+ insertBloc(reg.getSeuilBloquant(), reg.getTraitementLignesErreur(), Integer.toString(reg.getIdRegle()));
requete = getRequete(requete, TABLE_TEMP_MARK, Integer.toString(reg.getIdRegle()));
return requete;
}

Expand All @@ -338,10 +338,10 @@ public String ctlCondition(RegleControleEntity reg, Map<String, RegleControleEnt
*
* @param reg la règle à appliquer
*/
public String ctlMatchesRegexp(RegleControleEntity reg) {
public String ctlMatchesRegexp(RegleControle reg) {
String requete = "WITH ctl AS (SELECT id FROM " + this.tableTempData + " " + "WHERE ({2} ~ '{3}') IS FALSE ) "
+ insertBloc(reg.getBlockingThreshold(), reg.getErrorRowProcessing(), reg.getIdRegle());
requete = getRequete(requete, TABLE_TEMP_MARK, reg.getIdRegle(), reg.getRubriquePere(), reg.getCondition());
+ insertBloc(reg.getSeuilBloquant(), reg.getTraitementLignesErreur(), Integer.toString(reg.getIdRegle()));
requete = getRequete(requete, TABLE_TEMP_MARK, Integer.toString(reg.getIdRegle()), reg.getRubriquePere(), reg.getCondition());
return requete;
}

Expand All @@ -358,11 +358,11 @@ public String ctlMatchesRegexp(RegleControleEntity reg) {
*
* @param reg la règle à appliquer
*/
public String ctlIsValueIn(RegleControleEntity reg) {
public String ctlIsValueIn(RegleControle reg) {
String requete = "WITH " + "ctl AS ( SELECT id " + " FROM " + this.tableTempData + " "
+ "WHERE {2} not in ({3}) ) "
+ insertBloc(reg.getBlockingThreshold(), reg.getErrorRowProcessing(), reg.getIdRegle());
requete = getRequete(requete, TABLE_TEMP_MARK, reg.getIdRegle(), reg.getRubriquePere(), reg.getCondition());
+ insertBloc(reg.getSeuilBloquant(), reg.getTraitementLignesErreur(), Integer.toString(reg.getIdRegle()));
requete = getRequete(requete, TABLE_TEMP_MARK, Integer.toString(reg.getIdRegle()), reg.getRubriquePere(), reg.getCondition());
return requete;
}

Expand All @@ -373,7 +373,7 @@ public String ctlIsValueIn(RegleControleEntity reg) {
* @param reg
* @return
*/
private String conditionCardinalite(RegleControleEntity reg) {
private String conditionCardinalite(RegleControle reg) {
String cond = "";
String rubrique = reg.getRubriquePere();
String rubriqueF = reg.getRubriqueFils();
Expand Down Expand Up @@ -407,7 +407,7 @@ private String conditionCardinalite(RegleControleEntity reg) {
* @param condition0
* @return le résultat est en MAJUSCULE
*/
private String rewriteCondition(Map<String, RegleControleEntity> mapRubrique, String condition0) {
private String rewriteCondition(Map<String, RegleControle> mapRubrique, String condition0) {
StaticLoggerDispatcher.debug(logger, "Je rentre dans la méthode rewriteCondition");
// Passage en MAJUSCULE car la map contient des elements en majuscule
// bétonnage du code pour que le .uppercase ne lève pas de null pointerException
Expand All @@ -419,9 +419,9 @@ private String rewriteCondition(Map<String, RegleControleEntity> mapRubrique, St
String type = "";
String rubrique = "";
String format = "";
for (Entry<String, RegleControleEntity> entry : mapRubrique.entrySet()) {
for (Entry<String, RegleControle> entry : mapRubrique.entrySet()) {
StaticLoggerDispatcher.debug(logger, "A l'intérieur de la boucle FOR");
type = entry.getValue().getIdClasse().trim();
type = entry.getValue().getTypeControle().getNom().trim();
rubrique = entry.getKey().trim();
StaticLoggerDispatcher.debug(logger, "Mon type : " + type + ", ma rubrique : " + rubrique);
switch (type) {
Expand Down Expand Up @@ -454,16 +454,16 @@ private String rewriteCondition(Map<String, RegleControleEntity> mapRubrique, St
* @param mapRubrique
* @return
*/
private String writeFiltre(Map<String, RegleControleEntity> mapRubrique) {
private String writeFiltre(Map<String, RegleControle> mapRubrique) {
String filtre = "";
String type = "";
String rubrique = "";
String format = "";

int i = 0;

for (Entry<String, RegleControleEntity> entry : mapRubrique.entrySet()) {
type = entry.getValue().getIdClasse().trim();
for (Entry<String, RegleControle> entry : mapRubrique.entrySet()) {
type = entry.getValue().getTypeControle().getNom().trim();
rubrique = entry.getKey().trim();
format = entry.getValue().getCondition();

Expand Down Expand Up @@ -554,7 +554,7 @@ private String getRequete(String req, String... args) {
* @param borneSup
* @return
*/
private String conditionLongueur(RegleControleEntity reg) {
private String conditionLongueur(RegleControle reg) {
String cond = "";
String rubrique = reg.getRubriquePere();
String borneInf = reg.getBorneInf();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public static void fillControleRules(Connection connection, String envExecution,
regle.get(ColumnEnum.BORNE_SUP.getColumnName()).get(0),
regle.get(ColumnEnum.CONDITION.getColumnName()).get(0),
regle.get(ColumnEnum.PRE_ACTION.getColumnName()).get(0),
Integer.parseInt(regle.get(ColumnEnum.ID_REGLE.getColumnName()).get(0)),
Integer.parseInt(regle.get(ColumnEnum.XSD_ORDRE.getColumnName()).get(0)),
regle.get(ColumnEnum.XSD_LABEL_FILS.getColumnName()).get(0),
regle.get(ColumnEnum.XSD_ROLE.getColumnName()).get(0),
Expand Down
Loading

0 comments on commit 13e397b

Please sign in to comment.