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.stereotype.Repository;
|
16
|
|
17
|
import javax.annotation.PostConstruct;
|
18
|
import javax.sql.DataSource;
|
19
|
|
20
|
import java.security.MessageDigest;
|
21
|
import java.sql.Connection;
|
22
|
import java.sql.PreparedStatement;
|
23
|
import java.sql.ResultSet;
|
24
|
import java.util.ArrayList;
|
25
|
import java.util.List;
|
26
|
|
27
|
|
28
|
@Repository
|
29
|
public class BaseRepository {
|
30
|
@Autowired
|
31
|
private DataSourceBean dataSourceBean;
|
32
|
|
33
|
private DataSource statsDB;
|
34
|
|
35
|
@Autowired
|
36
|
private SpringRedisConfiguration springRedisConfiguration;
|
37
|
|
38
|
//private RedisTemplate redisTemplate;
|
39
|
|
40
|
private HashOperations<String, String, String> jedis;
|
41
|
|
42
|
private final Logger log = Logger.getLogger(this.getClass());
|
43
|
|
44
|
@PostConstruct
|
45
|
public void initDB() {
|
46
|
statsDB = dataSourceBean.getDataSource();
|
47
|
jedis = springRedisConfiguration.redisTemplate().opsForHash();
|
48
|
}
|
49
|
|
50
|
private static String MD5(String string) throws java.security.NoSuchAlgorithmException {
|
51
|
MessageDigest md = MessageDigest.getInstance("MD5");
|
52
|
md.update(string.getBytes());
|
53
|
|
54
|
byte byteData[] = md.digest();
|
55
|
StringBuilder sb = new StringBuilder();
|
56
|
for (byte aByteData : byteData) {
|
57
|
sb.append(Integer.toString((aByteData & 0xff) + 0x100, 16).substring(1));
|
58
|
}
|
59
|
|
60
|
return sb.toString();
|
61
|
}
|
62
|
|
63
|
public Result getNumber(String number){
|
64
|
String redis_result = jedis.get("STATS_NUMBERS",number);
|
65
|
if(redis_result != null){
|
66
|
return new Result("OK", "200", Integer.parseInt(redis_result.replaceAll(",","")));
|
67
|
}
|
68
|
return new Result("Not Found", "400", null);
|
69
|
}
|
70
|
|
71
|
public Result getFunder(String funder){
|
72
|
Boolean not_found = true;
|
73
|
ArrayList<String> items = new ArrayList<>();
|
74
|
items.add(funder.toLowerCase() + "pubstotal");
|
75
|
items.add(funder.toLowerCase() + "oapubs");
|
76
|
items.add(funder.toLowerCase() + "embpubs");
|
77
|
items.add(funder.toLowerCase() + "respubs");
|
78
|
items.add(funder.toLowerCase() + "projtotal");
|
79
|
items.add(funder.toLowerCase() + "projpubs");
|
80
|
|
81
|
List<String> result = jedis.multiGet("STATS_NUMBERS", items);
|
82
|
int pubs = 0, oa = 0, emb = 0, res = 0, proj = 0, proj_pubs = 0;
|
83
|
if (result.get(0) != null) {
|
84
|
pubs = Integer.parseInt(result.get(0).replaceAll(",",""));
|
85
|
not_found = false;
|
86
|
}
|
87
|
if (result.get(1) != null) {
|
88
|
oa = Integer.parseInt(result.get(1).replaceAll(",",""));
|
89
|
not_found = false;
|
90
|
}
|
91
|
if (result.get(2) != null) {
|
92
|
emb = Integer.parseInt(result.get(2).replaceAll(",",""));
|
93
|
not_found = false;
|
94
|
}
|
95
|
if (result.get(3) != null) {
|
96
|
res = Integer.parseInt(result.get(3).replaceAll(",",""));
|
97
|
not_found = false;
|
98
|
}
|
99
|
if (result.get(4) != null) {
|
100
|
proj = Integer.parseInt(result.get(4).replaceAll(",",""));
|
101
|
not_found = false;
|
102
|
}
|
103
|
if (result.get(5) != null) {
|
104
|
proj_pubs = Integer.parseInt(result.get(5).replaceAll(",",""));
|
105
|
not_found = false;
|
106
|
}
|
107
|
|
108
|
if(not_found){
|
109
|
return new Result("Not Found", "400", null);
|
110
|
}
|
111
|
|
112
|
return new Result("OK", "200", new Funder(pubs, oa, emb, res, proj, proj_pubs));
|
113
|
}
|
114
|
|
115
|
public Result getCommunity(String community){
|
116
|
Connection connection = null;
|
117
|
PreparedStatement st = null;
|
118
|
ResultSet rs = null;
|
119
|
|
120
|
String redisResponse = jedis.get("community:" + community, "result");
|
121
|
try {
|
122
|
if (redisResponse != null) {
|
123
|
return new Result("OK","200", new ObjectMapper().readValue(redisResponse, Community.class));
|
124
|
} else {
|
125
|
connection = statsDB.getConnection();
|
126
|
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;");
|
127
|
st.setString(1, community);
|
128
|
st.setString(2, community);
|
129
|
|
130
|
StatsByAccessMode publications = new StatsByAccessMode(0,0,0,0,0);
|
131
|
StatsByAccessMode software = new StatsByAccessMode(0,0,0,0,0);
|
132
|
StatsByAccessMode datasets = new StatsByAccessMode(0,0,0,0,0);
|
133
|
|
134
|
rs = st.executeQuery();
|
135
|
while (rs.next()) {
|
136
|
if(rs.getString(1).equals("publication")){
|
137
|
publications = new StatsByAccessMode(rs.getInt(2), rs.getInt(3), rs.getInt(4), rs.getInt(5), rs.getInt(6));
|
138
|
} else if(rs.getString(1).equals("software")){
|
139
|
software = new StatsByAccessMode(rs.getInt(2), rs.getInt(3), rs.getInt(4), rs.getInt(5), rs.getInt(6));
|
140
|
} else if(rs.getString(1).equals("dataset")){
|
141
|
datasets = new StatsByAccessMode(rs.getInt(2), rs.getInt(3), rs.getInt(4), rs.getInt(5), rs.getInt(6));
|
142
|
}
|
143
|
}
|
144
|
rs.close();
|
145
|
st.close();
|
146
|
|
147
|
st = connection.prepareStatement("SELECT COUNT(DISTINCT pr.id) FROM result_concepts rc, project_results pr, project p WHERE p.id=pr.id AND pr.result=rc.id AND (rc.concept=? OR rc.concept LIKE ? || '::%');");
|
148
|
st.setString(1, community);
|
149
|
st.setString(2, community);
|
150
|
|
151
|
int projects = 0;
|
152
|
rs = st.executeQuery();
|
153
|
while (rs.next()) {
|
154
|
projects = rs.getInt(1);
|
155
|
}
|
156
|
rs.close();
|
157
|
st.close();
|
158
|
|
159
|
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';");
|
160
|
st.setString(1, community);
|
161
|
|
162
|
int virtual_organizations = 0;
|
163
|
rs = st.executeQuery();
|
164
|
while (rs.next()) {
|
165
|
virtual_organizations = rs.getInt(1);
|
166
|
}
|
167
|
|
168
|
Community com = new Community(publications, datasets, software, projects, virtual_organizations);
|
169
|
String redis_key = "community:" + community;
|
170
|
jedis.put(redis_key, "persistent", "false");
|
171
|
jedis.put(redis_key, "result", new ObjectMapper().writeValueAsString(com));
|
172
|
|
173
|
return new Result("OK", "200", com);
|
174
|
}
|
175
|
} catch (Exception e) {
|
176
|
log.error(e.getMessage());
|
177
|
} finally {
|
178
|
DbUtils.closeQuietly(rs);
|
179
|
DbUtils.closeQuietly(st);
|
180
|
DbUtils.closeQuietly(connection);
|
181
|
}
|
182
|
return new Result("Not Found", "404", null);
|
183
|
}
|
184
|
}
|