Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add routine to disconnect all invisible children of subjects. #279

Merged
merged 6 commits into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/main/java/no/ndla/taxonomy/repositories/NodeRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,18 @@ List<Integer> findIdsFiltered(
""")
List<Node> findProgrammes();

@Query(
"""
SELECT DISTINCT n FROM Node n
LEFT JOIN FETCH n.resourceResourceTypes rrt
LEFT JOIN FETCH rrt.resourceType
LEFT JOIN FETCH n.parentConnections pc
LEFT JOIN FETCH n.childConnections cc
WHERE n.nodeType = "SUBJECT"
AND n.context = true
""")
List<Node> findRootSubjects();

@Query(
"""
SELECT DISTINCT n FROM Node n
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ public class VersionPostPut implements UpdatableDto<Version> {
@JsonProperty
@Schema(
description =
"If specified, set the id to this value. Must start with urn:subject: and be a valid URI. If omitted, an id will be assigned automatically.",
example = "urn:subject:1")
"If specified, set the id to this value. Must start with urn:version: and be a valid URI. If ommitted, an id will be assigned automatically.",
example = "urn:version:1")
public Optional<URI> id = Optional.empty();

@JsonProperty
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/no/ndla/taxonomy/service/NodeService.java
Original file line number Diff line number Diff line change
Expand Up @@ -436,4 +436,24 @@ public List<TaxonomyContextDTO> getContextByContextId(Optional<String> contextId
.filter(c -> c.contextId().equals(contextId.get()))
.toList();
}

@Transactional
public Optional<DomainEntity> disconnectAllInvisibleNodes() {
nodeRepository.findRootSubjects().forEach(subject -> {
disconnectInvisibleConnections(subject);
nodeRepository.save(subject);
});
return Optional.empty();
}
gunnarvelle marked this conversation as resolved.
Show resolved Hide resolved

private Node disconnectInvisibleConnections(Node node) {
if (!node.isVisible()) {
node.getParentConnections().forEach(nodeConnectionRepository::delete);
} else {
node.getChildConnections()
.forEach(nodeConnection ->
nodeConnection.getChild().ifPresent(this::disconnectInvisibleConnections));
}
return node;
}
}
30 changes: 27 additions & 3 deletions src/main/java/no/ndla/taxonomy/service/VersionService.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,18 @@
import java.time.Instant;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.stream.Collectors;
import no.ndla.taxonomy.domain.DomainEntity;
import no.ndla.taxonomy.domain.Version;
import no.ndla.taxonomy.domain.VersionType;
import no.ndla.taxonomy.repositories.VersionRepository;
import no.ndla.taxonomy.rest.v1.commands.VersionPostPut;
import no.ndla.taxonomy.service.dtos.VersionDTO;
import no.ndla.taxonomy.service.exceptions.NotFoundServiceException;
import no.ndla.taxonomy.service.task.Deleter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
Expand All @@ -30,16 +35,19 @@
@Service
public class VersionService {
final Logger logger = LoggerFactory.getLogger(this.getClass());
private final VersionRepository versionRepository;
private final EntityManager entityManager;
private final VersionRepository versionRepository;
private final NodeService nodeService;
private final URNValidator validator = new URNValidator();
private final ExecutorService executor = Executors.newSingleThreadExecutor();

@Value("${spring.datasource.hikari.schema:taxonomy_api}")
private String defaultSchema;

public VersionService(VersionRepository versionRepository, EntityManager entityManager) {
this.versionRepository = versionRepository;
public VersionService(EntityManager entityManager, VersionRepository versionRepository, NodeService nodeService) {
this.entityManager = entityManager;
this.versionRepository = versionRepository;
this.nodeService = nodeService;
}

@Transactional
Expand Down Expand Up @@ -83,6 +91,22 @@ public void publishBetaAndArchiveCurrent(URI id) {
beta.setLocked(true);
beta.setPublished(Instant.now());
versionRepository.save(beta);

disconnectAllInvisibleNodes(beta.getHash());
}

private void disconnectAllInvisibleNodes(String hash) {
// Use a task to run in a separate thread against a specified schema
try {
Deleter deleter = new Deleter();
deleter.setNodeService(nodeService);
deleter.setVersion(schemaFromHash(hash));
Future<DomainEntity> future = executor.submit(deleter);
future.get();
} catch (Exception e) {
logger.info(e.getMessage(), e);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Jeg husker vi snakka om det, men jeg husker ikke helt hva det var igjen. Hvorfor burde ikke denne være logger.error?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Det kunne den fint vore, men tenkte berre at det ikkje gav så masse ekstra verdi. Vil sannsynligvis ikkje skje uansett.

// throw new NotFoundServiceException("Failed to disconnect invisible children in schema", e);
gunnarvelle marked this conversation as resolved.
Show resolved Hide resolved
}
}

@Transactional
Expand Down
25 changes: 25 additions & 0 deletions src/main/java/no/ndla/taxonomy/service/task/Deleter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Part of NDLA taxonomy-api
* Copyright (C) 2022 NDLA
*
* See LICENSE
*/

package no.ndla.taxonomy.service.task;

import java.util.Optional;
import no.ndla.taxonomy.domain.DomainEntity;
import no.ndla.taxonomy.service.NodeService;

public class Deleter extends Task<DomainEntity> {
private NodeService nodeService;

public void setNodeService(NodeService nodeService) {
this.nodeService = nodeService;
}

@Override
protected Optional<DomainEntity> execute() {
return nodeService.disconnectAllInvisibleNodes();
}
}
2 changes: 1 addition & 1 deletion src/test/java/no/ndla/taxonomy/rest/v1/RestTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
@SpringBootTest
@ActiveProfiles("junit")
@Transactional
public abstract class RestTest extends AbstractIntegrationTest {
public class RestTest extends AbstractIntegrationTest {

@Autowired
EntityManager entityManager;
Expand Down
13 changes: 10 additions & 3 deletions src/test/java/no/ndla/taxonomy/rest/v1/VersionsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -179,10 +179,17 @@ public void can_update_version() throws Exception {

@Test
public void can_publish_version() throws Exception {
Version version = builder.version(); // BETA
testUtils.updateResource("/v1/versions/" + version.getPublicId() + "/publish", null);
var versionUri = URI.create("urn:version:beta");
final var createVersionCommand = new VersionPostPut() {
{
id = Optional.of(versionUri);
name = "Beta";
}
};
testUtils.createResource("/v1/versions", createVersionCommand);
testUtils.updateResource("/v1/versions/" + versionUri + "/publish", null);

Version updated = versionRepository.getByPublicId(version.getPublicId());
Version updated = versionRepository.getByPublicId(versionUri);
assertEquals(VersionType.PUBLISHED, updated.getVersionType());
assertTrue(updated.isLocked());
assertNotNull(updated.getPublished());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,15 @@
import org.springframework.test.context.DynamicPropertyRegistry;
import org.springframework.test.context.DynamicPropertySource;
import org.testcontainers.containers.PostgreSQLContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;

@Testcontainers
@DirtiesContext
public class AbstractIntegrationTest {

static final PostgreSQLContainer<?> postgresDB;

static {
postgresDB = new PostgreSQLContainer<>("postgres:13.12");
postgresDB.start();
}
@Container
private static final PostgreSQLContainer<?> postgresDB = new PostgreSQLContainer<>("postgres:13.15");

@Autowired
EntityManager entityManager;
Expand Down
Loading