1
|
package eu.dnetlib.openaire.exporter.funders;
|
2
|
|
3
|
import java.io.IOException;
|
4
|
import java.util.Date;
|
5
|
import java.util.List;
|
6
|
import java.util.Map;
|
7
|
import java.util.stream.Collectors;
|
8
|
|
9
|
import com.google.common.collect.Lists;
|
10
|
import eu.dnetlib.openaire.exporter.datasource.clients.ISLookupClient;
|
11
|
import eu.dnetlib.openaire.exporter.datasource.clients.MongoLoggerClient;
|
12
|
import eu.dnetlib.openaire.exporter.funders.context.Context;
|
13
|
import eu.dnetlib.openaire.exporter.funders.context.MappingUtils;
|
14
|
import eu.dnetlib.openaire.exporter.funders.model.ExtendedFunderDetails;
|
15
|
import eu.dnetlib.openaire.exporter.funders.model.FunderDetails;
|
16
|
import org.apache.commons.collections.MapUtils;
|
17
|
import org.apache.commons.logging.Log;
|
18
|
import org.apache.commons.logging.LogFactory;
|
19
|
import org.springframework.beans.factory.annotation.Autowired;
|
20
|
import org.springframework.cache.annotation.CacheEvict;
|
21
|
import org.springframework.cache.annotation.Cacheable;
|
22
|
import org.springframework.stereotype.Component;
|
23
|
|
24
|
@Component
|
25
|
public class FunderDao {
|
26
|
|
27
|
private static final Log log = LogFactory.getLog(FunderDao.class);
|
28
|
|
29
|
@Autowired
|
30
|
private ISLookupClient isLookupClient;
|
31
|
|
32
|
@Autowired
|
33
|
private MongoLoggerClient mongoLoggerClient;
|
34
|
|
35
|
@Cacheable("funders-cache")
|
36
|
public ExtendedFunderDetails getExtendedFunderDetails(final String funderId) throws FundersApiException {
|
37
|
log.info(String.format("cache miss for getFunderDetails('%s')", funderId));
|
38
|
final Context c = getContext(funderId);
|
39
|
final ExtendedFunderDetails details = MappingUtils.asExtendedFunderDetails(c);
|
40
|
details.setLastUpdateDate(getLastUpdateDate(c.getId()));
|
41
|
details.setRegistrationDate(getRegistrationDate(c.getId()));
|
42
|
|
43
|
return details;
|
44
|
}
|
45
|
|
46
|
public List<FunderDetails> listFunderDetails() throws FundersApiException {
|
47
|
try {
|
48
|
return isLookupClient.getContextMap().values().stream()
|
49
|
.map(c -> asFunderDetails(c))
|
50
|
.collect(Collectors.toList());
|
51
|
} catch (IOException e) {
|
52
|
throw new FundersApiException(e);
|
53
|
}
|
54
|
}
|
55
|
|
56
|
public List<String> listFunderIds() throws FundersApiException {
|
57
|
try {
|
58
|
return Lists.newArrayList(isLookupClient.getContextMap().keySet());
|
59
|
} catch (IOException e) {
|
60
|
throw new FundersApiException(e);
|
61
|
}
|
62
|
}
|
63
|
|
64
|
@CacheEvict(cacheNames = "funders-cache", allEntries = true)
|
65
|
public void dropCache() {
|
66
|
log.info("dropped funders info cache");
|
67
|
}
|
68
|
|
69
|
/* HELPERS */
|
70
|
|
71
|
private FunderDetails asFunderDetails(final Context c) {
|
72
|
return MappingUtils.asFunderDetails(c)
|
73
|
.setLastUpdateDate(getLastUpdateDate(c.getId()))
|
74
|
.setRegistrationDate(getRegistrationDate(c.getId()));
|
75
|
}
|
76
|
|
77
|
private Date getLastUpdateDate(final String id) {
|
78
|
return null;
|
79
|
}
|
80
|
|
81
|
private Date getRegistrationDate(final String id) {
|
82
|
return null;
|
83
|
}
|
84
|
|
85
|
private Context getContext(final String funderId) throws FundersApiException {
|
86
|
try {
|
87
|
final Map<String, Context> contextMap = isLookupClient.getContextMap();
|
88
|
if (MapUtils.isEmpty(contextMap)) {
|
89
|
throw new FundersApiException("Unable to find any context profile");
|
90
|
}
|
91
|
|
92
|
final Context context = contextMap.get(funderId);
|
93
|
if (context == null) {
|
94
|
throw new FundersApiException("Unexising funder id " + funderId);
|
95
|
}
|
96
|
return context;
|
97
|
} catch (IOException e) {
|
98
|
throw new FundersApiException(e);
|
99
|
}
|
100
|
}
|
101
|
|
102
|
}
|