uoa-monitor-service/src/main/java/eu/dnetlib/uoamonitorservice/controllers/SubCategoryController.java

236 lines
12 KiB
Java

package eu.dnetlib.uoamonitorservice.controllers;
import eu.dnetlib.uoamonitorservice.dto.SubCategoryFull;
import eu.dnetlib.uoamonitorservice.entities.Category;
import eu.dnetlib.uoamonitorservice.entities.Stakeholder;
import eu.dnetlib.uoamonitorservice.entities.SubCategory;
import eu.dnetlib.uoamonitorservice.entities.Topic;
import eu.dnetlib.uoamonitorservice.primitives.Visibility;
import eu.dnetlib.uoamonitorservice.service.CategoryService;
import eu.dnetlib.uoamonitorservice.service.StakeholderService;
import eu.dnetlib.uoamonitorservice.service.SubCategoryService;
import eu.dnetlib.uoamonitorservice.service.TopicService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
@RestController
@CrossOrigin(origins = "*")
public class SubCategoryController {
private final Logger log = LogManager.getLogger(this.getClass());
private final StakeholderService stakeholderService;
private final TopicService topicService;
private final CategoryService categoryService;
private final SubCategoryService subCategoryService;
@Autowired
public SubCategoryController(StakeholderService stakeholderService, TopicService topicService, CategoryService categoryService, SubCategoryService subCategoryService) {
this.stakeholderService = stakeholderService;
this.topicService = topicService;
this.categoryService = categoryService;
this.subCategoryService = subCategoryService;
}
/* @PreAuthorize("isAuthenticated()")
@RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/save", method = RequestMethod.POST)
public SubCategory<Section<Indicator>> saveSubCategory(@PathVariable("stakeholderId") String stakeholderId,
@PathVariable("topicId") String topicId,
@PathVariable("categoryId") String categoryId,
@RequestBody SubCategory<Section<Indicator>> subcategoryFull) {
log.debug("save subcategory");
log.debug("Alias: "+subcategoryFull.getAlias() + " - Id: "+subcategoryFull.getId() + " - Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId);
Category category = checkForExceptions(stakeholderId, topicId, categoryId);
SubCategory<String> subCategory = new SubCategory<>(subcategoryFull);
Date date = new Date();
subCategory.setUpdateDate(date);
subcategoryFull.setUpdateDate(date);
List<String> chartSections = new ArrayList<>();
List<String> numberSections = new ArrayList<>();
SubCategory<String> oldSubcategory = null;
if(subcategoryFull.getId() != null) {
oldSubcategory = subCategoryDAO.findById(subcategoryFull.getId());
if(oldSubcategory == null) {
// EXCEPTION - SubCategory not found
throw new EntityNotFoundException("save subcategory: SubCategory with id: " + subcategoryFull.getId() + " not found");
}
for(String chartSectionId : oldSubcategory.getCharts()) {
Section section = sectionDAO.findById(chartSectionId);
if (section == null) {
// EXCEPTION - Section not found
throw new EntityNotFoundException("Save subcategory: Chart section with id: "+chartSectionId+" not found (section exists in subcategory: "+subCategory.getId()+")");
}
chartSections.add(section.getId());
}
for(String numberSectionId : oldSubcategory.getNumbers()) {
Section section = sectionDAO.findById(numberSectionId);
if (section == null) {
// EXCEPTION - Section not found
throw new EntityNotFoundException("Save subcategory: Number section with id: "+numberSectionId+" not found (section exists in subcategory: "+subCategory.getId()+")");
}
numberSections.add(section.getId());
}
} else { // subcategory does not exist in DB
subCategory.setCreationDate(date);
subcategoryFull.setCreationDate(date);
for(Section chartSection : subcategoryFull.getCharts()) {
chartSections.add(chartSection.getId());
}
for(Section numberSection : subcategoryFull.getNumbers()) {
numberSections.add(numberSection.getId());
}
}
subCategory.setCharts(chartSections);
subCategory.setNumbers(numberSections);
Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId);
if(stakeholder.getDefaultId() == null) {
if(subcategoryFull.getId() == null) {
subCategoryDAO.save(subCategory);
onSaveDefaultSubCategory(subCategory, categoryId);
} else {
onUpdateDefaultSubCategory(subCategory, oldSubcategory);
subCategoryDAO.save(subCategory);
}
} else {
subCategoryDAO.save(subCategory);
}
List<String> subcategories = category.getSubCategories();
int index = subcategories.indexOf(subCategory.getId());
if(index == -1) {
subcategories.add(subCategory.getId());
categoryDAO.save(category);
log.debug("Subcategory saved!");
subcategoryFull.setId(subCategory.getId());
}
return subcategoryFull;
}
public void onSaveDefaultSubCategory(SubCategory subCategory, String categoryId) {
log.debug("On save default subCategory");
List<Category> categories = categoryDAO.findByDefaultId(categoryId);
for(Category category : categories) {
SubCategory subCategoryNew = new SubCategory();
subCategoryNew.copyFromDefault(subCategory);
subCategoryDAO.save(subCategoryNew);
List<String> subCategories = category.getSubCategories();
subCategories.add(subCategoryNew.getId());
categoryDAO.save(category);
}
}
public void onUpdateDefaultSubCategory(SubCategory subCategory, SubCategory oldSubcategory) {
log.debug("On update default subCategory");
List<SubCategory> subCategories = subCategoryDAO.findByDefaultId(subCategory.getId());
boolean changed = false;
for(SubCategory subCategoryBasedOnDefault : subCategories) {
if(subCategory.getName() != null && !subCategory.getName().equals(subCategoryBasedOnDefault.getName())
&& (oldSubcategory.getName() == null || oldSubcategory.getName().equals(subCategoryBasedOnDefault.getName()))) {
subCategoryBasedOnDefault.setName(subCategory.getName());
subCategoryBasedOnDefault.setAlias(subCategory.getAlias());
changed = true;
}
if(subCategory.getDescription() != null && !subCategory.getDescription().equals(subCategoryBasedOnDefault.getDescription())
&& (oldSubcategory.getDescription() == null || oldSubcategory.getDescription().equals(subCategoryBasedOnDefault.getDescription()))) {
subCategoryBasedOnDefault.setDescription(subCategory.getDescription());
changed = true;
}
if(!changed) {
continue;
}
subCategoryBasedOnDefault.setUpdateDate(subCategory.getUpdateDate());
subCategoryDAO.save(subCategoryBasedOnDefault);
}
}*/
@PreAuthorize("isAuthenticated()")
@RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/delete", method = RequestMethod.DELETE)
public boolean deleteSubCategory(@PathVariable("stakeholderId") String stakeholderId,
@PathVariable("topicId") String topicId,
@PathVariable("categoryId") String categoryId,
@PathVariable("subcategoryId") String subcategoryId,
@RequestParam(required = false) String children) {
log.debug("delete subcategory");
log.debug("Id: " + subcategoryId + " - Stakeholder: " + stakeholderId + " - Topic: " + topicId + " - Category: " + categoryId);
Stakeholder stakeholder = this.stakeholderService.findById(stakeholderId);
Topic topic = this.topicService.findByPath(stakeholder, topicId);
Category category = this.categoryService.findByPath(topic, categoryId);
SubCategory subCategory = this.subCategoryService.findByPath(category, subcategoryId);
this.subCategoryService.delete(stakeholder.getType(), subCategory, true);
return true;
}
/*@PreAuthorize("isAuthenticated()")
@RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/reorder", method = RequestMethod.POST)
public List<SubCategory> reorderSubCategories(@PathVariable("stakeholderId") String stakeholderId,
@PathVariable("topicId") String topicId,
@PathVariable("categoryId") String categoryId,
@RequestBody List<String> subCategories) {
log.debug("reorder subCategories");
log.debug("Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId);
Category<String> category = checkForExceptions(stakeholderId, topicId, categoryId);
List<String> oldSubcategories = category.getSubCategories();
for (String subcategoryId : oldSubcategories) {
if (!subCategories.contains(subcategoryId)) {
subCategories.add(subcategoryId);
}
}
category.setSubCategories(subCategories);
List<SubCategory> subCategoriesFull = new ArrayList<>();
for(String subCategoryId : subCategories) {
SubCategory subCategory = subCategoryDAO.findById(subCategoryId);
if(subCategory == null) {
// EXCEPTION - SubCategory not found
throw new EntityNotFoundException("Reorder subCategories: subCategory with id: " + subCategoryId + " not found");
}
subCategoriesFull.add(subCategory);
}
categoryDAO.save(category);
log.debug("SubCategories reordered!");
return subCategoriesFull;
}*/
@PreAuthorize("isAuthenticated()")
@RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/change-visibility", method = RequestMethod.POST)
public SubCategoryFull changeSubCategoryVisibility(@PathVariable("stakeholderId") String stakeholderId,
@PathVariable("topicId") String topicId,
@PathVariable("categoryId") String categoryId,
@PathVariable("subcategoryId") String subcategoryId,
@RequestParam("visibility") Visibility visibility,
@RequestParam(defaultValue = "false") Boolean propagate) {
log.debug("change subCategory visibility: " + visibility + " - toggle propagate: " + propagate);
log.debug("Stakeholder: " + stakeholderId + " - Topic: " + topicId + " - Category: " + categoryId + " - SubCategory: " + subcategoryId);
Stakeholder stakeholder = this.stakeholderService.findById(stakeholderId);
Topic topic = this.topicService.findByPath(stakeholder, topicId);
Category category = this.categoryService.findByPath(topic, categoryId);
SubCategory subCategory = this.subCategoryService.findByPath(category, subcategoryId);
return this.subCategoryService.changeVisibility(stakeholder.getType(), stakeholder.getAlias(), subCategory, visibility, propagate);
}
}