Project

General

Profile

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
					log.debug("retrieve value arg isValue: " + arg);
67
					if (arg.startsWith("collection(")) { // xquery
68
						arg = StringEscapeUtils.unescapeXml(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
					log.debug("retrieve value arg isInputField: " + arg);
75
					if (arg.startsWith("collection(")) { // xquery
76
						arg = StringEscapeUtils.unescapeXml(arg);
77
						resource = resourceDao.getResourceByQuery(arg); // query
78
					}else
79
						resource = resourceDao.getResource(arg); // profile id
80
				}else if (arguments.get(0).isJobConst()){
81
					// TODO
82
				}else if (arguments.get(0).isVariable()){
83
					// TODO				
84
					log.warn("RETRIEVEVALUE: support for variables not yet implemented.");
85
				}				
86
			}catch(Exception e){
87
				throw new ProcessingException(e);
88
			}
89
			
90
			if (resource == null){
91
				throw new ProcessingException("invalid profileId: " + arg + "; functionName: " + functionName + ", arg1: " + arguments.get(0).getArgument() + ", arg2: " + arguments.get(1).getArgument());
92
			}
93
			result = resource.getValue(arguments.get(1).getArgument());  // xpath expr
94
			break;
95
		case CURRENTDATE:
96
			SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); // TODO format string
97
			result = dateFormat.format(new Date());
98
			default:
99
				// unsupported
100
				break;
101
		}
102
		return result;
103
	}
104

    
105
	/**
106
	 * @return the resourceDao
107
	 */
108
	public ResourceDao getResourceDao() {
109
		return resourceDao;
110
	}
111

    
112
	/**
113
	 * @param resourceDao the resourceDao to set
114
	 */
115
	public void setResourceDao(ResourceDao resourceDao) {
116
		this.resourceDao = resourceDao;
117
	}
118

    
119
	private String evaluateXpath(String record, String xpathExpr, Map<String, String> nsMap){
120
		XPath xpath = XPathFactory.newInstance().newXPath();
121
		xpath.setNamespaceContext(new NamespaceContext() {
122
			
123
			@Override
124
			public Iterator getPrefixes(String namespaceURI) {
125
				return null;
126
			}
127
			
128
			@Override
129
			public String getPrefix(String namespaceURI) {
130
				// TODO Auto-generated method stub
131
				return null;
132
			}
133
			
134
			@Override
135
			public String getNamespaceURI(String prefix) {
136
				if ("dri".equals(prefix)){
137
                    return "http://www.driver-repository.eu/namespace/dri";                      
138
                }else if ("dr".equals(prefix)){
139
                        return "http://www.driver-repository.eu/namespace/dr";
140
                }else if ("dc".equals(prefix)){
141
                        return "http://purl.org/dc/elements/1.1/";
142
                }else if ("oaf".equals(prefix)){
143
                    return "http://namespace.openaire.eu/oaf";
144
                }else if ("prov".equals(prefix)){
145
                    return "http://www.openarchives.org/OAI/2.0/provenance";
146
                }
147
                return "";
148
			}
149
		});
150
		try {
151
			return xpath.evaluate(xpathExpr, new InputSource(new StringReader(record)));
152
		} catch (XPathExpressionException e) {
153
			log.fatal("cannot evaluate xpath");
154
			throw new IllegalStateException(e);
155
		}
156
	}
157
}
(15-15/17)