Commit 6858ecba authored by Günter Hipler's avatar Günter Hipler
Browse files

first version of all verbs

parent 319476f2
Pipeline #12857 passed with stage
in 2 minutes and 19 seconds
......@@ -13,7 +13,7 @@ import play.Environment
import play.api.libs.json.Json
import utilities.Json2XML
import scala.xml.Node
import scala.xml.{Elem, Node}
trait ElasticsearchComponent extends OaiRepository {
val client: Option[RestHighLevelClient]
......@@ -27,17 +27,16 @@ trait ElasticsearchComponent extends OaiRepository {
until: Option[String],
set: Option[String],
resumptionToken: Option[ResumptionToken],
metadataPrefix: String): Option[Seq[Node]] = {
metadataPrefix: String): Option[Seq[(String,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)))
val myMap = searchResponse.getHits.getHits.map(hit => (hit.getId, hit.getSourceAsMap)).map(
sourceTuple => (sourceTuple._1, Json2XML.singleJsonDoc2Xml(
Json.parse(play.libs.Json.toJson(sourceTuple._2).toString))))
Option(myMap.toSeq)
}
......@@ -45,16 +44,29 @@ trait ElasticsearchComponent extends OaiRepository {
until: Option[String],
set: Option[String],
resumptionToken: Option[ResumptionToken],
metadataPrefix: String): Option[Seq[Node]] = ???
metadataPrefix: String): Option[Seq[(String,Node)]] =
listRecords(from,
until,
set,
resumptionToken,
metadataPrefix)
override def getRecord(identifier: String,
metadataPrefix: String): Option[Node] = {
metadataPrefix: String): Option[(String, Elem)] = {
val getRequest = new GetRequest(index, identifier)
val getResponse = client.get.get(getRequest, RequestOptions.DEFAULT)
//todo: what if response is empty?
val sM : Option[util.Map[String, AnyRef]] = Option(getResponse.getSourceAsMap)
sM.map(hash => Json2XML.singleJsonDoc2Xml(Json.parse(play.libs.Json.toJson(hash).toString)))
val sM: Option[(String, util.Map[String, AnyRef])] = Option( (getResponse.getId,getResponse.getSourceAsMap))
sM.map(tuple => (tuple._1,Json2XML.singleJsonDoc2Xml(Json.parse(play.libs.Json.toJson(tuple._2).toString))))
//val myMap = searchResponse.getHits.getHits.map(hit => (hit.getId, hit.getSourceAsMap)).map(
// sourceTuple => (sourceTuple._1, Json2XML.singleJsonDoc2Xml(
// Json.parse(play.libs.Json.toJson(sourceTuple._2).toString))))
//Option(myMap.toSeq)
}
}
......@@ -10,17 +10,17 @@ trait OaiRepository {
until: Option[String],
set: Option[String],
resumptionToken: Option[ResumptionToken],
metadataPrefix: String): Option[Seq[Node]]
metadataPrefix: String): Option[Seq[(String,Node)]]
def listIdentiers(from: Option[String],
until: Option[String],
set: Option[String],
resumptionToken: Option[ResumptionToken],
metadataPrefix: String): Option[Seq[Node]]
metadataPrefix: String): Option[Seq[(String,Node)]]
def getRecord(identifier: String,
metadataPrefix: String
): Option[Node]
): Option[(String, Elem)]
......
package org.swissbib.memobase.oai.response
import java.time.Instant
import com.typesafe.config.{ConfigObject, ConfigValue}
import org.swissbib.memobase.oai.runner.OaiRequestRunner
import scala.xml.{Elem, Node, Text}
import scala.xml.transform.{RewriteRule, RuleTransformer}
......@@ -67,16 +63,21 @@ case class GetRecordResponse(override val oaiRequestRunner: OaiRequestRunner) ex
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/>
}
{if (oaiRequestRunner.result.nonEmpty)
makeHeader(identifier = oaiRequestRunner.result.get._1) }
{if (oaiRequestRunner.result.nonEmpty)
<metadata>
{oaiRequestRunner.result.get._2}
</metadata>}
{if (oaiRequestRunner.result.isEmpty)
<metadata/>}
</record>
</GetRecord>
case n => n
......@@ -136,7 +137,25 @@ case class ListIdentifiersResponse(override val oaiRequestRunner: OaiRequestRunn
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 ++
<ListIdentifiers>
{
oaiRequestRunner.resultList.get.map(
node => {
{makeHeader(identifier = node._1)}
}
)
}
</ListIdentifiers>
case n => n
}
}
new RuleTransformer(verbBody).transform(oaiFrame)
}
}
......@@ -186,8 +205,29 @@ case class ListRecordsResponse(override val oaiRequestRunner: OaiRequestRunner)
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 ++
<ListRecords>
{
oaiRequestRunner.resultList.get.map(
node => {
<record>
{makeHeader(identifier = node._1)}
<metadata>{node._2}</metadata>
</record>
}
)
}
</ListRecords>
case n => n
}
}
new RuleTransformer(verbBody).transform(oaiFrame)
}
}
......
......@@ -5,12 +5,13 @@ import org.swissbib.memobase.oai.request.{OaiRequest, UserRequest}
import org.swissbib.memobase.oai.response.{GetRecordResponse, IdentifyResponse, ListIdentifiersResponse, ListMetadaFormatsResponse, ListRecordsResponse, ListSetsResponse, OaiResponse}
import play.api.Configuration
import scala.xml.{Node, NodeSeq}
import scala.xml.{Elem, Node }
abstract class OaiRequestRunner (val config: Configuration,
val repository: OaiRepository,
val request: UserRequest){
var result: Option[Node] = None
var result: Option[(String, Elem)] = None
var resultList: Option[Seq[(String,Node)]] = None
//var result: Option[NodeSeq] = None
def run(): OaiResponse
}
......@@ -52,6 +53,13 @@ case class ListIdentifiersRunner(override val config: Configuration,
request) {
override def run(): OaiResponse = {
resultList = repository.listIdentiers(from = request.from,
until = request.until,
set = request.set,
resumptionToken = Option.empty,
metadataPrefix = request.metadataPrefix.get)
ListIdentifiersResponse(this)
}
}
......@@ -73,7 +81,7 @@ case class ListRecordsRunner(override val config: Configuration,
request) {
override def run(): OaiResponse = {
//ich benötige einen genrischen Typ result
var r = repository.listRecords(from = request.from,
resultList = repository.listRecords(from = request.from,
until = request.until,
set = request.set,
resumptionToken = Option.empty,
......
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