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.io.UnsupportedEncodingException;
13
import java.net.URLEncoder;
14
import java.util.ArrayList;
15
import java.util.HashMap;
16
import java.util.List;
17
import java.util.Map;
18

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

    
24
    @Autowired
25
    private StakeholderDAO stakeholderDAO;
26

    
27
    @Autowired
28
    private TopicDAO topicDAO;
29

    
30
    @Autowired
31
    private CategoryDAO categoryDAO;
32

    
33
    @Autowired
34
    private SubCategoryDAO subCategoryDAO;
35

    
36
    @Autowired
37
    private SectionDAO sectionDAO;
38

    
39
    @Autowired
40
    private IndicatorDAO indicatorDAO;
41

    
42

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

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

    
55
        Indicator oldIndicator = null;
56
        if(indicator.getId() != null) {
57
            oldIndicator = indicatorDAO.findById(indicator.getId());
58
        }
59

    
60
        String indicatorId = indicator.getId();
61
        indicatorDAO.save(indicator);
62

    
63
        Stakeholder<String> stakeholder = stakeholderDAO.findById(stakeholderId);
64
        // this indicator belongs in default profile and it is new or it is updated
65
        if(stakeholder.getDefaultId() == null) {
66
            if(indicatorId == null) {
67
                onSaveDefaultIndicator(indicator, sectionId);
68
            }
69
            else {
70
                onUpdateDefaultIndicator(indicator, stakeholder, oldIndicator);
71
            }
72
        }
73

    
74
        List<String> indicators = section.getIndicators();
75

    
76
        int index = indicators.indexOf(indicator.getId());
77
        if (index == -1) {
78
            indicators.add(indicator.getId());
79
            sectionDAO.save(section);
80
            log.debug("Indicator saved!");
81
        }
82

    
83
        return indicator;
84
    }
85

    
86
    public void onSaveDefaultIndicator(Indicator indicator, String defaultSectionId) throws UnsupportedEncodingException {
87
        log.debug("On save default indicator");
88

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

    
92
        for (Section section : sections) {
93
            Indicator indicatorNew = new Indicator();
94
            indicatorNew.copyFromDefault(indicator);
95
            for (IndicatorPath indicatorPath : indicatorNew.getIndicatorPaths()) {
96
                Stakeholder stakeholder = stakeholderDAO.findByAlias(section.getStakeholderAlias());
97
                parameterMapping(indicatorPath, stakeholder);
98
            }
99

    
100
            indicatorDAO.save(indicatorNew);
101

    
102
            List<String> indicators = section.getIndicators();
103
            indicators.add(indicatorNew.getId());
104

    
105
            sectionDAO.save(section);
106
        }
107
    }
108

    
109
    public void onUpdateDefaultIndicator(Indicator indicator, Stakeholder stakeholder, Indicator oldIndicator) throws UnsupportedEncodingException {
110
        log.debug("On update default indicator");
111

    
112
        // indicator already exists - check if changed and update all indicators based on it
113

    
114
        boolean changed = false;
115
        List<Indicator> indicators = indicatorDAO.findByDefaultId(indicator.getId());
116

    
117
        for(Indicator indicatorBasedOnDefault : indicators) {
118
            if(indicator.getName() != null && !indicator.getName().equals(indicatorBasedOnDefault.getName())
119
                    && (oldIndicator.getName() == null || oldIndicator.getName().equals(indicatorBasedOnDefault.getName()))) {
120

    
121
                indicatorBasedOnDefault.setName(indicator.getName());
122
                changed = true;
123
            }
124

    
125
            if(indicator.getDescription() != null && !indicator.getDescription().equals(indicatorBasedOnDefault.getDescription())
126
                    && (oldIndicator.getDescription() == null || oldIndicator.getDescription().equals(indicatorBasedOnDefault.getDescription()))) {
127

    
128
                indicatorBasedOnDefault.setName(indicator.getName());
129
                changed = true;
130
            }
131

    
132
            int i = 0;
133
            List<IndicatorPath> indicatorPaths = indicatorBasedOnDefault.getIndicatorPaths();
134

    
135
            for (IndicatorPath indicatorPath : indicator.getIndicatorPaths()) {
136
                IndicatorPath indicatorPathBasedOnDefault = indicatorBasedOnDefault.getIndicatorPaths().get(i);
137

    
138
                if(indicatorPathBasedOnDefault == null) {
139
                    // Add new indicator path in existing indicators
140
                    IndicatorPath indicatorPathNew = new IndicatorPath(indicatorPath);
141
                    parameterMapping(indicatorPathNew, stakeholder);
142
                    indicatorPaths.add(indicatorPathNew);
143
                    changed = true;
144
                } else {
145
                    IndicatorPath oldIndicatorPath = oldIndicator.getIndicatorPaths().get(i);
146

    
147
                    // Check if there are changes in indicator path and update existing indicators if needed
148
                    log.debug("update indicator path: "+i);
149
                    if(!indicatorPath.getType().equals(indicatorPathBasedOnDefault.getType())
150
                            && (oldIndicatorPath.getType().equals(indicatorPathBasedOnDefault.getType()))) {
151

    
152
                        indicatorPathBasedOnDefault.setType(indicatorPath.getType());
153
                        changed = true; // parameter "type" needs to be changed as well
154
                    }
155
                    log.debug("After type check: "+changed);
156

    
157
                    if(!indicatorPath.getSource().equals(indicatorPathBasedOnDefault.getSource())
158
                            && (oldIndicatorPath.getSource().equals(indicatorPathBasedOnDefault.getSource()))) {
159

    
160
                        indicatorPathBasedOnDefault.setSource(indicatorPath.getSource());
161
                        changed = true;
162
                    }
163
                    log.debug("After source check: "+changed);
164

    
165
                    if(!indicatorPath.getUrl().equals(indicatorPathBasedOnDefault.getUrl())
166
                            && (oldIndicatorPath.getUrl().equals(indicatorPathBasedOnDefault.getUrl()))) {
167

    
168
                        indicatorPathBasedOnDefault.setUrl(indicatorPath.getUrl());
169
                        changed = true;
170
                    }
171
                    log.debug("After url check: "+changed);
172

    
173
                    if(!indicatorPath.getChartObject().equals(indicatorPathBasedOnDefault.getChartObject())
174
                            && (oldIndicatorPath.getChartObject().equals(indicatorPathBasedOnDefault.getChartObject()))) {
175

    
176
                        indicatorPathBasedOnDefault.setChartObject(indicatorPath.getChartObject());
177
                        changed = true;
178
                    }
179
                    log.debug("After chartObject check: "+changed);
180

    
181
                    if(indicatorPath.getParameters() != null) {
182
                        if (indicatorPathBasedOnDefault.getParameters() == null) {
183
                            indicatorPathBasedOnDefault.setParameters(new HashMap<>());
184
                        }
185
                        //if (indicatorPath.getParameters().size() != indicatorPathBasedOnDefault.getParameters().size()) {
186
                            //log.debug("Different number of parameters");
187
                            for (Map.Entry<String, String> parameter : indicatorPath.getParameters().entrySet()) {
188
                                log.debug("\nindicatorPath: parameter.getKey(): "+parameter.getKey()+" - value: "+parameter.getValue()
189
                                        +"\nindicatorPathBasedOnDefault:parameters:key: "+  indicatorPathBasedOnDefault.getParameters().get(parameter.getKey())
190
                                        +"\noldIndicatorPath:parameters:key: "+  oldIndicatorPath.getParameters().get(parameter.getKey()));
191
                                if (!indicatorPathBasedOnDefault.getParameters().containsKey(parameter.getKey())
192
                                        || (oldIndicatorPath.getParameters() == null
193
                                            || (oldIndicatorPath.getParameters().get(parameter.getKey()).equals(indicatorPathBasedOnDefault.getParameters().get(parameter.getKey()))
194
                                                && !parameter.getValue().equals(indicatorPathBasedOnDefault.getParameters().get(parameter.getKey()))))
195
                                ) {
196
                                    indicatorPathBasedOnDefault.getParameters().put(parameter.getKey(), parameter.getValue());
197
                                    changed = true;
198
                                }
199
//                                else if(parameter.getKey().equals("type")) {
200
//                                    indicatorPathBasedOnDefault.getParameters().put(parameter.getKey(), parameter.getValue());
201
//                                    changed = true;
202
//                                }
203
                            }
204
                            parameterMapping(indicatorPathBasedOnDefault, stakeholder);
205
                        //}
206
                        log.debug("After parameters check: " + changed);
207
                    }
208

    
209
                    if(indicatorPath.getJsonPath() != null) {
210
                        int j = 0;
211
                        for (String jsonString : indicatorPath.getJsonPath()) {
212
                            log.debug("indicatorPath.getJsonPath(): " + jsonString);
213
                            String jsonStringBasedOnDefault = null;
214
                            if(indicatorPathBasedOnDefault.getJsonPath() != null ) {
215
                                jsonStringBasedOnDefault = indicatorPathBasedOnDefault.getJsonPath().get(j);
216
                            } else {
217
                                indicatorPathBasedOnDefault.setJsonPath(new ArrayList<>());
218
                            }
219
                            log.debug("indicatorPathBasedOnDefault.getJsonPath().get(" + j + "): " + jsonStringBasedOnDefault);
220

    
221
                            if (!jsonString.equals(jsonStringBasedOnDefault)
222
                                    && (oldIndicatorPath.getJsonPath() == null
223
                                    || oldIndicatorPath.getJsonPath().get(i).equals(jsonStringBasedOnDefault))
224
                            ) {
225
                                indicatorPathBasedOnDefault.getJsonPath().set(j, jsonString);
226
                                changed = true;
227
                            }
228
                            j++;
229
                        }
230
                        log.debug("After jsonPath check: " + changed);
231
                    }
232
                }
233
                i++;
234
            }
235

    
236
            if(!changed) {
237
//                break;
238
                continue;
239
            }
240

    
241
            indicatorDAO.save(indicatorBasedOnDefault);
242
        }
243
    }
244

    
245
    public void parameterMapping(IndicatorPath indicatorPath, Stakeholder stakeholder) throws UnsupportedEncodingException {
246
        if (indicatorPath.getParameters().containsKey("index_name")) {
247
            indicatorPath.getParameters().put("index_name", stakeholder.getIndex_name());
248
        } else if (indicatorPath.getParameters().containsKey("index_shortName")) {
249
            indicatorPath.getParameters().put("index_shortName", stakeholder.getIndex_name().toLowerCase());
250
        } else if (indicatorPath.getParameters().containsKey("index_id")) {
251
            indicatorPath.getParameters().put("index_id", stakeholder.getIndex_id());
252
        }
253

    
254

    
255
        String url = indicatorPath.getUrl();
256
        String encoded_index_id = urlEncode(URLEncoder.encode(stakeholder.getIndex_id(), "UTF-8"));
257
        url = url.replace("index_id", encoded_index_id);
258
        String encoded_index_name = urlEncode(URLEncoder.encode(stakeholder.getIndex_name(), "UTF-8"));
259
        url = url.replace("index_name", encoded_index_name);
260
        String encoded_index_shortName = urlEncode(URLEncoder.encode(stakeholder.getIndex_shortName(), "UTF-8"));
261
        url = url.replace("index_shortName", encoded_index_shortName);
262
        indicatorPath.setUrl(url);
263
    }
264

    
265
    public String urlEncode(String encodedIndicatorPathField) {
266
        String indicatorPathField = "";
267

    
268
        for( int i=0; i<encodedIndicatorPathField.length(); i++ ){
269
            String character = encodedIndicatorPathField.substring(i, i+1);
270

    
271
            if(character.equals("+")) {
272
                indicatorPathField = indicatorPathField.concat("%20");
273
            } else if(character.equals("%")) {
274
                //grab the hex in pairs
275
                String output = encodedIndicatorPathField.substring(i+1, (i + 3));
276

    
277
                if(output.equals("7E") || output.equals("27") || output.equals("28") || output.equals("29") || output.equals("21")) {
278
                    //convert hex to decimal
279
                    int decimal = Integer.parseInt(output, 16);
280
                    //convert the decimal to character
281
                    StringBuilder sb = new StringBuilder();
282
                    sb.append((char) decimal);
283

    
284
                    indicatorPathField = indicatorPathField.concat(sb.toString());
285
                } else {
286
                    indicatorPathField = indicatorPathField.concat(character + output);
287
                }
288

    
289
                i += 2;
290
            } else {
291
                indicatorPathField = indicatorPathField.concat(character);
292
            }
293
        }
294

    
295
        return indicatorPathField;
296
    }
297

    
298
    @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/{sectionId}/{indicatorId}/delete", method = RequestMethod.DELETE)
299
    public boolean deleteIndicator(@PathVariable("stakeholderId") String stakeholderId,
300
                                   @PathVariable("topicId") String topicId,
301
                                   @PathVariable("categoryId") String categoryId,
302
                                   @PathVariable("subcategoryId") String subcategoryId,
303
                                   @PathVariable("sectionId") String sectionId,
304
                                   @PathVariable("indicatorId") String indicatorId) {
305
        log.debug("delete indicator");
306
        log.debug("Id: "+indicatorId + " - Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId+ " - SubCategory: "+subcategoryId + " - Section: "+sectionId);
307

    
308
        Indicator indicator = indicatorDAO.findById(indicatorId);
309
        if(indicator != null) {
310
            Section<String> section = checkForExceptions(stakeholderId, topicId, categoryId, subcategoryId, sectionId, indicator.getType());
311

    
312
            List<String> indicators = section.getIndicators();
313

    
314
            int index = indicators.indexOf(indicatorId);
315
            if (index != -1) {
316
                indicators.remove(index);
317
                sectionDAO.save(section);
318

    
319
                indicatorDAO.delete(indicatorId);
320
                log.debug("Indicator deleted!");
321
            } else {
322
                // EXCEPTION - Indicator not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); -> Category: category.getAlias(); -> SubCategory: subcategory.getAlias(); -> Section: section.getTitle();
323
                throw new PathNotValidException("Delete indicator: Indicator with id: "+indicatorId+" not found in Sectiom: "+sectionId);
324
            }
325
        } else {
326
            // EXCEPTION - Indicator not found
327
            throw new EntityNotFoundException("Delete indicator: Indicator with id: "+indicatorId+" not found");
328
        }
329
        return true;
330
    }
331

    
332
//    @RequestMapping(value = "/{stakeholderId}/charts/delete", method = RequestMethod.DELETE)
333
//    public boolean deleteAllChartIndicators(@PathVariable("stakeholderId") String stakeholderId) {
334
//        log.debug("delete all chart indicators of stakeholder");
335
//        log.debug("Stakeholder: "+stakeholderId);
336
//
337
//        Stakeholder<String> stakeholder = stakeholderDAO.findById(stakeholderId);
338
//        if(stakeholder != null) {
339
//
340
//            for(String topicId : stakeholder.getTopics()) {
341
//                Topic<String> topic = topicDAO.findById(topicId);
342
//                if(topic != null) {
343
//                    for(String categoryId : topic.getCategories()) {
344
//                        Category<String> category = categoryDAO.findById(categoryId);
345
//                        if(category != null) {
346
//                            for(String subcategoryId : category.getSubCategories()) {
347
//                                SubCategory<String> subcategory = subCategoryDAO.findById(subcategoryId);
348
//                                if(subcategory != null) {
349
//
350
//                                    for(String sectionId : subcategory.getCharts()) {
351
//                                        Section<String> section = sectionDAO.findById(sectionId);
352
//                                        if (section != null) {
353
//
354
//                                            List<String> indicators = section.getIndicators();
355
//                                            Iterator<String> indicatorsIterator = section.getIndicators().iterator();
356
//
357
//                                            while (indicatorsIterator.hasNext()) {
358
//                                                String indicatorId = indicatorsIterator.next();
359
//                                                Indicator indicator = indicatorDAO.findById(indicatorId);
360
//                                                if (indicator != null) {
361
//                                                    int index = indicators.indexOf(indicatorId);
362
//                                                    if (index != -1) {
363
//                                                        indicatorsIterator.remove();
364
//                                                        //indicators.remove(index);
365
//
366
//                                                        indicatorDAO.delete(indicatorId);
367
//                                                        log.debug("Indicator deleted!");
368
//                                                    } else {
369
//                                                        // EXCEPTION - Indicator not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); -> Category: category.getAlias(); -> SubCategory: subcategory.getAlias(); -> Section: section.getTitle();
370
//                                                        throw new PathNotValidException("Delete indicator: Indicator with id: " + indicatorId + " not found in Section: " + sectionId);
371
//                                                    }
372
//                                                } else {
373
//                                                    // EXCEPTION - Indicator not found
374
//                                                    throw new EntityNotFoundException("Delete indicator: Indicator with id: " + indicatorId + " not found");
375
//                                                }
376
//                                            }
377
//                                            sectionDAO.save(section);
378
//                                        } else {
379
//                                            // EXCEPTION - Section not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); -> Category: category.getAlias(); -> SubCategory: subcategory.getAlias();
380
//                                            throw new PathNotValidException("Delete indicator: Section with id: " + sectionId + " not found in SubCategory: " + subcategoryId);
381
//                                        }
382
//                                    }
383
//                                } else {
384
//                                    // EXCEPTION - SubCategory not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); -> Category: category.getAlias();
385
//                                    throw new PathNotValidException("Delete indicator: SubCategory with id: "+subcategoryId+" not found in Category: "+categoryId);
386
//                                }
387
//                            }
388
//                        } else {
389
//                            // EXCEPTION - Category not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias();
390
//                            throw new PathNotValidException("Delete indicator: Category with id: "+categoryId+" not found in Topic: "+topicId);
391
//                        }
392
//                    }
393
//                } else {
394
//                    // EXCEPTION - Topic not found in Stakeholder: stakeholder.getAlias();
395
//                    throw new PathNotValidException("Delete indicator: Topic with id: "+topicId+" not found in Stakeholder: "+stakeholderId);
396
//                }
397
//            }
398
//        } else {
399
//            // EXCEPTION - Stakeholder not found
400
//            throw new EntityNotFoundException("Delete indicator: Stakeholder with id: "+stakeholderId+" not found");
401
//        }
402
//        return true;
403
//    }
404

    
405
    @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/{sectionId}/{type}/reorder", method = RequestMethod.POST)
406
    public List<Indicator> reorderIndicators(@PathVariable("stakeholderId") String stakeholderId,
407
                                             @PathVariable("topicId") String topicId,
408
                                             @PathVariable("categoryId") String categoryId,
409
                                             @PathVariable("subcategoryId") String subcategoryId,
410
                                             @PathVariable("sectionId") String sectionId,
411
                                             @PathVariable("type") String type,
412
                                             @RequestBody List<String> indicators) {
413
        log.debug("reorder indicators of type: "+type);
414
        log.debug("Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId+ " - SubCategory: "+subcategoryId + " - Section: "+sectionId);
415

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

    
418
        section.setIndicators(indicators);
419

    
420
        sectionDAO.save(section);
421
        log.debug("Indicators reordered!");
422

    
423
        List<Indicator> indicatorsFull = new ArrayList<>();
424
        for(String indicatorId : indicators) {
425
            indicatorsFull.add(indicatorDAO.findById(indicatorId));
426
        }
427
        return indicatorsFull;
428
    }
429

    
430
    @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/{sectionId}/{indicatorId}/toggle-status", method = RequestMethod.POST)
431
    public Boolean toggleIndicatorStatus(@PathVariable("stakeholderId") String stakeholderId,
432
                                         @PathVariable("topicId") String topicId,
433
                                         @PathVariable("categoryId") String categoryId,
434
                                         @PathVariable("subcategoryId") String subcategoryId,
435
                                         @PathVariable("sectionId") String sectionId,
436
                                         @PathVariable("indicatorId") String indicatorId) {
437
        log.debug("toggle indicator status (isActive)");
438
        log.debug("Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId+ " - SubCategory: "+subcategoryId + " - Section: "+sectionId+ " - Indicator: "+indicatorId);
439

    
440
        Indicator indicator = indicatorDAO.findById(indicatorId);
441
        if (indicator == null) {
442
            // EXCEPTION - Indicator not found
443
            throw new EntityNotFoundException("Toggle indicator status: Indicator with id: "+indicatorId+" not found");
444
        }
445
        indicator.setIsActive(!indicator.getIsActive());
446

    
447
        this.toggleIndicator(stakeholderId, topicId, categoryId, subcategoryId, sectionId, indicator);
448

    
449
        return indicator.getIsActive();
450
    }
451

    
452
    @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/{sectionId}/{indicatorId}/toggle-access", method = RequestMethod.POST)
453
    public Boolean toggleIndicatorAccess(@PathVariable("stakeholderId") String stakeholderId,
454
                                         @PathVariable("topicId") String topicId,
455
                                         @PathVariable("categoryId") String categoryId,
456
                                         @PathVariable("subcategoryId") String subcategoryId,
457
                                         @PathVariable("sectionId") String sectionId,
458
                                         @PathVariable("indicatorId") String indicatorId) {
459
        log.debug("toggle indicator access (isPublic)");
460
        log.debug("Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId+ " - SubCategory: "+subcategoryId + " - Section: "+sectionId+ " - Indicator: "+indicatorId);
461

    
462
        Indicator indicator = indicatorDAO.findById(indicatorId);
463
        if (indicator == null) {
464
            // EXCEPTION - Indicator not found
465
            throw new EntityNotFoundException("Toggle indicator access: Indicator with id: "+indicatorId+" not found");
466
        }
467
        indicator.setIsPublic(!indicator.getIsPublic());
468

    
469
        this.toggleIndicator(stakeholderId, topicId, categoryId, subcategoryId, sectionId, indicator);
470

    
471
        return indicator.getIsPublic();
472
    }
473

    
474
    public void toggleIndicator(String stakeholderId, String topicId, String categoryId, String subcategoryId, String sectionId, Indicator indicator) {
475
        Section<String> section = checkForExceptions(stakeholderId, topicId, categoryId, subcategoryId, sectionId, indicator.getType());
476
        List<String> indicators = section.getIndicators();
477

    
478
        if(indicators.contains(indicator.getId())) {
479
            indicatorDAO.save(indicator);
480
            log.debug("Indicator toggled!");
481
        } else {
482
            // EXCEPTION - Indicator not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); -> Category: category.getAlias(); -> SubCategory: subCategory.getAlias(); -> Section: section.getTitle();
483
            throw new PathNotValidException("Toggle indicators: Indicator with id: "+indicator.getId()+" not found in Section: "+sectionId);
484
        }
485

    
486
    }
487

    
488
    private Section checkForExceptions(String stakeholderId, String topicId, String categoryId, String subcategoryId, String sectionId, String indicatorType) {
489

    
490
        Stakeholder<String> stakeholder = stakeholderDAO.findById(stakeholderId);
491

    
492
        if(stakeholder == null) {
493
            // EXCEPTION - Stakeholder not found
494
            throw new EntityNotFoundException("Save indicator: Stakeholder with id: " + stakeholderId + " not found");
495
        }
496

    
497
        Topic<String> topic = topicDAO.findById(topicId);
498
        if(topic == null) {
499
            // EXCEPTION - Topic not found
500
            throw new EntityNotFoundException("Save indicator: Topic with id: "+topicId+" not found");
501
        }
502

    
503
        if(!stakeholder.getTopics().contains(topicId)) {
504
            // EXCEPTION - Topic not found in Stakeholder: stakeholder.getAlias();
505
            throw new PathNotValidException("Save indicator: Topic with id: " + topicId + " not found in Stakeholder: " + stakeholderId);
506
        }
507

    
508
        Category<String> category = categoryDAO.findById(categoryId);
509
        if(category == null) {
510
            // EXCEPTION - Category not found
511
            throw new EntityNotFoundException("Save indicator: Category with id: "+categoryId+" not found");
512
        }
513

    
514
        if(!topic.getCategories().contains(categoryId)) {
515
            // EXCEPTION - Category not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias();
516
            throw new PathNotValidException("Save indicator: Category with id: "+categoryId+" not found in Topic: "+topicId);
517
        }
518

    
519
        SubCategory<String> subcategory = subCategoryDAO.findById(subcategoryId);
520
        if(subcategory == null) {
521
            // EXCEPTION - SubCategory not found
522
            throw new EntityNotFoundException("Save indicator: SubCategory with id: "+subcategoryId+" not found");
523
        }
524

    
525
        if (!category.getSubCategories().contains(subcategoryId)) {
526
            // EXCEPTION - SubCategory not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); -> Category: category.getAlias();
527
            throw new PathNotValidException("Save indicator: SubCategory with id: "+subcategoryId+" not found in Category: "+categoryId);
528
        }
529

    
530
        Section<String> section = sectionDAO.findById(sectionId);
531
        if(section == null) {
532
            // EXCEPTION - Section not found
533
            throw new EntityNotFoundException("Save indicator: Section with id: "+sectionId+" not found");
534
        }
535

    
536
        if(indicatorType.equals("chart")) {
537
            if (!subcategory.getCharts().contains(sectionId)) {
538
                // EXCEPTION - Section not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); -> Category: category.getAlias(); -> SubCategory: subcategory.getAlias();
539
                throw new PathNotValidException("Save indicator: SubCategory with id: " + subcategoryId + " not found in Category: " + categoryId);
540
            }
541
        } else if(indicatorType.equals("number")) {
542
            if (!subcategory.getNumbers().contains(sectionId)) {
543
                // EXCEPTION - Section not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); -> Category: category.getAlias(); -> SubCategory: subcategory.getAlias();
544
                throw new PathNotValidException("Save indicator: SubCategory with id: " + subcategoryId + " not found in Category: " + categoryId);
545
            }
546
        }
547

    
548
        return  section;
549
    }
550
}
(3-3/8)