OaiRequestRunner.scala 7.91 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.common.validation.{NoContentForQueryParameter, OAIErrorCode}
import org.swissbib.memobase.oai.request.{BadArgumentsReq, BaseUserRequest, GetRecordReq, IdentifyReq, ListIdentifiersReq, ListIdentifiersReqExclusive, ListMetadataFormatsReq, ListRecordsReq, ListRecordsReqExclusive, ListSetsReq, ListSetsReqExclusive, NoContentForQueryReq}
import org.swissbib.memobase.oai.response.{BadArgumentsResponse, GetRecordResponse, IdentifyResponse, ListIdentifiersResponse, ListMetadaFormatsResponse, ListRecordsResponse, ListSetsResponse, OaiResponse}
29
30
import play.api.Configuration

31
32
import scala.util.{Failure, Success}

33

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


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

  override def run(): OaiResponse = {

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

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

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

61
62
63
      //todo: handle null
      case Failure(exception) =>
        GetRecordResponse(this,null)
64
65
66
67
68
69
70
71
        /*
        NoContentForQueryReq(
          config
          ,repository
          , NoContentForQueryParameter(request.parameter.verb
            ,request.parameter.allQueryParameter
          , OAIErrorCode.ID_DOES_NOT_EXIST.code)).createResponse(config,repository)
*/
72
    }
73

74
75
76
  }
}

77
78
79
80


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

83
    repository.listIdentiers(
84
85
86
      from = request.parameter.from,
      until = request.parameter.until,
      set = request.parameter.set,
Günter Hipler's avatar
Günter Hipler committed
87
      resumptionToken = Option.empty,
88
89
90
91
92
93
94
95
      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
96

97
98
99
  }
}

100
101
102
103
104
105
106
107
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 {
108
109
110
111
112
  override def run(): OaiResponse = {

    ListMetadaFormatsResponse(this)
  }
}
113
114
115
116


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

120
    repository.listRecords(from = request.parameter.from,
121
122
                                    until = request.parameter.until,
                                    set = request.parameter.set,
123
                                    resumptionToken = Option.empty,
124
125
126
127
128
129
130
                                    metadataPrefix = request.parameter.metadataPrefix) match {
      case Success(value) =>
        ListRecordsResponse(this, value)
      case Failure(exception) =>
        ListRecordsResponse(this, null)
    }

131

132

133
134
135
136

  }
}

137
138
case class ListRecordsExclusiveRunner(config: Configuration, repository: OaiRepository, request:
                                            ListRecordsReqExclusive) extends OaiRequestRunner {
139
140
  override def run(): OaiResponse = {
    //todo: this is only for first implementation reasons to make it runnable
141
142
143


    repository.listRecords(
144
145
146
      from = Option.empty,
      until = Option.empty,
      set = Option.empty,
147
      resumptionToken = Option(request.parameter.resumptionToken),
148
149
150
151
      metadataPrefix = "4711") match {
      case Success(value) => ListRecordsResponse(this, value)
      case Failure(exception) => ListRecordsResponse(this, null)
    }
152
153
154


  }
155
156
157
158
159
160

}


case class ListSetsRunner(config: Configuration, repository: OaiRepository, request:
                                                                    ListSetsReq) extends OaiRequestRunner {
161
162
163
164
165
166
  override def run(): OaiResponse = {

    ListSetsResponse(this)
  }
}

167
168
169
170
case class ListSetsExclusiveRunner(config: Configuration, repository: OaiRepository, request:
                                                                  ListSetsReqExclusive) extends OaiRequestRunner {
  override def run(): OaiResponse = ???
}
171

172
173
174
175
176
177
178
179
case class NoContentForQueryRunner(config: Configuration
                                   , repository: OaiRepository
                                   , request: NoContentForQueryReq) extends OaiRequestRunner {

  override def run(): OaiResponse = ???

}

180
181
182
//todo: make more detailed types
case class GetExceptionRunner(config: Configuration, repository: OaiRepository, request:
                                                          BadArgumentsReq) extends OaiRequestRunner {
183
184
185
186
  override def run(): OaiResponse = ???
}


187
188
case class BadArgumentsErrorRunner(config: Configuration, repository: OaiRepository, request:
                                                          BadArgumentsReq) extends OaiRequestRunner {
189

190
191
192
193
  override def run(): OaiResponse = BadArgumentsResponse(this)
}


194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/*
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)
  }

}