Project

General

Profile

1
package eu.dnetlib.uoaadmintools.controllers;
2

    
3
import eu.dnetlib.uoaadmintools.dao.*;
4
import eu.dnetlib.uoaadmintools.entities.statistics.*;
5
import eu.dnetlib.uoaadmintools.handlers.NotFoundException;
6
import org.apache.log4j.Logger;
7
import org.springframework.beans.factory.annotation.Autowired;
8
import org.springframework.web.bind.annotation.*;
9

    
10
import java.util.*;
11

    
12
@RestController
13
@CrossOrigin(origins = "*")
14
public class StatisticsController {
15
    private final Logger log = Logger.getLogger(this.getClass());
16

    
17
    @Autowired
18
    private StatisticsDAO statisticsDAO;
19

    
20
    @RequestMapping(value = "/statistics", method = RequestMethod.GET)
21
    public List<Statistics> getAllStatistics() throws NotFoundException {
22
        log.info("getAllStatistics");
23
            List<Statistics> statistics = statisticsDAO.findAll();
24
         if(statistics == null){
25
            throw new NotFoundException("Statistics not found");
26
        }
27
            return statistics;
28
    }
29

    
30

    
31

    
32
    @RequestMapping(value = "/statistics/{pid}", method = RequestMethod.GET)
33
    public Statistics getStatistics(@PathVariable(value = "pid") String pid) throws NotFoundException {
34
        Statistics statistics = statisticsDAO.findByPid(pid);
35
        if(statistics == null){
36
            throw new NotFoundException("Statistics not found");
37
        }
38
        return statistics;
39
    }
40

    
41
    @RequestMapping(value = "/statistics/save", method = RequestMethod.POST)
42
    public Statistics insertStatistics(@RequestBody Statistics statistics) {
43
        Statistics savedStatistics = statisticsDAO.save(statistics);
44
        return savedStatistics;
45
    }
46

    
47

    
48

    
49
    @RequestMapping(value = "/statistics/delete", method = RequestMethod.POST)
50
    public Boolean deleteStatistics(@RequestBody List<String> statistics) throws Exception {
51
        for (String id: statistics) {
52
            statisticsDAO.delete(id);
53
         }
54
        return true;
55
    }
56

    
57

    
58

    
59

    
60
    @RequestMapping(value = "statistics/{pid}/{entity}/charts", method = RequestMethod.POST)
61
    public Statistics toggleCharts(@PathVariable(value = "pid") String pid, @PathVariable(value = "entity") String entity, @RequestBody String key, @RequestParam String status, @RequestParam String monitor) throws NotFoundException {
62
        Statistics statistics = statisticsDAO.findByPid(pid);
63
        if(statistics == null){
64
            throw new NotFoundException("Statistics not found  for community");
65
        }
66
        StatisticsEntity statisticsEntity = statistics.getEntities().get(entity);
67
        if(statisticsEntity == null ){
68
            throw new NotFoundException("Statistics not found for entity");
69
        }
70
        ChartsMap charts = statisticsEntity.getCharts();
71
        if(charts == null){
72
            throw new NotFoundException("Statistics not found - no charts");
73
        }
74
        StatisticsStatus statisticsStatus= charts.getMap().get(key);
75
        if(statisticsStatus == null){
76
            throw new NotFoundException("Statistics not found for key");
77
        }
78
        if(Boolean.parseBoolean(monitor)){
79
            statisticsStatus.setShowInMonitor(Boolean.parseBoolean(status));
80
        }else{
81
            statisticsStatus.setShowInDashboard(Boolean.parseBoolean(status));
82
        }
83
//        stats.put(key,statisticsStatus);
84
        return statisticsDAO.save(statistics);
85
    }
86
    @RequestMapping(value = "statistics/{pid}/{entity}/numbers", method = RequestMethod.POST)
87
    public Statistics toggleNumber(@PathVariable(value = "pid") String pid, @PathVariable(value = "entity") String entity, @RequestBody String key, @RequestParam String status, @RequestParam String monitor) throws NotFoundException {
88
        Statistics statistics = statisticsDAO.findByPid(pid);
89
        if(statistics == null){
90
            throw new NotFoundException("Statistics not found  for community");
91
        }
92
        StatisticsEntity statisticsEntity = statistics.getEntities().get(entity);
93
        if(statisticsEntity == null ){
94
            throw new NotFoundException("Statistics not found for entity");
95
        }
96
        NumbersMap numbers = statisticsEntity.getNumbers();
97
        if(numbers == null){
98
            throw new NotFoundException("Statistics not found - no numbers");
99
        }
100
        StatisticsStatus statisticsStatus= numbers.getMap().get(key);
101
        if(statisticsStatus == null){
102
            throw new NotFoundException("Statistics not found for key");
103
        }
104
        if(Boolean.parseBoolean(monitor)){
105
            statisticsStatus.setShowInMonitor(Boolean.parseBoolean(status));
106
        }else{
107
            statisticsStatus.setShowInDashboard(Boolean.parseBoolean(status));
108
        }
109
//        stats.put(key,statisticsStatus);
110
        return statisticsDAO.save(statistics);
111
    }
112

    
113

    
114
}
(10-10/12)