Project

General

Profile

1
package eu.dnetlib.data.collective.transformation.engine.functions;
2

    
3
import java.util.LinkedList;
4
import java.util.List;
5

    
6
import eu.dnetlib.data.collective.transformation.VocabularyRegistry;
7
import org.springframework.beans.factory.annotation.Autowired;
8
import org.springframework.stereotype.Component;
9

    
10
/**
11
 * @author jochen
12
 */
13
@Component
14
public class Convert extends AbstractTransformationFunction {
15

    
16
	public static final String paramVocabularyName = "vocabularyName";
17
	public static final String paramFieldValue = "fieldValue";
18
	public static final String paramDefaultPattern = "defaultPattern";
19
	public static final String paramFunction = "function";
20

    
21
	@Autowired
22
	private VocabularyRegistry vocabularyRegistry;
23

    
24
	/**
25
	 * not implemented
26
	 *
27
	 * @see eu.dnetlib.data.collective.transformation.engine.functions.AbstractTransformationFunction#execute()
28
	 */
29
	public String execute() throws ProcessingException {
30
		return null;
31
	}
32

    
33
	/**
34
	 * extracts and returns the encoded value as used in the vocabulary
35
	 *
36
	 * @param vocabularyName the name of the vocabulary to be used
37
	 * @param fieldValues    the list of values to normalize
38
	 * @return encoded value
39
	 * @throws ProcessingException
40
	 */
41
	public String executeSingleValue(String vocabularyName, List<String> fieldValues) throws ProcessingException {
42
		if (!vocabularyRegistry.getVocabularies().containsKey(vocabularyName)) {
43
			throw new ProcessingException("unknown vocabulary: " + vocabularyName);
44
		}
45
		String returnValue = vocabularyRegistry.getVocabulary(vocabularyName).encoding(fieldValues);
46
		return returnValue;
47
	}
48

    
49
	public List<String> executeAllValues(String vocabularyName, List<String> fieldValues) throws ProcessingException {
50
		if (!vocabularyRegistry.getVocabularies().containsKey(vocabularyName)) {
51
			throw new ProcessingException("unknown vocabulary: " + vocabularyName);
52
		}
53
		List<String> computedValues = new LinkedList<String>();
54
		int numOfComputedValues = fieldValues.size();
55
		if (numOfComputedValues == 0) numOfComputedValues = 1; // return at least 1 value
56
		String returnValue = vocabularyRegistry.getVocabulary(vocabularyName).encoding(fieldValues);
57
		for (int i = 0; i < numOfComputedValues; i++) {
58
			computedValues.add(returnValue);
59
		}
60
		return computedValues;
61
	}
62

    
63
	public List<String> executeFilterByParams(String vocabName, List<String> fieldValues, String defaultPattern, String filterFunction)
64
			throws ProcessingException {
65
		return vocabularyRegistry.getVocabulary(vocabName).encoding(fieldValues, defaultPattern, filterFunction);
66
	}
67

    
68
	public VocabularyRegistry getVocabularyRegistry() {
69
		return vocabularyRegistry;
70
	}
71

    
72
	public void setVocabularyRegistry(VocabularyRegistry vocabularyRegistry) {
73
		this.vocabularyRegistry = vocabularyRegistry;
74
	}
75

    
76
}
(2-2/17)