RED-6467: Implemented undeletion as operation on entities

This commit is contained in:
Viktor Seifert 2023-04-12 18:22:53 +02:00
parent 7a6a357d03
commit e72c68843c
2 changed files with 8 additions and 24 deletions

View File

@ -4,8 +4,6 @@ import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import javax.persistence.EntityManager;
import javax.persistence.criteria.CriteriaBuilder;
import javax.transaction.Transactional;
import org.springframework.stereotype.Service;
@ -35,8 +33,6 @@ public class EntryPersistenceService {
private final FalseRecommendationEntryRepository falseRecommendationEntryRepository;
private final JDBCWriteUtils jdbcWriteUtils;
private final EntityManager entityManager;
@Transactional
public void deleteEntries(String typeId, List<String> values, long version, DictionaryEntryType dictionaryEntryType) {
@ -163,24 +159,15 @@ public class EntryPersistenceService {
private List<String> undeleteEntries(String typeId, Set<String> entries, long version) {
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
var undeleteEntriesQuery = criteriaBuilder.createCriteriaUpdate(DictionaryEntryEntity.class);
var entryRoot = undeleteEntriesQuery.from(DictionaryEntryEntity.class);
List<DictionaryEntryEntity> entryEntities = entryRepository.findAllByTypeIdAndValueIn(typeId, entries);
entryEntities.forEach(e -> {
e.setDeleted(false);
e.setVersion(version);
});
var typeIdPred = criteriaBuilder.equal(entryRoot.get("typeId"), typeId);
var entriesPred = entryRoot.get("value").in(entries);
undeleteEntriesQuery.where(typeIdPred, entriesPred);
undeleteEntriesQuery.set("version", version);
undeleteEntriesQuery.set("deleted", false);
entryRepository.saveAll(entryEntities);
int updated = entityManager.createQuery(undeleteEntriesQuery).executeUpdate();
log.debug("Updated {} entries", updated);
var existingEntriesQuery = criteriaBuilder.createQuery(DictionaryEntryEntity.class);
entryRoot = existingEntriesQuery.from(DictionaryEntryEntity.class);
existingEntriesQuery.where(typeIdPred, criteriaBuilder.equal(entryRoot.get("deleted"), false));
return entityManager.createQuery(existingEntriesQuery).getResultStream().map(DictionaryEntryEntity::getValue).toList();
return entryEntities.stream().map(DictionaryEntryEntity::getValue).toList();
}

View File

@ -37,10 +37,7 @@ public interface EntryRepository extends JpaRepository<DictionaryEntryEntity, Lo
void deleteAllEntriesForTypeId(String typeId, long version);
@Modifying(flushAutomatically = true, clearAutomatically = true)
@Transactional
@Query(value = "update dictionary_entry set deleted = false, version = :version where type_id = :typeId and value in (:entries) returning value", nativeQuery = true)
List<String> undeleteEntries(String typeId, Set<String> entries, long version);
List<DictionaryEntryEntity> findAllByTypeIdAndValueIn(String typeId, Set<String> entries);
@Modifying(flushAutomatically = true, clearAutomatically = true)