Advertisement
Guest User

Untitled

a guest
Jul 18th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.03 KB | None | 0 0
  1. package com.amfinesoft.epr.core.api;
  2.  
  3. @RestController
  4. @RequestMapping("/v1/accountings")
  5. public class AccountingController {
  6.  
  7.   @Resource
  8.   private ProductService            productService;
  9.  
  10.   @Resource
  11.   private AccountingByNatureService accountingByNatureService;
  12.  
  13.   @Resource
  14.   private DtoMapper                 dtoMapper;
  15.  
  16.   @ExceptionHandler(HttpResponseException.class)
  17.   public ResponseEntity<ErrorDto> handleHttpResponseException(final HttpResponseException exception) {
  18.     ErrorDto body = new ErrorDto();
  19.     body.setTimestamp(OffsetDateTime.now());
  20.     body.setStatus(exception.getStatusCode());
  21.     body.setError(exception.getReasonPhrase());
  22.     body.setMessage(exception.getMessage());
  23.     return ResponseEntity.status(exception.getStatusCode()).contentType(MediaType.APPLICATION_JSON_UTF8).body(body);
  24.   }
  25.  
  26.   @PostMapping("/insurer/{insurer}")
  27.   public ResponseEntity<AccountingByNaturePageDto> listInsurer(@PathVariable final String insurer, @RequestBody final SearchFilterDto filter) throws HttpResponseException {
  28.     if (!productService.getAuthorizedInsurers().contains(insurer)) {
  29.       throw new HttpResponseException(HttpStatus.FORBIDDEN.value(), "you cannot access " + insurer + "'s data");
  30.     }
  31.     AccountingByNaturePageDto dto = new AccountingByNaturePageDto();
  32.     dto.setPagination(accountingByNatureService.computeAccountingsByInsurerPagination(insurer, filter));
  33.     if (dto.getPagination().getNumResults() > 0) {
  34.       final List<AccountingByNature> results2 = accountingByNatureService.listAccountingsByInsurer(insurer, filter);
  35.       for (AccountingByNature a : results2) {
  36.         dto.getAccountings().add(dtoMapper.asDto(a));
  37.       }
  38.     }
  39.     return ResponseEntity.ok(dto);
  40.   }
  41.  
  42.   @PreAuthorize("hasAnyAuthority('ADMIN', 'EPR_EDIT')")
  43.   @PutMapping
  44.   @Transactional
  45.   public ResponseEntity<AccountingByNatureDto> createAccountingByNature(@RequestBody final AccountingByNatureDto dto) throws HttpResponseException {
  46.     dto.setId(null);
  47.     if ((dto == null) || StringUtils.isBlank(dto.getProductId())) {
  48.       throw new HttpResponseException(HttpStatus.BAD_REQUEST.value(), "productId is mandatory");
  49.     }
  50.     if (dto.getNature() == null) {
  51.       throw new HttpResponseException(HttpStatus.BAD_REQUEST.value(), "nature is mandatory");
  52.     }
  53.     Product product = productService.findById(dto.getProductId());
  54.     if (product == null) {
  55.       throw new HttpResponseException(HttpStatus.BAD_REQUEST.value(), "No such product " + dto.getProductId());
  56.     }
  57.     if (!productService.getAuthorizedInsurers().contains(product.getInsurer())) {
  58.       throw new HttpResponseException(HttpStatus.FORBIDDEN.value(), "you cannot access " + product.getInsurer() + "'s data");
  59.     }
  60.     AccountingByNature accounting = accountingByNatureService.findByProductAndNature(product, dto.getNature());
  61.     if (accounting != null) {
  62.       throw new HttpResponseException(HttpStatus.CONFLICT.value(), "AccountingByNature for " + dto.getProductId() + " & " + dto.getNature() + " already exists");
  63.     }
  64.     accounting = new AccountingByNature();
  65.     accounting.setProduct(product);
  66.     accounting.setNature(dto.getNature());
  67.     merge(accounting, dto);
  68.     accounting = accountingByNatureService.save(accounting);
  69.     return ResponseEntity.ok(dtoMapper.asDto(accounting));
  70.   }
  71.  
  72.   @PreAuthorize("hasAnyAuthority('ADMIN', 'EPR_EDIT')")
  73.   @PostMapping
  74.   @Transactional
  75.   public ResponseEntity<AccountingByNatureDto> updateAccountingByNature(@RequestBody final AccountingByNatureDto dto) throws HttpResponseException {
  76.     if ((dto == null) || (dto.getId() == null)) {
  77.       throw new HttpResponseException(HttpStatus.BAD_REQUEST.value(), "id is mandatory");
  78.     }
  79.     AccountingByNature accounting = accountingByNatureService.findById(dto.getId());
  80.     if (accounting == null) {
  81.       throw new HttpResponseException(HttpStatus.NOT_FOUND.value(), "No such accountingByNature " + dto.getId());
  82.     }
  83.     ensureProductLink(accounting);
  84.     merge(accounting, dto);
  85.     accounting = accountingByNatureService.save(accounting);
  86.     return ResponseEntity.ok(dtoMapper.asDto(accounting));
  87.   }
  88.  
  89.   @GetMapping("/{id}")
  90.   public ResponseEntity<AccountingByNatureDto> getAccountingByNature(@PathVariable final Long id) throws HttpResponseException {
  91.     AccountingByNature accounting = accountingByNatureService.findById(id);
  92.     if (accounting == null) {
  93.       throw new HttpResponseException(HttpStatus.NOT_FOUND.value(), "No such accountingByNature " + id);
  94.     }
  95.     ensureProductLink(accounting);
  96.     return ResponseEntity.ok(dtoMapper.asDto(accounting));
  97.   }
  98.  
  99.   @GetMapping("/productId/{productId}/nature/{nature}")
  100.   public ResponseEntity<AccountingByNatureDto> getAccountingByNature(@PathVariable final String productId, @PathVariable final String nature) throws HttpResponseException {
  101.     Nature nat = Nature.fromCode(nature);
  102.     if (nat == null) {
  103.       nat = Nature.fromLabel(nature);
  104.     }
  105.     if (nat == null) {
  106.       throw new HttpResponseException(HttpStatus.BAD_REQUEST.value(), "invalid nature " + nature);
  107.     }
  108.     Product product = productService.findById(productId);
  109.     if (product == null) {
  110.       throw new HttpResponseException(HttpStatus.BAD_REQUEST.value(), "No such product " + productId);
  111.     }
  112.     AccountingByNature accounting = accountingByNatureService.findByProductAndNature(product, nat);
  113.     if (accounting == null) {
  114.       throw new HttpResponseException(HttpStatus.NOT_FOUND.value(), "No such accountingByNature");
  115.     }
  116.     ensureProductLink(accounting);
  117.     return ResponseEntity.ok(dtoMapper.asDto(accounting));
  118.   }
  119.  
  120.   private void merge(final AccountingByNature accounting, final AccountingByNatureDto dto) {
  121.     accounting.setSubscriptionFee(dto.getSubscriptionFee());
  122.     accounting.setTransactionFee(dto.getTransactionFee());
  123.     accounting.setExitCost1Year(dto.getExitCost1Year());
  124.     accounting.setExitCostHalfRHP(dto.getExitCostHalfRHP());
  125.     accounting.setExitCostRHP(dto.getExitCostRHP());
  126.     accounting.setManagementFee(dto.getManagementFee());
  127.     accounting.setMaximumInvest(dto.getMaximumInvest());
  128.     accounting.setShareOfProfitSharing(dto.getShareOfProfitSharing());
  129.   }
  130.  
  131.   private void ensureProductLink(final AccountingByNature accounting) throws HttpResponseException {
  132.     // GROS HACK POUR CONTOURNER L'ABSENCE D'INTEGRITE DANS LA BASE
  133.     // DROPPER le try-catch QUAND ON AURA REMIS DE L'INTEGRITE (pas le contenu)
  134.     try {
  135.       if ((accounting.getProduct() == null) || StringUtils.isBlank(accounting.getProduct().getId())) {
  136.         throw new HttpResponseException(HttpStatus.PRECONDITION_FAILED.value(), "AccountingByNature not associated to a product");
  137.       }
  138.       if (!productService.getAuthorizedInsurers().contains(accounting.getProduct().getInsurer())) {
  139.         throw new HttpResponseException(HttpStatus.FORBIDDEN.value(), "you cannot access " + accounting.getProduct().getInsurer() + "'s data");
  140.       }
  141.     } catch (Exception e) {
  142.       throw new HttpResponseException(HttpStatus.PRECONDITION_FAILED.value(), "AccountingByNature not associated to a product");
  143.     }
  144.   }
  145.  
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement