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

116 lines
6.1 KiB
Java

package eu.dnetlib.uoamonitorservice.controllers;
import eu.dnetlib.uoamonitorservice.dto.SectionFull;
import eu.dnetlib.uoamonitorservice.entities.*;
import eu.dnetlib.uoamonitorservice.service.*;
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 SectionController {
private final Logger log = LogManager.getLogger(this.getClass());
private final StakeholderService stakeholderService;
private final TopicService topicService;
private final CategoryService categoryService;
private final SubCategoryService subCategoryService;
private final SectionService sectionService;
@Autowired
public SectionController(StakeholderService stakeholderService, TopicService topicService, CategoryService categoryService,
SubCategoryService subCategoryService, SectionService sectionService) {
this.stakeholderService = stakeholderService;
this.topicService = topicService;
this.categoryService = categoryService;
this.subCategoryService = subCategoryService;
this.sectionService = sectionService;
}
@PreAuthorize("isAuthenticated()")
@RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/save/{index}", method = RequestMethod.POST)
public SectionFull saveSection(@PathVariable("stakeholderId") String stakeholderId,
@PathVariable("topicId") String topicId,
@PathVariable("categoryId") String categoryId,
@PathVariable("subcategoryId") String subcategoryId,
@PathVariable("index") String index,
@RequestBody SectionFull sectionFull) {
log.debug("save section");
log.debug("Name: "+sectionFull.getTitle() + " - Id: "+sectionFull.getId() + " - Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId+ " - SubCategory: "+subcategoryId);
Stakeholder stakeholder = this.stakeholderService.findByPath(stakeholderId);
Topic topic = this.topicService.findByPath(stakeholder, topicId);
Category category = this.categoryService.findByPath(topic, categoryId);
SubCategory subCategory = this.subCategoryService.findByPath(category, subcategoryId);
return this.sectionService.save(stakeholder, subCategory, new Section(sectionFull), Integer.parseInt(index));
}
@PreAuthorize("isAuthenticated()")
@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,
@RequestParam(required = false) String children) {
log.debug("delete section");
log.debug("Id: " + sectionId + " - 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);
Section section = this.sectionService.findByPath(subCategory, sectionId);
this.sectionService.delete(stakeholder.getType(), section, true);
return true;
}
/*@PreAuthorize("isAuthenticated()")
@RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/{type}/reorder", method = RequestMethod.POST)
public List<Section> reorderSections(@PathVariable("stakeholderId") String stakeholderId,
@PathVariable("topicId") String topicId,
@PathVariable("categoryId") String categoryId,
@PathVariable("subcategoryId") String subcategoryId,
@PathVariable("type") String type,
@RequestBody List<String> sections) {
log.debug("reorder sections of type: "+type);
log.debug("Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId+ " - SubCategory: "+subcategoryId);
SubCategory<String> subCategory = checkForExceptions(stakeholderId, topicId, categoryId, subcategoryId);
if (type.equals("chart")) {
List<String> oldSections = subCategory.getCharts();
for (String sectionId : oldSections) {
if (!sections.contains(sectionId)) {
sections.add(sectionId);
}
}
subCategory.setCharts(sections);
} else if (type.equals("number")) {
List<String> oldSections = subCategory.getNumbers();
for (String sectionId : oldSections) {
if (!sections.contains(sectionId)) {
sections.add(sectionId);
}
}
subCategory.setNumbers(sections);
}
List<Section> sectionsFull = new ArrayList<>();
for(String sectionId : sections) {
Section section = sectionDAO.findById(sectionId);
if(section == null) {
// EXCEPTION - Section not found
throw new EntityNotFoundException("Reorder sections: Section with id: " + sectionId + " not found");
}
sectionsFull.add(section);
}
subCategoryDAO.save(subCategory);
log.debug("Sections reordered!");
return sectionsFull;
}*/
}