507 lines
22 KiB
Java
507 lines
22 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.IndicatorDAO;
|
|
import eu.dnetlib.uoamonitorservice.dao.StakeholderDAO;
|
|
import eu.dnetlib.uoamonitorservice.entities.*;
|
|
import org.apache.log4j.Logger;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
import java.text.SimpleDateFormat;
|
|
import java.util.ArrayList;
|
|
import java.util.Date;
|
|
import java.util.Iterator;
|
|
import java.util.List;
|
|
|
|
@RestController
|
|
@CrossOrigin(origins = "*")
|
|
public class StakeholderController {
|
|
private final Logger log = Logger.getLogger(this.getClass());
|
|
|
|
@Autowired
|
|
private StakeholderDAO stakeholderDAO;
|
|
|
|
@Autowired
|
|
private IndicatorDAO indicatorDAO;
|
|
|
|
public Stakeholder setIndicatorsForStakeholder(Stakeholder stakeholder) {
|
|
for (Topic topic: stakeholder.getTopics()) {
|
|
for(Category category : topic.getCategories()) {
|
|
List<SubCategory> subCategories = new ArrayList<>();
|
|
|
|
for(SubCategory<String> subCategory : category.getSubCategories()) {
|
|
SubCategory subCategoryFull = new SubCategory<Indicator>(subCategory);
|
|
|
|
List<Indicator> charts = new ArrayList<>();
|
|
for(String indicatorId : subCategory.getCharts()) {
|
|
charts.add(indicatorDAO.findById(indicatorId));
|
|
}
|
|
subCategoryFull.setCharts(charts);
|
|
|
|
List<Indicator> numbers = new ArrayList<>();
|
|
for(String indicatorId : subCategory.getNumbers()) {
|
|
numbers.add(indicatorDAO.findById(indicatorId));
|
|
}
|
|
subCategoryFull.setNumbers(numbers);
|
|
|
|
subCategories.add(subCategoryFull);
|
|
}
|
|
|
|
category.setSubCategories(subCategories);
|
|
}
|
|
|
|
}
|
|
return stakeholder;
|
|
}
|
|
|
|
@RequestMapping(value = "/stakeholder/all", method = RequestMethod.GET)
|
|
public List<Stakeholder> getAllStakeholders(@RequestParam(required = false) String type) {
|
|
List<Stakeholder> stakeholders;
|
|
if(type == null) {
|
|
stakeholders = stakeholderDAO.findAll();
|
|
} else {
|
|
stakeholders = stakeholderDAO.findByType(type);
|
|
}
|
|
|
|
for(Stakeholder stakeholder : stakeholders) {
|
|
this.setIndicatorsForStakeholder(stakeholder);
|
|
}
|
|
|
|
return stakeholders;
|
|
}
|
|
|
|
@RequestMapping(value = "/stakeholder/default", method = RequestMethod.GET)
|
|
public List<Stakeholder> getAllDefaultStakeholders(@RequestParam(required = false) String type) {
|
|
List<Stakeholder> stakeholders;
|
|
if(type == null) {
|
|
stakeholders = stakeholderDAO.findByIsDefaultProfile(true);
|
|
} else {
|
|
stakeholders = stakeholderDAO.findByIsDefaultProfileAndType(true, type);
|
|
}
|
|
|
|
for(Stakeholder stakeholder : stakeholders) {
|
|
this.setIndicatorsForStakeholder(stakeholder);
|
|
}
|
|
return stakeholders;
|
|
}
|
|
|
|
@RequestMapping(value = "/stakeholder", method = RequestMethod.GET)
|
|
public List<Stakeholder> getAllRealStakeholders(@RequestParam(required = false) String type) {
|
|
List<Stakeholder> stakeholders;
|
|
if(type == null) {
|
|
stakeholders = stakeholderDAO.findByIsDefaultProfile(false);
|
|
} else {
|
|
stakeholders = stakeholderDAO.findByIsDefaultProfileAndType(false, type);
|
|
}
|
|
|
|
for(Stakeholder stakeholder : stakeholders) {
|
|
this.setIndicatorsForStakeholder(stakeholder);
|
|
}
|
|
log.debug(new Date());
|
|
|
|
return stakeholders;
|
|
}
|
|
|
|
@RequestMapping(value = "/stakeholder/{alias}", method = RequestMethod.GET)
|
|
public Stakeholder getStakeholder(@PathVariable("alias") String alias) {
|
|
Stakeholder stakeholder = stakeholderDAO.findByAlias(alias);
|
|
this.setIndicatorsForStakeholder(stakeholder);
|
|
|
|
return stakeholder;
|
|
}
|
|
|
|
|
|
@RequestMapping(value = "/stakeholder/save", method = RequestMethod.POST)
|
|
public Stakeholder saveStakeholder(@RequestBody Stakeholder stakeholder) {
|
|
log.debug("save stakeholder");
|
|
|
|
Stakeholder stakeholderFull = new Stakeholder(stakeholder);
|
|
|
|
//this.minimizeIndicatorsForStakeholder(stakeholder);
|
|
List<Topic> topicsFull = new ArrayList<>();
|
|
|
|
for (Topic topic: stakeholder.getTopics()) {
|
|
Topic topicFull = new Topic(topic);
|
|
|
|
List<Category> categoriesFull = new ArrayList<>();
|
|
|
|
for(Category category : topic.getCategories()) {
|
|
Category categoryFull = new Category(category);
|
|
|
|
List<SubCategory> subCategories = new ArrayList<>();
|
|
List<SubCategory> subCategoriesFull = new ArrayList<>();
|
|
|
|
for(SubCategory<Indicator> subCategoryFull : category.getSubCategories()) {
|
|
SubCategory subCategory = new SubCategory<String>(subCategoryFull);
|
|
|
|
List<String> charts = new ArrayList<>();
|
|
|
|
ObjectMapper mapper = new ObjectMapper();
|
|
//Jackson's use of generics
|
|
List<Indicator> chartsFull = mapper.convertValue(subCategoryFull.getCharts(), new TypeReference<List<Indicator>>(){});
|
|
|
|
//List<Indicator> chartsFull = (List<Indicator>)subCategoryFull.getCharts();
|
|
//log.debug(chartsFull);
|
|
|
|
for(Indicator indicator : chartsFull) {
|
|
charts.add(indicator.getId());
|
|
}
|
|
|
|
subCategory.setCharts(charts);
|
|
subCategoryFull.setCharts(chartsFull);
|
|
|
|
List<String> numbers = new ArrayList<>();
|
|
List<Indicator> numbersFull = mapper.convertValue(subCategoryFull.getNumbers(), new TypeReference<List<Indicator>>(){});
|
|
|
|
for(Indicator indicator : numbersFull) {
|
|
numbers.add(indicator.getId());
|
|
}
|
|
subCategory.setNumbers(numbers);
|
|
subCategoryFull.setNumbers(numbersFull);
|
|
|
|
subCategories.add(subCategory);
|
|
subCategoriesFull.add(subCategoryFull);
|
|
}
|
|
|
|
category.setSubCategories(subCategories);
|
|
categoryFull.setSubCategories(subCategoriesFull);
|
|
|
|
categoriesFull.add(categoryFull);
|
|
}
|
|
topicFull.setCategories(categoriesFull);
|
|
topicsFull.add(topicFull);
|
|
}
|
|
stakeholderFull.setTopics(topicsFull);
|
|
|
|
log.debug("after minimize stakeholder");
|
|
Stakeholder stakeholderSaved = stakeholderDAO.save(stakeholder);
|
|
log.debug("stakeholder saved!");
|
|
|
|
stakeholderFull.setId(stakeholderSaved.getId());
|
|
return stakeholderFull;
|
|
}
|
|
|
|
@RequestMapping(value = "/{stakeholder}/{topic}/{category}/{subcategory}/indicator/{id}", method = RequestMethod.DELETE)
|
|
public boolean deleteIndicator(@PathVariable("stakeholder") String stakeholder,
|
|
@PathVariable("topic") String topic,
|
|
@PathVariable("category") String category,
|
|
@PathVariable("subcategory") String subcategory,
|
|
@PathVariable("id") String id) {
|
|
|
|
Stakeholder _stakeholder = stakeholderDAO.findByAlias(stakeholder);
|
|
if(_stakeholder != null) {
|
|
Topic _topic = _stakeholder.getTopics().stream()
|
|
.filter(current_topic -> current_topic.getAlias().equals(topic))
|
|
.findFirst()
|
|
.orElse(null);
|
|
if(_topic != null) {
|
|
Category _category = _topic.getCategories().stream()
|
|
.filter(current_category -> current_category.getAlias().equals(category))
|
|
.findFirst()
|
|
.orElse(null);
|
|
if(_category != null) {
|
|
SubCategory _subCategory = _category.getSubCategories().stream()
|
|
.filter(current_subCategory -> current_subCategory.getAlias().equals(subcategory))
|
|
.findFirst()
|
|
.orElse(null);
|
|
if(_subCategory != null) {
|
|
List<String> indicators = null;
|
|
|
|
Indicator indicator = indicatorDAO.findById(id);
|
|
if(indicator.hasType("chart")) {
|
|
indicators =_subCategory.getCharts();
|
|
} else if(indicator.hasType("number")) {
|
|
indicators =_subCategory.getNumbers();
|
|
}
|
|
|
|
if(indicators == null) {
|
|
// EXCEPTION - No indicators found
|
|
}
|
|
|
|
//List<String> finalIndicators = indicators;
|
|
//log.debug("Indicators size: "+finalIndicators.size());
|
|
// int index = IntStream.range(0, indicators.size())
|
|
// .filter(i -> indicatorId.equals(finalIndicators.get(i)))
|
|
// .findFirst()
|
|
// .orElse(-1); // return -1 if target is not found
|
|
|
|
boolean indicatorFound = false;
|
|
Iterator<String> indicatorIterator = indicators.iterator();
|
|
while (indicatorIterator.hasNext()) {
|
|
String indicatorId = indicatorIterator.next();
|
|
log.debug(id + " vs "+indicatorId);
|
|
if(id.equals(indicatorId)) {
|
|
indicatorIterator.remove();
|
|
indicatorFound = true;
|
|
break;
|
|
}
|
|
}
|
|
|
|
log.debug(indicatorFound);
|
|
if(!indicatorFound) {
|
|
return false;
|
|
// EXCEPTION - Indicator not found
|
|
}
|
|
//indicators.remove(index);
|
|
|
|
stakeholderDAO.save(_stakeholder);
|
|
indicatorDAO.delete(id);
|
|
} else {
|
|
// EXCEPTION - Subcategory not found
|
|
}
|
|
} else {
|
|
// EXCEPTION - Category not found
|
|
}
|
|
} else {
|
|
// EXCEPTION - Topic not found
|
|
}
|
|
} else {
|
|
// EXCEPTION - Stakeholder not found
|
|
}
|
|
return true;
|
|
}
|
|
|
|
// @RequestMapping(value = "/{stakeholder}/{topic}/{category}/{subcategory}/indicator/delete", method = RequestMethod.POST)
|
|
// public boolean deleteChartPost(@PathVariable("stakeholder") String stakeholder,
|
|
// @PathVariable("topic") String topic,
|
|
// @PathVariable("category") String category,
|
|
// @PathVariable("subcategory") String subcategory,
|
|
// @RequestBody String indicatorId) {
|
|
// //String id = chart.getId();
|
|
// return deleteIndicator(stakeholder, topic, category, subcategory, indicatorId);
|
|
// }
|
|
|
|
|
|
// path variables are alias-es. Each alias must be unique.
|
|
@RequestMapping(value = "/{stakeholder}/{topic}/{category}/{subcategory}/indicator/save", method = RequestMethod.POST)
|
|
public Indicator saveIndicator(@PathVariable("stakeholder") String stakeholder,
|
|
@PathVariable("topic") String topic,
|
|
@PathVariable("category") String category,
|
|
@PathVariable("subcategory") String subcategory,
|
|
@RequestBody Indicator indicator) {
|
|
|
|
Indicator indicatorSaved = null;
|
|
if(indicator.getId() != null) {
|
|
log.debug("indicator is already saved");
|
|
indicatorSaved = indicatorDAO.save(indicator);
|
|
} else {
|
|
log.debug("to save indicator");
|
|
Stakeholder _stakeholder = stakeholderDAO.findByAlias(stakeholder);
|
|
if (_stakeholder != null) {
|
|
Topic _topic = _stakeholder.getTopics().stream()
|
|
.filter(current_topic -> current_topic.getAlias().equals(topic))
|
|
.findFirst()
|
|
.orElse(null);
|
|
if (_topic != null) {
|
|
Category _category = _topic.getCategories().stream()
|
|
.filter(current_category -> current_category.getAlias().equals(category))
|
|
.findFirst()
|
|
.orElse(null);
|
|
if (_category != null) {
|
|
SubCategory _subCategory = _category.getSubCategories().stream()
|
|
.filter(current_subCategory -> current_subCategory.getAlias().equals(subcategory))
|
|
.findFirst()
|
|
.orElse(null);
|
|
if (_subCategory != null) {
|
|
indicatorSaved = indicatorDAO.save(indicator);
|
|
|
|
List<String> indicators = null;
|
|
|
|
if (indicator.hasType("chart")) {
|
|
indicators = _subCategory.getCharts();
|
|
} else if (indicator.hasType("number")) {
|
|
indicators = _subCategory.getNumbers();
|
|
}
|
|
|
|
String indicatorId;
|
|
if (indicator.getId() != null) {
|
|
indicatorId = indicators.stream()
|
|
.filter(current_indicator -> current_indicator.equals(indicator.getId()))
|
|
.findFirst()
|
|
.orElse(null);
|
|
|
|
if (indicatorId == null) { // indicator is not already at this position
|
|
indicators.add(indicator.getId());
|
|
}
|
|
}
|
|
stakeholderDAO.save(_stakeholder);
|
|
} else {
|
|
// EXCEPTION - Subcategory not found
|
|
}
|
|
} else {
|
|
// EXCEPTION - Category not found
|
|
}
|
|
} else {
|
|
// EXCEPTION - Topic not found
|
|
}
|
|
} else {
|
|
// EXCEPTION - Stakeholder not found
|
|
}
|
|
}
|
|
|
|
return indicatorSaved;
|
|
}
|
|
|
|
|
|
// The following are not supposed to be used
|
|
|
|
// should i delete indicators that were in the list but they are not in the new list?
|
|
@RequestMapping(value = "/{stakeholder}/{topic}/{category}/{subcategory}/charts/save", method = RequestMethod.POST)
|
|
public List<Indicator> saveCharts(@PathVariable("stakeholder") String stakeholder,
|
|
@PathVariable("topic") String topic,
|
|
@PathVariable("category") String category,
|
|
@PathVariable("subcategory") String subcategory,
|
|
@RequestBody List<Indicator> charts) {
|
|
log.debug(charts);
|
|
log.debug(charts.size());
|
|
log.debug(charts.getClass().getName());
|
|
log.debug(charts.get(0).getClass().getName());
|
|
return saveIndicators(stakeholder, topic, category, subcategory, charts, "chart");
|
|
}
|
|
|
|
// should i delete indicators that were in the list but they are not in the new list?
|
|
@RequestMapping(value = "/{stakeholder}/{topic}/{category}/{subcategory}/numbers/save", method = RequestMethod.POST)
|
|
public List<Indicator> saveNumbers(@PathVariable("stakeholder") String stakeholder,
|
|
@PathVariable("topic") String topic,
|
|
@PathVariable("category") String category,
|
|
@PathVariable("subcategory") String subcategory,
|
|
@RequestBody List<Indicator> numbers) {
|
|
return saveIndicators(stakeholder, topic, category, subcategory, numbers, "number");
|
|
}
|
|
|
|
public List<Indicator> saveIndicators(String stakeholder, String topic,
|
|
String category, String subcategory,
|
|
List<Indicator> indicators, String type) {
|
|
log.debug("to save indicators: "+indicators.size());
|
|
List<Indicator> indicatorsSaved = new ArrayList<>();
|
|
for(Indicator indicator : indicators) {
|
|
indicatorsSaved.add(indicatorDAO.save(indicator));
|
|
}
|
|
log.debug("saved indicators: "+indicators.size());
|
|
|
|
Stakeholder _stakeholder = stakeholderDAO.findByAlias(stakeholder);
|
|
if(_stakeholder != null) {
|
|
Topic _topic = _stakeholder.getTopics().stream()
|
|
.filter(current_topic -> current_topic.getAlias().equals(topic))
|
|
.findFirst()
|
|
.orElse(null);
|
|
if(_topic != null) {
|
|
Category _category = _topic.getCategories().stream()
|
|
.filter(current_category -> current_category.getAlias().equals(category))
|
|
.findFirst()
|
|
.orElse(null);
|
|
if(_category != null) {
|
|
SubCategory _subCategory = _category.getSubCategories().stream()
|
|
.filter(current_subCategory -> current_subCategory.getAlias().equals(subcategory))
|
|
.findFirst()
|
|
.orElse(null);
|
|
if(_subCategory != null) {
|
|
List<String> _indicators = null;
|
|
if(type.equals("chart")) {
|
|
_indicators = _subCategory.getCharts();
|
|
} else if(type.equals("number")) {
|
|
_indicators = _subCategory.getNumbers();
|
|
}
|
|
|
|
_indicators.clear();
|
|
for(Indicator indicator : indicators) {
|
|
_indicators.add(indicator.getId());
|
|
}
|
|
stakeholderDAO.save(_stakeholder);
|
|
} else {
|
|
// EXCEPTION - Subcategory not found
|
|
}
|
|
} else {
|
|
// EXCEPTION - Vategory not found
|
|
}
|
|
} else {
|
|
// EXCEPTION - Topic not found
|
|
}
|
|
} else {
|
|
// EXCEPTION - Stakeholder not found
|
|
}
|
|
|
|
return indicatorsSaved;
|
|
}
|
|
|
|
|
|
|
|
// Remember to check if alias is not already used before saving
|
|
@RequestMapping(value = "/{stakeholder}/topic/save", method = RequestMethod.POST)
|
|
public Stakeholder saveTopic(@PathVariable("stakeholder") String stakeholder,
|
|
@RequestBody Topic topic) {
|
|
Stakeholder stakeholderSaved = null;
|
|
Stakeholder _stakeholder = stakeholderDAO.findByAlias(stakeholder);
|
|
if (stakeholder != null) {
|
|
List<Topic> topics = _stakeholder.getTopics();
|
|
Topic _topic = topics.stream()
|
|
.filter(current_topic -> current_topic.getAlias().equals(topic.getAlias()))
|
|
.findFirst()
|
|
.orElse(null);
|
|
if(_topic != null) {
|
|
_topic = topic;
|
|
} else {
|
|
topics.add(topic);
|
|
_stakeholder.setTopics(topics);
|
|
}
|
|
stakeholderSaved = stakeholderDAO.save(_stakeholder);
|
|
}
|
|
return stakeholderSaved;
|
|
}
|
|
|
|
@RequestMapping(value = "/{stakeholder}/topics/save", method = RequestMethod.POST)
|
|
public Stakeholder saveTopics(@PathVariable("stakeholder") String stakeholder,
|
|
@RequestBody List<Topic> topics) {
|
|
Stakeholder stakeholderSaved = null;
|
|
Stakeholder _stakeholder = stakeholderDAO.findByAlias(stakeholder);
|
|
if (stakeholder != null) {
|
|
_stakeholder.setTopics(topics);
|
|
stakeholderSaved = stakeholderDAO.save(_stakeholder);
|
|
}
|
|
return stakeholderSaved;
|
|
}
|
|
|
|
|
|
@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;
|
|
}
|
|
}
|