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.enabling.resultset.ResultSetFactory;
|
17
|
import gr.uoa.di.driver.util.ServiceLocator;
|
18
|
import gr.uoa.di.driver.xml.ResourceToXmlConverter;
|
19
|
|
20
|
public class ISLookUpImpl<D extends DriverResource> implements ISLookUp<D> {
|
21
|
private static Logger logger = Logger.getLogger(ISLookUpImpl.class);
|
22
|
|
23
|
private ServiceLocator<ISLookUpService> lookupLocator = null;
|
24
|
private ResourceToXmlConverter<D> converter = null;
|
25
|
|
26
|
public D getById(String profileId) throws ISLookUpException {
|
27
|
try {
|
28
|
String xml = lookupLocator.getService().getResourceProfile(
|
29
|
profileId);
|
30
|
|
31
|
logger.debug("Collection xml..." + xml);
|
32
|
|
33
|
return this.parseXmlProfile(xml);
|
34
|
} catch (ISLookUpServiceException e) {
|
35
|
throw new ISLookUpException(e);
|
36
|
}
|
37
|
}
|
38
|
|
39
|
public List<D> getByid(String[] profileIds) throws ISLookUpException {
|
40
|
List<D> res = new ArrayList<D>();
|
41
|
|
42
|
for (String id : profileIds) {
|
43
|
res.add(this.getById(id));
|
44
|
}
|
45
|
|
46
|
return res;
|
47
|
}
|
48
|
|
49
|
public List<D> fetch(SearchCriteria criteria) throws ISLookUpException {
|
50
|
logger.debug("Fetching...");
|
51
|
try {
|
52
|
String xQuery = converter.toXQueryString(criteria);
|
53
|
logger.debug("xQuery: " + xQuery);
|
54
|
|
55
|
|
56
|
List<String> xmls = lookupLocator.getService().quickSearchProfile(xQuery);
|
57
|
List<D> res = new ArrayList<D>();
|
58
|
|
59
|
logger.debug("Getting results from result set. Result set size: "
|
60
|
+ xmls.size());
|
61
|
|
62
|
logger.debug("starting parsing");
|
63
|
|
64
|
if (xmls.size() > 0) {
|
65
|
for (String xml : xmls)
|
66
|
res.add(this.parseXmlProfile(xml));
|
67
|
}
|
68
|
logger.debug("parsed");
|
69
|
|
70
|
return res;
|
71
|
} catch (ISLookUpServiceException e) {
|
72
|
logger.error(e);
|
73
|
throw new ISLookUpException(e);
|
74
|
}
|
75
|
}
|
76
|
|
77
|
private D parseXmlProfile(String xml) throws ISLookUpException {
|
78
|
try {
|
79
|
return this.converter.XmlToObject(xml);
|
80
|
} catch (JAXBException e) {
|
81
|
throw new ISLookUpException("Error parsing xml profile", e);
|
82
|
} catch (Exception e) {
|
83
|
logger.error("Error parsing: " + xml);
|
84
|
throw new ISLookUpException("Error parsing xml profile", e);
|
85
|
}
|
86
|
}
|
87
|
|
88
|
public List<String> fetch(String XQuery) throws ISLookUpException {
|
89
|
|
90
|
try {
|
91
|
return lookupLocator.getService().quickSearchProfile(XQuery);
|
92
|
} catch (ISLookUpServiceException e) {
|
93
|
throw new ISLookUpException(e);
|
94
|
}
|
95
|
}
|
96
|
|
97
|
public List<D> performQuickSearch(SearchCriteria criteria)
|
98
|
throws ISLookUpException {
|
99
|
|
100
|
try {
|
101
|
String xQuery = converter.toXQueryString(criteria);
|
102
|
List<D> results = new ArrayList<D>();
|
103
|
List<String> xmls = lookupLocator.getService().quickSearchProfile(
|
104
|
xQuery);
|
105
|
|
106
|
for (String profile : xmls)
|
107
|
results.add(parseXmlProfile(profile));
|
108
|
|
109
|
return results;
|
110
|
} catch (ISLookUpServiceException e) {
|
111
|
throw new ISLookUpException(e);
|
112
|
}
|
113
|
|
114
|
}
|
115
|
|
116
|
public D getUniqueResult(SearchCriteria criteria) throws ISLookUpException {
|
117
|
List<D> list = this.performQuickSearch(criteria);
|
118
|
|
119
|
if (list.size() == 0)
|
120
|
return null;
|
121
|
else if (list.size() == 1)
|
122
|
return list.get(0);
|
123
|
else
|
124
|
throw new ISLookUpException("More than one results in query");
|
125
|
}
|
126
|
|
127
|
public void setLookupLocator(ServiceLocator<ISLookUpService> lookupLocator) {
|
128
|
this.lookupLocator = lookupLocator;
|
129
|
}
|
130
|
|
131
|
public void setConverter(ResourceToXmlConverter<D> converter) {
|
132
|
this.converter = converter;
|
133
|
}
|
134
|
}
|