Skip to content

Commit

Permalink
feat: wsimport - handshake and delete pending client tables on rerun
Browse files Browse the repository at this point in the history
  • Loading branch information
Nolife999 committed Oct 9, 2023
1 parent ac9f260 commit f2242e9
Show file tree
Hide file tree
Showing 29 changed files with 820 additions and 974 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public enum ViewEnum {
, EXT_ETAT_JEUDEREGLE("ext_etat_jeuderegle", SchemaEnum.ARC_METADATA, ColumnEnum.ID, ColumnEnum.VAL,
ColumnEnum.ISENV, ColumnEnum.MISE_A_JOUR_IMMEDIATE, ColumnEnum.ENV_DESCRIPTION) //
, EXT_EXPORT_FORMAT("ext_export_format", SchemaEnum.ARC_METADATA, ColumnEnum.ID, ColumnEnum.VAL) //
, EXT_MOD_FAMILLE("ext_mod_famille", SchemaEnum.ARC_METADATA, ColumnEnum.ID_FAMILLE) //
, EXT_MOD_PERIODICITE("ext_mod_periodicite", SchemaEnum.ARC_METADATA, ColumnEnum.ID, ColumnEnum.VAL) //
, EXT_MOD_TYPE_AUTORISE("ext_mod_type_autorise", SchemaEnum.ARC_METADATA, ColumnEnum.NOM_TYPE,
ColumnEnum.DESCRIPTION_TYPE) //
Expand Down Expand Up @@ -198,11 +199,16 @@ public String getFullName() {
}

public String getFullName(String schema) {
return (schema + SQL.DOT.getSqlCode() + this.tableName).toLowerCase();
return normalizeTableName(schema + SQL.DOT.getSqlCode() + this.tableName);
}

public static String getFullName(String schema, String providedTableName) {
return (providedTableName.contains(SQL.DOT.getSqlCode())? providedTableName : schema + SQL.DOT.getSqlCode() + providedTableName).toLowerCase();
return normalizeTableName(providedTableName.contains(SQL.DOT.getSqlCode())? providedTableName : schema + SQL.DOT.getSqlCode() + providedTableName);
}

public static String normalizeTableName(String providedTableName)
{
return providedTableName.toLowerCase();
}

public ColumnEnum col(ColumnEnum e) {
Expand Down
22 changes: 22 additions & 0 deletions arc-core/src/main/java/fr/insee/arc/core/model/DataWarehouse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package fr.insee.arc.core.model;

/**
* Liste des entrepots de donnés Pour l'instant cela ne représente juste qu'un
* répertoire ou arrive les données
*/
public enum DataWarehouse {

// nom de l'entrepot par défaut
DEFAULT("DEFAULT");

private DataWarehouse(String name) {
this.name = name;
}

private String name;

public String getName() {
return name;
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package fr.insee.arc.core.service.p2chargement.bo;
package fr.insee.arc.core.model;

public class Delimiters {

Expand All @@ -12,5 +12,9 @@ private Delimiters() {
public static final String RENAME_SUFFIX= "$new$";
public static final String PARTITION_NUMBER_PLACEHOLDER = "#pn#";

public static final String SQL_TOKEN_DELIMITER = "_";
public static final String SQL_SCHEMA_DELIMITER = ".";

public static final String HANDSHAKE_DELIMITER = ">";

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package fr.insee.arc.core.service.global.dao;

import fr.insee.arc.core.dataobjects.ArcPreparedStatementBuilder;
import fr.insee.arc.core.dataobjects.ColumnEnum;
import fr.insee.arc.utils.dao.SQL;

public class TableMetadata {

private TableMetadata() {
throw new IllegalStateException("Utility class");
}

public static ArcPreparedStatementBuilder queryTablesFromPgMetadata() {
ArcPreparedStatementBuilder query = new ArcPreparedStatementBuilder();
query.build(SQL.SELECT, ColumnEnum.TABLE_SCHEMA, "||'.'||", ColumnEnum.TABLE_NAME, SQL.AS,
ColumnEnum.TABLE_NAME);
query.build(SQL.FROM, "information_schema.tables");
return query;
}

}
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package fr.insee.arc.core.service.global.dao;

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.model.TraitementEtat;
import fr.insee.arc.core.model.TraitementPhase;
import fr.insee.arc.utils.dao.SQL;
import fr.insee.arc.utils.utils.FormatSQL;

public class TableNaming {
Expand All @@ -32,13 +31,48 @@ public static String temporaryTableName(String aEnvExecution, TraitementPhase aC
public static String phaseDataTableName(String aEnvExecution, TraitementPhase aPhase, TraitementEtat etat) {
return ViewEnum.getFullName(aEnvExecution, aPhase + "_" + etat.toString());
}

public static ArcPreparedStatementBuilder queryTablesFromPgMetadata()
{
ArcPreparedStatementBuilder query = new ArcPreparedStatementBuilder();
query.build(SQL.SELECT, ColumnEnum.TABLE_SCHEMA, "||'.'||", ColumnEnum.TABLE_NAME, SQL.AS, ColumnEnum.TABLE_NAME);
query.build(SQL.FROM, "information_schema.tables");
return query;
}

/**
* Build a table name from a several informations
* schema.token#1_token#2_..._token#n_suffix
* @param schema
* @param mainTable
* @param tokens
* @return
*/
public static String buildTableNameWithTokens(String schema, String mainSuffix, Object... tokens)
{
StringBuilder s = new StringBuilder();

if (tokens==null || tokens.length==0)
{
return ViewEnum.getFullName(schema, mainSuffix);
}

for (Object token:tokens)
{
// if any token is null, the table name will be invalid, return null
if (token==null)
{
return null;
}
s.append(token);
s.append(Delimiters.SQL_TOKEN_DELIMITER);
}
s.append(mainSuffix);

return ViewEnum.getFullName(schema, s.toString());

}

public static String buildTableNameWithTokens(String schema, ViewEnum mainTableSuffix, Object... tokens)
{
return buildTableNameWithTokens(schema, mainTableSuffix.getTableName(), tokens);
}

public static String buildTableNameWithTokens(String schema, ColumnEnum mainTableSuffix, Object... tokens)
{
return buildTableNameWithTokens(schema, mainTableSuffix.getColumnName(), tokens);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ private Patch() {
throw new IllegalStateException("Utility class");
}


/**
* sandbox was defined as arc.bas2 instead of arc_bas2
* @param envExecution
* @return
*/
public static String normalizeSchemaName(String envExecution)
{
return envExecution.replace(".", "_").toLowerCase();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
Expand All @@ -18,7 +16,7 @@
import fr.insee.arc.core.dataobjects.SchemaEnum;
import fr.insee.arc.core.dataobjects.ViewEnum;
import fr.insee.arc.core.model.TraitementPhase;
import fr.insee.arc.core.service.global.dao.TableNaming;
import fr.insee.arc.core.service.global.dao.TableMetadata;
import fr.insee.arc.core.service.p0initialisation.ApiInitialisationService;
import fr.insee.arc.core.util.BDParameters;
import fr.insee.arc.utils.dao.SQL;
Expand Down Expand Up @@ -279,7 +277,7 @@ private static List<String> retrieveTablesFromSchema(Connection connexion, Strin
throws ArcException {

ArcPreparedStatementBuilder query = new ArcPreparedStatementBuilder();
query.append(TableNaming.queryTablesFromPgMetadata());
query.append(TableMetadata.queryTablesFromPgMetadata());
query.append(condition.apply(envExecution));

return new GenericBean(UtilitaireDao.get(0).executeRequest(connexion, query)).mapContent()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
import fr.insee.arc.core.dataobjects.ColumnEnum;
import fr.insee.arc.core.dataobjects.DataObjectService;
import fr.insee.arc.core.dataobjects.ViewEnum;
import fr.insee.arc.core.model.Delimiters;
import fr.insee.arc.core.model.TraitementEtat;
import fr.insee.arc.core.model.TraitementPhase;
import fr.insee.arc.core.service.global.ApiService;
import fr.insee.arc.core.service.global.bo.Sandbox;
import fr.insee.arc.core.service.p2chargement.bo.CSVFileAttributes;
import fr.insee.arc.core.service.p2chargement.bo.CSVFormatRules;
import fr.insee.arc.core.service.p2chargement.bo.Delimiters;
import fr.insee.arc.core.service.p2chargement.bo.FileIdCard;
import fr.insee.arc.core.service.p2chargement.engine.ParseFormatRulesOperation;
import fr.insee.arc.utils.dao.SQL;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@

import com.opencsv.CSVReader;

import fr.insee.arc.core.model.Delimiters;
import fr.insee.arc.core.model.TraitementPhase;
import fr.insee.arc.core.service.global.bo.Sandbox;
import fr.insee.arc.core.service.p2chargement.bo.CSVFileAttributes;
import fr.insee.arc.core.service.p2chargement.bo.CSVFormatRules;
import fr.insee.arc.core.service.p2chargement.bo.Delimiters;
import fr.insee.arc.core.service.p2chargement.bo.FileIdCard;
import fr.insee.arc.core.service.p2chargement.dao.ChargeurCsvDao;
import fr.insee.arc.core.service.p2chargement.thread.ThreadChargementService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
import org.apache.logging.log4j.Logger;
import org.xml.sax.SAXException;

import fr.insee.arc.core.model.Delimiters;
import fr.insee.arc.core.model.TraitementPhase;
import fr.insee.arc.core.service.global.bo.Sandbox;
import fr.insee.arc.core.service.p2chargement.bo.Delimiters;
import fr.insee.arc.core.service.p2chargement.bo.FileIdCard;
import fr.insee.arc.core.service.p2chargement.dao.ChargeurXMLDao;
import fr.insee.arc.core.service.p2chargement.thread.ThreadChargementService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.xml.sax.Attributes;
import org.xml.sax.SAXParseException;

import fr.insee.arc.core.model.Delimiters;
import fr.insee.arc.core.service.p2chargement.bo.FileIdCard;
import fr.insee.arc.core.service.p2chargement.dao.HandlerXMLDao;
import fr.insee.arc.utils.format.Format;
Expand Down Expand Up @@ -395,7 +396,7 @@ private String renameColumn(String qName) {

if (!m.isEmpty()) {
for (Integer key : m.keySet()) {
qName += "_" + m.get(key);
qName += Delimiters.SQL_TOKEN_DELIMITER + m.get(key);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
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.dao.TableNaming;
import fr.insee.arc.core.service.p5mapping.engine.regles.RegleMappingClePrimaire;
import fr.insee.arc.utils.dao.ModeRequeteImpl;
import fr.insee.arc.utils.dao.UtilitaireDao;
Expand Down Expand Up @@ -810,7 +810,7 @@ private void construireListeIdentifiants(Map<String, String> reglesIdentifiantes
}

private static final String nomIdentifiantSuffixeGroupe(String nomVariable, int groupe) {
return new StringBuilder(nomVariable + underscore + groupe).toString();
return new StringBuilder(nomVariable + Delimiters.SQL_TOKEN_DELIMITER + groupe).toString();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

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.dao.TableNaming;
import fr.insee.arc.core.service.p5mapping.engine.regles.RegleMappingClePrimaire;
import fr.insee.arc.utils.exception.ArcException;
Expand Down Expand Up @@ -265,7 +266,7 @@ public String expressionSQLPrepUnion(Integer aNumeroGroupe, Map<String, String>
} else {
returned.append(", ");
}
String nomVariable = variable.getNomVariable() + underscore + aNumeroGroupe;
String nomVariable = variable.getNomVariable() + Delimiters.SQL_TOKEN_DELIMITER + aNumeroGroupe;
String expression = reglesIdentifiantes.get(nomVariable);
if (expression != null) {
expression = variable.getNomVariable() + "::text as " + variable.getNomVariable();
Expand Down
2 changes: 1 addition & 1 deletion arc-core/src/main/resources/BdD/script_global.sql
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ CONSTRAINT ihm_user_pkey PRIMARY KEY (idep)
CREATE TABLE IF NOT EXISTS arc.ihm_entrepot
(
id_entrepot text NOT NULL,
id_loader text,
id_loader text, -- deprecated
CONSTRAINT ihm_entrepot_pkey PRIMARY KEY (id_entrepot)
);
INSERT INTO arc.ihm_entrepot values ('DEFAULT','DEFAULT') ON CONFLICT DO NOTHING;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import fr.insee.arc.core.dataobjects.ArcPreparedStatementBuilder;
import fr.insee.arc.core.factory.ApiServiceFactory;
import fr.insee.arc.core.model.DataWarehouse;
import fr.insee.arc.core.model.TraitementEtat;
import fr.insee.arc.core.model.TraitementPhase;
import fr.insee.arc.core.service.engine.initialisation.BddPatcherTest;
Expand Down Expand Up @@ -70,7 +71,7 @@ private void executeTestSirene(String sandbox, String repertoire) throws IOExcep
10000000, null
).invokeApi();

String repertoireDeDepot = DirectoryPath.directoryReceptionEntrepot(repertoire, sandbox, "DEFAULT");
String repertoireDeDepot = DirectoryPath.directoryReceptionEntrepot(repertoire, sandbox, DataWarehouse.DEFAULT.getName());

Files.copy(this.getClass().getClassLoader().getResourceAsStream("testFiles/Cas_test_V2008.11.zip"), new File(repertoireDeDepot, "Cas_test_V2008.11.zip").toPath());
Files.copy(this.getClass().getClassLoader().getResourceAsStream("testFiles/Cas_test_V2016.02.zip"), new File(repertoireDeDepot, "Cas_test_V2016.02.zip").toPath());
Expand Down Expand Up @@ -120,7 +121,7 @@ private void executeTestSiera(String sandbox, String repertoire) throws IOExcept
10000000, null
).invokeApi();

String repertoireDeDepot = DirectoryPath.directoryReceptionEntrepot(repertoire, sandbox, "DEFAULT");
String repertoireDeDepot = DirectoryPath.directoryReceptionEntrepot(repertoire, sandbox, DataWarehouse.DEFAULT.getName());

Files.copy(this.getClass().getClassLoader().getResourceAsStream("testFiles/siera_ano.xml"), new File(repertoireDeDepot, "siera_ano.xml").toPath());

Expand Down Expand Up @@ -172,7 +173,7 @@ private void executeTestAnimal(String sandbox, String repertoire) throws IOExcep
10000000, null
).invokeApi();

String repertoireDeDepot = DirectoryPath.directoryReceptionEntrepot(repertoire, sandbox, "DEFAULT");
String repertoireDeDepot = DirectoryPath.directoryReceptionEntrepot(repertoire, sandbox, DataWarehouse.DEFAULT.getName());

Files.copy(this.getClass().getClassLoader().getResourceAsStream("testFiles/animals.tar.gz"), new File(repertoireDeDepot, "animals.tar.gz").toPath());

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package fr.insee.arc.core.service.global.dao;

import static org.junit.Assert.assertEquals;

import org.junit.Test;

import fr.insee.arc.core.dataobjects.ColumnEnum;
import fr.insee.arc.core.dataobjects.ViewEnum;

public class TableNamingTest {

@Test
public void buildTableNameTokensSuffix() {

String client = "ARTEMIS";
long timestamp = System.currentTimeMillis();

assertEquals("arc_bas2.artemis_"+timestamp+"_pilotage_fichier", TableNaming.buildTableNameWithTokens("arc_bas2", ViewEnum.PILOTAGE_FICHIER, client, timestamp));
assertEquals("arc_bas2.artemis_"+timestamp+"_id_source", TableNaming.buildTableNameWithTokens("arc_bas2", ColumnEnum.ID_SOURCE, client, timestamp));
assertEquals(null, TableNaming.buildTableNameWithTokens("arc_bas2", ColumnEnum.ID_SOURCE, null, timestamp));
assertEquals("arc_bas2.artemis_"+timestamp+"_test", TableNaming.buildTableNameWithTokens("arc_bas2", "test", client, timestamp));
assertEquals("arc_bas2.test", TableNaming.buildTableNameWithTokens("arc_bas2", "TEST"));

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import fr.insee.arc.core.dataobjects.DataObjectService;
import fr.insee.arc.core.dataobjects.SchemaEnum;
import fr.insee.arc.core.dataobjects.ViewEnum;
import fr.insee.arc.core.model.Delimiters;
import fr.insee.arc.utils.dao.SQL;
import fr.insee.arc.utils.dao.UtilitaireDao;
import fr.insee.arc.utils.exception.ArcException;
Expand Down Expand Up @@ -104,7 +105,7 @@ public void initializeViewSchemaNmcl(VObject viewSchemaNmcl) {
* @return
*/
private static String typeNomenclature(String nomTable) {
String[] tokens = nomTable.split(fr.insee.arc.utils.textUtils.IConstanteCaractere.underscore);
String[] tokens = nomTable.split(Delimiters.SQL_TOKEN_DELIMITER);
StringBuilder typeNomenclature = new StringBuilder();
for (int i = 0; i < tokens.length - 1; i++) {
typeNomenclature
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import fr.insee.arc.core.dataobjects.ArcPreparedStatementBuilder;
import fr.insee.arc.core.dataobjects.DataObjectService;
import fr.insee.arc.core.dataobjects.SchemaEnum;
import fr.insee.arc.core.model.Delimiters;
import fr.insee.arc.core.util.StaticLoggerDispatcher;
import fr.insee.arc.utils.dao.SQL;
import fr.insee.arc.utils.exception.ArcException;
Expand Down Expand Up @@ -156,7 +157,7 @@ private boolean validationNomTable(String nomTable) {
return false;
}

if (nomTable.split(underscore).length < 3) {
if (nomTable.split(Delimiters.SQL_TOKEN_DELIMITER).length < 3) {
this.views.getViewListNomenclatures().setMessage("nmclManagement.validateName.error.format");
return false;
}
Expand Down
Loading

0 comments on commit f2242e9

Please sign in to comment.