Search in sources :

Example 1 with PlaceCount

use of eu.einfracentral.dto.PlaceCount in project resource-catalogue by madgeek-arc.

the class StatisticsManager method servicesPerPlace.

@Override
public List<PlaceCount> servicesPerPlace(String providerId) {
    NamedParameterJdbcTemplate namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
    MapSqlParameterSource in = new MapSqlParameterSource();
    in.addValue("resource_organisation", providerId);
    String query = "SELECT unnest(geographical_availabilities) AS geographical_availability, count(unnest(geographical_availabilities)) AS count FROM infra_service_view WHERE latest=true AND active = true ";
    if (providerId != null) {
        query += " AND :resource_organisation=resource_organisation";
    }
    query += " GROUP BY unnest(geographical_availabilities);";
    List<Map<String, Object>> records = namedParameterJdbcTemplate.queryForList(query, in);
    Map<String, Integer> mapCounts = new HashMap<>();
    List<PlaceCount> placeCounts = new ArrayList<>();
    for (Map<String, Object> record : records) {
        if (record.get("geographical_availability").toString().equalsIgnoreCase("EU")) {
            for (String geographical_availability : vocabularyService.getRegion("EU")) {
                int count = Integer.parseInt(record.get("count").toString());
                if (mapCounts.containsKey(geographical_availability)) {
                    count += mapCounts.get(geographical_availability);
                }
                mapCounts.put(geographical_availability, count);
            }
        } else if (record.get("geographical_availability").toString().equalsIgnoreCase("WW")) {
            for (String geographical_availability : vocabularyService.getRegion("WW")) {
                int count = Integer.parseInt(record.get("count").toString());
                if (mapCounts.containsKey(geographical_availability)) {
                    count += mapCounts.get(geographical_availability);
                }
                mapCounts.put(geographical_availability, count);
            }
        } else {
            mapCounts.put(record.get("geographical_availability").toString(), Integer.parseInt(record.get("count").toString()));
        }
    }
    for (Map.Entry<String, Integer> entry : mapCounts.entrySet()) {
        placeCounts.add(new PlaceCount(entry.getKey(), entry.getValue()));
    }
    return placeCounts;
}
Also used : PlaceCount(eu.einfracentral.dto.PlaceCount) NamedParameterJdbcTemplate(org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate) MapSqlParameterSource(org.springframework.jdbc.core.namedparam.MapSqlParameterSource)

Aggregations

PlaceCount (eu.einfracentral.dto.PlaceCount)1 MapSqlParameterSource (org.springframework.jdbc.core.namedparam.MapSqlParameterSource)1 NamedParameterJdbcTemplate (org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate)1