Skip to content

Commit

Permalink
TriplestoreServiceLive: try loading knora-ontologies of DefaultRdfDat…
Browse files Browse the repository at this point in the history
…a through resources
  • Loading branch information
siers authored and seakayone committed Feb 13, 2024
1 parent 8d308f8 commit 385b46a
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@

package org.knora.webapi.store.triplestore.defaults

import zio.Chunk
import zio.NonEmptyChunk

import org.knora.webapi.messages.store.triplestoremessages.RdfDataObject
import org.knora.webapi.store.triplestore.upgrade.RepositoryUpdatePlan

object DefaultRdfData {

Expand All @@ -18,27 +20,7 @@ object DefaultRdfData {
* then a list of `RdfDataObject` instances containing the path and the name of the named graph
* can be supplied to the `ResetTriplestoreContent` message.
*/
val data = NonEmptyChunk(
RdfDataObject(
path = "knora-ontologies/knora-admin.ttl",
name = "http://www.knora.org/ontology/knora-admin"
),
RdfDataObject(
path = "knora-ontologies/knora-base.ttl",
name = "http://www.knora.org/ontology/knora-base"
),
RdfDataObject(
path = "knora-ontologies/standoff-onto.ttl",
name = "http://www.knora.org/ontology/standoff"
),
RdfDataObject(
path = "knora-ontologies/standoff-data.ttl",
name = "http://www.knora.org/data/standoff"
),
RdfDataObject(
path = "knora-ontologies/salsah-gui.ttl",
name = "http://www.knora.org/ontology/salsah-gui"
),
val data: NonEmptyChunk[RdfDataObject] = NonEmptyChunk(
RdfDataObject(
path = "test_data/project_data/admin-data.ttl",
name = "http://www.knora.org/data/admin"
Expand Down Expand Up @@ -83,5 +65,5 @@ object DefaultRdfData {
path = "test_data/project_ontologies/webern-onto.ttl",
name = "http://www.knora.org/ontology/0806/webern"
)
)
) ++ Chunk(RepositoryUpdatePlan.builtInNamedGraphs.toSeq: _*)
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import org.apache.http.client.methods.HttpGet
import org.apache.http.client.methods.HttpPost
import org.apache.http.client.utils.URIBuilder
import org.apache.http.config.SocketConfig
import org.apache.http.entity.AbstractHttpEntity
import org.apache.http.entity.ContentType
import org.apache.http.entity.FileEntity
import org.apache.http.entity.StringEntity
Expand All @@ -42,6 +43,7 @@ import java.nio.file.Paths
import java.nio.file.StandardCopyOption
import java.time.temporal.ChronoUnit
import java.util
import scala.io.Source

import dsp.errors.*
import org.knora.webapi.*
Expand Down Expand Up @@ -224,21 +226,17 @@ case class TriplestoreServiceLive(
rdfDataObjects: List[RdfDataObject],
prependDefaults: Boolean
): Task[Unit] = {

val calculateCompleteRdfDataObjectList: Task[NonEmptyChunk[RdfDataObject]] =
if (prependDefaults) { // prepend
if (rdfDataObjects.isEmpty) {
ZIO.succeed(DefaultRdfData.data)
} else {
// prepend default data objects like those of knora-base, knora-admin, etc.
ZIO.succeed(DefaultRdfData.data ++ NonEmptyChunk.fromIterable(rdfDataObjects.head, rdfDataObjects.tail))
}
} else { // don't prepend
if (rdfDataObjects.isEmpty) {
ZIO.fail(BadRequestException("Cannot insert list with empty data into triplestore."))
} else {
ZIO.succeed(NonEmptyChunk.fromIterable(rdfDataObjects.head, rdfDataObjects.tail))
}
if (prependDefaults) {
ZIO.succeed(DefaultRdfData.data.append(Chunk(rdfDataObjects: _*)))
} else {
NonEmptyChunk
.fromIterableOption(rdfDataObjects)
.fold[Task[NonEmptyChunk[RdfDataObject]]](
ZIO.fail(BadRequestException("Cannot insert list with empty data into triplestore."))
)(
ZIO.succeed(_)
)
}

for {
Expand All @@ -261,19 +259,12 @@ case class TriplestoreServiceLive(
uriBuilder
}

inputEntity <- loadRdfObject(elem.path)

httpPost <-
ZIO.attemptBlocking {
val httpPost = new HttpPost(uriBuilder.build())
// Add the input file to the body of the request.
// here we need to tweak the base directory path from "webapi"
// to the parent folder where the files can be found
val inputFile = Paths.get("..", elem.path)
if (!Files.exists(inputFile)) {
throw BadRequestException(s"File ${inputFile.toAbsolutePath} does not exist")
}
val fileEntity =
new FileEntity(inputFile.toFile, ContentType.create(mimeTypeTextTurtle, "UTF-8"))
httpPost.setEntity(fileEntity)
httpPost.setEntity(inputEntity)
httpPost
}
responseHandler <- ZIO.attempt(returnInsertGraphDataResponse(graphName)(_))
Expand All @@ -284,6 +275,30 @@ case class TriplestoreServiceLive(
} yield ()
}

/**
* Load the RdfDataObject into some AbstractHttpEntity from either a relative ../$path or through a resource.
*/
private def loadRdfObject(path: String): Task[AbstractHttpEntity] = {
val turtleContentType = ContentType.create(mimeTypeTextTurtle, "UTF-8")

val relativeFileEntity = {
// Add the input file to the body of the request.
// here we need to tweak the base directory path from "webapi"
// to the parent folder where the files can be found
val inputFile = Paths.get("..", path)
ZIO.attemptBlocking {
if (!Files.exists(inputFile))
throw BadRequestException(s"File ${inputFile.toAbsolutePath} does not exist")
new FileEntity(inputFile.toFile, turtleContentType)
}
}

val resourceStringEntity =
ZIO.attemptBlocking(new StringEntity(Source.fromResource(path).mkString, turtleContentType))

relativeFileEntity.orElse(resourceStringEntity)
}

/**
* Checks the Fuseki triplestore if it is available and configured correctly. If it is not
* configured, tries to automatically configure (initialize) the required dataset.
Expand Down

0 comments on commit 385b46a

Please sign in to comment.