JSON.kt 2.68 KB
Newer Older
Jonas Waeber's avatar
Jonas Waeber committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * search-doc-service
 * Copyright (C) 2020 Memoriav
 *
 * 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/>.
 */
Jonas Waeber's avatar
Jonas Waeber committed
18 19
package org.memobase.helpers

Jonas Waeber's avatar
Jonas Waeber committed
20
import ch.memobase.rdf.RICO
21
import ch.memobase.rdf.RICO.Types
Jonas Waeber's avatar
Jonas Waeber committed
22 23 24
import com.beust.klaxon.JsonArray
import com.beust.klaxon.JsonObject
import com.beust.klaxon.Klaxon
25
import com.beust.klaxon.KlaxonException
Jonas Waeber's avatar
Jonas Waeber committed
26
import java.io.StringReader
27
import org.apache.logging.log4j.LogManager
Jonas Waeber's avatar
Jonas Waeber committed
28
import org.memobase.model.UpdateQuery
Jonas Waeber's avatar
Jonas Waeber committed
29 30 31

object JSON {

32 33
    private val log = LogManager.getLogger("JsonParser")

34 35 36 37
    private const val graph = "@graph"
    private const val Record = "Record"
    private const val RecordSet = "RecordSet"
    private const val CorporateBody = "CorporateBody"
Jonas Waeber's avatar
Jonas Waeber committed
38 39 40
    const val institutionTag = "institution"
    const val recordTag = "record"
    const val recordSetTag = "recordSet"
41

Jonas Waeber's avatar
Jonas Waeber committed
42 43
    private val klaxon = Klaxon()

44 45 46 47 48 49 50
    fun parse(data: String): JsonObject {
        return try {
            klaxon.parseJsonObject(StringReader(data))
        } catch (ex: KlaxonException) {
            log.error("${ex.localizedMessage} -> $data.")
            JsonObject()
        }
Jonas Waeber's avatar
Jonas Waeber committed
51 52 53
    }

    fun unpack(input: JsonObject): Map<String, JsonObject> {
54
        val graph = input[graph] as JsonArray<JsonObject>
Jonas Waeber's avatar
Jonas Waeber committed
55
        return graph.map {
56 57 58 59 60
            if (it[KEYS.atType] == RICO.Record.uri && it[RICO.type.localName] !in listOf(
                    Types.Record.publication,
                    Types.Record.related
                )
            ) {
Jonas Waeber's avatar
Jonas Waeber committed
61
                Pair(recordTag, it)
62 63 64
            } else if (it[KEYS.atType] == RICO.RecordSet.uri &&
                it[RICO.type.localName] !in listOf(Types.RecordSet.original, Types.RecordSet.related)
            ) {
Jonas Waeber's avatar
Jonas Waeber committed
65
                Pair(recordSetTag, it)
Jonas Waeber's avatar
Jonas Waeber committed
66
            } else if (it[KEYS.atType] == RICO.CorporateBody.uri && it[KEYS.ricoType] == KEYS.CorporateBodyType.memobaseInstitution) {
Jonas Waeber's avatar
Jonas Waeber committed
67
                Pair(institutionTag, it)
Jonas Waeber's avatar
Jonas Waeber committed
68
            } else {
Jonas Waeber's avatar
Jonas Waeber committed
69
                Pair(it[KEYS.entityId] as String, it)
Jonas Waeber's avatar
Jonas Waeber committed
70 71 72 73 74
            }
        }.toMap()
    }


75 76 77
    fun queryToJson(query: UpdateQuery): String {
        return klaxon.toJsonString(query)
    }
Jonas Waeber's avatar
Jonas Waeber committed
78
}