Search in sources :

Example 31 with OperationFailedException

use of me.kadarh.mecaworks.service.exceptions.OperationFailedException in project mecaworks by KadarH.

the class UserCalculService method updateListEnginWithConsommation.

public void updateListEnginWithConsommation(int monthValue, int year) {
    log.info("calling method getListEnginWithConsommation(month,year) in UserCalculService -- ");
    try {
        List<BonEngin> bonEngins = bonEnginRepo.findAllBetweenDates(LocalDate.of(year, Month.of(monthValue).getValue(), 1), LocalDate.of(year, Month.of(monthValue).plus(1).getValue(), 1));
        Map<Engin, Double> consommationPrevue = bonEngins.stream().filter(bonEngin -> bonEngin.getConsommationH() != 0 && bonEngin.getConsommationH() != null).collect(Collectors.groupingBy(BonEngin::getEngin, Collectors.averagingDouble(BonEngin::getConsommationPrevu)));
        for (Map.Entry<Engin, Double> entry : consommationPrevue.entrySet()) {
            if (entry != null) {
                Engin engin = enginService.get(entry.getKey().getId());
                engin.setConsommationMoyenne(entry.getValue().floatValue() > engin.getSousFamille().getConsommationHMax() ? entry.getValue().floatValue() : engin.getSousFamille().getConsommationHMax().floatValue());
                engin = enginService.update(engin);
                log.info("Engin with id =" + engin.getId() + " updated successfully");
            }
        }
    } catch (NoSuchElementException e) {
        log.info("Operation failed : No element in Engin table -- ");
        throw new ResourceNotFoundException("Opération echouée, Il n'y a aucun element pour cet engin", e);
    } catch (Exception e) {
        log.info("Operation failed -- ");
        throw new OperationFailedException("Opération echouée, problème de la base", e);
    }
}
Also used : Engin(me.kadarh.mecaworks.domain.others.Engin) java.util(java.util) BonEnginRepo(me.kadarh.mecaworks.repo.bons.BonEnginRepo) Month(java.time.Month) BonFournisseur(me.kadarh.mecaworks.domain.bons.BonFournisseur) Quantite(me.kadarh.mecaworks.domain.user.Quantite) EnginService(me.kadarh.mecaworks.service.EnginService) BonEngin(me.kadarh.mecaworks.domain.bons.BonEngin) BonLivraison(me.kadarh.mecaworks.domain.bons.BonLivraison) Chantier(me.kadarh.mecaworks.domain.others.Chantier) Collectors(java.util.stream.Collectors) ResourceNotFoundException(me.kadarh.mecaworks.service.exceptions.ResourceNotFoundException) Slf4j(lombok.extern.slf4j.Slf4j) OperationFailedException(me.kadarh.mecaworks.service.exceptions.OperationFailedException) BonFournisseurRepo(me.kadarh.mecaworks.repo.bons.BonFournisseurRepo) ChronoUnit(java.time.temporal.ChronoUnit) ChantierBatch(me.kadarh.mecaworks.domain.user.ChantierBatch) StockRepo(me.kadarh.mecaworks.repo.others.StockRepo) Service(org.springframework.stereotype.Service) LocalDate(java.time.LocalDate) BonLivraisonRepo(me.kadarh.mecaworks.repo.bons.BonLivraisonRepo) Stock(me.kadarh.mecaworks.domain.others.Stock) Transactional(org.springframework.transaction.annotation.Transactional) BonEngin(me.kadarh.mecaworks.domain.bons.BonEngin) OperationFailedException(me.kadarh.mecaworks.service.exceptions.OperationFailedException) Engin(me.kadarh.mecaworks.domain.others.Engin) BonEngin(me.kadarh.mecaworks.domain.bons.BonEngin) ResourceNotFoundException(me.kadarh.mecaworks.service.exceptions.ResourceNotFoundException) ResourceNotFoundException(me.kadarh.mecaworks.service.exceptions.ResourceNotFoundException) OperationFailedException(me.kadarh.mecaworks.service.exceptions.OperationFailedException)

Example 32 with OperationFailedException

use of me.kadarh.mecaworks.service.exceptions.OperationFailedException in project mecaworks by KadarH.

the class UserCalculService method getListChantierWithQuantities.

public List<ChantierBatch> getListChantierWithQuantities(int month, int year) {
    log.info("calling method getListChantierWithQuantities(month,year) in UserCalculService -- ");
    try {
        List<ChantierBatch> list = new ArrayList<>();
        ChantierBatch chantierBatch;
        LocalDate from = LocalDate.of(year, Month.of(month).getValue(), 1);
        LocalDate to = LocalDate.of(year + 1, Month.of(month).plus(1).getValue(), 1);
        List<BonEngin> bonEngins = bonEnginRepo.findAllBetweenDates(from, to);
        List<BonLivraison> bonLivraisons = bonLivraisonRepo.findAllBetweenDates(from, to);
        List<BonFournisseur> bonFournisseurs = bonFournisseurRepo.findAllBetweenDates(from, to);
        Map<Chantier, Long> sum = bonEngins.stream().collect(Collectors.groupingBy(BonEngin::getChantierTravail, Collectors.summingLong(BonEngin::getQuantite)));
        Map<Chantier, Long> sum2 = bonEngins.stream().filter(bonEngin -> bonEngin.getEngin().getGroupe().getLocataire() && bonEngin.getId() != null).collect(Collectors.groupingBy(BonEngin::getChantierTravail, Collectors.summingLong(BonEngin::getQuantite)));
        Map<Chantier, Long> chargeLocataire = bonEngins.stream().collect(Collectors.groupingBy(BonEngin::getChantierTravail, Collectors.summingLong(BonEngin::getChargeHoraire)));
        Map<Chantier, Long> chargeLocataireExterne = bonEngins.stream().filter(bonEngin -> bonEngin.getEngin().getGroupe().getLocataire()).collect(Collectors.groupingBy(BonEngin::getChantierTravail, Collectors.summingLong(BonEngin::getChargeHoraire)));
        Map<Chantier, Long> consommationPrevue = bonEngins.stream().collect(Collectors.groupingBy(BonEngin::getChantierTravail, Collectors.summingLong(BonEngin::getConsommationPrevu)));
        Map<Chantier, Double> prix = bonFournisseurs.stream().collect(Collectors.groupingBy(BonFournisseur::getChantier, Collectors.averagingDouble(BonFournisseur::getPrixUnitaire)));
        Map<Chantier, Long> quantiteAchetee = bonFournisseurs.stream().collect(Collectors.groupingBy(BonFournisseur::getChantier, Collectors.summingLong(BonFournisseur::getQuantite)));
        Map<Chantier, Long> quantiteFlotante = bonLivraisons.stream().collect(Collectors.groupingBy(BonLivraison::getChantierDepart, Collectors.summingLong(BonLivraison::getQuantite)));
        for (Map.Entry<Chantier, Long> entry : sum.entrySet()) {
            if (entry != null) {
                // Fix in 0 if null
                Long quantite = entry.getValue();
                Long quantiteL = sum2.get(entry.getKey()) == null ? 0L : sum2.get(entry.getKey());
                Long cl = chargeLocataire.get(entry.getKey()) == null ? 0L : chargeLocataire.get(entry.getKey());
                Long clex = chargeLocataireExterne.get(entry.getKey()) == null ? 0L : chargeLocataireExterne.get(entry.getKey());
                Long cp = consommationPrevue.get(entry.getKey()) == null ? 0L : consommationPrevue.get(entry.getKey());
                Long qa = quantiteAchetee.get(entry.getKey()) == null ? 0L : quantiteAchetee.get(entry.getKey());
                Long qf = quantiteFlotante.get(entry.getKey()) == null ? 0L : quantiteFlotante.get(entry.getKey());
                Float p = prix.get(entry.getKey()) == null ? 0f : prix.get(entry.getKey()).floatValue();
                chantierBatch = new ChantierBatch(month, year, quantite, quantiteL, cl, clex, cp, p, qa, qf, entry.getKey());
                if (chantierBatch.getQuantiteLocation() == null)
                    chantierBatch.setQuantiteLocation(0L);
                if (chantierBatch.getQuantite() == null)
                    chantierBatch.setQuantite(0L);
                if (chantierBatch.getChargeLocataireExterne() == null)
                    chantierBatch.setChargeLocataireExterne(0L);
                if (chantierBatch.getChargeLocataire() == null)
                    chantierBatch.setChargeLocataire(0L);
                if (chantierBatch.getConsommationPrevue() == null)
                    chantierBatch.setConsommationPrevue(0L);
                if (chantierBatch.getGazoilAchete() == null)
                    chantierBatch.setGazoilAchete(0L);
                if (chantierBatch.getGazoilFlotant() == null)
                    chantierBatch.setGazoilFlotant(0L);
                list.add(chantierBatch);
            }
        }
        log.info("--> List of Chantier_Batch contains " + list.size() + " elements");
        return list;
    } catch (NoSuchElementException e) {
        log.info("Operation failed : No element in ChantierBatch table -- ");
        throw new ResourceNotFoundException("Opération echouée, Il n'y a aucun element pour les mois precedents", e);
    } catch (Exception e) {
        log.info("Operation failed -- ");
        throw new OperationFailedException("Opération echouée, problème de la base", e);
    }
}
Also used : Engin(me.kadarh.mecaworks.domain.others.Engin) java.util(java.util) BonEnginRepo(me.kadarh.mecaworks.repo.bons.BonEnginRepo) Month(java.time.Month) BonFournisseur(me.kadarh.mecaworks.domain.bons.BonFournisseur) Quantite(me.kadarh.mecaworks.domain.user.Quantite) EnginService(me.kadarh.mecaworks.service.EnginService) BonEngin(me.kadarh.mecaworks.domain.bons.BonEngin) BonLivraison(me.kadarh.mecaworks.domain.bons.BonLivraison) Chantier(me.kadarh.mecaworks.domain.others.Chantier) Collectors(java.util.stream.Collectors) ResourceNotFoundException(me.kadarh.mecaworks.service.exceptions.ResourceNotFoundException) Slf4j(lombok.extern.slf4j.Slf4j) OperationFailedException(me.kadarh.mecaworks.service.exceptions.OperationFailedException) BonFournisseurRepo(me.kadarh.mecaworks.repo.bons.BonFournisseurRepo) ChronoUnit(java.time.temporal.ChronoUnit) ChantierBatch(me.kadarh.mecaworks.domain.user.ChantierBatch) StockRepo(me.kadarh.mecaworks.repo.others.StockRepo) Service(org.springframework.stereotype.Service) LocalDate(java.time.LocalDate) BonLivraisonRepo(me.kadarh.mecaworks.repo.bons.BonLivraisonRepo) Stock(me.kadarh.mecaworks.domain.others.Stock) Transactional(org.springframework.transaction.annotation.Transactional) BonFournisseur(me.kadarh.mecaworks.domain.bons.BonFournisseur) OperationFailedException(me.kadarh.mecaworks.service.exceptions.OperationFailedException) LocalDate(java.time.LocalDate) ResourceNotFoundException(me.kadarh.mecaworks.service.exceptions.ResourceNotFoundException) OperationFailedException(me.kadarh.mecaworks.service.exceptions.OperationFailedException) ChantierBatch(me.kadarh.mecaworks.domain.user.ChantierBatch) BonLivraison(me.kadarh.mecaworks.domain.bons.BonLivraison) BonEngin(me.kadarh.mecaworks.domain.bons.BonEngin) Chantier(me.kadarh.mecaworks.domain.others.Chantier) ResourceNotFoundException(me.kadarh.mecaworks.service.exceptions.ResourceNotFoundException)

Aggregations

OperationFailedException (me.kadarh.mecaworks.service.exceptions.OperationFailedException)32 ResourceNotFoundException (me.kadarh.mecaworks.service.exceptions.ResourceNotFoundException)21 NoSuchElementException (java.util.NoSuchElementException)19 ExampleMatcher (org.springframework.data.domain.ExampleMatcher)14 BonEngin (me.kadarh.mecaworks.domain.bons.BonEngin)9 BonLivraison (me.kadarh.mecaworks.domain.bons.BonLivraison)8 BonFournisseur (me.kadarh.mecaworks.domain.bons.BonFournisseur)7 Chantier (me.kadarh.mecaworks.domain.others.Chantier)6 DataIntegrityViolationException (org.springframework.dao.DataIntegrityViolationException)6 LocalDate (java.time.LocalDate)5 ChronoUnit (java.time.temporal.ChronoUnit)5 Collectors (java.util.stream.Collectors)5 Slf4j (lombok.extern.slf4j.Slf4j)5 BonEnginRepo (me.kadarh.mecaworks.repo.bons.BonEnginRepo)5 BonFournisseurRepo (me.kadarh.mecaworks.repo.bons.BonFournisseurRepo)5 BonLivraisonRepo (me.kadarh.mecaworks.repo.bons.BonLivraisonRepo)5 Service (org.springframework.stereotype.Service)5 Transactional (org.springframework.transaction.annotation.Transactional)5 DateTimeParseException (java.time.format.DateTimeParseException)4 ArrayList (java.util.ArrayList)4