Project

General

Profile

1 50222 konstantin
package eu.dnetlib.uoaadmintools.controllers;
2
3 51032 argiro.kok
import eu.dnetlib.uoaadmintools.dao.*;
4 51273 argiro.kok
import eu.dnetlib.uoaadmintools.entities.statistics.*;
5 59470 konstantin
import eu.dnetlib.uoaadmintoolslibrary.handlers.ContentNotFoundException;
6 50222 konstantin
import org.apache.log4j.Logger;
7 51032 argiro.kok
import org.springframework.beans.factory.annotation.Autowired;
8 50222 konstantin
import org.springframework.web.bind.annotation.*;
9
10
import java.util.*;
11
12
@RestController
13
@CrossOrigin(origins = "*")
14 51032 argiro.kok
public class StatisticsController {
15 50222 konstantin
    private final Logger log = Logger.getLogger(this.getClass());
16
17
    @Autowired
18 51032 argiro.kok
    private StatisticsDAO statisticsDAO;
19 50222 konstantin
20 51032 argiro.kok
    @RequestMapping(value = "/statistics", method = RequestMethod.GET)
21 53941 argiro.kok
    public List<Statistics> getAllStatistics() throws ContentNotFoundException {
22 51032 argiro.kok
        log.info("getAllStatistics");
23
            List<Statistics> statistics = statisticsDAO.findAll();
24 51049 argiro.kok
         if(statistics == null){
25 53941 argiro.kok
            throw new ContentNotFoundException("Statistics not found");
26 51049 argiro.kok
        }
27 51032 argiro.kok
            return statistics;
28 50222 konstantin
    }
29
30
31
32 51032 argiro.kok
    @RequestMapping(value = "/statistics/{pid}", method = RequestMethod.GET)
33 53941 argiro.kok
    public Statistics getStatistics(@PathVariable(value = "pid") String pid) throws ContentNotFoundException {
34 51032 argiro.kok
        Statistics statistics = statisticsDAO.findByPid(pid);
35 51049 argiro.kok
        if(statistics == null){
36 53941 argiro.kok
            throw new ContentNotFoundException("Statistics not found");
37 51049 argiro.kok
        }
38 51032 argiro.kok
        return statistics;
39 50222 konstantin
    }
40
41 58368 konstantin
    @RequestMapping(value = "/statistics/{id}/toggle", method = RequestMethod.POST)
42
    public Boolean toggleStatistics(@PathVariable String id) throws Exception {
43
        Statistics statistics = statisticsDAO.findById(id);
44
        boolean status = statistics.getIsActive();
45
        statistics.setIsActive(!status);
46
        statisticsDAO.save(statistics);
47
        return statistics.getIsActive();
48
    }
49
50 51032 argiro.kok
    @RequestMapping(value = "/statistics/save", method = RequestMethod.POST)
51
    public Statistics insertStatistics(@RequestBody Statistics statistics) {
52
        Statistics savedStatistics = statisticsDAO.save(statistics);
53
        return savedStatistics;
54 50222 konstantin
    }
55
56
57
58 51032 argiro.kok
    @RequestMapping(value = "/statistics/delete", method = RequestMethod.POST)
59
    public Boolean deleteStatistics(@RequestBody List<String> statistics) throws Exception {
60
        for (String id: statistics) {
61
            statisticsDAO.delete(id);
62
         }
63 50222 konstantin
        return true;
64
    }
65
66
67
68 51273 argiro.kok
69
    @RequestMapping(value = "statistics/{pid}/{entity}/charts", method = RequestMethod.POST)
70 53941 argiro.kok
    public Statistics toggleCharts(@PathVariable(value = "pid") String pid, @PathVariable(value = "entity") String entity, @RequestBody String key, @RequestParam String status, @RequestParam String monitor) throws ContentNotFoundException {
71 51032 argiro.kok
        Statistics statistics = statisticsDAO.findByPid(pid);
72 51049 argiro.kok
        if(statistics == null){
73 59470 konstantin
            throw new ContentNotFoundException("Statistics not found  for portal");
74 51049 argiro.kok
        }
75 51273 argiro.kok
        StatisticsEntity statisticsEntity = statistics.getEntities().get(entity);
76
        if(statisticsEntity == null ){
77 53941 argiro.kok
            throw new ContentNotFoundException("Statistics not found for entity");
78 51273 argiro.kok
        }
79
        ChartsMap charts = statisticsEntity.getCharts();
80
        if(charts == null){
81 53941 argiro.kok
            throw new ContentNotFoundException("Statistics not found - no charts");
82 51273 argiro.kok
        }
83
        StatisticsStatus statisticsStatus= charts.getMap().get(key);
84 51049 argiro.kok
        if(statisticsStatus == null){
85 53941 argiro.kok
            throw new ContentNotFoundException("Statistics not found for key");
86 51049 argiro.kok
        }
87 51273 argiro.kok
        if(Boolean.parseBoolean(monitor)){
88
            statisticsStatus.setShowInMonitor(Boolean.parseBoolean(status));
89 51032 argiro.kok
        }else{
90
            statisticsStatus.setShowInDashboard(Boolean.parseBoolean(status));
91 50222 konstantin
        }
92 51273 argiro.kok
//        stats.put(key,statisticsStatus);
93 51032 argiro.kok
        return statisticsDAO.save(statistics);
94 50222 konstantin
    }
95 51273 argiro.kok
    @RequestMapping(value = "statistics/{pid}/{entity}/numbers", method = RequestMethod.POST)
96 53941 argiro.kok
    public Statistics toggleNumber(@PathVariable(value = "pid") String pid, @PathVariable(value = "entity") String entity, @RequestBody String key, @RequestParam String status, @RequestParam String monitor) throws ContentNotFoundException {
97 51273 argiro.kok
        Statistics statistics = statisticsDAO.findByPid(pid);
98
        if(statistics == null){
99 59470 konstantin
            throw new ContentNotFoundException("Statistics not found  for portal");
100 51273 argiro.kok
        }
101
        StatisticsEntity statisticsEntity = statistics.getEntities().get(entity);
102
        if(statisticsEntity == null ){
103 53941 argiro.kok
            throw new ContentNotFoundException("Statistics not found for entity");
104 51273 argiro.kok
        }
105
        NumbersMap numbers = statisticsEntity.getNumbers();
106
        if(numbers == null){
107 53941 argiro.kok
            throw new ContentNotFoundException("Statistics not found - no numbers");
108 51273 argiro.kok
        }
109
        StatisticsStatus statisticsStatus= numbers.getMap().get(key);
110
        if(statisticsStatus == null){
111 53941 argiro.kok
            throw new ContentNotFoundException("Statistics not found for key");
112 51273 argiro.kok
        }
113
        if(Boolean.parseBoolean(monitor)){
114
            statisticsStatus.setShowInMonitor(Boolean.parseBoolean(status));
115
        }else{
116
            statisticsStatus.setShowInDashboard(Boolean.parseBoolean(status));
117
        }
118
//        stats.put(key,statisticsStatus);
119
        return statisticsDAO.save(statistics);
120
    }
121 50222 konstantin
122
123 51032 argiro.kok
}