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.ContentNotFoundException;
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 ContentNotFoundException {
22
        log.info("getAllStatistics");
23
            List<Statistics> statistics = statisticsDAO.findAll();
24
         if(statistics == null){
25
            throw new ContentNotFoundException("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 ContentNotFoundException {
34
        Statistics statistics = statisticsDAO.findByPid(pid);
35
        if(statistics == null){
36
            throw new ContentNotFoundException("Statistics not found");
37
        }
38
        return statistics;
39
    }
40

    
41
    @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
    @RequestMapping(value = "/statistics/save", method = RequestMethod.POST)
51
    public Statistics insertStatistics(@RequestBody Statistics statistics) {
52
        Statistics savedStatistics = statisticsDAO.save(statistics);
53
        return savedStatistics;
54
    }
55

    
56

    
57

    
58
    @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
        return true;
64
    }
65

    
66

    
67

    
68

    
69
    @RequestMapping(value = "statistics/{pid}/{entity}/charts", method = RequestMethod.POST)
70
    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
        Statistics statistics = statisticsDAO.findByPid(pid);
72
        if(statistics == null){
73
            throw new ContentNotFoundException("Statistics not found  for portal");
74
        }
75
        StatisticsEntity statisticsEntity = statistics.getEntities().get(entity);
76
        if(statisticsEntity == null ){
77
            throw new ContentNotFoundException("Statistics not found for entity");
78
        }
79
        ChartsMap charts = statisticsEntity.getCharts();
80
        if(charts == null){
81
            throw new ContentNotFoundException("Statistics not found - no charts");
82
        }
83
        StatisticsStatus statisticsStatus= charts.getMap().get(key);
84
        if(statisticsStatus == null){
85
            throw new ContentNotFoundException("Statistics not found for key");
86
        }
87
        if(Boolean.parseBoolean(monitor)){
88
            statisticsStatus.setShowInMonitor(Boolean.parseBoolean(status));
89
        }else{
90
            statisticsStatus.setShowInDashboard(Boolean.parseBoolean(status));
91
        }
92
//        stats.put(key,statisticsStatus);
93
        return statisticsDAO.save(statistics);
94
    }
95
    @RequestMapping(value = "statistics/{pid}/{entity}/numbers", method = RequestMethod.POST)
96
    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
        Statistics statistics = statisticsDAO.findByPid(pid);
98
        if(statistics == null){
99
            throw new ContentNotFoundException("Statistics not found  for portal");
100
        }
101
        StatisticsEntity statisticsEntity = statistics.getEntities().get(entity);
102
        if(statisticsEntity == null ){
103
            throw new ContentNotFoundException("Statistics not found for entity");
104
        }
105
        NumbersMap numbers = statisticsEntity.getNumbers();
106
        if(numbers == null){
107
            throw new ContentNotFoundException("Statistics not found - no numbers");
108
        }
109
        StatisticsStatus statisticsStatus= numbers.getMap().get(key);
110
        if(statisticsStatus == null){
111
            throw new ContentNotFoundException("Statistics not found for key");
112
        }
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

    
122

    
123
}
(9-9/10)