RED-6467: Implemented undeletion as operation on entities
This commit is contained in:
parent
7a6a357d03
commit
e72c68843c
@ -4,8 +4,6 @@ import java.util.List;
|
|||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
import javax.persistence.EntityManager;
|
|
||||||
import javax.persistence.criteria.CriteriaBuilder;
|
|
||||||
import javax.transaction.Transactional;
|
import javax.transaction.Transactional;
|
||||||
|
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
@ -35,8 +33,6 @@ public class EntryPersistenceService {
|
|||||||
private final FalseRecommendationEntryRepository falseRecommendationEntryRepository;
|
private final FalseRecommendationEntryRepository falseRecommendationEntryRepository;
|
||||||
private final JDBCWriteUtils jdbcWriteUtils;
|
private final JDBCWriteUtils jdbcWriteUtils;
|
||||||
|
|
||||||
private final EntityManager entityManager;
|
|
||||||
|
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public void deleteEntries(String typeId, List<String> values, long version, DictionaryEntryType dictionaryEntryType) {
|
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) {
|
private List<String> undeleteEntries(String typeId, Set<String> entries, long version) {
|
||||||
|
|
||||||
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
|
List<DictionaryEntryEntity> entryEntities = entryRepository.findAllByTypeIdAndValueIn(typeId, entries);
|
||||||
var undeleteEntriesQuery = criteriaBuilder.createCriteriaUpdate(DictionaryEntryEntity.class);
|
entryEntities.forEach(e -> {
|
||||||
var entryRoot = undeleteEntriesQuery.from(DictionaryEntryEntity.class);
|
e.setDeleted(false);
|
||||||
|
e.setVersion(version);
|
||||||
|
});
|
||||||
|
|
||||||
var typeIdPred = criteriaBuilder.equal(entryRoot.get("typeId"), typeId);
|
entryRepository.saveAll(entryEntities);
|
||||||
var entriesPred = entryRoot.get("value").in(entries);
|
|
||||||
undeleteEntriesQuery.where(typeIdPred, entriesPred);
|
|
||||||
undeleteEntriesQuery.set("version", version);
|
|
||||||
undeleteEntriesQuery.set("deleted", false);
|
|
||||||
|
|
||||||
int updated = entityManager.createQuery(undeleteEntriesQuery).executeUpdate();
|
return entryEntities.stream().map(DictionaryEntryEntity::getValue).toList();
|
||||||
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();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -37,10 +37,7 @@ public interface EntryRepository extends JpaRepository<DictionaryEntryEntity, Lo
|
|||||||
void deleteAllEntriesForTypeId(String typeId, long version);
|
void deleteAllEntriesForTypeId(String typeId, long version);
|
||||||
|
|
||||||
|
|
||||||
@Modifying(flushAutomatically = true, clearAutomatically = true)
|
List<DictionaryEntryEntity> findAllByTypeIdAndValueIn(String typeId, Set<String> entries);
|
||||||
@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);
|
|
||||||
|
|
||||||
|
|
||||||
@Modifying(flushAutomatically = true, clearAutomatically = true)
|
@Modifying(flushAutomatically = true, clearAutomatically = true)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user