1
|
package eu.dnetlib.data.transformation.service;
|
2
|
|
3
|
import javax.xml.transform.TransformerConfigurationException;
|
4
|
|
5
|
import org.apache.commons.logging.Log;
|
6
|
import org.apache.commons.logging.LogFactory;
|
7
|
import org.springframework.core.io.Resource;
|
8
|
|
9
|
import eu.dnetlib.common.profile.ProfileNotFoundException;
|
10
|
import eu.dnetlib.common.profile.ResourceDao;
|
11
|
import eu.dnetlib.data.collective.transformation.VocabularyRegistry;
|
12
|
import eu.dnetlib.data.collective.transformation.engine.SimpleTransformationEngine;
|
13
|
import eu.dnetlib.data.collective.transformation.engine.core.TransformationImpl;
|
14
|
import eu.dnetlib.data.collective.transformation.utils.TransformationRulesImportTool;
|
15
|
import eu.dnetlib.enabling.datasources.common.Api;
|
16
|
import eu.dnetlib.enabling.datasources.common.Datasource;
|
17
|
import eu.dnetlib.enabling.datasources.common.LocalDatasourceManager;
|
18
|
import eu.dnetlib.miscutils.functional.UnaryFunction;
|
19
|
|
20
|
public class SimpleDataTransformer implements UnaryFunction<String, String> {
|
21
|
|
22
|
/**
|
23
|
* logger.
|
24
|
*/
|
25
|
private static final Log log = LogFactory.getLog(SimpleDataTransformer.class);
|
26
|
|
27
|
/**
|
28
|
* Transformation rule profile
|
29
|
*/
|
30
|
private String ruleProfile;
|
31
|
private SimpleTransformationEngine transformationEngine;
|
32
|
|
33
|
public SimpleDataTransformer(final String ruleProfile) {
|
34
|
this.ruleProfile = ruleProfile;
|
35
|
|
36
|
// TODO
|
37
|
// instantiate here the xml transformer
|
38
|
|
39
|
if (log.isDebugEnabled()) {
|
40
|
log.debug("************************************************************");
|
41
|
log.debug("New transformer created from profile " + ruleProfile);
|
42
|
log.debug("************************************************************");
|
43
|
}
|
44
|
|
45
|
}
|
46
|
|
47
|
@Override
|
48
|
public String evaluate(final String record) {
|
49
|
if (log.isDebugEnabled()) {
|
50
|
log.debug("************************************************************");
|
51
|
log.debug("INPUT: " + record);
|
52
|
log.debug("************************************************************");
|
53
|
}
|
54
|
|
55
|
final String output = transform(record);
|
56
|
|
57
|
if (log.isDebugEnabled()) {
|
58
|
log.debug("************************************************************");
|
59
|
log.debug("OUTPUT: " + output);
|
60
|
log.debug("************************************************************");
|
61
|
}
|
62
|
|
63
|
return output;
|
64
|
}
|
65
|
|
66
|
private String transform(final String record) {
|
67
|
// use here the xml transformer
|
68
|
return transformationEngine.transform(record);
|
69
|
}
|
70
|
|
71
|
protected void setupEngine(final VocabularyRegistry vocabularyRegistry,
|
72
|
final Resource transformationTemplate,
|
73
|
final Resource defaultSchema,
|
74
|
final TransformationRulesImportTool rulesProfileUtil,
|
75
|
final ResourceDao resourceDao,
|
76
|
final Resource blacklistApi,
|
77
|
final LocalDatasourceManager<Datasource<?, ?, ?>, Api<?>> dsManager) throws TransformerConfigurationException, ProfileNotFoundException {
|
78
|
transformationEngine = new SimpleTransformationEngine();
|
79
|
transformationEngine.setVocabularyRegistry(vocabularyRegistry);
|
80
|
transformationEngine.setDsManager(dsManager);
|
81
|
final TransformationImpl transformation = new TransformationImpl();
|
82
|
transformation.setSchema(defaultSchema);
|
83
|
transformation.setTemplate(transformationTemplate);
|
84
|
transformation.init();
|
85
|
if (log.isDebugEnabled()) {
|
86
|
log.debug("************************************************************");
|
87
|
log.debug(ruleProfile);
|
88
|
log.debug("************************************************************");
|
89
|
}
|
90
|
transformation.setRuleLanguageParser(rulesProfileUtil.getRuleLanguageParser(ruleProfile));
|
91
|
transformation.configureTransformation();
|
92
|
transformationEngine.setTransformation(transformation);
|
93
|
transformationEngine.setResourceDao(resourceDao);
|
94
|
transformationEngine.setBlacklistApi(blacklistApi);
|
95
|
}
|
96
|
|
97
|
public String getRuleProfile() {
|
98
|
return ruleProfile;
|
99
|
}
|
100
|
|
101
|
public void setRuleProfile(final String ruleProfile) {
|
102
|
this.ruleProfile = ruleProfile;
|
103
|
}
|
104
|
|
105
|
}
|