Project

General

Profile

1 57671 konstantin
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 57987 konstantin
import java.io.UnsupportedEncodingException;
13
import java.net.URLEncoder;
14 57671 konstantin
import java.util.ArrayList;
15 58708 konstantin
import java.util.HashMap;
16 57671 konstantin
import java.util.List;
17 57923 konstantin
import java.util.Map;
18 57671 konstantin
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 57964 konstantin
    private SectionDAO sectionDAO;
38
39
    @Autowired
40 57671 konstantin
    private IndicatorDAO indicatorDAO;
41
42
43 57964 konstantin
    @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/{sectionId}/save", method = RequestMethod.POST)
44 57671 konstantin
    public Indicator saveIndicator(@PathVariable("stakeholderId") String stakeholderId,
45
                                   @PathVariable("topicId") String topicId,
46
                                   @PathVariable("categoryId") String categoryId,
47
                                   @PathVariable("subcategoryId") String subcategoryId,
48 57964 konstantin
                                   @PathVariable("sectionId") String sectionId,
49 57987 konstantin
                                   @RequestBody Indicator indicator) throws UnsupportedEncodingException {
50 57671 konstantin
        log.debug("save indicator");
51 57964 konstantin
        log.debug("Name: "+indicator.getName() + " - Id: "+indicator.getId() + " - Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId+ " - SubCategory: "+subcategoryId + " - Section: "+sectionId);
52 57671 konstantin
53 57964 konstantin
        Section<String> section = checkForExceptions(stakeholderId, topicId, categoryId, subcategoryId, sectionId, indicator.getType());
54 57671 konstantin
55 58708 konstantin
        Indicator oldIndicator = null;
56
        if(indicator.getId() != null) {
57
            oldIndicator = indicatorDAO.findById(indicator.getId());
58
        }
59
60 57964 konstantin
        String indicatorId = indicator.getId();
61
        indicatorDAO.save(indicator);
62
63 57671 konstantin
        Stakeholder<String> stakeholder = stakeholderDAO.findById(stakeholderId);
64 57964 konstantin
        // 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 58708 konstantin
                onUpdateDefaultIndicator(indicator, stakeholder, oldIndicator);
71 57964 konstantin
            }
72
        }
73 57671 konstantin
74 57964 konstantin
        List<String> indicators = section.getIndicators();
75 57671 konstantin
76 57964 konstantin
        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 57671 konstantin
83
        return indicator;
84
    }
85
86 57987 konstantin
    public void onSaveDefaultIndicator(Indicator indicator, String defaultSectionId) throws UnsupportedEncodingException {
87 57923 konstantin
        log.debug("On save default indicator");
88
89
        // new indicator in default profile - add it on profiles of the same type
90 57964 konstantin
        List<Section> sections = sectionDAO.findByDefaultId(defaultSectionId);
91 57923 konstantin
92 57964 konstantin
        for (Section section : sections) {
93 57923 konstantin
            Indicator indicatorNew = new Indicator();
94
            indicatorNew.copyFromDefault(indicator);
95
            for (IndicatorPath indicatorPath : indicatorNew.getIndicatorPaths()) {
96 57964 konstantin
                Stakeholder stakeholder = stakeholderDAO.findByAlias(section.getStakeholderAlias());
97 57923 konstantin
                parameterMapping(indicatorPath, stakeholder);
98
            }
99
100
            indicatorDAO.save(indicatorNew);
101
102 57964 konstantin
            List<String> indicators = section.getIndicators();
103 57923 konstantin
            indicators.add(indicatorNew.getId());
104
105 57964 konstantin
            sectionDAO.save(section);
106 57923 konstantin
        }
107
    }
108
109 58708 konstantin
    public void onUpdateDefaultIndicator(Indicator indicator, Stakeholder stakeholder, Indicator oldIndicator) throws UnsupportedEncodingException {
110 57923 konstantin
        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 58708 konstantin
            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 57923 konstantin
            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 58708 konstantin
                    IndicatorPath oldIndicatorPath = oldIndicator.getIndicatorPaths().get(i);
146
147 57923 konstantin
                    // Check if there are changes in indicator path and update existing indicators if needed
148 58708 konstantin
                    log.debug("update indicator path: "+i);
149 58954 konstantin
                    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 58708 konstantin
                            && (oldIndicatorPath.getType().equals(indicatorPathBasedOnDefault.getType()))) {
156
157 57923 konstantin
                        indicatorPathBasedOnDefault.setType(indicatorPath.getType());
158 58708 konstantin
                        changed = true; // parameter "type" needs to be changed as well
159 57923 konstantin
                    }
160 58708 konstantin
                    log.debug("After type check: "+changed);
161
162 58954 konstantin
                    if(indicatorPath.getSource() != null
163
                            && !indicatorPath.getSource().equals(indicatorPathBasedOnDefault.getSource())
164 58708 konstantin
                            && (oldIndicatorPath.getSource().equals(indicatorPathBasedOnDefault.getSource()))) {
165
166 57923 konstantin
                        indicatorPathBasedOnDefault.setSource(indicatorPath.getSource());
167
                        changed = true;
168
                    }
169 58708 konstantin
                    log.debug("After source check: "+changed);
170
171 58954 konstantin
                    if(indicatorPath.getUrl() != null
172
                            && !indicatorPath.getUrl().equals(indicatorPathBasedOnDefault.getUrl())
173 58708 konstantin
                            && (oldIndicatorPath.getUrl().equals(indicatorPathBasedOnDefault.getUrl()))) {
174
175 57923 konstantin
                        indicatorPathBasedOnDefault.setUrl(indicatorPath.getUrl());
176
                        changed = true;
177
                    }
178 58708 konstantin
                    log.debug("After url check: "+changed);
179
180 58954 konstantin
                    if(indicatorPath.getChartObject() != null
181
                            && !indicatorPath.getChartObject().equals(indicatorPathBasedOnDefault.getChartObject())
182 58708 konstantin
                            && (oldIndicatorPath.getChartObject().equals(indicatorPathBasedOnDefault.getChartObject()))) {
183
184 57923 konstantin
                        indicatorPathBasedOnDefault.setChartObject(indicatorPath.getChartObject());
185
                        changed = true;
186
                    }
187 58708 konstantin
                    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 57923 konstantin
                            }
212 58708 konstantin
                            parameterMapping(indicatorPathBasedOnDefault, stakeholder);
213
                        //}
214
                        log.debug("After parameters check: " + changed);
215 57923 konstantin
                    }
216 58708 konstantin
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 57923 konstantin
                        }
238 58708 konstantin
                        log.debug("After jsonPath check: " + changed);
239 57923 konstantin
                    }
240
                }
241
                i++;
242
            }
243 58708 konstantin
244 57923 konstantin
            if(!changed) {
245 58708 konstantin
//                break;
246
                continue;
247 57923 konstantin
            }
248 58708 konstantin
249 57923 konstantin
            indicatorDAO.save(indicatorBasedOnDefault);
250
        }
251
    }
252
253 57987 konstantin
    public void parameterMapping(IndicatorPath indicatorPath, Stakeholder stakeholder) throws UnsupportedEncodingException {
254 58954 konstantin
        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 57923 konstantin
        }
263 57987 konstantin
264 58954 konstantin
//        // 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 57923 konstantin
    }
274
275 57987 konstantin
    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 57964 konstantin
    @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/{sectionId}/{indicatorId}/delete", method = RequestMethod.DELETE)
309 57671 konstantin
    public boolean deleteIndicator(@PathVariable("stakeholderId") String stakeholderId,
310
                                   @PathVariable("topicId") String topicId,
311
                                   @PathVariable("categoryId") String categoryId,
312
                                   @PathVariable("subcategoryId") String subcategoryId,
313 57964 konstantin
                                   @PathVariable("sectionId") String sectionId,
314 57671 konstantin
                                   @PathVariable("indicatorId") String indicatorId) {
315
        log.debug("delete indicator");
316 57964 konstantin
        log.debug("Id: "+indicatorId + " - Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId+ " - SubCategory: "+subcategoryId + " - Section: "+sectionId);
317 57671 konstantin
318 57964 konstantin
        Indicator indicator = indicatorDAO.findById(indicatorId);
319
        if(indicator != null) {
320
            Section<String> section = checkForExceptions(stakeholderId, topicId, categoryId, subcategoryId, sectionId, indicator.getType());
321 57671 konstantin
322 57964 konstantin
            List<String> indicators = section.getIndicators();
323 57671 konstantin
324 57964 konstantin
            int index = indicators.indexOf(indicatorId);
325
            if (index != -1) {
326
                indicators.remove(index);
327
                sectionDAO.save(section);
328 57671 konstantin
329 57964 konstantin
                indicatorDAO.delete(indicatorId);
330
                log.debug("Indicator deleted!");
331 57671 konstantin
            } else {
332 57964 konstantin
                // 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 57671 konstantin
            }
335
        } else {
336 57964 konstantin
            // EXCEPTION - Indicator not found
337
            throw new EntityNotFoundException("Delete indicator: Indicator with id: "+indicatorId+" not found");
338 57671 konstantin
        }
339
        return true;
340
    }
341
342 57964 konstantin
//    @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 57671 konstantin
415 57964 konstantin
    @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/{sectionId}/{type}/reorder", method = RequestMethod.POST)
416 57683 konstantin
    public List<Indicator> reorderIndicators(@PathVariable("stakeholderId") String stakeholderId,
417 57964 konstantin
                                             @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 57923 konstantin
        log.debug("reorder indicators of type: "+type);
424 57964 konstantin
        log.debug("Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId+ " - SubCategory: "+subcategoryId + " - Section: "+sectionId);
425 57671 konstantin
426 57964 konstantin
        Section<String> section = checkForExceptions(stakeholderId, topicId, categoryId, subcategoryId, sectionId, type);
427 57671 konstantin
428 57964 konstantin
        section.setIndicators(indicators);
429 57671 konstantin
430 57964 konstantin
        sectionDAO.save(section);
431
        log.debug("Indicators reordered!");
432 57671 konstantin
433 57683 konstantin
        List<Indicator> indicatorsFull = new ArrayList<>();
434
        for(String indicatorId : indicators) {
435
            indicatorsFull.add(indicatorDAO.findById(indicatorId));
436
        }
437
        return indicatorsFull;
438 57671 konstantin
    }
439 57923 konstantin
440 57964 konstantin
    @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/{sectionId}/{indicatorId}/toggle-status", method = RequestMethod.POST)
441 57923 konstantin
    public Boolean toggleIndicatorStatus(@PathVariable("stakeholderId") String stakeholderId,
442
                                         @PathVariable("topicId") String topicId,
443
                                         @PathVariable("categoryId") String categoryId,
444
                                         @PathVariable("subcategoryId") String subcategoryId,
445 57964 konstantin
                                         @PathVariable("sectionId") String sectionId,
446 57923 konstantin
                                         @PathVariable("indicatorId") String indicatorId) {
447
        log.debug("toggle indicator status (isActive)");
448 57964 konstantin
        log.debug("Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId+ " - SubCategory: "+subcategoryId + " - Section: "+sectionId+ " - Indicator: "+indicatorId);
449 57923 konstantin
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 57964 konstantin
        this.toggleIndicator(stakeholderId, topicId, categoryId, subcategoryId, sectionId, indicator);
458 57923 konstantin
459
        return indicator.getIsActive();
460
    }
461
462 57964 konstantin
    @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/{sectionId}/{indicatorId}/toggle-access", method = RequestMethod.POST)
463 57923 konstantin
    public Boolean toggleIndicatorAccess(@PathVariable("stakeholderId") String stakeholderId,
464
                                         @PathVariable("topicId") String topicId,
465
                                         @PathVariable("categoryId") String categoryId,
466
                                         @PathVariable("subcategoryId") String subcategoryId,
467 57964 konstantin
                                         @PathVariable("sectionId") String sectionId,
468 57923 konstantin
                                         @PathVariable("indicatorId") String indicatorId) {
469
        log.debug("toggle indicator access (isPublic)");
470 57964 konstantin
        log.debug("Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId+ " - SubCategory: "+subcategoryId + " - Section: "+sectionId+ " - Indicator: "+indicatorId);
471 57923 konstantin
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 57964 konstantin
        this.toggleIndicator(stakeholderId, topicId, categoryId, subcategoryId, sectionId, indicator);
480 57923 konstantin
481
        return indicator.getIsPublic();
482
    }
483
484 57964 konstantin
    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 57923 konstantin
        Stakeholder<String> stakeholder = stakeholderDAO.findById(stakeholderId);
501
502 57964 konstantin
        if(stakeholder == null) {
503
            // EXCEPTION - Stakeholder not found
504
            throw new EntityNotFoundException("Save indicator: Stakeholder with id: " + stakeholderId + " not found");
505
        }
506 57923 konstantin
507 57964 konstantin
        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 57923 konstantin
513 57964 konstantin
        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 57923 konstantin
518 57964 konstantin
        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 57934 konstantin
524 57964 konstantin
        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 57923 konstantin
            }
551 57964 konstantin
        } 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 57923 konstantin
        }
557 57964 konstantin
558
        return  section;
559 57923 konstantin
    }
560 57671 konstantin
}