1
|
package gr.uoa.di.driver.enabling.islookup.cache;
|
2
|
|
3
|
import java.util.ArrayList;
|
4
|
import java.util.List;
|
5
|
|
6
|
import javax.xml.bind.JAXBException;
|
7
|
|
8
|
import net.sf.ehcache.Ehcache;
|
9
|
import net.sf.ehcache.Element;
|
10
|
|
11
|
import org.apache.log4j.Logger;
|
12
|
|
13
|
import eu.dnetlib.api.enabling.ISLookUpService;
|
14
|
import eu.dnetlib.domain.DriverResource;
|
15
|
import eu.dnetlib.domain.SearchCriteria;
|
16
|
import gr.uoa.di.driver.enabling.ISLookUp;
|
17
|
import gr.uoa.di.driver.enabling.ISLookUpException;
|
18
|
import gr.uoa.di.driver.util.ServiceLocator;
|
19
|
import gr.uoa.di.driver.xml.ResourceToXmlConverter;
|
20
|
|
21
|
public class CachingISLookUp<D extends DriverResource> implements ISLookUp<D> {
|
22
|
static Logger logger = Logger.getLogger(CachingISLookUp.class);
|
23
|
|
24
|
private Ehcache criteriaCache = null;
|
25
|
private Ehcache idCache = null;
|
26
|
private ResourceToXmlConverter<D> converter = null;
|
27
|
private ServiceLocator<ISLookUpService> lookupLocator = null;
|
28
|
|
29
|
public D getById(String id) throws ISLookUpException {
|
30
|
logger.debug("Requested object with id " + id);
|
31
|
|
32
|
String key = id;
|
33
|
Element element = (Element) idCache.get(key);
|
34
|
|
35
|
if (element != null)
|
36
|
return this.parseXml((String) element.getObjectValue());
|
37
|
else
|
38
|
return null;
|
39
|
}
|
40
|
|
41
|
public List<D> getByid(String[] ids) throws ISLookUpException {
|
42
|
List<D> list = new ArrayList<D>();
|
43
|
|
44
|
for (String id : ids)
|
45
|
list.add(this.getById(id));
|
46
|
|
47
|
return list;
|
48
|
}
|
49
|
|
50
|
@SuppressWarnings("unchecked")
|
51
|
public List<D> fetch(SearchCriteria criteria) throws ISLookUpException {
|
52
|
if (logger.isDebugEnabled())
|
53
|
logger.debug("Fetching results for query: " +
|
54
|
converter.toXQueryString(criteria));
|
55
|
|
56
|
CriteriaCacheKey key = new CriteriaCacheKey(criteria);
|
57
|
key.setXQuery(converter.toXQueryString(criteria));
|
58
|
|
59
|
Element element = criteriaCache.get(key);
|
60
|
List<String> xmls = (List<String>) element.getObjectValue();
|
61
|
|
62
|
return this.processSearchResults(xmls);
|
63
|
}
|
64
|
|
65
|
private List<D> processSearchResults(List<String> xmls) {
|
66
|
List<D> result = new ArrayList<D>();
|
67
|
|
68
|
for (String xml:xmls) {
|
69
|
D d = this.parseXml(xml);
|
70
|
|
71
|
this.updateIdCache(d.getResourceId(), xml);
|
72
|
|
73
|
result.add(d);
|
74
|
}
|
75
|
|
76
|
return result;
|
77
|
}
|
78
|
|
79
|
private D parseXml(String xml) {
|
80
|
try {
|
81
|
return converter.XmlToObject(xml);
|
82
|
} catch (JAXBException je) {
|
83
|
logger.error("Error parsing xml profile", je);
|
84
|
return null;
|
85
|
}
|
86
|
}
|
87
|
|
88
|
private void updateIdCache(String id, String xml) {
|
89
|
Element e = new Element(id, xml);
|
90
|
|
91
|
idCache.put(e);
|
92
|
}
|
93
|
|
94
|
@SuppressWarnings("unchecked")
|
95
|
public List<D> performQuickSearch(SearchCriteria criteria)
|
96
|
throws ISLookUpException {
|
97
|
if (logger.isDebugEnabled())
|
98
|
logger.debug("Performing quick search for query: "
|
99
|
+ converter.toXQueryString(criteria));
|
100
|
|
101
|
CriteriaCacheKey key = new CriteriaCacheKey(criteria);
|
102
|
key.setXQuery(converter.toXQueryString(criteria));
|
103
|
Element element = criteriaCache.get(key);
|
104
|
|
105
|
return this.processSearchResults( (List<String>) element.getObjectValue());
|
106
|
}
|
107
|
|
108
|
public List<String> fetch(String XQuery) {
|
109
|
throw new UnsupportedOperationException();
|
110
|
}
|
111
|
|
112
|
public D getUniqueResult(SearchCriteria criteria) throws ISLookUpException {
|
113
|
if (logger.isDebugEnabled())
|
114
|
logger.debug("Fetching unique result for query: " +
|
115
|
converter.toXQueryString(criteria));
|
116
|
|
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
|
|
135
|
public void setCriteriaCache(Ehcache criteriaCache) {
|
136
|
this.criteriaCache = criteriaCache;
|
137
|
}
|
138
|
|
139
|
public void setIdCache(Ehcache idCache) {
|
140
|
this.idCache = idCache;
|
141
|
}
|
142
|
}
|