Project

General

Profile

1
package eu.dnetlib.uoamonitorservice.controllers;
2

    
3

    
4
import eu.dnetlib.uoamonitorservice.dao.*;
5
import eu.dnetlib.uoamonitorservice.entities.*;
6
import eu.dnetlib.uoamonitorservice.handlers.EntityNotFoundException;
7
import eu.dnetlib.uoamonitorservice.handlers.PathNotValidException;
8
import org.apache.log4j.Logger;
9
import org.springframework.beans.factory.annotation.Autowired;
10
import org.springframework.web.bind.annotation.*;
11

    
12
import java.util.ArrayList;
13
import java.util.Iterator;
14
import java.util.List;
15

    
16
@RestController
17
@CrossOrigin(origins = "*")
18
public class IndicatorController {
19
    private final Logger log = Logger.getLogger(this.getClass());
20

    
21
    @Autowired
22
    private StakeholderDAO stakeholderDAO;
23

    
24
    @Autowired
25
    private TopicDAO topicDAO;
26

    
27
    @Autowired
28
    private CategoryDAO categoryDAO;
29

    
30
    @Autowired
31
    private SubCategoryDAO subCategoryDAO;
32

    
33
    @Autowired
34
    private IndicatorDAO indicatorDAO;
35

    
36

    
37
    @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/save", method = RequestMethod.POST)
38
    public Indicator saveIndicator(@PathVariable("stakeholderId") String stakeholderId,
39
                                   @PathVariable("topicId") String topicId,
40
                                   @PathVariable("categoryId") String categoryId,
41
                                   @PathVariable("subcategoryId") String subcategoryId,
42
                                   @RequestBody Indicator indicator) {
43
        log.debug("save indicator");
44

    
45
//        if(stakeholderId == null) {
46
//            // EXCEPTION - Parameter for Stakeholder is not accepted
47
//        }
48
//        if(topicId == null) {
49
//            // EXCEPTION - Parameter for Topic is not accepted
50
//        }
51
//        if(categoryId == null) {
52
//            // EXCEPTION - Parameter for Category is not accepted
53
//        }
54
//        if(subcategoryId == null) {
55
//            // EXCEPTION - Parameter for SubCategory is not accepted
56
//        }
57
//        if(indicator == null) {
58
//            // EXCEPTION - Parameter for Indicator is not accepted
59
//        }
60

    
61
        Stakeholder<String> stakeholder = stakeholderDAO.findById(stakeholderId);
62

    
63
        if(stakeholder != null) {
64

    
65
            Topic<String> topic = topicDAO.findById(topicId);
66
            if(topic != null) {
67
                if(stakeholder.getTopics().contains(topicId)) {
68

    
69
                    Category<String> category = categoryDAO.findById(categoryId);
70
                    if(category != null) {
71
                        if(topic.getCategories().contains(categoryId)) {
72

    
73
                            SubCategory<String> subcategory = subCategoryDAO.findById(subcategoryId);
74
                            if(subcategory != null) {
75
                                if (category.getSubCategories().contains(subcategoryId)) {
76
                                    Indicator indicatorSaved = indicatorDAO.save(indicator);
77

    
78
                                    List<String> indicators = null;
79
                                    //if(indicator.hasType("chart")) {
80
                                    if(indicator.getType().equals("chart")) {
81
                                        indicators = subcategory.getCharts();
82
                                        //} else if(indicator.hasType("number")) {
83
                                    } else if(indicator.getType().equals("number")) {
84
                                        indicators = subcategory.getNumbers();
85
                                    }
86

    
87
                                    int index = indicators.indexOf(indicatorSaved.getId());
88
                                    if (index == -1) {
89
                                        indicators.add(indicatorSaved.getId());
90
                                        subCategoryDAO.save(subcategory);
91
                                        log.debug("Indicator saved!");
92

    
93
                                        indicator.setId(indicatorSaved.getId());
94
                                    }
95
                                } else {
96
                                    // EXCEPTION - SubCategory not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); -> Category: category.getAlias();
97
                                    throw new PathNotValidException("Save indicator: SubCategory with id: "+subcategoryId+" not found in Category: "+categoryId);
98
                                }
99
                            } else {
100
                                // EXCEPTION - SubCategory not found
101
                                throw new EntityNotFoundException("Save indicator: SubCategory with id: "+subcategoryId+" not found");
102
                            }
103
                        } else {
104
                            // EXCEPTION - Category not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias();
105
                            throw new PathNotValidException("Save indicator: Category with id: "+categoryId+" not found in Topic: "+topicId);
106
                        }
107
                    } else {
108
                        // EXCEPTION - Category not found
109
                        throw new EntityNotFoundException("Save indicator: Category with id: "+categoryId+" not found");
110
                    }
111
                } else {
112
                    // EXCEPTION - Topic not found in Stakeholder: stakeholder.getAlias();
113
                    throw new PathNotValidException("Save indicator: Topic with id: "+topicId+" not found in Stakeholder: "+stakeholderId);
114
                }
115
            } else {
116
                // EXCEPTION - Topic not found
117
                throw new EntityNotFoundException("Save indicator: Topic with id: "+topicId+" not found");
118
            }
119
        } else {
120
            // EXCEPTION - Stakeholder not found
121
            throw new EntityNotFoundException("Save indicator: Stakeholder with id: "+stakeholderId+" not found");
122
        }
123
        return indicator;
124
    }
125

    
126
    @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/{indicatorId}/delete", method = RequestMethod.DELETE)
127
    public boolean deleteIndicator(@PathVariable("stakeholderId") String stakeholderId,
128
                                   @PathVariable("topicId") String topicId,
129
                                   @PathVariable("categoryId") String categoryId,
130
                                   @PathVariable("subcategoryId") String subcategoryId,
131
                                   @PathVariable("indicatorId") String indicatorId) {
132
        log.debug("delete indicator");
133

    
134
        Stakeholder<String> stakeholder = stakeholderDAO.findById(stakeholderId);
135

    
136
        if(stakeholder != null) {
137

    
138
            Topic<String> topic = topicDAO.findById(topicId);
139
            if(topic != null) {
140
                if(stakeholder.getTopics().contains(topicId)) {
141

    
142
                    Category<String> category = categoryDAO.findById(categoryId);
143
                    if(category != null) {
144
                        if(topic.getCategories().contains(categoryId)) {
145

    
146
                            SubCategory<String> subcategory = subCategoryDAO.findById(subcategoryId);
147
                            if(subcategory != null) {
148
                                if(category.getSubCategories().contains(subcategoryId)) {
149

    
150
                                    Indicator indicator = indicatorDAO.findById(indicatorId);
151
                                    if(indicator != null) {
152
                                        List<String> indicators = null;
153
                                        if (indicator.getType().equals("chart")) {
154
                                            indicators = subcategory.getCharts();
155
                                        } else if (indicator.getType().equals("number")) {
156
                                            indicators = subcategory.getNumbers();
157
                                        }
158

    
159
                                        int index = indicators.indexOf(indicatorId);
160
                                        if (index != -1) {
161
                                            indicators.remove(index);
162
                                            subCategoryDAO.save(subcategory);
163

    
164
                                            indicatorDAO.delete(indicatorId);
165
                                            log.debug("Indicator deleted!");
166
                                        } else {
167
                                            // EXCEPTION - Indicator not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); -> Category: category.getAlias(); -> SubCategory: subcategory.getAlias();
168
                                            throw new PathNotValidException("Delete indicator: Indicator with id: "+indicatorId+" not found in SubCategory: "+subcategoryId);
169
                                        }
170
                                    } else {
171
                                        // EXCEPTION - Indicator not found
172
                                        throw new EntityNotFoundException("Delete indicator: Indicator with id: "+indicatorId+" not found");
173
                                    }
174
                                } else {
175
                                    // EXCEPTION - SubCategory not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); -> Category: category.getAlias();
176
                                    throw new PathNotValidException("Delete indicator: SubCategory with id: "+subcategoryId+" not found in Category: "+categoryId);
177
                                }
178
                            } else {
179
                                // EXCEPTION - SubCategory not found
180
                                throw new EntityNotFoundException("Delete indicator: SubCategory with id: "+subcategoryId+" not found");
181
                            }
182
                        } else {
183
                            // EXCEPTION - Category not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias();
184
                            throw new PathNotValidException("Delete indicator: Category with id: "+categoryId+" not found in Topic: "+topicId);
185
                        }
186
                    } else {
187
                        // EXCEPTION - Category not found
188
                        throw new EntityNotFoundException("Delete indicator: Category with id: "+categoryId+" not found");
189
                    }
190
                } else {
191
                    // EXCEPTION - Topic not found in Stakeholder: stakeholder.getAlias();
192
                    throw new PathNotValidException("Delete indicator: Topic with id: "+topicId+" not found in Stakeholder: "+stakeholderId);
193
                }
194
            } else {
195
                // EXCEPTION - Topic not found
196
                throw new EntityNotFoundException("Delete indicator: Topic with id: "+topicId+" not found");
197
            }
198
        } else {
199
            // EXCEPTION - Stakeholder not found
200
            throw new EntityNotFoundException("Delete indicator: Stakeholder with id: "+stakeholderId+" not found");
201
        }
202
        return true;
203
    }
204

    
205
    @RequestMapping(value = "/{stakeholderId}/charts/delete", method = RequestMethod.DELETE)
206
    public boolean deleteAllChartIndicators(@PathVariable("stakeholderId") String stakeholderId) {
207
        log.debug("delete indicator");
208

    
209
        Stakeholder<String> stakeholder = stakeholderDAO.findById(stakeholderId);
210
        if(stakeholder != null) {
211

    
212
            for(String topicId : stakeholder.getTopics()) {
213
                Topic<String> topic = topicDAO.findById(topicId);
214
                if(topic != null) {
215
                    for(String categoryId : topic.getCategories()) {
216
                        Category<String> category = categoryDAO.findById(categoryId);
217
                        if(category != null) {
218
                            for(String subcategoryId : category.getSubCategories()) {
219
                                SubCategory<String> subcategory = subCategoryDAO.findById(subcategoryId);
220
                                if(subcategory != null) {
221

    
222
                                    List<String> indicators = subcategory.getCharts();
223
                                    Iterator<String> indicatorsIterator = subcategory.getCharts().iterator();
224

    
225
                                    while (indicatorsIterator.hasNext()) {
226
                                        String indicatorId = indicatorsIterator.next();
227
                                        Indicator indicator = indicatorDAO.findById(indicatorId);
228
                                        if (indicator != null) {
229
                                            int index = indicators.indexOf(indicatorId);
230
                                            if (index != -1) {
231
                                                indicatorsIterator.remove();
232
                                                //indicators.remove(index);
233

    
234
                                                indicatorDAO.delete(indicatorId);
235
                                                log.debug("Indicator deleted!");
236
                                            } else {
237
                                                // EXCEPTION - Indicator not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); -> Category: category.getAlias(); -> SubCategory: subcategory.getAlias();
238
                                                throw new PathNotValidException("Delete indicator: Indicator with id: " + indicatorId + " not found in SubCategory: " + subcategoryId);
239
                                            }
240
                                        } else {
241
                                            // EXCEPTION - Indicator not found
242
                                            throw new EntityNotFoundException("Delete indicator: Indicator with id: " + indicatorId + " not found");
243
                                        }
244
                                    }
245
                                    subCategoryDAO.save(subcategory);
246
                                } else {
247
                                    // EXCEPTION - SubCategory not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); -> Category: category.getAlias();
248
                                    throw new PathNotValidException("Delete indicator: SubCategory with id: "+subcategoryId+" not found in Category: "+categoryId);
249
                                }
250
                            }
251
                        } else {
252
                            // EXCEPTION - Category not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias();
253
                            throw new PathNotValidException("Delete indicator: Category with id: "+categoryId+" not found in Topic: "+topicId);
254
                        }
255
                    }
256
                } else {
257
                    // EXCEPTION - Topic not found in Stakeholder: stakeholder.getAlias();
258
                    throw new PathNotValidException("Delete indicator: Topic with id: "+topicId+" not found in Stakeholder: "+stakeholderId);
259
                }
260
            }
261
        } else {
262
            // EXCEPTION - Stakeholder not found
263
            throw new EntityNotFoundException("Delete indicator: Stakeholder with id: "+stakeholderId+" not found");
264
        }
265
        return true;
266
    }
267

    
268
//    @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/{type}/reorder", method = RequestMethod.POST)
269
//    public boolean reorderIndicators(@PathVariable("stakeholderId") String stakeholderId,
270
//                                             @PathVariable("topicId") String topicId,
271
//                                             @PathVariable("categoryId") String categoryId,
272
//                                             @PathVariable("subcategoryId") String subcategoryId,
273
//                                             @PathVariable("type") String type,
274
//                                             @RequestBody List<Indicator> indicatorsFull) {
275
//        log.debug("reorder indicators");
276
//
277
//        Stakeholder<String> stakeholder = stakeholderDAO.findById(stakeholderId);
278
//
279
//        if (stakeholder != null) {
280
//
281
//            Topic<String> topic = topicDAO.findById(topicId);
282
//            if (topic != null) {
283
//                if (stakeholder.getTopics().contains(topicId)) {
284
//
285
//                    Category<String> category = categoryDAO.findById(categoryId);
286
//                    if (category != null) {
287
//                        if (topic.getCategories().contains(categoryId)) {
288
//
289
//                            SubCategory<String> subcategory = subCategoryDAO.findById(subcategoryId);
290
//                            if (subcategory != null) {
291
//                                if (category.getSubCategories().contains(subcategoryId)) {
292
//
293
//                                    List<String> indicators = new ArrayList<>();
294
//                                    for(Indicator indicator : indicatorsFull) {
295
//                                        indicators.add(indicator.getId());
296
//                                    }
297
//
298
//                                    if(type.equals("chart")) {
299
//                                        subcategory.setCharts(indicators);
300
//                                    } else if(type.equals("number")) {
301
//                                        subcategory.setNumbers(indicators);
302
//                                    }
303
//
304
//                                    subCategoryDAO.save(subcategory);
305
//                                    indicators = null;
306
//                                    log.debug("Indicators reordered!");
307
//                                } else {
308
//                                    // EXCEPTION - SubCategory not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); -> Category: category.getAlias();
309
//                                    throw new PathNotValidException("Reorder indicators: SubCategory with id: "+subcategoryId+" not found in Category: "+categoryId);
310
//                                }
311
//                            } else {
312
//                                // EXCEPTION - SubCategory not found
313
//                                throw new EntityNotFoundException("Reorder indicators: SubCategory with id: "+subcategoryId+" not found");
314
//                            }
315
//                        } else {
316
//                            // EXCEPTION - Category not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias();
317
//                            throw new PathNotValidException("Reorder indicators: Category with id: "+categoryId+" not found in Topic: "+topicId);
318
//                        }
319
//                    } else {
320
//                        // EXCEPTION - Category not found
321
//                        throw new EntityNotFoundException("Reorder indicators: Category with id: "+categoryId+" not found");
322
//                    }
323
//                } else {
324
//                    // EXCEPTION - Topic not found in Stakeholder: stakeholder.getAlias();
325
//                    throw new PathNotValidException("Reorder indicators: Topic with id: "+topicId+" not found in Stakeholder: "+stakeholderId);
326
//                }
327
//            } else {
328
//                // EXCEPTION - Topic not found
329
//                throw new EntityNotFoundException("Reorder indicators: Topic with id: "+topicId+" not found");
330
//            }
331
//        } else {
332
//            // EXCEPTION - Stakeholder not found
333
//            throw new EntityNotFoundException("Reorder indicators: Stakeholder with id: "+stakeholderId+" not found");
334
//        }
335
//        return true;
336
//    }
337

    
338
    @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/{type}/reorder", method = RequestMethod.POST)
339
    public List<Indicator> reorderIndicators(@PathVariable("stakeholderId") String stakeholderId,
340
                                     @PathVariable("topicId") String topicId,
341
                                     @PathVariable("categoryId") String categoryId,
342
                                     @PathVariable("subcategoryId") String subcategoryId,
343
                                     @PathVariable("type") String type,
344
                                     @RequestBody List<String> indicators) {
345
        log.debug("reorder indicators");
346

    
347
        Stakeholder<String> stakeholder = stakeholderDAO.findById(stakeholderId);
348

    
349
        if (stakeholder != null) {
350

    
351
            Topic<String> topic = topicDAO.findById(topicId);
352
            if (topic != null) {
353
                if (stakeholder.getTopics().contains(topicId)) {
354

    
355
                    Category<String> category = categoryDAO.findById(categoryId);
356
                    if (category != null) {
357
                        if (topic.getCategories().contains(categoryId)) {
358

    
359
                            SubCategory<String> subcategory = subCategoryDAO.findById(subcategoryId);
360
                            if (subcategory != null) {
361
                                if (category.getSubCategories().contains(subcategoryId)) {
362
                                    if(type.equals("chart")) {
363
                                        subcategory.setCharts(indicators);
364
                                    } else if(type.equals("number")) {
365
                                        subcategory.setNumbers(indicators);
366
                                    }
367

    
368
                                    subCategoryDAO.save(subcategory);
369
                                    log.debug("Indicators reordered!");
370
                                } else {
371
                                    // EXCEPTION - SubCategory not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); -> Category: category.getAlias();
372
                                    throw new PathNotValidException("Reorder indicators: SubCategory with id: "+subcategoryId+" not found in Category: "+categoryId);
373
                                }
374
                            } else {
375
                                // EXCEPTION - SubCategory not found
376
                                throw new EntityNotFoundException("Reorder indicators: SubCategory with id: "+subcategoryId+" not found");
377
                            }
378
                        } else {
379
                            // EXCEPTION - Category not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias();
380
                            throw new PathNotValidException("Reorder indicators: Category with id: "+categoryId+" not found in Topic: "+topicId);
381
                        }
382
                    } else {
383
                        // EXCEPTION - Category not found
384
                        throw new EntityNotFoundException("Reorder indicators: Category with id: "+categoryId+" not found");
385
                    }
386
                } else {
387
                    // EXCEPTION - Topic not found in Stakeholder: stakeholder.getAlias();
388
                    throw new PathNotValidException("Reorder indicators: Topic with id: "+topicId+" not found in Stakeholder: "+stakeholderId);
389
                }
390
            } else {
391
                // EXCEPTION - Topic not found
392
                throw new EntityNotFoundException("Reorder indicators: Topic with id: "+topicId+" not found");
393
            }
394
        } else {
395
            // EXCEPTION - Stakeholder not found
396
            throw new EntityNotFoundException("Reorder indicators: Stakeholder with id: "+stakeholderId+" not found");
397
        }
398

    
399
        List<Indicator> indicatorsFull = new ArrayList<>();
400
        for(String indicatorId : indicators) {
401
            indicatorsFull.add(indicatorDAO.findById(indicatorId));
402
        }
403
        return indicatorsFull;
404
    }
405
}
(2-2/6)