Skip to content

Commit 8d940a7

Browse files
committed
Add whelktool script for loading an RDF directory
1 parent a08c445 commit 8d940a7

1 file changed

Lines changed: 50 additions & 0 deletions

File tree

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import java.nio.file.FileVisitOption
2+
import java.nio.file.FileVisitResult
3+
import java.nio.file.Files
4+
import java.nio.file.Path
5+
import java.nio.file.Paths
6+
import java.nio.file.SimpleFileVisitor
7+
import java.nio.file.attribute.BasicFileAttributes
8+
9+
import whelk.Whelk
10+
import whelk.datatool.DocumentItem
11+
import static whelk.converter.RdfReader.readRdf
12+
13+
Map loadRdf(Whelk whelk, String rdfSourcePath) {
14+
var context = whelk.storage.loadDocumentByMainId(whelk.systemContextUri, null).data
15+
return new File(rdfSourcePath).withInputStream {
16+
readRdf(it, rdfSourcePath, context, whelk.systemContextUri)
17+
}
18+
}
19+
20+
DocumentItem saveRecord(Map data) {
21+
Map record = data[GRAPH].find { it -> it[TYPE] == 'Record' }
22+
assert record
23+
def mainId = record['mainEntity'][ID]
24+
assert mainId.endsWith('#it')
25+
Map mainEntity = data[GRAPH].find { it -> it[ID] == mainId }
26+
assert mainEntity
27+
List rest = data[GRAPH].findAll { it -> !it.is(record) && !it.is(mainEntity) }
28+
29+
return create(['@graph': [record, mainEntity] + rest])
30+
}
31+
32+
var rdfDirPath = Paths.get(System.getProperty("rdfdir"))
33+
var whelk = getWhelk()
34+
35+
var matcher = rdfDirPath.getFileSystem().getPathMatcher('glob:**/*.{rdf,ttl,jsonld}')
36+
37+
Files.walkFileTree(rdfDirPath, new SimpleFileVisitor<Path>() {
38+
@Override
39+
FileVisitResult visitFile(Path path, BasicFileAttributes attrs) {
40+
if (!matcher.matches(path)) {
41+
return FileVisitResult.CONTINUE
42+
}
43+
var data = loadRdf(whelk, path.toString())
44+
var docItem = saveRecord(data)
45+
selectFromIterable([docItem], { newItem ->
46+
newItem.scheduleSave()
47+
})
48+
return FileVisitResult.CONTINUE
49+
}
50+
})

0 commit comments

Comments
 (0)