Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/InseeFr/ARC.git
Browse files Browse the repository at this point in the history
  • Loading branch information
Nolife999 committed Sep 27, 2023
2 parents 714dee3 + c992b68 commit b866a8f
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,15 @@ public enum ColumnEnum {
, ENTREPOT("entrepot", TypeEnum.TEXT, "name of the datastore that recieve archive file")
, NOM_ARCHIVE("nom_archive", TypeEnum.TEXT, "name of archive file")

, TEST1(PgColumnEnum.TEST1), TEST2(PgColumnEnum.TEST2)
// aliases
, VARBDD("varbdd", TypeEnum.TEXT, "variable in BDD") // export
, POS("pos", TypeEnum.BIGINT, "ordinal position") // export
, MAXP("maxp", TypeEnum.BIGINT, "highest position") // export
, I("i", TypeEnum.BIGINT, "row number index") // maintenanceparametre

, TEST1(PgColumnEnum.TEST1)
, TEST2(PgColumnEnum.TEST2)

, COLUMN_NAME(PgColumnEnum.COLUMN_NAME)
, TABLE_NAME(PgColumnEnum.TABLE_NAME)
, TABLE_SCHEMA(PgColumnEnum.TABLE_SCHEMA)
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 @@ -8,6 +8,8 @@ public enum SQL {
COMMIT("COMMIT"),

INSERT_INTO("INSERT INTO"), DELETE ("DELETE FROM"),

UPDATE("UPDATE"), SET("SET"),

ON_CONFLICT_DO_NOTHING("ON CONFLICT DO NOTHING"),

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,15 @@ public ExportDao(VObjectService vObjectService, DataObjectService dataObjectServ
*/
public void initializeViewExport(VObject viewExport) {
ViewEnum dataModelExport = ViewEnum.EXPORT;
String nameOfViewExport = dataObjectService.getView(dataModelExport);
// view query
ArcPreparedStatementBuilder query = new ArcPreparedStatementBuilder();
query.append(SQL.SELECT);
query.append(query.sqlListeOfColumnsFromModel(dataModelExport));
query.append(SQL.FROM);
query.append(dataObjectService.getView(dataModelExport));
StringBuilder columns = query.sqlListeOfColumnsFromModel(dataModelExport);
query.build(SQL.SELECT, columns, SQL.FROM, nameOfViewExport);
// default value
HashMap<String, String> defaultInputFields = new HashMap<>();
// initialize vobject
vObjectService.initialize(viewExport, query, dataObjectService.getView(dataModelExport), defaultInputFields);
vObjectService.initialize(viewExport, query, nameOfViewExport, defaultInputFields);
}

/**
Expand All @@ -59,18 +58,15 @@ public void initializeViewExport(VObject viewExport) {
* @return A map of all exports to make with their associated rules
* @throws ArcException
*/
public HashMap<String, ArrayList<String>> startExportRetrieve(VObject viewExport) throws ArcException {
public HashMap<String, ArrayList<String>> startExportRetrieve() throws ArcException {
ViewEnum dataModelExport = ViewEnum.EXPORT;
String nameOfViewExport = dataObjectService.getView(dataModelExport);
// Récupérer les exports sélectionnés
ArcPreparedStatementBuilder query = new ArcPreparedStatementBuilder();
query.append(SQL.SELECT);
query.append(query.sqlListeOfColumnsFromModel(dataModelExport));
query.append(SQL.FROM);
query.append(dataObjectService.getView(dataModelExport));
query.append(SQL.WHERE);
query.append(ColumnEnum.FILE_NAME);
query.append(" IN ");
query.append("(" + query.sqlListeOfValues(getSelectedRecords().get("file_name")) + ") ");
StringBuilder columns = query.sqlListeOfColumnsFromModel(dataModelExport);
StringBuilder selectedRecords = query.sqlListeOfValues(getSelectedRecords().get("file_name"));
query.build(SQL.SELECT, columns, SQL.FROM, nameOfViewExport,
SQL.WHERE, ColumnEnum.FILE_NAME, SQL.IN, "(", selectedRecords, ") ");
return new GenericBean(UtilitaireDao.get(0).executeRequest(vObjectService.getConnection(), query)).mapContent();
}

Expand All @@ -85,22 +81,17 @@ public HashMap<String, ArrayList<String>> startExportRetrieve(VObject viewExport
*/
public void startExportUpdateState(List<String> fileName, int fileIndex, boolean isExported) throws ArcException {
ViewEnum dataModelExport = ViewEnum.EXPORT;
String nameOfViewExport = dataObjectService.getView(dataModelExport);
// Actualiser l'état de l'export
ArcPreparedStatementBuilder query = new ArcPreparedStatementBuilder();
query.append("UPDATE ");
query.append(dataObjectService.getView(dataModelExport));
query.append(" SET ");
query.append(ColumnEnum.ETAT);
query.append("=");
String etatSet;
if (isExported) {
query.append("to_char(current_timestamp,'YYYY-MM-DD HH24:MI:SS') ");
etatSet = "to_char(current_timestamp,'YYYY-MM-DD HH24:MI:SS') ";
} else {
query.append(query.quoteText(TraitementEtat.ENCOURS.toString()));
etatSet = query.quoteText(TraitementEtat.ENCOURS.toString());
}
query.append(SQL.WHERE);
query.append(ColumnEnum.FILE_NAME);
query.append("=");
query.append(query.quoteText(fileName.get(fileIndex)));
query.build(SQL.UPDATE, nameOfViewExport, SQL.SET, ColumnEnum.ETAT, "=", etatSet,
SQL.WHERE, ColumnEnum.FILE_NAME, "=", query.quoteText(fileName.get(fileIndex)));
UtilitaireDao.get(0).executeRequest(vObjectService.getConnection(), query);
}

Expand All @@ -117,23 +108,19 @@ public HashMap<String, ArrayList<String>> exportFileRetrieve(int n, List<String>

// lire la table how to export pour voir comment on va s'y prendre
// L'objectif est de créer une hashmap de correspondance entre la variable et la position
return new GenericBean(UtilitaireDao.get(0).executeRequest(vObjectService.getConnection(),
new ArcPreparedStatementBuilder(
"SELECT lower(varbdd) as varbdd, pos::int-1 as pos, max(pos::int) over() as maxp FROM "
+ howToExportReworked + " order by pos::int ")))
.mapContent();
ArcPreparedStatementBuilder query = new ArcPreparedStatementBuilder();
query.build(SQL.SELECT, "lower(", ColumnEnum.VARBDD, ")", SQL.AS, ColumnEnum.VARBDD, SQL.COMMA,
ColumnEnum.POS, SQL.CAST_OPERATOR, "int-1", SQL.AS, ColumnEnum.POS, SQL.COMMA,
"max(", ColumnEnum.POS, SQL.CAST_OPERATOR, "int) over()", SQL.AS, ColumnEnum.MAXP, SQL.FROM,
howToExportReworked, SQL.ORDER_BY, ColumnEnum.POS, SQL.CAST_OPERATOR, "int");
return new GenericBean(UtilitaireDao.get(0).executeRequest(vObjectService.getConnection(), query)).mapContent();
}

public ResultSet exportFileFilteredOrdered(Statement stmt, int n, List<String> tablesToExport, List<String> filterTable, List<String> orderTable, String bacASable) throws SQLException {
ArcPreparedStatementBuilder query = new ArcPreparedStatementBuilder();
query.append(SQL.SELECT);
query.append("*");
query.append(SQL.FROM);
query.append(bacASable + "." + tablesToExport.get(n));
query.append(SQL.WHERE);
query.append((StringUtils.isEmpty(filterTable.get(n)) ? "true" : filterTable.get(n)) + " ");
query.append(StringUtils.isEmpty(orderTable.get(n)) ? "" : "ORDER BY " + orderTable.get(n) + " ");

query.build(SQL.SELECT, "*", SQL.FROM, bacASable, SQL.DOT, tablesToExport.get(n),
SQL.WHERE, (StringUtils.isEmpty(filterTable.get(n)) ? SQL.TRUE : filterTable.get(n)), " ",
(StringUtils.isEmpty(orderTable.get(n)) ? "" : SQL.ORDER_BY + orderTable.get(n)), " ");
return stmt.executeQuery(query.getQuery().toString());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public String startExport(Model model) {
// Requêter les exports à réaliser
VObject viewExport = views.getViewExport();
dao.setSelectedRecords(viewExport.mapContentSelected());
HashMap<String, ArrayList<String>> rules = dao.startExportRetrieve(viewExport);
HashMap<String, ArrayList<String>> rules = dao.startExportRetrieve();

ArrayList<String> fileName = rules.get("file_name");
ArrayList<String> zip = rules.get("zip");
Expand All @@ -85,9 +85,9 @@ public String startExport(Model model) {
dao.startExportUpdateState(fileName, fileIndex, true);
}
} catch (ArcException | SQLException e) {
views.getViewExport().setMessage("Export failed because of database query");
views.getViewExport().setMessage("export.error.database");
} catch (IOException e) {
views.getViewExport().setMessage("Export failed because of file system problem");
views.getViewExport().setMessage("export.error.filesystem");
}

return generateDisplay(model, RESULT_SUCCESS);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ public String deleteFileExport(Model model) {
try {
FileUtilsArc.delete(new File(dirOut + File.separator + s));
} catch (ArcException e) {
this.views.getViewFileExport().setMessage("La suppression du fichier " + s + " a échouée");
this.views.getViewFileExport().setMessage("export.file.delete.error");
this.views.getViewFileExport().setMessageArgs(s);
}
}
}
Expand All @@ -55,7 +56,7 @@ public String updateFileExport(Model model) {
try {
FileUtilsArc.renameTo(fileIn, fileOut);
} catch (ArcException e) {
this.views.getViewFileExport().setMessage("Le renommage a échoué");
this.views.getViewFileExport().setMessage("export.file.rename.error");
}

}
Expand Down
2 changes: 2 additions & 0 deletions arc-web/src/main/resources/messages_en.properties
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ fileManagement.createDirectory.error=Directory creation failed : {0}

export.error.database=Export failed because of database query
export.error.filesystem=Export failed because of file system problem
export.file.delete.error=Deletion of file {0} failed
export.file.rename.error=Renaming failed

query.complete=Query complete!

Expand Down
2 changes: 2 additions & 0 deletions arc-web/src/main/resources/messages_fr.properties
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ fileManagement.createDirectory.error=La cr\u00e9ation du r\u00e9pertoire a \u00e

export.error.database=L'export a \u00e9chou\u00e9 \u00e0 cause de la requ\u00eate sur la base de donn\u00e9es
export.error.filesystem=L'export a \u00e9chou\u00e9 \u00e0 cause d'un probl\u00e8me du syst\u00e8me de fichiers
export.file.delete.error=La suppression du fichier {0} a \u00e9chou\u00e9
export.file.rename.error=Le renommage a \u00e9chou\u00e9

query.complete=Requ\u00eate termin\u00e9e !

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,14 @@ public void initializeViewExport() {
@Test
public void startExportRetrieve() throws ArcException {

VObject viewExport = new ViewExport();

// select the first record of viewNorm and set it as the selected record
ArcPreparedStatementBuilder query = new ArcPreparedStatementBuilder();
query.build(SQL.SELECT, "*", SQL.FROM, pdao.getDataObjectService().getView(ViewEnum.EXPORT), SQL.WHERE, "file_name='test1'");
Map<String, ArrayList<String>> viewExportSelectedRecords = new GenericBean(UtilitaireDao.get(0).executeRequest(c, query)).mapContent();
pdao.setSelectedRecords(viewExportSelectedRecords);

// execute query
HashMap<String, ArrayList<String>> retrievedData = pdao.startExportRetrieve(viewExport);
HashMap<String, ArrayList<String>> retrievedData = pdao.startExportRetrieve();

// test the content of the view
List<String> viewColumns = ColumnEnum
Expand Down

0 comments on commit b866a8f

Please sign in to comment.