Skip to content

Commit

Permalink
Contract for import rdf + refactoring (#408)
Browse files Browse the repository at this point in the history
* Contract for import rdf + refactoring

* Rename header variable from accept to content type

* Fix wrong return method
  • Loading branch information
torleifg authored Jan 14, 2025
1 parent f0825c7 commit 21e11e1
Show file tree
Hide file tree
Showing 17 changed files with 454 additions and 339 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package no.fdk.concept_catalog.controller

import no.fdk.concept_catalog.elastic.ElasticUpdater
import no.fdk.concept_catalog.model.Begrep
import no.fdk.concept_catalog.model.JsonPatchOperation
import no.fdk.concept_catalog.model.Paginated
import no.fdk.concept_catalog.model.SearchOperation
import no.fdk.concept_catalog.model.Suggestion
import no.fdk.concept_catalog.model.*
import no.fdk.concept_catalog.rdf.jenaLangFromAcceptHeader
import no.fdk.concept_catalog.rdf.jenaLangFromContentTypeHeader
import no.fdk.concept_catalog.security.EndpointPermissions
import no.fdk.concept_catalog.service.ConceptService
import no.fdk.concept_catalog.service.statusFromString
Expand All @@ -28,7 +26,6 @@ class ConceptsController(
private val conceptService: ConceptService,
private val elasticUpdater: ElasticUpdater
) {

@PostMapping(
value = [""],
consumes = [MediaType.APPLICATION_JSON_VALUE]
Expand All @@ -42,6 +39,7 @@ class ConceptsController(
user == null -> ResponseEntity(HttpStatus.UNAUTHORIZED)
!endpointPermissions.hasOrgWritePermission(jwt, concept.ansvarligVirksomhet.id) ->
ResponseEntity(HttpStatus.FORBIDDEN)

else -> {
logger.info("creating concept for ${concept.ansvarligVirksomhet.id}")
conceptService.createConcept(concept, user, jwt).id
Expand All @@ -65,6 +63,7 @@ class ConceptsController(
user == null -> ResponseEntity(HttpStatus.UNAUTHORIZED)
concepts.any { !endpointPermissions.hasOrgAdminPermission(jwt, it.ansvarligVirksomhet.id) } ->
ResponseEntity(HttpStatus.FORBIDDEN)

else -> {
logger.info("creating ${concepts.size} concepts for ${concepts.firstOrNull()?.ansvarligVirksomhet?.id}")
conceptService.createConcepts(concepts, user, jwt)
Expand All @@ -73,6 +72,33 @@ class ConceptsController(
}
}

@PostMapping(
value = ["/{catalogId}/import"],
produces = [MediaType.APPLICATION_JSON_VALUE],
consumes = ["text/turtle", "text/n3", "application/rdf+json", "application/ld+json", "application/rdf+xml",
"application/n-triples", "application/n-quads", "application/trig", "application/trix"]
)
fun createBegreperFromRDF(
@AuthenticationPrincipal jwt: Jwt,
@RequestHeader(HttpHeaders.CONTENT_TYPE) contentType: String,
@PathVariable catalogId: String,
@RequestBody concepts: String
): ResponseEntity<Void> {
val user = endpointPermissions.getUser(jwt)

return when {
user == null -> ResponseEntity(HttpStatus.UNAUTHORIZED)
!endpointPermissions.hasOrgAdminPermission(jwt, catalogId) -> ResponseEntity(HttpStatus.FORBIDDEN)

else -> {
logger.info("Importing RDF concepts for $catalogId")
conceptService.createConcepts(concepts, jenaLangFromContentTypeHeader(contentType), user, jwt)

return ResponseEntity<Void>(HttpStatus.NOT_IMPLEMENTED)
}
}
}

@PostMapping(value = ["/{id}/revisjon"])
fun createRevision(
@AuthenticationPrincipal jwt: Jwt,
Expand All @@ -86,6 +112,7 @@ class ConceptsController(
concept == null -> ResponseEntity(HttpStatus.NOT_FOUND)
!endpointPermissions.hasOrgWritePermission(jwt, concept.ansvarligVirksomhet.id) ->
ResponseEntity(HttpStatus.FORBIDDEN)

!concept.erPublisert -> ResponseEntity(HttpStatus.BAD_REQUEST)
conceptService.findIdOfUnpublishedRevision(concept) != null -> ResponseEntity(HttpStatus.BAD_REQUEST)
else -> {
Expand Down Expand Up @@ -119,6 +146,7 @@ class ConceptsController(
concept == null -> ResponseEntity(HttpStatus.NOT_FOUND)
!endpointPermissions.hasOrgWritePermission(jwt, concept.ansvarligVirksomhet.id) ->
ResponseEntity(HttpStatus.FORBIDDEN)

concept.erPublisert -> ResponseEntity(HttpStatus.BAD_REQUEST)
else -> {
logger.info("deleting concept $id")
Expand Down Expand Up @@ -158,6 +186,7 @@ class ConceptsController(
concept == null -> ResponseEntity(HttpStatus.NOT_FOUND)
endpointPermissions.hasOrgReadPermission(jwt, concept.ansvarligVirksomhet.id) ->
ResponseEntity(concept, HttpStatus.OK)

else -> ResponseEntity(HttpStatus.FORBIDDEN)
}
}
Expand All @@ -172,6 +201,7 @@ class ConceptsController(
concept == null -> ResponseEntity(HttpStatus.NOT_FOUND)
endpointPermissions.hasOrgReadPermission(jwt, concept.ansvarligVirksomhet.id) ->
ResponseEntity(conceptService.findRevisions(concept), HttpStatus.OK)

else -> ResponseEntity(HttpStatus.FORBIDDEN)
}
}
Expand All @@ -187,6 +217,7 @@ class ConceptsController(
endpointPermissions.hasOrgWritePermission(jwt, concept.ansvarligVirksomhet.id) -> {
ResponseEntity(conceptService.publish(concept), HttpStatus.OK)
}

else -> ResponseEntity(HttpStatus.FORBIDDEN)
}
}
Expand All @@ -208,12 +239,14 @@ class ConceptsController(
user == null -> ResponseEntity(HttpStatus.UNAUTHORIZED)
!endpointPermissions.hasOrgWritePermission(jwt, concept.ansvarligVirksomhet.id) ->
ResponseEntity(HttpStatus.FORBIDDEN)

concept.erPublisert -> {
logger.info("creating revision of ${concept.id} for ${concept.ansvarligVirksomhet.id}")
conceptService.createRevisionOfConcept(patchOperations, concept, user, jwt).id
?.let { ResponseEntity(locationHeaderForCreated(newId = it), HttpStatus.CREATED) }
?: ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR)
}

else -> ResponseEntity(conceptService.updateConcept(concept, patchOperations, user, jwt), HttpStatus.OK)
}
}
Expand Down
5 changes: 4 additions & 1 deletion src/main/kotlin/no/fdk/concept_catalog/rdf/RDFUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ import org.springframework.web.client.HttpServerErrorException
import java.io.StringWriter

fun Model.rdfResponse(lang: Lang): String =
StringWriter().use{ out ->
StringWriter().use { out ->
write(out, lang.name)
out.toString()
}

fun jenaLangFromContentTypeHeader(contentType: String?): Lang =
jenaLangFromAcceptHeader(contentType)

fun jenaLangFromAcceptHeader(accept: String?): Lang =
when {
accept == null -> throw HttpServerErrorException(HttpStatus.NOT_ACCEPTABLE)
Expand Down
Loading

0 comments on commit 21e11e1

Please sign in to comment.