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

    
9
/**
10
 * @author jochen
11
 */
12
public class Convert extends AbstractTransformationFunction {
13

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

    
19
	@Autowired
20
	private VocabularyRegistry vocabularyRegistry;
21

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

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

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

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

    
66
	public VocabularyRegistry getVocabularyRegistry() {
67
		return vocabularyRegistry;
68
	}
69

    
70
	public void setVocabularyRegistry(VocabularyRegistry vocabularyRegistry) {
71
		this.vocabularyRegistry = vocabularyRegistry;
72
	}
73

    
74
}
(2-2/17)