Project

General

Profile

« Previous | Next » 

Revision 58978

[Trunk | Monitor Service]:
1. Adding parameter in delete methods to delete or disconnect all profiles based on the default one that is being deleted (not for stakeholder).
2. In delete methods remove iterations (for deleting inner elements) and call "deleteTree()" method.

View differences:

modules/uoa-monitor-service/trunk/src/main/java/eu/dnetlib/uoamonitorservice/controllers/IndicatorController.java
11 11

  
12 12
import java.io.UnsupportedEncodingException;
13 13
import java.net.URLEncoder;
14
import java.util.ArrayList;
15
import java.util.HashMap;
16
import java.util.List;
17
import java.util.Map;
14
import java.util.*;
18 15

  
19 16
@RestController
20 17
@CrossOrigin(origins = "*")
......
146 143

  
147 144
                    // Check if there are changes in indicator path and update existing indicators if needed
148 145
                    log.debug("update indicator path: "+i);
149
                    log.debug("indicatorPath.getType(): "+indicatorPath.getType());
150
                    log.debug("indicatorPathBasedOnDefault.getType(): "+indicatorPathBasedOnDefault.getType());
151
                    log.debug("oldIndicatorPath.getType(): "+oldIndicatorPath.getType());
152 146

  
153 147
                    if(indicatorPath.getType() != null
154 148
                            && !indicatorPath.getType().equals(indicatorPathBasedOnDefault.getType())
......
311 305
                                   @PathVariable("categoryId") String categoryId,
312 306
                                   @PathVariable("subcategoryId") String subcategoryId,
313 307
                                   @PathVariable("sectionId") String sectionId,
314
                                   @PathVariable("indicatorId") String indicatorId) {
308
                                   @PathVariable("indicatorId") String indicatorId,
309
                                   @RequestParam(required = false) String children) {
315 310
        log.debug("delete indicator");
316 311
        log.debug("Id: "+indicatorId + " - Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId+ " - SubCategory: "+subcategoryId + " - Section: "+sectionId);
317 312

  
......
323 318

  
324 319
            int index = indicators.indexOf(indicatorId);
325 320
            if (index != -1) {
321

  
322
                // this indicator belongs in default profile
323
                if(section.getDefaultId() == null && children != null) {
324
                    onDeleteDefaultIndicator(indicatorId, sectionId, children);
325
                }
326

  
327

  
326 328
                indicators.remove(index);
327 329
                sectionDAO.save(section);
328 330

  
......
339 341
        return true;
340 342
    }
341 343

  
344
    public boolean onDeleteDefaultIndicator(String defaultIndicatorId, String defaultSectionId, String children) {
345
        if(children.equals("delete")) {
346
//            // 1st way
347
//            List<Section> sections = sectionDAO.findByDefaultId(defaultSectionId);
348
//
349
//            for(Section section : sections) {
350
//                List<String> indicators = section.getIndicators();
351
//
352
//                Iterator<String> indicatorsIterator = indicators.iterator();
353
//                while(indicatorsIterator.hasNext()) {
354
//                    String indicatorId = indicatorsIterator.next();
355
//
356
//                    Indicator indicator = indicatorDAO.findById(indicatorId);
357
//                    if (indicator.getDefaultId().equals(defaultIndicatorId)) {
358
//                        indicatorsIterator.remove();
359
//                        sectionDAO.save(section);
360
//
361
//                        indicatorDAO.delete(indicatorId);
362
//                        log.debug("Indicator deleted!");
363
//
364
//                        break;
365
//                    }
366
//                }
367
//            }
368

  
369
            // 2nd way
370
            List<Section> sections = sectionDAO.findByDefaultId(defaultSectionId);
371
            List<Indicator> indicators = indicatorDAO.findByDefaultId(defaultIndicatorId);
372

  
373
            for(Section section : sections) {
374
                Iterator<Indicator> indicatorsIterator = indicators.iterator();
375
                while(indicatorsIterator.hasNext()) {
376
                    String indicatorId = indicatorsIterator.next().getId();
377
                    if(section.getIndicators().contains(indicatorId)) {
378
                        indicatorsIterator.remove();
379

  
380
                        section.getIndicators().remove(indicatorId);
381
                        sectionDAO.save(section);
382

  
383
                        indicatorDAO.delete(indicatorId);
384
                        log.debug("Indicator with id: "+indicatorId+" deleted!");
385

  
386
                        break;
387
                    }
388
                }
389
            }
390

  
391
//            // 3rd way - parentId
392
//            List<Indicator> indicators = indicatorDAO.findByDefaultId(defaultIndicatorId);
393
//            for(Indicator indicator : indicators) {
394
//                Section section = sectionDAO.findById(indicator.getParent());
395
//                List<String> sectionIndicators = section.getIndicators();
396
//
397
//                sectionIndicators.remove(indicator.getId());
398
//                sectionDAO.save(section);
399
//
400
//                indicatorDAO.delete(indicator.getId());
401
//                log.debug("Indicator deleted!");
402
//            }
403
        } else if(children.equals("disconnect")) {
404
            List<Indicator> indicators = indicatorDAO.findByDefaultId(defaultIndicatorId);
405
            for(Indicator indicator : indicators) {
406
                indicator.setDefaultId(null);
407
                indicatorDAO.save(indicator);
408
                log.debug("DefaultId for Indicator with id: "+indicator.getId()+" empty!");
409
            }
410
        }
411
        return true;
412
    }
413

  
342 414
//    @RequestMapping(value = "/{stakeholderId}/charts/delete", method = RequestMethod.DELETE)
343 415
//    public boolean deleteAllChartIndicators(@PathVariable("stakeholderId") String stakeholderId) {
344 416
//        log.debug("delete all chart indicators of stakeholder");
......
557 629

  
558 630
        return  section;
559 631
    }
632

  
633
    public void deleteTree(Section section) {
634
        List<String> indicators = section.getIndicators();
635
        for(String indicatorId : indicators) {
636
            indicatorDAO.delete(indicatorId);
637
        }
638
    }
639

  
640
    public void disConnectTree(Section section) {
641
        List<String> indicators = section.getIndicators();
642
        for(String indicatorId : indicators) {
643
            Indicator indicator = indicatorDAO.findById(indicatorId);
644
            indicator.setDefaultId(null);
645
            indicatorDAO.save(indicator);
646
        }
647
    }
560 648
}
modules/uoa-monitor-service/trunk/src/main/java/eu/dnetlib/uoamonitorservice/controllers/CategoryController.java
9 9
import org.springframework.web.bind.annotation.*;
10 10

  
11 11
import java.util.ArrayList;
12
import java.util.Iterator;
12 13
import java.util.List;
13 14

  
14 15
@RestController
......
183 184
    @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/delete", method = RequestMethod.DELETE)
184 185
    public boolean deleteCategory(@PathVariable("stakeholderId") String stakeholderId,
185 186
                                  @PathVariable("topicId") String topicId,
186
                                  @PathVariable("categoryId") String categoryId) {
187
                                  @PathVariable("categoryId") String categoryId,
188
                                  @RequestParam(required = false) String children) {
187 189
        log.debug("delete category");
188 190
        log.debug("Id: "+categoryId + " - Stakeholder: "+stakeholderId + " - Topic: "+topicId);
189 191

  
......
201 203
                        List<String> categories = topic.getCategories();
202 204
                        int index = categories.indexOf(categoryId);
203 205
                        if(index != -1) {
204
                            for(String subCategoryId : category.getSubCategories()) {
205
                                SubCategory<String> subcategory = subCategoryDAO.findById(subCategoryId);
206
                                if(subcategory == null) {
207
                                    // EXCEPTION - SubCategory not found
208
                                    throw new EntityNotFoundException("Delete category: SubCategory with id: "+subCategoryId+" not found (subcategory exists in category: "+categoryId+")");
209
                                }
206
                            // this category belongs in default profile
207
                            if(topic.getDefaultId() == null && children != null) {
208
                                onDeleteDefaultCategory(categoryId, topicId, children);
209
                            }
210 210

  
211
                                for(String chartSectionId : subcategory.getCharts()) {
212
                                    Section<String> chartSection = sectionDAO.findById(chartSectionId);
213
                                    if (chartSection == null) {
214
                                        // EXCEPTION - Section not found
215
                                        throw new EntityNotFoundException("Delete topic: Section with id: "+chartSectionId+" not found (section exists in subcategory: "+subCategoryId+")");
216
                                    }
211
//                            for(String subCategoryId : category.getSubCategories()) {
212
//                                SubCategory<String> subcategory = subCategoryDAO.findById(subCategoryId);
213
//                                if(subcategory == null) {
214
//                                    // EXCEPTION - SubCategory not found
215
//                                    throw new EntityNotFoundException("Delete category: SubCategory with id: "+subCategoryId+" not found (subcategory exists in category: "+categoryId+")");
216
//                                }
217
//
218
//                                for(String chartSectionId : subcategory.getCharts()) {
219
//                                    Section<String> chartSection = sectionDAO.findById(chartSectionId);
220
//                                    if (chartSection == null) {
221
//                                        // EXCEPTION - Section not found
222
//                                        throw new EntityNotFoundException("Delete topic: Section with id: "+chartSectionId+" not found (section exists in subcategory: "+subCategoryId+")");
223
//                                    }
224
//
225
//                                    for (String chartId : chartSection.getIndicators()) {
226
//                                        indicatorDAO.delete(chartId);
227
//                                    }
228
//                                    subcategory.setCharts(null);
229
//                                    sectionDAO.delete(chartSectionId);
230
//                                }
231
//
232
//                                for(String numberSectionId : subcategory.getNumbers()) {
233
//                                    Section<String> numberSection = sectionDAO.findById(numberSectionId);
234
//                                    if (numberSection == null) {
235
//                                        // EXCEPTION - Section not found
236
//                                        throw new EntityNotFoundException("Delete topic: Section with id: "+numberSectionId+" not found (section exists in subcategory: "+subCategoryId+")");
237
//                                    }
238
//
239
//                                    for (String numberId : numberSection.getIndicators()) {
240
//                                        indicatorDAO.delete(numberId);
241
//                                    }
242
//                                    subcategory.setNumbers(null);
243
//                                    sectionDAO.delete(numberSectionId);
244
//                                }
245
//
246
//                                subCategoryDAO.delete(subCategoryId);
247
//                            }
248
                            subCategoryController.deleteTree(category);
217 249

  
218
                                    for (String chartId : chartSection.getIndicators()) {
219
                                        indicatorDAO.delete(chartId);
220
                                    }
221
                                    subcategory.setCharts(null);
222
                                    sectionDAO.delete(chartSectionId);
223
                                }
224

  
225
                                for(String numberSectionId : subcategory.getNumbers()) {
226
                                    Section<String> numberSection = sectionDAO.findById(numberSectionId);
227
                                    if (numberSection == null) {
228
                                        // EXCEPTION - Section not found
229
                                        throw new EntityNotFoundException("Delete topic: Section with id: "+numberSectionId+" not found (section exists in subcategory: "+subCategoryId+")");
230
                                    }
231

  
232
                                    for (String numberId : numberSection.getIndicators()) {
233
                                        indicatorDAO.delete(numberId);
234
                                    }
235
                                    subcategory.setNumbers(null);
236
                                    sectionDAO.delete(numberSectionId);
237
                                }
238

  
239
                                subCategoryDAO.delete(subCategoryId);
240
                            }
241 250
                            category.setSubCategories(null);
242 251

  
243 252
                            categories.remove(index);
......
269 278
        return true;
270 279
    }
271 280

  
281

  
282
    public boolean onDeleteDefaultCategory(String defaultCategoryId, String defaultTopicId, String children) {
283
        if(children.equals("delete")) {
284
            List<Topic> topics = topicDAO.findByDefaultId(defaultTopicId);
285
            List<Category> categories = categoryDAO.findByDefaultId(defaultCategoryId);
286

  
287
            for(Topic topic : topics) {
288
                Iterator<Category> categoriesIterator = categories.iterator();
289
                while(categoriesIterator.hasNext()) {
290
                    Category category = categoriesIterator.next();
291

  
292
                    String categoryId = category.getId();
293

  
294
                    if(topic.getCategories() != null && topic.getCategories().contains(categoryId)) {
295
                        categoriesIterator.remove();
296

  
297
                        topic.getCategories().remove(categoryId);
298
                        topicDAO.save(topic);
299

  
300
                        subCategoryController.deleteTree(category);
301

  
302
                        categoryDAO.delete(categoryId);
303
                        log.debug("Category with id: "+categoryId+" deleted!");
304

  
305
                        break;
306
                    }
307
                }
308
            }
309
        } else if(children.equals("disconnect")) {
310
            List<Category> categories = categoryDAO.findByDefaultId(defaultCategoryId);
311
            for(Category category : categories) {
312
                subCategoryController.disConnectTree(category);
313

  
314
                category.setDefaultId(null);
315
                categoryDAO.save(category);
316

  
317
                log.debug("DefaultId for Category with id: "+category.getId()+" empty!");
318
            }
319
        }
320
        return true;
321
    }
322

  
272 323
    @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/toggle-status", method = RequestMethod.POST)
273 324
    public Boolean toggleCategoryStatus(@PathVariable("stakeholderId") String stakeholderId,
274 325
                                        @PathVariable("topicId") String topicId,
......
335 386
            throw new EntityNotFoundException("Toggle category: Stakeholder with id: "+stakeholderId+" not found");
336 387
        }
337 388
    }
389

  
390
    public void deleteTree(Topic topic) {
391
        List<String> categories = topic.getCategories();
392
        for(String categoryId : categories) {
393
            Category category = categoryDAO.findById(categoryId);
394
            if (category == null) {
395
                // EXCEPTION - Category not found
396
                throw new EntityNotFoundException("Category delete tree: Category with id: "+categoryId+" not found (category exists in topic: "+topic.getId()+")");
397
            }
398

  
399
            subCategoryController.deleteTree(category);
400

  
401
            categoryDAO.delete(categoryId);
402
        }
403
    }
404

  
405
    public void disConnectTree(Topic topic) {
406
        List<String> categories = topic.getCategories();
407
        for(String categoryId : categories) {
408
            Category category = categoryDAO.findById(categoryId);
409
            if (category == null) {
410
                // EXCEPTION - Category not found
411
                throw new EntityNotFoundException("Category disconnect tree: Category with id: "+categoryId+" not found (category exists in topic: "+topic.getId()+")");
412
            }
413

  
414
            subCategoryController.disConnectTree(category);
415

  
416
            category.setDefaultId(null);
417
            categoryDAO.save(category);
418
        }
419
    }
338 420
}
modules/uoa-monitor-service/trunk/src/main/java/eu/dnetlib/uoamonitorservice/controllers/TopicController.java
9 9
import org.springframework.web.bind.annotation.*;
10 10

  
11 11
import java.util.ArrayList;
12
import java.util.Iterator;
12 13
import java.util.List;
13 14

  
14 15
@RestController
......
162 163

  
163 164
    @RequestMapping(value = "/{stakeholderId}/{topicId}/delete", method = RequestMethod.DELETE)
164 165
    public boolean deleteTopic(@PathVariable("stakeholderId") String stakeholderId,
165
                               @PathVariable("topicId") String topicId) {
166
                               @PathVariable("topicId") String topicId,
167
                               @RequestParam(required = false) String children) {
166 168
        log.debug("delete topic");
167 169
        log.debug("Id: "+topicId + " - Stakeholder: "+stakeholderId);
168 170

  
......
176 178
                List<String> topics = stakeholder.getTopics();
177 179
                int index = topics.indexOf(topicId);
178 180
                if(index != -1) {
179
                    for(String categoryId : topic.getCategories()) {
180
                        Category<String> category = categoryDAO.findById(categoryId);
181
                        if(category == null) {
182
                            // EXCEPTION - Category not found
183
                            throw new EntityNotFoundException("Delete topic: Category with id: "+categoryId+" not found (category exists in topic: "+topicId+")");
184
                        }
181
                    // this topic belongs in default profile
182
                    if(stakeholder.getDefaultId() == null && children != null) {
183
                        onDeleteDefaultTopic(topicId, stakeholderId, children);
184
                    }
185 185

  
186
                        for(String subCategoryId : category.getSubCategories()) {
187
                            SubCategory<String> subcategory = subCategoryDAO.findById(subCategoryId);
188
                            if (subcategory == null) {
189
                                // EXCEPTION - SubCategory not found
190
                                throw new EntityNotFoundException("Delete topic: SubCategory with id: "+subCategoryId+" not found (subcategory exists in category: "+categoryId+")");
191
                            }
186
//                    for(String categoryId : topic.getCategories()) {
187
//                        Category<String> category = categoryDAO.findById(categoryId);
188
//                        if(category == null) {
189
//                            // EXCEPTION - Category not found
190
//                            throw new EntityNotFoundException("Delete topic: Category with id: "+categoryId+" not found (category exists in topic: "+topicId+")");
191
//                        }
192
//
193
//                        for(String subCategoryId : category.getSubCategories()) {
194
//                            SubCategory<String> subcategory = subCategoryDAO.findById(subCategoryId);
195
//                            if (subcategory == null) {
196
//                                // EXCEPTION - SubCategory not found
197
//                                throw new EntityNotFoundException("Delete topic: SubCategory with id: "+subCategoryId+" not found (subcategory exists in category: "+categoryId+")");
198
//                            }
199
//
200
//                            for(String chartSectionId : subcategory.getCharts()) {
201
//                                Section<String> chartSection = sectionDAO.findById(chartSectionId);
202
//                                if (chartSection == null) {
203
//                                    // EXCEPTION - Section not found
204
//                                    throw new EntityNotFoundException("Delete topic: Section with id: "+chartSectionId+" not found (section exists in subcategory: "+subCategoryId+")");
205
//                                }
206
//
207
//                                for (String chartId : chartSection.getIndicators()) {
208
//                                    indicatorDAO.delete(chartId);
209
//                                }
210
//                                subcategory.setCharts(null);
211
//                                sectionDAO.delete(chartSectionId);
212
//                            }
213
//
214
//                            for(String numberSectionId : subcategory.getNumbers()) {
215
//                                Section<String> numberSection = sectionDAO.findById(numberSectionId);
216
//                                if (numberSection == null) {
217
//                                    // EXCEPTION - Section not found
218
//                                    throw new EntityNotFoundException("Delete topic: Section with id: "+numberSectionId+" not found (section exists in subcategory: "+subCategoryId+")");
219
//                                }
220
//
221
//                                for (String numberId : numberSection.getIndicators()) {
222
//                                    indicatorDAO.delete(numberId);
223
//                                }
224
//                                subcategory.setNumbers(null);
225
//                                sectionDAO.delete(numberSectionId);
226
//                            }
227
//
228
//                            subCategoryDAO.delete(subCategoryId);
229
//                        }
230
//                        category.setSubCategories(null);
231
//                        categoryDAO.delete(categoryId);
232
//                    }
233
                    categoryController.deleteTree(topic);
192 234

  
193
                            for(String chartSectionId : subcategory.getCharts()) {
194
                                Section<String> chartSection = sectionDAO.findById(chartSectionId);
195
                                if (chartSection == null) {
196
                                    // EXCEPTION - Section not found
197
                                    throw new EntityNotFoundException("Delete topic: Section with id: "+chartSectionId+" not found (section exists in subcategory: "+subCategoryId+")");
198
                                }
199

  
200
                                for (String chartId : chartSection.getIndicators()) {
201
                                    indicatorDAO.delete(chartId);
202
                                }
203
                                subcategory.setCharts(null);
204
                                sectionDAO.delete(chartSectionId);
205
                            }
206

  
207
                            for(String numberSectionId : subcategory.getNumbers()) {
208
                                Section<String> numberSection = sectionDAO.findById(numberSectionId);
209
                                if (numberSection == null) {
210
                                    // EXCEPTION - Section not found
211
                                    throw new EntityNotFoundException("Delete topic: Section with id: "+numberSectionId+" not found (section exists in subcategory: "+subCategoryId+")");
212
                                }
213

  
214
                                for (String numberId : numberSection.getIndicators()) {
215
                                    indicatorDAO.delete(numberId);
216
                                }
217
                                subcategory.setNumbers(null);
218
                                sectionDAO.delete(numberSectionId);
219
                            }
220

  
221
                            subCategoryDAO.delete(subCategoryId);
222
                        }
223
                        category.setSubCategories(null);
224
                        categoryDAO.delete(categoryId);
225
                    }
226 235
                    topic.setCategories(null);
227 236

  
228 237
                    topics.remove(index);
......
246 255
        return true;
247 256
    }
248 257

  
258

  
259
    public boolean onDeleteDefaultTopic(String defaultTopicId, String defaultStakeholderId, String children) {
260
        if(children.equals("delete")) {
261
            List<Stakeholder> stakeholders = stakeholderDAO.findByDefaultId(defaultStakeholderId);
262
            List<Topic> topics = topicDAO.findByDefaultId(defaultTopicId);
263

  
264
            for(Stakeholder stakeholder : stakeholders) {
265
                Iterator<Topic> topicsIterator = topics.iterator();
266
                while(topicsIterator.hasNext()) {
267
                    Topic topic = topicsIterator.next();
268

  
269
                    String topicId = topic.getId();
270

  
271
                    if(stakeholder.getTopics() != null && stakeholder.getTopics().contains(topicId)) {
272
                        topicsIterator.remove();
273

  
274
                        stakeholder.getTopics().remove(topicId);
275
                        stakeholderDAO.save(stakeholder);
276

  
277
                        categoryController.deleteTree(topic);
278

  
279
                        topicDAO.delete(topicId);
280
                        log.debug("Topic with id: "+topicId+" deleted!");
281

  
282
                        break;
283
                    }
284
                }
285
            }
286
        } else if(children.equals("disconnect")) {
287
            List<Topic> topics = topicDAO.findByDefaultId(defaultTopicId);
288
            for(Topic topic : topics) {
289
                categoryController.disConnectTree(topic);
290

  
291
                topic.setDefaultId(null);
292
                topicDAO.save(topic);
293

  
294
                log.debug("DefaultId for Topic with id: "+topic.getId()+" empty!");
295
            }
296
        }
297
        return true;
298
    }
299

  
249 300
    @RequestMapping(value = "/{stakeholderId}/{topicId}/toggle-status", method = RequestMethod.POST)
250 301
    public Boolean toggleTopicStatus(@PathVariable("stakeholderId") String stakeholderId,
251 302
                                     @PathVariable("topicId") String topicId) {
......
298 349
            throw new EntityNotFoundException("Toggle topic: Stakeholder with id: "+stakeholderId+" not found");
299 350
        }
300 351
    }
352

  
353
    public void deleteTree(Stakeholder stakeholder) {
354
        List<String> topics = stakeholder.getTopics();
355
        for(String topicId : topics) {
356
            Topic topic = topicDAO.findById(topicId);
357
            if (topic == null) {
358
                // EXCEPTION - Topic not found
359
                throw new EntityNotFoundException("Topic delete tree: Topic with id: "+topicId+" not found (topic exists in stakeholder: "+stakeholder.getId()+")");
360
            }
361

  
362
            categoryController.deleteTree(topic);
363

  
364
            topicDAO.delete(topicId);
365
        }
366
    }
301 367
}
modules/uoa-monitor-service/trunk/src/main/java/eu/dnetlib/uoamonitorservice/controllers/SectionController.java
9 9
import org.springframework.web.bind.annotation.*;
10 10

  
11 11
import java.util.ArrayList;
12
import java.util.Iterator;
12 13
import java.util.List;
13 14

  
14 15
@RestController
......
34 35
    @Autowired
35 36
    private IndicatorDAO indicatorDAO;
36 37

  
38
    @Autowired
39
    private IndicatorController indicatorController;
40

  
37 41
    public Section<Indicator> buildSection(Section<Indicator> sectionFull) {
38 42
        Section<String> section = new Section<>(sectionFull);
39 43

  
......
176 180

  
177 181
    @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/{sectionId}/delete", method = RequestMethod.DELETE)
178 182
    public boolean deleteSection(@PathVariable("stakeholderId") String stakeholderId,
179
                                   @PathVariable("topicId") String topicId,
180
                                   @PathVariable("categoryId") String categoryId,
181
                                   @PathVariable("subcategoryId") String subcategoryId,
182
                                   @PathVariable("sectionId") String sectionId) {
183
                                 @PathVariable("topicId") String topicId,
184
                                 @PathVariable("categoryId") String categoryId,
185
                                 @PathVariable("subcategoryId") String subcategoryId,
186
                                 @PathVariable("sectionId") String sectionId,
187
                                 @RequestParam(required = false) String children) {
183 188
        log.debug("delete section");
184 189
        log.debug("Id: "+sectionId + " - Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId+ " - SubCategory: "+subcategoryId);
185 190

  
......
187 192
        if(section != null) {
188 193
            SubCategory<String> subCategory = checkForExceptions(stakeholderId, topicId, categoryId, subcategoryId);
189 194

  
195
            String type = "";
190 196
            List<String> sections = null;
191 197
            if (section.getType().equals("chart")) {
192 198
                sections = subCategory.getCharts();
199
                type = "chart";
193 200
            } else if (section.getType().equals("number")) {
194 201
                sections = subCategory.getNumbers();
202
                type = "number";
195 203
            }
196 204

  
197 205
            int index = sections.indexOf(sectionId);
198 206
            if (index != -1) {
207
                // this section belongs in default profile
208
                if(subCategory.getDefaultId() == null && children != null) {
209
                    onDeleteDefaultSection(sectionId, subcategoryId, children, type);
210
                }
211

  
212
                indicatorController.deleteTree(section);
213

  
199 214
                sections.remove(index);
200 215
                subCategoryDAO.save(subCategory);
201 216

  
......
212 227
        return true;
213 228
    }
214 229

  
230
    public boolean onDeleteDefaultSection(String defaultSectionId, String defaultSubCategoryId, String children, String type) {
231
        if(children.equals("delete")) {
232
            List<SubCategory> subCategories = subCategoryDAO.findByDefaultId(defaultSubCategoryId);
233
            List<Section> sections = sectionDAO.findByDefaultId(defaultSectionId);
234

  
235
            for(SubCategory subCategory : subCategories) {
236
                Iterator<Section> sectionsIterator = sections.iterator();
237
                while(sectionsIterator.hasNext()) {
238
                    Section section = sectionsIterator.next();
239

  
240
                    String sectionId = section.getId();
241
                    List<String> subCategorySections = null;
242
                    if(type.equals("chart")) {
243
                        subCategorySections = subCategory.getCharts();
244
                    } else if(type.equals("number")) {
245
                        subCategorySections = subCategory.getNumbers();
246
                    }
247
                    if(subCategorySections != null && subCategorySections.contains(sectionId)) {
248
                        sectionsIterator.remove();
249

  
250
                        subCategorySections.remove(sectionId);
251
                        subCategoryDAO.save(subCategory);
252

  
253
                        indicatorController.deleteTree(section);
254

  
255
                        sectionDAO.delete(sectionId);
256
                        log.debug("Section with id: "+sectionId+" deleted!");
257

  
258
                        break;
259
                    }
260
                }
261
            }
262
        } else if(children.equals("disconnect")) {
263
            List<Section> sections = sectionDAO.findByDefaultId(defaultSectionId);
264
            for(Section section : sections) {
265
                indicatorController.disConnectTree(section);
266

  
267
                section.setDefaultId(null);
268
                sectionDAO.save(section);
269

  
270
                log.debug("DefaultId for Section with id: "+section.getId()+" empty!");
271
            }
272
        }
273
        return true;
274
    }
275

  
215 276
    @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/{type}/reorder", method = RequestMethod.POST)
216 277
    public List<Section> reorderSections(@PathVariable("stakeholderId") String stakeholderId,
217 278
                                         @PathVariable("topicId") String topicId,
......
346 407

  
347 408
        return  subcategory;
348 409
    }
410

  
411
    public void deleteTree(SubCategory subCategory) {
412
        List<String> sections = subCategory.getCharts();
413
        for(String sectionId : sections) {
414
            Section section = sectionDAO.findById(sectionId);
415
            if (section == null) {
416
                // EXCEPTION - Section not found
417
                throw new EntityNotFoundException("Section delete tree: Chart Section with id: "+sectionId+" not found (section exists in subCategory: "+subCategory.getId()+")");
418
            }
419

  
420
            indicatorController.deleteTree(section);
421

  
422
            sectionDAO.delete(sectionId);
423
        }
424

  
425
        sections = subCategory.getNumbers();
426
        for(String sectionId : sections) {
427
            Section section = sectionDAO.findById(sectionId);
428
            if (section == null) {
429
                // EXCEPTION - Section not found
430
                throw new EntityNotFoundException("Section delete tree: Number Section with id: "+sectionId+" not found (section exists in subCategory: "+subCategory.getId()+")");
431
            }
432

  
433
            indicatorController.deleteTree(section);
434

  
435
            sectionDAO.delete(sectionId);
436
        }
437
    }
438

  
439
    public void disConnectTree(SubCategory subCategory) {
440
        List<String> sections = subCategory.getCharts();
441
        for(String sectionId : sections) {
442
            Section section = sectionDAO.findById(sectionId);
443
            if (section == null) {
444
                // EXCEPTION - Section not found
445
                throw new EntityNotFoundException("Section disconnect tree: Chart Section with id: "+sectionId+" not found (section exists in subCategory: "+subCategory.getId()+")");
446
            }
447

  
448
            indicatorController.disConnectTree(section);
449

  
450
            section.setDefaultId(null);
451
            sectionDAO.save(section);
452
        }
453

  
454
        sections = subCategory.getNumbers();
455
        for(String sectionId : sections) {
456
            Section section = sectionDAO.findById(sectionId);
457
            if (section == null) {
458
                // EXCEPTION - Section not found
459
                throw new EntityNotFoundException("Section disconnect tree: Number Section with id: "+sectionId+" not found (section exists in subCategory: "+subCategory.getId()+")");
460
            }
461

  
462
            indicatorController.disConnectTree(section);
463

  
464
            section.setDefaultId(null);
465
            sectionDAO.save(section);
466
        }
467
    }
349 468
}
modules/uoa-monitor-service/trunk/src/main/java/eu/dnetlib/uoamonitorservice/controllers/StakeholderController.java
293 293

  
294 294
        if(stakeholder != null) {
295 295

  
296
            for(String topicId : stakeholder.getTopics()) {
297
                Topic<String> topic = topicDAO.findById(topicId);
298
                if (topic == null) {
299
                    // EXCEPTION - Topic not found
300
                    throw new EntityNotFoundException("Delete stakeholder: Topic with id: "+topicId+" not found (topic exists in stakeholder: "+stakeholderId+")");
301
                }
296
//            for(String topicId : stakeholder.getTopics()) {
297
//                Topic<String> topic = topicDAO.findById(topicId);
298
//                if (topic == null) {
299
//                    // EXCEPTION - Topic not found
300
//                    throw new EntityNotFoundException("Delete stakeholder: Topic with id: "+topicId+" not found (topic exists in stakeholder: "+stakeholderId+")");
301
//                }
302
//
303
//                for (String categoryId : topic.getCategories()) {
304
//                    Category<String> category = categoryDAO.findById(categoryId);
305
//                    if (category == null) {
306
//                        // EXCEPTION - Category not found
307
//                        throw new EntityNotFoundException("Delete stakeholder: Category with id: "+categoryId+" not found (category exists in topic: "+topicId+")");
308
//                    }
309
//
310
//                    for (String subCategoryId : category.getSubCategories()) {
311
//                        SubCategory<String> subcategory = subCategoryDAO.findById(subCategoryId);
312
//                        if (subcategory == null) {
313
//                            // EXCEPTION - SubCategory not found
314
//                            throw new EntityNotFoundException("Delete stakeholder: SubCategory with id: "+subCategoryId+" not found (subcategory exists in category: "+categoryId+")");
315
//                        }
316
//
317
//                        for(String chartSectionId : subcategory.getCharts()) {
318
//                            Section<String> chartSection = sectionDAO.findById(chartSectionId);
319
//                            if (chartSection == null) {
320
//                                // EXCEPTION - Section not found
321
//                                throw new EntityNotFoundException("Delete topic: Section with id: "+chartSectionId+" not found (section exists in subcategory: "+subCategoryId+")");
322
//                            }
323
//
324
//                            for (String chartId : chartSection.getIndicators()) {
325
//                                indicatorDAO.delete(chartId);
326
//                            }
327
//                            subcategory.setCharts(null);
328
//                            sectionDAO.delete(chartSectionId);
329
//                        }
330
//
331
//                        for(String numberSectionId : subcategory.getNumbers()) {
332
//                            Section<String> numberSection = sectionDAO.findById(numberSectionId);
333
//                            if (numberSection == null) {
334
//                                // EXCEPTION - Section not found
335
//                                throw new EntityNotFoundException("Delete topic: Section with id: "+numberSectionId+" not found (section exists in subcategory: "+subCategoryId+")");
336
//                            }
337
//
338
//                            for (String numberId : numberSection.getIndicators()) {
339
//                                indicatorDAO.delete(numberId);
340
//                            }
341
//                            subcategory.setNumbers(null);
342
//                            sectionDAO.delete(numberSectionId);
343
//                        }
344
//
345
//                        subCategoryDAO.delete(subCategoryId);
346
//                    }
347
//                    category.setSubCategories(null);
348
//                    categoryDAO.delete(categoryId);
349
//                }
350
//                topic.setCategories(null);
351
//                topicDAO.delete(topicId);
352
//            }
302 353

  
303
                for (String categoryId : topic.getCategories()) {
304
                    Category<String> category = categoryDAO.findById(categoryId);
305
                    if (category == null) {
306
                        // EXCEPTION - Category not found
307
                        throw new EntityNotFoundException("Delete stakeholder: Category with id: "+categoryId+" not found (category exists in topic: "+topicId+")");
308
                    }
354
            topicController.deleteTree(stakeholder);
309 355

  
310
                    for (String subCategoryId : category.getSubCategories()) {
311
                        SubCategory<String> subcategory = subCategoryDAO.findById(subCategoryId);
312
                        if (subcategory == null) {
313
                            // EXCEPTION - SubCategory not found
314
                            throw new EntityNotFoundException("Delete stakeholder: SubCategory with id: "+subCategoryId+" not found (subcategory exists in category: "+categoryId+")");
315
                        }
316

  
317
                        for(String chartSectionId : subcategory.getCharts()) {
318
                            Section<String> chartSection = sectionDAO.findById(chartSectionId);
319
                            if (chartSection == null) {
320
                                // EXCEPTION - Section not found
321
                                throw new EntityNotFoundException("Delete topic: Section with id: "+chartSectionId+" not found (section exists in subcategory: "+subCategoryId+")");
322
                            }
323

  
324
                            for (String chartId : chartSection.getIndicators()) {
325
                                indicatorDAO.delete(chartId);
326
                            }
327
                            subcategory.setCharts(null);
328
                            sectionDAO.delete(chartSectionId);
329
                        }
330

  
331
                        for(String numberSectionId : subcategory.getNumbers()) {
332
                            Section<String> numberSection = sectionDAO.findById(numberSectionId);
333
                            if (numberSection == null) {
334
                                // EXCEPTION - Section not found
335
                                throw new EntityNotFoundException("Delete topic: Section with id: "+numberSectionId+" not found (section exists in subcategory: "+subCategoryId+")");
336
                            }
337

  
338
                            for (String numberId : numberSection.getIndicators()) {
339
                                indicatorDAO.delete(numberId);
340
                            }
341
                            subcategory.setNumbers(null);
342
                            sectionDAO.delete(numberSectionId);
343
                        }
344

  
345
                        subCategoryDAO.delete(subCategoryId);
346
                    }
347
                    category.setSubCategories(null);
348
                    categoryDAO.delete(categoryId);
349
                }
350
                topic.setCategories(null);
351
                topicDAO.delete(topicId);
352
            }
353 356
            stakeholder.setTopics(null);
354 357
            stakeholderDAO.delete(stakeholderId);
355 358
            log.debug("Stakeholder deleted!");
modules/uoa-monitor-service/trunk/src/main/java/eu/dnetlib/uoamonitorservice/controllers/SubCategoryController.java
9 9
import org.springframework.web.bind.annotation.*;
10 10

  
11 11
import java.util.ArrayList;
12
import java.util.Iterator;
12 13
import java.util.List;
13 14

  
14 15
@RestController
......
188 189
    public boolean deleteSubCategory(@PathVariable("stakeholderId") String stakeholderId,
189 190
                                     @PathVariable("topicId") String topicId,
190 191
                                     @PathVariable("categoryId") String categoryId,
191
                                     @PathVariable("subcategoryId") String subcategoryId) {
192
                                     @PathVariable("subcategoryId") String subcategoryId,
193
                                     @RequestParam(required = false) String children) {
192 194
        log.debug("delete subcategory");
193 195
        log.debug("Id: "+subcategoryId + " - Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId);
194 196

  
......
199 201
            List<String> subcategories = category.getSubCategories();
200 202
            int index = subcategories.indexOf(subcategoryId);
201 203
            if(index != -1) {
202
                for(String chartSectionId : subcategory.getCharts()) {
203
                    Section<String> chartSection = sectionDAO.findById(chartSectionId);
204
                    if (chartSection == null) {
205
                        // EXCEPTION - Section not found
206
                        throw new EntityNotFoundException("Delete SubCategory: Section with id: "+chartSectionId+" not found (section exists in subcategory: "+subcategoryId+")");
207
                    }
208

  
209
                    for (String chartId : chartSection.getIndicators()) {
210
                        indicatorDAO.delete(chartId);
211
                    }
212
                    subcategory.setCharts(null);
213
                    sectionDAO.delete(chartSectionId);
204
                // this subCategory belongs in default profile
205
                if(category.getDefaultId() == null && children != null) {
206
                    onDeleteDefaultSubCategory(subcategoryId, categoryId, children);
214 207
                }
215 208

  
216
                for(String numberSectionId : subcategory.getNumbers()) {
217
                    Section<String> numberSection = sectionDAO.findById(numberSectionId);
218
                    if (numberSection == null) {
219
                        // EXCEPTION - Section not found
220
                        throw new EntityNotFoundException("Delete SubCategory: Section with id: "+numberSectionId+" not found (section exists in subcategory: "+subcategoryId+")");
221
                    }
209
//                for(String chartSectionId : subcategory.getCharts()) {
210
//                    Section<String> chartSection = sectionDAO.findById(chartSectionId);
211
//                    if (chartSection == null) {
212
//                        // EXCEPTION - Section not found
213
//                        throw new EntityNotFoundException("Delete SubCategory: Section with id: "+chartSectionId+" not found (section exists in subcategory: "+subcategoryId+")");
214
//                    }
215
//
216
//                    for (String chartId : chartSection.getIndicators()) {
217
//                        indicatorDAO.delete(chartId);
218
//                    }
219
//                    subcategory.setCharts(null);
220
//                    sectionDAO.delete(chartSectionId);
221
//                }
222
//
223
//                for(String numberSectionId : subcategory.getNumbers()) {
224
//                    Section<String> numberSection = sectionDAO.findById(numberSectionId);
225
//                    if (numberSection == null) {
226
//                        // EXCEPTION - Section not found
227
//                        throw new EntityNotFoundException("Delete SubCategory: Section with id: "+numberSectionId+" not found (section exists in subcategory: "+subcategoryId+")");
228
//                    }
229
//
230
//                    for (String numberId : numberSection.getIndicators()) {
231
//                        indicatorDAO.delete(numberId);
232
//                    }
233
//                    subcategory.setNumbers(null);
234
//                    sectionDAO.delete(numberSectionId);
235
//                }
222 236

  
223
                    for (String numberId : numberSection.getIndicators()) {
224
                        indicatorDAO.delete(numberId);
225
                    }
226
                    subcategory.setNumbers(null);
227
                    sectionDAO.delete(numberSectionId);
228
                }
237
                sectionController.deleteTree(subcategory);
229 238

  
239
                subcategory.setCharts(null);
240
                subcategory.setNumbers(null);
241

  
230 242
                subcategories.remove(index);
231 243
                categoryDAO.save(category);
232 244

  
......
244 256
        return true;
245 257
    }
246 258

  
259
    public boolean onDeleteDefaultSubCategory(String defaultSubCategoryId, String defaultCategoryId, String children) {
260
        if(children.equals("delete")) {
261
            List<Category> categories = categoryDAO.findByDefaultId(defaultCategoryId);
262
            List<SubCategory> subCategories = subCategoryDAO.findByDefaultId(defaultSubCategoryId);
263

  
264
            for(Category category : categories) {
265
                Iterator<SubCategory> subCategoriesIterator = subCategories.iterator();
266
                while(subCategoriesIterator.hasNext()) {
267
                    SubCategory subCategory = subCategoriesIterator.next();
268

  
269
                    String subCategoryId = subCategory.getId();
270

  
271
                    if(category.getSubCategories() != null && category.getSubCategories().contains(subCategoryId)) {
272
                        subCategoriesIterator.remove();
273

  
274
                        category.getSubCategories().remove(subCategoryId);
275
                        categoryDAO.save(category);
276

  
277
                        sectionController.deleteTree(subCategory);
278

  
279
                        subCategoryDAO.delete(subCategoryId);
280
                        log.debug("SubCategory with id: "+subCategoryId+" deleted!");
281

  
282
                        break;
283
                    }
284
                }
285
            }
286
        } else if(children.equals("disconnect")) {
287
            List<SubCategory> subCategories = subCategoryDAO.findByDefaultId(defaultSubCategoryId);
288
            for(SubCategory subCategory : subCategories) {
289
                sectionController.disConnectTree(subCategory);
290

  
291
                subCategory.setDefaultId(null);
292
                subCategoryDAO.save(subCategory);
293

  
294
                log.debug("DefaultId for SubCategory with id: "+subCategory.getId()+" empty!");
295
            }
296
        }
297
        return true;
298
    }
299

  
247 300
    @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/toggle-status", method = RequestMethod.POST)
248 301
    public Boolean toggleSubCategoryStatus(@PathVariable("stakeholderId") String stakeholderId,
249 302
                                           @PathVariable("topicId") String topicId,
......
342 395
        return  category;
343 396
    }
344 397

  
398
    public void deleteTree(Category category) {
399
        List<String> subCategories = category.getSubCategories();
400
        for(String subCategoryId : subCategories) {
401
            SubCategory subCategory = subCategoryDAO.findById(subCategoryId);
402
            if (subCategory == null) {
403
                // EXCEPTION - SubCategory not found
404
                throw new EntityNotFoundException("SubCategory delete tree: SubCategory with id: "+subCategoryId+" not found (subCategory exists in category: "+category.getId()+")");
405
            }
406

  
407
            sectionController.deleteTree(subCategory);
408

  
409
            subCategoryDAO.delete(subCategoryId);
410
        }
411
    }
412

  
413
    public void disConnectTree(Category category) {
414
        List<String> subCategories = category.getSubCategories();
415
        for(String subCategoryId : subCategories) {
416
            SubCategory subCategory = subCategoryDAO.findById(subCategoryId);
417
            if (subCategory == null) {
418
                // EXCEPTION - SubCategory not found
419
                throw new EntityNotFoundException("SubCategory disconnect tree: SubCategory with id: "+subCategoryId+" not found (subCategory exists in category: "+category.getId()+")");
420
            }
421

  
422
            sectionController.disConnectTree(subCategory);
423

  
424
            subCategory.setDefaultId(null);
425
            subCategoryDAO.save(subCategory);
426
        }
427
    }
345 428
}

Also available in: Unified diff