Skip to content

Commit

Permalink
Merge pull request #99 from InseeFr/refactor_fileIdCard
Browse files Browse the repository at this point in the history
Refactor file id card
  • Loading branch information
Nolife999 authored Nov 17, 2023
2 parents ae0bc34 + 2ebfe46 commit 3cd0575
Show file tree
Hide file tree
Showing 44 changed files with 1,703 additions and 824 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,42 +4,86 @@

import fr.insee.arc.core.service.global.dao.DateConversion;
import fr.insee.arc.core.service.p2chargement.bo.IdCardChargement;
import fr.insee.arc.core.service.p3normage.bo.IdCardNormage;
import fr.insee.arc.core.service.p4controle.bo.IdCardControle;
import fr.insee.arc.core.service.p5mapping.bo.IdCardMapping;

public class FileIdCard {

private final String integrationDate = DateConversion.queryDateConversion(new Date());

private String fileName;
private String idSource;
private String idNorme;
private String validite;
private String periodicite;
private final String integrationDate = DateConversion.queryDateConversion(new Date());
private String jointure;

private IdCardChargement idCardChargement;

public IdCardChargement getIdCardChargement() {
return idCardChargement;
}

public void setIdCardChargement(IdCardChargement regleChargement) {
this.idCardChargement = regleChargement;
}

private IdCardNormage idCardNormage;

public IdCardNormage getIdCardNormage() {
return idCardNormage;
}

public void setIdCardNormage(IdCardNormage idCardNormage) {
this.idCardNormage = idCardNormage;
}

private IdCardControle idCardControle;

public IdCardControle getIdCardControle() {
return idCardControle;
}

public void setIdCardControle(IdCardControle idCardControle) {
this.idCardControle = idCardControle;
}

private IdCardMapping idCardMapping;

public IdCardMapping getIdCardMapping() {
return idCardMapping;
}

public void setIdCardMapping(IdCardMapping idCardMapping) {
this.idCardMapping = idCardMapping;
}

public FileIdCard(String idSource) {
this.fileName = idSource;
this.idSource = idSource;
}

public void setFileIdCard(String idNorme, String validite, String periodicite) {
/**
* Set the pilotage values for a file
* @param idNorme
* @param validite
* @param periodicite
* @param jointure
*/
public void setFileIdCard(String idNorme, String validite, String periodicite, String jointure) {
this.idNorme = idNorme;
this.validite = validite;
this.periodicite = periodicite;
this.jointure = jointure;
}


public String getFileName() {
return fileName;
public String getIdSource() {
return idSource;
}


public void setFileName(String fileName) {
this.fileName = fileName;
public void setIdSource(String idSource) {
this.idSource = idSource;
}


Expand Down Expand Up @@ -78,6 +122,13 @@ public void setPeriodicite(String periodicite) {
}




public String getJointure() {
return jointure;
}

public void setJointure(String jointure) {
this.jointure = jointure;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,10 @@ private static String conditionRegle(String tablePilotage) {
requete.append("EXISTS ( SELECT * FROM " + tablePilotage + " b ");
requete.append("WHERE a.id_norme=b.id_norme ");
requete.append("AND a.periodicite=b.periodicite ");
requete.append("AND a.validite_inf<=to_date(b.validite,'"+ArcDateFormat.DATE_FORMAT_CONVERSION.getDatastoreFormat()+"') ");
requete.append("AND a.validite_sup>=to_date(b.validite,'"+ArcDateFormat.DATE_FORMAT_CONVERSION.getDatastoreFormat()+"') ");
requete.append("AND a.validite_inf<=to_date(b.validite,'"
+ ArcDateFormat.DATE_FORMAT_CONVERSION.getDatastoreFormat() + "') ");
requete.append("AND a.validite_sup>=to_date(b.validite,'"
+ ArcDateFormat.DATE_FORMAT_CONVERSION.getDatastoreFormat() + "') ");
requete.append(") ");
return requete.toString();
}
Expand All @@ -116,8 +118,10 @@ private static String conditionRegle(FileIdCard fileIdCard) {
requete.append("\n ");
requete.append("a.id_norme='" + fileIdCard.getIdNorme() + "' ");
requete.append("AND a.periodicite='" + fileIdCard.getPeriodicite() + "' ");
requete.append("AND a.validite_inf<=to_date('" + fileIdCard.getValidite() + "','"+ArcDateFormat.DATE_FORMAT_CONVERSION.getDatastoreFormat()+"') ");
requete.append("AND a.validite_sup>=to_date('" + fileIdCard.getValidite() + "','"+ArcDateFormat.DATE_FORMAT_CONVERSION.getDatastoreFormat()+"') ");
requete.append("AND a.validite_inf<=to_date('" + fileIdCard.getValidite() + "','"
+ ArcDateFormat.DATE_FORMAT_CONVERSION.getDatastoreFormat() + "') ");
requete.append("AND a.validite_sup>=to_date('" + fileIdCard.getValidite() + "','"
+ ArcDateFormat.DATE_FORMAT_CONVERSION.getDatastoreFormat() + "') ");
requete.append(";");
return requete.toString();
}
Expand All @@ -129,13 +133,13 @@ private static String conditionRegle(FileIdCard fileIdCard) {
* @param id_source
* @param tableRegle
* @return SQL pil.id_source, pil.jointure, pil.id_norme, pil.validite,
* pil.periodicite, pil.validite
* pil.periodicite
*/
public static String getNormeAttributes(String idSource, String tablePilotage) {
StringBuilder requete = new StringBuilder();
requete.append("\n SELECT pil." + ColumnEnum.ID_SOURCE.getColumnName()
+ ", pil.jointure, pil.id_norme, pil.validite, pil.periodicite, pil.validite " + "FROM " + tablePilotage
+ " pil " + " WHERE " + ColumnEnum.ID_SOURCE.getColumnName() + "='" + idSource + "' ");
+ ", pil.jointure, pil.id_norme, pil.validite, pil.periodicite " + "FROM " + tablePilotage + " pil "
+ " WHERE " + ColumnEnum.ID_SOURCE.getColumnName() + "='" + idSource + "' ");
return requete.toString();
}

Expand All @@ -148,11 +152,10 @@ public static String getNormeAttributes(String idSource, String tablePilotage) {
* @throws ArcException
*/
public static Map<String, List<String>> getBean(Connection c, String req) throws ArcException {
GenericBean gb = new GenericBean(
UtilitaireDao.get(0).executeRequest(c, new ArcPreparedStatementBuilder(req)));
GenericBean gb = new GenericBean(UtilitaireDao.get(0).executeRequest(c, new ArcPreparedStatementBuilder(req)));
return gb.mapContent(true);
}

/**
* Méthodes pour marquer la table de pilotage temporaire avec le jeu de règle
* appliqué
Expand All @@ -163,7 +166,8 @@ public static String marqueJeuDeRegleApplique(TraitementPhase currentPhase, Stri
return marqueJeuDeRegleApplique(currentPhase, envExecution, pilTemp, null);
}

public static String marqueJeuDeRegleApplique(TraitementPhase currentPhase, String envExecution, String pilTemp, String defaultEtatTraitement) {
public static String marqueJeuDeRegleApplique(TraitementPhase currentPhase, String envExecution, String pilTemp,
String defaultEtatTraitement) {
StringBuilder requete = new StringBuilder();
requete.append("WITH ");
requete.append("prep AS (SELECT a." + ColumnEnum.ID_SOURCE.getColumnName()
Expand All @@ -181,5 +185,31 @@ public static String marqueJeuDeRegleApplique(TraitementPhase currentPhase, Stri
requete.append("WHERE a.phase_traitement='" + currentPhase + "'; ");
return requete.toString();
}


/**
* Set the id card for a file idSource
*
* @param connection
* @param idSource
* @param pilotageTable
* @return
* @throws ArcException
*/
public static FileIdCard fileIdCardFromPilotage(Connection connection, String pilotageTable, String idSource)
throws ArcException {

FileIdCard fileIdCard = new FileIdCard(idSource);

Map<String, List<String>> fileIds = RulesOperations.getBean(connection,
RulesOperations.getNormeAttributes(idSource, pilotageTable));

fileIdCard.setFileIdCard(fileIds.get(ColumnEnum.ID_NORME.getColumnName()).get(0),
fileIds.get(ColumnEnum.VALIDITE.getColumnName()).get(0),
fileIds.get(ColumnEnum.PERIODICITE.getColumnName()).get(0),
fileIds.get(ColumnEnum.JOINTURE.getColumnName()).get(0));

return fileIdCard;

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public void execQueryCreateContainerWithArcMetadata() throws ArcException {
req.append("DROP TABLE IF EXISTS " + ViewEnum.TMP_CHARGEMENT_ARC.getFullName() + ";");
req.append("CREATE TEMPORARY TABLE " + ViewEnum.TMP_CHARGEMENT_ARC.getFullName());
req.append(" AS (SELECT ");
req.append("\n '" + fileIdCard.getFileName() + "'::text collate \"C\" as "
req.append("\n '" + fileIdCard.getIdSource() + "'::text collate \"C\" as "
+ ColumnEnum.ID_SOURCE.getColumnName());
req.append("\n ,id::integer as id");
req.append("\n ," + fileIdCard.getIntegrationDate() + "::text collate \"C\" as date_integration ");
Expand Down Expand Up @@ -439,7 +439,7 @@ private List<String> execQuerySelectColumnsFromLoadTable() throws ArcException {
public void execQueryBilan(String tableChargementPilTemp, TraitementPhase currentPhase) throws ArcException {

StringBuilder requeteBilan = new StringBuilder();
requeteBilan.append(ApiService.pilotageMarkIdsource(tableChargementPilTemp, fileIdCard.getFileName(),
requeteBilan.append(ApiService.pilotageMarkIdsource(tableChargementPilTemp, fileIdCard.getIdSource(),
currentPhase, TraitementEtat.OK.toString(), null));

UtilitaireDao.get(0).executeBlock(sandbox.getConnection(), requeteBilan);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public void execQueryCreateTemporaryLoadDataTable(String tableTempA) throws ArcE
public void execQueryBilan(String tableChargementPilTemp, TraitementPhase currentPhase, String rapport, String jointure)
throws ArcException {
StringBuilder requeteBilan = new StringBuilder();
requeteBilan.append(ApiService.pilotageMarkIdsource(tableChargementPilTemp, fileIdCard.getFileName(),
requeteBilan.append(ApiService.pilotageMarkIdsource(tableChargementPilTemp, fileIdCard.getIdSource(),
currentPhase, TraitementEtat.OK.toString(), rapport, jointure));
UtilitaireDao.get(0).executeBlock(sandbox.getConnection(), requeteBilan);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ private void buildInsertQuery(List<Integer> lineCols, List<Integer> lineIds, Lis
.append(",") //
.append(XMLColumns.getShort(ColumnEnum.VALIDITE)); //

req2.append("('").append(fileIdCard.getFileName()).append("',").append(this.idLigne).append(",")
req2.append("('").append(fileIdCard.getIdSource()).append("',").append(this.idLigne).append(",")
.append(this.fileIdCard.getIntegrationDate()).append(",'").append(this.fileIdCard.getIdNorme())
.append("','").append(this.fileIdCard.getPeriodicite()).append("','")
.append(this.fileIdCard.getValidite()).append("'");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,39 +1,39 @@
package fr.insee.arc.core.service.p2chargement.factory;

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;

public enum TypeChargement {
CLEF_VALEUR("clef-valeur"),
XML("xml"),
PLAT("plat"),
XML_COMPLEXE("xml-complexe");

private String nom;

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

public String getNom() {
return nom;
}

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

public static TypeChargement getEnum(String code) {

switch (code) {
case "clef-valeur":
return CLEF_VALEUR;
case "xml":
return XML;
case "plat":
return PLAT;
case "xml-complexe":
return XML_COMPLEXE;
default:
return null;
}
}

CLEF_VALEUR("clef-valeur"), XML("xml"), PLAT("plat"), XML_COMPLEXE("xml-complexe");

private String nom;

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

public String getNom() {
return nom;
}

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

public static TypeChargement getEnum(String code) throws ArcException {

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

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

return filtered.get(0);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -101,17 +101,17 @@ public void calculeNormeAndValiditeFichiers(InputStream file, FileIdCard normeOk
// Gênant si la norme utilise une ligne qui n'est pas dans les xxx premières lignes, mais choix temporaire pour éviter
// de charger un fichier entier à la recherche de sa norme
while (normeOk.getIdNorme() == null && nbBoucle<LIMIT_BOUCLE) {
calculerNormeAndValidite(normeOk, requeteFichierBrutalement(normeOk.getFileName(), br, nbBoucle));
calculerNormeAndValidite(normeOk, requeteFichierBrutalement(normeOk.getIdSource(), br, nbBoucle));

nbBoucle++;
}

} catch (IOException e) {
throw new ArcException(e, ArcExceptionMessage.FILE_READ_FAILED, normeOk.getFileName());
throw new ArcException(e, ArcExceptionMessage.FILE_READ_FAILED, normeOk.getIdSource());
}

if (normeOk.getIdNorme()==null) {
throw new ArcException(ArcExceptionMessage.LOAD_NORM_NOT_FOUND, normeOk.getFileName());
throw new ArcException(ArcExceptionMessage.LOAD_NORM_NOT_FOUND, normeOk.getIdSource());
}


Expand Down Expand Up @@ -165,7 +165,7 @@ private void calculerNormeAndValidite(FileIdCard normeOk, String requeteFichier)

NormeRules normFound = listeNorme.get(Integer.parseInt(result.get(0).get(0)));

normeOk.setFileIdCard(normFound.getIdNorme(), result.get(0).get(2), normFound.getPeriodicite());
normeOk.setFileIdCard(normFound.getIdNorme(), result.get(0).get(2), normFound.getPeriodicite(), null);

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public static void fillChargementRules(Connection connection, String envExecutio
}

if (regle.get(ColumnEnum.TYPE_FICHIER.getColumnName()).size() > 1) {
throw new ArcException(ArcExceptionMessage.LOAD_RULES_NOT_FOUND, fileIdCard.getIdNorme());
throw new ArcException(ArcExceptionMessage.LOAD_RULES_SEVERAL, fileIdCard.getIdNorme());
}

fileIdCard.setIdCardChargement(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ private void computeHeaders() throws ArcException {
}
} catch (IOException fileReadException) {
throw new ArcException(fileReadException, ArcExceptionMessage.FILE_READ_FAILED,
fileIdCard.getFileName());
fileIdCard.getIdSource());
}
} else {
String[] headers = Format.tokenizeAndTrim(userDefinedHeaders, Delimiters.HEADERS_DELIMITER);
Expand Down Expand Up @@ -172,7 +172,7 @@ private void importCsvDataToTable() throws ArcException {
}
} catch (IOException fileReadException) {
throw new ArcException(fileReadException, ArcExceptionMessage.FILE_READ_FAILED,
this.fileIdCard.getFileName());
this.fileIdCard.getIdSource());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public void execution() throws ArcException {
saxParser.parse(f, handler);
} catch (ParserConfigurationException | SAXException | IOException e) {
ArcException businessException = new ArcException(e, ArcExceptionMessage.XML_SAX_PARSING_FAILED,
fileIdCard.getFileName()).logMessageException();
fileIdCard.getIdSource()).logMessageException();
rapport = FormatSQL.quoteTextWithoutEnclosings(businessException.getMessage());
throw businessException;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public void execution() throws ArcException {
saxParser.parse(f, handler);
} catch (ParserConfigurationException | SAXException | IOException e) {
ArcException businessException = new ArcException(e, ArcExceptionMessage.XML_SAX_PARSING_FAILED,
this.fileIdCard.getFileName()).logMessageException();
this.fileIdCard.getIdSource()).logMessageException();
rapport = FormatSQL.quoteTextWithoutEnclosings(businessException.getMessage());
throw businessException;
}
Expand Down
Loading

0 comments on commit 3cd0575

Please sign in to comment.