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:

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