Project

General

Profile

« Previous | Next » 

Revision 57964

[Trunk | Monitor Service]:
1. Section entity level added. Hierarchy changed from: Stakeholder > Topic > Category > SubCategory > Indicator to: Stakeholder > Topic > Category > SubCategory > Section > Indicator.
2. Section.java & SectionDAO.java & MongoDBSectionDAO.java & SectionController.java: New entity 'Section', DAOs for Section, Controller for section added.
3. In all controllers fix hierarchy of model to include Section.
4. SubCategoryController.java & IndicatorController.java: Helper method 'checkForExceptions()' added (instead of checking in every method separately).

View differences:

SubCategoryController.java
29 29
    private SubCategoryDAO subCategoryDAO;
30 30

  
31 31
    @Autowired
32
    private SectionDAO sectionDAO;
33

  
34
    @Autowired
32 35
    private IndicatorDAO indicatorDAO;
33 36

  
34
    public SubCategory<Indicator> buildSubCategory(SubCategory<Indicator> subcategoryFull) {
37
    @Autowired
38
    private SectionController sectionController;
39

  
40
    public SubCategory<Section<Indicator>> buildSubCategory(SubCategory<Section<Indicator>> subcategoryFull) {
35 41
        SubCategory<String> subCategory = new SubCategory<>(subcategoryFull);
36 42

  
37
        List<String> charts = new ArrayList<>();
38
        List<Indicator> chartsFull = new ArrayList<>();
39
        for(Indicator chart : subcategoryFull.getCharts()) {
40
            Indicator chartSaved = indicatorDAO.save(chart);
41
            chart.setId(chartSaved.getId());
42
            chartsFull.add(chart);
43
            charts.add(chartSaved.getId());
43
        List<String> sectionCharts = new ArrayList<>();
44
        List<Section<Indicator>> sectionChartsFull = new ArrayList<>();
45

  
46
        for(Section section : subcategoryFull.getCharts()) {
47
            Section<Indicator> sectionFull = sectionController.buildSection(section);
48
            sectionChartsFull.add(sectionFull);
49
            sectionCharts.add(sectionFull.getId());
44 50
        }
45
        subcategoryFull.setCharts(chartsFull);
46
        subCategory.setCharts(charts);
51
        subcategoryFull.setCharts(sectionChartsFull);
52
        subCategory.setCharts(sectionCharts);
47 53

  
48
        List<String> numbers = new ArrayList<>();
49
        List<Indicator> numbersFull = new ArrayList<>();
50
        for(Indicator numbr : subcategoryFull.getNumbers()) {
51
            Indicator numberSaved = indicatorDAO.save(numbr);
52
            numbr.setId(numberSaved.getId());
53
            numbersFull.add(numbr);
54
            numbers.add(numberSaved.getId());
54
        List<String> sectionNumbers = new ArrayList<>();
55
        List<Section<Indicator>> sectionNumbersFull = new ArrayList<>();
56

  
57
        for(Section section : subcategoryFull.getNumbers()) {
58
            Section<Indicator> sectionFull = sectionController.buildSection(section);
59
            sectionNumbersFull.add(sectionFull);
60
            sectionNumbers.add(sectionFull.getId());
55 61
        }
56
        subcategoryFull.setNumbers(numbersFull);
57
        subCategory.setNumbers(numbers);
62
        subcategoryFull.setNumbers(sectionNumbersFull);
63
        subCategory.setNumbers(sectionNumbers);
58 64

  
59 65
        subCategoryDAO.save(subCategory);
60 66

  
......
63 69
    }
64 70

  
65 71
    @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/save", method = RequestMethod.POST)
66
    public SubCategory<Indicator> saveSubCategory(@PathVariable("stakeholderId") String stakeholderId,
67
                                                  @PathVariable("topicId") String topicId,
68
                                                  @PathVariable("categoryId") String categoryId,
69
                                                  @RequestBody SubCategory<Indicator> subcategoryFull) {
72
    public SubCategory<Section<Indicator>> saveSubCategory(@PathVariable("stakeholderId") String stakeholderId,
73
                                                           @PathVariable("topicId") String topicId,
74
                                                           @PathVariable("categoryId") String categoryId,
75
                                                           @RequestBody SubCategory<Section<Indicator>> subcategoryFull) {
70 76
        log.debug("save subcategory");
71 77
        log.debug("Alias: "+subcategoryFull.getAlias() + " - Id: "+subcategoryFull.getId() + " - Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId);
72 78

  
73
        Stakeholder<String> stakeholder = stakeholderDAO.findById(stakeholderId);
79
        Category category = checkForExceptions(stakeholderId, topicId, categoryId);
80
        SubCategory<String> subCategory = new SubCategory<>(subcategoryFull);
74 81

  
75
        if(stakeholder != null) {
82
//        List<String> charts = new ArrayList<>();
83
//        for(Indicator chart : subcategoryFull.getCharts()) {
84
//            charts.add(chart.getId());
85
//        }
86
//        subCategory.setCharts(charts);
87
//
88
//        List<String> numbers = new ArrayList<>();
89
//        for(Indicator numbr : subcategoryFull.getNumbers()) {
90
//            numbers.add(numbr.getId());
91
//        }
92
//        subCategory.setNumbers(numbers);
76 93

  
77
            Topic<String> topic = topicDAO.findById(topicId);
78
            if(topic != null) {
79
                if(stakeholder.getTopics().contains(topicId)) {
94
        List<String> chartSections = new ArrayList<>();
95
        for(Section chartSection : subcategoryFull.getCharts()) {
96
            chartSections.add(chartSection.getId());
97
        }
98
        subCategory.setCharts(chartSections);
80 99

  
81
                    Category<String> category = categoryDAO.findById(categoryId);
82
                    if(category != null) {
83
                        if(topic.getCategories().contains(categoryId)) {
84
                            SubCategory<String> subCategory = new SubCategory<>(subcategoryFull);
100
        List<String> numberSections = new ArrayList<>();
101
        for(Section numberSection : subcategoryFull.getNumbers()) {
102
            numberSections.add(numberSection.getId());
103
        }
104
        subCategory.setNumbers(numberSections);
85 105

  
86
                            List<String> charts = new ArrayList<>();
87
                            for(Indicator chart : subcategoryFull.getCharts()) {
88
                                charts.add(chart.getId());
89
                            }
90
                            subCategory.setCharts(charts);
106
        subCategoryDAO.save(subCategory);
91 107

  
92
                            List<String> numbers = new ArrayList<>();
93
                            for(Indicator numbr : subcategoryFull.getNumbers()) {
94
                                numbers.add(numbr.getId());
95
                            }
96
                            subCategory.setNumbers(numbers);
108
        Stakeholder<String> stakeholder = stakeholderDAO.findById(stakeholderId);
109
        if(stakeholder.getDefaultId() == null) {
110
            if(subcategoryFull.getId() == null) {
111
                onSaveDefaultSubCategory(subCategory, categoryId);
112
            } else {
113
                onUpdateDefaultSubCategory(subCategory);
114
            }
115
        }
97 116

  
98
                            subCategoryDAO.save(subCategory);
117
        List<String> subcategories = category.getSubCategories();
118
        int index = subcategories.indexOf(subCategory.getId());
119
        if(index == -1) {
120
            subcategories.add(subCategory.getId());
121
            categoryDAO.save(category);
122
            log.debug("Subcategory saved!");
99 123

  
100
                            if(stakeholder.getDefaultId() == null) {
101
                                if(topic.getId() == null) {
102
                                    onSaveDefaultSubCategory(subCategory, categoryId);
103
                                } else {
104
                                    onUpdateDefaultSubCategory(subCategory);
105
                                }
106
                            }
124
            subcategoryFull.setId(subCategory.getId());
125
        }
107 126

  
108
                            List<String> subcategories = category.getSubCategories();
109
                            int index = subcategories.indexOf(subCategory.getId());
110
                            if(index == -1) {
111
                                subcategories.add(subCategory.getId());
112
                                categoryDAO.save(category);
113
                                log.debug("Subcategory saved!");
127
        chartSections = null;
128
        numberSections = null;
129
        subCategory = null;
114 130

  
115
                                subcategoryFull.setId(subCategory.getId());
116
                            }
117

  
118
                            charts = null;
119
                            numbers = null;
120
                            subCategory = null;
121
                        } else {
122
                            // EXCEPTION - Category not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias();
123
                            throw new PathNotValidException("Save subcategory: Category with id: "+categoryId+" not found in Topic: "+topicId);
124
                        }
125
                    } else {
126
                        // EXCEPTION - Category not found
127
                        throw new EntityNotFoundException("Save subcategory: Category with id: "+categoryId+" not found");
128
                    }
129
                } else {
130
                    // EXCEPTION - Topic not found in Stakeholder: stakeholder.getAlias();
131
                    throw new PathNotValidException("Save subcategory: Topic with id: "+topicId+" not found in Stakeholder: "+stakeholderId);
132
                }
133
            } else {
134
                // EXCEPTION - Topic not found
135
                throw new EntityNotFoundException("Save subcategory: Topic with id: "+topicId+" not found");
136
            }
137
        } else {
138
            // EXCEPTION - Stakeholder not found
139
            throw new EntityNotFoundException("Save subcategory: Stakeholder with id: "+stakeholderId+" not found");
140
        }
141 131
        return subcategoryFull;
142 132
    }
143 133

  
......
188 178
        log.debug("delete subcategory");
189 179
        log.debug("Id: "+subcategoryId + " - Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId);
190 180

  
191
        Stakeholder<String> stakeholder = stakeholderDAO.findById(stakeholderId);
181
        Category category = checkForExceptions(stakeholderId, topicId, categoryId);
192 182

  
193
        if(stakeholder != null) {
183
        SubCategory<String> subcategory = subCategoryDAO.findById(subcategoryId);
184
        if(subcategory != null) {
185
            List<String> subcategories = category.getSubCategories();
186
            int index = subcategories.indexOf(subcategoryId);
187
            if(index != -1) {
188
                for(String chartSectionId : subcategory.getCharts()) {
189
                    Section<String> chartSection = sectionDAO.findById(chartSectionId);
190
                    if (chartSection == null) {
191
                        // EXCEPTION - Section not found
192
                        throw new EntityNotFoundException("Delete SubCategory: Section with id: "+chartSectionId+" not found (section exists in subcategory: "+subcategoryId+")");
193
                    }
194 194

  
195
            Topic<String> topic = topicDAO.findById(topicId);
196
            if(topic != null) {
197
                if(stakeholder.getTopics().contains(topicId)) {
195
                    for (String chartId : chartSection.getIndicators()) {
196
                        indicatorDAO.delete(chartId);
197
                    }
198
                    subcategory.setCharts(null);
199
                    sectionDAO.delete(chartSectionId);
200
                }
198 201

  
199
                    Category<String> category = categoryDAO.findById(categoryId);
200
                    if(category != null) {
201
                        if(topic.getCategories().contains(categoryId)) {
202
                for(String numberSectionId : subcategory.getNumbers()) {
203
                    Section<String> numberSection = sectionDAO.findById(numberSectionId);
204
                    if (numberSection == null) {
205
                        // EXCEPTION - Section not found
206
                        throw new EntityNotFoundException("Delete SubCategory: Section with id: "+numberSectionId+" not found (section exists in subcategory: "+subcategoryId+")");
207
                    }
202 208

  
203
                            SubCategory<String> subcategory = subCategoryDAO.findById(subcategoryId);
204
                            if(subcategory != null) {
205
                                List<String> subcategories = category.getSubCategories();
206
                                int index = subcategories.indexOf(subcategoryId);
207
                                if(index != -1) {
208
                                    for(String chartId : subcategory.getCharts()) {
209
                                        indicatorDAO.delete(chartId);
210
                                    }
211
                                    subcategory.setCharts(null);
209
                    for (String numberId : numberSection.getIndicators()) {
210
                        indicatorDAO.delete(numberId);
211
                    }
212
                    subcategory.setNumbers(null);
213
                    sectionDAO.delete(numberSectionId);
214
                }
212 215

  
213
                                    for(String numberId : subcategory.getNumbers()) {
214
                                        indicatorDAO.delete(numberId);
215
                                    }
216
                                    subcategory.setNumbers(null);
216
                subcategories.remove(index);
217
                categoryDAO.save(category);
217 218

  
218
                                    subcategories.remove(index);
219
                                    categoryDAO.save(category);
220

  
221
                                    subCategoryDAO.delete(subcategoryId);
222
                                    log.debug("Subcategory deleted!");
223
                                } else {
224
                                    // EXCEPTION - SubCategory not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); -> Category: category.getAlias();
225
                                    throw new PathNotValidException("Delete subcategory: Subcategory with id: "+subcategoryId+" not found in Category: "+categoryId);
226
                                }
227

  
228
                            } else {
229
                                // EXCEPTION - SubCategory not found
230
                                throw new EntityNotFoundException("Delete subcategory: SubCategory with id: "+subcategoryId+" not found");
231
                            }
232
                        } else {
233
                            // EXCEPTION - Category not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias();
234
                            throw new PathNotValidException("Delete subcategory: Category with id: "+categoryId+" not found in Topic: "+topicId);
235
                        }
236
                    } else {
237
                        // EXCEPTION - Category not found
238
                        throw new EntityNotFoundException("Delete subcategory: Category with id: "+categoryId+" not found");
239
                    }
240
                } else {
241
                    // EXCEPTION - Topic not found in Stakeholder: stakeholder.getAlias();
242
                    throw new PathNotValidException("Delete subcategory: Topic with id: "+topicId+" not found in Stakeholder: "+stakeholderId);
243
                }
219
                subCategoryDAO.delete(subcategoryId);
220
                log.debug("Subcategory deleted!");
244 221
            } else {
245
                // EXCEPTION - Topic not found
246
                throw new EntityNotFoundException("Delete subcategory: Topic with id: "+topicId+" not found");
222
                // EXCEPTION - SubCategory not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); -> Category: category.getAlias();
223
                throw new PathNotValidException("Delete subcategory: Subcategory with id: "+subcategoryId+" not found in Category: "+categoryId);
247 224
            }
225

  
248 226
        } else {
249
            // EXCEPTION - Stakeholder not found
250
            throw new EntityNotFoundException("Delete subcategory: Stakeholder with id: "+stakeholderId+" not found");
227
            // EXCEPTION - SubCategory not found
228
            throw new EntityNotFoundException("Delete subcategory: SubCategory with id: "+subcategoryId+" not found");
251 229
        }
252 230
        return true;
253 231
    }
......
293 271
    }
294 272

  
295 273
    public void toggleSubCategory(String stakeholderId, String topicId, String categoryId, SubCategory subcategory) {
296
        Stakeholder<String> stakeholder = stakeholderDAO.findById(stakeholderId);
274
        Category category = checkForExceptions(stakeholderId, topicId, categoryId);
297 275

  
298
        if (stakeholder != null) {
276
        if (category.getSubCategories().contains(subcategory.getId())) {
277
            subCategoryDAO.save(subcategory);
278
            log.debug("SubCategory toggled!");
279
        } else {
280
            // EXCEPTION - SubCategory not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); -> Category: category.getAlias();
281
            throw new PathNotValidException("Toggle subCategory: SubCategory with id: "+subcategory.getId()+" not found in Category: "+categoryId);
282
        }
283
    }
299 284

  
300
            Topic<String> topic = topicDAO.findById(topicId);
301
            if (topic != null) {
302
                if (stakeholder.getTopics().contains(topicId)) {
303 285

  
304
                    Category<String> category = categoryDAO.findById(categoryId);
305
                    if (category != null) {
306
                        if (topic.getCategories().contains(categoryId)) {
307
                            if (category.getSubCategories().contains(subcategory.getId())) {
308
                                subCategoryDAO.save(subcategory);
309
                                log.debug("SubCategory toggled!");
310
                            } else {
311
                                // EXCEPTION - SubCategory not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); -> Category: category.getAlias();
312
                                throw new PathNotValidException("Toggle subCategory: SubCategory with id: "+subcategory.getId()+" not found in Category: "+categoryId);
313
                            }
314
                        } else {
315
                            // EXCEPTION - Category not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias();
316
                            throw new PathNotValidException("Toggle subCategory: Category with id: "+categoryId+" not found in Topic: "+topicId);
317
                        }
318
                    } else {
319
                        // EXCEPTION - Category not found
320
                        throw new EntityNotFoundException("Toggle subCategory: Category with id: "+categoryId+" not found");
321
                    }
322
                } else {
323
                    // EXCEPTION - Topic not found in Stakeholder: stakeholder.getAlias();
324
                    throw new PathNotValidException("Toggle subCategory: Topic with id: "+topicId+" not found in Stakeholder: "+stakeholderId);
325
                }
326
            } else {
327
                // EXCEPTION - Topic not found
328
                throw new EntityNotFoundException("Toggle subCategory: Topic with id: "+topicId+" not found");
329
            }
330
        } else {
286
    private Category checkForExceptions(String stakeholderId, String topicId, String categoryId) {
287

  
288
        Stakeholder<String> stakeholder = stakeholderDAO.findById(stakeholderId);
289

  
290
        if(stakeholder == null) {
331 291
            // EXCEPTION - Stakeholder not found
332
            throw new EntityNotFoundException("Toggle subCategory: Stakeholder with id: "+stakeholderId+" not found");
292
            throw new EntityNotFoundException("Save indicator: Stakeholder with id: " + stakeholderId + " not found");
333 293
        }
294

  
295
        Topic<String> topic = topicDAO.findById(topicId);
296
        if(topic == null) {
297
            // EXCEPTION - Topic not found
298
            throw new EntityNotFoundException("Save indicator: Topic with id: "+topicId+" not found");
299
        }
300

  
301
        if(!stakeholder.getTopics().contains(topicId)) {
302
            // EXCEPTION - Topic not found in Stakeholder: stakeholder.getAlias();
303
            throw new PathNotValidException("Save indicator: Topic with id: " + topicId + " not found in Stakeholder: " + stakeholderId);
304
        }
305

  
306
        Category<String> category = categoryDAO.findById(categoryId);
307
        if(category == null) {
308
            // EXCEPTION - Category not found
309
            throw new EntityNotFoundException("Save indicator: Category with id: "+categoryId+" not found");
310
        }
311

  
312
        if(!topic.getCategories().contains(categoryId)) {
313
            // EXCEPTION - Category not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias();
314
            throw new PathNotValidException("Save indicator: Category with id: "+categoryId+" not found in Topic: "+topicId);
315
        }
316

  
317
//        SubCategory<String> subcategory = subCategoryDAO.findById(subcategoryId);
318
//        if(subcategory == null) {
319
//            // EXCEPTION - SubCategory not found
320
//            throw new EntityNotFoundException("Save indicator: SubCategory with id: "+subcategoryId+" not found");
321
//        }
322
//
323
//        if (!category.getSubCategories().contains(subcategoryId)) {
324
//            // EXCEPTION - SubCategory not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); -> Category: category.getAlias();
325
//            throw new PathNotValidException("Save indicator: SubCategory with id: "+subcategoryId+" not found in Category: "+categoryId);
326
//        }
327

  
328
        return  category;
334 329
    }
335 330

  
336 331
}

Also available in: Unified diff