diff --git a/whelktool/scripts/cleanups/2023/08/lxl-4230-move-more-bad-identifiedby.groovy b/whelktool/scripts/cleanups/2023/08/lxl-4230-move-more-bad-identifiedby.groovy new file mode 100644 index 0000000000..e52ef2483c --- /dev/null +++ b/whelktool/scripts/cleanups/2023/08/lxl-4230-move-more-bad-identifiedby.groovy @@ -0,0 +1,37 @@ +String where = "collection = 'bib' and data#>>'{@graph,1,identifiedBy}' like '%MTM medietyp%'" + +selectBySqlWhere(where) { data -> + + // Find the two relevant identifiers + List identifiedBys = asList(data.graph[1].identifiedBy) + Map medieTypIdentifier = identifiedBys.find { identifiedBy -> + identifiedBy["typeNote"] == "MTM medietyp" + } + Map medieNummerIdentifier = identifiedBys.find { identifiedBy -> + identifiedBy["typeNote"] == "MTM medienummer" + } + if (medieNummerIdentifier == null || medieTypIdentifier == null || medieTypIdentifier.value == null) + return + + // Make sure there's a qualifier list in medieNummerIdentifier to put stuff in + if (medieNummerIdentifier.qualifier == null) + medieNummerIdentifier.put("qualifier", []) + if (! medieNummerIdentifier.qualifier instanceof List) + medieNummerIdentifier.put("qualifier", [medieNummerIdentifier.qualifier]) + + // Make the switch and drop the "medietyp" + List qualifiers = medieNummerIdentifier.qualifier + qualifiers.add(medieTypIdentifier.value) + identifiedBys.remove(medieTypIdentifier) + + //System.err.println("ID by after change: " + identifiedBys) + data.scheduleSave() +} + +private List asList(Object o) { + if (o == null) + return [] + if (o instanceof List) + return o + return [o] +}