Revision 51121
Added by Tsampikos Livisianos about 6 years ago
BaseRepository.java | ||
---|---|---|
6 | 6 |
import eu.dnetlib.statsapi.entity.Community; |
7 | 7 |
import eu.dnetlib.statsapi.entity.Funder; |
8 | 8 |
import eu.dnetlib.statsapi.entity.Result; |
9 |
import eu.dnetlib.statsapi.entity.StatsByAccessMode; |
|
9 | 10 |
|
10 | 11 |
import org.apache.commons.dbutils.DbUtils; |
11 | 12 |
import org.apache.log4j.Logger; |
... | ... | |
14 | 15 |
import org.springframework.stereotype.Repository; |
15 | 16 |
|
16 | 17 |
import javax.annotation.PostConstruct; |
17 |
//import javax.sql.DataSource;
|
|
18 |
import javax.sql.DataSource; |
|
18 | 19 |
|
19 | 20 |
import java.security.MessageDigest; |
20 |
//import java.sql.Connection;
|
|
21 |
//import java.sql.PreparedStatement;
|
|
22 |
//import java.sql.ResultSet;
|
|
21 |
import java.sql.Connection; |
|
22 |
import java.sql.PreparedStatement; |
|
23 |
import java.sql.ResultSet; |
|
23 | 24 |
import java.util.ArrayList; |
24 | 25 |
import java.util.List; |
25 | 26 |
|
26 | 27 |
|
27 | 28 |
@Repository |
28 | 29 |
public class BaseRepository { |
29 |
//@Autowired
|
|
30 |
//private DataSourceBean dataSourceBean;
|
|
30 |
@Autowired |
|
31 |
private DataSourceBean dataSourceBean; |
|
31 | 32 |
|
32 |
//private DataSource statsDB;
|
|
33 |
private DataSource statsDB; |
|
33 | 34 |
|
34 | 35 |
@Autowired |
35 | 36 |
private SpringRedisConfiguration springRedisConfiguration; |
... | ... | |
42 | 43 |
|
43 | 44 |
@PostConstruct |
44 | 45 |
public void initDB() { |
45 |
//statsDB = dataSourceBean.getDataSource();
|
|
46 |
statsDB = dataSourceBean.getDataSource(); |
|
46 | 47 |
jedis = springRedisConfiguration.redisTemplate().opsForHash(); |
47 | 48 |
} |
48 | 49 |
|
... | ... | |
112 | 113 |
} |
113 | 114 |
|
114 | 115 |
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"); |
|
116 |
Connection connection = null; |
|
117 |
PreparedStatement st = null; |
|
118 |
ResultSet rs = null; |
|
125 | 119 |
|
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 |
} |
|
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); |
|
160 | 129 |
|
161 |
if(not_found){ |
|
162 |
return new Result("Not Found", "400", null); |
|
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); |
|
163 | 181 |
} |
164 |
|
|
165 |
return new Result("OK", "200", new Community(pubs, oa, emb, res, proj, vo, datasets, software)); |
|
182 |
return new Result("Not Found", "404", null); |
|
166 | 183 |
} |
167 | 184 |
} |
Also available in: Unified diff
replace old community stats with configurable queries