blob: e38a7b6ef2f70679594fdbcbd1c93b6c2eeee564 [file] [log] [blame]
/*
*******************************************************************************
* Copyright (c) 2019 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*******************************************************************************
*/
package org.eclipse.openk.contactbasedata.service;
import lombok.extern.log4j.Log4j2;
import org.eclipse.openk.contactbasedata.exceptions.NotFoundException;
import org.eclipse.openk.contactbasedata.mapper.CommunicationTypeMapper;
import org.eclipse.openk.contactbasedata.model.RefCommunicationType;
import org.eclipse.openk.contactbasedata.repository.CommunicationTypeRepository;
import org.eclipse.openk.contactbasedata.viewmodel.CommunicationTypeDto;
import org.springframework.beans.factory.annotation.Autowired;
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());
}
@Transactional
public CommunicationTypeDto insertCommunicationType(CommunicationTypeDto communicationTypeDto) {
RefCommunicationType communicationTypeToSave = communicationTypeMapper.toRefCommunicationType(communicationTypeDto);
communicationTypeToSave.setUuid(UUID.randomUUID());
RefCommunicationType savedCommunicationType = communicationTypeRepository.save(communicationTypeToSave);
return communicationTypeMapper.toCommunicationTypeDto(savedCommunicationType);
}
@Transactional
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);
}
@Transactional
public void removeCommunicationType(UUID uuid) {
RefCommunicationType existingCommunicationType = communicationTypeRepository.findByUuid(uuid)
.orElseThrow( () -> new NotFoundException("communication.type.uuid.not.existing"));
communicationTypeRepository.delete(existingCommunicationType);
}
}