Skip to content

Commit

Permalink
moved calls to PilotageDao
Browse files Browse the repository at this point in the history
  • Loading branch information
lmanelphe committed Oct 4, 2023
1 parent 5c7c3eb commit 2fe4716
Show file tree
Hide file tree
Showing 9 changed files with 313 additions and 217 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ public enum ColumnEnum {
, POS("pos", TypeEnum.BIGINT, "ordinal position") // export
, MAXP("maxp", TypeEnum.BIGINT, "highest position") // export
, I("i", TypeEnum.BIGINT, "row number index") // maintenanceparametre
, NB("nb", TypeEnum.BIGINT, "count") // pilotage

, TEST1(PgColumnEnum.TEST1)
, TEST2(PgColumnEnum.TEST2)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ public enum ViewEnum {

// tables représentant le contenu des vobject (utilisées pour les tests)
, VIEW_PILOTAGE_FICHIER("pilotage_fichier", SchemaEnum.SANDBOX, ColumnEnum.DATE_ENTREE) //

, VIEW_RAPPORT_FICHIER("pilotage_fichier", SchemaEnum.SANDBOX, ColumnEnum.DATE_ENTREE,
ColumnEnum.PHASE_TRAITEMENT, ColumnEnum.ETAT_TRAITEMENT, ColumnEnum.RAPPORT, ColumnEnum.NB) //
// tables de pilotage
, PILOTAGE_FICHIER("pilotage_fichier", SchemaEnum.SANDBOX, ColumnEnum.ID_SOURCE, ColumnEnum.ID_NORME,
ColumnEnum.VALIDITE, ColumnEnum.PERIODICITE, ColumnEnum.PHASE_TRAITEMENT, ColumnEnum.ETAT_TRAITEMENT,
Expand Down
2 changes: 2 additions & 0 deletions arc-utils/src/main/java/fr/insee/arc/utils/dao/SQL.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public enum SQL {

LIKE("LIKE"),

IS_NULL("IS NULL"), IS_NOT_NULL("IS NOT NULL"),

GROUP_BY("GROUP BY"),

TRUE("TRUE"), FALSE("FALSE"),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package fr.insee.arc.web.gui.pilotage.dao;

import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
Expand All @@ -15,9 +14,12 @@

import fr.insee.arc.core.dataobjects.ArcPreparedStatementBuilder;
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.TraitementEtat;
import fr.insee.arc.core.model.TraitementPhase;
import fr.insee.arc.core.service.global.dao.HashFileNameConversion;
import fr.insee.arc.utils.dao.SQL;
import fr.insee.arc.utils.dao.UtilitaireDao;
import fr.insee.arc.utils.dataobjects.TypeEnum;
import fr.insee.arc.utils.exception.ArcException;
Expand All @@ -27,7 +29,7 @@
import fr.insee.arc.utils.utils.ManipString;
import fr.insee.arc.web.gui.all.util.VObject;
import fr.insee.arc.web.gui.all.util.VObjectHelperDao;
import fr.insee.arc.web.gui.pilotage.service.ServiceViewArchiveBAS;
import fr.insee.arc.web.gui.all.util.VObjectService;

@Component
public class PilotageDao extends VObjectHelperDao {
Expand Down Expand Up @@ -81,6 +83,20 @@ public void initializePilotageBAS(VObject viewPilotageBAS) {
this.vObjectService.initialize(viewPilotageBAS, requete, null, defaultInputFields);

}

/**
* dao call to get sandbox description
* @throws ArcException
*/
public String getSandboxDescription(String bacASable) throws ArcException {
ViewEnum dataEtatJeuderegle = ViewEnum.EXT_ETAT_JEUDEREGLE;
// query
ArcPreparedStatementBuilder query = new ArcPreparedStatementBuilder();
query.build(SQL.SELECT, ColumnEnum.ENV_DESCRIPTION, SQL.FROM, dataEtatJeuderegle.getFullName());
query.build(SQL.WHERE, "replace(id,'.','_') = ", query.quoteText(bacASable));
// return list
return UtilitaireDao.get(0).getString(null, query);
}

public void initializeRapportBAS(VObject viewRapportBAS) {
ViewEnum dataModelRapportBAS = ViewEnum.PILOTAGE_FICHIER;
Expand Down Expand Up @@ -119,7 +135,8 @@ public void initializeEntrepotBAS(VObject viewEntrepotBAS) throws ArcException {
ViewEnum dataModelEntrepotBAS = ViewEnum.IHM_ENTREPOT;
// view query
ArcPreparedStatementBuilder requete = new ArcPreparedStatementBuilder();
if (Boolean.TRUE.equals(UtilitaireDao.get(0).hasResults(null, FormatSQL.tableExists("arc.ihm_entrepot")))) {
boolean existsEntrepot = UtilitaireDao.get(0).hasResults(null, FormatSQL.tableExists(dataModelEntrepotBAS.getFullName()));
if (existsEntrepot) {
requete.append("select id_entrepot from ");
requete.append(dataObjectService.getView(dataModelEntrepotBAS));
} else {
Expand Down Expand Up @@ -218,5 +235,227 @@ public void execQueryDownloadArchive(HttpServletResponse response, VObject viewA
listRepertoire);

}

public void downloadFichierBAS(VObject viewFichierBAS, HttpServletResponse response,
String repertoire, String bacASable) {
// récupération de la liste des id_source
Map<String, ArrayList<String>> selection = viewFichierBAS.mapContentSelected();
ArcPreparedStatementBuilder query = vObjectService.queryView(viewFichierBAS);
// si la selection de fichiers n'est pas vide, on se restreint aux fichiers
// sélectionner
//
if (!selection.isEmpty()) {
query.build(SQL.AND, ColumnEnum.ID_SOURCE, SQL.IN, "(", query.sqlListeOfValues(selection.get("id_source")), ") ");
}

// optimisation pour avoir des bloc successifs sur la même archive
query.build(SQL.ORDER_BY, ColumnEnum.CONTAINER);

vObjectService.downloadXML(viewFichierBAS, response, query, repertoire, bacASable, TraitementPhase.RECEPTION.toString());
}

public ArcPreparedStatementBuilder queryUpdateToDelete(VObject viewFichierBAS, String code) {
Map<String, ArrayList<String>> selection = viewFichierBAS.mapContentSelected();
ArcPreparedStatementBuilder querySelection = requestSelectToMark(viewFichierBAS, selection);
return requeteUpdateToMark(querySelection, code);
}

public void execQueryUpdateToDelete(ArcPreparedStatementBuilder updateToDelete) throws ArcException {
UtilitaireDao.get(0).executeRequest(null, updateToDelete);
}

public void downloadBdBAS(VObject viewFichierBAS, HttpServletResponse response,
ArrayList<String> tableDownload, String phase, String etatBdd, String date) throws ArcException {

// List of queries that will be executed to download
List<ArcPreparedStatementBuilder> tableauRequete = new ArrayList<>();
// Name of the file containing the data download
List<String> fileNames = new ArrayList<>();
ArcPreparedStatementBuilder requete;

for (String t : tableDownload) {

// Check if the table to download got children
requete = new ArcPreparedStatementBuilder();
requete.append(FormatSQL.getAllInheritedTables(ManipString.substringBeforeFirst(t, "."),
ManipString.substringAfterFirst(t, ".")));
requete.append(" LIMIT 1");

if (Boolean.TRUE.equals(UtilitaireDao.get(0).hasResults(null, requete))) {

// Get the files to download
requete = new ArcPreparedStatementBuilder();
requete.append("SELECT id_source FROM " + dataObjectService.getView(ViewEnum.PILOTAGE_FICHIER));
requete.append("\n WHERE phase_traitement=" + requete.quoteText(phase) + " ");
requete.append("\n AND etat_traitement=" + requete.quoteText(etatBdd) + "::text[] ");
requete.append("\n AND date_entree=" + requete.quoteText(date) + " ");

// Si des fichiers ont été selectionnés, on ajoute a la requete la liste des
// fichiers
if (!viewFichierBAS.mapContentSelected().isEmpty()) {
ArrayList<String> filesSelected = viewFichierBAS.mapContentSelected().get("id_source");
requete.append("AND id_source IN (");
for (int i = 0; i < filesSelected.size(); i++) {
if (i > 0) {
requete.append(",");
}
requete.append("'" + filesSelected.get(i) + "'");
}
requete.append(")");
}

ArrayList<String> idSources = new GenericBean(UtilitaireDao.get(0).executeRequest(null, requete))
.mapContent().get("id_source");

// for each files, generate the download query
for (String idSource : idSources) {
tableauRequete.add(new ArcPreparedStatementBuilder(
"SELECT * FROM " + HashFileNameConversion.tableOfIdSource(t, idSource)));
fileNames.add(t + "_" + idSource);
}

}
// if no children
else {

requete = new ArcPreparedStatementBuilder();
requete.append("WITH prep as ( ");
requete.append("SELECT id_source FROM " + dataObjectService.getView(ViewEnum.PILOTAGE_FICHIER));
requete.append("\n WHERE phase_traitement=" + requete.quoteText(phase) + " ");
requete.append("\n AND etat_traitement=" + requete.quoteText(etatBdd) + "::text[] ");
requete.append("\n AND date_entree=" + requete.quoteText(date) + " ");

// Si des fichiers ont été selectionnés, on ajoute a la requete la liste des
// fichiers
if (!viewFichierBAS.mapContentSelected().isEmpty()) {
requete.append("\n AND id_source IN (");
requete.append(
requete.sqlListeOfValues(viewFichierBAS.mapContentSelected().get("id_source")));
requete.append(")");
}
requete.append(" ) ");
requete.append("\n SELECT * from " + t
+ " a where exists (select 1 from prep b where a.id_source=b.id_source) ");
tableauRequete.add(requete);
fileNames.add(t);
}

}

this.vObjectService.download(viewFichierBAS, response, fileNames, tableauRequete);
}

public void downloadEnvelopeBAS(VObject viewFichierBAS, HttpServletResponse response,
String chemin, ArrayList<String> listRepertoire) {
// récupération de la liste des noms d'enloppe
Map<String, ArrayList<String>> selection = viewFichierBAS.mapContentSelected();
ArcPreparedStatementBuilder query = new ArcPreparedStatementBuilder();
query.append("select distinct alias_de_table.container as nom_fichier from (");
query.append(viewFichierBAS.getMainQuery());
query.append(") alias_de_table ");
query.append(vObjectService.buildFilter(viewFichierBAS.getFilterFields(), viewFichierBAS.getHeadersDLabel()));
if (!selection.isEmpty()) {
query.append(" AND container IN (" + query.sqlListeOfValues(selection.get("container")) + ") ");
}
vObjectService.downloadEnveloppe(viewFichierBAS, response, query, chemin, listRepertoire);
}



/**
* Prepare a request selecting the line to change when marking files for
* deletion/replay.
*/
private ArcPreparedStatementBuilder requestSelectToMark(VObject viewFichierBAS, Map<String, ArrayList<String>> selection) {
ArcPreparedStatementBuilder querySelection = new ArcPreparedStatementBuilder();
querySelection.append("select distinct container, id_source from (");
querySelection.append(viewFichierBAS.getMainQuery());
querySelection.append(") alias_de_table ");
querySelection.append(this.vObjectService.buildFilter(viewFichierBAS.getFilterFields(),
viewFichierBAS.getHeadersDLabel()));
// si la selection de fichiers n'est pas vide, on se restreint aux fichiers
// sélectionné
if (!selection.isEmpty()) {
// concaténation des informations
ArrayList<String> infoConcatenee = new ArrayList<>();
ArrayList<String> listContainer = selection.get("container");
ArrayList<String> listIdSource = selection.get("id_source");

for (int i = 0; i < selection.get("id_source").size(); i++) {
infoConcatenee.add(listContainer.get(i) + "+" + listIdSource.get(i));
}
querySelection.append(" AND container||'+'||id_source IN " + Format.sqlListe(infoConcatenee) + " ");
}
return querySelection;
}

/**
* Mark the line selected by querySelection for deletion/replay (depending on
* value).
*/
private ArcPreparedStatementBuilder requeteUpdateToMark(ArcPreparedStatementBuilder querySelection, String value) {
ArcPreparedStatementBuilder updateToDelete = new ArcPreparedStatementBuilder();
updateToDelete.append("WITH ");
updateToDelete.append("prep AS ( ");
updateToDelete.append(querySelection);
updateToDelete.append(" ) ");
updateToDelete.append("UPDATE " + dataObjectService.getView(ViewEnum.PILOTAGE_FICHIER) + " a ");
updateToDelete.append("SET to_delete=" + value + " ");
updateToDelete.append(
"WHERE EXISTS (SELECT 1 FROM prep WHERE a.container=prep.container AND a.id_source=prep.id_source); ");
return updateToDelete;
}

public void execQueryUpdateEnvDescription(VObject viewPilotageBAS, String envDescription, String bacASable) throws ArcException {
ArcPreparedStatementBuilder envQuery = new ArcPreparedStatementBuilder();
envQuery.append("update arc.ext_etat_jeuderegle set env_description = ");
envQuery.append(envQuery.quoteText(viewPilotageBAS.getCustomValue(envDescription)));
envQuery.append("where replace(id,'.','_') = ");
envQuery.append(envQuery.quoteText(bacASable));
UtilitaireDao.get(0).executeRequest(null, envQuery);
}

public String execQueryTime() throws ArcException {
return UtilitaireDao.get(0).getString(null, new ArcPreparedStatementBuilder("SELECT last_init from arc.pilotage_batch"));
}

public String execQueryState() throws ArcException {
return UtilitaireDao.get(0).getString(null, new ArcPreparedStatementBuilder("SELECT case when operation='O' then 'active' else 'inactive' end from arc.pilotage_batch;"));
}

public void execQueryDelayBatchTime() throws ArcException {
UtilitaireDao.get(0).executeRequest(null,
new ArcPreparedStatementBuilder("UPDATE arc.pilotage_batch set last_init=to_char(current_date + interval '7 days','yyyy-mm-dd')||':22';"));
}

public void execQueryForwardBatchTime() throws ArcException {
UtilitaireDao.get(0).executeRequest(null,
new ArcPreparedStatementBuilder("UPDATE arc.pilotage_batch set last_init=to_char(current_date-interval '1 days','yyyy-mm-dd')||':22';"));
}

public void execQueryToggleOn() throws ArcException {
UtilitaireDao.get(0).executeRequest(null, new ArcPreparedStatementBuilder("UPDATE arc.pilotage_batch set operation='O'; "));
}

public void execQueryToggleOff() throws ArcException {
UtilitaireDao.get(0).executeRequest(null, new ArcPreparedStatementBuilder("UPDATE arc.pilotage_batch set operation='N'; "));
}


public VObjectService getvObjectService() {
return vObjectService;
}

public void setvObjectService(VObjectService vObjectService) {
this.vObjectService = vObjectService;
}

public DataObjectService getDataObjectService() {
return dataObjectService;
}

public void setDataObjectService(DataObjectService dataObjectService) {
this.dataObjectService = dataObjectService;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,14 @@
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.context.WebApplicationContext;

import fr.insee.arc.core.dataobjects.ArcPreparedStatementBuilder;
import fr.insee.arc.core.model.TraitementPhase;
import fr.insee.arc.utils.dao.UtilitaireDao;
import fr.insee.arc.utils.exception.ArcException;
import fr.insee.arc.utils.utils.LoggerHelper;
import fr.insee.arc.utils.utils.ManipString;
import fr.insee.arc.web.gui.all.service.ArcWebGenericService;
import fr.insee.arc.web.gui.all.util.ConstantVObject.ColumnRendering;
import fr.insee.arc.web.gui.all.util.LineObject;
import fr.insee.arc.web.gui.all.util.VObject;
import fr.insee.arc.web.gui.all.util.ConstantVObject.ColumnRendering;
import fr.insee.arc.web.gui.pilotage.dao.PilotageDao;
import fr.insee.arc.web.gui.pilotage.model.ModelPilotage;

Expand Down Expand Up @@ -124,12 +122,8 @@ public void initializePilotageBAS(VObject viewPilotageBAS) {
this.vObjectService.applyColumnRendering(viewPilotageBAS, columns);

// display comment for the sandbox
ArcPreparedStatementBuilder envQuery = new ArcPreparedStatementBuilder();
envQuery.append("select env_description from arc.ext_etat_jeuderegle where replace(id,'.','_') = ");
envQuery.append(envQuery.quoteText(getBacASable()));

try {
String envDescription = UtilitaireDao.get(0).getString(null, envQuery);
String envDescription = dao.getSandboxDescription(getBacASable());
viewPilotageBAS.setCustomValue(ENV_DESCRIPTION, envDescription);
} catch (ArcException e) {
loggerDispatcher.error("Error in initializePilotageBAS", e, LOGGER);
Expand Down
Loading

0 comments on commit 2fe4716

Please sign in to comment.