在Java中动态生成服务

发布于 2025-01-27 15:57:59 字数 9042 浏览 1 评论 0原文

如何从lignecommandedto检索数据并将其传递给lignebpdto,以动态生成lignebpservice,如果affercter()为true,则从lignecommandeservice中。

用于检查属性属性是否为True的Affercter()方法,如果我称之为GenererBP()Methode,则该方法会创建一个lignebp,因此我需要填充来自lignecommandedto的数据。

    package org.backend.gcmd.service;
    
    import org.backend.gcmd.dto.LigneBpDTO;
    import org.backend.gcmd.dto.LigneCommandeDTO;
    import org.backend.gcmd.entity.LigneCommandeEntity;
    import org.backend.gcmd.exceptions.technical.ObjectNotFoundException;
    import org.backend.gcmd.mapper.LigneCommandeMapper;
    import org.backend.gcmd.repository.LigneCommandeRepository;
    import org.backend.gcmd.validator.Validate;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.data.domain.Page;
    import org.springframework.data.domain.Pageable;
    import org.springframework.stereotype.Service;
    import org.springframework.transaction.annotation.Transactional;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    
    import java.util.Optional;
    
    import static java.util.stream.Collectors.toList;
    
    @Service
    @Transactional
    public class LigneCommandeService {
    
        @Autowired
        private LigneCommandeRepository ligneCommandeRepository;
    
        @Autowired
        private LigneCommandeMapper ligneCommandeMapper;
    
        @Autowired
        private LigneBpService ligneBpService;


        private LigneBpDTO ligneBpDTO;

        @Autowired
        private LigneBpMapper ligneBpMapper;

    
        public LigneCommandeDTO findById(Long id) {
            Validate.notNull(id, "id mus be not null");
            Optional<LigneCommandeEntity> entity = ligneCommandeRepository.findById(id);
            if (entity.isPresent()) {
                return ligneCommandeMapper.convertToDto(entity.get());
            } else {
                throw new ObjectNotFoundException("LigneCommandeDTO not found");
            }
        }
    
        public LigneCommandeDTO save(LigneCommandeDTO dto) {
            Validate.notNull(dto, "LigneCommandeDTO must be not null");
            LigneCommandeEntity entity = ligneCommandeMapper.convertToEntity(dto);
            LigneCommandeEntity saved = ligneCommandeRepository.save(entity);
            return ligneCommandeMapper.convertToDto(saved);
        }
    
        public LigneCommandeDTO update(LigneCommandeDTO dto) {
            Validate.notNull(dto, "LigneCommandeDTO must be not null");
            Validate.notNull(dto.getId(), "LigneCommandeDTO id must be not null");
            findById(dto.getId());
            LigneCommandeEntity entity = ligneCommandeMapper.convertToEntity(dto);
            LigneCommandeEntity saved = ligneCommandeRepository.save(entity);
            return ligneCommandeMapper.convertToDto(saved);
        }
    
    
        public Page<LigneCommandeDTO> findAllByDeletedFalse(Pageable pageable) {
            Page<LigneCommandeEntity> page = ligneCommandeRepository.findAllByDeletedFalse(pageable);
            return ligneCommandeMapper.convertToPageDto(page);
        }
    
        public LigneCommandeDTO affecter(Long id, Boolean isAffected) {
            Validate.notNull(id, "LigneCommandeDTO must be not null");
            Validate.notNull(isAffected, "LigneCommandeDTO id must be not null");
            LigneCommandeDTO lcdto = findById(id);
            lcdto.setIsAffected(isAffected);
            update(lcdto);
            genererbp();
            return null;
        }
        
        public LigneBpDTO genererbp(LigneCommandeDTO ligneCommandeDTO) {
            if (ligneCommandeDTO.getIsAffected()) {
                return ligneBpService.findAllByDeletedFalse((Pageable) ligneBpDTO)
                        .stream()
                        .map(ligneBpMapper::convertToDto)
                        .collect(toList());
            }
            return null;
        }
    }
    

映射班

    package org.backend.gcmd.mapper;
    
    import org.springframework.data.domain.Page;
    
    import java.util.List;
    
    public interface Mapper<D, E> {
    
        Page<D> convertToPageDto(Page<E> page);
    
        D convertToDto(E entity);
    
        E convertToEntity(D dto);
    
        List<D> convertToDtoList(List<E> entities);
    
        List<E> convertToEntitiesList(List<D> entities);
    }
    
package org.backend.gcmd.mapper;

import org.backend.gcmd.dto.LigneBpDTO;
import org.backend.gcmd.entity.LigneBpEntity;
import org.backend.gcmd.repository.BulltinPrestationRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Component
public class LigneBpMapper implements Mapper<LigneBpDTO, LigneBpEntity> {

    @Autowired
    BulltinPrestationRepository bulltinprestationRepository;

    @Override
    public Page<LigneBpDTO> convertToPageDto(Page<LigneBpEntity> page) {
        return page.map(this::convertToDto);
    }

    @Override
    public LigneBpDTO convertToDto(LigneBpEntity entity) {
        LigneBpDTO dto = new LigneBpDTO();
        dto.setId(entity.getId());
        dto.setDate(entity.getDate());
        dto.setHeure(entity.getHeure());
        dto.setNombre(entity.getNombre());
        dto.setPrestation(entity.getPrestation());
        dto.setProduit(entity.getProduit());
        dto.setSensTrafic(entity.getSensTrafic());
        dto.setTarifUnifie(entity.getTarifUnifie());
        dto.setTcConv(entity.getTcConv());
        dto.setTcSuppl(entity.getTcSuppl());
        dto.setTonnageMinimum(entity.getTonnageMinimum());
        dto.setTonnageReel(entity.getTonnageReel());

        return dto;
    }

    @Override
    public LigneBpEntity convertToEntity(LigneBpDTO dto) {
        LigneBpEntity entity = new LigneBpEntity();
        entity.setId(dto.getId());
        entity.setDate(dto.getDate());
        entity.setHeure(dto.getHeure());
        entity.setNombre(dto.getNombre());
        entity.setPrestation(dto.getPrestation());
        entity.setProduit(dto.getProduit());
        entity.setSensTrafic(dto.getSensTrafic());
        entity.setTarifUnifie(dto.getTarifUnifie());
        entity.setTcConv(dto.getTcConv());
        entity.setTcSuppl(dto.getTcSuppl());
        entity.setTonnageMinimum(dto.getTonnageMinimum());
        entity.setTonnageReel(dto.getTonnageReel());
        return entity;
    }

    @Override
    public List<LigneBpDTO> convertToDtoList(List<LigneBpEntity> entities) {
        return entities.stream().map(this::convertToDto).collect(Collectors.toCollection(ArrayList::new));
    }

    @Override
    public List<LigneBpEntity> convertToEntitiesList(List<LigneBpDTO> dtos) {
        return dtos.stream().map(this::convertToEntity).collect(Collectors.toCollection(ArrayList::new));
    }
}
controller
package org.backend.gcmd.controller;

import org.backend.gcmd.dto.LigneBpDTO;
import org.backend.gcmd.dto.LigneCommandeDTO;
import org.backend.gcmd.service.LigneBpService;
import org.backend.gcmd.service.LigneCommandeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/gcmd/v1/ligneCommandes")
public class LigneCommandeController {
    @Autowired
    private LigneCommandeService ligneCommandeService;


    @GetMapping("{id}")
    public ResponseEntity<LigneCommandeDTO> findById(@PathVariable Long id) {
        LigneCommandeDTO ligneCommandeDTO = ligneCommandeService.findById(id);
        return ResponseEntity.status(HttpStatus.OK).body(ligneCommandeDTO);
    }

    @PostMapping
    public ResponseEntity<LigneCommandeDTO> save(@RequestBody LigneCommandeDTO ligneCommandeDTO) {
        return ResponseEntity.status(HttpStatus.CREATED).body(ligneCommandeService.save(ligneCommandeDTO));
    }

    @PutMapping("{id}")
    public ResponseEntity<LigneCommandeDTO> update(@PathVariable Long id,
                                                   @RequestBody LigneCommandeDTO ligneCommandeDTO) {
        ligneCommandeDTO.setId(id);
        return ResponseEntity.status(HttpStatus.ACCEPTED).body(ligneCommandeService.update(ligneCommandeDTO));
    }

    @GetMapping
    public ResponseEntity<Page<LigneCommandeDTO>> findAllByDeletedFalse(Pageable pageable) {
        return ResponseEntity.status(HttpStatus.OK).body(ligneCommandeService.findAllByDeletedFalse(pageable));
    }
}

How to retrieve data from ligneCommandeDto and pass it to lignebpdto to generate dynamically a lignebpService from ligneCommandeService if affercter() is true.

the affercter() method for checking if the isAffected attribute is true, if that I call the genererbp() methode , this methode create a ligneBp so I need to fill il with data from ligneCommandeDTO.

    package org.backend.gcmd.service;
    
    import org.backend.gcmd.dto.LigneBpDTO;
    import org.backend.gcmd.dto.LigneCommandeDTO;
    import org.backend.gcmd.entity.LigneCommandeEntity;
    import org.backend.gcmd.exceptions.technical.ObjectNotFoundException;
    import org.backend.gcmd.mapper.LigneCommandeMapper;
    import org.backend.gcmd.repository.LigneCommandeRepository;
    import org.backend.gcmd.validator.Validate;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.data.domain.Page;
    import org.springframework.data.domain.Pageable;
    import org.springframework.stereotype.Service;
    import org.springframework.transaction.annotation.Transactional;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    
    import java.util.Optional;
    
    import static java.util.stream.Collectors.toList;
    
    @Service
    @Transactional
    public class LigneCommandeService {
    
        @Autowired
        private LigneCommandeRepository ligneCommandeRepository;
    
        @Autowired
        private LigneCommandeMapper ligneCommandeMapper;
    
        @Autowired
        private LigneBpService ligneBpService;


        private LigneBpDTO ligneBpDTO;

        @Autowired
        private LigneBpMapper ligneBpMapper;

    
        public LigneCommandeDTO findById(Long id) {
            Validate.notNull(id, "id mus be not null");
            Optional<LigneCommandeEntity> entity = ligneCommandeRepository.findById(id);
            if (entity.isPresent()) {
                return ligneCommandeMapper.convertToDto(entity.get());
            } else {
                throw new ObjectNotFoundException("LigneCommandeDTO not found");
            }
        }
    
        public LigneCommandeDTO save(LigneCommandeDTO dto) {
            Validate.notNull(dto, "LigneCommandeDTO must be not null");
            LigneCommandeEntity entity = ligneCommandeMapper.convertToEntity(dto);
            LigneCommandeEntity saved = ligneCommandeRepository.save(entity);
            return ligneCommandeMapper.convertToDto(saved);
        }
    
        public LigneCommandeDTO update(LigneCommandeDTO dto) {
            Validate.notNull(dto, "LigneCommandeDTO must be not null");
            Validate.notNull(dto.getId(), "LigneCommandeDTO id must be not null");
            findById(dto.getId());
            LigneCommandeEntity entity = ligneCommandeMapper.convertToEntity(dto);
            LigneCommandeEntity saved = ligneCommandeRepository.save(entity);
            return ligneCommandeMapper.convertToDto(saved);
        }
    
    
        public Page<LigneCommandeDTO> findAllByDeletedFalse(Pageable pageable) {
            Page<LigneCommandeEntity> page = ligneCommandeRepository.findAllByDeletedFalse(pageable);
            return ligneCommandeMapper.convertToPageDto(page);
        }
    
        public LigneCommandeDTO affecter(Long id, Boolean isAffected) {
            Validate.notNull(id, "LigneCommandeDTO must be not null");
            Validate.notNull(isAffected, "LigneCommandeDTO id must be not null");
            LigneCommandeDTO lcdto = findById(id);
            lcdto.setIsAffected(isAffected);
            update(lcdto);
            genererbp();
            return null;
        }
        
        public LigneBpDTO genererbp(LigneCommandeDTO ligneCommandeDTO) {
            if (ligneCommandeDTO.getIsAffected()) {
                return ligneBpService.findAllByDeletedFalse((Pageable) ligneBpDTO)
                        .stream()
                        .map(ligneBpMapper::convertToDto)
                        .collect(toList());
            }
            return null;
        }
    }
    

the mapper class

    package org.backend.gcmd.mapper;
    
    import org.springframework.data.domain.Page;
    
    import java.util.List;
    
    public interface Mapper<D, E> {
    
        Page<D> convertToPageDto(Page<E> page);
    
        D convertToDto(E entity);
    
        E convertToEntity(D dto);
    
        List<D> convertToDtoList(List<E> entities);
    
        List<E> convertToEntitiesList(List<D> entities);
    }
    
package org.backend.gcmd.mapper;

import org.backend.gcmd.dto.LigneBpDTO;
import org.backend.gcmd.entity.LigneBpEntity;
import org.backend.gcmd.repository.BulltinPrestationRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Component
public class LigneBpMapper implements Mapper<LigneBpDTO, LigneBpEntity> {

    @Autowired
    BulltinPrestationRepository bulltinprestationRepository;

    @Override
    public Page<LigneBpDTO> convertToPageDto(Page<LigneBpEntity> page) {
        return page.map(this::convertToDto);
    }

    @Override
    public LigneBpDTO convertToDto(LigneBpEntity entity) {
        LigneBpDTO dto = new LigneBpDTO();
        dto.setId(entity.getId());
        dto.setDate(entity.getDate());
        dto.setHeure(entity.getHeure());
        dto.setNombre(entity.getNombre());
        dto.setPrestation(entity.getPrestation());
        dto.setProduit(entity.getProduit());
        dto.setSensTrafic(entity.getSensTrafic());
        dto.setTarifUnifie(entity.getTarifUnifie());
        dto.setTcConv(entity.getTcConv());
        dto.setTcSuppl(entity.getTcSuppl());
        dto.setTonnageMinimum(entity.getTonnageMinimum());
        dto.setTonnageReel(entity.getTonnageReel());

        return dto;
    }

    @Override
    public LigneBpEntity convertToEntity(LigneBpDTO dto) {
        LigneBpEntity entity = new LigneBpEntity();
        entity.setId(dto.getId());
        entity.setDate(dto.getDate());
        entity.setHeure(dto.getHeure());
        entity.setNombre(dto.getNombre());
        entity.setPrestation(dto.getPrestation());
        entity.setProduit(dto.getProduit());
        entity.setSensTrafic(dto.getSensTrafic());
        entity.setTarifUnifie(dto.getTarifUnifie());
        entity.setTcConv(dto.getTcConv());
        entity.setTcSuppl(dto.getTcSuppl());
        entity.setTonnageMinimum(dto.getTonnageMinimum());
        entity.setTonnageReel(dto.getTonnageReel());
        return entity;
    }

    @Override
    public List<LigneBpDTO> convertToDtoList(List<LigneBpEntity> entities) {
        return entities.stream().map(this::convertToDto).collect(Collectors.toCollection(ArrayList::new));
    }

    @Override
    public List<LigneBpEntity> convertToEntitiesList(List<LigneBpDTO> dtos) {
        return dtos.stream().map(this::convertToEntity).collect(Collectors.toCollection(ArrayList::new));
    }
}
controller
package org.backend.gcmd.controller;

import org.backend.gcmd.dto.LigneBpDTO;
import org.backend.gcmd.dto.LigneCommandeDTO;
import org.backend.gcmd.service.LigneBpService;
import org.backend.gcmd.service.LigneCommandeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/gcmd/v1/ligneCommandes")
public class LigneCommandeController {
    @Autowired
    private LigneCommandeService ligneCommandeService;


    @GetMapping("{id}")
    public ResponseEntity<LigneCommandeDTO> findById(@PathVariable Long id) {
        LigneCommandeDTO ligneCommandeDTO = ligneCommandeService.findById(id);
        return ResponseEntity.status(HttpStatus.OK).body(ligneCommandeDTO);
    }

    @PostMapping
    public ResponseEntity<LigneCommandeDTO> save(@RequestBody LigneCommandeDTO ligneCommandeDTO) {
        return ResponseEntity.status(HttpStatus.CREATED).body(ligneCommandeService.save(ligneCommandeDTO));
    }

    @PutMapping("{id}")
    public ResponseEntity<LigneCommandeDTO> update(@PathVariable Long id,
                                                   @RequestBody LigneCommandeDTO ligneCommandeDTO) {
        ligneCommandeDTO.setId(id);
        return ResponseEntity.status(HttpStatus.ACCEPTED).body(ligneCommandeService.update(ligneCommandeDTO));
    }

    @GetMapping
    public ResponseEntity<Page<LigneCommandeDTO>> findAllByDeletedFalse(Pageable pageable) {
        return ResponseEntity.status(HttpStatus.OK).body(ligneCommandeService.findAllByDeletedFalse(pageable));
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文