1
|
/**
|
2
|
*
|
3
|
*/
|
4
|
package eu.dnetlib.data.collective.transformation.engine.functions;
|
5
|
|
6
|
import java.io.StringReader;
|
7
|
import java.text.SimpleDateFormat;
|
8
|
import java.util.Date;
|
9
|
import java.util.Iterator;
|
10
|
import java.util.List;
|
11
|
import java.util.Map;
|
12
|
|
13
|
import javax.xml.namespace.NamespaceContext;
|
14
|
import javax.xml.xpath.XPath;
|
15
|
import javax.xml.xpath.XPathExpressionException;
|
16
|
import javax.xml.xpath.XPathFactory;
|
17
|
|
18
|
import org.apache.commons.lang.StringEscapeUtils;
|
19
|
import org.apache.commons.logging.Log;
|
20
|
import org.apache.commons.logging.LogFactory;
|
21
|
import org.xml.sax.InputSource;
|
22
|
|
23
|
import eu.dnetlib.common.profile.Resource;
|
24
|
import eu.dnetlib.common.profile.ResourceDao;
|
25
|
import eu.dnetlib.data.collective.transformation.rulelanguage.Argument;
|
26
|
|
27
|
/**
|
28
|
* @author jochen
|
29
|
*
|
30
|
*/
|
31
|
public class RetrieveValue extends AbstractTransformationFunction {
|
32
|
|
33
|
public static final Log log = LogFactory.getLog(RetrieveValue.class);
|
34
|
public static final String paramFunctionName = "functionName";
|
35
|
public static final String paramFunctionProfileId = "functionParameterProfileId";
|
36
|
public static final String paramFunctionExpr = "functionParameterExpr";
|
37
|
|
38
|
public enum FUNCTION {PROFILEFIELD, CURRENTDATE};
|
39
|
|
40
|
@javax.annotation.Resource
|
41
|
private ResourceDao resourceDao;
|
42
|
|
43
|
/* (non-Javadoc)
|
44
|
* @see eu.dnetlib.data.collective.transformation.engine.functions.AbstractTransformationFunction#execute()
|
45
|
*/
|
46
|
@Override
|
47
|
String execute() throws ProcessingException {
|
48
|
// TODO Auto-generated method stub
|
49
|
return null;
|
50
|
}
|
51
|
|
52
|
public String executeSingleValue(String functionName, List<Argument> arguments, String objRecord, Map<String, String> namespaceMap) throws ProcessingException{
|
53
|
String result = "";
|
54
|
FUNCTION function = FUNCTION.valueOf(functionName);
|
55
|
|
56
|
switch(function){
|
57
|
case PROFILEFIELD:
|
58
|
if (arguments.size() != 2){
|
59
|
throw new ProcessingException("invalid number of arguments - required 2 but found :" + arguments.size());
|
60
|
}
|
61
|
String arg = "";
|
62
|
Resource resource = null;
|
63
|
try{
|
64
|
if (arguments.get(0).isValue()){
|
65
|
arg = arguments.get(0).getArgument();
|
66
|
if (arg.startsWith("collection(")) { // xquery
|
67
|
arg = StringEscapeUtils.unescapeXml(arg);
|
68
|
log.debug("retriev value arg: " + arg);
|
69
|
resource = resourceDao.getResourceByQuery(arg); // query
|
70
|
}else
|
71
|
resource = resourceDao.getResource(arg); // profile id
|
72
|
}else if (arguments.get(0).isInputField()){
|
73
|
arg = evaluateXpath(objRecord, arguments.get(0).getArgument(), namespaceMap);
|
74
|
if (arg.startsWith("collection(")) { // xquery
|
75
|
arg = StringEscapeUtils.unescapeXml(arg);
|
76
|
resource = resourceDao.getResourceByQuery(arg); // query
|
77
|
}else
|
78
|
resource = resourceDao.getResource(arg); // profile id
|
79
|
}else if (arguments.get(0).isJobConst()){
|
80
|
// TODO
|
81
|
}else if (arguments.get(0).isVariable()){
|
82
|
// TODO
|
83
|
log.warn("RETRIEVEVALUE: support for variables not yet implemented.");
|
84
|
}
|
85
|
}catch(Exception e){
|
86
|
throw new ProcessingException(e);
|
87
|
}
|
88
|
|
89
|
if (resource == null){
|
90
|
throw new ProcessingException("invalid profileId: " + arg + "; functionName: " + functionName + ", arg1: " + arguments.get(0).getArgument() + ", arg2: " + arguments.get(1).getArgument());
|
91
|
}
|
92
|
result = resource.getValue(arguments.get(1).getArgument()); // xpath expr
|
93
|
break;
|
94
|
case CURRENTDATE:
|
95
|
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); // TODO format string
|
96
|
result = dateFormat.format(new Date());
|
97
|
default:
|
98
|
// unsupported
|
99
|
break;
|
100
|
}
|
101
|
return result;
|
102
|
}
|
103
|
|
104
|
/**
|
105
|
* @return the resourceDao
|
106
|
*/
|
107
|
public ResourceDao getResourceDao() {
|
108
|
return resourceDao;
|
109
|
}
|
110
|
|
111
|
/**
|
112
|
* @param resourceDao the resourceDao to set
|
113
|
*/
|
114
|
public void setResourceDao(ResourceDao resourceDao) {
|
115
|
this.resourceDao = resourceDao;
|
116
|
}
|
117
|
|
118
|
private String evaluateXpath(String record, String xpathExpr, Map<String, String> nsMap){
|
119
|
XPath xpath = XPathFactory.newInstance().newXPath();
|
120
|
xpath.setNamespaceContext(new NamespaceContext() {
|
121
|
|
122
|
@Override
|
123
|
public Iterator getPrefixes(String namespaceURI) {
|
124
|
return null;
|
125
|
}
|
126
|
|
127
|
@Override
|
128
|
public String getPrefix(String namespaceURI) {
|
129
|
// TODO Auto-generated method stub
|
130
|
return null;
|
131
|
}
|
132
|
|
133
|
@Override
|
134
|
public String getNamespaceURI(String prefix) {
|
135
|
if ("dri".equals(prefix)){
|
136
|
return "http://www.driver-repository.eu/namespace/dri";
|
137
|
}else if ("dr".equals(prefix)){
|
138
|
return "http://www.driver-repository.eu/namespace/dr";
|
139
|
}else if ("dc".equals(prefix)){
|
140
|
return "http://purl.org/dc/elements/1.1/";
|
141
|
}else if ("oaf".equals(prefix)){
|
142
|
return "http://namespace.openaire.eu/oaf";
|
143
|
}else if ("prov".equals(prefix)){
|
144
|
return "http://www.openarchives.org/OAI/2.0/provenance";
|
145
|
}
|
146
|
return "";
|
147
|
}
|
148
|
});
|
149
|
try {
|
150
|
return xpath.evaluate(xpathExpr, new InputSource(new StringReader(record)));
|
151
|
} catch (XPathExpressionException e) {
|
152
|
log.fatal("cannot evaluate xpath");
|
153
|
throw new IllegalStateException(e);
|
154
|
}
|
155
|
}
|
156
|
}
|