Add new api method
This commit is contained in:
parent
5ccc097048
commit
fa78bf4855
5 changed files with 100 additions and 66 deletions
|
|
@ -1,8 +1,7 @@
|
|||
package org.micord.controller;
|
||||
|
||||
import org.micord.enums.ConfigType;
|
||||
import org.micord.exceptions.ValidationException;
|
||||
import org.micord.models.requests.DownloadCSVRequest;
|
||||
import org.micord.models.requests.RequestParameters;
|
||||
import org.micord.service.ApiService;
|
||||
import org.micord.service.ValidationService;
|
||||
import org.slf4j.Logger;
|
||||
|
|
@ -11,7 +10,6 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|||
import org.springframework.core.io.InputStreamResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
|
@ -22,7 +20,7 @@ import java.io.FileNotFoundException;
|
|||
import java.io.IOException;
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* REST Controller for API operations.
|
||||
|
|
@ -40,23 +38,40 @@ public class ApiController {
|
|||
private ValidationService validationService;
|
||||
|
||||
@PostMapping("/block")
|
||||
public ResponseEntity<?> block(@RequestBody List<String> ids) throws SQLException, FileNotFoundException {
|
||||
public ResponseEntity<?> block(@RequestBody RequestParameters request) throws SQLException, FileNotFoundException {
|
||||
|
||||
List<String> ids = request.getIds();
|
||||
validationService.validateAll(ids);
|
||||
|
||||
logger.debug("Starting block process for ids: {}", ids);
|
||||
apiService.process(ConfigType.BLOCK, ids);
|
||||
apiService.process(ConfigType.BLOCK, request);
|
||||
logger.debug("Finished block process for ids: {}", ids);
|
||||
|
||||
return ResponseEntity.ok("Операция \"Блокировка\" завершена успешно.");
|
||||
}
|
||||
|
||||
@PostMapping("/removeMilitaryDraftNotices")
|
||||
public ResponseEntity<?> removeMilitaryDraftNotices(@RequestBody RequestParameters request) throws SQLException, FileNotFoundException {
|
||||
|
||||
List<String> ids = request.getIds();
|
||||
validationService.validateAll(ids);
|
||||
|
||||
logger.debug("Starting removeMilitaryDraftNotices process for ids: {}", ids);
|
||||
apiService.process(ConfigType.REMOVE_MILITARY_DRAFT_NOTICES, request);
|
||||
logger.debug("Finished removeMilitaryDraftNotices process for ids: {}", ids);
|
||||
|
||||
return ResponseEntity.ok("Операция \"Удаление повесток\" завершена успешно.");
|
||||
}
|
||||
|
||||
|
||||
@PostMapping("/unblock")
|
||||
public ResponseEntity<?> unblock(@RequestBody List<String> ids) throws SQLException, FileNotFoundException {
|
||||
public ResponseEntity<?> unblock(@RequestBody RequestParameters request) throws SQLException, FileNotFoundException {
|
||||
|
||||
List<String> ids = request.getIds();
|
||||
validationService.validateAll(ids);
|
||||
|
||||
logger.debug("Starting unblock process for ids: {}", ids);
|
||||
apiService.process(ConfigType.UNBLOCK, ids);
|
||||
apiService.process(ConfigType.UNBLOCK, request);
|
||||
logger.debug("Finished unblock process for ids: {}", ids);
|
||||
|
||||
return ResponseEntity.ok("Операция \"Разблокировка\" завершена успешно.");
|
||||
|
|
@ -64,11 +79,12 @@ public class ApiController {
|
|||
|
||||
|
||||
@PostMapping("/removeFromSystem")
|
||||
public ResponseEntity<?> removeFromSystem(@RequestBody List<String> ids) throws SQLException, FileNotFoundException {
|
||||
public ResponseEntity<?> removeFromSystem(@RequestBody RequestParameters request) throws SQLException, FileNotFoundException {
|
||||
List<String> ids = request.getIds();
|
||||
validationService.validateAll(ids);
|
||||
|
||||
logger.debug("Starting removeFromSystem process for ids: {}", ids);
|
||||
apiService.process(ConfigType.REMOVE_FROM_SYSTEM, ids);
|
||||
apiService.process(ConfigType.REMOVE_FROM_SYSTEM, request);
|
||||
logger.debug("Finished removeFromSystem process for ids: {}", ids);
|
||||
|
||||
return ResponseEntity.ok("Операция \"Удаление данных по гражданину\" завершена успешно.");
|
||||
|
|
@ -76,11 +92,12 @@ public class ApiController {
|
|||
|
||||
|
||||
@PostMapping("/removeFromCallList")
|
||||
public ResponseEntity<?> removeFromCallList(@RequestBody List<String> ids) throws SQLException, FileNotFoundException {
|
||||
public ResponseEntity<?> removeFromCallList(@RequestBody RequestParameters request) throws SQLException, FileNotFoundException {
|
||||
List<String> ids = request.getIds();
|
||||
validationService.validateAll(ids);
|
||||
|
||||
logger.debug("Starting removeFromCallList process for ids: {}", ids);
|
||||
apiService.process(ConfigType.REMOVE_FROM_CALL_LIST, ids);
|
||||
apiService.process(ConfigType.REMOVE_FROM_CALL_LIST, request);
|
||||
logger.debug("Finished removeFromCallList process for ids: {}", ids);
|
||||
|
||||
return ResponseEntity.ok("Операция \"Удаление из списков на вызов\" завершена успешно.");
|
||||
|
|
@ -88,15 +105,15 @@ public class ApiController {
|
|||
|
||||
|
||||
@PostMapping("/downloadCSV")
|
||||
public ResponseEntity<Resource> downloadCSV(@RequestBody DownloadCSVRequest request) throws IOException {
|
||||
logger.debug("Starting downloadCSV process for request: {}", request.getType());
|
||||
public ResponseEntity<Resource> downloadCSV(@RequestBody RequestParameters request) throws IOException {
|
||||
logger.debug("Starting downloadCSV process for request: {}", request.getDownloadType());
|
||||
|
||||
validateRequestDates(request);
|
||||
|
||||
File csvFile = apiService.download(ConfigType.DOWNLOAD_CSV, request);
|
||||
InputStreamResource resource = new InputStreamResource(new FileInputStream(csvFile));
|
||||
|
||||
logger.debug("Finished downloadCSV process for request: {}. Sending to user...", request.getType());
|
||||
logger.debug("Finished downloadCSV process for request: {}. Sending to user...", request.getDownloadType());
|
||||
|
||||
return ResponseEntity.ok()
|
||||
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + csvFile.getName())
|
||||
|
|
@ -105,7 +122,7 @@ public class ApiController {
|
|||
.body(resource);
|
||||
}
|
||||
|
||||
private void validateRequestDates(DownloadCSVRequest request) {
|
||||
private void validateRequestDates(RequestParameters request) {
|
||||
if (request.getStartDate() != null && request.getEndDate() != null) {
|
||||
if (request.getStartDate().isAfter(request.getEndDate())) {
|
||||
throw new IllegalArgumentException("Start date must be before end date");
|
||||
|
|
|
|||
|
|
@ -10,7 +10,8 @@ public enum ConfigType {
|
|||
REMOVE_FROM_SYSTEM("removeFromSystem"),
|
||||
REMOVE_FROM_CALL_LIST("removeFromCallList"),
|
||||
DOWNLOAD_CSV("downloadCSV"),
|
||||
VALIDATE_BLOCK("validateBlock");
|
||||
VALIDATE_BLOCK("validateBlock"),
|
||||
REMOVE_MILITARY_DRAFT_NOTICES("removeMilitaryDraftNotices");
|
||||
|
||||
private final String type;
|
||||
|
||||
|
|
|
|||
|
|
@ -7,8 +7,8 @@ import java.time.LocalDate;
|
|||
import java.util.List;
|
||||
|
||||
@Data
|
||||
public class DownloadCSVRequest {
|
||||
private String type;
|
||||
public class RequestParameters {
|
||||
private String downloadType;
|
||||
private List<String> ids;
|
||||
private LocalDate startDate;
|
||||
private LocalDate endDate;
|
||||
|
|
@ -1,7 +1,7 @@
|
|||
package org.micord.service;
|
||||
|
||||
import org.micord.enums.ConfigType;
|
||||
import org.micord.models.requests.DownloadCSVRequest;
|
||||
import org.micord.models.requests.RequestParameters;
|
||||
import org.micord.models.requests.DownloadRequest;
|
||||
import org.micord.models.requests.Requests;
|
||||
import org.slf4j.Logger;
|
||||
|
|
@ -30,15 +30,15 @@ public class ApiService {
|
|||
@Autowired
|
||||
private ConfigService configService;
|
||||
|
||||
public void process(ConfigType methodName, List<String> ids) throws FileNotFoundException {
|
||||
public void process(ConfigType methodName, RequestParameters parameters) throws FileNotFoundException {
|
||||
Requests config = configService.getConfig(methodName, Requests.class);
|
||||
sqlAndAqlService.processSqlAndAqlRequests(config, ids);
|
||||
sqlAndAqlService.processSqlAndAqlRequests(config, parameters);
|
||||
}
|
||||
|
||||
public File download(ConfigType methodName, DownloadCSVRequest request) throws IOException {
|
||||
public File download(ConfigType methodName, RequestParameters request) throws IOException {
|
||||
Requests config = configService.getConfig(methodName, Requests.class);
|
||||
|
||||
String type = request.getType();
|
||||
String type = request.getDownloadType();
|
||||
List<String> ids = Optional.ofNullable(request.getIds())
|
||||
.filter(list -> !list.isEmpty())
|
||||
.orElse(null);
|
||||
|
|
|
|||
|
|
@ -31,6 +31,7 @@ import java.sql.Connection;
|
|||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.time.LocalDate;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.IntStream;
|
||||
|
|
@ -46,25 +47,26 @@ public class RequestService {
|
|||
@Autowired
|
||||
private HttpClient httpClient;
|
||||
|
||||
public void processS3Requests(List<S3Request> s3Requests, List<String> ids) {
|
||||
public void processS3Requests(List<S3Request> s3Requests, RequestParameters parameters) {
|
||||
logger.info("A. Starting processing of S3 requests");
|
||||
|
||||
for (S3Request request : s3Requests) {
|
||||
processS3Request(request, ids);
|
||||
processS3Request(request, parameters);
|
||||
}
|
||||
}
|
||||
|
||||
private void processS3Request(S3Request request, List<String> ids) {
|
||||
private void processS3Request(S3Request request, RequestParameters parameters) {
|
||||
logger.info("B. Starting processing of single S3 request");
|
||||
try {
|
||||
List<String> files = new ArrayList<>();
|
||||
List<String> ids = parameters.getIds();
|
||||
|
||||
if (request.getRequestArguments() != null && !request.getRequestArguments().isEmpty()) {
|
||||
for (RequestArgument argument : request.getRequestArguments()) {
|
||||
try (Connection connection = DatabaseConnection.getConnection(
|
||||
argument.getRequestArgumentConnectionParams())) {
|
||||
|
||||
Map<String, Object> query = buildSqlQueryForS3(argument.getRequestArgumentURL(), ids);
|
||||
Map<String, Object> query = buildSqlQueryForS3(argument.getRequestArgumentURL(), parameters);
|
||||
logger.info("C. Calling query {} for ids {}: ", query.get("requestURL"), ids);
|
||||
logger.debug("Starting fetching paths from database for S3 request");
|
||||
long startExecTime = System.currentTimeMillis();
|
||||
|
|
@ -159,27 +161,30 @@ public class RequestService {
|
|||
|
||||
|
||||
@Transactional
|
||||
public void processSqlAndAqlRequests(Requests config, List<String> ids) {
|
||||
logger.debug("Starting transactional processing of requests");
|
||||
if (config.getSqlRequests() != null) {
|
||||
for (SqlRequest request : config.getSqlRequests()) {
|
||||
processSqlRequests(request, ids);
|
||||
}
|
||||
public void processSqlAndAqlRequests(Requests config, RequestParameters parameters) {
|
||||
logger.info("Starting transactional processing of requests");
|
||||
|
||||
if (config.getS3Requests() != null && !config.getS3Requests().isEmpty()) {
|
||||
processS3Requests(config.getS3Requests(), parameters);
|
||||
}
|
||||
|
||||
if (config.getAqlRequests() != null) {
|
||||
for (AqlRequest request : config.getAqlRequests()) {
|
||||
processAqlRequests(request, ids);
|
||||
processAqlRequests(request, parameters);
|
||||
}
|
||||
}
|
||||
if (config.getS3Requests() != null && !config.getS3Requests().isEmpty()) {
|
||||
processS3Requests(config.getS3Requests(), ids);
|
||||
|
||||
if (config.getSqlRequests() != null) {
|
||||
for (SqlRequest request : config.getSqlRequests()) {
|
||||
processSqlRequests(request, parameters);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void processSqlRequests(SqlRequest request, List<String> ids) {
|
||||
private void processSqlRequests(SqlRequest request, RequestParameters parameters) {
|
||||
logger.debug("Starting transactional processing of SQL requests");
|
||||
Map<String, Object> query = buildSqlQuery(request, ids);
|
||||
Map<String, Object> query = buildSqlQuery(request, parameters);
|
||||
List<String> ids = parameters.getIds();
|
||||
logger.debug("Opening connection for SQL Request: {}", request.getRequestURL());
|
||||
long startExecTime = System.currentTimeMillis();
|
||||
try (Connection connection = DatabaseConnection.getConnection(
|
||||
|
|
@ -204,21 +209,13 @@ public class RequestService {
|
|||
}
|
||||
}
|
||||
|
||||
private Map<String, Object> buildSqlQueryForS3(String requestURL, List<String> ids) {
|
||||
logger.debug("Starting building SQL query for request: {}", requestURL);
|
||||
private Map<String, Object> buildSqlQueryForS3(String url, RequestParameters parameters) {
|
||||
logger.debug("Starting building SQL query for request: {}", url);
|
||||
long startExecTime = System.currentTimeMillis();
|
||||
Map<String, Object> resultMap = new HashMap<>();
|
||||
String endpointArguments;
|
||||
|
||||
if (requestURL.contains(":=")) {
|
||||
endpointArguments = "'{" + ids.stream()
|
||||
.map(String::trim)
|
||||
.collect(Collectors.joining(", ")) + "}'";
|
||||
} else {
|
||||
endpointArguments = "(" + ids.stream()
|
||||
.map(s -> "'" + s.trim() + "'")
|
||||
.collect(Collectors.joining(", ")) + ")";
|
||||
}
|
||||
String requestURL = prepareDatesFilterInRequestURL(url, parameters.getStartDate(), parameters.getEndDate());
|
||||
String endpointArguments = replaceEndpointArgumentsInQuery(parameters, requestURL);
|
||||
|
||||
resultMap.put("requestURL", requestURL
|
||||
.replace("${endpointArguments}", endpointArguments));
|
||||
|
|
@ -229,23 +226,26 @@ public class RequestService {
|
|||
return resultMap;
|
||||
}
|
||||
|
||||
private Map<String, Object> buildSqlQuery(BaseRequest request, List<String> ids) {
|
||||
private String prepareDatesFilterInRequestURL(String requestURL, LocalDate startDate, LocalDate endDate) {
|
||||
|
||||
if (startDate != null) {
|
||||
requestURL = requestURL.replace("${startDate}", startDate.toString());
|
||||
}
|
||||
|
||||
if (endDate != null) {
|
||||
requestURL = requestURL.replace("${endDate}", endDate.toString());
|
||||
}
|
||||
|
||||
return requestURL;
|
||||
}
|
||||
|
||||
private Map<String, Object> buildSqlQuery(BaseRequest request, RequestParameters parameters) {
|
||||
logger.debug("Starting building SQL query for request: {}", request.getRequestURL());
|
||||
long startExecTime = System.currentTimeMillis();
|
||||
Map<String, Object> resultMap = new HashMap<>();
|
||||
String endpointArguments;
|
||||
|
||||
String requestURL = request.getRequestURL();
|
||||
|
||||
if (requestURL.contains(":=")) {
|
||||
endpointArguments = "'{" + ids.stream()
|
||||
.map(String::trim)
|
||||
.collect(Collectors.joining(", ")) + "}'";
|
||||
} else {
|
||||
endpointArguments = "(" + ids.stream()
|
||||
.map(s -> "'" + s.trim() + "'")
|
||||
.collect(Collectors.joining(", ")) + ")";
|
||||
}
|
||||
String requestURL = prepareDatesFilterInRequestURL(request.getRequestURL(), parameters.getStartDate(), parameters.getEndDate());
|
||||
endpointArguments = replaceEndpointArgumentsInQuery(parameters, requestURL);
|
||||
|
||||
if (request.getRequestArguments() != null && !request.getRequestArguments().isEmpty()) {
|
||||
for (RequestArgument argument : request.getRequestArguments()) {
|
||||
|
|
@ -286,6 +286,22 @@ public class RequestService {
|
|||
return resultMap;
|
||||
}
|
||||
|
||||
private String replaceEndpointArgumentsInQuery(RequestParameters parameters, String requestURL) {
|
||||
String endpointArguments;
|
||||
List<String> ids = parameters.getIds();
|
||||
|
||||
if (requestURL.contains(":=")) {
|
||||
endpointArguments = "'{" + ids.stream()
|
||||
.map(String::trim)
|
||||
.collect(Collectors.joining(", ")) + "}'";
|
||||
} else {
|
||||
endpointArguments = "(" + ids.stream()
|
||||
.map(s -> "'" + s.trim() + "'")
|
||||
.collect(Collectors.joining(", ")) + ")";
|
||||
}
|
||||
return endpointArguments;
|
||||
}
|
||||
|
||||
|
||||
private boolean executeSqlQuery(Connection connection, String query) throws SQLException {
|
||||
try (PreparedStatement stmt = connection.prepareStatement(query)) {
|
||||
|
|
@ -305,7 +321,7 @@ public class RequestService {
|
|||
return results;
|
||||
}
|
||||
|
||||
private void processAqlRequests(AqlRequest request, List<String> ids) {
|
||||
private void processAqlRequests(AqlRequest request, RequestParameters parameters) {
|
||||
ArangoDatabase arangoDb = ArangoDBConnection.getConnection(request.getAqlConnectionParams());
|
||||
|
||||
// TODO: implement for multiple request arguments
|
||||
|
|
@ -327,7 +343,7 @@ public class RequestService {
|
|||
|
||||
logger.info("Stream transaction started with ID: {}", transactionId);
|
||||
|
||||
Map<String, Object> entities = executeSelectAqlRequest(arangoDb, requestArgument, ids, transactionId);
|
||||
Map<String, Object> entities = executeSelectAqlRequest(arangoDb, requestArgument, parameters.getIds(), transactionId);
|
||||
|
||||
if (entities.isEmpty()) {
|
||||
logger.warn("No entities found for main AQL request.");
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue