Skip to content

Commit

Permalink
ResetEnvironmentService
Browse files Browse the repository at this point in the history
  • Loading branch information
Nolife999 committed Sep 22, 2023
1 parent dc54db2 commit e766062
Show file tree
Hide file tree
Showing 15 changed files with 194 additions and 157 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -366,33 +366,6 @@ public static ArcPreparedStatementBuilder listeColonneTableMetierSelonFamilleNor
return requete;
}

/**
* Return the query that marks the files or all file if idSource not provided
* The mark indicates reset etape to 0 for the previous phase, meaning the file
* is no longer processed in the current phase
*
* @param idSource
* @return
*/
public static StringBuilder resetPreviousPhaseMark(String tablePil, String idSource, String tableSource) {
StringBuilder requete = new StringBuilder();

// mettre à etape = 0 la phase marquée à 3
requete.append("\n UPDATE " + tablePil + " a ");
requete.append("\n SET etape=0 ");
requete.append("\n WHERE a.etape=3 ");
if (idSource != null) {
requete.append("\n AND a."+ColumnEnum.ID_SOURCE.getColumnName()+" = '" + idSource + "' ");
}

if (tableSource != null) {
requete.append("\n AND EXISTS (SELECT 1 FROM " + tableSource + " b where a."+ColumnEnum.ID_SOURCE.getColumnName()+"=b."+ColumnEnum.ID_SOURCE.getColumnName()+") ");
}

requete.append("\n ;");
return requete;
}


/**
*
Expand Down Expand Up @@ -479,7 +452,7 @@ private void repriseSurErreur(Connection connexion, String phase, String tablePi
requete.append(PilotageOperations.queryUpdatePilotageError(phase, tablePil, exception));
requete.append("\n RETURNING "+ColumnEnum.ID_SOURCE.getColumnName()+") ");

requete.append(resetPreviousPhaseMark(tablePil, null, "t0"));
requete.append(PilotageOperations.resetPreviousPhaseMark(tablePil, null, "t0"));

UtilitaireDao.get(0).executeBlock(connexion, requete);
}
Expand Down Expand Up @@ -523,7 +496,7 @@ public void repriseSurErreur(Connection connexion, String phase, String tablePil
requete.append("\n AND "+ColumnEnum.ID_SOURCE.getColumnName()+" = '" + idSource + "' ");
requete.append("\n ;");

requete.append(resetPreviousPhaseMark(tablePil, idSource, null));
requete.append(PilotageOperations.resetPreviousPhaseMark(tablePil, idSource, null));

UtilitaireDao.get(0).executeBlock(connexion, requete);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public static ArcPreparedStatementBuilder querySelectIdSourceFromPilotage(String
return query;
}

public static String accessSelectEtapeForIdSource(Connection connection, String envExecution, TraitementPhase phase, TraitementEtat etat, String idSource) throws ArcException
public static String execQuerySelectEtapeForIdSource(Connection connection, String envExecution, TraitementPhase phase, TraitementEtat etat, String idSource) throws ArcException
{
ArcPreparedStatementBuilder query = new ArcPreparedStatementBuilder();

Expand All @@ -161,4 +161,31 @@ public static String accessSelectEtapeForIdSource(Connection connection, String

return UtilitaireDao.get(0).getString(connection,query);
}

/**
* Return the query that marks the files or all file if idSource not provided
* The mark indicates reset etape to 0 for the previous phase, meaning the file
* is no longer processed in the current phase
*
* @param idSource
* @return
*/
public static StringBuilder resetPreviousPhaseMark(String tablePil, String idSource, String tableSource) {
StringBuilder requete = new StringBuilder();

// mettre à etape = 0 la phase marquée à 3
requete.append("\n UPDATE " + tablePil + " a ");
requete.append("\n SET etape=0 ");
requete.append("\n WHERE a.etape=3 ");
if (idSource != null) {
requete.append("\n AND a."+ColumnEnum.ID_SOURCE.getColumnName()+" = '" + idSource + "' ");
}

if (tableSource != null) {
requete.append("\n AND EXISTS (SELECT 1 FROM " + tableSource + " b where a."+ColumnEnum.ID_SOURCE.getColumnName()+"=b."+ColumnEnum.ID_SOURCE.getColumnName()+") ");
}

requete.append("\n ;");
return requete;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ private static String marquageFinal(String tablePil, String tablePilTemp, String
requete.append("\n WHERE a."+ColumnEnum.ID_SOURCE.getColumnName()+" = '" + idSource + "' ");
requete.append("\n AND a.etape = 1 ; ");

requete.append(ApiService.resetPreviousPhaseMark(tablePil, idSource, null));
requete.append(PilotageOperations.resetPreviousPhaseMark(tablePil, idSource, null));

requete.append("\n set enable_hashjoin = on; ");
return requete.toString();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package fr.insee.arc.core.service.p0initialisation;

import java.nio.file.Paths;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Component;

import fr.insee.arc.core.dataobjects.ArcPreparedStatementBuilder;
import fr.insee.arc.core.dataobjects.ColumnEnum;
import fr.insee.arc.core.model.TraitementEtat;
import fr.insee.arc.core.model.TraitementPhase;
import fr.insee.arc.core.service.global.ApiService;
Expand All @@ -21,15 +20,14 @@
import fr.insee.arc.core.service.p0initialisation.pilotage.CleanPilotage;
import fr.insee.arc.core.service.p0initialisation.pilotage.SynchronizeDataByPilotage;
import fr.insee.arc.core.service.p0initialisation.useroperation.ReplayOrDeleteFiles;
import fr.insee.arc.core.service.p0initialisation.useroperation.ResetEnvironmentOperation;
import fr.insee.arc.core.service.p1reception.ApiReceptionService;
import fr.insee.arc.utils.dao.UtilitaireDao;
import fr.insee.arc.utils.exception.ArcException;
import fr.insee.arc.utils.files.FileUtilsArc;
import fr.insee.arc.utils.ressourceUtils.PropertiesHandler;
import fr.insee.arc.utils.structure.GenericBean;
import fr.insee.arc.utils.utils.FormatSQL;
import fr.insee.arc.utils.utils.LoggerHelper;
import fr.insee.arc.utils.utils.ManipString;

/**
* ApiNormageService
Expand Down Expand Up @@ -73,110 +71,21 @@ public void executer() throws ArcException {

// marque les fichiers ou les archives à rejouer
// efface des fichiers de la table de pilotage marqués par l'utilisateur comme étant à effacer
new ReplayOrDeleteFiles(this.coordinatorSandbox).replay();
new ReplayOrDeleteFiles(this.coordinatorSandbox).processMarkedFiles();

// Met en cohérence les table de données avec la table de pilotage de
// l'environnement
// La table de pilotage fait foi
new SynchronizeDataByPilotage(this.coordinatorSandbox).synchronizeDataByPilotage();

// remettre les archives ou elle doivent etre en cas de restauration de la base
new RestoreFileSystem(this.connexion.getCoordinatorConnection(), envExecution).execute();
new RestoreFileSystem(this.coordinatorSandbox).execute();

}

/**
* Méthode pour remettre le système d'information dans la phase précédente
* Nettoyage des tables _ok et _ko ainsi que mise à jour de la table de pilotage
* de fichier
*
* @param phase
* @param querySelection
* @param listEtat
*/
public void retourPhasePrecedente(TraitementPhase phase, ArcPreparedStatementBuilder querySelection,
ArrayList<TraitementEtat> listEtat) {
LOGGER.info("Retour arrière pour la phase : {}", phase);
ArcPreparedStatementBuilder requete;
// MAJ de la table de pilotage
Integer nbLignes = 0;

// reset etape=3 file to etape=0
try {
UtilitaireDao.get(0).executeRequest(this.connexion.getCoordinatorConnection(),
new ArcPreparedStatementBuilder(resetPreviousPhaseMark(this.tablePil, null, null)));
} catch (Exception e) {
LoggerHelper.error(LOGGER, e);
}

// Delete the selected file entries from the pilotage table from all the phases
// after the undo phase
for (TraitementPhase phaseNext : phase.nextPhases()) {
requete = new ArcPreparedStatementBuilder();
requete.append("WITH TMP_DELETE AS (DELETE FROM " + this.tablePil + " WHERE phase_traitement = "
+ requete.quoteText(phaseNext.toString()) + " ");
if (querySelection.length() > 0) {
requete.append("AND " + ColumnEnum.ID_SOURCE.getColumnName() + " IN (SELECT distinct "
+ ColumnEnum.ID_SOURCE.getColumnName() + " FROM (");
requete.append(querySelection);
requete.append(") q1 ) ");
}
requete.append("RETURNING 1) select count(1) from TMP_DELETE;");
nbLignes = nbLignes + UtilitaireDao.get(0).getInt(this.connexion.getCoordinatorConnection(), requete);
}

// Mark the selected file entries to be reload then rebuild the file system for
// the reception phase
if (phase.equals(TraitementPhase.RECEPTION)) {
requete = new ArcPreparedStatementBuilder();
requete.append("UPDATE " + this.tablePil + " set to_delete='R' WHERE phase_traitement = "
+ requete.quoteText(phase.toString()) + " ");
if (querySelection.length() > 0) {
requete.append("AND " + ColumnEnum.ID_SOURCE.getColumnName() + " IN (SELECT distinct "
+ ColumnEnum.ID_SOURCE.getColumnName() + " FROM (");
requete.append(querySelection);
requete.append(") q1 ) ");
}
try {
UtilitaireDao.get(0).executeRequest(connexion.getCoordinatorConnection(), requete);
} catch (ArcException e) {
LoggerHelper.error(LOGGER, e);
}

try {
new ReplayOrDeleteFiles(this.coordinatorSandbox).reinstate();
} catch (Exception e) {
LoggerHelper.error(LOGGER, e);
}

nbLignes++;
}

// Delete the selected file entries from the pilotage table from the undo phase
requete = new ArcPreparedStatementBuilder();
requete.append("WITH TMP_DELETE AS (DELETE FROM " + this.tablePil + " WHERE phase_traitement = "
+ requete.quoteText(phase.toString()) + " ");
if (querySelection.length() > 0) {
requete.append("AND " + ColumnEnum.ID_SOURCE.getColumnName() + " IN (SELECT distinct "
+ ColumnEnum.ID_SOURCE.getColumnName() + " FROM (");
requete.append(querySelection);
requete.append(") q1 ) ");
}
requete.append("RETURNING 1) select count(1) from TMP_DELETE;");
nbLignes = nbLignes + UtilitaireDao.get(0).getInt(this.connexion.getCoordinatorConnection(), requete);

// Run a database synchronization with the pilotage table
try {
new SynchronizeDataByPilotage(this.coordinatorSandbox).synchronizeDataByPilotage();
} catch (Exception e) {
LoggerHelper.error(LOGGER, e);
}

if (nbLignes > 0) {
DatabaseMaintenance.maintenanceDatabaseClassic(connexion.getCoordinatorConnection(), envExecution);
}

// Penser à tuer la connexion
List<TraitementEtat> listEtat) throws ArcException {
new ResetEnvironmentOperation(this.coordinatorSandbox).retourPhasePrecedente(phase, querySelection, listEtat);
}


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

import java.util.ArrayList;
import java.util.Arrays;
Expand All @@ -9,16 +9,16 @@
import fr.insee.arc.core.dataobjects.ArcPreparedStatementBuilder;
import fr.insee.arc.core.model.TraitementEtat;
import fr.insee.arc.core.model.TraitementPhase;
import fr.insee.arc.core.service.p0initialisation.ApiInitialisationService;
import fr.insee.arc.core.util.StaticLoggerDispatcher;
import fr.insee.arc.utils.exception.ArcException;

public class ResetEnvironmentOperations {
public class ResetEnvironmentService {

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

protected static final Logger LOGGER = LogManager.getLogger(ResetEnvironmentOperations.class);
protected static final Logger LOGGER = LogManager.getLogger(ResetEnvironmentService.class);

/**
* Retour arriere vers une phase
Expand All @@ -27,9 +27,10 @@ private ResetEnvironmentOperations() {
* @param env
* @param rootDirectory
* @param undoFilesSelection
* @throws ArcException
*/
public static void backToTargetPhase(TraitementPhase phaseAExecuter, String env, String rootDirectory,
ArcPreparedStatementBuilder undoFilesSelection) {
ArcPreparedStatementBuilder undoFilesSelection) throws ArcException {
if (phaseAExecuter.getOrdre() == TraitementPhase.INITIALISATION.getOrdre()) {
resetBAS(env, rootDirectory);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import fr.insee.arc.core.service.global.bo.Sandbox;
import fr.insee.arc.core.service.global.dao.DataStorage;
import fr.insee.arc.core.service.global.dao.FileSystemManagement;
import fr.insee.arc.core.service.p1reception.ApiReceptionService;
Expand All @@ -24,12 +25,10 @@ public class RestoreFileSystem {
private Connection connection;
private String envExecution;



public RestoreFileSystem(Connection connection, String envExecution) {
public RestoreFileSystem(Sandbox sandbox) {
super();
this.connection = connection;
this.envExecution = envExecution;
this.connection = sandbox.getConnection();
this.envExecution = sandbox.getSchema();
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ private static void dropUnusedDataTables(Connection coordinatorConnexion, Connec
// retrieve the idSource of the childDataTable
String idSource = PhaseOperations.selectIdSourceOfChildDataTable(executorConnection,
childDataTable);
String etape = PilotageOperations.accessSelectEtapeForIdSource(coordinatorConnexion, envExecution,
String etape = PilotageOperations.execQuerySelectEtapeForIdSource(coordinatorConnexion, envExecution,
phase, etat, idSource);

// if no references in pilotage table, mark for drop
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import fr.insee.arc.core.dataobjects.ViewEnum;
import fr.insee.arc.core.model.TraitementPhase;
import fr.insee.arc.core.service.global.ApiService;
import fr.insee.arc.core.service.global.dao.PilotageOperations;
import fr.insee.arc.core.service.global.dao.TableNaming;
import fr.insee.arc.utils.dao.CopyObjectsToDatabase;
import fr.insee.arc.utils.dao.SQL;
Expand Down Expand Up @@ -35,7 +36,7 @@ public static void resetEtapePilotageDao(Connection connection, String envExecut

requete.append("DELETE FROM " + tablePil + " WHERE etat_traitement='{ENCOURS}';");

requete.append(ApiService.resetPreviousPhaseMark(tablePil, null, null));
requete.append(PilotageOperations.resetPreviousPhaseMark(tablePil, null, null));

requete.append("WITH tmp_1 as (select " + ColumnEnum.ID_SOURCE.getColumnName() + ", max(");
new StringBuilder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ public ReplayOrDeleteFiles(Sandbox sandbox) {
private Sandbox sandbox;


public void replay() throws ArcException
public void processMarkedFiles() throws ArcException
{
reinstate();
replayMarkedFiles();

cleanToDelete();
deleteMarkedFiles();
}


Expand All @@ -41,7 +41,7 @@ public void replay() throws ArcException
* @param tablePil
* @throws ArcException
*/
public void reinstate() throws ArcException {
public void replayMarkedFiles() throws ArcException {
LoggerHelper.info(LOGGER, "reinstateWithRename");

Connection connection=sandbox.getConnection();
Expand Down Expand Up @@ -82,7 +82,7 @@ public void reinstate() throws ArcException {
* @param tablePil
* @throws ArcException
*/
private void cleanToDelete() throws ArcException {
private void deleteMarkedFiles() throws ArcException {
LoggerHelper.info(LOGGER, "Delete file marked by user as to be deleted");

Connection connection=sandbox.getConnection();
Expand Down
Loading

0 comments on commit e766062

Please sign in to comment.