Skip to content

Commit

Permalink
small refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
soimugeo committed Sep 23, 2024
1 parent 2d6c25d commit 3291829
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import edu.stanford.protege.webprotege.linearizationservice.uiHistoryConcern.changes.*;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.*;

@Service
public class NewRevisionsEventEmitterServiceImpl implements NewRevisionsEventEmitterService {
Expand All @@ -22,15 +22,15 @@ public NewRevisionsEventEmitterServiceImpl(EventDispatcher eventDispatcher,

@Override
public void emitNewRevisionsEvent(ProjectId projectId, List<EntityLinearizationHistory> entityLinearizationHistories) {
List<ProjectChangeForEntity> changeList = projectChangesManager.getProjectChangesForHistories(projectId, entityLinearizationHistories);
Set<ProjectChangeForEntity> changeList = projectChangesManager.getProjectChangesForHistories(projectId, entityLinearizationHistories);
NewLinearizationRevisionsEvent revisionsEvent = NewLinearizationRevisionsEvent.create(EventId.generate(), projectId, changeList);
eventDispatcher.dispatchEvent(revisionsEvent);
}

@Override
public void emitNewRevisionsEvent(ProjectId projectId, String whoficEntityIri, LinearizationRevision entityLinearizationRevision) {
ProjectChangeForEntity projectChange = projectChangesManager.getProjectChangesForRevision(projectId, whoficEntityIri, entityLinearizationRevision);
NewLinearizationRevisionsEvent revisionsEvent = NewLinearizationRevisionsEvent.create(EventId.generate(), projectId, List.of(projectChange));
NewLinearizationRevisionsEvent revisionsEvent = NewLinearizationRevisionsEvent.create(EventId.generate(), projectId, Set.of(projectChange));
eventDispatcher.dispatchEvent(revisionsEvent);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import edu.stanford.protege.webprotege.common.*;
import org.jetbrains.annotations.NotNull;

import java.util.List;
import java.util.*;

import static edu.stanford.protege.webprotege.linearizationservice.uiHistoryConcern.changes.NewLinearizationRevisionsEvent.CHANNEL;

Expand All @@ -13,14 +13,14 @@
public record NewLinearizationRevisionsEvent(
EventId eventId,
ProjectId projectId,
List<ProjectChangeForEntity> changeList
Set<ProjectChangeForEntity> changes
) implements ProjectEvent {
public final static String CHANNEL = "webprotege.events.projects.linearizations.NewLinearizationRevisionsEvent";

public static NewLinearizationRevisionsEvent create(EventId eventId,
ProjectId projectId,
List<ProjectChangeForEntity> changeList) {
return new NewLinearizationRevisionsEvent(eventId, projectId, changeList);
Set<ProjectChangeForEntity> changes) {
return new NewLinearizationRevisionsEvent(eventId, projectId, changes);
}

@NotNull
Expand All @@ -35,8 +35,8 @@ public EventId eventId() {
return eventId;
}

public List<ProjectChangeForEntity> getProjectChanges() {
return changeList;
public Set<ProjectChangeForEntity> getProjectChanges() {
return changes;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.stream.Stream;
import java.util.stream.*;

import static edu.stanford.protege.webprotege.linearizationservice.mappers.LinearizationEventMapper.groupEventsByViews;

Expand All @@ -39,17 +39,17 @@ public ProjectChangesManager(
}


public List<ProjectChangeForEntity> getProjectChangesForHistories(ProjectId projectId, List<EntityLinearizationHistory> entityLinearizationHistories) {
public Set<ProjectChangeForEntity> getProjectChangesForHistories(ProjectId projectId, List<EntityLinearizationHistory> entityLinearizationHistories) {
Map<String, String> entityIrisAndNames = new HashMap<>();
List<LinearizationRevisionWithEntity> linRevisions = entityLinearizationHistories.stream()
Set<LinearizationRevisionWithEntity> linRevisions = entityLinearizationHistories.stream()
.flatMap(history ->
history.getLinearizationRevisions()
.stream()
.map(revision -> new LinearizationRevisionWithEntity(revision, history.getWhoficEntityIri()))
)
.sorted(Comparator.comparing(LinearizationRevisionWithEntity::getRevision))
.peek(revisionWithEntity -> entityIrisAndNames.put(revisionWithEntity.getWhoficEntityIri(), revisionWithEntity.getWhoficEntityIri()))
.toList();
.collect(Collectors.toSet());

List<EntityNode> renderedEntitiesList = entityRendererManager.getRenderedEntities(entityIrisAndNames.keySet(), projectId);
var linearizationDefinitions = definitionRepository.getLinearizationDefinitions();
Expand All @@ -63,7 +63,7 @@ public List<ProjectChangeForEntity> getProjectChangesForHistories(ProjectId proj
entityTextOptional.ifPresent(browserText -> entityIrisAndNames.put(revisionWithEntity.getWhoficEntityIri(), browserText));
});

List<ProjectChangeForEntity> projectChangeForEntityList = linRevisions.stream()
Set<ProjectChangeForEntity> projectChangeForEntityList = linRevisions.stream()
.flatMap(revisionWithEntity -> {
ProjectChange projectChange = getProjectChangesForRevision(
revisionWithEntity.getRevision(),
Expand All @@ -78,7 +78,7 @@ public List<ProjectChangeForEntity> getProjectChangesForHistories(ProjectId proj
return Stream.of(projectChangeForEntity);
}
)
.toList();
.collect(Collectors.toSet());

return projectChangeForEntityList;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,13 @@ public void GIVEN_validHistories_WHEN_getProjectChangesForHistoriesCalled_THEN_r
when(entityRendererManager.getRenderedEntities(anySet(), eq(projectId))).thenReturn(Collections.emptyList());
when(definitionRepository.getLinearizationDefinitions()).thenReturn(Collections.emptyList());

List<ProjectChangeForEntity> result = projectChangesManager.getProjectChangesForHistories(projectId, entityHistories);
Set<ProjectChangeForEntity> result = projectChangesManager.getProjectChangesForHistories(projectId, entityHistories);

assertNotNull(result);
assertEquals(1, result.size());
Iterator<ProjectChangeForEntity> iterator = result.iterator();

ProjectChange projectChange = result.get(0).projectChange();
ProjectChange projectChange = iterator.next().projectChange();
assertNotNull(projectChange);
assertEquals(12345L, projectChange.getTimestamp());
assertEquals("user1", projectChange.getAuthor().id());
Expand All @@ -78,7 +79,7 @@ public void GIVEN_emptyHistories_WHEN_getProjectChangesForHistoriesCalled_THEN_r
ProjectId projectId = new ProjectId("testProject");
List<EntityLinearizationHistory> emptyHistories = Collections.emptyList();

List<ProjectChangeForEntity> result = projectChangesManager.getProjectChangesForHistories(projectId, emptyHistories);
Set<ProjectChangeForEntity> result = projectChangesManager.getProjectChangesForHistories(projectId, emptyHistories);

assertNotNull(result);
assertTrue(result.isEmpty());
Expand Down

0 comments on commit 3291829

Please sign in to comment.