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

445 lines
19 KiB
Java

package eu.dnetlib.uoamonitorservice.controllers;
//import com.fasterxml.jackson.core.type.TypeReference;
//import com.fasterxml.jackson.databind.ObjectMapper;
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.Date;
import java.util.List;
@RestController
@CrossOrigin(origins = "*")
public class StakeholderController {
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;
@Autowired
private TopicController topicController;
@RequestMapping(value = "/build-stakeholder", method = RequestMethod.POST)
public Stakeholder<Topic<Category<SubCategory<Section<Indicator>>>>> buildFullStakeholder(@RequestBody Stakeholder<Topic<Category<SubCategory<Section<Indicator>>>>> stakeholderFull) {
log.debug("build stakeholder");
log.debug("Alias: "+stakeholderFull.getAlias());
Stakeholder<String> stakeholder = new Stakeholder<>(stakeholderFull);
List<String> topics = new ArrayList<>();
List<Topic<Category<SubCategory<Section<Indicator>>>>> topicsFull = new ArrayList<>();
for(Topic topic : stakeholderFull.getTopics()) {
Topic<Category<SubCategory<Section<Indicator>>>> topicFull = topicController.buildTopic(topic);
topicsFull.add(topicFull);
topics.add(topicFull.getId());
}
stakeholderFull.setTopics(topicsFull);
stakeholder.setTopics(topics);
Date date = new Date();
stakeholder.setCreationDate(date);
stakeholder.setUpdateDate(date);
stakeholderFull.setCreationDate(date);
stakeholderFull.setUpdateDate(date);
Stakeholder<String> stakeholderSaved = stakeholderDAO.save(stakeholder);
stakeholderFull.setId(stakeholderSaved.getId());
return stakeholderFull;
//return null;
}
public Stakeholder setFullEntities(Stakeholder<String> stakeholder) {
Stakeholder<Topic> stakeholderFull = new Stakeholder<>(stakeholder);
List<Topic> topics = new ArrayList<>();
for (String topicId: (List<String>)stakeholder.getTopics()) {
Topic<String> topic = topicDAO.findById(topicId);
if(topic == null) {
// EXCEPTION - Topic not found
throw new EntityNotFoundException("Get stakeholder: Topic with id: "+topicId+" not found (topic exists in stakeholder: "+stakeholder.getId()+")");
}
Topic<Category> topicFull = new Topic<Category>(topic);
List<Category> categories = new ArrayList<>();
for(String categoryId : topic.getCategories()) {
Category<String> category = categoryDAO.findById(categoryId);
if(category == null) {
// EXCEPTION - Category not found
throw new EntityNotFoundException("Get stakeholder: Category with id: "+categoryId+" not found (category exists in topic: "+topicId+")");
}
Category<SubCategory> categoryFull = new Category<SubCategory>(category);
List<SubCategory> subCategories = new ArrayList<>();
for(String subCategoryId : category.getSubCategories()) {
SubCategory<String> subCategory = subCategoryDAO.findById(subCategoryId);
if(subCategory == null) {
// EXCEPTION - SubCategory not found
throw new EntityNotFoundException("Get stakeholder: SubCategory with id: "+subCategoryId+" not found (subCategory exists in category: "+categoryId+")");
}
SubCategory subCategoryFull = new SubCategory<Section<Indicator>>(subCategory);
List<Section> sectionsCharts = new ArrayList<>();
for(String sectionId : subCategory.getCharts()) {
sectionsCharts.add(getSectionFull(sectionId, subCategoryId));
}
subCategoryFull.setCharts(sectionsCharts);
List<Section> sectionsNumbers = new ArrayList<>();
for(String sectionId : subCategory.getNumbers()) {
sectionsNumbers.add(getSectionFull(sectionId, subCategoryId));
}
subCategoryFull.setNumbers(sectionsNumbers);
// List<Indicator> charts = new ArrayList<>();
// for(String indicatorId : subCategory.getCharts()) {
// Indicator indicator = indicatorDAO.findById(indicatorId);
// if(indicator == null) {
// // EXCEPTION - Indicator not found
// throw new EntityNotFoundException("Get stakeholder: Indicator with id: "+indicatorId+" not found (indicator exists in subCategory: "+subCategoryId+")");
// }
// charts.add(indicator);
// }
// subCategoryFull.setCharts(charts);
//
// List<Indicator> numbers = new ArrayList<>();
// for (String indicatorId : subCategory.getNumbers()) {
// Indicator indicator = indicatorDAO.findById(indicatorId);
// if (indicator == null) {
// // EXCEPTION - Indicator not found
// throw new EntityNotFoundException("Get stakeholder: Indicator with id: " + indicatorId + " not found (indicator exists in subCategory: " + subCategoryId + ")");
// }
// numbers.add(indicator);
// }
// subCategoryFull.setNumbers(numbers);
subCategories.add(subCategoryFull);
}
categoryFull.setSubCategories(subCategories);
categories.add(categoryFull);
}
topicFull.setCategories(categories);
topics.add(topicFull);
}
stakeholderFull.setTopics(topics);
return stakeholderFull;
}
private Section getSectionFull(String sectionId, String subCategoryId) {
Section<String> section = sectionDAO.findById(sectionId);
if (section == null) {
// EXCEPTION - Section not found
throw new EntityNotFoundException("Get stakeholder: Section with id: " + sectionId + " not found (section exists in subCategory: " + subCategoryId + ")");
}
Section sectionFull = new Section<Indicator>(section);
List<Indicator> indicators = new ArrayList<>();
for (String indicatorId : section.getIndicators()) {
Indicator indicator = indicatorDAO.findById(indicatorId);
if (indicator == null) {
// EXCEPTION - Indicator not found
throw new EntityNotFoundException("Get stakeholder: Indicator with id: " + indicatorId + " not found (indicator exists in section: " + sectionId + ")");
}
indicators.add(indicator);
}
sectionFull.setIndicators(indicators);
return sectionFull;
}
@RequestMapping(value = "/stakeholder/all", method = RequestMethod.GET)
public List<Stakeholder> getAllStakeholders(@RequestParam(required = false) String type) {
log.debug("get all stakeholders" + (type != null ? " with type: "+type : ""));
List<Stakeholder> stakeholders;
if(type == null) {
stakeholders = stakeholderDAO.findAll();
} else {
stakeholders = stakeholderDAO.findByType(type);
}
List<Stakeholder> stakeholdersFull = new ArrayList<>();
for(Stakeholder stakeholder : stakeholders) {
stakeholdersFull.add(this.setFullEntities(stakeholder));
}
return stakeholdersFull;
}
@RequestMapping(value = "/stakeholder/default", method = RequestMethod.GET)
public List<Stakeholder> getAllDefaultStakeholders(@RequestParam(required = false) String type) {
log.debug("get all default stakeholders" + (type != null ? " with type: "+type : ""));
List<Stakeholder> stakeholders;
if(type == null) {
stakeholders = stakeholderDAO.findByDefaultId(null);
} else {
stakeholders = stakeholderDAO.findByDefaultIdAndType(null, type);
}
List<Stakeholder> stakeholdersFull = new ArrayList<>();
for(Stakeholder stakeholder : stakeholders) {
stakeholdersFull.add(this.setFullEntities(stakeholder));
}
return stakeholdersFull;
}
@RequestMapping(value = "/stakeholder", method = RequestMethod.GET)
public List<Stakeholder> getAllRealStakeholders(@RequestParam(required = false) String type) {
log.debug("get all NOT default stakeholders" + (type != null ? " with type: "+type : ""));
List<Stakeholder> stakeholders;
if(type == null) {
stakeholders = stakeholderDAO.findByDefaultIdNot(null);
} else {
stakeholders = stakeholderDAO.findByDefaultIdNotAndType(null, type);
}
List<Stakeholder> stakeholdersFull = new ArrayList<>();
for(Stakeholder stakeholder : stakeholders) {
stakeholdersFull.add(this.setFullEntities(stakeholder));
}
log.debug(new Date());
return stakeholdersFull;
}
@RequestMapping(value = "/stakeholder/{alias}", method = RequestMethod.GET)
public Stakeholder getStakeholder(@PathVariable("alias") String alias) {
log.debug("get stakeholder: "+alias);
Stakeholder<String> stakeholder = stakeholderDAO.findByAlias(alias);
if(stakeholder == null) {
// EXCEPTION - Stakeholder not found
throw new EntityNotFoundException("Get stakeholder: Stakeholder with alias: "+alias+" not found");
}
return this.setFullEntities(stakeholder);
}
@RequestMapping(value = "/save", method = RequestMethod.POST)
public Stakeholder<Topic> saveStakeholder(@RequestBody Stakeholder<Topic> stakeholderFull) {
log.debug("save stakeholder");
log.debug("Alias: "+stakeholderFull.getAlias() + " - Id: "+stakeholderFull.getId());
// if(stakeholderFull == null) {
// log.debug("stakeholder null");
// // EXCEPTION - Parameter for Stakeholder is not accepted
// }
Stakeholder<String> stakeholder = new Stakeholder<>(stakeholderFull);
Date date = new Date();
stakeholder.setUpdateDate(date);
// stakeholder does not exist in DB
if(stakeholderFull.getId() == null) {
stakeholder.setCreationDate(date);
}
List<String> topics = new ArrayList<>();
for(Topic topic : stakeholderFull.getTopics()) {
topics.add(topic.getId());
}
stakeholder.setTopics(topics);
Stakeholder<String> stakeholderSaved = stakeholderDAO.save(stakeholder);
stakeholderFull.setId(stakeholderSaved.getId());
stakeholderFull.setCreationDate(stakeholderSaved.getCreationDate());
stakeholderFull.setUpdateDate(stakeholderSaved.getUpdateDate());
topics = null;
stakeholder = null;
stakeholderSaved = null;
return stakeholderFull;
}
@RequestMapping(value = "/{stakeholderId}/delete", method = RequestMethod.DELETE)
public boolean deleteStakeholder(@PathVariable("stakeholderId") String stakeholderId) {
log.debug("delete stakeholder");
log.debug("Id: "+stakeholderId);
Stakeholder<String> stakeholder = stakeholderDAO.findById(stakeholderId);
if(stakeholder != null) {
// for(String topicId : stakeholder.getTopics()) {
// Topic<String> topic = topicDAO.findById(topicId);
// if (topic == null) {
// // EXCEPTION - Topic not found
// throw new EntityNotFoundException("Delete stakeholder: Topic with id: "+topicId+" not found (topic exists in stakeholder: "+stakeholderId+")");
// }
//
// for (String categoryId : topic.getCategories()) {
// Category<String> category = categoryDAO.findById(categoryId);
// if (category == null) {
// // EXCEPTION - Category not found
// throw new EntityNotFoundException("Delete stakeholder: Category with id: "+categoryId+" not found (category exists in topic: "+topicId+")");
// }
//
// for (String subCategoryId : category.getSubCategories()) {
// SubCategory<String> subcategory = subCategoryDAO.findById(subCategoryId);
// if (subcategory == null) {
// // EXCEPTION - SubCategory not found
// throw new EntityNotFoundException("Delete stakeholder: SubCategory with id: "+subCategoryId+" not found (subcategory exists in category: "+categoryId+")");
// }
//
// for(String chartSectionId : subcategory.getCharts()) {
// Section<String> chartSection = sectionDAO.findById(chartSectionId);
// if (chartSection == null) {
// // EXCEPTION - Section not found
// throw new EntityNotFoundException("Delete topic: Section with id: "+chartSectionId+" not found (section exists in subcategory: "+subCategoryId+")");
// }
//
// for (String chartId : chartSection.getIndicators()) {
// indicatorDAO.delete(chartId);
// }
// subcategory.setCharts(null);
// sectionDAO.delete(chartSectionId);
// }
//
// for(String numberSectionId : subcategory.getNumbers()) {
// Section<String> numberSection = sectionDAO.findById(numberSectionId);
// if (numberSection == null) {
// // EXCEPTION - Section not found
// throw new EntityNotFoundException("Delete topic: Section with id: "+numberSectionId+" not found (section exists in subcategory: "+subCategoryId+")");
// }
//
// for (String numberId : numberSection.getIndicators()) {
// indicatorDAO.delete(numberId);
// }
// subcategory.setNumbers(null);
// sectionDAO.delete(numberSectionId);
// }
//
// subCategoryDAO.delete(subCategoryId);
// }
// category.setSubCategories(null);
// categoryDAO.delete(categoryId);
// }
// topic.setCategories(null);
// topicDAO.delete(topicId);
// }
topicController.deleteTree(stakeholder);
stakeholder.setTopics(null);
stakeholderDAO.delete(stakeholderId);
log.debug("Stakeholder deleted!");
} else {
// EXCEPTION - Stakeholder not found
throw new EntityNotFoundException("Delete stakeholder: Stakeholder with id: "+stakeholderId+" not found");
}
return true;
}
@RequestMapping(value = "/{stakeholderId}/toggle-status", method = RequestMethod.POST)
public Boolean toggleStakeholderStatus(@PathVariable("stakeholderId") String stakeholderId) {
log.debug("toggle stakeholder status (isActive)");
log.debug("Stakeholder: "+stakeholderId);
Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId);
if (stakeholder == null) {
// EXCEPTION - Stakeholder not found
throw new EntityNotFoundException("Toggle stakeholder status: Stakeholder with id: "+stakeholderId+" not found");
}
stakeholder.setIsActive(!stakeholder.getIsActive());
stakeholderDAO.save(stakeholder);
log.debug("Stakeholder toggled!");
return stakeholder.getIsActive();
}
@RequestMapping(value = "/{stakeholderId}/toggle-access", method = RequestMethod.POST)
public Boolean toggleStakeholderAccess(@PathVariable("stakeholderId") String stakeholderId) {
log.debug("toggle stakeholder access (isPublic)");
log.debug("Stakeholder: "+stakeholderId);
Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId);
if (stakeholder == null) {
// EXCEPTION - Stakeholder not found
throw new EntityNotFoundException("Toggle stakeholder access: Stakeholder with id: "+stakeholderId+" not found");
}
stakeholder.setIsPublic(!stakeholder.getIsPublic());
stakeholderDAO.save(stakeholder);
log.debug("Stakeholder toggled!");
return stakeholder.getIsPublic();
}
// The following are not supposed to be used
// @RequestMapping(value = "/stakeholder/dates", method = RequestMethod.GET)
// public List<Date> getAllStakeholderDates() {
// List<Stakeholder> profiles = stakeholderDAO.findAll();
// List<Date> profileDates = new ArrayList<>();
//
// int i=0;
// for(Stakeholder profile : profiles) {
// log.debug(profile.getCreationDate());
// profileDates.add(profile.getCreationDate());
// log.debug(profileDates.get(i));
// i++;
// }
// return profileDates;
// }
//
// @RequestMapping(value = "/stakeholder/dates1", method = RequestMethod.GET)
// public List<String> getAllStakeholderDates1() {
// List<Stakeholder> profiles = stakeholderDAO.findAll();
// List<String> profileDates = new ArrayList<>();
//
// for(Stakeholder profile : profiles) {
// log.debug(profile.getCreationDate().toString());
// profileDates.add(profile.getCreationDate().toString());
// }
// return profileDates;
// }
//
// @RequestMapping(value = "/stakeholder/dates2", method = RequestMethod.GET)
// public List<String> getAllStakeholderDates2() {
// List<Stakeholder> profiles = stakeholderDAO.findAll();
// List<String> profileDates = new ArrayList<>();
// SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//
// for(Stakeholder profile : profiles) {
// log.debug(format.format(profile.getCreationDate()));
// profileDates.add(format.format(profile.getCreationDate()));
// }
// return profileDates;
// }
}