Extractors.scala 12.6 KB
Newer Older
1
2
/*
 * IIIF Manifest Creator
3
 * Copyright (C) 2021  Memobase
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 *
 * 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/>.
 */

19
package ch.memobase.edm
20
21
22
23
24

import ujson.Value.{Value => JValue}
import ujson.{Arr => JArr, Obj => JObj, Str => JStr}

import scala.collection.mutable
25
import scala.collection.mutable.ArrayBuffer
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import scala.util.Try

object Extractors {
  val jsonGraph: String => Try[JValue] = jsonString =>
    Try { ujson.read(jsonString).obj("@graph").arr }

  private val fedoraResource =
    (jsonGraph: JArr) =>
      (resourceType: String) =>
        Try {
          jsonGraph.value.collectFirst {
            case res
                if (res.obj
                  .contains("type") && res.obj("type").str == resourceType) ||
                  (res.obj.contains("@type") && res
                    .obj("@type")
                    .str == resourceType) =>
              res.obj.value
          }.get
        }

  private val getBlankNodeContent = (graph: JArr) =>
    (resource: mutable.LinkedHashMap[String, JValue]) =>
      (property: String) =>
        graph.value.collectFirst {
          case res
              if res.obj("@id") == resource.getOrElse(property, JStr("")) =>
            res.obj.value
        }

56
57
  private val objContainsValue = (resource: JObj) => (property: String) =>
    (value: String) => {
58
59
60
61
      resource.value.get(property).map(v => {
        v.strOpt.isDefined && v.str == value || v.arrOpt.isDefined && v.arr.exists(_.str == value)
      }) match {
        case Some(true) => true
62
63
        case _ => false
      }
64

65
66
67
68
69
70
71
72
73
74
75
    }

  private val getAllBlankNodeContent = (graph: JArr) =>
    (resource: mutable.LinkedHashMap[String, JValue]) =>
      (property: String) =>
        graph.value.collect {
          case res if objContainsValue(resource)(property) (res.obj("@id").str)
            => res.obj.value
        }


76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
  private val getBlankNodesContent = (graph: JArr) =>
    (resource: mutable.LinkedHashMap[String, JValue]) =>
      (property: String) =>
        graph.value
          .filter(res =>
            res.obj("@id") == resource.getOrElse(property, JStr(""))
          )
          .map(_.obj.value)

  val digitalObject: JArr => Try[mutable.LinkedHashMap[String, JValue]] = {
    jsonGraph => fedoraResource(jsonGraph)("digitalObject")
  }
  val record: JArr => Try[mutable.LinkedHashMap[String, JValue]] = jsonGraph =>
    fedoraResource(jsonGraph)(
      "https://www.ica.org/standards/RiC/ontology#Record"
    )
  /*val recordSet: JArr => Try[mutable.LinkedHashMap[String, JValue]] =
    jsonGraph => fedoraResource(jsonGraph)("recordSet")*/

  private val stringValue = (jsonObj: JObj) =>
    (valueKey: String) => jsonObj.value.get(valueKey).flatMap(v => v.strOpt)
  private val numValue = (jsonObj: JObj) =>
    (valueKey: String) =>
      stringValue(jsonObj)(valueKey).flatMap(v => Some(v.toDouble))
  private val arrayValues = (jsonObj: JObj) =>
    (valueKey: String) =>
      jsonObj.value.get(valueKey).flatMap(v => Some(v.arr.toList.map(_.str)))

  val ricoType: JObj => Option[String] = record => stringValue(record)("type")
  val descriptiveNote: JObj => Option[String] = record =>
    stringValue(record)("descriptiveNote")
  val scopeAndContent: JObj => Option[String] = record =>
    stringValue(record)("scopeAndContent")
  val publishedBy
      : JArr => mutable.LinkedHashMap[String, JValue] => Option[String] =
    graph =>
      record =>
        getBlankNodeContent(graph)(record)("publishedBy").flatMap(v =>
          stringValue(v)("name")
        )
116
117
118
119
120
121
122
123
124
125

  val publishedByGH
  : JArr => mutable.LinkedHashMap[String, JValue] => List[String] =
    graph =>
      record =>
        getAllBlankNodeContent(graph)(record)("publishedBy").flatMap(v =>
          stringValue(v)("name")
        ).toList


126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
  val placeOfCapture
      : JArr => mutable.LinkedHashMap[String, JValue] => Option[String] =
    graph =>
      record =>
        getBlankNodeContent(graph)(record)("P60556").flatMap(v =>
          stringValue(v)("name")
        )
  val spatial: JArr => mutable.LinkedHashMap[String, JValue] => Option[String] =
    graph =>
      record =>
        getBlankNodeContent(graph)(record)("spatial").flatMap(v =>
          stringValue(v)("name")
        )
  val hasLanguage
      : JArr => mutable.LinkedHashMap[String, JValue] => Option[String] =
    graph =>
      record =>
        getBlankNodeContent(graph)(record)("hasLanguage").flatMap(v =>
          stringValue(v)("name")
        )

147
148
149
150
151
152
153
154
155
156
157
158
159
160
  //GH
  val resourceAllLanguages
  : JArr => mutable.LinkedHashMap[String, JValue] => List[String] =
    graph =>
      record =>
        getAllBlankNodeContent(graph)(record)(
          "hasLanguage"
        )
          .filter(obj =>
            ((obj("type").str == "content") || (obj("type").str == "caption")))
          .flatMap(
            v => stringValue(v)("name")).toList


161
162
163
164
165
166
167
168
169
170
171
172
  //noinspection ScalaStyle
  val resourceCreator: JArr => mutable.LinkedHashMap[String, JValue] => List[
    (String, String)
  ] =
    graph =>
      record =>
        getBlankNodesContent(graph)(record)(
          "recordResourceOrInstantiationIsSourceOfCreationRelation"
        )
          .flatMap(obj =>
            getBlankNodeContent(graph)(obj)("creationRelationHasTarget")
              .flatMap(v => stringValue(v)("name")) match {
173
174
175
              case Some(name) =>
                Some(obj.getOrElse("name", obj("type")).str, name)
              case None => None
176
177
178
179
            }
          )
          .toList

180
181

  val resourceContributorRDauP60441: JArr => mutable.LinkedHashMap[String, JValue] => Option[String] =
182
183
    graph =>
      record =>
184
185
186
187
188
189
190
191
192
        getBlankNodeContent(graph)(record)("P60441").flatMap(v =>
        stringValue(v)("name"))


  val resourceSourceOfRelationCreation: JArr => mutable.LinkedHashMap[String, JValue] => String => List[(String, String)] =
    graph =>
      record =>
        relationType =>
        getAllBlankNodeContent(graph)(record)(
193
          "recordResourceOrInstantiationIsSourceOfCreationRelation"
194
        ).filter(obj => obj("type").str == relationType)
195
196
197
198
199
200
201
          .flatMap(obj =>
            getBlankNodeContent(graph)(obj)("creationRelationHasTarget")
              .flatMap(v => stringValue(v)("name")) match {
              case Some(name) =>
                Some(obj.getOrElse("name", obj("type")).str, name)
              case None => None
            }
202
          ).toList
203
204


205
206
  val identifiedBy
  : JArr => mutable.LinkedHashMap[String, JValue] => ArrayBuffer[String] =
207
208
    graph =>
      record =>
209
210
211
212
213
        getAllBlankNodeContent(graph)(record)("identifiedBy")
          .filter(obj =>
            obj("type").str == "main" || obj("type").str == "original")
          .flatMap(v  =>
            stringValue(v)("identifier") match {
214
              case Some(_) =>
215
216
217
                Some(v.getOrElse("identifier", v("type")).str)
              case None => None
            }
218

219
          )
220
221


222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
  val resourceSubjects
  : JArr => mutable.LinkedHashMap[String, JValue] => List[String] =
    graph =>
      record =>
        getAllBlankNodeContent(graph)(record)("hasSubject")
          .filter(obj =>
            obj("@type").str == """http://www.w3.org/2004/02/skos/core#Concept""")
          .flatMap(v  =>
            stringValue(v)("prefLabel") match {
              case Some(s) => Some(s)
              case None => None
            }
          ).toList :::
            getAllBlankNodeContent(graph)(record)("hasSubject")
              .filter(obj =>
                obj("@type").str == """https://www.ica.org/standards/RiC/ontology#Agent""")
              .flatMap(v  =>
                stringValue(v)("name") match {
                  case Some(s) => Some(s)
                  case None => None
                }
              ).toList


  val resourceGenres
  : JArr => mutable.LinkedHashMap[String, JValue] => List[String] =
    graph =>
      record =>
        getAllBlankNodeContent(graph)(record)("hasGenre")
          .filter(obj =>
            obj("@type").str == "http://www.w3.org/2004/02/skos/core#Concept")
          .flatMap(v  =>
            stringValue(v)("prefLabel") match {
              case Some(s) => Some(s)
              case None => None
            }
          ).toList


  val resourceTitles
  : JArr => mutable.LinkedHashMap[String, JValue] => List[String] =
    graph =>
      record =>
        getAllBlankNodeContent(graph)(record)("hasTitle")
          .filter(obj =>
            obj("type").str == "main")
          .flatMap(v  =>
            stringValue(v)("title") match {
              case Some(s) => Some(s)
              case None => None
            }
          ).toList
  val title: JObj => Option[String] = record => stringValue(record)("title")

276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
  val producer
      : JArr => mutable.LinkedHashMap[String, JValue] => Option[String] =
    graph =>
      record =>
        getBlankNodeContent(graph)(record)("P60441").flatMap(v =>
          stringValue(v)("name")
        )

  val componentColor: JObj => Option[List[String]] = digitalObject =>
    arrayValues(digitalObject)("componentColor")
  val dctAbstract: JObj => Option[String] = record =>
    stringValue(record)("abstract")
  val mediaResourceDescription: JObj => Option[String] = digitalObject =>
    stringValue(digitalObject)("mediaResourceDescription")

  val conditionsOfUse: JObj => Option[String] = record =>
    stringValue(record)("conditionsOfUse")
  val regulatedByHolder
      : JArr => mutable.LinkedHashMap[String, JValue] => Option[String] =
    graph =>
      digitalObject =>
        getBlankNodesContent(graph)(digitalObject)("regulatedBy")
          .collectFirst {
            case res if res.obj("type").str == "holder" =>
              res.obj("name").str
          }
302
303
304
305
306
307
308
309
310
311
312
313
314

  //IIIF arbeitet mit digital object
  val regulatedMultipleByHolder
  : JArr => mutable.LinkedHashMap[String, JValue] => List[String] =
    graph =>
      record =>
        getAllBlankNodeContent(graph)(record)("regulatedBy")
          .collect {
            case res if res.obj("type").str == "holder" =>
              res.obj("name").str
          }.toList


315
316
317
318
319
320
321
322
323
324
325
326
327
  val license: JArr => mutable.LinkedHashMap[String, JValue] => Option[String] =
    graph =>
      digitalObject =>
        getBlankNodesContent(graph)(digitalObject)("regulatedBy")
          .collectFirst {
            case res if res.obj("type").str == "usage" =>
              res.obj("name").str
          }
  /* val logo: JArr => mutable.LinkedHashMap[String, JValue] => Option[String] =
    graph =>
      recordSet =>
        getBlankNodeContent(graph)(recordSet)("heldBy")
          .flatMap(v => v.get("P154").flatMap(vi => Some(vi.str))) */
328
329
  val digitalObjectId: JObj => Option[String] = digitalObject =>
  stringValue(digitalObject)("@id")
330
331
332
333

  val dobjectId: JObj => Option[String] = digitalObject =>
    stringValue(digitalObject)("@id").flatMap(Some(_))

334
335
  val recordId: JObj  => Option[String] = record => stringValue(record)("@id").flatMap(Some(_))

336
337
  val recordSource: JObj => Option[String] = record => stringValue(record)("source")

338

339
340
  val manifestId: JObj => Option[String] = digitalObject =>
    digitalObjectId(digitalObject).flatMap(v => Some(s"$v/manifest"))
341
  val sequenceId: JObj => Option[String] = digitalObject =>
342
    digitalObjectId(digitalObject).flatMap(v => Some(s"$v/sequence/default"))
343
  val canvasId: JObj => Option[String] = digitalObject =>
344
345
346
347
348
    digitalObjectId(digitalObject).flatMap(v => Some(s"$v/canvas/default"))
  // This is a slight concession to the fact that the locator is sent as invalid
  // sftp link. The locator link can however be built easily from the digitalObject
  // id, so we check only the existence of the locator property, but discard a potential
  // value.
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
  val imageResourceId: JObj => Option[String] = digitalObject =>
    stringValue(digitalObject)("locator")
  val mimeType: JObj => Option[String] = digitalObject =>
    stringValue(digitalObject)("hasMimeType")
  val imageHeight: JObj => Option[Double] = digitalObject =>
    numValue(digitalObject)("height")
  val imageWidth: JObj => Option[Double] = digitalObject =>
    numValue(digitalObject)("width")
  val creationDate
      : JArr => mutable.LinkedHashMap[String, JValue] => Option[String] =
    graph =>
      record =>
        getBlankNodeContent(graph)(record)("created").flatMap(v =>
          stringValue(v)("normalizedDateValue")
        )
}