<commit_before>/**
 * Delete duplicate SPI bib records (if other holding than sigel S then just log).
 * Move identifiedBy, technicalNote and hasNote fields from bib to holding for sigel S.
 * Link the S holding to the correct bib record.
 *
 * See LXL-3155 for more information
 */

import java.util.concurrent.atomic.AtomicInteger

class Script {
    static PrintWriter otherHoldingReport
    static PrintWriter deleteReport
    static PrintWriter holdReport
    static PrintWriter failedReport
}

Script.otherHoldingReport = getReportWriter("has-other-holding")
Script.deleteReport = getReportWriter("deleted")
Script.holdReport = getReportWriter("hold-updated")
Script.failedReport = getReportWriter("failed")

TSV = 'SPI-5705-ids.tsv'
new File(scriptDir, TSV).readLines().tail().collect{ it.split("\\t") }.each { columns ->
    try {
        process(getUri(columns[0]).toString(), getUri(columns[1]).toString())
    }
    catch (Exception e) {
        println(e)
        e.printStackTrace()
        Script.failedReport.println("${columns[0]} ${columns[1]} ${e}")
    }
}

void process(String duplicateUri, String keepUri) {
    def identifiedBy
    def hasNote
    selectByIds([duplicateUri]) { bib ->
        identifiedBy = bib.graph[0].remove('identifiedBy')
        bib.graph[0].remove('technicalNote')
        hasNote = bib.graph[1].remove('hasNote')

        if (!hasOtherHolding(duplicateUri)) {
            Script.deleteReport.println(duplicateUri)
            bib.scheduleDelete()
        } else {
            Script.otherHoldingReport.println(duplicateUri)
            bib.scheduleSave()
        }
    }
    boolean found = false
    selectBySqlWhere(whereSHolding(duplicateUri)) { hold ->
        StringBuilder msg = new StringBuilder().append(hold.doc.getURI()).append("\n")
        hold.graph[1]['itemOf']['@id'] = keepUri + '#it'
        add(msg, hold.graph[0], 'identifiedBy', identifiedBy)
        add(msg, hold.graph[0], 'cataloguersNote', ['S bestånd flyttat från SPI-dubblett maskinellt. Fel kan förekomma.'])
        add(msg, hold.graph[1], 'cataloguersNote', ['Katalog 56-86, SPI20191219'])
        add(msg, hold.graph[1], 'hasNote', hasNote)
        hold.scheduleSave()
        Script.holdReport.println(msg)
        found = true
    }

    if(!found) {
        Script.failedReport.println("No holding for S: $duplicateUri")
    }
}

void add(msg, thing, field, value) {
    def list = (thing[field] ?: []).with {it instanceof List ? it : [it]}
    msg.append("${field}: ").append(list).append(" -> ")
    list.addAll(value instanceof List ? value : [value])
    msg.append(list).append("\n")
    thing[value] = list
}

String getUri(String id) {<commit_msg>Fix SPI. Move holding before commiting delete of bib<commit_after>/**
 * Delete duplicate SPI bib records (if other holding than sigel S then just log).
 * Move identifiedBy, technicalNote and hasNote fields from bib to holding for sigel S.
 * Link the S holding to the correct bib record.
 *
 * See LXL-3155 for more information
 */

import java.util.concurrent.atomic.AtomicInteger

class Script {
    static PrintWriter otherHoldingReport
    static PrintWriter deleteReport
    static PrintWriter holdReport
    static PrintWriter failedReport
}

Script.otherHoldingReport = getReportWriter("has-other-holding")
Script.deleteReport = getReportWriter("deleted")
Script.holdReport = getReportWriter("hold-updated")
Script.failedReport = getReportWriter("failed")

TSV = 'SPI-5705-ids.tsv'
new File(scriptDir, TSV).readLines().tail().collect{ it.split("\\t") }.each { columns ->
    try {
        process(getUri(columns[0]).toString(), getUri(columns[1]).toString())
    }
    catch (Exception e) {
        println(e)
        e.printStackTrace()
        Script.failedReport.println("${columns[0]} ${columns[1]} ${e}")
    }
}

void process(String duplicateUri, String keepUri) {
    def identifiedBy
    def hasNote
    selectByIds([duplicateUri]) { bib ->
        identifiedBy = bib.graph[0].remove('identifiedBy')
        bib.graph[0].remove('technicalNote')
        hasNote = bib.graph[1].remove('hasNote')

        if (!hasOtherHolding(duplicateUri)) {
            Script.deleteReport.println(duplicateUri)
            bib.scheduleDelete()
        } else {
            Script.otherHoldingReport.println(duplicateUri)
            bib.scheduleSave()
        }

        boolean found = false
        selectBySqlWhere(whereSHolding(duplicateUri)) { hold ->
            StringBuilder msg = new StringBuilder().append(hold.doc.getURI()).append("\n")
            hold.graph[1]['itemOf']['@id'] = keepUri + '#it'
            add(msg, hold.graph[0], 'identifiedBy', identifiedBy)
            add(msg, hold.graph[0], 'cataloguersNote', ['S bestånd flyttat från SPI-dubblett maskinellt. Fel kan förekomma.'])
            add(msg, hold.graph[1], 'cataloguersNote', ['Katalog 56-86, SPI20191219'])
            add(msg, hold.graph[1], 'hasNote', hasNote)
            hold.scheduleSave()
            Script.holdReport.println(msg)
            found = true
        }
        if(!found) {
            Script.failedReport.println("No holding for S: $duplicateUri")
        }
    }
}

void add(msg, thing, field, value) {
    def list = (thing[field] ?: []).with {it instanceof List ? it : [it]}
    msg.append("${field}: ").append(list).append(" -> ")
    list.addAll(value instanceof List ? value : [value])
    msg.append(list).append("\n")
    thing[value] = list
}

String getUri(String id) {
{'fraction_non_alphanumeric': 0.13627217852569964, 'fraction_numerical': 0.01170660325589903, 'mean_word_length': 3.167682926829268, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2277053', 'n_tokens_mistral': 1662, 'n_tokens_neox': 1610, 'n_words': 374}