getAll endpoint returned and added new getByFilter endpoint

This commit is contained in:
Ivan Nikolaev-Axenov 2024-08-23 16:09:05 +03:00
parent cebd7c2a8c
commit e3439cacc2

View file

@ -5,9 +5,11 @@ import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -57,63 +59,67 @@ public class RegistryController extends AbstractQueueController {
super(operator);
this.stateLoader = stateLoader;
this.predicateBuilder = imdgProvider
.getImdg(IMDGDistributedNames.Map_ExecutionDeposit, ExecutionDeposit.class)
.predicateBuilder();
.getImdg(IMDGDistributedNames.Map_ExecutionDeposit, ExecutionDeposit.class)
.predicateBuilder();
}
@ApiOperation(value = "get all registries.")
@ApiResponses(value = {@ApiResponse(code = 200, message = "OK", response = CommonGetAllResponse.class)})
@RequestMapping(method = RequestMethod.GET)
@ResponseBody
public CommonGetAllResponse getAll(@RequestParam(name = "designation", required = false) List<String> designations,
@RequestParam(name = "status", required = false) String status,
@RequestParam(name = "pageSize", required = false) Integer pageSize,
@RequestParam(name = "pageIndex", required = false) Integer pageIndex) {
if (designations != null && !designations.isEmpty()) {
for (String designation : designations) {
if (IEnumKey.getEnumByKey(RegistryDesignation.class, designation) == null) {
throw new ResponseStatusException(HttpStatus.NOT_ACCEPTABLE, "Unknown registry designation: " + designation);
}
}
}
if (status != null && !status.isEmpty() && IEnumKey.getEnumByKey(RegistryStatus.class, status) == null) {
throw new ResponseStatusException(HttpStatus.NOT_ACCEPTABLE, "Unknown registry status: " + status);
public CommonGetAllResponse getAll() {
// long t1 = System.currentTimeMillis();
ImdgPredicate filter = predicateBuilder.or(
predicateBuilder.equals("registryDesignation", RegistryDesignation.A.getKey()),
predicateBuilder.equals("registryDesignation", RegistryDesignation.D.getKey()),
predicateBuilder.and(
predicateBuilder.or(
predicateBuilder.equals("registryDesignation", RegistryDesignation.O.getKey()),
predicateBuilder.equals("registryDesignation", RegistryDesignation.T.getKey())
),
predicateBuilder.greatEqual("settlementDate", LocalDate.now())
)
);
Collection<Map<String, Object>> all = stateLoader.getAllMetaTransform(IMDGDistributedNames.Map_Registry, Registry.class,
filter);
// long t2 = System.currentTimeMillis();
// log.info("Info select and format {} data {}", all.size(), t2 - t1);
CommonGetAllResponse response = new CommonGetAllResponse();
response.fromEntity(all);
// log.info("Info select and format {} data {}", all.size(), System.currentTimeMillis() - t2);
return response;
}
@ApiOperation(value = "get registries by filter.")
@ApiResponses(value = {@ApiResponse(code = 200, message = "OK", response = CommonGetAllResponse.class)})
@RequestMapping(value = "/filter", method = RequestMethod.GET)
@ResponseBody
public CommonGetAllResponse getByFilter(@RequestParam(name = "designation", required = false) Optional<List<String>> designations,
@RequestParam(name = "status", required = false) Optional<String> status,
@RequestParam(name = "pageSize") Integer pageSize,
@RequestParam(name = "pageIndex") Integer pageIndex) {
if (designations.isEmpty() && status.isEmpty()) {
throw new ResponseStatusException(HttpStatus.NOT_ACCEPTABLE, "Request should have at least one of the parameters: designation, status");
}
StopWatch sw = new StopWatch("registry-controller-stopwatch");
sw.start();
ImdgPredicate filter;
if (designations == null || designations.isEmpty()) {
filter = predicateBuilder.or(
predicateBuilder.equals("registryDesignation", RegistryDesignation.A.getKey()),
predicateBuilder.equals("registryDesignation", RegistryDesignation.D.getKey()),
predicateBuilder.and(
predicateBuilder.or(
predicateBuilder.equals("registryDesignation", RegistryDesignation.O.getKey()),
predicateBuilder.equals("registryDesignation", RegistryDesignation.T.getKey())
),
predicateBuilder.greatEqual("settlementDate", LocalDate.now())
)
);
if (status != null && !status.isEmpty()) {
filter = predicateBuilder.and(filter, predicateBuilder.in("registryStatus", status));
}
} else if (status == null || status.isEmpty()) {
filter = predicateBuilder.in("registryDesignation", designations.toArray(String[]::new));
} else {
filter = predicateBuilder.and(
predicateBuilder.in("registryDesignation", designations.toArray(String[]::new)),
predicateBuilder.in("registryStatus", status)
);
}
List<ImdgPredicate> filters = new ArrayList<>();
designations.ifPresent(ds -> filters.add(predicateBuilder.in("registryDesignation", ds.stream()
.map(d -> Optional.ofNullable(IEnumKey.getEnumByKey(RegistryDesignation.class, d))
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_ACCEPTABLE, "Unknown registry designation: " + d)))
.map(RegistryDesignation::getKey)
.toArray(String[]::new))));
status.ifPresent(s -> filters.add(predicateBuilder.in("registryStatus", Optional.ofNullable(IEnumKey.getEnumByKey(RegistryStatus.class, s))
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_ACCEPTABLE, "Unknown registry status: " + s))
.getKey())));
Collection<Map<String, Object>> all;
if (pageSize != null && pageIndex != null) {
all = stateLoader.getAllMetaTransformWithPagination(IMDGDistributedNames.Map_Registry, Registry.class, filter, pageSize, pageIndex);
} else {
all = stateLoader.getAllMetaTransform(IMDGDistributedNames.Map_Registry, Registry.class, filter);
}
Collection<Map<String, Object>> all = stateLoader.getAllMetaTransformWithPagination(IMDGDistributedNames.Map_Registry,
Registry.class,
predicateBuilder.and(filters.toArray(ImdgPredicate[]::new)),
pageSize,
pageIndex);
sw.stop();
log.info("Info select and format {} data took {} ms", all.size(), sw.getLastTaskTimeMillis());
@ -131,8 +137,8 @@ public class RegistryController extends AbstractQueueController {
@RequestMapping(value = "/splitDeposit", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public CudResponse splitDepositAction(
@ApiParam(value = "Параметры команды в JSON формате.", required = true)
@RequestBody RSplitDepositActionNew rSplitDepositActionNew) throws ExecutionException, InterruptedException {
@ApiParam(value = "Параметры команды в JSON формате.", required = true)
@RequestBody RSplitDepositActionNew rSplitDepositActionNew) throws ExecutionException, InterruptedException {
return processRequest(Consts.REGISTRY_SPLIT_DEPOSIT_ACTION, rSplitDepositActionNew);
}
@ -141,10 +147,10 @@ public class RegistryController extends AbstractQueueController {
@RequestMapping(value = "/returnDeposit/{id}", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public CudResponse returnDepositAction(
@ApiParam(value = "Идентификатор изменяемого объекта.", required = true, example = "1234")
@PathVariable("id") Long id,
@ApiParam(value = "Параметры команды в JSON формате.", required = true)
@RequestBody ReturnDepositActionNew returnDepositAction) throws ExecutionException, InterruptedException {
@ApiParam(value = "Идентификатор изменяемого объекта.", required = true, example = "1234")
@PathVariable("id") Long id,
@ApiParam(value = "Параметры команды в JSON формате.", required = true)
@RequestBody ReturnDepositActionNew returnDepositAction) throws ExecutionException, InterruptedException {
returnDepositAction.setId(id);
return processRequest(Consts.REGISTRY_RETURN_DEPOSIT_ACTION, returnDepositAction);
}
@ -154,10 +160,10 @@ public class RegistryController extends AbstractQueueController {
@RequestMapping(value = "/identificationFunds/{id}", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public CudResponse identificationFundsAction(
@ApiParam(value = "Идентификатор изменяемого объекта.", required = true, example = "1234")
@PathVariable("id") Long id,
@ApiParam(value = "Параметры команды в JSON формате.", required = true)
@RequestBody IdentificationFundsActionNew identificationFundsActionNew) throws ExecutionException, InterruptedException {
@ApiParam(value = "Идентификатор изменяемого объекта.", required = true, example = "1234")
@PathVariable("id") Long id,
@ApiParam(value = "Параметры команды в JSON формате.", required = true)
@RequestBody IdentificationFundsActionNew identificationFundsActionNew) throws ExecutionException, InterruptedException {
identificationFundsActionNew.setId(id);
return processRequest(Consts.REGISTRY_IDENTIFICATION_FUNDS, identificationFundsActionNew);
}
@ -167,10 +173,10 @@ public class RegistryController extends AbstractQueueController {
@RequestMapping(value = "/changeRefundDate/{groupId}", method = RequestMethod.PUT, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public CudResponse changeRefundDate(
@ApiParam(value = "Идентификатор группы связанных регистров.", required = true, example = "1234")
@PathVariable("groupId") Long groupId,
@ApiParam(value = "Параметры команды в JSON формате.", required = true)
@RequestBody ChangeRefundDateActionNew returnDepositAction) throws ExecutionException, InterruptedException {
@ApiParam(value = "Идентификатор группы связанных регистров.", required = true, example = "1234")
@PathVariable("groupId") Long groupId,
@ApiParam(value = "Параметры команды в JSON формате.", required = true)
@RequestBody ChangeRefundDateActionNew returnDepositAction) throws ExecutionException, InterruptedException {
returnDepositAction.setGroupId(groupId);
return processRequest(Consts.REGISTRY_CHANGE_REFUND_DATE_ACTION, returnDepositAction);
}
@ -180,10 +186,10 @@ public class RegistryController extends AbstractQueueController {
@RequestMapping(value = "/changeStatusExtract/{id}", method = RequestMethod.PUT, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public CudResponse changeStatusExtract(
@ApiParam(value = "Идентификатор регистра.", required = true, example = "1234")
@PathVariable("id") Long id,
@ApiParam(value = "Параметры команды в JSON формате.", required = true)
@RequestBody ChangeStatusExtractActionNew changeStatusExtractAction) throws ExecutionException, InterruptedException {
@ApiParam(value = "Идентификатор регистра.", required = true, example = "1234")
@PathVariable("id") Long id,
@ApiParam(value = "Параметры команды в JSON формате.", required = true)
@RequestBody ChangeStatusExtractActionNew changeStatusExtractAction) throws ExecutionException, InterruptedException {
changeStatusExtractAction.setId(id);
return processRequest(Consts.REGISTRY_CHANGE_STATUS_EXTRACT_ACTION, changeStatusExtractAction);
}