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

334 lines
15 KiB
Java

package eu.dnetlib.uoamonitorservice.controllers;
import eu.dnetlib.uoamonitorservice.dao.*;
import eu.dnetlib.uoamonitorservice.entities.*;
import eu.dnetlib.uoamonitorservice.handlers.EntityNotFoundException;
import eu.dnetlib.uoamonitorservice.handlers.PathNotValidException;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;
@RestController
@CrossOrigin(origins = "*")
public class SectionController {
private final Logger log = Logger.getLogger(this.getClass());
@Autowired
private StakeholderDAO stakeholderDAO;
@Autowired
private TopicDAO topicDAO;
@Autowired
private CategoryDAO categoryDAO;
@Autowired
private SubCategoryDAO subCategoryDAO;
@Autowired
private SectionDAO sectionDAO;
@Autowired
private IndicatorDAO indicatorDAO;
public Section<Indicator> buildSection(Section<Indicator> sectionFull) {
Section<String> section = new Section<>(sectionFull);
List<String> indicators = new ArrayList<>();
List<Indicator> indicatorsFull = new ArrayList<>();
for(Indicator chart : sectionFull.getIndicators()) {
Indicator chartSaved = indicatorDAO.save(chart);
chart.setId(chartSaved.getId());
indicatorsFull.add(chart);
indicators.add(chartSaved.getId());
}
sectionFull.setIndicators(indicatorsFull);
section.setIndicators(indicators);
sectionDAO.save(section);
sectionFull.setId(section.getId());
return sectionFull;
}
@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);
String sectionId = sectionFull.getId();
List<String> indicators = new ArrayList<>();
for(Indicator indicator : sectionFull.getIndicators()) {
indicators.add(indicator.getId());
}
section.setIndicators(indicators);
sectionDAO.save(section);
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) {
onSaveDefaultSection(section, topicId, categoryId, subcategoryId, stakeholder);
}
else {
onUpdateDefaultSection(section, stakeholder);
}
}
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) {
Section sectionNew = new Section();
sectionNew.copyFromDefault(section);
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) {
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())) {
changed = true;
}
if(!changed) {
break;
}
sectionBasedOnDefault.setTitle(section.getTitle());
sectionDAO.save(sectionBasedOnDefault);
}
}
@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) {
log.debug("delete section");
log.debug("Id: "+sectionId + " - Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId+ " - SubCategory: "+subcategoryId);
Section section = sectionDAO.findById(sectionId);
if(section != null) {
SubCategory<String> subCategory = checkForExceptions(stakeholderId, topicId, categoryId, subcategoryId);
List<String> sections = null;
if (section.getType().equals("chart")) {
sections = subCategory.getCharts();
} else if (section.getType().equals("number")) {
sections = subCategory.getNumbers();
}
int index = sections.indexOf(sectionId);
if (index != -1) {
sections.remove(index);
subCategoryDAO.save(subCategory);
sectionDAO.delete(sectionId);
log.debug("Section deleted!");
} else {
// EXCEPTION - Section not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); -> Category: category.getAlias(); -> SubCategory: subcategory.getAlias();
throw new PathNotValidException("Delete section: Section with id: "+sectionId+" not found in SubCategory: "+subcategoryId);
}
} else {
// EXCEPTION - Section not found
throw new EntityNotFoundException("Delete section: Section with id: "+sectionId+" not found");
}
return true;
}
@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")) {
subCategory.setCharts(sections);
} else if (type.equals("number")) {
subCategory.setNumbers(sections);
}
subCategoryDAO.save(subCategory);
log.debug("Sections reordered!");
List<Section> sectionsFull = new ArrayList<>();
for(String sectionId : sections) {
sectionsFull.add(sectionDAO.findById(sectionId));
}
return sectionsFull;
}
// @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/{sectionId}/toggle-status", method = RequestMethod.POST)
// public Boolean toggleSectionStatus(@PathVariable("stakeholderId") String stakeholderId,
// @PathVariable("topicId") String topicId,
// @PathVariable("categoryId") String categoryId,
// @PathVariable("subcategoryId") String subcategoryId,
// @PathVariable("sectionId") String sectionId) {
// log.debug("toggle section status (isActive)");
// log.debug("Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId+ " - SubCategory: "+subcategoryId+ " - Section: "+sectionId);
//
// Section section = sectionDAO.findById(sectionId);
// if (section == null) {
// // EXCEPTION - Section not found
// throw new EntityNotFoundException("Toggle section status: Section with id: "+sectionId+" not found");
// }
// section.setIsActive(!section.getIsActive());
//
// this.toggleSection(stakeholderId, topicId, categoryId, subcategoryId, section);
//
// return section.getIsActive();
// }
// @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/{sectionId}/toggle-access", method = RequestMethod.POST)
// public Boolean toggleSectionAccess(@PathVariable("stakeholderId") String stakeholderId,
// @PathVariable("topicId") String topicId,
// @PathVariable("categoryId") String categoryId,
// @PathVariable("subcategoryId") String subcategoryId,
// @PathVariable("sectionId") String sectionId) {
// log.debug("toggle section access (isPublic)");
// log.debug("Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId+ " - SubCategory: "+subcategoryId);
//
// Section section = sectionDAO.findById(sectionId);
// if (section == null) {
// // EXCEPTION - Section not found
// throw new EntityNotFoundException("Toggle section access: Section with id: "+sectionId+" not found");
// }
// section.setIsPublic(!section.getIsPublic());
//
// this.toggleSection(stakeholderId, topicId, categoryId, subcategoryId);
//
// return section.getIsPublic();
// }
public void toggleSection(String stakeholderId, String topicId, String categoryId, String subcategoryId, Section section) {
SubCategory<String> subCategory = checkForExceptions(stakeholderId, topicId, categoryId, subcategoryId);
List<String> sections = null;
if (section.getType().equals("chart")) {
sections = subCategory.getCharts();
} else if (section.getType().equals("number")) {
sections = subCategory.getNumbers();
}
if(sections.contains(section.getId())) {
sectionDAO.save(section);
log.debug("Section toggled!");
} else {
// EXCEPTION - Section not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); -> Category: category.getAlias(); -> SubCategory: subCategory.getAlias();
throw new PathNotValidException("Toggle section: Section with id: "+section.getId()+" not found in SubCategory: "+subcategoryId);
}
}
private SubCategory checkForExceptions(String stakeholderId, String topicId, String categoryId, String subcategoryId) {
Stakeholder<String> stakeholder = stakeholderDAO.findById(stakeholderId);
if(stakeholder == null) {
// EXCEPTION - Stakeholder not found
throw new EntityNotFoundException("Save indicator: Stakeholder with id: " + stakeholderId + " not found");
}
Topic<String> topic = topicDAO.findById(topicId);
if(topic == null) {
// EXCEPTION - Topic not found
throw new EntityNotFoundException("Save indicator: Topic with id: "+topicId+" not found");
}
if(!stakeholder.getTopics().contains(topicId)) {
// EXCEPTION - Topic not found in Stakeholder: stakeholder.getAlias();
throw new PathNotValidException("Save indicator: Topic with id: " + topicId + " not found in Stakeholder: " + stakeholderId);
}
Category<String> category = categoryDAO.findById(categoryId);
if(category == null) {
// EXCEPTION - Category not found
throw new EntityNotFoundException("Save indicator: Category with id: "+categoryId+" not found");
}
if(!topic.getCategories().contains(categoryId)) {
// EXCEPTION - Category not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias();
throw new PathNotValidException("Save indicator: Category with id: "+categoryId+" not found in Topic: "+topicId);
}
SubCategory<String> subcategory = subCategoryDAO.findById(subcategoryId);
if(subcategory == null) {
// EXCEPTION - SubCategory not found
throw new EntityNotFoundException("Save indicator: SubCategory with id: "+subcategoryId+" not found");
}
if (!category.getSubCategories().contains(subcategoryId)) {
// EXCEPTION - SubCategory not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); -> Category: category.getAlias();
throw new PathNotValidException("Save indicator: SubCategory with id: "+subcategoryId+" not found in Category: "+categoryId);
}
return subcategory;
}
}