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 eu.dnetlib.uoamonitorservice.handlers.utils.RolesUtils;
|
9
|
import org.apache.log4j.Logger;
|
10
|
import org.springframework.beans.factory.annotation.Autowired;
|
11
|
import org.springframework.security.access.AccessDeniedException;
|
12
|
import org.springframework.security.access.prepost.PreAuthorize;
|
13
|
import org.springframework.web.bind.annotation.*;
|
14
|
|
15
|
import java.io.UnsupportedEncodingException;
|
16
|
import java.net.URLEncoder;
|
17
|
import java.util.*;
|
18
|
|
19
|
@RestController
|
20
|
@CrossOrigin(origins = "*")
|
21
|
public class IndicatorController {
|
22
|
private final Logger log = Logger.getLogger(this.getClass());
|
23
|
|
24
|
@Autowired
|
25
|
private RolesUtils rolesUtils;
|
26
|
|
27
|
@Autowired
|
28
|
private StakeholderDAO stakeholderDAO;
|
29
|
|
30
|
@Autowired
|
31
|
private TopicDAO topicDAO;
|
32
|
|
33
|
@Autowired
|
34
|
private CategoryDAO categoryDAO;
|
35
|
|
36
|
@Autowired
|
37
|
private SubCategoryDAO subCategoryDAO;
|
38
|
|
39
|
@Autowired
|
40
|
private SectionDAO sectionDAO;
|
41
|
|
42
|
@Autowired
|
43
|
private IndicatorDAO indicatorDAO;
|
44
|
|
45
|
|
46
|
@PreAuthorize("isAuthenticated()")
|
47
|
@RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/{sectionId}/save", method = RequestMethod.POST)
|
48
|
public Indicator saveIndicator(@PathVariable("stakeholderId") String stakeholderId,
|
49
|
@PathVariable("topicId") String topicId,
|
50
|
@PathVariable("categoryId") String categoryId,
|
51
|
@PathVariable("subcategoryId") String subcategoryId,
|
52
|
@PathVariable("sectionId") String sectionId,
|
53
|
@RequestBody Indicator indicator) throws UnsupportedEncodingException {
|
54
|
log.debug("save indicator");
|
55
|
log.debug("Name: "+indicator.getName() + " - Id: "+indicator.getId() + " - Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId+ " - SubCategory: "+subcategoryId + " - Section: "+sectionId);
|
56
|
|
57
|
Section<String> section = checkForExceptions(stakeholderId, topicId, categoryId, subcategoryId, sectionId, indicator.getType());
|
58
|
|
59
|
Date date = new Date();
|
60
|
indicator.setUpdateDate(date);
|
61
|
|
62
|
Indicator oldIndicator = null;
|
63
|
if(indicator.getId() != null) {
|
64
|
oldIndicator = indicatorDAO.findById(indicator.getId());
|
65
|
} else { // indicator does not exist in DB
|
66
|
indicator.setCreationDate(date);
|
67
|
}
|
68
|
|
69
|
String indicatorId = indicator.getId();
|
70
|
|
71
|
Stakeholder<String> stakeholder = stakeholderDAO.findById(stakeholderId);
|
72
|
// this indicator belongs in default profile and it is new or it is updated
|
73
|
if(stakeholder.getDefaultId() == null) {
|
74
|
if(indicatorId == null) {
|
75
|
indicatorDAO.save(indicator);
|
76
|
onSaveDefaultIndicator(indicator, sectionId);
|
77
|
}
|
78
|
else {
|
79
|
onUpdateDefaultIndicator(indicator, stakeholder, oldIndicator);
|
80
|
indicatorDAO.save(indicator);
|
81
|
}
|
82
|
} else {
|
83
|
indicatorDAO.save(indicator);
|
84
|
}
|
85
|
|
86
|
List<String> indicators = section.getIndicators();
|
87
|
|
88
|
int index = indicators.indexOf(indicator.getId());
|
89
|
if (index == -1) {
|
90
|
indicators.add(indicator.getId());
|
91
|
sectionDAO.save(section);
|
92
|
log.debug("Indicator saved!");
|
93
|
}
|
94
|
|
95
|
return indicator;
|
96
|
}
|
97
|
|
98
|
public void onSaveDefaultIndicator(Indicator indicator, String defaultSectionId) throws UnsupportedEncodingException {
|
99
|
log.debug("On save default indicator");
|
100
|
|
101
|
// new indicator in default profile - add it on profiles of the same type
|
102
|
List<Section> sections = sectionDAO.findByDefaultId(defaultSectionId);
|
103
|
|
104
|
for (Section section : sections) {
|
105
|
Indicator indicatorNew = new Indicator();
|
106
|
indicatorNew.copyFromDefault(indicator);
|
107
|
for (IndicatorPath indicatorPath : indicatorNew.getIndicatorPaths()) {
|
108
|
Stakeholder stakeholder = stakeholderDAO.findByAlias(section.getStakeholderAlias());
|
109
|
parameterMapping(indicatorPath, stakeholder);
|
110
|
}
|
111
|
|
112
|
indicatorDAO.save(indicatorNew);
|
113
|
|
114
|
List<String> indicators = section.getIndicators();
|
115
|
indicators.add(indicatorNew.getId());
|
116
|
|
117
|
sectionDAO.save(section);
|
118
|
}
|
119
|
}
|
120
|
|
121
|
public void onUpdateDefaultIndicator(Indicator indicator, Stakeholder stakeholder, Indicator oldIndicator) throws UnsupportedEncodingException {
|
122
|
log.debug("On update default indicator");
|
123
|
|
124
|
// indicator already exists - check if changed and update all indicators based on it
|
125
|
|
126
|
boolean changed;
|
127
|
List<Indicator> indicators = indicatorDAO.findByDefaultId(indicator.getId());
|
128
|
|
129
|
for(Indicator indicatorBasedOnDefault : indicators) {
|
130
|
changed = false;
|
131
|
|
132
|
if(indicator.getName() != null && !indicator.getName().equals(indicatorBasedOnDefault.getName())
|
133
|
&& (oldIndicator.getName() == null || oldIndicator.getName().equals(indicatorBasedOnDefault.getName()))) {
|
134
|
|
135
|
indicatorBasedOnDefault.setName(indicator.getName());
|
136
|
changed = true;
|
137
|
}
|
138
|
|
139
|
if(indicator.getDescription() != null && !indicator.getDescription().equals(indicatorBasedOnDefault.getDescription())) {
|
140
|
|
141
|
indicatorBasedOnDefault.setDescription(indicator.getDescription());
|
142
|
changed = true;
|
143
|
}
|
144
|
|
145
|
if(indicator.getAdditionalDescription() != null && !indicator.getAdditionalDescription().equals(indicatorBasedOnDefault.getAdditionalDescription())
|
146
|
&& (oldIndicator.getAdditionalDescription() == null || oldIndicator.getAdditionalDescription().equals(indicatorBasedOnDefault.getAdditionalDescription()))) {
|
147
|
|
148
|
indicatorBasedOnDefault.setAdditionalDescription(indicator.getAdditionalDescription());
|
149
|
changed = true;
|
150
|
}
|
151
|
|
152
|
int i = 0;
|
153
|
List<IndicatorPath> indicatorPaths = indicatorBasedOnDefault.getIndicatorPaths();
|
154
|
if(indicatorPaths == null && indicator.getIndicatorPaths() != null) {
|
155
|
indicatorPaths = new ArrayList<>();
|
156
|
}
|
157
|
|
158
|
for (IndicatorPath indicatorPath : indicator.getIndicatorPaths()) {
|
159
|
IndicatorPath indicatorPathBasedOnDefault = null;
|
160
|
if(i < indicatorPaths.size()) {
|
161
|
indicatorPathBasedOnDefault = indicatorPaths.get(i);
|
162
|
}
|
163
|
|
164
|
if(indicatorPathBasedOnDefault == null) {
|
165
|
// Add new indicator path in existing indicators
|
166
|
IndicatorPath indicatorPathNew = new IndicatorPath(indicatorPath);
|
167
|
parameterMapping(indicatorPathNew, stakeholder);
|
168
|
indicatorPaths.add(indicatorPathNew);
|
169
|
changed = true;
|
170
|
} else {
|
171
|
IndicatorPath oldIndicatorPath = oldIndicator.getIndicatorPaths().get(i);
|
172
|
|
173
|
// Check if there are changes in indicator path and update existing indicators if needed
|
174
|
log.debug("update indicator path: "+i + " (indicator id: "+indicatorBasedOnDefault.getId()+")");
|
175
|
|
176
|
if(indicatorPath.getType() != null
|
177
|
&& !indicatorPath.getType().equals(indicatorPathBasedOnDefault.getType())
|
178
|
&& (oldIndicatorPath.getType().equals(indicatorPathBasedOnDefault.getType()))) {
|
179
|
|
180
|
indicatorPathBasedOnDefault.setType(indicatorPath.getType());
|
181
|
changed = true; // parameter "type" needs to be changed as well
|
182
|
}
|
183
|
log.debug("After type check: "+changed);
|
184
|
|
185
|
if(indicatorPath.getSource() != null
|
186
|
&& !indicatorPath.getSource().equals(indicatorPathBasedOnDefault.getSource())
|
187
|
&& (oldIndicatorPath.getSource().equals(indicatorPathBasedOnDefault.getSource()))) {
|
188
|
|
189
|
indicatorPathBasedOnDefault.setSource(indicatorPath.getSource());
|
190
|
changed = true;
|
191
|
}
|
192
|
log.debug("After source check: "+changed);
|
193
|
|
194
|
if(indicatorPath.getUrl() != null
|
195
|
&& !indicatorPath.getUrl().equals(indicatorPathBasedOnDefault.getUrl())
|
196
|
&& (oldIndicatorPath.getUrl().equals(indicatorPathBasedOnDefault.getUrl()))) {
|
197
|
|
198
|
indicatorPathBasedOnDefault.setUrl(indicatorPath.getUrl());
|
199
|
changed = true;
|
200
|
}
|
201
|
log.debug("After url check: "+changed);
|
202
|
|
203
|
if(indicatorPath.getChartObject() != null
|
204
|
&& !indicatorPath.getChartObject().equals(indicatorPathBasedOnDefault.getChartObject())
|
205
|
&& (oldIndicatorPath.getChartObject().equals(indicatorPathBasedOnDefault.getChartObject()))) {
|
206
|
|
207
|
indicatorPathBasedOnDefault.setChartObject(indicatorPath.getChartObject());
|
208
|
changed = true;
|
209
|
}
|
210
|
log.debug("After chartObject check: "+changed);
|
211
|
|
212
|
if(indicatorPath.getParameters() != null) {
|
213
|
if (indicatorPathBasedOnDefault.getParameters() == null) {
|
214
|
indicatorPathBasedOnDefault.setParameters(new HashMap<>());
|
215
|
}
|
216
|
//if (indicatorPath.getParameters().size() != indicatorPathBasedOnDefault.getParameters().size()) {
|
217
|
//log.debug("Different number of parameters");
|
218
|
for (Map.Entry<String, String> parameter : indicatorPath.getParameters().entrySet()) {
|
219
|
log.debug("\nindicatorPath: parameter.getKey(): "+parameter.getKey()+" - value: "+parameter.getValue()
|
220
|
+"\nindicatorPathBasedOnDefault:parameters:key: "+ indicatorPathBasedOnDefault.getParameters().get(parameter.getKey())
|
221
|
+"\noldIndicatorPath:parameters:key: "+ (oldIndicatorPath.getParameters() == null ? "null" : oldIndicatorPath.getParameters().get(parameter.getKey())));
|
222
|
if (!indicatorPathBasedOnDefault.getParameters().containsKey(parameter.getKey())
|
223
|
|| (oldIndicatorPath.getParameters() == null
|
224
|
|| (oldIndicatorPath.getParameters().get(parameter.getKey()).equals(indicatorPathBasedOnDefault.getParameters().get(parameter.getKey()))
|
225
|
&& !parameter.getValue().equals(indicatorPathBasedOnDefault.getParameters().get(parameter.getKey()))))
|
226
|
) {
|
227
|
indicatorPathBasedOnDefault.getParameters().put(parameter.getKey(), parameter.getValue());
|
228
|
changed = true;
|
229
|
}
|
230
|
// else if(parameter.getKey().equals("type")) {
|
231
|
// indicatorPathBasedOnDefault.getParameters().put(parameter.getKey(), parameter.getValue());
|
232
|
// changed = true;
|
233
|
// }
|
234
|
}
|
235
|
// TODO when deleting indicator path parameters... ???
|
236
|
parameterMapping(indicatorPathBasedOnDefault, stakeholder);
|
237
|
//}
|
238
|
}
|
239
|
log.debug("After parameters check: " + changed);
|
240
|
|
241
|
if(indicatorPath.getJsonPath() != null) {
|
242
|
boolean jsonPathChanged = false;
|
243
|
boolean breaked = false;
|
244
|
|
245
|
int oldJsonPathSize = 0;
|
246
|
if(oldIndicatorPath.getJsonPath() != null) {
|
247
|
oldJsonPathSize = oldIndicatorPath.getJsonPath().size();
|
248
|
}
|
249
|
int basedOnDefaultJsonPathSize = 0;
|
250
|
if(indicatorPathBasedOnDefault.getJsonPath() != null) {
|
251
|
basedOnDefaultJsonPathSize = indicatorPathBasedOnDefault.getJsonPath().size();
|
252
|
}
|
253
|
log.debug("old: "+oldJsonPathSize+" - based on default: "+basedOnDefaultJsonPathSize+" - new: "+indicatorPath.getJsonPath().size());
|
254
|
if(oldJsonPathSize == basedOnDefaultJsonPathSize) {
|
255
|
if(indicatorPathBasedOnDefault.getJsonPath() == null && indicatorPath.getJsonPath().size() > 0) {
|
256
|
indicatorPathBasedOnDefault.setJsonPath(new ArrayList<>());
|
257
|
}
|
258
|
|
259
|
int basedOnDefaultIndex = 0;
|
260
|
int oldIndex = 0;
|
261
|
|
262
|
Iterator<String> jsonStringBasedOnDefaultIterator = indicatorPathBasedOnDefault.getJsonPath().iterator();
|
263
|
while (jsonStringBasedOnDefaultIterator.hasNext()) {
|
264
|
String jsonStringBasedOnDefault = jsonStringBasedOnDefaultIterator.next();
|
265
|
if(oldIndicatorPath.getJsonPath().get(oldIndex).equals(jsonStringBasedOnDefault)) {
|
266
|
if(basedOnDefaultIndex >= indicatorPath.getJsonPath().size()) { // string deleted
|
267
|
jsonStringBasedOnDefaultIterator.remove();
|
268
|
jsonPathChanged = true;
|
269
|
} else { // check if string changed
|
270
|
if(!indicatorPath.getJsonPath().get(basedOnDefaultIndex).equals(jsonStringBasedOnDefault)) {
|
271
|
indicatorPathBasedOnDefault.getJsonPath().set(basedOnDefaultIndex, indicatorPath.getJsonPath().get(basedOnDefaultIndex));
|
272
|
jsonPathChanged = true;
|
273
|
}
|
274
|
basedOnDefaultIndex++;
|
275
|
}
|
276
|
oldIndex++;
|
277
|
} else {
|
278
|
breaked = true;
|
279
|
jsonPathChanged = false;
|
280
|
log.debug("not the same: "+oldIndex);
|
281
|
break;
|
282
|
}
|
283
|
}
|
284
|
|
285
|
int index=0;
|
286
|
if(!breaked && indicatorPath.getJsonPath().size() > indicatorPathBasedOnDefault.getJsonPath().size()) { // strings added
|
287
|
jsonPathChanged = true;
|
288
|
for(index=indicatorPathBasedOnDefault.getJsonPath().size(); index < indicatorPath.getJsonPath().size(); index++) {
|
289
|
indicatorPathBasedOnDefault.getJsonPath().add(indicatorPath.getJsonPath().get(index));
|
290
|
}
|
291
|
}
|
292
|
|
293
|
if(jsonPathChanged) {
|
294
|
changed = true;
|
295
|
}
|
296
|
}
|
297
|
// TODO when deleting indicator path json path strings...
|
298
|
}
|
299
|
log.debug("After jsonPath check: " + changed);
|
300
|
}
|
301
|
i++;
|
302
|
}
|
303
|
// TODO when deleting indicator paths...
|
304
|
|
305
|
if(!changed) {
|
306
|
// break;
|
307
|
continue;
|
308
|
}
|
309
|
|
310
|
indicatorBasedOnDefault.setUpdateDate(indicator.getUpdateDate());
|
311
|
indicatorDAO.save(indicatorBasedOnDefault);
|
312
|
}
|
313
|
}
|
314
|
|
315
|
public void parameterMapping(IndicatorPath indicatorPath, Stakeholder stakeholder) throws UnsupportedEncodingException {
|
316
|
if (indicatorPath.getParameters() != null) {
|
317
|
if (indicatorPath.getParameters().containsKey("index_name")) {
|
318
|
indicatorPath.getParameters().put("index_name", stakeholder.getIndex_name());
|
319
|
} else if (indicatorPath.getParameters().containsKey("index_shortName")) {
|
320
|
indicatorPath.getParameters().put("index_shortName", stakeholder.getIndex_name().toLowerCase());
|
321
|
} else if (indicatorPath.getParameters().containsKey("index_id")) {
|
322
|
indicatorPath.getParameters().put("index_id", stakeholder.getIndex_id());
|
323
|
}
|
324
|
}
|
325
|
|
326
|
// // url encoding for number indicators
|
327
|
// String url = indicatorPath.getUrl();
|
328
|
// String encoded_index_id = urlEncode(URLEncoder.encode(stakeholder.getIndex_id(), "UTF-8"));
|
329
|
// url = url.replace("index_id", encoded_index_id);
|
330
|
// String encoded_index_name = urlEncode(URLEncoder.encode(stakeholder.getIndex_name(), "UTF-8"));
|
331
|
// url = url.replace("index_name", encoded_index_name);
|
332
|
// String encoded_index_shortName = urlEncode(URLEncoder.encode(stakeholder.getIndex_shortName(), "UTF-8"));
|
333
|
// url = url.replace("index_shortName", encoded_index_shortName);
|
334
|
// indicatorPath.setUrl(url);
|
335
|
}
|
336
|
|
337
|
public String urlEncode(String encodedIndicatorPathField) {
|
338
|
String indicatorPathField = "";
|
339
|
|
340
|
for( int i=0; i<encodedIndicatorPathField.length(); i++ ){
|
341
|
String character = encodedIndicatorPathField.substring(i, i+1);
|
342
|
|
343
|
if(character.equals("+")) {
|
344
|
indicatorPathField = indicatorPathField.concat("%20");
|
345
|
} else if(character.equals("%")) {
|
346
|
//grab the hex in pairs
|
347
|
String output = encodedIndicatorPathField.substring(i+1, (i + 3));
|
348
|
|
349
|
if(output.equals("7E") || output.equals("27") || output.equals("28") || output.equals("29") || output.equals("21")) {
|
350
|
//convert hex to decimal
|
351
|
int decimal = Integer.parseInt(output, 16);
|
352
|
//convert the decimal to character
|
353
|
StringBuilder sb = new StringBuilder();
|
354
|
sb.append((char) decimal);
|
355
|
|
356
|
indicatorPathField = indicatorPathField.concat(sb.toString());
|
357
|
} else {
|
358
|
indicatorPathField = indicatorPathField.concat(character + output);
|
359
|
}
|
360
|
|
361
|
i += 2;
|
362
|
} else {
|
363
|
indicatorPathField = indicatorPathField.concat(character);
|
364
|
}
|
365
|
}
|
366
|
|
367
|
return indicatorPathField;
|
368
|
}
|
369
|
|
370
|
@PreAuthorize("isAuthenticated()")
|
371
|
@RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/{sectionId}/{indicatorId}/delete", method = RequestMethod.DELETE)
|
372
|
public boolean deleteIndicator(@PathVariable("stakeholderId") String stakeholderId,
|
373
|
@PathVariable("topicId") String topicId,
|
374
|
@PathVariable("categoryId") String categoryId,
|
375
|
@PathVariable("subcategoryId") String subcategoryId,
|
376
|
@PathVariable("sectionId") String sectionId,
|
377
|
@PathVariable("indicatorId") String indicatorId,
|
378
|
@RequestParam(required = false) String children) {
|
379
|
log.debug("delete indicator");
|
380
|
log.debug("Id: "+indicatorId + " - Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId+ " - SubCategory: "+subcategoryId + " - Section: "+sectionId);
|
381
|
|
382
|
Indicator indicator = indicatorDAO.findById(indicatorId);
|
383
|
if(indicator != null) {
|
384
|
Section<String> section = checkForExceptions(stakeholderId, topicId, categoryId, subcategoryId, sectionId, indicator.getType());
|
385
|
|
386
|
Stakeholder<String> stakeholder = stakeholderDAO.findById(stakeholderId);
|
387
|
List<String> roles = rolesUtils.getRoles();
|
388
|
if(indicator.getDefaultId() != null && !rolesUtils.hasCreateAndDeleteAuthority(roles, stakeholder.getType())) {
|
389
|
// EXCEPTION - Access denied
|
390
|
throw new AccessDeniedException("Delete indicator: You are not authorized to delete a default Indicator in stakeholder with id: "+stakeholderId);
|
391
|
}
|
392
|
|
393
|
List<String> indicators = section.getIndicators();
|
394
|
|
395
|
int index = indicators.indexOf(indicatorId);
|
396
|
if (index != -1) {
|
397
|
|
398
|
// this indicator belongs in default profile
|
399
|
if(section.getDefaultId() == null && children != null) {
|
400
|
onDeleteDefaultIndicator(indicatorId, sectionId, children);
|
401
|
}
|
402
|
|
403
|
|
404
|
indicators.remove(index);
|
405
|
sectionDAO.save(section);
|
406
|
|
407
|
indicatorDAO.delete(indicatorId);
|
408
|
log.debug("Indicator deleted!");
|
409
|
} else {
|
410
|
// EXCEPTION - Indicator not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); -> Category: category.getAlias(); -> SubCategory: subcategory.getAlias(); -> Section: section.getTitle();
|
411
|
throw new PathNotValidException("Delete indicator: Indicator with id: "+indicatorId+" not found in Sectiom: "+sectionId);
|
412
|
}
|
413
|
} else {
|
414
|
// EXCEPTION - Indicator not found
|
415
|
throw new EntityNotFoundException("Delete indicator: Indicator with id: "+indicatorId+" not found");
|
416
|
}
|
417
|
return true;
|
418
|
}
|
419
|
|
420
|
public boolean onDeleteDefaultIndicator(String defaultIndicatorId, String defaultSectionId, String children) {
|
421
|
if(children.equals("delete")) {
|
422
|
// // 1st way
|
423
|
// List<Section> sections = sectionDAO.findByDefaultId(defaultSectionId);
|
424
|
//
|
425
|
// for(Section section : sections) {
|
426
|
// List<String> indicators = section.getIndicators();
|
427
|
//
|
428
|
// Iterator<String> indicatorsIterator = indicators.iterator();
|
429
|
// while(indicatorsIterator.hasNext()) {
|
430
|
// String indicatorId = indicatorsIterator.next();
|
431
|
//
|
432
|
// Indicator indicator = indicatorDAO.findById(indicatorId);
|
433
|
// if (indicator.getDefaultId().equals(defaultIndicatorId)) {
|
434
|
// indicatorsIterator.remove();
|
435
|
// sectionDAO.save(section);
|
436
|
//
|
437
|
// indicatorDAO.delete(indicatorId);
|
438
|
// log.debug("Indicator deleted!");
|
439
|
//
|
440
|
// break;
|
441
|
// }
|
442
|
// }
|
443
|
// }
|
444
|
|
445
|
// 2nd way
|
446
|
List<Section> sections = sectionDAO.findByDefaultId(defaultSectionId);
|
447
|
List<Indicator> indicators = indicatorDAO.findByDefaultId(defaultIndicatorId);
|
448
|
|
449
|
for(Section section : sections) {
|
450
|
Iterator<Indicator> indicatorsIterator = indicators.iterator();
|
451
|
while(indicatorsIterator.hasNext()) {
|
452
|
String indicatorId = indicatorsIterator.next().getId();
|
453
|
if(section.getIndicators().contains(indicatorId)) {
|
454
|
indicatorsIterator.remove();
|
455
|
|
456
|
section.getIndicators().remove(indicatorId);
|
457
|
sectionDAO.save(section);
|
458
|
|
459
|
indicatorDAO.delete(indicatorId);
|
460
|
log.debug("Indicator with id: "+indicatorId+" deleted!");
|
461
|
|
462
|
break;
|
463
|
}
|
464
|
}
|
465
|
}
|
466
|
|
467
|
// // 3rd way - parentId
|
468
|
// List<Indicator> indicators = indicatorDAO.findByDefaultId(defaultIndicatorId);
|
469
|
// for(Indicator indicator : indicators) {
|
470
|
// Section section = sectionDAO.findById(indicator.getParent());
|
471
|
// List<String> sectionIndicators = section.getIndicators();
|
472
|
//
|
473
|
// sectionIndicators.remove(indicator.getId());
|
474
|
// sectionDAO.save(section);
|
475
|
//
|
476
|
// indicatorDAO.delete(indicator.getId());
|
477
|
// log.debug("Indicator deleted!");
|
478
|
// }
|
479
|
} else if(children.equals("disconnect")) {
|
480
|
List<Indicator> indicators = indicatorDAO.findByDefaultId(defaultIndicatorId);
|
481
|
for(Indicator indicator : indicators) {
|
482
|
indicator.setDefaultId(null);
|
483
|
indicatorDAO.save(indicator);
|
484
|
log.debug("DefaultId for Indicator with id: "+indicator.getId()+" empty!");
|
485
|
}
|
486
|
}
|
487
|
return true;
|
488
|
}
|
489
|
|
490
|
// @RequestMapping(value = "/{stakeholderId}/charts/delete", method = RequestMethod.DELETE)
|
491
|
// public boolean deleteAllChartIndicators(@PathVariable("stakeholderId") String stakeholderId) {
|
492
|
// log.debug("delete all chart indicators of stakeholder");
|
493
|
// log.debug("Stakeholder: "+stakeholderId);
|
494
|
//
|
495
|
// Stakeholder<String> stakeholder = stakeholderDAO.findById(stakeholderId);
|
496
|
// if(stakeholder != null) {
|
497
|
//
|
498
|
// for(String topicId : stakeholder.getTopics()) {
|
499
|
// Topic<String> topic = topicDAO.findById(topicId);
|
500
|
// if(topic != null) {
|
501
|
// for(String categoryId : topic.getCategories()) {
|
502
|
// Category<String> category = categoryDAO.findById(categoryId);
|
503
|
// if(category != null) {
|
504
|
// for(String subcategoryId : category.getSubCategories()) {
|
505
|
// SubCategory<String> subcategory = subCategoryDAO.findById(subcategoryId);
|
506
|
// if(subcategory != null) {
|
507
|
//
|
508
|
// for(String sectionId : subcategory.getCharts()) {
|
509
|
// Section<String> section = sectionDAO.findById(sectionId);
|
510
|
// if (section != null) {
|
511
|
//
|
512
|
// List<String> indicators = section.getIndicators();
|
513
|
// Iterator<String> indicatorsIterator = section.getIndicators().iterator();
|
514
|
//
|
515
|
// while (indicatorsIterator.hasNext()) {
|
516
|
// String indicatorId = indicatorsIterator.next();
|
517
|
// Indicator indicator = indicatorDAO.findById(indicatorId);
|
518
|
// if (indicator != null) {
|
519
|
// int index = indicators.indexOf(indicatorId);
|
520
|
// if (index != -1) {
|
521
|
// indicatorsIterator.remove();
|
522
|
// //indicators.remove(index);
|
523
|
//
|
524
|
// indicatorDAO.delete(indicatorId);
|
525
|
// log.debug("Indicator deleted!");
|
526
|
// } else {
|
527
|
// // EXCEPTION - Indicator not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); -> Category: category.getAlias(); -> SubCategory: subcategory.getAlias(); -> Section: section.getTitle();
|
528
|
// throw new PathNotValidException("Delete indicator: Indicator with id: " + indicatorId + " not found in Section: " + sectionId);
|
529
|
// }
|
530
|
// } else {
|
531
|
// // EXCEPTION - Indicator not found
|
532
|
// throw new EntityNotFoundException("Delete indicator: Indicator with id: " + indicatorId + " not found");
|
533
|
// }
|
534
|
// }
|
535
|
// sectionDAO.save(section);
|
536
|
// } else {
|
537
|
// // EXCEPTION - Section not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); -> Category: category.getAlias(); -> SubCategory: subcategory.getAlias();
|
538
|
// throw new PathNotValidException("Delete indicator: Section with id: " + sectionId + " not found in SubCategory: " + subcategoryId);
|
539
|
// }
|
540
|
// }
|
541
|
// } else {
|
542
|
// // EXCEPTION - SubCategory not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); -> Category: category.getAlias();
|
543
|
// throw new PathNotValidException("Delete indicator: SubCategory with id: "+subcategoryId+" not found in Category: "+categoryId);
|
544
|
// }
|
545
|
// }
|
546
|
// } else {
|
547
|
// // EXCEPTION - Category not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias();
|
548
|
// throw new PathNotValidException("Delete indicator: Category with id: "+categoryId+" not found in Topic: "+topicId);
|
549
|
// }
|
550
|
// }
|
551
|
// } else {
|
552
|
// // EXCEPTION - Topic not found in Stakeholder: stakeholder.getAlias();
|
553
|
// throw new PathNotValidException("Delete indicator: Topic with id: "+topicId+" not found in Stakeholder: "+stakeholderId);
|
554
|
// }
|
555
|
// }
|
556
|
// } else {
|
557
|
// // EXCEPTION - Stakeholder not found
|
558
|
// throw new EntityNotFoundException("Delete indicator: Stakeholder with id: "+stakeholderId+" not found");
|
559
|
// }
|
560
|
// return true;
|
561
|
// }
|
562
|
|
563
|
@PreAuthorize("isAuthenticated()")
|
564
|
@RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/{sectionId}/{type}/reorder", method = RequestMethod.POST)
|
565
|
public List<Indicator> reorderIndicators(@PathVariable("stakeholderId") String stakeholderId,
|
566
|
@PathVariable("topicId") String topicId,
|
567
|
@PathVariable("categoryId") String categoryId,
|
568
|
@PathVariable("subcategoryId") String subcategoryId,
|
569
|
@PathVariable("sectionId") String sectionId,
|
570
|
@PathVariable("type") String type,
|
571
|
@RequestBody List<String> indicators) {
|
572
|
log.debug("reorder indicators of type: "+type);
|
573
|
log.debug("Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId+ " - SubCategory: "+subcategoryId + " - Section: "+sectionId);
|
574
|
|
575
|
Section<String> section = checkForExceptions(stakeholderId, topicId, categoryId, subcategoryId, sectionId, type);
|
576
|
|
577
|
section.setIndicators(indicators);
|
578
|
|
579
|
sectionDAO.save(section);
|
580
|
log.debug("Indicators reordered!");
|
581
|
|
582
|
List<Indicator> indicatorsFull = new ArrayList<>();
|
583
|
for(String indicatorId : indicators) {
|
584
|
indicatorsFull.add(indicatorDAO.findById(indicatorId));
|
585
|
}
|
586
|
return indicatorsFull;
|
587
|
}
|
588
|
|
589
|
// @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/{sectionId}/{indicatorId}/toggle-status", method = RequestMethod.POST)
|
590
|
// public Boolean toggleIndicatorStatus(@PathVariable("stakeholderId") String stakeholderId,
|
591
|
// @PathVariable("topicId") String topicId,
|
592
|
// @PathVariable("categoryId") String categoryId,
|
593
|
// @PathVariable("subcategoryId") String subcategoryId,
|
594
|
// @PathVariable("sectionId") String sectionId,
|
595
|
// @PathVariable("indicatorId") String indicatorId) {
|
596
|
// log.debug("toggle indicator status (isActive)");
|
597
|
// log.debug("Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId+ " - SubCategory: "+subcategoryId + " - Section: "+sectionId+ " - Indicator: "+indicatorId);
|
598
|
//
|
599
|
// Indicator indicator = indicatorDAO.findById(indicatorId);
|
600
|
// if (indicator == null) {
|
601
|
// // EXCEPTION - Indicator not found
|
602
|
// throw new EntityNotFoundException("Toggle indicator status: Indicator with id: "+indicatorId+" not found");
|
603
|
// }
|
604
|
// indicator.setIsActive(!indicator.getIsActive());
|
605
|
//
|
606
|
// this.toggleIndicator(stakeholderId, topicId, categoryId, subcategoryId, sectionId, indicator);
|
607
|
//
|
608
|
// return indicator.getIsActive();
|
609
|
// }
|
610
|
//
|
611
|
// @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/{sectionId}/{indicatorId}/toggle-access", method = RequestMethod.POST)
|
612
|
// public Boolean toggleIndicatorAccess(@PathVariable("stakeholderId") String stakeholderId,
|
613
|
// @PathVariable("topicId") String topicId,
|
614
|
// @PathVariable("categoryId") String categoryId,
|
615
|
// @PathVariable("subcategoryId") String subcategoryId,
|
616
|
// @PathVariable("sectionId") String sectionId,
|
617
|
// @PathVariable("indicatorId") String indicatorId) {
|
618
|
// log.debug("toggle indicator access (isPublic)");
|
619
|
// log.debug("Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId+ " - SubCategory: "+subcategoryId + " - Section: "+sectionId+ " - Indicator: "+indicatorId);
|
620
|
//
|
621
|
// Indicator indicator = indicatorDAO.findById(indicatorId);
|
622
|
// if (indicator == null) {
|
623
|
// // EXCEPTION - Indicator not found
|
624
|
// throw new EntityNotFoundException("Toggle indicator access: Indicator with id: "+indicatorId+" not found");
|
625
|
// }
|
626
|
// indicator.setIsPublic(!indicator.getIsPublic());
|
627
|
//
|
628
|
// this.toggleIndicator(stakeholderId, topicId, categoryId, subcategoryId, sectionId, indicator);
|
629
|
//
|
630
|
// return indicator.getIsPublic();
|
631
|
// }
|
632
|
|
633
|
@PreAuthorize("isAuthenticated()")
|
634
|
@RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/{sectionId}/{indicatorId}/change-visibility", method = RequestMethod.POST)
|
635
|
public Visibility changeTopicVisibility(@PathVariable("stakeholderId") String stakeholderId,
|
636
|
@PathVariable("topicId") String topicId,
|
637
|
@PathVariable("categoryId") String categoryId,
|
638
|
@PathVariable("subcategoryId") String subcategoryId,
|
639
|
@PathVariable("sectionId") String sectionId,
|
640
|
@PathVariable("indicatorId") String indicatorId,
|
641
|
@RequestParam("visibility") Visibility visibility) {
|
642
|
log.debug("change indicator visibility: "+visibility);
|
643
|
log.debug("Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId+ " - SubCategory: "+subcategoryId + " - Section: "+sectionId+ " - Indicator: "+indicatorId);
|
644
|
|
645
|
Indicator indicator = indicatorDAO.findById(indicatorId);
|
646
|
if (indicator == null) {
|
647
|
// EXCEPTION - Indicator not found
|
648
|
throw new EntityNotFoundException("Change indicator visibility: Indicator with id: "+indicatorId+" not found");
|
649
|
}
|
650
|
indicator.setVisibility(visibility);
|
651
|
|
652
|
this.toggleIndicator(stakeholderId, topicId, categoryId, subcategoryId, sectionId, indicator);
|
653
|
|
654
|
return indicator.getVisibility();
|
655
|
}
|
656
|
|
657
|
public void toggleIndicator(String stakeholderId, String topicId, String categoryId, String subcategoryId, String sectionId, Indicator indicator) {
|
658
|
Section<String> section = checkForExceptions(stakeholderId, topicId, categoryId, subcategoryId, sectionId, indicator.getType());
|
659
|
List<String> indicators = section.getIndicators();
|
660
|
|
661
|
if(indicators.contains(indicator.getId())) {
|
662
|
indicatorDAO.save(indicator);
|
663
|
log.debug("Indicator toggled!");
|
664
|
} else {
|
665
|
// EXCEPTION - Indicator not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); -> Category: category.getAlias(); -> SubCategory: subCategory.getAlias(); -> Section: section.getTitle();
|
666
|
throw new PathNotValidException("Toggle indicators: Indicator with id: "+indicator.getId()+" not found in Section: "+sectionId);
|
667
|
}
|
668
|
|
669
|
}
|
670
|
|
671
|
private Section checkForExceptions(String stakeholderId, String topicId, String categoryId, String subcategoryId, String sectionId, String indicatorType) {
|
672
|
|
673
|
Stakeholder<String> stakeholder = stakeholderDAO.findById(stakeholderId);
|
674
|
|
675
|
if(stakeholder == null) {
|
676
|
// EXCEPTION - Stakeholder not found
|
677
|
throw new EntityNotFoundException("Save indicator: Stakeholder with id: " + stakeholderId + " not found");
|
678
|
}
|
679
|
|
680
|
List<String> roles = rolesUtils.getRoles();
|
681
|
if(!rolesUtils.hasUpdateAuthority(roles, stakeholder.getType(), stakeholder.getAlias())) {
|
682
|
// EXCEPTION - Access denied
|
683
|
throw new AccessDeniedException("CheckForExceptions Indicator: You are not authorized to update stakeholder with id: "+stakeholderId);
|
684
|
}
|
685
|
|
686
|
Topic<String> topic = topicDAO.findById(topicId);
|
687
|
if(topic == null) {
|
688
|
// EXCEPTION - Topic not found
|
689
|
throw new EntityNotFoundException("Save indicator: Topic with id: "+topicId+" not found");
|
690
|
}
|
691
|
|
692
|
if(!stakeholder.getTopics().contains(topicId)) {
|
693
|
// EXCEPTION - Topic not found in Stakeholder: stakeholder.getAlias();
|
694
|
throw new PathNotValidException("Save indicator: Topic with id: " + topicId + " not found in Stakeholder: " + stakeholderId);
|
695
|
}
|
696
|
|
697
|
Category<String> category = categoryDAO.findById(categoryId);
|
698
|
if(category == null) {
|
699
|
// EXCEPTION - Category not found
|
700
|
throw new EntityNotFoundException("Save indicator: Category with id: "+categoryId+" not found");
|
701
|
}
|
702
|
|
703
|
if(!topic.getCategories().contains(categoryId)) {
|
704
|
// EXCEPTION - Category not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias();
|
705
|
throw new PathNotValidException("Save indicator: Category with id: "+categoryId+" not found in Topic: "+topicId);
|
706
|
}
|
707
|
|
708
|
SubCategory<String> subcategory = subCategoryDAO.findById(subcategoryId);
|
709
|
if(subcategory == null) {
|
710
|
// EXCEPTION - SubCategory not found
|
711
|
throw new EntityNotFoundException("Save indicator: SubCategory with id: "+subcategoryId+" not found");
|
712
|
}
|
713
|
|
714
|
if (!category.getSubCategories().contains(subcategoryId)) {
|
715
|
// EXCEPTION - SubCategory not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); -> Category: category.getAlias();
|
716
|
throw new PathNotValidException("Save indicator: SubCategory with id: "+subcategoryId+" not found in Category: "+categoryId);
|
717
|
}
|
718
|
|
719
|
Section<String> section = sectionDAO.findById(sectionId);
|
720
|
if(section == null) {
|
721
|
// EXCEPTION - Section not found
|
722
|
throw new EntityNotFoundException("Save indicator: Section with id: "+sectionId+" not found");
|
723
|
}
|
724
|
|
725
|
if(indicatorType.equals("chart")) {
|
726
|
if (!subcategory.getCharts().contains(sectionId)) {
|
727
|
// EXCEPTION - Section not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); -> Category: category.getAlias(); -> SubCategory: subcategory.getAlias();
|
728
|
throw new PathNotValidException("Save indicator: SubCategory with id: " + subcategoryId + " not found in Category: " + categoryId);
|
729
|
}
|
730
|
} else if(indicatorType.equals("number")) {
|
731
|
if (!subcategory.getNumbers().contains(sectionId)) {
|
732
|
// EXCEPTION - Section not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); -> Category: category.getAlias(); -> SubCategory: subcategory.getAlias();
|
733
|
throw new PathNotValidException("Save indicator: SubCategory with id: " + subcategoryId + " not found in Category: " + categoryId);
|
734
|
}
|
735
|
}
|
736
|
|
737
|
return section;
|
738
|
}
|
739
|
|
740
|
public void deleteTree(Section section) {
|
741
|
List<String> indicators = section.getIndicators();
|
742
|
for(String indicatorId : indicators) {
|
743
|
indicatorDAO.delete(indicatorId);
|
744
|
}
|
745
|
}
|
746
|
|
747
|
public void disConnectTree(Section section) {
|
748
|
List<String> indicators = section.getIndicators();
|
749
|
for(String indicatorId : indicators) {
|
750
|
Indicator indicator = indicatorDAO.findById(indicatorId);
|
751
|
indicator.setDefaultId(null);
|
752
|
indicatorDAO.save(indicator);
|
753
|
}
|
754
|
}
|
755
|
}
|