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
                    log.debug("indicatorPath.getType(): "+indicatorPath.getType());
150
                    log.debug("indicatorPathBasedOnDefault.getType(): "+indicatorPathBasedOnDefault.getType());
151
                    log.debug("oldIndicatorPath.getType(): "+oldIndicatorPath.getType());
152

    
153
                    if(indicatorPath.getType() != null
154
                            && !indicatorPath.getType().equals(indicatorPathBasedOnDefault.getType())
155
                            && (oldIndicatorPath.getType().equals(indicatorPathBasedOnDefault.getType()))) {
156

    
157
                        indicatorPathBasedOnDefault.setType(indicatorPath.getType());
158
                        changed = true; // parameter "type" needs to be changed as well
159
                    }
160
                    log.debug("After type check: "+changed);
161

    
162
                    if(indicatorPath.getSource() != null
163
                            && !indicatorPath.getSource().equals(indicatorPathBasedOnDefault.getSource())
164
                            && (oldIndicatorPath.getSource().equals(indicatorPathBasedOnDefault.getSource()))) {
165

    
166
                        indicatorPathBasedOnDefault.setSource(indicatorPath.getSource());
167
                        changed = true;
168
                    }
169
                    log.debug("After source check: "+changed);
170

    
171
                    if(indicatorPath.getUrl() != null
172
                            && !indicatorPath.getUrl().equals(indicatorPathBasedOnDefault.getUrl())
173
                            && (oldIndicatorPath.getUrl().equals(indicatorPathBasedOnDefault.getUrl()))) {
174

    
175
                        indicatorPathBasedOnDefault.setUrl(indicatorPath.getUrl());
176
                        changed = true;
177
                    }
178
                    log.debug("After url check: "+changed);
179

    
180
                    if(indicatorPath.getChartObject() != null
181
                            && !indicatorPath.getChartObject().equals(indicatorPathBasedOnDefault.getChartObject())
182
                            && (oldIndicatorPath.getChartObject().equals(indicatorPathBasedOnDefault.getChartObject()))) {
183

    
184
                        indicatorPathBasedOnDefault.setChartObject(indicatorPath.getChartObject());
185
                        changed = true;
186
                    }
187
                    log.debug("After chartObject check: "+changed);
188

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

    
217
                    if(indicatorPath.getJsonPath() != null) {
218
                        int j = 0;
219
                        for (String jsonString : indicatorPath.getJsonPath()) {
220
                            log.debug("indicatorPath.getJsonPath(): " + jsonString);
221
                            String jsonStringBasedOnDefault = null;
222
                            if(indicatorPathBasedOnDefault.getJsonPath() != null ) {
223
                                jsonStringBasedOnDefault = indicatorPathBasedOnDefault.getJsonPath().get(j);
224
                            } else {
225
                                indicatorPathBasedOnDefault.setJsonPath(new ArrayList<>());
226
                            }
227
                            log.debug("indicatorPathBasedOnDefault.getJsonPath().get(" + j + "): " + jsonStringBasedOnDefault);
228

    
229
                            if (!jsonString.equals(jsonStringBasedOnDefault)
230
                                    && (oldIndicatorPath.getJsonPath() == null
231
                                    || oldIndicatorPath.getJsonPath().get(i).equals(jsonStringBasedOnDefault))
232
                            ) {
233
                                indicatorPathBasedOnDefault.getJsonPath().set(j, jsonString);
234
                                changed = true;
235
                            }
236
                            j++;
237
                        }
238
                        log.debug("After jsonPath check: " + changed);
239
                    }
240
                }
241
                i++;
242
            }
243

    
244
            if(!changed) {
245
//                break;
246
                continue;
247
            }
248

    
249
            indicatorDAO.save(indicatorBasedOnDefault);
250
        }
251
    }
252

    
253
    public void parameterMapping(IndicatorPath indicatorPath, Stakeholder stakeholder) throws UnsupportedEncodingException {
254
        if (indicatorPath.getParameters() != null) {
255
            if (indicatorPath.getParameters().containsKey("index_name")) {
256
                indicatorPath.getParameters().put("index_name", stakeholder.getIndex_name());
257
            } else if (indicatorPath.getParameters().containsKey("index_shortName")) {
258
                indicatorPath.getParameters().put("index_shortName", stakeholder.getIndex_name().toLowerCase());
259
            } else if (indicatorPath.getParameters().containsKey("index_id")) {
260
                indicatorPath.getParameters().put("index_id", stakeholder.getIndex_id());
261
            }
262
        }
263

    
264
//        // url encoding for number indicators
265
//        String url = indicatorPath.getUrl();
266
//        String encoded_index_id = urlEncode(URLEncoder.encode(stakeholder.getIndex_id(), "UTF-8"));
267
//        url = url.replace("index_id", encoded_index_id);
268
//        String encoded_index_name = urlEncode(URLEncoder.encode(stakeholder.getIndex_name(), "UTF-8"));
269
//        url = url.replace("index_name", encoded_index_name);
270
//        String encoded_index_shortName = urlEncode(URLEncoder.encode(stakeholder.getIndex_shortName(), "UTF-8"));
271
//        url = url.replace("index_shortName", encoded_index_shortName);
272
//        indicatorPath.setUrl(url);
273
    }
274

    
275
    public String urlEncode(String encodedIndicatorPathField) {
276
        String indicatorPathField = "";
277

    
278
        for( int i=0; i<encodedIndicatorPathField.length(); i++ ){
279
            String character = encodedIndicatorPathField.substring(i, i+1);
280

    
281
            if(character.equals("+")) {
282
                indicatorPathField = indicatorPathField.concat("%20");
283
            } else if(character.equals("%")) {
284
                //grab the hex in pairs
285
                String output = encodedIndicatorPathField.substring(i+1, (i + 3));
286

    
287
                if(output.equals("7E") || output.equals("27") || output.equals("28") || output.equals("29") || output.equals("21")) {
288
                    //convert hex to decimal
289
                    int decimal = Integer.parseInt(output, 16);
290
                    //convert the decimal to character
291
                    StringBuilder sb = new StringBuilder();
292
                    sb.append((char) decimal);
293

    
294
                    indicatorPathField = indicatorPathField.concat(sb.toString());
295
                } else {
296
                    indicatorPathField = indicatorPathField.concat(character + output);
297
                }
298

    
299
                i += 2;
300
            } else {
301
                indicatorPathField = indicatorPathField.concat(character);
302
            }
303
        }
304

    
305
        return indicatorPathField;
306
    }
307

    
308
    @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/{sectionId}/{indicatorId}/delete", method = RequestMethod.DELETE)
309
    public boolean deleteIndicator(@PathVariable("stakeholderId") String stakeholderId,
310
                                   @PathVariable("topicId") String topicId,
311
                                   @PathVariable("categoryId") String categoryId,
312
                                   @PathVariable("subcategoryId") String subcategoryId,
313
                                   @PathVariable("sectionId") String sectionId,
314
                                   @PathVariable("indicatorId") String indicatorId) {
315
        log.debug("delete indicator");
316
        log.debug("Id: "+indicatorId + " - Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId+ " - SubCategory: "+subcategoryId + " - Section: "+sectionId);
317

    
318
        Indicator indicator = indicatorDAO.findById(indicatorId);
319
        if(indicator != null) {
320
            Section<String> section = checkForExceptions(stakeholderId, topicId, categoryId, subcategoryId, sectionId, indicator.getType());
321

    
322
            List<String> indicators = section.getIndicators();
323

    
324
            int index = indicators.indexOf(indicatorId);
325
            if (index != -1) {
326
                indicators.remove(index);
327
                sectionDAO.save(section);
328

    
329
                indicatorDAO.delete(indicatorId);
330
                log.debug("Indicator deleted!");
331
            } else {
332
                // EXCEPTION - Indicator not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); -> Category: category.getAlias(); -> SubCategory: subcategory.getAlias(); -> Section: section.getTitle();
333
                throw new PathNotValidException("Delete indicator: Indicator with id: "+indicatorId+" not found in Sectiom: "+sectionId);
334
            }
335
        } else {
336
            // EXCEPTION - Indicator not found
337
            throw new EntityNotFoundException("Delete indicator: Indicator with id: "+indicatorId+" not found");
338
        }
339
        return true;
340
    }
341

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

    
415
    @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/{sectionId}/{type}/reorder", method = RequestMethod.POST)
416
    public List<Indicator> reorderIndicators(@PathVariable("stakeholderId") String stakeholderId,
417
                                             @PathVariable("topicId") String topicId,
418
                                             @PathVariable("categoryId") String categoryId,
419
                                             @PathVariable("subcategoryId") String subcategoryId,
420
                                             @PathVariable("sectionId") String sectionId,
421
                                             @PathVariable("type") String type,
422
                                             @RequestBody List<String> indicators) {
423
        log.debug("reorder indicators of type: "+type);
424
        log.debug("Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId+ " - SubCategory: "+subcategoryId + " - Section: "+sectionId);
425

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

    
428
        section.setIndicators(indicators);
429

    
430
        sectionDAO.save(section);
431
        log.debug("Indicators reordered!");
432

    
433
        List<Indicator> indicatorsFull = new ArrayList<>();
434
        for(String indicatorId : indicators) {
435
            indicatorsFull.add(indicatorDAO.findById(indicatorId));
436
        }
437
        return indicatorsFull;
438
    }
439

    
440
    @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/{sectionId}/{indicatorId}/toggle-status", method = RequestMethod.POST)
441
    public Boolean toggleIndicatorStatus(@PathVariable("stakeholderId") String stakeholderId,
442
                                         @PathVariable("topicId") String topicId,
443
                                         @PathVariable("categoryId") String categoryId,
444
                                         @PathVariable("subcategoryId") String subcategoryId,
445
                                         @PathVariable("sectionId") String sectionId,
446
                                         @PathVariable("indicatorId") String indicatorId) {
447
        log.debug("toggle indicator status (isActive)");
448
        log.debug("Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId+ " - SubCategory: "+subcategoryId + " - Section: "+sectionId+ " - Indicator: "+indicatorId);
449

    
450
        Indicator indicator = indicatorDAO.findById(indicatorId);
451
        if (indicator == null) {
452
            // EXCEPTION - Indicator not found
453
            throw new EntityNotFoundException("Toggle indicator status: Indicator with id: "+indicatorId+" not found");
454
        }
455
        indicator.setIsActive(!indicator.getIsActive());
456

    
457
        this.toggleIndicator(stakeholderId, topicId, categoryId, subcategoryId, sectionId, indicator);
458

    
459
        return indicator.getIsActive();
460
    }
461

    
462
    @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/{sectionId}/{indicatorId}/toggle-access", method = RequestMethod.POST)
463
    public Boolean toggleIndicatorAccess(@PathVariable("stakeholderId") String stakeholderId,
464
                                         @PathVariable("topicId") String topicId,
465
                                         @PathVariable("categoryId") String categoryId,
466
                                         @PathVariable("subcategoryId") String subcategoryId,
467
                                         @PathVariable("sectionId") String sectionId,
468
                                         @PathVariable("indicatorId") String indicatorId) {
469
        log.debug("toggle indicator access (isPublic)");
470
        log.debug("Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId+ " - SubCategory: "+subcategoryId + " - Section: "+sectionId+ " - Indicator: "+indicatorId);
471

    
472
        Indicator indicator = indicatorDAO.findById(indicatorId);
473
        if (indicator == null) {
474
            // EXCEPTION - Indicator not found
475
            throw new EntityNotFoundException("Toggle indicator access: Indicator with id: "+indicatorId+" not found");
476
        }
477
        indicator.setIsPublic(!indicator.getIsPublic());
478

    
479
        this.toggleIndicator(stakeholderId, topicId, categoryId, subcategoryId, sectionId, indicator);
480

    
481
        return indicator.getIsPublic();
482
    }
483

    
484
    public void toggleIndicator(String stakeholderId, String topicId, String categoryId, String subcategoryId, String sectionId, Indicator indicator) {
485
        Section<String> section = checkForExceptions(stakeholderId, topicId, categoryId, subcategoryId, sectionId, indicator.getType());
486
        List<String> indicators = section.getIndicators();
487

    
488
        if(indicators.contains(indicator.getId())) {
489
            indicatorDAO.save(indicator);
490
            log.debug("Indicator toggled!");
491
        } else {
492
            // EXCEPTION - Indicator not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); -> Category: category.getAlias(); -> SubCategory: subCategory.getAlias(); -> Section: section.getTitle();
493
            throw new PathNotValidException("Toggle indicators: Indicator with id: "+indicator.getId()+" not found in Section: "+sectionId);
494
        }
495

    
496
    }
497

    
498
    private Section checkForExceptions(String stakeholderId, String topicId, String categoryId, String subcategoryId, String sectionId, String indicatorType) {
499

    
500
        Stakeholder<String> stakeholder = stakeholderDAO.findById(stakeholderId);
501

    
502
        if(stakeholder == null) {
503
            // EXCEPTION - Stakeholder not found
504
            throw new EntityNotFoundException("Save indicator: Stakeholder with id: " + stakeholderId + " not found");
505
        }
506

    
507
        Topic<String> topic = topicDAO.findById(topicId);
508
        if(topic == null) {
509
            // EXCEPTION - Topic not found
510
            throw new EntityNotFoundException("Save indicator: Topic with id: "+topicId+" not found");
511
        }
512

    
513
        if(!stakeholder.getTopics().contains(topicId)) {
514
            // EXCEPTION - Topic not found in Stakeholder: stakeholder.getAlias();
515
            throw new PathNotValidException("Save indicator: Topic with id: " + topicId + " not found in Stakeholder: " + stakeholderId);
516
        }
517

    
518
        Category<String> category = categoryDAO.findById(categoryId);
519
        if(category == null) {
520
            // EXCEPTION - Category not found
521
            throw new EntityNotFoundException("Save indicator: Category with id: "+categoryId+" not found");
522
        }
523

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

    
529
        SubCategory<String> subcategory = subCategoryDAO.findById(subcategoryId);
530
        if(subcategory == null) {
531
            // EXCEPTION - SubCategory not found
532
            throw new EntityNotFoundException("Save indicator: SubCategory with id: "+subcategoryId+" not found");
533
        }
534

    
535
        if (!category.getSubCategories().contains(subcategoryId)) {
536
            // EXCEPTION - SubCategory not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); -> Category: category.getAlias();
537
            throw new PathNotValidException("Save indicator: SubCategory with id: "+subcategoryId+" not found in Category: "+categoryId);
538
        }
539

    
540
        Section<String> section = sectionDAO.findById(sectionId);
541
        if(section == null) {
542
            // EXCEPTION - Section not found
543
            throw new EntityNotFoundException("Save indicator: Section with id: "+sectionId+" not found");
544
        }
545

    
546
        if(indicatorType.equals("chart")) {
547
            if (!subcategory.getCharts().contains(sectionId)) {
548
                // EXCEPTION - Section not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); -> Category: category.getAlias(); -> SubCategory: subcategory.getAlias();
549
                throw new PathNotValidException("Save indicator: SubCategory with id: " + subcategoryId + " not found in Category: " + categoryId);
550
            }
551
        } else if(indicatorType.equals("number")) {
552
            if (!subcategory.getNumbers().contains(sectionId)) {
553
                // EXCEPTION - Section not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); -> Category: category.getAlias(); -> SubCategory: subcategory.getAlias();
554
                throw new PathNotValidException("Save indicator: SubCategory with id: " + subcategoryId + " not found in Category: " + categoryId);
555
            }
556
        }
557

    
558
        return  section;
559
    }
560
}
(3-3/8)