SUPPORT-8974 send empty id list

This commit is contained in:
ivanov.denis 2025-03-06 12:03:02 +03:00
parent f78f3ea446
commit 87927541cc
5 changed files with 114 additions and 60 deletions

View file

@ -51,17 +51,44 @@ public class ConfigExecutorService {
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<ExportDataRequest> entity = new HttpEntity<>(request, headers);
ResponseEntity<byte[]> response = restTemplate.exchange(url.concat("/").concat("downloadCSV"),
HttpMethod.POST, entity, byte[].class
);
try {
String content = Base64.getEncoder().encodeToString(response.getBody());
FileModel fileModel = new FileModel();
fileModel.setFileContent(content);
fileModel.setFileExtension(".csv");
fileModel.setFileName(
request.getType() + "_" + new SimpleDateFormat("dd.MM.yyyy").format(new Date()) + ".csv");
return fileModel;
ResponseEntity<byte[]> response = restTemplate.exchange(url.concat("/").concat("downloadCSV"),
HttpMethod.POST, entity, byte[].class
);
String content = Base64.getEncoder().encodeToString(response.getBody());
FileModel fileModel = new FileModel();
fileModel.setFileContent(content);
fileModel.setFileExtension(".csv");
fileModel.setFileName(
request.getType() + "_" + new SimpleDateFormat("dd.MM.yyyy").format(new Date()) + ".csv");
return fileModel;
} catch (HttpClientErrorException e) {
if (e.getStatusCode() == HttpStatus.BAD_REQUEST) {
Map<String, Object> responseMap = new Gson().fromJson(e.getResponseBodyAsString(),
new TypeToken<Map<String, Object>>() {
}.getType()
);
if (responseMap.get("details") instanceof Map) {
@SuppressWarnings("unchecked")
Map<String, String> details = (Map<String, String>) responseMap.get("details");
String detailsStr = String.join("\n", details.values());
throw new ConfigExecutorException("eks.error.misc", new RuntimeException(detailsStr, e));
}
throw new ConfigExecutorException((String) responseMap.get("details"), e);
}
else {
throw new RuntimeException("Export data failed with error", e);
}
}
catch (Exception e) {
throw new RuntimeException("Export data failed with error", e);
}
}
public String call(String methodPath, ConfigExecuteRequest configExecuteRequest,
@ -95,6 +122,13 @@ public class ConfigExecutorService {
}.getType()
);
if (responseMap.get("details") instanceof Map) {
@SuppressWarnings("unchecked")
Map<String, String> details = (Map<String, String>) responseMap.get("details");
String detailsStr = String.join("\n", details.values());
throw new ConfigExecutorException("eks.error.misc", new RuntimeException(detailsStr, e));
}
throw new ConfigExecutorException((String) responseMap.get("details"), e);
}
else {

View file

@ -21,4 +21,5 @@ public class RequestValidationRules {
@XmlAttribute(name = "isIdsFormatted")
private Boolean isIdsFormatted = true;
}

View file

@ -24,7 +24,6 @@ import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@Service
public class ValidationService {
@ -78,64 +77,74 @@ public class ValidationService {
public Map<String, Boolean> validateRequest(BaseRequest request, List<String> ids) throws ValidationException, SQLException, FileNotFoundException {
RequestValidationRules requestValidationRules = new RequestValidationRules();
Map<String, Boolean> validationResults = validateIds(request, ids, requestValidationRules);
if (isRequestValidatedByValidationConfig) validateByValidationConfig(ids, BaseRequest.class);
Map<String, Boolean> emptyIdsRules = getRulesForEmptyIds(request, ids, requestValidationRules);
return validationResults;
if (isRequestValidatedByValidationConfig) {
validateByValidationConfig(ids, BaseRequest.class);
}
return emptyIdsRules;
}
public Map<String, Boolean> validateMilitaryNoticeRequest(BaseRequest request, RequestParameters parameters) throws ValidationException, SQLException, FileNotFoundException {
RequestValidationRules requestValidationRules = new RequestValidationRules();
List<String> ids = parameters.getIds();
Map<String, Boolean> validateIds = validateIds(request, ids, requestValidationRules);
Map<String, Boolean> validateDates = validateDates(request, parameters);
Map<String, Boolean> emptyIdsRules = getRulesForEmptyIds(request, ids, requestValidationRules);
Map<String, Boolean> emptyDatesRules = getRulesForEmptyDates(request, parameters);
if (isMilitaryNoticeRequestValidatedByValidationConfig) validateByValidationConfig(ids, RequestParameters.class);
if (isMilitaryNoticeRequestValidatedByValidationConfig) {
validateByValidationConfig(ids, RequestParameters.class);
}
Map<String, Boolean> merged = new HashMap<>();
merged.putAll(validateIds);
merged.putAll(validateDates);
merged.putAll(emptyIdsRules);
merged.putAll(emptyDatesRules);
return merged;
}
public Map<String, Boolean> validateDownloadRequest(BaseDownloadRequest request, RequestParameters downloadRequest, List<String> ids) throws ValidationException {
RequestValidationRules requestValidationRules = new RequestValidationRules();
Map<String, Boolean> validateIds = validateIds(request, ids, requestValidationRules);
Map<String, Boolean> validateDates = validateDates(request, downloadRequest);
Map<String, Boolean> emptyIdsRules = getRulesForEmptyIds(request, ids, requestValidationRules);
Map<String, Boolean> emptyDatesRules = getRulesForEmptyDates(request, downloadRequest);
Map<String, Boolean> merged = new HashMap<>();
merged.putAll(validateIds);
merged.putAll(validateDates);
merged.putAll(emptyIdsRules);
merged.putAll(emptyDatesRules);
return merged;
}
private static Map<String, Boolean> validateIds(BaseRequest request, List<String> ids, RequestValidationRules requestValidationRules) {
private static Map<String, Boolean> getRulesForEmptyIds(BaseRequest request, List<String> ids, RequestValidationRules defaultRules) {
if (request.getRequestValidationRules() == null) {
return Map.of(ValidationService.IS_EMPTY_IDS_ALLOWED, requestValidationRules.getIsEmptyIdsAllowed());
if (request.getRequestValidationRules() == null && defaultRules.getIsEmptyIdsAllowed()) {
return Map.of(ValidationService.IS_EMPTY_IDS_ALLOWED, true);
}
Boolean emptyIdsAllowed = request.getRequestValidationRules().getIsEmptyIdsAllowed();
if (emptyIdsAllowed) {
return Map.of(ValidationService.IS_EMPTY_IDS_ALLOWED, emptyIdsAllowed);
if (request.getRequestValidationRules() != null && request.getRequestValidationRules().getIsEmptyIdsAllowed()) {
return Map.of(ValidationService.IS_EMPTY_IDS_ALLOWED, true);
}
if (ids == null || ids.isEmpty()) {
throw new IllegalRequestParametersException("eks.error.argument.missing");
}
Boolean isIdsFormatted = request.getRequestValidationRules().getIsIdsFormatted();
if (isIdsFormatted) {
String uuidRegex = "^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$";
boolean isIdsFormatted = (request.getRequestValidationRules() != null && request.getRequestValidationRules().getIsIdsFormatted())
|| (request.getRequestValidationRules() == null && defaultRules.getIsIdsFormatted());
boolean invalidIdFound = ids.stream().anyMatch(id -> !id.matches(uuidRegex));
if (invalidIdFound) {
throw new IllegalRequestParametersException("eks.error.argument.invalid");
}
if (!isIdsFormatted) {
return Map.of(ValidationService.IS_EMPTY_IDS_ALLOWED, false);
}
return Map.of(ValidationService.IS_EMPTY_IDS_ALLOWED, emptyIdsAllowed);
String uuidRegex = "^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$";
boolean invalidIdFound = ids.stream().anyMatch(id -> !id.matches(uuidRegex));
if (!invalidIdFound) {
return Map.of(ValidationService.IS_EMPTY_IDS_ALLOWED, false);
}
throw new IllegalRequestParametersException("eks.error.argument.invalid");
}
private static <T extends RequestParameters> Boolean validateEmptyDates(BaseRequest request, T parameters) {
@ -151,7 +160,7 @@ public class ValidationService {
return emptyDatesAllowed;
}
private <R extends BaseRequest, T extends RequestParameters> Map<String, Boolean> validateDates(R request, T parameters) {
private <R extends BaseRequest, T extends RequestParameters> Map<String, Boolean> getRulesForEmptyDates(R request, T parameters) {
Boolean emptyDatesAllowed = validateEmptyDates(request, parameters);
if (!emptyDatesAllowed && parameters.getStartDate() != null && parameters.getEndDate() != null) {
if (parameters.getStartDate().isAfter(parameters.getEndDate())) {

View file

@ -30,7 +30,8 @@ export class ConfigExecuteBtn extends AbstractButton {
public endDate: DateTimePicker;
@NotNull()
public methodPath: string;
private script: ConfigExecutorRpcService;
private rpcService: ConfigExecutorRpcService;
private messagesService: MessagesService;
constructor(el: ElementRef, cd: ChangeDetectorRef) {
@ -39,33 +40,44 @@ export class ConfigExecuteBtn extends AbstractButton {
initialize() {
super.initialize();
this.script = this.getScript(ConfigExecutorRpcService);
this.rpcService = this.getScript(ConfigExecutorRpcService);
this.messagesService = this.injector.get(MessagesService);
}
doClickActions(): Promise<any> {
const value: string = this.ervuIdField.getValue();
if (value && this.methodPath.trim().length !== 0) {
const ids: string[] = value.replace(/[{}]/g, '')
.split(',')
.map(id => id.trim().replace(/"/g, ''));
let configExecuteRequest: ConfigExecuteRequest = new ConfigExecuteRequest();
let withDate = false;
configExecuteRequest.ids = ids;
if (this.startDate || this.endDate) {
withDate = true;
configExecuteRequest.startDate = this.startDate ? this.startDate.getDateValue() : null;
configExecuteRequest.endDate = this.endDate ? this.endDate.getDateValue() : null;
}
return this.script.callConfigExecutor(this.methodPath, configExecuteRequest, withDate, true)
.then(successMsg => this.messagesService.success(successMsg))
.catch(error => Promise.reject(error));
if (this.methodPath.trim().length == 0) {
return;
}
const ids: string[] = this.ervuIdField.getValue()
? ConfigExecuteBtn.parseIds(this.ervuIdField.getValue())
: new Array<string>();
let configExecuteRequest: ConfigExecuteRequest = new ConfigExecuteRequest();
let withDate = false;
configExecuteRequest.ids = ids;
if (this.startDate || this.endDate) {
withDate = true;
configExecuteRequest.startDate = this.startDate ? this.startDate.getDateValue() : null;
configExecuteRequest.endDate = this.endDate ? this.endDate.getDateValue() : null;
}
return this.rpcService.callConfigExecutor(this.methodPath, configExecuteRequest, withDate, true)
.then(successMsg => this.messagesService.success(successMsg))
.catch(error => Promise.reject(error));
}
getFocusElement(): HTMLInputElement {
return this.el.nativeElement.querySelector('button');
}
private static parseIds(value: string): string[] {
return value.replace(/[{}]/g, '')
.split(',')
.map(id => id.trim().replace(/"/g, ''));
}
}

View file

@ -1549,7 +1549,6 @@
<componentRootId>62666192-d5b8-4c04-a487-10285ceb39d1</componentRootId>
<name>Блокировка граждан</name>
<container>true</container>
<expanded>false</expanded>
<childrenReordered>false</childrenReordered>
<scripts id="d1ce20ca-453b-4610-a2a5-bb6498db5cf5">
<properties>
@ -1919,7 +1918,6 @@
<componentRootId>9af05980-e46b-4ed0-9758-dd80e48adfb5</componentRootId>
<name>Hbox</name>
<container>true</container>
<expanded>false</expanded>
<childrenReordered>false</childrenReordered>
<scripts id="bf098f19-480e-44e4-9084-aa42955c4d0f"/>
<scripts id="b6068710-0f31-48ec-8e03-c0c1480a40c0">