Project

General

Profile

« Previous | Next » 

Revision 59814

[Trunk | Monitor Service]:
1. RolesUtils.java: New class connected to "AuthorizationService" and returns helper methods for roles and authorities.
2. StakeholderController.java & TopicController.java & CategoryController.java & SubCategoryController.java & SectionController.java & IndicatorController.java:
a. Add authorization checks according to user roles (authorization library).
b. Handle new fields "createDate" and "updateDate" (StakeholderController.java already had these fields).
c. [Bug fix] On save method, if it is default entity, add it before "onSaveDefault...()" or after "onUpdateDefault...()".
d. (not in SectionController) Comment methods for toggling status and access and add method for changing visibility.
e.g. "changeIndicatorVisibility()" (/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/{sectionId}/{indicatorId}/change-visibility).
3. StakeholderController.java: Method "getAllRealStakeholders()" (/stakeholder) returns now basic Stakeholder info (topicIds, not full entities).
4. IndicatorController.java:
a. [Bug fix] On "onUpdateDefaultIndicator()", "changed" is set to false for each indicatorBasedOnDefault.
b. On "onUpdateDefaultIndicator()" handle update policy for "description" and "additionalDescription".
c. [Bug fix] On "onUpdateDefaultIndicator()", bug fixes when updating "jsonPath".

View differences:

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

  
12 15
import java.io.UnsupportedEncodingException;
......
19 22
    private final Logger log = Logger.getLogger(this.getClass());
20 23

  
21 24
    @Autowired
25
    private RolesUtils rolesUtils;
26

  
27
    @Autowired
22 28
    private StakeholderDAO stakeholderDAO;
23 29

  
24 30
    @Autowired
......
37 43
    private IndicatorDAO indicatorDAO;
38 44

  
39 45

  
46
    @PreAuthorize("isAuthenticated()")
40 47
    @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/{sectionId}/save", method = RequestMethod.POST)
41 48
    public Indicator saveIndicator(@PathVariable("stakeholderId") String stakeholderId,
42 49
                                   @PathVariable("topicId") String topicId,
......
49 56

  
50 57
        Section<String> section = checkForExceptions(stakeholderId, topicId, categoryId, subcategoryId, sectionId, indicator.getType());
51 58

  
59
        Date date = new Date();
60
        indicator.setUpdateDate(date);
61

  
52 62
        Indicator oldIndicator = null;
53 63
        if(indicator.getId() != null) {
54 64
            oldIndicator = indicatorDAO.findById(indicator.getId());
65
        } else { // indicator does not exist in DB
66
            indicator.setCreationDate(date);
55 67
        }
56 68

  
57 69
        String indicatorId = indicator.getId();
58
        indicatorDAO.save(indicator);
59 70

  
60 71
        Stakeholder<String> stakeholder = stakeholderDAO.findById(stakeholderId);
61 72
        // this indicator belongs in default profile and it is new or it is updated
62 73
        if(stakeholder.getDefaultId() == null) {
63 74
            if(indicatorId == null) {
75
                indicatorDAO.save(indicator);
64 76
                onSaveDefaultIndicator(indicator, sectionId);
65 77
            }
66 78
            else {
67 79
                onUpdateDefaultIndicator(indicator, stakeholder, oldIndicator);
80
                indicatorDAO.save(indicator);
68 81
            }
82
        } else {
83
            indicatorDAO.save(indicator);
69 84
        }
70 85

  
71 86
        List<String> indicators = section.getIndicators();
......
86 101
        // new indicator in default profile - add it on profiles of the same type
87 102
        List<Section> sections = sectionDAO.findByDefaultId(defaultSectionId);
88 103

  
89
        for (Section section : sections) {
104
       for (Section section : sections) {
90 105
            Indicator indicatorNew = new Indicator();
91 106
            indicatorNew.copyFromDefault(indicator);
92 107
            for (IndicatorPath indicatorPath : indicatorNew.getIndicatorPaths()) {
......
108 123

  
109 124
        // indicator already exists - check if changed and update all indicators based on it
110 125

  
111
        boolean changed = false;
126
        boolean changed;
112 127
        List<Indicator> indicators = indicatorDAO.findByDefaultId(indicator.getId());
113 128

  
114 129
        for(Indicator indicatorBasedOnDefault : indicators) {
130
            changed = false;
131

  
115 132
            if(indicator.getName() != null && !indicator.getName().equals(indicatorBasedOnDefault.getName())
116 133
                    && (oldIndicator.getName() == null || oldIndicator.getName().equals(indicatorBasedOnDefault.getName()))) {
117 134

  
......
119 136
                changed = true;
120 137
            }
121 138

  
122
            if(indicator.getDescription() != null && !indicator.getDescription().equals(indicatorBasedOnDefault.getDescription())
123
                    && (oldIndicator.getDescription() == null || oldIndicator.getDescription().equals(indicatorBasedOnDefault.getDescription()))) {
139
            if(indicator.getDescription() != null && !indicator.getDescription().equals(indicatorBasedOnDefault.getDescription())) {
124 140

  
125 141
                indicatorBasedOnDefault.setDescription(indicator.getDescription());
126 142
                changed = true;
127 143
            }
128 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

  
129 152
            int i = 0;
130 153
            List<IndicatorPath> indicatorPaths = indicatorBasedOnDefault.getIndicatorPaths();
154
            if(indicatorPaths == null && indicator.getIndicatorPaths() != null) {
155
                indicatorPaths = new ArrayList<>();
156
            }
131 157

  
132 158
            for (IndicatorPath indicatorPath : indicator.getIndicatorPaths()) {
133
                IndicatorPath indicatorPathBasedOnDefault = indicatorBasedOnDefault.getIndicatorPaths().get(i);
159
                IndicatorPath indicatorPathBasedOnDefault = null;
160
                if(i < indicatorPaths.size()) {
161
                    indicatorPathBasedOnDefault = indicatorPaths.get(i);
162
                }
134 163

  
135 164
                if(indicatorPathBasedOnDefault == null) {
136 165
                    // Add new indicator path in existing indicators
......
142 171
                    IndicatorPath oldIndicatorPath = oldIndicator.getIndicatorPaths().get(i);
143 172

  
144 173
                    // Check if there are changes in indicator path and update existing indicators if needed
145
                    log.debug("update indicator path: "+i);
174
                    log.debug("update indicator path: "+i + " (indicator id: "+indicatorBasedOnDefault.getId()+")");
146 175

  
147 176
                    if(indicatorPath.getType() != null
148 177
                            && !indicatorPath.getType().equals(indicatorPathBasedOnDefault.getType())
......
189 218
                            for (Map.Entry<String, String> parameter : indicatorPath.getParameters().entrySet()) {
190 219
                                log.debug("\nindicatorPath: parameter.getKey(): "+parameter.getKey()+" - value: "+parameter.getValue()
191 220
                                        +"\nindicatorPathBasedOnDefault:parameters:key: "+  indicatorPathBasedOnDefault.getParameters().get(parameter.getKey())
192
                                        +"\noldIndicatorPath:parameters:key: "+  oldIndicatorPath.getParameters().get(parameter.getKey()));
221
                                        +"\noldIndicatorPath:parameters:key: "+  (oldIndicatorPath.getParameters() == null ? "null" : oldIndicatorPath.getParameters().get(parameter.getKey())));
193 222
                                if (!indicatorPathBasedOnDefault.getParameters().containsKey(parameter.getKey())
194 223
                                        || (oldIndicatorPath.getParameters() == null
195 224
                                            || (oldIndicatorPath.getParameters().get(parameter.getKey()).equals(indicatorPathBasedOnDefault.getParameters().get(parameter.getKey()))
......
203 232
//                                    changed = true;
204 233
//                                }
205 234
                            }
206
                            parameterMapping(indicatorPathBasedOnDefault, stakeholder);
235
                            // TODO when deleting indicator path parameters... ???
236
                        parameterMapping(indicatorPathBasedOnDefault, stakeholder);
207 237
                        //}
208
                        log.debug("After parameters check: " + changed);
209 238
                    }
239
                    log.debug("After parameters check: " + changed);
210 240

  
211 241
                    if(indicatorPath.getJsonPath() != null) {
212
                        int j = 0;
213
                        for (String jsonString : indicatorPath.getJsonPath()) {
214
                            log.debug("indicatorPath.getJsonPath(): " + jsonString);
215
                            String jsonStringBasedOnDefault = null;
216
                            if(indicatorPathBasedOnDefault.getJsonPath() != null ) {
217
                                jsonStringBasedOnDefault = indicatorPathBasedOnDefault.getJsonPath().get(j);
218
                            } else {
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) {
219 256
                                indicatorPathBasedOnDefault.setJsonPath(new ArrayList<>());
220 257
                            }
221
                            log.debug("indicatorPathBasedOnDefault.getJsonPath().get(" + j + "): " + jsonStringBasedOnDefault);
222 258

  
223
                            if (!jsonString.equals(jsonStringBasedOnDefault)
224
                                    && (oldIndicatorPath.getJsonPath() == null
225
                                    || oldIndicatorPath.getJsonPath().get(i).equals(jsonStringBasedOnDefault))
226
                            ) {
227
                                indicatorPathBasedOnDefault.getJsonPath().set(j, jsonString);
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) {
228 294
                                changed = true;
229 295
                            }
230
                            j++;
231 296
                        }
232
                        log.debug("After jsonPath check: " + changed);
297
                        // TODO when deleting indicator path json path strings...
233 298
                    }
299
                    log.debug("After jsonPath check: " + changed);
234 300
                }
235 301
                i++;
236 302
            }
303
            // TODO when deleting indicator paths...
237 304

  
238 305
            if(!changed) {
239 306
//                break;
240 307
                continue;
241 308
            }
242 309

  
310
            indicatorBasedOnDefault.setUpdateDate(indicator.getUpdateDate());
243 311
            indicatorDAO.save(indicatorBasedOnDefault);
244 312
        }
245 313
    }
......
299 367
        return indicatorPathField;
300 368
    }
301 369

  
370
    @PreAuthorize("isAuthenticated()")
302 371
    @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/{sectionId}/{indicatorId}/delete", method = RequestMethod.DELETE)
303 372
    public boolean deleteIndicator(@PathVariable("stakeholderId") String stakeholderId,
304 373
                                   @PathVariable("topicId") String topicId,
......
314 383
        if(indicator != null) {
315 384
            Section<String> section = checkForExceptions(stakeholderId, topicId, categoryId, subcategoryId, sectionId, indicator.getType());
316 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

  
317 393
            List<String> indicators = section.getIndicators();
318 394

  
319 395
            int index = indicators.indexOf(indicatorId);
......
484 560
//        return true;
485 561
//    }
486 562

  
563
    @PreAuthorize("isAuthenticated()")
487 564
    @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/{sectionId}/{type}/reorder", method = RequestMethod.POST)
488 565
    public List<Indicator> reorderIndicators(@PathVariable("stakeholderId") String stakeholderId,
489 566
                                             @PathVariable("topicId") String topicId,
......
509 586
        return indicatorsFull;
510 587
    }
511 588

  
512
    @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/{sectionId}/{indicatorId}/toggle-status", method = RequestMethod.POST)
513
    public Boolean toggleIndicatorStatus(@PathVariable("stakeholderId") String stakeholderId,
514
                                         @PathVariable("topicId") String topicId,
515
                                         @PathVariable("categoryId") String categoryId,
516
                                         @PathVariable("subcategoryId") String subcategoryId,
517
                                         @PathVariable("sectionId") String sectionId,
518
                                         @PathVariable("indicatorId") String indicatorId) {
519
        log.debug("toggle indicator status (isActive)");
520
        log.debug("Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId+ " - SubCategory: "+subcategoryId + " - Section: "+sectionId+ " - Indicator: "+indicatorId);
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
//    }
521 632

  
522
        Indicator indicator = indicatorDAO.findById(indicatorId);
523
        if (indicator == null) {
524
            // EXCEPTION - Indicator not found
525
            throw new EntityNotFoundException("Toggle indicator status: Indicator with id: "+indicatorId+" not found");
526
        }
527
        indicator.setIsActive(!indicator.getIsActive());
528

  
529
        this.toggleIndicator(stakeholderId, topicId, categoryId, subcategoryId, sectionId, indicator);
530

  
531
        return indicator.getIsActive();
532
    }
533

  
534
    @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/{sectionId}/{indicatorId}/toggle-access", method = RequestMethod.POST)
535
    public Boolean toggleIndicatorAccess(@PathVariable("stakeholderId") String stakeholderId,
536
                                         @PathVariable("topicId") String topicId,
537
                                         @PathVariable("categoryId") String categoryId,
538
                                         @PathVariable("subcategoryId") String subcategoryId,
539
                                         @PathVariable("sectionId") String sectionId,
540
                                         @PathVariable("indicatorId") String indicatorId) {
541
        log.debug("toggle indicator access (isPublic)");
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);
542 643
        log.debug("Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId+ " - SubCategory: "+subcategoryId + " - Section: "+sectionId+ " - Indicator: "+indicatorId);
543 644

  
544 645
        Indicator indicator = indicatorDAO.findById(indicatorId);
545 646
        if (indicator == null) {
546 647
            // EXCEPTION - Indicator not found
547
            throw new EntityNotFoundException("Toggle indicator access: Indicator with id: "+indicatorId+" not found");
648
            throw new EntityNotFoundException("Change indicator visibility: Indicator with id: "+indicatorId+" not found");
548 649
        }
549
        indicator.setIsPublic(!indicator.getIsPublic());
650
        indicator.setVisibility(visibility);
550 651

  
551 652
        this.toggleIndicator(stakeholderId, topicId, categoryId, subcategoryId, sectionId, indicator);
552 653

  
553
        return indicator.getIsPublic();
654
        return indicator.getVisibility();
554 655
    }
555 656

  
556 657
    public void toggleIndicator(String stakeholderId, String topicId, String categoryId, String subcategoryId, String sectionId, Indicator indicator) {
......
576 677
            throw new EntityNotFoundException("Save indicator: Stakeholder with id: " + stakeholderId + " not found");
577 678
        }
578 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

  
579 686
        Topic<String> topic = topicDAO.findById(topicId);
580 687
        if(topic == null) {
581 688
            // EXCEPTION - Topic not found

Also available in: Unified diff