Commit 463fd3c4 authored by Matija Obreza's avatar Matija Obreza

Update FaoInstitute.uniqueAcceNumbs by checking that numbers are actually unique in the database

parent 9648bd26
......@@ -162,4 +162,7 @@ public interface AccessionRepository extends JpaRepository<Accession, Long>, Acc
@Query("update Accession accession set accession.doi = null where accession.doi is not null")
@Modifying
void removeDOIs();
@Query("select count(a.id) - count(distinct a.accessionNumber) from Accession a where a.institute.id = ?1")
public long countNonuniqueAccessionNumbers(long instituteId);
}
......@@ -34,6 +34,7 @@ import org.genesys2.server.exception.NotFoundElement;
import org.genesys2.server.model.impl.Country;
import org.genesys2.server.model.impl.FaoInstitute;
import org.genesys2.server.model.impl.FaoInstituteSetting;
import org.genesys2.server.persistence.AccessionRepository;
import org.genesys2.server.persistence.FaoInstituteRepository;
import org.genesys2.server.persistence.FaoInstituteSettingRepository;
import org.genesys2.server.persistence.GenesysLowlevelRepository;
......@@ -86,6 +87,9 @@ public class InstituteServiceImpl implements InstituteService {
@Autowired
private CustomAclService aclService;
@Autowired
private AccessionRepository accessionRepository;
@Override
public Page<FaoInstitute> list(Pageable pageable) {
return instituteRepository.listInstitutes(pageable);
......@@ -110,6 +114,7 @@ public class InstituteServiceImpl implements InstituteService {
copyValues(target, institute);
instituteSettingRepository.save(target.getSettings().values());
target.setUniqueAcceNumbs(0 == accessionRepository.countNonuniqueAccessionNumbers(target.getId()));
return instituteRepository.save(target);
}
......@@ -177,6 +182,11 @@ public class InstituteServiceImpl implements InstituteService {
@Transactional(readOnly = false)
@CacheEvict(value = { "statistics", HIBERNATE_CACHENAME }, allEntries = true)
public List<FaoInstitute> update(final @Valid Collection<FaoInstitute> institutes) {
institutes.forEach(institute -> {
if (institute.getId() != null) {
institute.setUniqueAcceNumbs(0 == accessionRepository.countNonuniqueAccessionNumbers(institute.getId()));
}
});
return instituteRepository.save(institutes);
}
......@@ -194,7 +204,7 @@ public class InstituteServiceImpl implements InstituteService {
public void setUniqueAcceNumbs(FaoInstitute faoInstitute, boolean uniqueAcceNumbs) {
final FaoInstitute inst = instituteRepository.findOne(faoInstitute.getId());
LOG.info("Setting 'uniqueAcceNumbs' to {} for {}", uniqueAcceNumbs, faoInstitute);
inst.setUniqueAcceNumbs(uniqueAcceNumbs);
inst.setUniqueAcceNumbs(0 == accessionRepository.countNonuniqueAccessionNumbers(inst.getId()));
instituteRepository.save(inst);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment