Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.amfinesoft.epr.core.api;
- @RestController
- @RequestMapping("/v1/accountings")
- public class AccountingController {
- @Resource
- private ProductService productService;
- @Resource
- private AccountingByNatureService accountingByNatureService;
- @Resource
- private DtoMapper dtoMapper;
- @ExceptionHandler(HttpResponseException.class)
- public ResponseEntity<ErrorDto> handleHttpResponseException(final HttpResponseException exception) {
- ErrorDto body = new ErrorDto();
- body.setTimestamp(OffsetDateTime.now());
- body.setStatus(exception.getStatusCode());
- body.setError(exception.getReasonPhrase());
- body.setMessage(exception.getMessage());
- return ResponseEntity.status(exception.getStatusCode()).contentType(MediaType.APPLICATION_JSON_UTF8).body(body);
- }
- @PostMapping("/insurer/{insurer}")
- public ResponseEntity<AccountingByNaturePageDto> listInsurer(@PathVariable final String insurer, @RequestBody final SearchFilterDto filter) throws HttpResponseException {
- if (!productService.getAuthorizedInsurers().contains(insurer)) {
- throw new HttpResponseException(HttpStatus.FORBIDDEN.value(), "you cannot access " + insurer + "'s data");
- }
- AccountingByNaturePageDto dto = new AccountingByNaturePageDto();
- dto.setPagination(accountingByNatureService.computeAccountingsByInsurerPagination(insurer, filter));
- if (dto.getPagination().getNumResults() > 0) {
- final List<AccountingByNature> results2 = accountingByNatureService.listAccountingsByInsurer(insurer, filter);
- for (AccountingByNature a : results2) {
- dto.getAccountings().add(dtoMapper.asDto(a));
- }
- }
- return ResponseEntity.ok(dto);
- }
- @PreAuthorize("hasAnyAuthority('ADMIN', 'EPR_EDIT')")
- @PutMapping
- @Transactional
- public ResponseEntity<AccountingByNatureDto> createAccountingByNature(@RequestBody final AccountingByNatureDto dto) throws HttpResponseException {
- dto.setId(null);
- if ((dto == null) || StringUtils.isBlank(dto.getProductId())) {
- throw new HttpResponseException(HttpStatus.BAD_REQUEST.value(), "productId is mandatory");
- }
- if (dto.getNature() == null) {
- throw new HttpResponseException(HttpStatus.BAD_REQUEST.value(), "nature is mandatory");
- }
- Product product = productService.findById(dto.getProductId());
- if (product == null) {
- throw new HttpResponseException(HttpStatus.BAD_REQUEST.value(), "No such product " + dto.getProductId());
- }
- if (!productService.getAuthorizedInsurers().contains(product.getInsurer())) {
- throw new HttpResponseException(HttpStatus.FORBIDDEN.value(), "you cannot access " + product.getInsurer() + "'s data");
- }
- AccountingByNature accounting = accountingByNatureService.findByProductAndNature(product, dto.getNature());
- if (accounting != null) {
- throw new HttpResponseException(HttpStatus.CONFLICT.value(), "AccountingByNature for " + dto.getProductId() + " & " + dto.getNature() + " already exists");
- }
- accounting = new AccountingByNature();
- accounting.setProduct(product);
- accounting.setNature(dto.getNature());
- merge(accounting, dto);
- accounting = accountingByNatureService.save(accounting);
- return ResponseEntity.ok(dtoMapper.asDto(accounting));
- }
- @PreAuthorize("hasAnyAuthority('ADMIN', 'EPR_EDIT')")
- @PostMapping
- @Transactional
- public ResponseEntity<AccountingByNatureDto> updateAccountingByNature(@RequestBody final AccountingByNatureDto dto) throws HttpResponseException {
- if ((dto == null) || (dto.getId() == null)) {
- throw new HttpResponseException(HttpStatus.BAD_REQUEST.value(), "id is mandatory");
- }
- AccountingByNature accounting = accountingByNatureService.findById(dto.getId());
- if (accounting == null) {
- throw new HttpResponseException(HttpStatus.NOT_FOUND.value(), "No such accountingByNature " + dto.getId());
- }
- ensureProductLink(accounting);
- merge(accounting, dto);
- accounting = accountingByNatureService.save(accounting);
- return ResponseEntity.ok(dtoMapper.asDto(accounting));
- }
- @GetMapping("/{id}")
- public ResponseEntity<AccountingByNatureDto> getAccountingByNature(@PathVariable final Long id) throws HttpResponseException {
- AccountingByNature accounting = accountingByNatureService.findById(id);
- if (accounting == null) {
- throw new HttpResponseException(HttpStatus.NOT_FOUND.value(), "No such accountingByNature " + id);
- }
- ensureProductLink(accounting);
- return ResponseEntity.ok(dtoMapper.asDto(accounting));
- }
- @GetMapping("/productId/{productId}/nature/{nature}")
- public ResponseEntity<AccountingByNatureDto> getAccountingByNature(@PathVariable final String productId, @PathVariable final String nature) throws HttpResponseException {
- Nature nat = Nature.fromCode(nature);
- if (nat == null) {
- nat = Nature.fromLabel(nature);
- }
- if (nat == null) {
- throw new HttpResponseException(HttpStatus.BAD_REQUEST.value(), "invalid nature " + nature);
- }
- Product product = productService.findById(productId);
- if (product == null) {
- throw new HttpResponseException(HttpStatus.BAD_REQUEST.value(), "No such product " + productId);
- }
- AccountingByNature accounting = accountingByNatureService.findByProductAndNature(product, nat);
- if (accounting == null) {
- throw new HttpResponseException(HttpStatus.NOT_FOUND.value(), "No such accountingByNature");
- }
- ensureProductLink(accounting);
- return ResponseEntity.ok(dtoMapper.asDto(accounting));
- }
- private void merge(final AccountingByNature accounting, final AccountingByNatureDto dto) {
- accounting.setSubscriptionFee(dto.getSubscriptionFee());
- accounting.setTransactionFee(dto.getTransactionFee());
- accounting.setExitCost1Year(dto.getExitCost1Year());
- accounting.setExitCostHalfRHP(dto.getExitCostHalfRHP());
- accounting.setExitCostRHP(dto.getExitCostRHP());
- accounting.setManagementFee(dto.getManagementFee());
- accounting.setMaximumInvest(dto.getMaximumInvest());
- accounting.setShareOfProfitSharing(dto.getShareOfProfitSharing());
- }
- private void ensureProductLink(final AccountingByNature accounting) throws HttpResponseException {
- // GROS HACK POUR CONTOURNER L'ABSENCE D'INTEGRITE DANS LA BASE
- // DROPPER le try-catch QUAND ON AURA REMIS DE L'INTEGRITE (pas le contenu)
- try {
- if ((accounting.getProduct() == null) || StringUtils.isBlank(accounting.getProduct().getId())) {
- throw new HttpResponseException(HttpStatus.PRECONDITION_FAILED.value(), "AccountingByNature not associated to a product");
- }
- if (!productService.getAuthorizedInsurers().contains(accounting.getProduct().getInsurer())) {
- throw new HttpResponseException(HttpStatus.FORBIDDEN.value(), "you cannot access " + accounting.getProduct().getInsurer() + "'s data");
- }
- } catch (Exception e) {
- throw new HttpResponseException(HttpStatus.PRECONDITION_FAILED.value(), "AccountingByNature not associated to a product");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement