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

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

8
import scala.xml.{Elem, Node}
9

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


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

  override def run(): OaiResponse = {

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

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

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

39

40
41
42
43
    GetRecordResponse(this)
  }
}

44
45
46
47


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

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

57
58
59
60
    ListIdentifiersResponse(this)
  }
}

61
62
63
64
65
66
67
68
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 {
69
70
71
72
73
  override def run(): OaiResponse = {

    ListMetadaFormatsResponse(this)
  }
}
74
75
76
77


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

    resultListnew = repository.listRecords1(
      from = request.parameter.from,
      until = request.parameter.until,
      set = request.parameter.set,
      resumptionToken = Option.empty,
      metadataPrefix = request.parameter.metadataPrefix)

96

97
98
99
100
101

    ListRecordsResponse(this)
  }
}

102
103
case class ListRecordsExclusiveRunner(config: Configuration, repository: OaiRepository, request:
                                            ListRecordsReqExclusive) extends OaiRequestRunner {
104
105
106
107
108
109
  override def run(): OaiResponse = {
    //todo: this is only for first implementation reasons to make it runnable
    resultList = repository.listRecords(
      from = Option.empty,
      until = Option.empty,
      set = Option.empty,
110
      resumptionToken = Option(request.parameter.resumptionToken),
111
112
113
114
115
      metadataPrefix = "4711")


    ListRecordsResponse(this)
  }
116
117
118
119
120
121

}


case class ListSetsRunner(config: Configuration, repository: OaiRepository, request:
                                                                    ListSetsReq) extends OaiRequestRunner {
122
123
124
125
126
127
  override def run(): OaiResponse = {

    ListSetsResponse(this)
  }
}

128
129
130
131
case class ListSetsExclusiveRunner(config: Configuration, repository: OaiRepository, request:
                                                                  ListSetsReqExclusive) extends OaiRequestRunner {
  override def run(): OaiResponse = ???
}
132

133
134
135
//todo: make more detailed types
case class GetExceptionRunner(config: Configuration, repository: OaiRepository, request:
                                                          BadArgumentsReq) extends OaiRequestRunner {
136
137
138
139
  override def run(): OaiResponse = ???
}


140
141
142
143
144
145
case class BadArgumentsErrorRunner(config: Configuration, repository: OaiRepository, request:
                                                          BadArgumentsReq) extends OaiRequestRunner {
  override def run(): OaiResponse = BadArgumentsResponse(this)
}


146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
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)
  }

}