Commit 319476f2 authored by Günter Hipler's avatar Günter Hipler
Browse files

first steps implemenatation ListRecords

parent dadf706e
Pipeline #12689 passed with stage
in 2 minutes and 9 seconds
......@@ -4,7 +4,7 @@ import javax.inject.Inject
import modules.OaiRepository
import org.swissbib.memobase.oai.request.{OaiRequest, OaiRequestIllegalVerb}
import org.swissbib.memobase.oai.common.verb.OaiVerb
import org.swissbib.memobase.oai.common.verb.OaiVerb.{OaiVerb, WRONGVERB, getVerb}
import org.swissbib.memobase.oai.common.verb.OaiVerb.{OaiVerb, WRONGVERB}
import play.api.Configuration
import play.api.mvc.{AbstractController, Action, AnyContent, ControllerComponents, Rendering}
......
......@@ -4,7 +4,10 @@ import java.util
import com.typesafe.config.Config
import org.elasticsearch.action.get.GetRequest
import org.elasticsearch.action.search.{SearchRequest, SearchResponse}
import org.elasticsearch.client.{RequestOptions, RestHighLevelClient}
import org.elasticsearch.index.query.QueryBuilders
import org.elasticsearch.search.builder.SearchSourceBuilder
import org.swissbib.memobase.oai.common.util.ResumptionToken
import play.Environment
import play.api.libs.json.Json
......@@ -24,13 +27,25 @@ trait ElasticsearchComponent extends OaiRepository {
until: Option[String],
set: Option[String],
resumptionToken: Option[ResumptionToken],
metadataPrefix: String): Seq[Node] = ???
metadataPrefix: String): Option[Seq[Node]] = {
//todo - handling for different parameters
val searchSourceBuilder = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery())
//val searchSourceBuilder = new SearchSourceBuilder().query(QueryBuilders.matchQuery("bla","bla"))
val searchRequest = new SearchRequest().source(searchSourceBuilder).indices(index)
val searchResponse: SearchResponse = client.get.search(searchRequest, RequestOptions.DEFAULT)
val myMap = searchResponse.getHits.getHits.map(_.getSourceAsMap).map(
source => Json2XML.singleJsonDoc2Xml(
Json.parse(play.libs.Json.toJson(source).toString)))
Option(myMap.toSeq)
}
override def listIdentiers(from: Option[String],
until: Option[String],
set: Option[String],
resumptionToken: Option[ResumptionToken],
metadataPrefix: String): Seq[Node] = ???
metadataPrefix: String): Option[Seq[Node]] = ???
override def getRecord(identifier: String,
metadataPrefix: String): Option[Node] = {
......
......@@ -20,8 +20,20 @@ class KafkaModule(environment: Environment, configuration: Configuration)
//bind[SolrComponent].to[SolrComponentImpl].in[Singleton]
val t1 = environment
val t2 = configuration
bind[KafkaComponent].to[KafkaComponentImpl].in[Singleton]
//val t1 = environment
//val t2 = configuration
//bind[KafkaComponent].to[KafkaComponentImpl].in[Singleton]
}
}
/*
Fehler in 2.13.3
[info] Non-compiled module 'compiler-bridge_2.13' for Scala 2.13.3. Compiling...
[info] Compilation completed in 8.085s.
[error] /home/swissbib/environment/code/repositories/memoriav/gitlab/services/externalapis/oai/app/modules/KafkaModule.scala:25:51: Auto-application to `()` is deprecated. Supply the empty argument list `()` explicitly to invoke method in,
[error] or remove the empty argument list from its definition (Java-defined methods are exempt).
[error] In Scala 3, an unapplied method like this will be eta-expanded into a function.
[error] bind[KafkaComponent].to[KafkaComponentImpl].in[Singleton]
*/
......@@ -10,13 +10,13 @@ trait OaiRepository {
until: Option[String],
set: Option[String],
resumptionToken: Option[ResumptionToken],
metadataPrefix: String): Seq[Node]
metadataPrefix: String): Option[Seq[Node]]
def listIdentiers(from: Option[String],
until: Option[String],
set: Option[String],
resumptionToken: Option[ResumptionToken],
metadataPrefix: String): Seq[Node]
metadataPrefix: String): Option[Seq[Node]]
def getRecord(identifier: String,
metadataPrefix: String
......
......@@ -3,14 +3,26 @@ package org.swissbib.memobase.oai.response
import java.time.Instant
import com.typesafe.config.{ConfigObject, ConfigValue}
import org.swissbib.memobase.oai.common.util.OaiConfigSets
import org.swissbib.memobase.oai.runner.OaiRequestRunner
import scala.xml.{Elem, Node}
import scala.xml.{Elem, Node, Text}
import scala.xml.transform.{RewriteRule, RuleTransformer}
class OaiResponse(val oaiRequestRunner:OaiRequestRunner) {
def makeHeader (del:Boolean = false,
set: String = "memobaseSet",
datestamp: Instant = Instant.now(),
identifier: String):Elem =
<header status={if (del) Some(Text("deleted")) else None}>
<identifier>{identifier}</identifier>
<datestamp>{datestamp}</datestamp>
<setSpec>{set}</setSpec>
</header>
protected def createOaiFrame: Elem = {
/*
val oaiFrame =
......@@ -28,6 +40,7 @@ class OaiResponse(val oaiRequestRunner:OaiRequestRunner) {
{createRequestTag}
</OAI-PMH>
oaiFrame
}
......@@ -45,13 +58,32 @@ case class GetRecordResponse(override val oaiRequestRunner: OaiRequestRunner) ex
override def createRequestTag: Elem =
<request verb="GetRecord">{oaiRequestRunner.repository.oaiConfig.identify.baseUrl}</request>
override def createResponse: Seq[Node] = {
//todo create the body using the executed action in repository
createOaiFrame
val oaiFrame = createOaiFrame
val verbBody: RewriteRule = new RewriteRule {
override def transform(n: Node): Seq[Node] = n match {
case elem: Elem if elem.label == "request" =>
elem ++
<GetRecord>
<record>
{makeHeader(identifier = "4711")}
{if (oaiRequestRunner.result.nonEmpty)
<metadata>{oaiRequestRunner.result.get}</metadata>
else
<metadata/>
}
</record>
</GetRecord>
case n => n
}
}
new RuleTransformer(verbBody).transform(oaiFrame)
}
}
......@@ -86,14 +118,11 @@ case class IdentifyResponse(override val oaiRequestRunner: OaiRequestRunner) ext
<deletedRecord>{oaiRequestRunner.repository.oaiConfig.identify.deletedRecord}</deletedRecord>
<granularity>{oaiRequestRunner.repository.oaiConfig.identify.granularity}</granularity>
</Identify>
case n => n
}
}
new RuleTransformer(verbBody).transform(oaiFrame)
}
}
......@@ -144,11 +173,7 @@ case class ListMetadaFormatsResponse(override val oaiRequestRunner: OaiRequestRu
case n => n
}
}
new RuleTransformer(verbBody).transform(oaiFrame)
}
}
......@@ -194,8 +219,6 @@ case class ListSetsResponse(override val oaiRequestRunner: OaiRequestRunner) ext
case n => n
}
}
new RuleTransformer(verbBody).transform(oaiFrame)
}
......
......@@ -2,16 +2,16 @@ package org.swissbib.memobase.oai.runner
import modules.OaiRepository
import org.swissbib.memobase.oai.request.{OaiRequest, UserRequest}
import org.swissbib.memobase.oai.response.{GetRecordResponse, IdentifyResponse, ListIdentifiersResponse,
ListMetadaFormatsResponse, ListRecordsResponse, ListSetsResponse, OaiResponse}
import org.swissbib.memobase.oai.response.{GetRecordResponse, IdentifyResponse, ListIdentifiersResponse, ListMetadaFormatsResponse, ListRecordsResponse, ListSetsResponse, OaiResponse}
import play.api.Configuration
import scala.xml.Node
import scala.xml.{Node, NodeSeq}
abstract class OaiRequestRunner (val config: Configuration,
val repository: OaiRepository,
val request: UserRequest){
var result: Option[Node] = None
//var result: Option[NodeSeq] = None
def run(): OaiResponse
}
......@@ -72,6 +72,13 @@ case class ListRecordsRunner(override val config: Configuration,
repository,
request) {
override def run(): OaiResponse = {
//ich benötige einen genrischen Typ result
var r = repository.listRecords(from = request.from,
until = request.until,
set = request.set,
resumptionToken = Option.empty,
metadataPrefix = request.metadataPrefix.get)
ListRecordsResponse(this)
}
......
......@@ -8,19 +8,19 @@ import de.odysseus.staxon.xml.util.PrettyXMLEventWriter
import javax.xml.stream.XMLOutputFactory
import play.api.libs.json.{JsObject, JsValue}
import scala.xml.Elem
import scala.xml.{Elem, Node}
object Json2XML {
def singleJsonDoc2Xml(doc:JsValue, root: String = "record"):Elem = {
def singleJsonDoc2Xml(doc: JsValue, root: String = "record"): Elem = {
//only Helper function until we know how and produce valid OAI data
val esJsonRespone = JsObject(Seq(root -> doc)).toString
val esJsonRespone = JsObject(Seq(root -> doc)).toString
val config = new JsonXMLConfigBuilder().multiplePI(false).build();
val output = new StringWriter()
val reader = new JsonXMLInputFactory(config).createXMLEventReader(
new ByteArrayInputStream( esJsonRespone.getBytes(Charset.forName("UTF-8")))
new ByteArrayInputStream(esJsonRespone.getBytes(Charset.forName("UTF-8")))
)
val writer = XMLOutputFactory.newInstance().createXMLEventWriter(output)
val prettyWriter = new PrettyXMLEventWriter(writer)
......@@ -32,5 +32,10 @@ object Json2XML {
}
def multipleJsonDoc2Xml(doc: JsValue, root: String = "record"): Seq[Node] = {
}
Seq[Node]()
}
}
\ No newline at end of file
package utilities
object Validations {
def isValidDate: Boolean = ???
/*
https://stackoverflow.com/questions/5982484/scala-lift-check-if-date-is-correctly-formatted
https://medium.com/@hussachai/normalizing-a-date-string-in-the-scala-way-f37a2bdcc4b9
check dates with fold - seems interesting
*/
}
import scala.io.Source
import scala.util.{Try,Success,Failure}
object Test extends App{
def readTextFile(filename: String): Try[List[String]] = {
Try(Source.fromFile(filename).getLines.toList)
}
val filename = "/etc/passwd"
readTextFile(filename) match {
case Success(lines) => lines.foreach(println)
case Failure(f) => println(f)
}
}
......@@ -3,7 +3,7 @@ import Dependencies._
//import play.sbt.PlaySettings
ThisBuild / scalaVersion := "2.13.2"
ThisBuild / scalaVersion := "2.13.3"
//ThisBuild / scalaVersion := "2.12.11"
ThisBuild / organization := "ch.memobase"
ThisBuild / organizationName := "memobase"
......@@ -32,7 +32,10 @@ lazy val root = (project in file("."))
scalatestplusplay % Test,
elasticsearch,
logging,
json2xml
json2xml,
scala_xml_module,
upickl,
httprequests
),
scalacOptions ++= Seq(
"-feature",
......
......@@ -35,6 +35,10 @@ object Dependencies {
lazy val elasticsearch = "org.elasticsearch.client" % "elasticsearch-rest-high-level-client" % "7.7.0"
lazy val json2xml = "de.odysseus.staxon" % "staxon" % "1.3"
lazy val upickl = "com.lihaoyi" %% "upickle" % "1.2.0"
lazy val scala_xml_module = "org.scala-lang.modules" %% "scala-xml" % "1.3.0"
lazy val httprequests = "com.lihaoyi" %% "requests" % "0.6.5"
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment