OaiRequestRunner.scala 7.23 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/*
 * generic OAI Server - agnostic in relation to the data repository
 * initially created for memobase project
 *
 * Copyright (C) 2021  UB Basel
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 *
 */


23
24
package org.swissbib.memobase.oai.runner

25
26
27
28
29
30
import modules.OaiRepository
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}
31
32
import play.api.Configuration

33
34
import scala.util.{Failure, Success}

35

36
sealed abstract class OaiRequestRunner {
37
  def run(): OaiResponse
38
39
  val config: Configuration
  val repository: OaiRepository
40
41
42
}


43
//todo: this is wrong!
44
45
case class IdentifyRunner(config: Configuration, repository: OaiRepository, request: IdentifyReq)
                                                                                extends OaiRequestRunner {
46
47
48
49
50
51
52
53

  override def run(): OaiResponse = {

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

54
55
case class GetRecordRunner(config: Configuration, repository: OaiRepository, request: GetRecordReq)
                                                                                extends OaiRequestRunner {
56
57
  override def run(): OaiResponse = {

58
59
60
    repository.getRecord(request.parameter.identifier,request.parameter.metadataPrefix) match {
      case Success(value) =>
        GetRecordResponse(this,value)
61

62
63
64
65
      //todo: handle null
      case Failure(exception) =>
        GetRecordResponse(this,null)
    }
66

67
68
69
  }
}

70
71
72
73


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

76
    repository.listIdentiers(
77
78
79
      from = request.parameter.from,
      until = request.parameter.until,
      set = request.parameter.set,
Günter Hipler's avatar
Günter Hipler committed
80
      resumptionToken = Option.empty,
81
82
83
84
85
86
87
88
      metadataPrefix = request.parameter.metadataPrefix) match {
      case Success(value) =>
        ListIdentifiersResponse(this,value)

        //todo: handle null
      case Failure(exception) =>
        ListIdentifiersResponse(this,null)
    }
Günter Hipler's avatar
Günter Hipler committed
89

90
91
92
  }
}

93
94
95
96
97
98
99
100
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 {
101
102
103
104
105
  override def run(): OaiResponse = {

    ListMetadaFormatsResponse(this)
  }
}
106
107
108
109


case class ListRecordsRunner(config: Configuration, repository: OaiRepository, request:
                                                                            ListRecordsReq) extends OaiRequestRunner {
110
  override def run(): OaiResponse = {
111
    //ich benötige einen genrischen Typ result
112

113
    repository.listRecords(from = request.parameter.from,
114
115
                                    until = request.parameter.until,
                                    set = request.parameter.set,
116
                                    resumptionToken = Option.empty,
117
118
119
120
121
122
123
                                    metadataPrefix = request.parameter.metadataPrefix) match {
      case Success(value) =>
        ListRecordsResponse(this, value)
      case Failure(exception) =>
        ListRecordsResponse(this, null)
    }

124

125

126
127
128
129

  }
}

130
131
case class ListRecordsExclusiveRunner(config: Configuration, repository: OaiRepository, request:
                                            ListRecordsReqExclusive) extends OaiRequestRunner {
132
133
  override def run(): OaiResponse = {
    //todo: this is only for first implementation reasons to make it runnable
134
135
136


    repository.listRecords(
137
138
139
      from = Option.empty,
      until = Option.empty,
      set = Option.empty,
140
      resumptionToken = Option(request.parameter.resumptionToken),
141
142
143
144
      metadataPrefix = "4711") match {
      case Success(value) => ListRecordsResponse(this, value)
      case Failure(exception) => ListRecordsResponse(this, null)
    }
145
146
147


  }
148
149
150
151
152
153

}


case class ListSetsRunner(config: Configuration, repository: OaiRepository, request:
                                                                    ListSetsReq) extends OaiRequestRunner {
154
155
156
157
158
159
  override def run(): OaiResponse = {

    ListSetsResponse(this)
  }
}

160
161
162
163
case class ListSetsExclusiveRunner(config: Configuration, repository: OaiRepository, request:
                                                                  ListSetsReqExclusive) extends OaiRequestRunner {
  override def run(): OaiResponse = ???
}
164

165
166
167
//todo: make more detailed types
case class GetExceptionRunner(config: Configuration, repository: OaiRepository, request:
                                                          BadArgumentsReq) extends OaiRequestRunner {
168
169
170
171
  override def run(): OaiResponse = ???
}


172
173
174
175
176
177
case class BadArgumentsErrorRunner(config: Configuration, repository: OaiRepository, request:
                                                          BadArgumentsReq) extends OaiRequestRunner {
  override def run(): OaiResponse = BadArgumentsResponse(this)
}


178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*
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)
  }

}