Skip to content

Commit

Permalink
[Upd] Modified MergeFormMetadata module to support extraction of exec…
Browse files Browse the repository at this point in the history
…utionId
  • Loading branch information
blcham committed Oct 11, 2021
1 parent 7505825 commit 264cfdf
Showing 1 changed file with 24 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
import cz.cvut.spipes.engine.ExecutionContextFactory;
import cz.cvut.spipes.form.JenaFormUtils;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.jena.rdf.model.*;
import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.ModelFactory;
import org.apache.jena.util.ResourceUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -17,20 +18,31 @@

/**
* Inputs are sample form and Q&A model. Questions from both models are remapped to new IRIs based on
* question origin combined with executionId.
* question origin combined with executionId. New question instances are created using questionInstanceTemplate
* which defaults to "doc:question-{_questionOriginHash}-{_executionId}".
*/
public class MergeFormMetadataModule extends AnnotatedAbstractModule {

private static final Logger LOG = LoggerFactory.getLogger(MergeFormMetadataModule.class);
private static final Random RANDOM = new Random();

private static final String TYPE_URI = KBSS_MODULE.uri + "merge-form-metadata";
private static final String QUESTION_ORGIN_HASH_VAR = "{_questionOriginHash}";
private static final String EXECUTION_ID_VAR = "{_executionId}";

@Parameter(urlPrefix = SML.uri, name = "replace")
private boolean isReplace = false;

@Parameter(name = "execution-id")
private String executionId = Long.toString(RANDOM.nextLong());
private String executionId = DigestUtils.md5Hex(Long.toString(RANDOM.nextLong()));

@Parameter(name = "question-instance-template")
private String questionInstanceTemplate =
VocabularyJena.s_c_question.toString()
+ "-"
+ QUESTION_ORGIN_HASH_VAR
+ "-"
+ EXECUTION_ID_VAR;


@Override
Expand All @@ -43,12 +55,16 @@ ExecutionContext executeSelf() {

JenaFormUtils.getQuestions(constructedModel).forEachRemaining(
q -> {
String hash = DigestUtils.md5Hex(JenaFormUtils.getQuestionOrigin(q).toString()+executionId);
String newQuestionUrl = VocabularyJena.s_c_question + "-" + hash;
if (LOG.isTraceEnabled()) {
LOG.trace("Renaming questions {} -> {}", q, newQuestionUrl);
String originHash = DigestUtils.md5Hex(JenaFormUtils.getQuestionOrigin(q).toString());
String newQuestionUrl = questionInstanceTemplate
.replace(QUESTION_ORGIN_HASH_VAR, originHash)
.replace(EXECUTION_ID_VAR, executionId);
if (!q.equals(newQuestionUrl)) {
if (LOG.isTraceEnabled()) {
LOG.trace("Renaming questions {} -> {}", q, newQuestionUrl);
}
ResourceUtils.renameResource(q, newQuestionUrl);
}
ResourceUtils.renameResource(q, newQuestionUrl);
}
);

Expand Down

0 comments on commit 264cfdf

Please sign in to comment.