diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/CategoryController.java b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/CategoryController.java index ccd7518..aa2728b 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/CategoryController.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/CategoryController.java @@ -9,6 +9,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import java.util.ArrayList; +import java.util.Iterator; import java.util.List; @RestController @@ -183,7 +184,8 @@ public class CategoryController { @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/delete", method = RequestMethod.DELETE) public boolean deleteCategory(@PathVariable("stakeholderId") String stakeholderId, @PathVariable("topicId") String topicId, - @PathVariable("categoryId") String categoryId) { + @PathVariable("categoryId") String categoryId, + @RequestParam(required = false) String children) { log.debug("delete category"); log.debug("Id: "+categoryId + " - Stakeholder: "+stakeholderId + " - Topic: "+topicId); @@ -201,43 +203,50 @@ public class CategoryController { List categories = topic.getCategories(); int index = categories.indexOf(categoryId); if(index != -1) { - for(String subCategoryId : category.getSubCategories()) { - SubCategory subcategory = subCategoryDAO.findById(subCategoryId); - if(subcategory == null) { - // EXCEPTION - SubCategory not found - throw new EntityNotFoundException("Delete category: SubCategory with id: "+subCategoryId+" not found (subcategory exists in category: "+categoryId+")"); - } - - for(String chartSectionId : subcategory.getCharts()) { - Section chartSection = sectionDAO.findById(chartSectionId); - if (chartSection == null) { - // EXCEPTION - Section not found - throw new EntityNotFoundException("Delete topic: Section with id: "+chartSectionId+" not found (section exists in subcategory: "+subCategoryId+")"); - } - - for (String chartId : chartSection.getIndicators()) { - indicatorDAO.delete(chartId); - } - subcategory.setCharts(null); - sectionDAO.delete(chartSectionId); - } - - for(String numberSectionId : subcategory.getNumbers()) { - Section numberSection = sectionDAO.findById(numberSectionId); - if (numberSection == null) { - // EXCEPTION - Section not found - throw new EntityNotFoundException("Delete topic: Section with id: "+numberSectionId+" not found (section exists in subcategory: "+subCategoryId+")"); - } - - for (String numberId : numberSection.getIndicators()) { - indicatorDAO.delete(numberId); - } - subcategory.setNumbers(null); - sectionDAO.delete(numberSectionId); - } - - subCategoryDAO.delete(subCategoryId); + // this category belongs in default profile + if(topic.getDefaultId() == null && children != null) { + onDeleteDefaultCategory(categoryId, topicId, children); } + +// for(String subCategoryId : category.getSubCategories()) { +// SubCategory subcategory = subCategoryDAO.findById(subCategoryId); +// if(subcategory == null) { +// // EXCEPTION - SubCategory not found +// throw new EntityNotFoundException("Delete category: SubCategory with id: "+subCategoryId+" not found (subcategory exists in category: "+categoryId+")"); +// } +// +// for(String chartSectionId : subcategory.getCharts()) { +// Section chartSection = sectionDAO.findById(chartSectionId); +// if (chartSection == null) { +// // EXCEPTION - Section not found +// throw new EntityNotFoundException("Delete topic: Section with id: "+chartSectionId+" not found (section exists in subcategory: "+subCategoryId+")"); +// } +// +// for (String chartId : chartSection.getIndicators()) { +// indicatorDAO.delete(chartId); +// } +// subcategory.setCharts(null); +// sectionDAO.delete(chartSectionId); +// } +// +// for(String numberSectionId : subcategory.getNumbers()) { +// Section numberSection = sectionDAO.findById(numberSectionId); +// if (numberSection == null) { +// // EXCEPTION - Section not found +// throw new EntityNotFoundException("Delete topic: Section with id: "+numberSectionId+" not found (section exists in subcategory: "+subCategoryId+")"); +// } +// +// for (String numberId : numberSection.getIndicators()) { +// indicatorDAO.delete(numberId); +// } +// subcategory.setNumbers(null); +// sectionDAO.delete(numberSectionId); +// } +// +// subCategoryDAO.delete(subCategoryId); +// } + subCategoryController.deleteTree(category); + category.setSubCategories(null); categories.remove(index); @@ -269,6 +278,48 @@ public class CategoryController { return true; } + + public boolean onDeleteDefaultCategory(String defaultCategoryId, String defaultTopicId, String children) { + if(children.equals("delete")) { + List topics = topicDAO.findByDefaultId(defaultTopicId); + List categories = categoryDAO.findByDefaultId(defaultCategoryId); + + for(Topic topic : topics) { + Iterator categoriesIterator = categories.iterator(); + while(categoriesIterator.hasNext()) { + Category category = categoriesIterator.next(); + + String categoryId = category.getId(); + + if(topic.getCategories() != null && topic.getCategories().contains(categoryId)) { + categoriesIterator.remove(); + + topic.getCategories().remove(categoryId); + topicDAO.save(topic); + + subCategoryController.deleteTree(category); + + categoryDAO.delete(categoryId); + log.debug("Category with id: "+categoryId+" deleted!"); + + break; + } + } + } + } else if(children.equals("disconnect")) { + List categories = categoryDAO.findByDefaultId(defaultCategoryId); + for(Category category : categories) { + subCategoryController.disConnectTree(category); + + category.setDefaultId(null); + categoryDAO.save(category); + + log.debug("DefaultId for Category with id: "+category.getId()+" empty!"); + } + } + return true; + } + @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/toggle-status", method = RequestMethod.POST) public Boolean toggleCategoryStatus(@PathVariable("stakeholderId") String stakeholderId, @PathVariable("topicId") String topicId, @@ -335,4 +386,35 @@ public class CategoryController { throw new EntityNotFoundException("Toggle category: Stakeholder with id: "+stakeholderId+" not found"); } } + + public void deleteTree(Topic topic) { + List categories = topic.getCategories(); + for(String categoryId : categories) { + Category category = categoryDAO.findById(categoryId); + if (category == null) { + // EXCEPTION - Category not found + throw new EntityNotFoundException("Category delete tree: Category with id: "+categoryId+" not found (category exists in topic: "+topic.getId()+")"); + } + + subCategoryController.deleteTree(category); + + categoryDAO.delete(categoryId); + } + } + + public void disConnectTree(Topic topic) { + List categories = topic.getCategories(); + for(String categoryId : categories) { + Category category = categoryDAO.findById(categoryId); + if (category == null) { + // EXCEPTION - Category not found + throw new EntityNotFoundException("Category disconnect tree: Category with id: "+categoryId+" not found (category exists in topic: "+topic.getId()+")"); + } + + subCategoryController.disConnectTree(category); + + category.setDefaultId(null); + categoryDAO.save(category); + } + } } diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/IndicatorController.java b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/IndicatorController.java index 87c6575..10d28d7 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/IndicatorController.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/IndicatorController.java @@ -11,10 +11,7 @@ import org.springframework.web.bind.annotation.*; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; @RestController @CrossOrigin(origins = "*") @@ -146,9 +143,6 @@ public class IndicatorController { // Check if there are changes in indicator path and update existing indicators if needed log.debug("update indicator path: "+i); - log.debug("indicatorPath.getType(): "+indicatorPath.getType()); - log.debug("indicatorPathBasedOnDefault.getType(): "+indicatorPathBasedOnDefault.getType()); - log.debug("oldIndicatorPath.getType(): "+oldIndicatorPath.getType()); if(indicatorPath.getType() != null && !indicatorPath.getType().equals(indicatorPathBasedOnDefault.getType()) @@ -311,7 +305,8 @@ public class IndicatorController { @PathVariable("categoryId") String categoryId, @PathVariable("subcategoryId") String subcategoryId, @PathVariable("sectionId") String sectionId, - @PathVariable("indicatorId") String indicatorId) { + @PathVariable("indicatorId") String indicatorId, + @RequestParam(required = false) String children) { log.debug("delete indicator"); log.debug("Id: "+indicatorId + " - Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId+ " - SubCategory: "+subcategoryId + " - Section: "+sectionId); @@ -323,6 +318,13 @@ public class IndicatorController { int index = indicators.indexOf(indicatorId); if (index != -1) { + + // this indicator belongs in default profile + if(section.getDefaultId() == null && children != null) { + onDeleteDefaultIndicator(indicatorId, sectionId, children); + } + + indicators.remove(index); sectionDAO.save(section); @@ -339,6 +341,76 @@ public class IndicatorController { return true; } + public boolean onDeleteDefaultIndicator(String defaultIndicatorId, String defaultSectionId, String children) { + if(children.equals("delete")) { +// // 1st way +// List
sections = sectionDAO.findByDefaultId(defaultSectionId); +// +// for(Section section : sections) { +// List indicators = section.getIndicators(); +// +// Iterator indicatorsIterator = indicators.iterator(); +// while(indicatorsIterator.hasNext()) { +// String indicatorId = indicatorsIterator.next(); +// +// Indicator indicator = indicatorDAO.findById(indicatorId); +// if (indicator.getDefaultId().equals(defaultIndicatorId)) { +// indicatorsIterator.remove(); +// sectionDAO.save(section); +// +// indicatorDAO.delete(indicatorId); +// log.debug("Indicator deleted!"); +// +// break; +// } +// } +// } + + // 2nd way + List
sections = sectionDAO.findByDefaultId(defaultSectionId); + List indicators = indicatorDAO.findByDefaultId(defaultIndicatorId); + + for(Section section : sections) { + Iterator indicatorsIterator = indicators.iterator(); + while(indicatorsIterator.hasNext()) { + String indicatorId = indicatorsIterator.next().getId(); + if(section.getIndicators().contains(indicatorId)) { + indicatorsIterator.remove(); + + section.getIndicators().remove(indicatorId); + sectionDAO.save(section); + + indicatorDAO.delete(indicatorId); + log.debug("Indicator with id: "+indicatorId+" deleted!"); + + break; + } + } + } + +// // 3rd way - parentId +// List indicators = indicatorDAO.findByDefaultId(defaultIndicatorId); +// for(Indicator indicator : indicators) { +// Section section = sectionDAO.findById(indicator.getParent()); +// List sectionIndicators = section.getIndicators(); +// +// sectionIndicators.remove(indicator.getId()); +// sectionDAO.save(section); +// +// indicatorDAO.delete(indicator.getId()); +// log.debug("Indicator deleted!"); +// } + } else if(children.equals("disconnect")) { + List indicators = indicatorDAO.findByDefaultId(defaultIndicatorId); + for(Indicator indicator : indicators) { + indicator.setDefaultId(null); + indicatorDAO.save(indicator); + log.debug("DefaultId for Indicator with id: "+indicator.getId()+" empty!"); + } + } + return true; + } + // @RequestMapping(value = "/{stakeholderId}/charts/delete", method = RequestMethod.DELETE) // public boolean deleteAllChartIndicators(@PathVariable("stakeholderId") String stakeholderId) { // log.debug("delete all chart indicators of stakeholder"); @@ -557,4 +629,20 @@ public class IndicatorController { return section; } + + public void deleteTree(Section section) { + List indicators = section.getIndicators(); + for(String indicatorId : indicators) { + indicatorDAO.delete(indicatorId); + } + } + + public void disConnectTree(Section section) { + List indicators = section.getIndicators(); + for(String indicatorId : indicators) { + Indicator indicator = indicatorDAO.findById(indicatorId); + indicator.setDefaultId(null); + indicatorDAO.save(indicator); + } + } } diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/SectionController.java b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/SectionController.java index 939eab1..8661148 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/SectionController.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/SectionController.java @@ -9,6 +9,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import java.util.ArrayList; +import java.util.Iterator; import java.util.List; @RestController @@ -34,6 +35,9 @@ public class SectionController { @Autowired private IndicatorDAO indicatorDAO; + @Autowired + private IndicatorController indicatorController; + public Section buildSection(Section sectionFull) { Section section = new Section<>(sectionFull); @@ -176,10 +180,11 @@ public class SectionController { @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/{sectionId}/delete", method = RequestMethod.DELETE) public boolean deleteSection(@PathVariable("stakeholderId") String stakeholderId, - @PathVariable("topicId") String topicId, - @PathVariable("categoryId") String categoryId, - @PathVariable("subcategoryId") String subcategoryId, - @PathVariable("sectionId") String sectionId) { + @PathVariable("topicId") String topicId, + @PathVariable("categoryId") String categoryId, + @PathVariable("subcategoryId") String subcategoryId, + @PathVariable("sectionId") String sectionId, + @RequestParam(required = false) String children) { log.debug("delete section"); log.debug("Id: "+sectionId + " - Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId+ " - SubCategory: "+subcategoryId); @@ -187,15 +192,25 @@ public class SectionController { if(section != null) { SubCategory subCategory = checkForExceptions(stakeholderId, topicId, categoryId, subcategoryId); + String type = ""; List sections = null; if (section.getType().equals("chart")) { sections = subCategory.getCharts(); + type = "chart"; } else if (section.getType().equals("number")) { sections = subCategory.getNumbers(); + type = "number"; } int index = sections.indexOf(sectionId); if (index != -1) { + // this section belongs in default profile + if(subCategory.getDefaultId() == null && children != null) { + onDeleteDefaultSection(sectionId, subcategoryId, children, type); + } + + indicatorController.deleteTree(section); + sections.remove(index); subCategoryDAO.save(subCategory); @@ -212,6 +227,52 @@ public class SectionController { return true; } + public boolean onDeleteDefaultSection(String defaultSectionId, String defaultSubCategoryId, String children, String type) { + if(children.equals("delete")) { + List subCategories = subCategoryDAO.findByDefaultId(defaultSubCategoryId); + List
sections = sectionDAO.findByDefaultId(defaultSectionId); + + for(SubCategory subCategory : subCategories) { + Iterator
sectionsIterator = sections.iterator(); + while(sectionsIterator.hasNext()) { + Section section = sectionsIterator.next(); + + String sectionId = section.getId(); + List subCategorySections = null; + if(type.equals("chart")) { + subCategorySections = subCategory.getCharts(); + } else if(type.equals("number")) { + subCategorySections = subCategory.getNumbers(); + } + if(subCategorySections != null && subCategorySections.contains(sectionId)) { + sectionsIterator.remove(); + + subCategorySections.remove(sectionId); + subCategoryDAO.save(subCategory); + + indicatorController.deleteTree(section); + + sectionDAO.delete(sectionId); + log.debug("Section with id: "+sectionId+" deleted!"); + + break; + } + } + } + } else if(children.equals("disconnect")) { + List
sections = sectionDAO.findByDefaultId(defaultSectionId); + for(Section section : sections) { + indicatorController.disConnectTree(section); + + section.setDefaultId(null); + sectionDAO.save(section); + + log.debug("DefaultId for Section with id: "+section.getId()+" empty!"); + } + } + return true; + } + @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/{type}/reorder", method = RequestMethod.POST) public List
reorderSections(@PathVariable("stakeholderId") String stakeholderId, @PathVariable("topicId") String topicId, @@ -346,4 +407,62 @@ public class SectionController { return subcategory; } + + public void deleteTree(SubCategory subCategory) { + List sections = subCategory.getCharts(); + for(String sectionId : sections) { + Section section = sectionDAO.findById(sectionId); + if (section == null) { + // EXCEPTION - Section not found + throw new EntityNotFoundException("Section delete tree: Chart Section with id: "+sectionId+" not found (section exists in subCategory: "+subCategory.getId()+")"); + } + + indicatorController.deleteTree(section); + + sectionDAO.delete(sectionId); + } + + sections = subCategory.getNumbers(); + for(String sectionId : sections) { + Section section = sectionDAO.findById(sectionId); + if (section == null) { + // EXCEPTION - Section not found + throw new EntityNotFoundException("Section delete tree: Number Section with id: "+sectionId+" not found (section exists in subCategory: "+subCategory.getId()+")"); + } + + indicatorController.deleteTree(section); + + sectionDAO.delete(sectionId); + } + } + + public void disConnectTree(SubCategory subCategory) { + List sections = subCategory.getCharts(); + for(String sectionId : sections) { + Section section = sectionDAO.findById(sectionId); + if (section == null) { + // EXCEPTION - Section not found + throw new EntityNotFoundException("Section disconnect tree: Chart Section with id: "+sectionId+" not found (section exists in subCategory: "+subCategory.getId()+")"); + } + + indicatorController.disConnectTree(section); + + section.setDefaultId(null); + sectionDAO.save(section); + } + + sections = subCategory.getNumbers(); + for(String sectionId : sections) { + Section section = sectionDAO.findById(sectionId); + if (section == null) { + // EXCEPTION - Section not found + throw new EntityNotFoundException("Section disconnect tree: Number Section with id: "+sectionId+" not found (section exists in subCategory: "+subCategory.getId()+")"); + } + + indicatorController.disConnectTree(section); + + section.setDefaultId(null); + sectionDAO.save(section); + } + } } diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/StakeholderController.java b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/StakeholderController.java index 2fa0bac..f6b1ba5 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/StakeholderController.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/StakeholderController.java @@ -293,63 +293,66 @@ public class StakeholderController { if(stakeholder != null) { - for(String topicId : stakeholder.getTopics()) { - Topic topic = topicDAO.findById(topicId); - if (topic == null) { - // EXCEPTION - Topic not found - throw new EntityNotFoundException("Delete stakeholder: Topic with id: "+topicId+" not found (topic exists in stakeholder: "+stakeholderId+")"); - } +// for(String topicId : stakeholder.getTopics()) { +// Topic topic = topicDAO.findById(topicId); +// if (topic == null) { +// // EXCEPTION - Topic not found +// throw new EntityNotFoundException("Delete stakeholder: Topic with id: "+topicId+" not found (topic exists in stakeholder: "+stakeholderId+")"); +// } +// +// for (String categoryId : topic.getCategories()) { +// Category category = categoryDAO.findById(categoryId); +// if (category == null) { +// // EXCEPTION - Category not found +// throw new EntityNotFoundException("Delete stakeholder: Category with id: "+categoryId+" not found (category exists in topic: "+topicId+")"); +// } +// +// for (String subCategoryId : category.getSubCategories()) { +// SubCategory subcategory = subCategoryDAO.findById(subCategoryId); +// if (subcategory == null) { +// // EXCEPTION - SubCategory not found +// throw new EntityNotFoundException("Delete stakeholder: SubCategory with id: "+subCategoryId+" not found (subcategory exists in category: "+categoryId+")"); +// } +// +// for(String chartSectionId : subcategory.getCharts()) { +// Section chartSection = sectionDAO.findById(chartSectionId); +// if (chartSection == null) { +// // EXCEPTION - Section not found +// throw new EntityNotFoundException("Delete topic: Section with id: "+chartSectionId+" not found (section exists in subcategory: "+subCategoryId+")"); +// } +// +// for (String chartId : chartSection.getIndicators()) { +// indicatorDAO.delete(chartId); +// } +// subcategory.setCharts(null); +// sectionDAO.delete(chartSectionId); +// } +// +// for(String numberSectionId : subcategory.getNumbers()) { +// Section numberSection = sectionDAO.findById(numberSectionId); +// if (numberSection == null) { +// // EXCEPTION - Section not found +// throw new EntityNotFoundException("Delete topic: Section with id: "+numberSectionId+" not found (section exists in subcategory: "+subCategoryId+")"); +// } +// +// for (String numberId : numberSection.getIndicators()) { +// indicatorDAO.delete(numberId); +// } +// subcategory.setNumbers(null); +// sectionDAO.delete(numberSectionId); +// } +// +// subCategoryDAO.delete(subCategoryId); +// } +// category.setSubCategories(null); +// categoryDAO.delete(categoryId); +// } +// topic.setCategories(null); +// topicDAO.delete(topicId); +// } - for (String categoryId : topic.getCategories()) { - Category category = categoryDAO.findById(categoryId); - if (category == null) { - // EXCEPTION - Category not found - throw new EntityNotFoundException("Delete stakeholder: Category with id: "+categoryId+" not found (category exists in topic: "+topicId+")"); - } + topicController.deleteTree(stakeholder); - for (String subCategoryId : category.getSubCategories()) { - SubCategory subcategory = subCategoryDAO.findById(subCategoryId); - if (subcategory == null) { - // EXCEPTION - SubCategory not found - throw new EntityNotFoundException("Delete stakeholder: SubCategory with id: "+subCategoryId+" not found (subcategory exists in category: "+categoryId+")"); - } - - for(String chartSectionId : subcategory.getCharts()) { - Section chartSection = sectionDAO.findById(chartSectionId); - if (chartSection == null) { - // EXCEPTION - Section not found - throw new EntityNotFoundException("Delete topic: Section with id: "+chartSectionId+" not found (section exists in subcategory: "+subCategoryId+")"); - } - - for (String chartId : chartSection.getIndicators()) { - indicatorDAO.delete(chartId); - } - subcategory.setCharts(null); - sectionDAO.delete(chartSectionId); - } - - for(String numberSectionId : subcategory.getNumbers()) { - Section numberSection = sectionDAO.findById(numberSectionId); - if (numberSection == null) { - // EXCEPTION - Section not found - throw new EntityNotFoundException("Delete topic: Section with id: "+numberSectionId+" not found (section exists in subcategory: "+subCategoryId+")"); - } - - for (String numberId : numberSection.getIndicators()) { - indicatorDAO.delete(numberId); - } - subcategory.setNumbers(null); - sectionDAO.delete(numberSectionId); - } - - subCategoryDAO.delete(subCategoryId); - } - category.setSubCategories(null); - categoryDAO.delete(categoryId); - } - topic.setCategories(null); - topicDAO.delete(topicId); - } stakeholder.setTopics(null); stakeholderDAO.delete(stakeholderId); log.debug("Stakeholder deleted!"); diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/SubCategoryController.java b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/SubCategoryController.java index 2af2a5b..ca38883 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/SubCategoryController.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/SubCategoryController.java @@ -9,6 +9,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import java.util.ArrayList; +import java.util.Iterator; import java.util.List; @RestController @@ -188,7 +189,8 @@ public class SubCategoryController { public boolean deleteSubCategory(@PathVariable("stakeholderId") String stakeholderId, @PathVariable("topicId") String topicId, @PathVariable("categoryId") String categoryId, - @PathVariable("subcategoryId") String subcategoryId) { + @PathVariable("subcategoryId") String subcategoryId, + @RequestParam(required = false) String children) { log.debug("delete subcategory"); log.debug("Id: "+subcategoryId + " - Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId); @@ -199,33 +201,43 @@ public class SubCategoryController { List subcategories = category.getSubCategories(); int index = subcategories.indexOf(subcategoryId); if(index != -1) { - for(String chartSectionId : subcategory.getCharts()) { - Section chartSection = sectionDAO.findById(chartSectionId); - if (chartSection == null) { - // EXCEPTION - Section not found - throw new EntityNotFoundException("Delete SubCategory: Section with id: "+chartSectionId+" not found (section exists in subcategory: "+subcategoryId+")"); - } - - for (String chartId : chartSection.getIndicators()) { - indicatorDAO.delete(chartId); - } - subcategory.setCharts(null); - sectionDAO.delete(chartSectionId); + // this subCategory belongs in default profile + if(category.getDefaultId() == null && children != null) { + onDeleteDefaultSubCategory(subcategoryId, categoryId, children); } - for(String numberSectionId : subcategory.getNumbers()) { - Section numberSection = sectionDAO.findById(numberSectionId); - if (numberSection == null) { - // EXCEPTION - Section not found - throw new EntityNotFoundException("Delete SubCategory: Section with id: "+numberSectionId+" not found (section exists in subcategory: "+subcategoryId+")"); - } +// for(String chartSectionId : subcategory.getCharts()) { +// Section chartSection = sectionDAO.findById(chartSectionId); +// if (chartSection == null) { +// // EXCEPTION - Section not found +// throw new EntityNotFoundException("Delete SubCategory: Section with id: "+chartSectionId+" not found (section exists in subcategory: "+subcategoryId+")"); +// } +// +// for (String chartId : chartSection.getIndicators()) { +// indicatorDAO.delete(chartId); +// } +// subcategory.setCharts(null); +// sectionDAO.delete(chartSectionId); +// } +// +// for(String numberSectionId : subcategory.getNumbers()) { +// Section numberSection = sectionDAO.findById(numberSectionId); +// if (numberSection == null) { +// // EXCEPTION - Section not found +// throw new EntityNotFoundException("Delete SubCategory: Section with id: "+numberSectionId+" not found (section exists in subcategory: "+subcategoryId+")"); +// } +// +// for (String numberId : numberSection.getIndicators()) { +// indicatorDAO.delete(numberId); +// } +// subcategory.setNumbers(null); +// sectionDAO.delete(numberSectionId); +// } - for (String numberId : numberSection.getIndicators()) { - indicatorDAO.delete(numberId); - } - subcategory.setNumbers(null); - sectionDAO.delete(numberSectionId); - } + sectionController.deleteTree(subcategory); + + subcategory.setCharts(null); + subcategory.setNumbers(null); subcategories.remove(index); categoryDAO.save(category); @@ -244,6 +256,47 @@ public class SubCategoryController { return true; } + public boolean onDeleteDefaultSubCategory(String defaultSubCategoryId, String defaultCategoryId, String children) { + if(children.equals("delete")) { + List categories = categoryDAO.findByDefaultId(defaultCategoryId); + List subCategories = subCategoryDAO.findByDefaultId(defaultSubCategoryId); + + for(Category category : categories) { + Iterator subCategoriesIterator = subCategories.iterator(); + while(subCategoriesIterator.hasNext()) { + SubCategory subCategory = subCategoriesIterator.next(); + + String subCategoryId = subCategory.getId(); + + if(category.getSubCategories() != null && category.getSubCategories().contains(subCategoryId)) { + subCategoriesIterator.remove(); + + category.getSubCategories().remove(subCategoryId); + categoryDAO.save(category); + + sectionController.deleteTree(subCategory); + + subCategoryDAO.delete(subCategoryId); + log.debug("SubCategory with id: "+subCategoryId+" deleted!"); + + break; + } + } + } + } else if(children.equals("disconnect")) { + List subCategories = subCategoryDAO.findByDefaultId(defaultSubCategoryId); + for(SubCategory subCategory : subCategories) { + sectionController.disConnectTree(subCategory); + + subCategory.setDefaultId(null); + subCategoryDAO.save(subCategory); + + log.debug("DefaultId for SubCategory with id: "+subCategory.getId()+" empty!"); + } + } + return true; + } + @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/toggle-status", method = RequestMethod.POST) public Boolean toggleSubCategoryStatus(@PathVariable("stakeholderId") String stakeholderId, @PathVariable("topicId") String topicId, @@ -342,4 +395,34 @@ public class SubCategoryController { return category; } + public void deleteTree(Category category) { + List subCategories = category.getSubCategories(); + for(String subCategoryId : subCategories) { + SubCategory subCategory = subCategoryDAO.findById(subCategoryId); + if (subCategory == null) { + // EXCEPTION - SubCategory not found + throw new EntityNotFoundException("SubCategory delete tree: SubCategory with id: "+subCategoryId+" not found (subCategory exists in category: "+category.getId()+")"); + } + + sectionController.deleteTree(subCategory); + + subCategoryDAO.delete(subCategoryId); + } + } + + public void disConnectTree(Category category) { + List subCategories = category.getSubCategories(); + for(String subCategoryId : subCategories) { + SubCategory subCategory = subCategoryDAO.findById(subCategoryId); + if (subCategory == null) { + // EXCEPTION - SubCategory not found + throw new EntityNotFoundException("SubCategory disconnect tree: SubCategory with id: "+subCategoryId+" not found (subCategory exists in category: "+category.getId()+")"); + } + + sectionController.disConnectTree(subCategory); + + subCategory.setDefaultId(null); + subCategoryDAO.save(subCategory); + } + } } diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/TopicController.java b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/TopicController.java index 923c23e..09d1071 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/TopicController.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/TopicController.java @@ -9,6 +9,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import java.util.ArrayList; +import java.util.Iterator; import java.util.List; @RestController @@ -162,7 +163,8 @@ public class TopicController { @RequestMapping(value = "/{stakeholderId}/{topicId}/delete", method = RequestMethod.DELETE) public boolean deleteTopic(@PathVariable("stakeholderId") String stakeholderId, - @PathVariable("topicId") String topicId) { + @PathVariable("topicId") String topicId, + @RequestParam(required = false) String children) { log.debug("delete topic"); log.debug("Id: "+topicId + " - Stakeholder: "+stakeholderId); @@ -176,53 +178,60 @@ public class TopicController { List topics = stakeholder.getTopics(); int index = topics.indexOf(topicId); if(index != -1) { - for(String categoryId : topic.getCategories()) { - Category category = categoryDAO.findById(categoryId); - if(category == null) { - // EXCEPTION - Category not found - throw new EntityNotFoundException("Delete topic: Category with id: "+categoryId+" not found (category exists in topic: "+topicId+")"); - } - - for(String subCategoryId : category.getSubCategories()) { - SubCategory subcategory = subCategoryDAO.findById(subCategoryId); - if (subcategory == null) { - // EXCEPTION - SubCategory not found - throw new EntityNotFoundException("Delete topic: SubCategory with id: "+subCategoryId+" not found (subcategory exists in category: "+categoryId+")"); - } - - for(String chartSectionId : subcategory.getCharts()) { - Section chartSection = sectionDAO.findById(chartSectionId); - if (chartSection == null) { - // EXCEPTION - Section not found - throw new EntityNotFoundException("Delete topic: Section with id: "+chartSectionId+" not found (section exists in subcategory: "+subCategoryId+")"); - } - - for (String chartId : chartSection.getIndicators()) { - indicatorDAO.delete(chartId); - } - subcategory.setCharts(null); - sectionDAO.delete(chartSectionId); - } - - for(String numberSectionId : subcategory.getNumbers()) { - Section numberSection = sectionDAO.findById(numberSectionId); - if (numberSection == null) { - // EXCEPTION - Section not found - throw new EntityNotFoundException("Delete topic: Section with id: "+numberSectionId+" not found (section exists in subcategory: "+subCategoryId+")"); - } - - for (String numberId : numberSection.getIndicators()) { - indicatorDAO.delete(numberId); - } - subcategory.setNumbers(null); - sectionDAO.delete(numberSectionId); - } - - subCategoryDAO.delete(subCategoryId); - } - category.setSubCategories(null); - categoryDAO.delete(categoryId); + // this topic belongs in default profile + if(stakeholder.getDefaultId() == null && children != null) { + onDeleteDefaultTopic(topicId, stakeholderId, children); } + +// for(String categoryId : topic.getCategories()) { +// Category category = categoryDAO.findById(categoryId); +// if(category == null) { +// // EXCEPTION - Category not found +// throw new EntityNotFoundException("Delete topic: Category with id: "+categoryId+" not found (category exists in topic: "+topicId+")"); +// } +// +// for(String subCategoryId : category.getSubCategories()) { +// SubCategory subcategory = subCategoryDAO.findById(subCategoryId); +// if (subcategory == null) { +// // EXCEPTION - SubCategory not found +// throw new EntityNotFoundException("Delete topic: SubCategory with id: "+subCategoryId+" not found (subcategory exists in category: "+categoryId+")"); +// } +// +// for(String chartSectionId : subcategory.getCharts()) { +// Section chartSection = sectionDAO.findById(chartSectionId); +// if (chartSection == null) { +// // EXCEPTION - Section not found +// throw new EntityNotFoundException("Delete topic: Section with id: "+chartSectionId+" not found (section exists in subcategory: "+subCategoryId+")"); +// } +// +// for (String chartId : chartSection.getIndicators()) { +// indicatorDAO.delete(chartId); +// } +// subcategory.setCharts(null); +// sectionDAO.delete(chartSectionId); +// } +// +// for(String numberSectionId : subcategory.getNumbers()) { +// Section numberSection = sectionDAO.findById(numberSectionId); +// if (numberSection == null) { +// // EXCEPTION - Section not found +// throw new EntityNotFoundException("Delete topic: Section with id: "+numberSectionId+" not found (section exists in subcategory: "+subCategoryId+")"); +// } +// +// for (String numberId : numberSection.getIndicators()) { +// indicatorDAO.delete(numberId); +// } +// subcategory.setNumbers(null); +// sectionDAO.delete(numberSectionId); +// } +// +// subCategoryDAO.delete(subCategoryId); +// } +// category.setSubCategories(null); +// categoryDAO.delete(categoryId); +// } + categoryController.deleteTree(topic); + topic.setCategories(null); topics.remove(index); @@ -246,6 +255,48 @@ public class TopicController { return true; } + + public boolean onDeleteDefaultTopic(String defaultTopicId, String defaultStakeholderId, String children) { + if(children.equals("delete")) { + List stakeholders = stakeholderDAO.findByDefaultId(defaultStakeholderId); + List topics = topicDAO.findByDefaultId(defaultTopicId); + + for(Stakeholder stakeholder : stakeholders) { + Iterator topicsIterator = topics.iterator(); + while(topicsIterator.hasNext()) { + Topic topic = topicsIterator.next(); + + String topicId = topic.getId(); + + if(stakeholder.getTopics() != null && stakeholder.getTopics().contains(topicId)) { + topicsIterator.remove(); + + stakeholder.getTopics().remove(topicId); + stakeholderDAO.save(stakeholder); + + categoryController.deleteTree(topic); + + topicDAO.delete(topicId); + log.debug("Topic with id: "+topicId+" deleted!"); + + break; + } + } + } + } else if(children.equals("disconnect")) { + List topics = topicDAO.findByDefaultId(defaultTopicId); + for(Topic topic : topics) { + categoryController.disConnectTree(topic); + + topic.setDefaultId(null); + topicDAO.save(topic); + + log.debug("DefaultId for Topic with id: "+topic.getId()+" empty!"); + } + } + return true; + } + @RequestMapping(value = "/{stakeholderId}/{topicId}/toggle-status", method = RequestMethod.POST) public Boolean toggleTopicStatus(@PathVariable("stakeholderId") String stakeholderId, @PathVariable("topicId") String topicId) { @@ -298,4 +349,19 @@ public class TopicController { throw new EntityNotFoundException("Toggle topic: Stakeholder with id: "+stakeholderId+" not found"); } } + + public void deleteTree(Stakeholder stakeholder) { + List topics = stakeholder.getTopics(); + for(String topicId : topics) { + Topic topic = topicDAO.findById(topicId); + if (topic == null) { + // EXCEPTION - Topic not found + throw new EntityNotFoundException("Topic delete tree: Topic with id: "+topicId+" not found (topic exists in stakeholder: "+stakeholder.getId()+")"); + } + + categoryController.deleteTree(topic); + + topicDAO.delete(topicId); + } + } }