Skip to content
This repository has been archived by the owner on Dec 16, 2022. It is now read-only.

Commit

Permalink
#1731: Delete old taxonomies on upload
Browse files Browse the repository at this point in the history
  • Loading branch information
kdondziak committed Feb 18, 2021
1 parent 871629e commit 44443a5
Showing 1 changed file with 20 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
package org.benetech.servicenet.service.impl;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import javax.persistence.EntityManager;
import org.benetech.servicenet.domain.AbstractEntity;
import org.benetech.servicenet.domain.DataImportReport;
import org.benetech.servicenet.domain.Service;
import org.benetech.servicenet.service.ServiceBasedImportService;
import org.benetech.servicenet.service.ServiceImportService;
import org.benetech.servicenet.service.ServiceService;
import org.benetech.servicenet.service.ServiceTaxonomyService;
import org.benetech.servicenet.validator.EntityValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.persistence.EntityManager;
import java.util.Optional;

@Component
public class ServiceImportServiceImpl implements ServiceImportService {

Expand All @@ -24,17 +27,24 @@ public class ServiceImportServiceImpl implements ServiceImportService {
@Autowired
private ServiceBasedImportService serviceBasedImportService;

@Autowired
private ServiceTaxonomyService serviceTaxonomyService;

@Override
public Service createOrUpdateService(Service filledService, String externalDbId, String providerName,
DataImportReport report) {
EntityValidator.validateAndFix(filledService, filledService.getOrganization(), report, externalDbId);

Service service = new Service(filledService);
Optional<Service> serviceFromDb = serviceService.findWithEagerAssociations(externalDbId, providerName);
List<String> taxonomiesToKeep = filledService.getTaxonomies().stream()
.map(t -> t.getTaxonomy().getTaxonomyId())
.collect(Collectors.toList());
if (serviceFromDb.isPresent()) {
if (serviceFromDb.get().deepEquals(filledService)) {
return serviceFromDb.get();
}
deleteOldTaxonomies(serviceFromDb.get(), taxonomiesToKeep);
fillDataFromDb(service, serviceFromDb.get());
em.merge(service);
report.incrementNumberOfUpdatedServices();
Expand Down Expand Up @@ -62,6 +72,13 @@ public Service createOrUpdateService(Service filledService, String externalDbId,
return service;
}

private void deleteOldTaxonomies(Service serviceFromDb, List<String> taxonomiesToKeep) {
serviceFromDb.getTaxonomies().stream()
.filter(serviceTaxonomy -> !taxonomiesToKeep.contains(serviceTaxonomy.getTaxonomy().getTaxonomyId()))
.map(AbstractEntity::getId)
.forEach(uuid -> serviceTaxonomyService.delete(uuid));
}

private void fillDataFromDb(Service newService, Service serviceFromDb) {
newService.setPhones(serviceFromDb.getPhones());
newService.setEligibility(serviceFromDb.getEligibility());
Expand Down

0 comments on commit 44443a5

Please sign in to comment.