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

245 lines
12 KiB
Java

package eu.dnetlib.uoamonitorservice.controllers;
import eu.dnetlib.uoamonitorservice.dto.CategoryFull;
import eu.dnetlib.uoamonitorservice.entities.Category;
import eu.dnetlib.uoamonitorservice.entities.Stakeholder;
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.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 CategoryController {
private final Logger log = LogManager.getLogger(this.getClass());
private final StakeholderService stakeholderService;
private final TopicService topicService;
private final CategoryService categoryService;
@Autowired
public CategoryController(StakeholderService stakeholderService, TopicService topicService, CategoryService categoryService) {
this.stakeholderService = stakeholderService;
this.topicService = topicService;
this.categoryService = categoryService;
}
/*@PreAuthorize("isAuthenticated()")
@RequestMapping(value = "/{stakeholderId}/{topicId}/save", method = RequestMethod.POST)
public Category<SubCategory> saveCategory(@PathVariable("stakeholderId") String stakeholderId,
@PathVariable("topicId") String topicId,
@RequestBody Category<SubCategory> categoryFull) {
log.debug("save category");
log.debug("Alias: " + categoryFull.getAlias() + " - Id: " + categoryFull.getId() + " - Stakeholder: " + stakeholderId + " - Topic: " + topicId);
Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId);
if (stakeholder != null) {
if (!rolesUtils.hasUpdateAuthority(stakeholder.getType(), stakeholder.getAlias())) {
// EXCEPTION - Access denied
throw new ForbiddenException("Save Category: You are not authorized to update stakeholder with id: " + stakeholderId);
}
Category<String> oldCategory = null;
if (categoryFull.getId() != null) {
oldCategory = categoryDAO.findById(categoryFull.getId());
if (oldCategory == null) {
// EXCEPTION - Category not found
throw new EntityNotFoundException("save category: Category with id: " + categoryFull.getId() + " not found");
}
}
Topic<String> topic = topicDAO.findById(topicId);
if (topic != null) {
if (stakeholder.getTopics().contains(topicId)) {
Category<String> category = new Category<>(categoryFull);
Date date = new Date();
category.setUpdateDate(date);
categoryFull.setUpdateDate(date);
List<String> subCategories = new ArrayList<>();
// if category not exists (no id), create a new default subcategory, identical to category
if (categoryFull.getId() == null) {
category.setCreationDate(date);
categoryFull.setCreationDate(date);
SubCategory<String> subCategory = new SubCategory<>();
subCategory.createOverviewSubCategory(categoryFull);
subCategoryDAO.save(subCategory);
List<SubCategory> subCategoriesFull = categoryFull.getSubCategories();
subCategoriesFull.add(subCategory);
for (SubCategory oldSubCategory : subCategoriesFull) {
subCategories.add(oldSubCategory.getId());
}
} else {
for (String subCategoryId : oldCategory.getSubCategories()) {
SubCategory subCategory = subCategoryDAO.findById(subCategoryId);
if (subCategory == null) {
// EXCEPTION - SubCategory not found
throw new EntityNotFoundException("Save category: SubCategory with id: " + subCategoryId + " not found (subcategory exists in category: " + category.getId() + ")");
}
subCategories.add(subCategory.getId());
}
}
category.setSubCategories(subCategories);
if (stakeholder.getDefaultId() == null) {
if (categoryFull.getId() == null) {
categoryDAO.save(category);
onSaveDefaultCategory(category, topicId);
} else {
onUpdateDefaultCategory(category, oldCategory);
categoryDAO.save(category);
}
} else {
categoryDAO.save(category);
}
List<String> categories = topic.getCategories();
int index = categories.indexOf(category.getId());
if (index == -1) {
categories.add(category.getId());
topicDAO.save(topic);
log.debug("Category saved!");
categoryFull.setId(category.getId());
}
} else {
// EXCEPTION - Topic not found in Stakeholder: stakeholder.getAlias();
throw new PathNotValidException("Save category: Topic with id: " + topicId + " not found in Stakeholder: " + stakeholderId);
}
} else {
// EXCEPTION - Topic not found
throw new EntityNotFoundException("Save category: Topic with id: " + topicId + " not found");
}
} else {
// EXCEPTION - Stakeholder not found
throw new EntityNotFoundException("Save category: Stakeholder with id: " + stakeholderId + " not found");
}
return categoryFull;
}*/
/*public void onSaveDefaultCategory(Category<String> category, String topicId) {
log.debug("On save default category");
List<Topic> topics = topicDAO.findByDefaultId(topicId);
for (Topic topic : topics) {
Category categoryNew = new Category();
categoryNew.copyFromDefault(category);
categoryDAO.save(categoryNew);
List<String> categories = topic.getCategories();
categories.add(categoryNew.getId());
topicDAO.save(topic);
}
String subCategoryOverviewId = category.getSubCategories().get(0);
SubCategory subCategoryOverview = subCategoryDAO.findById(subCategoryOverviewId);
subCategoryController.onSaveDefaultSubCategory(subCategoryOverview, category.getId());
}*/
/*public void onUpdateDefaultCategory(Category category, Category oldCategory) {
log.debug("On update default category");
List<Category> categories = categoryDAO.findByDefaultId(category.getId());
boolean changed = false;
for (Category categoryBasedOnDefault : categories) {
if (category.getName() != null && !category.getName().equals(categoryBasedOnDefault.getName())
&& (oldCategory.getName() == null || oldCategory.getName().equals(categoryBasedOnDefault.getName()))) {
categoryBasedOnDefault.setName(category.getName());
categoryBasedOnDefault.setAlias(category.getAlias());
changed = true;
}
if (category.getDescription() != null && !category.getDescription().equals(categoryBasedOnDefault.getDescription())
&& (oldCategory.getDescription() == null || oldCategory.getDescription().equals(categoryBasedOnDefault.getDescription()))) {
categoryBasedOnDefault.setDescription(category.getDescription());
changed = true;
}
if (!changed) {
continue;
}
categoryBasedOnDefault.setUpdateDate(category.getUpdateDate());
categoryDAO.save(categoryBasedOnDefault);
}
}*/
@PreAuthorize("isAuthenticated()")
@RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/delete", method = RequestMethod.DELETE)
public boolean deleteCategory(@PathVariable("stakeholderId") String stakeholderId,
@PathVariable("topicId") String topicId,
@PathVariable("categoryId") String categoryId,
@RequestParam(required = false) String children) {
log.debug("delete category");
log.debug("Id: " + categoryId + " - Stakeholder: " + stakeholderId + " - Topic: " + topicId);
Stakeholder stakeholder = this.stakeholderService.findById(stakeholderId);
Topic topic = this.topicService.findByPath(stakeholder, topicId);
Category category = this.categoryService.findByPath(topic, categoryId);
this.categoryService.delete(stakeholder.getType(), category, true);
return true;
}
/*@PreAuthorize("isAuthenticated()")
@RequestMapping(value = "/{stakeholderId}/{topicId}/reorder", method = RequestMethod.POST)
public List<Category> reorderCategories(@PathVariable("stakeholderId") String stakeholderId,
@PathVariable("topicId") String topicId,
@RequestBody List<String> categories) {
log.debug("reorder categories");
log.debug("Stakeholder: " + stakeholderId + " - Topic: " + topicId);
Topic<String> topic = checkForExceptions(stakeholderId, topicId);
List<String> oldCategories = topic.getCategories();
for (String categoryId : oldCategories) {
if (!categories.contains(categoryId)) {
categories.add(categoryId);
}
}
topic.setCategories(categories);
List<Category> categoriesFull = new ArrayList<>();
for (String categoryId : categories) {
Category category = categoryDAO.findById(categoryId);
if (category == null) {
// EXCEPTION - Category not found
throw new EntityNotFoundException("Reorder Categories: Category with id: " + categoryId + " not found");
}
categoriesFull.add(category);
}
topicDAO.save(topic);
log.debug("Categories reordered!");
return categoriesFull;
}*/
@PreAuthorize("isAuthenticated()")
@RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/change-visibility", method = RequestMethod.POST)
public CategoryFull changeCategoryVisibility(@PathVariable("stakeholderId") String stakeholderId,
@PathVariable("topicId") String topicId,
@PathVariable("categoryId") String categoryId,
@RequestParam("visibility") Visibility visibility, @RequestParam(defaultValue = "false") Boolean propagate) {
log.debug("change category visibility: " + visibility + " - toggle propagate: " + propagate);
log.debug("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);
return this.categoryService.changeVisibility(stakeholder.getType(), stakeholder.getAlias(), category, visibility, propagate);
}
}