-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add bulk script for removing topic subdivisions (#1513)
- Loading branch information
Showing
6 changed files
with
105 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
whelktool/src/main/resources/bulk-change-scripts/removeTopicSubdivision.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/** | ||
* Remove all uses of a certain TopicSubdivision within ComplexSubject | ||
* The TopicSubdivision itself is not removed, only the usages. | ||
* | ||
* Parameters: | ||
* bulk:deprecate - The subdivision(s) to be removed | ||
* bulk:keep - If specified, add this regular Topic to :subject instead | ||
*/ | ||
|
||
import whelk.JsonLd | ||
import whelk.util.DocumentUtil | ||
|
||
import static whelk.JsonLd.ID_KEY | ||
import static whelk.datatool.bulkchange.BulkJobDocument.DEPRECATE_KEY | ||
import static whelk.datatool.bulkchange.BulkJobDocument.KEEP_KEY | ||
|
||
List deprecateLinks = asList(parameters.get(DEPRECATE_KEY)) | ||
Map keepLink = parameters.get(KEEP_KEY) | ||
|
||
deprecateLinks.each { deprecate -> | ||
selectByIds([deprecate[ID_KEY]]) { obsoleteSubdivision -> | ||
selectByIds(obsoleteSubdivision.getDependers()) { depender -> | ||
Map thing = depender.graph[1] as Map | ||
|
||
if (thing[JsonLd.TYPE_KEY] == 'ComplexSubject') { | ||
return | ||
} | ||
|
||
def modified = DocumentUtil.traverse(thing) { value, path -> | ||
if (value instanceof Map && value[JsonLd.TYPE_KEY] == 'ComplexSubject') { | ||
var t = asList(value.get('termComponentList')) | ||
if (deprecate in t) { | ||
// TODO? add way to do this with an op? SplitReplace? [Replace, Insert]? | ||
if (keepLink && path.size() > 1) { | ||
var parent = DocumentUtil.getAtPath(thing, path.dropRight(1)) | ||
if (parent instanceof List && !parent.contains(keepLink)) { | ||
parent.add(keepLink) | ||
} | ||
} | ||
|
||
return mapSubject(value, t, deprecate) | ||
} | ||
} | ||
return DocumentUtil.NOP | ||
} | ||
|
||
if (modified) { | ||
depender.scheduleSave(loud: isLoudAllowed) | ||
} | ||
} | ||
} | ||
} | ||
|
||
static DocumentUtil.Operation mapSubject(Map subject, termComponentList, deprecateLink) { | ||
var t2 = termComponentList.findAll { it != deprecateLink } | ||
if (t2.size() == 0) { | ||
return new DocumentUtil.Remove() | ||
} | ||
if (t2.size() == 1) { | ||
return new DocumentUtil.Replace(t2.first()) | ||
} | ||
|
||
Map result = new HashMap(subject) | ||
result.termComponentList = t2 | ||
return new DocumentUtil.Replace(result) | ||
} |