246 lines
11 KiB
Java
246 lines
11 KiB
Java
package eu.dnetlib.uoamonitorservice.controllers;
|
|
|
|
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 Section saveSection(@PathVariable("stakeholderId") String stakeholderId,
|
|
@PathVariable("topicId") String topicId,
|
|
@PathVariable("categoryId") String categoryId,
|
|
@PathVariable("subcategoryId") String subcategoryId,
|
|
@PathVariable("index") String index,
|
|
@RequestBody Section<Indicator> sectionFull) {
|
|
log.debug("save section");
|
|
log.debug("Name: "+sectionFull.getTitle() + " - Id: "+sectionFull.getId() + " - Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId+ " - SubCategory: "+subcategoryId);
|
|
|
|
SubCategory<String> subCategory = checkForExceptions(stakeholderId, topicId, categoryId, subcategoryId);
|
|
|
|
Section<String> section = new Section<>(sectionFull);
|
|
|
|
Date date = new Date();
|
|
section.setUpdateDate(date);
|
|
sectionFull.setUpdateDate(date);
|
|
|
|
List<String> indicators = new ArrayList<>();
|
|
|
|
Section<String> oldSection = null;
|
|
if(sectionFull.getId() != null) {
|
|
oldSection = sectionDAO.findById(sectionFull.getId());
|
|
if(oldSection == null) {
|
|
// EXCEPTION - Section not found
|
|
throw new EntityNotFoundException("save section: Section with id: " + sectionFull.getId() + " not found");
|
|
}
|
|
|
|
for(String indicatorId : oldSection.getIndicators()) {
|
|
Indicator indicator = indicatorDAO.findById(indicatorId);
|
|
if (indicator == null) {
|
|
// EXCEPTION - Indicator not found
|
|
throw new EntityNotFoundException("Save section: Indicator with id: "+indicatorId+" not found (indicator exists in section: "+section.getId()+")");
|
|
}
|
|
indicators.add(indicator.getId());
|
|
}
|
|
} else { // section does not exist in DB
|
|
section.setCreationDate(date);
|
|
sectionFull.setCreationDate(date);
|
|
|
|
for(Indicator indicator : sectionFull.getIndicators()) {
|
|
indicators.add(indicator.getId());
|
|
}
|
|
}
|
|
|
|
String sectionId = sectionFull.getId();
|
|
|
|
section.setIndicators(indicators);
|
|
|
|
Stakeholder<String> stakeholder = stakeholderDAO.findById(stakeholderId);
|
|
// this section belongs in default profile and it is new or it is updated
|
|
if(stakeholder.getDefaultId() == null) {
|
|
if(sectionId == null) {
|
|
sectionDAO.save(section);
|
|
onSaveDefaultSection(section, topicId, categoryId, subcategoryId, stakeholder);
|
|
}
|
|
else {
|
|
onUpdateDefaultSection(section, stakeholder, oldSection);
|
|
sectionDAO.save(section);
|
|
}
|
|
} else {
|
|
sectionDAO.save(section);
|
|
}
|
|
|
|
List<String> sections = null;
|
|
if(sectionFull.getType().equals("chart")) {
|
|
sections = subCategory.getCharts();
|
|
} else if(sectionFull.getType().equals("number")) {
|
|
sections = subCategory.getNumbers();
|
|
}
|
|
|
|
int existing_index = sections.indexOf(section.getId());
|
|
if (existing_index == -1) {
|
|
if(Integer.parseInt(index) != -1) {
|
|
sections.add(Integer.parseInt(index), section.getId());
|
|
} else {
|
|
sections.add(section.getId());
|
|
}
|
|
subCategoryDAO.save(subCategory);
|
|
log.debug("Section saved!");
|
|
|
|
sectionFull.setId(section.getId());
|
|
}
|
|
|
|
return sectionFull;
|
|
}
|
|
|
|
public void onSaveDefaultSection(Section section,
|
|
String defaultTopicId, String defaultCategoryId,
|
|
String defaultSubcategoryId, Stakeholder defaultStakeholder) {
|
|
log.debug("On save default section");
|
|
|
|
|
|
// new section in default profile - add it on profiles of the same type
|
|
List<SubCategory> subCategories = subCategoryDAO.findByDefaultId(defaultSubcategoryId);
|
|
|
|
for (SubCategory subCategory : subCategories) {
|
|
Category parentCategory = categoryDAO.findBySubCategoriesContaining(subCategory.getId());
|
|
Topic parentTopic = topicDAO.findByCategoriesContaining(parentCategory.getId());
|
|
Stakeholder parentStakeholder = stakeholderDAO.findByTopicsContaining(parentTopic.getId());
|
|
|
|
Section sectionNew = new Section();
|
|
sectionNew.copyFromDefault(section);
|
|
|
|
sectionNew.setStakeholderAlias(parentStakeholder.getAlias());
|
|
|
|
sectionDAO.save(sectionNew);
|
|
|
|
List<String> sections = null;
|
|
if (section.getType().equals("chart")) {
|
|
sections = subCategory.getCharts();
|
|
} else if (section.getType().equals("number")) {
|
|
sections = subCategory.getNumbers();
|
|
}
|
|
sections.add(sectionNew.getId());
|
|
|
|
subCategoryDAO.save(subCategory);
|
|
}
|
|
}
|
|
|
|
public void onUpdateDefaultSection(Section section, Stakeholder stakeholder, Section oldSection) {
|
|
log.debug("On update default section");
|
|
|
|
// section already exists - check if changed and update all sections based on it
|
|
|
|
boolean changed = false;
|
|
List<Section> sections = sectionDAO.findByDefaultId(section.getId());
|
|
|
|
for(Section sectionBasedOnDefault : sections) {
|
|
if(section.getTitle() != null && !section.getTitle().equals(sectionBasedOnDefault.getTitle())
|
|
&& (oldSection.getTitle() == null || oldSection.getTitle().equals(sectionBasedOnDefault.getTitle()))) {
|
|
|
|
sectionBasedOnDefault.setTitle(section.getTitle());
|
|
changed = true;
|
|
}
|
|
|
|
if(!changed) {
|
|
// break;
|
|
continue;
|
|
}
|
|
|
|
// sectionBasedOnDefault.setTitle(section.getTitle());
|
|
sectionBasedOnDefault.setUpdateDate(section.getUpdateDate());
|
|
sectionDAO.save(sectionBasedOnDefault);
|
|
}
|
|
}*/
|
|
|
|
@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;
|
|
}*/
|
|
}
|