Revision 51326
Added by Tsampikos Livisianos about 6 years ago
BaseRepository.java | ||
---|---|---|
12 | 12 |
import org.apache.log4j.Logger; |
13 | 13 |
import org.springframework.beans.factory.annotation.Autowired; |
14 | 14 |
import org.springframework.data.redis.core.HashOperations; |
15 |
import org.springframework.data.redis.core.RedisTemplate; |
|
15 | 16 |
import org.springframework.stereotype.Repository; |
16 | 17 |
|
17 | 18 |
import javax.annotation.PostConstruct; |
... | ... | |
23 | 24 |
import java.sql.ResultSet; |
24 | 25 |
import java.util.ArrayList; |
25 | 26 |
import java.util.List; |
27 |
import java.util.Set; |
|
26 | 28 |
|
27 |
|
|
28 | 29 |
@Repository |
29 | 30 |
public class BaseRepository { |
30 | 31 |
@Autowired |
... | ... | |
112 | 113 |
return new Result("OK", "200", new Funder(pubs, oa, emb, res, proj, proj_pubs)); |
113 | 114 |
} |
114 | 115 |
|
115 |
public Result getCommunity(String community){ |
|
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){ |
|
116 | 127 |
Connection connection = null; |
117 | 128 |
PreparedStatement st = null; |
118 | 129 |
ResultSet rs = null; |
119 | 130 |
|
120 |
String redisResponse = jedis.get("community:" + community, "result"); |
|
131 |
String redisKey = "community:" + community_id; |
|
132 |
String redisResponse = jedis.get(redisKey, "result"); |
|
121 | 133 |
try { |
122 | 134 |
if (redisResponse != null) { |
123 | 135 |
return new Result("OK","200", new ObjectMapper().readValue(redisResponse, Community.class)); |
124 | 136 |
} else { |
125 | 137 |
connection = statsDB.getConnection(); |
126 | 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;"); |
127 |
st.setString(1, community); |
|
128 |
st.setString(2, community); |
|
139 |
st.setString(1, community_id);
|
|
140 |
st.setString(2, community_id);
|
|
129 | 141 |
|
130 | 142 |
StatsByAccessMode publications = new StatsByAccessMode(); |
131 | 143 |
StatsByAccessMode software = new StatsByAccessMode(); |
... | ... | |
133 | 145 |
|
134 | 146 |
rs = st.executeQuery(); |
135 | 147 |
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)); |
|
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; |
|
142 | 158 |
} |
143 | 159 |
} |
144 | 160 |
rs.close(); |
145 | 161 |
st.close(); |
146 | 162 |
|
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); |
|
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);
|
|
150 | 166 |
|
151 | 167 |
int projects = 0; |
152 | 168 |
rs = st.executeQuery(); |
153 | 169 |
while (rs.next()) { |
154 |
projects = rs.getInt(1); |
|
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 |
} |
|
155 | 178 |
} |
156 | 179 |
rs.close(); |
157 | 180 |
st.close(); |
158 | 181 |
|
159 | 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';"); |
160 |
st.setString(1, community); |
|
183 |
st.setString(1, community_id);
|
|
161 | 184 |
|
162 | 185 |
int virtual_organizations = 0; |
163 | 186 |
rs = st.executeQuery(); |
... | ... | |
165 | 188 |
virtual_organizations = rs.getInt(1); |
166 | 189 |
} |
167 | 190 |
|
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)); |
|
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)); |
|
172 | 194 |
|
173 |
return new Result("OK", "200", com); |
|
195 |
return new Result("OK", "200", community);
|
|
174 | 196 |
} |
175 | 197 |
} catch (Exception e) { |
176 | 198 |
log.error(e.getMessage()); |
Also available in: Unified diff
add project count to each result type