1
|
package eu.dnetlib.uoaadmintools.controllers;
|
2
|
|
3
|
import eu.dnetlib.uoaadmintools.entities.Topic;
|
4
|
import eu.dnetlib.uoaadmintools.dao.TopicDAO;
|
5
|
import eu.dnetlib.uoaadmintools.entities.Question;
|
6
|
import eu.dnetlib.uoaadmintools.dao.QuestionDAO;
|
7
|
import eu.dnetlib.uoaadmintools.entities.TopicResponse;
|
8
|
|
9
|
import org.apache.log4j.Logger;
|
10
|
import org.springframework.web.bind.annotation.CrossOrigin;
|
11
|
import org.springframework.web.bind.annotation.RequestMapping;
|
12
|
import org.springframework.web.bind.annotation.RequestMethod;
|
13
|
import org.springframework.web.bind.annotation.RequestBody;
|
14
|
import org.springframework.web.bind.annotation.PathVariable;
|
15
|
import org.springframework.beans.factory.annotation.Autowired;
|
16
|
import org.springframework.web.bind.annotation.RequestParam;
|
17
|
import org.springframework.web.bind.annotation.RestController;
|
18
|
|
19
|
import java.util.ArrayList;
|
20
|
import java.util.Comparator;
|
21
|
import java.util.List;
|
22
|
|
23
|
@RestController
|
24
|
@CrossOrigin(origins = "*")
|
25
|
public class TopicController {
|
26
|
private final Logger log = Logger.getLogger(this.getClass());
|
27
|
|
28
|
@Autowired
|
29
|
private TopicDAO topicDAO;
|
30
|
|
31
|
@Autowired
|
32
|
private QuestionDAO questionDAO;
|
33
|
|
34
|
@RequestMapping(value = "/topic", method = RequestMethod.GET)
|
35
|
public List<Topic> getAllTopics() {
|
36
|
return topicDAO.findAll();
|
37
|
}
|
38
|
|
39
|
@RequestMapping(value = "/topic", method = RequestMethod.DELETE)
|
40
|
public void deleteAllTopics() {
|
41
|
topicDAO.deleteAll();
|
42
|
}
|
43
|
|
44
|
//TODO: if id is provided check that it exists!
|
45
|
@RequestMapping(value = "/topic", method = RequestMethod.POST)
|
46
|
public Topic insertOrUpdateTopic(@RequestBody Topic topic) {
|
47
|
return topicDAO.save(topic);
|
48
|
}
|
49
|
|
50
|
@RequestMapping(value = "/topic/{id}", method = RequestMethod.GET)
|
51
|
public Topic getTopic(@PathVariable(value = "id") String id) {
|
52
|
return topicDAO.findById(id);
|
53
|
}
|
54
|
|
55
|
@RequestMapping(value = "/topic/{id}", method = RequestMethod.DELETE)
|
56
|
public void deleteTopic(@PathVariable(value = "id") String id) {
|
57
|
List<Question> questions = questionDAO.findByTopicsIn(id);
|
58
|
for (Question question : questions) {
|
59
|
List<String> topics = question.getTopics();
|
60
|
topics.remove(id);
|
61
|
if (topics.isEmpty()) {
|
62
|
questionDAO.delete(question.getId());
|
63
|
} else {
|
64
|
questionDAO.save(question);
|
65
|
}
|
66
|
}
|
67
|
topicDAO.delete(id);
|
68
|
}
|
69
|
|
70
|
@RequestMapping(value = "/topic/delete", method = RequestMethod.POST)
|
71
|
public void deleteTopics(@RequestBody List<String> topics) {
|
72
|
for (String topic : topics) {
|
73
|
deleteTopic(topic);
|
74
|
}
|
75
|
}
|
76
|
|
77
|
@RequestMapping(value = "/topic/toggle", method = RequestMethod.POST)
|
78
|
public void toggleTopics(@RequestBody List<String> topics, @RequestParam(value="order", defaultValue = "") String order) throws Exception {
|
79
|
for (String id: topics) {
|
80
|
Topic topic = topicDAO.findById(id);
|
81
|
topic.setQuestionOrder(order);
|
82
|
topicDAO.save(topic);
|
83
|
}
|
84
|
}
|
85
|
|
86
|
@RequestMapping(value = "/topic/active", method = RequestMethod.GET)
|
87
|
public List<TopicResponse> getActiveTopic() {
|
88
|
List<TopicResponse> topicResponses = new ArrayList<>();
|
89
|
for(Topic topic : topicDAO.findAll()) {
|
90
|
TopicResponse topicResponse = new TopicResponse(topic);
|
91
|
List<Question> questions = questionDAO.findByTopicsInAndIsActiveIsTrue(topic.getId());
|
92
|
if(topic.getQuestionOrder().equals("hits")) {
|
93
|
questions.sort(Comparator.comparingInt(Question::getHitCount).reversed());
|
94
|
} else if(topic.getQuestionOrder().equals("weight")) {
|
95
|
questions.sort(Comparator.comparingDouble(Question::getWeight).reversed());
|
96
|
}
|
97
|
topicResponse.setQuestions(questions);
|
98
|
topicResponses.add(topicResponse);
|
99
|
}
|
100
|
return topicResponses;
|
101
|
}
|
102
|
|
103
|
}
|