Project

General

Profile

1
package eu.dnetlib.data.search.utils.vocabulary;
2

    
3
import eu.dnetlib.api.data.SearchService;
4
import eu.dnetlib.api.data.SearchServiceException;
5
import eu.dnetlib.domain.data.FormattedSearchResult;
6
import gr.uoa.di.driver.util.ServiceLocator;
7
import gr.uoa.di.driver.xml.VocabularyXmlConverter;
8
import org.apache.log4j.Logger;
9

    
10
import javax.xml.bind.JAXBException;
11
import java.util.Locale;
12

    
13
/**
14
 * Loads vocabulary that is created after a search query. 
15
 * The vocabulary is created based on the {@link Vocabulary}} transformer. 
16
 * @author kiatrop
17
 *
18
 */
19
public class IndexVocabularyLoader extends VocabularyLoader {
20
	private ServiceLocator<SearchService> searchServiceLocator = null;
21
	
22
	//in the case of IS vocabs, the only locale is the default
23
	//private String localeCountry = null;
24
	//private String localeLanguage = null;
25
	
26
	//the default number of the results in search request (same as result set)
27
	private static final int REQUEST_SIZE = 100;
28

    
29
	private Logger logger = Logger.getLogger(IndexVocabularyLoader.class);
30

    
31
	public String getVocabularyXml(Vocabulary vocabulary, Locale locale) {
32
		logger.debug("Getting vocabulary with name " + vocabulary.getName() + " and locale " + locale);
33

    
34
		String xml = null;
35
		FormattedSearchResult fsr = null;
36

    
37
		//long time = System.currentTimeMillis();
38
		//String stringLocale = locale.getLanguage() + "_" + locale.getCountry();
39

    
40
		try {
41
			fsr = searchServiceLocator.getService().search(
42
					((IndexVocabulary)vocabulary).getQuery(),
43
					((IndexVocabulary)vocabulary).getTransformer(),
44
					"vocabulary", null, 1, REQUEST_SIZE);
45

    
46
			if (fsr !=null) {
47
				int totalResultNumber = fsr.getResultsNumber();
48

    
49
				//make sure that every result is loaded
50
				if (totalResultNumber > REQUEST_SIZE) {
51

    
52
					if (fsr != null) {
53
						fsr = searchServiceLocator.getService().search(
54
								((IndexVocabulary) vocabulary).getQuery(),
55
								((IndexVocabulary) vocabulary).getTransformer(),
56
								"vocabulary", null, 1, totalResultNumber);
57
					}
58

    
59
					xml = fsr.getFormattedResult();
60
				}
61
			}
62

    
63
		} catch (SearchServiceException sse) {
64
			logger.error("Error getting Vocabulary xml", sse);
65
		}
66

    
67
		//time = System.currentTimeMillis() - time;
68
		//System.out.println("old Vocabulary creation took " + time);
69

    
70

    
71
		return xml;
72
	}
73

    
74
	public ServiceLocator<SearchService> getSearchServiceLocator() {
75
		return searchServiceLocator;
76
	}
77

    
78
	public void setSearchServiceLocator(
79
			ServiceLocator<SearchService> searchServiceLocator) {
80
		this.searchServiceLocator = searchServiceLocator;
81
	}
82

    
83
	@Override
84
	protected eu.dnetlib.domain.enabling.Vocabulary getVocabulary(Vocabulary vocabulary, Locale locale) {
85

    
86
		String xml = getVocabularyXml(vocabulary, locale);
87
		VocabularyXmlConverter converter = null;
88
		try {
89
			converter = new VocabularyXmlConverter();
90
			return converter.XmlToObject(xml);
91

    
92
		} catch (JAXBException jaxbe) {
93
			logger.error("Fail to create vocabulary " + vocabulary.getName(), jaxbe);
94
			return null;
95
		}
96
	}
97
}
98
 
(4-4/13)