OaiRequestRunner.scala 6.13 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 result: Option[NodeSeq] = None
14
  def run(): OaiResponse
15
16
  val config: Configuration
  val repository: OaiRepository
17
18
19
}


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

  override def run(): OaiResponse = {

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

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

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

38

39
40
41
42
    GetRecordResponse(this)
  }
}

43
44
45
46


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

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

56
57
58
59
    ListIdentifiersResponse(this)
  }
}

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

    ListMetadaFormatsResponse(this)
  }
}
73
74
75
76


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

86
87
88
89
90

    ListRecordsResponse(this)
  }
}

91
92
case class ListRecordsExclusiveRunner(config: Configuration, repository: OaiRepository, request:
                                            ListRecordsReqExclusive) extends OaiRequestRunner {
93
94
95
96
97
98
  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,
99
      resumptionToken = Option(request.parameter.resumptionToken),
100
101
102
103
104
      metadataPrefix = "4711")


    ListRecordsResponse(this)
  }
105
106
107
108
109
110

}


case class ListSetsRunner(config: Configuration, repository: OaiRepository, request:
                                                                    ListSetsReq) extends OaiRequestRunner {
111
112
113
114
115
116
  override def run(): OaiResponse = {

    ListSetsResponse(this)
  }
}

117
118
119
120
case class ListSetsExclusiveRunner(config: Configuration, repository: OaiRepository, request:
                                                                  ListSetsReqExclusive) extends OaiRequestRunner {
  override def run(): OaiResponse = ???
}
121

122
123
124
//todo: make more detailed types
case class GetExceptionRunner(config: Configuration, repository: OaiRepository, request:
                                                          BadArgumentsReq) extends OaiRequestRunner {
125
126
127
128
  override def run(): OaiResponse = ???
}


129
130
131
132
133
134
case class BadArgumentsErrorRunner(config: Configuration, repository: OaiRepository, request:
                                                          BadArgumentsReq) extends OaiRequestRunner {
  override def run(): OaiResponse = BadArgumentsResponse(this)
}


135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
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)
  }

}