1. Schema changes: Move each entity on its own collection - each entity keeps ids for its sub-entities.

2. New controllers for each entity: TopicController.java, CategoryController.java, SubCategoryController.java, IndicatorController.java.
3. New DAOs for each entity: TopicDAO.java, MongoDBTopicDAO.java, CategoryDAO.java, MongoDBCategoryDAO.java, SubCategoryDAO.java, MongoDBSubCategoryDAO.java.
4. New custom Exceptions: EntityNotFoundException.java, PathNotValidException.java.
5. ExceptionsHandler.java: Handle new EntityNotFoundException (id not in db) and PathNotValidException (id exists in db but not in path given).
This commit is contained in:
Konstantina Galouni 2019-11-22 15:50:59 +00:00
parent 6bed8383d5
commit 26f7388680
22 changed files with 1286 additions and 672 deletions

View File

@ -0,0 +1,191 @@
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 CategoryController {
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 IndicatorDAO indicatorDAO;
@Autowired
private SubCategoryController subCategoryController;
public Category<SubCategory> buildCategory(Category<SubCategory> categoryFull) {
Category<String> category = new Category<>(categoryFull);
List<String> subCategories = new ArrayList<>();
List<SubCategory> subCategoriesFull = new ArrayList<>();
for(SubCategory<Indicator> subCategory : categoryFull.getSubCategories()) {
SubCategory<Indicator> subcategoryFull = subCategoryController.buildSubCategory(subCategory);
subCategoriesFull.add(subcategoryFull);
subCategories.add(subcategoryFull.getId());
}
categoryFull.setSubCategories(subCategoriesFull);
category.setSubCategories(subCategories);
Category<String> categorySaved = categoryDAO.save(category);
categoryFull.setId(categorySaved.getId());
return categoryFull;
}
@RequestMapping(value = "/{stakeholderId}/{topicId}/save", method = RequestMethod.POST)
public Category<SubCategory> saveCategory(@PathVariable("stakeholderId") String stakeholderId,
@PathVariable("topicId") String topicId,
@RequestBody Category<SubCategory> categoryFull) {
log.debug("save category");
Stakeholder<String> stakeholder = stakeholderDAO.findById(stakeholderId);
if(stakeholder != null) {
Topic<String> topic = topicDAO.findById(topicId);
if(topic != null) {
if(stakeholder.getTopics().contains(topicId)) {
// if category not exists (no id), create a new default subcategory, identical to category
if(categoryFull.getId() == null) {
SubCategory<String> subCategory = new SubCategory<>();
subCategory.setName(categoryFull.getName());
subCategory.setAlias(categoryFull.getAlias());
subCategory.setIsActive(categoryFull.getIsActive());
subCategory.setIsPublic(categoryFull.getIsPublic());
subCategory.setIsDefault(categoryFull.getIsDefault());
subCategory.setCharts(new ArrayList<String>());
subCategory.setNumbers(new ArrayList<String>());
SubCategory<String> subCategorySaved = subCategoryDAO.save(subCategory);
List<SubCategory> subCategories = categoryFull.getSubCategories();
subCategories.add(subCategorySaved);
}
Category<String> category = new Category<>(categoryFull);
List<String> subCategories = new ArrayList<>();
for(SubCategory subCategory : categoryFull.getSubCategories()) {
subCategories.add(subCategory.getId());
}
category.setSubCategories(subCategories);
Category<String> categorySaved = categoryDAO.save(category);
List<String> categories = topic.getCategories();
int index = categories.indexOf(categorySaved.getId());
if(index == -1) {
categories.add(categorySaved.getId());
topicDAO.save(topic);
log.debug("Category saved!");
categoryFull.setId(categorySaved.getId());
}
subCategories = null;
category = null;
} else {
// EXCEPTION - Topic not found in Stakeholder: stakeholder.getAlias();
throw new PathNotValidException("Save category: Topic with id: "+topicId+" not found in Stakeholder: "+stakeholderId);
}
} else {
// EXCEPTION - Topic not found
throw new EntityNotFoundException("Save category: Topic with id: "+topicId+" not found");
}
} else {
// EXCEPTION - Stakeholder not found
throw new EntityNotFoundException("Save category: Stakeholder with id: "+stakeholderId+" not found");
}
return categoryFull;
}
@RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/delete", method = RequestMethod.DELETE)
public boolean deleteCategory(@PathVariable("stakeholderId") String stakeholderId,
@PathVariable("topicId") String topicId,
@PathVariable("categoryId") String categoryId) {
log.debug("delete category");
Stakeholder<String> stakeholder = stakeholderDAO.findById(stakeholderId);
if(stakeholder != null) {
Topic<String> topic = topicDAO.findById(topicId);
if(topic != null) {
if(stakeholder.getTopics().contains(topicId)) {
Category<String> category = categoryDAO.findById(categoryId);
if(category != null) {
List<String> categories = topic.getCategories();
int index = categories.indexOf(categoryId);
if(index != -1) {
for(String subCategoryId : category.getSubCategories()) {
SubCategory<String> subcategory = subCategoryDAO.findById(subCategoryId);
if(subcategory == null) {
// EXCEPTION - SubCategory not found
throw new EntityNotFoundException("Delete category: SubCategory with id: "+subCategoryId+" not found (subcategory exists in category: "+categoryId+")");
}
for(String chartId : subcategory.getCharts()) {
indicatorDAO.delete(chartId);
}
subcategory.setCharts(null);
for(String numberId : subcategory.getNumbers()) {
indicatorDAO.delete(numberId);
}
subcategory.setNumbers(null);
subCategoryDAO.delete(subCategoryId);
}
category.setSubCategories(null);
categories.remove(index);
topicDAO.save(topic);
categoryDAO.delete(categoryId);
log.debug("Category deleted!");
} else {
// EXCEPTION - Category not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias();
throw new PathNotValidException("Delete category: Category with id: "+categoryId+" not found in Topic: "+topicId);
}
} else {
// EXCEPTION - Category not found
throw new EntityNotFoundException("Delete category: Category with id: "+categoryId+" not found");
}
} else {
// EXCEPTION - Topic not found in Stakeholder: stakeholder.getAlias();
throw new PathNotValidException("Delete category: Topic with id: "+topicId+" not found in Stakeholder: "+stakeholderId);
}
} else {
// EXCEPTION - Topic not found
throw new EntityNotFoundException("Delete category: Topic with id: "+topicId+" not found");
}
} else {
// EXCEPTION - Stakeholder not found
throw new EntityNotFoundException("Delete category: Stakeholder with id: "+stakeholderId+" not found");
}
return true;
}
}

View File

@ -0,0 +1,337 @@
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.Iterator;
import java.util.List;
@RestController
@CrossOrigin(origins = "*")
public class IndicatorController {
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 IndicatorDAO indicatorDAO;
@RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/save", method = RequestMethod.POST)
public Indicator saveIndicator(@PathVariable("stakeholderId") String stakeholderId,
@PathVariable("topicId") String topicId,
@PathVariable("categoryId") String categoryId,
@PathVariable("subcategoryId") String subcategoryId,
@RequestBody Indicator indicator) {
log.debug("save indicator");
// if(stakeholderId == null) {
// // EXCEPTION - Parameter for Stakeholder is not accepted
// }
// if(topicId == null) {
// // EXCEPTION - Parameter for Topic is not accepted
// }
// if(categoryId == null) {
// // EXCEPTION - Parameter for Category is not accepted
// }
// if(subcategoryId == null) {
// // EXCEPTION - Parameter for SubCategory is not accepted
// }
// if(indicator == null) {
// // EXCEPTION - Parameter for Indicator is not accepted
// }
Stakeholder<String> stakeholder = stakeholderDAO.findById(stakeholderId);
if(stakeholder != null) {
Topic<String> topic = topicDAO.findById(topicId);
if(topic != null) {
if(stakeholder.getTopics().contains(topicId)) {
Category<String> category = categoryDAO.findById(categoryId);
if(category != null) {
if(topic.getCategories().contains(categoryId)) {
SubCategory<String> subcategory = subCategoryDAO.findById(subcategoryId);
if(subcategory != null) {
if (category.getSubCategories().contains(subcategoryId)) {
Indicator indicatorSaved = indicatorDAO.save(indicator);
List<String> indicators = null;
//if(indicator.hasType("chart")) {
if(indicator.getType().equals("chart")) {
indicators = subcategory.getCharts();
//} else if(indicator.hasType("number")) {
} else if(indicator.getType().equals("number")) {
indicators = subcategory.getNumbers();
}
int index = indicators.indexOf(indicatorSaved.getId());
if (index == -1) {
indicators.add(indicatorSaved.getId());
subCategoryDAO.save(subcategory);
log.debug("Indicator saved!");
indicator.setId(indicatorSaved.getId());
}
} else {
// 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);
}
} else {
// EXCEPTION - SubCategory not found
throw new EntityNotFoundException("Save indicator: SubCategory with id: "+subcategoryId+" not found");
}
} else {
// 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);
}
} else {
// EXCEPTION - Category not found
throw new EntityNotFoundException("Save indicator: Category with id: "+categoryId+" not found");
}
} else {
// EXCEPTION - Topic not found in Stakeholder: stakeholder.getAlias();
throw new PathNotValidException("Save indicator: Topic with id: "+topicId+" not found in Stakeholder: "+stakeholderId);
}
} else {
// EXCEPTION - Topic not found
throw new EntityNotFoundException("Save indicator: Topic with id: "+topicId+" not found");
}
} else {
// EXCEPTION - Stakeholder not found
throw new EntityNotFoundException("Save indicator: Stakeholder with id: "+stakeholderId+" not found");
}
return indicator;
}
@RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/{indicatorId}/delete", method = RequestMethod.DELETE)
public boolean deleteIndicator(@PathVariable("stakeholderId") String stakeholderId,
@PathVariable("topicId") String topicId,
@PathVariable("categoryId") String categoryId,
@PathVariable("subcategoryId") String subcategoryId,
@PathVariable("indicatorId") String indicatorId) {
log.debug("delete indicator");
Stakeholder<String> stakeholder = stakeholderDAO.findById(stakeholderId);
if(stakeholder != null) {
Topic<String> topic = topicDAO.findById(topicId);
if(topic != null) {
if(stakeholder.getTopics().contains(topicId)) {
Category<String> category = categoryDAO.findById(categoryId);
if(category != null) {
if(topic.getCategories().contains(categoryId)) {
SubCategory<String> subcategory = subCategoryDAO.findById(subcategoryId);
if(subcategory != null) {
if(category.getSubCategories().contains(subcategoryId)) {
Indicator indicator = indicatorDAO.findById(indicatorId);
if(indicator != null) {
List<String> indicators = null;
if (indicator.getType().equals("chart")) {
indicators = subcategory.getCharts();
} else if (indicator.getType().equals("number")) {
indicators = subcategory.getNumbers();
}
int index = indicators.indexOf(indicatorId);
if (index != -1) {
indicators.remove(index);
subCategoryDAO.save(subcategory);
indicatorDAO.delete(indicatorId);
log.debug("Indicator deleted!");
} else {
// EXCEPTION - Indicator not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); -> Category: category.getAlias(); -> SubCategory: subcategory.getAlias();
throw new PathNotValidException("Delete indicator: Indicator with id: "+indicatorId+" not found in SubCategory: "+subcategoryId);
}
} else {
// EXCEPTION - Indicator not found
throw new EntityNotFoundException("Delete indicator: Indicator with id: "+indicatorId+" not found");
}
} else {
// EXCEPTION - SubCategory not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); -> Category: category.getAlias();
throw new PathNotValidException("Delete indicator: SubCategory with id: "+subcategoryId+" not found in Category: "+categoryId);
}
} else {
// EXCEPTION - SubCategory not found
throw new EntityNotFoundException("Delete indicator: SubCategory with id: "+subcategoryId+" not found");
}
} else {
// EXCEPTION - Category not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias();
throw new PathNotValidException("Delete indicator: Category with id: "+categoryId+" not found in Topic: "+topicId);
}
} else {
// EXCEPTION - Category not found
throw new EntityNotFoundException("Delete indicator: Category with id: "+categoryId+" not found");
}
} else {
// EXCEPTION - Topic not found in Stakeholder: stakeholder.getAlias();
throw new PathNotValidException("Delete indicator: Topic with id: "+topicId+" not found in Stakeholder: "+stakeholderId);
}
} else {
// EXCEPTION - Topic not found
throw new EntityNotFoundException("Delete indicator: Topic with id: "+topicId+" not found");
}
} else {
// EXCEPTION - Stakeholder not found
throw new EntityNotFoundException("Delete indicator: Stakeholder with id: "+stakeholderId+" not found");
}
return true;
}
@RequestMapping(value = "/{stakeholderId}/charts/delete", method = RequestMethod.DELETE)
public boolean deleteAllChartIndicators(@PathVariable("stakeholderId") String stakeholderId) {
log.debug("delete indicator");
Stakeholder<String> stakeholder = stakeholderDAO.findById(stakeholderId);
if(stakeholder != null) {
for(String topicId : stakeholder.getTopics()) {
Topic<String> topic = topicDAO.findById(topicId);
if(topic != null) {
for(String categoryId : topic.getCategories()) {
Category<String> category = categoryDAO.findById(categoryId);
if(category != null) {
for(String subcategoryId : category.getSubCategories()) {
SubCategory<String> subcategory = subCategoryDAO.findById(subcategoryId);
if(subcategory != null) {
List<String> indicators = subcategory.getCharts();
Iterator<String> indicatorsIterator = subcategory.getCharts().iterator();
while (indicatorsIterator.hasNext()) {
String indicatorId = indicatorsIterator.next();
Indicator indicator = indicatorDAO.findById(indicatorId);
if (indicator != null) {
int index = indicators.indexOf(indicatorId);
if (index != -1) {
indicatorsIterator.remove();
//indicators.remove(index);
indicatorDAO.delete(indicatorId);
log.debug("Indicator deleted!");
} else {
// EXCEPTION - Indicator not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); -> Category: category.getAlias(); -> SubCategory: subcategory.getAlias();
throw new PathNotValidException("Delete indicator: Indicator with id: " + indicatorId + " not found in SubCategory: " + subcategoryId);
}
} else {
// EXCEPTION - Indicator not found
throw new EntityNotFoundException("Delete indicator: Indicator with id: " + indicatorId + " not found");
}
}
subCategoryDAO.save(subcategory);
} else {
// EXCEPTION - SubCategory not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); -> Category: category.getAlias();
throw new PathNotValidException("Delete indicator: SubCategory with id: "+subcategoryId+" not found in Category: "+categoryId);
}
}
} else {
// EXCEPTION - Category not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias();
throw new PathNotValidException("Delete indicator: Category with id: "+categoryId+" not found in Topic: "+topicId);
}
}
} else {
// EXCEPTION - Topic not found in Stakeholder: stakeholder.getAlias();
throw new PathNotValidException("Delete indicator: Topic with id: "+topicId+" not found in Stakeholder: "+stakeholderId);
}
}
} else {
// EXCEPTION - Stakeholder not found
throw new EntityNotFoundException("Delete indicator: Stakeholder with id: "+stakeholderId+" not found");
}
return true;
}
@RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/{type}/reorder", method = RequestMethod.POST)
public boolean reorderIndicators(@PathVariable("stakeholderId") String stakeholderId,
@PathVariable("topicId") String topicId,
@PathVariable("categoryId") String categoryId,
@PathVariable("subcategoryId") String subcategoryId,
@PathVariable("type") String type,
@RequestBody List<Indicator> indicatorsFull) {
log.debug("reorder indicators");
Stakeholder<String> stakeholder = stakeholderDAO.findById(stakeholderId);
if (stakeholder != null) {
Topic<String> topic = topicDAO.findById(topicId);
if (topic != null) {
if (stakeholder.getTopics().contains(topicId)) {
Category<String> category = categoryDAO.findById(categoryId);
if (category != null) {
if (topic.getCategories().contains(categoryId)) {
SubCategory<String> subcategory = subCategoryDAO.findById(subcategoryId);
if (subcategory != null) {
if (category.getSubCategories().contains(subcategoryId)) {
List<String> indicators = new ArrayList<>();
for(Indicator indicator : indicatorsFull) {
indicators.add(indicator.getId());
}
if(type.equals("chart")) {
subcategory.setCharts(indicators);
} else if(type.equals("number")) {
subcategory.setNumbers(indicators);
}
subCategoryDAO.save(subcategory);
indicators = null;
log.debug("Indicators reordered!");
} else {
// EXCEPTION - SubCategory not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); -> Category: category.getAlias();
throw new PathNotValidException("Reorder indicators: SubCategory with id: "+subcategoryId+" not found in Category: "+categoryId);
}
} else {
// EXCEPTION - SubCategory not found
throw new EntityNotFoundException("Reorder indicators: SubCategory with id: "+subcategoryId+" not found");
}
} else {
// EXCEPTION - Category not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias();
throw new PathNotValidException("Reorder indicators: Category with id: "+categoryId+" not found in Topic: "+topicId);
}
} else {
// EXCEPTION - Category not found
throw new EntityNotFoundException("Reorder indicators: Category with id: "+categoryId+" not found");
}
} else {
// EXCEPTION - Topic not found in Stakeholder: stakeholder.getAlias();
throw new PathNotValidException("Reorder indicators: Topic with id: "+topicId+" not found in Stakeholder: "+stakeholderId);
}
} else {
// EXCEPTION - Topic not found
throw new EntityNotFoundException("Reorder indicators: Topic with id: "+topicId+" not found");
}
} else {
// EXCEPTION - Stakeholder not found
throw new EntityNotFoundException("Reorder indicators: Stakeholder with id: "+stakeholderId+" not found");
}
return true;
}
}

View File

@ -1,18 +1,16 @@
package eu.dnetlib.uoamonitorservice.controllers; package eu.dnetlib.uoamonitorservice.controllers;
import com.fasterxml.jackson.core.type.TypeReference; //import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper; //import com.fasterxml.jackson.databind.ObjectMapper;
import eu.dnetlib.uoamonitorservice.dao.IndicatorDAO; import eu.dnetlib.uoamonitorservice.dao.*;
import eu.dnetlib.uoamonitorservice.dao.StakeholderDAO;
import eu.dnetlib.uoamonitorservice.entities.*; import eu.dnetlib.uoamonitorservice.entities.*;
import eu.dnetlib.uoamonitorservice.handlers.EntityNotFoundException;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.Iterator;
import java.util.List; import java.util.List;
@RestController @RestController
@ -23,15 +21,62 @@ public class StakeholderController {
@Autowired @Autowired
private StakeholderDAO stakeholderDAO; private StakeholderDAO stakeholderDAO;
@Autowired
private TopicDAO topicDAO;
@Autowired
private CategoryDAO categoryDAO;
@Autowired
private SubCategoryDAO subCategoryDAO;
@Autowired @Autowired
private IndicatorDAO indicatorDAO; private IndicatorDAO indicatorDAO;
public Stakeholder setIndicatorsForStakeholder(Stakeholder stakeholder) { @Autowired
for (Topic topic: stakeholder.getTopics()) { private TopicController topicController;
for(Category category : topic.getCategories()) {
@RequestMapping(value = "/build-stakeholder", method = RequestMethod.POST)
public Stakeholder<Topic<Category<SubCategory<Indicator>>>> buildFullStakeholder(@RequestBody Stakeholder<Topic<Category<SubCategory<Indicator>>>> stakeholderFull) {
log.debug("build stakeholder");
Stakeholder<String> stakeholder = new Stakeholder<>(stakeholderFull);
List<String> topics = new ArrayList<>();
List<Topic<Category<SubCategory<Indicator>>>> topicsFull = new ArrayList<>();
for(Topic topic : stakeholderFull.getTopics()) {
Topic<Category<SubCategory<Indicator>>> topicFull = topicController.buildTopic(topic);
topicsFull.add(topicFull);
topics.add(topicFull.getId());
}
stakeholderFull.setTopics(topicsFull);
stakeholder.setTopics(topics);
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);
Topic<Category> topicFull = new Topic<Category>(topic);
List<Category> categories = new ArrayList<>();
for(String categoryId : topic.getCategories()) {
Category<String> category = categoryDAO.findById(categoryId);
Category<SubCategory> categoryFull = new Category<SubCategory>(category);
List<SubCategory> subCategories = new ArrayList<>(); List<SubCategory> subCategories = new ArrayList<>();
for(SubCategory<String> subCategory : category.getSubCategories()) { for(String subCategoryId : category.getSubCategories()) {
SubCategory<String> subCategory = subCategoryDAO.findById(subCategoryId);
SubCategory subCategoryFull = new SubCategory<Indicator>(subCategory); SubCategory subCategoryFull = new SubCategory<Indicator>(subCategory);
List<Indicator> charts = new ArrayList<>(); List<Indicator> charts = new ArrayList<>();
@ -49,11 +94,16 @@ public class StakeholderController {
subCategories.add(subCategoryFull); subCategories.add(subCategoryFull);
} }
category.setSubCategories(subCategories); categoryFull.setSubCategories(subCategories);
categories.add(categoryFull);
} }
topicFull.setCategories(categories);
topics.add(topicFull);
} }
return stakeholder;
stakeholderFull.setTopics(topics);
return stakeholderFull;
} }
@RequestMapping(value = "/stakeholder/all", method = RequestMethod.GET) @RequestMapping(value = "/stakeholder/all", method = RequestMethod.GET)
@ -65,11 +115,12 @@ public class StakeholderController {
stakeholders = stakeholderDAO.findByType(type); stakeholders = stakeholderDAO.findByType(type);
} }
List<Stakeholder> stakeholdersFull = new ArrayList<>();
for(Stakeholder stakeholder : stakeholders) { for(Stakeholder stakeholder : stakeholders) {
this.setIndicatorsForStakeholder(stakeholder); stakeholdersFull.add(this.setFullEntities(stakeholder));
} }
return stakeholders; return stakeholdersFull;
} }
@RequestMapping(value = "/stakeholder/default", method = RequestMethod.GET) @RequestMapping(value = "/stakeholder/default", method = RequestMethod.GET)
@ -81,10 +132,11 @@ public class StakeholderController {
stakeholders = stakeholderDAO.findByIsDefaultProfileAndType(true, type); stakeholders = stakeholderDAO.findByIsDefaultProfileAndType(true, type);
} }
List<Stakeholder> stakeholdersFull = new ArrayList<>();
for(Stakeholder stakeholder : stakeholders) { for(Stakeholder stakeholder : stakeholders) {
this.setIndicatorsForStakeholder(stakeholder); stakeholdersFull.add(this.setFullEntities(stakeholder));
} }
return stakeholders; return stakeholdersFull;
} }
@RequestMapping(value = "/stakeholder", method = RequestMethod.GET) @RequestMapping(value = "/stakeholder", method = RequestMethod.GET)
@ -96,648 +148,149 @@ public class StakeholderController {
stakeholders = stakeholderDAO.findByIsDefaultProfileAndType(false, type); stakeholders = stakeholderDAO.findByIsDefaultProfileAndType(false, type);
} }
List<Stakeholder> stakeholdersFull = new ArrayList<>();
for(Stakeholder stakeholder : stakeholders) { for(Stakeholder stakeholder : stakeholders) {
this.setIndicatorsForStakeholder(stakeholder); stakeholdersFull.add(this.setFullEntities(stakeholder));
} }
log.debug(new Date()); log.debug(new Date());
return stakeholders; return stakeholdersFull;
} }
@RequestMapping(value = "/stakeholder/{alias}", method = RequestMethod.GET) @RequestMapping(value = "/stakeholder/{alias}", method = RequestMethod.GET)
public Stakeholder getStakeholder(@PathVariable("alias") String alias) { public Stakeholder getStakeholder(@PathVariable("alias") String alias) {
Stakeholder stakeholder = stakeholderDAO.findByAlias(alias); Stakeholder<String> stakeholder = stakeholderDAO.findByAlias(alias);
this.setIndicatorsForStakeholder(stakeholder); if(stakeholder == null) {
// EXCEPTION - Stakeholder not found
return stakeholder; throw new EntityNotFoundException("Get stakeholder: Stakeholder with alias: "+alias+" not found");
}
return this.setFullEntities(stakeholder);
} }
@RequestMapping(value = "/save", method = RequestMethod.POST)
@RequestMapping(value = "/stakeholder/save", method = RequestMethod.POST) public Stakeholder<Topic> saveStakeholder(@RequestBody Stakeholder<Topic> stakeholderFull) {
public Stakeholder saveStakeholder(@RequestBody Stakeholder stakeholder) {
log.debug("save stakeholder"); log.debug("save stakeholder");
Stakeholder stakeholderFull = new Stakeholder(stakeholder); // if(stakeholderFull == null) {
// log.debug("stakeholder null");
// // EXCEPTION - Parameter for Stakeholder is not accepted
// }
List<Topic> topicsFull = new ArrayList<>(); Stakeholder<String> stakeholder = new Stakeholder<>(stakeholderFull);
for (Topic topic: stakeholder.getTopics()) { List<String> topics = new ArrayList<>();
Topic topicFull = new Topic(topic); for(Topic topic : stakeholderFull.getTopics()) {
topics.add(topic.getId());
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); stakeholder.setTopics(topics);
log.debug("after minimize stakeholder");
Stakeholder stakeholderSaved = stakeholderDAO.save(stakeholder);
log.debug("stakeholder saved!");
Stakeholder<String> stakeholderSaved = stakeholderDAO.save(stakeholder);
stakeholderFull.setId(stakeholderSaved.getId()); stakeholderFull.setId(stakeholderSaved.getId());
topics = null;
stakeholder = null;
stakeholderSaved = null;
return stakeholderFull; return stakeholderFull;
} }
@RequestMapping(value = "/{stakeholder}", method = RequestMethod.DELETE)
public boolean deleteStakeholder(@PathVariable("stakeholder") String stakeholder) { @RequestMapping(value = "/{stakeholderId}/delete", method = RequestMethod.DELETE)
Stakeholder _stakeholder = stakeholderDAO.findById(stakeholder); public boolean deleteStakeholder(@PathVariable("stakeholderId") String stakeholderId) {
if(_stakeholder != null) { log.debug("delete stakeholder");
for (Topic topic : _stakeholder.getTopics()) {
for (Category category : topic.getCategories()) { Stakeholder<String> stakeholder = stakeholderDAO.findById(stakeholderId);
for (SubCategory<String> subcategory : category.getSubCategories()) {
for(String chartId : subcategory.getCharts()) { 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 chartId : subcategory.getCharts()) {
indicatorDAO.delete(chartId); indicatorDAO.delete(chartId);
} }
for(String numberId : subcategory.getNumbers()) { subcategory.setCharts(null);
for (String numberId : subcategory.getNumbers()) {
indicatorDAO.delete(numberId); indicatorDAO.delete(numberId);
} }
subcategory.setCharts(null);
subcategory.setNumbers(null); subcategory.setNumbers(null);
subCategoryDAO.delete(subCategoryId);
} }
category.setSubCategories(null); category.setSubCategories(null);
categoryDAO.delete(categoryId);
} }
topic.setCategories(null); topic.setCategories(null);
topicDAO.delete(topicId);
} }
stakeholder.setTopics(null);
} else { stakeholderDAO.delete(stakeholderId);
return false; log.debug("Stakeholder deleted!");
// EXCEPTION - Stakeholder not found
}
stakeholderDAO.delete(_stakeholder.getId());
_stakeholder = null;
return true;
}
@RequestMapping(value = "/{stakeholder}/{topic}", method = RequestMethod.DELETE)
public Stakeholder deleteTopic(@PathVariable("stakeholder") String stakeholder,
@PathVariable("topic") String topic) {
Stakeholder _stakeholder = stakeholderDAO.findByAlias(stakeholder);
if(_stakeholder != null) {
boolean topicFound = false;
Iterator<Topic> topicIterator = _stakeholder.getTopics().iterator();
while (topicIterator.hasNext()) {
Topic _topic = topicIterator.next();
if(_topic.getAlias().equals(topic)) {
for (Category category : _topic.getCategories()) {
for (SubCategory<String> subcategory : category.getSubCategories()) {
for(String chartId : subcategory.getCharts()) {
indicatorDAO.delete(chartId);
}
for(String numberId : subcategory.getNumbers()) {
indicatorDAO.delete(numberId);
}
subcategory.setCharts(null);
subcategory.setNumbers(null);
}
category.setSubCategories(null);
}
_topic.setCategories(null);
topicIterator.remove();
stakeholderDAO.save(_stakeholder);
topicFound = true;
break;
}
}
if(!topicFound) {
// EXCEPTION - Topic not found
}
} else {
// EXCEPTION - Stakeholder not found
}
this.setIndicatorsForStakeholder(_stakeholder);
return _stakeholder;
}
@RequestMapping(value = "/{stakeholder}/{topic}/{category}", method = RequestMethod.DELETE)
public Stakeholder deleteCategory(@PathVariable("stakeholder") String stakeholder,
@PathVariable("topic") String topic,
@PathVariable("category") String category) {
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) {
boolean categoryFound = false;
Iterator<Category> categoryIterator = _topic.getCategories().iterator();
while (categoryIterator.hasNext()) {
Category _category = categoryIterator.next();
if (_category.getAlias().equals(category)) {
for (SubCategory<String> subcategory : _category.getSubCategories()) {
for(String chartId : subcategory.getCharts()) {
indicatorDAO.delete(chartId);
}
for(String numberId : subcategory.getNumbers()) {
indicatorDAO.delete(numberId);
}
subcategory.setCharts(null);
subcategory.setNumbers(null);
}
_category.setSubCategories(null);
categoryIterator.remove();
stakeholderDAO.save(_stakeholder);
categoryFound = true;
break;
}
}
if(!categoryFound) {
// EXCEPTION - Category not found
}
} else {
// EXCEPTION - Topic not found
}
} else {
// EXCEPTION - Stakeholder not found
}
this.setIndicatorsForStakeholder(_stakeholder);
return _stakeholder;
}
@RequestMapping(value = "/{stakeholder}/{topic}/{category}/{subcategory}", method = RequestMethod.DELETE)
public Stakeholder deleteSubCategory(@PathVariable("stakeholder") String stakeholder,
@PathVariable("topic") String topic,
@PathVariable("category") String category,
@PathVariable("subcategory") String subcategory) {
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) {
boolean subCategoryFound = false;
Iterator<SubCategory> subCategoryIterator = _category.getSubCategories().iterator();
while (subCategoryIterator.hasNext()) {
SubCategory<String> _subCategory = subCategoryIterator.next();
if (_subCategory.getAlias().equals(subcategory)) {
for(String chartId : _subCategory.getCharts()) {
indicatorDAO.delete(chartId);
}
for(String numberId : _subCategory.getNumbers()) {
indicatorDAO.delete(numberId);
}
_subCategory.setCharts(null);
_subCategory.setNumbers(null);
subCategoryIterator.remove();
stakeholderDAO.save(_stakeholder);
subCategoryFound = true;
break;
}
}
if (!subCategoryFound) {
// EXCEPTION - SubCategory not found
}
} else {
// EXCEPTION - Category not found
}
} else {
// EXCEPTION - Topic not found
}
} else {
// EXCEPTION - Stakeholder not found
}
this.setIndicatorsForStakeholder(_stakeholder);
return _stakeholder;
}
@RequestMapping(value = "/{stakeholder}/{topic}/{category}/{subcategory}/{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 { } else {
// EXCEPTION - Stakeholder not found // EXCEPTION - Stakeholder not found
throw new EntityNotFoundException("Delete stakeholder: Stakeholder with id: "+stakeholderId+" not found");
} }
return true; 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;
}
@RequestMapping(value = "/{stakeholder}/{topic}/{category}/{subcategory}/indicators/reorder", method = RequestMethod.POST)
public List<Indicator> reorderIndicators(String stakeholder, String topic,
String category, String subcategory,
List<Indicator> indicators, String type) {
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 indicators;
}
// The following are not supposed to be used // The following are not supposed to be used
// @RequestMapping(value = "/stakeholder/dates", method = RequestMethod.GET)
// should i delete indicators that were in the list but they are not in the new list? // public List<Date> getAllStakeholderDates() {
@RequestMapping(value = "/{stakeholder}/{topic}/{category}/{subcategory}/charts/save", method = RequestMethod.POST) // List<Stakeholder> profiles = stakeholderDAO.findAll();
public List<Indicator> saveCharts(@PathVariable("stakeholder") String stakeholder, // List<Date> profileDates = new ArrayList<>();
@PathVariable("topic") String topic, //
@PathVariable("category") String category, // int i=0;
@PathVariable("subcategory") String subcategory, // for(Stakeholder profile : profiles) {
@RequestBody List<Indicator> charts) { // log.debug(profile.getCreationDate());
log.debug(charts); // profileDates.add(profile.getCreationDate());
log.debug(charts.size()); // log.debug(profileDates.get(i));
log.debug(charts.getClass().getName()); // i++;
log.debug(charts.get(0).getClass().getName()); // }
return saveIndicators(stakeholder, topic, category, subcategory, charts, "chart"); // return profileDates;
} // }
//
// should i delete indicators that were in the list but they are not in the new list? // @RequestMapping(value = "/stakeholder/dates1", method = RequestMethod.GET)
@RequestMapping(value = "/{stakeholder}/{topic}/{category}/{subcategory}/numbers/save", method = RequestMethod.POST) // public List<String> getAllStakeholderDates1() {
public List<Indicator> saveNumbers(@PathVariable("stakeholder") String stakeholder, // List<Stakeholder> profiles = stakeholderDAO.findAll();
@PathVariable("topic") String topic, // List<String> profileDates = new ArrayList<>();
@PathVariable("category") String category, //
@PathVariable("subcategory") String subcategory, // for(Stakeholder profile : profiles) {
@RequestBody List<Indicator> numbers) { // log.debug(profile.getCreationDate().toString());
return saveIndicators(stakeholder, topic, category, subcategory, numbers, "number"); // profileDates.add(profile.getCreationDate().toString());
} // }
// return profileDates;
public List<Indicator> saveIndicators(String stakeholder, String topic, // }
String category, String subcategory, //
List<Indicator> indicators, String type) { // @RequestMapping(value = "/stakeholder/dates2", method = RequestMethod.GET)
log.debug("to save indicators: "+indicators.size()); // public List<String> getAllStakeholderDates2() {
List<Indicator> indicatorsSaved = new ArrayList<>(); // List<Stakeholder> profiles = stakeholderDAO.findAll();
for(Indicator indicator : indicators) { // List<String> profileDates = new ArrayList<>();
indicatorsSaved.add(indicatorDAO.save(indicator)); // SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
} //
log.debug("saved indicators: "+indicators.size()); // for(Stakeholder profile : profiles) {
// log.debug(format.format(profile.getCreationDate()));
Stakeholder _stakeholder = stakeholderDAO.findByAlias(stakeholder); // profileDates.add(format.format(profile.getCreationDate()));
if(_stakeholder != null) { // }
Topic _topic = _stakeholder.getTopics().stream() // return profileDates;
.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;
}
} }

View File

@ -0,0 +1,206 @@
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 SubCategoryController {
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 IndicatorDAO indicatorDAO;
public SubCategory<Indicator> buildSubCategory(SubCategory<Indicator> subcategoryFull) {
SubCategory<String> subCategory = new SubCategory<>(subcategoryFull);
List<String> charts = new ArrayList<>();
List<Indicator> chartsFull = new ArrayList<>();
for(Indicator chart : subcategoryFull.getCharts()) {
Indicator chartSaved = indicatorDAO.save(chart);
chart.setId(chartSaved.getId());
chartsFull.add(chart);
charts.add(chartSaved.getId());
}
subcategoryFull.setCharts(chartsFull);
subCategory.setCharts(charts);
List<String> numbers = new ArrayList<>();
List<Indicator> numbersFull = new ArrayList<>();
for(Indicator numbr : subcategoryFull.getNumbers()) {
Indicator numberSaved = indicatorDAO.save(numbr);
numbr.setId(numberSaved.getId());
numbersFull.add(numbr);
numbers.add(numberSaved.getId());
}
subcategoryFull.setNumbers(numbersFull);
subCategory.setNumbers(numbers);
SubCategory<String> subCategorySaved = subCategoryDAO.save(subCategory);
subcategoryFull.setId(subCategorySaved.getId());
return subcategoryFull;
}
@RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/save", method = RequestMethod.POST)
public SubCategory<Indicator> saveSubCategory(@PathVariable("stakeholderId") String stakeholderId,
@PathVariable("topicId") String topicId,
@PathVariable("categoryId") String categoryId,
@RequestBody SubCategory<Indicator> subcategoryFull) {
log.debug("save subcategory");
Stakeholder<String> stakeholder = stakeholderDAO.findById(stakeholderId);
if(stakeholder != null) {
Topic<String> topic = topicDAO.findById(topicId);
if(topic != null) {
if(stakeholder.getTopics().contains(topicId)) {
Category<String> category = categoryDAO.findById(categoryId);
if(category != null) {
if(topic.getCategories().contains(categoryId)) {
SubCategory<String> subCategory = new SubCategory<>(subcategoryFull);
List<String> charts = new ArrayList<>();
for(Indicator chart : subcategoryFull.getCharts()) {
charts.add(chart.getId());
}
subCategory.setCharts(charts);
List<String> numbers = new ArrayList<>();
for(Indicator numbr : subcategoryFull.getNumbers()) {
numbers.add(numbr.getId());
}
subCategory.setNumbers(numbers);
SubCategory<String> subCategorySaved = subCategoryDAO.save(subCategory);
List<String> subcategories = category.getSubCategories();
int index = subcategories.indexOf(subCategorySaved.getId());
if(index == -1) {
subcategories.add(subCategorySaved.getId());
categoryDAO.save(category);
log.debug("Subcategory saved!");
subcategoryFull.setId(subCategorySaved.getId());
}
charts = null;
numbers = null;
subCategory = null;
} else {
// EXCEPTION - Category not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias();
throw new PathNotValidException("Save subcategory: Category with id: "+categoryId+" not found in Topic: "+topicId);
}
} else {
// EXCEPTION - Category not found
throw new EntityNotFoundException("Save subcategory: Category with id: "+categoryId+" not found");
}
} else {
// EXCEPTION - Topic not found in Stakeholder: stakeholder.getAlias();
throw new PathNotValidException("Save subcategory: Topic with id: "+topicId+" not found in Stakeholder: "+stakeholderId);
}
} else {
// EXCEPTION - Topic not found
throw new EntityNotFoundException("Save subcategory: Topic with id: "+topicId+" not found");
}
} else {
// EXCEPTION - Stakeholder not found
throw new EntityNotFoundException("Save subcategory: Stakeholder with id: "+stakeholderId+" not found");
}
return subcategoryFull;
}
@RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/delete", method = RequestMethod.DELETE)
public boolean deleteSubCategory(@PathVariable("stakeholderId") String stakeholderId,
@PathVariable("topicId") String topicId,
@PathVariable("categoryId") String categoryId,
@PathVariable("subcategoryId") String subcategoryId) {
log.debug("delete subcategory");
Stakeholder<String> stakeholder = stakeholderDAO.findById(stakeholderId);
if(stakeholder != null) {
Topic<String> topic = topicDAO.findById(topicId);
if(topic != null) {
if(stakeholder.getTopics().contains(topicId)) {
Category<String> category = categoryDAO.findById(categoryId);
if(category != null) {
if(topic.getCategories().contains(categoryId)) {
SubCategory<String> subcategory = subCategoryDAO.findById(subcategoryId);
if(subcategory != null) {
List<String> subcategories = category.getSubCategories();
int index = subcategories.indexOf(subcategoryId);
if(index != -1) {
for(String chartId : subcategory.getCharts()) {
indicatorDAO.delete(chartId);
}
subcategory.setCharts(null);
for(String numberId : subcategory.getNumbers()) {
indicatorDAO.delete(numberId);
}
subcategory.setNumbers(null);
subcategories.remove(index);
categoryDAO.save(category);
subCategoryDAO.delete(subcategoryId);
log.debug("Subcategory deleted!");
} else {
// EXCEPTION - SubCategory not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); -> Category: category.getAlias();
throw new PathNotValidException("Delete subcategory: Subcategory with id: "+subcategoryId+" not found in Category: "+categoryId);
}
} else {
// EXCEPTION - SubCategory not found
throw new EntityNotFoundException("Delete subcategory: SubCategory with id: "+subcategoryId+" not found");
}
} else {
// EXCEPTION - Category not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias();
throw new PathNotValidException("Delete subcategory: Category with id: "+categoryId+" not found in Topic: "+topicId);
}
} else {
// EXCEPTION - Category not found
throw new EntityNotFoundException("Delete subcategory: Category with id: "+categoryId+" not found");
}
} else {
// EXCEPTION - Topic not found in Stakeholder: stakeholder.getAlias();
throw new PathNotValidException("Delete subcategory: Topic with id: "+topicId+" not found in Stakeholder: "+stakeholderId);
}
} else {
// EXCEPTION - Topic not found
throw new EntityNotFoundException("Delete subcategory: Topic with id: "+topicId+" not found");
}
} else {
// EXCEPTION - Stakeholder not found
throw new EntityNotFoundException("Delete subcategory: Stakeholder with id: "+stakeholderId+" not found");
}
return true;
}
}

View File

@ -32,7 +32,7 @@ public class TestController {
} }
@RequestMapping(value = "/test-error2", method = RequestMethod.GET) @RequestMapping(value = "/test-error2", method = RequestMethod.GET)
public String getParam(@RequestParam() String param) { public String getParam(@RequestParam String param) {
return param; return param;
} }
@ -41,5 +41,4 @@ public class TestController {
String str = null; String str = null;
return str.substring(2); return str.substring(2);
} }
} }

View File

@ -0,0 +1,163 @@
package eu.dnetlib.uoamonitorservice.controllers;
import eu.dnetlib.uoamonitorservice.dao.*;
import eu.dnetlib.uoamonitorservice.entities.Category;
import eu.dnetlib.uoamonitorservice.entities.Stakeholder;
import eu.dnetlib.uoamonitorservice.entities.SubCategory;
import eu.dnetlib.uoamonitorservice.entities.Topic;
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 TopicController {
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 IndicatorDAO indicatorDAO;
@Autowired
private CategoryController categoryController;
public Topic<Category> buildTopic(Topic<Category> topicFull) {
Topic<String> topic = new Topic<>(topicFull);
List<String> categories = new ArrayList<>();
List<Category> categoriesFull = new ArrayList<>();
for(Category<SubCategory> category : topicFull.getCategories()) {
Category<SubCategory> categoryFull = categoryController.buildCategory(category);
categoriesFull.add(categoryFull);
categories.add(categoryFull.getId());
}
topicFull.setCategories(categoriesFull);
topic.setCategories(categories);
Topic<String> topicSaved = topicDAO.save(topic);
topicFull.setId(topicSaved.getId());
return topicFull;
}
@RequestMapping(value = "/{stakeholderId}/save", method = RequestMethod.POST)
public Topic<Category> saveTopic(@PathVariable("stakeholderId") String stakeholderId,
@RequestBody Topic<Category> topicFull) {
log.debug("save topic");
Stakeholder<String> stakeholder = stakeholderDAO.findById(stakeholderId);
if(stakeholder != null) {
Topic<String> topic = new Topic<>(topicFull);
List<String> categories = new ArrayList<>();
for(Category category : topicFull.getCategories()) {
categories.add(category.getId());
}
topic.setCategories(categories);
Topic<String> topicSaved = topicDAO.save(topic);
List<String> topics = stakeholder.getTopics();
int index = topics.indexOf(topicSaved.getId());
if(index == -1) {
topics.add(topicSaved.getId());
stakeholderDAO.save(stakeholder);
log.debug("Topic saved!");
topicFull.setId(topicSaved.getId());
}
categories = null;
topic = null;
} else {
// EXCEPTION - Stakeholder not found
throw new EntityNotFoundException("Save topic: Stakeholder with id: "+stakeholderId+" not found");
}
return topicFull;
}
@RequestMapping(value = "/{stakeholderId}/{topicId}/delete", method = RequestMethod.DELETE)
public boolean deleteTopic(@PathVariable("stakeholderId") String stakeholderId,
@PathVariable("topicId") String topicId) {
log.debug("delete topic");
Stakeholder<String> stakeholder = stakeholderDAO.findById(stakeholderId);
if(stakeholder != null) {
Topic<String> topic = topicDAO.findById(topicId);
if(topic != null) {
List<String> topics = stakeholder.getTopics();
int index = topics.indexOf(topicId);
if(index != -1) {
for(String categoryId : topic.getCategories()) {
Category<String> category = categoryDAO.findById(categoryId);
if(category == null) {
// EXCEPTION - Category not found
throw new EntityNotFoundException("Delete topic: 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 chartId : subcategory.getCharts()) {
indicatorDAO.delete(chartId);
}
subcategory.setCharts(null);
for (String numberId : subcategory.getNumbers()) {
indicatorDAO.delete(numberId);
}
subcategory.setNumbers(null);
subCategoryDAO.delete(subCategoryId);
}
category.setSubCategories(null);
categoryDAO.delete(categoryId);
}
topic.setCategories(null);
topics.remove(index);
stakeholderDAO.save(stakeholder);
topicDAO.delete(topicId);
log.debug("Category deleted!");
} else {
// EXCEPTION - Topic not found in Stakeholder: stakeholder.getAlias();
throw new PathNotValidException("Delete topic: Topic with id: "+topicId+" not found in Stakeholder: "+stakeholderId);
}
} else {
// EXCEPTION - Topic not found
throw new EntityNotFoundException("Delete topic: Topic with id: "+topicId+" not found");
}
} else {
// EXCEPTION - Stakeholder not found
throw new EntityNotFoundException("Delete topic: Stakeholder with id: "+stakeholderId+" not found");
}
return true;
}
}

View File

@ -0,0 +1,14 @@
package eu.dnetlib.uoamonitorservice.dao;
import eu.dnetlib.uoamonitorservice.entities.Category;
import java.util.List;
public interface CategoryDAO {
List<Category> findAll();
Category findById(String Id);
void delete(String Id);
Category save(Category category);
}

View File

@ -0,0 +1,15 @@
package eu.dnetlib.uoamonitorservice.dao;
import eu.dnetlib.uoamonitorservice.entities.Category;
import org.springframework.data.mongodb.repository.MongoRepository;
import java.util.List;
public interface MongoDBCategoryDAO extends CategoryDAO, MongoRepository<Category, String> {
List<Category> findAll();
Category findById(String Id);
void delete(String Id);
Category save(Category category);
}

View File

@ -0,0 +1,15 @@
package eu.dnetlib.uoamonitorservice.dao;
import eu.dnetlib.uoamonitorservice.entities.SubCategory;
import org.springframework.data.mongodb.repository.MongoRepository;
import java.util.List;
public interface MongoDBSubCategoryDAO extends SubCategoryDAO, MongoRepository<SubCategory, String> {
List<SubCategory> findAll();
SubCategory findById(String Id);
void delete(String Id);
SubCategory save(SubCategory subCategory);
}

View File

@ -0,0 +1,15 @@
package eu.dnetlib.uoamonitorservice.dao;
import eu.dnetlib.uoamonitorservice.entities.Topic;
import org.springframework.data.mongodb.repository.MongoRepository;
import java.util.List;
public interface MongoDBTopicDAO extends TopicDAO, MongoRepository<Topic, String> {
List<Topic> findAll();
Topic findById(String Id);
void delete(String Id);
Topic save(Topic topic);
}

View File

@ -0,0 +1,14 @@
package eu.dnetlib.uoamonitorservice.dao;
import eu.dnetlib.uoamonitorservice.entities.SubCategory;
import java.util.List;
public interface SubCategoryDAO {
List<SubCategory> findAll();
SubCategory findById(String Id);
void delete(String Id);
SubCategory save(SubCategory subCategory);
}

View File

@ -0,0 +1,14 @@
package eu.dnetlib.uoamonitorservice.dao;
import eu.dnetlib.uoamonitorservice.entities.Topic;
import java.util.List;
public interface TopicDAO {
List<Topic> findAll();
Topic findById(String Id);
void delete(String Id);
Topic save(Topic topic);
}

View File

@ -5,7 +5,7 @@ import org.springframework.data.annotation.Id;
import java.util.List; import java.util.List;
public class Category { public class Category<StringOrSubcategory> {
@Id @Id
@JsonProperty("_id") @JsonProperty("_id")
private String id; private String id;
@ -16,10 +16,11 @@ public class Category {
private boolean isPublic; private boolean isPublic;
private boolean isOverview; private boolean isOverview;
private boolean isDefault; private boolean isDefault;
private List<SubCategory> subCategories; private List<StringOrSubcategory> subCategories;
public Category() {} public Category() {}
public Category(Category category) { public Category(Category category) {
id = category.getId();
name = category.getName(); name = category.getName();
alias = category.getAlias(); alias = category.getAlias();
isActive = category.getIsActive(); isActive = category.getIsActive();
@ -28,6 +29,14 @@ public class Category {
isDefault = category.getIsDefault(); isDefault = category.getIsDefault();
} }
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() { public String getName() {
return name; return name;
} }
@ -76,11 +85,11 @@ public class Category {
this.isDefault = isDefault; this.isDefault = isDefault;
} }
public List<SubCategory> getSubCategories() { public List<StringOrSubcategory> getSubCategories() {
return subCategories; return subCategories;
} }
public void setSubCategories(List<SubCategory> subCategories) { public void setSubCategories(List<StringOrSubcategory> subCategories) {
this.subCategories = subCategories; this.subCategories = subCategories;
} }
} }

View File

@ -1,6 +1,7 @@
package eu.dnetlib.uoamonitorservice.entities; package eu.dnetlib.uoamonitorservice.entities;
import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonProperty;
import org.apache.log4j.Logger;
import org.springframework.data.annotation.Id; import org.springframework.data.annotation.Id;
import java.util.List; import java.util.List;
@ -54,8 +55,11 @@ public class Indicator {
this.description = description; this.description = description;
} }
public IndicatorType getType() { // public IndicatorType getType() {
return type; // return type;
// }
public String getType() {
return type.name();
} }
public void setType(IndicatorType type) { public void setType(IndicatorType type) {
@ -110,7 +114,7 @@ public class Indicator {
this.indicatorPaths = indicatorPaths; this.indicatorPaths = indicatorPaths;
} }
public boolean hasType(String str) { // public String hasType() {
return this.type.equals(str); // return this.type.name();
} // }
} }

View File

@ -15,7 +15,8 @@ public class IndicatorPath {
private List<String> jsonPath; private List<String> jsonPath;
private String chartObject; private String chartObject;
private Map<String, String> parameters; private Map<String, String> parameters;
private Map<String, Map<String, String>> filters; private Map<String, String> filters;
//private Map<String, Map<String, String>> filters;
public IndicatorPathType getType() { public IndicatorPathType getType() {
return type; return type;
@ -65,11 +66,11 @@ public class IndicatorPath {
this.parameters = parameters; this.parameters = parameters;
} }
public Map<String, Map<String, String>> getFilters() { public Map<String, String> getFilters() {
return filters; return filters;
} }
public void setFilters(Map<String, Map<String, String>> filters) { public void setFilters(Map<String, String> filters) {
this.filters = filters; this.filters = filters;
} }
} }

View File

@ -14,7 +14,7 @@ enum StakeholderType
} }
public class Stakeholder { public class Stakeholder<StringOrTopic> {
@Id @Id
@JsonProperty("_id") @JsonProperty("_id")
private String id; private String id;
@ -31,10 +31,11 @@ public class Stakeholder {
private Date updateDate; private Date updateDate;
private List<String> managers; private List<String> managers;
private List<Topic> topics; private List<StringOrTopic> topics;
public Stakeholder() {} public Stakeholder() {}
public Stakeholder(Stakeholder stakeholder) { public Stakeholder(Stakeholder stakeholder) {
id = stakeholder.getId();
type = stakeholder.getType(); type = stakeholder.getType();
index_id = stakeholder.getIndex_id(); index_id = stakeholder.getIndex_id();
index_name = stakeholder.getIndex_name(); index_name = stakeholder.getIndex_name();
@ -144,11 +145,11 @@ public class Stakeholder {
this.managers = managers; this.managers = managers;
} }
public List<Topic> getTopics() { public List<StringOrTopic> getTopics() {
return topics; return topics;
} }
public void setTopics(List<Topic> topics) { public void setTopics(List<StringOrTopic> topics) {
this.topics = topics; this.topics = topics;
} }
} }

View File

@ -20,6 +20,7 @@ public class SubCategory<StringOrIndicator> {
public SubCategory() {} public SubCategory() {}
public SubCategory(SubCategory subCategory) { public SubCategory(SubCategory subCategory) {
id = subCategory.getId();
name = subCategory.getName(); name = subCategory.getName();
alias = subCategory.getAlias(); alias = subCategory.getAlias();
isActive = subCategory.getIsActive(); isActive = subCategory.getIsActive();
@ -27,6 +28,14 @@ public class SubCategory<StringOrIndicator> {
isDefault = subCategory.getIsDefault(); isDefault = subCategory.getIsDefault();
} }
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() { public String getName() {
return name; return name;
} }

View File

@ -5,7 +5,7 @@ import org.springframework.data.annotation.Id;
import java.util.List; import java.util.List;
public class Topic { public class Topic<StringOrCategory> {
@Id @Id
@JsonProperty("_id") @JsonProperty("_id")
private String id; private String id;
@ -16,10 +16,11 @@ public class Topic {
private boolean isActive; private boolean isActive;
private boolean isPublic; private boolean isPublic;
private boolean isDefault; private boolean isDefault;
private List<Category> categories; private List<StringOrCategory> categories;
public Topic() {} public Topic() {}
public Topic(Topic topic) { public Topic(Topic topic) {
id = topic.getId();
name = topic.getName(); name = topic.getName();
alias = topic.getAlias(); alias = topic.getAlias();
description = topic.getDescription(); description = topic.getDescription();
@ -28,6 +29,14 @@ public class Topic {
isDefault = topic.getIsDefault(); isDefault = topic.getIsDefault();
} }
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() { public String getName() {
return name; return name;
} }
@ -76,11 +85,11 @@ public class Topic {
this.isDefault = isDefault; this.isDefault = isDefault;
} }
public List<Category> getCategories() { public List<StringOrCategory> getCategories() {
return categories; return categories;
} }
public void setCategories(List<Category> categories) { public void setCategories(List<StringOrCategory> categories) {
this.categories = categories; this.categories = categories;
} }
} }

View File

@ -19,33 +19,34 @@ public class AuthorizationHandler extends HandlerInterceptorAdapter {
helper.setUserInfoUrl(userInfoUrl); helper.setUserInfoUrl(userInfoUrl);
this.allowedPostRequests = allowedPostRequests; this.allowedPostRequests = allowedPostRequests;
} }
@Override // Comment this method ONLY FOR TEST
public boolean preHandle( // @Override
HttpServletRequest request, // public boolean preHandle(
HttpServletResponse response, // HttpServletRequest request,
Object handler) throws Exception { // HttpServletResponse response,
// log.debug("request method " + request.getRemoteHost()); // Object handler) throws Exception {
log.debug("properties: " + helper.getOriginServer() + " "+ helper.getUserInfoUrl()); //// log.debug("request method " + request.getRemoteHost());
log.debug(allowedPostRequests); // log.debug("properties: " + helper.getOriginServer() + " "+ helper.getUserInfoUrl());
log.debug(allowedPostRequests.contains(request.getServletPath())); // log.debug(allowedPostRequests);
log.debug(request.getServletPath()); // log.debug(allowedPostRequests.contains(request.getServletPath()));
if((request.getMethod().equals("POST") || request.getMethod().equals("DELETE")) && // log.debug(request.getServletPath());
!allowedPostRequests.contains(request.getServletPath())) { // if((request.getMethod().equals("POST") || request.getMethod().equals("DELETE")) &&
//TODO check domain & check user info // !allowedPostRequests.contains(request.getServletPath())) {
if(!this.helper.checkCookies(request) || !helper.isAuthorized(helper.getToken(request))){ // //TODO check domain & check user info
// if(!this.helper.checkCookies(request) || !helper.isAuthorized(helper.getToken(request))){
response.setHeader("Access-Control-Allow-Credentials","true"); //
response.setHeader("Access-Control-Allow-Origin","*"); // response.setHeader("Access-Control-Allow-Credentials","true");
response.setHeader("Vary","Origin"); // response.setHeader("Access-Control-Allow-Origin","*");
// response.setHeader("Vary","Origin");
response.setStatus(403); //
response.sendError(403, "Forbidden: You don't have permission to access. Maybe you are not registered."); // response.setStatus(403);
return false; // response.sendError(403, "Forbidden: You don't have permission to access. Maybe you are not registered.");
} // return false;
// }
} //
return true; // }
} // return true;
// }
// @Override // @Override

View File

@ -0,0 +1,11 @@
package eu.dnetlib.uoamonitorservice.handlers;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;
@ResponseStatus(HttpStatus.NOT_FOUND)
public class EntityNotFoundException extends RuntimeException {
public EntityNotFoundException(String message){
super(message);
}
}

View File

@ -19,7 +19,7 @@ public class ExceptionsHandler {
public ResponseEntity<ExceptionResponse> invalidInput(Exception ex) { public ResponseEntity<ExceptionResponse> invalidInput(Exception ex) {
ExceptionResponse response = new ExceptionResponse(); ExceptionResponse response = new ExceptionResponse();
response.setErrorCode("Validation Error"); response.setErrorCode("Validation Error");
response.setErrorMessage("Invalid inputs."); response.setErrorMessage("Invalid inputs");
response.setErrors(ex.getMessage()); response.setErrors(ex.getMessage());
response.setStatus(HttpStatus.BAD_REQUEST); response.setStatus(HttpStatus.BAD_REQUEST);
log.debug("invalidInput exception"); log.debug("invalidInput exception");
@ -47,4 +47,26 @@ public class ExceptionsHandler {
log.debug("notFoundException exception"); log.debug("notFoundException exception");
return new ResponseEntity<ExceptionResponse>(response, HttpStatus.NOT_FOUND); return new ResponseEntity<ExceptionResponse>(response, HttpStatus.NOT_FOUND);
} }
@ExceptionHandler(EntityNotFoundException.class)
public ResponseEntity<ExceptionResponse> entityNotFoundException(Exception ex) {
ExceptionResponse response = new ExceptionResponse();
response.setErrorCode("Not found Exception");
response.setErrorMessage("Entity not found Exception");
response.setErrors(ex.getMessage());
response.setStatus(HttpStatus.NOT_FOUND);
log.debug("entityNotFoundException exception");
return new ResponseEntity<ExceptionResponse>(response, HttpStatus.NOT_FOUND);
}
@ExceptionHandler(PathNotValidException.class)
public ResponseEntity<ExceptionResponse> pathNotValidException(Exception ex) {
ExceptionResponse response = new ExceptionResponse();
response.setErrorCode("Not found Exception");
response.setErrorMessage("Path not valid Exception");
response.setErrors(ex.getMessage());
response.setStatus(HttpStatus.NOT_FOUND);
log.debug("pathNotValidException exception");
return new ResponseEntity<ExceptionResponse>(response, HttpStatus.NOT_FOUND);
}
} }

View File

@ -0,0 +1,11 @@
package eu.dnetlib.uoamonitorservice.handlers;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;
@ResponseStatus(HttpStatus.NOT_FOUND)
public class PathNotValidException extends RuntimeException {
public PathNotValidException(String message){
super(message);
}
}