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

3
import modules.{OAIContent, 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

9
sealed abstract class OaiRequestRunner {
10
  var result: Option[(String, OAIContent)] = None
11
  var resultList: Option[ResultList] = None
12
  //var result: Option[NodeSeq] = None
13
  def run(): OaiResponse
14
15
  val config: Configuration
  val repository: OaiRepository
16
17
18
}


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

  override def run(): OaiResponse = {

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

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

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

37

38
39
40
41
    GetRecordResponse(this)
  }
}

42
43
44
45


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

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

55
56
57
58
    ListIdentifiersResponse(this)
  }
}

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

    ListMetadaFormatsResponse(this)
  }
}
72
73
74
75


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

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
91

    ListRecordsResponse(this)
  }
}

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


    ListRecordsResponse(this)
  }
106
107
108
109
110
111

}


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

    ListSetsResponse(this)
  }
}

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

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


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


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

}