-
Notifications
You must be signed in to change notification settings - Fork 4
/
MergeFormMetadataModule.java
95 lines (74 loc) · 3.13 KB
/
MergeFormMetadataModule.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package cz.cvut.spipes.modules;
import cz.cvut.sforms.SFormsVocabularyJena;
import cz.cvut.spipes.constants.KBSS_MODULE;
import cz.cvut.spipes.constants.SML;
import cz.cvut.spipes.engine.ExecutionContext;
import cz.cvut.spipes.form.JenaFormUtils;
import org.apache.commons.codec.digest.DigestUtils;
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;
import java.util.Random;
/**
* Inputs are sample form and Q&A model. Questions from both models are remapped to new IRIs based on
* 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";
// TODO use official uri templates
private static final String QUESTION_ORIGIN_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 = DigestUtils.md5Hex(Long.toString(RANDOM.nextLong()));
@Parameter(name = "question-instance-template")
private String questionInstanceTemplate =
SFormsVocabularyJena.s_c_question.toString()
+ "-"
+ QUESTION_ORIGIN_HASH_VAR
+ "-"
+ EXECUTION_ID_VAR;
@Override
ExecutionContext executeSelf() {
Model inpModel = this.getExecutionContext().getDefaultModel();
Model constructedModel = ModelFactory.createDefaultModel();
constructedModel.add(inpModel);
JenaFormUtils.getQuestions(constructedModel).forEachRemaining(
q -> {
String originHash = DigestUtils.md5Hex(JenaFormUtils.getQuestionOrigin(q).toString());
String newQuestionUrl = questionInstanceTemplate
.replace(QUESTION_ORIGIN_HASH_VAR, originHash)
.replace(EXECUTION_ID_VAR, executionId);
if (!q.getURI().equals(newQuestionUrl)) {
if (LOG.isTraceEnabled()) {
LOG.trace("Renaming questions {} -> {}", q, newQuestionUrl);
}
ResourceUtils.renameResource(q, newQuestionUrl);
}
}
);
return createOutputContext(isReplace, constructedModel);
}
public boolean isReplace() {
return isReplace;
}
public void setReplace(boolean replace) {
isReplace = replace;
}
public String getExecutionId() {
return executionId;
}
public void setExecutionId(String executionId) {
this.executionId = executionId;
}
@Override
public String getTypeURI() {
return TYPE_URI;
}
}