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.List;
14
import java.util.Map;
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 SectionDAO sectionDAO;
35

    
36
    @Autowired
37
    private IndicatorDAO indicatorDAO;
38

    
39

    
40
    @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/{sectionId}/save", method = RequestMethod.POST)
41
    public Indicator saveIndicator(@PathVariable("stakeholderId") String stakeholderId,
42
                                   @PathVariable("topicId") String topicId,
43
                                   @PathVariable("categoryId") String categoryId,
44
                                   @PathVariable("subcategoryId") String subcategoryId,
45
                                   @PathVariable("sectionId") String sectionId,
46
                                   @RequestBody Indicator indicator) {
47
        log.debug("save indicator");
48
        log.debug("Name: "+indicator.getName() + " - Id: "+indicator.getId() + " - Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId+ " - SubCategory: "+subcategoryId + " - Section: "+sectionId);
49

    
50
        Section<String> section = checkForExceptions(stakeholderId, topicId, categoryId, subcategoryId, sectionId, indicator.getType());
51

    
52
        String indicatorId = indicator.getId();
53
        indicatorDAO.save(indicator);
54

    
55
        Stakeholder<String> stakeholder = stakeholderDAO.findById(stakeholderId);
56
        // this indicator belongs in default profile and it is new or it is updated
57
        if(stakeholder.getDefaultId() == null) {
58
            if(indicatorId == null) {
59
                onSaveDefaultIndicator(indicator, sectionId);
60
            }
61
            else {
62
                onUpdateDefaultIndicator(indicator, stakeholder);
63
            }
64
        }
65

    
66
        List<String> indicators = section.getIndicators();
67

    
68
        int index = indicators.indexOf(indicator.getId());
69
        if (index == -1) {
70
            indicators.add(indicator.getId());
71
            sectionDAO.save(section);
72
            log.debug("Indicator saved!");
73
        }
74

    
75
        return indicator;
76
    }
77

    
78
    public void onSaveDefaultIndicator(Indicator indicator, String defaultSectionId) {
79
        log.debug("On save default indicator");
80

    
81
        // new indicator in default profile - add it on profiles of the same type
82
        List<Section> sections = sectionDAO.findByDefaultId(defaultSectionId);
83

    
84
        for (Section section : sections) {
85
            Indicator indicatorNew = new Indicator();
86
            indicatorNew.copyFromDefault(indicator);
87
            for (IndicatorPath indicatorPath : indicatorNew.getIndicatorPaths()) {
88
                Stakeholder stakeholder = stakeholderDAO.findByAlias(section.getStakeholderAlias());
89
                parameterMapping(indicatorPath, stakeholder);
90
            }
91

    
92
            indicatorDAO.save(indicatorNew);
93

    
94
            List<String> indicators = section.getIndicators();
95
            indicators.add(indicatorNew.getId());
96

    
97
            sectionDAO.save(section);
98
        }
99
    }
100

    
101
    public void onUpdateDefaultIndicator(Indicator indicator, Stakeholder stakeholder) {
102
        log.debug("On update default indicator");
103

    
104
        // indicator already exists - check if changed and update all indicators based on it
105

    
106
        boolean changed = false;
107
        List<Indicator> indicators = indicatorDAO.findByDefaultId(indicator.getId());
108

    
109
        for(Indicator indicatorBasedOnDefault : indicators) {
110
            int i = 0;
111
            List<IndicatorPath> indicatorPaths = indicatorBasedOnDefault.getIndicatorPaths();
112

    
113
            for (IndicatorPath indicatorPath : indicator.getIndicatorPaths()) {
114
                IndicatorPath indicatorPathBasedOnDefault = indicatorBasedOnDefault.getIndicatorPaths().get(i);
115

    
116
                if(indicatorPathBasedOnDefault == null) {
117
                    // Add new indicator path in existing indicators
118
                    IndicatorPath indicatorPathNew = new IndicatorPath(indicatorPath);
119
                    parameterMapping(indicatorPathNew, stakeholder);
120
                    indicatorPaths.add(indicatorPathNew);
121
                    changed = true;
122
                } else {
123
                    // Check if there are changes in indicator path and update existing indicators if needed
124
                    if(!indicatorPath.getType().equals(indicatorPathBasedOnDefault.getType())) {
125
                        indicatorPathBasedOnDefault.setType(indicatorPath.getType());
126
                        changed = true;
127
                    }
128
                    if(!indicatorPath.getSource().equals(indicatorPathBasedOnDefault.getSource())) {
129
                        indicatorPathBasedOnDefault.setSource(indicatorPath.getSource());
130
                        changed = true;
131
                    }
132
                    if(!indicatorPath.getUrl().equals(indicatorPathBasedOnDefault.getUrl())) {
133
                        indicatorPathBasedOnDefault.setUrl(indicatorPath.getUrl());
134
                        changed = true;
135
                    }
136
                    if(!indicatorPath.getChartObject().equals(indicatorPathBasedOnDefault.getChartObject())) {
137
                        indicatorPathBasedOnDefault.setChartObject(indicatorPath.getChartObject());
138
                        changed = true;
139
                    }
140
                    if(indicatorPath.getParameters().size() != indicatorPathBasedOnDefault.getParameters().size()) {
141
                        for (Map.Entry<String, String> parameter : indicatorPath.getParameters().entrySet()) {
142
                            if(!indicatorPathBasedOnDefault.getParameters().containsKey(parameter.getKey())) {
143
                                indicatorPathBasedOnDefault.getParameters().put(parameter.getKey(), parameter.getValue());
144
                            }
145
                        }
146
                        parameterMapping(indicatorPathBasedOnDefault, stakeholder);
147
                        changed = true;
148
                    }
149
                    int j=0;
150
                    for(String jsonString : indicatorPath.getJsonPath()) {
151
                        String jsonStringBasedOnDefault = indicatorPathBasedOnDefault.getJsonPath().get(j);
152
                        if(!jsonString.equals(jsonStringBasedOnDefault)) {
153
                            indicatorPathBasedOnDefault.getJsonPath().set(j, jsonString);
154
                            changed = true;
155
                        }
156
                        j++;
157
                    }
158
                }
159
                i++;
160
            }
161
            if(!changed) {
162
                break;
163
            }
164
            indicatorDAO.save(indicatorBasedOnDefault);
165
        }
166
    }
167

    
168
    public void parameterMapping(IndicatorPath indicatorPath, Stakeholder stakeholder) {
169
        if (indicatorPath.getParameters().containsKey("funder_name")) {
170
            indicatorPath.getParameters().put("funder_name", stakeholder.getIndex_name());
171
        } else if (indicatorPath.getParameters().containsKey("fsn")) {
172
            indicatorPath.getParameters().put("fsn", stakeholder.getIndex_name().toLowerCase());
173
        } else if (indicatorPath.getParameters().containsKey("funder_id")) {
174
            indicatorPath.getParameters().put("funder_id", stakeholder.getIndex_id());
175
        }
176
    }
177

    
178
    @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/{sectionId}/{indicatorId}/delete", method = RequestMethod.DELETE)
179
    public boolean deleteIndicator(@PathVariable("stakeholderId") String stakeholderId,
180
                                   @PathVariable("topicId") String topicId,
181
                                   @PathVariable("categoryId") String categoryId,
182
                                   @PathVariable("subcategoryId") String subcategoryId,
183
                                   @PathVariable("sectionId") String sectionId,
184
                                   @PathVariable("indicatorId") String indicatorId) {
185
        log.debug("delete indicator");
186
        log.debug("Id: "+indicatorId + " - Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId+ " - SubCategory: "+subcategoryId + " - Section: "+sectionId);
187

    
188
        Indicator indicator = indicatorDAO.findById(indicatorId);
189
        if(indicator != null) {
190
            Section<String> section = checkForExceptions(stakeholderId, topicId, categoryId, subcategoryId, sectionId, indicator.getType());
191

    
192
            List<String> indicators = section.getIndicators();
193

    
194
            int index = indicators.indexOf(indicatorId);
195
            if (index != -1) {
196
                indicators.remove(index);
197
                sectionDAO.save(section);
198

    
199
                indicatorDAO.delete(indicatorId);
200
                log.debug("Indicator deleted!");
201
            } else {
202
                // EXCEPTION - Indicator not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); -> Category: category.getAlias(); -> SubCategory: subcategory.getAlias(); -> Section: section.getTitle();
203
                throw new PathNotValidException("Delete indicator: Indicator with id: "+indicatorId+" not found in Sectiom: "+sectionId);
204
            }
205
        } else {
206
            // EXCEPTION - Indicator not found
207
            throw new EntityNotFoundException("Delete indicator: Indicator with id: "+indicatorId+" not found");
208
        }
209
        return true;
210
    }
211

    
212
//    @RequestMapping(value = "/{stakeholderId}/charts/delete", method = RequestMethod.DELETE)
213
//    public boolean deleteAllChartIndicators(@PathVariable("stakeholderId") String stakeholderId) {
214
//        log.debug("delete all chart indicators of stakeholder");
215
//        log.debug("Stakeholder: "+stakeholderId);
216
//
217
//        Stakeholder<String> stakeholder = stakeholderDAO.findById(stakeholderId);
218
//        if(stakeholder != null) {
219
//
220
//            for(String topicId : stakeholder.getTopics()) {
221
//                Topic<String> topic = topicDAO.findById(topicId);
222
//                if(topic != null) {
223
//                    for(String categoryId : topic.getCategories()) {
224
//                        Category<String> category = categoryDAO.findById(categoryId);
225
//                        if(category != null) {
226
//                            for(String subcategoryId : category.getSubCategories()) {
227
//                                SubCategory<String> subcategory = subCategoryDAO.findById(subcategoryId);
228
//                                if(subcategory != null) {
229
//
230
//                                    for(String sectionId : subcategory.getCharts()) {
231
//                                        Section<String> section = sectionDAO.findById(sectionId);
232
//                                        if (section != null) {
233
//
234
//                                            List<String> indicators = section.getIndicators();
235
//                                            Iterator<String> indicatorsIterator = section.getIndicators().iterator();
236
//
237
//                                            while (indicatorsIterator.hasNext()) {
238
//                                                String indicatorId = indicatorsIterator.next();
239
//                                                Indicator indicator = indicatorDAO.findById(indicatorId);
240
//                                                if (indicator != null) {
241
//                                                    int index = indicators.indexOf(indicatorId);
242
//                                                    if (index != -1) {
243
//                                                        indicatorsIterator.remove();
244
//                                                        //indicators.remove(index);
245
//
246
//                                                        indicatorDAO.delete(indicatorId);
247
//                                                        log.debug("Indicator deleted!");
248
//                                                    } else {
249
//                                                        // EXCEPTION - Indicator not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); -> Category: category.getAlias(); -> SubCategory: subcategory.getAlias(); -> Section: section.getTitle();
250
//                                                        throw new PathNotValidException("Delete indicator: Indicator with id: " + indicatorId + " not found in Section: " + sectionId);
251
//                                                    }
252
//                                                } else {
253
//                                                    // EXCEPTION - Indicator not found
254
//                                                    throw new EntityNotFoundException("Delete indicator: Indicator with id: " + indicatorId + " not found");
255
//                                                }
256
//                                            }
257
//                                            sectionDAO.save(section);
258
//                                        } else {
259
//                                            // EXCEPTION - Section not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); -> Category: category.getAlias(); -> SubCategory: subcategory.getAlias();
260
//                                            throw new PathNotValidException("Delete indicator: Section with id: " + sectionId + " not found in SubCategory: " + subcategoryId);
261
//                                        }
262
//                                    }
263
//                                } else {
264
//                                    // EXCEPTION - SubCategory not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); -> Category: category.getAlias();
265
//                                    throw new PathNotValidException("Delete indicator: SubCategory with id: "+subcategoryId+" not found in Category: "+categoryId);
266
//                                }
267
//                            }
268
//                        } else {
269
//                            // EXCEPTION - Category not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias();
270
//                            throw new PathNotValidException("Delete indicator: Category with id: "+categoryId+" not found in Topic: "+topicId);
271
//                        }
272
//                    }
273
//                } else {
274
//                    // EXCEPTION - Topic not found in Stakeholder: stakeholder.getAlias();
275
//                    throw new PathNotValidException("Delete indicator: Topic with id: "+topicId+" not found in Stakeholder: "+stakeholderId);
276
//                }
277
//            }
278
//        } else {
279
//            // EXCEPTION - Stakeholder not found
280
//            throw new EntityNotFoundException("Delete indicator: Stakeholder with id: "+stakeholderId+" not found");
281
//        }
282
//        return true;
283
//    }
284

    
285
    @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/{sectionId}/{type}/reorder", method = RequestMethod.POST)
286
    public List<Indicator> reorderIndicators(@PathVariable("stakeholderId") String stakeholderId,
287
                                             @PathVariable("topicId") String topicId,
288
                                             @PathVariable("categoryId") String categoryId,
289
                                             @PathVariable("subcategoryId") String subcategoryId,
290
                                             @PathVariable("sectionId") String sectionId,
291
                                             @PathVariable("type") String type,
292
                                             @RequestBody List<String> indicators) {
293
        log.debug("reorder indicators of type: "+type);
294
        log.debug("Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId+ " - SubCategory: "+subcategoryId + " - Section: "+sectionId);
295

    
296
        Section<String> section = checkForExceptions(stakeholderId, topicId, categoryId, subcategoryId, sectionId, type);
297

    
298
        section.setIndicators(indicators);
299

    
300
        sectionDAO.save(section);
301
        log.debug("Indicators reordered!");
302

    
303
        List<Indicator> indicatorsFull = new ArrayList<>();
304
        for(String indicatorId : indicators) {
305
            indicatorsFull.add(indicatorDAO.findById(indicatorId));
306
        }
307
        return indicatorsFull;
308
    }
309

    
310
    @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/{sectionId}/{indicatorId}/toggle-status", method = RequestMethod.POST)
311
    public Boolean toggleIndicatorStatus(@PathVariable("stakeholderId") String stakeholderId,
312
                                         @PathVariable("topicId") String topicId,
313
                                         @PathVariable("categoryId") String categoryId,
314
                                         @PathVariable("subcategoryId") String subcategoryId,
315
                                         @PathVariable("sectionId") String sectionId,
316
                                         @PathVariable("indicatorId") String indicatorId) {
317
        log.debug("toggle indicator status (isActive)");
318
        log.debug("Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId+ " - SubCategory: "+subcategoryId + " - Section: "+sectionId+ " - Indicator: "+indicatorId);
319

    
320
        Indicator indicator = indicatorDAO.findById(indicatorId);
321
        if (indicator == null) {
322
            // EXCEPTION - Indicator not found
323
            throw new EntityNotFoundException("Toggle indicator status: Indicator with id: "+indicatorId+" not found");
324
        }
325
        indicator.setIsActive(!indicator.getIsActive());
326

    
327
        this.toggleIndicator(stakeholderId, topicId, categoryId, subcategoryId, sectionId, indicator);
328

    
329
        return indicator.getIsActive();
330
    }
331

    
332
    @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/{sectionId}/{indicatorId}/toggle-access", method = RequestMethod.POST)
333
    public Boolean toggleIndicatorAccess(@PathVariable("stakeholderId") String stakeholderId,
334
                                         @PathVariable("topicId") String topicId,
335
                                         @PathVariable("categoryId") String categoryId,
336
                                         @PathVariable("subcategoryId") String subcategoryId,
337
                                         @PathVariable("sectionId") String sectionId,
338
                                         @PathVariable("indicatorId") String indicatorId) {
339
        log.debug("toggle indicator access (isPublic)");
340
        log.debug("Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId+ " - SubCategory: "+subcategoryId + " - Section: "+sectionId+ " - Indicator: "+indicatorId);
341

    
342
        Indicator indicator = indicatorDAO.findById(indicatorId);
343
        if (indicator == null) {
344
            // EXCEPTION - Indicator not found
345
            throw new EntityNotFoundException("Toggle indicator access: Indicator with id: "+indicatorId+" not found");
346
        }
347
        indicator.setIsPublic(!indicator.getIsPublic());
348

    
349
        this.toggleIndicator(stakeholderId, topicId, categoryId, subcategoryId, sectionId, indicator);
350

    
351
        return indicator.getIsPublic();
352
    }
353

    
354
    public void toggleIndicator(String stakeholderId, String topicId, String categoryId, String subcategoryId, String sectionId, Indicator indicator) {
355
        Section<String> section = checkForExceptions(stakeholderId, topicId, categoryId, subcategoryId, sectionId, indicator.getType());
356
        List<String> indicators = section.getIndicators();
357

    
358
        if(indicators.contains(indicator.getId())) {
359
            indicatorDAO.save(indicator);
360
            log.debug("Indicator toggled!");
361
        } else {
362
            // EXCEPTION - Indicator not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); -> Category: category.getAlias(); -> SubCategory: subCategory.getAlias(); -> Section: section.getTitle();
363
            throw new PathNotValidException("Toggle indicators: Indicator with id: "+indicator.getId()+" not found in Section: "+sectionId);
364
        }
365

    
366
    }
367

    
368
    private Section checkForExceptions(String stakeholderId, String topicId, String categoryId, String subcategoryId, String sectionId, String indicatorType) {
369

    
370
        Stakeholder<String> stakeholder = stakeholderDAO.findById(stakeholderId);
371

    
372
        if(stakeholder == null) {
373
            // EXCEPTION - Stakeholder not found
374
            throw new EntityNotFoundException("Save indicator: Stakeholder with id: " + stakeholderId + " not found");
375
        }
376

    
377
        Topic<String> topic = topicDAO.findById(topicId);
378
        if(topic == null) {
379
            // EXCEPTION - Topic not found
380
            throw new EntityNotFoundException("Save indicator: Topic with id: "+topicId+" not found");
381
        }
382

    
383
        if(!stakeholder.getTopics().contains(topicId)) {
384
            // EXCEPTION - Topic not found in Stakeholder: stakeholder.getAlias();
385
            throw new PathNotValidException("Save indicator: Topic with id: " + topicId + " not found in Stakeholder: " + stakeholderId);
386
        }
387

    
388
        Category<String> category = categoryDAO.findById(categoryId);
389
        if(category == null) {
390
            // EXCEPTION - Category not found
391
            throw new EntityNotFoundException("Save indicator: Category with id: "+categoryId+" not found");
392
        }
393

    
394
        if(!topic.getCategories().contains(categoryId)) {
395
            // EXCEPTION - Category not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias();
396
            throw new PathNotValidException("Save indicator: Category with id: "+categoryId+" not found in Topic: "+topicId);
397
        }
398

    
399
        SubCategory<String> subcategory = subCategoryDAO.findById(subcategoryId);
400
        if(subcategory == null) {
401
            // EXCEPTION - SubCategory not found
402
            throw new EntityNotFoundException("Save indicator: SubCategory with id: "+subcategoryId+" not found");
403
        }
404

    
405
        if (!category.getSubCategories().contains(subcategoryId)) {
406
            // EXCEPTION - SubCategory not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); -> Category: category.getAlias();
407
            throw new PathNotValidException("Save indicator: SubCategory with id: "+subcategoryId+" not found in Category: "+categoryId);
408
        }
409

    
410
        Section<String> section = sectionDAO.findById(sectionId);
411
        if(section == null) {
412
            // EXCEPTION - Section not found
413
            throw new EntityNotFoundException("Save indicator: Section with id: "+sectionId+" not found");
414
        }
415

    
416
        if(indicatorType.equals("chart")) {
417
            if (!subcategory.getCharts().contains(sectionId)) {
418
                // EXCEPTION - Section not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); -> Category: category.getAlias(); -> SubCategory: subcategory.getAlias();
419
                throw new PathNotValidException("Save indicator: SubCategory with id: " + subcategoryId + " not found in Category: " + categoryId);
420
            }
421
        } else if(indicatorType.equals("number")) {
422
            if (!subcategory.getNumbers().contains(sectionId)) {
423
                // EXCEPTION - Section not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); -> Category: category.getAlias(); -> SubCategory: subcategory.getAlias();
424
                throw new PathNotValidException("Save indicator: SubCategory with id: " + subcategoryId + " not found in Category: " + categoryId);
425
            }
426
        }
427

    
428
        return  section;
429
    }
430
}
(3-3/8)