package be.mentoringsystems.merke.service.impl; import be.mentoringsystems.merke.helper.DTOHelper; import be.mentoringsystems.merke.model.QueryParams; import be.mentoringsystems.merke.model.db.Context; import be.mentoringsystems.merke.model.db.Login; import be.mentoringsystems.merke.model.db.Municipality; import be.mentoringsystems.merke.model.db.Vendor; import be.mentoringsystems.merke.model.db.Vendorstall; import be.mentoringsystems.merke.model.dto.PagedListDTO; import be.mentoringsystems.merke.model.dto.VendorDTO; import be.mentoringsystems.merke.persistence.VendorDAO; import be.mentoringsystems.merke.service.ContextService; import be.mentoringsystems.merke.service.LoginService; import be.mentoringsystems.merke.service.MunicipalityService; import be.mentoringsystems.merke.service.VendorService; import be.mentoringsystems.merke.service.VendorstallService; import java.util.ArrayList; import java.util.List; import java.util.UUID; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; /** * * @author anthonyarents */ @Service @Transactional(propagation = Propagation.SUPPORTS) public class VendorServiceImpl implements VendorService { @Autowired private transient VendorDAO vendorDAO; @Autowired private transient LoginService loginService; @Autowired private transient VendorstallService vendorstallService; @Autowired private transient MunicipalityService municipalityService; @Autowired private transient ContextService contextService; private static final Logger LOGGER = LogManager.getLogger(VendorServiceImpl.class); @Override @Transactional(propagation = Propagation.REQUIRED) public void save(final Vendor obj) { vendorDAO.save(obj); } @Override @Transactional(propagation = Propagation.REQUIRED) public Vendor save(final VendorDTO dto) { Vendor obj; if (dto.getId() == null) { obj = new Vendor(); LOGGER.info("Creating a new Vendor"); } else { if (dto.getMunicipalityIds() == null) { obj = getById(dto.getId()); } else { obj = vendorDAO.getByIdWithMunicipalities(dto.getId()); } LOGGER.info("Updating existing Vendor({})", dto.getId()); } if (obj != null) { DTOHelper.copyNonNull(dto, obj); if (dto.getMunicipalityIds() != null) { List lm = new ArrayList<>(); List municipalitiesWithContext = new ArrayList<>(); List municipalitiesWithoutContext = new ArrayList<>(); List subscribedContexts = obj.getContexts(); for (UUID mid : dto.getMunicipalityIds()) { lm.add(municipalityService.getById(mid)); } obj.getMunicipalities().retainAll(lm); lm.removeAll(obj.getMunicipalities()); if (!lm.isEmpty()) { lm.forEach((newlyAddedMunicipality) -> { if (newlyAddedMunicipality.getContextId() != null) { municipalitiesWithContext.add(newlyAddedMunicipality); } else { municipalitiesWithoutContext.add(newlyAddedMunicipality); } }); obj.getMunicipalities().addAll(lm); } if (!municipalitiesWithContext.isEmpty()) { municipalitiesWithContext.forEach((municipality) -> { Context cityInMerke = contextService.getById(municipality.getContextId()); if (!subscribedContexts.contains(cityInMerke)) { //TODO : ask user for confirmation first! //If user says no, add to seperate list so we prevent asking the same thing twice? subscribedContexts.add(cityInMerke); } }); } if (!municipalitiesWithoutContext.isEmpty()) { municipalitiesWithoutContext.forEach((municipality) -> { //TODO : send message to user which municipalities they want to subscribe to, then save those in a seperate list? }); } } //obj.setActive(true); save(obj); LOGGER.info("Vendor({}) saved", obj.getId()); LOGGER.info("Updating Login"); final Login login = loginService.getById(obj.getLoginId()); login.setEmail(obj.getEmail()); login.setFirstname(obj.getFirstname()); login.setLastname(obj.getLastname()); loginService.save(login); LOGGER.info("Login({}) saved", login.getId()); } return obj; } @Override @Transactional(propagation = Propagation.REQUIRED) public void delete(final Vendor obj) { final QueryParams params = new QueryParams(); params.addFilter("vendorId", obj.getId()); List stalls = vendorstallService.getAll(params); for (Vendorstall stall : stalls) { vendorstallService.delete(stall); } obj.setDeleted(true); save(obj); LOGGER.info("Vendor({}) deleted", obj.getId()); loginService.delete(obj.getLoginId()); } @Override @Transactional(propagation = Propagation.REQUIRED) public void delete(final UUID id) { final Vendor obj = getById(id); delete(obj); } @Override @Transactional(readOnly = true) public Vendor getById(final UUID id) { return vendorDAO.getById(id); } @Override @Transactional(readOnly = true) public Vendor getByLoginId(final UUID loginId) { QueryParams queryParams = new QueryParams(); queryParams.addFilter("loginId", loginId); List vendors = getAll(queryParams); if (vendors.isEmpty()) { return null; } else { return vendors.get(0); } } @Override @Transactional(propagation = Propagation.REQUIRED) public boolean addMunicipality(final UUID vendorId, final UUID municipalityId) { final Vendor vendor = getById(vendorId); final Municipality municipality = municipalityService.getById(municipalityId); if (!vendor.getMunicipalities().contains(municipality)) { vendor.getMunicipalities().add(municipality); save(vendor); } return true; } @Override @Transactional(propagation = Propagation.REQUIRED) public boolean removeMunicipality(final UUID vendorId, final UUID municipalityId) { final Vendor vendor = getById(vendorId); final Municipality municipality = municipalityService.getById(municipalityId); if (vendor.getMunicipalities().contains(municipality)) { vendor.getMunicipalities().remove(municipality); save(vendor); } return true; } @Override @Transactional(readOnly = true) public List getAll(final QueryParams queryParams) { return vendorDAO.getAll(queryParams); } @Override @Transactional(readOnly = true) public Long getCount(final QueryParams queryParams) { return vendorDAO.getCount(queryParams); } @Override @Transactional(readOnly = true) public PagedListDTO getAllDTO(final QueryParams queryParams) { queryParams.addFilter("contextId", loginService.getCurrentLogin().getContextId()); final PagedListDTO result = new PagedListDTO(); result.setData(getAll(queryParams)); result.setTotal(getCount(queryParams)); return result; } }