1
|
package eu.dnetlib.statsapi.repos;
|
2
|
|
3
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
4
|
|
5
|
|
6
|
import eu.dnetlib.statsapi.entity.Community;
|
7
|
import eu.dnetlib.statsapi.entity.Funder;
|
8
|
import eu.dnetlib.statsapi.entity.Result;
|
9
|
import eu.dnetlib.statsapi.entity.StatsByAccessMode;
|
10
|
|
11
|
import org.apache.commons.dbutils.DbUtils;
|
12
|
import org.apache.log4j.Logger;
|
13
|
import org.springframework.beans.factory.annotation.Autowired;
|
14
|
import org.springframework.data.redis.core.HashOperations;
|
15
|
import org.springframework.data.redis.core.RedisTemplate;
|
16
|
import org.springframework.stereotype.Repository;
|
17
|
|
18
|
import javax.annotation.PostConstruct;
|
19
|
import javax.sql.DataSource;
|
20
|
|
21
|
import java.security.MessageDigest;
|
22
|
import java.sql.Connection;
|
23
|
import java.sql.PreparedStatement;
|
24
|
import java.sql.ResultSet;
|
25
|
import java.util.ArrayList;
|
26
|
import java.util.List;
|
27
|
import java.util.Set;
|
28
|
|
29
|
@Repository
|
30
|
public class BaseRepository {
|
31
|
@Autowired
|
32
|
private DataSourceBean dataSourceBean;
|
33
|
|
34
|
private DataSource statsDB;
|
35
|
|
36
|
@Autowired
|
37
|
private SpringRedisConfiguration springRedisConfiguration;
|
38
|
|
39
|
//private RedisTemplate redisTemplate;
|
40
|
|
41
|
private HashOperations<String, String, String> jedis;
|
42
|
|
43
|
private final Logger log = Logger.getLogger(this.getClass());
|
44
|
|
45
|
@PostConstruct
|
46
|
public void initDB() {
|
47
|
statsDB = dataSourceBean.getDataSource();
|
48
|
jedis = springRedisConfiguration.redisTemplate().opsForHash();
|
49
|
}
|
50
|
|
51
|
private static String MD5(String string) throws java.security.NoSuchAlgorithmException {
|
52
|
MessageDigest md = MessageDigest.getInstance("MD5");
|
53
|
md.update(string.getBytes());
|
54
|
|
55
|
byte byteData[] = md.digest();
|
56
|
StringBuilder sb = new StringBuilder();
|
57
|
for (byte aByteData : byteData) {
|
58
|
sb.append(Integer.toString((aByteData & 0xff) + 0x100, 16).substring(1));
|
59
|
}
|
60
|
|
61
|
return sb.toString();
|
62
|
}
|
63
|
|
64
|
public Result getNumber(String number){
|
65
|
String redis_result = jedis.get("STATS_NUMBERS",number);
|
66
|
if(redis_result != null){
|
67
|
return new Result("OK", "200", Integer.parseInt(redis_result.replaceAll(",","")));
|
68
|
}
|
69
|
return new Result("Not Found", "400", null);
|
70
|
}
|
71
|
|
72
|
public Result getFunder(String funder){
|
73
|
Boolean not_found = true;
|
74
|
ArrayList<String> items = new ArrayList<>();
|
75
|
items.add(funder.toLowerCase() + "pubstotal");
|
76
|
items.add(funder.toLowerCase() + "oapubs");
|
77
|
items.add(funder.toLowerCase() + "embpubs");
|
78
|
items.add(funder.toLowerCase() + "respubs");
|
79
|
items.add(funder.toLowerCase() + "projtotal");
|
80
|
items.add(funder.toLowerCase() + "projpubs");
|
81
|
|
82
|
List<String> result = jedis.multiGet("STATS_NUMBERS", items);
|
83
|
int pubs = 0, oa = 0, emb = 0, res = 0, proj = 0, proj_pubs = 0;
|
84
|
if (result.get(0) != null) {
|
85
|
pubs = Integer.parseInt(result.get(0).replaceAll(",",""));
|
86
|
not_found = false;
|
87
|
}
|
88
|
if (result.get(1) != null) {
|
89
|
oa = Integer.parseInt(result.get(1).replaceAll(",",""));
|
90
|
not_found = false;
|
91
|
}
|
92
|
if (result.get(2) != null) {
|
93
|
emb = Integer.parseInt(result.get(2).replaceAll(",",""));
|
94
|
not_found = false;
|
95
|
}
|
96
|
if (result.get(3) != null) {
|
97
|
res = Integer.parseInt(result.get(3).replaceAll(",",""));
|
98
|
not_found = false;
|
99
|
}
|
100
|
if (result.get(4) != null) {
|
101
|
proj = Integer.parseInt(result.get(4).replaceAll(",",""));
|
102
|
not_found = false;
|
103
|
}
|
104
|
if (result.get(5) != null) {
|
105
|
proj_pubs = Integer.parseInt(result.get(5).replaceAll(",",""));
|
106
|
not_found = false;
|
107
|
}
|
108
|
|
109
|
if(not_found){
|
110
|
return new Result("Not Found", "400", null);
|
111
|
}
|
112
|
|
113
|
return new Result("OK", "200", new Funder(pubs, oa, emb, res, proj, proj_pubs));
|
114
|
}
|
115
|
|
116
|
public Result refreshCommunities() {
|
117
|
RedisTemplate<String, String> jd = springRedisConfiguration.redisTemplate();
|
118
|
Set<String> keys = jd.keys("community:*");
|
119
|
for(String key : keys) {
|
120
|
jd.delete(key);
|
121
|
getCommunity(key.substring(key.lastIndexOf(":")+1));
|
122
|
}
|
123
|
return new Result("OK", "200", null);
|
124
|
}
|
125
|
|
126
|
public Result getCommunity(String community_id){
|
127
|
Connection connection = null;
|
128
|
PreparedStatement st = null;
|
129
|
ResultSet rs = null;
|
130
|
|
131
|
String redisKey = "community:" + community_id;
|
132
|
String redisResponse = jedis.get(redisKey, "result");
|
133
|
try {
|
134
|
if (redisResponse != null) {
|
135
|
return new Result("OK","200", new ObjectMapper().readValue(redisResponse, Community.class));
|
136
|
} else {
|
137
|
connection = statsDB.getConnection();
|
138
|
st = connection.prepareStatement("SELECT r.type, COUNT(DISTINCT r.id) AS total, COUNT(DISTINCT CASE WHEN r.bestlicense='Open Access' THEN r.id END) AS open_access, COUNT(DISTINCT CASE WHEN r.bestlicense='Embargo' THEN r.id END) AS embargo, COUNT(DISTINCT CASE WHEN r.bestlicense='Restricted' THEN r.id END) AS restricted, COUNT(DISTINCT CASE WHEN r.bestlicense='Closed Access' THEN r.id END) AS closed_access FROM result_concepts rc, result r WHERE rc.id=r.id AND (rc.concept=? OR rc.concept LIKE ? || '::%') GROUP BY r.type;");
|
139
|
st.setString(1, community_id);
|
140
|
st.setString(2, community_id);
|
141
|
|
142
|
StatsByAccessMode publications = new StatsByAccessMode();
|
143
|
StatsByAccessMode software = new StatsByAccessMode();
|
144
|
StatsByAccessMode datasets = new StatsByAccessMode();
|
145
|
|
146
|
rs = st.executeQuery();
|
147
|
while (rs.next()) {
|
148
|
switch (rs.getString(1)) {
|
149
|
case "publication":
|
150
|
publications = new StatsByAccessMode(rs.getInt(2), rs.getInt(3), rs.getInt(4), rs.getInt(5), rs.getInt(6));
|
151
|
break;
|
152
|
case "software":
|
153
|
software = new StatsByAccessMode(rs.getInt(2), rs.getInt(3), rs.getInt(4), rs.getInt(5), rs.getInt(6));
|
154
|
break;
|
155
|
case "dataset":
|
156
|
datasets = new StatsByAccessMode(rs.getInt(2), rs.getInt(3), rs.getInt(4), rs.getInt(5), rs.getInt(6));
|
157
|
break;
|
158
|
}
|
159
|
}
|
160
|
rs.close();
|
161
|
st.close();
|
162
|
|
163
|
st = connection.prepareStatement("SELECT r.type, COUNT(DISTINCT pr.id) FROM result r, result_concepts rc, project_results pr WHERE r.id=rc.id AND r.id=pr.result AND (rc.concept=? OR rc.concept LIKE ? || '::%') GROUP BY r.type;");
|
164
|
st.setString(1, community_id);
|
165
|
st.setString(2, community_id);
|
166
|
|
167
|
int projects = 0;
|
168
|
rs = st.executeQuery();
|
169
|
while (rs.next()) {
|
170
|
projects += rs.getInt(2);
|
171
|
if(rs.getString(1).equals("publication")) {
|
172
|
publications.setProjects(rs.getInt(2));
|
173
|
} else if(rs.getString(1).equals("software")){
|
174
|
software.setProjects(rs.getInt(2));
|
175
|
} else if(rs.getString(1).equals("dataset")){
|
176
|
datasets.setProjects(rs.getInt(2));
|
177
|
}
|
178
|
}
|
179
|
rs.close();
|
180
|
st.close();
|
181
|
|
182
|
st = connection.prepareStatement("SELECT COUNT(DISTINCT c.name) FROM result_concepts rc, concept c, category cat WHERE c.id=rc.concept AND cat.id=c.category AND cat.id=? || '::virtual';");
|
183
|
st.setString(1, community_id);
|
184
|
|
185
|
int virtual_organizations = 0;
|
186
|
rs = st.executeQuery();
|
187
|
while (rs.next()) {
|
188
|
virtual_organizations = rs.getInt(1);
|
189
|
}
|
190
|
|
191
|
Community community = new Community(publications, datasets, software, projects, virtual_organizations);
|
192
|
jedis.put(redisKey, "persistent", "false");
|
193
|
jedis.put(redisKey, "result", new ObjectMapper().writeValueAsString(community));
|
194
|
|
195
|
return new Result("OK", "200", community);
|
196
|
}
|
197
|
} catch (Exception e) {
|
198
|
log.error(e.getMessage());
|
199
|
} finally {
|
200
|
DbUtils.closeQuietly(rs);
|
201
|
DbUtils.closeQuietly(st);
|
202
|
DbUtils.closeQuietly(connection);
|
203
|
}
|
204
|
return new Result("Not Found", "404", null);
|
205
|
}
|
206
|
}
|