OaiRequestRunner.scala 5.5 KB
Newer Older
1
2
3
package org.swissbib.memobase.oai.runner

import modules.OaiRepository
4
5
6
7
8
import org.swissbib.memobase.oai.request.{BadArgumentsReq, GetRecordReq, IdentifyReq, ListIdentifiersReq,
  ListIdentifiersReqExclusive, ListMetadataFormatsReq, ListRecordsReq, ListRecordsReqExclusive, ListSetsReq,
  ListSetsReqExclusive}
import org.swissbib.memobase.oai.response.{GetRecordResponse, IdentifyResponse, ListIdentifiersResponse,
  ListMetadaFormatsResponse, ListRecordsResponse, ListSetsResponse, OaiResponse}
9
10
import play.api.Configuration

11
import scala.xml.{Elem, Node}
12

13
sealed abstract class OaiRequestRunner {
Günter Hipler's avatar
Günter Hipler committed
14
15
  var result: Option[(String, Elem)] = None
  var resultList: Option[Seq[(String,Node)]] = None
16
  //var result: Option[NodeSeq] = None
17
  def run(): OaiResponse
18
19
  val config: Configuration
  val repository: OaiRepository
20
21
22
}


23
//todo: this is wrong!
24
25
case class IdentifyRunner(config: Configuration, repository: OaiRepository, request: IdentifyReq)
                                                                                extends OaiRequestRunner {
26
27
28
29
30
31
32
33

  override def run(): OaiResponse = {

    //Identify doesn't need any action on repository
    IdentifyResponse(this)
  }
}

34
35
case class GetRecordRunner(config: Configuration, repository: OaiRepository, request: GetRecordReq)
                                                                                extends OaiRequestRunner {
36
37
38
  override def run(): OaiResponse = {

    //Identify doesnit need any action on repository
39
    result = repository.getRecord(request.parameter.identifier,request.parameter.metadataPrefix)
40

41

42
43
44
45
    GetRecordResponse(this)
  }
}

46
47
48
49


case class ListIdentifiersRunner(config: Configuration, repository: OaiRepository, request: ListIdentifiersReq)
                                                                                extends OaiRequestRunner {
50
51
  override def run(): OaiResponse = {

52
53
54
55
    resultList = repository.listIdentiers(
      from = request.parameter.from,
      until = request.parameter.until,
      set = request.parameter.set,
Günter Hipler's avatar
Günter Hipler committed
56
      resumptionToken = Option.empty,
57
      metadataPrefix = request.parameter.metadataPrefix)
Günter Hipler's avatar
Günter Hipler committed
58

59
60
61
62
    ListIdentifiersResponse(this)
  }
}

63
64
65
66
67
68
69
70
case class ListIdentifiersExclusiveRunner(config: Configuration, repository: OaiRepository, request:
                                                        ListIdentifiersReqExclusive)  extends OaiRequestRunner {
  override def run(): OaiResponse = ???
}


case class ListMetadataFormatsRunner(config: Configuration, repository: OaiRepository, request:
                                                            ListMetadataFormatsReq) extends OaiRequestRunner {
71
72
73
74
75
  override def run(): OaiResponse = {

    ListMetadaFormatsResponse(this)
  }
}
76
77
78
79


case class ListRecordsRunner(config: Configuration, repository: OaiRepository, request:
                                                                            ListRecordsReq) extends OaiRequestRunner {
80
  override def run(): OaiResponse = {
81
    //ich benötige einen genrischen Typ result
82
83
84
    resultList = repository.listRecords(from = request.parameter.from,
                                    until = request.parameter.until,
                                    set = request.parameter.set,
85
                                    resumptionToken = Option.empty,
86
                                    metadataPrefix = request.parameter.metadataPrefix)
87

88
89
90
91
92

    ListRecordsResponse(this)
  }
}

93
94
95
96
97
98
99
100
101
case class ListRecordsExclusiveRunner(config: Configuration, repository: OaiRepository, request:
                                            ListRecordsReqExclusive) extends OaiRequestRunner {
  override def run(): OaiResponse = ???

}


case class ListSetsRunner(config: Configuration, repository: OaiRepository, request:
                                                                    ListSetsReq) extends OaiRequestRunner {
102
103
104
105
106
107
  override def run(): OaiResponse = {

    ListSetsResponse(this)
  }
}

108
109
110
111
case class ListSetsExclusiveRunner(config: Configuration, repository: OaiRepository, request:
                                                                  ListSetsReqExclusive) extends OaiRequestRunner {
  override def run(): OaiResponse = ???
}
112

113
114
115
//todo: make more detailed types
case class GetExceptionRunner(config: Configuration, repository: OaiRepository, request:
                                                          BadArgumentsReq) extends OaiRequestRunner {
116
117
118
119
  override def run(): OaiResponse = ???
}


120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
I think we do not need it any more...
 */
object XmlCreator {

  //todo: check if e.g. scalatags might be the better alternative
  private val pRepName = "#REP_NAME#".r
  private val pBaseUrl = "#BASE_URL#".r
  private val pProtVersion = "#PROT_VERSION#".r
  private val pEarliest = "#EARLIEST_DATESTAMP#".r
  private val pEmail = "#ADMIN_EMAIL#".r
  private val pDeleted = "#DELETED#".r
  private val pGranularity = "#GRANULARITY#".r


  def createIdentifyFragment(rep:OaiRepository): String = {

    pRepName.replaceAllIn(
        pBaseUrl.replaceAllIn(
          pProtVersion.replaceAllIn(
            pEarliest.replaceAllIn(
              pEmail.replaceAllIn(
                pDeleted.replaceAllIn(
                  pGranularity.replaceAllIn(
                    scala.io.Source.fromResource("resources/identify.xml").getLines().mkString,
                    rep.oaiConfig.identify.granularity
                  ),rep.oaiConfig.identify.deletedRecord)
              ,rep.oaiConfig.identify.adminEmail),
              rep.oaiConfig.identify.earliestDatestamp),
          rep.oaiConfig.identify.protocolVersion)
        ,rep.oaiConfig.identify.baseUrl),
      rep.oaiConfig.identify.repositoryName)
  }

}