blob: 02b0d1ce1a5bde812afcf6202b5aa4a845f7e92d [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.constants.Constants;
import org.eclipse.openk.contactbasedata.exceptions.NotFoundException;
import org.eclipse.openk.contactbasedata.mapper.CompanyMapper;
import org.eclipse.openk.contactbasedata.mapper.ContactMapper;
import org.eclipse.openk.contactbasedata.model.TblCompany;
import org.eclipse.openk.contactbasedata.model.TblContact;
import org.eclipse.openk.contactbasedata.repository.CompanyRepository;
import org.eclipse.openk.contactbasedata.repository.ContactRepository;
import org.eclipse.openk.contactbasedata.repository.PersonTypeRepository;
import org.eclipse.openk.contactbasedata.repository.SalutationRepository;
import org.eclipse.openk.contactbasedata.viewmodel.CompanyDto;
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.UUID;
@Log4j2
@Service
public class CompanyService {
@Autowired
private CompanyRepository companyRepository;
@Autowired
private ContactRepository contactRepository;
@Autowired
private SalutationRepository salutationRepository;
@Autowired
private PersonTypeRepository personTypeRepository;
@Autowired
private CompanyMapper companyMapper;
@Autowired
private ContactMapper contactMapper;
@Autowired
private BaseContactService baseContactService;
public CompanyDto findCompany(UUID contactUuid) {
return companyMapper.toCompanyDto(
companyRepository.findByTblContactUuid(contactUuid).orElseThrow(NotFoundException::new)
);
}
public Page<CompanyDto> findCompanies(Pageable pageable) {
return companyRepository.findAll(pageable).map(companyMapper::toCompanyDto);
}
@Transactional
public CompanyDto insertCompany(CompanyDto companyDto) {
TblContact contactToSave = new TblContact();
contactToSave.setUuid(UUID.randomUUID());
contactToSave.setContactType(Constants.CONTACT_TYPE_COMPANY);
TblCompany companyToSave = companyMapper.toTblCompany(companyDto);
companyToSave.setContact(contactToSave);
contactRepository.save(companyToSave.getContact());
setFromCompanyDto( companyToSave, companyDto );
// Then save dependent Model-Object
return companyMapper.toCompanyDto(companyRepository.save(companyToSave));
}
@Transactional
public CompanyDto updateCompany(CompanyDto companyDto){
TblCompany companyUpdated;
//Interne Person holen
TblCompany existingCompany = companyRepository
.findByTblContactUuid(companyDto.getContactUuid())
.orElseThrow(() -> new NotFoundException("contact.uuid.not.existing"));
existingCompany.setCompanyName(companyDto.getCompanyName());
existingCompany.setCompanyType(companyDto.getCompanyType());
setFromCompanyDto( existingCompany, companyDto );
companyUpdated = companyRepository.save(existingCompany);
return companyMapper.toCompanyDto(companyUpdated);
}
private void setFromCompanyDto( TblCompany destTblCompany, CompanyDto sourceDto ) {
destTblCompany.getContact().setNote(sourceDto.getContactNote());
}
}