package whelk.plugin.libris

import groovy.util.logging.Slf4j as Log

import whelk.Document
import whelk.plugin.BasicFilter

@Log
class JsonLdTermReducer extends BasicFilter {

    JsonLdTermReducer() {
    }

    boolean valid(Document doc) {
        return doc && doc.isJson() && doc.contentType == "application/ld+json"
    }

    Document doFilter(Document doc) {
        def dataMap = doFilter(doc.dataAsMap, doc.dataset)
        return doc.withData(dataMap)
    }

    Map doFilter(Map dataMap, String dataset) {
        def thing = dataMap.about ?: dataMap.focus ?: dataMap
        reduce(thing)
        return dataMap
    }

    void reduce(thing) {

        add thing, 'title', thing.instanceTitle?.titleValue

        add thing, 'isbn', thing.identifier?.findAll {
            it.identifierScheme?.get("@id") == "/def/identifiers/isbn"
        }.collect {
            it.identifierValue?.replaceAll("-", "")
        }

        add thing, 'creator', ['attributedTo', 'influencedBy'].collect {
            asList(thing[it])
        }.flatten().findAll()

    }

    void add(owner, term, value) {
        if (value) {
            def container = owner[term]
            if (container instanceof List) {
                container += value
            } else {
                owner[term] = value
            }
        }
    }

    List asList(value) {
        return value instanceof List? value : [value]
    }
}

package whelk.plugin.libris

import groovy.util.logging.Slf4j as Log

import whelk.Document
import whelk.plugin.BasicFilter

@Log
class JsonLdTermReducer extends BasicFilter {

    JsonLdTermReducer() {
    }

    boolean valid(Document doc) {
        return doc && doc.isJson() && doc.contentType == "application/ld+json"
    }

    Document doFilter(Document doc) {
        def dataMap = doFilter(doc.dataAsMap, doc.dataset)
        return doc.withData(dataMap)
    }

    Map doFilter(Map dataMap, String dataset) {
        def thing = dataMap.about ?: dataMap.focus ?: dataMap
        reduce(thing)
        return dataMap
    }

    void reduce(thing) {

        add thing, 'title', thing.instanceTitle?.titleValue

        add thing, 'isbn', thing.identifier?.findAll {
            it.identifierScheme?.get("@id") == "/def/identifiers/isbn"
        }.collect {
            it.identifierValue?.replaceAll("-", "")
        }

        add thing, 'creator', ['attributedTo', 'influencedBy'].collect {
            asList(thing[it])
        }.flatten().findAll()

    }

    void add(owner, term, value) {
        // TODO: treat result as a set; i.e. don't add if value or ID is present
        if (value) {
            def container = owner[term]
            if (container instanceof List) {
                container += value
            } else {
                owner[term] = value
            }
        }
    }

    List asList(value) {
        return value instanceof List? value : [value]
    }
}

Add note about treating value as a set
Add note about treating value as a set
