Skip to content

Commit

Permalink
fileIdCard in mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
lmanelphe committed Dec 11, 2023
1 parent 33f4616 commit 1480d1d
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 82 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,13 @@
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import fr.insee.arc.core.dataobjects.ArcPreparedStatementBuilder;
import fr.insee.arc.core.dataobjects.ColumnEnum;
import fr.insee.arc.core.dataobjects.ViewEnum;
import fr.insee.arc.core.model.Delimiters;
import fr.insee.arc.core.service.global.bo.JeuDeRegle;
import fr.insee.arc.core.service.global.bo.FileIdCard;
import fr.insee.arc.core.service.p5mapping.bo.RegleMapping;
import fr.insee.arc.core.service.p5mapping.bo.TableMapping;
import fr.insee.arc.core.service.p5mapping.bo.VariableMapping;
import fr.insee.arc.core.service.p5mapping.bo.rules.RegleMappingClePrimaire;
Expand Down Expand Up @@ -83,7 +82,7 @@ public class MappingQueries implements IConstanteCaractere, IConstanteNumerique
private String requeteTextuelleInsertion;
private boolean isRequeteCalculee;
private String idFamille;
private JeuDeRegle jeuDeRegle;
private FileIdCard fileIdCard;
private String environnement;
private Connection connexion;
/*
Expand All @@ -96,7 +95,6 @@ public class MappingQueries implements IConstanteCaractere, IConstanteNumerique
private String nomTableModVariableMetier;
private String nomTablePrecedente;
private String nomTableSource;
private String nomTableRegleMapping;
private String nomTableTemporairePrepUnion;
private String nomTableTemporaireIdTable;
private String nomTableTemporaireFinale;
Expand All @@ -112,7 +110,7 @@ private MappingQueries() {
}

public MappingQueries(Connection aConnexion, MappingQueriesFactory aRegleMappingFactory, String anIdFamille,
JeuDeRegle aJeuDeRegle, String anEnvironnement, String aNomTablePrecedente, int threadId) {
FileIdCard aFileIdCard, String anEnvironnement, String aNomTablePrecedente, int threadId) {
this();
this.regleMappingFactory = aRegleMappingFactory;
/**
Expand All @@ -125,11 +123,10 @@ public MappingQueries(Connection aConnexion, MappingQueriesFactory aRegleMapping
this.regleMappingFactory.setEnsembleTableMapping(this.ensembleTableMapping);
this.regleMappingFactory.setIdFamille(this.idFamille);
this.connexion = aConnexion;
this.jeuDeRegle = aJeuDeRegle;
this.fileIdCard = aFileIdCard;
this.environnement = anEnvironnement;
this.nomTablePrecedente = aNomTablePrecedente;
this.nomTableModVariableMetier = ViewEnum.MOD_VARIABLE_METIER.getFullName(this.environnement);
this.nomTableRegleMapping = ViewEnum.MAPPING_REGLE.getFullName(this.environnement);
}

public void construire() throws ArcException {
Expand All @@ -155,48 +152,18 @@ public void construire() throws ArcException {
* @param mapVariable
* @throws ArcException
*/
private void attribuerExpressionRegleMapping(Map<String, VariableMapping> mapVariable) throws ArcException {
ArcPreparedStatementBuilder requete = new ArcPreparedStatementBuilder();
requete.append("SELECT DISTINCT variable_sortie as variable_sortie, expr_regle_col as expr_regle_col FROM ")
.append(this.nomTableRegleMapping).append("\n WHERE ").append(this.jeuDeRegle.getSqlEquals())
.append(";");

List<List<String>> resultTemp = UtilitaireDao.get(0).executeRequest(this.connexion, requete);
if (resultTemp.size() == 2) {
throw new ArcException(ArcExceptionMessage.MAPPING_RULES_NOT_FOUND);
}
List<List<String>> result = new ArrayList<>();

for (int i = 0; i < resultTemp.size(); i++) {
// mise en minuscule des rubriques
List<String> temp = new ArrayList<>();
temp.add(resultTemp.get(i).get(0).toLowerCase());

String exprCol = resultTemp.get(i).get(1);
if (exprCol == null) {
exprCol = "";
}
private void attribuerExpressionRegleMapping(Map<String, VariableMapping> mapVariable) throws ArcException {
List<RegleMapping> reglesMapping = this.fileIdCard.getIdCardMapping().getReglesMapping();

Matcher m = Pattern.compile("\\{[^\\{\\} ]*\\}").matcher(exprCol);
for (int i = 0; i < reglesMapping.size(); i++) {

StringBuffer sb = new StringBuffer();
while (m.find()) {
m.appendReplacement(sb, m.group().toLowerCase());
}
m.appendTail(sb);
temp.add(sb.toString());
result.add(temp);
}

for (int i = ARRAY_THIRD_COLUMN_INDEX; i < result.size(); i++) {

if (mapVariable.get(result.get(i).get(ARRAY_FIRST_COLUMN_INDEX)) == null) {
if (mapVariable.get(reglesMapping.get(i).getVariableSortie()) == null) {
throw new ArcException(ArcExceptionMessage.MAPPING_RULES_NOT_FOUND,
result.get(i).get(ARRAY_FIRST_COLUMN_INDEX));
reglesMapping.get(i).getVariableSortie());
}

mapVariable.get(result.get(i).get(ARRAY_FIRST_COLUMN_INDEX))
.setExpressionRegle(result.get(i).get(ARRAY_SECOND_COLUMN_INDEX));
mapVariable.get(reglesMapping.get(i).getVariableSortie())
.setExpressionRegle(reglesMapping.get(i).getExprRegleCol());
}
}

Expand Down Expand Up @@ -256,13 +223,13 @@ private void construireListeRubriqueParTable() {

Set<String> s = new HashSet<>();

for (VariableMapping var : this.ensembleVariableMapping) {
for (VariableMapping variable : this.ensembleVariableMapping) {

if (table.getEnsembleVariableMapping().contains(var) && !var.toString().startsWith(ID_KEY_PREFIX)
&& !var.toString().startsWith(FOREIGN_KEY_PREFIX) || var.toString().equals(table.getPrimaryKey()))
if (table.getEnsembleVariableMapping().contains(variable) && !variable.toString().startsWith(ID_KEY_PREFIX)
&& !variable.toString().startsWith(FOREIGN_KEY_PREFIX) || variable.toString().equals(table.getPrimaryKey()))

{
s.addAll(var.getEnsembleIdentifiantsRubriques());
s.addAll(variable.getEnsembleIdentifiantsRubriques());
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import fr.insee.arc.core.dataobjects.ArcPreparedStatementBuilder;
import fr.insee.arc.core.dataobjects.ViewEnum;
import fr.insee.arc.core.service.global.bo.JeuDeRegle;
import fr.insee.arc.core.service.global.bo.FileIdCard;
import fr.insee.arc.utils.dao.UtilitaireDao;
import fr.insee.arc.utils.exception.ArcException;

Expand All @@ -16,16 +16,16 @@ private ThreadMappingQueries() {

/**
*
* @param aJeuDeRegle
* @param fileIdCard
* @return Le bon id_famille
* @throws ArcException
*/
public static String fetchIdFamille(Connection connexion, JeuDeRegle aJeuDeRegle, String envExecution)
public static String fetchIdFamille(Connection connexion, FileIdCard fileIdCard, String envExecution)
throws ArcException {
ArcPreparedStatementBuilder requete = new ArcPreparedStatementBuilder();
requete.append("SELECT id_famille FROM " + ViewEnum.NORME.getFullName(envExecution))
.append("\n WHERE id_norme = " + requete.quoteText(aJeuDeRegle.getIdNorme()))
.append("\n AND periodicite = " + requete.quoteText(aJeuDeRegle.getPeriodicite()));
.append("\n WHERE id_norme = " + requete.quoteText(fileIdCard.getIdNorme()))
.append("\n AND periodicite = " + requete.quoteText(fileIdCard.getPeriodicite()));
return UtilitaireDao.get(0).getString(connexion, requete);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import fr.insee.arc.core.service.p5mapping.bo.IdCardMapping;
import fr.insee.arc.core.service.p5mapping.bo.RegleMapping;
import fr.insee.arc.utils.exception.ArcException;
import fr.insee.arc.utils.exception.ArcExceptionMessage;

public class MappingRulesOperation {

Expand All @@ -32,6 +33,10 @@ public static void fillMappingRules(Connection connection, String envExecution,
throws ArcException {
Map<String, List<String>> regle = RulesOperations.getBean(connection,
RulesOperations.getRegles(ViewEnum.MAPPING_REGLE.getFullName(envExecution), fileIdCard));

if (regle.get(ColumnEnum.VARIABLE_SORTIE.getColumnName()).isEmpty()) {
throw new ArcException(ArcExceptionMessage.MAPPING_RULES_NOT_FOUND, fileIdCard.getIdNorme());
}

List<RegleMapping> listRegles = new ArrayList<>();
for(int i = 0; i < regle.get(ColumnEnum.VARIABLE_SORTIE.getColumnName()).size(); i++) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
package fr.insee.arc.core.service.p5mapping.thread;

import java.util.List;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import fr.insee.arc.core.dataobjects.ArcPreparedStatementBuilder;
import fr.insee.arc.core.dataobjects.ColumnEnum;
import fr.insee.arc.core.service.global.bo.JeuDeRegle;
import fr.insee.arc.core.service.global.bo.JeuDeRegleDao;
import fr.insee.arc.core.service.global.bo.FileIdCard;
import fr.insee.arc.core.service.global.dao.DatabaseConnexionConfiguration;
import fr.insee.arc.core.service.global.dao.GenericQueryDao;
import fr.insee.arc.core.service.global.dao.PilotageOperations;
Expand All @@ -21,9 +18,9 @@
import fr.insee.arc.core.service.p5mapping.dao.MappingQueries;
import fr.insee.arc.core.service.p5mapping.dao.ThreadMappingQueries;
import fr.insee.arc.core.service.p5mapping.operation.MappingOperation;
import fr.insee.arc.core.service.p5mapping.operation.MappingRulesOperation;
import fr.insee.arc.core.util.StaticLoggerDispatcher;
import fr.insee.arc.utils.exception.ArcException;
import fr.insee.arc.utils.exception.ArcExceptionMessage;
import fr.insee.arc.utils.utils.Sleep;

/**
Expand All @@ -39,6 +36,8 @@ public class ThreadMappingService extends ApiMappingService implements Runnable,
private int indice;
private String tableTempControleOk;
private String tableMappingPilTemp;

private FileIdCard fileIdCard;

private ThreadOperations arcThreadGenericDao;

Expand Down Expand Up @@ -110,8 +109,12 @@ private void preparerExecution() throws ArcException {
}

private void execute() throws ArcException {

JeuDeRegle jdr = getTheRulesSetOfTheFile();

this.fileIdCard = RulesOperations.fileIdCardFromPilotage(this.connexion.getExecutorConnection(),
tableMappingPilTemp, this.idSource);

// Récupération des jeux de règles
MappingRulesOperation.fillMappingRules(this.connexion.getExecutorConnection(), envExecution, fileIdCard);

MappingOperation serviceMapping = new MappingOperation();
this.regleMappingFactory = serviceMapping.construireRegleMappingFactory(this.connexion.getExecutorConnection(),
Expand All @@ -120,13 +123,13 @@ private void execute() throws ArcException {
/*
* Récupération de l'id_famille
*/
String idFamille = ThreadMappingQueries.fetchIdFamille(this.connexion.getExecutorConnection(), jdr,
String idFamille = ThreadMappingQueries.fetchIdFamille(this.connexion.getExecutorConnection(), fileIdCard,
this.getEnvExecution());
/*
* Instancier une requête de mapping générique pour ce jeu de règles.
*/
MappingQueries requeteMapping = new MappingQueries(this.connexion.getExecutorConnection(),
this.regleMappingFactory, idFamille, jdr, this.getEnvExecution(), this.tableTempControleOk,
this.regleMappingFactory, idFamille, this.fileIdCard, this.getEnvExecution(), this.tableTempControleOk,
this.indice);
/*
* Construire la requête de mapping (dérivation des règles)
Expand Down Expand Up @@ -158,25 +161,6 @@ private void execute() throws ArcException {

}

private JeuDeRegle getTheRulesSetOfTheFile() throws ArcException {

/*
* Construire l'ensemble des jeux de règles
*/
List<JeuDeRegle> listeJeuxDeRegles = JeuDeRegleDao.recupJeuDeRegle(this.connexion.getExecutorConnection(),
this.getEnvExecution(), this.tableTempControleOk);

if (listeJeuxDeRegles.isEmpty()) {
throw new ArcException(ArcExceptionMessage.MAPPING_RULES_NOT_FOUND);
}

if (listeJeuxDeRegles.size() > 1) {
throw new ArcException(ArcExceptionMessage.MAPPING_RULES_NON_UNIQUE);
}

return listeJeuxDeRegles.get(0);
}

@Override
public ScalableConnection getConnexion() {
return connexion;
Expand Down

0 comments on commit 1480d1d

Please sign in to comment.