blob: ddc7a6cdcd2c1c93c434decd2ec6abfbd05d006c [file] [log] [blame]
package org.eclipse.openk.contactbasedata.service;
import lombok.extern.log4j.Log4j2;
import org.eclipse.openk.contactbasedata.constants.Constants;
import org.eclipse.openk.contactbasedata.exceptions.BadRequestException;
import org.eclipse.openk.contactbasedata.exceptions.NotFoundException;
import org.eclipse.openk.contactbasedata.mapper.CommunicationTypeMapper;
import org.eclipse.openk.contactbasedata.mapper.ContactMapper;
import org.eclipse.openk.contactbasedata.mapper.ExternalPersonMapper;
import org.eclipse.openk.contactbasedata.model.*;
import org.eclipse.openk.contactbasedata.repository.*;
import org.eclipse.openk.contactbasedata.viewmodel.CommunicationTypeDto;
import org.eclipse.openk.contactbasedata.viewmodel.ExternalPersonDto;
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 java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;
@Log4j2
@Service
public class CommunicationTypeService {
@Autowired
private CommunicationTypeRepository communicationTypeRepository;
@Autowired
private CommunicationTypeMapper communicationTypeMapper;
public CommunicationTypeDto findCommunicationType(UUID communicationTypeUuid) {
return communicationTypeMapper.toCommunicationTypeDto(
communicationTypeRepository.findByUuid(communicationTypeUuid).orElseThrow(NotFoundException::new)
);
}
public List<CommunicationTypeDto> findCommunicationTypes() {
return communicationTypeRepository.findAll().stream()
.map(communicationTypeMapper::toCommunicationTypeDto)
.collect(Collectors.toList());
}
public CommunicationTypeDto insertCommunicationType(CommunicationTypeDto communicationTypeDto) {
RefCommunicationType communicationTypeToSave = communicationTypeMapper.toRefCommunicationType(communicationTypeDto);
communicationTypeToSave.setUuid(UUID.randomUUID());
RefCommunicationType savedCommunicationType = communicationTypeRepository.save(communicationTypeToSave);
return communicationTypeMapper.toCommunicationTypeDto(savedCommunicationType);
}
public CommunicationTypeDto updateCommunicationType(CommunicationTypeDto communicationTypeDto){
RefCommunicationType communicationTypeUpdated;
RefCommunicationType existingCommunicationType = communicationTypeRepository
.findByUuid(communicationTypeDto.getUuid())
.orElseThrow(() -> new NotFoundException("communication.type.uuid.not.existing"));
RefCommunicationType communicationTypeToSave = communicationTypeMapper.toRefCommunicationType(communicationTypeDto);
communicationTypeToSave.setId(existingCommunicationType.getId());
communicationTypeUpdated = communicationTypeRepository.save(communicationTypeToSave);
return communicationTypeMapper.toCommunicationTypeDto(communicationTypeUpdated);
}
public void removeCommunicationType(UUID uuid) {
RefCommunicationType existingCommunicationType = communicationTypeRepository.findByUuid(uuid)
.orElseThrow( () -> new NotFoundException("communication.type.uuid.not.existing"));
communicationTypeRepository.delete(existingCommunicationType);
}
}