Skip to content

Commit cb014e7

Browse files
authored
dev(validator-plugin-cnig): ajout du champ nomreg dans les assiettes (#389)
1 parent 5e40866 commit cb014e7

File tree

4 files changed

+70
-10
lines changed

4 files changed

+70
-10
lines changed

validator-plugin-cnig/src/main/java/fr/ign/validator/cnig/CnigPlugin.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ public void setup(Context context) {
5454
context.addListener(new CustomizeIdurbaPreProcess());
5555

5656
/*
57-
* PostProcess - Join SUP files to add a column "fichiers" and "nomsuplitt"
58-
* (must run before CreateShapefilesPostProcess)
57+
* PostProcess - Join SUP files to add columns "fichiers", "nomsuplitt" and
58+
* "nomreg" (must run before CreateShapefilesPostProcess)
5959
*/
6060
context.addListener(new SupRelationsPostProcess());
6161
/*

validator-plugin-cnig/src/main/java/fr/ign/validator/cnig/process/SupRelationsPostProcess.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public void afterValidate(Context context, Document document) throws Exception {
100100

101101
log.info(
102102
MARKER,
103-
"Perform joins to add columns 'fichier' and 'nomsuplitt' to normalized GENERATEUR and ASSIETTE tables..."
103+
"Perform joins to add columns 'fichier', 'nomsuplitt' and 'nomreg' to normalized GENERATEUR and ASSIETTE tables..."
104104
);
105105
File tempDirectory = getTempDirectory(context);
106106
AdditionalColumnsBuilder builder = new AdditionalColumnsBuilder(

validator-plugin-cnig/src/main/java/fr/ign/validator/cnig/sup/AdditionalColumnsBuilder.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
* <ul>
2727
* <li>"fichier" is reported from "acte_sup" table exploring relations</li>
2828
* <li>"nomsuplitt" is reported from "servitude" table exploring relations</li>
29+
* <li>"nomreg" is reported from "servitude" table exploring relations</li>
2930
* </ul>
3031
*
3132
* @author MBorne
@@ -91,7 +92,7 @@ public void addColumnsToGenerateurAndAssietteFiles(File dataDirectory) throws Ex
9192
* @throws IOException
9293
*/
9394
private void addColumnsToFile(FileType fileType, File file) throws Exception {
94-
log.info(MARKER, "Add 'fichier' and 'nomsuplitt' columns to {} ...", file);
95+
log.info(MARKER, "Add 'fichier', 'nomsuplitt' and 'nomreg' columns to {} ...", file);
9596
/*
9697
* lecture des métadonnées du fichier en entrée
9798
*/
@@ -114,7 +115,7 @@ private void addColumnsToFile(FileType fileType, File file) throws Exception {
114115
);
115116
CSVPrinter printer = new CSVPrinter(fileWriter, CSVFormat.RFC4180);
116117

117-
/* create output header adding "fichier","nomsuplitt" */
118+
/* create output header adding "fichier","nomsuplitt","nomreg" */
118119
printer.printRecord(createOutputHeader(inputHeader));
119120

120121
while (reader.hasNext()) {
@@ -129,12 +130,15 @@ private void addColumnsToFile(FileType fileType, File file) throws Exception {
129130
/* retrieve "nomsuplitt" using joins */
130131
List<DatabaseSUP.Servitude> servitudes = getServitudesById(database, fileType, id);
131132
List<String> nomSupLitts = database.getNomSupLitts(servitudes);
133+
/* retrive "nomreg" usin joins */
134+
List<String> nomRegs = database.getNomRegs(servitudes);
132135

133136
printer.printRecord(
134137
createOutputRow(
135138
inputRow,
136139
fichiers,
137-
nomSupLitts
140+
nomSupLitts,
141+
nomRegs
138142
)
139143
);
140144
}
@@ -157,6 +161,7 @@ private List<String> createOutputHeader(String[] inputHeader) {
157161
List<String> outputHeader = new ArrayList<String>(Arrays.asList(inputHeader));
158162
outputHeader.add(DatabaseSUP.COLUMN_FICHIER);
159163
outputHeader.add(DatabaseSUP.COLUMN_NOMSUPLITT);
164+
outputHeader.add(DatabaseSUP.COLUMN_NOMREG);
160165
return outputHeader;
161166
}
162167

@@ -167,13 +172,16 @@ private List<String> createOutputHeader(String[] inputHeader) {
167172
* @param fichiers
168173
* @return
169174
*/
170-
private List<String> createOutputRow(String[] inputRow, List<String> fichiers, List<String> nomSupLitts) {
175+
private List<String> createOutputRow(String[] inputRow, List<String> fichiers, List<String> nomSupLitts,
176+
List<String> nomRegs) {
171177
List<String> outputRow = new ArrayList<String>(Arrays.asList(inputRow));
172178

173179
// COLUMN_FICHIER
174180
outputRow.add(concat(fichiers));
175181
// COLUMN_NOMSUPLITT
176182
outputRow.add(concat(nomSupLitts));
183+
// COLUMN NOMREG
184+
outputRow.add(concat(nomRegs));
177185

178186
return outputRow;
179187
}

validator-plugin-cnig/src/main/java/fr/ign/validator/cnig/sup/DatabaseSUP.java

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public class DatabaseSUP {
4141
public static final Marker MARKER = MarkerManager.getMarker("DatabaseSUP");
4242

4343
/**
44-
* SERVITUDE / Servitude in CNIG standard ("idsup","nomsuplitt")
44+
* SERVITUDE / Servitude in CNIG standard ("idsup","nomsuplitt","nomreg")
4545
*/
4646
public static final String TABLE_SERVITUDE = "servitude";
4747

@@ -76,6 +76,7 @@ public class DatabaseSUP {
7676

7777
public static final String COLUMN_IDSUP = "idsup";
7878
public static final String COLUMN_NOMSUPLITT = "nomsuplitt";
79+
public static final String COLUMN_NOMREG = "nomreg";
7980

8081
public static final String COLUMN_IDACTE = "idacte";
8182
public static final String COLUMN_FICHIER = "fichier";
@@ -98,6 +99,7 @@ public class ActeServitude {
9899
public class Servitude {
99100
public String idsup;
100101
public String nomsuplitt;
102+
public String nomreg;
101103
}
102104

103105
/**
@@ -113,6 +115,11 @@ public class AssietteSup {
113115
*/
114116
private Database database;
115117

118+
/**
119+
* COLUMN nomreg exists in SERVITUDE
120+
*/
121+
private boolean nomRegExists;
122+
116123
/**
117124
* Create DatabaseSUP as an SQLite database.
118125
*
@@ -121,6 +128,8 @@ public class AssietteSup {
121128
*/
122129
public DatabaseSUP(Database database) throws SQLException {
123130
this.database = database;
131+
this.nomRegExists = false;
132+
this.fetchNomRegExistence();
124133
}
125134

126135
/**
@@ -316,7 +325,10 @@ public List<String> getFichiers(List<ActeServitude> actes) {
316325
* @return
317326
*/
318327
public List<Servitude> findServitudesByGenerateur(String idGen) {
319-
String sql = "SELECT DISTINCT s.idsup,s.nomsuplitt FROM generateur g "
328+
String nomRegString = this.nomRegExists ? ",s.nomreg" : "";
329+
String sql = "SELECT DISTINCT s.idsup,s.nomsuplitt"
330+
+ nomRegString
331+
+ " FROM generateur g "
320332
+ " LEFT JOIN servitude s ON s.idsup = g.idsup "
321333
+ " WHERE g.idgen = ?";
322334
try {
@@ -335,7 +347,10 @@ public List<Servitude> findServitudesByGenerateur(String idGen) {
335347
* @return
336348
*/
337349
public List<Servitude> findServitudesByAssiette(String idAss) {
338-
String sql = "SELECT DISTINCT s.idsup,s.nomsuplitt FROM assiette a "
350+
String nomRegString = this.nomRegExists ? ",s.nomreg" : "";
351+
String sql = "SELECT DISTINCT s.idsup,s.nomsuplitt "
352+
+ nomRegString
353+
+ " FROM assiette a "
339354
+ " LEFT JOIN generateur g ON a.idgen = g.idgen "
340355
+ " LEFT JOIN servitude s ON s.idsup = g.idsup "
341356
+ " WHERE a.idass = ?";
@@ -361,6 +376,9 @@ private List<Servitude> fetchServitudes(ResultSet rs) throws SQLException {
361376
Servitude servitude = new Servitude();
362377
servitude.idsup = rs.getString(COLUMN_IDSUP);
363378
servitude.nomsuplitt = rs.getString(COLUMN_NOMSUPLITT);
379+
if (this.nomRegExists) {
380+
servitude.nomreg = rs.getString(COLUMN_NOMREG);
381+
}
364382
result.add(servitude);
365383
}
366384
return result;
@@ -383,6 +401,23 @@ public List<String> getNomSupLitts(List<Servitude> servitudes) {
383401
return new ArrayList<>(result);
384402
}
385403

404+
/**
405+
* Helper to extract "nomReg" values
406+
*
407+
* @param actes
408+
* @return
409+
*/
410+
public List<String> getNomRegs(List<Servitude> servitudes) {
411+
HashSet<String> result = new HashSet<>(servitudes.size());
412+
for (Servitude servitude : servitudes) {
413+
if (StringUtils.isEmpty(servitude.nomreg)) {
414+
continue;
415+
}
416+
result.add(servitude.nomreg);
417+
}
418+
return new ArrayList<>(result);
419+
}
420+
386421
/**
387422
* Validation - Find non unique IDGEN values.
388423
*
@@ -445,4 +480,21 @@ private List<AssietteSup> fetchAssietteSup(ResultSet rs) throws SQLException {
445480
return result;
446481
}
447482

483+
/**
484+
* Fetch existence of nomReg in SERVITUDE table
485+
*/
486+
private void fetchNomRegExistence() {
487+
String sql = "SELECT COUNT(*) AS count "
488+
+ " FROM pragma_table_info('servitude') "
489+
+ " WHERE name='NOMREG' ";
490+
try {
491+
PreparedStatement sth = getConnection().prepareStatement(sql);
492+
ResultSet rs = sth.executeQuery();
493+
while (rs.next()) {
494+
this.nomRegExists = rs.getInt("count") > 0;
495+
}
496+
} catch (SQLException e) {
497+
throw new RuntimeException(e);
498+
}
499+
}
448500
}

0 commit comments

Comments
 (0)