Skip to content

Commit

Permalink
[Enhancement kbss-cvut/termit-ui#520] Refactor ChangeRecordDao public…
Browse files Browse the repository at this point in the history
… interface and add documentation.
  • Loading branch information
lukaskabc committed Nov 14, 2024
1 parent e572355 commit a657a98
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
import cz.cvut.kbss.termit.persistence.context.DescriptorFactory;
import cz.cvut.kbss.termit.persistence.context.VocabularyContextMapper;
import cz.cvut.kbss.termit.persistence.dao.changetracking.ChangeRecordDao;
import cz.cvut.kbss.termit.persistence.dao.changetracking.ChangeTrackingContextResolver;
import cz.cvut.kbss.termit.persistence.snapshot.AssetSnapshotLoader;
import cz.cvut.kbss.termit.persistence.validation.VocabularyContentValidator;
import cz.cvut.kbss.termit.service.snapshot.SnapshotProvider;
Expand Down Expand Up @@ -90,7 +89,6 @@ public class VocabularyDao extends BaseAssetDao<Vocabulary>
"} GROUP BY ?date HAVING (?cnt > 0) ORDER BY ?date";

private static final String REMOVE_GLOSSARY_TERMS_QUERY_FILE = "remove/removeGlossaryTerms.ru";
private final ChangeTrackingContextResolver changeTrackingContextResolver;
private final ChangeRecordDao changeRecordDao;

private volatile long lastModified;
Expand All @@ -102,12 +100,11 @@ public class VocabularyDao extends BaseAssetDao<Vocabulary>
@Autowired
public VocabularyDao(EntityManager em, Configuration config, DescriptorFactory descriptorFactory,
VocabularyContextMapper contextMapper, ApplicationContext context,
ChangeTrackingContextResolver changeTrackingContextResolver, ChangeRecordDao changeRecordDao) {
ChangeRecordDao changeRecordDao) {
super(Vocabulary.class, em, config.getPersistence(), descriptorFactory);
this.contextMapper = contextMapper;
refreshLastModified();
this.context = context;
this.changeTrackingContextResolver = changeTrackingContextResolver;
this.changeRecordDao = changeRecordDao;
}

Expand Down Expand Up @@ -411,13 +408,7 @@ public List<AggregatedChangeInfo> getChangesOfContent(Vocabulary vocabulary) {
*/
public List<AbstractChangeRecord> getDetailedHistoryOfContent(Vocabulary vocabulary, ChangeRecordFilterDto filter, Pageable pageReq) {
Objects.requireNonNull(vocabulary);
return changeRecordDao.findAllFiltered(
changeTrackingContextResolver.resolveChangeTrackingContext(vocabulary),
filter,
Optional.empty(),
Optional.of(URI.create(SKOS.CONCEPT)), // term
pageReq
);
return changeRecordDao.findAllRelatedToType(vocabulary, filter, URI.create(SKOS.CONCEPT), pageReq);
}

private Query createContentChangesQuery(Vocabulary vocabulary) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,29 +83,49 @@ public List<AbstractChangeRecord> findAll(Asset<?> asset) {
return findAll(asset, new ChangeRecordFilterDto());
}

private Optional<URI> resolveChangeTrackingContext(Asset<?> asset) {
try {
return Optional.of(contextResolver.resolveChangeTrackingContext(asset));
} catch (NoResultException e) {
return Optional.empty();
}
}

/**
* Finds all change records related to the specified asset matching the filter.
*
* @param asset
* @param filterDto
* @return
* @param asset the asset
* @param filterDto filter parameters
*/
public List<AbstractChangeRecord> findAll(Asset<?> asset, ChangeRecordFilterDto filterDto) {
URI changeTrackingContext = null;
try {
changeTrackingContext = contextResolver.resolveChangeTrackingContext(asset);
} catch (NoResultException e) {
return List.of();
}
return findAllFiltered(changeTrackingContext, filterDto, Optional.of(asset), Optional.empty(), Pageable.unpaged());
return resolveChangeTrackingContext(asset).map(context ->
findAllFiltered(context, filterDto, Optional.of(asset), Optional.empty(), Pageable.unpaged()))
.orElseGet(List::of);
}

/**
* Finds all records from change context resolved from {@code changeContextAsset}
* that are matching the filter and are related to an entity of the type {@code relatedEntityType}.
*/
public List<AbstractChangeRecord> findAllRelatedToType(Asset<?> changeContextAsset, ChangeRecordFilterDto filterDto, URI relatedEntityType, Pageable pageable) {
return resolveChangeTrackingContext(changeContextAsset).map(context ->
findAllFiltered(context,
filterDto,
Optional.empty(),
Optional.ofNullable(relatedEntityType),
pageable
)).orElseGet(List::of);
}

/**
* Finds all change records matching the filter.
*
* @param changeContext the context of change records
* @param filter filter parameters
* @param asset if present, only changes of the asset will be returned
* @param assetType if present, only changes related to this asset type will be returned.
* @param assetType if present, only changes related to an asset of this type will be returned.
*/
public List<AbstractChangeRecord> findAllFiltered(URI changeContext, ChangeRecordFilterDto filter, Optional<Asset<?>> asset, Optional<URI> assetType, Pageable pageable) {
private List<AbstractChangeRecord> findAllFiltered(URI changeContext, ChangeRecordFilterDto filter, Optional<Asset<?>> asset, Optional<URI> assetType, Pageable pageable) {
TypedQuery<AbstractChangeRecord> query = em.createNativeQuery("""
SELECT DISTINCT ?record WHERE {
""" + /* Select anything from change context */ """
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,6 @@ class VocabularyDaoTest extends BaseDaoTestRunner {
@Autowired
private VocabularyDao sut;

@Autowired
private TermDao termDao;

@SpyBean
private ChangeRecordDao changeRecordDao;

Expand Down Expand Up @@ -952,17 +949,17 @@ void getAnyExternalRelationsReturnsTermsWithBothRelations(URI termRelation) {
void getDetailedHistoryOfContentCallsChangeRecordDaoWithFilter() {
final Vocabulary vocabulary = Generator.generateVocabularyWithId();
final List<AbstractChangeRecord> records = List.of();
final Optional<URI> skosConcept = Optional.of(URI.create(SKOS.CONCEPT));
final URI skosConcept = URI.create(SKOS.CONCEPT);
final Pageable unpaged = Pageable.unpaged();
final ChangeRecordFilterDto filterDto = new ChangeRecordFilterDto();
filterDto.setAuthorName("Name of the author");

doReturn(vocabulary.getUri()).when(changeTrackingContextResolver).resolveChangeTrackingContext(vocabulary);
doReturn(records).when(changeRecordDao).findAllFiltered(vocabulary.getUri(), filterDto, Optional.empty(), skosConcept, unpaged);
doReturn(records).when(changeRecordDao).findAllRelatedToType(vocabulary, filterDto, skosConcept, unpaged);

sut.getDetailedHistoryOfContent(vocabulary, filterDto, unpaged);

verify(changeTrackingContextResolver).resolveChangeTrackingContext(vocabulary);
verify(changeRecordDao).findAllFiltered(vocabulary.getUri(), filterDto, Optional.empty(), skosConcept, unpaged);
verify(changeRecordDao).findAllRelatedToType(vocabulary, filterDto, skosConcept, unpaged);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@

@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
class ChangeRecordDaoTest extends BaseDaoTestRunner {
private static final URI SKOS_CONCEPT = URI.create(SKOS.CONCEPT);

@Autowired
private ChangeTrackingContextResolver contextResolver;
Expand Down Expand Up @@ -286,6 +287,11 @@ void getAuthorsRetrievesUsersAssociatedWithPersistChangeRecordsOfSpecifiedAsset(
assertEquals(Collections.singleton(author), result);
}

@Test
void voidFindAllReturnsChangeRecordsWithoutVocabularyChanges() {

}

@Test
void findAllFilteredReturnsRecordsOfExistingTermFilteredByTermName() {
enableRdfsInference(em);
Expand All @@ -306,6 +312,7 @@ void findAllFilteredReturnsRecordsOfExistingTermFilteredByTermName() {
final List<AbstractChangeRecord> secondChanges = Generator.generateChangeRecords(secondTerm, author);
final List<AbstractChangeRecord> thirdChanges = Generator.generateChangeRecords(thirdTerm, author);

final Descriptor changeContextDescriptor = persistDescriptor(contextResolver.resolveChangeTrackingContext(vocabulary));
final Descriptor vocabularyDescriptor = persistDescriptor(vocabulary.getUri());

transactional(() -> {
Expand All @@ -319,7 +326,7 @@ void findAllFilteredReturnsRecordsOfExistingTermFilteredByTermName() {

Stream.of(firstChanges, secondChanges, thirdChanges)
.flatMap(Collection::stream)
.forEach(r -> em.persist(r, vocabularyDescriptor));
.forEach(r -> em.persist(r, changeContextDescriptor));
});

final ChangeRecordFilterDto filter = new ChangeRecordFilterDto();
Expand All @@ -328,7 +335,7 @@ void findAllFilteredReturnsRecordsOfExistingTermFilteredByTermName() {
final int recordsCount = firstChanges.size() + secondChanges.size();
final Pageable pageable = Pageable.ofSize(recordsCount * 2);

final List<AbstractChangeRecord> contentChanges = sut.findAllFiltered(vocabulary.getUri(), filter, Optional.empty(), Optional.of(URI.create(SKOS.CONCEPT)), pageable);
final List<AbstractChangeRecord> contentChanges = sut.findAllRelatedToType(vocabulary, filter, SKOS_CONCEPT, pageable);

assertEquals(recordsCount, contentChanges.size());
final long persistCount = contentChanges.stream().filter(ch -> ch instanceof PersistChangeRecord).count();
Expand Down Expand Up @@ -364,6 +371,7 @@ void findAllFilteredReturnsRecordsOfDeletedTermFilteredByTermName() {
deleteChangeRecord.setAuthor(author);
deleteChangeRecord.setLabel(termToRemove.getLabel());

final Descriptor changeContextDescriptor = persistDescriptor(contextResolver.resolveChangeTrackingContext(vocabulary));
final Descriptor vocabularyDescriptor = persistDescriptor(vocabulary.getUri());

transactional(() -> {
Expand All @@ -374,7 +382,7 @@ void findAllFilteredReturnsRecordsOfDeletedTermFilteredByTermName() {

Stream.of(firstChanges, termToRemoveChanges, List.of(deleteChangeRecord))
.flatMap(Collection::stream)
.forEach(r -> em.persist(r, vocabularyDescriptor));
.forEach(r -> em.persist(r, changeContextDescriptor));
});

final ChangeRecordFilterDto filter = new ChangeRecordFilterDto();
Expand All @@ -383,7 +391,7 @@ void findAllFilteredReturnsRecordsOfDeletedTermFilteredByTermName() {
final int recordsCount = termToRemoveChanges.size() + 1; // +1 for the delete record
final Pageable pageable = Pageable.unpaged();

final List<AbstractChangeRecord> contentChanges = sut.findAllFiltered(vocabulary.getUri(), filter, Optional.empty(), Optional.of(URI.create(SKOS.CONCEPT)), pageable);
final List<AbstractChangeRecord> contentChanges = sut.findAllRelatedToType(vocabulary, filter, SKOS_CONCEPT, pageable);

assertEquals(recordsCount, contentChanges.size());
final long persistCount = contentChanges.stream().filter(ch -> ch instanceof PersistChangeRecord).count();
Expand Down Expand Up @@ -413,6 +421,7 @@ void findAllFilteredReturnsRecordsOfExistingTermFilteredByChangedAttributeName()
final URI anotherChangedAttribute = URI.create(RDFS.LABEL);
final String changedAttributeName = "definition";

final Descriptor changeContextDescriptor = persistDescriptor(contextResolver.resolveChangeTrackingContext(vocabulary));
final Descriptor vocabularyDescriptor = persistDescriptor(vocabulary.getUri());

Stream.of(firstChanges, secondChanges).flatMap(Collection::stream)
Expand All @@ -435,15 +444,15 @@ void findAllFilteredReturnsRecordsOfExistingTermFilteredByChangedAttributeName()

Stream.of(firstChanges, secondChanges)
.flatMap(Collection::stream)
.forEach(r -> em.persist(r, vocabularyDescriptor));
.forEach(r -> em.persist(r, changeContextDescriptor));
});

final ChangeRecordFilterDto filter = new ChangeRecordFilterDto();
filter.setChangedAttributeName(changedAttributeName);

final Pageable pageable = Pageable.unpaged();

final List<AbstractChangeRecord> contentChanges = sut.findAllFiltered(vocabulary.getUri(), filter, Optional.empty(), Optional.of(URI.create(SKOS.CONCEPT)), pageable);
final List<AbstractChangeRecord> contentChanges = sut.findAllRelatedToType(vocabulary, filter, SKOS_CONCEPT, pageable);

assertEquals(recordCount.get(), contentChanges.size());
final long persistCount = contentChanges.stream().filter(ch -> ch instanceof PersistChangeRecord).count();
Expand Down Expand Up @@ -477,6 +486,7 @@ void findAllFilteredReturnsRecordsOfExistingTermFilteredByAuthorName() {
firstChanges.add(Generator.generateUpdateChange(firstTerm));
secondChanges.add(Generator.generateUpdateChange(secondTerm));

final Descriptor changeContextDescriptor = persistDescriptor(contextResolver.resolveChangeTrackingContext(vocabulary));
final Descriptor vocabularyDescriptor = persistDescriptor(vocabulary.getUri());

transactional(() -> {
Expand All @@ -487,7 +497,7 @@ void findAllFilteredReturnsRecordsOfExistingTermFilteredByAuthorName() {

Stream.of(firstChanges, secondChanges)
.flatMap(Collection::stream)
.forEach(r -> em.persist(r, vocabularyDescriptor));
.forEach(r -> em.persist(r, changeContextDescriptor));
});

final ChangeRecordFilterDto filter = new ChangeRecordFilterDto();
Expand All @@ -496,7 +506,7 @@ void findAllFilteredReturnsRecordsOfExistingTermFilteredByAuthorName() {

final Pageable pageable = Pageable.unpaged();

final List<AbstractChangeRecord> contentChanges = sut.findAllFiltered(vocabulary.getUri(), filter, Optional.empty(), Optional.of(URI.create(SKOS.CONCEPT)), pageable);
final List<AbstractChangeRecord> contentChanges = sut.findAllRelatedToType(vocabulary, filter, SKOS_CONCEPT, pageable);

assertEquals(recordCount, contentChanges.size());
final long persistCount = contentChanges.stream().filter(ch -> ch instanceof PersistChangeRecord).count();
Expand Down Expand Up @@ -531,6 +541,7 @@ void findAllFilteredReturnsRecordsOfExistingTermFilteredByChangeType(Class<? ext

final int recordCount = (int) Stream.of(firstChanges, secondChanges, List.of(deleteChangeRecord)).flatMap(List::stream).filter(typeClass::isInstance).count();

final Descriptor changeContextDescriptor = persistDescriptor(contextResolver.resolveChangeTrackingContext(vocabulary));
final Descriptor vocabularyDescriptor = persistDescriptor(vocabulary.getUri());

transactional(() -> {
Expand All @@ -541,7 +552,7 @@ void findAllFilteredReturnsRecordsOfExistingTermFilteredByChangeType(Class<? ext

Stream.of(firstChanges, secondChanges, List.of(deleteChangeRecord))
.flatMap(Collection::stream)
.forEach(r -> em.persist(r, vocabularyDescriptor));
.forEach(r -> em.persist(r, changeContextDescriptor));
});

final ChangeRecordFilterDto filter = new ChangeRecordFilterDto();
Expand All @@ -550,7 +561,7 @@ void findAllFilteredReturnsRecordsOfExistingTermFilteredByChangeType(Class<? ext

final Pageable pageable = Pageable.unpaged();

final List<AbstractChangeRecord> contentChanges = sut.findAllFiltered(vocabulary.getUri(), filter, Optional.empty(), Optional.of(URI.create(SKOS.CONCEPT)), pageable);
final List<AbstractChangeRecord> contentChanges = sut.findAllRelatedToType(vocabulary, filter, SKOS_CONCEPT, pageable);

assertEquals(recordCount, contentChanges.size());
assertTrue(contentChanges.stream().allMatch(typeClass::isInstance));
Expand Down

0 comments on commit a657a98

Please sign in to comment.