Commit 76aa91e6 authored by Bergamote Orange's avatar Bergamote Orange
Browse files

extracted SQL strings & optimize imports

parent 8b552216
......@@ -5,11 +5,6 @@ import lombok.experimental.UtilityClass;
@UtilityClass
public class BatchConstants {
public static final String EXPOSED_VISITS_TABLE = "exposed_visits";
public static final String SINGLE_PLACE_CLUSTER_PERIOD_TABLE = "cluster_periods";
public static final String PERIOD_COLUMN = "period_start";
public static final String TIMESLOT_COLUMN = "timeslot";
public static final String LTID_COL = "ltid";
public static final String VENUE_TYPE_COL = "venue_type";
public static final String VENUE_CAT1_COL = "venue_category1";
......@@ -25,6 +20,18 @@ public class BatchConstants {
public static final String PREFIXES_PARTITION_KEY = "prefixes";
public static final String LTIDS_LIST_PARTITION_KEY = "ltids";
// SQL properties
public static final String EXPOSED_VISITS_TABLE = "exposed_visits";
public static final String SINGLE_PLACE_CLUSTER_PERIOD_TABLE = "cluster_periods";
public static final String PERIOD_COLUMN = "period_start";
public static final String TIMESLOT_COLUMN = "timeslot";
// JDBC SQL Queries
public static final String SQL_SELECT_BY_LTID_IN_SINGLEPLACECLUSTERPERIOD = "select * from " + SINGLE_PLACE_CLUSTER_PERIOD_TABLE + " WHERE ltid= ?";
public static final String SQL_SELECT_DISTINCT_LTID_FROM_EXPOSEDVISITS = "select distinct " + LTID_COL + " from " + EXPOSED_VISITS_TABLE + " order by " + LTID_COL;
public static final String SQL_SELECT_DISTINCT_FROM_CLUSTERPERIODS_ORDERBY_LTID = "select distinct " + LTID_COL + " from " + SINGLE_PLACE_CLUSTER_PERIOD_TABLE + " ORDER BY " + LTID_COL;
public static final String SQL_SELECT_FROM_EXPOSEDVISITS_WHERE_LTID_ORDERBY_PERIOD_AND_TIMESLOT = "select * from " + EXPOSED_VISITS_TABLE
+ " WHERE ltid= ? ORDER BY " + PERIOD_COLUMN + ", " + TIMESLOT_COLUMN;
public static final String SQL_TRUNCATE_TABLE_CLUSTERPERIODS = "truncate table " + SINGLE_PLACE_CLUSTER_PERIOD_TABLE + ";";
}
......@@ -27,8 +27,7 @@ import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import javax.sql.DataSource;
import java.util.List;
import static fr.gouv.clea.config.BatchConstants.EXPOSED_VISITS_TABLE;
import static fr.gouv.clea.config.BatchConstants.LTID_COL;
import static fr.gouv.clea.config.BatchConstants.SQL_SELECT_DISTINCT_LTID_FROM_EXPOSEDVISITS;
@Configuration
public class IdentificationStepBatchConfig {
......@@ -74,7 +73,7 @@ public class IdentificationStepBatchConfig {
JdbcCursorItemReader<String> reader = new JdbcCursorItemReader<>();
reader.setDataSource(dataSource);
reader.setSql("select distinct " + LTID_COL + " from " + EXPOSED_VISITS_TABLE + " order by " + LTID_COL);
reader.setSql(SQL_SELECT_DISTINCT_LTID_FROM_EXPOSEDVISITS);
reader.setRowMapper((rs, i) -> rs.getString(1));
return reader;
}
......
......@@ -7,13 +7,11 @@ import org.springframework.batch.core.repository.support.JobRepositoryFactoryBea
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.io.IOException;
import javax.sql.DataSource;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Map;
import javax.sql.DataSource;
@Component
public class NoPersistenceBatchConfigurer extends DefaultBatchConfigurer {
......
......@@ -14,8 +14,7 @@ import org.springframework.context.annotation.Configuration;
import javax.sql.DataSource;
import java.util.List;
import static fr.gouv.clea.config.BatchConstants.LTID_COL;
import static fr.gouv.clea.config.BatchConstants.SINGLE_PLACE_CLUSTER_PERIOD_TABLE;
import static fr.gouv.clea.config.BatchConstants.SQL_SELECT_DISTINCT_FROM_CLUSTERPERIODS_ORDERBY_LTID;
@Configuration
public class PrefixesStepBatchConfig {
......@@ -52,7 +51,7 @@ public class PrefixesStepBatchConfig {
reader.setSaveState(false);
reader.setDataSource(dataSource);
reader.setVerifyCursorPosition(false);
reader.setSql("select distinct " + LTID_COL + " from " + SINGLE_PLACE_CLUSTER_PERIOD_TABLE + " ORDER BY " + LTID_COL);
reader.setSql(SQL_SELECT_DISTINCT_FROM_CLUSTERPERIODS_ORDERBY_LTID);
reader.setRowMapper((rs, i) -> rs.getString(1));
return new ListItemReader(reader);
}
......
......@@ -7,12 +7,9 @@ import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcOperations;
import org.springframework.jdbc.core.JdbcTemplate;
import javax.sql.DataSource;
import static fr.gouv.clea.config.BatchConstants.SINGLE_PLACE_CLUSTER_PERIOD_TABLE;
import static fr.gouv.clea.config.BatchConstants.SQL_TRUNCATE_TABLE_CLUSTERPERIODS;
@Configuration
public class PurgeIntermediateTableStepBatchConfig {
......@@ -30,7 +27,7 @@ public class PurgeIntermediateTableStepBatchConfig {
@Bean
public Tasklet clearTable(final JdbcTemplate jdbcTemplate) {
return (contribution, chunkContext) -> {
jdbcTemplate.execute("truncate table " + SINGLE_PLACE_CLUSTER_PERIOD_TABLE + ";");
jdbcTemplate.execute(SQL_TRUNCATE_TABLE_CLUSTERPERIODS);
return RepeatStatus.FINISHED;
};
}
......
......@@ -11,12 +11,11 @@ import org.springframework.batch.item.ExecutionContext;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.jdbc.core.JdbcTemplate;
import javax.sql.DataSource;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicLong;
import static fr.gouv.clea.config.BatchConstants.*;
import static fr.gouv.clea.config.BatchConstants.SQL_SELECT_FROM_EXPOSEDVISITS_WHERE_LTID_ORDERBY_PERIOD_AND_TIMESLOT;
/**
* This class is executing in many Threads
......@@ -38,8 +37,7 @@ public class SinglePlaceExposedVisitsBuilder implements ItemProcessor<String, Si
@Override
public SinglePlaceExposedVisits process(final String ltid) {
final List<ExposedVisit> list = jdbcTemplate.query("select * from " + EXPOSED_VISITS_TABLE
+ " WHERE ltid= ? ORDER BY " + PERIOD_COLUMN + ", " + TIMESLOT_COLUMN,
final List<ExposedVisit> list = jdbcTemplate.query(SQL_SELECT_FROM_EXPOSEDVISITS_WHERE_LTID_ORDERBY_PERIOD_AND_TIMESLOT,
rowMapper, UUID.fromString(ltid));
ExposedVisit firstExposedVisit = list.stream().findFirst().orElse(null);
if (null != firstExposedVisit) {
......
......@@ -5,11 +5,9 @@ import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.batch.item.ItemWriter;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSourceUtils;
import javax.sql.DataSource;
import java.util.List;
import java.util.stream.Collectors;
......
package fr.gouv.clea.indexation.processor;
import fr.gouv.clea.config.BatchProperties;
import fr.gouv.clea.dto.ClusterPeriod;
import fr.gouv.clea.dto.SinglePlaceCluster;
import fr.gouv.clea.dto.SinglePlaceClusterPeriod;
import fr.gouv.clea.indexation.SinglePlaceClusterPeriodRowMapper;
import fr.gouv.clea.indexation.model.output.ClusterFile;
import fr.gouv.clea.indexation.model.output.ClusterFileItem;
import fr.gouv.clea.indexation.model.output.Prefix;
import fr.gouv.clea.mapper.ClusterPeriodModelsMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.jdbc.core.JdbcTemplate;
import javax.sql.DataSource;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import static fr.gouv.clea.config.BatchConstants.SINGLE_PLACE_CLUSTER_PERIOD_TABLE;
import static fr.gouv.clea.config.BatchConstants.SQL_SELECT_BY_LTID_IN_SINGLEPLACECLUSTERPERIOD;
@Slf4j
......
......@@ -14,7 +14,6 @@ import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import static fr.gouv.clea.config.BatchConstants.JSON_FILE_EXTENSION;
......
Supports Markdown
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