Skip to content

Commit

Permalink
Support changes in Record (#1499)
Browse files Browse the repository at this point in the history
  • Loading branch information
kwahlin authored Oct 17, 2024
1 parent c5b1fc2 commit 970fe1c
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
13 changes: 10 additions & 3 deletions rest/src/main/groovy/whelk/rest/api/BulkChangePreviewAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import static whelk.JsonLd.ID_KEY;
import static whelk.JsonLd.RECORD_KEY;
import static whelk.util.Unicode.stripPrefix;

public class BulkChangePreviewAPI extends HttpServlet {
Expand Down Expand Up @@ -118,7 +121,12 @@ private static Map<String, String> makeLink(String id, int offset, int limit) {
// FIXME mangle the data in a more ergonomic way
@SuppressWarnings("unchecked")
private Map<?,?> makePreviewChangeSet(Document doc, Transform transform) {
var modified = new ModifiedThing(doc.getThing(), transform, whelk.getJsonld().repeatableTerms);
var thing = new LinkedHashMap<String, Object>(doc.getThing());
var record = new LinkedHashMap<String, Object>(doc.getRecord());
// Remove @id from record to prevent from being shown as a link in the diff view
record.remove(ID_KEY);
thing.put(RECORD_KEY, record);
var modified = new ModifiedThing(thing, transform, whelk.getJsonld().repeatableTerms);
var beforeDoc = doc.clone();
var afterDoc = doc.clone();
((List<Map<?,?>>) beforeDoc.data.get(JsonLd.GRAPH_KEY)).set(1, modified.getBefore());
Expand All @@ -127,9 +135,8 @@ private Map<?,?> makePreviewChangeSet(Document doc, Transform transform) {
new DocumentVersion(beforeDoc, "", ""),
new DocumentVersion(afterDoc, "", "")
), whelk.getJsonld());

var result = history.m_changeSetsMap;
result.put(JsonLd.ID_KEY, beforeDoc.getCompleteId());
result.put(ID_KEY, beforeDoc.getCompleteId());
((Map<String,Object>) DocumentUtil.getAtPath(result, List.of("changeSets", 0))).put("version", modified.getBefore());
((Map<String,Object>) DocumentUtil.getAtPath(result, List.of("changeSets", 1))).put("version", modified.getAfter());

Expand Down
2 changes: 2 additions & 0 deletions whelk-core/src/main/groovy/whelk/Document.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,8 @@ class Document {

Map getThing() { get(thingPath) as Map }

Map getRecord() { get(recordPath) as Map }

void setDescriptionCreator(creator) { set(descriptionCreatorPath, creator) }

String getDescriptionCreator() { get(descriptionCreatorPath) }
Expand Down
7 changes: 6 additions & 1 deletion whelktool/src/main/groovy/whelk/datatool/WhelkTool.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import java.util.concurrent.TimeUnit
import java.util.concurrent.atomic.AtomicInteger

import static java.util.concurrent.TimeUnit.SECONDS
import static whelk.JsonLd.RECORD_KEY
import static whelk.util.Jackson.mapper

class WhelkTool {
Expand Down Expand Up @@ -924,12 +925,16 @@ class DocumentItem {
}

boolean modify(Map matchForm, Map targetForm) {
Map thing = (Map) this.graph[1]
thing[RECORD_KEY] = (Map) this.graph[0]

var m = new ModifiedThing(
(Map) this.graph[1],
thing,
new Transform(matchForm, targetForm),
whelk.jsonld.repeatableTerms)

this.graph[1] = m.after
this.graph[0] = m.after.remove(RECORD_KEY)

return m.isModified()
}
Expand Down

0 comments on commit 970fe1c

Please sign in to comment.