-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'EMC-73-big-ttl' into 'develop'
Factor out Turtle file generation and expose endpoint via WholeCatalogueTurtleController Closes EMC-73 See merge request eip/catalogue!571
- Loading branch information
Showing
7 changed files
with
320 additions
and
110 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
...src/main/java/uk/ac/ceh/gateway/catalogue/controllers/WholeCatalogueTurtleController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package uk.ac.ceh.gateway.catalogue.controllers; | ||
|
||
import lombok.SneakyThrows; | ||
import lombok.ToString; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.http.HttpEntity; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.stereotype.Controller; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import uk.ac.ceh.gateway.catalogue.services.DocumentsToTurtleService; | ||
|
||
@Slf4j | ||
@ToString | ||
@Controller | ||
public class WholeCatalogueTurtleController { | ||
|
||
private final DocumentsToTurtleService docsToTurtle; | ||
|
||
public WholeCatalogueTurtleController(DocumentsToTurtleService docsToTurtle) { | ||
this.docsToTurtle = docsToTurtle; | ||
} | ||
|
||
@GetMapping("{catalogueId}/catalogue.ttl") | ||
@SneakyThrows | ||
public HttpEntity<String> getTtl(@PathVariable String catalogueId) { | ||
return docsToTurtle.getBigTtl(catalogueId).map(ttl -> { | ||
log.info("serving big turtle for {}", catalogueId); | ||
return ResponseEntity.ok() | ||
.contentType(MediaType.valueOf("text/turtle")) | ||
.body(ttl); | ||
}).orElseGet(() -> { | ||
log.info("not serving big turtle for unknown catalogue {}", catalogueId); | ||
return ResponseEntity.notFound().build(); | ||
}); | ||
} | ||
} |
107 changes: 107 additions & 0 deletions
107
java/src/main/java/uk/ac/ceh/gateway/catalogue/services/CatalogueToTurtleService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package uk.ac.ceh.gateway.catalogue.services; | ||
|
||
import com.google.common.collect.ImmutableSet; | ||
import freemarker.template.Configuration; | ||
import lombok.SneakyThrows; | ||
import lombok.extern.slf4j.Slf4j; | ||
import lombok.val; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils; | ||
import uk.ac.ceh.gateway.catalogue.catalogue.Catalogue; | ||
import uk.ac.ceh.gateway.catalogue.catalogue.CatalogueService; | ||
import uk.ac.ceh.gateway.catalogue.model.MetadataDocument; | ||
import uk.ac.ceh.gateway.catalogue.repository.DocumentRepository; | ||
|
||
import java.util.*; | ||
import java.util.stream.Collectors; | ||
|
||
@Slf4j | ||
@Service | ||
public class CatalogueToTurtleService implements DocumentsToTurtleService { | ||
private static final Set<String> REQUIRED_TYPES = ImmutableSet.of( | ||
"service", | ||
"dataset", | ||
"signpost" | ||
); | ||
private final CatalogueService catalogueService; | ||
private final Configuration configuration; | ||
private final DocumentRepository documentRepository; | ||
private final MetadataListingService listing; | ||
private final String baseUri; | ||
|
||
public CatalogueToTurtleService( | ||
CatalogueService catalogueService, | ||
DocumentRepository documentRepository, | ||
Configuration configuration, | ||
MetadataListingService listing, | ||
@Value("${documents.baseUri}") String baseUri | ||
) { | ||
this.catalogueService = catalogueService; | ||
this.configuration = configuration; | ||
this.documentRepository = documentRepository; | ||
this.baseUri = baseUri; | ||
this.listing = listing; | ||
} | ||
|
||
@Override | ||
public Optional<String> getBigTtl(String catalogueId) { | ||
return Optional.ofNullable(catalogueService.retrieve(catalogueId)).map(catalogue -> { | ||
List<String> ids = getRequiredIds(catalogueId); | ||
String catalogueTtl = generateCatalogueTtl(getCatalogueModel(catalogue, ids)); | ||
List<String> recordsTtl = getRecordsTtl(ids); | ||
|
||
String bigTtl = catalogueTtl.concat(String.join("\n", recordsTtl)); | ||
log.debug("Big turtle to send: {}", bigTtl); | ||
return bigTtl; | ||
}); | ||
} | ||
|
||
private List<String> getRequiredIds(String catalogueId) { | ||
try { | ||
List<String> ids = listing.getPublicDocumentsOfCatalogue(catalogueId); | ||
|
||
return ids.stream() | ||
.map(this::getMetadataDocument) | ||
.filter(doc -> REQUIRED_TYPES.contains(doc.getType())) | ||
.map(MetadataDocument::getId) | ||
.collect(Collectors.toList()); | ||
} catch (NullPointerException e) { | ||
// no git commits | ||
return Collections.emptyList(); | ||
} | ||
} | ||
|
||
@SneakyThrows | ||
private String generateCatalogueTtl(Map<String, Object> model) { | ||
val freemarkerTemplate = configuration.getTemplate("rdf/catalogue.ttl.ftlh"); | ||
return FreeMarkerTemplateUtils.processTemplateIntoString(freemarkerTemplate, model); | ||
} | ||
|
||
private Map<String, Object> getCatalogueModel(Catalogue catalogue, List<String> ids) { | ||
Map<String, Object> model = new HashMap<>(); | ||
model.put("records", ids); | ||
model.put("catalogue", catalogue.getId()); | ||
model.put("title", catalogue.getTitle()); | ||
model.put("baseUri", baseUri); | ||
return model; | ||
} | ||
|
||
private List<String> getRecordsTtl(List<String> ids) { | ||
return ids.stream() | ||
.map(this::getMetadataDocument) | ||
.map(this::docToString) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
@SneakyThrows | ||
private MetadataDocument getMetadataDocument(String id) { | ||
return documentRepository.read(id); | ||
} | ||
|
||
@SneakyThrows | ||
private String docToString(MetadataDocument model) { | ||
val freemarkerTemplate = configuration.getTemplate("rdf/ttlUnprefixed.ftlh"); | ||
return FreeMarkerTemplateUtils.processTemplateIntoString(freemarkerTemplate, model); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
java/src/main/java/uk/ac/ceh/gateway/catalogue/services/DocumentsToTurtleService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package uk.ac.ceh.gateway.catalogue.services; | ||
|
||
import java.util.Optional; | ||
|
||
public interface DocumentsToTurtleService { | ||
Optional<String> getBigTtl(String catalogueId); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.