-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a task to fix the default graph in existing datasets
- Loading branch information
Showing
5 changed files
with
125 additions
and
13 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
110 changes: 110 additions & 0 deletions
110
...nstancev4/src/main/java/nl/knaw/huygens/timbuctoo/server/tasks/MoveDefaultGraphsTask.java
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,110 @@ | ||
package nl.knaw.huygens.timbuctoo.server.tasks; | ||
|
||
import io.dropwizard.servlets.tasks.Task; | ||
import nl.knaw.huygens.timbuctoo.v5.dataset.DataSetRepository; | ||
import nl.knaw.huygens.timbuctoo.v5.dataset.dto.DataSet; | ||
import nl.knaw.huygens.timbuctoo.v5.datastores.quadstore.dto.CursorQuad; | ||
import nl.knaw.huygens.timbuctoo.v5.datastores.quadstore.dto.Direction; | ||
import nl.knaw.huygens.timbuctoo.v5.datastores.rssource.DataSetQuadGenerator; | ||
import nl.knaw.huygens.timbuctoo.v5.graphql.mutations.PredicateMutationRdfPatcher; | ||
import nl.knaw.huygens.timbuctoo.v5.graphql.mutations.dto.PredicateMutation; | ||
import nl.knaw.huygens.timbuctoo.v5.rdfio.implementations.BasicRdfPatchSerializer; | ||
import nl.knaw.huygens.timbuctoo.v5.util.Graph; | ||
import nl.knaw.huygens.timbuctoo.v5.util.RdfConstants; | ||
|
||
import java.io.PrintWriter; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.stream.Stream; | ||
|
||
import static nl.knaw.huygens.timbuctoo.v5.graphql.mutations.dto.PredicateMutation.replace; | ||
|
||
public class MoveDefaultGraphsTask extends Task { | ||
private static final DataSetQuadGenerator DATASET_QUAD_GENERATOR = new DataSetQuadGenerator(); | ||
|
||
private final DataSetRepository dataSetRepository; | ||
|
||
public MoveDefaultGraphsTask(DataSetRepository dataSetRepository) { | ||
super("moveDefaultGraphs"); | ||
this.dataSetRepository = dataSetRepository; | ||
} | ||
|
||
@Override | ||
public void execute(Map<String, List<String>> parameters, PrintWriter output) throws Exception { | ||
boolean hasMutations; | ||
PredicateMutation predicateMutation; | ||
|
||
for (DataSet dataSet : dataSetRepository.getDataSets()) { | ||
hasMutations = false; | ||
predicateMutation = new PredicateMutation(); | ||
|
||
String baseUri = dataSet.getMetadata().getBaseUri().endsWith("/") ? | ||
dataSet.getMetadata().getBaseUri() : | ||
dataSet.getMetadata().getBaseUri() + "/"; | ||
|
||
Optional<Graph> graphOne = Optional.of(new Graph(baseUri)); | ||
Optional<Graph> graphTwo = Optional.of(new Graph(baseUri.substring(0, baseUri.length() - 1))); | ||
try (Stream<CursorQuad> quads = dataSet.getQuadStore().getAllQuads() | ||
.filter(quad -> quad.getDirection() == Direction.OUT) | ||
.filter(quad -> quad.inGraph(graphOne) || quad.inGraph(graphTwo))) { | ||
for (CursorQuad quad : (Iterable<CursorQuad>) quads::iterator) { | ||
if (!hasMutations) { | ||
hasMutations = true; | ||
output.println(dataSet.getMetadata().getCombinedId()); | ||
output.println(); | ||
output.flush(); | ||
} | ||
|
||
Optional<String> dataType = quad.getValuetype(); | ||
Optional<String> language = quad.getLanguage(); | ||
if (dataType == null || !dataType.isPresent()) { | ||
output.println(DATASET_QUAD_GENERATOR.onRelation( | ||
quad.getSubject(), | ||
quad.getPredicate(), | ||
quad.getObject(), | ||
quad.getGraph().orElse(null) | ||
)); | ||
} else if (language != null && language.isPresent() && dataType.get().equals(RdfConstants.LANGSTRING)) { | ||
output.println(DATASET_QUAD_GENERATOR.onLanguageTaggedString( | ||
quad.getSubject(), | ||
quad.getPredicate(), | ||
quad.getObject(), | ||
language.get(), | ||
quad.getGraph().orElse(null) | ||
)); | ||
} else { | ||
output.println(DATASET_QUAD_GENERATOR.onValue( | ||
quad.getSubject(), | ||
quad.getPredicate(), | ||
quad.getObject(), | ||
dataType.get(), | ||
quad.getGraph().orElse(null) | ||
)); | ||
} | ||
|
||
predicateMutation.entity(baseUri, replace(quad.getPredicate(), new PredicateMutation.Value( | ||
quad.getObject(), | ||
quad.getValuetype().orElse(null), | ||
quad.getLanguage().orElse(null), | ||
null | ||
))); | ||
} | ||
|
||
if (hasMutations) { | ||
output.println(); | ||
output.flush(); | ||
|
||
PredicateMutationRdfPatcher rdfPatcher = new PredicateMutationRdfPatcher(predicateMutation); | ||
rdfPatcher.sendQuads(new BasicRdfPatchSerializer(output), null, dataSet); | ||
output.println(); | ||
output.flush(); | ||
|
||
if (!parameters.containsKey("dry-run")) { | ||
dataSet.getImportManager().generateLog(baseUri, null, rdfPatcher).get(); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,5 @@ | ||
|
||
|
||
* ) ) ) | ||
` ) /( ( ) ( /( ( ( /( | ||
( )(_)))\ ( )\()) ))\ ( )\()) ( ( | ||
(_(_())((_) )\ '((_)\ /((_) )\ (_))/ )\ )\ | ||
|_ _| (_) _((_)) | |(_)(_))( ((_)| |_ ((_) ((_) | ||
| | | || ' \()| '_ \| || |/ _| | _|/ _ \/ _ \ | ||
|_| |_||_|_|_| |_.__/ \_,_|\__| \__|\___/\___/ v6 | ||
|
||
_____ _ _ _ | ||
/__ (_)_ __ ___ | |__ _ _ ___| |_ ___ ___ | ||
/ /\/ | '_ ` _ \| '_ \| | | |/ __| __/ _ \ / _ \ | ||
/ / | | | | | | | |_) | |_| | (__| || (_) | (_) | | ||
\/ |_|_| |_| |_|_.__/ \__,_|\___|\__\___/ \___/ v7 |