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
|
|
10
|
import org.apache.commons.dbutils.DbUtils;
|
11
|
import org.apache.log4j.Logger;
|
12
|
import org.springframework.beans.factory.annotation.Autowired;
|
13
|
import org.springframework.data.redis.core.HashOperations;
|
14
|
import org.springframework.stereotype.Repository;
|
15
|
|
16
|
import javax.annotation.PostConstruct;
|
17
|
//import javax.sql.DataSource;
|
18
|
|
19
|
import java.security.MessageDigest;
|
20
|
//import java.sql.Connection;
|
21
|
//import java.sql.PreparedStatement;
|
22
|
//import java.sql.ResultSet;
|
23
|
import java.util.ArrayList;
|
24
|
import java.util.List;
|
25
|
|
26
|
|
27
|
@Repository
|
28
|
public class BaseRepository {
|
29
|
//@Autowired
|
30
|
//private DataSourceBean dataSourceBean;
|
31
|
|
32
|
//private DataSource statsDB;
|
33
|
|
34
|
@Autowired
|
35
|
private SpringRedisConfiguration springRedisConfiguration;
|
36
|
|
37
|
//private RedisTemplate redisTemplate;
|
38
|
|
39
|
private HashOperations<String, String, String> jedis;
|
40
|
|
41
|
private final Logger log = Logger.getLogger(this.getClass());
|
42
|
|
43
|
@PostConstruct
|
44
|
public void initDB() {
|
45
|
//statsDB = dataSourceBean.getDataSource();
|
46
|
jedis = springRedisConfiguration.redisTemplate().opsForHash();
|
47
|
}
|
48
|
|
49
|
private static String MD5(String string) throws java.security.NoSuchAlgorithmException {
|
50
|
MessageDigest md = MessageDigest.getInstance("MD5");
|
51
|
md.update(string.getBytes());
|
52
|
|
53
|
byte byteData[] = md.digest();
|
54
|
StringBuilder sb = new StringBuilder();
|
55
|
for (byte aByteData : byteData) {
|
56
|
sb.append(Integer.toString((aByteData & 0xff) + 0x100, 16).substring(1));
|
57
|
}
|
58
|
|
59
|
return sb.toString();
|
60
|
}
|
61
|
|
62
|
public Result getNumber(String number){
|
63
|
String redis_result = jedis.get("STATS_NUMBERS",number);
|
64
|
if(redis_result != null){
|
65
|
return new Result("OK", "200", Integer.parseInt(redis_result.replaceAll(",","")));
|
66
|
}
|
67
|
return new Result("Not Found", "400", null);
|
68
|
}
|
69
|
|
70
|
public Result getFunder(String funder){
|
71
|
Boolean not_found = true;
|
72
|
ArrayList<String> items = new ArrayList<>();
|
73
|
items.add(funder.toLowerCase() + "pubstotal");
|
74
|
items.add(funder.toLowerCase() + "oapubs");
|
75
|
items.add(funder.toLowerCase() + "embpubs");
|
76
|
items.add(funder.toLowerCase() + "respubs");
|
77
|
items.add(funder.toLowerCase() + "projtotal");
|
78
|
items.add(funder.toLowerCase() + "projpubs");
|
79
|
|
80
|
List<String> result = jedis.multiGet("STATS_NUMBERS", items);
|
81
|
int pubs = 0, oa = 0, emb = 0, res = 0, proj = 0, proj_pubs = 0;
|
82
|
if (result.get(0) != null) {
|
83
|
pubs = Integer.parseInt(result.get(0).replaceAll(",",""));
|
84
|
not_found = false;
|
85
|
}
|
86
|
if (result.get(1) != null) {
|
87
|
oa = Integer.parseInt(result.get(1).replaceAll(",",""));
|
88
|
not_found = false;
|
89
|
}
|
90
|
if (result.get(2) != null) {
|
91
|
emb = Integer.parseInt(result.get(2).replaceAll(",",""));
|
92
|
not_found = false;
|
93
|
}
|
94
|
if (result.get(3) != null) {
|
95
|
res = Integer.parseInt(result.get(3).replaceAll(",",""));
|
96
|
not_found = false;
|
97
|
}
|
98
|
if (result.get(4) != null) {
|
99
|
proj = Integer.parseInt(result.get(4).replaceAll(",",""));
|
100
|
not_found = false;
|
101
|
}
|
102
|
if (result.get(5) != null) {
|
103
|
proj_pubs = Integer.parseInt(result.get(5).replaceAll(",",""));
|
104
|
not_found = false;
|
105
|
}
|
106
|
|
107
|
if(not_found){
|
108
|
return new Result("Not Found", "400", null);
|
109
|
}
|
110
|
|
111
|
return new Result("OK", "200", new Funder(pubs, oa, emb, res, proj, proj_pubs));
|
112
|
}
|
113
|
|
114
|
public Result getCommunity(String community){
|
115
|
Boolean not_found = true;
|
116
|
ArrayList<String> items = new ArrayList<>();
|
117
|
items.add(community.toLowerCase() + "pubs");
|
118
|
items.add(community.toLowerCase() + "oa");
|
119
|
items.add(community.toLowerCase() + "emb");
|
120
|
items.add(community.toLowerCase() + "res");
|
121
|
items.add(community.toLowerCase() + "proj");
|
122
|
items.add(community.toLowerCase() + "vo");
|
123
|
items.add(community.toLowerCase() + "datasets");
|
124
|
items.add(community.toLowerCase() + "software");
|
125
|
|
126
|
List<String> result = jedis.multiGet("STATS_NUMBERS", items);
|
127
|
int pubs = 0, oa = 0, emb = 0, res = 0, proj = 0, vo = 0, datasets = 0, software =0;
|
128
|
if (result.get(0) != null) {
|
129
|
pubs = Integer.parseInt(result.get(0).replaceAll(",",""));
|
130
|
not_found = false;
|
131
|
}
|
132
|
if (result.get(1) != null) {
|
133
|
oa = Integer.parseInt(result.get(1).replaceAll(",",""));
|
134
|
not_found = false;
|
135
|
}
|
136
|
if (result.get(2) != null) {
|
137
|
emb = Integer.parseInt(result.get(2).replaceAll(",",""));
|
138
|
not_found = false;
|
139
|
}
|
140
|
if (result.get(3) != null) {
|
141
|
res = Integer.parseInt(result.get(3).replaceAll(",",""));
|
142
|
not_found = false;
|
143
|
}
|
144
|
if (result.get(4) != null) {
|
145
|
proj = Integer.parseInt(result.get(4).replaceAll(",",""));
|
146
|
not_found = false;
|
147
|
}
|
148
|
if (result.get(5) != null) {
|
149
|
vo = Integer.parseInt(result.get(5).replaceAll(",",""));
|
150
|
not_found = false;
|
151
|
}
|
152
|
if (result.get(6) != null) {
|
153
|
datasets = Integer.parseInt(result.get(6).replaceAll(",",""));
|
154
|
not_found = false;
|
155
|
}
|
156
|
if (result.get(7) != null) {
|
157
|
software = Integer.parseInt(result.get(7).replaceAll(",",""));
|
158
|
not_found = false;
|
159
|
}
|
160
|
|
161
|
if(not_found){
|
162
|
return new Result("Not Found", "400", null);
|
163
|
}
|
164
|
|
165
|
return new Result("OK", "200", new Community(pubs, oa, emb, res, proj, vo, datasets, software));
|
166
|
}
|
167
|
}
|