Skip to content

Commit

Permalink
35548 Allow index-refresh all records
Browse files Browse the repository at this point in the history
Updated dina-base to support refresh all.
  • Loading branch information
cgendreau committed Jan 20, 2025
1 parent 1b10643 commit 8ee6ca5
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 5 deletions.
8 changes: 7 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@

<asciidoctor-maven-plugin.version>2.1.0</asciidoctor-maven-plugin.version>
<asciidoctorj.diagram.version>2.0.2</asciidoctorj.diagram.version>
<dina-base-api.version>0.131</dina-base-api.version>
<dina-base-api.version>0.134</dina-base-api.version>

<postgresql.version>42.4.4</postgresql.version>

Expand Down Expand Up @@ -80,6 +80,12 @@
<groupId>io.github.aafc-bicoe</groupId>
<artifactId>dina-base-api</artifactId>
<version>${dina-base-api.version}</version>
<exclusions>
<exclusion>
<groupId>org.eclipse.persistence</groupId>
<artifactId>javax.persistence</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.github.aafc-bicoe</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ public interface ObjectStoreMetadataMapper
@Mapping(target = "acSubtype", ignore = true)
ObjectStoreMetadata toEntity(ObjectStoreMetadataDto dto, @Context Set<String> provided, @Context String scope);

@Mapping(target = "acSubtype", ignore = true)
void patchEntity(@MappingTarget ObjectStoreMetadata entity, ObjectStoreMetadataDto dto, @Context Set<String> provided, @Context String scope);

/**
* Default method to intercept the mapping and set the context to the relationship
* @param dto
Expand All @@ -53,7 +56,6 @@ default DerivativeDto toDto(Derivative dto, @Context Set<String> provided, @Cont
}

// Specific type mapping

default String objectSubtypeToString(ObjectSubtype objectSubtype) {
if (objectSubtype != null &&
objectSubtype.getDcType() != null &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,14 @@ public ResponseEntity<?> handlePost(@RequestBody EntityModel<IndexRefreshDto> in
}

try {
indexRefreshService.reindexDocument(indexRefreshDto.getId().toString(),
// refresh a single document
if (indexRefreshDto.getId() != null) {
indexRefreshService.reindexDocument(indexRefreshDto.getId().toString(),
indexRefreshDto.getDocType());
} else {
//refresh all documents of a certain type
indexRefreshService.reindexAll(indexRefreshDto.getDocType());
}
} catch (IllegalStateException isEx) {
return ResponseEntity.badRequest().build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import ca.gc.aafc.dina.dto.ExternalRelationDto;
import ca.gc.aafc.dina.dto.JsonApiDto;
import ca.gc.aafc.dina.dto.JsonApiExternalResource;
import ca.gc.aafc.dina.exception.ResourceNotFoundException;
import ca.gc.aafc.dina.repository.DinaRepositoryV2;
import ca.gc.aafc.dina.service.AuditService;
import ca.gc.aafc.dina.service.DinaService;
Expand Down Expand Up @@ -64,7 +65,8 @@ protected JsonApiExternalResource externalRelationDtoToJsonApiExternalResource(
}

@GetMapping(TMP_V2_TYPE + "/{id}")
public ResponseEntity<RepresentationModel<?>> handleFindOne(@PathVariable UUID id, HttpServletRequest req) {
public ResponseEntity<RepresentationModel<?>> handleFindOne(@PathVariable UUID id, HttpServletRequest req)
throws ResourceNotFoundException {
String queryString = decodeQueryString(req);

JsonApiDto<ObjectStoreMetadataDto> jsonApiDto = getOne(id, queryString);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package ca.gc.aafc.objectstore.api.service;

import java.util.Set;
import java.util.UUID;
import java.util.stream.Stream;

import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import ca.gc.aafc.dina.jpa.BaseDAO;
import ca.gc.aafc.dina.messaging.message.DocumentOperationNotification;
import ca.gc.aafc.dina.messaging.message.DocumentOperationType;
import ca.gc.aafc.dina.messaging.producer.DocumentOperationNotificationMessageProducer;
Expand All @@ -18,14 +22,19 @@
@ConditionalOnProperty(prefix = "dina.messaging", name = "isProducer", havingValue = "true")
public class IndexRefreshService {

private static final String METADATA_SQL = "SELECT uuid FROM ObjectStoreMetadata ORDER BY id";

private final DocumentOperationNotificationMessageProducer searchRabbitMQMessageProducer;
private final Set<String> supportedDocumentTypes;
private final BaseDAO baseDAO;

public IndexRefreshService(DocumentOperationNotificationMessageProducer searchRabbitMQMessageProducer) {
public IndexRefreshService(DocumentOperationNotificationMessageProducer searchRabbitMQMessageProducer,
BaseDAO baseDAO) {
this.searchRabbitMQMessageProducer = searchRabbitMQMessageProducer;

// supported document type
supportedDocumentTypes = Set.of(ObjectStoreMetadataDto.TYPENAME);
this.baseDAO = baseDAO;
}

public void reindexDocument(String docId, String type) {
Expand All @@ -40,4 +49,28 @@ public void reindexDocument(String docId, String type) {
.operationType(DocumentOperationType.UPDATE).build();
searchRabbitMQMessageProducer.send(don);
}

/**
* Usually the transaction boundaries are at the repository level but here we only need one for
* reindexAll
* @param type
*/
@Transactional(readOnly = true)
public void reindexAll(String type) {

if (!supportedDocumentTypes.contains(type)) {
throw new IllegalStateException("Unsupported document type");
}

Stream<UUID> objStream =
baseDAO.streamAllByQuery(UUID.class, METADATA_SQL, null);

objStream.forEach(uuid -> {
DocumentOperationNotification don = DocumentOperationNotification.builder()
.documentId(uuid.toString())
.documentType(type)
.operationType(DocumentOperationType.UPDATE).build();
searchRabbitMQMessageProducer.send(don);
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package ca.gc.aafc.objectstore.api.repository;

import org.junit.jupiter.api.Test;
import org.springframework.hateoas.EntityModel;

import ca.gc.aafc.dina.jpa.BaseDAO;
import ca.gc.aafc.dina.messaging.message.DocumentOperationNotification;
import ca.gc.aafc.dina.messaging.producer.DocumentOperationNotificationMessageProducer;
import ca.gc.aafc.dina.security.auth.DinaAdminCUDAuthorizationService;
import ca.gc.aafc.objectstore.api.BaseIntegrationTest;
import ca.gc.aafc.objectstore.api.dto.IndexRefreshDto;
import ca.gc.aafc.objectstore.api.dto.ObjectStoreMetadataDto;
import ca.gc.aafc.objectstore.api.entities.ObjectStoreMetadata;
import ca.gc.aafc.objectstore.api.entities.ObjectUpload;
import ca.gc.aafc.objectstore.api.service.IndexRefreshService;
import ca.gc.aafc.objectstore.api.testsupport.factories.ObjectStoreMetadataFactory;
import ca.gc.aafc.objectstore.api.testsupport.factories.ObjectUploadFactory;

import static org.junit.jupiter.api.Assertions.assertFalse;

import java.util.ArrayList;
import java.util.List;
import javax.inject.Inject;

public class IndexRefreshRepositoryIT extends BaseIntegrationTest {

@Inject
private BaseDAO baseDAO;

@Inject
private DinaAdminCUDAuthorizationService dinaAdminCUDAuthorizationService;

@Test
public void indexRefreshRepository_onRefreshAll_messageSent() {
// we are not using beans to avoid the RabbitMQ part
List<DocumentOperationNotification> messages = new ArrayList<>();
DocumentOperationNotificationMessageProducer messageProducer = messages::add;
IndexRefreshService service = new IndexRefreshService(messageProducer, baseDAO);
IndexRefreshRepository repo = new IndexRefreshRepository(dinaAdminCUDAuthorizationService, service);

ObjectUpload newObjectUpload = objectUploadService.create(ObjectUploadFactory.buildTestObjectUpload());
ObjectStoreMetadata objectStoreMetadata = ObjectStoreMetadataFactory
.newObjectStoreMetadata()
.fileExtension(null)
.acHashValue(null)
.fileIdentifier(newObjectUpload.getFileIdentifier())
.build();

objectStoreMetaDataService.create(objectStoreMetadata);

IndexRefreshDto dto = new IndexRefreshDto();
dto.setDocType(ObjectStoreMetadataDto.TYPENAME);
repo.handlePost(EntityModel.of(dto));

// we may get more than 1 message if the database includes records from other tests
assertFalse(messages.isEmpty());
}


}

0 comments on commit 8ee6ca5

Please sign in to comment.