Project

General

Profile

1
package gr.uoa.di.driver.enabling.islookup;
2

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

    
6
import javax.xml.bind.JAXBException;
7

    
8
import org.apache.log4j.Logger;
9

    
10
import eu.dnetlib.api.enabling.ISLookUpService;
11
import eu.dnetlib.api.enabling.ISLookUpServiceException;
12
import eu.dnetlib.domain.DriverResource;
13
import eu.dnetlib.domain.SearchCriteria;
14
import gr.uoa.di.driver.enabling.ISLookUp;
15
import gr.uoa.di.driver.enabling.ISLookUpException;
16
import gr.uoa.di.driver.util.ServiceLocator;
17
import gr.uoa.di.driver.xml.ResourceToXmlConverter;
18

    
19
public class ISLookUpImpl<D extends DriverResource> implements ISLookUp<D> {
20
	private static Logger logger = Logger.getLogger(ISLookUpImpl.class);
21

    
22
	private ServiceLocator<ISLookUpService> lookupLocator = null;
23
	private ResourceToXmlConverter<D> converter = null;
24

    
25
	public D getById(String profileId) throws ISLookUpException {
26
		try {
27
			String xml = lookupLocator.getService().getResourceProfile(
28
					profileId);
29

    
30
			logger.debug("Collection xml..." + xml);
31
			
32
			return this.parseXmlProfile(xml);
33
		} catch (ISLookUpServiceException e) {
34
			throw new ISLookUpException(e);
35
		}
36
	}
37

    
38
	public List<D> getByid(String[] profileIds) throws ISLookUpException {
39
		List<D> res = new ArrayList<D>();
40

    
41
		for (String id : profileIds) {
42
			res.add(this.getById(id));
43
		}
44

    
45
		return res;
46
	}
47

    
48
	public List<D> fetch(SearchCriteria criteria) throws ISLookUpException {
49
		logger.debug("Fetching...");
50
		try {
51
			String xQuery = converter.toXQueryString(criteria);
52
			logger.debug("xQuery: " + xQuery);
53

    
54

    
55
			List<String> xmls = lookupLocator.getService().quickSearchProfile(xQuery);
56
			List<D> res = new ArrayList<D>();
57

    
58
			logger.debug("Getting results from result set. Result set size: "
59
					+ xmls.size());
60
			
61
			logger.debug("starting parsing");
62
			
63
			if (xmls.size() > 0) {
64
				for (String xml : xmls)
65
					res.add(this.parseXmlProfile(xml));
66
			}
67
			logger.debug("parsed");
68
			
69
			return res;
70
		} catch (ISLookUpServiceException e) {
71
			logger.error(e);
72
			throw new ISLookUpException(e);
73
		}
74
	}
75

    
76
	private D parseXmlProfile(String xml) throws ISLookUpException {
77
		try {
78
			return this.converter.XmlToObject(xml);
79
		} catch (JAXBException e) {
80
			throw new ISLookUpException("Error parsing xml profile", e);
81
		} catch (Exception e) {
82
			logger.error("Error parsing: " + xml);
83
			throw new ISLookUpException("Error parsing xml profile", e);
84
		}
85
	}
86

    
87
	public List<String> fetch(String XQuery) throws ISLookUpException {
88

    
89
		try {
90
			return lookupLocator.getService().quickSearchProfile(XQuery);
91
		} catch (ISLookUpServiceException e) {
92
			throw new ISLookUpException(e);
93
		}
94
	}
95

    
96
	public List<D> performQuickSearch(SearchCriteria criteria)
97
			throws ISLookUpException {
98

    
99
		try {
100
			String xQuery = converter.toXQueryString(criteria);
101
			List<D> results = new ArrayList<D>();
102
			List<String> xmls = lookupLocator.getService().quickSearchProfile(
103
					xQuery);
104

    
105
			for (String profile : xmls)
106
				results.add(parseXmlProfile(profile));
107

    
108
			return results;
109
		} catch (ISLookUpServiceException e) {
110
			throw new ISLookUpException(e);
111
		}
112

    
113
	}
114

    
115
	public D getUniqueResult(SearchCriteria criteria) throws ISLookUpException {
116
		List<D> list = this.performQuickSearch(criteria);
117

    
118
		if (list.size() == 0)
119
			return null;
120
		else if (list.size() == 1)
121
			return list.get(0);
122
		else
123
			throw new ISLookUpException("More than one results in query");
124
	}
125

    
126
	public void setLookupLocator(ServiceLocator<ISLookUpService> lookupLocator) {
127
		this.lookupLocator = lookupLocator;
128
	}
129

    
130
	public void setConverter(ResourceToXmlConverter<D> converter) {
131
		this.converter = converter;
132
	}
133
}
(2-2/4)