OaiRequestRunner.scala 7.28 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
import modules.OaiRepository
26
27
28
import org.swissbib.memobase.oai.request.{BadArgumentsReq, BaseUserRequest, GetRecordReq, IdentifyReq,
  ListIdentifiersReq, ListIdentifiersReqExclusive, ListMetadataFormatsReq,
  ListRecordsReq, ListRecordsReqExclusive, ListSetsReq, ListSetsReqExclusive}
29
30
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
  val request: BaseUserRequest
41
42
43
}


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

  override def run(): OaiResponse = {

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

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

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

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

68
69
70
  }
}

71
72
73
74


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

77
    repository.listIdentiers(
78
79
80
      from = request.parameter.from,
      until = request.parameter.until,
      set = request.parameter.set,
Günter Hipler's avatar
Günter Hipler committed
81
      resumptionToken = Option.empty,
82
83
84
85
86
87
88
89
      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
90

91
92
93
  }
}

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

    ListMetadaFormatsResponse(this)
  }
}
107
108
109
110


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

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

125

126

127
128
129
130

  }
}

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


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


  }
149
150
151
152
153
154

}


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

    ListSetsResponse(this)
  }
}

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

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


173
174
case class BadArgumentsErrorRunner(config: Configuration, repository: OaiRepository, request:
                                                          BadArgumentsReq) extends OaiRequestRunner {
175

176
177
178
179
  override def run(): OaiResponse = BadArgumentsResponse(this)
}


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
213
214
/*
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)
  }

}