Project

General

Profile

1
package eu.dnetlib.uoamonitorservice.controllers;
2

    
3

    
4
import eu.dnetlib.uoaadmintoolslibrary.handlers.utils.RolesUtils;
5
import eu.dnetlib.uoamonitorservice.dao.*;
6
import eu.dnetlib.uoamonitorservice.entities.*;
7
import eu.dnetlib.uoamonitorservice.handlers.EntityNotFoundException;
8
import eu.dnetlib.uoaadmintoolslibrary.handlers.ForbiddenException;
9
import eu.dnetlib.uoamonitorservice.handlers.PathNotValidException;
10
import org.apache.log4j.Logger;
11
import org.springframework.beans.factory.annotation.Autowired;
12
import org.springframework.security.access.AccessDeniedException;
13
import org.springframework.security.access.prepost.PreAuthorize;
14
import org.springframework.web.bind.annotation.*;
15

    
16
import java.io.UnsupportedEncodingException;
17
import java.lang.reflect.Field;
18
import java.net.URLEncoder;
19
import java.util.*;
20

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

    
26
    @Autowired
27
    private RolesUtils rolesUtils;
28

    
29
    @Autowired
30
    private StakeholderDAO stakeholderDAO;
31

    
32
    @Autowired
33
    private TopicDAO topicDAO;
34

    
35
    @Autowired
36
    private CategoryDAO categoryDAO;
37

    
38
    @Autowired
39
    private SubCategoryDAO subCategoryDAO;
40

    
41
    @Autowired
42
    private SectionDAO sectionDAO;
43

    
44
    @Autowired
45
    private IndicatorDAO indicatorDAO;
46

    
47
    @Autowired
48
    private SectionController sectionController;
49

    
50
    @Autowired
51
    private StakeholderController stakeholderController;
52

    
53
    @PreAuthorize("isAuthenticated()")
54
    @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/save-bulk", method = RequestMethod.POST)
55
    public Stakeholder saveBulkIndicators(@PathVariable("stakeholderId") String stakeholderId,
56
                                        @PathVariable("topicId") String topicId,
57
                                        @PathVariable("categoryId") String categoryId,
58
                                        @PathVariable("subcategoryId") String subcategoryId,
59
                                        @RequestBody List<Indicator> indicators) throws UnsupportedEncodingException {
60
        log.debug("save bulk indicators");
61
        log.debug("Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId+ " - SubCategory: "+subcategoryId);
62

    
63
        Stakeholder<String> stakeholder = stakeholderDAO.findById(stakeholderId);
64

    
65
        Date date = new Date();
66

    
67
        createSectionsAndSaveBulk(date, indicators, stakeholder, topicId, categoryId, subcategoryId);
68
//        createSectionAndSaveBulk(date, "number", "Numbers imported from file", number_indicators, stakeholder, topicId, categoryId, subcategoryId);
69

    
70
        return stakeholderController.setFullEntities(stakeholder, rolesUtils.getRoles());
71
    }
72

    
73
    private void createSectionsAndSaveBulk(Date date, List<Indicator> new_indicators,
74
                                          Stakeholder stakeholder, String topicId, String categoryId, String subcategoryId) throws UnsupportedEncodingException {
75
        Section chart_section = null;
76
        Section number_section = null;
77

    
78
        List<String> chart_indicators = null;
79
        List<String> number_indicators = null;
80

    
81
        for(Indicator indicator : new_indicators) {
82
            if(indicator.getType().equals("chart")) {
83
                if(chart_section == null) {
84
                    chart_section = createSection(chart_section, "chart", "Charts imported from file", date, stakeholder, topicId, categoryId, subcategoryId);
85
                    chart_indicators = chart_section.getIndicators();
86
                }
87
                saveIndicatorAndAddInSection(indicator, date, stakeholder, chart_section, chart_indicators);
88

    
89
            } else if(indicator.getType().equals("number")) {
90
                if(number_section == null) {
91
                    number_section = createSection(number_section, "number", "Numbers imported from file", date, stakeholder, topicId, categoryId, subcategoryId);
92
                    number_indicators = number_section.getIndicators();
93
                }
94
                saveIndicatorAndAddInSection(indicator, date, stakeholder, number_section, number_indicators);
95
            }
96
        }
97

    
98
        if(chart_section != null) {
99
            sectionDAO.save(chart_section);
100
        }
101
        if(number_section != null) {
102
            sectionDAO.save(number_section);
103
        }
104
    }
105

    
106
    private Section createSection(Section section, String type, String title, Date date,
107
                                  Stakeholder stakeholder, String topicId, String categoryId, String subcategoryId) {
108
        section = new Section<>();
109
        section.setType(type);
110
        section.setTitle(title);
111
        section.setStakeholderAlias(stakeholder.getAlias());
112
        section.setUpdateDate(date);
113
        section.setCreationDate(date);
114
        section.setIndicators(new ArrayList<>());
115
        sectionController.saveSection(stakeholder.getId(), topicId, categoryId, subcategoryId, "-1", section);
116

    
117
        return section;
118
    }
119

    
120
    private void saveIndicatorAndAddInSection(Indicator indicator, Date date, Stakeholder stakeholder, Section section, List<String> indicators) throws UnsupportedEncodingException {
121
        // indicator does not exist in DB
122
        indicator.setCreationDate(date);
123
        indicator.setUpdateDate(date);
124

    
125
        if (stakeholder.getDefaultId() == null) {   // this indicator belongs in default profile and it is new
126
            indicatorDAO.save(indicator);
127
            onSaveDefaultIndicator(indicator, section.getId());
128
        } else {    // this indicator belongs in a stakeholder's profile and it is new
129
            indicatorDAO.save(indicator);
130
        }
131

    
132
        indicators.add(indicator.getId());
133
        log.debug("Indicator saved!");
134
    }
135

    
136
    @PreAuthorize("isAuthenticated()")
137
    @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/{sectionId}/save", method = RequestMethod.POST)
138
    public Indicator saveIndicator(@PathVariable("stakeholderId") String stakeholderId,
139
                                   @PathVariable("topicId") String topicId,
140
                                   @PathVariable("categoryId") String categoryId,
141
                                   @PathVariable("subcategoryId") String subcategoryId,
142
                                   @PathVariable("sectionId") String sectionId,
143
                                   @RequestBody Indicator indicator) throws UnsupportedEncodingException {
144
        log.debug("save indicator");
145
        log.debug("Name: "+indicator.getName() + " - Id: "+indicator.getId() + " - Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId+ " - SubCategory: "+subcategoryId + " - Section: "+sectionId);
146

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

    
149
        Date date = new Date();
150
        indicator.setUpdateDate(date);
151

    
152
        Indicator oldIndicator = null;
153
        if(indicator.getId() != null) {
154
            oldIndicator = indicatorDAO.findById(indicator.getId());
155
            if(oldIndicator == null) {
156
                // EXCEPTION - Indicator not found
157
                throw new EntityNotFoundException("save indicator: Indicator with id: " + indicator.getId() + " not found");
158
            }
159
        } else { // indicator does not exist in DB
160
            indicator.setCreationDate(date);
161
        }
162

    
163
        String indicatorId = indicator.getId();
164

    
165
        Stakeholder<String> stakeholder = stakeholderDAO.findById(stakeholderId);
166
        // this indicator belongs in default profile and it is new or it is updated
167
        if(stakeholder.getDefaultId() == null) {
168
            if(indicatorId == null) {
169
                indicatorDAO.save(indicator);
170
                onSaveDefaultIndicator(indicator, sectionId);
171
            }
172
            else {
173
                onUpdateDefaultIndicator(indicator, stakeholder, oldIndicator);
174
                indicatorDAO.save(indicator);
175
            }
176
        } else {
177
            indicatorDAO.save(indicator);
178
        }
179

    
180
        List<String> indicators = section.getIndicators();
181

    
182
        int index = indicators.indexOf(indicator.getId());
183
        if (index == -1) {
184
            indicators.add(indicator.getId());
185
            sectionDAO.save(section);
186
            log.debug("Indicator saved!");
187
        }
188

    
189
        return indicator;
190
    }
191

    
192
    public void onSaveDefaultIndicator(Indicator indicator, String defaultSectionId) throws UnsupportedEncodingException {
193
        log.debug("On save default indicator");
194

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

    
198
       for (Section section : sections) {
199
            Indicator indicatorNew = new Indicator();
200
            indicatorNew.copyFromDefault(indicator);
201
            for (IndicatorPath indicatorPath : indicatorNew.getIndicatorPaths()) {
202
                Stakeholder stakeholder = stakeholderDAO.findByAlias(section.getStakeholderAlias());
203
                parameterMapping(indicatorPath, stakeholder);
204
            }
205

    
206
            indicatorDAO.save(indicatorNew);
207

    
208
            List<String> indicators = section.getIndicators();
209
            indicators.add(indicatorNew.getId());
210

    
211
            sectionDAO.save(section);
212
        }
213
    }
214

    
215
    public void onUpdateDefaultIndicator(Indicator indicator, Stakeholder stakeholder, Indicator oldIndicator) throws UnsupportedEncodingException {
216
        log.debug("On update default indicator");
217

    
218
        // indicator already exists - check if changed and update all indicators based on it
219

    
220
        boolean changed;
221
        List<Indicator> indicators = indicatorDAO.findByDefaultId(indicator.getId());
222

    
223
        for(Indicator indicatorBasedOnDefault : indicators) {
224
            changed = false;
225

    
226
//            if(indicator.getName() != null && !indicator.getName().equals(indicatorBasedOnDefault.getName())
227
//                    && (oldIndicator.getName() == null || oldIndicator.getName().equals(indicatorBasedOnDefault.getName()))) {
228
            if((
229
                    (indicator.getName() == null && oldIndicator.getName() != null)
230
                            ||
231
                            (indicator.getName() != null && !indicator.getName().equals(indicatorBasedOnDefault.getName()))
232
            ) && (
233
                    (oldIndicator.getName() == null && indicatorBasedOnDefault.getName() == null)
234
                            ||
235
                            (oldIndicator.getName() != null && oldIndicator.getName().equals(indicatorBasedOnDefault.getName()))
236
            )) {
237
                indicatorBasedOnDefault.setName(indicator.getName());
238
                changed = true;
239
            }
240

    
241
            if(indicator.getDescription() != null && !indicator.getDescription().equals(indicatorBasedOnDefault.getDescription())
242
                || indicator.getDescription() == null && indicatorBasedOnDefault.getDescription() != null) {
243

    
244
                indicatorBasedOnDefault.setDescription(indicator.getDescription());
245
                changed = true;
246
            }
247

    
248
//            if(indicator.getAdditionalDescription() != null && !indicator.getAdditionalDescription().equals(indicatorBasedOnDefault.getAdditionalDescription())
249
//                    && (oldIndicator.getAdditionalDescription() == null || oldIndicator.getAdditionalDescription().equals(indicatorBasedOnDefault.getAdditionalDescription()))) {
250
            if((
251
                    (indicator.getAdditionalDescription() == null && oldIndicator.getAdditionalDescription() != null)
252
                            ||
253
                            (indicator.getAdditionalDescription() != null && !indicator.getAdditionalDescription().equals(indicatorBasedOnDefault.getAdditionalDescription()))
254
            ) && (
255
                    (oldIndicator.getAdditionalDescription() == null && indicatorBasedOnDefault.getAdditionalDescription() == null)
256
                            ||
257
                            (oldIndicator.getAdditionalDescription() != null && oldIndicator.getAdditionalDescription().equals(indicatorBasedOnDefault.getAdditionalDescription()))
258
            )) {
259
                indicatorBasedOnDefault.setAdditionalDescription(indicator.getAdditionalDescription());
260
                changed = true;
261
            }
262

    
263
            int i = 0;
264
            List<IndicatorPath> indicatorPaths = indicatorBasedOnDefault.getIndicatorPaths();
265
            if(indicatorPaths == null && indicator.getIndicatorPaths() != null) {
266
                indicatorPaths = new ArrayList<>();
267
            }
268

    
269
            for (IndicatorPath indicatorPath : indicator.getIndicatorPaths()) {
270
                IndicatorPath indicatorPathBasedOnDefault = null;
271
                if(i < indicatorPaths.size()) {
272
                    indicatorPathBasedOnDefault = indicatorPaths.get(i);
273
                }
274

    
275
                if(indicatorPathBasedOnDefault == null) {
276
                    // Add new indicator path in existing indicators
277
                    IndicatorPath indicatorPathNew = new IndicatorPath(indicatorPath);
278
                    parameterMapping(indicatorPathNew, stakeholder);
279
                    indicatorPaths.add(indicatorPathNew);
280
                    changed = true;
281
                } else {
282
                    IndicatorPath oldIndicatorPath = oldIndicator.getIndicatorPaths().get(i);
283

    
284
                    // Check if there are changes in indicator path and update existing indicators if needed
285
                    log.debug("update indicator path: "+i + " (indicator id: "+indicatorBasedOnDefault.getId()+")");
286

    
287
//                    if(indicatorPath.getType() != null
288
//                            && !indicatorPath.getType().equals(indicatorPathBasedOnDefault.getType())
289
//                            && (oldIndicatorPath.getType().equals(indicatorPathBasedOnDefault.getType()))) {
290
                    if((
291
                            (indicatorPath.getType() == null && oldIndicatorPath.getType() != null)
292
                                    ||
293
                                    (indicatorPath.getType() != null && !indicatorPath.getType().equals(indicatorPathBasedOnDefault.getType()))
294
                    ) && (
295
                            (oldIndicatorPath.getType() == null && indicatorPathBasedOnDefault.getType() == null)
296
                                    ||
297
                                    (oldIndicatorPath.getType() != null && oldIndicatorPath.getType().equals(indicatorPathBasedOnDefault.getType()))
298
                    )) {
299
                        indicatorPathBasedOnDefault.setType(indicatorPath.getType());
300
                        changed = true; // parameter "type" needs to be changed as well
301
                    }
302
                    log.debug("After type check: "+changed);
303

    
304
//                    if(indicatorPath.getSource() != null
305
//                            && !indicatorPath.getSource().equals(indicatorPathBasedOnDefault.getSource())
306
//                            && (oldIndicatorPath.getSource().equals(indicatorPathBasedOnDefault.getSource()))) {
307
                    if((
308
                            (indicatorPath.getSource() == null && oldIndicatorPath.getSource() != null)
309
                                    ||
310
                                    (indicatorPath.getSource() != null && !indicatorPath.getSource().equals(indicatorPathBasedOnDefault.getSource()))
311
                    ) && (
312
                            (oldIndicatorPath.getSource() == null && indicatorPathBasedOnDefault.getSource() == null)
313
                                    ||
314
                                    (oldIndicatorPath.getSource() != null && oldIndicatorPath.getSource().equals(indicatorPathBasedOnDefault.getSource()))
315
                    )) {
316
                        indicatorPathBasedOnDefault.setSource(indicatorPath.getSource());
317
                        changed = true;
318
                    }
319
                    log.debug("After source check: "+changed);
320

    
321
//                    if(indicatorPath.getUrl() != null
322
//                            && !indicatorPath.getUrl().equals(indicatorPathBasedOnDefault.getUrl())
323
//                            && (oldIndicatorPath.getUrl().equals(indicatorPathBasedOnDefault.getUrl()))) {
324
                    if((
325
                            (indicatorPath.getUrl() == null && oldIndicatorPath.getUrl() != null)
326
                                    ||
327
                                    (indicatorPath.getUrl() != null && !indicatorPath.getUrl().equals(indicatorPathBasedOnDefault.getUrl()))
328
                    ) && (
329
                            (oldIndicatorPath.getUrl() == null && indicatorPathBasedOnDefault.getUrl() == null)
330
                                    ||
331
                                    (oldIndicatorPath.getUrl() != null && oldIndicatorPath.getUrl().equals(indicatorPathBasedOnDefault.getUrl()))
332
                    )) {
333
                        indicatorPathBasedOnDefault.setUrl(indicatorPath.getUrl());
334
                        changed = true;
335
                    }
336
                    log.debug("After url check: "+changed);
337

    
338
                    if((
339
                            (indicatorPath.getChartObject() == null && oldIndicatorPath.getChartObject() != null)
340
                            ||
341
                            (indicatorPath.getChartObject() != null && !indicatorPath.getChartObject().equals(indicatorPathBasedOnDefault.getChartObject()))
342
                        ) && (
343
                            (oldIndicatorPath.getChartObject() == null && indicatorPathBasedOnDefault.getChartObject() == null)
344
                            ||
345
                            (oldIndicatorPath.getChartObject() != null && oldIndicatorPath.getChartObject().equals(indicatorPathBasedOnDefault.getChartObject()))
346
                    )) {
347

    
348
                        indicatorPathBasedOnDefault.setChartObject(indicatorPath.getChartObject());
349
                        changed = true;
350
                    }
351
                    log.debug("After chartObject check: "+changed);
352

    
353
                    if(indicatorPath.getParameters() != null) {
354
                        if (indicatorPathBasedOnDefault.getParameters() == null) {
355
                            indicatorPathBasedOnDefault.setParameters(new HashMap<>());
356
                        }
357
                        //if (indicatorPath.getParameters().size() != indicatorPathBasedOnDefault.getParameters().size()) {
358
                            //log.debug("Different number of parameters");
359
                        for (Map.Entry<String, String> parameter : indicatorPath.getParameters().entrySet()) {
360
                            log.debug("\nindicatorPath: parameter.getKey(): "+parameter.getKey()+" - value: "+parameter.getValue()
361
                                    +"\nindicatorPathBasedOnDefault:parameters:key: "+  indicatorPathBasedOnDefault.getParameters().get(parameter.getKey())
362
                                    +"\noldIndicatorPath:parameters:key: "+  (oldIndicatorPath.getParameters() == null ? "null" : oldIndicatorPath.getParameters().get(parameter.getKey())));
363
                            if (!indicatorPathBasedOnDefault.getParameters().containsKey(parameter.getKey())
364
                                    || (oldIndicatorPath.getParameters() == null || oldIndicatorPath.getParameters().get(parameter.getKey()) == null
365
                                        || (oldIndicatorPath.getParameters().get(parameter.getKey()).equals(indicatorPathBasedOnDefault.getParameters().get(parameter.getKey()))
366
                                            && !parameter.getValue().equals(indicatorPathBasedOnDefault.getParameters().get(parameter.getKey()))))
367
                            ) {
368
                                indicatorPathBasedOnDefault.getParameters().put(parameter.getKey(), parameter.getValue());
369
                                changed = true;
370
                            }
371
//                            else if(parameter.getKey().equals("type")) {
372
//                                indicatorPathBasedOnDefault.getParameters().put(parameter.getKey(), parameter.getValue());
373
//                                changed = true;
374
//                            }
375
                        }
376

    
377
                        // When deleting indicator path parameters in a default profile, delete them also from all children profiles
378
                        if(oldIndicatorPath.getParameters() != null && indicatorPath.getParameters().size() < oldIndicatorPath.getParameters().size()) {
379
                            for (Map.Entry<String, String> parameter : oldIndicatorPath.getParameters().entrySet()) {
380
                                if(!indicatorPath.getParameters().containsKey(parameter.getKey())) {
381
                                    indicatorPathBasedOnDefault.getParameters().remove(parameter.getKey());
382
                                }
383
                            }
384
                        }
385
                        parameterMapping(indicatorPathBasedOnDefault, stakeholder);
386
                        //}
387
                    }
388
                    log.debug("After parameters check: " + changed);
389

    
390
                    if(indicatorPath.getJsonPath() != null) {
391
                        boolean jsonPathChanged = false;
392
                        boolean breaked = false;
393

    
394
                        int oldJsonPathSize = 0;
395
                        if(oldIndicatorPath.getJsonPath() != null) {
396
                            oldJsonPathSize = oldIndicatorPath.getJsonPath().size();
397
                        }
398
                        int basedOnDefaultJsonPathSize = 0;
399
                        if(indicatorPathBasedOnDefault.getJsonPath() != null) {
400
                            basedOnDefaultJsonPathSize = indicatorPathBasedOnDefault.getJsonPath().size();
401
                        }
402
                        log.debug("old: "+oldJsonPathSize+" - based on default: "+basedOnDefaultJsonPathSize+" - new: "+indicatorPath.getJsonPath().size());
403
                        if(oldJsonPathSize == basedOnDefaultJsonPathSize) {
404
                            if(indicatorPathBasedOnDefault.getJsonPath() == null && indicatorPath.getJsonPath().size() > 0) {
405
                                indicatorPathBasedOnDefault.setJsonPath(new ArrayList<>());
406
                            }
407

    
408
                            int basedOnDefaultIndex = 0;
409
                            int oldIndex = 0;
410

    
411
                            Iterator<String> jsonStringBasedOnDefaultIterator = indicatorPathBasedOnDefault.getJsonPath().iterator();
412
                            while (jsonStringBasedOnDefaultIterator.hasNext()) {
413
                                String jsonStringBasedOnDefault = jsonStringBasedOnDefaultIterator.next();
414
                                if(oldIndicatorPath.getJsonPath().get(oldIndex).equals(jsonStringBasedOnDefault)) {
415
                                    if(basedOnDefaultIndex >= indicatorPath.getJsonPath().size()) { // string deleted
416
                                        jsonStringBasedOnDefaultIterator.remove();
417
                                        jsonPathChanged = true;
418
                                    } else {    // check if string changed
419
                                        if(!indicatorPath.getJsonPath().get(basedOnDefaultIndex).equals(jsonStringBasedOnDefault)) {
420
                                            indicatorPathBasedOnDefault.getJsonPath().set(basedOnDefaultIndex, indicatorPath.getJsonPath().get(basedOnDefaultIndex));
421
                                            jsonPathChanged = true;
422
                                        }
423
                                        basedOnDefaultIndex++;
424
                                    }
425
                                    oldIndex++;
426
                                } else {
427
                                    breaked = true;
428
                                    jsonPathChanged = false;
429
                                    log.debug("not the same: "+oldIndex);
430
                                    break;
431
                                }
432
                            }
433

    
434
                            int index=0;
435
                            if(!breaked && indicatorPath.getJsonPath().size() > indicatorPathBasedOnDefault.getJsonPath().size()) { // strings added
436
                                jsonPathChanged = true;
437
                                for(index=indicatorPathBasedOnDefault.getJsonPath().size(); index < indicatorPath.getJsonPath().size(); index++) {
438
                                    indicatorPathBasedOnDefault.getJsonPath().add(indicatorPath.getJsonPath().get(index));
439
                                }
440
                            }
441

    
442
                            if(jsonPathChanged) {
443
                                changed = true;
444
                            }
445
                        }
446
                        // TODO when deleting indicator path json path strings... --> is this done? (line 327)
447
                    }
448
                    log.debug("After jsonPath check: " + changed);
449
                }
450
                i++;
451
            }
452
            // TODO when deleting indicator paths...
453

    
454
            if(!changed) {
455
//                break;
456
                continue;
457
            }
458

    
459
            indicatorBasedOnDefault.setUpdateDate(indicator.getUpdateDate());
460
            indicatorDAO.save(indicatorBasedOnDefault);
461
        }
462
    }
463

    
464
    public void parameterMapping(IndicatorPath indicatorPath, Stakeholder stakeholder) throws UnsupportedEncodingException {
465
        if (indicatorPath.getParameters() != null) {
466
            if (indicatorPath.getParameters().containsKey("index_name")) {
467
                indicatorPath.getParameters().put("index_name", stakeholder.getIndex_name());
468
            } else if (indicatorPath.getParameters().containsKey("index_shortName")) {
469
                indicatorPath.getParameters().put("index_shortName", stakeholder.getIndex_name().toLowerCase());
470
            } else if (indicatorPath.getParameters().containsKey("index_id")) {
471
                indicatorPath.getParameters().put("index_id", stakeholder.getIndex_id());
472
            }
473
        }
474

    
475
//        // url encoding for number indicators
476
//        String url = indicatorPath.getUrl();
477
//        String encoded_index_id = urlEncode(URLEncoder.encode(stakeholder.getIndex_id(), "UTF-8"));
478
//        url = url.replace("index_id", encoded_index_id);
479
//        String encoded_index_name = urlEncode(URLEncoder.encode(stakeholder.getIndex_name(), "UTF-8"));
480
//        url = url.replace("index_name", encoded_index_name);
481
//        String encoded_index_shortName = urlEncode(URLEncoder.encode(stakeholder.getIndex_shortName(), "UTF-8"));
482
//        url = url.replace("index_shortName", encoded_index_shortName);
483
//        indicatorPath.setUrl(url);
484
    }
485

    
486
    public String urlEncode(String encodedIndicatorPathField) {
487
        String indicatorPathField = "";
488

    
489
        for( int i=0; i<encodedIndicatorPathField.length(); i++ ){
490
            String character = encodedIndicatorPathField.substring(i, i+1);
491

    
492
            if(character.equals("+")) {
493
                indicatorPathField = indicatorPathField.concat("%20");
494
            } else if(character.equals("%")) {
495
                //grab the hex in pairs
496
                String output = encodedIndicatorPathField.substring(i+1, (i + 3));
497

    
498
                if(output.equals("7E") || output.equals("27") || output.equals("28") || output.equals("29") || output.equals("21")) {
499
                    //convert hex to decimal
500
                    int decimal = Integer.parseInt(output, 16);
501
                    //convert the decimal to character
502
                    StringBuilder sb = new StringBuilder();
503
                    sb.append((char) decimal);
504

    
505
                    indicatorPathField = indicatorPathField.concat(sb.toString());
506
                } else {
507
                    indicatorPathField = indicatorPathField.concat(character + output);
508
                }
509

    
510
                i += 2;
511
            } else {
512
                indicatorPathField = indicatorPathField.concat(character);
513
            }
514
        }
515

    
516
        return indicatorPathField;
517
    }
518

    
519
    @PreAuthorize("isAuthenticated()")
520
    @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/{sectionId}/{indicatorId}/delete", method = RequestMethod.DELETE)
521
    public boolean deleteIndicator(@PathVariable("stakeholderId") String stakeholderId,
522
                                   @PathVariable("topicId") String topicId,
523
                                   @PathVariable("categoryId") String categoryId,
524
                                   @PathVariable("subcategoryId") String subcategoryId,
525
                                   @PathVariable("sectionId") String sectionId,
526
                                   @PathVariable("indicatorId") String indicatorId,
527
                                   @RequestParam(required = false) String children) {
528
        log.debug("delete indicator");
529
        log.debug("Id: "+indicatorId + " - Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId+ " - SubCategory: "+subcategoryId + " - Section: "+sectionId);
530

    
531
        Indicator indicator = indicatorDAO.findById(indicatorId);
532
        if(indicator != null) {
533
            Section<String> section = checkForExceptions(stakeholderId, topicId, categoryId, subcategoryId, sectionId, indicator.getType());
534

    
535
            Stakeholder<String> stakeholder = stakeholderDAO.findById(stakeholderId);
536
            List<String> roles = rolesUtils.getRoles();
537
            if(indicator.getDefaultId() != null && !rolesUtils.hasCreateAndDeleteAuthority(roles, stakeholder.getType())) {
538
                // EXCEPTION - Access denied
539
                throw new ForbiddenException("Delete indicator: You are not authorized to delete a default Indicator in stakeholder with id: "+stakeholderId);
540
            }
541

    
542
            List<String> indicators = section.getIndicators();
543

    
544
            int index = indicators.indexOf(indicatorId);
545
            if (index != -1) {
546

    
547
                // this indicator belongs in default profile
548
                if(section.getDefaultId() == null && children != null) {
549
                    onDeleteDefaultIndicator(indicatorId, sectionId, children);
550
                }
551

    
552

    
553
                indicators.remove(index);
554
                sectionDAO.save(section);
555

    
556
                indicatorDAO.delete(indicatorId);
557
                log.debug("Indicator deleted!");
558
            } else {
559
                // EXCEPTION - Indicator not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); -> Category: category.getAlias(); -> SubCategory: subcategory.getAlias(); -> Section: section.getTitle();
560
                throw new PathNotValidException("Delete indicator: Indicator with id: "+indicatorId+" not found in Sectiom: "+sectionId);
561
            }
562
        } else {
563
            // EXCEPTION - Indicator not found
564
            throw new EntityNotFoundException("Delete indicator: Indicator with id: "+indicatorId+" not found");
565
        }
566
        return true;
567
    }
568

    
569
    public boolean onDeleteDefaultIndicator(String defaultIndicatorId, String defaultSectionId, String children) {
570
        if(children.equals("delete")) {
571
//            // 1st way
572
//            List<Section> sections = sectionDAO.findByDefaultId(defaultSectionId);
573
//
574
//            for(Section section : sections) {
575
//                List<String> indicators = section.getIndicators();
576
//
577
//                Iterator<String> indicatorsIterator = indicators.iterator();
578
//                while(indicatorsIterator.hasNext()) {
579
//                    String indicatorId = indicatorsIterator.next();
580
//
581
//                    Indicator indicator = indicatorDAO.findById(indicatorId);
582
//                    if (indicator.getDefaultId().equals(defaultIndicatorId)) {
583
//                        indicatorsIterator.remove();
584
//                        sectionDAO.save(section);
585
//
586
//                        indicatorDAO.delete(indicatorId);
587
//                        log.debug("Indicator deleted!");
588
//
589
//                        break;
590
//                    }
591
//                }
592
//            }
593

    
594
            // 2nd way
595
            List<Section> sections = sectionDAO.findByDefaultId(defaultSectionId);
596
            List<Indicator> indicators = indicatorDAO.findByDefaultId(defaultIndicatorId);
597

    
598
            for(Section section : sections) {
599
                Iterator<Indicator> indicatorsIterator = indicators.iterator();
600
                while(indicatorsIterator.hasNext()) {
601
                    String indicatorId = indicatorsIterator.next().getId();
602
                    if(section.getIndicators().contains(indicatorId)) {
603
                        indicatorsIterator.remove();
604

    
605
                        section.getIndicators().remove(indicatorId);
606
                        sectionDAO.save(section);
607

    
608
                        indicatorDAO.delete(indicatorId);
609
                        log.debug("Indicator with id: "+indicatorId+" deleted!");
610

    
611
                        break;
612
                    }
613
                }
614
            }
615

    
616
//            // 3rd way - parentId
617
//            List<Indicator> indicators = indicatorDAO.findByDefaultId(defaultIndicatorId);
618
//            for(Indicator indicator : indicators) {
619
//                Section section = sectionDAO.findById(indicator.getParent());
620
//                List<String> sectionIndicators = section.getIndicators();
621
//
622
//                sectionIndicators.remove(indicator.getId());
623
//                sectionDAO.save(section);
624
//
625
//                indicatorDAO.delete(indicator.getId());
626
//                log.debug("Indicator deleted!");
627
//            }
628
        } else if(children.equals("disconnect")) {
629
            List<Indicator> indicators = indicatorDAO.findByDefaultId(defaultIndicatorId);
630
            for(Indicator indicator : indicators) {
631
                indicator.setDefaultId(null);
632
                indicatorDAO.save(indicator);
633
                log.debug("DefaultId for Indicator with id: "+indicator.getId()+" empty!");
634
            }
635
        }
636
        return true;
637
    }
638

    
639
//    @RequestMapping(value = "/{stakeholderId}/charts/delete", method = RequestMethod.DELETE)
640
//    public boolean deleteAllChartIndicators(@PathVariable("stakeholderId") String stakeholderId) {
641
//        log.debug("delete all chart indicators of stakeholder");
642
//        log.debug("Stakeholder: "+stakeholderId);
643
//
644
//        Stakeholder<String> stakeholder = stakeholderDAO.findById(stakeholderId);
645
//        if(stakeholder != null) {
646
//
647
//            for(String topicId : stakeholder.getTopics()) {
648
//                Topic<String> topic = topicDAO.findById(topicId);
649
//                if(topic != null) {
650
//                    for(String categoryId : topic.getCategories()) {
651
//                        Category<String> category = categoryDAO.findById(categoryId);
652
//                        if(category != null) {
653
//                            for(String subcategoryId : category.getSubCategories()) {
654
//                                SubCategory<String> subcategory = subCategoryDAO.findById(subcategoryId);
655
//                                if(subcategory != null) {
656
//
657
//                                    for(String sectionId : subcategory.getCharts()) {
658
//                                        Section<String> section = sectionDAO.findById(sectionId);
659
//                                        if (section != null) {
660
//
661
//                                            List<String> indicators = section.getIndicators();
662
//                                            Iterator<String> indicatorsIterator = section.getIndicators().iterator();
663
//
664
//                                            while (indicatorsIterator.hasNext()) {
665
//                                                String indicatorId = indicatorsIterator.next();
666
//                                                Indicator indicator = indicatorDAO.findById(indicatorId);
667
//                                                if (indicator != null) {
668
//                                                    int index = indicators.indexOf(indicatorId);
669
//                                                    if (index != -1) {
670
//                                                        indicatorsIterator.remove();
671
//                                                        //indicators.remove(index);
672
//
673
//                                                        indicatorDAO.delete(indicatorId);
674
//                                                        log.debug("Indicator deleted!");
675
//                                                    } else {
676
//                                                        // EXCEPTION - Indicator not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); -> Category: category.getAlias(); -> SubCategory: subcategory.getAlias(); -> Section: section.getTitle();
677
//                                                        throw new PathNotValidException("Delete indicator: Indicator with id: " + indicatorId + " not found in Section: " + sectionId);
678
//                                                    }
679
//                                                } else {
680
//                                                    // EXCEPTION - Indicator not found
681
//                                                    throw new EntityNotFoundException("Delete indicator: Indicator with id: " + indicatorId + " not found");
682
//                                                }
683
//                                            }
684
//                                            sectionDAO.save(section);
685
//                                        } else {
686
//                                            // EXCEPTION - Section not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); -> Category: category.getAlias(); -> SubCategory: subcategory.getAlias();
687
//                                            throw new PathNotValidException("Delete indicator: Section with id: " + sectionId + " not found in SubCategory: " + subcategoryId);
688
//                                        }
689
//                                    }
690
//                                } else {
691
//                                    // EXCEPTION - SubCategory not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); -> Category: category.getAlias();
692
//                                    throw new PathNotValidException("Delete indicator: SubCategory with id: "+subcategoryId+" not found in Category: "+categoryId);
693
//                                }
694
//                            }
695
//                        } else {
696
//                            // EXCEPTION - Category not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias();
697
//                            throw new PathNotValidException("Delete indicator: Category with id: "+categoryId+" not found in Topic: "+topicId);
698
//                        }
699
//                    }
700
//                } else {
701
//                    // EXCEPTION - Topic not found in Stakeholder: stakeholder.getAlias();
702
//                    throw new PathNotValidException("Delete indicator: Topic with id: "+topicId+" not found in Stakeholder: "+stakeholderId);
703
//                }
704
//            }
705
//        } else {
706
//            // EXCEPTION - Stakeholder not found
707
//            throw new EntityNotFoundException("Delete indicator: Stakeholder with id: "+stakeholderId+" not found");
708
//        }
709
//        return true;
710
//    }
711

    
712
    @PreAuthorize("isAuthenticated()")
713
    @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/{sectionId}/{type}/reorder", method = RequestMethod.POST)
714
    public List<Indicator> reorderIndicators(@PathVariable("stakeholderId") String stakeholderId,
715
                                             @PathVariable("topicId") String topicId,
716
                                             @PathVariable("categoryId") String categoryId,
717
                                             @PathVariable("subcategoryId") String subcategoryId,
718
                                             @PathVariable("sectionId") String sectionId,
719
                                             @PathVariable("type") String type,
720
                                             @RequestBody ReorderEvent reorderEvent) {
721
        log.debug("reorder indicators of type: "+type);
722
        log.debug("Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId+ " - SubCategory: "+subcategoryId + " - Section: "+sectionId);
723

    
724
        List<String> indicators = reorderEvent.getIds();
725
        String actionType = reorderEvent.getAction();
726
        String targetId = reorderEvent.getTarget();
727

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

    
730
        List<String> oldIndicators = section.getIndicators();
731
        for (String indicatorId : oldIndicators) {
732
            if ((!actionType.equals("removed") || !targetId.equals(indicatorId)) && !indicators.contains(indicatorId)) {
733
                indicators.add(indicatorId);
734
            }
735
        }
736
        section.setIndicators(indicators);
737

    
738
        List<Indicator> indicatorsFull = new ArrayList<>();
739
        for(String indicatorId : indicators) {
740
            Indicator indicator = indicatorDAO.findById(indicatorId);
741
            if(indicator == null) {
742
                // EXCEPTION - Indicator not found
743
                throw new EntityNotFoundException("Reorder indicators: Indicator with id: " + indicatorId + " not found");
744
            }
745
            indicatorsFull.add(indicator);
746
        }
747

    
748
        sectionDAO.save(section);
749
        log.debug("Indicators reordered!");
750

    
751
        return indicatorsFull;
752
    }
753

    
754
//    @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/{sectionId}/{indicatorId}/toggle-status", method = RequestMethod.POST)
755
//    public Boolean toggleIndicatorStatus(@PathVariable("stakeholderId") String stakeholderId,
756
//                                         @PathVariable("topicId") String topicId,
757
//                                         @PathVariable("categoryId") String categoryId,
758
//                                         @PathVariable("subcategoryId") String subcategoryId,
759
//                                         @PathVariable("sectionId") String sectionId,
760
//                                         @PathVariable("indicatorId") String indicatorId) {
761
//        log.debug("toggle indicator status (isActive)");
762
//        log.debug("Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId+ " - SubCategory: "+subcategoryId + " - Section: "+sectionId+ " - Indicator: "+indicatorId);
763
//
764
//        Indicator indicator = indicatorDAO.findById(indicatorId);
765
//        if (indicator == null) {
766
//            // EXCEPTION - Indicator not found
767
//            throw new EntityNotFoundException("Toggle indicator status: Indicator with id: "+indicatorId+" not found");
768
//        }
769
//        indicator.setIsActive(!indicator.getIsActive());
770
//
771
//        this.toggleIndicator(stakeholderId, topicId, categoryId, subcategoryId, sectionId, indicator);
772
//
773
//        return indicator.getIsActive();
774
//    }
775
//
776
//    @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/{sectionId}/{indicatorId}/toggle-access", method = RequestMethod.POST)
777
//    public Boolean toggleIndicatorAccess(@PathVariable("stakeholderId") String stakeholderId,
778
//                                         @PathVariable("topicId") String topicId,
779
//                                         @PathVariable("categoryId") String categoryId,
780
//                                         @PathVariable("subcategoryId") String subcategoryId,
781
//                                         @PathVariable("sectionId") String sectionId,
782
//                                         @PathVariable("indicatorId") String indicatorId) {
783
//        log.debug("toggle indicator access (isPublic)");
784
//        log.debug("Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId+ " - SubCategory: "+subcategoryId + " - Section: "+sectionId+ " - Indicator: "+indicatorId);
785
//
786
//        Indicator indicator = indicatorDAO.findById(indicatorId);
787
//        if (indicator == null) {
788
//            // EXCEPTION - Indicator not found
789
//            throw new EntityNotFoundException("Toggle indicator access: Indicator with id: "+indicatorId+" not found");
790
//        }
791
//        indicator.setIsPublic(!indicator.getIsPublic());
792
//
793
//        this.toggleIndicator(stakeholderId, topicId, categoryId, subcategoryId, sectionId, indicator);
794
//
795
//        return indicator.getIsPublic();
796
//    }
797

    
798
    @PreAuthorize("isAuthenticated()")
799
    @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/{sectionId}/{indicatorId}/change-visibility", method = RequestMethod.POST)
800
    public Visibility changeTopicVisibility(@PathVariable("stakeholderId") String stakeholderId,
801
                                            @PathVariable("topicId") String topicId,
802
                                            @PathVariable("categoryId") String categoryId,
803
                                            @PathVariable("subcategoryId") String subcategoryId,
804
                                            @PathVariable("sectionId") String sectionId,
805
                                         @PathVariable("indicatorId") String indicatorId,
806
                                            @RequestParam("visibility") Visibility visibility) {
807
        log.debug("change indicator visibility: "+visibility);
808
        log.debug("Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId+ " - SubCategory: "+subcategoryId + " - Section: "+sectionId+ " - Indicator: "+indicatorId);
809

    
810
        Indicator indicator = indicatorDAO.findById(indicatorId);
811
        if (indicator == null) {
812
            // EXCEPTION - Indicator not found
813
            throw new EntityNotFoundException("Change indicator visibility: Indicator with id: "+indicatorId+" not found");
814
        }
815
        indicator.setVisibility(visibility);
816

    
817
        this.toggleIndicator(stakeholderId, topicId, categoryId, subcategoryId, sectionId, indicator);
818

    
819
        return indicator.getVisibility();
820
    }
821

    
822
    public void toggleIndicator(String stakeholderId, String topicId, String categoryId, String subcategoryId, String sectionId, Indicator indicator) {
823
        Section<String> section = checkForExceptions(stakeholderId, topicId, categoryId, subcategoryId, sectionId, indicator.getType());
824
        List<String> indicators = section.getIndicators();
825

    
826
        if(indicators.contains(indicator.getId())) {
827
            indicatorDAO.save(indicator);
828
            log.debug("Indicator toggled!");
829
        } else {
830
            // EXCEPTION - Indicator not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); -> Category: category.getAlias(); -> SubCategory: subCategory.getAlias(); -> Section: section.getTitle();
831
            throw new PathNotValidException("Toggle indicators: Indicator with id: "+indicator.getId()+" not found in Section: "+sectionId);
832
        }
833

    
834
    }
835

    
836
    private Section checkForExceptions(String stakeholderId, String topicId, String categoryId, String subcategoryId, String sectionId, String indicatorType) {
837

    
838
        Stakeholder<String> stakeholder = stakeholderDAO.findById(stakeholderId);
839

    
840
        if(stakeholder == null) {
841
            // EXCEPTION - Stakeholder not found
842
            throw new EntityNotFoundException("Save indicator: Stakeholder with id: " + stakeholderId + " not found");
843
        }
844

    
845
        List<String> roles = rolesUtils.getRoles();
846
        if(!rolesUtils.hasUpdateAuthority(roles, stakeholder.getType(), stakeholder.getAlias())) {
847
            // EXCEPTION - Access denied
848
            throw new ForbiddenException("CheckForExceptions Indicator: You are not authorized to update stakeholder with id: "+stakeholderId);
849
        }
850

    
851
        Topic<String> topic = topicDAO.findById(topicId);
852
        if(topic == null) {
853
            // EXCEPTION - Topic not found
854
            throw new EntityNotFoundException("Save indicator: Topic with id: "+topicId+" not found");
855
        }
856

    
857
        if(!stakeholder.getTopics().contains(topicId)) {
858
            // EXCEPTION - Topic not found in Stakeholder: stakeholder.getAlias();
859
            throw new PathNotValidException("Save indicator: Topic with id: " + topicId + " not found in Stakeholder: " + stakeholderId);
860
        }
861

    
862
        Category<String> category = categoryDAO.findById(categoryId);
863
        if(category == null) {
864
            // EXCEPTION - Category not found
865
            throw new EntityNotFoundException("Save indicator: Category with id: "+categoryId+" not found");
866
        }
867

    
868
        if(!topic.getCategories().contains(categoryId)) {
869
            // EXCEPTION - Category not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias();
870
            throw new PathNotValidException("Save indicator: Category with id: "+categoryId+" not found in Topic: "+topicId);
871
        }
872

    
873
        SubCategory<String> subcategory = subCategoryDAO.findById(subcategoryId);
874
        if(subcategory == null) {
875
            // EXCEPTION - SubCategory not found
876
            throw new EntityNotFoundException("Save indicator: SubCategory with id: "+subcategoryId+" not found");
877
        }
878

    
879
        if (!category.getSubCategories().contains(subcategoryId)) {
880
            // EXCEPTION - SubCategory not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); -> Category: category.getAlias();
881
            throw new PathNotValidException("Save indicator: SubCategory with id: "+subcategoryId+" not found in Category: "+categoryId);
882
        }
883

    
884
        Section<String> section = sectionDAO.findById(sectionId);
885
        if(section == null) {
886
            // EXCEPTION - Section not found
887
            throw new EntityNotFoundException("Save indicator: Section with id: "+sectionId+" not found");
888
        }
889

    
890
        if(indicatorType.equals("chart")) {
891
            if (!subcategory.getCharts().contains(sectionId)) {
892
                // EXCEPTION - Section not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); -> Category: category.getAlias(); -> SubCategory: subcategory.getAlias();
893
                throw new PathNotValidException("Save indicator: SubCategory with id: " + subcategoryId + " not found in Category: " + categoryId);
894
            }
895
        } else if(indicatorType.equals("number")) {
896
            if (!subcategory.getNumbers().contains(sectionId)) {
897
                // EXCEPTION - Section not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); -> Category: category.getAlias(); -> SubCategory: subcategory.getAlias();
898
                throw new PathNotValidException("Save indicator: SubCategory with id: " + subcategoryId + " not found in Category: " + categoryId);
899
            }
900
        }
901

    
902
        return  section;
903
    }
904

    
905
    public void deleteTree(Section section) {
906
        List<String> indicators = section.getIndicators();
907
        for(String indicatorId : indicators) {
908
            indicatorDAO.delete(indicatorId);
909
        }
910
    }
911

    
912
    public void disConnectTree(Section section) {
913
        List<String> indicators = section.getIndicators();
914
        for(String indicatorId : indicators) {
915
            Indicator indicator = indicatorDAO.findById(indicatorId);
916
            indicator.setDefaultId(null);
917
            indicatorDAO.save(indicator);
918
        }
919
    }
920
}
(3-3/10)