1 |
57671
|
konstantin
|
package eu.dnetlib.uoamonitorservice.controllers;
|
2 |
|
|
|
3 |
|
|
import eu.dnetlib.uoamonitorservice.dao.*;
|
4 |
|
|
import eu.dnetlib.uoamonitorservice.entities.Category;
|
5 |
|
|
import eu.dnetlib.uoamonitorservice.entities.Stakeholder;
|
6 |
|
|
import eu.dnetlib.uoamonitorservice.entities.SubCategory;
|
7 |
|
|
import eu.dnetlib.uoamonitorservice.entities.Topic;
|
8 |
|
|
import eu.dnetlib.uoamonitorservice.handlers.EntityNotFoundException;
|
9 |
|
|
import eu.dnetlib.uoamonitorservice.handlers.PathNotValidException;
|
10 |
|
|
import org.apache.log4j.Logger;
|
11 |
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
12 |
|
|
import org.springframework.web.bind.annotation.*;
|
13 |
|
|
|
14 |
|
|
import java.util.ArrayList;
|
15 |
|
|
import java.util.List;
|
16 |
|
|
|
17 |
|
|
@RestController
|
18 |
|
|
@CrossOrigin(origins = "*")
|
19 |
|
|
public class TopicController {
|
20 |
|
|
private final Logger log = Logger.getLogger(this.getClass());
|
21 |
|
|
|
22 |
|
|
@Autowired
|
23 |
|
|
private StakeholderDAO stakeholderDAO;
|
24 |
|
|
|
25 |
|
|
@Autowired
|
26 |
|
|
private TopicDAO topicDAO;
|
27 |
|
|
|
28 |
|
|
@Autowired
|
29 |
|
|
private CategoryDAO categoryDAO;
|
30 |
|
|
|
31 |
|
|
@Autowired
|
32 |
|
|
private SubCategoryDAO subCategoryDAO;
|
33 |
|
|
|
34 |
|
|
@Autowired
|
35 |
|
|
private IndicatorDAO indicatorDAO;
|
36 |
|
|
|
37 |
|
|
@Autowired
|
38 |
|
|
private CategoryController categoryController;
|
39 |
|
|
|
40 |
|
|
public Topic<Category> buildTopic(Topic<Category> topicFull) {
|
41 |
|
|
Topic<String> topic = new Topic<>(topicFull);
|
42 |
|
|
|
43 |
|
|
List<String> categories = new ArrayList<>();
|
44 |
|
|
List<Category> categoriesFull = new ArrayList<>();
|
45 |
|
|
for(Category<SubCategory> category : topicFull.getCategories()) {
|
46 |
|
|
Category<SubCategory> categoryFull = categoryController.buildCategory(category);
|
47 |
|
|
categoriesFull.add(categoryFull);
|
48 |
|
|
categories.add(categoryFull.getId());
|
49 |
|
|
}
|
50 |
|
|
topicFull.setCategories(categoriesFull);
|
51 |
|
|
topic.setCategories(categories);
|
52 |
|
|
|
53 |
57923
|
konstantin
|
topicDAO.save(topic);
|
54 |
57671
|
konstantin
|
|
55 |
57923
|
konstantin
|
topicFull.setId(topic.getId());
|
56 |
57671
|
konstantin
|
return topicFull;
|
57 |
|
|
}
|
58 |
|
|
|
59 |
|
|
@RequestMapping(value = "/{stakeholderId}/save", method = RequestMethod.POST)
|
60 |
|
|
public Topic<Category> saveTopic(@PathVariable("stakeholderId") String stakeholderId,
|
61 |
|
|
@RequestBody Topic<Category> topicFull) {
|
62 |
|
|
log.debug("save topic");
|
63 |
57923
|
konstantin
|
log.debug("Alias: "+topicFull.getAlias() + " - Id: "+topicFull.getId()+ " - Stakeholder: "+stakeholderId);
|
64 |
57671
|
konstantin
|
|
65 |
|
|
Stakeholder<String> stakeholder = stakeholderDAO.findById(stakeholderId);
|
66 |
|
|
|
67 |
|
|
if(stakeholder != null) {
|
68 |
|
|
|
69 |
|
|
Topic<String> topic = new Topic<>(topicFull);
|
70 |
|
|
|
71 |
|
|
List<String> categories = new ArrayList<>();
|
72 |
|
|
for(Category category : topicFull.getCategories()) {
|
73 |
|
|
categories.add(category.getId());
|
74 |
|
|
}
|
75 |
|
|
topic.setCategories(categories);
|
76 |
|
|
|
77 |
57923
|
konstantin
|
topicDAO.save(topic);
|
78 |
57671
|
konstantin
|
|
79 |
57923
|
konstantin
|
if(stakeholder.getDefaultId() == null) {
|
80 |
|
|
if(topicFull.getId() == null) {
|
81 |
|
|
onSaveDefaultTopic(topic, stakeholderId);
|
82 |
|
|
} else {
|
83 |
|
|
onUpdateDefaultTopic(topic);
|
84 |
|
|
}
|
85 |
|
|
}
|
86 |
|
|
|
87 |
57671
|
konstantin
|
List<String> topics = stakeholder.getTopics();
|
88 |
57923
|
konstantin
|
int index = topics.indexOf(topic.getId());
|
89 |
57671
|
konstantin
|
if(index == -1) {
|
90 |
57923
|
konstantin
|
topics.add(topic.getId());
|
91 |
57671
|
konstantin
|
stakeholderDAO.save(stakeholder);
|
92 |
|
|
log.debug("Topic saved!");
|
93 |
|
|
|
94 |
57923
|
konstantin
|
topicFull.setId(topic.getId());
|
95 |
57671
|
konstantin
|
}
|
96 |
|
|
|
97 |
|
|
categories = null;
|
98 |
|
|
topic = null;
|
99 |
|
|
} else {
|
100 |
|
|
// EXCEPTION - Stakeholder not found
|
101 |
|
|
throw new EntityNotFoundException("Save topic: Stakeholder with id: "+stakeholderId+" not found");
|
102 |
|
|
}
|
103 |
|
|
return topicFull;
|
104 |
|
|
}
|
105 |
|
|
|
106 |
57923
|
konstantin
|
public void onSaveDefaultTopic(Topic topic, String stakeholderId) {
|
107 |
|
|
log.debug("On save default topic");
|
108 |
|
|
|
109 |
|
|
List<Stakeholder> stakeholders = stakeholderDAO.findByDefaultId(stakeholderId);
|
110 |
|
|
for(Stakeholder _stakeholder : stakeholders) {
|
111 |
|
|
Topic topicNew = new Topic();
|
112 |
|
|
topicNew.copyFromDefault(topic);
|
113 |
|
|
|
114 |
|
|
topicDAO.save(topicNew);
|
115 |
|
|
|
116 |
|
|
List<String> topics = _stakeholder.getTopics();
|
117 |
|
|
topics.add(topicNew.getId());
|
118 |
|
|
|
119 |
|
|
stakeholderDAO.save(_stakeholder);
|
120 |
|
|
}
|
121 |
|
|
}
|
122 |
|
|
|
123 |
|
|
public void onUpdateDefaultTopic(Topic topic) {
|
124 |
|
|
log.debug("On update default topic");
|
125 |
|
|
|
126 |
|
|
List<Topic> topics = topicDAO.findByDefaultId(topic.getId());
|
127 |
|
|
boolean changed = false;
|
128 |
|
|
for(Topic topicBasedOnDefault : topics) {
|
129 |
|
|
if(topic.getName() != null && !topic.getName().equals(topicBasedOnDefault.getName())) {
|
130 |
|
|
changed = true;
|
131 |
|
|
}
|
132 |
|
|
if(topic.getDescription() != null && !topic.getDescription().equals(topicBasedOnDefault.getDescription())) {
|
133 |
|
|
changed = true;
|
134 |
|
|
}
|
135 |
|
|
|
136 |
|
|
if(!changed) {
|
137 |
|
|
break;
|
138 |
|
|
}
|
139 |
|
|
topicBasedOnDefault.setName(topic.getName());
|
140 |
|
|
topicBasedOnDefault.setDescription(topic.getDescription());
|
141 |
|
|
topicDAO.save(topicBasedOnDefault);
|
142 |
|
|
}
|
143 |
|
|
}
|
144 |
|
|
|
145 |
57671
|
konstantin
|
@RequestMapping(value = "/{stakeholderId}/{topicId}/delete", method = RequestMethod.DELETE)
|
146 |
|
|
public boolean deleteTopic(@PathVariable("stakeholderId") String stakeholderId,
|
147 |
|
|
@PathVariable("topicId") String topicId) {
|
148 |
|
|
log.debug("delete topic");
|
149 |
57923
|
konstantin
|
log.debug("Id: "+topicId + " - Stakeholder: "+stakeholderId);
|
150 |
57671
|
konstantin
|
|
151 |
|
|
Stakeholder<String> stakeholder = stakeholderDAO.findById(stakeholderId);
|
152 |
|
|
|
153 |
|
|
if(stakeholder != null) {
|
154 |
|
|
|
155 |
|
|
Topic<String> topic = topicDAO.findById(topicId);
|
156 |
|
|
if(topic != null) {
|
157 |
|
|
|
158 |
|
|
List<String> topics = stakeholder.getTopics();
|
159 |
|
|
int index = topics.indexOf(topicId);
|
160 |
|
|
if(index != -1) {
|
161 |
|
|
for(String categoryId : topic.getCategories()) {
|
162 |
|
|
Category<String> category = categoryDAO.findById(categoryId);
|
163 |
|
|
if(category == null) {
|
164 |
|
|
// EXCEPTION - Category not found
|
165 |
|
|
throw new EntityNotFoundException("Delete topic: Category with id: "+categoryId+" not found (category exists in topic: "+topicId+")");
|
166 |
|
|
}
|
167 |
|
|
|
168 |
|
|
for(String subCategoryId : category.getSubCategories()) {
|
169 |
|
|
SubCategory<String> subcategory = subCategoryDAO.findById(subCategoryId);
|
170 |
|
|
if (subcategory == null) {
|
171 |
|
|
// EXCEPTION - SubCategory not found
|
172 |
|
|
throw new EntityNotFoundException("Delete stakeholder: SubCategory with id: "+subCategoryId+" not found (subcategory exists in category: "+categoryId+")");
|
173 |
|
|
}
|
174 |
|
|
|
175 |
|
|
for (String chartId : subcategory.getCharts()) {
|
176 |
|
|
indicatorDAO.delete(chartId);
|
177 |
|
|
}
|
178 |
|
|
subcategory.setCharts(null);
|
179 |
|
|
|
180 |
|
|
for (String numberId : subcategory.getNumbers()) {
|
181 |
|
|
indicatorDAO.delete(numberId);
|
182 |
|
|
}
|
183 |
|
|
subcategory.setNumbers(null);
|
184 |
|
|
|
185 |
|
|
subCategoryDAO.delete(subCategoryId);
|
186 |
|
|
}
|
187 |
|
|
category.setSubCategories(null);
|
188 |
|
|
categoryDAO.delete(categoryId);
|
189 |
|
|
}
|
190 |
|
|
topic.setCategories(null);
|
191 |
|
|
|
192 |
|
|
topics.remove(index);
|
193 |
|
|
stakeholderDAO.save(stakeholder);
|
194 |
|
|
|
195 |
|
|
topicDAO.delete(topicId);
|
196 |
|
|
log.debug("Category deleted!");
|
197 |
|
|
} else {
|
198 |
|
|
// EXCEPTION - Topic not found in Stakeholder: stakeholder.getAlias();
|
199 |
|
|
throw new PathNotValidException("Delete topic: Topic with id: "+topicId+" not found in Stakeholder: "+stakeholderId);
|
200 |
|
|
}
|
201 |
|
|
|
202 |
|
|
} else {
|
203 |
|
|
// EXCEPTION - Topic not found
|
204 |
|
|
throw new EntityNotFoundException("Delete topic: Topic with id: "+topicId+" not found");
|
205 |
|
|
}
|
206 |
|
|
} else {
|
207 |
|
|
// EXCEPTION - Stakeholder not found
|
208 |
|
|
throw new EntityNotFoundException("Delete topic: Stakeholder with id: "+stakeholderId+" not found");
|
209 |
|
|
}
|
210 |
|
|
return true;
|
211 |
|
|
}
|
212 |
57934
|
konstantin
|
|
213 |
|
|
@RequestMapping(value = "/{stakeholderId}/{topicId}/toggle-status", method = RequestMethod.POST)
|
214 |
|
|
public Boolean toggleTopicStatus(@PathVariable("stakeholderId") String stakeholderId,
|
215 |
|
|
@PathVariable("topicId") String topicId) {
|
216 |
|
|
log.debug("toggle topic status (isActive)");
|
217 |
|
|
log.debug("Stakeholder: "+stakeholderId + " - Topic: "+topicId);
|
218 |
|
|
|
219 |
|
|
Topic topic = topicDAO.findById(topicId);
|
220 |
|
|
if (topic == null) {
|
221 |
|
|
// EXCEPTION - Topic not found
|
222 |
|
|
throw new EntityNotFoundException("Toggle topic status: Topic with id: "+topicId+" not found");
|
223 |
|
|
}
|
224 |
|
|
topic.setIsActive(!topic.getIsActive());
|
225 |
|
|
|
226 |
|
|
this.toggleTopic(stakeholderId, topic);
|
227 |
|
|
|
228 |
|
|
return topic.getIsActive();
|
229 |
|
|
}
|
230 |
|
|
|
231 |
|
|
@RequestMapping(value = "/{stakeholderId}/{topicId}/toggle-access", method = RequestMethod.POST)
|
232 |
|
|
public Boolean toggleIndicatorAccess(@PathVariable("stakeholderId") String stakeholderId,
|
233 |
|
|
@PathVariable("topicId") String topicId) {
|
234 |
|
|
log.debug("toggle topic access (isPublic)");
|
235 |
|
|
log.debug("Stakeholder: "+stakeholderId + " - Topic: "+topicId);
|
236 |
|
|
|
237 |
|
|
Topic topic = topicDAO.findById(topicId);
|
238 |
|
|
if (topic == null) {
|
239 |
|
|
// EXCEPTION - Topic not found
|
240 |
|
|
throw new EntityNotFoundException("Toggle topic access: Topic with id: "+topicId+" not found");
|
241 |
|
|
}
|
242 |
|
|
topic.setIsPublic(!topic.getIsPublic());
|
243 |
|
|
|
244 |
|
|
this.toggleTopic(stakeholderId, topic);
|
245 |
|
|
|
246 |
|
|
return topic.getIsPublic();
|
247 |
|
|
}
|
248 |
|
|
|
249 |
|
|
public void toggleTopic(String stakeholderId, Topic topic) {
|
250 |
|
|
Stakeholder<String> stakeholder = stakeholderDAO.findById(stakeholderId);
|
251 |
|
|
|
252 |
|
|
if (stakeholder != null) {
|
253 |
|
|
if (stakeholder.getTopics().contains(topic.getId())) {
|
254 |
|
|
topicDAO.save(topic);
|
255 |
|
|
log.debug("Topic toggled!");
|
256 |
|
|
} else {
|
257 |
|
|
// EXCEPTION - Topic not found in Stakeholder: stakeholder.getAlias();
|
258 |
|
|
throw new PathNotValidException("Toggle topic: Topic with id: "+topic.getId()+" not found in Stakeholder: "+stakeholderId);
|
259 |
|
|
}
|
260 |
|
|
} else {
|
261 |
|
|
// EXCEPTION - Stakeholder not found
|
262 |
|
|
throw new EntityNotFoundException("Toggle topic: Stakeholder with id: "+stakeholderId+" not found");
|
263 |
|
|
}
|
264 |
|
|
}
|
265 |
57671
|
konstantin
|
}
|