ParameterTypes.scala 5.41 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.common.validation

25
import org.swissbib.memobase.oai.common.util.ResumptionToken
26
import org.swissbib.memobase.oai.common.validation.OAIErrorCode.OAIErrorCode
27
import org.swissbib.memobase.oai.common.verb.OaiVerb.{GetRecord, Identify, ListIdentifiers, ListMetadataFormats, ListRecords, ListSets, OaiVerb}
28

29
30
import scala.util.Try

31
sealed abstract class OaiParameterBase() extends Product with Serializable {
32
  val allQueryParameter: Map[String, Seq[String]]
33
34
35
36
37
38
39
40
41
42
43
44
45
46
}

case class ParameterToCheck(verb:Option[OaiVerb],
                            metadataPrefix: Option[String],
                            set: Option[String],
                            from:Option[String],
                            until:Option[String],
                            identifier: Option[String],
                            resumptionToken: Option[String],
                            allQueryParameter: Map[String, Seq[String]]) extends OaiParameterBase {
}



47
case class CheckedIdentifyParameter (verb:OaiVerb, allQueryParameter: Map[String, Seq[String]]) extends OaiParameterBase {
48
49
50
51
  require(verb == Identify)

}

52
53
case class CheckedGetRecordParameter (verb:OaiVerb, identifier: String, metadataPrefix: String
                                      ,allQueryParameter: Map[String, Seq[String]]) extends OaiParameterBase {
54
55
56
57
  require(verb == GetRecord)

}

58
59
60
case class CheckedListIdentifiersParameter (verb:OaiVerb, metadataPrefix: String, set:Option[String]
                                            ,from:Option[String], until:Option[String]
                                            ,allQueryParameter: Map[String, Seq[String]]) extends OaiParameterBase {
61
62
63
  require(verb == ListIdentifiers)
}

64
65
66
case class CheckedListIdentifiersParameterExclusive (verb:OaiVerb
                                                     ,resumptionToken: ResumptionToken
                                                    ,allQueryParameter: Map[String, Seq[String]]) extends OaiParameterBase {
67
68
69
  require(verb == ListIdentifiers)
}

70
71
72
case class CheckedListMetadaFormatsParameter(verb:OaiVerb
                                             ,identifier: Option[String]
                                              ,allQueryParameter: Map[String, Seq[String]]) extends OaiParameterBase {
73
74
75
76
77
78
79
  require(verb == ListMetadataFormats)
}


final case class CheckedListRecordsParameter (verb:OaiVerb,
                                              metadataPrefix: String,
                                              from: Option[String],
80
81
82
                                              until: Option[String]
                                             ,set: Option[String]
                                             ,allQueryParameter: Map[String, Seq[String]]) extends OaiParameterBase {
83
84
85
  require(verb == ListRecords)
}

86
87
88
final case class CheckedListRecordsParameterExclusive (verb:OaiVerb
                                                       ,resumptionToken: ResumptionToken
                                                      ,allQueryParameter: Map[String, Seq[String]]) extends OaiParameterBase {
89
90
91
  require(verb == ListRecords)
}

92
93
94
case class CheckedListSetsParameterExclusive (verb:OaiVerb
                                              ,resumptionToken: String
                                             ,allQueryParameter: Map[String, Seq[String]]) extends OaiParameterBase {
95
96
97
  require(verb == ListSets)
}

98
99
case class CheckedListSetsParameter (verb:OaiVerb,
                                     allQueryParameter: Map[String, Seq[String]]) extends OaiParameterBase {
100
101
102
  require(verb == ListSets)
}

103
104
case class BadArgumentsParameter (verb: OaiVerb
                                  ,badArguments: Map[String,String]
105
                                  ,allQueryParameter: Map[String, Seq[String]]) extends OaiParameterBase
106

107
108
109
case class NoContentForQueryParameter (verb: OaiVerb
                                  ,allQueryParameter: Map[String, Seq[String]]
                                     ,errorCode: String ) extends OaiParameterBase
110

111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133


object OAIErrorCode extends Enumeration {

  type OAIErrorCode = Value

  protected case class OAIErrorCodeVal(code: String) extends super.Val {
    //https://www.scala-lang.org/api/current/scala/Enumeration.html
  }

  import scala.language.implicitConversions

  implicit def getCode(x: Value): String = x.asInstanceOf[OAIErrorCodeVal].code

  val NO_RECORDS_MATCH: OAIErrorCodeVal = OAIErrorCodeVal("noRecordsMatch")
  val ID_DOES_NOT_EXIST: OAIErrorCodeVal = OAIErrorCodeVal("idDoesNotExist")


  def getErrorCode(code: Option[String]): Try[OAIErrorCode] = {
    Try[OAIErrorCode](OAIErrorCode.withName(code.getOrElse(throw
      new Exception(s"no code for name $code"))))
  }
}