Commit aca0afa8 authored by LAVENIER's avatar LAVENIER
Browse files

Merge branch 'release/1.8.4'

parents 1dfaada7 15fb35ab
......@@ -3,7 +3,7 @@
<groupId>net.sumaris</groupId>
<artifactId>sumaris-pod</artifactId>
<version>1.8.3</version>
<version>1.8.4</version>
<packaging>pom</packaging>
<name>SUMARiS</name>
<description>SUMARiS :: Maven parent</description>
......
......@@ -5,7 +5,7 @@
<parent>
<groupId>net.sumaris</groupId>
<artifactId>sumaris-pod</artifactId>
<version>1.8.3</version>
<version>1.8.4</version>
</parent>
<artifactId>sumaris-core-extraction</artifactId>
......
......@@ -24,6 +24,7 @@ package net.sumaris.core.extraction.dao.technical;
import lombok.extern.slf4j.Slf4j;
import net.sumaris.core.util.Dates;
import org.apache.commons.collections4.MapUtils;
import org.springframework.jdbc.datasource.DataSourceUtils;
import javax.sql.DataSource;
......@@ -31,6 +32,7 @@ import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;
import java.util.Date;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;
......@@ -46,6 +48,8 @@ public class Daos extends net.sumaris.core.dao.technical.Daos {
private final static String SQL_TO_DATE = "TO_DATE('%s', '%s')";
private final static String NON_COLUMN_CHAR_REGEXP = "[^A-Z0-9_]";
protected Daos() {
// Helper class
}
......@@ -135,4 +139,28 @@ public class Daos extends net.sumaris.core.dao.technical.Daos {
DataSourceUtils.releaseConnection(conn, dataSource);
}
}
/**
* Do column names replacement, but escape sql keyword (e.g. 'DATE' replacement will keep TO_DATE(...) unchanged)
* @param sqlQuery
* @param columnNamesMapping
* @return
*/
public static String sqlReplaceColumnNames(String sqlQuery, Map<String, String> columnNamesMapping) {
if (MapUtils.isEmpty(columnNamesMapping)) return sqlQuery; // Skip
sqlQuery = sqlQuery.toUpperCase();
for (Map.Entry<String, String> entry: columnNamesMapping.entrySet()) {
String sourceColumnName = entry.getKey().toUpperCase();
String targetColumnName = entry.getValue().toUpperCase();
sqlQuery = sqlQuery
.replaceAll("(^|" + NON_COLUMN_CHAR_REGEXP + ")"
+ sourceColumnName
+ "("+NON_COLUMN_CHAR_REGEXP+"|$)",
"$1" + targetColumnName + "$2");
}
return sqlQuery;
}
}
......@@ -74,8 +74,10 @@ public class AggregationFree1DaoImpl<
super.fillContextTableNames(context);
// Rename some columns
context.addColumnNameReplacement(AggRdbSpecification.COLUMN_FISHING_TIME, AggFree1Specification.COLUMN_FISHING_DURATION);
context.addColumnNameReplacement(RdbSpecification.COLUMN_INDIVIDUAL_SEX, Free1Specification.COLUMN_SEX);
context.addColumnNameReplacement(AggRdbSpecification.COLUMN_FISHING_TIME, Free1Specification.COLUMN_FISHING_DURATION)
.addColumnNameReplacement(RdbSpecification.COLUMN_DATE, Free1Specification.COLUMN_FISHING_DATE)
.addColumnNameReplacement(RdbSpecification.COLUMN_TIME, Free1Specification.COLUMN_FISHING_TIME)
.addColumnNameReplacement(RdbSpecification.COLUMN_INDIVIDUAL_SEX, Free1Specification.COLUMN_SEX);
}
@Override
......
......@@ -779,22 +779,12 @@ public class AggregationRdbTripDaoImpl<
}
protected int execute(C context, XMLQuery xmlQuery) {
String sqlQuery = doSqlReplacement(context, xmlQuery.getSQLQueryAsString());
return queryUpdate(sqlQuery);
}
protected String doSqlReplacement(C context, String sqlQuery) {
sqlQuery = sqlQuery.toUpperCase();
String sqlQuery = xmlQuery.getSQLQueryAsString();
// Do column nanes replacement
// E.g. Rename FISHING_TIME into FISHING_DURATION
if (context != null && MapUtils.isNotEmpty(context.getColumnNamesMapping())) {
for (Map.Entry<String, String> entry : context.getColumnNamesMapping().entrySet()) {
sqlQuery = sqlQuery.replaceAll(entry.getKey().toUpperCase(), entry.getValue().toUpperCase());
}
}
// Do column nanes replacement (e.g. see FREE extraction)
sqlQuery = Daos.sqlReplaceColumnNames(sqlQuery, context.getColumnNamesMapping());
return sqlQuery;
return queryUpdate(sqlQuery);
}
protected long countFrom(String tableName) {
......@@ -869,7 +859,9 @@ public class AggregationRdbTripDaoImpl<
.collect(Collectors.toMap(
c -> c,
c -> query(
doSqlReplacement(context, String.format("SELECT DISTINCT %s FROM %s where %s IS NOT NULL", c, tableName, c)),
Daos.sqlReplaceColumnNames(
String.format("SELECT DISTINCT %s FROM %s where %s IS NOT NULL", c, tableName, c),
context.getColumnNamesMapping()),
Object.class)
.stream()
.map(String::valueOf)
......
......@@ -548,8 +548,8 @@ public class ExtractionRdbTripDaoImpl<C extends ExtractionRdbTripContextVO, F ex
.map(pmfmStrategy -> toPmfmColumnVO(pmfmStrategy, null))
// Group by pmfmId
.collect(Collectors.toMap(ExtractionPmfmColumnVO::getPmfmId, ps -> ps))
.values().stream()
.collect(Collectors.groupingBy(ExtractionPmfmColumnVO::getPmfmId))
.values().stream().map(list -> list.get(0))
// Sort by label
.sorted(Comparator.comparing(ExtractionPmfmColumnVO::getLabel, String::compareTo))
......
......@@ -27,7 +27,5 @@ public interface AggFree1Specification extends AggRdbSpecification {
String FORMAT = AggSpecification.FORMAT_PREFIX + Free1Specification.FORMAT;
String VERSION_1 = Free1Specification.VERSION_1;
// Agg columns
String COLUMN_FISHING_DURATION = "fishing_duration";
}
......@@ -30,5 +30,7 @@ public interface Free1Specification extends RdbSpecification {
String VERSION_1 = "1";
String[] SHEET_NAMES = {RdbSpecification.TR_SHEET_NAME, RdbSpecification.HH_SHEET_NAME, RdbSpecification.SL_SHEET_NAME, RdbSpecification.HL_SHEET_NAME};
String COLUMN_FISHING_DATE = "fishing_date";
String COLUMN_FISHING_DURATION = "fishing_duration";
String COLUMN_SEX = CostSpecification.COLUMN_SEX;
}
\ No newline at end of file
......@@ -45,6 +45,9 @@ public interface RdbSpecification {
String COLUMN_VESSEL_IDENTIFIER = "vessel_identifier";
String COLUMN_TRIP_CODE = "trip_code";
String COLUMN_DATE = "date";
String COLUMN_TIME = "time";
String COLUMN_FISHING_TIME = "fishing_time";
String COLUMN_INDIVIDUAL_SEX = "individual_sex";
String[] SHEET_NAMES = {
......
......@@ -49,9 +49,10 @@ public class AggregationRdbTripContextVO extends AggregationTripContextVO {
String landingTableName; // CL table
// Allow to rename column name (e.g FREE1 use FISHING_DURATION instead of FISHING_TIME)
Map<String, String> columnNamesMapping = Maps.newHashMap();
Map<String, String> columnNamesMapping = Maps.newLinkedHashMap();
public void addColumnNameReplacement(String sourceColumnName, String targetColumnName) {
public <C extends AggregationRdbTripContextVO> C addColumnNameReplacement(String sourceColumnName, String targetColumnName) {
columnNamesMapping.put(sourceColumnName, targetColumnName);
return (C)this;
}
}
......@@ -22,6 +22,8 @@ package net.sumaris.core.extraction.dao.technical;
* #L%
*/
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;
import net.sumaris.core.config.SumarisConfiguration;
import net.sumaris.core.dao.technical.DatabaseType;
import net.sumaris.core.extraction.DatabaseResource;
......@@ -109,6 +111,28 @@ public class DaosTest {
}
}
@Test
public void sqlReplaceColumnNames(){
String sql = "SELECT\n" +
" TO_DATE(S.DATE || ' ' || S.TIME, 'YYYY-MM-DD HH24:MI') AS FISHING_TIME\n" +
" SUM(S.FISHING_TIME) AS TRIP_COUNT_BY_FISHING_TIME\n" +
" S.FISHING_TIME_NOT_REPLACED\n" +
"FROM TRIP T\n" +
"WHERE\n" +
" 0 < S.FISHING_TIME";
String expectedSql = "SELECT\n" +
" TO_DATE(S.FISHING_DATE || ' ' || S.TIME, 'YYYY-MM-DD HH24:MI') AS FISHING_DURATION\n" +
" SUM(S.FISHING_DURATION) AS TRIP_COUNT_BY_FISHING_TIME\n" +
" S.FISHING_TIME_NOT_REPLACED\n" +
"FROM TRIP T\n" +
"WHERE\n" +
" 0 < S.FISHING_DURATION";
String actualSql = Daos.sqlReplaceColumnNames(sql, ImmutableMap.of("date", "fishing_date",
"fishing_time", "fishing_duration"));
Assert.assertEquals(expectedSql, actualSql);
}
/* -- protected method -- */
protected Integer getDaoHashCode(Connection conn, String expression) throws SQLException {
......
......@@ -152,12 +152,13 @@ public class AggregationServiceTest extends AbstractServiceTest {
AggregationStrataVO strata = new AggregationStrataVO();
strata.setSpatialColumnName(ProductRdbStation.COLUMN_STATISTICAL_RECTANGLE);
strata.setTimeColumnName(ProductRdbStation.COLUMN_YEAR);
strata.setTimeColumnName(AggRdbSpecification.COLUMN_QUARTER);
ExtractionFilterVO filter = ExtractionFilterVO.builder()
.sheetName(AggRjbTripSpecification.HH_SHEET_NAME)
.criteria(ImmutableList.of(
ExtractionFilterCriterionVO.builder()
.sheetName(AggRjbTripSpecification.HH_SHEET_NAME)
.name(ProductRdbStation.COLUMN_YEAR)
.operator(ExtractionFilterOperatorEnum.EQUALS.getSymbol())
.value(yearRawData)
......
......@@ -3,7 +3,7 @@
<parent>
<artifactId>sumaris-pod</artifactId>
<groupId>net.sumaris</groupId>
<version>1.8.3</version>
<version>1.8.4</version>
</parent>
<modelVersion>4.0.0</modelVersion>
......
......@@ -3,7 +3,7 @@
<parent>
<artifactId>sumaris-pod</artifactId>
<groupId>net.sumaris</groupId>
<version>1.8.3</version>
<version>1.8.4</version>
</parent>
<modelVersion>4.0.0</modelVersion>
......
......@@ -4,7 +4,7 @@
<parent>
<groupId>net.sumaris</groupId>
<artifactId>sumaris-pod</artifactId>
<version>1.8.3</version>
<version>1.8.4</version>
</parent>
<artifactId>sumaris-core-shared</artifactId>
......
......@@ -4,7 +4,7 @@
<parent>
<groupId>net.sumaris</groupId>
<artifactId>sumaris-pod</artifactId>
<version>1.8.3</version>
<version>1.8.4</version>
</parent>
<artifactId>sumaris-core</artifactId>
......
......@@ -4,7 +4,7 @@
<parent>
<groupId>net.sumaris</groupId>
<artifactId>sumaris-pod</artifactId>
<version>1.8.3</version>
<version>1.8.4</version>
</parent>
<artifactId>sumaris-server</artifactId>
......
......@@ -4,7 +4,7 @@
<parent>
<groupId>net.sumaris</groupId>
<artifactId>sumaris-pod</artifactId>
<version>1.8.3</version>
<version>1.8.4</version>
</parent>
<artifactId>sumaris-test-shared</artifactId>
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment