Revision 57671
Added by Konstantina Galouni over 4 years ago
StakeholderController.java | ||
---|---|---|
1 | 1 |
package eu.dnetlib.uoamonitorservice.controllers; |
2 | 2 |
|
3 |
import com.fasterxml.jackson.core.type.TypeReference; |
|
4 |
import com.fasterxml.jackson.databind.ObjectMapper; |
|
5 |
import eu.dnetlib.uoamonitorservice.dao.IndicatorDAO; |
|
6 |
import eu.dnetlib.uoamonitorservice.dao.StakeholderDAO; |
|
3 |
//import com.fasterxml.jackson.core.type.TypeReference; |
|
4 |
//import com.fasterxml.jackson.databind.ObjectMapper; |
|
5 |
import eu.dnetlib.uoamonitorservice.dao.*; |
|
7 | 6 |
import eu.dnetlib.uoamonitorservice.entities.*; |
7 |
import eu.dnetlib.uoamonitorservice.handlers.EntityNotFoundException; |
|
8 | 8 |
import org.apache.log4j.Logger; |
9 | 9 |
import org.springframework.beans.factory.annotation.Autowired; |
10 | 10 |
import org.springframework.web.bind.annotation.*; |
11 | 11 |
|
12 |
import java.text.SimpleDateFormat; |
|
13 | 12 |
import java.util.ArrayList; |
14 | 13 |
import java.util.Date; |
15 |
import java.util.Iterator; |
|
16 | 14 |
import java.util.List; |
17 | 15 |
|
18 | 16 |
@RestController |
... | ... | |
24 | 22 |
private StakeholderDAO stakeholderDAO; |
25 | 23 |
|
26 | 24 |
@Autowired |
25 |
private TopicDAO topicDAO; |
|
26 |
|
|
27 |
@Autowired |
|
28 |
private CategoryDAO categoryDAO; |
|
29 |
|
|
30 |
@Autowired |
|
31 |
private SubCategoryDAO subCategoryDAO; |
|
32 |
|
|
33 |
@Autowired |
|
27 | 34 |
private IndicatorDAO indicatorDAO; |
28 | 35 |
|
29 |
public Stakeholder setIndicatorsForStakeholder(Stakeholder stakeholder) { |
|
30 |
for (Topic topic: stakeholder.getTopics()) { |
|
31 |
for(Category category : topic.getCategories()) { |
|
36 |
@Autowired |
|
37 |
private TopicController topicController; |
|
38 |
|
|
39 |
@RequestMapping(value = "/build-stakeholder", method = RequestMethod.POST) |
|
40 |
public Stakeholder<Topic<Category<SubCategory<Indicator>>>> buildFullStakeholder(@RequestBody Stakeholder<Topic<Category<SubCategory<Indicator>>>> stakeholderFull) { |
|
41 |
log.debug("build stakeholder"); |
|
42 |
|
|
43 |
Stakeholder<String> stakeholder = new Stakeholder<>(stakeholderFull); |
|
44 |
|
|
45 |
List<String> topics = new ArrayList<>(); |
|
46 |
List<Topic<Category<SubCategory<Indicator>>>> topicsFull = new ArrayList<>(); |
|
47 |
for(Topic topic : stakeholderFull.getTopics()) { |
|
48 |
Topic<Category<SubCategory<Indicator>>> topicFull = topicController.buildTopic(topic); |
|
49 |
topicsFull.add(topicFull); |
|
50 |
topics.add(topicFull.getId()); |
|
51 |
} |
|
52 |
stakeholderFull.setTopics(topicsFull); |
|
53 |
stakeholder.setTopics(topics); |
|
54 |
|
|
55 |
Stakeholder<String> stakeholderSaved = stakeholderDAO.save(stakeholder); |
|
56 |
stakeholderFull.setId(stakeholderSaved.getId()); |
|
57 |
return stakeholderFull; |
|
58 |
//return null; |
|
59 |
} |
|
60 |
|
|
61 |
public Stakeholder setFullEntities(Stakeholder<String> stakeholder) { |
|
62 |
Stakeholder<Topic> stakeholderFull = new Stakeholder<>(stakeholder); |
|
63 |
|
|
64 |
List<Topic> topics = new ArrayList<>(); |
|
65 |
|
|
66 |
for (String topicId: (List<String>)stakeholder.getTopics()) { |
|
67 |
Topic<String> topic = topicDAO.findById(topicId); |
|
68 |
Topic<Category> topicFull = new Topic<Category>(topic); |
|
69 |
|
|
70 |
List<Category> categories = new ArrayList<>(); |
|
71 |
|
|
72 |
for(String categoryId : topic.getCategories()) { |
|
73 |
Category<String> category = categoryDAO.findById(categoryId); |
|
74 |
Category<SubCategory> categoryFull = new Category<SubCategory>(category); |
|
75 |
|
|
32 | 76 |
List<SubCategory> subCategories = new ArrayList<>(); |
33 | 77 |
|
34 |
for(SubCategory<String> subCategory : category.getSubCategories()) { |
|
78 |
for(String subCategoryId : category.getSubCategories()) { |
|
79 |
SubCategory<String> subCategory = subCategoryDAO.findById(subCategoryId); |
|
35 | 80 |
SubCategory subCategoryFull = new SubCategory<Indicator>(subCategory); |
36 | 81 |
|
37 | 82 |
List<Indicator> charts = new ArrayList<>(); |
... | ... | |
49 | 94 |
subCategories.add(subCategoryFull); |
50 | 95 |
} |
51 | 96 |
|
52 |
category.setSubCategories(subCategories); |
|
97 |
categoryFull.setSubCategories(subCategories); |
|
98 |
categories.add(categoryFull); |
|
53 | 99 |
} |
54 | 100 |
|
101 |
topicFull.setCategories(categories); |
|
102 |
topics.add(topicFull); |
|
55 | 103 |
} |
56 |
return stakeholder; |
|
104 |
|
|
105 |
stakeholderFull.setTopics(topics); |
|
106 |
return stakeholderFull; |
|
57 | 107 |
} |
58 | 108 |
|
59 | 109 |
@RequestMapping(value = "/stakeholder/all", method = RequestMethod.GET) |
... | ... | |
65 | 115 |
stakeholders = stakeholderDAO.findByType(type); |
66 | 116 |
} |
67 | 117 |
|
118 |
List<Stakeholder> stakeholdersFull = new ArrayList<>(); |
|
68 | 119 |
for(Stakeholder stakeholder : stakeholders) { |
69 |
this.setIndicatorsForStakeholder(stakeholder);
|
|
120 |
stakeholdersFull.add(this.setFullEntities(stakeholder));
|
|
70 | 121 |
} |
71 | 122 |
|
72 |
return stakeholders; |
|
123 |
return stakeholdersFull;
|
|
73 | 124 |
} |
74 | 125 |
|
75 | 126 |
@RequestMapping(value = "/stakeholder/default", method = RequestMethod.GET) |
... | ... | |
81 | 132 |
stakeholders = stakeholderDAO.findByIsDefaultProfileAndType(true, type); |
82 | 133 |
} |
83 | 134 |
|
135 |
List<Stakeholder> stakeholdersFull = new ArrayList<>(); |
|
84 | 136 |
for(Stakeholder stakeholder : stakeholders) { |
85 |
this.setIndicatorsForStakeholder(stakeholder);
|
|
137 |
stakeholdersFull.add(this.setFullEntities(stakeholder));
|
|
86 | 138 |
} |
87 |
return stakeholders; |
|
139 |
return stakeholdersFull;
|
|
88 | 140 |
} |
89 | 141 |
|
90 | 142 |
@RequestMapping(value = "/stakeholder", method = RequestMethod.GET) |
... | ... | |
96 | 148 |
stakeholders = stakeholderDAO.findByIsDefaultProfileAndType(false, type); |
97 | 149 |
} |
98 | 150 |
|
151 |
List<Stakeholder> stakeholdersFull = new ArrayList<>(); |
|
99 | 152 |
for(Stakeholder stakeholder : stakeholders) { |
100 |
this.setIndicatorsForStakeholder(stakeholder);
|
|
153 |
stakeholdersFull.add(this.setFullEntities(stakeholder));
|
|
101 | 154 |
} |
102 | 155 |
log.debug(new Date()); |
103 | 156 |
|
104 |
return stakeholders; |
|
157 |
return stakeholdersFull;
|
|
105 | 158 |
} |
106 | 159 |
|
107 | 160 |
@RequestMapping(value = "/stakeholder/{alias}", method = RequestMethod.GET) |
108 | 161 |
public Stakeholder getStakeholder(@PathVariable("alias") String alias) { |
109 |
Stakeholder stakeholder = stakeholderDAO.findByAlias(alias); |
|
110 |
this.setIndicatorsForStakeholder(stakeholder); |
|
111 |
|
|
112 |
return stakeholder; |
|
162 |
Stakeholder<String> stakeholder = stakeholderDAO.findByAlias(alias); |
|
163 |
if(stakeholder == null) { |
|
164 |
// EXCEPTION - Stakeholder not found |
|
165 |
throw new EntityNotFoundException("Get stakeholder: Stakeholder with alias: "+alias+" not found"); |
|
166 |
} |
|
167 |
return this.setFullEntities(stakeholder); |
|
113 | 168 |
} |
114 | 169 |
|
115 |
|
|
116 |
@RequestMapping(value = "/stakeholder/save", method = RequestMethod.POST) |
|
117 |
public Stakeholder saveStakeholder(@RequestBody Stakeholder stakeholder) { |
|
170 |
@RequestMapping(value = "/save", method = RequestMethod.POST) |
|
171 |
public Stakeholder<Topic> saveStakeholder(@RequestBody Stakeholder<Topic> stakeholderFull) { |
|
118 | 172 |
log.debug("save stakeholder"); |
119 | 173 |
|
120 |
Stakeholder stakeholderFull = new Stakeholder(stakeholder); |
|
174 |
// if(stakeholderFull == null) { |
|
175 |
// log.debug("stakeholder null"); |
|
176 |
// // EXCEPTION - Parameter for Stakeholder is not accepted |
|
177 |
// } |
|
121 | 178 |
|
122 |
List<Topic> topicsFull = new ArrayList<>();
|
|
179 |
Stakeholder<String> stakeholder = new Stakeholder<>(stakeholderFull);
|
|
123 | 180 |
|
124 |
for (Topic topic: stakeholder.getTopics()) { |
|
125 |
Topic topicFull = new Topic(topic); |
|
126 |
|
|
127 |
List<Category> categoriesFull = new ArrayList<>(); |
|
128 |
|
|
129 |
for(Category category : topic.getCategories()) { |
|
130 |
Category categoryFull = new Category(category); |
|
131 |
|
|
132 |
List<SubCategory> subCategories = new ArrayList<>(); |
|
133 |
List<SubCategory> subCategoriesFull = new ArrayList<>(); |
|
134 |
|
|
135 |
for(SubCategory<Indicator> subCategoryFull : category.getSubCategories()) { |
|
136 |
SubCategory subCategory = new SubCategory<String>(subCategoryFull); |
|
137 |
|
|
138 |
List<String> charts = new ArrayList<>(); |
|
139 |
|
|
140 |
ObjectMapper mapper = new ObjectMapper(); |
|
141 |
//Jackson's use of generics |
|
142 |
List<Indicator> chartsFull = mapper.convertValue(subCategoryFull.getCharts(), new TypeReference<List<Indicator>>(){}); |
|
143 |
|
|
144 |
//List<Indicator> chartsFull = (List<Indicator>)subCategoryFull.getCharts(); |
|
145 |
//log.debug(chartsFull); |
|
146 |
|
|
147 |
for(Indicator indicator : chartsFull) { |
|
148 |
charts.add(indicator.getId()); |
|
149 |
} |
|
150 |
|
|
151 |
subCategory.setCharts(charts); |
|
152 |
subCategoryFull.setCharts(chartsFull); |
|
153 |
|
|
154 |
List<String> numbers = new ArrayList<>(); |
|
155 |
List<Indicator> numbersFull = mapper.convertValue(subCategoryFull.getNumbers(), new TypeReference<List<Indicator>>(){}); |
|
156 |
|
|
157 |
for(Indicator indicator : numbersFull) { |
|
158 |
numbers.add(indicator.getId()); |
|
159 |
} |
|
160 |
subCategory.setNumbers(numbers); |
|
161 |
subCategoryFull.setNumbers(numbersFull); |
|
162 |
|
|
163 |
subCategories.add(subCategory); |
|
164 |
subCategoriesFull.add(subCategoryFull); |
|
165 |
} |
|
166 |
|
|
167 |
category.setSubCategories(subCategories); |
|
168 |
categoryFull.setSubCategories(subCategoriesFull); |
|
169 |
|
|
170 |
categoriesFull.add(categoryFull); |
|
171 |
} |
|
172 |
topicFull.setCategories(categoriesFull); |
|
173 |
topicsFull.add(topicFull); |
|
181 |
List<String> topics = new ArrayList<>(); |
|
182 |
for(Topic topic : stakeholderFull.getTopics()) { |
|
183 |
topics.add(topic.getId()); |
|
174 | 184 |
} |
175 |
stakeholderFull.setTopics(topicsFull);
|
|
185 |
stakeholder.setTopics(topics);
|
|
176 | 186 |
|
177 |
log.debug("after minimize stakeholder"); |
|
178 |
Stakeholder stakeholderSaved = stakeholderDAO.save(stakeholder); |
|
179 |
log.debug("stakeholder saved!"); |
|
180 |
|
|
187 |
Stakeholder<String> stakeholderSaved = stakeholderDAO.save(stakeholder); |
|
181 | 188 |
stakeholderFull.setId(stakeholderSaved.getId()); |
182 |
return stakeholderFull; |
|
183 |
} |
|
184 | 189 |
|
185 |
@RequestMapping(value = "/{stakeholder}", method = RequestMethod.DELETE) |
|
186 |
public boolean deleteStakeholder(@PathVariable("stakeholder") String stakeholder) { |
|
187 |
Stakeholder _stakeholder = stakeholderDAO.findById(stakeholder); |
|
188 |
if(_stakeholder != null) { |
|
189 |
for (Topic topic : _stakeholder.getTopics()) { |
|
190 |
for (Category category : topic.getCategories()) { |
|
191 |
for (SubCategory<String> subcategory : category.getSubCategories()) { |
|
192 |
for(String chartId : subcategory.getCharts()) { |
|
193 |
indicatorDAO.delete(chartId); |
|
194 |
} |
|
195 |
for(String numberId : subcategory.getNumbers()) { |
|
196 |
indicatorDAO.delete(numberId); |
|
197 |
} |
|
190 |
topics = null; |
|
191 |
stakeholder = null; |
|
192 |
stakeholderSaved = null; |
|
198 | 193 |
|
199 |
subcategory.setCharts(null); |
|
200 |
subcategory.setNumbers(null); |
|
201 |
} |
|
202 |
category.setSubCategories(null); |
|
203 |
} |
|
204 |
topic.setCategories(null); |
|
205 |
} |
|
206 |
|
|
207 |
} else { |
|
208 |
return false; |
|
209 |
// EXCEPTION - Stakeholder not found |
|
210 |
} |
|
211 |
|
|
212 |
stakeholderDAO.delete(_stakeholder.getId()); |
|
213 |
_stakeholder = null; |
|
214 |
return true; |
|
194 |
return stakeholderFull; |
|
215 | 195 |
} |
216 | 196 |
|
217 |
@RequestMapping(value = "/{stakeholder}/{topic}", method = RequestMethod.DELETE) |
|
218 |
public Stakeholder deleteTopic(@PathVariable("stakeholder") String stakeholder, |
|
219 |
@PathVariable("topic") String topic) { |
|
220 | 197 |
|
221 |
Stakeholder _stakeholder = stakeholderDAO.findByAlias(stakeholder);
|
|
222 |
if(_stakeholder != null) {
|
|
223 |
boolean topicFound = false;
|
|
198 |
@RequestMapping(value = "/{stakeholderId}/delete", method = RequestMethod.DELETE)
|
|
199 |
public boolean deleteStakeholder(@PathVariable("stakeholderId") String stakeholderId) {
|
|
200 |
log.debug("delete stakeholder");
|
|
224 | 201 |
|
225 |
Iterator<Topic> topicIterator = _stakeholder.getTopics().iterator(); |
|
226 |
while (topicIterator.hasNext()) { |
|
227 |
Topic _topic = topicIterator.next(); |
|
228 |
if(_topic.getAlias().equals(topic)) { |
|
229 |
for (Category category : _topic.getCategories()) { |
|
230 |
for (SubCategory<String> subcategory : category.getSubCategories()) { |
|
231 |
for(String chartId : subcategory.getCharts()) { |
|
232 |
indicatorDAO.delete(chartId); |
|
233 |
} |
|
234 |
for(String numberId : subcategory.getNumbers()) { |
|
235 |
indicatorDAO.delete(numberId); |
|
236 |
} |
|
202 |
Stakeholder<String> stakeholder = stakeholderDAO.findById(stakeholderId); |
|
237 | 203 |
|
238 |
subcategory.setCharts(null); |
|
239 |
subcategory.setNumbers(null); |
|
240 |
} |
|
241 |
category.setSubCategories(null); |
|
242 |
} |
|
243 |
_topic.setCategories(null); |
|
244 |
topicIterator.remove(); |
|
245 |
stakeholderDAO.save(_stakeholder); |
|
204 |
if(stakeholder != null) { |
|
246 | 205 |
|
247 |
topicFound = true; |
|
248 |
break; |
|
206 |
for(String topicId : stakeholder.getTopics()) { |
|
207 |
Topic<String> topic = topicDAO.findById(topicId); |
|
208 |
if (topic == null) { |
|
209 |
// EXCEPTION - Topic not found |
|
210 |
throw new EntityNotFoundException("Delete stakeholder: Topic with id: "+topicId+" not found (topic exists in stakeholder: "+stakeholderId+")"); |
|
249 | 211 |
} |
250 |
} |
|
251 |
if(!topicFound) { |
|
252 |
// EXCEPTION - Topic not found |
|
253 |
} |
|
254 |
} else { |
|
255 |
// EXCEPTION - Stakeholder not found |
|
256 |
} |
|
257 |
this.setIndicatorsForStakeholder(_stakeholder); |
|
258 | 212 |
|
259 |
return _stakeholder; |
|
260 |
} |
|
261 |
|
|
262 |
@RequestMapping(value = "/{stakeholder}/{topic}/{category}", method = RequestMethod.DELETE) |
|
263 |
public Stakeholder deleteCategory(@PathVariable("stakeholder") String stakeholder, |
|
264 |
@PathVariable("topic") String topic, |
|
265 |
@PathVariable("category") String category) { |
|
266 |
|
|
267 |
Stakeholder _stakeholder = stakeholderDAO.findByAlias(stakeholder); |
|
268 |
if(_stakeholder != null) { |
|
269 |
Topic _topic = _stakeholder.getTopics().stream() |
|
270 |
.filter(current_topic -> current_topic.getAlias().equals(topic)) |
|
271 |
.findFirst() |
|
272 |
.orElse(null); |
|
273 |
if(_topic != null) { |
|
274 |
boolean categoryFound = false; |
|
275 |
|
|
276 |
Iterator<Category> categoryIterator = _topic.getCategories().iterator(); |
|
277 |
while (categoryIterator.hasNext()) { |
|
278 |
Category _category = categoryIterator.next(); |
|
279 |
if (_category.getAlias().equals(category)) { |
|
280 |
for (SubCategory<String> subcategory : _category.getSubCategories()) { |
|
281 |
for(String chartId : subcategory.getCharts()) { |
|
282 |
indicatorDAO.delete(chartId); |
|
283 |
} |
|
284 |
for(String numberId : subcategory.getNumbers()) { |
|
285 |
indicatorDAO.delete(numberId); |
|
286 |
} |
|
287 |
|
|
288 |
subcategory.setCharts(null); |
|
289 |
subcategory.setNumbers(null); |
|
290 |
} |
|
291 |
_category.setSubCategories(null); |
|
292 |
categoryIterator.remove(); |
|
293 |
stakeholderDAO.save(_stakeholder); |
|
294 |
|
|
295 |
categoryFound = true; |
|
296 |
break; |
|
213 |
for (String categoryId : topic.getCategories()) { |
|
214 |
Category<String> category = categoryDAO.findById(categoryId); |
|
215 |
if (category == null) { |
|
216 |
// EXCEPTION - Category not found |
|
217 |
throw new EntityNotFoundException("Delete stakeholder: Category with id: "+categoryId+" not found (category exists in topic: "+topicId+")"); |
|
297 | 218 |
} |
298 |
} |
|
299 |
if(!categoryFound) { |
|
300 |
// EXCEPTION - Category not found |
|
301 |
} |
|
302 |
} else { |
|
303 |
// EXCEPTION - Topic not found |
|
304 |
} |
|
305 |
} else { |
|
306 |
// EXCEPTION - Stakeholder not found |
|
307 |
} |
|
308 |
this.setIndicatorsForStakeholder(_stakeholder); |
|
309 | 219 |
|
310 |
return _stakeholder; |
|
311 |
} |
|
312 |
|
|
313 |
@RequestMapping(value = "/{stakeholder}/{topic}/{category}/{subcategory}", method = RequestMethod.DELETE) |
|
314 |
public Stakeholder deleteSubCategory(@PathVariable("stakeholder") String stakeholder, |
|
315 |
@PathVariable("topic") String topic, |
|
316 |
@PathVariable("category") String category, |
|
317 |
@PathVariable("subcategory") String subcategory) { |
|
318 |
|
|
319 |
Stakeholder _stakeholder = stakeholderDAO.findByAlias(stakeholder); |
|
320 |
if(_stakeholder != null) { |
|
321 |
Topic _topic = _stakeholder.getTopics().stream() |
|
322 |
.filter(current_topic -> current_topic.getAlias().equals(topic)) |
|
323 |
.findFirst() |
|
324 |
.orElse(null); |
|
325 |
if(_topic != null) { |
|
326 |
Category _category = _topic.getCategories().stream() |
|
327 |
.filter(current_category -> current_category.getAlias().equals(category)) |
|
328 |
.findFirst() |
|
329 |
.orElse(null); |
|
330 |
if(_category != null) { |
|
331 |
boolean subCategoryFound = false; |
|
332 |
|
|
333 |
Iterator<SubCategory> subCategoryIterator = _category.getSubCategories().iterator(); |
|
334 |
while (subCategoryIterator.hasNext()) { |
|
335 |
SubCategory<String> _subCategory = subCategoryIterator.next(); |
|
336 |
if (_subCategory.getAlias().equals(subcategory)) { |
|
337 |
|
|
338 |
for(String chartId : _subCategory.getCharts()) { |
|
339 |
indicatorDAO.delete(chartId); |
|
340 |
} |
|
341 |
for(String numberId : _subCategory.getNumbers()) { |
|
342 |
indicatorDAO.delete(numberId); |
|
343 |
} |
|
344 |
|
|
345 |
_subCategory.setCharts(null); |
|
346 |
_subCategory.setNumbers(null); |
|
347 |
|
|
348 |
subCategoryIterator.remove(); |
|
349 |
stakeholderDAO.save(_stakeholder); |
|
350 |
|
|
351 |
subCategoryFound = true; |
|
352 |
break; |
|
353 |
|
|
220 |
for (String subCategoryId : category.getSubCategories()) { |
|
221 |
SubCategory<String> subcategory = subCategoryDAO.findById(subCategoryId); |
|
222 |
if (subcategory == null) { |
|
223 |
// EXCEPTION - SubCategory not found |
|
224 |
throw new EntityNotFoundException("Delete stakeholder: SubCategory with id: "+subCategoryId+" not found (subcategory exists in category: "+categoryId+")"); |
|
354 | 225 |
} |
355 |
} |
|
356 |
if (!subCategoryFound) { |
|
357 |
// EXCEPTION - SubCategory not found |
|
358 |
} |
|
359 |
} else { |
|
360 |
// EXCEPTION - Category not found |
|
361 |
} |
|
362 |
} else { |
|
363 |
// EXCEPTION - Topic not found |
|
364 |
} |
|
365 |
} else { |
|
366 |
// EXCEPTION - Stakeholder not found |
|
367 |
} |
|
368 |
this.setIndicatorsForStakeholder(_stakeholder); |
|
369 | 226 |
|
370 |
return _stakeholder; |
|
371 |
} |
|
372 |
|
|
373 |
@RequestMapping(value = "/{stakeholder}/{topic}/{category}/{subcategory}/{id}", method = RequestMethod.DELETE) |
|
374 |
public boolean deleteIndicator(@PathVariable("stakeholder") String stakeholder, |
|
375 |
@PathVariable("topic") String topic, |
|
376 |
@PathVariable("category") String category, |
|
377 |
@PathVariable("subcategory") String subcategory, |
|
378 |
@PathVariable("id") String id) { |
|
379 |
|
|
380 |
Stakeholder _stakeholder = stakeholderDAO.findByAlias(stakeholder); |
|
381 |
if(_stakeholder != null) { |
|
382 |
Topic _topic = _stakeholder.getTopics().stream() |
|
383 |
.filter(current_topic -> current_topic.getAlias().equals(topic)) |
|
384 |
.findFirst() |
|
385 |
.orElse(null); |
|
386 |
if(_topic != null) { |
|
387 |
Category _category = _topic.getCategories().stream() |
|
388 |
.filter(current_category -> current_category.getAlias().equals(category)) |
|
389 |
.findFirst() |
|
390 |
.orElse(null); |
|
391 |
if(_category != null) { |
|
392 |
SubCategory _subCategory = _category.getSubCategories().stream() |
|
393 |
.filter(current_subCategory -> current_subCategory.getAlias().equals(subcategory)) |
|
394 |
.findFirst() |
|
395 |
.orElse(null); |
|
396 |
if(_subCategory != null) { |
|
397 |
List<String> indicators = null; |
|
398 |
|
|
399 |
Indicator indicator = indicatorDAO.findById(id); |
|
400 |
if(indicator.hasType("chart")) { |
|
401 |
indicators =_subCategory.getCharts(); |
|
402 |
} else if(indicator.hasType("number")) { |
|
403 |
indicators =_subCategory.getNumbers(); |
|
227 |
for (String chartId : subcategory.getCharts()) { |
|
228 |
indicatorDAO.delete(chartId); |
|
404 | 229 |
} |
230 |
subcategory.setCharts(null); |
|
405 | 231 |
|
406 |
if(indicators == null) {
|
|
407 |
// EXCEPTION - No indicators found
|
|
232 |
for (String numberId : subcategory.getNumbers()) {
|
|
233 |
indicatorDAO.delete(numberId);
|
|
408 | 234 |
} |
235 |
subcategory.setNumbers(null); |
|
409 | 236 |
|
410 |
//List<String> finalIndicators = indicators; |
|
411 |
//log.debug("Indicators size: "+finalIndicators.size()); |
|
412 |
// int index = IntStream.range(0, indicators.size()) |
|
413 |
// .filter(i -> indicatorId.equals(finalIndicators.get(i))) |
|
414 |
// .findFirst() |
|
415 |
// .orElse(-1); // return -1 if target is not found |
|
416 |
|
|
417 |
boolean indicatorFound = false; |
|
418 |
Iterator<String> indicatorIterator = indicators.iterator(); |
|
419 |
while (indicatorIterator.hasNext()) { |
|
420 |
String indicatorId = indicatorIterator.next(); |
|
421 |
log.debug(id + " vs "+indicatorId); |
|
422 |
if(id.equals(indicatorId)) { |
|
423 |
indicatorIterator.remove(); |
|
424 |
indicatorFound = true; |
|
425 |
break; |
|
426 |
} |
|
427 |
} |
|
428 |
|
|
429 |
log.debug(indicatorFound); |
|
430 |
if(!indicatorFound) { |
|
431 |
return false; |
|
432 |
// EXCEPTION - Indicator not found |
|
433 |
} |
|
434 |
//indicators.remove(index); |
|
435 |
|
|
436 |
stakeholderDAO.save(_stakeholder); |
|
437 |
indicatorDAO.delete(id); |
|
438 |
} else { |
|
439 |
// EXCEPTION - Subcategory not found |
|
237 |
subCategoryDAO.delete(subCategoryId); |
|
440 | 238 |
} |
441 |
} else {
|
|
442 |
// EXCEPTION - Category not found
|
|
239 |
category.setSubCategories(null);
|
|
240 |
categoryDAO.delete(categoryId);
|
|
443 | 241 |
} |
444 |
} else {
|
|
445 |
// EXCEPTION - Topic not found
|
|
242 |
topic.setCategories(null);
|
|
243 |
topicDAO.delete(topicId);
|
|
446 | 244 |
} |
245 |
stakeholder.setTopics(null); |
|
246 |
stakeholderDAO.delete(stakeholderId); |
|
247 |
log.debug("Stakeholder deleted!"); |
|
447 | 248 |
} else { |
448 | 249 |
// EXCEPTION - Stakeholder not found |
250 |
throw new EntityNotFoundException("Delete stakeholder: Stakeholder with id: "+stakeholderId+" not found"); |
|
449 | 251 |
} |
450 | 252 |
return true; |
451 | 253 |
} |
452 | 254 |
|
453 |
// @RequestMapping(value = "/{stakeholder}/{topic}/{category}/{subcategory}/indicator/delete", method = RequestMethod.POST) |
|
454 |
// public boolean deleteChartPost(@PathVariable("stakeholder") String stakeholder, |
|
455 |
// @PathVariable("topic") String topic, |
|
456 |
// @PathVariable("category") String category, |
|
457 |
// @PathVariable("subcategory") String subcategory, |
|
458 |
// @RequestBody String indicatorId) { |
|
459 |
// //String id = chart.getId(); |
|
460 |
// return deleteIndicator(stakeholder, topic, category, subcategory, indicatorId); |
|
461 |
// } |
|
462 | 255 |
|
463 |
|
|
464 |
// path variables are alias-es. Each alias must be unique. |
|
465 |
@RequestMapping(value = "/{stakeholder}/{topic}/{category}/{subcategory}/indicator/save", method = RequestMethod.POST) |
|
466 |
public Indicator saveIndicator(@PathVariable("stakeholder") String stakeholder, |
|
467 |
@PathVariable("topic") String topic, |
|
468 |
@PathVariable("category") String category, |
|
469 |
@PathVariable("subcategory") String subcategory, |
|
470 |
@RequestBody Indicator indicator) { |
|
471 |
|
|
472 |
Indicator indicatorSaved = null; |
|
473 |
if(indicator.getId() != null) { |
|
474 |
log.debug("indicator is already saved"); |
|
475 |
indicatorSaved = indicatorDAO.save(indicator); |
|
476 |
} else { |
|
477 |
log.debug("to save indicator"); |
|
478 |
Stakeholder _stakeholder = stakeholderDAO.findByAlias(stakeholder); |
|
479 |
if (_stakeholder != null) { |
|
480 |
Topic _topic = _stakeholder.getTopics().stream() |
|
481 |
.filter(current_topic -> current_topic.getAlias().equals(topic)) |
|
482 |
.findFirst() |
|
483 |
.orElse(null); |
|
484 |
if (_topic != null) { |
|
485 |
Category _category = _topic.getCategories().stream() |
|
486 |
.filter(current_category -> current_category.getAlias().equals(category)) |
|
487 |
.findFirst() |
|
488 |
.orElse(null); |
|
489 |
if (_category != null) { |
|
490 |
SubCategory _subCategory = _category.getSubCategories().stream() |
|
491 |
.filter(current_subCategory -> current_subCategory.getAlias().equals(subcategory)) |
|
492 |
.findFirst() |
|
493 |
.orElse(null); |
|
494 |
if (_subCategory != null) { |
|
495 |
indicatorSaved = indicatorDAO.save(indicator); |
|
496 |
|
|
497 |
List<String> indicators = null; |
|
498 |
|
|
499 |
if (indicator.hasType("chart")) { |
|
500 |
indicators = _subCategory.getCharts(); |
|
501 |
} else if (indicator.hasType("number")) { |
|
502 |
indicators = _subCategory.getNumbers(); |
|
503 |
} |
|
504 |
|
|
505 |
String indicatorId; |
|
506 |
if (indicator.getId() != null) { |
|
507 |
indicatorId = indicators.stream() |
|
508 |
.filter(current_indicator -> current_indicator.equals(indicator.getId())) |
|
509 |
.findFirst() |
|
510 |
.orElse(null); |
|
511 |
|
|
512 |
if (indicatorId == null) { // indicator is not already at this position |
|
513 |
indicators.add(indicator.getId()); |
|
514 |
} |
|
515 |
} |
|
516 |
stakeholderDAO.save(_stakeholder); |
|
517 |
} else { |
|
518 |
// EXCEPTION - Subcategory not found |
|
519 |
} |
|
520 |
} else { |
|
521 |
// EXCEPTION - Category not found |
|
522 |
} |
|
523 |
} else { |
|
524 |
// EXCEPTION - Topic not found |
|
525 |
} |
|
526 |
} else { |
|
527 |
// EXCEPTION - Stakeholder not found |
|
528 |
} |
|
529 |
} |
|
530 |
|
|
531 |
return indicatorSaved; |
|
532 |
} |
|
533 |
|
|
534 |
@RequestMapping(value = "/{stakeholder}/{topic}/{category}/{subcategory}/indicators/reorder", method = RequestMethod.POST) |
|
535 |
public List<Indicator> reorderIndicators(String stakeholder, String topic, |
|
536 |
String category, String subcategory, |
|
537 |
List<Indicator> indicators, String type) { |
|
538 |
Stakeholder _stakeholder = stakeholderDAO.findByAlias(stakeholder); |
|
539 |
if(_stakeholder != null) { |
|
540 |
Topic _topic = _stakeholder.getTopics().stream() |
|
541 |
.filter(current_topic -> current_topic.getAlias().equals(topic)) |
|
542 |
.findFirst() |
|
543 |
.orElse(null); |
|
544 |
if(_topic != null) { |
|
545 |
Category _category = _topic.getCategories().stream() |
|
546 |
.filter(current_category -> current_category.getAlias().equals(category)) |
|
547 |
.findFirst() |
|
548 |
.orElse(null); |
|
549 |
if(_category != null) { |
|
550 |
SubCategory _subCategory = _category.getSubCategories().stream() |
|
551 |
.filter(current_subCategory -> current_subCategory.getAlias().equals(subcategory)) |
|
552 |
.findFirst() |
|
553 |
.orElse(null); |
|
554 |
if(_subCategory != null) { |
|
555 |
List<String> _indicators = null; |
|
556 |
if(type.equals("chart")) { |
|
557 |
_indicators = _subCategory.getCharts(); |
|
558 |
} else if(type.equals("number")) { |
|
559 |
_indicators = _subCategory.getNumbers(); |
|
560 |
} |
|
561 |
|
|
562 |
_indicators.clear(); |
|
563 |
for(Indicator indicator : indicators) { |
|
564 |
_indicators.add(indicator.getId()); |
|
565 |
} |
|
566 |
stakeholderDAO.save(_stakeholder); |
|
567 |
} else { |
|
568 |
// EXCEPTION - Subcategory not found |
|
569 |
} |
|
570 |
} else { |
|
571 |
// EXCEPTION - Vategory not found |
|
572 |
} |
|
573 |
} else { |
|
574 |
// EXCEPTION - Topic not found |
|
575 |
} |
|
576 |
} else { |
|
577 |
// EXCEPTION - Stakeholder not found |
|
578 |
} |
|
579 |
|
|
580 |
return indicators; |
|
581 |
} |
|
582 |
|
|
583 |
|
|
584 |
|
|
585 | 256 |
// The following are not supposed to be used |
586 |
|
|
587 |
// should i delete indicators that were in the list but they are not in the new list? |
|
588 |
@RequestMapping(value = "/{stakeholder}/{topic}/{category}/{subcategory}/charts/save", method = RequestMethod.POST) |
|
589 |
public List<Indicator> saveCharts(@PathVariable("stakeholder") String stakeholder, |
|
590 |
@PathVariable("topic") String topic, |
|
591 |
@PathVariable("category") String category, |
|
592 |
@PathVariable("subcategory") String subcategory, |
|
593 |
@RequestBody List<Indicator> charts) { |
|
594 |
log.debug(charts); |
|
595 |
log.debug(charts.size()); |
|
596 |
log.debug(charts.getClass().getName()); |
|
597 |
log.debug(charts.get(0).getClass().getName()); |
|
598 |
return saveIndicators(stakeholder, topic, category, subcategory, charts, "chart"); |
|
599 |
} |
|
600 |
|
|
601 |
// should i delete indicators that were in the list but they are not in the new list? |
|
602 |
@RequestMapping(value = "/{stakeholder}/{topic}/{category}/{subcategory}/numbers/save", method = RequestMethod.POST) |
|
603 |
public List<Indicator> saveNumbers(@PathVariable("stakeholder") String stakeholder, |
|
604 |
@PathVariable("topic") String topic, |
|
605 |
@PathVariable("category") String category, |
|
606 |
@PathVariable("subcategory") String subcategory, |
|
607 |
@RequestBody List<Indicator> numbers) { |
|
608 |
return saveIndicators(stakeholder, topic, category, subcategory, numbers, "number"); |
|
609 |
} |
|
610 |
|
|
611 |
public List<Indicator> saveIndicators(String stakeholder, String topic, |
|
612 |
String category, String subcategory, |
|
613 |
List<Indicator> indicators, String type) { |
|
614 |
log.debug("to save indicators: "+indicators.size()); |
|
615 |
List<Indicator> indicatorsSaved = new ArrayList<>(); |
|
616 |
for(Indicator indicator : indicators) { |
|
617 |
indicatorsSaved.add(indicatorDAO.save(indicator)); |
|
618 |
} |
|
619 |
log.debug("saved indicators: "+indicators.size()); |
|
620 |
|
|
621 |
Stakeholder _stakeholder = stakeholderDAO.findByAlias(stakeholder); |
|
622 |
if(_stakeholder != null) { |
|
623 |
Topic _topic = _stakeholder.getTopics().stream() |
|
624 |
.filter(current_topic -> current_topic.getAlias().equals(topic)) |
|
625 |
.findFirst() |
|
626 |
.orElse(null); |
|
627 |
if(_topic != null) { |
|
628 |
Category _category = _topic.getCategories().stream() |
|
629 |
.filter(current_category -> current_category.getAlias().equals(category)) |
|
630 |
.findFirst() |
|
631 |
.orElse(null); |
|
632 |
if(_category != null) { |
|
633 |
SubCategory _subCategory = _category.getSubCategories().stream() |
|
634 |
.filter(current_subCategory -> current_subCategory.getAlias().equals(subcategory)) |
|
635 |
.findFirst() |
|
636 |
.orElse(null); |
|
637 |
if(_subCategory != null) { |
|
638 |
List<String> _indicators = null; |
|
639 |
if(type.equals("chart")) { |
|
640 |
_indicators = _subCategory.getCharts(); |
|
641 |
} else if(type.equals("number")) { |
|
642 |
_indicators = _subCategory.getNumbers(); |
|
643 |
} |
|
644 |
|
|
645 |
_indicators.clear(); |
|
646 |
for(Indicator indicator : indicators) { |
|
647 |
_indicators.add(indicator.getId()); |
|
648 |
} |
|
649 |
stakeholderDAO.save(_stakeholder); |
|
650 |
} else { |
|
651 |
// EXCEPTION - Subcategory not found |
|
652 |
} |
|
653 |
} else { |
|
654 |
// EXCEPTION - Vategory not found |
|
655 |
} |
|
656 |
} else { |
|
657 |
// EXCEPTION - Topic not found |
|
658 |
} |
|
659 |
} else { |
|
660 |
// EXCEPTION - Stakeholder not found |
|
661 |
} |
|
662 |
|
|
663 |
return indicatorsSaved; |
|
664 |
} |
|
665 |
|
|
666 |
|
|
667 |
|
|
668 |
// Remember to check if alias is not already used before saving |
|
669 |
@RequestMapping(value = "/{stakeholder}/topic/save", method = RequestMethod.POST) |
|
670 |
public Stakeholder saveTopic(@PathVariable("stakeholder") String stakeholder, |
|
671 |
@RequestBody Topic topic) { |
|
672 |
Stakeholder stakeholderSaved = null; |
|
673 |
Stakeholder _stakeholder = stakeholderDAO.findByAlias(stakeholder); |
|
674 |
if (stakeholder != null) { |
|
675 |
List<Topic> topics = _stakeholder.getTopics(); |
|
676 |
Topic _topic = topics.stream() |
|
677 |
.filter(current_topic -> current_topic.getAlias().equals(topic.getAlias())) |
|
678 |
.findFirst() |
|
679 |
.orElse(null); |
|
680 |
if(_topic != null) { |
|
681 |
_topic = topic; |
|
682 |
} else { |
|
683 |
topics.add(topic); |
|
684 |
_stakeholder.setTopics(topics); |
|
685 |
} |
|
686 |
stakeholderSaved = stakeholderDAO.save(_stakeholder); |
|
687 |
} |
|
688 |
return stakeholderSaved; |
|
689 |
} |
|
690 |
|
|
691 |
@RequestMapping(value = "/{stakeholder}/topics/save", method = RequestMethod.POST) |
|
692 |
public Stakeholder saveTopics(@PathVariable("stakeholder") String stakeholder, |
|
693 |
@RequestBody List<Topic> topics) { |
|
694 |
Stakeholder stakeholderSaved = null; |
|
695 |
Stakeholder _stakeholder = stakeholderDAO.findByAlias(stakeholder); |
|
696 |
if (stakeholder != null) { |
|
697 |
_stakeholder.setTopics(topics); |
|
698 |
stakeholderSaved = stakeholderDAO.save(_stakeholder); |
|
699 |
} |
|
700 |
return stakeholderSaved; |
|
701 |
} |
|
702 |
|
|
703 |
|
|
704 |
@RequestMapping(value = "/stakeholder/dates", method = RequestMethod.GET) |
|
705 |
public List<Date> getAllStakeholderDates() { |
|
706 |
List<Stakeholder> profiles = stakeholderDAO.findAll(); |
|
707 |
List<Date> profileDates = new ArrayList<>(); |
|
708 |
|
|
709 |
int i=0; |
|
710 |
for(Stakeholder profile : profiles) { |
|
711 |
log.debug(profile.getCreationDate()); |
|
712 |
profileDates.add(profile.getCreationDate()); |
|
713 |
log.debug(profileDates.get(i)); |
|
714 |
i++; |
|
715 |
} |
|
716 |
return profileDates; |
|
717 |
} |
|
718 |
|
|
719 |
@RequestMapping(value = "/stakeholder/dates1", method = RequestMethod.GET) |
|
720 |
public List<String> getAllStakeholderDates1() { |
|
721 |
List<Stakeholder> profiles = stakeholderDAO.findAll(); |
|
722 |
List<String> profileDates = new ArrayList<>(); |
|
723 |
|
|
724 |
for(Stakeholder profile : profiles) { |
|
725 |
log.debug(profile.getCreationDate().toString()); |
|
726 |
profileDates.add(profile.getCreationDate().toString()); |
|
727 |
} |
|
728 |
return profileDates; |
|
729 |
} |
|
730 |
|
|
731 |
@RequestMapping(value = "/stakeholder/dates2", method = RequestMethod.GET) |
|
732 |
public List<String> getAllStakeholderDates2() { |
|
733 |
List<Stakeholder> profiles = stakeholderDAO.findAll(); |
|
734 |
List<String> profileDates = new ArrayList<>(); |
|
735 |
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
|
736 |
|
|
737 |
for(Stakeholder profile : profiles) { |
|
738 |
log.debug(format.format(profile.getCreationDate())); |
|
739 |
profileDates.add(format.format(profile.getCreationDate())); |
|
740 |
} |
|
741 |
return profileDates; |
|
742 |
} |
|
257 |
// @RequestMapping(value = "/stakeholder/dates", method = RequestMethod.GET) |
|
258 |
// public List<Date> getAllStakeholderDates() { |
|
259 |
// List<Stakeholder> profiles = stakeholderDAO.findAll(); |
|
260 |
// List<Date> profileDates = new ArrayList<>(); |
|
261 |
// |
|
262 |
// int i=0; |
|
263 |
// for(Stakeholder profile : profiles) { |
|
264 |
// log.debug(profile.getCreationDate()); |
|
265 |
// profileDates.add(profile.getCreationDate()); |
|
266 |
// log.debug(profileDates.get(i)); |
|
267 |
// i++; |
|
268 |
// } |
|
269 |
// return profileDates; |
|
270 |
// } |
|
271 |
// |
|
272 |
// @RequestMapping(value = "/stakeholder/dates1", method = RequestMethod.GET) |
|
273 |
// public List<String> getAllStakeholderDates1() { |
|
274 |
// List<Stakeholder> profiles = stakeholderDAO.findAll(); |
|
275 |
// List<String> profileDates = new ArrayList<>(); |
|
276 |
// |
|
277 |
// for(Stakeholder profile : profiles) { |
|
278 |
// log.debug(profile.getCreationDate().toString()); |
|
279 |
// profileDates.add(profile.getCreationDate().toString()); |
|
280 |
// } |
|
281 |
// return profileDates; |
|
282 |
// } |
|
283 |
// |
|
284 |
// @RequestMapping(value = "/stakeholder/dates2", method = RequestMethod.GET) |
|
285 |
// public List<String> getAllStakeholderDates2() { |
|
286 |
// List<Stakeholder> profiles = stakeholderDAO.findAll(); |
|
287 |
// List<String> profileDates = new ArrayList<>(); |
|
288 |
// SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
|
289 |
// |
|
290 |
// for(Stakeholder profile : profiles) { |
|
291 |
// log.debug(format.format(profile.getCreationDate())); |
|
292 |
// profileDates.add(format.format(profile.getCreationDate())); |
|
293 |
// } |
|
294 |
// return profileDates; |
|
295 |
// } |
|
743 | 296 |
} |
Also available in: Unified diff
1. Schema changes: Move each entity on its own collection - each entity keeps ids for its sub-entities.
2. New controllers for each entity: TopicController.java, CategoryController.java, SubCategoryController.java, IndicatorController.java.
3. New DAOs for each entity: TopicDAO.java, MongoDBTopicDAO.java, CategoryDAO.java, MongoDBCategoryDAO.java, SubCategoryDAO.java, MongoDBSubCategoryDAO.java.
4. New custom Exceptions: EntityNotFoundException.java, PathNotValidException.java.
5. ExceptionsHandler.java: Handle new EntityNotFoundException (id not in db) and PathNotValidException (id exists in db but not in path given).