From 89188267666dfc96cb41f63eb930e7c36ad5a71c Mon Sep 17 00:00:00 2001 From: "k.triantafyllou" Date: Tue, 5 Mar 2024 18:51:14 +0200 Subject: [PATCH 01/11] Remove comments. Add getAliases method in stakeholderService. Remove parametersMapping --- pom.xml | 10 - scripts/2020-10-22.js | 245 +++++++++ scripts/2021-06-24.js | 65 +++ scripts/2023-05-30.js | 182 +++++++ scripts/2024-02-27.js | 16 + .../configuration/SwaggerConfig.java | 1 - .../configuration/mongo/MongoConnection.java | 1 - .../controllers/CategoryController.java | 79 --- .../controllers/IndicatorController.java | 226 -------- .../controllers/MonitorController.java | 19 - .../MonitorLibraryCheckDeployController.java | 2 - .../MonitorServiceCheckDeployController.java | 1 - .../controllers/SectionController.java | 235 -------- .../controllers/StakeholderController.java | 252 +-------- .../controllers/SubCategoryController.java | 104 ---- .../controllers/TestController.java | 44 -- .../controllers/TopicController.java | 97 ---- .../service/StakeholderService.java | 4 + src/main/resources/monitorservice.properties | 27 + update_db.js | 505 ------------------ 20 files changed, 542 insertions(+), 1573 deletions(-) create mode 100644 scripts/2020-10-22.js create mode 100644 scripts/2021-06-24.js create mode 100644 scripts/2023-05-30.js create mode 100644 scripts/2024-02-27.js delete mode 100644 src/main/java/eu/dnetlib/uoamonitorservice/controllers/TestController.java delete mode 100644 update_db.js diff --git a/pom.xml b/pom.xml index 52759cb..834ad4f 100644 --- a/pom.xml +++ b/pom.xml @@ -61,10 +61,6 @@ eu.dnetlib.uoamonitorservice.application.UoaMonitorServiceApplication true - - - - @@ -73,12 +69,6 @@ repackage - - - - - - diff --git a/scripts/2020-10-22.js b/scripts/2020-10-22.js new file mode 100644 index 0000000..2f06f0d --- /dev/null +++ b/scripts/2020-10-22.js @@ -0,0 +1,245 @@ +function upperCaseEnumValues() { + stakeholders = db.stakeholder.find().map(function (stakeholders) { + return stakeholders; + }); + for(var i=0; i"+content+"

", + "isActive" : true + } + ); + + print("div help text for divId 'footer' added for "+portalType +" (id: "+ portal._id + " - pid: " + portal.pid + " - footer divId id: "+footerDivIdID + ")"); + } +} + +use monitordb; + +addHomePageInPortalType("funder"); +addFooterDivIdForPortalType("funder"); +addFooterHelpTextForPortalType("funder"); +addHomePageInPortalType("ri"); +addFooterDivIdForPortalType("ri"); +addFooterHelpTextForPortalType("ri"); +addHomePageInPortalType("organization"); +addFooterDivIdForPortalType("organization"); +addFooterHelpTextForPortalType("organization"); diff --git a/scripts/2023-05-30.js b/scripts/2023-05-30.js new file mode 100644 index 0000000..4b5a508 --- /dev/null +++ b/scripts/2023-05-30.js @@ -0,0 +1,182 @@ +function statsProfileOfIndicatorsAsVariable() { + print("statsProfileOfIndicatorsAsVariable"); + + numOfNumbers = 0; + numOfCharts = 0; + numOfNonMonitorProfiles = 0; + numOfMonitorProfiles = 0; + numOfNoProfiles = 0; + differentProfiles = new Set(); + + // indicators = db.indicator.find({"type": "chart"}).map(function (indicator) { + // indicators = db.indicator.find({"type": "number"}).map(function (indicator) { + indicators = db.indicator.find().map(function (indicator) { + return indicator; + }); + + print(indicators.length); + + for (var i = 0; i < indicators.length; i++) { + indicator = indicators[i]; + + indicatorPaths = indicator.indicatorPaths; + if(indicatorPaths) { + for (var j = 0; j < indicatorPaths.length; j++) { + indicatorPath = indicatorPaths[j]; + chartObjectStr = ""; + // if(indicator.type == "chart") { + chartObjectStr = indicatorPath.chartObject; + // chartObject = JSON.parse(chartObjectStr); + if(indicator.type == "chart") { + numOfCharts++; + } else { + numOfNumbers++; + } + + // if(i==0) { + // if(chartObject.chartDescription != null && chartObject.chartDescription.queries != null) { + // print(chartObject.chartDescription.queries.length); + // for(var z = 0; z < chartObject.chartDescription.queries.length; z++) { + // query = chartObject.chartDescription.queries[z].query; + // print(query.profile); + // query.profile = "((__statsProfile__))"; + // } + // } + // indicatorPath.chartObject = JSON.stringify(chartObject); + + if(chartObjectStr != null) { + var included = chartObjectStr.includes('"profile":"monitor"'); + if (!included) { + numOfNonMonitorProfiles++; + print("Indicator with id: " + indicator._id + " has not monitor profile."); + } else { + numOfMonitorProfiles++; + } + + splitted = chartObjectStr.split('"profile":"'); + if (splitted.length == 1) { + numOfNoProfiles++; + } + for (var z = 1; z < splitted.length; z = z + 2) { + prof = splitted[z].split('"')[0]; + differentProfiles.add(prof); + } + + chartObjectStr = chartObjectStr.split('"profile":"monitor"').join('"profile":"((__profile__))"'); + chartObjectStr = chartObjectStr.split('"profile":"OpenAIRE All-inclusive"').join('"profile":"((__profile__))"'); + chartObjectStr = chartObjectStr.split('"profile":"OpenAIRE Monitor"').join('"profile":"((__profile__))"'); + indicatorPath.chartObject = chartObjectStr; + } else { + print("Indicator with id: " + indicator._id + " has no chartObject"); + } + } + } + + // save indicator + db.indicator.save(indicator); + } + print("\n"); + print("numOfNumbers: "+numOfNumbers); + print("numOfCharts: "+numOfCharts); + print("numOfMonitorProfiles: "+numOfMonitorProfiles); + print("numOfNonMonitorProfiles: "+numOfNonMonitorProfiles); + print("numOfNoProfiles: "+numOfNoProfiles); + print("Different profiles are: "); + for (var item of differentProfiles) { + print(item); + } +} + +function addFundingLevelInFilters(filter) { + if(filter.groupFilters && filter.groupFilters.length > 0) { + var index = filter.groupFilters.findIndex(filter => filter.field.includes('project')); + if(index !== -1) { + print('before: ' + JSON.stringify(filter)); + var prefix = filter.groupFilters[index].field.substring(0, filter.groupFilters[index].field.indexOf('project')); + if(!filter.groupFilters.find(filter => filter.field === prefix + "project.funding level 1")) { + filter.groupFilters.push({ + "field": prefix + "project.funding level 1", + "type": "contains", + "values": [ + '((__index_shortName__))' + ] + }); + print('after: ' + JSON.stringify(filter)); + } else { + print('Already added'); + } + } + return filter; + } +} + +function addFundingStreamInDefaultMSCA() { + print("addFundingStreamInDefaultMSCA") + + var stakeholder = db.stakeholder.findOne({"alias": "default-fl1"}); + if(stakeholder) { + stakeholder.topics.forEach((topic) => { + var topicObj = db.topic.findOne({"_id": ObjectId(topic)}); + topicObj.categories.forEach((category) => { + var categoryObj = db.category.findOne({"_id": ObjectId(category)}); + categoryObj.subCategories.forEach((subCategory) => { + var subCategoryObj = db.subCategory.findOne({"_id": ObjectId(subCategory)}); + subCategoryObj.numbers.forEach((number) => { + var section = db.section.findOne({"_id": ObjectId(number)}); + section.indicators.forEach((indicator) => { + var indicatorObject = db.indicator.findOne({"_id": ObjectId(indicator)}); + if(indicatorObject.indicatorPaths[0].parameters) { + indicatorObject.indicatorPaths[0].parameters['index_shortName'] = stakeholder.index_shortName.toLowerCase(); + if(indicatorObject.indicatorPaths[0] && indicatorObject.indicatorPaths[0].chartObject) { + var json = JSON.parse(indicatorObject.indicatorPaths[0].chartObject); + if(json.series && json.series.length > 0) { + json.series.forEach(query => { + if(query.query && query.query.filters && query.query.filters.length > 0) { + query.query.filters.forEach(filter => { + filter = addFundingLevelInFilters(filter); + }); + } + }); + indicatorObject.indicatorPaths[0].chartObject = JSON.stringify(json); + db.indicator.save(indicatorObject); + } + } + } + }); + }); + subCategoryObj.charts.forEach((chart) => { + var section = db.section.findOne({"_id": ObjectId(chart)}); + section.indicators.forEach((indicator) => { + var indicatorObject = db.indicator.findOne({"_id": ObjectId(indicator)}); + if(indicatorObject.indicatorPaths[0].parameters) { + indicatorObject.indicatorPaths[0].parameters['index_shortName'] = stakeholder.index_shortName.toLowerCase(); + if (indicatorObject.indicatorPaths[0] && indicatorObject.indicatorPaths[0].chartObject) { + var json = JSON.parse(indicatorObject.indicatorPaths[0].chartObject); + if (json.chartDescription && json.chartDescription.queries && json.chartDescription.queries.length > 0) { + json.chartDescription.queries.forEach(query => { + if (query.query && query.query.filters && query.query.filters.length > 0) { + query.query.filters.forEach(filter => { + filter = addFundingLevelInFilters(filter); + }); + } + }); + indicatorObject.indicatorPaths[0].chartObject = JSON.stringify(json); + db.indicator.save(indicatorObject); + } + } + } + }); + }); + }); + + }); + }); + } else { + print("Profile doesn't exist") + } +} + +use monitordb; + +statsProfileOfIndicatorsAsVariable(); +addFundingStreamInDefaultMSCA(); diff --git a/scripts/2024-02-27.js b/scripts/2024-02-27.js new file mode 100644 index 0000000..5f84810 --- /dev/null +++ b/scripts/2024-02-27.js @@ -0,0 +1,16 @@ +function deleteIndexParameters() { + var indicators = db.indicator.find(); + indicators.forEach(indicator => { + indicator.indicatorPaths.forEach(path => { + if(path.parameters) { + delete path.parameters['index_id']; + delete path.parameters['index_name']; + delete path.parameters['index_shortName']; + } + db.indicator.save(indicator); + }); + }) +} + +use irish-prod; +deleteIndexParameters(); diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/configuration/SwaggerConfig.java b/src/main/java/eu/dnetlib/uoamonitorservice/configuration/SwaggerConfig.java index 3387c9d..84d90a6 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/configuration/SwaggerConfig.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/configuration/SwaggerConfig.java @@ -41,7 +41,6 @@ public class SwaggerConfig extends WebMvcConfigurerAdapter { @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) -// .globalOperationParameters(globalParameterList()) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("eu.dnetlib.uoamonitorservice.controllers")) diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/configuration/mongo/MongoConnection.java b/src/main/java/eu/dnetlib/uoamonitorservice/configuration/mongo/MongoConnection.java index 42c31fd..17e52d1 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/configuration/mongo/MongoConnection.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/configuration/mongo/MongoConnection.java @@ -14,7 +14,6 @@ import org.springframework.data.mongodb.core.SimpleMongoDbFactory; import org.springframework.data.mongodb.repository.config.EnableMongoRepositories; import java.util.Collections; -//"uoahelptexts" @Configuration @EnableMongoRepositories(basePackages = {"eu.dnetlib.uoamonitorservice.dao", "eu.dnetlib.uoaadmintoolslibrary.dao"}) public class MongoConnection { diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/CategoryController.java b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/CategoryController.java index d2f0d23..e3f6634 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/CategoryController.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/CategoryController.java @@ -259,51 +259,10 @@ public class CategoryController { if(topic.getDefaultId() == null && children != null) { onDeleteDefaultCategory(categoryId, topicId, children); } - -// for(String subCategoryId : category.getSubCategories()) { -// SubCategory 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 chartSectionId : subcategory.getCharts()) { -// Section chartSection = sectionDAO.findById(chartSectionId); -// if (chartSection == null) { -// // EXCEPTION - Section not found -// throw new EntityNotFoundException("Delete topic: Section with id: "+chartSectionId+" not found (section exists in subcategory: "+subCategoryId+")"); -// } -// -// for (String chartId : chartSection.getIndicators()) { -// indicatorDAO.delete(chartId); -// } -// subcategory.setCharts(null); -// sectionDAO.delete(chartSectionId); -// } -// -// for(String numberSectionId : subcategory.getNumbers()) { -// Section numberSection = sectionDAO.findById(numberSectionId); -// if (numberSection == null) { -// // EXCEPTION - Section not found -// throw new EntityNotFoundException("Delete topic: Section with id: "+numberSectionId+" not found (section exists in subcategory: "+subCategoryId+")"); -// } -// -// for (String numberId : numberSection.getIndicators()) { -// indicatorDAO.delete(numberId); -// } -// subcategory.setNumbers(null); -// sectionDAO.delete(numberSectionId); -// } -// -// subCategoryDAO.delete(subCategoryId); -// } subCategoryController.deleteTree(category); - category.setSubCategories(null); - categories.remove(index); topicDAO.save(topic); - categoryDAO.delete(categoryId); log.debug("Category deleted!"); } else { @@ -406,44 +365,6 @@ public class CategoryController { return categoriesFull; } -// @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/toggle-status", method = RequestMethod.POST) -// public Boolean toggleCategoryStatus(@PathVariable("stakeholderId") String stakeholderId, -// @PathVariable("topicId") String topicId, -// @PathVariable("categoryId") String categoryId) { -// log.debug("toggle category status (isActive)"); -// log.debug("Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId); -// -// Category category = categoryDAO.findById(categoryId); -// if (category == null) { -// // EXCEPTION - Category not found -// throw new EntityNotFoundException("Toggle category status: Category with id: "+categoryId+" not found"); -// } -// category.setIsActive(!category.getIsActive()); -// -// this.toggleCategory(stakeholderId, topicId, category); -// -// return category.getIsActive(); -// } -// -// @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/toggle-access", method = RequestMethod.POST) -// public Boolean toggleCategoryAccess(@PathVariable("stakeholderId") String stakeholderId, -// @PathVariable("topicId") String topicId, -// @PathVariable("categoryId") String categoryId) { -// log.debug("toggle category access (isPublic)"); -// log.debug("Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId); -// -// Category category = categoryDAO.findById(categoryId); -// if (category == null) { -// // EXCEPTION - Category not found -// throw new EntityNotFoundException("Toggle category access: Category with id: "+categoryId+" not found"); -// } -// category.setIsPublic(!category.getIsPublic()); -// -// this.toggleCategory(stakeholderId, topicId, category); -// -// return category.getIsPublic(); -// } - @PreAuthorize("isAuthenticated()") @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/change-visibility", method = RequestMethod.POST) public Category changeCategoryVisibility(@PathVariable("stakeholderId") String stakeholderId, diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/IndicatorController.java b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/IndicatorController.java index 9c33130..f67c5c2 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/IndicatorController.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/IndicatorController.java @@ -221,16 +221,9 @@ public class IndicatorController { if(section.getDefaultId() != null && section.getDefaultId().equals(defaultSection.getId())) { Indicator indicatorNew = new Indicator(); indicatorNew.copyFromDefault(indicator, subCategory.getVisibility()); - for (IndicatorPath indicatorPath : indicatorNew.getIndicatorPaths()) { - Stakeholder stakeholder = stakeholderDAO.findByAlias(section.getStakeholderAlias()); - parameterMapping(indicatorPath, stakeholder); - } - indicatorDAO.save(indicatorNew); - List indicators = section.getIndicators(); indicators.add(indicatorNew.getId()); - sectionDAO.save(section); } } @@ -300,7 +293,6 @@ public class IndicatorController { if(indicatorPathBasedOnDefault == null) { // Add new indicator path in existing indicators IndicatorPath indicatorPathNew = new IndicatorPath(indicatorPath); - parameterMapping(indicatorPathNew, stakeholder); indicatorPaths.add(indicatorPathNew); changed = true; } else { @@ -407,10 +399,6 @@ public class IndicatorController { indicatorPathBasedOnDefault.getParameters().put(parameter.getKey(), parameter.getValue()); changed = true; } -// else if(parameter.getKey().equals("type")) { -// indicatorPathBasedOnDefault.getParameters().put(parameter.getKey(), parameter.getValue()); -// changed = true; -// } } // When deleting indicator path parameters in a default profile, delete them also from all children profiles @@ -421,8 +409,6 @@ public class IndicatorController { } } } - parameterMapping(indicatorPathBasedOnDefault, stakeholder); - //} } log.debug("After parameters check: " + changed); @@ -500,65 +486,6 @@ public class IndicatorController { } } - public void parameterMapping(IndicatorPath indicatorPath, Stakeholder stakeholder) throws UnsupportedEncodingException { - if (indicatorPath.getParameters() != null) { - if (indicatorPath.getParameters().containsKey("index_name")) { - indicatorPath.getParameters().put("index_name", stakeholder.getIndex_name()); - } else if (indicatorPath.getParameters().containsKey("index_shortName")) { - if(stakeholder.getIndex_shortName() != null) { - indicatorPath.getParameters().put("index_shortName", stakeholder.getIndex_shortName().toLowerCase()); - } else { - indicatorPath.getParameters().remove("index_shortName"); - } - } else if (indicatorPath.getParameters().containsKey("index_id")) { - indicatorPath.getParameters().put("index_id", stakeholder.getIndex_id()); - } - } - -// // url encoding for number indicators -// String url = indicatorPath.getUrl(); -// String encoded_index_id = urlEncode(URLEncoder.encode(stakeholder.getIndex_id(), "UTF-8")); -// url = url.replace("index_id", encoded_index_id); -// String encoded_index_name = urlEncode(URLEncoder.encode(stakeholder.getIndex_name(), "UTF-8")); -// url = url.replace("index_name", encoded_index_name); -// String encoded_index_shortName = urlEncode(URLEncoder.encode(stakeholder.getIndex_shortName(), "UTF-8")); -// url = url.replace("index_shortName", encoded_index_shortName); -// indicatorPath.setUrl(url); - } - - public String urlEncode(String encodedIndicatorPathField) { - String indicatorPathField = ""; - - for( int i=0; i sections = sectionDAO.findByDefaultId(defaultSectionId); -// -// for(Section section : sections) { -// List indicators = section.getIndicators(); -// -// Iterator indicatorsIterator = indicators.iterator(); -// while(indicatorsIterator.hasNext()) { -// String indicatorId = indicatorsIterator.next(); -// -// Indicator indicator = indicatorDAO.findById(indicatorId); -// if (indicator.getDefaultId().equals(defaultIndicatorId)) { -// indicatorsIterator.remove(); -// sectionDAO.save(section); -// -// indicatorDAO.delete(indicatorId); -// log.debug("Indicator deleted!"); -// -// break; -// } -// } -// } - // 2nd way List
sections = sectionDAO.findByDefaultId(defaultSectionId); List indicators = indicatorDAO.findByDefaultId(defaultIndicatorId); @@ -654,19 +558,6 @@ public class IndicatorController { } } } - -// // 3rd way - parentId -// List indicators = indicatorDAO.findByDefaultId(defaultIndicatorId); -// for(Indicator indicator : indicators) { -// Section section = sectionDAO.findById(indicator.getParent()); -// List sectionIndicators = section.getIndicators(); -// -// sectionIndicators.remove(indicator.getId()); -// sectionDAO.save(section); -// -// indicatorDAO.delete(indicator.getId()); -// log.debug("Indicator deleted!"); -// } } else if(children.equals("disconnect")) { List indicators = indicatorDAO.findByDefaultId(defaultIndicatorId); for(Indicator indicator : indicators) { @@ -678,79 +569,6 @@ public class IndicatorController { return true; } -// @RequestMapping(value = "/{stakeholderId}/charts/delete", method = RequestMethod.DELETE) -// public boolean deleteAllChartIndicators(@PathVariable("stakeholderId") String stakeholderId) { -// log.debug("delete all chart indicators of stakeholder"); -// log.debug("Stakeholder: "+stakeholderId); -// -// Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId); -// if(stakeholder != null) { -// -// for(String topicId : stakeholder.getTopics()) { -// Topic topic = topicDAO.findById(topicId); -// if(topic != null) { -// for(String categoryId : topic.getCategories()) { -// Category category = categoryDAO.findById(categoryId); -// if(category != null) { -// for(String subcategoryId : category.getSubCategories()) { -// SubCategory subcategory = subCategoryDAO.findById(subcategoryId); -// if(subcategory != null) { -// -// for(String sectionId : subcategory.getCharts()) { -// Section section = sectionDAO.findById(sectionId); -// if (section != null) { -// -// List indicators = section.getIndicators(); -// Iterator indicatorsIterator = section.getIndicators().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(); -> Section: section.getTitle(); -// throw new PathNotValidException("Delete indicator: Indicator with id: " + indicatorId + " not found in Section: " + sectionId); -// } -// } else { -// // EXCEPTION - Indicator not found -// throw new EntityNotFoundException("Delete indicator: Indicator with id: " + indicatorId + " not found"); -// } -// } -// sectionDAO.save(section); -// } else { -// // EXCEPTION - Section not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); -> Category: category.getAlias(); -> SubCategory: subcategory.getAlias(); -// throw new PathNotValidException("Delete indicator: Section with id: " + sectionId + " not found in SubCategory: " + subcategoryId); -// } -// } -// } 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; -// } - @PreAuthorize("isAuthenticated()") @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/{sectionId}/{type}/reorder", method = RequestMethod.POST) public List reorderIndicators(@PathVariable("stakeholderId") String stakeholderId, @@ -793,50 +611,6 @@ public class IndicatorController { return indicatorsFull; } -// @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/{sectionId}/{indicatorId}/toggle-status", method = RequestMethod.POST) -// public Boolean toggleIndicatorStatus(@PathVariable("stakeholderId") String stakeholderId, -// @PathVariable("topicId") String topicId, -// @PathVariable("categoryId") String categoryId, -// @PathVariable("subcategoryId") String subcategoryId, -// @PathVariable("sectionId") String sectionId, -// @PathVariable("indicatorId") String indicatorId) { -// log.debug("toggle indicator status (isActive)"); -// log.debug("Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId+ " - SubCategory: "+subcategoryId + " - Section: "+sectionId+ " - Indicator: "+indicatorId); -// -// Indicator indicator = indicatorDAO.findById(indicatorId); -// if (indicator == null) { -// // EXCEPTION - Indicator not found -// throw new EntityNotFoundException("Toggle indicator status: Indicator with id: "+indicatorId+" not found"); -// } -// indicator.setIsActive(!indicator.getIsActive()); -// -// this.toggleIndicator(stakeholderId, topicId, categoryId, subcategoryId, sectionId, indicator); -// -// return indicator.getIsActive(); -// } -// -// @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/{sectionId}/{indicatorId}/toggle-access", method = RequestMethod.POST) -// public Boolean toggleIndicatorAccess(@PathVariable("stakeholderId") String stakeholderId, -// @PathVariable("topicId") String topicId, -// @PathVariable("categoryId") String categoryId, -// @PathVariable("subcategoryId") String subcategoryId, -// @PathVariable("sectionId") String sectionId, -// @PathVariable("indicatorId") String indicatorId) { -// log.debug("toggle indicator access (isPublic)"); -// log.debug("Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId+ " - SubCategory: "+subcategoryId + " - Section: "+sectionId+ " - Indicator: "+indicatorId); -// -// Indicator indicator = indicatorDAO.findById(indicatorId); -// if (indicator == null) { -// // EXCEPTION - Indicator not found -// throw new EntityNotFoundException("Toggle indicator access: Indicator with id: "+indicatorId+" not found"); -// } -// indicator.setIsPublic(!indicator.getIsPublic()); -// -// this.toggleIndicator(stakeholderId, topicId, categoryId, subcategoryId, sectionId, indicator); -// -// return indicator.getIsPublic(); -// } - @PreAuthorize("isAuthenticated()") @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/{sectionId}/{indicatorId}/change-visibility", method = RequestMethod.POST) public Indicator changeIndicatorVisibility(@PathVariable("stakeholderId") String stakeholderId, diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/MonitorController.java b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/MonitorController.java index fa68f01..24587d1 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/MonitorController.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/MonitorController.java @@ -16,9 +16,6 @@ import java.util.*; @CrossOrigin(origins = "*") public class MonitorController { private final Logger log = LogManager.getLogger(this.getClass()); -// -// @Autowired -// private LayoutService layoutService; @Autowired private PortalService portalService; @@ -36,11 +33,6 @@ public class MonitorController { if (!old_pid.equals(new_pid)) { pageService.updatePid(old_pid, new_pid, portal.getType()); } -// String old_pid = portalResponse.getPid(); -// String new_pid = portal.getPid(); -// if(!old_pid.equals(new_pid)) { -// layoutService.updatePid(old_pid, new_pid); -// } return portalResponse; } @@ -60,16 +52,5 @@ public class MonitorController { return true; } - -// @RequestMapping(value = "/{pid}/layout", method = RequestMethod.GET) -// public Layout getLayoutForCommunity(@PathVariable(value = "pid") String pid) { -// return layoutService.findByPid(pid); -// } -// -// @RequestMapping(value = "/{pid}/layout", method = RequestMethod.POST) -// public Layout updateLayoutForCommunity(@PathVariable(value = "pid") String pid, @RequestBody Layout layout) { -// return layoutService.save(layout); -// } - } diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/MonitorLibraryCheckDeployController.java b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/MonitorLibraryCheckDeployController.java index 6b84e53..802fe33 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/MonitorLibraryCheckDeployController.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/MonitorLibraryCheckDeployController.java @@ -25,7 +25,6 @@ import java.util.Map; @RestController @CrossOrigin(origins = "*") @RequestMapping("/monitor-library") -//@ConditionalOnBean(UoaMonitorServiceApplication.class) public class MonitorLibraryCheckDeployController { private final Logger log = LogManager.getLogger(this.getClass()); @@ -63,7 +62,6 @@ public class MonitorLibraryCheckDeployController { response.put("monitorservice.mongodb.port", mongoConfig.getPort()+""); response.put("monitorservice.mongodb.username", mongoConfig.getUsername() == null ? null : "[unexposed value]"); response.put("monitorservice.mongodb.password", mongoConfig.getPassword() == null ? null : "[unexposed value]"); -// response.put("Define also", "monitorservice.mongodb.username, monitorservice.mongodb.password"); if(globalVars.date != null) { response.put("Date of deploy", globalVars.date.toString()); diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/MonitorServiceCheckDeployController.java b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/MonitorServiceCheckDeployController.java index fea6107..45aecec 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/MonitorServiceCheckDeployController.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/MonitorServiceCheckDeployController.java @@ -58,7 +58,6 @@ public class MonitorServiceCheckDeployController { response.put("monitorservice.mongodb.port", mongoConfig.getPort()+""); response.put("monitorservice.mongodb.username", mongoConfig.getUsername() == null ? null : "[unexposed value]"); response.put("monitorservice.mongodb.password", mongoConfig.getPassword() == null ? null : "[unexposed value]"); -// response.put("Define also", "monitorservice.mongodb.username, monitorservice.mongodb.password"); if(globalVars.date != null) { response.put("Date of deploy", globalVars.date.toString()); diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/SectionController.java b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/SectionController.java index a7506ee..90b47b0 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/SectionController.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/SectionController.java @@ -196,241 +196,6 @@ public class SectionController { public void onUpdateDefaultSection(Section section, Stakeholder stakeholder, Section oldSection) { log.debug("On update default section"); - - // section already exists - check if changed and update all sections based on it - - boolean changed = false; - List
sections = sectionDAO.findByDefaultId(section.getId()); - - for(Section sectionBasedOnDefault : sections) { - if(section.getTitle() != null && !section.getTitle().equals(sectionBasedOnDefault.getTitle()) - && (oldSection.getTitle() == null || oldSection.getTitle().equals(sectionBasedOnDefault.getTitle()))) { - - sectionBasedOnDefault.setTitle(section.getTitle()); - changed = true; - } - - if(!changed) { -// break; - continue; - } - -// sectionBasedOnDefault.setTitle(section.getTitle()); - sectionBasedOnDefault.setUpdateDate(section.getUpdateDate()); - sectionDAO.save(sectionBasedOnDefault); - } - } - - @PreAuthorize("isAuthenticated()") - @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/{sectionId}/delete", method = RequestMethod.DELETE) - public boolean deleteSection(@PathVariable("stakeholderId") String stakeholderId, - @PathVariable("topicId") String topicId, - @PathVariable("categoryId") String categoryId, - @PathVariable("subcategoryId") String subcategoryId, - @PathVariable("sectionId") String sectionId, - @RequestParam(required = false) String children) { - log.debug("delete section"); - log.debug("Id: "+sectionId + " - Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId+ " - SubCategory: "+subcategoryId); - - Section section = sectionDAO.findById(sectionId); - if(section != null) { - SubCategory subCategory = checkForExceptions(stakeholderId, topicId, categoryId, subcategoryId); - - Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId); - if(section.getDefaultId() != null && !rolesUtils.hasCreateAndDeleteAuthority(stakeholder.getType())) { - // EXCEPTION - Access denied - throw new ForbiddenException("Delete section: You are not authorized to delete a default Section in stakeholder with id: "+stakeholderId); - } - - String type = ""; - List sections = null; - if (section.getType().equals("chart")) { - sections = subCategory.getCharts(); - type = "chart"; - } else if (section.getType().equals("number")) { - sections = subCategory.getNumbers(); - type = "number"; - } - - int index = sections.indexOf(sectionId); - if (index != -1) { - // this section belongs in default profile - if(subCategory.getDefaultId() == null && children != null) { - onDeleteDefaultSection(sectionId, subcategoryId, children, type); - } - - indicatorController.deleteTree(section); - - sections.remove(index); - subCategoryDAO.save(subCategory); - - sectionDAO.delete(sectionId); - log.debug("Section deleted!"); - } else { - // EXCEPTION - Section not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); -> Category: category.getAlias(); -> SubCategory: subcategory.getAlias(); - throw new PathNotValidException("Delete section: Section with id: "+sectionId+" not found in SubCategory: "+subcategoryId); - } - } else { - // EXCEPTION - Section not found - throw new EntityNotFoundException("Delete section: Section with id: "+sectionId+" not found"); - } - return true; - } - - public boolean onDeleteDefaultSection(String defaultSectionId, String defaultSubCategoryId, String children, String type) { - if(children.equals("delete")) { - List subCategories = subCategoryDAO.findByDefaultId(defaultSubCategoryId); - List
sections = sectionDAO.findByDefaultId(defaultSectionId); - - for(SubCategory subCategory : subCategories) { - Iterator
sectionsIterator = sections.iterator(); - while(sectionsIterator.hasNext()) { - Section section = sectionsIterator.next(); - - String sectionId = section.getId(); - List subCategorySections = null; - if(type.equals("chart")) { - subCategorySections = subCategory.getCharts(); - } else if(type.equals("number")) { - subCategorySections = subCategory.getNumbers(); - } - if(subCategorySections != null && subCategorySections.contains(sectionId)) { - sectionsIterator.remove(); - - subCategorySections.remove(sectionId); - subCategoryDAO.save(subCategory); - - indicatorController.deleteTree(section); - - sectionDAO.delete(sectionId); - log.debug("Section with id: "+sectionId+" deleted!"); - - break; - } - } - } - } else if(children.equals("disconnect")) { - List
sections = sectionDAO.findByDefaultId(defaultSectionId); - for(Section section : sections) { - indicatorController.disConnectTree(section); - - section.setDefaultId(null); - sectionDAO.save(section); - - log.debug("DefaultId for Section with id: "+section.getId()+" empty!"); - } - } - return true; - } - - @PreAuthorize("isAuthenticated()") - @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/{type}/reorder", method = RequestMethod.POST) - public List
reorderSections(@PathVariable("stakeholderId") String stakeholderId, - @PathVariable("topicId") String topicId, - @PathVariable("categoryId") String categoryId, - @PathVariable("subcategoryId") String subcategoryId, - @PathVariable("type") String type, - @RequestBody List sections) { - log.debug("reorder sections of type: "+type); - log.debug("Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId+ " - SubCategory: "+subcategoryId); - - SubCategory subCategory = checkForExceptions(stakeholderId, topicId, categoryId, subcategoryId); - - if (type.equals("chart")) { - List oldSections = subCategory.getCharts(); - for (String sectionId : oldSections) { - if (!sections.contains(sectionId)) { - sections.add(sectionId); - } - } - subCategory.setCharts(sections); - } else if (type.equals("number")) { - List oldSections = subCategory.getNumbers(); - for (String sectionId : oldSections) { - if (!sections.contains(sectionId)) { - sections.add(sectionId); - } - } - subCategory.setNumbers(sections); - } - - List
sectionsFull = new ArrayList<>(); - for(String sectionId : sections) { - Section section = sectionDAO.findById(sectionId); - if(section == null) { - // EXCEPTION - Section not found - throw new EntityNotFoundException("Reorder sections: Section with id: " + sectionId + " not found"); - } - sectionsFull.add(section); - } - - subCategoryDAO.save(subCategory); - log.debug("Sections reordered!"); - - return sectionsFull; - } - -// @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/{sectionId}/toggle-status", method = RequestMethod.POST) -// public Boolean toggleSectionStatus(@PathVariable("stakeholderId") String stakeholderId, -// @PathVariable("topicId") String topicId, -// @PathVariable("categoryId") String categoryId, -// @PathVariable("subcategoryId") String subcategoryId, -// @PathVariable("sectionId") String sectionId) { -// log.debug("toggle section status (isActive)"); -// log.debug("Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId+ " - SubCategory: "+subcategoryId+ " - Section: "+sectionId); -// -// Section section = sectionDAO.findById(sectionId); -// if (section == null) { -// // EXCEPTION - Section not found -// throw new EntityNotFoundException("Toggle section status: Section with id: "+sectionId+" not found"); -// } -// section.setIsActive(!section.getIsActive()); -// -// this.toggleSection(stakeholderId, topicId, categoryId, subcategoryId, section); -// -// return section.getIsActive(); -// } - -// @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/{sectionId}/toggle-access", method = RequestMethod.POST) -// public Boolean toggleSectionAccess(@PathVariable("stakeholderId") String stakeholderId, -// @PathVariable("topicId") String topicId, -// @PathVariable("categoryId") String categoryId, -// @PathVariable("subcategoryId") String subcategoryId, -// @PathVariable("sectionId") String sectionId) { -// log.debug("toggle section access (isPublic)"); -// log.debug("Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId+ " - SubCategory: "+subcategoryId); -// -// Section section = sectionDAO.findById(sectionId); -// if (section == null) { -// // EXCEPTION - Section not found -// throw new EntityNotFoundException("Toggle section access: Section with id: "+sectionId+" not found"); -// } -// section.setIsPublic(!section.getIsPublic()); -// -// this.toggleSection(stakeholderId, topicId, categoryId, subcategoryId); -// -// return section.getIsPublic(); -// } - - - public void toggleSection(String stakeholderId, String topicId, String categoryId, String subcategoryId, Section section) { - SubCategory subCategory = checkForExceptions(stakeholderId, topicId, categoryId, subcategoryId); - - List sections = null; - if (section.getType().equals("chart")) { - sections = subCategory.getCharts(); - } else if (section.getType().equals("number")) { - sections = subCategory.getNumbers(); - } - - if(sections.contains(section.getId())) { - sectionDAO.save(section); - log.debug("Section toggled!"); - } else { - // EXCEPTION - Section not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); -> Category: category.getAlias(); -> SubCategory: subCategory.getAlias(); - throw new PathNotValidException("Toggle section: Section with id: "+section.getId()+" not found in SubCategory: "+subcategoryId); - } - } private SubCategory checkForExceptions(String stakeholderId, String topicId, String categoryId, String subcategoryId) { diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/StakeholderController.java b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/StakeholderController.java index f6293a4..8e2a33f 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/StakeholderController.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/StakeholderController.java @@ -56,19 +56,10 @@ public class StakeholderController { @PreAuthorize("isAuthenticated()") @RequestMapping(value = "/stakeholder/alias", method = RequestMethod.GET) public List getAllReservedStakeholderAlias() { -// log.debug("get all stakeholder reserved alias-es"); - List stakeholderAlias = new ArrayList<>(); - - List stakeholders = stakeholderDAO.findAll(); - if(stakeholders != null) { - stakeholders.forEach(stakeholder -> { - stakeholderAlias.add(stakeholder.getAlias()); - }); - } - stakeholderAlias.add( "all"); + List stakeholderAlias = this.stakeholderService.getAllAliases(); + stakeholderAlias.add("all"); stakeholderAlias.add("default"); stakeholderAlias.add("alias"); - return stakeholderAlias; } @@ -120,22 +111,11 @@ public class StakeholderController { boolean addAll = false; boolean addPublicAndRestricted = false; -// if(roles == null -// || roles.contains(authorizationService.PORTAL_ADMIN) -// || roles.contains(authorizationService.curator(stakeholder.getType())) -// || roles.contains(authorizationService.manager(stakeholder.getType(), stakeholder.getAlias()))) { if(rolesUtils.hasUpdateAuthority(stakeholder.getType(), stakeholder.getAlias())) { - //if(visibility == null || visibility == (Visibility.PRIVATE)) { addAll = true; - //} - //if(visibility == null || visibility == (Visibility.PRIVATE) || visibility == (Visibility.RESTRICTED)) { addPublicAndRestricted = true; - //} -// } else if(roles != null && roles.contains(authorizationService.member(stakeholder.getType(), stakeholder.getAlias()))) { } else if(rolesUtils.isMember(stakeholder.getType(), stakeholder.getAlias())) { - //if(visibility == null || visibility == (Visibility.PRIVATE) || visibility == (Visibility.RESTRICTED)) { addPublicAndRestricted = true; - //} } Stakeholder stakeholderFull = new Stakeholder<>(stakeholder); @@ -201,62 +181,18 @@ public class StakeholderController { sectionsNumbers.add(getSectionFull(sectionId, subCategoryId, addAll, addPublicAndRestricted)); } subCategoryFull.setNumbers(sectionsNumbers); - -// List charts = new ArrayList<>(); -// for(String indicatorId : subCategory.getCharts()) { -// Indicator indicator = indicatorDAO.findById(indicatorId); -// if(indicator == null) { -// // EXCEPTION - Indicator not found -// throw new EntityNotFoundException("Get stakeholder: Indicator with id: "+indicatorId+" not found (indicator exists in subCategory: "+subCategoryId+")"); -// } -// charts.add(indicator); -// } -// subCategoryFull.setCharts(charts); -// -// List numbers = new ArrayList<>(); -// for (String indicatorId : subCategory.getNumbers()) { -// Indicator indicator = indicatorDAO.findById(indicatorId); -// if (indicator == null) { -// // EXCEPTION - Indicator not found -// throw new EntityNotFoundException("Get stakeholder: Indicator with id: " + indicatorId + " not found (indicator exists in subCategory: " + subCategoryId + ")"); -// } -// numbers.add(indicator); -// } -// subCategoryFull.setNumbers(numbers); - subCategories.add(subCategoryFull); } - categoryFull.setSubCategories(subCategories); categories.add(categoryFull); } - topicFull.setCategories(categories); topics.add(topicFull); } - stakeholderFull.setTopics(topics); return stakeholderFull; } -// private SubCategory setFullSubcategory(SubCategory subCategory) { -// SubCategory subCategoryFull = new SubCategory>(subCategory); -// -// List
sectionsCharts = new ArrayList<>(); -// -// for(String sectionId : subCategory.getCharts()) { -// sectionsCharts.add(getSectionFull(sectionId, subCategoryId, addAll, addPublicAndRestricted)); -// } -// subCategoryFull.setCharts(sectionsCharts); -// -// List
sectionsNumbers = new ArrayList<>(); -// -// for(String sectionId : subCategory.getNumbers()) { -// sectionsNumbers.add(getSectionFull(sectionId, subCategoryId, addAll, addPublicAndRestricted)); -// } -// subCategoryFull.setNumbers(sectionsNumbers); -// } - private Section getSectionFull(String sectionId, String subCategoryId, boolean addAll, boolean addPublicAndRestricted) { Section section = sectionDAO.findById(sectionId); if (section == null) { @@ -323,10 +259,6 @@ public class StakeholderController { // Remove stakeholders for which i do not have authority if(stakeholders != null && stakeholders.size() > 0) { -// log.debug("ROLES: "); -// roles.forEach(role -> log.debug(role)); -// -// if (roles.contains(authorizationService.PORTAL_ADMIN)) { if (rolesUtils.isPortalAdmin()) { for(Stakeholder stakeholder : stakeholders) { stakeholdersFull.add(this.setFullEntities(stakeholder)); @@ -337,8 +269,6 @@ public class StakeholderController { Iterator stakeholderIterator = stakeholders.iterator(); while(stakeholderIterator.hasNext()) { Stakeholder stakeholder = stakeholderIterator.next(); - -// if(roles.contains(authorizationService.curator(stakeholder.getType()))) { if(rolesUtils.isCurator(stakeholder.getType())) { stakeholdersFull.add(this.setFullEntities(stakeholder)); continue; @@ -359,13 +289,11 @@ public class StakeholderController { @PreAuthorize("isAuthenticated()") @RequestMapping(value = "/my-stakeholder", method = RequestMethod.GET) public List getMyRealStakeholders(@RequestParam(required = false) String type) { -// log.debug("get my NOT default stakeholders" + (type != null ? " with type: "+type : "")); return stakeholderService.getStakeholdersByTypeAndRole(type, null, true); } @RequestMapping(value = "/stakeholder/{alias:.+}", method = RequestMethod.GET) public Stakeholder getStakeholder(@PathVariable("alias") String alias) { -// log.debug("get stakeholder: "+alias); Stakeholder stakeholder = stakeholderDAO.findByAlias(alias); if(stakeholder == null) { @@ -373,7 +301,6 @@ public class StakeholderController { throw new EntityNotFoundException("Get stakeholder: Stakeholder with alias: "+alias+" not found"); } -// List roles = authorizationService.getRoles(); if(stakeholder.getDefaultId() == null && !rolesUtils.isLoggedIn()) { // EXCEPTION - Unauthorized throw new AccessDeniedException("Get stakeholder: You are not authorized (not logged in) to access stakeholder with alias: "+alias); @@ -386,7 +313,6 @@ public class StakeholderController { if((stakeholder.getVisibility() == Visibility.PRIVATE && !rolesUtils.hasUpdateAuthority(stakeholder.getType(), stakeholder.getAlias()) || (stakeholder.getVisibility() == Visibility.RESTRICTED && !rolesUtils.hasUpdateAuthority(stakeholder.getType(), stakeholder.getAlias()) && !rolesUtils.isMember(stakeholder.getType(), stakeholder.getAlias())))) { // // EXCEPTION - Access denied -// throw new ForbiddenException("Get stakeholder: You are not authorized to get stakeholder with alias: "+alias); List topicsEmpty = stakeholder.getTopics(); topicsEmpty.clear(); stakeholder.setTopics(topicsEmpty); @@ -397,7 +323,6 @@ public class StakeholderController { return this.setFullEntities(stakeholder); } -// @PreAuthorize("isAuthenticated()") @PreAuthorize("hasAnyAuthority(" + "@AuthorizationService.PORTAL_ADMIN, " + "@AuthorizationService.curator(#_stakeholder.getType()), " @@ -407,26 +332,13 @@ public class StakeholderController { public Stakeholder saveStakeholder(@RequestBody Stakeholder _stakeholder) { log.debug("save stakeholder"); log.debug("Alias: "+_stakeholder.getAlias() + " - Id: "+_stakeholder.getId()); - -// if(_stakeholder == null) { -// log.debug("stakeholder null"); -// // EXCEPTION - Parameter for Stakeholder is not accepted -// } - Stakeholder stakeholder = new Stakeholder<>(_stakeholder); Date date = new Date(); stakeholder.setUpdateDate(date); - List topics = new ArrayList<>(); - - // stakeholder does not exist in DB if(_stakeholder.getId() == null) { stakeholder.setCreationDate(date); - -// for(Topic topic : _stakeholder.getTopics()) { -// topics.add(topic.getId()); -// } } else { Stakeholder oldStakeholder = stakeholderDAO.findById(_stakeholder.getId()); if(oldStakeholder == null) { @@ -441,21 +353,12 @@ public class StakeholderController { } topics.add(topic.getId()); } -// stakeholder.setTopics(topics); -// _stakeholder = this.setFullEntities(stakeholder, rolesUtils.getRoles()); } - stakeholder.setTopics(topics); - Stakeholder stakeholderSaved = stakeholderDAO.save(stakeholder); _stakeholder.setId(stakeholderSaved.getId()); _stakeholder.setCreationDate(stakeholderSaved.getCreationDate()); _stakeholder.setUpdateDate(stakeholderSaved.getUpdateDate()); - - topics = null; - stakeholder = null; - stakeholderSaved = null; - return _stakeholder; } @@ -470,78 +373,14 @@ public class StakeholderController { if(stakeholder != null) { pid = stakeholder.getAlias(); - -// if(!roles.contains(authorizationService.PORTAL_ADMIN) -// && !roles.contains(authorizationService.curator(stakeholder.getType()))) { if(!rolesUtils.hasCreateAndDeleteAuthority(stakeholder.getType())) { // EXCEPTION - Access denied throw new ForbiddenException("Delete stakeholder: You are not authorized to delete stakeholder with id: "+stakeholderId); } - -// for(String topicId : stakeholder.getTopics()) { -// Topic 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 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 subcategory = subCategoryDAO.findById(subCategoryId); -// if (subcategory == null) { -// // EXCEPTION - SubCategory not found -// throw new EntityNotFoundException("Delete stakeholder: SubCategory with id: "+subCategoryId+" not found (subcategory exists in category: "+categoryId+")"); -// } -// -// for(String chartSectionId : subcategory.getCharts()) { -// Section chartSection = sectionDAO.findById(chartSectionId); -// if (chartSection == null) { -// // EXCEPTION - Section not found -// throw new EntityNotFoundException("Delete topic: Section with id: "+chartSectionId+" not found (section exists in subcategory: "+subCategoryId+")"); -// } -// -// for (String chartId : chartSection.getIndicators()) { -// indicatorDAO.delete(chartId); -// } -// subcategory.setCharts(null); -// sectionDAO.delete(chartSectionId); -// } -// -// for(String numberSectionId : subcategory.getNumbers()) { -// Section numberSection = sectionDAO.findById(numberSectionId); -// if (numberSection == null) { -// // EXCEPTION - Section not found -// throw new EntityNotFoundException("Delete topic: Section with id: "+numberSectionId+" not found (section exists in subcategory: "+subCategoryId+")"); -// } -// -// for (String numberId : numberSection.getIndicators()) { -// indicatorDAO.delete(numberId); -// } -// subcategory.setNumbers(null); -// sectionDAO.delete(numberSectionId); -// } -// -// subCategoryDAO.delete(subCategoryId); -// } -// category.setSubCategories(null); -// categoryDAO.delete(categoryId); -// } -// topic.setCategories(null); -// topicDAO.delete(topicId); -// } - topicController.deleteTree(stakeholder); - stakeholder.setTopics(null); stakeholderDAO.delete(stakeholderId); log.debug("Stakeholder deleted!"); - Portal portal = portalService.getPortal(pid); if(portal != null) { portalService.deletePortal(portal.getId()); @@ -553,44 +392,6 @@ public class StakeholderController { return true; } - -// @RequestMapping(value = "/{stakeholderId}/toggle-status", method = RequestMethod.POST) -// public Boolean toggleStakeholderStatus(@PathVariable("stakeholderId") String stakeholderId) { -// log.debug("toggle stakeholder status (isActive)"); -// log.debug("Stakeholder: "+stakeholderId); -// -// Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId); -// if (stakeholder == null) { -// // EXCEPTION - Stakeholder not found -// throw new EntityNotFoundException("Toggle stakeholder status: Stakeholder with id: "+stakeholderId+" not found"); -// } -// stakeholder.setIsActive(!stakeholder.getIsActive()); -// -// stakeholderDAO.save(stakeholder); -// log.debug("Stakeholder toggled!"); -// -// return stakeholder.getIsActive(); -// } -// -// @RequestMapping(value = "/{stakeholderId}/toggle-access", method = RequestMethod.POST) -// public Boolean toggleStakeholderAccess(@PathVariable("stakeholderId") String stakeholderId) { -// log.debug("toggle stakeholder access (isPublic)"); -// log.debug("Stakeholder: "+stakeholderId); -// -// Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId); -// if (stakeholder == null) { -// // EXCEPTION - Stakeholder not found -// throw new EntityNotFoundException("Toggle stakeholder access: Stakeholder with id: "+stakeholderId+" not found"); -// } -// stakeholder.setIsPublic(!stakeholder.getIsPublic()); -// -// stakeholderDAO.save(stakeholder); -// log.debug("Stakeholder toggled!"); -// -// return stakeholder.getIsPublic(); -// } - - @PreAuthorize("isAuthenticated()") @RequestMapping(value = "/{stakeholderId}/change-visibility", method = RequestMethod.POST) public Stakeholder changeStakeholderVisibility(@PathVariable("stakeholderId") String stakeholderId, @@ -603,12 +404,6 @@ public class StakeholderController { // EXCEPTION - Stakeholder not found throw new EntityNotFoundException("Change stakeholder visibility: Stakeholder with id: "+stakeholderId+" not found"); } - -// List roles = authorizationService.getRoles(); - -// if(!roles.contains(authorizationService.PORTAL_ADMIN) -// && !roles.contains(authorizationService.curator(stakeholder.getType())) -// && !roles.contains(authorizationService.manager(stakeholder.getType(), stakeholder.getAlias()))) { if(!rolesUtils.hasUpdateAuthority(stakeholder.getType(), stakeholder.getAlias())) { // EXCEPTION - Access denied throw new ForbiddenException("Change stakeholder visibility: You are not authorized to update stakeholder with id: "+stakeholderId); @@ -621,7 +416,7 @@ public class StakeholderController { Stakeholder stakeholderFull = new Stakeholder<>(stakeholder); List topicsFull = new ArrayList<>(); - if(propagate != null && propagate) { + if (propagate != null && propagate) { for (String topicId : stakeholder.getTopics()) { topicsFull.add(topicController.changeVisibilityTree(topicId, visibility, propagate)); } @@ -636,45 +431,4 @@ public class StakeholderController { return stakeholder; } - - // The following are not supposed to be used -// @RequestMapping(value = "/stakeholder/dates", method = RequestMethod.GET) -// public List getAllStakeholderDates() { -// List profiles = stakeholderDAO.findAll(); -// List 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 getAllStakeholderDates1() { -// List profiles = stakeholderDAO.findAll(); -// List 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 getAllStakeholderDates2() { -// List profiles = stakeholderDAO.findAll(); -// List 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; -// } } diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/SubCategoryController.java b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/SubCategoryController.java index 06c005f..aaa499b 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/SubCategoryController.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/SubCategoryController.java @@ -142,20 +142,6 @@ public class SubCategoryController { numberSections.add(numberSection.getId()); } } - -// List charts = new ArrayList<>(); -// for(Indicator chart : subcategoryFull.getCharts()) { -// charts.add(chart.getId()); -// } -// subCategory.setCharts(charts); -// -// List numbers = new ArrayList<>(); -// for(Indicator numbr : subcategoryFull.getNumbers()) { -// numbers.add(numbr.getId()); -// } -// subCategory.setNumbers(numbers); - - subCategory.setCharts(chartSections); subCategory.setNumbers(numberSections); @@ -181,11 +167,6 @@ public class SubCategoryController { subcategoryFull.setId(subCategory.getId()); } - - chartSections = null; - numberSections = null; - subCategory = null; - return subcategoryFull; } @@ -227,12 +208,8 @@ public class SubCategoryController { } if(!changed) { -// break; continue; } - -// subCategoryBasedOnDefault.setName(subCategory.getName()); -// subCategoryBasedOnDefault.setDescription(subCategory.getDescription()); subCategoryBasedOnDefault.setUpdateDate(subCategory.getUpdateDate()); subCategoryDAO.save(subCategoryBasedOnDefault); } @@ -266,35 +243,6 @@ public class SubCategoryController { if(category.getDefaultId() == null && children != null) { onDeleteDefaultSubCategory(subcategoryId, categoryId, children); } - -// for(String chartSectionId : subcategory.getCharts()) { -// Section chartSection = sectionDAO.findById(chartSectionId); -// if (chartSection == null) { -// // EXCEPTION - Section not found -// throw new EntityNotFoundException("Delete SubCategory: Section with id: "+chartSectionId+" not found (section exists in subcategory: "+subcategoryId+")"); -// } -// -// for (String chartId : chartSection.getIndicators()) { -// indicatorDAO.delete(chartId); -// } -// subcategory.setCharts(null); -// sectionDAO.delete(chartSectionId); -// } -// -// for(String numberSectionId : subcategory.getNumbers()) { -// Section numberSection = sectionDAO.findById(numberSectionId); -// if (numberSection == null) { -// // EXCEPTION - Section not found -// throw new EntityNotFoundException("Delete SubCategory: Section with id: "+numberSectionId+" not found (section exists in subcategory: "+subcategoryId+")"); -// } -// -// for (String numberId : numberSection.getIndicators()) { -// indicatorDAO.delete(numberId); -// } -// subcategory.setNumbers(null); -// sectionDAO.delete(numberSectionId); -// } - sectionController.deleteTree(subcategory); subcategory.setCharts(null); @@ -393,46 +341,6 @@ public class SubCategoryController { return subCategoriesFull; } -// @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/toggle-status", method = RequestMethod.POST) -// public Boolean toggleSubCategoryStatus(@PathVariable("stakeholderId") String stakeholderId, -// @PathVariable("topicId") String topicId, -// @PathVariable("categoryId") String categoryId, -// @PathVariable("subcategoryId") String subcategoryId) { -// log.debug("toggle subCategory status (isActive)"); -// log.debug("Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId+ " - SubCategory: "+subcategoryId); -// -// SubCategory subCategory = subCategoryDAO.findById(subcategoryId); -// if (subCategory == null) { -// // EXCEPTION - SubCategory not found -// throw new EntityNotFoundException("Toggle subCategory status: SubCategory with id: "+subcategoryId+" not found"); -// } -// subCategory.setIsActive(!subCategory.getIsActive()); -// -// this.toggleSubCategory(stakeholderId, topicId, categoryId, subCategory); -// -// return subCategory.getIsActive(); -// } -// -// @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/toggle-access", method = RequestMethod.POST) -// public Boolean toggleSubCategoryAccess(@PathVariable("stakeholderId") String stakeholderId, -// @PathVariable("topicId") String topicId, -// @PathVariable("categoryId") String categoryId, -// @PathVariable("subcategoryId") String subcategoryId) { -// log.debug("toggle subCategory access (isPublic)"); -// log.debug("Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId+ " - SubCategory: "+subcategoryId); -// -// SubCategory subCategory = subCategoryDAO.findById(subcategoryId); -// if (subCategory == null) { -// // EXCEPTION - SubCategory not found -// throw new EntityNotFoundException("Toggle subCategory access: SubCategory with id: "+subcategoryId+" not found"); -// } -// subCategory.setIsPublic(!subCategory.getIsPublic()); -// -// this.toggleSubCategory(stakeholderId, topicId, categoryId, subCategory); -// -// return subCategory.getIsPublic(); -// } - @PreAuthorize("isAuthenticated()") @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/change-visibility", method = RequestMethod.POST) public SubCategory changeSubCategoryVisibility(@PathVariable("stakeholderId") String stakeholderId, @@ -522,18 +430,6 @@ public class SubCategoryController { // EXCEPTION - Category not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); throw new PathNotValidException("Save indicator: Category with id: "+categoryId+" not found in Topic: "+topicId); } - -// SubCategory subcategory = subCategoryDAO.findById(subcategoryId); -// if(subcategory == null) { -// // EXCEPTION - SubCategory not found -// throw new EntityNotFoundException("Save indicator: SubCategory with id: "+subcategoryId+" not found"); -// } -// -// if (!category.getSubCategories().contains(subcategoryId)) { -// // EXCEPTION - SubCategory not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); -> Category: category.getAlias(); -// throw new PathNotValidException("Save indicator: SubCategory with id: "+subcategoryId+" not found in Category: "+categoryId); -// } - return category; } diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/TestController.java b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/TestController.java deleted file mode 100644 index 6c9fc80..0000000 --- a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/TestController.java +++ /dev/null @@ -1,44 +0,0 @@ -package eu.dnetlib.uoamonitorservice.controllers; - -import eu.dnetlib.uoamonitorservice.dao.StakeholderDAO; -import eu.dnetlib.uoamonitorservice.entities.Stakeholder; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.web.bind.annotation.*; - -import java.util.List; - -@RestController -@CrossOrigin(origins = "*") -public class TestController { - private final Logger log = LogManager.getLogger(this.getClass()); - - @Autowired - private StakeholderDAO stakeholderDAO; - -// @RequestMapping("/") -// public String index() { -// return "Greetings from Spring Boot!"; -// } - - // Check ExceptionHandler - @RequestMapping(value = "/test-error1", method = RequestMethod.GET) - public Stakeholder getFirstStakeholder() { - List stakeholders; - stakeholders = stakeholderDAO.findAll(); - - return stakeholders.get(0); - } - - @RequestMapping(value = "/test-error2", method = RequestMethod.GET) - public String getParam(@RequestParam String param) { - return param; - } - - @RequestMapping(value = "/test-error3", method = RequestMethod.GET) - public String getSubstringOfNull() { - String str = null; - return str.substring(2); - } -} diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/TopicController.java b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/TopicController.java index ab2e4b8..de3c423 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/TopicController.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/TopicController.java @@ -132,9 +132,6 @@ public class TopicController { topicFull.setId(topic.getId()); } - - categories = null; - topic = null; } else { // EXCEPTION - Stakeholder not found throw new EntityNotFoundException("Save topic: Stakeholder with id: "+stakeholderId+" not found"); @@ -186,12 +183,8 @@ public class TopicController { } if(!changed) { -// break; continue; } - -// topicBasedOnDefault.setName(topic.getName()); -// topicBasedOnDefault.setDescription(topic.getDescription()); topicBasedOnDefault.setUpdateDate(topic.getUpdateDate()); topicDAO.save(topicBasedOnDefault); } @@ -229,54 +222,6 @@ public class TopicController { if(stakeholder.getDefaultId() == null && children != null) { onDeleteDefaultTopic(topicId, stakeholderId, children); } - -// for(String categoryId : topic.getCategories()) { -// Category 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 subcategory = subCategoryDAO.findById(subCategoryId); -// if (subcategory == null) { -// // EXCEPTION - SubCategory not found -// throw new EntityNotFoundException("Delete topic: SubCategory with id: "+subCategoryId+" not found (subcategory exists in category: "+categoryId+")"); -// } -// -// for(String chartSectionId : subcategory.getCharts()) { -// Section chartSection = sectionDAO.findById(chartSectionId); -// if (chartSection == null) { -// // EXCEPTION - Section not found -// throw new EntityNotFoundException("Delete topic: Section with id: "+chartSectionId+" not found (section exists in subcategory: "+subCategoryId+")"); -// } -// -// for (String chartId : chartSection.getIndicators()) { -// indicatorDAO.delete(chartId); -// } -// subcategory.setCharts(null); -// sectionDAO.delete(chartSectionId); -// } -// -// for(String numberSectionId : subcategory.getNumbers()) { -// Section numberSection = sectionDAO.findById(numberSectionId); -// if (numberSection == null) { -// // EXCEPTION - Section not found -// throw new EntityNotFoundException("Delete topic: Section with id: "+numberSectionId+" not found (section exists in subcategory: "+subCategoryId+")"); -// } -// -// for (String numberId : numberSection.getIndicators()) { -// indicatorDAO.delete(numberId); -// } -// subcategory.setNumbers(null); -// sectionDAO.delete(numberSectionId); -// } -// -// subCategoryDAO.delete(subCategoryId); -// } -// category.setSubCategories(null); -// categoryDAO.delete(categoryId); -// } categoryController.deleteTree(topic); topic.setCategories(null); @@ -387,42 +332,6 @@ public class TopicController { } } -// @RequestMapping(value = "/{stakeholderId}/{topicId}/toggle-status", method = RequestMethod.POST) -// public Boolean toggleTopicStatus(@PathVariable("stakeholderId") String stakeholderId, -// @PathVariable("topicId") String topicId) { -// log.debug("toggle topic status (isActive)"); -// log.debug("Stakeholder: "+stakeholderId + " - Topic: "+topicId); -// -// Topic topic = topicDAO.findById(topicId); -// if (topic == null) { -// // EXCEPTION - Topic not found -// throw new EntityNotFoundException("Toggle topic status: Topic with id: "+topicId+" not found"); -// } -// topic.setIsActive(!topic.getIsActive()); -// -// this.toggleTopic(stakeholderId, topic); -// -// return topic.getIsActive(); -// } -// -// @RequestMapping(value = "/{stakeholderId}/{topicId}/toggle-access", method = RequestMethod.POST) -// public Boolean toggleTopicAccess(@PathVariable("stakeholderId") String stakeholderId, -// @PathVariable("topicId") String topicId) { -// log.debug("toggle topic access (isPublic)"); -// log.debug("Stakeholder: "+stakeholderId + " - Topic: "+topicId); -// -// Topic topic = topicDAO.findById(topicId); -// if (topic == null) { -// // EXCEPTION - Topic not found -// throw new EntityNotFoundException("Toggle topic access: Topic with id: "+topicId+" not found"); -// } -// topic.setIsPublic(!topic.getIsPublic()); -// -// this.toggleTopic(stakeholderId, topic); -// -// return topic.getIsPublic(); -// } - @PreAuthorize("isAuthenticated()") @RequestMapping(value = "/{stakeholderId}/{topicId}/change-visibility", method = RequestMethod.POST) public Topic changeTopicVisibility(@PathVariable("stakeholderId") String stakeholderId, @@ -449,10 +358,6 @@ public class TopicController { // EXCEPTION - Stakeholder not found throw new EntityNotFoundException("Toggle topic: Stakeholder with id: "+stakeholderId+" not found"); } - - -// this.toggleTopic(stakeholderId, topic); - } public Topic changeVisibilityTree(String topicId, Visibility visibility, Boolean propagate) { @@ -489,9 +394,7 @@ public class TopicController { // EXCEPTION - Topic not found throw new EntityNotFoundException("Topic delete tree: Topic with id: "+topicId+" not found (topic exists in stakeholder: "+stakeholder.getId()+")"); } - categoryController.deleteTree(topic); - topicDAO.delete(topicId); } } diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/service/StakeholderService.java b/src/main/java/eu/dnetlib/uoamonitorservice/service/StakeholderService.java index accce6c..61b4ba4 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/service/StakeholderService.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/service/StakeholderService.java @@ -19,6 +19,10 @@ public class StakeholderService { @Autowired RolesUtils rolesUtils; + public List getAllAliases() { + return this.dao.findAll().stream().map(Stakeholder::getAlias).collect(Collectors.toList()); + } + public List getStakeholdersByTypeAndRole(String type, String defaultId, boolean manage) { List stakeholders; if(type != null && defaultId != null) { diff --git a/src/main/resources/monitorservice.properties b/src/main/resources/monitorservice.properties index 8ccd226..82627cb 100644 --- a/src/main/resources/monitorservice.properties +++ b/src/main/resources/monitorservice.properties @@ -26,3 +26,30 @@ monitorservice.globalVars.version=@version@ #monitorservice.userInfoUrl = https://services.openaire.eu/uoa-user-management/api/users/getUserInfo?accessToken= #monitorservice.originServer = .openaire.eu +# Monitor +monitorservice.mongodb.host=localhost +monitorservice.mongodb.port=27017 +monitorservice.mongodb.database=production + +# Notification +notification.mongodb.host = localhost +notification.mongodb.port = 27017 +notification.mongodb.database = openaire_notification + +# Admin Tools Library +admintoolslibrary.mail.from = openaire.test@gmail.com +admintoolslibrary.mail.username = openaire.test@gmail.com +admintoolslibrary.mail.password = koujreltgjduicjp +admintoolslibrary.mail.host = smtp.gmail.com +admintoolslibrary.mail.port = 587 +admintoolslibrary.mail.auth = true +admintoolslibrary.mail.sslProtocols = TLSv1.2 +admintoolslibrary.mail.defaultEncoding=UTF-8 +admintoolslibrary.mail.protocol=smtp +admintoolslibrary.mail.testConnection=false +admintoolslibrary.google.secret = 6LcVtFIUAAAAAIlEaz6Am2PBC3j5lHG7vBo6uW4_ + +# Authorization +authorization.security.userInfoUrl = http://mpagasas.di.uoa.gr:19080/login-service/userInfo + +server.port=8888 \ No newline at end of file diff --git a/update_db.js b/update_db.js deleted file mode 100644 index 0c896e4..0000000 --- a/update_db.js +++ /dev/null @@ -1,505 +0,0 @@ -//version compatibility: 1.0.0-SNAPSHOT - -//use openaire_monitor; - -function upperCaseEnumValues() { - stakeholders = db.stakeholder.find().map(function (stakeholders) { - return stakeholders; - }); - for(var i=0; i"+content+"

", - "isActive" : true - } - ); - - print("div help text for divId 'footer' added for "+portalType +" (id: "+ portal._id + " - pid: " + portal.pid + " - footer divId id: "+footerDivIdID + ")"); - } -} - -function statsProfileOfIndicatorsAsVariable() { - print("statsProfileOfIndicatorsAsVariable"); - - numOfNumbers = 0; - numOfCharts = 0; - numOfNonMonitorProfiles = 0; - numOfMonitorProfiles = 0; - numOfNoProfiles = 0; - differentProfiles = new Set(); - - // indicators = db.indicator.find({"type": "chart"}).map(function (indicator) { - // indicators = db.indicator.find({"type": "number"}).map(function (indicator) { - indicators = db.indicator.find().map(function (indicator) { - return indicator; - }); - - print(indicators.length); - - for (var i = 0; i < indicators.length; i++) { - indicator = indicators[i]; - - indicatorPaths = indicator.indicatorPaths; - if(indicatorPaths) { - for (var j = 0; j < indicatorPaths.length; j++) { - indicatorPath = indicatorPaths[j]; - chartObjectStr = ""; - // if(indicator.type == "chart") { - chartObjectStr = indicatorPath.chartObject; - // chartObject = JSON.parse(chartObjectStr); - if(indicator.type == "chart") { - numOfCharts++; - } else { - numOfNumbers++; - } - - // if(i==0) { - // if(chartObject.chartDescription != null && chartObject.chartDescription.queries != null) { - // print(chartObject.chartDescription.queries.length); - // for(var z = 0; z < chartObject.chartDescription.queries.length; z++) { - // query = chartObject.chartDescription.queries[z].query; - // print(query.profile); - // query.profile = "((__statsProfile__))"; - // } - // } - // indicatorPath.chartObject = JSON.stringify(chartObject); - - if(chartObjectStr != null) { - var included = chartObjectStr.includes('"profile":"monitor"'); - if (!included) { - numOfNonMonitorProfiles++; - print("Indicator with id: " + indicator._id + " has not monitor profile."); - } else { - numOfMonitorProfiles++; - } - - splitted = chartObjectStr.split('"profile":"'); - if (splitted.length == 1) { - numOfNoProfiles++; - } - for (var z = 1; z < splitted.length; z = z + 2) { - prof = splitted[z].split('"')[0]; - differentProfiles.add(prof); - } - - chartObjectStr = chartObjectStr.split('"profile":"monitor"').join('"profile":"((__profile__))"'); - chartObjectStr = chartObjectStr.split('"profile":"OpenAIRE All-inclusive"').join('"profile":"((__profile__))"'); - chartObjectStr = chartObjectStr.split('"profile":"OpenAIRE Monitor"').join('"profile":"((__profile__))"'); - indicatorPath.chartObject = chartObjectStr; - } else { - print("Indicator with id: " + indicator._id + " has no chartObject"); - } - } - } - - // save indicator - db.indicator.save(indicator); - } - print("\n"); - print("numOfNumbers: "+numOfNumbers); - print("numOfCharts: "+numOfCharts); - print("numOfMonitorProfiles: "+numOfMonitorProfiles); - print("numOfNonMonitorProfiles: "+numOfNonMonitorProfiles); - print("numOfNoProfiles: "+numOfNoProfiles); - print("Different profiles are: "); - for (var item of differentProfiles) { - print(item); - } -} - -function addFundingLevelInFilters(filter) { - if(filter.groupFilters && filter.groupFilters.length > 0) { - var index = filter.groupFilters.findIndex(filter => filter.field.includes('project')); - if(index !== -1) { - print('before: ' + JSON.stringify(filter)); - var prefix = filter.groupFilters[index].field.substring(0, filter.groupFilters[index].field.indexOf('project')); - if(!filter.groupFilters.find(filter => filter.field === prefix + "project.funding level 1")) { - filter.groupFilters.push({ - "field": prefix + "project.funding level 1", - "type": "contains", - "values": [ - '((__index_shortName__))' - ] - }); - print('after: ' + JSON.stringify(filter)); - } else { - print('Already added'); - } - } - return filter; - } -} - -function addFundingStreamInDefaultMSCA() { - print("addFundingStreamInDefaultMSCA") - - var stakeholder = db.stakeholder.findOne({"alias": "default-fl1"}); - if(stakeholder) { - stakeholder.topics.forEach((topic) => { - var topicObj = db.topic.findOne({"_id": ObjectId(topic)}); - topicObj.categories.forEach((category) => { - var categoryObj = db.category.findOne({"_id": ObjectId(category)}); - categoryObj.subCategories.forEach((subCategory) => { - var subCategoryObj = db.subCategory.findOne({"_id": ObjectId(subCategory)}); - subCategoryObj.numbers.forEach((number) => { - var section = db.section.findOne({"_id": ObjectId(number)}); - section.indicators.forEach((indicator) => { - var indicatorObject = db.indicator.findOne({"_id": ObjectId(indicator)}); - if(indicatorObject.indicatorPaths[0].parameters) { - indicatorObject.indicatorPaths[0].parameters['index_shortName'] = stakeholder.index_shortName.toLowerCase(); - if(indicatorObject.indicatorPaths[0] && indicatorObject.indicatorPaths[0].chartObject) { - var json = JSON.parse(indicatorObject.indicatorPaths[0].chartObject); - if(json.series && json.series.length > 0) { - json.series.forEach(query => { - if(query.query && query.query.filters && query.query.filters.length > 0) { - query.query.filters.forEach(filter => { - filter = addFundingLevelInFilters(filter); - }); - } - }); - indicatorObject.indicatorPaths[0].chartObject = JSON.stringify(json); - db.indicator.save(indicatorObject); - } - } - } - }); - }); - subCategoryObj.charts.forEach((chart) => { - var section = db.section.findOne({"_id": ObjectId(chart)}); - section.indicators.forEach((indicator) => { - var indicatorObject = db.indicator.findOne({"_id": ObjectId(indicator)}); - if(indicatorObject.indicatorPaths[0].parameters) { - indicatorObject.indicatorPaths[0].parameters['index_shortName'] = stakeholder.index_shortName.toLowerCase(); - if (indicatorObject.indicatorPaths[0] && indicatorObject.indicatorPaths[0].chartObject) { - var json = JSON.parse(indicatorObject.indicatorPaths[0].chartObject); - if (json.chartDescription && json.chartDescription.queries && json.chartDescription.queries.length > 0) { - json.chartDescription.queries.forEach(query => { - if (query.query && query.query.filters && query.query.filters.length > 0) { - query.query.filters.forEach(filter => { - filter = addFundingLevelInFilters(filter); - }); - } - }); - indicatorObject.indicatorPaths[0].chartObject = JSON.stringify(json); - db.indicator.save(indicatorObject); - } - } - } - }); - }); - }); - - }); - }); - } else { - print("Profile doesn't exist") - } - -} - - -use monitordb; -// use 1_openaire-mongodb-beta; // dev db - -// 29-09-2020 - 22-10-2020 -//upperCaseEnumValues(); -// addHeightInIndicators(); -// addVisibility(); -// removeIsActiveAndIsPublic(); -// -// addAdminToolsCollections(); -// // addPortals(); -// -// uniqueIndexes(); - -// 04-06-2021 - 24-06-2021 -// addHomePageInPortalType("funder"); -// addFooterDivIdForPortalType("funder"); -// addFooterHelpTextForPortalType("funder"); -// addHomePageInPortalType("ri"); -// addFooterDivIdForPortalType("ri"); -// addFooterHelpTextForPortalType("ri"); -// addHomePageInPortalType("organization"); -// addFooterDivIdForPortalType("organization"); -// addFooterHelpTextForPortalType("organization"); - -// 11-04-2023 -statsProfileOfIndicatorsAsVariable(); -// 30-05-2023 -addFundingStreamInDefaultMSCA(); From 76c39d6eb3472a0086057efda05a4da23eeda726 Mon Sep 17 00:00:00 2001 From: "k.triantafyllou" Date: Wed, 6 Mar 2024 01:27:38 +0200 Subject: [PATCH 02/11] Add monitor deploy service --- .../controllers/EmailController.java | 17 +++--- .../controllers/MonitorController.java | 6 +- .../MonitorLibraryCheckDeployController.java | 53 ++--------------- .../MonitorServiceCheckDeployController.java | 41 ++----------- .../handlers/utils/UserInfo.java | 43 -------------- .../service/MonitorDeployService.java | 59 +++++++++++++++++++ 6 files changed, 80 insertions(+), 139 deletions(-) delete mode 100644 src/main/java/eu/dnetlib/uoamonitorservice/handlers/utils/UserInfo.java create mode 100644 src/main/java/eu/dnetlib/uoamonitorservice/service/MonitorDeployService.java diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/EmailController.java b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/EmailController.java index 24dc8c2..d8380ac 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/EmailController.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/EmailController.java @@ -17,21 +17,20 @@ import java.util.*; public class EmailController { private final Logger log = LogManager.getLogger(this.getClass()); - @Autowired - private EmailSender emailSender; + private final EmailSender emailSender; + private final VerifyRecaptcha verifyRecaptcha; @Autowired - private VerifyRecaptcha verifyRecaptcha; + public EmailController(EmailSender emailSender, VerifyRecaptcha verifyRecaptcha) { + this.emailSender = emailSender; + this.verifyRecaptcha = verifyRecaptcha; + } @RequestMapping(value = "/contact", method = RequestMethod.POST) public Boolean contact(@RequestBody EmailRecaptcha form) throws InvalidReCaptchaException { verifyRecaptcha.processResponse(form.getRecaptcha()); Email email = form.getEmail(); - ArrayList sendTo = new ArrayList<>(); - for(String userMail: email.getRecipients()){ - sendTo.add(userMail); - } - return emailSender.send(sendTo, email.getSubject(), email.getBody(), false); + return emailSender.send(email.getRecipients(), email.getSubject(), email.getBody(), false); } @RequestMapping(value = "/sendMail", method = RequestMethod.POST) @@ -40,7 +39,7 @@ public class EmailController { String successString = "success"; String failureString = "failure"; Map> mailResults = new HashMap<>(); - boolean bcc = (optional.isPresent())?optional.get():true; + boolean bcc = optional.orElse(true); for(String userMail:email.getRecipients()){ ArrayList sendTo = new ArrayList<>(); sendTo.add(userMail); diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/MonitorController.java b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/MonitorController.java index 24587d1..1c959e3 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/MonitorController.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/MonitorController.java @@ -45,11 +45,7 @@ public class MonitorController { @RequestMapping(value = "/delete", method = RequestMethod.POST) public Boolean deletePortals(@RequestBody List portals) { - for (String id: portals) { - String pid = portalService.deletePortal(id); -// layoutService.deleteByPid(pid); - } - + portals.forEach(id -> portalService.deletePortal(id)); return true; } } diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/MonitorLibraryCheckDeployController.java b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/MonitorLibraryCheckDeployController.java index 802fe33..3fd7065 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/MonitorLibraryCheckDeployController.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/MonitorLibraryCheckDeployController.java @@ -1,25 +1,14 @@ package eu.dnetlib.uoamonitorservice.controllers; -import com.mongodb.BasicDBObject; -import com.mongodb.CommandResult; -import com.mongodb.DBObject; -import eu.dnetlib.uoamonitorservice.application.UoaMonitorServiceApplication; -import eu.dnetlib.uoamonitorservice.configuration.GlobalVars; -import eu.dnetlib.uoamonitorservice.configuration.mongo.MongoConnection; -import eu.dnetlib.uoamonitorservice.configuration.properties.MongoConfig; +import eu.dnetlib.uoamonitorservice.service.MonitorDeployService; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; -import org.springframework.context.annotation.Conditional; -import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.web.bind.annotation.CrossOrigin; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; -import java.util.HashMap; import java.util.Map; @RestController @@ -28,14 +17,11 @@ import java.util.Map; public class MonitorLibraryCheckDeployController { private final Logger log = LogManager.getLogger(this.getClass()); - @Autowired - private MongoConnection mongoConnection; + private final MonitorDeployService service; - @Autowired - private MongoConfig mongoConfig; - - @Autowired - private GlobalVars globalVars; + public MonitorLibraryCheckDeployController(MonitorDeployService service) { + this.service = service; + } @RequestMapping(value = {"", "/health_check"}, method = RequestMethod.GET) public String hello() { @@ -46,33 +32,6 @@ public class MonitorLibraryCheckDeployController { @PreAuthorize("hasAnyAuthority(@AuthorizationService.PORTAL_ADMIN)") @RequestMapping(value = "/health_check/advanced", method = RequestMethod.GET) public Map checkEverything() { - Map response = new HashMap<>(); - - MongoTemplate mt = mongoConnection.getMongoTemplate(); - DBObject ping = new BasicDBObject("ping", "1"); - try { - CommandResult answer = mt.getDb().command(ping); - response.put("Mongo try: error", answer.getErrorMessage()); - } catch (Exception e) { - response.put("Mongo catch: error", e.getMessage()); - } - - response.put("monitorservice.mongodb.database", mongoConfig.getDatabase()); - response.put("monitorservice.mongodb.host", mongoConfig.getHost()); - response.put("monitorservice.mongodb.port", mongoConfig.getPort()+""); - response.put("monitorservice.mongodb.username", mongoConfig.getUsername() == null ? null : "[unexposed value]"); - response.put("monitorservice.mongodb.password", mongoConfig.getPassword() == null ? null : "[unexposed value]"); - - if(globalVars.date != null) { - response.put("Date of deploy", globalVars.date.toString()); - } - if(globalVars.getBuildDate() != null) { - response.put("Date of build", globalVars.getBuildDate()); - } - if(globalVars.getVersion() != null) { - response.put("Version", globalVars.getVersion()); - } - - return response; + return this.service.checkEverything(); } } diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/MonitorServiceCheckDeployController.java b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/MonitorServiceCheckDeployController.java index 45aecec..ae7fee7 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/MonitorServiceCheckDeployController.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/MonitorServiceCheckDeployController.java @@ -6,6 +6,7 @@ import com.mongodb.DBObject; import eu.dnetlib.uoamonitorservice.configuration.GlobalVars; import eu.dnetlib.uoamonitorservice.configuration.mongo.MongoConnection; import eu.dnetlib.uoamonitorservice.configuration.properties.MongoConfig; +import eu.dnetlib.uoamonitorservice.service.MonitorDeployService; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; @@ -24,14 +25,11 @@ import java.util.Map; public class MonitorServiceCheckDeployController { private final Logger log = LogManager.getLogger(this.getClass()); - @Autowired - private MongoConnection mongoConnection; + private final MonitorDeployService service; - @Autowired - private MongoConfig mongoConfig; - - @Autowired - private GlobalVars globalVars; + public MonitorServiceCheckDeployController(MonitorDeployService service) { + this.service = service; + } @RequestMapping(value = {"", "/health_check"}, method = RequestMethod.GET) public String hello() { @@ -42,33 +40,6 @@ public class MonitorServiceCheckDeployController { @PreAuthorize("hasAnyAuthority(@AuthorizationService.PORTAL_ADMIN)") @RequestMapping(value = "/health_check/advanced", method = RequestMethod.GET) public Map checkEverything() { - Map response = new HashMap<>(); - - MongoTemplate mt = mongoConnection.getMongoTemplate(); - DBObject ping = new BasicDBObject("ping", "1"); - try { - CommandResult answer = mt.getDb().command(ping); - response.put("Mongo try: error", answer.getErrorMessage()); - } catch (Exception e) { - response.put("Mongo catch: error", e.getMessage()); - } - - response.put("monitorservice.mongodb.database", mongoConfig.getDatabase()); - response.put("monitorservice.mongodb.host", mongoConfig.getHost()); - response.put("monitorservice.mongodb.port", mongoConfig.getPort()+""); - response.put("monitorservice.mongodb.username", mongoConfig.getUsername() == null ? null : "[unexposed value]"); - response.put("monitorservice.mongodb.password", mongoConfig.getPassword() == null ? null : "[unexposed value]"); - - if(globalVars.date != null) { - response.put("Date of deploy", globalVars.date.toString()); - } - if(globalVars.getBuildDate() != null) { - response.put("Date of build", globalVars.getBuildDate()); - } - if(globalVars.getVersion() != null) { - response.put("Version", globalVars.getVersion()); - } - - return response; + return this.service.checkEverything(); } } diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/handlers/utils/UserInfo.java b/src/main/java/eu/dnetlib/uoamonitorservice/handlers/utils/UserInfo.java deleted file mode 100644 index 446b49e..0000000 --- a/src/main/java/eu/dnetlib/uoamonitorservice/handlers/utils/UserInfo.java +++ /dev/null @@ -1,43 +0,0 @@ -package eu.dnetlib.uoamonitorservice.handlers.utils; - -import java.util.ArrayList; -import java.util.List; - -public class UserInfo { - String name; - String email; - List edu_person_entitlements = new ArrayList(); - - @Override - public String toString() { - return "UserInfo{" + - "name='" + name + '\'' + - ", email='" + email + '\'' + - ", edu_person_entitlements=" + edu_person_entitlements + - '}'; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public String getEmail() { - return email; - } - - public void setEmail(String email) { - this.email = email; - } - - public List getEdu_person_entitlements() { - return edu_person_entitlements; - } - - public void setEdu_person_entitlements(List edu_person_entitlements) { - this.edu_person_entitlements = edu_person_entitlements; - } -} diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/service/MonitorDeployService.java b/src/main/java/eu/dnetlib/uoamonitorservice/service/MonitorDeployService.java new file mode 100644 index 0000000..c25daee --- /dev/null +++ b/src/main/java/eu/dnetlib/uoamonitorservice/service/MonitorDeployService.java @@ -0,0 +1,59 @@ +package eu.dnetlib.uoamonitorservice.service; + +import com.mongodb.BasicDBObject; +import com.mongodb.CommandResult; +import com.mongodb.DBObject; +import eu.dnetlib.uoamonitorservice.configuration.GlobalVars; +import eu.dnetlib.uoamonitorservice.configuration.mongo.MongoConnection; +import eu.dnetlib.uoamonitorservice.configuration.properties.MongoConfig; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.mongodb.core.MongoTemplate; +import org.springframework.stereotype.Service; + +import java.util.HashMap; +import java.util.Map; + +@Service +public class MonitorDeployService { + private final MongoConnection mongoConnection; + private final MongoConfig mongoConfig; + private final GlobalVars globalVars; + + @Autowired + public MonitorDeployService(MongoConnection mongoConnection, MongoConfig mongoConfig, GlobalVars globalVars) { + this.mongoConnection = mongoConnection; + this.mongoConfig = mongoConfig; + this.globalVars = globalVars; + } + + public Map checkEverything() { + Map response = new HashMap<>(); + + MongoTemplate mt = mongoConnection.getMongoTemplate(); + DBObject ping = new BasicDBObject("ping", "1"); + try { + CommandResult answer = mt.getDb().command(ping); + response.put("Mongo try: error", answer.getErrorMessage()); + } catch (Exception e) { + response.put("Mongo catch: error", e.getMessage()); + } + + response.put("monitorservice.mongodb.database", mongoConfig.getDatabase()); + response.put("monitorservice.mongodb.host", mongoConfig.getHost()); + response.put("monitorservice.mongodb.port", mongoConfig.getPort() + ""); + response.put("monitorservice.mongodb.username", mongoConfig.getUsername() == null ? null : "[unexposed value]"); + response.put("monitorservice.mongodb.password", mongoConfig.getPassword() == null ? null : "[unexposed value]"); + + if (globalVars.date != null) { + response.put("Date of deploy", globalVars.date.toString()); + } + if (globalVars.getBuildDate() != null) { + response.put("Date of build", globalVars.getBuildDate()); + } + if (globalVars.getVersion() != null) { + response.put("Version", globalVars.getVersion()); + } + + return response; + } +} From 1158b676e41b641c16b894475acac718002d26e7 Mon Sep 17 00:00:00 2001 From: "k.triantafyllou" Date: Wed, 6 Mar 2024 01:48:31 +0200 Subject: [PATCH 03/11] Revert properties. Remove some comments. Remove mongodb DAOs. Move primitives classes and enums to new package primitives. --- .../controllers/CategoryController.java | 170 ++++++----- .../controllers/EmailController.java | 17 +- .../controllers/IndicatorController.java | 263 ++++++++---------- .../uoamonitorservice/dao/CategoryDAO.java | 5 +- .../uoamonitorservice/dao/IndicatorDAO.java | 5 +- .../dao/MongoDBCategoryDAO.java | 19 -- .../dao/MongoDBIndicatorDAO.java | 18 -- .../dao/MongoDBSectionDAO.java | 17 -- .../dao/MongoDBStakeholderDAO.java | 26 -- .../dao/MongoDBSubCategoryDAO.java | 17 -- .../dao/MongoDBTopicDAO.java | 19 -- .../uoamonitorservice/dao/SectionDAO.java | 5 +- .../uoamonitorservice/dao/StakeholderDAO.java | 5 +- .../uoamonitorservice/dao/SubCategoryDAO.java | 5 +- .../uoamonitorservice/dao/TopicDAO.java | 5 +- .../uoamonitorservice/entities/Indicator.java | 15 +- .../uoamonitorservice/entities/Section.java | 12 +- .../entities/Stakeholder.java | 30 +- .../uoamonitorservice/primitives/Format.java | 5 + .../IndicatorPath.java | 16 +- .../primitives/IndicatorPathType.java | 7 + .../primitives/IndicatorSize.java | 7 + .../primitives/IndicatorType.java | 7 + .../uoamonitorservice/primitives/Locale.java | 11 + .../ReorderEvent.java | 2 +- .../primitives/StakeholderType.java | 9 + src/main/resources/monitorservice.properties | 30 +- 27 files changed, 293 insertions(+), 454 deletions(-) delete mode 100644 src/main/java/eu/dnetlib/uoamonitorservice/dao/MongoDBCategoryDAO.java delete mode 100644 src/main/java/eu/dnetlib/uoamonitorservice/dao/MongoDBIndicatorDAO.java delete mode 100644 src/main/java/eu/dnetlib/uoamonitorservice/dao/MongoDBSectionDAO.java delete mode 100644 src/main/java/eu/dnetlib/uoamonitorservice/dao/MongoDBStakeholderDAO.java delete mode 100644 src/main/java/eu/dnetlib/uoamonitorservice/dao/MongoDBSubCategoryDAO.java delete mode 100644 src/main/java/eu/dnetlib/uoamonitorservice/dao/MongoDBTopicDAO.java create mode 100644 src/main/java/eu/dnetlib/uoamonitorservice/primitives/Format.java rename src/main/java/eu/dnetlib/uoamonitorservice/{entities => primitives}/IndicatorPath.java (88%) create mode 100644 src/main/java/eu/dnetlib/uoamonitorservice/primitives/IndicatorPathType.java create mode 100644 src/main/java/eu/dnetlib/uoamonitorservice/primitives/IndicatorSize.java create mode 100644 src/main/java/eu/dnetlib/uoamonitorservice/primitives/IndicatorType.java create mode 100644 src/main/java/eu/dnetlib/uoamonitorservice/primitives/Locale.java rename src/main/java/eu/dnetlib/uoamonitorservice/{entities => primitives}/ReorderEvent.java (94%) create mode 100644 src/main/java/eu/dnetlib/uoamonitorservice/primitives/StakeholderType.java diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/CategoryController.java b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/CategoryController.java index e3f6634..ea5110e 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/CategoryController.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/CategoryController.java @@ -1,10 +1,13 @@ package eu.dnetlib.uoamonitorservice.controllers; +import eu.dnetlib.uoaadmintoolslibrary.handlers.ForbiddenException; import eu.dnetlib.uoaadmintoolslibrary.handlers.utils.RolesUtils; -import eu.dnetlib.uoamonitorservice.dao.*; +import eu.dnetlib.uoamonitorservice.dao.CategoryDAO; +import eu.dnetlib.uoamonitorservice.dao.StakeholderDAO; +import eu.dnetlib.uoamonitorservice.dao.SubCategoryDAO; +import eu.dnetlib.uoamonitorservice.dao.TopicDAO; import eu.dnetlib.uoamonitorservice.entities.*; import eu.dnetlib.uoamonitorservice.handlers.EntityNotFoundException; -import eu.dnetlib.uoaadmintoolslibrary.handlers.ForbiddenException; import eu.dnetlib.uoamonitorservice.handlers.PathNotValidException; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -45,7 +48,7 @@ public class CategoryController { List subCategories = new ArrayList<>(); List subCategoriesFull = new ArrayList<>(); - for(SubCategory> subCategory : categoryFull.getSubCategories()) { + for (SubCategory> subCategory : categoryFull.getSubCategories()) { SubCategory> subcategoryFull = subCategoryController.buildSubCategory(subCategory); subCategoriesFull.add(subcategoryFull); subCategories.add(subcategoryFull.getId()); @@ -72,28 +75,28 @@ public class CategoryController { @PathVariable("topicId") String topicId, @RequestBody Category categoryFull) { log.debug("save category"); - log.debug("Alias: "+categoryFull.getAlias() + " - Id: "+categoryFull.getId() + " - Stakeholder: "+stakeholderId + " - Topic: "+topicId); + log.debug("Alias: " + categoryFull.getAlias() + " - Id: " + categoryFull.getId() + " - Stakeholder: " + stakeholderId + " - Topic: " + topicId); Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId); - if(stakeholder != null) { - if(!rolesUtils.hasUpdateAuthority(stakeholder.getType(), stakeholder.getAlias())) { + if (stakeholder != null) { + if (!rolesUtils.hasUpdateAuthority(stakeholder.getType(), stakeholder.getAlias())) { // EXCEPTION - Access denied - throw new ForbiddenException("Save Category: You are not authorized to update stakeholder with id: "+stakeholderId); + throw new ForbiddenException("Save Category: You are not authorized to update stakeholder with id: " + stakeholderId); } Category oldCategory = null; - if(categoryFull.getId() != null) { + if (categoryFull.getId() != null) { oldCategory = categoryDAO.findById(categoryFull.getId()); - if(oldCategory == null) { + if (oldCategory == null) { // EXCEPTION - Category not found throw new EntityNotFoundException("save category: Category with id: " + categoryFull.getId() + " not found"); } } Topic topic = topicDAO.findById(topicId); - if(topic != null) { - if(stakeholder.getTopics().contains(topicId)) { + if (topic != null) { + if (stakeholder.getTopics().contains(topicId)) { Category category = new Category<>(categoryFull); Date date = new Date(); @@ -103,7 +106,7 @@ public class CategoryController { List subCategories = new ArrayList<>(); // if category not exists (no id), create a new default subcategory, identical to category - if(categoryFull.getId() == null) { + if (categoryFull.getId() == null) { category.setCreationDate(date); categoryFull.setCreationDate(date); @@ -115,15 +118,15 @@ public class CategoryController { List subCategoriesFull = categoryFull.getSubCategories(); subCategoriesFull.add(subCategory); - for(SubCategory oldSubCategory : subCategoriesFull) { + for (SubCategory oldSubCategory : subCategoriesFull) { subCategories.add(oldSubCategory.getId()); } } else { - for(String subCategoryId : oldCategory.getSubCategories()) { + for (String subCategoryId : oldCategory.getSubCategories()) { SubCategory subCategory = subCategoryDAO.findById(subCategoryId); if (subCategory == null) { // EXCEPTION - SubCategory not found - throw new EntityNotFoundException("Save category: SubCategory with id: "+subCategoryId+" not found (subcategory exists in category: "+category.getId()+")"); + throw new EntityNotFoundException("Save category: SubCategory with id: " + subCategoryId + " not found (subcategory exists in category: " + category.getId() + ")"); } subCategories.add(subCategory.getId()); } @@ -131,8 +134,8 @@ public class CategoryController { category.setSubCategories(subCategories); - if(stakeholder.getDefaultId() == null) { - if(categoryFull.getId() == null) { + if (stakeholder.getDefaultId() == null) { + if (categoryFull.getId() == null) { categoryDAO.save(category); onSaveDefaultCategory(category, topicId); } else { @@ -145,27 +148,24 @@ public class CategoryController { List categories = topic.getCategories(); int index = categories.indexOf(category.getId()); - if(index == -1) { + if (index == -1) { categories.add(category.getId()); topicDAO.save(topic); log.debug("Category saved!"); categoryFull.setId(category.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); + 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"); + 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"); + throw new EntityNotFoundException("Save category: Stakeholder with id: " + stakeholderId + " not found"); } return categoryFull; } @@ -174,7 +174,7 @@ public class CategoryController { log.debug("On save default category"); List topics = topicDAO.findByDefaultId(topicId); - for(Topic topic : topics) { + for (Topic topic : topics) { Category categoryNew = new Category(); categoryNew.copyFromDefault(category); @@ -195,28 +195,24 @@ public class CategoryController { List categories = categoryDAO.findByDefaultId(category.getId()); boolean changed = false; - for(Category categoryBasedOnDefault : categories) { - if(category.getName() != null && !category.getName().equals(categoryBasedOnDefault.getName()) + for (Category categoryBasedOnDefault : categories) { + if (category.getName() != null && !category.getName().equals(categoryBasedOnDefault.getName()) && (oldCategory.getName() == null || oldCategory.getName().equals(categoryBasedOnDefault.getName()))) { categoryBasedOnDefault.setName(category.getName()); categoryBasedOnDefault.setAlias(category.getAlias()); changed = true; } - if(category.getDescription() != null && !category.getDescription().equals(categoryBasedOnDefault.getDescription()) + if (category.getDescription() != null && !category.getDescription().equals(categoryBasedOnDefault.getDescription()) && (oldCategory.getDescription() == null || oldCategory.getDescription().equals(categoryBasedOnDefault.getDescription()))) { categoryBasedOnDefault.setDescription(category.getDescription()); changed = true; } - if(!changed) { -// break; + if (!changed) { continue; } - -// categoryBasedOnDefault.setName(category.getName()); -// categoryBasedOnDefault.setDescription(category.getDescription()); categoryBasedOnDefault.setUpdateDate(category.getUpdateDate()); categoryDAO.save(categoryBasedOnDefault); } @@ -229,34 +225,34 @@ public class CategoryController { @PathVariable("categoryId") String categoryId, @RequestParam(required = false) String children) { log.debug("delete category"); - log.debug("Id: "+categoryId + " - Stakeholder: "+stakeholderId + " - Topic: "+topicId); + log.debug("Id: " + categoryId + " - Stakeholder: " + stakeholderId + " - Topic: " + topicId); Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId); - if(stakeholder != null) { - if(!rolesUtils.hasUpdateAuthority(stakeholder.getType(), stakeholder.getAlias())) { + if (stakeholder != null) { + if (!rolesUtils.hasUpdateAuthority(stakeholder.getType(), stakeholder.getAlias())) { // EXCEPTION - Access denied - throw new ForbiddenException("Delete category: You are not authorized to update stakeholder with id: "+stakeholderId); + throw new ForbiddenException("Delete category: You are not authorized to update stakeholder with id: " + stakeholderId); } Topic topic = topicDAO.findById(topicId); - if(topic != null) { - if(stakeholder.getTopics().contains(topicId)) { + if (topic != null) { + if (stakeholder.getTopics().contains(topicId)) { Category category = categoryDAO.findById(categoryId); - if(category != null) { + if (category != null) { - if(category.getDefaultId() != null && !rolesUtils.hasCreateAndDeleteAuthority(stakeholder.getType())) { + if (category.getDefaultId() != null && !rolesUtils.hasCreateAndDeleteAuthority(stakeholder.getType())) { // EXCEPTION - Access denied - throw new ForbiddenException("Delete category: You are not authorized to delete a default Category in stakeholder with id: "+stakeholderId); + throw new ForbiddenException("Delete category: You are not authorized to delete a default Category in stakeholder with id: " + stakeholderId); } List categories = topic.getCategories(); int index = categories.indexOf(categoryId); - if(index != -1) { + if (index != -1) { // this category belongs in default profile - if(topic.getDefaultId() == null && children != null) { + if (topic.getDefaultId() == null && children != null) { onDeleteDefaultCategory(categoryId, topicId, children); } subCategoryController.deleteTree(category); @@ -267,42 +263,42 @@ public class CategoryController { 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); + 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"); + 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); + 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"); + 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"); + throw new EntityNotFoundException("Delete category: Stakeholder with id: " + stakeholderId + " not found"); } return true; } public boolean onDeleteDefaultCategory(String defaultCategoryId, String defaultTopicId, String children) { - if(children.equals("delete")) { + if (children.equals("delete")) { List topics = topicDAO.findByDefaultId(defaultTopicId); List categories = categoryDAO.findByDefaultId(defaultCategoryId); - for(Topic topic : topics) { + for (Topic topic : topics) { Iterator categoriesIterator = categories.iterator(); - while(categoriesIterator.hasNext()) { + while (categoriesIterator.hasNext()) { Category category = categoriesIterator.next(); String categoryId = category.getId(); - if(topic.getCategories() != null && topic.getCategories().contains(categoryId)) { + if (topic.getCategories() != null && topic.getCategories().contains(categoryId)) { categoriesIterator.remove(); topic.getCategories().remove(categoryId); @@ -311,21 +307,21 @@ public class CategoryController { subCategoryController.deleteTree(category); categoryDAO.delete(categoryId); - log.debug("Category with id: "+categoryId+" deleted!"); + log.debug("Category with id: " + categoryId + " deleted!"); break; } } } - } else if(children.equals("disconnect")) { + } else if (children.equals("disconnect")) { List categories = categoryDAO.findByDefaultId(defaultCategoryId); - for(Category category : categories) { + for (Category category : categories) { subCategoryController.disConnectTree(category); category.setDefaultId(null); categoryDAO.save(category); - log.debug("DefaultId for Category with id: "+category.getId()+" empty!"); + log.debug("DefaultId for Category with id: " + category.getId() + " empty!"); } } return true; @@ -334,10 +330,10 @@ public class CategoryController { @PreAuthorize("isAuthenticated()") @RequestMapping(value = "/{stakeholderId}/{topicId}/reorder", method = RequestMethod.POST) public List reorderCategories(@PathVariable("stakeholderId") String stakeholderId, - @PathVariable("topicId") String topicId, - @RequestBody List categories) { + @PathVariable("topicId") String topicId, + @RequestBody List categories) { log.debug("reorder categories"); - log.debug("Stakeholder: "+stakeholderId + " - Topic: "+topicId); + log.debug("Stakeholder: " + stakeholderId + " - Topic: " + topicId); Topic topic = checkForExceptions(stakeholderId, topicId); @@ -350,9 +346,9 @@ public class CategoryController { topic.setCategories(categories); List categoriesFull = new ArrayList<>(); - for(String categoryId : categories) { + for (String categoryId : categories) { Category category = categoryDAO.findById(categoryId); - if(category == null) { + if (category == null) { // EXCEPTION - Category not found throw new EntityNotFoundException("Reorder Categories: Category with id: " + categoryId + " not found"); } @@ -368,18 +364,18 @@ public class CategoryController { @PreAuthorize("isAuthenticated()") @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/change-visibility", method = RequestMethod.POST) public Category changeCategoryVisibility(@PathVariable("stakeholderId") String stakeholderId, - @PathVariable("topicId") String topicId, - @PathVariable("categoryId") String categoryId, - @RequestParam("visibility") Visibility visibility, @RequestParam(required = false) Boolean propagate) { - log.debug("change category visibility: "+visibility + " - toggle propagate: "+((propagate != null && propagate) ? "true" : "false")); - log.debug("Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId); + @PathVariable("topicId") String topicId, + @PathVariable("categoryId") String categoryId, + @RequestParam("visibility") Visibility visibility, @RequestParam(required = false) Boolean propagate) { + log.debug("change category visibility: " + visibility + " - toggle propagate: " + ((propagate != null && propagate) ? "true" : "false")); + log.debug("Stakeholder: " + stakeholderId + " - Topic: " + topicId + " - Category: " + categoryId); Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId); if (stakeholder != null) { - if(!rolesUtils.hasUpdateAuthority(stakeholder.getType(), stakeholder.getAlias())) { + if (!rolesUtils.hasUpdateAuthority(stakeholder.getType(), stakeholder.getAlias())) { // EXCEPTION - Access denied - throw new ForbiddenException("Toggle category: You are not authorized to update stakeholder with id: "+stakeholderId); + throw new ForbiddenException("Toggle category: You are not authorized to update stakeholder with id: " + stakeholderId); } Topic topic = topicDAO.findById(topicId); @@ -389,19 +385,19 @@ public class CategoryController { return changeVisibilityTree(categoryId, visibility, propagate); } else { // EXCEPTION - Category not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); - throw new PathNotValidException("Toggle category: Category with id: "+categoryId+" not found in Topic: "+topicId); + throw new PathNotValidException("Toggle category: Category with id: " + categoryId + " not found in Topic: " + topicId); } } else { // EXCEPTION - Topic not found in Stakeholder: stakeholder.getAlias(); - throw new PathNotValidException("Toggle category: Topic with id: "+topicId+" not found in Stakeholder: "+stakeholderId); + throw new PathNotValidException("Toggle category: Topic with id: " + topicId + " not found in Stakeholder: " + stakeholderId); } } else { // EXCEPTION - Topic not found - throw new EntityNotFoundException("Toggle category: Topic with id: "+topicId+" not found"); + throw new EntityNotFoundException("Toggle category: Topic with id: " + topicId + " not found"); } } else { // EXCEPTION - Stakeholder not found - throw new EntityNotFoundException("Toggle category: Stakeholder with id: "+stakeholderId+" not found"); + throw new EntityNotFoundException("Toggle category: Stakeholder with id: " + stakeholderId + " not found"); } } @@ -409,13 +405,13 @@ public class CategoryController { Category category = categoryDAO.findById(categoryId); if (category == null) { // EXCEPTION - Category not found - throw new EntityNotFoundException("Change category visibility: Category with id: "+categoryId+" not found"); + throw new EntityNotFoundException("Change category visibility: Category with id: " + categoryId + " not found"); } Category categoryFull = new Category(category); List subCategoriesFull = new ArrayList<>(); - if(propagate != null && propagate) { + if (propagate != null && propagate) { for (String subCategoryId : category.getSubCategories()) { subCategoriesFull.add(subCategoryController.changeVisibilityTree(subCategoryId, visibility, propagate)); } @@ -436,36 +432,36 @@ public class CategoryController { Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId); - if(stakeholder == null) { + if (stakeholder == null) { // EXCEPTION - Stakeholder not found throw new EntityNotFoundException("checkForExceptions category: Stakeholder with id: " + stakeholderId + " not found"); } - if(!rolesUtils.hasUpdateAuthority(stakeholder.getType(), stakeholder.getAlias())) { + if (!rolesUtils.hasUpdateAuthority(stakeholder.getType(), stakeholder.getAlias())) { // EXCEPTION - Access denied - throw new ForbiddenException("checkForExceptions category: You are not authorized to update stakeholder with id: "+stakeholderId); + throw new ForbiddenException("checkForExceptions category: You are not authorized to update stakeholder with id: " + stakeholderId); } Topic topic = topicDAO.findById(topicId); - if(topic == null) { + if (topic == null) { // EXCEPTION - Topic not found - throw new EntityNotFoundException("checkForExceptions category: Topic with id: "+topicId+" not found"); + throw new EntityNotFoundException("checkForExceptions category: Topic with id: " + topicId + " not found"); } - if(!stakeholder.getTopics().contains(topicId)) { + if (!stakeholder.getTopics().contains(topicId)) { // EXCEPTION - Topic not found in Stakeholder: stakeholder.getAlias(); - throw new PathNotValidException("checkForExceptions category: Topic with id: "+topicId+" not found in Stakeholder: "+stakeholderId); + throw new PathNotValidException("checkForExceptions category: Topic with id: " + topicId + " not found in Stakeholder: " + stakeholderId); } - return topic; + return topic; } public void deleteTree(Topic topic) { List categories = topic.getCategories(); - for(String categoryId : categories) { + for (String categoryId : categories) { Category category = categoryDAO.findById(categoryId); if (category == null) { // EXCEPTION - Category not found - throw new EntityNotFoundException("Category delete tree: Category with id: "+categoryId+" not found (category exists in topic: "+topic.getId()+")"); + throw new EntityNotFoundException("Category delete tree: Category with id: " + categoryId + " not found (category exists in topic: " + topic.getId() + ")"); } subCategoryController.deleteTree(category); @@ -476,11 +472,11 @@ public class CategoryController { public void disConnectTree(Topic topic) { List categories = topic.getCategories(); - for(String categoryId : categories) { + for (String categoryId : categories) { Category category = categoryDAO.findById(categoryId); if (category == null) { // EXCEPTION - Category not found - throw new EntityNotFoundException("Category disconnect tree: Category with id: "+categoryId+" not found (category exists in topic: "+topic.getId()+")"); + throw new EntityNotFoundException("Category disconnect tree: Category with id: " + categoryId + " not found (category exists in topic: " + topic.getId() + ")"); } subCategoryController.disConnectTree(category); diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/EmailController.java b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/EmailController.java index d8380ac..960fe75 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/EmailController.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/EmailController.java @@ -10,7 +10,10 @@ import org.apache.logging.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; -import java.util.*; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; @RestController @CrossOrigin(origins = "*") @@ -27,7 +30,7 @@ public class EmailController { } @RequestMapping(value = "/contact", method = RequestMethod.POST) - public Boolean contact(@RequestBody EmailRecaptcha form) throws InvalidReCaptchaException { + public Boolean contact(@RequestBody EmailRecaptcha form) throws InvalidReCaptchaException { verifyRecaptcha.processResponse(form.getRecaptcha()); Email email = form.getEmail(); return emailSender.send(email.getRecipients(), email.getSubject(), email.getBody(), false); @@ -40,17 +43,17 @@ public class EmailController { String failureString = "failure"; Map> mailResults = new HashMap<>(); boolean bcc = optional.orElse(true); - for(String userMail:email.getRecipients()){ + for (String userMail : email.getRecipients()) { ArrayList sendTo = new ArrayList<>(); sendTo.add(userMail); - boolean success =emailSender.send(sendTo,email.getSubject(),email.getBody(), bcc); - if(success){ - if(!mailResults.containsKey(successString)) { + boolean success = emailSender.send(sendTo, email.getSubject(), email.getBody(), bcc); + if (success) { + if (!mailResults.containsKey(successString)) { mailResults.put(successString, new ArrayList<>()); } mailResults.get(successString).add(userMail); } else { - if(!mailResults.containsKey(failureString)) { + if (!mailResults.containsKey(failureString)) { mailResults.put(failureString, new ArrayList<>()); } mailResults.get(failureString).add(userMail); diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/IndicatorController.java b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/IndicatorController.java index f67c5c2..f8746a5 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/IndicatorController.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/IndicatorController.java @@ -1,12 +1,14 @@ package eu.dnetlib.uoamonitorservice.controllers; +import eu.dnetlib.uoaadmintoolslibrary.handlers.ForbiddenException; import eu.dnetlib.uoaadmintoolslibrary.handlers.utils.RolesUtils; import eu.dnetlib.uoamonitorservice.dao.*; import eu.dnetlib.uoamonitorservice.entities.*; import eu.dnetlib.uoamonitorservice.handlers.EntityNotFoundException; -import eu.dnetlib.uoaadmintoolslibrary.handlers.ForbiddenException; import eu.dnetlib.uoamonitorservice.handlers.PathNotValidException; +import eu.dnetlib.uoamonitorservice.primitives.IndicatorPath; +import eu.dnetlib.uoamonitorservice.primitives.ReorderEvent; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; @@ -51,27 +53,25 @@ public class IndicatorController { @PreAuthorize("isAuthenticated()") @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/save-bulk", method = RequestMethod.POST) public Stakeholder saveBulkIndicators(@PathVariable("stakeholderId") String stakeholderId, - @PathVariable("topicId") String topicId, - @PathVariable("categoryId") String categoryId, - @PathVariable("subcategoryId") String subcategoryId, - @RequestBody List> sections) throws UnsupportedEncodingException { + @PathVariable("topicId") String topicId, + @PathVariable("categoryId") String categoryId, + @PathVariable("subcategoryId") String subcategoryId, + @RequestBody List> sections) throws UnsupportedEncodingException { log.debug("save bulk indicators"); - log.debug("Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId+ " - SubCategory: "+subcategoryId); + log.debug("Stakeholder: " + stakeholderId + " - Topic: " + topicId + " - Category: " + categoryId + " - SubCategory: " + subcategoryId); Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId); Date date = new Date(); createSectionsAndSaveBulk(date, sections, stakeholder, topicId, categoryId, subcategoryId); -// createSectionAndSaveBulk(date, "number", "Numbers imported from file", number_indicators, stakeholder, topicId, categoryId, subcategoryId); - return stakeholderController.setFullEntities(stakeholder); } private void createSectionsAndSaveBulk(Date date, List> old_sections, - Stakeholder stakeholder, String topicId, String categoryId, String subcategoryId) throws UnsupportedEncodingException { - for(Section section : old_sections) { - if(section == null) { + Stakeholder stakeholder, String topicId, String categoryId, String subcategoryId) throws UnsupportedEncodingException { + for (Section section : old_sections) { + if (section == null) { continue; } @@ -81,17 +81,17 @@ public class IndicatorController { List chart_indicators = null; List number_indicators = null; - if(section.getType().equals("chart")) { + if (section.getType().equals("chart")) { chart_section = createSection(chart_section, "chart", section.getTitle(), date, stakeholder, topicId, categoryId, subcategoryId); chart_indicators = chart_section.getIndicators(); - } else if(section.getType().equals("number")) { + } else if (section.getType().equals("number")) { number_section = createSection(number_section, "number", section.getTitle(), date, stakeholder, topicId, categoryId, subcategoryId); number_indicators = number_section.getIndicators(); } List indicators = section.getIndicators(); for (Indicator indicator : indicators) { - if(indicator == null) { + if (indicator == null) { continue; } @@ -126,10 +126,6 @@ public class IndicatorController { return section; } -// private void saveIndicatorAndAddInSection(Indicator indicator, Date date, Stakeholder stakeholder, Section section, List indicators) throws UnsupportedEncodingException { -// saveIndicatorAndAddInSection(indicator, date, stakeholder, section, indicators); -// } - private void saveIndicatorAndAddInSection(Indicator indicator, Date date, Stakeholder stakeholder, String subcategoryId, Section section, List indicators) throws UnsupportedEncodingException { // indicator does not exist in DB indicator.setCreationDate(date); @@ -155,7 +151,7 @@ public class IndicatorController { @PathVariable("sectionId") String sectionId, @RequestBody Indicator indicator) throws UnsupportedEncodingException { log.debug("save indicator"); - log.debug("Name: "+indicator.getName() + " - Id: "+indicator.getId() + " - Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId+ " - SubCategory: "+subcategoryId + " - Section: "+sectionId); + log.debug("Name: " + indicator.getName() + " - Id: " + indicator.getId() + " - Stakeholder: " + stakeholderId + " - Topic: " + topicId + " - Category: " + categoryId + " - SubCategory: " + subcategoryId + " - Section: " + sectionId); Section section = checkForExceptions(stakeholderId, topicId, categoryId, subcategoryId, sectionId, indicator.getType()); @@ -163,9 +159,9 @@ public class IndicatorController { indicator.setUpdateDate(date); Indicator oldIndicator = null; - if(indicator.getId() != null) { + if (indicator.getId() != null) { oldIndicator = indicatorDAO.findById(indicator.getId()); - if(oldIndicator == null) { + if (oldIndicator == null) { // EXCEPTION - Indicator not found throw new EntityNotFoundException("save indicator: Indicator with id: " + indicator.getId() + " not found"); } @@ -177,12 +173,11 @@ public class IndicatorController { Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId); // this indicator belongs in default profile and it is new or it is updated - if(stakeholder.getDefaultId() == null) { - if(indicatorId == null) { + if (stakeholder.getDefaultId() == null) { + if (indicatorId == null) { indicatorDAO.save(indicator); onSaveDefaultIndicator(indicator, section, subcategoryId); - } - else { + } else { onUpdateDefaultIndicator(indicator, stakeholder, oldIndicator); indicatorDAO.save(indicator); } @@ -210,7 +205,7 @@ public class IndicatorController { for (SubCategory subCategory : subCategories) { List sections = null; - if(defaultSection.getType().equals("chart")) { + if (defaultSection.getType().equals("chart")) { sections = subCategory.getCharts(); } else { sections = subCategory.getNumbers(); @@ -218,7 +213,7 @@ public class IndicatorController { for (String sectionId : sections) { Section section = sectionDAO.findById(sectionId); - if(section.getDefaultId() != null && section.getDefaultId().equals(defaultSection.getId())) { + if (section.getDefaultId() != null && section.getDefaultId().equals(defaultSection.getId())) { Indicator indicatorNew = new Indicator(); indicatorNew.copyFromDefault(indicator, subCategory.getVisibility()); indicatorDAO.save(indicatorNew); @@ -238,12 +233,9 @@ public class IndicatorController { boolean changed; List indicators = indicatorDAO.findByDefaultId(indicator.getId()); - for(Indicator indicatorBasedOnDefault : indicators) { + for (Indicator indicatorBasedOnDefault : indicators) { changed = false; - -// if(indicator.getName() != null && !indicator.getName().equals(indicatorBasedOnDefault.getName()) -// && (oldIndicator.getName() == null || oldIndicator.getName().equals(indicatorBasedOnDefault.getName()))) { - if(( + if (( (indicator.getName() == null && oldIndicator.getName() != null) || (indicator.getName() != null && !indicator.getName().equals(indicatorBasedOnDefault.getName())) @@ -256,16 +248,13 @@ public class IndicatorController { changed = true; } - if(indicator.getDescription() != null && !indicator.getDescription().equals(indicatorBasedOnDefault.getDescription()) - || indicator.getDescription() == null && indicatorBasedOnDefault.getDescription() != null) { + if (indicator.getDescription() != null && !indicator.getDescription().equals(indicatorBasedOnDefault.getDescription()) + || indicator.getDescription() == null && indicatorBasedOnDefault.getDescription() != null) { indicatorBasedOnDefault.setDescription(indicator.getDescription()); changed = true; } - -// if(indicator.getAdditionalDescription() != null && !indicator.getAdditionalDescription().equals(indicatorBasedOnDefault.getAdditionalDescription()) -// && (oldIndicator.getAdditionalDescription() == null || oldIndicator.getAdditionalDescription().equals(indicatorBasedOnDefault.getAdditionalDescription()))) { - if(( + if (( (indicator.getAdditionalDescription() == null && oldIndicator.getAdditionalDescription() != null) || (indicator.getAdditionalDescription() != null && !indicator.getAdditionalDescription().equals(indicatorBasedOnDefault.getAdditionalDescription())) @@ -280,17 +269,17 @@ public class IndicatorController { int i = 0; List indicatorPaths = indicatorBasedOnDefault.getIndicatorPaths(); - if(indicatorPaths == null && indicator.getIndicatorPaths() != null) { + if (indicatorPaths == null && indicator.getIndicatorPaths() != null) { indicatorPaths = new ArrayList<>(); } for (IndicatorPath indicatorPath : indicator.getIndicatorPaths()) { IndicatorPath indicatorPathBasedOnDefault = null; - if(i < indicatorPaths.size()) { + if (i < indicatorPaths.size()) { indicatorPathBasedOnDefault = indicatorPaths.get(i); } - if(indicatorPathBasedOnDefault == null) { + if (indicatorPathBasedOnDefault == null) { // Add new indicator path in existing indicators IndicatorPath indicatorPathNew = new IndicatorPath(indicatorPath); indicatorPaths.add(indicatorPathNew); @@ -299,12 +288,8 @@ public class IndicatorController { IndicatorPath oldIndicatorPath = oldIndicator.getIndicatorPaths().get(i); // Check if there are changes in indicator path and update existing indicators if needed - log.debug("update indicator path: "+i + " (indicator id: "+indicatorBasedOnDefault.getId()+")"); - -// if(indicatorPath.getType() != null -// && !indicatorPath.getType().equals(indicatorPathBasedOnDefault.getType()) -// && (oldIndicatorPath.getType().equals(indicatorPathBasedOnDefault.getType()))) { - if(( + log.debug("update indicator path: " + i + " (indicator id: " + indicatorBasedOnDefault.getId() + ")"); + if (( (indicatorPath.getType() == null && oldIndicatorPath.getType() != null) || (indicatorPath.getType() != null && !indicatorPath.getType().equals(indicatorPathBasedOnDefault.getType())) @@ -316,9 +301,9 @@ public class IndicatorController { indicatorPathBasedOnDefault.setType(indicatorPath.getType()); changed = true; // parameter "type" needs to be changed as well } - log.debug("After type check: "+changed); + log.debug("After type check: " + changed); - if(( + if (( (indicatorPath.getFormat() == null && oldIndicatorPath.getFormat() != null) || (indicatorPath.getFormat() != null && !indicatorPath.getFormat().equals(indicatorPathBasedOnDefault.getFormat())) @@ -330,12 +315,8 @@ public class IndicatorController { indicatorPathBasedOnDefault.setFormat(indicatorPath.getFormat()); changed = true; } - log.debug("After type check: "+changed); - -// if(indicatorPath.getSource() != null -// && !indicatorPath.getSource().equals(indicatorPathBasedOnDefault.getSource()) -// && (oldIndicatorPath.getSource().equals(indicatorPathBasedOnDefault.getSource()))) { - if(( + log.debug("After type check: " + changed); + if (( (indicatorPath.getSource() == null && oldIndicatorPath.getSource() != null) || (indicatorPath.getSource() != null && !indicatorPath.getSource().equals(indicatorPathBasedOnDefault.getSource())) @@ -347,12 +328,8 @@ public class IndicatorController { indicatorPathBasedOnDefault.setSource(indicatorPath.getSource()); changed = true; } - log.debug("After source check: "+changed); - -// if(indicatorPath.getUrl() != null -// && !indicatorPath.getUrl().equals(indicatorPathBasedOnDefault.getUrl()) -// && (oldIndicatorPath.getUrl().equals(indicatorPathBasedOnDefault.getUrl()))) { - if(( + log.debug("After source check: " + changed); + if (( (indicatorPath.getUrl() == null && oldIndicatorPath.getUrl() != null) || (indicatorPath.getUrl() != null && !indicatorPath.getUrl().equals(indicatorPathBasedOnDefault.getUrl())) @@ -364,37 +341,35 @@ public class IndicatorController { indicatorPathBasedOnDefault.setUrl(indicatorPath.getUrl()); changed = true; } - log.debug("After url check: "+changed); + log.debug("After url check: " + changed); - if(( + if (( (indicatorPath.getChartObject() == null && oldIndicatorPath.getChartObject() != null) - || - (indicatorPath.getChartObject() != null && !indicatorPath.getChartObject().equals(indicatorPathBasedOnDefault.getChartObject())) - ) && ( + || + (indicatorPath.getChartObject() != null && !indicatorPath.getChartObject().equals(indicatorPathBasedOnDefault.getChartObject())) + ) && ( (oldIndicatorPath.getChartObject() == null && indicatorPathBasedOnDefault.getChartObject() == null) - || - (oldIndicatorPath.getChartObject() != null && oldIndicatorPath.getChartObject().equals(indicatorPathBasedOnDefault.getChartObject())) + || + (oldIndicatorPath.getChartObject() != null && oldIndicatorPath.getChartObject().equals(indicatorPathBasedOnDefault.getChartObject())) )) { indicatorPathBasedOnDefault.setChartObject(indicatorPath.getChartObject()); changed = true; } - log.debug("After chartObject check: "+changed); + log.debug("After chartObject check: " + changed); - if(indicatorPath.getParameters() != null) { + if (indicatorPath.getParameters() != null) { if (indicatorPathBasedOnDefault.getParameters() == null) { indicatorPathBasedOnDefault.setParameters(new HashMap<>()); } - //if (indicatorPath.getParameters().size() != indicatorPathBasedOnDefault.getParameters().size()) { - //log.debug("Different number of parameters"); for (Map.Entry parameter : indicatorPath.getParameters().entrySet()) { - log.debug("\nindicatorPath: parameter.getKey(): "+parameter.getKey()+" - value: "+parameter.getValue() - +"\nindicatorPathBasedOnDefault:parameters:key: "+ indicatorPathBasedOnDefault.getParameters().get(parameter.getKey()) - +"\noldIndicatorPath:parameters:key: "+ (oldIndicatorPath.getParameters() == null ? "null" : oldIndicatorPath.getParameters().get(parameter.getKey()))); + log.debug("\nindicatorPath: parameter.getKey(): " + parameter.getKey() + " - value: " + parameter.getValue() + + "\nindicatorPathBasedOnDefault:parameters:key: " + indicatorPathBasedOnDefault.getParameters().get(parameter.getKey()) + + "\noldIndicatorPath:parameters:key: " + (oldIndicatorPath.getParameters() == null ? "null" : oldIndicatorPath.getParameters().get(parameter.getKey()))); if (!indicatorPathBasedOnDefault.getParameters().containsKey(parameter.getKey()) || (oldIndicatorPath.getParameters() == null || oldIndicatorPath.getParameters().get(parameter.getKey()) == null - || (oldIndicatorPath.getParameters().get(parameter.getKey()).equals(indicatorPathBasedOnDefault.getParameters().get(parameter.getKey())) - && !parameter.getValue().equals(indicatorPathBasedOnDefault.getParameters().get(parameter.getKey())))) + || (oldIndicatorPath.getParameters().get(parameter.getKey()).equals(indicatorPathBasedOnDefault.getParameters().get(parameter.getKey())) + && !parameter.getValue().equals(indicatorPathBasedOnDefault.getParameters().get(parameter.getKey())))) ) { indicatorPathBasedOnDefault.getParameters().put(parameter.getKey(), parameter.getValue()); changed = true; @@ -402,9 +377,9 @@ public class IndicatorController { } // When deleting indicator path parameters in a default profile, delete them also from all children profiles - if(oldIndicatorPath.getParameters() != null && indicatorPath.getParameters().size() < oldIndicatorPath.getParameters().size()) { + if (oldIndicatorPath.getParameters() != null && indicatorPath.getParameters().size() < oldIndicatorPath.getParameters().size()) { for (Map.Entry parameter : oldIndicatorPath.getParameters().entrySet()) { - if(!indicatorPath.getParameters().containsKey(parameter.getKey())) { + if (!indicatorPath.getParameters().containsKey(parameter.getKey())) { indicatorPathBasedOnDefault.getParameters().remove(parameter.getKey()); } } @@ -412,21 +387,21 @@ public class IndicatorController { } log.debug("After parameters check: " + changed); - if(indicatorPath.getJsonPath() != null) { + if (indicatorPath.getJsonPath() != null) { boolean jsonPathChanged = false; boolean breaked = false; int oldJsonPathSize = 0; - if(oldIndicatorPath.getJsonPath() != null) { + if (oldIndicatorPath.getJsonPath() != null) { oldJsonPathSize = oldIndicatorPath.getJsonPath().size(); } int basedOnDefaultJsonPathSize = 0; - if(indicatorPathBasedOnDefault.getJsonPath() != null) { + if (indicatorPathBasedOnDefault.getJsonPath() != null) { basedOnDefaultJsonPathSize = indicatorPathBasedOnDefault.getJsonPath().size(); } - log.debug("old: "+oldJsonPathSize+" - based on default: "+basedOnDefaultJsonPathSize+" - new: "+indicatorPath.getJsonPath().size()); - if(oldJsonPathSize == basedOnDefaultJsonPathSize) { - if(indicatorPathBasedOnDefault.getJsonPath() == null && indicatorPath.getJsonPath().size() > 0) { + log.debug("old: " + oldJsonPathSize + " - based on default: " + basedOnDefaultJsonPathSize + " - new: " + indicatorPath.getJsonPath().size()); + if (oldJsonPathSize == basedOnDefaultJsonPathSize) { + if (indicatorPathBasedOnDefault.getJsonPath() == null && indicatorPath.getJsonPath().size() > 0) { indicatorPathBasedOnDefault.setJsonPath(new ArrayList<>()); } @@ -436,12 +411,12 @@ public class IndicatorController { Iterator jsonStringBasedOnDefaultIterator = indicatorPathBasedOnDefault.getJsonPath().iterator(); while (jsonStringBasedOnDefaultIterator.hasNext()) { String jsonStringBasedOnDefault = jsonStringBasedOnDefaultIterator.next(); - if(oldIndicatorPath.getJsonPath().get(oldIndex).equals(jsonStringBasedOnDefault)) { - if(basedOnDefaultIndex >= indicatorPath.getJsonPath().size()) { // string deleted + if (oldIndicatorPath.getJsonPath().get(oldIndex).equals(jsonStringBasedOnDefault)) { + if (basedOnDefaultIndex >= indicatorPath.getJsonPath().size()) { // string deleted jsonStringBasedOnDefaultIterator.remove(); jsonPathChanged = true; } else { // check if string changed - if(!indicatorPath.getJsonPath().get(basedOnDefaultIndex).equals(jsonStringBasedOnDefault)) { + if (!indicatorPath.getJsonPath().get(basedOnDefaultIndex).equals(jsonStringBasedOnDefault)) { indicatorPathBasedOnDefault.getJsonPath().set(basedOnDefaultIndex, indicatorPath.getJsonPath().get(basedOnDefaultIndex)); jsonPathChanged = true; } @@ -451,20 +426,20 @@ public class IndicatorController { } else { breaked = true; jsonPathChanged = false; - log.debug("not the same: "+oldIndex); + log.debug("not the same: " + oldIndex); break; } } - int index=0; - if(!breaked && indicatorPath.getJsonPath().size() > indicatorPathBasedOnDefault.getJsonPath().size()) { // strings added + int index = 0; + if (!breaked && indicatorPath.getJsonPath().size() > indicatorPathBasedOnDefault.getJsonPath().size()) { // strings added jsonPathChanged = true; - for(index=indicatorPathBasedOnDefault.getJsonPath().size(); index < indicatorPath.getJsonPath().size(); index++) { + for (index = indicatorPathBasedOnDefault.getJsonPath().size(); index < indicatorPath.getJsonPath().size(); index++) { indicatorPathBasedOnDefault.getJsonPath().add(indicatorPath.getJsonPath().get(index)); } } - if(jsonPathChanged) { + if (jsonPathChanged) { changed = true; } } @@ -476,7 +451,7 @@ public class IndicatorController { } // TODO when deleting indicator paths... - if(!changed) { + if (!changed) { // break; continue; } @@ -496,16 +471,16 @@ public class IndicatorController { @PathVariable("indicatorId") String indicatorId, @RequestParam(required = false) String children) { log.debug("delete indicator"); - log.debug("Id: "+indicatorId + " - Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId+ " - SubCategory: "+subcategoryId + " - Section: "+sectionId); + log.debug("Id: " + indicatorId + " - Stakeholder: " + stakeholderId + " - Topic: " + topicId + " - Category: " + categoryId + " - SubCategory: " + subcategoryId + " - Section: " + sectionId); Indicator indicator = indicatorDAO.findById(indicatorId); - if(indicator != null) { + if (indicator != null) { Section section = checkForExceptions(stakeholderId, topicId, categoryId, subcategoryId, sectionId, indicator.getType()); Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId); - if(indicator.getDefaultId() != null && !rolesUtils.hasCreateAndDeleteAuthority(stakeholder.getType())) { + if (indicator.getDefaultId() != null && !rolesUtils.hasCreateAndDeleteAuthority(stakeholder.getType())) { // EXCEPTION - Access denied - throw new ForbiddenException("Delete indicator: You are not authorized to delete a default Indicator in stakeholder with id: "+stakeholderId); + throw new ForbiddenException("Delete indicator: You are not authorized to delete a default Indicator in stakeholder with id: " + stakeholderId); } List indicators = section.getIndicators(); @@ -514,7 +489,7 @@ public class IndicatorController { if (index != -1) { // this indicator belongs in default profile - if(section.getDefaultId() == null && children != null) { + if (section.getDefaultId() == null && children != null) { onDeleteDefaultIndicator(indicatorId, sectionId, children); } @@ -526,44 +501,44 @@ public class IndicatorController { log.debug("Indicator deleted!"); } else { // EXCEPTION - Indicator not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); -> Category: category.getAlias(); -> SubCategory: subcategory.getAlias(); -> Section: section.getTitle(); - throw new PathNotValidException("Delete indicator: Indicator with id: "+indicatorId+" not found in Sectiom: "+sectionId); + throw new PathNotValidException("Delete indicator: Indicator with id: " + indicatorId + " not found in Sectiom: " + sectionId); } } else { // EXCEPTION - Indicator not found - throw new EntityNotFoundException("Delete indicator: Indicator with id: "+indicatorId+" not found"); + throw new EntityNotFoundException("Delete indicator: Indicator with id: " + indicatorId + " not found"); } return true; } public boolean onDeleteDefaultIndicator(String defaultIndicatorId, String defaultSectionId, String children) { - if(children.equals("delete")) { + if (children.equals("delete")) { // 2nd way List
sections = sectionDAO.findByDefaultId(defaultSectionId); List indicators = indicatorDAO.findByDefaultId(defaultIndicatorId); - for(Section section : sections) { + for (Section section : sections) { Iterator indicatorsIterator = indicators.iterator(); - while(indicatorsIterator.hasNext()) { + while (indicatorsIterator.hasNext()) { String indicatorId = indicatorsIterator.next().getId(); - if(section.getIndicators().contains(indicatorId)) { + if (section.getIndicators().contains(indicatorId)) { indicatorsIterator.remove(); section.getIndicators().remove(indicatorId); sectionDAO.save(section); indicatorDAO.delete(indicatorId); - log.debug("Indicator with id: "+indicatorId+" deleted!"); + log.debug("Indicator with id: " + indicatorId + " deleted!"); break; } } } - } else if(children.equals("disconnect")) { + } else if (children.equals("disconnect")) { List indicators = indicatorDAO.findByDefaultId(defaultIndicatorId); - for(Indicator indicator : indicators) { + for (Indicator indicator : indicators) { indicator.setDefaultId(null); indicatorDAO.save(indicator); - log.debug("DefaultId for Indicator with id: "+indicator.getId()+" empty!"); + log.debug("DefaultId for Indicator with id: " + indicator.getId() + " empty!"); } } return true; @@ -578,8 +553,8 @@ public class IndicatorController { @PathVariable("sectionId") String sectionId, @PathVariable("type") String type, @RequestBody ReorderEvent reorderEvent) { - log.debug("reorder indicators of type: "+type); - log.debug("Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId+ " - SubCategory: "+subcategoryId + " - Section: "+sectionId); + log.debug("reorder indicators of type: " + type); + log.debug("Stakeholder: " + stakeholderId + " - Topic: " + topicId + " - Category: " + categoryId + " - SubCategory: " + subcategoryId + " - Section: " + sectionId); List indicators = reorderEvent.getIds(); String actionType = reorderEvent.getAction(); @@ -596,9 +571,9 @@ public class IndicatorController { section.setIndicators(indicators); List indicatorsFull = new ArrayList<>(); - for(String indicatorId : indicators) { + for (String indicatorId : indicators) { Indicator indicator = indicatorDAO.findById(indicatorId); - if(indicator == null) { + if (indicator == null) { // EXCEPTION - Indicator not found throw new EntityNotFoundException("Reorder indicators: Indicator with id: " + indicatorId + " not found"); } @@ -614,14 +589,14 @@ public class IndicatorController { @PreAuthorize("isAuthenticated()") @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/{sectionId}/{indicatorId}/change-visibility", method = RequestMethod.POST) public Indicator changeIndicatorVisibility(@PathVariable("stakeholderId") String stakeholderId, - @PathVariable("topicId") String topicId, - @PathVariable("categoryId") String categoryId, - @PathVariable("subcategoryId") String subcategoryId, - @PathVariable("sectionId") String sectionId, - @PathVariable("indicatorId") String indicatorId, - @RequestParam("visibility") Visibility visibility) { - log.debug("change indicator visibility: "+visibility); - log.debug("Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId+ " - SubCategory: "+subcategoryId + " - Section: "+sectionId+ " - Indicator: "+indicatorId); + @PathVariable("topicId") String topicId, + @PathVariable("categoryId") String categoryId, + @PathVariable("subcategoryId") String subcategoryId, + @PathVariable("sectionId") String sectionId, + @PathVariable("indicatorId") String indicatorId, + @RequestParam("visibility") Visibility visibility) { + log.debug("change indicator visibility: " + visibility); + log.debug("Stakeholder: " + stakeholderId + " - Topic: " + topicId + " - Category: " + categoryId + " - SubCategory: " + subcategoryId + " - Section: " + sectionId + " - Indicator: " + indicatorId); Indicator indicator = indicatorDAO.findById(indicatorId); if (indicator == null) { @@ -632,16 +607,16 @@ public class IndicatorController { Section section = checkForExceptions(stakeholderId, topicId, categoryId, subcategoryId, sectionId, indicator.getType()); List indicators = section.getIndicators(); - if(indicators.contains(indicatorId)) { + if (indicators.contains(indicatorId)) { return changeVisibilityTree(indicatorId, indicator, visibility); } else { // EXCEPTION - Indicator not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); -> Category: category.getAlias(); -> SubCategory: subCategory.getAlias(); -> Section: section.getTitle(); - throw new PathNotValidException("Toggle indicators: Indicator with id: "+indicatorId+" not found in Section: "+sectionId); + throw new PathNotValidException("Toggle indicators: Indicator with id: " + indicatorId + " not found in Section: " + sectionId); } } public Indicator changeVisibilityTree(String indicatorId, Indicator indicator, Visibility visibility) { - if(indicator == null) { + if (indicator == null) { indicator = indicatorDAO.findById(indicatorId); if (indicator == null) { // EXCEPTION - Indicator not found @@ -660,79 +635,79 @@ public class IndicatorController { Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId); - if(stakeholder == null) { + if (stakeholder == null) { // EXCEPTION - Stakeholder not found throw new EntityNotFoundException("Save indicator: Stakeholder with id: " + stakeholderId + " not found"); } - if(!rolesUtils.hasUpdateAuthority(stakeholder.getType(), stakeholder.getAlias())) { + if (!rolesUtils.hasUpdateAuthority(stakeholder.getType(), stakeholder.getAlias())) { // EXCEPTION - Access denied - throw new ForbiddenException("CheckForExceptions Indicator: You are not authorized to update stakeholder with id: "+stakeholderId); + throw new ForbiddenException("CheckForExceptions Indicator: You are not authorized to update stakeholder with id: " + stakeholderId); } Topic topic = topicDAO.findById(topicId); - if(topic == null) { + if (topic == null) { // EXCEPTION - Topic not found - throw new EntityNotFoundException("Save indicator: Topic with id: "+topicId+" not found"); + throw new EntityNotFoundException("Save indicator: Topic with id: " + topicId + " not found"); } - if(!stakeholder.getTopics().contains(topicId)) { + if (!stakeholder.getTopics().contains(topicId)) { // EXCEPTION - Topic not found in Stakeholder: stakeholder.getAlias(); throw new PathNotValidException("Save indicator: Topic with id: " + topicId + " not found in Stakeholder: " + stakeholderId); } Category category = categoryDAO.findById(categoryId); - if(category == null) { + if (category == null) { // EXCEPTION - Category not found - throw new EntityNotFoundException("Save indicator: Category with id: "+categoryId+" not found"); + throw new EntityNotFoundException("Save indicator: Category with id: " + categoryId + " not found"); } - if(!topic.getCategories().contains(categoryId)) { + if (!topic.getCategories().contains(categoryId)) { // EXCEPTION - Category not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); - throw new PathNotValidException("Save indicator: Category with id: "+categoryId+" not found in Topic: "+topicId); + throw new PathNotValidException("Save indicator: Category with id: " + categoryId + " not found in Topic: " + topicId); } SubCategory subcategory = subCategoryDAO.findById(subcategoryId); - if(subcategory == null) { + if (subcategory == null) { // EXCEPTION - SubCategory not found - throw new EntityNotFoundException("Save indicator: SubCategory with id: "+subcategoryId+" not found"); + throw new EntityNotFoundException("Save indicator: SubCategory with id: " + subcategoryId + " not found"); } if (!category.getSubCategories().contains(subcategoryId)) { // EXCEPTION - SubCategory not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); -> Category: category.getAlias(); - throw new PathNotValidException("Save indicator: SubCategory with id: "+subcategoryId+" not found in Category: "+categoryId); + throw new PathNotValidException("Save indicator: SubCategory with id: " + subcategoryId + " not found in Category: " + categoryId); } Section section = sectionDAO.findById(sectionId); - if(section == null) { + if (section == null) { // EXCEPTION - Section not found - throw new EntityNotFoundException("Save indicator: Section with id: "+sectionId+" not found"); + throw new EntityNotFoundException("Save indicator: Section with id: " + sectionId + " not found"); } - if(indicatorType.equals("chart")) { + if (indicatorType.equals("chart")) { if (!subcategory.getCharts().contains(sectionId)) { // EXCEPTION - Section not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); -> Category: category.getAlias(); -> SubCategory: subcategory.getAlias(); throw new PathNotValidException("Save indicator: SubCategory with id: " + subcategoryId + " not found in Category: " + categoryId); } - } else if(indicatorType.equals("number")) { + } else if (indicatorType.equals("number")) { if (!subcategory.getNumbers().contains(sectionId)) { // EXCEPTION - Section not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); -> Category: category.getAlias(); -> SubCategory: subcategory.getAlias(); throw new PathNotValidException("Save indicator: SubCategory with id: " + subcategoryId + " not found in Category: " + categoryId); } } - return section; + return section; } public void deleteTree(Section section) { List indicators = section.getIndicators(); - for(String indicatorId : indicators) { + for (String indicatorId : indicators) { indicatorDAO.delete(indicatorId); } } public void disConnectTree(Section section) { List indicators = section.getIndicators(); - for(String indicatorId : indicators) { + for (String indicatorId : indicators) { Indicator indicator = indicatorDAO.findById(indicatorId); indicator.setDefaultId(null); indicatorDAO.save(indicator); diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/dao/CategoryDAO.java b/src/main/java/eu/dnetlib/uoamonitorservice/dao/CategoryDAO.java index fed3a16..b8d4db0 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/dao/CategoryDAO.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/dao/CategoryDAO.java @@ -1,10 +1,13 @@ package eu.dnetlib.uoamonitorservice.dao; import eu.dnetlib.uoamonitorservice.entities.Category; +import org.springframework.data.mongodb.repository.MongoRepository; +import org.springframework.stereotype.Repository; import java.util.List; -public interface CategoryDAO { +@Repository +public interface CategoryDAO extends MongoRepository { List findAll(); List findByDefaultId(String DefaultId); diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/dao/IndicatorDAO.java b/src/main/java/eu/dnetlib/uoamonitorservice/dao/IndicatorDAO.java index 0ac32bd..8940da8 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/dao/IndicatorDAO.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/dao/IndicatorDAO.java @@ -1,10 +1,13 @@ package eu.dnetlib.uoamonitorservice.dao; import eu.dnetlib.uoamonitorservice.entities.Indicator; +import org.springframework.data.mongodb.repository.MongoRepository; +import org.springframework.stereotype.Repository; import java.util.List; -public interface IndicatorDAO { +@Repository +public interface IndicatorDAO extends MongoRepository { List findAll(); List findByDefaultId(String DefaultId); diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/dao/MongoDBCategoryDAO.java b/src/main/java/eu/dnetlib/uoamonitorservice/dao/MongoDBCategoryDAO.java deleted file mode 100644 index fc71604..0000000 --- a/src/main/java/eu/dnetlib/uoamonitorservice/dao/MongoDBCategoryDAO.java +++ /dev/null @@ -1,19 +0,0 @@ -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 { - List findAll(); - List findByDefaultId(String DefaultId); - - Category findBySubCategoriesContaining(String subCategory); - - Category findById(String Id); - - void delete(String Id); - - Category save(Category category); -} diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/dao/MongoDBIndicatorDAO.java b/src/main/java/eu/dnetlib/uoamonitorservice/dao/MongoDBIndicatorDAO.java deleted file mode 100644 index f35ee01..0000000 --- a/src/main/java/eu/dnetlib/uoamonitorservice/dao/MongoDBIndicatorDAO.java +++ /dev/null @@ -1,18 +0,0 @@ -package eu.dnetlib.uoamonitorservice.dao; - -import eu.dnetlib.uoamonitorservice.entities.Indicator; -import org.springframework.data.mongodb.repository.MongoRepository; - -import java.util.List; - -public interface MongoDBIndicatorDAO extends IndicatorDAO, MongoRepository { - List findAll(); - List findByDefaultId(String DefaultId); - - Indicator findById(String Id); - - void delete(String Id); - - Indicator save(Indicator indicator); - //List saveAll(List indicators); -} diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/dao/MongoDBSectionDAO.java b/src/main/java/eu/dnetlib/uoamonitorservice/dao/MongoDBSectionDAO.java deleted file mode 100644 index 51eeb6c..0000000 --- a/src/main/java/eu/dnetlib/uoamonitorservice/dao/MongoDBSectionDAO.java +++ /dev/null @@ -1,17 +0,0 @@ -package eu.dnetlib.uoamonitorservice.dao; - -import eu.dnetlib.uoamonitorservice.entities.Section; -import org.springframework.data.mongodb.repository.MongoRepository; - -import java.util.List; - -public interface MongoDBSectionDAO extends SectionDAO, MongoRepository { - List
findAll(); - List
findByDefaultId(String DefaultId); - - Section findById(String Id); - - void delete(String Id); - - Section save(Section indicator); -} diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/dao/MongoDBStakeholderDAO.java b/src/main/java/eu/dnetlib/uoamonitorservice/dao/MongoDBStakeholderDAO.java deleted file mode 100644 index 0258a37..0000000 --- a/src/main/java/eu/dnetlib/uoamonitorservice/dao/MongoDBStakeholderDAO.java +++ /dev/null @@ -1,26 +0,0 @@ -package eu.dnetlib.uoamonitorservice.dao; - -import eu.dnetlib.uoamonitorservice.entities.Stakeholder; -import org.springframework.data.mongodb.repository.MongoRepository; - -import java.util.List; - -public interface MongoDBStakeholderDAO extends StakeholderDAO, MongoRepository { - List findAll(); - List findByType(String Type); - - List findByDefaultId(String DefaultId); - List findByDefaultIdAndType(String DefaultId, String Type); - - List findByDefaultIdNot(String DefaultId); - List findByDefaultIdNotAndType(String DefaultId, String Type); - - Stakeholder findByTopicsContaining(String topic); - - Stakeholder findById(String Id); - Stakeholder findByAlias(String Alias); - - void delete(String Id); - - Stakeholder save(Stakeholder stakeholder); -} diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/dao/MongoDBSubCategoryDAO.java b/src/main/java/eu/dnetlib/uoamonitorservice/dao/MongoDBSubCategoryDAO.java deleted file mode 100644 index 375085e..0000000 --- a/src/main/java/eu/dnetlib/uoamonitorservice/dao/MongoDBSubCategoryDAO.java +++ /dev/null @@ -1,17 +0,0 @@ -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 { - List findAll(); - List findByDefaultId(String DefaultId); - - SubCategory findById(String Id); - - void delete(String Id); - - SubCategory save(SubCategory subCategory); -} diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/dao/MongoDBTopicDAO.java b/src/main/java/eu/dnetlib/uoamonitorservice/dao/MongoDBTopicDAO.java deleted file mode 100644 index 1f7e967..0000000 --- a/src/main/java/eu/dnetlib/uoamonitorservice/dao/MongoDBTopicDAO.java +++ /dev/null @@ -1,19 +0,0 @@ -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 { - List findAll(); - List findByDefaultId(String DefaultId); - - Topic findByCategoriesContaining(String category); - - Topic findById(String Id); - - void delete(String Id); - - Topic save(Topic topic); -} diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/dao/SectionDAO.java b/src/main/java/eu/dnetlib/uoamonitorservice/dao/SectionDAO.java index 7a75e99..cd24e5a 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/dao/SectionDAO.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/dao/SectionDAO.java @@ -1,10 +1,13 @@ package eu.dnetlib.uoamonitorservice.dao; import eu.dnetlib.uoamonitorservice.entities.Section; +import org.springframework.data.mongodb.repository.MongoRepository; +import org.springframework.stereotype.Repository; import java.util.List; -public interface SectionDAO { +@Repository +public interface SectionDAO extends MongoRepository { List
findAll(); List
findByDefaultId(String DefaultId); diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/dao/StakeholderDAO.java b/src/main/java/eu/dnetlib/uoamonitorservice/dao/StakeholderDAO.java index d5daaad..113c105 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/dao/StakeholderDAO.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/dao/StakeholderDAO.java @@ -1,10 +1,13 @@ package eu.dnetlib.uoamonitorservice.dao; import eu.dnetlib.uoamonitorservice.entities.Stakeholder; +import org.springframework.data.mongodb.repository.MongoRepository; +import org.springframework.stereotype.Repository; import java.util.List; -public interface StakeholderDAO { +@Repository +public interface StakeholderDAO extends MongoRepository { List findAll(); List findByType(String Type); diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/dao/SubCategoryDAO.java b/src/main/java/eu/dnetlib/uoamonitorservice/dao/SubCategoryDAO.java index 8a9f406..2e22285 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/dao/SubCategoryDAO.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/dao/SubCategoryDAO.java @@ -1,10 +1,13 @@ package eu.dnetlib.uoamonitorservice.dao; import eu.dnetlib.uoamonitorservice.entities.SubCategory; +import org.springframework.data.mongodb.repository.MongoRepository; +import org.springframework.stereotype.Repository; import java.util.List; -public interface SubCategoryDAO { +@Repository +public interface SubCategoryDAO extends MongoRepository { List findAll(); List findByDefaultId(String DefaultId); diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/dao/TopicDAO.java b/src/main/java/eu/dnetlib/uoamonitorservice/dao/TopicDAO.java index b57f8ce..f6a36fc 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/dao/TopicDAO.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/dao/TopicDAO.java @@ -1,10 +1,13 @@ package eu.dnetlib.uoamonitorservice.dao; import eu.dnetlib.uoamonitorservice.entities.Topic; +import org.springframework.data.mongodb.repository.MongoRepository; +import org.springframework.stereotype.Repository; import java.util.List; -public interface TopicDAO { +@Repository +public interface TopicDAO extends MongoRepository { List findAll(); List findByDefaultId(String DefaultId); diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/entities/Indicator.java b/src/main/java/eu/dnetlib/uoamonitorservice/entities/Indicator.java index ca6947c..a4aa824 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/entities/Indicator.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/entities/Indicator.java @@ -1,23 +1,14 @@ package eu.dnetlib.uoamonitorservice.entities; import com.fasterxml.jackson.annotation.JsonProperty; +import eu.dnetlib.uoamonitorservice.primitives.IndicatorPath; +import eu.dnetlib.uoamonitorservice.primitives.IndicatorSize; +import eu.dnetlib.uoamonitorservice.primitives.IndicatorType; import org.springframework.data.annotation.Id; import java.util.Date; import java.util.List; -enum IndicatorType { - // Do not rename or remove existring values. This may cause problems with already stored values in DB - number, chart, - NUMBER, CHART; -} - -enum IndicatorSize { - // Do not rename or remove existring values. This may cause problems with already stored values in DB - small, medium, large, - SMALL, MEDIUM, LARGE; -} - public class Indicator { @Id @JsonProperty("_id") diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/entities/Section.java b/src/main/java/eu/dnetlib/uoamonitorservice/entities/Section.java index 1f0f064..c0664af 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/entities/Section.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/entities/Section.java @@ -1,18 +1,13 @@ package eu.dnetlib.uoamonitorservice.entities; import com.fasterxml.jackson.annotation.JsonProperty; +import eu.dnetlib.uoamonitorservice.primitives.IndicatorType; import org.springframework.data.annotation.Id; import java.util.ArrayList; import java.util.Date; import java.util.List; -enum SectionType { - // Do not rename or remove existring values. This may cause problems with already stored values in DB - number, chart, - NUMBER, CHART; -} - public class Section { @Id @JsonProperty("_id") @@ -21,7 +16,7 @@ public class Section { private String title; private String defaultId; private String stakeholderAlias; - private SectionType type; + private IndicatorType type; private Date creationDate; private Date updateDate; private List indicators; @@ -90,8 +85,7 @@ public class Section { if(type == null) { this.type = null; } else { - SectionType sectionType = SectionType.valueOf(type); - this.type = sectionType; + this.type = IndicatorType.valueOf(type); } } diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/entities/Stakeholder.java b/src/main/java/eu/dnetlib/uoamonitorservice/entities/Stakeholder.java index 089e459..4d72840 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/entities/Stakeholder.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/entities/Stakeholder.java @@ -1,32 +1,14 @@ package eu.dnetlib.uoamonitorservice.entities; import com.fasterxml.jackson.annotation.JsonProperty; +import eu.dnetlib.uoamonitorservice.primitives.Locale; +import eu.dnetlib.uoamonitorservice.primitives.StakeholderType; import org.springframework.data.annotation.Id; import java.util.Date; import java.util.List; -enum StakeholderType -{ - // Do not rename or remove existing values. This may cause problems with already stored values in DB - funder, ri, project, organization, - country, researcher, datasource, - FUNDER, RI, PROJECT, ORGANIZATION, - COUNTRY, RESEARCHER, DATASOURCE; -} - -enum Locale { - EN("en"), EU("eu"); - - public final String label; - - Locale(String label) { - this.label = label; - } -} - - public class Stakeholder { @Id @JsonProperty("_id") @@ -84,14 +66,6 @@ public class Stakeholder { this.id = id; } -// public StakeholderType getType() { -// return type; -// } -// -// public void setType(StakeholderType type) { -// this.type = type; -// } - public String getType() { if(type == null) { return null; diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/primitives/Format.java b/src/main/java/eu/dnetlib/uoamonitorservice/primitives/Format.java new file mode 100644 index 0000000..7e2a0f1 --- /dev/null +++ b/src/main/java/eu/dnetlib/uoamonitorservice/primitives/Format.java @@ -0,0 +1,5 @@ +package eu.dnetlib.uoamonitorservice.primitives; + +public enum Format { + NUMBER, PERCENTAGE +} diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/entities/IndicatorPath.java b/src/main/java/eu/dnetlib/uoamonitorservice/primitives/IndicatorPath.java similarity index 88% rename from src/main/java/eu/dnetlib/uoamonitorservice/entities/IndicatorPath.java rename to src/main/java/eu/dnetlib/uoamonitorservice/primitives/IndicatorPath.java index 63bc04a..60ed5f4 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/entities/IndicatorPath.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/primitives/IndicatorPath.java @@ -1,22 +1,8 @@ -package eu.dnetlib.uoamonitorservice.entities; +package eu.dnetlib.uoamonitorservice.primitives; import java.util.List; import java.util.Map; -enum IndicatorPathType { - // Do not rename or remove existring values. This may cause problems with already stored values in DB - table, bar, column, pie, line, other, - TABLE, BAR, COLUMN, PIE, LINE, OTHER; -} - -//enum SourceType { -// STATISTICS, SEARCH, METRICS, STATS_TOOL,OLD,IMAGE; -//} - -enum Format { - NUMBER, PERCENTAGE -} - public class IndicatorPath { private IndicatorPathType type; // for charts is type of chart {table, bar, column, etc} private Format format = Format.NUMBER; // for numbers is if number is percentage or not diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/primitives/IndicatorPathType.java b/src/main/java/eu/dnetlib/uoamonitorservice/primitives/IndicatorPathType.java new file mode 100644 index 0000000..72ef0ba --- /dev/null +++ b/src/main/java/eu/dnetlib/uoamonitorservice/primitives/IndicatorPathType.java @@ -0,0 +1,7 @@ +package eu.dnetlib.uoamonitorservice.primitives; + +public enum IndicatorPathType { + // Do not rename or remove existing values. This may cause problems with already stored values in DB + table, bar, column, pie, line, other, + TABLE, BAR, COLUMN, PIE, LINE, OTHER; +} diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/primitives/IndicatorSize.java b/src/main/java/eu/dnetlib/uoamonitorservice/primitives/IndicatorSize.java new file mode 100644 index 0000000..3228307 --- /dev/null +++ b/src/main/java/eu/dnetlib/uoamonitorservice/primitives/IndicatorSize.java @@ -0,0 +1,7 @@ +package eu.dnetlib.uoamonitorservice.primitives; + +public enum IndicatorSize { + // Do not rename or remove existring values. This may cause problems with already stored values in DB + small, medium, large, + SMALL, MEDIUM, LARGE; +} diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/primitives/IndicatorType.java b/src/main/java/eu/dnetlib/uoamonitorservice/primitives/IndicatorType.java new file mode 100644 index 0000000..1e5019f --- /dev/null +++ b/src/main/java/eu/dnetlib/uoamonitorservice/primitives/IndicatorType.java @@ -0,0 +1,7 @@ +package eu.dnetlib.uoamonitorservice.primitives; + +public enum IndicatorType { + // Do not rename or remove existing values. This may cause problems with already stored values in DB + number, chart, + NUMBER, CHART; +} diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/primitives/Locale.java b/src/main/java/eu/dnetlib/uoamonitorservice/primitives/Locale.java new file mode 100644 index 0000000..49adf2b --- /dev/null +++ b/src/main/java/eu/dnetlib/uoamonitorservice/primitives/Locale.java @@ -0,0 +1,11 @@ +package eu.dnetlib.uoamonitorservice.primitives; + +public enum Locale { + EN("en"), EU("eu"); + + public final String label; + + Locale(String label) { + this.label = label; + } +} diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/entities/ReorderEvent.java b/src/main/java/eu/dnetlib/uoamonitorservice/primitives/ReorderEvent.java similarity index 94% rename from src/main/java/eu/dnetlib/uoamonitorservice/entities/ReorderEvent.java rename to src/main/java/eu/dnetlib/uoamonitorservice/primitives/ReorderEvent.java index 9818c8f..caba8e8 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/entities/ReorderEvent.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/primitives/ReorderEvent.java @@ -1,4 +1,4 @@ -package eu.dnetlib.uoamonitorservice.entities; +package eu.dnetlib.uoamonitorservice.primitives; import java.util.List; diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/primitives/StakeholderType.java b/src/main/java/eu/dnetlib/uoamonitorservice/primitives/StakeholderType.java new file mode 100644 index 0000000..8e58b2b --- /dev/null +++ b/src/main/java/eu/dnetlib/uoamonitorservice/primitives/StakeholderType.java @@ -0,0 +1,9 @@ +package eu.dnetlib.uoamonitorservice.primitives; + +public enum StakeholderType { + // Do not rename or remove existing values. This may cause problems with already stored values in DB + funder, ri, project, organization, + country, researcher, datasource, + FUNDER, RI, PROJECT, ORGANIZATION, + COUNTRY, RESEARCHER, DATASOURCE; +} diff --git a/src/main/resources/monitorservice.properties b/src/main/resources/monitorservice.properties index 82627cb..86a31d4 100644 --- a/src/main/resources/monitorservice.properties +++ b/src/main/resources/monitorservice.properties @@ -24,32 +24,4 @@ monitorservice.globalVars.version=@version@ #production #monitorservice.userInfoUrl = https://services.openaire.eu/uoa-user-management/api/users/getUserInfo?accessToken= -#monitorservice.originServer = .openaire.eu - -# Monitor -monitorservice.mongodb.host=localhost -monitorservice.mongodb.port=27017 -monitorservice.mongodb.database=production - -# Notification -notification.mongodb.host = localhost -notification.mongodb.port = 27017 -notification.mongodb.database = openaire_notification - -# Admin Tools Library -admintoolslibrary.mail.from = openaire.test@gmail.com -admintoolslibrary.mail.username = openaire.test@gmail.com -admintoolslibrary.mail.password = koujreltgjduicjp -admintoolslibrary.mail.host = smtp.gmail.com -admintoolslibrary.mail.port = 587 -admintoolslibrary.mail.auth = true -admintoolslibrary.mail.sslProtocols = TLSv1.2 -admintoolslibrary.mail.defaultEncoding=UTF-8 -admintoolslibrary.mail.protocol=smtp -admintoolslibrary.mail.testConnection=false -admintoolslibrary.google.secret = 6LcVtFIUAAAAAIlEaz6Am2PBC3j5lHG7vBo6uW4_ - -# Authorization -authorization.security.userInfoUrl = http://mpagasas.di.uoa.gr:19080/login-service/userInfo - -server.port=8888 \ No newline at end of file +#monitorservice.originServer = .openaire.eu \ No newline at end of file From e345f5b689f02e8655b1abbf1092b34cf98f890a Mon Sep 17 00:00:00 2001 From: "k.triantafyllou" Date: Fri, 8 Mar 2024 20:55:51 +0200 Subject: [PATCH 04/11] [new-model | WIP]: Refactoring the code in order to improve performance and code. --- .../controllers/CategoryController.java | 11 +- .../controllers/IndicatorController.java | 11 +- .../controllers/SectionController.java | 108 +----- .../controllers/StakeholderController.java | 359 ++++-------------- .../controllers/SubCategoryController.java | 17 +- .../controllers/TopicController.java | 21 +- .../uoamonitorservice/dao/CategoryDAO.java | 5 +- .../uoamonitorservice/dao/IndicatorDAO.java | 3 +- .../uoamonitorservice/dao/SectionDAO.java | 3 +- .../uoamonitorservice/dao/StakeholderDAO.java | 7 +- .../uoamonitorservice/dao/SubCategoryDAO.java | 3 +- .../uoamonitorservice/dao/TopicDAO.java | 5 +- .../uoamonitorservice/dto/CategoryFull.java | 18 + .../uoamonitorservice/dto/SectionFull.java | 19 + .../dto/StakeholderFull.java | 15 + .../dto/SubCategoryFull.java | 18 + .../uoamonitorservice/dto/TopicFull.java | 18 + .../uoamonitorservice/entities/Category.java | 127 +------ .../uoamonitorservice/entities/Indicator.java | 43 +-- .../uoamonitorservice/entities/Section.java | 118 +----- .../entities/Stakeholder.java | 222 +---------- .../entities/SubCategory.java | 137 +------ .../uoamonitorservice/entities/Topic.java | 130 +------ .../entities/Visibility.java | 38 -- .../generics/CategoryGeneric.java | 88 +++++ .../uoamonitorservice/generics/Common.java | 42 ++ .../generics/SectionGeneric.java | 119 ++++++ .../generics/StakeholderGeneric.java | 193 ++++++++++ .../generics/SubCategoryGeneric.java | 107 ++++++ .../generics/TopicGeneric.java | 100 +++++ .../primitives/Visibility.java | 5 + .../service/CategoryService.java | 52 +++ .../service/CommonService.java | 37 ++ .../service/IndicatorService.java | 38 ++ .../service/SectionService.java | 71 ++++ .../service/StakeholderService.java | 59 ++- .../service/SubCategoryService.java | 55 +++ .../service/TopicService.java | 53 +++ 38 files changed, 1286 insertions(+), 1189 deletions(-) create mode 100644 src/main/java/eu/dnetlib/uoamonitorservice/dto/CategoryFull.java create mode 100644 src/main/java/eu/dnetlib/uoamonitorservice/dto/SectionFull.java create mode 100644 src/main/java/eu/dnetlib/uoamonitorservice/dto/StakeholderFull.java create mode 100644 src/main/java/eu/dnetlib/uoamonitorservice/dto/SubCategoryFull.java create mode 100644 src/main/java/eu/dnetlib/uoamonitorservice/dto/TopicFull.java delete mode 100644 src/main/java/eu/dnetlib/uoamonitorservice/entities/Visibility.java create mode 100644 src/main/java/eu/dnetlib/uoamonitorservice/generics/CategoryGeneric.java create mode 100644 src/main/java/eu/dnetlib/uoamonitorservice/generics/Common.java create mode 100644 src/main/java/eu/dnetlib/uoamonitorservice/generics/SectionGeneric.java create mode 100644 src/main/java/eu/dnetlib/uoamonitorservice/generics/StakeholderGeneric.java create mode 100644 src/main/java/eu/dnetlib/uoamonitorservice/generics/SubCategoryGeneric.java create mode 100644 src/main/java/eu/dnetlib/uoamonitorservice/generics/TopicGeneric.java create mode 100644 src/main/java/eu/dnetlib/uoamonitorservice/primitives/Visibility.java create mode 100644 src/main/java/eu/dnetlib/uoamonitorservice/service/CategoryService.java create mode 100644 src/main/java/eu/dnetlib/uoamonitorservice/service/CommonService.java create mode 100644 src/main/java/eu/dnetlib/uoamonitorservice/service/IndicatorService.java create mode 100644 src/main/java/eu/dnetlib/uoamonitorservice/service/SectionService.java create mode 100644 src/main/java/eu/dnetlib/uoamonitorservice/service/SubCategoryService.java create mode 100644 src/main/java/eu/dnetlib/uoamonitorservice/service/TopicService.java diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/CategoryController.java b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/CategoryController.java index ea5110e..cc91eab 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/CategoryController.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/CategoryController.java @@ -1,3 +1,4 @@ +/* package eu.dnetlib.uoamonitorservice.controllers; import eu.dnetlib.uoaadmintoolslibrary.handlers.ForbiddenException; @@ -9,6 +10,7 @@ import eu.dnetlib.uoamonitorservice.dao.TopicDAO; import eu.dnetlib.uoamonitorservice.entities.*; import eu.dnetlib.uoamonitorservice.handlers.EntityNotFoundException; import eu.dnetlib.uoamonitorservice.handlers.PathNotValidException; +import eu.dnetlib.uoamonitorservice.primitives.Visibility; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; @@ -77,7 +79,7 @@ public class CategoryController { log.debug("save category"); log.debug("Alias: " + categoryFull.getAlias() + " - Id: " + categoryFull.getId() + " - Stakeholder: " + stakeholderId + " - Topic: " + topicId); - Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId); + Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId); if (stakeholder != null) { if (!rolesUtils.hasUpdateAuthority(stakeholder.getType(), stakeholder.getAlias())) { @@ -227,7 +229,7 @@ public class CategoryController { log.debug("delete category"); log.debug("Id: " + categoryId + " - Stakeholder: " + stakeholderId + " - Topic: " + topicId); - Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId); + Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId); if (stakeholder != null) { if (!rolesUtils.hasUpdateAuthority(stakeholder.getType(), stakeholder.getAlias())) { @@ -370,7 +372,7 @@ public class CategoryController { log.debug("change category visibility: " + visibility + " - toggle propagate: " + ((propagate != null && propagate) ? "true" : "false")); log.debug("Stakeholder: " + stakeholderId + " - Topic: " + topicId + " - Category: " + categoryId); - Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId); + Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId); if (stakeholder != null) { if (!rolesUtils.hasUpdateAuthority(stakeholder.getType(), stakeholder.getAlias())) { @@ -430,7 +432,7 @@ public class CategoryController { private Topic checkForExceptions(String stakeholderId, String topicId) { - Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId); + Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId); if (stakeholder == null) { // EXCEPTION - Stakeholder not found @@ -486,3 +488,4 @@ public class CategoryController { } } } +*/ diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/IndicatorController.java b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/IndicatorController.java index f8746a5..d1fc52b 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/IndicatorController.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/IndicatorController.java @@ -1,3 +1,4 @@ +/* package eu.dnetlib.uoamonitorservice.controllers; @@ -9,6 +10,7 @@ import eu.dnetlib.uoamonitorservice.handlers.EntityNotFoundException; import eu.dnetlib.uoamonitorservice.handlers.PathNotValidException; import eu.dnetlib.uoamonitorservice.primitives.IndicatorPath; import eu.dnetlib.uoamonitorservice.primitives.ReorderEvent; +import eu.dnetlib.uoamonitorservice.primitives.Visibility; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; @@ -60,7 +62,7 @@ public class IndicatorController { log.debug("save bulk indicators"); log.debug("Stakeholder: " + stakeholderId + " - Topic: " + topicId + " - Category: " + categoryId + " - SubCategory: " + subcategoryId); - Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId); + Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId); Date date = new Date(); @@ -171,7 +173,7 @@ public class IndicatorController { String indicatorId = indicator.getId(); - Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId); + Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId); // this indicator belongs in default profile and it is new or it is updated if (stakeholder.getDefaultId() == null) { if (indicatorId == null) { @@ -477,7 +479,7 @@ public class IndicatorController { if (indicator != null) { Section section = checkForExceptions(stakeholderId, topicId, categoryId, subcategoryId, sectionId, indicator.getType()); - Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId); + Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId); if (indicator.getDefaultId() != null && !rolesUtils.hasCreateAndDeleteAuthority(stakeholder.getType())) { // EXCEPTION - Access denied throw new ForbiddenException("Delete indicator: You are not authorized to delete a default Indicator in stakeholder with id: " + stakeholderId); @@ -633,7 +635,7 @@ public class IndicatorController { private Section checkForExceptions(String stakeholderId, String topicId, String categoryId, String subcategoryId, String sectionId, String indicatorType) { - Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId); + Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId); if (stakeholder == null) { // EXCEPTION - Stakeholder not found @@ -714,3 +716,4 @@ public class IndicatorController { } } } +*/ diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/SectionController.java b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/SectionController.java index 90b47b0..05720fa 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/SectionController.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/SectionController.java @@ -1,11 +1,16 @@ +/* package eu.dnetlib.uoamonitorservice.controllers; import eu.dnetlib.uoaadmintoolslibrary.handlers.utils.RolesUtils; import eu.dnetlib.uoamonitorservice.dao.*; +import eu.dnetlib.uoamonitorservice.dto.SectionFull; import eu.dnetlib.uoamonitorservice.entities.*; import eu.dnetlib.uoamonitorservice.handlers.EntityNotFoundException; import eu.dnetlib.uoaadmintoolslibrary.handlers.ForbiddenException; import eu.dnetlib.uoamonitorservice.handlers.PathNotValidException; +import eu.dnetlib.uoamonitorservice.primitives.Visibility; +import eu.dnetlib.uoamonitorservice.service.SectionService; +import eu.dnetlib.uoamonitorservice.service.SubCategoryService; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; @@ -14,7 +19,6 @@ import org.springframework.web.bind.annotation.*; import java.util.ArrayList; import java.util.Date; -import java.util.Iterator; import java.util.List; @RestController @@ -22,71 +26,22 @@ import java.util.List; public class SectionController { private final Logger log = LogManager.getLogger(this.getClass()); - @Autowired - private RolesUtils rolesUtils; - - @Autowired - private StakeholderDAO stakeholderDAO; - - @Autowired - private TopicDAO topicDAO; - - @Autowired - private CategoryDAO categoryDAO; - - @Autowired - private SubCategoryDAO subCategoryDAO; - - @Autowired - private SectionDAO sectionDAO; - - @Autowired - private IndicatorDAO indicatorDAO; - - @Autowired - private IndicatorController indicatorController; - - public Section buildSection(Section sectionFull) { - Section section = new Section<>(sectionFull); - - List indicators = new ArrayList<>(); - List indicatorsFull = new ArrayList<>(); - for(Indicator chart : sectionFull.getIndicators()) { - Indicator chartSaved = indicatorDAO.save(chart); - chart.setId(chartSaved.getId()); - indicatorsFull.add(chart); - indicators.add(chartSaved.getId()); - } - sectionFull.setIndicators(indicatorsFull); - section.setIndicators(indicators); - - Date date = new Date(); - section.setCreationDate(date); - section.setUpdateDate(date); - - sectionFull.setCreationDate(date); - sectionFull.setUpdateDate(date); - - sectionDAO.save(section); - - sectionFull.setId(section.getId()); - return sectionFull; - } + private SectionService service; @PreAuthorize("isAuthenticated()") @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/save/{index}", method = RequestMethod.POST) - public Section saveSection(@PathVariable("stakeholderId") String stakeholderId, - @PathVariable("topicId") String topicId, - @PathVariable("categoryId") String categoryId, - @PathVariable("subcategoryId") String subcategoryId, - @PathVariable("index") String index, - @RequestBody Section sectionFull) { + public SectionFull saveSection(@PathVariable("stakeholderId") String stakeholderId, + @PathVariable("topicId") String topicId, + @PathVariable("categoryId") String categoryId, + @PathVariable("subcategoryId") String subcategoryId, + @PathVariable("index") String index, + @RequestBody SectionFull sectionFull) { log.debug("save section"); log.debug("Name: "+sectionFull.getTitle() + " - Id: "+sectionFull.getId() + " - Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId+ " - SubCategory: "+subcategoryId); - SubCategory subCategory = checkForExceptions(stakeholderId, topicId, categoryId, subcategoryId); + SubCategory subCategory = this.subCategoryService.checkForExceptions(stakeholderId, topicId, categoryId, subcategoryId, "Save Section"); - Section section = new Section<>(sectionFull); + Section section = new Section(sectionFull); Date date = new Date(); section.setUpdateDate(date); @@ -123,7 +78,7 @@ public class SectionController { section.setIndicators(indicators); - Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId); + Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId); // this section belongs in default profile and it is new or it is updated if(stakeholder.getDefaultId() == null) { if(sectionId == null) { @@ -200,23 +155,13 @@ public class SectionController { private SubCategory checkForExceptions(String stakeholderId, String topicId, String categoryId, String subcategoryId) { - Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId); - - if(stakeholder == null) { - // EXCEPTION - Stakeholder not found - throw new EntityNotFoundException("Save indicator: Stakeholder with id: " + stakeholderId + " not found"); - } + Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId).orElseThrow(() -> new EntityNotFoundException("Save indicator: Stakeholder with id: " + stakeholderId + " not found")); if(!rolesUtils.hasUpdateAuthority(stakeholder.getType(), stakeholder.getAlias())) { // EXCEPTION - Access denied throw new ForbiddenException("CheckForExceptions Section: You are not authorized to update stakeholder with id: "+stakeholderId); } - Topic topic = topicDAO.findById(topicId); - if(topic == null) { - // EXCEPTION - Topic not found - throw new EntityNotFoundException("Save indicator: Topic with id: "+topicId+" not found"); - } - + Topic topic = topicDAO.findById(topicId).orElseThrow(() -> new EntityNotFoundException("Save indicator: Topic with id: "+topicId+" not found")); if(!stakeholder.getTopics().contains(topicId)) { // EXCEPTION - Topic not found in Stakeholder: stakeholder.getAlias(); throw new PathNotValidException("Save indicator: Topic with id: " + topicId + " not found in Stakeholder: " + stakeholderId); @@ -304,22 +249,5 @@ public class SectionController { sectionDAO.save(section); } } - - public Section changeVisibilityTree(String sectionId, Visibility visibility) { - Section section = sectionDAO.findById(sectionId); - if (section == null) { - // EXCEPTION - Section not found - throw new EntityNotFoundException("Change section visibility: Section with id: " + sectionId + " not found"); - } - - Section sectionFull = new Section(section); - List indicatorsFull = new ArrayList(); - - for (String indicatorId : section.getIndicators()) { - indicatorsFull.add(indicatorController.changeVisibilityTree(indicatorId, null, visibility)); - } - - sectionFull.setIndicators(indicatorsFull); - return sectionFull; - } } +*/ diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/StakeholderController.java b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/StakeholderController.java index 8e2a33f..cb1f8ba 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/StakeholderController.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/StakeholderController.java @@ -1,57 +1,33 @@ package eu.dnetlib.uoamonitorservice.controllers; -import eu.dnetlib.uoaadmintoolslibrary.entities.Portal; -import eu.dnetlib.uoaadmintoolslibrary.handlers.utils.RolesUtils; -import eu.dnetlib.uoaadmintoolslibrary.services.PortalService; -import eu.dnetlib.uoamonitorservice.dao.*; -import eu.dnetlib.uoamonitorservice.entities.*; -import eu.dnetlib.uoamonitorservice.handlers.EntityNotFoundException; import eu.dnetlib.uoaadmintoolslibrary.handlers.ForbiddenException; +import eu.dnetlib.uoaadmintoolslibrary.services.PortalService; +import eu.dnetlib.uoamonitorservice.dto.StakeholderFull; +import eu.dnetlib.uoamonitorservice.entities.Stakeholder; import eu.dnetlib.uoamonitorservice.service.StakeholderService; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; - import org.springframework.security.access.AccessDeniedException; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.web.bind.annotation.*; -import java.util.*; +import java.util.List; +import java.util.stream.Collectors; @RestController @CrossOrigin(origins = "*") public class StakeholderController { private final Logger log = LogManager.getLogger(this.getClass()); - @Autowired - private RolesUtils rolesUtils; - - @Autowired + private PortalService portalService; private StakeholderService stakeholderService; @Autowired - private StakeholderDAO stakeholderDAO; - - @Autowired - private TopicDAO topicDAO; - - @Autowired - private CategoryDAO categoryDAO; - - @Autowired - private SubCategoryDAO subCategoryDAO; - - @Autowired - private SectionDAO sectionDAO; - - @Autowired - private IndicatorDAO indicatorDAO; - - @Autowired - private TopicController topicController; - - @Autowired - private PortalService portalService; + public StakeholderController(PortalService portalService, StakeholderService stakeholderService) { + this.portalService = portalService; + this.stakeholderService = stakeholderService; + } @PreAuthorize("isAuthenticated()") @RequestMapping(value = "/stakeholder/alias", method = RequestMethod.GET) @@ -63,221 +39,46 @@ public class StakeholderController { return stakeholderAlias; } -// @PreAuthorize("isAuthenticated()") - @PreAuthorize("hasAnyAuthority(" + - "@AuthorizationService.PORTAL_ADMIN, " + - "@AuthorizationService.curator(#stakeholderFull.getType()))") + /* @PreAuthorize("hasAnyAuthority(" + + "@AuthorizationService.PORTAL_ADMIN, " + + "@AuthorizationService.curator(#stakeholderFull.getType()))") @RequestMapping(value = "/build-stakeholder", method = RequestMethod.POST) - public Stakeholder>>>> buildFullStakeholder(@RequestBody Stakeholder>>>> stakeholderFull) { + public StakeholderFull buildFullStakeholder(@RequestBody StakeholderFull stakeholderFull) { log.debug("build stakeholder"); - log.debug("Alias: "+stakeholderFull.getAlias()); - - Stakeholder stakeholder = new Stakeholder<>(stakeholderFull); - - List topics = new ArrayList<>(); - List>>>> topicsFull = new ArrayList<>(); - for(Topic topic : stakeholderFull.getTopics()) { - Topic>>> topicFull = topicController.buildTopic(topic); - topicsFull.add(topicFull); - topics.add(topicFull.getId()); - } - stakeholderFull.setTopics(topicsFull); - stakeholder.setTopics(topics); - + log.debug("Alias: " + stakeholderFull.getAlias()); + Stakeholder stakeholder = new Stakeholder(stakeholderFull); + stakeholderFull.setTopics(stakeholderFull.getTopics().stream().map(topic -> topicController.buildTopic(topic))); + stakeholder.setTopics(stakeholderFull.getTopics().stream().map(TopicFull::getId).collect(Collectors.toList())); Date date = new Date(); stakeholder.setCreationDate(date); stakeholder.setUpdateDate(date); - stakeholderFull.setCreationDate(date); stakeholderFull.setUpdateDate(date); - - Stakeholder stakeholderSaved = stakeholderDAO.save(stakeholder); - stakeholderFull.setId(stakeholderSaved.getId()); - + stakeholder = stakeholderDAO.save(stakeholder); + stakeholderFull.setId(stakeholder.getId()); Portal portal = portalService.getPortal(stakeholderFull.getAlias()); - if(portal == null) { + if (portal == null) { portal = new Portal(); portal.setPid(stakeholderFull.getAlias()); portal.setName(stakeholderFull.getName()); portal.setType(stakeholderFull.getType()); portalService.insertPortal(portal); } - return stakeholderFull; - //return null; - } + }*/ - public Stakeholder setFullEntities(Stakeholder stakeholder) { - boolean addAll = false; - boolean addPublicAndRestricted = false; - - if(rolesUtils.hasUpdateAuthority(stakeholder.getType(), stakeholder.getAlias())) { - addAll = true; - addPublicAndRestricted = true; - } else if(rolesUtils.isMember(stakeholder.getType(), stakeholder.getAlias())) { - addPublicAndRestricted = true; - } - - Stakeholder stakeholderFull = new Stakeholder<>(stakeholder); - - List topics = new ArrayList<>(); - - for (String topicId: (List)stakeholder.getTopics()) { - Topic topic = topicDAO.findById(topicId); - if(topic == null) { - // EXCEPTION - Topic not found - throw new EntityNotFoundException("Get stakeholder: Topic with id: "+topicId+" not found (topic exists in stakeholder: "+stakeholder.getId()+")"); - } - - if((!addAll && topic.getVisibility() == Visibility.PRIVATE) - || (!addPublicAndRestricted && topic.getVisibility() == Visibility.RESTRICTED)) { - continue; - } - - Topic topicFull = new Topic(topic); - - List categories = new ArrayList<>(); - - for(String categoryId : topic.getCategories()) { - Category category = categoryDAO.findById(categoryId); - if(category == null) { - // EXCEPTION - Category not found - throw new EntityNotFoundException("Get stakeholder: Category with id: "+categoryId+" not found (category exists in topic: "+topicId+")"); - } - - if((!addAll && category.getVisibility() == Visibility.PRIVATE) - || (!addPublicAndRestricted && category.getVisibility() == Visibility.RESTRICTED)) { - continue; - } - - Category categoryFull = new Category(category); - - List subCategories = new ArrayList<>(); - - for(String subCategoryId : category.getSubCategories()) { - SubCategory subCategory = subCategoryDAO.findById(subCategoryId); - if(subCategory == null) { - // EXCEPTION - SubCategory not found - throw new EntityNotFoundException("Get stakeholder: SubCategory with id: "+subCategoryId+" not found (subCategory exists in category: "+categoryId+")"); - } - - if((!addAll && subCategory.getVisibility() == Visibility.PRIVATE) - || (!addPublicAndRestricted && subCategory.getVisibility() == Visibility.RESTRICTED)) { - continue; - } - - SubCategory subCategoryFull = new SubCategory>(subCategory); - - List
sectionsCharts = new ArrayList<>(); - - for(String sectionId : subCategory.getCharts()) { - sectionsCharts.add(getSectionFull(sectionId, subCategoryId, addAll, addPublicAndRestricted)); - } - subCategoryFull.setCharts(sectionsCharts); - - List
sectionsNumbers = new ArrayList<>(); - - for(String sectionId : subCategory.getNumbers()) { - sectionsNumbers.add(getSectionFull(sectionId, subCategoryId, addAll, addPublicAndRestricted)); - } - subCategoryFull.setNumbers(sectionsNumbers); - subCategories.add(subCategoryFull); - } - categoryFull.setSubCategories(subCategories); - categories.add(categoryFull); - } - topicFull.setCategories(categories); - topics.add(topicFull); - } - stakeholderFull.setTopics(topics); - return stakeholderFull; - } - - private Section getSectionFull(String sectionId, String subCategoryId, boolean addAll, boolean addPublicAndRestricted) { - Section section = sectionDAO.findById(sectionId); - if (section == null) { - // EXCEPTION - Section not found - throw new EntityNotFoundException("Get stakeholder: Section with id: " + sectionId + " not found (section exists in subCategory: " + subCategoryId + ")"); - } - - Section sectionFull = new Section(section); - - List indicators = new ArrayList<>(); - for (String indicatorId : section.getIndicators()) { - Indicator indicator = indicatorDAO.findById(indicatorId); - if (indicator == null) { - // EXCEPTION - Indicator not found - throw new EntityNotFoundException("Get stakeholder: Indicator with id: " + indicatorId + " not found (indicator exists in section: " + sectionId + ")"); - } - - if((!addAll && indicator.getVisibility() == Visibility.PRIVATE) - || (!addPublicAndRestricted && indicator.getVisibility() == Visibility.RESTRICTED)) { - continue; - } - - indicators.add(indicator); - } - sectionFull.setIndicators(indicators); - - return sectionFull; - } - - @PreAuthorize("hasAnyAuthority(" + - "@AuthorizationService.PORTAL_ADMIN)") + @PreAuthorize("hasAnyAuthority(" + "@AuthorizationService.PORTAL_ADMIN)") @RequestMapping(value = "/stakeholder/all", method = RequestMethod.GET) public List getAllStakeholders(@RequestParam(required = false) String type) { -// log.debug("get all stakeholders" + (type != null ? " with type: "+type : "")); - - List stakeholders; - if(type == null) { - stakeholders = stakeholderDAO.findAll(); - } else { - stakeholders = stakeholderDAO.findByType(type); - } - - List stakeholdersFull = new ArrayList<>(); - for(Stakeholder stakeholder : stakeholders) { - stakeholdersFull.add(this.setFullEntities(stakeholder)); - } - - return stakeholdersFull; + return this.stakeholderService.getAll(type); } @PreAuthorize("isAuthenticated()") @RequestMapping(value = "/stakeholder/default", method = RequestMethod.GET) - public List getAllDefaultStakeholders(@RequestParam(required = false) String type) { -// log.debug("get all default stakeholders" + (type != null ? " with type: "+type : "")); - - List stakeholders; - if(type == null) { - stakeholders = stakeholderDAO.findByDefaultId(null); - } else { - stakeholders = stakeholderDAO.findByDefaultIdAndType(null, type); - } - - List stakeholdersFull = new ArrayList<>(); - - // Remove stakeholders for which i do not have authority - if(stakeholders != null && stakeholders.size() > 0) { - if (rolesUtils.isPortalAdmin()) { - for(Stakeholder stakeholder : stakeholders) { - stakeholdersFull.add(this.setFullEntities(stakeholder)); - } - return stakeholdersFull; - } - - Iterator stakeholderIterator = stakeholders.iterator(); - while(stakeholderIterator.hasNext()) { - Stakeholder stakeholder = stakeholderIterator.next(); - if(rolesUtils.isCurator(stakeholder.getType())) { - stakeholdersFull.add(this.setFullEntities(stakeholder)); - continue; - } - stakeholderIterator.remove(); - } - } - - return stakeholdersFull; + public List getAllDefaultStakeholders(@RequestParam(required = false) String type) { + return this.stakeholderService.getAllDefaultByRole(type).stream() + .map(stakeholder -> this.stakeholderService.getFullStakeholder(stakeholder)) + .collect(Collectors.toList()); } @RequestMapping(value = "/stakeholder", method = RequestMethod.GET) @@ -293,126 +94,98 @@ public class StakeholderController { } @RequestMapping(value = "/stakeholder/{alias:.+}", method = RequestMethod.GET) - public Stakeholder getStakeholder(@PathVariable("alias") String alias) { - - Stakeholder stakeholder = stakeholderDAO.findByAlias(alias); - if(stakeholder == null) { - // EXCEPTION - Stakeholder not found - throw new EntityNotFoundException("Get stakeholder: Stakeholder with alias: "+alias+" not found"); + public StakeholderFull getStakeholder(@PathVariable("alias") String alias) { + StakeholderFull stakeholder = this.stakeholderService.getFullStakeholder(this.stakeholderService.findByAlias(alias)); + if (stakeholder == null) { + throw new ForbiddenException("Get stakeholder: You are not authorized to access stakeholder with alias: " + alias); } - - if(stakeholder.getDefaultId() == null && !rolesUtils.isLoggedIn()) { - // EXCEPTION - Unauthorized - throw new AccessDeniedException("Get stakeholder: You are not authorized (not logged in) to access stakeholder with alias: "+alias); - } - if(stakeholder.getDefaultId() == null && !rolesUtils.hasCreateAndDeleteAuthority(stakeholder.getType())) { - // EXCEPTION - Access denied - throw new ForbiddenException("Get stakeholder: You are not authorized to access stakeholder with alias: "+alias); - } - - if((stakeholder.getVisibility() == Visibility.PRIVATE && !rolesUtils.hasUpdateAuthority(stakeholder.getType(), stakeholder.getAlias()) - || (stakeholder.getVisibility() == Visibility.RESTRICTED && !rolesUtils.hasUpdateAuthority(stakeholder.getType(), stakeholder.getAlias()) && !rolesUtils.isMember(stakeholder.getType(), stakeholder.getAlias())))) { -// // EXCEPTION - Access denied - List topicsEmpty = stakeholder.getTopics(); - topicsEmpty.clear(); - stakeholder.setTopics(topicsEmpty); - stakeholder.setVisibility(Visibility.PRIVATE); - return stakeholder; - } - - return this.setFullEntities(stakeholder); + return stakeholder; } - @PreAuthorize("hasAnyAuthority(" - + "@AuthorizationService.PORTAL_ADMIN, " - + "@AuthorizationService.curator(#_stakeholder.getType()), " - + "@AuthorizationService.manager(#_stakeholder.getType(), #_stakeholder.getAlias()) " - + ")") + /*@PreAuthorize("hasAnyAuthority(" + + "@AuthorizationService.PORTAL_ADMIN, " + + "@AuthorizationService.curator(#_stakeholder.getType()), " + + "@AuthorizationService.manager(#_stakeholder.getType(), #_stakeholder.getAlias()) " + + ")") @RequestMapping(value = "/save", method = RequestMethod.POST) - public Stakeholder saveStakeholder(@RequestBody Stakeholder _stakeholder) { + public Stakeholder saveStakeholder(@RequestBody Stakeholder stakeholder) { log.debug("save stakeholder"); - log.debug("Alias: "+_stakeholder.getAlias() + " - Id: "+_stakeholder.getId()); - Stakeholder stakeholder = new Stakeholder<>(_stakeholder); - + log.debug("Alias: " + stakeholder.getAlias() + " - Id: " + stakeholder.getId()); Date date = new Date(); stakeholder.setUpdateDate(date); List topics = new ArrayList<>(); - if(_stakeholder.getId() == null) { + if (stakeholder.getId() == null) { stakeholder.setCreationDate(date); } else { - Stakeholder oldStakeholder = stakeholderDAO.findById(_stakeholder.getId()); - if(oldStakeholder == null) { + Stakeholder oldStakeholder = stakeholderDAO.findById(stakeholder.getId()); + if (oldStakeholder == null) { // EXCEPTION - Stakeholder not found - throw new EntityNotFoundException("save stakeholder: Stakeholder with id: "+_stakeholder.getId()+" not found"); + throw new EntityNotFoundException("save stakeholder: Stakeholder with id: " + stakeholder.getId() + " not found"); } - for(String topicId : oldStakeholder.getTopics()) { + for (String topicId : oldStakeholder.getTopics()) { Topic topic = topicDAO.findById(topicId); if (topic == null) { // EXCEPTION - Topic not found - throw new EntityNotFoundException("Save stakeholder: Topic with id: "+topicId+" not found (topic exists in stakeholder: "+stakeholder.getId()+")"); + throw new EntityNotFoundException("Save stakeholder: Topic with id: " + topicId + " not found (topic exists in stakeholder: " + stakeholder.getId() + ")"); } topics.add(topic.getId()); } } stakeholder.setTopics(topics); - Stakeholder stakeholderSaved = stakeholderDAO.save(stakeholder); - _stakeholder.setId(stakeholderSaved.getId()); - _stakeholder.setCreationDate(stakeholderSaved.getCreationDate()); - _stakeholder.setUpdateDate(stakeholderSaved.getUpdateDate()); - return _stakeholder; - } + return stakeholderDAO.save(stakeholder); + }*/ - @PreAuthorize("isAuthenticated()") + /* @PreAuthorize("isAuthenticated()") @RequestMapping(value = "/{stakeholderId}/delete", method = RequestMethod.DELETE) public boolean deleteStakeholder(@PathVariable("stakeholderId") String stakeholderId) { log.debug("delete stakeholder"); - log.debug("Id: "+stakeholderId); + log.debug("Id: " + stakeholderId); - Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId); + Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId); String pid = null; - if(stakeholder != null) { + if (stakeholder != null) { pid = stakeholder.getAlias(); - if(!rolesUtils.hasCreateAndDeleteAuthority(stakeholder.getType())) { + if (!rolesUtils.hasCreateAndDeleteAuthority(stakeholder.getType())) { // EXCEPTION - Access denied - throw new ForbiddenException("Delete stakeholder: You are not authorized to delete stakeholder with id: "+stakeholderId); + throw new ForbiddenException("Delete stakeholder: You are not authorized to delete stakeholder with id: " + stakeholderId); } topicController.deleteTree(stakeholder); stakeholder.setTopics(null); stakeholderDAO.delete(stakeholderId); log.debug("Stakeholder deleted!"); Portal portal = portalService.getPortal(pid); - if(portal != null) { + if (portal != null) { portalService.deletePortal(portal.getId()); } } else { // EXCEPTION - Stakeholder not found - throw new EntityNotFoundException("Delete stakeholder: Stakeholder with id: "+stakeholderId+" not found"); + throw new EntityNotFoundException("Delete stakeholder: Stakeholder with id: " + stakeholderId + " not found"); } return true; - } + }*/ - @PreAuthorize("isAuthenticated()") + /*@PreAuthorize("isAuthenticated()") @RequestMapping(value = "/{stakeholderId}/change-visibility", method = RequestMethod.POST) public Stakeholder changeStakeholderVisibility(@PathVariable("stakeholderId") String stakeholderId, - @RequestParam("visibility") Visibility visibility, @RequestParam(required = false) Boolean propagate) { - log.debug("change stakeholder visibility: "+visibility + " - toggle propagate: "+((propagate != null && propagate) ? "true" : "false")); - log.debug("Stakeholder: "+stakeholderId); + @RequestParam("visibility") Visibility visibility, @RequestParam(required = false) Boolean propagate) { + log.debug("change stakeholder visibility: " + visibility + " - toggle propagate: " + ((propagate != null && propagate) ? "true" : "false")); + log.debug("Stakeholder: " + stakeholderId); Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId); if (stakeholder == null) { // EXCEPTION - Stakeholder not found - throw new EntityNotFoundException("Change stakeholder visibility: Stakeholder with id: "+stakeholderId+" not found"); + throw new EntityNotFoundException("Change stakeholder visibility: Stakeholder with id: " + stakeholderId + " not found"); } - if(!rolesUtils.hasUpdateAuthority(stakeholder.getType(), stakeholder.getAlias())) { + if (!rolesUtils.hasUpdateAuthority(stakeholder.getType(), stakeholder.getAlias())) { // EXCEPTION - Access denied - throw new ForbiddenException("Change stakeholder visibility: You are not authorized to update stakeholder with id: "+stakeholderId); + throw new ForbiddenException("Change stakeholder visibility: You are not authorized to update stakeholder with id: " + stakeholderId); } return changeStakeholderVisibilityTree(stakeholder, visibility, propagate); } - private Stakeholder changeStakeholderVisibilityTree(Stakeholder stakeholder, Visibility visibility, Boolean propagate) { + private Stakeholder changeStakeholderVisibilityTree(Stakeholder stakeholder, Visibility visibility, Boolean propagate) { Stakeholder stakeholderFull = new Stakeholder<>(stakeholder); List topicsFull = new ArrayList<>(); @@ -430,5 +203,5 @@ public class StakeholderController { stakeholderFull.setTopics(topicsFull); return stakeholder; - } + }*/ } diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/SubCategoryController.java b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/SubCategoryController.java index aaa499b..7d86bb6 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/SubCategoryController.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/SubCategoryController.java @@ -1,3 +1,4 @@ +/* package eu.dnetlib.uoamonitorservice.controllers; import eu.dnetlib.uoaadmintoolslibrary.handlers.utils.RolesUtils; @@ -6,6 +7,7 @@ import eu.dnetlib.uoamonitorservice.entities.*; import eu.dnetlib.uoamonitorservice.handlers.EntityNotFoundException; import eu.dnetlib.uoaadmintoolslibrary.handlers.ForbiddenException; import eu.dnetlib.uoamonitorservice.handlers.PathNotValidException; +import eu.dnetlib.uoamonitorservice.primitives.Visibility; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; @@ -145,7 +147,7 @@ public class SubCategoryController { subCategory.setCharts(chartSections); subCategory.setNumbers(numberSections); - Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId); + Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId); if(stakeholder.getDefaultId() == null) { if(subcategoryFull.getId() == null) { subCategoryDAO.save(subCategory); @@ -230,7 +232,7 @@ public class SubCategoryController { SubCategory subcategory = subCategoryDAO.findById(subcategoryId); if(subcategory != null) { - Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId); + Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId); if(subcategory.getDefaultId() != null && !rolesUtils.hasCreateAndDeleteAuthority(stakeholder.getType())) { // EXCEPTION - Access denied throw new ForbiddenException("Delete subcategory: You are not authorized to delete a default SubCategory in stakeholder with id: "+stakeholderId); @@ -344,10 +346,10 @@ public class SubCategoryController { @PreAuthorize("isAuthenticated()") @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/change-visibility", method = RequestMethod.POST) public SubCategory changeSubCategoryVisibility(@PathVariable("stakeholderId") String stakeholderId, - @PathVariable("topicId") String topicId, - @PathVariable("categoryId") String categoryId, - @PathVariable("subcategoryId") String subcategoryId, - @RequestParam("visibility") Visibility visibility, @RequestParam(required = false) Boolean propagate) { + @PathVariable("topicId") String topicId, + @PathVariable("categoryId") String categoryId, + @PathVariable("subcategoryId") String subcategoryId, + @RequestParam("visibility") Visibility visibility, @RequestParam(required = false) Boolean propagate) { log.debug("change subCategory visibility: "+visibility + " - toggle propagate: "+((propagate != null && propagate) ? "true" : "false")); log.debug("Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId+ " - SubCategory: "+subcategoryId); @@ -398,7 +400,7 @@ public class SubCategoryController { private Category checkForExceptions(String stakeholderId, String topicId, String categoryId) { - Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId); + Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId); if(stakeholder == null) { // EXCEPTION - Stakeholder not found @@ -464,3 +466,4 @@ public class SubCategoryController { } } } +*/ diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/TopicController.java b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/TopicController.java index de3c423..4099a67 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/TopicController.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/TopicController.java @@ -1,11 +1,14 @@ +/* package eu.dnetlib.uoamonitorservice.controllers; import eu.dnetlib.uoaadmintoolslibrary.handlers.utils.RolesUtils; import eu.dnetlib.uoamonitorservice.dao.*; +import eu.dnetlib.uoamonitorservice.dto.TopicFull; import eu.dnetlib.uoamonitorservice.entities.*; import eu.dnetlib.uoamonitorservice.handlers.EntityNotFoundException; import eu.dnetlib.uoaadmintoolslibrary.handlers.ForbiddenException; import eu.dnetlib.uoamonitorservice.handlers.PathNotValidException; +import eu.dnetlib.uoamonitorservice.primitives.Visibility; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; @@ -37,9 +40,8 @@ public class TopicController { @Autowired private CategoryDAO categoryDAO; - public Topic buildTopic(Topic topicFull) { - Topic topic = new Topic<>(topicFull); - + public TopicFull buildTopic(TopicFull topicFull) { + Topic topic = new Topic(topicFull); List categories = new ArrayList<>(); List categoriesFull = new ArrayList<>(); for(Category category : topicFull.getCategories()) { @@ -70,7 +72,7 @@ public class TopicController { log.debug("save topic"); log.debug("Alias: "+topicFull.getAlias() + " - Id: "+topicFull.getId()+ " - Stakeholder: "+stakeholderId); - Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId); + Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId); if(stakeholder != null) { if(!rolesUtils.hasUpdateAuthority(stakeholder.getType(), stakeholder.getAlias())) { @@ -198,7 +200,7 @@ public class TopicController { log.debug("delete topic"); log.debug("Id: "+topicId + " - Stakeholder: "+stakeholderId); - Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId); + Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId); if(stakeholder != null) { @@ -296,7 +298,7 @@ public class TopicController { log.debug("reorder topics"); log.debug("Stakeholder: "+stakeholderId); - Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId); + Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId); if(stakeholder != null) { if(!rolesUtils.hasUpdateAuthority(stakeholder.getType(), stakeholder.getAlias())) { @@ -335,12 +337,12 @@ public class TopicController { @PreAuthorize("isAuthenticated()") @RequestMapping(value = "/{stakeholderId}/{topicId}/change-visibility", method = RequestMethod.POST) public Topic changeTopicVisibility(@PathVariable("stakeholderId") String stakeholderId, - @PathVariable("topicId") String topicId, - @RequestParam("visibility") Visibility visibility, @RequestParam(required = false) Boolean propagate) { + @PathVariable("topicId") String topicId, + @RequestParam("visibility") Visibility visibility, @RequestParam(required = false) Boolean propagate) { log.debug("change topic visibility: "+visibility + " - toggle propagate: "+((propagate != null && propagate) ? "true" : "false")); log.debug("Stakeholder: "+stakeholderId + " - Topic: "+topicId); - Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId); + Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId); if (stakeholder != null) { if(!rolesUtils.hasUpdateAuthority(stakeholder.getType(), stakeholder.getAlias())) { @@ -399,3 +401,4 @@ public class TopicController { } } } +*/ diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/dao/CategoryDAO.java b/src/main/java/eu/dnetlib/uoamonitorservice/dao/CategoryDAO.java index b8d4db0..eb99550 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/dao/CategoryDAO.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/dao/CategoryDAO.java @@ -5,15 +5,16 @@ import org.springframework.data.mongodb.repository.MongoRepository; import org.springframework.stereotype.Repository; import java.util.List; +import java.util.Optional; @Repository public interface CategoryDAO extends MongoRepository { List findAll(); List findByDefaultId(String DefaultId); - Category findBySubCategoriesContaining(String subCategory); + Optional findBySubCategoriesContaining(String subCategory); - Category findById(String Id); + Optional findById(String Id); void delete(String Id); diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/dao/IndicatorDAO.java b/src/main/java/eu/dnetlib/uoamonitorservice/dao/IndicatorDAO.java index 8940da8..e121bae 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/dao/IndicatorDAO.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/dao/IndicatorDAO.java @@ -5,13 +5,14 @@ import org.springframework.data.mongodb.repository.MongoRepository; import org.springframework.stereotype.Repository; import java.util.List; +import java.util.Optional; @Repository public interface IndicatorDAO extends MongoRepository { List findAll(); List findByDefaultId(String DefaultId); - Indicator findById(String Id); + Optional findById(String Id); void delete(String Id); diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/dao/SectionDAO.java b/src/main/java/eu/dnetlib/uoamonitorservice/dao/SectionDAO.java index cd24e5a..bc27f31 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/dao/SectionDAO.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/dao/SectionDAO.java @@ -5,13 +5,14 @@ import org.springframework.data.mongodb.repository.MongoRepository; import org.springframework.stereotype.Repository; import java.util.List; +import java.util.Optional; @Repository public interface SectionDAO extends MongoRepository { List
findAll(); List
findByDefaultId(String DefaultId); - Section findById(String Id); + Optional
findById(String Id); void delete(String Id); diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/dao/StakeholderDAO.java b/src/main/java/eu/dnetlib/uoamonitorservice/dao/StakeholderDAO.java index 113c105..2e9a14d 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/dao/StakeholderDAO.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/dao/StakeholderDAO.java @@ -5,6 +5,7 @@ import org.springframework.data.mongodb.repository.MongoRepository; import org.springframework.stereotype.Repository; import java.util.List; +import java.util.Optional; @Repository public interface StakeholderDAO extends MongoRepository { @@ -17,10 +18,10 @@ public interface StakeholderDAO extends MongoRepository { List findByDefaultIdNot(String DefaultId); List findByDefaultIdNotAndType(String DefaultId, String Type); - Stakeholder findByTopicsContaining(String topic); + Optional findByTopicsContaining(String topic); - Stakeholder findById(String Id); - Stakeholder findByAlias(String Alias); + Optional findById(String Id); + Optional findByAlias(String Alias); void delete(String Id); diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/dao/SubCategoryDAO.java b/src/main/java/eu/dnetlib/uoamonitorservice/dao/SubCategoryDAO.java index 2e22285..1b28c99 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/dao/SubCategoryDAO.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/dao/SubCategoryDAO.java @@ -5,13 +5,14 @@ import org.springframework.data.mongodb.repository.MongoRepository; import org.springframework.stereotype.Repository; import java.util.List; +import java.util.Optional; @Repository public interface SubCategoryDAO extends MongoRepository { List findAll(); List findByDefaultId(String DefaultId); - SubCategory findById(String Id); + Optional findById(String Id); void delete(String Id); diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/dao/TopicDAO.java b/src/main/java/eu/dnetlib/uoamonitorservice/dao/TopicDAO.java index f6a36fc..4683610 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/dao/TopicDAO.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/dao/TopicDAO.java @@ -5,15 +5,16 @@ import org.springframework.data.mongodb.repository.MongoRepository; import org.springframework.stereotype.Repository; import java.util.List; +import java.util.Optional; @Repository public interface TopicDAO extends MongoRepository { List findAll(); List findByDefaultId(String DefaultId); - Topic findByCategoriesContaining(String category); + Optional findByCategoriesContaining(String category); - Topic findById(String Id); + Optional findById(String Id); void delete(String Id); diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/dto/CategoryFull.java b/src/main/java/eu/dnetlib/uoamonitorservice/dto/CategoryFull.java new file mode 100644 index 0000000..3211e1d --- /dev/null +++ b/src/main/java/eu/dnetlib/uoamonitorservice/dto/CategoryFull.java @@ -0,0 +1,18 @@ +package eu.dnetlib.uoamonitorservice.dto; + +import eu.dnetlib.uoamonitorservice.generics.CategoryGeneric; + +import java.util.List; +import java.util.Objects; + +public class CategoryFull extends CategoryGeneric { + public CategoryFull() { + super(); + } + + public CategoryFull(CategoryGeneric category, List subCategories) { + super(category); + subCategories.removeIf(Objects::isNull); + this.setSubCategories(subCategories); + } +} diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/dto/SectionFull.java b/src/main/java/eu/dnetlib/uoamonitorservice/dto/SectionFull.java new file mode 100644 index 0000000..fc7f74b --- /dev/null +++ b/src/main/java/eu/dnetlib/uoamonitorservice/dto/SectionFull.java @@ -0,0 +1,19 @@ +package eu.dnetlib.uoamonitorservice.dto; + +import eu.dnetlib.uoamonitorservice.dao.IndicatorDAO; +import eu.dnetlib.uoamonitorservice.entities.Indicator; +import eu.dnetlib.uoamonitorservice.entities.Section; +import eu.dnetlib.uoamonitorservice.generics.SectionGeneric; +import eu.dnetlib.uoamonitorservice.handlers.EntityNotFoundException; + +import java.util.List; +import java.util.stream.Collectors; + +public class SectionFull extends SectionGeneric { + public SectionFull() {} + + public SectionFull(Section section, List indicators) { + super(section); + this.setIndicators(indicators); + } +} diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/dto/StakeholderFull.java b/src/main/java/eu/dnetlib/uoamonitorservice/dto/StakeholderFull.java new file mode 100644 index 0000000..1db6f7a --- /dev/null +++ b/src/main/java/eu/dnetlib/uoamonitorservice/dto/StakeholderFull.java @@ -0,0 +1,15 @@ +package eu.dnetlib.uoamonitorservice.dto; + +import eu.dnetlib.uoamonitorservice.generics.StakeholderGeneric; + +import java.util.List; +import java.util.Objects; + +public class StakeholderFull extends StakeholderGeneric { + + public StakeholderFull(StakeholderGeneric stakeholder, List topics) { + super(stakeholder); + topics.removeIf(Objects::isNull); + this.setTopics(topics); + } +} diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/dto/SubCategoryFull.java b/src/main/java/eu/dnetlib/uoamonitorservice/dto/SubCategoryFull.java new file mode 100644 index 0000000..ca16ffd --- /dev/null +++ b/src/main/java/eu/dnetlib/uoamonitorservice/dto/SubCategoryFull.java @@ -0,0 +1,18 @@ +package eu.dnetlib.uoamonitorservice.dto; + +import eu.dnetlib.uoamonitorservice.generics.SubCategoryGeneric; + +import java.util.List; +import java.util.Objects; + +public class SubCategoryFull extends SubCategoryGeneric { + public SubCategoryFull() {} + + public SubCategoryFull(SubCategoryGeneric subCategory, List numbers, List charts) { + super(subCategory); + numbers.removeIf(Objects::isNull); + charts.removeIf(Objects::isNull); + this.setNumbers(numbers); + this.setCharts(charts); + } +} diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/dto/TopicFull.java b/src/main/java/eu/dnetlib/uoamonitorservice/dto/TopicFull.java new file mode 100644 index 0000000..99cf4c5 --- /dev/null +++ b/src/main/java/eu/dnetlib/uoamonitorservice/dto/TopicFull.java @@ -0,0 +1,18 @@ +package eu.dnetlib.uoamonitorservice.dto; + +import eu.dnetlib.uoamonitorservice.generics.TopicGeneric; + +import java.util.List; +import java.util.Objects; + +public class TopicFull extends TopicGeneric { + public TopicFull() { + super(); + } + + public TopicFull(TopicGeneric topic, List categories) { + super(topic); + categories.removeIf(Objects::isNull); + this.setCategories(categories); + } +} diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/entities/Category.java b/src/main/java/eu/dnetlib/uoamonitorservice/entities/Category.java index 6656b97..3eb926e 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/entities/Category.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/entities/Category.java @@ -1,129 +1,30 @@ package eu.dnetlib.uoamonitorservice.entities; -import com.fasterxml.jackson.annotation.JsonProperty; -import org.springframework.data.annotation.Id; +import eu.dnetlib.uoamonitorservice.dto.CategoryFull; +import eu.dnetlib.uoamonitorservice.generics.CategoryGeneric; +import eu.dnetlib.uoamonitorservice.generics.TopicGeneric; +import org.springframework.data.mongodb.core.mapping.Document; import java.util.ArrayList; -import java.util.Date; -import java.util.List; -public class Category { - @Id - @JsonProperty("_id") - private String id; - - private String name; - private String alias; - private String description; - private Visibility visibility = Visibility.PRIVATE; - private Date creationDate; - private Date updateDate; - private boolean isOverview; - private String defaultId; - private List subCategories; - - public Category() {} - public Category(Category category) { - id = category.getId(); - name = category.getName(); - alias = category.getAlias(); - description = category.getDescription(); - setVisibility(category.getVisibility()); - creationDate = category.getCreationDate(); - updateDate = category.getUpdateDate(); - isOverview = category.getIsOverview(); - defaultId = category.getDefaultId(); +@Document +public class Category extends CategoryGeneric { + public Category() { + super(); } - public void copyFromDefault(Category defaultCategory) { + public Category(CategoryFull category) { + super(category); + } + + public void copyFromDefault(CategoryGeneric defaultCategory) { setName(defaultCategory.getName()); setAlias(defaultCategory.getAlias()); setDescription(defaultCategory.getDescription()); setVisibility(defaultCategory.getVisibility()); setCreationDate(defaultCategory.getCreationDate()); setUpdateDate(defaultCategory.getUpdateDate()); - setIsOverview(defaultCategory.getIsOverview()); setDefaultId(defaultCategory.getId()); - setSubCategories(new ArrayList()); - } - - public String getId() { - return id; - } - - public void setId(String id) { - this.id = id; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public String getAlias() { - return alias; - } - - public void setAlias(String alias) { - this.alias = alias; - } - - public String getDescription() { - return description; - } - - public void setDescription(String description) { - this.description = description; - } - - public Visibility getVisibility() { - return visibility; - } - - public void setVisibility(Visibility visibility) { - this.visibility = visibility; - } - - public Date getCreationDate() { - return creationDate; - } - - public void setCreationDate(Date creationDate) { - this.creationDate = creationDate; - } - - public Date getUpdateDate() { - return updateDate; - } - - public void setUpdateDate(Date updateDate) { - this.updateDate = updateDate; - } - - public boolean getIsOverview() { - return isOverview; - } - - public void setIsOverview(boolean isOverview) { - this.isOverview = isOverview; - } - - public String getDefaultId() { - return defaultId; - } - - public void setDefaultId(String defaultId) { - this.defaultId = defaultId; - } - - public List getSubCategories() { - return subCategories; - } - - public void setSubCategories(List subCategories) { - this.subCategories = subCategories; + setSubCategories(new ArrayList<>()); } } diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/entities/Indicator.java b/src/main/java/eu/dnetlib/uoamonitorservice/entities/Indicator.java index a4aa824..88467cb 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/entities/Indicator.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/entities/Indicator.java @@ -1,28 +1,30 @@ package eu.dnetlib.uoamonitorservice.entities; import com.fasterxml.jackson.annotation.JsonProperty; +import eu.dnetlib.uoamonitorservice.generics.Common; import eu.dnetlib.uoamonitorservice.primitives.IndicatorPath; import eu.dnetlib.uoamonitorservice.primitives.IndicatorSize; import eu.dnetlib.uoamonitorservice.primitives.IndicatorType; +import eu.dnetlib.uoamonitorservice.primitives.Visibility; +import org.springframework.data.annotation.CreatedDate; import org.springframework.data.annotation.Id; +import org.springframework.data.annotation.LastModifiedDate; import java.util.Date; import java.util.List; -public class Indicator { +public class Indicator extends Common { @Id @JsonProperty("_id") private String id; - - private String name; - private String description; private String additionalDescription; private IndicatorType type; //number,chart private IndicatorSize width; //small,medium,large private IndicatorSize height = IndicatorSize.MEDIUM; //small,medium,large private List tags; // this field is not used anywhere now - private Visibility visibility = Visibility.PRIVATE; + @CreatedDate private Date creationDate; + @LastModifiedDate private Date updateDate; private String defaultId; private List indicatorPaths; @@ -50,22 +52,6 @@ public class Indicator { this.id = id; } - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public String getDescription() { - return description; - } - - public void setDescription(String description) { - this.description = description; - } - public String getAdditionalDescription() { return additionalDescription; } @@ -81,13 +67,6 @@ public class Indicator { return type.name(); } -// public String getStringType() { -// return type.name(); -// } - -// public void setType(IndicatorType type) { -// this.type = type; -// } public void setType(String type) { if(type == null) { this.type = null; @@ -121,14 +100,6 @@ public class Indicator { this.tags = tags; } - public Visibility getVisibility() { - return visibility; - } - - public void setVisibility(Visibility visibility) { - this.visibility = visibility; - } - public Date getCreationDate() { return creationDate; } diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/entities/Section.java b/src/main/java/eu/dnetlib/uoamonitorservice/entities/Section.java index c0664af..667e8bd 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/entities/Section.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/entities/Section.java @@ -1,115 +1,21 @@ package eu.dnetlib.uoamonitorservice.entities; -import com.fasterxml.jackson.annotation.JsonProperty; -import eu.dnetlib.uoamonitorservice.primitives.IndicatorType; -import org.springframework.data.annotation.Id; +import eu.dnetlib.uoamonitorservice.dto.SectionFull; +import eu.dnetlib.uoamonitorservice.generics.SectionGeneric; +import org.springframework.data.mongodb.core.mapping.Document; -import java.util.ArrayList; -import java.util.Date; import java.util.List; +import java.util.Objects; +import java.util.stream.Collectors; -public class Section { - @Id - @JsonProperty("_id") - private String id; - - private String title; - private String defaultId; - private String stakeholderAlias; - private IndicatorType type; - private Date creationDate; - private Date updateDate; - private List indicators; - +@Document +public class Section extends SectionGeneric { public Section() {} - public Section(Section section) { - id = section.getId(); - title = section.getTitle(); - defaultId = section.getDefaultId(); - stakeholderAlias = section.getStakeholderAlias(); - setType(section.getType()); - creationDate = section.getCreationDate(); - updateDate = section.getUpdateDate(); - } - - public void copyFromDefault(Section defaultSection) { - setTitle(defaultSection.getTitle()); - setType(defaultSection.getType()); - setDefaultId(defaultSection.id); - setCreationDate(defaultSection.getCreationDate()); - setUpdateDate(defaultSection.getUpdateDate()); - setIndicators(new ArrayList<>()); - } - - public String getId() { - return id; - } - - public void setId(String id) { - this.id = id; - } - - public String getTitle() { - return title; - } - - public void setTitle(String title) { - this.title = title; - } - - public String getDefaultId() { - return defaultId; - } - - public void setDefaultId(String defaultId) { - this.defaultId = defaultId; - } - - public String getStakeholderAlias() { - return stakeholderAlias; - } - - public void setStakeholderAlias(String stakeholderAlias) { - this.stakeholderAlias = stakeholderAlias; - } - - public String getType() { - if(type == null) { - return null; - } - return type.name(); - } - - public void setType(String type) { - if(type == null) { - this.type = null; - } else { - this.type = IndicatorType.valueOf(type); - } - } - - public Date getCreationDate() { - return creationDate; - } - - public void setCreationDate(Date creationDate) { - this.creationDate = creationDate; - } - - public Date getUpdateDate() { - return updateDate; - } - - public void setUpdateDate(Date updateDate) { - this.updateDate = updateDate; - } - - public List getIndicators() { - return indicators; - } - - public void setIndicators(List indicators) { - this.indicators = indicators; + public Section(SectionFull section) { + super(section); + List indicators = section.getIndicators().stream().map(Indicator::getId).collect(Collectors.toList()); + indicators.removeIf(Objects::isNull); + this.setIndicators(indicators); } } diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/entities/Stakeholder.java b/src/main/java/eu/dnetlib/uoamonitorservice/entities/Stakeholder.java index 4d72840..bd66180 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/entities/Stakeholder.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/entities/Stakeholder.java @@ -1,224 +1,16 @@ package eu.dnetlib.uoamonitorservice.entities; -import com.fasterxml.jackson.annotation.JsonProperty; -import eu.dnetlib.uoamonitorservice.primitives.Locale; -import eu.dnetlib.uoamonitorservice.primitives.StakeholderType; -import org.springframework.data.annotation.Id; +import eu.dnetlib.uoamonitorservice.generics.StakeholderGeneric; +import org.springframework.data.mongodb.core.mapping.Document; -import java.util.Date; -import java.util.List; - - -public class Stakeholder { - @Id - @JsonProperty("_id") - private String id; - - private StakeholderType type; // private StakeholderType type; - private Date projectUpdateDate = null; - private String index_id; - private String index_name; - private String index_shortName; - private String statsProfile = "monitor"; - private String logoUrl; - private boolean isUpload = false; - private String name; - private String alias; - private String description; - private String defaultId = null; - private Locale locale = Locale.EU; - private Visibility visibility = Visibility.PRIVATE; - private String funderType; - private Date creationDate; - private Date updateDate; - - private List topics; +@Document +public class Stakeholder extends StakeholderGeneric { public Stakeholder() { - - } - public Stakeholder(Stakeholder stakeholder) { - id = stakeholder.getId(); - setType(stakeholder.getType()); - projectUpdateDate = stakeholder.getProjectUpdateDate(); - index_id = stakeholder.getIndex_id(); - index_name = stakeholder.getIndex_name(); - index_shortName = stakeholder.getIndex_shortName(); - statsProfile = stakeholder.getStatsProfile(); - logoUrl = stakeholder.getLogoUrl(); - isUpload = stakeholder.getIsUpload(); - name = stakeholder.getName(); - alias = stakeholder.getAlias(); - description = stakeholder.getDescription(); - defaultId = stakeholder.getDefaultId(); - setLocale(stakeholder.getLocale()); - setVisibility(stakeholder.getVisibility()); - this.funderType = stakeholder.getFunderType(); - creationDate = stakeholder.getCreationDate(); - updateDate = stakeholder.getUpdateDate(); + super(); } - public String getId() { - return id; - } - - public void setId(String id) { - this.id = id; - } - - public String getType() { - if(type == null) { - return null; - } - return type.name(); - } - - public void setType(String type) { - if(type == null) { - this.type = null; - } else { - StakeholderType stakeholderType = StakeholderType.valueOf(type); - this.type = stakeholderType; - } - } - - public Date getProjectUpdateDate() { - return projectUpdateDate; - } - - public void setProjectUpdateDate(Date projectUpdateDate) { - this.projectUpdateDate = projectUpdateDate; - } - - public String getIndex_id() { - return index_id; - } - - public void setIndex_id(String index_id) { - this.index_id = index_id; - } - - public String getIndex_name() { - return index_name; - } - - public void setIndex_name(String index_name) { - this.index_name = index_name; - } - - public String getIndex_shortName() { - return index_shortName; - } - - public void setIndex_shortName(String index_shortName) { - this.index_shortName = index_shortName; - } - - public String getStatsProfile() { - return statsProfile; - } - - public void setStatsProfile(String statsProfile) { - this.statsProfile = statsProfile; - } - - public String getLogoUrl() { - return logoUrl; - } - - public void setLogoUrl(String logoUrl) { - this.logoUrl = logoUrl; - } - - public boolean getIsUpload() { - return isUpload; - } - - public void setIsUpload(boolean isUpload) { - this.isUpload = isUpload; - } - - public String getName() { return name; } - - public void setName(String name) { this.name = name; } - - public String getAlias() { - return alias; - } - - public void setAlias(String alias) { - this.alias = alias; - } - - public String getDescription() { - return description; - } - - public void setDescription(String description) { - this.description = description; - } - - public String getDefaultId() { - return defaultId; - } - - public void setDefaultId(String defaultId) { - this.defaultId = defaultId; - } - - public String getLocale() { - return locale.label; - } - - public void setLocale(String label) { - Locale locale = null; - for (Locale l : Locale.values()) { - if (l.label.equals(label)) { - locale = l; - } - } - this.locale = locale; - } - - public Visibility getVisibility() { - //return visibility.getLabel(); - return visibility; - } - - public void setVisibility(Visibility visibility) { - //this.visibility = Visibility.get(visibility); - this.visibility = visibility; - } - - public String getFunderType() { - return funderType; - } - - public void setFunderType(String funderType) { - this.funderType = funderType; - } - - public Date getCreationDate() { - return creationDate; - } - - public void setCreationDate(Date creationDate) { - this.creationDate = creationDate; - } - - public Date getUpdateDate() { - return updateDate; - } - - public void setUpdateDate(Date updateDate) { - this.updateDate = updateDate; - } - - public List getTopics() { - return topics; - } - - public void setTopics(List topics) { - this.topics = topics; + public Stakeholder(StakeholderGeneric stakeholder) { + super(stakeholder); } } diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/entities/SubCategory.java b/src/main/java/eu/dnetlib/uoamonitorservice/entities/SubCategory.java index fc1c242..7a67d66 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/entities/SubCategory.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/entities/SubCategory.java @@ -1,138 +1,13 @@ package eu.dnetlib.uoamonitorservice.entities; -import com.fasterxml.jackson.annotation.JsonProperty; -import org.springframework.data.annotation.Id; - -import java.util.ArrayList; -import java.util.Date; -import java.util.List; - -public class SubCategory { - @Id - @JsonProperty("_id") - private String id; - - private String name; - private String alias; - private String description; - private Visibility visibility = Visibility.PRIVATE; - private Date creationDate; - private Date updateDate; - private String defaultId; - private List charts; - private List numbers; +import eu.dnetlib.uoamonitorservice.generics.SubCategoryGeneric; +import org.springframework.data.mongodb.core.mapping.Document; +@Document +public class SubCategory extends SubCategoryGeneric { public SubCategory() {} - public SubCategory(SubCategory subCategory) { - id = subCategory.getId(); - name = subCategory.getName(); - alias = subCategory.getAlias(); - description = subCategory.getDescription(); - setVisibility(subCategory.getVisibility()); - creationDate = subCategory.getCreationDate(); - updateDate = subCategory.getUpdateDate(); - defaultId = subCategory.getDefaultId(); - } - public void createOverviewSubCategory(Category category) { - setName("Overview"); - setAlias("overview"); - setVisibility(category.getVisibility()); - setCreationDate(category.getCreationDate()); - setUpdateDate(category.getUpdateDate()); - setCharts(new ArrayList<>()); - setNumbers(new ArrayList<>()); - } - - public void copyFromDefault(SubCategory defaultSubCategory) { - setName(defaultSubCategory.getName()); - setAlias(defaultSubCategory.getAlias()); - setDescription(defaultSubCategory.getDescription()); - setVisibility(defaultSubCategory.getVisibility()); - setCreationDate(defaultSubCategory.getCreationDate()); - setUpdateDate(defaultSubCategory.getUpdateDate()); - setDefaultId(defaultSubCategory.getId()); - setCharts(new ArrayList()); - setNumbers(new ArrayList()); - } - - public String getId() { - return id; - } - - public void setId(String id) { - this.id = id; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public String getAlias() { - return alias; - } - - public void setAlias(String alias) { - this.alias = alias; - } - - public String getDescription() { - return description; - } - - public void setDescription(String description) { - this.description = description; - } - - public Visibility getVisibility() { - return visibility; - } - - public void setVisibility(Visibility visibility) { - this.visibility = visibility; - } - - public Date getCreationDate() { - return creationDate; - } - - public void setCreationDate(Date creationDate) { - this.creationDate = creationDate; - } - - public Date getUpdateDate() { - return updateDate; - } - - public void setUpdateDate(Date updateDate) { - this.updateDate = updateDate; - } - - public String getDefaultId() { - return defaultId; - } - - public void setDefaultId(String defaultId) { - this.defaultId = defaultId; - } - - public List getCharts() { - return charts; - } - - public void setCharts(List charts) { - this.charts = charts; - } - - public List getNumbers() { - return numbers; - } - - public void setNumbers(List numbers) { - this.numbers = numbers; + public SubCategory(SubCategoryGeneric subCategory) { + super(subCategory); } } diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/entities/Topic.java b/src/main/java/eu/dnetlib/uoamonitorservice/entities/Topic.java index 1fa77ae..a235cf9 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/entities/Topic.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/entities/Topic.java @@ -1,129 +1,15 @@ package eu.dnetlib.uoamonitorservice.entities; -import com.fasterxml.jackson.annotation.JsonProperty; -import org.springframework.data.annotation.Id; +import eu.dnetlib.uoamonitorservice.generics.TopicGeneric; +import org.springframework.data.mongodb.core.mapping.Document; -import java.util.ArrayList; -import java.util.Date; -import java.util.List; +@Document +public class Topic extends TopicGeneric { -public class Topic { - @Id - @JsonProperty("_id") - private String id; - - private String name; - private String alias; - private String description; - private String icon; - private Visibility visibility = Visibility.PRIVATE; - private Date creationDate; - private Date updateDate; - private String defaultId; - private List categories; - - public Topic() {} - public Topic(Topic topic) { - id = topic.getId(); - name = topic.getName(); - alias = topic.getAlias(); - description = topic.getDescription(); - icon = topic.getIcon(); - setVisibility(topic.getVisibility()); - creationDate = topic.getCreationDate(); - updateDate = topic.getUpdateDate(); - defaultId = topic.getDefaultId(); + public Topic() { + super(); } - - public void copyFromDefault(Topic defaultTopic) { - setName(defaultTopic.getName()); - setAlias(defaultTopic.getAlias()); - setDescription(defaultTopic.getDescription()); - setIcon(defaultTopic.getIcon()); - setVisibility(defaultTopic.getVisibility()); - setCreationDate(defaultTopic.getCreationDate()); - setUpdateDate(defaultTopic.getUpdateDate()); - setDefaultId(defaultTopic.getId()); - setCategories(new ArrayList()); - } - - public String getId() { - return id; - } - - public void setId(String id) { - this.id = id; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public String getAlias() { - return alias; - } - - public void setAlias(String alias) { - this.alias = alias; - } - - public String getDescription() { - return description; - } - - public void setDescription(String description) { - this.description = description; - } - - public String getIcon() { - return icon; - } - - public void setIcon(String icon) { - this.icon = icon; - } - - public Visibility getVisibility() { - return visibility; - } - - public void setVisibility(Visibility visibility) { - this.visibility = visibility; - } - - public Date getCreationDate() { - return creationDate; - } - - public void setCreationDate(Date creationDate) { - this.creationDate = creationDate; - } - - public Date getUpdateDate() { - return updateDate; - } - - public void setUpdateDate(Date updateDate) { - this.updateDate = updateDate; - } - - public String getDefaultId() { - return defaultId; - } - - public void setDefaultId(String defaultId) { - this.defaultId = defaultId; - } - - public List getCategories() { - return categories; - } - - public void setCategories(List categories) { - this.categories = categories; + public Topic(TopicGeneric topic) { + super(topic); } } diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/entities/Visibility.java b/src/main/java/eu/dnetlib/uoamonitorservice/entities/Visibility.java deleted file mode 100644 index 50b655c..0000000 --- a/src/main/java/eu/dnetlib/uoamonitorservice/entities/Visibility.java +++ /dev/null @@ -1,38 +0,0 @@ -package eu.dnetlib.uoamonitorservice.entities; - -import java.util.HashMap; -import java.util.Map; - -public enum Visibility { - // Do not rename or remove existring values. This may cause problems with already stored values in DB - // PUBLIC("public"), RESTRICTED("restricted"), PRIVATE("private"); - - PUBLIC, RESTRICTED, PRIVATE -// private String label; -// -// private Visibility(String label) { -// this.label = label; -// } -// -// public String getLabel() { -// return this.label; -// } -// -// //Lookup table -// private static final Map lookup = new HashMap<>(); -// -// //Populate the lookup table on loading time -// static -// { -// for(Visibility visibility : Visibility.values()) -// { -// lookup.put(visibility.getLabel(), visibility); -// } -// } -// -// //This method can be used for reverse lookup purpose -// public static Visibility get(String visibility) -// { -// return lookup.get(visibility); -// } -} diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/generics/CategoryGeneric.java b/src/main/java/eu/dnetlib/uoamonitorservice/generics/CategoryGeneric.java new file mode 100644 index 0000000..0ba9657 --- /dev/null +++ b/src/main/java/eu/dnetlib/uoamonitorservice/generics/CategoryGeneric.java @@ -0,0 +1,88 @@ +package eu.dnetlib.uoamonitorservice.generics; + +import com.fasterxml.jackson.annotation.JsonProperty; +import eu.dnetlib.uoamonitorservice.entities.Category; +import eu.dnetlib.uoamonitorservice.primitives.Visibility; +import org.springframework.data.annotation.CreatedDate; +import org.springframework.data.annotation.Id; +import org.springframework.data.annotation.LastModifiedDate; +import org.springframework.data.mongodb.core.mapping.Document; + +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + +@Document +public class CategoryGeneric extends Common { + @Id + @JsonProperty("_id") + protected String id; + @CreatedDate + protected Date creationDate; + @LastModifiedDate + protected Date updateDate; + protected boolean isOverview; + protected String defaultId; + protected List subCategories; + + public CategoryGeneric() {} + + public CategoryGeneric(CategoryGeneric category) { + id = category.getId(); + name = category.getName(); + alias = category.getAlias(); + description = category.getDescription(); + setVisibility(category.getVisibility()); + creationDate = category.getCreationDate(); + updateDate = category.getUpdateDate(); + defaultId = category.getDefaultId(); + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public Date getCreationDate() { + return creationDate; + } + + public void setCreationDate(Date creationDate) { + this.creationDate = creationDate; + } + + public Date getUpdateDate() { + return updateDate; + } + + public void setUpdateDate(Date updateDate) { + this.updateDate = updateDate; + } + + public boolean getIsOverview() { + return isOverview; + } + + public void setIsOverview(boolean isOverview) { + this.isOverview = isOverview; + } + + public String getDefaultId() { + return defaultId; + } + + public void setDefaultId(String defaultId) { + this.defaultId = defaultId; + } + + public List getSubCategories() { + return subCategories; + } + + public void setSubCategories(List subCategories) { + this.subCategories = subCategories; + } +} diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/generics/Common.java b/src/main/java/eu/dnetlib/uoamonitorservice/generics/Common.java new file mode 100644 index 0000000..35cb80b --- /dev/null +++ b/src/main/java/eu/dnetlib/uoamonitorservice/generics/Common.java @@ -0,0 +1,42 @@ +package eu.dnetlib.uoamonitorservice.generics; + +import eu.dnetlib.uoamonitorservice.primitives.Visibility; + +public class Common { + protected String name; + protected String description; + protected String alias; + protected Visibility visibility = Visibility.PRIVATE; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public String getAlias() { + return alias; + } + + public void setAlias(String alias) { + this.alias = alias; + } + + public Visibility getVisibility() { + return visibility; + } + + public void setVisibility(Visibility visibility) { + this.visibility = visibility; + } +} diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/generics/SectionGeneric.java b/src/main/java/eu/dnetlib/uoamonitorservice/generics/SectionGeneric.java new file mode 100644 index 0000000..995ac50 --- /dev/null +++ b/src/main/java/eu/dnetlib/uoamonitorservice/generics/SectionGeneric.java @@ -0,0 +1,119 @@ +package eu.dnetlib.uoamonitorservice.generics; + +import com.fasterxml.jackson.annotation.JsonProperty; +import eu.dnetlib.uoamonitorservice.primitives.IndicatorType; +import org.springframework.data.annotation.CreatedDate; +import org.springframework.data.annotation.Id; +import org.springframework.data.annotation.LastModifiedDate; + +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + +public class SectionGeneric { + @Id + @JsonProperty("_id") + protected String id; + protected String title; + protected String defaultId; + protected String stakeholderAlias; + protected IndicatorType type; + @CreatedDate + protected Date creationDate; + @LastModifiedDate + protected Date updateDate; + protected List indicators; + + public SectionGeneric() { + } + + public SectionGeneric(SectionGeneric section) { + id = section.getId(); + title = section.getTitle(); + defaultId = section.getDefaultId(); + stakeholderAlias = section.getStakeholderAlias(); + setType(section.getType()); + creationDate = section.getCreationDate(); + updateDate = section.getUpdateDate(); + } + + public void copyFromDefault(SectionGeneric defaultSection) { + setTitle(defaultSection.getTitle()); + setType(defaultSection.getType()); + setDefaultId(defaultSection.id); + setCreationDate(defaultSection.getCreationDate()); + setUpdateDate(defaultSection.getUpdateDate()); + setIndicators(new ArrayList<>()); + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public String getDefaultId() { + return defaultId; + } + + public void setDefaultId(String defaultId) { + this.defaultId = defaultId; + } + + public String getStakeholderAlias() { + return stakeholderAlias; + } + + public void setStakeholderAlias(String stakeholderAlias) { + this.stakeholderAlias = stakeholderAlias; + } + + public String getType() { + if (type == null) { + return null; + } + return type.name(); + } + + public void setType(String type) { + if (type == null) { + this.type = null; + } else { + this.type = IndicatorType.valueOf(type); + } + } + + public Date getCreationDate() { + return creationDate; + } + + public void setCreationDate(Date creationDate) { + this.creationDate = creationDate; + } + + public Date getUpdateDate() { + return updateDate; + } + + public void setUpdateDate(Date updateDate) { + this.updateDate = updateDate; + } + + public List getIndicators() { + return indicators; + } + + public void setIndicators(List indicators) { + this.indicators = indicators; + } +} diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/generics/StakeholderGeneric.java b/src/main/java/eu/dnetlib/uoamonitorservice/generics/StakeholderGeneric.java new file mode 100644 index 0000000..a5912b9 --- /dev/null +++ b/src/main/java/eu/dnetlib/uoamonitorservice/generics/StakeholderGeneric.java @@ -0,0 +1,193 @@ +package eu.dnetlib.uoamonitorservice.generics; + +import com.fasterxml.jackson.annotation.JsonProperty; +import eu.dnetlib.uoamonitorservice.primitives.Locale; +import eu.dnetlib.uoamonitorservice.primitives.StakeholderType; +import eu.dnetlib.uoamonitorservice.primitives.Visibility; +import org.springframework.data.annotation.CreatedDate; +import org.springframework.data.annotation.Id; +import org.springframework.data.annotation.LastModifiedDate; + +import java.util.Date; +import java.util.List; + + +public class StakeholderGeneric extends Common { + @Id + @JsonProperty("_id") + protected String id; + protected StakeholderType type; + protected Date projectUpdateDate = null; + protected String index_id; + protected String index_name; + protected String index_shortName; + protected String statsProfile = "monitor"; + protected String logoUrl; + protected boolean isUpload = false; + protected String defaultId = null; + protected Locale locale = Locale.EU; + protected String funderType; + @CreatedDate + protected Date creationDate; + @LastModifiedDate + protected Date updateDate; + protected List topics; + + public StakeholderGeneric() { + } + + public StakeholderGeneric(StakeholderGeneric stakeholder) { + id = stakeholder.getId(); + setType(stakeholder.getType()); + projectUpdateDate = stakeholder.getProjectUpdateDate(); + index_id = stakeholder.getIndex_id(); + index_name = stakeholder.getIndex_name(); + index_shortName = stakeholder.getIndex_shortName(); + statsProfile = stakeholder.getStatsProfile(); + logoUrl = stakeholder.getLogoUrl(); + isUpload = stakeholder.getIsUpload(); + name = stakeholder.getName(); + alias = stakeholder.getAlias(); + description = stakeholder.getDescription(); + defaultId = stakeholder.getDefaultId(); + setLocale(stakeholder.getLocale()); + setVisibility(stakeholder.getVisibility()); + this.funderType = stakeholder.getFunderType(); + creationDate = stakeholder.getCreationDate(); + updateDate = stakeholder.getUpdateDate(); + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getType() { + if (type == null) { + return null; + } + return type.name(); + } + + public void setType(String type) { + if (type == null) { + this.type = null; + } else { + StakeholderType stakeholderType = StakeholderType.valueOf(type); + this.type = stakeholderType; + } + } + + public Date getProjectUpdateDate() { + return projectUpdateDate; + } + + public void setProjectUpdateDate(Date projectUpdateDate) { + this.projectUpdateDate = projectUpdateDate; + } + + public String getIndex_id() { + return index_id; + } + + public void setIndex_id(String index_id) { + this.index_id = index_id; + } + + public String getIndex_name() { + return index_name; + } + + public void setIndex_name(String index_name) { + this.index_name = index_name; + } + + public String getIndex_shortName() { + return index_shortName; + } + + public void setIndex_shortName(String index_shortName) { + this.index_shortName = index_shortName; + } + + public String getStatsProfile() { + return statsProfile; + } + + public void setStatsProfile(String statsProfile) { + this.statsProfile = statsProfile; + } + + public String getLogoUrl() { + return logoUrl; + } + + public void setLogoUrl(String logoUrl) { + this.logoUrl = logoUrl; + } + + public boolean getIsUpload() { + return isUpload; + } + + public void setIsUpload(boolean isUpload) { + this.isUpload = isUpload; + } + + public String getDefaultId() { + return defaultId; + } + + public void setDefaultId(String defaultId) { + this.defaultId = defaultId; + } + + public String getLocale() { + return locale.label; + } + + public void setLocale(String label) { + Locale locale = null; + for (Locale l : Locale.values()) { + if (l.label.equals(label)) { + locale = l; + } + } + this.locale = locale; + } + + public String getFunderType() { + return funderType; + } + + public void setFunderType(String funderType) { + this.funderType = funderType; + } + + public Date getCreationDate() { + return creationDate; + } + + public void setCreationDate(Date creationDate) { + this.creationDate = creationDate; + } + + public Date getUpdateDate() { + return updateDate; + } + + public void setUpdateDate(Date updateDate) { + this.updateDate = updateDate; + } + + public List getTopics() { + return topics; + } + + public void setTopics(List topics) { + this.topics = topics; + } +} diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/generics/SubCategoryGeneric.java b/src/main/java/eu/dnetlib/uoamonitorservice/generics/SubCategoryGeneric.java new file mode 100644 index 0000000..c285d17 --- /dev/null +++ b/src/main/java/eu/dnetlib/uoamonitorservice/generics/SubCategoryGeneric.java @@ -0,0 +1,107 @@ +package eu.dnetlib.uoamonitorservice.generics; + +import com.fasterxml.jackson.annotation.JsonProperty; +import eu.dnetlib.uoamonitorservice.entities.Category; +import eu.dnetlib.uoamonitorservice.primitives.Visibility; +import org.springframework.data.annotation.CreatedDate; +import org.springframework.data.annotation.Id; +import org.springframework.data.annotation.LastModifiedDate; + +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + +public class SubCategoryGeneric extends Common { + @Id + @JsonProperty("_id") + protected String id; + @CreatedDate + protected Date creationDate; + @LastModifiedDate + protected Date updateDate; + protected String defaultId; + protected List charts; + protected List numbers; + + public SubCategoryGeneric() {} + public SubCategoryGeneric(SubCategoryGeneric subCategory) { + id = subCategory.getId(); + name = subCategory.getName(); + alias = subCategory.getAlias(); + description = subCategory.getDescription(); + setVisibility(subCategory.getVisibility()); + creationDate = subCategory.getCreationDate(); + updateDate = subCategory.getUpdateDate(); + defaultId = subCategory.getDefaultId(); + } + + public void createOverviewSubCategory(Category category) { + setName("Overview"); + setAlias("overview"); + setVisibility(category.getVisibility()); + setCreationDate(category.getCreationDate()); + setUpdateDate(category.getUpdateDate()); + setCharts(new ArrayList<>()); + setNumbers(new ArrayList<>()); + } + + public void copyFromDefault(SubCategoryGeneric defaultSubCategory) { + setName(defaultSubCategory.getName()); + setAlias(defaultSubCategory.getAlias()); + setDescription(defaultSubCategory.getDescription()); + setVisibility(defaultSubCategory.getVisibility()); + setCreationDate(defaultSubCategory.getCreationDate()); + setUpdateDate(defaultSubCategory.getUpdateDate()); + setDefaultId(defaultSubCategory.getId()); + setCharts(new ArrayList()); + setNumbers(new ArrayList()); + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public Date getCreationDate() { + return creationDate; + } + + public void setCreationDate(Date creationDate) { + this.creationDate = creationDate; + } + + public Date getUpdateDate() { + return updateDate; + } + + public void setUpdateDate(Date updateDate) { + this.updateDate = updateDate; + } + + public String getDefaultId() { + return defaultId; + } + + public void setDefaultId(String defaultId) { + this.defaultId = defaultId; + } + + public List getCharts() { + return charts; + } + + public void setCharts(List charts) { + this.charts = charts; + } + + public List getNumbers() { + return numbers; + } + + public void setNumbers(List numbers) { + this.numbers = numbers; + } +} diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/generics/TopicGeneric.java b/src/main/java/eu/dnetlib/uoamonitorservice/generics/TopicGeneric.java new file mode 100644 index 0000000..0ea1693 --- /dev/null +++ b/src/main/java/eu/dnetlib/uoamonitorservice/generics/TopicGeneric.java @@ -0,0 +1,100 @@ +package eu.dnetlib.uoamonitorservice.generics; + +import com.fasterxml.jackson.annotation.JsonProperty; +import eu.dnetlib.uoamonitorservice.primitives.Visibility; +import org.springframework.data.annotation.CreatedDate; +import org.springframework.data.annotation.Id; +import org.springframework.data.annotation.LastModifiedDate; + +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + +public class TopicGeneric extends Common { + @Id + @JsonProperty("_id") + protected String id; + protected String icon; + @CreatedDate + protected Date creationDate; + @LastModifiedDate + protected Date updateDate; + protected String defaultId; + protected List categories; + + public TopicGeneric() { + } + + public TopicGeneric(TopicGeneric topic) { + id = topic.getId(); + name = topic.getName(); + alias = topic.getAlias(); + description = topic.getDescription(); + icon = topic.getIcon(); + setVisibility(topic.getVisibility()); + creationDate = topic.getCreationDate(); + updateDate = topic.getUpdateDate(); + defaultId = topic.getDefaultId(); + } + + public void copyFromDefault(TopicGeneric defaultTopic) { + setName(defaultTopic.getName()); + setAlias(defaultTopic.getAlias()); + setDescription(defaultTopic.getDescription()); + setIcon(defaultTopic.getIcon()); + setVisibility(defaultTopic.getVisibility()); + setCreationDate(defaultTopic.getCreationDate()); + setUpdateDate(defaultTopic.getUpdateDate()); + setDefaultId(defaultTopic.getId()); + setCategories(new ArrayList<>()); + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + + public String getIcon() { + return icon; + } + + public void setIcon(String icon) { + this.icon = icon; + } + + public Date getCreationDate() { + return creationDate; + } + + public void setCreationDate(Date creationDate) { + this.creationDate = creationDate; + } + + public Date getUpdateDate() { + return updateDate; + } + + public void setUpdateDate(Date updateDate) { + this.updateDate = updateDate; + } + + public String getDefaultId() { + return defaultId; + } + + public void setDefaultId(String defaultId) { + this.defaultId = defaultId; + } + + public List getCategories() { + return categories; + } + + public void setCategories(List categories) { + this.categories = categories; + } +} diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/primitives/Visibility.java b/src/main/java/eu/dnetlib/uoamonitorservice/primitives/Visibility.java new file mode 100644 index 0000000..575482c --- /dev/null +++ b/src/main/java/eu/dnetlib/uoamonitorservice/primitives/Visibility.java @@ -0,0 +1,5 @@ +package eu.dnetlib.uoamonitorservice.primitives; + +public enum Visibility { + PUBLIC, RESTRICTED, PRIVATE +} diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/service/CategoryService.java b/src/main/java/eu/dnetlib/uoamonitorservice/service/CategoryService.java new file mode 100644 index 0000000..cfb8e35 --- /dev/null +++ b/src/main/java/eu/dnetlib/uoamonitorservice/service/CategoryService.java @@ -0,0 +1,52 @@ +package eu.dnetlib.uoamonitorservice.service; + +import eu.dnetlib.uoamonitorservice.dao.CategoryDAO; +import eu.dnetlib.uoamonitorservice.dto.CategoryFull; +import eu.dnetlib.uoamonitorservice.dto.TopicFull; +import eu.dnetlib.uoamonitorservice.entities.Category; +import eu.dnetlib.uoamonitorservice.entities.Topic; +import eu.dnetlib.uoamonitorservice.handlers.EntityNotFoundException; +import eu.dnetlib.uoamonitorservice.handlers.PathNotValidException; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.security.access.method.P; +import org.springframework.stereotype.Service; + +import java.util.stream.Collectors; + +@Service +public class CategoryService { + + private final CategoryDAO dao; + + private final SubCategoryService subCategoryService; + private final CommonService commonService; + + @Autowired + public CategoryService(SubCategoryService subCategoryService, CommonService commonService, CategoryDAO dao) { + this.subCategoryService = subCategoryService; + this.commonService = commonService; + this.dao = dao; + } + + public Category findByPath(Topic topic, String categoryId, String message) { + if (!topic.getCategories().contains(categoryId)) { + throw new PathNotValidException(message + ": Category with id: " + categoryId + " not found in Topic: " + topic.getId()); + } + return this.dao.findById(categoryId).orElseThrow(() -> new EntityNotFoundException(message + ": Category with id: " + categoryId + " not found")); + } + + public CategoryFull getFullCategory(String type, String alias, String categoryId) { + Category category = this.find(categoryId); + if(commonService.hasVisibilityAuthority(type, alias, category)) { + return new CategoryFull(category, category.getSubCategories().stream() + .map(subCategoryId -> this.subCategoryService.getFullSubCategory(type, alias, subCategoryId)) + .collect(Collectors.toList())); + } else { + return null; + } + } + + public Category find(String id) { + return dao.findById(id).orElseThrow(() -> new EntityNotFoundException("Category with id: " + id + " not found")); + } +} diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/service/CommonService.java b/src/main/java/eu/dnetlib/uoamonitorservice/service/CommonService.java new file mode 100644 index 0000000..cf43df1 --- /dev/null +++ b/src/main/java/eu/dnetlib/uoamonitorservice/service/CommonService.java @@ -0,0 +1,37 @@ +package eu.dnetlib.uoamonitorservice.service; + +import eu.dnetlib.uoaauthorizationlibrary.security.AuthorizationService; +import eu.dnetlib.uoamonitorservice.generics.Common; +import eu.dnetlib.uoamonitorservice.primitives.Visibility; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +@Service +public class CommonService { + + AuthorizationService authorizationService; + + @Autowired + public CommonService(AuthorizationService authorizationService) { + this.authorizationService = authorizationService; + } + + public boolean hasAccessAuthority(String type, String alias, boolean isDefault) { + if(this.authorizationService.isPortalAdmin() || this.authorizationService.isCurator(type)) { + return true; + } else if(!isDefault) { + return this.authorizationService.isManager(type, alias); + } + return false; + } + + public boolean hasVisibilityAuthority(String type, String alias, Common common) { + if(authorizationService.isPortalAdmin() || authorizationService.isCurator(type) || authorizationService.isManager(type, alias)) { + return true; + } else if(authorizationService.isMember(type, alias) && common.getVisibility() != Visibility.PRIVATE) { + return true; + } else { + return common.getVisibility() == Visibility.PUBLIC; + } + } +} diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/service/IndicatorService.java b/src/main/java/eu/dnetlib/uoamonitorservice/service/IndicatorService.java new file mode 100644 index 0000000..06ed3d5 --- /dev/null +++ b/src/main/java/eu/dnetlib/uoamonitorservice/service/IndicatorService.java @@ -0,0 +1,38 @@ +package eu.dnetlib.uoamonitorservice.service; + +import eu.dnetlib.uoamonitorservice.dao.IndicatorDAO; +import eu.dnetlib.uoamonitorservice.entities.Indicator; +import eu.dnetlib.uoamonitorservice.handlers.EntityNotFoundException; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +@Service +public class IndicatorService { + + private final IndicatorDAO dao; + + private final CommonService commonService; + + @Autowired + public IndicatorService(IndicatorDAO dao, CommonService commonService) { + this.dao = dao; + this.commonService = commonService; + } + + public Indicator save(Indicator indicator) { + return this.dao.save(indicator); + } + + public Indicator find(String id) { + return dao.findById(id).orElseThrow(() -> new EntityNotFoundException("Indicator with id: " + id + " not found")); + } + + public Indicator getIndicator(String type, String alias, String id) { + Indicator indicator = this.find(id); + if(this.commonService.hasVisibilityAuthority(type, alias, indicator)) { + return indicator; + } else { + return null; + } + } +} diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/service/SectionService.java b/src/main/java/eu/dnetlib/uoamonitorservice/service/SectionService.java new file mode 100644 index 0000000..40f9c7f --- /dev/null +++ b/src/main/java/eu/dnetlib/uoamonitorservice/service/SectionService.java @@ -0,0 +1,71 @@ +package eu.dnetlib.uoamonitorservice.service; + +import eu.dnetlib.uoamonitorservice.dao.SectionDAO; +import eu.dnetlib.uoamonitorservice.dto.SectionFull; +import eu.dnetlib.uoamonitorservice.entities.Indicator; +import eu.dnetlib.uoamonitorservice.entities.Section; +import eu.dnetlib.uoamonitorservice.handlers.EntityNotFoundException; +import eu.dnetlib.uoamonitorservice.primitives.Visibility; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.List; +import java.util.stream.Collectors; + +@Service +public class SectionService { + + private final SectionDAO dao; + + private final IndicatorService indicatorService; + + @Autowired + public SectionService(SectionDAO dao, IndicatorService indicatorService) { + this.dao = dao; + this.indicatorService = indicatorService; + } + + public SectionFull buildSection(SectionFull sectionFull) { + sectionFull.setIndicators(sectionFull.getIndicators().stream().map(indicator -> this.indicatorService.save(indicator)).collect(Collectors.toList())); + Section section = dao.save(new Section(sectionFull)); + return new SectionFull(section, sectionFull.getIndicators()); + } + + public SectionFull changeVisibilityTree(String sectionId, Visibility visibility) { + Section section = this.find(sectionId); + SectionFull sectionFull = new SectionFull(section, section.getIndicators().stream().map(indicatorId -> this.indicatorService.find(indicatorId)).collect(Collectors.toList())); + sectionFull.setIndicators(sectionFull.getIndicators().stream().map(indicator -> { + indicator.setVisibility(visibility); + return indicatorService.save(indicator); + }).collect(Collectors.toList())); + return sectionFull; + } + + public SectionFull saveSection(SectionFull sectionFull) { + Section section = new Section(sectionFull); + if(section.getId() != null) { + section.setIndicators(this.getIndicatorsId(section.getId())); + } + return sectionFull; + } + + public Section find(String id) { + return dao.findById(id).orElseThrow(() -> new EntityNotFoundException("Section with id: " + id + " not found")); + } + + public SectionFull getFullSection(String type, String alias, String id) { + Section section = this.find(id); + return new SectionFull(section, section.getIndicators().stream() + .map(indicatorId -> this.indicatorService.getIndicator(type, alias, indicatorId)) + .collect(Collectors.toList())); + } + + public List getIndicators(String id) { + Section section = this.find(id); + return section.getIndicators().stream().map(this.indicatorService::find).collect(Collectors.toList()); + } + + public List getIndicatorsId(String id) { + return this.getIndicators(id).stream().map(Indicator::getId).collect(Collectors.toList()); + } +} diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/service/StakeholderService.java b/src/main/java/eu/dnetlib/uoamonitorservice/service/StakeholderService.java index 61b4ba4..fc94b40 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/service/StakeholderService.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/service/StakeholderService.java @@ -1,9 +1,11 @@ package eu.dnetlib.uoamonitorservice.service; -import eu.dnetlib.uoaadmintoolslibrary.handlers.utils.RolesUtils; +import eu.dnetlib.uoaauthorizationlibrary.security.AuthorizationService; import eu.dnetlib.uoamonitorservice.dao.StakeholderDAO; +import eu.dnetlib.uoamonitorservice.dto.StakeholderFull; import eu.dnetlib.uoamonitorservice.entities.Stakeholder; -import eu.dnetlib.uoamonitorservice.entities.Visibility; +import eu.dnetlib.uoamonitorservice.handlers.EntityNotFoundException; +import eu.dnetlib.uoamonitorservice.primitives.Visibility; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -13,16 +15,39 @@ import java.util.stream.Collectors; @Service public class StakeholderService { - @Autowired - StakeholderDAO dao; + private final StakeholderDAO dao; + + private final CommonService commonService; + private final TopicService topicService; @Autowired - RolesUtils rolesUtils; + public StakeholderService(StakeholderDAO dao, CommonService commonService, TopicService topicService) { + this.dao = dao; + this.commonService = commonService; + this.topicService = topicService; + } + + public Stakeholder findByAlias(String alias) { + return this.dao.findByAlias(alias).orElseThrow(() -> new EntityNotFoundException("Stakeholder with alias: " + alias + " not found")); + } public List getAllAliases() { return this.dao.findAll().stream().map(Stakeholder::getAlias).collect(Collectors.toList()); } + public List getAll(String type) { + if(type != null) { + return this.dao.findByType(type); + } + return this.dao.findAll(); + } + + public List getAllDefaultByRole(String type) { + return (type == null?this.dao.findByDefaultId(null):this.dao.findByDefaultIdAndType(null, type)).stream() + .filter(stakeholder -> this.commonService.hasAccessAuthority(stakeholder.getType(), stakeholder.getAlias(), true)) + .collect(Collectors.toList()); + } + public List getStakeholdersByTypeAndRole(String type, String defaultId, boolean manage) { List stakeholders; if(type != null && defaultId != null) { @@ -35,12 +60,24 @@ public class StakeholderService { stakeholders = dao.findByDefaultIdNot(null); } return stakeholders.stream().filter(stakeholder -> - (!manage && - (stakeholder.getVisibility() == Visibility.PUBLIC - || stakeholder.getVisibility() == Visibility.RESTRICTED)) - || rolesUtils.isPortalAdmin() - || rolesUtils.isCurator(stakeholder.getType()) - || rolesUtils.isManager(stakeholder.getType(), stakeholder.getId())) + (!manage && (stakeholder.getVisibility() == Visibility.PUBLIC || stakeholder.getVisibility() == Visibility.RESTRICTED)) + || this.commonService.hasAccessAuthority(stakeholder.getType(), stakeholder.getAlias(), false)) .collect(Collectors.toList()); } + + public Stakeholder findByPath(String stakeholderId) { + return dao.findById(stakeholderId).orElseThrow(() -> new EntityNotFoundException("Stakeholder with id: " + stakeholderId + " not found")); + } + + public StakeholderFull getFullStakeholder(Stakeholder stakeholder) { + if(this.commonService.hasAccessAuthority(stakeholder.getType(), stakeholder.getAlias(), stakeholder.getDefaultId() == null) || + (stakeholder.getDefaultId() != null && this.commonService.hasVisibilityAuthority(stakeholder.getType(), stakeholder.getAlias(), stakeholder))) { + return new StakeholderFull(stakeholder, + stakeholder.getTopics().stream() + .map(topicId -> topicService.getFullTopic(stakeholder.getType(), stakeholder.getAlias(), topicId)) + .collect(Collectors.toList())); + } else { + return null; + } + } } diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/service/SubCategoryService.java b/src/main/java/eu/dnetlib/uoamonitorservice/service/SubCategoryService.java new file mode 100644 index 0000000..5307049 --- /dev/null +++ b/src/main/java/eu/dnetlib/uoamonitorservice/service/SubCategoryService.java @@ -0,0 +1,55 @@ +package eu.dnetlib.uoamonitorservice.service; + +import eu.dnetlib.uoamonitorservice.dao.SubCategoryDAO; +import eu.dnetlib.uoamonitorservice.dto.SubCategoryFull; +import eu.dnetlib.uoamonitorservice.dto.TopicFull; +import eu.dnetlib.uoamonitorservice.entities.Category; +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.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.stream.Collectors; + +@Service +public class SubCategoryService { + + private final SubCategoryDAO dao; + + private final CommonService commonService; + private final SectionService sectionService; + + @Autowired + public SubCategoryService( CommonService commonService, SubCategoryDAO dao, SectionService sectionService) { + this.commonService = commonService; + this.dao = dao; + this.sectionService = sectionService; + } + + public SubCategory findByPath(Category category, String subcategoryId, String message) { + if (!category.getSubCategories().contains(subcategoryId)) { + throw new PathNotValidException(message + ": SubCategory with id: " + subcategoryId + " not found in Category: " + category.getId()); + } + return this.dao.findById(subcategoryId).orElseThrow(() -> new EntityNotFoundException(message + ": SubCategory with id: " + subcategoryId + " not found")); + } + + public SubCategoryFull getFullSubCategory(String type, String alias, String subCategoryId) { + SubCategory subCategory = this.find(subCategoryId); + if(commonService.hasVisibilityAuthority(type, alias, subCategory)) { + return new SubCategoryFull(subCategory, subCategory.getNumbers().stream() + .map(sectionId -> this.sectionService.getFullSection(type, alias, sectionId)) + .collect(Collectors.toList()), + subCategory.getCharts().stream() + .map(sectionId -> this.sectionService.getFullSection(type, alias, sectionId)) + .collect(Collectors.toList())); + } else { + return null; + } + } + + public SubCategory find(String id) { + return dao.findById(id).orElseThrow(() -> new EntityNotFoundException("SubCategory with id: " + id + " not found")); + } +} diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/service/TopicService.java b/src/main/java/eu/dnetlib/uoamonitorservice/service/TopicService.java new file mode 100644 index 0000000..1c0024e --- /dev/null +++ b/src/main/java/eu/dnetlib/uoamonitorservice/service/TopicService.java @@ -0,0 +1,53 @@ +package eu.dnetlib.uoamonitorservice.service; + +import eu.dnetlib.uoamonitorservice.dao.TopicDAO; +import eu.dnetlib.uoamonitorservice.dto.TopicFull; +import eu.dnetlib.uoamonitorservice.entities.Indicator; +import eu.dnetlib.uoamonitorservice.entities.Stakeholder; +import eu.dnetlib.uoamonitorservice.entities.Topic; +import eu.dnetlib.uoamonitorservice.generics.Common; +import eu.dnetlib.uoamonitorservice.handlers.EntityNotFoundException; +import eu.dnetlib.uoamonitorservice.handlers.PathNotValidException; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.security.access.method.P; +import org.springframework.stereotype.Service; + +import java.util.stream.Collectors; + +@Service +public class TopicService { + + private final TopicDAO dao; + + private final CategoryService categoryService; + private final CommonService commonService; + + @Autowired + public TopicService(TopicDAO dao, CategoryService categoryService, CommonService commonService) { + this.dao = dao; + this.categoryService = categoryService; + this.commonService = commonService; + } + + public Topic findByPath(Stakeholder stakeholder, String topicId, String message) { + if (!stakeholder.getTopics().contains(topicId)) { + throw new PathNotValidException(message + ": Topic with id: " + topicId + " not found in Stakeholder: " + stakeholder.getId()); + } + return this.dao.findById(topicId).orElseThrow(() -> new EntityNotFoundException(message + ": Topic with id: " + topicId + " not found")); + } + + public TopicFull getFullTopic(String type, String alias, String id) { + Topic topic = this.find(id); + if(commonService.hasVisibilityAuthority(type, alias, topic)) { + return new TopicFull(topic, topic.getCategories().stream() + .map(categoryId -> this.categoryService.getFullCategory(type, alias, categoryId)) + .collect(Collectors.toList())); + } else { + return null; + } + } + + public Topic find(String id) { + return dao.findById(id).orElseThrow(() -> new EntityNotFoundException("Topic with id: " + id + " not found")); + } +} From 959d553ca44714d85e61da8cdab722dd181c93ba Mon Sep 17 00:00:00 2001 From: "k.triantafyllou" Date: Wed, 13 Mar 2024 00:20:55 +0200 Subject: [PATCH 05/11] [new-model]: Refactor all delete and change visibility methods. --- .../controllers/SectionController.java | 18 ++ .../controllers/StakeholderController.java | 158 ++++-------- .../controllers/TopicController.java | 229 ++---------------- .../uoamonitorservice/dao/CategoryDAO.java | 2 +- .../uoamonitorservice/dao/SectionDAO.java | 1 + .../uoamonitorservice/dao/StakeholderDAO.java | 2 +- .../uoamonitorservice/dao/SubCategoryDAO.java | 3 + .../uoamonitorservice/dao/TopicDAO.java | 2 +- .../dto/StakeholderFull.java | 4 + .../uoamonitorservice/entities/Category.java | 5 + .../uoamonitorservice/entities/Indicator.java | 44 ---- .../uoamonitorservice/entities/Section.java | 5 +- .../entities/Stakeholder.java | 9 +- .../entities/SubCategory.java | 11 +- .../uoamonitorservice/entities/Topic.java | 10 +- .../generics/CategoryGeneric.java | 40 --- .../uoamonitorservice/generics/Common.java | 53 ++++ .../generics/SectionGeneric.java | 47 +--- .../generics/StakeholderGeneric.java | 40 --- .../generics/SubCategoryGeneric.java | 44 +--- .../generics/TopicGeneric.java | 40 --- .../service/CategoryService.java | 68 +++++- .../service/CommonService.java | 12 + .../service/IndicatorService.java | 48 +++- .../service/SectionService.java | 74 +++++- .../service/StakeholderService.java | 63 ++++- .../service/SubCategoryService.java | 80 +++++- .../service/TopicService.java | 81 ++++++- 28 files changed, 580 insertions(+), 613 deletions(-) diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/SectionController.java b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/SectionController.java index 05720fa..2b145f2 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/SectionController.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/SectionController.java @@ -249,5 +249,23 @@ public class SectionController { sectionDAO.save(section); } } + + public Section changeVisibilityTree(String sectionId, Visibility visibility) { + Section section = sectionDAO.findById(sectionId); + if (section == null) { + // EXCEPTION - Section not found + throw new EntityNotFoundException("Change section visibility: Section with id: " + sectionId + " not found"); + } + + Section sectionFull = new Section(section); + List indicatorsFull = new ArrayList(); + + for (String indicatorId : section.getIndicators()) { + indicatorsFull.add(indicatorController.changeVisibilityTree(indicatorId, null, visibility)); + } + + sectionFull.setIndicators(indicatorsFull); + return sectionFull; + } } */ diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/StakeholderController.java b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/StakeholderController.java index cb1f8ba..d72bd06 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/StakeholderController.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/StakeholderController.java @@ -1,10 +1,14 @@ package eu.dnetlib.uoamonitorservice.controllers; +import eu.dnetlib.uoaadmintoolslibrary.entities.Portal; import eu.dnetlib.uoaadmintoolslibrary.handlers.ForbiddenException; import eu.dnetlib.uoaadmintoolslibrary.services.PortalService; import eu.dnetlib.uoamonitorservice.dto.StakeholderFull; +import eu.dnetlib.uoamonitorservice.dto.TopicFull; import eu.dnetlib.uoamonitorservice.entities.Stakeholder; +import eu.dnetlib.uoamonitorservice.primitives.Visibility; import eu.dnetlib.uoamonitorservice.service.StakeholderService; +import eu.dnetlib.uoamonitorservice.service.TopicService; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; @@ -20,8 +24,8 @@ import java.util.stream.Collectors; public class StakeholderController { private final Logger log = LogManager.getLogger(this.getClass()); - private PortalService portalService; - private StakeholderService stakeholderService; + private final PortalService portalService; + private final StakeholderService stakeholderService; @Autowired public StakeholderController(PortalService portalService, StakeholderService stakeholderService) { @@ -39,33 +43,24 @@ public class StakeholderController { return stakeholderAlias; } - /* @PreAuthorize("hasAnyAuthority(" + + @PreAuthorize("hasAnyAuthority(" + "@AuthorizationService.PORTAL_ADMIN, " + - "@AuthorizationService.curator(#stakeholderFull.getType()))") + "@AuthorizationService.curator(#stakeholder.getType()))") @RequestMapping(value = "/build-stakeholder", method = RequestMethod.POST) - public StakeholderFull buildFullStakeholder(@RequestBody StakeholderFull stakeholderFull) { + public StakeholderFull buildFullStakeholder(@RequestBody StakeholderFull stakeholder) { log.debug("build stakeholder"); - log.debug("Alias: " + stakeholderFull.getAlias()); - Stakeholder stakeholder = new Stakeholder(stakeholderFull); - stakeholderFull.setTopics(stakeholderFull.getTopics().stream().map(topic -> topicController.buildTopic(topic))); - stakeholder.setTopics(stakeholderFull.getTopics().stream().map(TopicFull::getId).collect(Collectors.toList())); - Date date = new Date(); - stakeholder.setCreationDate(date); - stakeholder.setUpdateDate(date); - stakeholderFull.setCreationDate(date); - stakeholderFull.setUpdateDate(date); - stakeholder = stakeholderDAO.save(stakeholder); - stakeholderFull.setId(stakeholder.getId()); - Portal portal = portalService.getPortal(stakeholderFull.getAlias()); + log.debug("Alias: " + stakeholder.getAlias()); + stakeholder = this.stakeholderService.buildStakeholder(stakeholder); + Portal portal = portalService.getPortal(stakeholder.getAlias()); if (portal == null) { portal = new Portal(); - portal.setPid(stakeholderFull.getAlias()); - portal.setName(stakeholderFull.getName()); - portal.setType(stakeholderFull.getType()); + portal.setPid(stakeholder.getAlias()); + portal.setName(stakeholder.getName()); + portal.setType(stakeholder.getType()); portalService.insertPortal(portal); } - return stakeholderFull; - }*/ + return stakeholder; + } @PreAuthorize("hasAnyAuthority(" + "@AuthorizationService.PORTAL_ADMIN)") @RequestMapping(value = "/stakeholder/all", method = RequestMethod.GET) @@ -77,7 +72,7 @@ public class StakeholderController { @RequestMapping(value = "/stakeholder/default", method = RequestMethod.GET) public List getAllDefaultStakeholders(@RequestParam(required = false) String type) { return this.stakeholderService.getAllDefaultByRole(type).stream() - .map(stakeholder -> this.stakeholderService.getFullStakeholder(stakeholder)) + .map(this.stakeholderService::getFullStakeholder) .collect(Collectors.toList()); } @@ -102,106 +97,37 @@ public class StakeholderController { return stakeholder; } - /*@PreAuthorize("hasAnyAuthority(" + @PreAuthorize("hasAnyAuthority(" + "@AuthorizationService.PORTAL_ADMIN, " - + "@AuthorizationService.curator(#_stakeholder.getType()), " - + "@AuthorizationService.manager(#_stakeholder.getType(), #_stakeholder.getAlias()) " + + "@AuthorizationService.curator(#stakeholder.getType()), " + + "@AuthorizationService.manager(#stakeholder.getType(), #stakeholder.getAlias()) " + ")") @RequestMapping(value = "/save", method = RequestMethod.POST) - public Stakeholder saveStakeholder(@RequestBody Stakeholder stakeholder) { + public StakeholderFull saveStakeholder(@RequestBody StakeholderFull stakeholder) { log.debug("save stakeholder"); log.debug("Alias: " + stakeholder.getAlias() + " - Id: " + stakeholder.getId()); - Date date = new Date(); - stakeholder.setUpdateDate(date); - List topics = new ArrayList<>(); - if (stakeholder.getId() == null) { - stakeholder.setCreationDate(date); - } else { - Stakeholder oldStakeholder = stakeholderDAO.findById(stakeholder.getId()); - if (oldStakeholder == null) { - // EXCEPTION - Stakeholder not found - throw new EntityNotFoundException("save stakeholder: Stakeholder with id: " + stakeholder.getId() + " not found"); - } - for (String topicId : oldStakeholder.getTopics()) { - Topic topic = topicDAO.findById(topicId); - if (topic == null) { - // EXCEPTION - Topic not found - throw new EntityNotFoundException("Save stakeholder: Topic with id: " + topicId + " not found (topic exists in stakeholder: " + stakeholder.getId() + ")"); - } - topics.add(topic.getId()); - } - } - stakeholder.setTopics(topics); - return stakeholderDAO.save(stakeholder); - }*/ - - /* @PreAuthorize("isAuthenticated()") - @RequestMapping(value = "/{stakeholderId}/delete", method = RequestMethod.DELETE) - public boolean deleteStakeholder(@PathVariable("stakeholderId") String stakeholderId) { - log.debug("delete stakeholder"); - log.debug("Id: " + stakeholderId); - - Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId); - String pid = null; - - if (stakeholder != null) { - pid = stakeholder.getAlias(); - if (!rolesUtils.hasCreateAndDeleteAuthority(stakeholder.getType())) { - // EXCEPTION - Access denied - throw new ForbiddenException("Delete stakeholder: You are not authorized to delete stakeholder with id: " + stakeholderId); - } - topicController.deleteTree(stakeholder); - stakeholder.setTopics(null); - stakeholderDAO.delete(stakeholderId); - log.debug("Stakeholder deleted!"); - Portal portal = portalService.getPortal(pid); - if (portal != null) { - portalService.deletePortal(portal.getId()); - } - } else { - // EXCEPTION - Stakeholder not found - throw new EntityNotFoundException("Delete stakeholder: Stakeholder with id: " + stakeholderId + " not found"); - } - return true; - }*/ - - /*@PreAuthorize("isAuthenticated()") - @RequestMapping(value = "/{stakeholderId}/change-visibility", method = RequestMethod.POST) - public Stakeholder changeStakeholderVisibility(@PathVariable("stakeholderId") String stakeholderId, - @RequestParam("visibility") Visibility visibility, @RequestParam(required = false) Boolean propagate) { - log.debug("change stakeholder visibility: " + visibility + " - toggle propagate: " + ((propagate != null && propagate) ? "true" : "false")); - log.debug("Stakeholder: " + stakeholderId); - - Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId); - if (stakeholder == null) { - // EXCEPTION - Stakeholder not found - throw new EntityNotFoundException("Change stakeholder visibility: Stakeholder with id: " + stakeholderId + " not found"); - } - if (!rolesUtils.hasUpdateAuthority(stakeholder.getType(), stakeholder.getAlias())) { - // EXCEPTION - Access denied - throw new ForbiddenException("Change stakeholder visibility: You are not authorized to update stakeholder with id: " + stakeholderId); - } - - return changeStakeholderVisibilityTree(stakeholder, visibility, propagate); + stakeholder.update(this.stakeholderService.save(new Stakeholder(stakeholder))); + return stakeholder; } - private Stakeholder changeStakeholderVisibilityTree(Stakeholder stakeholder, Visibility visibility, Boolean propagate) { - Stakeholder stakeholderFull = new Stakeholder<>(stakeholder); - List topicsFull = new ArrayList<>(); - - if (propagate != null && propagate) { - for (String topicId : stakeholder.getTopics()) { - topicsFull.add(topicController.changeVisibilityTree(topicId, visibility, propagate)); - } + @PreAuthorize("isAuthenticated()") + @RequestMapping(value = "/{stakeholderId}/delete", method = RequestMethod.DELETE) + public boolean deleteStakeholder(@PathVariable("stakeholderId") String id) { + log.debug("delete stakeholder"); + log.debug("Id: " + id); + Portal portal = portalService.getPortal(this.stakeholderService.delete(id)); + if (portal != null) { + portalService.deletePortal(portal.getId()); } + return true; + } - stakeholder.setVisibility(visibility); - stakeholderDAO.save(stakeholder); - log.debug("Stakeholder toggled!"); - - stakeholderFull.setVisibility(visibility); - stakeholderFull.setTopics(topicsFull); - - return stakeholder; - }*/ + @PreAuthorize("isAuthenticated()") + @RequestMapping(value = "/{stakeholderId}/change-visibility", method = RequestMethod.POST) + public StakeholderFull changeStakeholderVisibility(@PathVariable("stakeholderId") String stakeholderId, + @RequestParam("visibility") Visibility visibility, @RequestParam(defaultValue = "false") Boolean propagate) { + log.debug("change stakeholder visibility: " + visibility + " - toggle propagate: " + ((propagate != null && propagate) ? "true" : "false")); + log.debug("Stakeholder: " + stakeholderId); + return this.stakeholderService.changeVisibility(stakeholderId, visibility, propagate); + } } diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/TopicController.java b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/TopicController.java index 4099a67..c156be3 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/TopicController.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/TopicController.java @@ -1,71 +1,32 @@ -/* package eu.dnetlib.uoamonitorservice.controllers; -import eu.dnetlib.uoaadmintoolslibrary.handlers.utils.RolesUtils; -import eu.dnetlib.uoamonitorservice.dao.*; import eu.dnetlib.uoamonitorservice.dto.TopicFull; -import eu.dnetlib.uoamonitorservice.entities.*; -import eu.dnetlib.uoamonitorservice.handlers.EntityNotFoundException; -import eu.dnetlib.uoaadmintoolslibrary.handlers.ForbiddenException; -import eu.dnetlib.uoamonitorservice.handlers.PathNotValidException; +import eu.dnetlib.uoamonitorservice.entities.Stakeholder; +import eu.dnetlib.uoamonitorservice.entities.Topic; import eu.dnetlib.uoamonitorservice.primitives.Visibility; +import eu.dnetlib.uoamonitorservice.service.StakeholderService; +import eu.dnetlib.uoamonitorservice.service.TopicService; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.web.bind.annotation.*; -import java.util.ArrayList; -import java.util.Date; -import java.util.Iterator; -import java.util.List; - @RestController @CrossOrigin(origins = "*") public class TopicController { private final Logger log = LogManager.getLogger(this.getClass()); - @Autowired - private RolesUtils rolesUtils; + private TopicService topicService; + private StakeholderService stakeholderService; @Autowired - private StakeholderDAO stakeholderDAO; - - @Autowired - private TopicDAO topicDAO; - - @Autowired - private CategoryController categoryController; - - @Autowired - private CategoryDAO categoryDAO; - - public TopicFull buildTopic(TopicFull topicFull) { - Topic topic = new Topic(topicFull); - List categories = new ArrayList<>(); - List categoriesFull = new ArrayList<>(); - for(Category category : topicFull.getCategories()) { - Category categoryFull = categoryController.buildCategory(category); - categoriesFull.add(categoryFull); - categories.add(categoryFull.getId()); - } - topicFull.setCategories(categoriesFull); - topic.setCategories(categories); - - Date date = new Date(); - topic.setCreationDate(date); - topic.setUpdateDate(date); - - topicFull.setCreationDate(date); - topicFull.setUpdateDate(date); - - topicDAO.save(topic); - - topicFull.setId(topic.getId()); - return topicFull; + public TopicController(TopicService topicService, StakeholderService stakeholderService) { + this.topicService = topicService; + this.stakeholderService = stakeholderService; } - @PreAuthorize("isAuthenticated()") + /*@PreAuthorize("isAuthenticated()") @RequestMapping(value = "/{stakeholderId}/save", method = RequestMethod.POST) public Topic saveTopic(@PathVariable("stakeholderId") String stakeholderId, @RequestBody Topic topicFull) { @@ -190,7 +151,7 @@ public class TopicController { topicBasedOnDefault.setUpdateDate(topic.getUpdateDate()); topicDAO.save(topicBasedOnDefault); } - } + }*/ @PreAuthorize("isAuthenticated()") @RequestMapping(value = "/{stakeholderId}/{topicId}/delete", method = RequestMethod.DELETE) @@ -198,100 +159,15 @@ public class TopicController { @PathVariable("topicId") String topicId, @RequestParam(required = false) String children) { log.debug("delete topic"); - log.debug("Id: "+topicId + " - Stakeholder: "+stakeholderId); - - Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId); - - if(stakeholder != null) { - - if(!rolesUtils.hasUpdateAuthority(stakeholder.getType(), stakeholder.getAlias())) { - // EXCEPTION - Access denied - throw new ForbiddenException("Delete topic: You are not authorized to update stakeholder with id: "+stakeholderId); - } - - Topic topic = topicDAO.findById(topicId); - if(topic != null) { - - if(topic.getDefaultId() != null && !rolesUtils.hasCreateAndDeleteAuthority(stakeholder.getType())) { - // EXCEPTION - Access denied - throw new ForbiddenException("Delete topic: You are not authorized to delete a default Topic in stakeholder with id: "+stakeholderId); - } - - List topics = stakeholder.getTopics(); - int index = topics.indexOf(topicId); - if(index != -1) { - // this topic belongs in default profile - if(stakeholder.getDefaultId() == null && children != null) { - onDeleteDefaultTopic(topicId, stakeholderId, children); - } - categoryController.deleteTree(topic); - - topic.setCategories(null); - - topics.remove(index); - stakeholderDAO.save(stakeholder); - - topicDAO.delete(topicId); - log.debug("Topic 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"); - } + log.debug("Id: " + topicId + " - Stakeholder: " + stakeholderId); + Stakeholder stakeholder = stakeholderService.findById(stakeholderId); + Topic topic = this.topicService.findByPath(stakeholder, topicId); + this.topicService.delete(stakeholder.getType(), topic, true); return true; } - public boolean onDeleteDefaultTopic(String defaultTopicId, String defaultStakeholderId, String children) { - if(children.equals("delete")) { - List stakeholders = stakeholderDAO.findByDefaultId(defaultStakeholderId); - List topics = topicDAO.findByDefaultId(defaultTopicId); - - for(Stakeholder stakeholder : stakeholders) { - Iterator topicsIterator = topics.iterator(); - while(topicsIterator.hasNext()) { - Topic topic = topicsIterator.next(); - - String topicId = topic.getId(); - - if(stakeholder.getTopics() != null && stakeholder.getTopics().contains(topicId)) { - topicsIterator.remove(); - - stakeholder.getTopics().remove(topicId); - stakeholderDAO.save(stakeholder); - - categoryController.deleteTree(topic); - - topicDAO.delete(topicId); - log.debug("Topic with id: "+topicId+" deleted!"); - - break; - } - } - } - } else if(children.equals("disconnect")) { - List topics = topicDAO.findByDefaultId(defaultTopicId); - for(Topic topic : topics) { - categoryController.disConnectTree(topic); - - topic.setDefaultId(null); - topicDAO.save(topic); - - log.debug("DefaultId for Topic with id: "+topic.getId()+" cleared!"); - } - } - return true; - } - - @PreAuthorize("isAuthenticated()") + /*@PreAuthorize("isAuthenticated()") @RequestMapping(value = "/{stakeholderId}/reorder", method = RequestMethod.POST) public List reorderTopics(@PathVariable("stakeholderId") String stakeholderId, @RequestBody List topics) { @@ -332,73 +208,16 @@ public class TopicController { // EXCEPTION - Stakeholder not found throw new EntityNotFoundException("Reorder topics: Stakeholder with id: "+stakeholderId+" not found"); } - } + }*/ @PreAuthorize("isAuthenticated()") @RequestMapping(value = "/{stakeholderId}/{topicId}/change-visibility", method = RequestMethod.POST) - public Topic changeTopicVisibility(@PathVariable("stakeholderId") String stakeholderId, - @PathVariable("topicId") String topicId, - @RequestParam("visibility") Visibility visibility, @RequestParam(required = false) Boolean propagate) { - log.debug("change topic visibility: "+visibility + " - toggle propagate: "+((propagate != null && propagate) ? "true" : "false")); - log.debug("Stakeholder: "+stakeholderId + " - Topic: "+topicId); - - Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId); - - if (stakeholder != null) { - if(!rolesUtils.hasUpdateAuthority(stakeholder.getType(), stakeholder.getAlias())) { - // EXCEPTION - Access denied - throw new ForbiddenException("Toggle topic: You are not authorized to update stakeholder with id: "+stakeholderId); - } - - if (stakeholder.getTopics().contains(topicId)) { - return changeVisibilityTree(topicId, visibility, propagate); - } else { - // EXCEPTION - Topic not found in Stakeholder: stakeholder.getAlias(); - throw new PathNotValidException("Toggle topic: Topic with id: "+topicId+" not found in Stakeholder: "+stakeholderId); - } - } else { - // EXCEPTION - Stakeholder not found - throw new EntityNotFoundException("Toggle topic: Stakeholder with id: "+stakeholderId+" not found"); - } - } - - public Topic changeVisibilityTree(String topicId, Visibility visibility, Boolean propagate) { - Topic topic = topicDAO.findById(topicId); - if (topic == null) { - // EXCEPTION - Topic not found - throw new EntityNotFoundException("Change topic visibility: Topic with id: "+topicId+" not found"); - } - - Topic topicFull = new Topic(topic); - List categoriesFull = new ArrayList<>(); - - if(propagate != null && propagate) { - for (String categoryId : topic.getCategories()) { - categoriesFull.add(categoryController.changeVisibilityTree(categoryId, visibility, propagate)); - } - } - - topic.setVisibility(visibility); - topicDAO.save(topic); - log.debug("Topic toggled!"); - - topicFull.setVisibility(visibility); - topicFull.setCategories(categoriesFull); - - return topicFull; - } - - public void deleteTree(Stakeholder stakeholder) { - List topics = stakeholder.getTopics(); - for(String topicId : topics) { - Topic topic = topicDAO.findById(topicId); - if (topic == null) { - // EXCEPTION - Topic not found - throw new EntityNotFoundException("Topic delete tree: Topic with id: "+topicId+" not found (topic exists in stakeholder: "+stakeholder.getId()+")"); - } - categoryController.deleteTree(topic); - topicDAO.delete(topicId); - } + public TopicFull changeTopicVisibility(@PathVariable("stakeholderId") String stakeholderId, + @PathVariable("topicId") String topicId, + @RequestParam("visibility") Visibility visibility, @RequestParam(required = false) Boolean propagate) { + log.debug("change topic visibility: " + visibility + " - toggle propagate: " + ((propagate != null && propagate) ? "true" : "false")); + log.debug("Stakeholder: " + stakeholderId + " - Topic: " + topicId); + Stakeholder stakeholder = this.stakeholderService.findById(stakeholderId); + return this.topicService.changeVisibility(stakeholder.getType(), stakeholder.getAlias(), topicId, visibility, propagate); } } -*/ diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/dao/CategoryDAO.java b/src/main/java/eu/dnetlib/uoamonitorservice/dao/CategoryDAO.java index eb99550..4c3d755 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/dao/CategoryDAO.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/dao/CategoryDAO.java @@ -12,7 +12,7 @@ public interface CategoryDAO extends MongoRepository { List findAll(); List findByDefaultId(String DefaultId); - Optional findBySubCategoriesContaining(String subCategory); + List findBySubCategoriesContaining(String subCategory); Optional findById(String Id); diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/dao/SectionDAO.java b/src/main/java/eu/dnetlib/uoamonitorservice/dao/SectionDAO.java index bc27f31..1bd2d4b 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/dao/SectionDAO.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/dao/SectionDAO.java @@ -11,6 +11,7 @@ import java.util.Optional; public interface SectionDAO extends MongoRepository { List
findAll(); List
findByDefaultId(String DefaultId); + List
findByIndicatorsContaining(String id); Optional
findById(String Id); diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/dao/StakeholderDAO.java b/src/main/java/eu/dnetlib/uoamonitorservice/dao/StakeholderDAO.java index 2e9a14d..91a0307 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/dao/StakeholderDAO.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/dao/StakeholderDAO.java @@ -18,7 +18,7 @@ public interface StakeholderDAO extends MongoRepository { List findByDefaultIdNot(String DefaultId); List findByDefaultIdNotAndType(String DefaultId, String Type); - Optional findByTopicsContaining(String topic); + List findByTopicsContaining(String topic); Optional findById(String Id); Optional findByAlias(String Alias); diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/dao/SubCategoryDAO.java b/src/main/java/eu/dnetlib/uoamonitorservice/dao/SubCategoryDAO.java index 1b28c99..237f294 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/dao/SubCategoryDAO.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/dao/SubCategoryDAO.java @@ -12,6 +12,9 @@ public interface SubCategoryDAO extends MongoRepository { List findAll(); List findByDefaultId(String DefaultId); + List findByNumbersContaining(String id); + List findByChartsContaining(String id); + Optional findById(String Id); void delete(String Id); diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/dao/TopicDAO.java b/src/main/java/eu/dnetlib/uoamonitorservice/dao/TopicDAO.java index 4683610..9b7f3f0 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/dao/TopicDAO.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/dao/TopicDAO.java @@ -12,7 +12,7 @@ public interface TopicDAO extends MongoRepository { List findAll(); List findByDefaultId(String DefaultId); - Optional findByCategoriesContaining(String category); + List findByCategoriesContaining(String category); Optional findById(String Id); diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/dto/StakeholderFull.java b/src/main/java/eu/dnetlib/uoamonitorservice/dto/StakeholderFull.java index 1db6f7a..10f84b4 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/dto/StakeholderFull.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/dto/StakeholderFull.java @@ -7,6 +7,10 @@ import java.util.Objects; public class StakeholderFull extends StakeholderGeneric { + public StakeholderFull() { + super(); + } + public StakeholderFull(StakeholderGeneric stakeholder, List topics) { super(stakeholder); topics.removeIf(Objects::isNull); diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/entities/Category.java b/src/main/java/eu/dnetlib/uoamonitorservice/entities/Category.java index 3eb926e..21a3ec8 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/entities/Category.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/entities/Category.java @@ -2,10 +2,13 @@ package eu.dnetlib.uoamonitorservice.entities; import eu.dnetlib.uoamonitorservice.dto.CategoryFull; import eu.dnetlib.uoamonitorservice.generics.CategoryGeneric; +import eu.dnetlib.uoamonitorservice.generics.Common; import eu.dnetlib.uoamonitorservice.generics.TopicGeneric; import org.springframework.data.mongodb.core.mapping.Document; import java.util.ArrayList; +import java.util.Objects; +import java.util.stream.Collectors; @Document public class Category extends CategoryGeneric { @@ -15,6 +18,8 @@ public class Category extends CategoryGeneric { public Category(CategoryFull category) { super(category); + this.subCategories = category.getSubCategories().stream().map(Common::getId).collect(Collectors.toList()); + this.subCategories.removeIf(Objects::isNull); } public void copyFromDefault(CategoryGeneric defaultCategory) { diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/entities/Indicator.java b/src/main/java/eu/dnetlib/uoamonitorservice/entities/Indicator.java index 88467cb..a0ae38a 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/entities/Indicator.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/entities/Indicator.java @@ -14,19 +14,11 @@ import java.util.Date; import java.util.List; public class Indicator extends Common { - @Id - @JsonProperty("_id") - private String id; private String additionalDescription; private IndicatorType type; //number,chart private IndicatorSize width; //small,medium,large private IndicatorSize height = IndicatorSize.MEDIUM; //small,medium,large private List tags; // this field is not used anywhere now - @CreatedDate - private Date creationDate; - @LastModifiedDate - private Date updateDate; - private String defaultId; private List indicatorPaths; public void copyFromDefault(Indicator defaultIndicator, Visibility visibility) { @@ -44,14 +36,6 @@ public class Indicator extends Common { setIndicatorPaths(defaultIndicator.getIndicatorPaths()); } - public String getId() { - return id; - } - - public void setId(String id) { - this.id = id; - } - public String getAdditionalDescription() { return additionalDescription; } @@ -100,30 +84,6 @@ public class Indicator extends Common { this.tags = tags; } - public Date getCreationDate() { - return creationDate; - } - - public void setCreationDate(Date creationDate) { - this.creationDate = creationDate; - } - - public Date getUpdateDate() { - return updateDate; - } - - public void setUpdateDate(Date updateDate) { - this.updateDate = updateDate; - } - - public String getDefaultId() { - return defaultId; - } - - public void setDefaultId(String defaultId) { - this.defaultId = defaultId; - } - public List getIndicatorPaths() { return indicatorPaths; } @@ -131,8 +91,4 @@ public class Indicator extends Common { public void setIndicatorPaths(List indicatorPaths) { this.indicatorPaths = indicatorPaths; } - -// public String hasType() { -// return this.type.name(); -// } } diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/entities/Section.java b/src/main/java/eu/dnetlib/uoamonitorservice/entities/Section.java index 667e8bd..5f9e4de 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/entities/Section.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/entities/Section.java @@ -14,8 +14,7 @@ public class Section extends SectionGeneric { public Section(SectionFull section) { super(section); - List indicators = section.getIndicators().stream().map(Indicator::getId).collect(Collectors.toList()); - indicators.removeIf(Objects::isNull); - this.setIndicators(indicators); + this.indicators = section.getIndicators().stream().map(Indicator::getId).collect(Collectors.toList()); + this.indicators.removeIf(Objects::isNull); } } diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/entities/Stakeholder.java b/src/main/java/eu/dnetlib/uoamonitorservice/entities/Stakeholder.java index bd66180..184dc8c 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/entities/Stakeholder.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/entities/Stakeholder.java @@ -1,8 +1,13 @@ package eu.dnetlib.uoamonitorservice.entities; +import eu.dnetlib.uoamonitorservice.dto.StakeholderFull; +import eu.dnetlib.uoamonitorservice.generics.Common; import eu.dnetlib.uoamonitorservice.generics.StakeholderGeneric; import org.springframework.data.mongodb.core.mapping.Document; +import java.util.Objects; +import java.util.stream.Collectors; + @Document public class Stakeholder extends StakeholderGeneric { @@ -10,7 +15,9 @@ public class Stakeholder extends StakeholderGeneric { super(); } - public Stakeholder(StakeholderGeneric stakeholder) { + public Stakeholder(StakeholderFull stakeholder) { super(stakeholder); + this.topics = stakeholder.getTopics().stream().map(Common::getId).collect(Collectors.toList()); + this.topics.removeIf(Objects::isNull); } } diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/entities/SubCategory.java b/src/main/java/eu/dnetlib/uoamonitorservice/entities/SubCategory.java index 7a67d66..1791cc2 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/entities/SubCategory.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/entities/SubCategory.java @@ -1,13 +1,22 @@ package eu.dnetlib.uoamonitorservice.entities; +import eu.dnetlib.uoamonitorservice.dto.SubCategoryFull; +import eu.dnetlib.uoamonitorservice.generics.Common; import eu.dnetlib.uoamonitorservice.generics.SubCategoryGeneric; import org.springframework.data.mongodb.core.mapping.Document; +import java.util.Objects; +import java.util.stream.Collectors; + @Document public class SubCategory extends SubCategoryGeneric { public SubCategory() {} - public SubCategory(SubCategoryGeneric subCategory) { + public SubCategory(SubCategoryFull subCategory) { super(subCategory); + this.numbers = subCategory.getNumbers().stream().map(Common::getId).collect(Collectors.toList()); + this.charts = subCategory.getCharts().stream().map(Common::getId).collect(Collectors.toList()); + this.numbers.removeIf(Objects::isNull); + this.charts.removeIf(Objects::isNull); } } diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/entities/Topic.java b/src/main/java/eu/dnetlib/uoamonitorservice/entities/Topic.java index a235cf9..e11ccb2 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/entities/Topic.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/entities/Topic.java @@ -1,15 +1,23 @@ package eu.dnetlib.uoamonitorservice.entities; +import eu.dnetlib.uoamonitorservice.dto.TopicFull; +import eu.dnetlib.uoamonitorservice.generics.Common; import eu.dnetlib.uoamonitorservice.generics.TopicGeneric; import org.springframework.data.mongodb.core.mapping.Document; +import java.util.Objects; +import java.util.stream.Collectors; + @Document public class Topic extends TopicGeneric { public Topic() { super(); } - public Topic(TopicGeneric topic) { + + public Topic(TopicFull topic) { super(topic); + this.categories = topic.getCategories().stream().map(Common::getId).collect(Collectors.toList()); + this.categories.removeIf(Objects::isNull); } } diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/generics/CategoryGeneric.java b/src/main/java/eu/dnetlib/uoamonitorservice/generics/CategoryGeneric.java index 0ba9657..f0e8f57 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/generics/CategoryGeneric.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/generics/CategoryGeneric.java @@ -14,15 +14,7 @@ import java.util.List; @Document public class CategoryGeneric extends Common { - @Id - @JsonProperty("_id") - protected String id; - @CreatedDate - protected Date creationDate; - @LastModifiedDate - protected Date updateDate; protected boolean isOverview; - protected String defaultId; protected List subCategories; public CategoryGeneric() {} @@ -38,30 +30,6 @@ public class CategoryGeneric extends Common { defaultId = category.getDefaultId(); } - public String getId() { - return id; - } - - public void setId(String id) { - this.id = id; - } - - public Date getCreationDate() { - return creationDate; - } - - public void setCreationDate(Date creationDate) { - this.creationDate = creationDate; - } - - public Date getUpdateDate() { - return updateDate; - } - - public void setUpdateDate(Date updateDate) { - this.updateDate = updateDate; - } - public boolean getIsOverview() { return isOverview; } @@ -70,14 +38,6 @@ public class CategoryGeneric extends Common { this.isOverview = isOverview; } - public String getDefaultId() { - return defaultId; - } - - public void setDefaultId(String defaultId) { - this.defaultId = defaultId; - } - public List getSubCategories() { return subCategories; } diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/generics/Common.java b/src/main/java/eu/dnetlib/uoamonitorservice/generics/Common.java index 35cb80b..8a77b1e 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/generics/Common.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/generics/Common.java @@ -1,13 +1,66 @@ package eu.dnetlib.uoamonitorservice.generics; +import com.fasterxml.jackson.annotation.JsonProperty; +import eu.dnetlib.uoamonitorservice.entities.Stakeholder; import eu.dnetlib.uoamonitorservice.primitives.Visibility; +import org.springframework.data.annotation.CreatedDate; +import org.springframework.data.annotation.Id; +import org.springframework.data.annotation.LastModifiedDate; + +import java.util.Date; public class Common { + @Id + @JsonProperty("_id") + protected String id; + @CreatedDate + protected Date creationDate; + @LastModifiedDate + protected Date updateDate; + protected String defaultId; protected String name; protected String description; protected String alias; protected Visibility visibility = Visibility.PRIVATE; + public void update(Common common) { + this.id = common.getId(); + this.creationDate = common.getCreationDate(); + this.updateDate = common.getUpdateDate(); + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public Date getCreationDate() { + return creationDate; + } + + public void setCreationDate(Date creationDate) { + this.creationDate = creationDate; + } + + public Date getUpdateDate() { + return updateDate; + } + + public void setUpdateDate(Date updateDate) { + this.updateDate = updateDate; + } + + public String getDefaultId() { + return defaultId; + } + + public void setDefaultId(String defaultId) { + this.defaultId = defaultId; + } + public String getName() { return name; } diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/generics/SectionGeneric.java b/src/main/java/eu/dnetlib/uoamonitorservice/generics/SectionGeneric.java index 995ac50..48132b5 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/generics/SectionGeneric.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/generics/SectionGeneric.java @@ -1,27 +1,14 @@ package eu.dnetlib.uoamonitorservice.generics; -import com.fasterxml.jackson.annotation.JsonProperty; import eu.dnetlib.uoamonitorservice.primitives.IndicatorType; -import org.springframework.data.annotation.CreatedDate; -import org.springframework.data.annotation.Id; -import org.springframework.data.annotation.LastModifiedDate; import java.util.ArrayList; -import java.util.Date; import java.util.List; -public class SectionGeneric { - @Id - @JsonProperty("_id") - protected String id; +public class SectionGeneric extends Common { protected String title; - protected String defaultId; protected String stakeholderAlias; protected IndicatorType type; - @CreatedDate - protected Date creationDate; - @LastModifiedDate - protected Date updateDate; protected List indicators; public SectionGeneric() { @@ -46,14 +33,6 @@ public class SectionGeneric { setIndicators(new ArrayList<>()); } - public String getId() { - return id; - } - - public void setId(String id) { - this.id = id; - } - public String getTitle() { return title; } @@ -62,14 +41,6 @@ public class SectionGeneric { this.title = title; } - public String getDefaultId() { - return defaultId; - } - - public void setDefaultId(String defaultId) { - this.defaultId = defaultId; - } - public String getStakeholderAlias() { return stakeholderAlias; } @@ -93,22 +64,6 @@ public class SectionGeneric { } } - public Date getCreationDate() { - return creationDate; - } - - public void setCreationDate(Date creationDate) { - this.creationDate = creationDate; - } - - public Date getUpdateDate() { - return updateDate; - } - - public void setUpdateDate(Date updateDate) { - this.updateDate = updateDate; - } - public List getIndicators() { return indicators; } diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/generics/StakeholderGeneric.java b/src/main/java/eu/dnetlib/uoamonitorservice/generics/StakeholderGeneric.java index a5912b9..7a3c40a 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/generics/StakeholderGeneric.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/generics/StakeholderGeneric.java @@ -13,9 +13,6 @@ import java.util.List; public class StakeholderGeneric extends Common { - @Id - @JsonProperty("_id") - protected String id; protected StakeholderType type; protected Date projectUpdateDate = null; protected String index_id; @@ -24,13 +21,8 @@ public class StakeholderGeneric extends Common { protected String statsProfile = "monitor"; protected String logoUrl; protected boolean isUpload = false; - protected String defaultId = null; protected Locale locale = Locale.EU; protected String funderType; - @CreatedDate - protected Date creationDate; - @LastModifiedDate - protected Date updateDate; protected List topics; public StakeholderGeneric() { @@ -57,14 +49,6 @@ public class StakeholderGeneric extends Common { updateDate = stakeholder.getUpdateDate(); } - public String getId() { - return id; - } - - public void setId(String id) { - this.id = id; - } - public String getType() { if (type == null) { return null; @@ -137,14 +121,6 @@ public class StakeholderGeneric extends Common { this.isUpload = isUpload; } - public String getDefaultId() { - return defaultId; - } - - public void setDefaultId(String defaultId) { - this.defaultId = defaultId; - } - public String getLocale() { return locale.label; } @@ -167,22 +143,6 @@ public class StakeholderGeneric extends Common { this.funderType = funderType; } - public Date getCreationDate() { - return creationDate; - } - - public void setCreationDate(Date creationDate) { - this.creationDate = creationDate; - } - - public Date getUpdateDate() { - return updateDate; - } - - public void setUpdateDate(Date updateDate) { - this.updateDate = updateDate; - } - public List getTopics() { return topics; } diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/generics/SubCategoryGeneric.java b/src/main/java/eu/dnetlib/uoamonitorservice/generics/SubCategoryGeneric.java index c285d17..5b45e6f 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/generics/SubCategoryGeneric.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/generics/SubCategoryGeneric.java @@ -12,14 +12,6 @@ import java.util.Date; import java.util.List; public class SubCategoryGeneric extends Common { - @Id - @JsonProperty("_id") - protected String id; - @CreatedDate - protected Date creationDate; - @LastModifiedDate - protected Date updateDate; - protected String defaultId; protected List charts; protected List numbers; @@ -53,40 +45,8 @@ public class SubCategoryGeneric extends Common { setCreationDate(defaultSubCategory.getCreationDate()); setUpdateDate(defaultSubCategory.getUpdateDate()); setDefaultId(defaultSubCategory.getId()); - setCharts(new ArrayList()); - setNumbers(new ArrayList()); - } - - public String getId() { - return id; - } - - public void setId(String id) { - this.id = id; - } - - public Date getCreationDate() { - return creationDate; - } - - public void setCreationDate(Date creationDate) { - this.creationDate = creationDate; - } - - public Date getUpdateDate() { - return updateDate; - } - - public void setUpdateDate(Date updateDate) { - this.updateDate = updateDate; - } - - public String getDefaultId() { - return defaultId; - } - - public void setDefaultId(String defaultId) { - this.defaultId = defaultId; + setCharts(new ArrayList<>()); + setNumbers(new ArrayList<>()); } public List getCharts() { diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/generics/TopicGeneric.java b/src/main/java/eu/dnetlib/uoamonitorservice/generics/TopicGeneric.java index 0ea1693..7dc1104 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/generics/TopicGeneric.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/generics/TopicGeneric.java @@ -11,15 +11,7 @@ import java.util.Date; import java.util.List; public class TopicGeneric extends Common { - @Id - @JsonProperty("_id") - protected String id; protected String icon; - @CreatedDate - protected Date creationDate; - @LastModifiedDate - protected Date updateDate; - protected String defaultId; protected List categories; public TopicGeneric() { @@ -49,14 +41,6 @@ public class TopicGeneric extends Common { setCategories(new ArrayList<>()); } - public String getId() { - return id; - } - - public void setId(String id) { - this.id = id; - } - public String getIcon() { return icon; @@ -66,30 +50,6 @@ public class TopicGeneric extends Common { this.icon = icon; } - public Date getCreationDate() { - return creationDate; - } - - public void setCreationDate(Date creationDate) { - this.creationDate = creationDate; - } - - public Date getUpdateDate() { - return updateDate; - } - - public void setUpdateDate(Date updateDate) { - this.updateDate = updateDate; - } - - public String getDefaultId() { - return defaultId; - } - - public void setDefaultId(String defaultId) { - this.defaultId = defaultId; - } - public List getCategories() { return categories; } diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/service/CategoryService.java b/src/main/java/eu/dnetlib/uoamonitorservice/service/CategoryService.java index cfb8e35..066e6ec 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/service/CategoryService.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/service/CategoryService.java @@ -1,14 +1,15 @@ package eu.dnetlib.uoamonitorservice.service; +import eu.dnetlib.uoaadmintoolslibrary.handlers.ForbiddenException; import eu.dnetlib.uoamonitorservice.dao.CategoryDAO; +import eu.dnetlib.uoamonitorservice.dao.TopicDAO; import eu.dnetlib.uoamonitorservice.dto.CategoryFull; -import eu.dnetlib.uoamonitorservice.dto.TopicFull; import eu.dnetlib.uoamonitorservice.entities.Category; import eu.dnetlib.uoamonitorservice.entities.Topic; import eu.dnetlib.uoamonitorservice.handlers.EntityNotFoundException; import eu.dnetlib.uoamonitorservice.handlers.PathNotValidException; +import eu.dnetlib.uoamonitorservice.primitives.Visibility; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.security.access.method.P; import org.springframework.stereotype.Service; import java.util.stream.Collectors; @@ -16,16 +17,18 @@ import java.util.stream.Collectors; @Service public class CategoryService { + private final TopicDAO topicDAO; private final CategoryDAO dao; private final SubCategoryService subCategoryService; private final CommonService commonService; @Autowired - public CategoryService(SubCategoryService subCategoryService, CommonService commonService, CategoryDAO dao) { + public CategoryService(TopicDAO topicDAO, CategoryDAO dao, SubCategoryService subCategoryService, CommonService commonService) { + this.topicDAO = topicDAO; + this.dao = dao; this.subCategoryService = subCategoryService; this.commonService = commonService; - this.dao = dao; } public Category findByPath(Topic topic, String categoryId, String message) { @@ -49,4 +52,61 @@ public class CategoryService { public Category find(String id) { return dao.findById(id).orElseThrow(() -> new EntityNotFoundException("Category with id: " + id + " not found")); } + + public CategoryFull buildCategory(CategoryFull categoryFull) { + categoryFull.setSubCategories(categoryFull.getSubCategories().stream().map(this.subCategoryService::buildSubcategory).collect(Collectors.toList())); + categoryFull.update(this.save(new Category(categoryFull))); + return categoryFull; + } + + public Category save(Category category) { + category.getSubCategories().forEach(this.subCategoryService::find); + return this.dao.save(category); + } + + public void delete(String type, Category category, boolean remove) { + if(this.commonService.hasDeleteAuthority(type)) { + this.dao.findByDefaultId(category.getId()).forEach(child -> { + this.delete(type, child.getId(), remove); + }); + category.getSubCategories().forEach(subcategoryId -> { + this.subCategoryService.delete(type, subcategoryId, false); + }); + if(remove){ + this.removeCategory(category.getId()); + } + this.dao.delete(category); + } else { + throw new ForbiddenException("Delete category: You are not authorized to delete category with id: " + category.getId()); + } + } + + public void delete(String type, String id, boolean remove) { + Category category = this.find(id); + this.delete(type, category, remove); + } + + public void removeCategory(String id) { + this.topicDAO.findByCategoriesContaining(id).forEach(topic -> { + topic.getCategories().remove(id); + this.topicDAO.save(topic); + }); + } + + public CategoryFull changeVisibility(String type, String alias, String id, Visibility visibility, Boolean propagate) { + if(this.commonService.hasEditAuthority(type, alias)) { + CategoryFull categoryFull = this.getFullCategory(type, alias, id); + categoryFull.setVisibility(visibility); + if(propagate) { + categoryFull.setSubCategories(categoryFull.getSubCategories().stream() + .map(category -> this.subCategoryService.changeVisibility(type, alias, category.getId(), visibility, true)) + .collect(Collectors.toList())); + } + Category category = this.save(new Category(categoryFull)); + categoryFull.update(category); + return categoryFull; + } else { + throw new ForbiddenException("Change category visibility: You are not authorized to update category with id: " + id); + } + } } diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/service/CommonService.java b/src/main/java/eu/dnetlib/uoamonitorservice/service/CommonService.java index cf43df1..5073704 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/service/CommonService.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/service/CommonService.java @@ -25,6 +25,18 @@ public class CommonService { return false; } + public boolean hasDeleteAuthority(String type) { + return this.authorizationService.isPortalAdmin() || this.authorizationService.isCurator(type); + } + + public boolean hasCreateAuthority(String type) { + return this.authorizationService.isPortalAdmin() || this.authorizationService.isCurator(type); + } + + public boolean hasEditAuthority(String type, String alias) { + return this.authorizationService.isPortalAdmin() || this.authorizationService.isCurator(type) || this.authorizationService.isManager(type, alias); + } + public boolean hasVisibilityAuthority(String type, String alias, Common common) { if(authorizationService.isPortalAdmin() || authorizationService.isCurator(type) || authorizationService.isManager(type, alias)) { return true; diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/service/IndicatorService.java b/src/main/java/eu/dnetlib/uoamonitorservice/service/IndicatorService.java index 06ed3d5..9407b3b 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/service/IndicatorService.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/service/IndicatorService.java @@ -1,20 +1,29 @@ package eu.dnetlib.uoamonitorservice.service; +import eu.dnetlib.uoaadmintoolslibrary.handlers.ForbiddenException; import eu.dnetlib.uoamonitorservice.dao.IndicatorDAO; +import eu.dnetlib.uoamonitorservice.dao.SectionDAO; +import eu.dnetlib.uoamonitorservice.dto.SectionFull; import eu.dnetlib.uoamonitorservice.entities.Indicator; +import eu.dnetlib.uoamonitorservice.entities.Section; import eu.dnetlib.uoamonitorservice.handlers.EntityNotFoundException; +import eu.dnetlib.uoamonitorservice.primitives.Visibility; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import java.util.stream.Collectors; + @Service public class IndicatorService { + private final SectionDAO sectionDAO; private final IndicatorDAO dao; private final CommonService commonService; @Autowired - public IndicatorService(IndicatorDAO dao, CommonService commonService) { + public IndicatorService(SectionDAO sectionDAO, IndicatorDAO dao, CommonService commonService) { + this.sectionDAO = sectionDAO; this.dao = dao; this.commonService = commonService; } @@ -35,4 +44,41 @@ public class IndicatorService { return null; } } + + public void delete(String type, Indicator indicator, boolean remove) { + if(this.commonService.hasDeleteAuthority(type)) { + this.dao.findByDefaultId(indicator.getId()).forEach(child -> { + this.delete(type, child.getId(), remove); + }); + if(remove) { + this.removeIndicator(indicator.getId()); + } + this.dao.delete(indicator); + } else { + throw new ForbiddenException("Delete indicator: You are not authorized to delete indicator with id: " + indicator.getId()); + } + } + + + public void delete(String type, String id, boolean remove) { + Indicator indicator = this.find(id); + this.delete(type, indicator, remove); + } + + public void removeIndicator(String id) { + this.sectionDAO.findByIndicatorsContaining(id).forEach(section -> { + section.getIndicators().remove(id); + this.sectionDAO.save(section); + }); + } + + public Indicator changeVisibility(String type, String alias, String id, Visibility visibility) { + if(this.commonService.hasEditAuthority(type, alias)) { + Indicator indicator = this.getIndicator(type, alias, id); + indicator.setVisibility(visibility); + return this.save(indicator); + } else { + throw new ForbiddenException("Change section visibility: You are not authorized to update section with id: " + id); + } + } } diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/service/SectionService.java b/src/main/java/eu/dnetlib/uoamonitorservice/service/SectionService.java index 40f9c7f..e1fda14 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/service/SectionService.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/service/SectionService.java @@ -1,6 +1,8 @@ package eu.dnetlib.uoamonitorservice.service; +import eu.dnetlib.uoaadmintoolslibrary.handlers.ForbiddenException; import eu.dnetlib.uoamonitorservice.dao.SectionDAO; +import eu.dnetlib.uoamonitorservice.dao.SubCategoryDAO; import eu.dnetlib.uoamonitorservice.dto.SectionFull; import eu.dnetlib.uoamonitorservice.entities.Indicator; import eu.dnetlib.uoamonitorservice.entities.Section; @@ -15,25 +17,23 @@ import java.util.stream.Collectors; @Service public class SectionService { + private final SubCategoryDAO subCategoryDAO; private final SectionDAO dao; private final IndicatorService indicatorService; + private final CommonService commonService; @Autowired - public SectionService(SectionDAO dao, IndicatorService indicatorService) { + public SectionService(SubCategoryDAO subCategoryDAO, SectionDAO dao, IndicatorService indicatorService, CommonService commonService) { + this.subCategoryDAO = subCategoryDAO; this.dao = dao; this.indicatorService = indicatorService; - } - - public SectionFull buildSection(SectionFull sectionFull) { - sectionFull.setIndicators(sectionFull.getIndicators().stream().map(indicator -> this.indicatorService.save(indicator)).collect(Collectors.toList())); - Section section = dao.save(new Section(sectionFull)); - return new SectionFull(section, sectionFull.getIndicators()); + this.commonService = commonService; } public SectionFull changeVisibilityTree(String sectionId, Visibility visibility) { Section section = this.find(sectionId); - SectionFull sectionFull = new SectionFull(section, section.getIndicators().stream().map(indicatorId -> this.indicatorService.find(indicatorId)).collect(Collectors.toList())); + SectionFull sectionFull = new SectionFull(section, section.getIndicators().stream().map(this.indicatorService::find).collect(Collectors.toList())); sectionFull.setIndicators(sectionFull.getIndicators().stream().map(indicator -> { indicator.setVisibility(visibility); return indicatorService.save(indicator); @@ -68,4 +68,62 @@ public class SectionService { public List getIndicatorsId(String id) { return this.getIndicators(id).stream().map(Indicator::getId).collect(Collectors.toList()); } + + public SectionFull buildSection(SectionFull sectionFull) { + sectionFull.setIndicators(sectionFull.getIndicators().stream().map(this.indicatorService::save).collect(Collectors.toList())); + Section section = this.save(new Section(sectionFull)); + return new SectionFull(section, sectionFull.getIndicators()); + } + + public Section save(Section section) { + section.getIndicators().forEach(this.indicatorService::find); + return this.dao.save(section); + } + + public void delete(String type, Section section, boolean remove) { + if(this.commonService.hasDeleteAuthority(type)) { + this.dao.findByDefaultId(section.getId()).forEach(child -> { + this.delete(type, child.getId(), remove); + }); + section.getIndicators().forEach(indicatorId -> { + this.indicatorService.delete(type, indicatorId, false); + }); + if (remove) { + this.removeSection(section.getId()); + } + this.dao.delete(section); + } else { + throw new ForbiddenException("Delete section: You are not authorized to delete section with id: " + section.getId()); + } + } + + public void delete(String type, String id, boolean remove) { + Section section = this.find(id); + this.delete(type, section, remove); + } + + public void removeSection(String id) { + this.subCategoryDAO.findByNumbersContaining(id).forEach(subCategory -> { + subCategory.getNumbers().remove(id); + this.subCategoryDAO.save(subCategory); + }); + this.subCategoryDAO.findByChartsContaining(id).forEach(subCategory -> { + subCategory.getCharts().remove(id); + this.subCategoryDAO.save(subCategory); + }); + } + + public SectionFull changeVisibility(String type, String alias, String id, Visibility visibility) { + if(this.commonService.hasEditAuthority(type, alias)) { + SectionFull sectionFull = this.getFullSection(type, alias, id); + sectionFull.setIndicators(sectionFull.getIndicators().stream() + .map(category -> this.indicatorService.changeVisibility(type, alias, category.getId(), visibility)) + .collect(Collectors.toList())); + Section section = this.save(new Section(sectionFull)); + sectionFull.update(section); + return sectionFull; + } else { + throw new ForbiddenException("Change section visibility: You are not authorized to update section with id: " + id); + } + } } diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/service/StakeholderService.java b/src/main/java/eu/dnetlib/uoamonitorservice/service/StakeholderService.java index fc94b40..dcaf1ee 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/service/StakeholderService.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/service/StakeholderService.java @@ -1,6 +1,6 @@ package eu.dnetlib.uoamonitorservice.service; -import eu.dnetlib.uoaauthorizationlibrary.security.AuthorizationService; +import eu.dnetlib.uoaadmintoolslibrary.handlers.ForbiddenException; import eu.dnetlib.uoamonitorservice.dao.StakeholderDAO; import eu.dnetlib.uoamonitorservice.dto.StakeholderFull; import eu.dnetlib.uoamonitorservice.entities.Stakeholder; @@ -27,10 +27,22 @@ public class StakeholderService { this.topicService = topicService; } + public Stakeholder findById(String id) { + return this.dao.findById(id).orElseThrow(() -> new EntityNotFoundException("Stakeholder with id: " + id + " not found")); + } + public Stakeholder findByAlias(String alias) { return this.dao.findByAlias(alias).orElseThrow(() -> new EntityNotFoundException("Stakeholder with alias: " + alias + " not found")); } + public List findByDefaultId(String id ){ + return this.dao.findByDefaultId(id); + } + + public Stakeholder findByPath(String stakeholderId) { + return dao.findById(stakeholderId).orElseThrow(() -> new EntityNotFoundException("Stakeholder with id: " + stakeholderId + " not found")); + } + public List getAllAliases() { return this.dao.findAll().stream().map(Stakeholder::getAlias).collect(Collectors.toList()); } @@ -65,10 +77,6 @@ public class StakeholderService { .collect(Collectors.toList()); } - public Stakeholder findByPath(String stakeholderId) { - return dao.findById(stakeholderId).orElseThrow(() -> new EntityNotFoundException("Stakeholder with id: " + stakeholderId + " not found")); - } - public StakeholderFull getFullStakeholder(Stakeholder stakeholder) { if(this.commonService.hasAccessAuthority(stakeholder.getType(), stakeholder.getAlias(), stakeholder.getDefaultId() == null) || (stakeholder.getDefaultId() != null && this.commonService.hasVisibilityAuthority(stakeholder.getType(), stakeholder.getAlias(), stakeholder))) { @@ -80,4 +88,49 @@ public class StakeholderService { return null; } } + + public StakeholderFull buildStakeholder(StakeholderFull stakeholderFull) { + stakeholderFull.setTopics(stakeholderFull.getTopics().stream().map(this.topicService::buildTopic).collect(Collectors.toList())); + stakeholderFull.update(this.save(new Stakeholder(stakeholderFull))); + return stakeholderFull; + } + + public Stakeholder save(Stakeholder stakeholder) { + stakeholder.getTopics().forEach(this.topicService::find); + return this.dao.save(stakeholder); + } + + public String delete(String id) { + Stakeholder stakeholder = this.findById(id); + if(this.commonService.hasDeleteAuthority(stakeholder.getType())) { + this.dao.findByDefaultId(stakeholder.getId()).forEach(child -> { + this.delete(child.getId()); + }); + stakeholder.getTopics().forEach(topicId -> { + this.topicService.delete(stakeholder.getType(), topicId, false); + }); + this.dao.delete(id); + return stakeholder.getAlias(); + } else { + throw new ForbiddenException("Delete stakeholder: You are not authorized to delete stakeholder with id: " + id); + } + } + + public StakeholderFull changeVisibility(String id, Visibility visibility, Boolean propagate) { + Stakeholder stakeholder = this.findById(id); + stakeholder.setVisibility(visibility); + if(this.commonService.hasEditAuthority(stakeholder.getType(), stakeholder.getAlias())) { + StakeholderFull stakeholderFull = this.getFullStakeholder(stakeholder); + if(propagate) { + stakeholderFull.setTopics(stakeholder.getTopics().stream(). + map(topicId -> this.topicService.changeVisibility(stakeholderFull.getType(), stakeholderFull.getAlias(), topicId, visibility, true)) + .collect(Collectors.toList())); + } + stakeholder = this.save(stakeholder); + stakeholderFull.update(stakeholder); + return stakeholderFull; + } else { + throw new ForbiddenException("Change stakeholder visibility: You are not authorized to update stakeholder with id: " + id); + } + } } diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/service/SubCategoryService.java b/src/main/java/eu/dnetlib/uoamonitorservice/service/SubCategoryService.java index 5307049..d036819 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/service/SubCategoryService.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/service/SubCategoryService.java @@ -1,13 +1,21 @@ package eu.dnetlib.uoamonitorservice.service; +import eu.dnetlib.uoaadmintoolslibrary.handlers.ForbiddenException; +import eu.dnetlib.uoamonitorservice.dao.CategoryDAO; import eu.dnetlib.uoamonitorservice.dao.SubCategoryDAO; +import eu.dnetlib.uoamonitorservice.dto.CategoryFull; import eu.dnetlib.uoamonitorservice.dto.SubCategoryFull; import eu.dnetlib.uoamonitorservice.dto.TopicFull; import eu.dnetlib.uoamonitorservice.entities.Category; import eu.dnetlib.uoamonitorservice.entities.SubCategory; import eu.dnetlib.uoamonitorservice.entities.Topic; +import eu.dnetlib.uoamonitorservice.generics.Common; +import eu.dnetlib.uoamonitorservice.generics.SectionGeneric; +import eu.dnetlib.uoamonitorservice.generics.SubCategoryGeneric; import eu.dnetlib.uoamonitorservice.handlers.EntityNotFoundException; import eu.dnetlib.uoamonitorservice.handlers.PathNotValidException; +import eu.dnetlib.uoamonitorservice.primitives.IndicatorType; +import eu.dnetlib.uoamonitorservice.primitives.Visibility; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -16,15 +24,17 @@ import java.util.stream.Collectors; @Service public class SubCategoryService { + private final CategoryDAO categoryDAO; private final SubCategoryDAO dao; private final CommonService commonService; private final SectionService sectionService; @Autowired - public SubCategoryService( CommonService commonService, SubCategoryDAO dao, SectionService sectionService) { - this.commonService = commonService; + public SubCategoryService(CategoryDAO categoryDAO, SubCategoryDAO dao,CommonService commonService, SectionService sectionService) { + this.categoryDAO = categoryDAO; this.dao = dao; + this.commonService = commonService; this.sectionService = sectionService; } @@ -52,4 +62,70 @@ public class SubCategoryService { public SubCategory find(String id) { return dao.findById(id).orElseThrow(() -> new EntityNotFoundException("SubCategory with id: " + id + " not found")); } + + public SubCategoryFull buildSubcategory(SubCategoryFull subCategoryFull) { + subCategoryFull.setNumbers(subCategoryFull.getNumbers().stream().map(this.sectionService::buildSection).collect(Collectors.toList())); + subCategoryFull.setCharts(subCategoryFull.getCharts().stream().map(this.sectionService::buildSection).collect(Collectors.toList())); + subCategoryFull.update(this.save(new SubCategory(subCategoryFull))); + return subCategoryFull; + } + + public SubCategory save(SubCategory subCategory) { + subCategory.getNumbers().forEach(this.sectionService::find); + subCategory.getCharts().forEach(this.sectionService::find); + return this.dao.save(subCategory); + } + + public void delete(String type, SubCategory subCategory, boolean remove ) { + if(this.commonService.hasDeleteAuthority(type)) { + this.dao.findByDefaultId(subCategory.getId()).forEach(child -> { + this.delete(type, child.getId(), remove); + }); + + subCategory.getNumbers().forEach(sectionId -> { + this.sectionService.delete(type, sectionId, false); + }); + subCategory.getCharts().forEach(sectionId -> { + this.sectionService.delete(type, sectionId, false); + }); + if (remove) { + this.removeSubCategory(subCategory.getId()); + } + this.dao.delete(subCategory); + } else { + throw new ForbiddenException("Delete subCategory: You are not authorized to delete subCategory with id: " + subCategory.getId()); + } + } + + public void delete(String type, String id, boolean remove) { + SubCategory subCategory = this.find(id); + this.delete(type, subCategory, remove); + } + + public void removeSubCategory(String id) { + this.categoryDAO.findBySubCategoriesContaining(id).forEach(category -> { + category.getSubCategories().remove(id); + this.categoryDAO.save(category); + }); + } + + public SubCategoryFull changeVisibility(String type, String alias, String id, Visibility visibility, Boolean propagate) { + if(this.commonService.hasEditAuthority(type, alias)) { + SubCategoryFull subCategoryFull = this.getFullSubCategory(type, alias, id); + subCategoryFull.setVisibility(visibility); + if(propagate) { + subCategoryFull.setNumbers(subCategoryFull.getNumbers().stream() + .map(category -> this.sectionService.changeVisibility(type, alias, category.getId(), visibility)) + .collect(Collectors.toList())); + subCategoryFull.setCharts(subCategoryFull.getCharts().stream() + .map(category -> this.sectionService.changeVisibility(type, alias, category.getId(), visibility)) + .collect(Collectors.toList())); + } + SubCategory subCategory = this.save(new SubCategory(subCategoryFull)); + subCategoryFull.update(subCategory); + return subCategoryFull; + } else { + throw new ForbiddenException("Change subCategory visibility: You are not authorized to update subCategory with id: " + id); + } + } } diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/service/TopicService.java b/src/main/java/eu/dnetlib/uoamonitorservice/service/TopicService.java index 1c0024e..2c2931c 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/service/TopicService.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/service/TopicService.java @@ -1,15 +1,15 @@ package eu.dnetlib.uoamonitorservice.service; +import eu.dnetlib.uoaadmintoolslibrary.handlers.ForbiddenException; +import eu.dnetlib.uoamonitorservice.dao.StakeholderDAO; import eu.dnetlib.uoamonitorservice.dao.TopicDAO; import eu.dnetlib.uoamonitorservice.dto.TopicFull; -import eu.dnetlib.uoamonitorservice.entities.Indicator; import eu.dnetlib.uoamonitorservice.entities.Stakeholder; import eu.dnetlib.uoamonitorservice.entities.Topic; -import eu.dnetlib.uoamonitorservice.generics.Common; import eu.dnetlib.uoamonitorservice.handlers.EntityNotFoundException; import eu.dnetlib.uoamonitorservice.handlers.PathNotValidException; +import eu.dnetlib.uoamonitorservice.primitives.Visibility; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.security.access.method.P; import org.springframework.stereotype.Service; import java.util.stream.Collectors; @@ -17,31 +17,33 @@ import java.util.stream.Collectors; @Service public class TopicService { + private final StakeholderDAO stakeholderDAO; private final TopicDAO dao; private final CategoryService categoryService; private final CommonService commonService; @Autowired - public TopicService(TopicDAO dao, CategoryService categoryService, CommonService commonService) { + public TopicService(StakeholderDAO stakeholderDAO, TopicDAO dao, CategoryService categoryService, CommonService commonService) { + this.stakeholderDAO = stakeholderDAO; this.dao = dao; this.categoryService = categoryService; this.commonService = commonService; } - public Topic findByPath(Stakeholder stakeholder, String topicId, String message) { + public Topic findByPath(Stakeholder stakeholder, String topicId) { if (!stakeholder.getTopics().contains(topicId)) { - throw new PathNotValidException(message + ": Topic with id: " + topicId + " not found in Stakeholder: " + stakeholder.getId()); + throw new PathNotValidException("Topic with id: " + topicId + " not found in Stakeholder: " + stakeholder.getId()); } - return this.dao.findById(topicId).orElseThrow(() -> new EntityNotFoundException(message + ": Topic with id: " + topicId + " not found")); + return this.dao.findById(topicId).orElseThrow(() -> new EntityNotFoundException("Topic with id: " + topicId + " not found")); } public TopicFull getFullTopic(String type, String alias, String id) { Topic topic = this.find(id); - if(commonService.hasVisibilityAuthority(type, alias, topic)) { - return new TopicFull(topic, topic.getCategories().stream() - .map(categoryId -> this.categoryService.getFullCategory(type, alias, categoryId)) - .collect(Collectors.toList())); + if (commonService.hasVisibilityAuthority(type, alias, topic)) { + return new TopicFull(topic, topic.getCategories().stream() + .map(categoryId -> this.categoryService.getFullCategory(type, alias, categoryId)) + .collect(Collectors.toList())); } else { return null; } @@ -50,4 +52,61 @@ public class TopicService { public Topic find(String id) { return dao.findById(id).orElseThrow(() -> new EntityNotFoundException("Topic with id: " + id + " not found")); } + + public TopicFull buildTopic(TopicFull topicFull) { + topicFull.setCategories(topicFull.getCategories().stream().map(this.categoryService::buildCategory).collect(Collectors.toList())); + topicFull.update(this.save(new Topic(topicFull))); + return topicFull; + } + + public Topic save(Topic topic) { + topic.getCategories().forEach(this.categoryService::find); + return this.dao.save(topic); + } + + public void delete(String type, Topic topic, boolean remove) { + if (this.commonService.hasDeleteAuthority(type)) { + this.dao.findByDefaultId(topic.getId()).forEach(child -> { + this.delete(type, child.getId(), remove); + }); + topic.getCategories().forEach(categoryId -> { + this.categoryService.delete(type, categoryId, false); + }); + if(remove) { + this.removeTopic(topic.getId()); + } + this.dao.delete(topic); + } else { + throw new ForbiddenException("Delete topic: You are not authorized to delete topic with id: " + topic.getId()); + } + } + + public void delete(String type, String id, boolean remove) { + Topic topic = this.find(id); + this.delete(type, topic, remove); + } + + public void removeTopic(String id) { + this.stakeholderDAO.findByTopicsContaining(id).forEach(stakeholder -> { + stakeholder.getTopics().remove(id); + this.stakeholderDAO.save(stakeholder); + }); + } + + public TopicFull changeVisibility(String type, String alias, String id, Visibility visibility, Boolean propagate) { + if (this.commonService.hasEditAuthority(type, alias)) { + TopicFull topicFull = this.getFullTopic(type, alias, id); + topicFull.setVisibility(visibility); + if (propagate) { + topicFull.setCategories(topicFull.getCategories().stream() + .map(category -> this.categoryService.changeVisibility(type, alias, category.getId(), visibility, true)) + .collect(Collectors.toList())); + } + Topic topic = this.save(new Topic(topicFull)); + topicFull.update(topic); + return topicFull; + } else { + throw new ForbiddenException("Change topic visibility: You are not authorized to update topic with id: " + id); + } + } } From cf505bd214bed9f9a4fc8981f4d43d1045235abd Mon Sep 17 00:00:00 2001 From: "k.triantafyllou" Date: Thu, 14 Mar 2024 19:01:28 +0200 Subject: [PATCH 06/11] Fix delete and change visibility methods for all entities. --- .../controllers/CategoryController.java | 317 ++---------------- .../controllers/IndicatorController.java | 261 ++------------ .../controllers/SectionController.java | 222 ++++++------ .../controllers/SubCategoryController.java | 316 +++-------------- .../controllers/TopicController.java | 9 +- .../service/CategoryService.java | 52 +-- .../service/IndicatorService.java | 19 +- .../service/SectionService.java | 41 +-- .../service/StakeholderService.java | 4 +- .../service/SubCategoryService.java | 45 ++- .../service/TopicService.java | 36 +- 11 files changed, 336 insertions(+), 986 deletions(-) diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/CategoryController.java b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/CategoryController.java index cc91eab..b6980d5 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/CategoryController.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/CategoryController.java @@ -1,77 +1,36 @@ -/* package eu.dnetlib.uoamonitorservice.controllers; -import eu.dnetlib.uoaadmintoolslibrary.handlers.ForbiddenException; -import eu.dnetlib.uoaadmintoolslibrary.handlers.utils.RolesUtils; -import eu.dnetlib.uoamonitorservice.dao.CategoryDAO; -import eu.dnetlib.uoamonitorservice.dao.StakeholderDAO; -import eu.dnetlib.uoamonitorservice.dao.SubCategoryDAO; -import eu.dnetlib.uoamonitorservice.dao.TopicDAO; -import eu.dnetlib.uoamonitorservice.entities.*; -import eu.dnetlib.uoamonitorservice.handlers.EntityNotFoundException; -import eu.dnetlib.uoamonitorservice.handlers.PathNotValidException; +import eu.dnetlib.uoamonitorservice.dto.CategoryFull; +import eu.dnetlib.uoamonitorservice.entities.Category; +import eu.dnetlib.uoamonitorservice.entities.Stakeholder; +import eu.dnetlib.uoamonitorservice.entities.Topic; import eu.dnetlib.uoamonitorservice.primitives.Visibility; +import eu.dnetlib.uoamonitorservice.service.CategoryService; +import eu.dnetlib.uoamonitorservice.service.StakeholderService; +import eu.dnetlib.uoamonitorservice.service.TopicService; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.web.bind.annotation.*; -import java.util.ArrayList; -import java.util.Date; -import java.util.Iterator; -import java.util.List; - @RestController @CrossOrigin(origins = "*") public class CategoryController { private final Logger log = LogManager.getLogger(this.getClass()); - @Autowired - private RolesUtils rolesUtils; + private final StakeholderService stakeholderService; + private final TopicService topicService; + private final CategoryService categoryService; @Autowired - private StakeholderDAO stakeholderDAO; - - @Autowired - private TopicDAO topicDAO; - - @Autowired - private CategoryDAO categoryDAO; - - @Autowired - private SubCategoryDAO subCategoryDAO; - - @Autowired - private SubCategoryController subCategoryController; - - public Category buildCategory(Category categoryFull) { - Category category = new Category<>(categoryFull); - - List subCategories = new ArrayList<>(); - List subCategoriesFull = new ArrayList<>(); - for (SubCategory> subCategory : categoryFull.getSubCategories()) { - SubCategory> subcategoryFull = subCategoryController.buildSubCategory(subCategory); - subCategoriesFull.add(subcategoryFull); - subCategories.add(subcategoryFull.getId()); - } - categoryFull.setSubCategories(subCategoriesFull); - category.setSubCategories(subCategories); - - Date date = new Date(); - category.setCreationDate(date); - category.setUpdateDate(date); - - categoryFull.setCreationDate(date); - categoryFull.setUpdateDate(date); - - categoryDAO.save(category); - - categoryFull.setId(category.getId()); - return categoryFull; + public CategoryController(StakeholderService stakeholderService, TopicService topicService, CategoryService categoryService) { + this.stakeholderService = stakeholderService; + this.topicService = topicService; + this.categoryService = categoryService; } - @PreAuthorize("isAuthenticated()") + /*@PreAuthorize("isAuthenticated()") @RequestMapping(value = "/{stakeholderId}/{topicId}/save", method = RequestMethod.POST) public Category saveCategory(@PathVariable("stakeholderId") String stakeholderId, @PathVariable("topicId") String topicId, @@ -170,9 +129,9 @@ public class CategoryController { throw new EntityNotFoundException("Save category: Stakeholder with id: " + stakeholderId + " not found"); } return categoryFull; - } + }*/ - public void onSaveDefaultCategory(Category category, String topicId) { + /*public void onSaveDefaultCategory(Category category, String topicId) { log.debug("On save default category"); List topics = topicDAO.findByDefaultId(topicId); @@ -190,9 +149,9 @@ public class CategoryController { String subCategoryOverviewId = category.getSubCategories().get(0); SubCategory subCategoryOverview = subCategoryDAO.findById(subCategoryOverviewId); subCategoryController.onSaveDefaultSubCategory(subCategoryOverview, category.getId()); - } + }*/ - public void onUpdateDefaultCategory(Category category, Category oldCategory) { + /*public void onUpdateDefaultCategory(Category category, Category oldCategory) { log.debug("On update default category"); List categories = categoryDAO.findByDefaultId(category.getId()); @@ -218,7 +177,7 @@ public class CategoryController { categoryBasedOnDefault.setUpdateDate(category.getUpdateDate()); categoryDAO.save(categoryBasedOnDefault); } - } + }*/ @PreAuthorize("isAuthenticated()") @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/delete", method = RequestMethod.DELETE) @@ -228,108 +187,14 @@ public class CategoryController { @RequestParam(required = false) String children) { log.debug("delete category"); log.debug("Id: " + categoryId + " - Stakeholder: " + stakeholderId + " - Topic: " + topicId); - - Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId); - - if (stakeholder != null) { - if (!rolesUtils.hasUpdateAuthority(stakeholder.getType(), stakeholder.getAlias())) { - // EXCEPTION - Access denied - throw new ForbiddenException("Delete category: You are not authorized to update stakeholder with id: " + stakeholderId); - } - - Topic topic = topicDAO.findById(topicId); - if (topic != null) { - if (stakeholder.getTopics().contains(topicId)) { - - Category category = categoryDAO.findById(categoryId); - if (category != null) { - - if (category.getDefaultId() != null && !rolesUtils.hasCreateAndDeleteAuthority(stakeholder.getType())) { - // EXCEPTION - Access denied - throw new ForbiddenException("Delete category: You are not authorized to delete a default Category in stakeholder with id: " + stakeholderId); - } - - - List categories = topic.getCategories(); - int index = categories.indexOf(categoryId); - if (index != -1) { - // this category belongs in default profile - if (topic.getDefaultId() == null && children != null) { - onDeleteDefaultCategory(categoryId, topicId, children); - } - subCategoryController.deleteTree(category); - 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"); - } + Stakeholder stakeholder = this.stakeholderService.findById(stakeholderId); + Topic topic = this.topicService.findByPath(stakeholder, topicId); + Category category = this.categoryService.findByPath(topic, categoryId); + this.categoryService.delete(stakeholder.getType(), category, true); return true; } - - public boolean onDeleteDefaultCategory(String defaultCategoryId, String defaultTopicId, String children) { - if (children.equals("delete")) { - List topics = topicDAO.findByDefaultId(defaultTopicId); - List categories = categoryDAO.findByDefaultId(defaultCategoryId); - - for (Topic topic : topics) { - Iterator categoriesIterator = categories.iterator(); - while (categoriesIterator.hasNext()) { - Category category = categoriesIterator.next(); - - String categoryId = category.getId(); - - if (topic.getCategories() != null && topic.getCategories().contains(categoryId)) { - categoriesIterator.remove(); - - topic.getCategories().remove(categoryId); - topicDAO.save(topic); - - subCategoryController.deleteTree(category); - - categoryDAO.delete(categoryId); - log.debug("Category with id: " + categoryId + " deleted!"); - - break; - } - } - } - } else if (children.equals("disconnect")) { - List categories = categoryDAO.findByDefaultId(defaultCategoryId); - for (Category category : categories) { - subCategoryController.disConnectTree(category); - - category.setDefaultId(null); - categoryDAO.save(category); - - log.debug("DefaultId for Category with id: " + category.getId() + " empty!"); - } - } - return true; - } - - @PreAuthorize("isAuthenticated()") + /*@PreAuthorize("isAuthenticated()") @RequestMapping(value = "/{stakeholderId}/{topicId}/reorder", method = RequestMethod.POST) public List reorderCategories(@PathVariable("stakeholderId") String stakeholderId, @PathVariable("topicId") String topicId, @@ -361,131 +226,19 @@ public class CategoryController { log.debug("Categories reordered!"); return categoriesFull; - } + }*/ @PreAuthorize("isAuthenticated()") @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/change-visibility", method = RequestMethod.POST) - public Category changeCategoryVisibility(@PathVariable("stakeholderId") String stakeholderId, - @PathVariable("topicId") String topicId, - @PathVariable("categoryId") String categoryId, - @RequestParam("visibility") Visibility visibility, @RequestParam(required = false) Boolean propagate) { - log.debug("change category visibility: " + visibility + " - toggle propagate: " + ((propagate != null && propagate) ? "true" : "false")); + public CategoryFull changeCategoryVisibility(@PathVariable("stakeholderId") String stakeholderId, + @PathVariable("topicId") String topicId, + @PathVariable("categoryId") String categoryId, + @RequestParam("visibility") Visibility visibility, @RequestParam(defaultValue = "false") Boolean propagate) { + log.debug("change category visibility: " + visibility + " - toggle propagate: " + propagate); log.debug("Stakeholder: " + stakeholderId + " - Topic: " + topicId + " - Category: " + categoryId); - - Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId); - - if (stakeholder != null) { - if (!rolesUtils.hasUpdateAuthority(stakeholder.getType(), stakeholder.getAlias())) { - // EXCEPTION - Access denied - throw new ForbiddenException("Toggle category: You are not authorized to update stakeholder with id: " + stakeholderId); - } - - Topic topic = topicDAO.findById(topicId); - if (topic != null) { - if (stakeholder.getTopics().contains(topicId)) { - if (topic.getCategories().contains(categoryId)) { - return changeVisibilityTree(categoryId, visibility, propagate); - } else { - // EXCEPTION - Category not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); - throw new PathNotValidException("Toggle category: Category with id: " + categoryId + " not found in Topic: " + topicId); - } - } else { - // EXCEPTION - Topic not found in Stakeholder: stakeholder.getAlias(); - throw new PathNotValidException("Toggle category: Topic with id: " + topicId + " not found in Stakeholder: " + stakeholderId); - } - } else { - // EXCEPTION - Topic not found - throw new EntityNotFoundException("Toggle category: Topic with id: " + topicId + " not found"); - } - } else { - // EXCEPTION - Stakeholder not found - throw new EntityNotFoundException("Toggle category: Stakeholder with id: " + stakeholderId + " not found"); - } - } - - public Category changeVisibilityTree(String categoryId, Visibility visibility, Boolean propagate) { - Category category = categoryDAO.findById(categoryId); - if (category == null) { - // EXCEPTION - Category not found - throw new EntityNotFoundException("Change category visibility: Category with id: " + categoryId + " not found"); - } - - Category categoryFull = new Category(category); - List subCategoriesFull = new ArrayList<>(); - - if (propagate != null && propagate) { - for (String subCategoryId : category.getSubCategories()) { - subCategoriesFull.add(subCategoryController.changeVisibilityTree(subCategoryId, visibility, propagate)); - } - } - - category.setVisibility(visibility); - categoryDAO.save(category); - log.debug("Category toggled!"); - - categoryFull.setVisibility(visibility); - categoryFull.setSubCategories(subCategoriesFull); - - return categoryFull; - } - - - private Topic checkForExceptions(String stakeholderId, String topicId) { - - Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId); - - if (stakeholder == null) { - // EXCEPTION - Stakeholder not found - throw new EntityNotFoundException("checkForExceptions category: Stakeholder with id: " + stakeholderId + " not found"); - } - if (!rolesUtils.hasUpdateAuthority(stakeholder.getType(), stakeholder.getAlias())) { - // EXCEPTION - Access denied - throw new ForbiddenException("checkForExceptions category: You are not authorized to update stakeholder with id: " + stakeholderId); - } - - Topic topic = topicDAO.findById(topicId); - if (topic == null) { - // EXCEPTION - Topic not found - throw new EntityNotFoundException("checkForExceptions category: Topic with id: " + topicId + " not found"); - } - - if (!stakeholder.getTopics().contains(topicId)) { - // EXCEPTION - Topic not found in Stakeholder: stakeholder.getAlias(); - throw new PathNotValidException("checkForExceptions category: Topic with id: " + topicId + " not found in Stakeholder: " + stakeholderId); - } - - return topic; - } - - public void deleteTree(Topic topic) { - List categories = topic.getCategories(); - for (String categoryId : categories) { - Category category = categoryDAO.findById(categoryId); - if (category == null) { - // EXCEPTION - Category not found - throw new EntityNotFoundException("Category delete tree: Category with id: " + categoryId + " not found (category exists in topic: " + topic.getId() + ")"); - } - - subCategoryController.deleteTree(category); - - categoryDAO.delete(categoryId); - } - } - - public void disConnectTree(Topic topic) { - List categories = topic.getCategories(); - for (String categoryId : categories) { - Category category = categoryDAO.findById(categoryId); - if (category == null) { - // EXCEPTION - Category not found - throw new EntityNotFoundException("Category disconnect tree: Category with id: " + categoryId + " not found (category exists in topic: " + topic.getId() + ")"); - } - - subCategoryController.disConnectTree(category); - - category.setDefaultId(null); - categoryDAO.save(category); - } + Stakeholder stakeholder = this.stakeholderService.findById(stakeholderId); + Topic topic = this.topicService.findByPath(stakeholder, topicId); + Category category = this.categoryService.findByPath(topic, categoryId); + return this.categoryService.changeVisibility(stakeholder.getType(), stakeholder.getAlias(), category, visibility, propagate); } } -*/ diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/IndicatorController.java b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/IndicatorController.java index d1fc52b..6d513cc 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/IndicatorController.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/IndicatorController.java @@ -1,58 +1,39 @@ -/* package eu.dnetlib.uoamonitorservice.controllers; -import eu.dnetlib.uoaadmintoolslibrary.handlers.ForbiddenException; -import eu.dnetlib.uoaadmintoolslibrary.handlers.utils.RolesUtils; -import eu.dnetlib.uoamonitorservice.dao.*; import eu.dnetlib.uoamonitorservice.entities.*; -import eu.dnetlib.uoamonitorservice.handlers.EntityNotFoundException; -import eu.dnetlib.uoamonitorservice.handlers.PathNotValidException; -import eu.dnetlib.uoamonitorservice.primitives.IndicatorPath; -import eu.dnetlib.uoamonitorservice.primitives.ReorderEvent; import eu.dnetlib.uoamonitorservice.primitives.Visibility; +import eu.dnetlib.uoamonitorservice.service.*; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.web.bind.annotation.*; -import java.io.UnsupportedEncodingException; -import java.util.*; - @RestController @CrossOrigin(origins = "*") public class IndicatorController { private final Logger log = LogManager.getLogger(this.getClass()); - @Autowired - private RolesUtils rolesUtils; + private final StakeholderService stakeholderService; + private final TopicService topicService; + private final CategoryService categoryService; + private final SubCategoryService subCategoryService; + private final SectionService sectionService; + private final IndicatorService indicatorService; @Autowired - private StakeholderDAO stakeholderDAO; + public IndicatorController(StakeholderService stakeholderService, TopicService topicService, CategoryService categoryService, + SubCategoryService subCategoryService, SectionService sectionService, IndicatorService indicatorService) { + this.stakeholderService = stakeholderService; + this.topicService = topicService; + this.categoryService = categoryService; + this.subCategoryService = subCategoryService; + this.sectionService = sectionService; + this.indicatorService = indicatorService; + } - @Autowired - private TopicDAO topicDAO; - - @Autowired - private CategoryDAO categoryDAO; - - @Autowired - private SubCategoryDAO subCategoryDAO; - - @Autowired - private SectionDAO sectionDAO; - - @Autowired - private IndicatorDAO indicatorDAO; - - @Autowired - private SectionController sectionController; - - @Autowired - private StakeholderController stakeholderController; - - @PreAuthorize("isAuthenticated()") + /*@PreAuthorize("isAuthenticated()") @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/save-bulk", method = RequestMethod.POST) public Stakeholder saveBulkIndicators(@PathVariable("stakeholderId") String stakeholderId, @PathVariable("topicId") String topicId, @@ -143,8 +124,8 @@ public class IndicatorController { indicators.add(indicator.getId()); log.debug("Indicator saved!"); } - - @PreAuthorize("isAuthenticated()") +*/ + /* @PreAuthorize("isAuthenticated()") @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/{sectionId}/save", method = RequestMethod.POST) public Indicator saveIndicator(@PathVariable("stakeholderId") String stakeholderId, @PathVariable("topicId") String topicId, @@ -462,7 +443,7 @@ public class IndicatorController { indicatorDAO.save(indicatorBasedOnDefault); } } - +*/ @PreAuthorize("isAuthenticated()") @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/{sectionId}/{indicatorId}/delete", method = RequestMethod.DELETE) public boolean deleteIndicator(@PathVariable("stakeholderId") String stakeholderId, @@ -474,79 +455,18 @@ public class IndicatorController { @RequestParam(required = false) String children) { log.debug("delete indicator"); log.debug("Id: " + indicatorId + " - Stakeholder: " + stakeholderId + " - Topic: " + topicId + " - Category: " + categoryId + " - SubCategory: " + subcategoryId + " - Section: " + sectionId); - - Indicator indicator = indicatorDAO.findById(indicatorId); - if (indicator != null) { - Section section = checkForExceptions(stakeholderId, topicId, categoryId, subcategoryId, sectionId, indicator.getType()); - - Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId); - if (indicator.getDefaultId() != null && !rolesUtils.hasCreateAndDeleteAuthority(stakeholder.getType())) { - // EXCEPTION - Access denied - throw new ForbiddenException("Delete indicator: You are not authorized to delete a default Indicator in stakeholder with id: " + stakeholderId); - } - - List indicators = section.getIndicators(); - - int index = indicators.indexOf(indicatorId); - if (index != -1) { - - // this indicator belongs in default profile - if (section.getDefaultId() == null && children != null) { - onDeleteDefaultIndicator(indicatorId, sectionId, children); - } - - - indicators.remove(index); - sectionDAO.save(section); - - 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(); -> Section: section.getTitle(); - throw new PathNotValidException("Delete indicator: Indicator with id: " + indicatorId + " not found in Sectiom: " + sectionId); - } - } else { - // EXCEPTION - Indicator not found - throw new EntityNotFoundException("Delete indicator: Indicator with id: " + indicatorId + " not found"); - } + Stakeholder stakeholder = this.stakeholderService.findById(stakeholderId); + Topic topic = this.topicService.findByPath(stakeholder, topicId); + Category category = this.categoryService.findByPath(topic, categoryId); + SubCategory subCategory = this.subCategoryService.findByPath(category, subcategoryId); + Section section = this.sectionService.findByPath(subCategory, sectionId); + Indicator indicator = this.indicatorService.findByPath(section, indicatorId); + this.indicatorService.delete(stakeholder.getType(), indicator, true); return true; } - public boolean onDeleteDefaultIndicator(String defaultIndicatorId, String defaultSectionId, String children) { - if (children.equals("delete")) { - // 2nd way - List
sections = sectionDAO.findByDefaultId(defaultSectionId); - List indicators = indicatorDAO.findByDefaultId(defaultIndicatorId); - for (Section section : sections) { - Iterator indicatorsIterator = indicators.iterator(); - while (indicatorsIterator.hasNext()) { - String indicatorId = indicatorsIterator.next().getId(); - if (section.getIndicators().contains(indicatorId)) { - indicatorsIterator.remove(); - - section.getIndicators().remove(indicatorId); - sectionDAO.save(section); - - indicatorDAO.delete(indicatorId); - log.debug("Indicator with id: " + indicatorId + " deleted!"); - - break; - } - } - } - } else if (children.equals("disconnect")) { - List indicators = indicatorDAO.findByDefaultId(defaultIndicatorId); - for (Indicator indicator : indicators) { - indicator.setDefaultId(null); - indicatorDAO.save(indicator); - log.debug("DefaultId for Indicator with id: " + indicator.getId() + " empty!"); - } - } - return true; - } - - @PreAuthorize("isAuthenticated()") + /*@PreAuthorize("isAuthenticated()") @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/{sectionId}/{type}/reorder", method = RequestMethod.POST) public List reorderIndicators(@PathVariable("stakeholderId") String stakeholderId, @PathVariable("topicId") String topicId, @@ -586,7 +506,7 @@ public class IndicatorController { log.debug("Indicators reordered!"); return indicatorsFull; - } + }*/ @PreAuthorize("isAuthenticated()") @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/{sectionId}/{indicatorId}/change-visibility", method = RequestMethod.POST) @@ -599,121 +519,12 @@ public class IndicatorController { @RequestParam("visibility") Visibility visibility) { log.debug("change indicator visibility: " + visibility); log.debug("Stakeholder: " + stakeholderId + " - Topic: " + topicId + " - Category: " + categoryId + " - SubCategory: " + subcategoryId + " - Section: " + sectionId + " - Indicator: " + indicatorId); - - Indicator indicator = indicatorDAO.findById(indicatorId); - if (indicator == null) { - // EXCEPTION - Indicator not found - throw new EntityNotFoundException("Change indicator visibility: Indicator with id: " + indicatorId + " not found"); - } - - Section section = checkForExceptions(stakeholderId, topicId, categoryId, subcategoryId, sectionId, indicator.getType()); - List indicators = section.getIndicators(); - - if (indicators.contains(indicatorId)) { - return changeVisibilityTree(indicatorId, indicator, visibility); - } else { - // EXCEPTION - Indicator not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); -> Category: category.getAlias(); -> SubCategory: subCategory.getAlias(); -> Section: section.getTitle(); - throw new PathNotValidException("Toggle indicators: Indicator with id: " + indicatorId + " not found in Section: " + sectionId); - } - } - - public Indicator changeVisibilityTree(String indicatorId, Indicator indicator, Visibility visibility) { - if (indicator == null) { - indicator = indicatorDAO.findById(indicatorId); - if (indicator == null) { - // EXCEPTION - Indicator not found - throw new EntityNotFoundException("Change indicator visibility: Indicator with id: " + indicatorId + " not found"); - } - } - - indicator.setVisibility(visibility); - indicatorDAO.save(indicator); - log.debug("Indicator toggled!"); - - return indicator; - } - - private Section checkForExceptions(String stakeholderId, String topicId, String categoryId, String subcategoryId, String sectionId, String indicatorType) { - - Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId); - - if (stakeholder == null) { - // EXCEPTION - Stakeholder not found - throw new EntityNotFoundException("Save indicator: Stakeholder with id: " + stakeholderId + " not found"); - } - if (!rolesUtils.hasUpdateAuthority(stakeholder.getType(), stakeholder.getAlias())) { - // EXCEPTION - Access denied - throw new ForbiddenException("CheckForExceptions Indicator: You are not authorized to update stakeholder with id: " + stakeholderId); - } - - Topic topic = topicDAO.findById(topicId); - if (topic == null) { - // EXCEPTION - Topic not found - throw new EntityNotFoundException("Save indicator: Topic with id: " + topicId + " not found"); - } - - if (!stakeholder.getTopics().contains(topicId)) { - // EXCEPTION - Topic not found in Stakeholder: stakeholder.getAlias(); - throw new PathNotValidException("Save indicator: Topic with id: " + topicId + " not found in Stakeholder: " + stakeholderId); - } - - Category category = categoryDAO.findById(categoryId); - if (category == null) { - // EXCEPTION - Category not found - throw new EntityNotFoundException("Save indicator: Category with id: " + categoryId + " not found"); - } - - if (!topic.getCategories().contains(categoryId)) { - // EXCEPTION - Category not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); - throw new PathNotValidException("Save indicator: Category with id: " + categoryId + " not found in Topic: " + topicId); - } - - SubCategory subcategory = subCategoryDAO.findById(subcategoryId); - if (subcategory == null) { - // EXCEPTION - SubCategory not found - throw new EntityNotFoundException("Save indicator: SubCategory with id: " + subcategoryId + " not found"); - } - - if (!category.getSubCategories().contains(subcategoryId)) { - // EXCEPTION - SubCategory not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); -> Category: category.getAlias(); - throw new PathNotValidException("Save indicator: SubCategory with id: " + subcategoryId + " not found in Category: " + categoryId); - } - - Section section = sectionDAO.findById(sectionId); - if (section == null) { - // EXCEPTION - Section not found - throw new EntityNotFoundException("Save indicator: Section with id: " + sectionId + " not found"); - } - - if (indicatorType.equals("chart")) { - if (!subcategory.getCharts().contains(sectionId)) { - // EXCEPTION - Section not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); -> Category: category.getAlias(); -> SubCategory: subcategory.getAlias(); - throw new PathNotValidException("Save indicator: SubCategory with id: " + subcategoryId + " not found in Category: " + categoryId); - } - } else if (indicatorType.equals("number")) { - if (!subcategory.getNumbers().contains(sectionId)) { - // EXCEPTION - Section not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); -> Category: category.getAlias(); -> SubCategory: subcategory.getAlias(); - throw new PathNotValidException("Save indicator: SubCategory with id: " + subcategoryId + " not found in Category: " + categoryId); - } - } - - return section; - } - - public void deleteTree(Section section) { - List indicators = section.getIndicators(); - for (String indicatorId : indicators) { - indicatorDAO.delete(indicatorId); - } - } - - public void disConnectTree(Section section) { - List indicators = section.getIndicators(); - for (String indicatorId : indicators) { - Indicator indicator = indicatorDAO.findById(indicatorId); - indicator.setDefaultId(null); - indicatorDAO.save(indicator); - } + Stakeholder stakeholder = this.stakeholderService.findById(stakeholderId); + Topic topic = this.topicService.findByPath(stakeholder, topicId); + Category category = this.categoryService.findByPath(topic, categoryId); + SubCategory subCategory = this.subCategoryService.findByPath(category, subcategoryId); + Section section = this.sectionService.findByPath(subCategory, sectionId); + Indicator indicator = this.indicatorService.findByPath(section, indicatorId); + return this.indicatorService.changeVisibility(stakeholder.getType(), stakeholder.getAlias(), indicator, visibility); } } -*/ diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/SectionController.java b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/SectionController.java index 2b145f2..4531344 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/SectionController.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/SectionController.java @@ -1,47 +1,48 @@ -/* package eu.dnetlib.uoamonitorservice.controllers; -import eu.dnetlib.uoaadmintoolslibrary.handlers.utils.RolesUtils; -import eu.dnetlib.uoamonitorservice.dao.*; -import eu.dnetlib.uoamonitorservice.dto.SectionFull; import eu.dnetlib.uoamonitorservice.entities.*; -import eu.dnetlib.uoamonitorservice.handlers.EntityNotFoundException; -import eu.dnetlib.uoaadmintoolslibrary.handlers.ForbiddenException; -import eu.dnetlib.uoamonitorservice.handlers.PathNotValidException; -import eu.dnetlib.uoamonitorservice.primitives.Visibility; -import eu.dnetlib.uoamonitorservice.service.SectionService; -import eu.dnetlib.uoamonitorservice.service.SubCategoryService; +import eu.dnetlib.uoamonitorservice.service.*; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.web.bind.annotation.*; -import java.util.ArrayList; -import java.util.Date; -import java.util.List; - @RestController @CrossOrigin(origins = "*") public class SectionController { private final Logger log = LogManager.getLogger(this.getClass()); - private SectionService service; + private final StakeholderService stakeholderService; + private final TopicService topicService; + private final CategoryService categoryService; + private final SubCategoryService subCategoryService; + private final SectionService sectionService; - @PreAuthorize("isAuthenticated()") + @Autowired + public SectionController(StakeholderService stakeholderService, TopicService topicService, CategoryService categoryService, + SubCategoryService subCategoryService, SectionService sectionService) { + this.stakeholderService = stakeholderService; + this.topicService = topicService; + this.categoryService = categoryService; + this.subCategoryService = subCategoryService; + this.sectionService = sectionService; + } + + /*@PreAuthorize("isAuthenticated()") @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/save/{index}", method = RequestMethod.POST) - public SectionFull saveSection(@PathVariable("stakeholderId") String stakeholderId, - @PathVariable("topicId") String topicId, - @PathVariable("categoryId") String categoryId, - @PathVariable("subcategoryId") String subcategoryId, - @PathVariable("index") String index, - @RequestBody SectionFull sectionFull) { + public Section saveSection(@PathVariable("stakeholderId") String stakeholderId, + @PathVariable("topicId") String topicId, + @PathVariable("categoryId") String categoryId, + @PathVariable("subcategoryId") String subcategoryId, + @PathVariable("index") String index, + @RequestBody Section sectionFull) { log.debug("save section"); log.debug("Name: "+sectionFull.getTitle() + " - Id: "+sectionFull.getId() + " - Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId+ " - SubCategory: "+subcategoryId); - SubCategory subCategory = this.subCategoryService.checkForExceptions(stakeholderId, topicId, categoryId, subcategoryId, "Save Section"); + SubCategory subCategory = checkForExceptions(stakeholderId, topicId, categoryId, subcategoryId); - Section section = new Section(sectionFull); + Section section = new Section<>(sectionFull); Date date = new Date(); section.setUpdateDate(date); @@ -78,7 +79,7 @@ public class SectionController { section.setIndicators(indicators); - Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId); + Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId); // this section belongs in default profile and it is new or it is updated if(stakeholder.getDefaultId() == null) { if(sectionId == null) { @@ -151,121 +152,94 @@ public class SectionController { public void onUpdateDefaultSection(Section section, Stakeholder stakeholder, Section oldSection) { log.debug("On update default section"); - } - private SubCategory checkForExceptions(String stakeholderId, String topicId, String categoryId, String subcategoryId) { + // section already exists - check if changed and update all sections based on it - Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId).orElseThrow(() -> new EntityNotFoundException("Save indicator: Stakeholder with id: " + stakeholderId + " not found")); - if(!rolesUtils.hasUpdateAuthority(stakeholder.getType(), stakeholder.getAlias())) { - // EXCEPTION - Access denied - throw new ForbiddenException("CheckForExceptions Section: You are not authorized to update stakeholder with id: "+stakeholderId); - } + boolean changed = false; + List
sections = sectionDAO.findByDefaultId(section.getId()); - Topic topic = topicDAO.findById(topicId).orElseThrow(() -> new EntityNotFoundException("Save indicator: Topic with id: "+topicId+" not found")); - if(!stakeholder.getTopics().contains(topicId)) { - // EXCEPTION - Topic not found in Stakeholder: stakeholder.getAlias(); - throw new PathNotValidException("Save indicator: Topic with id: " + topicId + " not found in Stakeholder: " + stakeholderId); - } + for(Section sectionBasedOnDefault : sections) { + if(section.getTitle() != null && !section.getTitle().equals(sectionBasedOnDefault.getTitle()) + && (oldSection.getTitle() == null || oldSection.getTitle().equals(sectionBasedOnDefault.getTitle()))) { - Category category = categoryDAO.findById(categoryId); - if(category == null) { - // EXCEPTION - Category not found - throw new EntityNotFoundException("Save indicator: Category with id: "+categoryId+" not found"); - } - - if(!topic.getCategories().contains(categoryId)) { - // EXCEPTION - Category not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); - throw new PathNotValidException("Save indicator: Category with id: "+categoryId+" not found in Topic: "+topicId); - } - - SubCategory subcategory = subCategoryDAO.findById(subcategoryId); - if(subcategory == null) { - // EXCEPTION - SubCategory not found - throw new EntityNotFoundException("Save indicator: SubCategory with id: "+subcategoryId+" not found"); - } - - if (!category.getSubCategories().contains(subcategoryId)) { - // EXCEPTION - SubCategory not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); -> Category: category.getAlias(); - throw new PathNotValidException("Save indicator: SubCategory with id: "+subcategoryId+" not found in Category: "+categoryId); - } - - return subcategory; - } - - public void deleteTree(SubCategory subCategory) { - List sections = subCategory.getCharts(); - for(String sectionId : sections) { - Section section = sectionDAO.findById(sectionId); - if (section == null) { - // EXCEPTION - Section not found - throw new EntityNotFoundException("Section delete tree: Chart Section with id: "+sectionId+" not found (section exists in subCategory: "+subCategory.getId()+")"); + sectionBasedOnDefault.setTitle(section.getTitle()); + changed = true; } - indicatorController.deleteTree(section); - - sectionDAO.delete(sectionId); - } - - sections = subCategory.getNumbers(); - for(String sectionId : sections) { - Section section = sectionDAO.findById(sectionId); - if (section == null) { - // EXCEPTION - Section not found - throw new EntityNotFoundException("Section delete tree: Number Section with id: "+sectionId+" not found (section exists in subCategory: "+subCategory.getId()+")"); + if(!changed) { +// break; + continue; } - indicatorController.deleteTree(section); - - sectionDAO.delete(sectionId); +// sectionBasedOnDefault.setTitle(section.getTitle()); + sectionBasedOnDefault.setUpdateDate(section.getUpdateDate()); + sectionDAO.save(sectionBasedOnDefault); } + }*/ + + @PreAuthorize("isAuthenticated()") + @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/{sectionId}/delete", method = RequestMethod.DELETE) + public boolean deleteSection(@PathVariable("stakeholderId") String stakeholderId, + @PathVariable("topicId") String topicId, + @PathVariable("categoryId") String categoryId, + @PathVariable("subcategoryId") String subcategoryId, + @PathVariable("sectionId") String sectionId, + @RequestParam(required = false) String children) { + log.debug("delete section"); + log.debug("Id: " + sectionId + " - Stakeholder: " + stakeholderId + " - Topic: " + topicId + " - Category: " + categoryId + " - SubCategory: " + subcategoryId); + Stakeholder stakeholder = this.stakeholderService.findById(stakeholderId); + Topic topic = this.topicService.findByPath(stakeholder, topicId); + Category category = this.categoryService.findByPath(topic, categoryId); + SubCategory subCategory = this.subCategoryService.findByPath(category, subcategoryId); + Section section = this.sectionService.findByPath(subCategory, sectionId); + this.sectionService.delete(stakeholder.getType(), section, true); + return true; } - public void disConnectTree(SubCategory subCategory) { - List sections = subCategory.getCharts(); + /*@PreAuthorize("isAuthenticated()") + @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/{type}/reorder", method = RequestMethod.POST) + public List
reorderSections(@PathVariable("stakeholderId") String stakeholderId, + @PathVariable("topicId") String topicId, + @PathVariable("categoryId") String categoryId, + @PathVariable("subcategoryId") String subcategoryId, + @PathVariable("type") String type, + @RequestBody List sections) { + log.debug("reorder sections of type: "+type); + log.debug("Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId+ " - SubCategory: "+subcategoryId); + + SubCategory subCategory = checkForExceptions(stakeholderId, topicId, categoryId, subcategoryId); + + if (type.equals("chart")) { + List oldSections = subCategory.getCharts(); + for (String sectionId : oldSections) { + if (!sections.contains(sectionId)) { + sections.add(sectionId); + } + } + subCategory.setCharts(sections); + } else if (type.equals("number")) { + List oldSections = subCategory.getNumbers(); + for (String sectionId : oldSections) { + if (!sections.contains(sectionId)) { + sections.add(sectionId); + } + } + subCategory.setNumbers(sections); + } + + List
sectionsFull = new ArrayList<>(); for(String sectionId : sections) { Section section = sectionDAO.findById(sectionId); - if (section == null) { + if(section == null) { // EXCEPTION - Section not found - throw new EntityNotFoundException("Section disconnect tree: Chart Section with id: "+sectionId+" not found (section exists in subCategory: "+subCategory.getId()+")"); + throw new EntityNotFoundException("Reorder sections: Section with id: " + sectionId + " not found"); } - - indicatorController.disConnectTree(section); - - section.setDefaultId(null); - sectionDAO.save(section); + sectionsFull.add(section); } - sections = subCategory.getNumbers(); - for(String sectionId : sections) { - Section section = sectionDAO.findById(sectionId); - if (section == null) { - // EXCEPTION - Section not found - throw new EntityNotFoundException("Section disconnect tree: Number Section with id: "+sectionId+" not found (section exists in subCategory: "+subCategory.getId()+")"); - } + subCategoryDAO.save(subCategory); + log.debug("Sections reordered!"); - indicatorController.disConnectTree(section); - - section.setDefaultId(null); - sectionDAO.save(section); - } - } - - public Section changeVisibilityTree(String sectionId, Visibility visibility) { - Section section = sectionDAO.findById(sectionId); - if (section == null) { - // EXCEPTION - Section not found - throw new EntityNotFoundException("Change section visibility: Section with id: " + sectionId + " not found"); - } - - Section sectionFull = new Section(section); - List indicatorsFull = new ArrayList(); - - for (String indicatorId : section.getIndicators()) { - indicatorsFull.add(indicatorController.changeVisibilityTree(indicatorId, null, visibility)); - } - - sectionFull.setIndicators(indicatorsFull); - return sectionFull; - } + return sectionsFull; + }*/ } -*/ diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/SubCategoryController.java b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/SubCategoryController.java index 7d86bb6..ef9b4d6 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/SubCategoryController.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/SubCategoryController.java @@ -1,93 +1,40 @@ -/* package eu.dnetlib.uoamonitorservice.controllers; -import eu.dnetlib.uoaadmintoolslibrary.handlers.utils.RolesUtils; -import eu.dnetlib.uoamonitorservice.dao.*; -import eu.dnetlib.uoamonitorservice.entities.*; -import eu.dnetlib.uoamonitorservice.handlers.EntityNotFoundException; -import eu.dnetlib.uoaadmintoolslibrary.handlers.ForbiddenException; -import eu.dnetlib.uoamonitorservice.handlers.PathNotValidException; +import eu.dnetlib.uoamonitorservice.dto.SubCategoryFull; +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.primitives.Visibility; +import eu.dnetlib.uoamonitorservice.service.CategoryService; +import eu.dnetlib.uoamonitorservice.service.StakeholderService; +import eu.dnetlib.uoamonitorservice.service.SubCategoryService; +import eu.dnetlib.uoamonitorservice.service.TopicService; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.web.bind.annotation.*; -import java.util.ArrayList; -import java.util.Date; -import java.util.Iterator; -import java.util.List; - @RestController @CrossOrigin(origins = "*") public class SubCategoryController { private final Logger log = LogManager.getLogger(this.getClass()); - @Autowired - private RolesUtils rolesUtils; + private final StakeholderService stakeholderService; + private final TopicService topicService; + private final CategoryService categoryService; + private final SubCategoryService subCategoryService; @Autowired - private StakeholderDAO stakeholderDAO; - - @Autowired - private TopicDAO topicDAO; - - @Autowired - private CategoryDAO categoryDAO; - - @Autowired - private SubCategoryDAO subCategoryDAO; - - @Autowired - private SectionDAO sectionDAO; - - @Autowired - private IndicatorDAO indicatorDAO; - - @Autowired - private SectionController sectionController; - - public SubCategory> buildSubCategory(SubCategory> subcategoryFull) { - SubCategory subCategory = new SubCategory<>(subcategoryFull); - - List sectionCharts = new ArrayList<>(); - List> sectionChartsFull = new ArrayList<>(); - - for(Section section : subcategoryFull.getCharts()) { - Section sectionFull = sectionController.buildSection(section); - sectionChartsFull.add(sectionFull); - sectionCharts.add(sectionFull.getId()); - } - subcategoryFull.setCharts(sectionChartsFull); - subCategory.setCharts(sectionCharts); - - List sectionNumbers = new ArrayList<>(); - List> sectionNumbersFull = new ArrayList<>(); - - for(Section section : subcategoryFull.getNumbers()) { - Section sectionFull = sectionController.buildSection(section); - sectionNumbersFull.add(sectionFull); - sectionNumbers.add(sectionFull.getId()); - } - subcategoryFull.setNumbers(sectionNumbersFull); - subCategory.setNumbers(sectionNumbers); - - Date date = new Date(); - subCategory.setCreationDate(date); - subCategory.setUpdateDate(date); - - subcategoryFull.setCreationDate(date); - subcategoryFull.setUpdateDate(date); - - - subCategoryDAO.save(subCategory); - - subcategoryFull.setId(subCategory.getId()); - return subcategoryFull; + public SubCategoryController(StakeholderService stakeholderService, TopicService topicService, CategoryService categoryService, SubCategoryService subCategoryService) { + this.stakeholderService = stakeholderService; + this.topicService = topicService; + this.categoryService = categoryService; + this.subCategoryService = subCategoryService; } - @PreAuthorize("isAuthenticated()") + /* @PreAuthorize("isAuthenticated()") @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/save", method = RequestMethod.POST) public SubCategory> saveSubCategory(@PathVariable("stakeholderId") String stakeholderId, @PathVariable("topicId") String topicId, @@ -215,7 +162,7 @@ public class SubCategoryController { subCategoryBasedOnDefault.setUpdateDate(subCategory.getUpdateDate()); subCategoryDAO.save(subCategoryBasedOnDefault); } - } + }*/ @PreAuthorize("isAuthenticated()") @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/delete", method = RequestMethod.DELETE) @@ -225,90 +172,16 @@ public class SubCategoryController { @PathVariable("subcategoryId") String subcategoryId, @RequestParam(required = false) String children) { log.debug("delete subcategory"); - log.debug("Id: "+subcategoryId + " - Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId); - - Category category = checkForExceptions(stakeholderId, topicId, categoryId); - - SubCategory subcategory = subCategoryDAO.findById(subcategoryId); - if(subcategory != null) { - - Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId); - if(subcategory.getDefaultId() != null && !rolesUtils.hasCreateAndDeleteAuthority(stakeholder.getType())) { - // EXCEPTION - Access denied - throw new ForbiddenException("Delete subcategory: You are not authorized to delete a default SubCategory in stakeholder with id: "+stakeholderId); - } - - List subcategories = category.getSubCategories(); - int index = subcategories.indexOf(subcategoryId); - if(index != -1) { - // this subCategory belongs in default profile - if(category.getDefaultId() == null && children != null) { - onDeleteDefaultSubCategory(subcategoryId, categoryId, children); - } - sectionController.deleteTree(subcategory); - - subcategory.setCharts(null); - 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"); - } + log.debug("Id: " + subcategoryId + " - Stakeholder: " + stakeholderId + " - Topic: " + topicId + " - Category: " + categoryId); + Stakeholder stakeholder = this.stakeholderService.findById(stakeholderId); + Topic topic = this.topicService.findByPath(stakeholder, topicId); + Category category = this.categoryService.findByPath(topic, categoryId); + SubCategory subCategory = this.subCategoryService.findByPath(category, subcategoryId); + this.subCategoryService.delete(stakeholder.getType(), subCategory, true); return true; } - public boolean onDeleteDefaultSubCategory(String defaultSubCategoryId, String defaultCategoryId, String children) { - if(children.equals("delete")) { - List categories = categoryDAO.findByDefaultId(defaultCategoryId); - List subCategories = subCategoryDAO.findByDefaultId(defaultSubCategoryId); - - for(Category category : categories) { - Iterator subCategoriesIterator = subCategories.iterator(); - while(subCategoriesIterator.hasNext()) { - SubCategory subCategory = subCategoriesIterator.next(); - - String subCategoryId = subCategory.getId(); - - if(category.getSubCategories() != null && category.getSubCategories().contains(subCategoryId)) { - subCategoriesIterator.remove(); - - category.getSubCategories().remove(subCategoryId); - categoryDAO.save(category); - - sectionController.deleteTree(subCategory); - - subCategoryDAO.delete(subCategoryId); - log.debug("SubCategory with id: "+subCategoryId+" deleted!"); - - break; - } - } - } - } else if(children.equals("disconnect")) { - List subCategories = subCategoryDAO.findByDefaultId(defaultSubCategoryId); - for(SubCategory subCategory : subCategories) { - sectionController.disConnectTree(subCategory); - - subCategory.setDefaultId(null); - subCategoryDAO.save(subCategory); - - log.debug("DefaultId for SubCategory with id: "+subCategory.getId()+" empty!"); - } - } - return true; - } - - @PreAuthorize("isAuthenticated()") + /*@PreAuthorize("isAuthenticated()") @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/reorder", method = RequestMethod.POST) public List reorderSubCategories(@PathVariable("stakeholderId") String stakeholderId, @PathVariable("topicId") String topicId, @@ -341,129 +214,22 @@ public class SubCategoryController { log.debug("SubCategories reordered!"); return subCategoriesFull; - } + }*/ @PreAuthorize("isAuthenticated()") @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/change-visibility", method = RequestMethod.POST) - public SubCategory changeSubCategoryVisibility(@PathVariable("stakeholderId") String stakeholderId, - @PathVariable("topicId") String topicId, - @PathVariable("categoryId") String categoryId, - @PathVariable("subcategoryId") String subcategoryId, - @RequestParam("visibility") Visibility visibility, @RequestParam(required = false) Boolean propagate) { - log.debug("change subCategory visibility: "+visibility + " - toggle propagate: "+((propagate != null && propagate) ? "true" : "false")); - log.debug("Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId+ " - SubCategory: "+subcategoryId); - - SubCategory subCategory = subCategoryDAO.findById(subcategoryId); - if (subCategory == null) { - // EXCEPTION - SubCategory not found - throw new EntityNotFoundException("Change subCategory visibility: SubCategory with id: "+subcategoryId+" not found"); - } - Category category = checkForExceptions(stakeholderId, topicId, categoryId); - - if (category.getSubCategories().contains(subcategoryId)) { - return changeVisibilityTree(subcategoryId, visibility, propagate); - } else { - // EXCEPTION - SubCategory not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); -> Category: category.getAlias(); - throw new PathNotValidException("Toggle subCategory: SubCategory with id: "+subcategoryId+" not found in Category: "+categoryId); - } - } - - public SubCategory changeVisibilityTree(String subCategoryId, Visibility visibility, Boolean propagate) { - SubCategory subCategory = subCategoryDAO.findById(subCategoryId); - if (subCategory == null) { - // EXCEPTION - SubCategory not found - throw new EntityNotFoundException("Change subCategory visibility: SubCategory with id: " + subCategoryId + " not found"); - } - - SubCategory
subCategoryFull = new SubCategory(subCategory); - List
chartsFull = new ArrayList(); - List
numbersFull = new ArrayList(); - - if (propagate != null && propagate) { - for (String sectionId : subCategory.getCharts()) { - chartsFull.add(sectionController.changeVisibilityTree(sectionId, visibility)); - } - for (String sectionId : subCategory.getNumbers()) { - numbersFull.add(sectionController.changeVisibilityTree(sectionId, visibility)); - } - } - subCategory.setVisibility(visibility); - subCategoryDAO.save(subCategory); - log.debug("SubCategory toggled!"); - - subCategoryFull.setVisibility(visibility); - subCategoryFull.setCharts(chartsFull); - subCategoryFull.setNumbers(numbersFull); - - return subCategoryFull; - } - - private Category checkForExceptions(String stakeholderId, String topicId, String categoryId) { - - Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId); - - if(stakeholder == null) { - // EXCEPTION - Stakeholder not found - throw new EntityNotFoundException("Save indicator: Stakeholder with id: " + stakeholderId + " not found"); - } - if(!rolesUtils.hasUpdateAuthority(stakeholder.getType(), stakeholder.getAlias())) { - // EXCEPTION - Access denied - throw new ForbiddenException("CheckForExceptions SubCategory: You are not authorized to update stakeholder with id: "+stakeholderId); - } - - Topic topic = topicDAO.findById(topicId); - if(topic == null) { - // EXCEPTION - Topic not found - throw new EntityNotFoundException("Save indicator: Topic with id: "+topicId+" not found"); - } - - if(!stakeholder.getTopics().contains(topicId)) { - // EXCEPTION - Topic not found in Stakeholder: stakeholder.getAlias(); - throw new PathNotValidException("Save indicator: Topic with id: " + topicId + " not found in Stakeholder: " + stakeholderId); - } - - Category category = categoryDAO.findById(categoryId); - if(category == null) { - // EXCEPTION - Category not found - throw new EntityNotFoundException("Save indicator: Category with id: "+categoryId+" not found"); - } - - if(!topic.getCategories().contains(categoryId)) { - // EXCEPTION - Category not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); - throw new PathNotValidException("Save indicator: Category with id: "+categoryId+" not found in Topic: "+topicId); - } - return category; - } - - public void deleteTree(Category category) { - List subCategories = category.getSubCategories(); - for(String subCategoryId : subCategories) { - SubCategory subCategory = subCategoryDAO.findById(subCategoryId); - if (subCategory == null) { - // EXCEPTION - SubCategory not found - throw new EntityNotFoundException("SubCategory delete tree: SubCategory with id: "+subCategoryId+" not found (subCategory exists in category: "+category.getId()+")"); - } - - sectionController.deleteTree(subCategory); - - subCategoryDAO.delete(subCategoryId); - } - } - - public void disConnectTree(Category category) { - List subCategories = category.getSubCategories(); - for(String subCategoryId : subCategories) { - SubCategory subCategory = subCategoryDAO.findById(subCategoryId); - if (subCategory == null) { - // EXCEPTION - SubCategory not found - throw new EntityNotFoundException("SubCategory disconnect tree: SubCategory with id: "+subCategoryId+" not found (subCategory exists in category: "+category.getId()+")"); - } - - sectionController.disConnectTree(subCategory); - - subCategory.setDefaultId(null); - subCategoryDAO.save(subCategory); - } + public SubCategoryFull changeSubCategoryVisibility(@PathVariable("stakeholderId") String stakeholderId, + @PathVariable("topicId") String topicId, + @PathVariable("categoryId") String categoryId, + @PathVariable("subcategoryId") String subcategoryId, + @RequestParam("visibility") Visibility visibility, + @RequestParam(defaultValue = "false") Boolean propagate) { + log.debug("change subCategory visibility: " + visibility + " - toggle propagate: " + propagate); + log.debug("Stakeholder: " + stakeholderId + " - Topic: " + topicId + " - Category: " + categoryId + " - SubCategory: " + subcategoryId); + Stakeholder stakeholder = this.stakeholderService.findById(stakeholderId); + Topic topic = this.topicService.findByPath(stakeholder, topicId); + Category category = this.categoryService.findByPath(topic, categoryId); + SubCategory subCategory = this.subCategoryService.findByPath(category, subcategoryId); + return this.subCategoryService.changeVisibility(stakeholder.getType(), stakeholder.getAlias(), subCategory, visibility, propagate); } } -*/ diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/TopicController.java b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/TopicController.java index c156be3..c0b1546 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/TopicController.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/TopicController.java @@ -17,8 +17,8 @@ import org.springframework.web.bind.annotation.*; public class TopicController { private final Logger log = LogManager.getLogger(this.getClass()); - private TopicService topicService; - private StakeholderService stakeholderService; + private final TopicService topicService; + private final StakeholderService stakeholderService; @Autowired public TopicController(TopicService topicService, StakeholderService stakeholderService) { @@ -215,9 +215,10 @@ public class TopicController { public TopicFull changeTopicVisibility(@PathVariable("stakeholderId") String stakeholderId, @PathVariable("topicId") String topicId, @RequestParam("visibility") Visibility visibility, @RequestParam(required = false) Boolean propagate) { - log.debug("change topic visibility: " + visibility + " - toggle propagate: " + ((propagate != null && propagate) ? "true" : "false")); + log.debug("change topic visibility: " + visibility + " - toggle propagate: " + propagate); log.debug("Stakeholder: " + stakeholderId + " - Topic: " + topicId); Stakeholder stakeholder = this.stakeholderService.findById(stakeholderId); - return this.topicService.changeVisibility(stakeholder.getType(), stakeholder.getAlias(), topicId, visibility, propagate); + Topic topic = this.topicService.findByPath(stakeholder, topicId); + return this.topicService.changeVisibility(stakeholder.getType(), stakeholder.getAlias(), topic, visibility, propagate); } } diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/service/CategoryService.java b/src/main/java/eu/dnetlib/uoamonitorservice/service/CategoryService.java index 066e6ec..be68f64 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/service/CategoryService.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/service/CategoryService.java @@ -31,17 +31,16 @@ public class CategoryService { this.commonService = commonService; } - public Category findByPath(Topic topic, String categoryId, String message) { + public Category findByPath(Topic topic, String categoryId) { if (!topic.getCategories().contains(categoryId)) { - throw new PathNotValidException(message + ": Category with id: " + categoryId + " not found in Topic: " + topic.getId()); + throw new PathNotValidException("Category with id: " + categoryId + " not found in Topic: " + topic.getId()); } - return this.dao.findById(categoryId).orElseThrow(() -> new EntityNotFoundException(message + ": Category with id: " + categoryId + " not found")); + return this.dao.findById(categoryId).orElseThrow(() -> new EntityNotFoundException("Category with id: " + categoryId + " not found")); } - public CategoryFull getFullCategory(String type, String alias, String categoryId) { - Category category = this.find(categoryId); - if(commonService.hasVisibilityAuthority(type, alias, category)) { - return new CategoryFull(category, category.getSubCategories().stream() + public CategoryFull getFullCategory(String type, String alias, Category category) { + if (commonService.hasVisibilityAuthority(type, alias, category)) { + return new CategoryFull(category, category.getSubCategories().stream() .map(subCategoryId -> this.subCategoryService.getFullSubCategory(type, alias, subCategoryId)) .collect(Collectors.toList())); } else { @@ -49,6 +48,11 @@ public class CategoryService { } } + public CategoryFull getFullCategory(String type, String alias, String categoryId) { + Category category = this.find(categoryId); + return this.getFullCategory(type, alias, category); + } + public Category find(String id) { return dao.findById(id).orElseThrow(() -> new EntityNotFoundException("Category with id: " + id + " not found")); } @@ -65,14 +69,14 @@ public class CategoryService { } public void delete(String type, Category category, boolean remove) { - if(this.commonService.hasDeleteAuthority(type)) { + if (this.commonService.hasDeleteAuthority(type)) { this.dao.findByDefaultId(category.getId()).forEach(child -> { this.delete(type, child.getId(), remove); }); category.getSubCategories().forEach(subcategoryId -> { this.subCategoryService.delete(type, subcategoryId, false); }); - if(remove){ + if (remove) { this.removeCategory(category.getId()); } this.dao.delete(category); @@ -93,20 +97,28 @@ public class CategoryService { }); } - public CategoryFull changeVisibility(String type, String alias, String id, Visibility visibility, Boolean propagate) { - if(this.commonService.hasEditAuthority(type, alias)) { - CategoryFull categoryFull = this.getFullCategory(type, alias, id); - categoryFull.setVisibility(visibility); - if(propagate) { - categoryFull.setSubCategories(categoryFull.getSubCategories().stream() - .map(category -> this.subCategoryService.changeVisibility(type, alias, category.getId(), visibility, true)) + public CategoryFull changeVisibility(String type, String alias, CategoryFull category, Visibility visibility, Boolean propagate) { + if (this.commonService.hasEditAuthority(type, alias)) { + category.setVisibility(visibility); + if (propagate) { + category.setSubCategories(category.getSubCategories().stream() + .map(subCategory -> this.subCategoryService.changeVisibility(type, alias, subCategory, visibility, true)) .collect(Collectors.toList())); } - Category category = this.save(new Category(categoryFull)); - categoryFull.update(category); - return categoryFull; + category.update(this.save(new Category(category))); + return category; } else { - throw new ForbiddenException("Change category visibility: You are not authorized to update category with id: " + id); + throw new ForbiddenException("Change category visibility: You are not authorized to update category with id: " + category.getId()); } } + + public CategoryFull changeVisibility(String type, String alias, Category category, Visibility visibility, Boolean propagate) { + CategoryFull categoryFull = this.getFullCategory(type, alias, category); + return this.changeVisibility(type, alias, categoryFull, visibility, propagate); + } + + public CategoryFull changeVisibility(String type, String alias, String id, Visibility visibility, Boolean propagate) { + Category category = this.find(id); + return this.changeVisibility(type, alias, category, visibility, propagate); + } } diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/service/IndicatorService.java b/src/main/java/eu/dnetlib/uoamonitorservice/service/IndicatorService.java index 9407b3b..9f8dbec 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/service/IndicatorService.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/service/IndicatorService.java @@ -6,7 +6,9 @@ import eu.dnetlib.uoamonitorservice.dao.SectionDAO; import eu.dnetlib.uoamonitorservice.dto.SectionFull; import eu.dnetlib.uoamonitorservice.entities.Indicator; import eu.dnetlib.uoamonitorservice.entities.Section; +import eu.dnetlib.uoamonitorservice.entities.SubCategory; import eu.dnetlib.uoamonitorservice.handlers.EntityNotFoundException; +import eu.dnetlib.uoamonitorservice.handlers.PathNotValidException; import eu.dnetlib.uoamonitorservice.primitives.Visibility; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -32,6 +34,13 @@ public class IndicatorService { return this.dao.save(indicator); } + public Indicator findByPath(Section section, String indicatorId) { + if (!section.getIndicators().contains(indicatorId)) { + throw new PathNotValidException("Indicator with id: " + indicatorId + " not found in Section: " + section.getId()); + } + return this.dao.findById(indicatorId).orElseThrow(() -> new EntityNotFoundException("Indicator with id: " + indicatorId + " not found")); + } + public Indicator find(String id) { return dao.findById(id).orElseThrow(() -> new EntityNotFoundException("Indicator with id: " + id + " not found")); } @@ -72,13 +81,17 @@ public class IndicatorService { }); } - public Indicator changeVisibility(String type, String alias, String id, Visibility visibility) { + public Indicator changeVisibility(String type, String alias, Indicator indicator, Visibility visibility) { if(this.commonService.hasEditAuthority(type, alias)) { - Indicator indicator = this.getIndicator(type, alias, id); indicator.setVisibility(visibility); return this.save(indicator); } else { - throw new ForbiddenException("Change section visibility: You are not authorized to update section with id: " + id); + throw new ForbiddenException("Change section visibility: You are not authorized to update section with id: " + indicator.getId()); } } + + public Indicator changeVisibility(String type, String alias, String id, Visibility visibility) { + Indicator indicator = this.find(id); + return this.changeVisibility(type, alias, indicator, visibility); + } } diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/service/SectionService.java b/src/main/java/eu/dnetlib/uoamonitorservice/service/SectionService.java index e1fda14..780f254 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/service/SectionService.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/service/SectionService.java @@ -4,9 +4,12 @@ import eu.dnetlib.uoaadmintoolslibrary.handlers.ForbiddenException; import eu.dnetlib.uoamonitorservice.dao.SectionDAO; import eu.dnetlib.uoamonitorservice.dao.SubCategoryDAO; import eu.dnetlib.uoamonitorservice.dto.SectionFull; +import eu.dnetlib.uoamonitorservice.entities.Category; import eu.dnetlib.uoamonitorservice.entities.Indicator; import eu.dnetlib.uoamonitorservice.entities.Section; +import eu.dnetlib.uoamonitorservice.entities.SubCategory; import eu.dnetlib.uoamonitorservice.handlers.EntityNotFoundException; +import eu.dnetlib.uoamonitorservice.handlers.PathNotValidException; import eu.dnetlib.uoamonitorservice.primitives.Visibility; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -31,22 +34,11 @@ public class SectionService { this.commonService = commonService; } - public SectionFull changeVisibilityTree(String sectionId, Visibility visibility) { - Section section = this.find(sectionId); - SectionFull sectionFull = new SectionFull(section, section.getIndicators().stream().map(this.indicatorService::find).collect(Collectors.toList())); - sectionFull.setIndicators(sectionFull.getIndicators().stream().map(indicator -> { - indicator.setVisibility(visibility); - return indicatorService.save(indicator); - }).collect(Collectors.toList())); - return sectionFull; - } - - public SectionFull saveSection(SectionFull sectionFull) { - Section section = new Section(sectionFull); - if(section.getId() != null) { - section.setIndicators(this.getIndicatorsId(section.getId())); + public Section findByPath(SubCategory subCategory, String sectionId) { + if (!subCategory.getNumbers().contains(sectionId) || !subCategory.getCharts().contains(sectionId)) { + throw new PathNotValidException("Section with id: " + sectionId + " not found in SubCategory: " + subCategory.getId()); } - return sectionFull; + return this.dao.findById(sectionId).orElseThrow(() -> new EntityNotFoundException("Section with id: " + sectionId + " not found")); } public Section find(String id) { @@ -113,17 +105,20 @@ public class SectionService { }); } - public SectionFull changeVisibility(String type, String alias, String id, Visibility visibility) { + public SectionFull changeVisibility(String type, String alias, SectionFull section, Visibility visibility) { if(this.commonService.hasEditAuthority(type, alias)) { - SectionFull sectionFull = this.getFullSection(type, alias, id); - sectionFull.setIndicators(sectionFull.getIndicators().stream() - .map(category -> this.indicatorService.changeVisibility(type, alias, category.getId(), visibility)) + section.setIndicators(section.getIndicators().stream() + .map(indicator -> this.indicatorService.changeVisibility(type, alias, indicator, visibility)) .collect(Collectors.toList())); - Section section = this.save(new Section(sectionFull)); - sectionFull.update(section); - return sectionFull; + section.update(this.save(new Section(section))); + return section; } else { - throw new ForbiddenException("Change section visibility: You are not authorized to update section with id: " + id); + throw new ForbiddenException("Change section visibility: You are not authorized to update section with id: " + section.getId()); } } + + public SectionFull changeVisibility(String type, String alias, String id, Visibility visibility) { + SectionFull sectionFull = this.getFullSection(type, alias, id); + return this.changeVisibility(type, alias, sectionFull, visibility); + } } diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/service/StakeholderService.java b/src/main/java/eu/dnetlib/uoamonitorservice/service/StakeholderService.java index dcaf1ee..57b031c 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/service/StakeholderService.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/service/StakeholderService.java @@ -122,8 +122,8 @@ public class StakeholderService { if(this.commonService.hasEditAuthority(stakeholder.getType(), stakeholder.getAlias())) { StakeholderFull stakeholderFull = this.getFullStakeholder(stakeholder); if(propagate) { - stakeholderFull.setTopics(stakeholder.getTopics().stream(). - map(topicId -> this.topicService.changeVisibility(stakeholderFull.getType(), stakeholderFull.getAlias(), topicId, visibility, true)) + stakeholderFull.setTopics(stakeholderFull.getTopics().stream(). + map(topic -> this.topicService.changeVisibility(stakeholderFull.getType(), stakeholderFull.getAlias(), topic.getId(), visibility, true)) .collect(Collectors.toList())); } stakeholder = this.save(stakeholder); diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/service/SubCategoryService.java b/src/main/java/eu/dnetlib/uoamonitorservice/service/SubCategoryService.java index d036819..a81560e 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/service/SubCategoryService.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/service/SubCategoryService.java @@ -38,15 +38,14 @@ public class SubCategoryService { this.sectionService = sectionService; } - public SubCategory findByPath(Category category, String subcategoryId, String message) { + public SubCategory findByPath(Category category, String subcategoryId) { if (!category.getSubCategories().contains(subcategoryId)) { - throw new PathNotValidException(message + ": SubCategory with id: " + subcategoryId + " not found in Category: " + category.getId()); + throw new PathNotValidException("SubCategory with id: " + subcategoryId + " not found in Category: " + category.getId()); } - return this.dao.findById(subcategoryId).orElseThrow(() -> new EntityNotFoundException(message + ": SubCategory with id: " + subcategoryId + " not found")); + return this.dao.findById(subcategoryId).orElseThrow(() -> new EntityNotFoundException("SubCategory with id: " + subcategoryId + " not found")); } - public SubCategoryFull getFullSubCategory(String type, String alias, String subCategoryId) { - SubCategory subCategory = this.find(subCategoryId); + public SubCategoryFull getFullSubCategory(String type, String alias, SubCategory subCategory) { if(commonService.hasVisibilityAuthority(type, alias, subCategory)) { return new SubCategoryFull(subCategory, subCategory.getNumbers().stream() .map(sectionId -> this.sectionService.getFullSection(type, alias, sectionId)) @@ -59,6 +58,11 @@ public class SubCategoryService { } } + public SubCategoryFull getFullSubCategory(String type, String alias, String subCategoryId) { + SubCategory subCategory = this.find(subCategoryId); + return this.getFullSubCategory(type, alias, subCategory); + } + public SubCategory find(String id) { return dao.findById(id).orElseThrow(() -> new EntityNotFoundException("SubCategory with id: " + id + " not found")); } @@ -109,23 +113,32 @@ public class SubCategoryService { }); } - public SubCategoryFull changeVisibility(String type, String alias, String id, Visibility visibility, Boolean propagate) { + public SubCategoryFull changeVisibility(String type, String alias, SubCategoryFull subCategory, Visibility visibility, Boolean propagate) { if(this.commonService.hasEditAuthority(type, alias)) { - SubCategoryFull subCategoryFull = this.getFullSubCategory(type, alias, id); - subCategoryFull.setVisibility(visibility); + subCategory.setVisibility(visibility); if(propagate) { - subCategoryFull.setNumbers(subCategoryFull.getNumbers().stream() - .map(category -> this.sectionService.changeVisibility(type, alias, category.getId(), visibility)) + subCategory.setNumbers(subCategory.getNumbers().stream() + .map(section -> this.sectionService.changeVisibility(type, alias, section, visibility)) .collect(Collectors.toList())); - subCategoryFull.setCharts(subCategoryFull.getCharts().stream() - .map(category -> this.sectionService.changeVisibility(type, alias, category.getId(), visibility)) + subCategory.setCharts(subCategory.getCharts().stream() + .map(section -> this.sectionService.changeVisibility(type, alias, section, visibility)) .collect(Collectors.toList())); } - SubCategory subCategory = this.save(new SubCategory(subCategoryFull)); - subCategoryFull.update(subCategory); - return subCategoryFull; + subCategory.update(this.save(new SubCategory(subCategory))); + return subCategory; } else { - throw new ForbiddenException("Change subCategory visibility: You are not authorized to update subCategory with id: " + id); + throw new ForbiddenException("Change subCategory visibility: You are not authorized to update subCategory with id: " + subCategory.getId()); } } + + public SubCategoryFull changeVisibility(String type, String alias, SubCategory subCategory, Visibility visibility, Boolean propagate) { + SubCategoryFull subCategoryFull = this.getFullSubCategory(type, alias, subCategory); + return this.changeVisibility(type, alias, subCategoryFull, visibility, propagate); + } + + + public SubCategoryFull changeVisibility(String type, String alias, String id, Visibility visibility, Boolean propagate) { + SubCategory subCategory = this.find(id); + return this.changeVisibility(type, alias, subCategory, visibility, propagate); + } } diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/service/TopicService.java b/src/main/java/eu/dnetlib/uoamonitorservice/service/TopicService.java index 2c2931c..3fd8657 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/service/TopicService.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/service/TopicService.java @@ -38,8 +38,7 @@ public class TopicService { return this.dao.findById(topicId).orElseThrow(() -> new EntityNotFoundException("Topic with id: " + topicId + " not found")); } - public TopicFull getFullTopic(String type, String alias, String id) { - Topic topic = this.find(id); + public TopicFull getFullTopic(String type, String alias, Topic topic) { if (commonService.hasVisibilityAuthority(type, alias, topic)) { return new TopicFull(topic, topic.getCategories().stream() .map(categoryId -> this.categoryService.getFullCategory(type, alias, categoryId)) @@ -49,6 +48,11 @@ public class TopicService { } } + public TopicFull getFullTopic(String type, String alias, String id) { + Topic topic = this.find(id); + return this.getFullTopic(type, alias, topic); + } + public Topic find(String id) { return dao.findById(id).orElseThrow(() -> new EntityNotFoundException("Topic with id: " + id + " not found")); } @@ -72,7 +76,7 @@ public class TopicService { topic.getCategories().forEach(categoryId -> { this.categoryService.delete(type, categoryId, false); }); - if(remove) { + if (remove) { this.removeTopic(topic.getId()); } this.dao.delete(topic); @@ -93,20 +97,28 @@ public class TopicService { }); } - public TopicFull changeVisibility(String type, String alias, String id, Visibility visibility, Boolean propagate) { + public TopicFull changeVisibility(String type, String alias, TopicFull topic, Visibility visibility, Boolean propagate) { if (this.commonService.hasEditAuthority(type, alias)) { - TopicFull topicFull = this.getFullTopic(type, alias, id); - topicFull.setVisibility(visibility); + topic.setVisibility(visibility); if (propagate) { - topicFull.setCategories(topicFull.getCategories().stream() - .map(category -> this.categoryService.changeVisibility(type, alias, category.getId(), visibility, true)) + topic.setCategories(topic.getCategories().stream() + .map(category -> this.categoryService.changeVisibility(type, alias, category, visibility, true)) .collect(Collectors.toList())); } - Topic topic = this.save(new Topic(topicFull)); - topicFull.update(topic); - return topicFull; + topic.update(this.save(new Topic(topic))); + return topic; } else { - throw new ForbiddenException("Change topic visibility: You are not authorized to update topic with id: " + id); + throw new ForbiddenException("Change topic visibility: You are not authorized to update topic with id: " + topic.getId()); } } + + public TopicFull changeVisibility(String type, String alias, Topic topic, Visibility visibility, Boolean propagate) { + TopicFull topicFull = this.getFullTopic(type, alias, topic); + return this.changeVisibility(type, alias, topicFull, visibility, propagate); + } + + public TopicFull changeVisibility(String type, String alias, String id, Visibility visibility, Boolean propagate) { + Topic topic = this.find(id); + return this.changeVisibility(type, alias, topic, visibility, propagate); + } } From c3cc942332e3f1bae9237a2af73c19315619f885 Mon Sep 17 00:00:00 2001 From: "k.triantafyllou" Date: Thu, 21 Mar 2024 12:42:34 +0200 Subject: [PATCH 07/11] Refactor save topic endpoint and fix change visibility. --- .../controllers/StakeholderController.java | 5 +- .../controllers/TopicController.java | 130 +----------------- .../entities/Stakeholder.java | 8 ++ .../uoamonitorservice/entities/Topic.java | 27 ++++ .../uoamonitorservice/generics/Common.java | 13 ++ .../generics/TopicGeneric.java | 13 +- .../service/CategoryService.java | 5 - .../service/IndicatorService.java | 5 - .../service/SectionService.java | 7 +- .../service/StakeholderService.java | 22 +-- .../service/SubCategoryService.java | 6 - .../service/TopicService.java | 45 +++++- 12 files changed, 110 insertions(+), 176 deletions(-) diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/StakeholderController.java b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/StakeholderController.java index d72bd06..cc0e850 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/StakeholderController.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/StakeholderController.java @@ -126,8 +126,9 @@ public class StakeholderController { @RequestMapping(value = "/{stakeholderId}/change-visibility", method = RequestMethod.POST) public StakeholderFull changeStakeholderVisibility(@PathVariable("stakeholderId") String stakeholderId, @RequestParam("visibility") Visibility visibility, @RequestParam(defaultValue = "false") Boolean propagate) { - log.debug("change stakeholder visibility: " + visibility + " - toggle propagate: " + ((propagate != null && propagate) ? "true" : "false")); + log.debug("change stakeholder visibility: " + visibility + " - toggle propagate: " + propagate); log.debug("Stakeholder: " + stakeholderId); - return this.stakeholderService.changeVisibility(stakeholderId, visibility, propagate); + Stakeholder stakeholder = this.stakeholderService.findByPath(stakeholderId); + return this.stakeholderService.changeVisibility(stakeholder, visibility, propagate); } } diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/TopicController.java b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/TopicController.java index c0b1546..1c9ff6a 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/TopicController.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/TopicController.java @@ -26,133 +26,15 @@ public class TopicController { this.stakeholderService = stakeholderService; } - /*@PreAuthorize("isAuthenticated()") + @PreAuthorize("isAuthenticated()") @RequestMapping(value = "/{stakeholderId}/save", method = RequestMethod.POST) - public Topic saveTopic(@PathVariable("stakeholderId") String stakeholderId, - @RequestBody Topic topicFull) { + public TopicFull saveTopic(@PathVariable("stakeholderId") String stakeholderId, @RequestBody TopicFull topicFull) { log.debug("save topic"); - log.debug("Alias: "+topicFull.getAlias() + " - Id: "+topicFull.getId()+ " - Stakeholder: "+stakeholderId); - - Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId); - - if(stakeholder != null) { - if(!rolesUtils.hasUpdateAuthority(stakeholder.getType(), stakeholder.getAlias())) { - // EXCEPTION - Access denied - throw new ForbiddenException("Save Topic: You are not authorized to update stakeholder with id: "+stakeholderId); - } - - Topic topic = new Topic<>(topicFull); - Date date = new Date(); - topic.setUpdateDate(date); - topicFull.setUpdateDate(date); - - List categories = new ArrayList<>(); - - Topic oldTopic = null; - if(topicFull.getId() != null) { - oldTopic = topicDAO.findById(topicFull.getId()); - if(oldTopic == null) { - // EXCEPTION - Topic not found - throw new EntityNotFoundException("save topic: Topic with id: "+topicFull.getId()+" not found"); - } - for(String categoryId : oldTopic.getCategories()) { - Category category = categoryDAO.findById(categoryId); - if (category == null) { - // EXCEPTION - Category not found - throw new EntityNotFoundException("Save topic: Category with id: "+categoryId+" not found (category exists in topic: "+topic.getId()+")"); - } - categories.add(category.getId()); - } - } else { // topic does not exist in DB - topic.setCreationDate(date); - topicFull.setCreationDate(date); - - for(Category category : topicFull.getCategories()) { - categories.add(category.getId()); - } - } - - topic.setCategories(categories); - - if(stakeholder.getDefaultId() == null) { - if(topicFull.getId() == null) { - topicDAO.save(topic); - onSaveDefaultTopic(topic, stakeholderId); - } else { - onUpdateDefaultTopic(topic, oldTopic); - topicDAO.save(topic); - } - } else { - topicDAO.save(topic); - } - - List topics = stakeholder.getTopics(); - int index = topics.indexOf(topic.getId()); - if(index == -1) { - topics.add(topic.getId()); - stakeholderDAO.save(stakeholder); - log.debug("Topic saved!"); - - topicFull.setId(topic.getId()); - } - } else { - // EXCEPTION - Stakeholder not found - throw new EntityNotFoundException("Save topic: Stakeholder with id: "+stakeholderId+" not found"); - } - return topicFull; + log.debug("Alias: " + topicFull.getAlias() + " - Id: " + topicFull.getId() + " - Stakeholder: " + stakeholderId); + Stakeholder stakeholder = this.stakeholderService.findByPath(stakeholderId); + return this.topicService.save(stakeholder, new Topic(topicFull)); } - public void onSaveDefaultTopic(Topic topic, String stakeholderId) { - log.debug("On save default topic"); - - List stakeholders = stakeholderDAO.findByDefaultId(stakeholderId); - for(Stakeholder _stakeholder : stakeholders) { - Topic topicNew = new Topic(); - topicNew.copyFromDefault(topic); - - topicDAO.save(topicNew); - - List topics = _stakeholder.getTopics(); - topics.add(topicNew.getId()); - - stakeholderDAO.save(_stakeholder); - } - } - - public void onUpdateDefaultTopic(Topic topic, Topic oldTopic) { - log.debug("On update default topic"); - - List topics = topicDAO.findByDefaultId(topic.getId()); - boolean changed = false; - for(Topic topicBasedOnDefault : topics) { - if(topic.getName() != null && !topic.getName().equals(topicBasedOnDefault.getName()) - && (oldTopic.getName() == null || oldTopic.getName().equals(topicBasedOnDefault.getName()))) { - - topicBasedOnDefault.setName(topic.getName()); - topicBasedOnDefault.setAlias(topic.getAlias()); - changed = true; - } - if(topic.getDescription() != null && !topic.getDescription().equals(topicBasedOnDefault.getDescription()) - && (oldTopic.getDescription() == null || oldTopic.getDescription().equals(topicBasedOnDefault.getDescription()))) { - - topicBasedOnDefault.setDescription(topic.getDescription()); - changed = true; - } - if(topic.getIcon() != null && !topic.getIcon().equals(topicBasedOnDefault.getIcon()) - && (oldTopic.getIcon() == null || oldTopic.getIcon().equals(topicBasedOnDefault.getIcon()))) { - - topicBasedOnDefault.setIcon(topic.getIcon()); - changed = true; - } - - if(!changed) { - continue; - } - topicBasedOnDefault.setUpdateDate(topic.getUpdateDate()); - topicDAO.save(topicBasedOnDefault); - } - }*/ - @PreAuthorize("isAuthenticated()") @RequestMapping(value = "/{stakeholderId}/{topicId}/delete", method = RequestMethod.DELETE) public boolean deleteTopic(@PathVariable("stakeholderId") String stakeholderId, @@ -214,7 +96,7 @@ public class TopicController { @RequestMapping(value = "/{stakeholderId}/{topicId}/change-visibility", method = RequestMethod.POST) public TopicFull changeTopicVisibility(@PathVariable("stakeholderId") String stakeholderId, @PathVariable("topicId") String topicId, - @RequestParam("visibility") Visibility visibility, @RequestParam(required = false) Boolean propagate) { + @RequestParam("visibility") Visibility visibility, @RequestParam(defaultValue = "false") Boolean propagate) { log.debug("change topic visibility: " + visibility + " - toggle propagate: " + propagate); log.debug("Stakeholder: " + stakeholderId + " - Topic: " + topicId); Stakeholder stakeholder = this.stakeholderService.findById(stakeholderId); diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/entities/Stakeholder.java b/src/main/java/eu/dnetlib/uoamonitorservice/entities/Stakeholder.java index 184dc8c..319b1c2 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/entities/Stakeholder.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/entities/Stakeholder.java @@ -20,4 +20,12 @@ public class Stakeholder extends StakeholderGeneric { this.topics = stakeholder.getTopics().stream().map(Common::getId).collect(Collectors.toList()); this.topics.removeIf(Objects::isNull); } + + public void addTopic(String id) { + this.topics.add(id); + } + + public void removeTopic(String id) { + this.topics.remove(id); + } } diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/entities/Topic.java b/src/main/java/eu/dnetlib/uoamonitorservice/entities/Topic.java index e11ccb2..19eef8d 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/entities/Topic.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/entities/Topic.java @@ -5,6 +5,8 @@ import eu.dnetlib.uoamonitorservice.generics.Common; import eu.dnetlib.uoamonitorservice.generics.TopicGeneric; import org.springframework.data.mongodb.core.mapping.Document; +import java.util.ArrayList; +import java.util.List; import java.util.Objects; import java.util.stream.Collectors; @@ -15,9 +17,34 @@ public class Topic extends TopicGeneric { super(); } + public Topic(Topic topic) { + super(topic); + } + public Topic(TopicFull topic) { super(topic); this.categories = topic.getCategories().stream().map(Common::getId).collect(Collectors.toList()); this.categories.removeIf(Objects::isNull); } + + public Topic(TopicFull topic, List categories) { + super(topic); + this.categories = new ArrayList<>(categories); + } + + public Topic copy() { + Topic topic = new Topic(this); + topic.setDefaultId(this.getId()); + topic.setId(null); + return topic; + } + + public Topic override(Topic topic, Topic old) { + topic = (Topic) super.override(topic, old); + if(this.getIcon() != null && !this.getIcon().equals(topic.getIcon()) && + (old.getIcon() == null || old.getIcon().equals(topic.getIcon()))) { + topic.setIcon(this.getIcon()); + } + return topic; + } } diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/generics/Common.java b/src/main/java/eu/dnetlib/uoamonitorservice/generics/Common.java index 8a77b1e..2075c09 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/generics/Common.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/generics/Common.java @@ -29,6 +29,19 @@ public class Common { this.updateDate = common.getUpdateDate(); } + public Common override(Common common, Common old) { + if(this.getName() != null && !this.getName().equals(common.getName()) && + (old.getName() == null || old.getName().equals(common.getName()))) { + common.setName(this.getName()); + common.setAlias(this.getAlias()); + } + if(this.getDescription() != null && !this.getDescription().equals(common.getDescription()) && + (old.getDescription() == null || old.getDescription().equals(common.getDescription()))) { + common.setDescription(this.getDescription()); + } + return common; + } + public String getId() { return id; } diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/generics/TopicGeneric.java b/src/main/java/eu/dnetlib/uoamonitorservice/generics/TopicGeneric.java index 7dc1104..af48845 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/generics/TopicGeneric.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/generics/TopicGeneric.java @@ -27,18 +27,7 @@ public class TopicGeneric extends Common { creationDate = topic.getCreationDate(); updateDate = topic.getUpdateDate(); defaultId = topic.getDefaultId(); - } - - public void copyFromDefault(TopicGeneric defaultTopic) { - setName(defaultTopic.getName()); - setAlias(defaultTopic.getAlias()); - setDescription(defaultTopic.getDescription()); - setIcon(defaultTopic.getIcon()); - setVisibility(defaultTopic.getVisibility()); - setCreationDate(defaultTopic.getCreationDate()); - setUpdateDate(defaultTopic.getUpdateDate()); - setDefaultId(defaultTopic.getId()); - setCategories(new ArrayList<>()); + categories = new ArrayList<>(); } diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/service/CategoryService.java b/src/main/java/eu/dnetlib/uoamonitorservice/service/CategoryService.java index be68f64..7e97767 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/service/CategoryService.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/service/CategoryService.java @@ -116,9 +116,4 @@ public class CategoryService { CategoryFull categoryFull = this.getFullCategory(type, alias, category); return this.changeVisibility(type, alias, categoryFull, visibility, propagate); } - - public CategoryFull changeVisibility(String type, String alias, String id, Visibility visibility, Boolean propagate) { - Category category = this.find(id); - return this.changeVisibility(type, alias, category, visibility, propagate); - } } diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/service/IndicatorService.java b/src/main/java/eu/dnetlib/uoamonitorservice/service/IndicatorService.java index 9f8dbec..7030aa1 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/service/IndicatorService.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/service/IndicatorService.java @@ -89,9 +89,4 @@ public class IndicatorService { throw new ForbiddenException("Change section visibility: You are not authorized to update section with id: " + indicator.getId()); } } - - public Indicator changeVisibility(String type, String alias, String id, Visibility visibility) { - Indicator indicator = this.find(id); - return this.changeVisibility(type, alias, indicator, visibility); - } } diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/service/SectionService.java b/src/main/java/eu/dnetlib/uoamonitorservice/service/SectionService.java index 780f254..696c54e 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/service/SectionService.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/service/SectionService.java @@ -35,7 +35,7 @@ public class SectionService { } public Section findByPath(SubCategory subCategory, String sectionId) { - if (!subCategory.getNumbers().contains(sectionId) || !subCategory.getCharts().contains(sectionId)) { + if (!subCategory.getNumbers().contains(sectionId) && !subCategory.getCharts().contains(sectionId)) { throw new PathNotValidException("Section with id: " + sectionId + " not found in SubCategory: " + subCategory.getId()); } return this.dao.findById(sectionId).orElseThrow(() -> new EntityNotFoundException("Section with id: " + sectionId + " not found")); @@ -116,9 +116,4 @@ public class SectionService { throw new ForbiddenException("Change section visibility: You are not authorized to update section with id: " + section.getId()); } } - - public SectionFull changeVisibility(String type, String alias, String id, Visibility visibility) { - SectionFull sectionFull = this.getFullSection(type, alias, id); - return this.changeVisibility(type, alias, sectionFull, visibility); - } } diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/service/StakeholderService.java b/src/main/java/eu/dnetlib/uoamonitorservice/service/StakeholderService.java index 57b031c..56259b0 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/service/StakeholderService.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/service/StakeholderService.java @@ -116,21 +116,23 @@ public class StakeholderService { } } - public StakeholderFull changeVisibility(String id, Visibility visibility, Boolean propagate) { - Stakeholder stakeholder = this.findById(id); - stakeholder.setVisibility(visibility); + public StakeholderFull changeVisibility(StakeholderFull stakeholder, Visibility visibility, Boolean propagate) { if(this.commonService.hasEditAuthority(stakeholder.getType(), stakeholder.getAlias())) { - StakeholderFull stakeholderFull = this.getFullStakeholder(stakeholder); if(propagate) { - stakeholderFull.setTopics(stakeholderFull.getTopics().stream(). - map(topic -> this.topicService.changeVisibility(stakeholderFull.getType(), stakeholderFull.getAlias(), topic.getId(), visibility, true)) + stakeholder.setTopics(stakeholder.getTopics().stream(). + map(topic -> this.topicService.changeVisibility(stakeholder.getType(), stakeholder.getAlias(), topic, visibility, true)) .collect(Collectors.toList())); } - stakeholder = this.save(stakeholder); - stakeholderFull.update(stakeholder); - return stakeholderFull; + stakeholder.setVisibility(visibility); + stakeholder.update(this.save(new Stakeholder(stakeholder))); + return stakeholder; } else { - throw new ForbiddenException("Change stakeholder visibility: You are not authorized to update stakeholder with id: " + id); + throw new ForbiddenException("Change stakeholder visibility: You are not authorized to update stakeholder with id: " + stakeholder.getId()); } } + + public StakeholderFull changeVisibility(Stakeholder stakeholder, Visibility visibility, Boolean propagate) { + StakeholderFull stakeholderFull = this.getFullStakeholder(stakeholder); + return this.changeVisibility(stakeholderFull, visibility, propagate); + } } diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/service/SubCategoryService.java b/src/main/java/eu/dnetlib/uoamonitorservice/service/SubCategoryService.java index a81560e..f0da766 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/service/SubCategoryService.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/service/SubCategoryService.java @@ -135,10 +135,4 @@ public class SubCategoryService { SubCategoryFull subCategoryFull = this.getFullSubCategory(type, alias, subCategory); return this.changeVisibility(type, alias, subCategoryFull, visibility, propagate); } - - - public SubCategoryFull changeVisibility(String type, String alias, String id, Visibility visibility, Boolean propagate) { - SubCategory subCategory = this.find(id); - return this.changeVisibility(type, alias, subCategory, visibility, propagate); - } } diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/service/TopicService.java b/src/main/java/eu/dnetlib/uoamonitorservice/service/TopicService.java index 3fd8657..93b46f5 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/service/TopicService.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/service/TopicService.java @@ -12,6 +12,7 @@ import eu.dnetlib.uoamonitorservice.primitives.Visibility; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import java.util.List; import java.util.stream.Collectors; @Service @@ -63,6 +64,38 @@ public class TopicService { return topicFull; } + public TopicFull save(Stakeholder stakeholder, Topic topic) { + if(topic.getId() != null) { + if(this.commonService.hasEditAuthority(stakeholder.getType(), stakeholder.getAlias())) { + this.updateChildren(topic); + topic = this.save(topic); + } else { + throw new ForbiddenException("You are not authorized to update stakeholder with id: " + stakeholder.getId()); + } + } else { + if(this.commonService.hasCreateAuthority(stakeholder.getType())) { + this.createChildren(stakeholder, topic); + topic = this.save(topic); + this.addTopic(stakeholder, topic.getId()); + } else { + throw new ForbiddenException("You are not authorized to create a topic in stakeholder with id: " + stakeholder.getId()); + } + } + return this.getFullTopic(stakeholder.getType(), stakeholder.getAlias(), topic); + } + + public void createChildren(Stakeholder defaultStakeholder, Topic topic) { + this.stakeholderDAO.findByDefaultId(defaultStakeholder.getId()).forEach(stakeholder -> { + this.save(stakeholder, topic.copy()); + }); + } + + public void updateChildren(Topic topic) { + this.dao.findByDefaultId(topic.getId()).forEach(child -> { + this.save(topic.override(child, this.find(topic.getId()))); + }); + } + public Topic save(Topic topic) { topic.getCategories().forEach(this.categoryService::find); return this.dao.save(topic); @@ -90,9 +123,14 @@ public class TopicService { this.delete(type, topic, remove); } + public void addTopic(Stakeholder stakeholder, String id) { + stakeholder.addTopic(id); + this.stakeholderDAO.save(stakeholder); + } + public void removeTopic(String id) { this.stakeholderDAO.findByTopicsContaining(id).forEach(stakeholder -> { - stakeholder.getTopics().remove(id); + stakeholder.removeTopic(id); this.stakeholderDAO.save(stakeholder); }); } @@ -116,9 +154,4 @@ public class TopicService { TopicFull topicFull = this.getFullTopic(type, alias, topic); return this.changeVisibility(type, alias, topicFull, visibility, propagate); } - - public TopicFull changeVisibility(String type, String alias, String id, Visibility visibility, Boolean propagate) { - Topic topic = this.find(id); - return this.changeVisibility(type, alias, topic, visibility, propagate); - } } From 8a56c3ad8bfc1a827089801573d609654025ab86 Mon Sep 17 00:00:00 2001 From: "k.triantafyllou" Date: Wed, 27 Mar 2024 14:07:41 +0200 Subject: [PATCH 08/11] Save category, subcategory, section refactor. --- .../controllers/CategoryController.java | 47 ++---- .../controllers/SectionController.java | 148 ++---------------- .../controllers/StakeholderController.java | 4 +- .../controllers/SubCategoryController.java | 138 ++-------------- .../uoamonitorservice/dto/CategoryFull.java | 2 +- .../uoamonitorservice/dto/SectionFull.java | 7 +- .../dto/StakeholderFull.java | 2 +- .../dto/SubCategoryFull.java | 4 +- .../uoamonitorservice/dto/TopicFull.java | 2 +- .../uoamonitorservice/entities/Category.java | 40 +++-- .../uoamonitorservice/entities/Section.java | 21 +++ .../entities/SubCategory.java | 44 +++++- .../uoamonitorservice/entities/Topic.java | 8 + .../generics/CategoryGeneric.java | 2 + .../generics/SectionGeneric.java | 5 + .../generics/SubCategoryGeneric.java | 32 +--- .../service/CategoryService.java | 63 +++++++- .../service/SectionService.java | 88 ++++++++--- .../service/StakeholderService.java | 3 +- .../service/SubCategoryService.java | 65 +++++++- .../service/TopicService.java | 21 +-- 21 files changed, 348 insertions(+), 398 deletions(-) diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/CategoryController.java b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/CategoryController.java index b6980d5..ce406b4 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/CategoryController.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/CategoryController.java @@ -30,17 +30,17 @@ public class CategoryController { this.categoryService = categoryService; } - /*@PreAuthorize("isAuthenticated()") + @PreAuthorize("isAuthenticated()") @RequestMapping(value = "/{stakeholderId}/{topicId}/save", method = RequestMethod.POST) - public Category saveCategory(@PathVariable("stakeholderId") String stakeholderId, + public CategoryFull saveCategory(@PathVariable("stakeholderId") String stakeholderId, @PathVariable("topicId") String topicId, - @RequestBody Category categoryFull) { + @RequestBody CategoryFull categoryFull) { log.debug("save category"); log.debug("Alias: " + categoryFull.getAlias() + " - Id: " + categoryFull.getId() + " - Stakeholder: " + stakeholderId + " - Topic: " + topicId); - - Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId); - - if (stakeholder != null) { + Stakeholder stakeholder = stakeholderService.findByPath(stakeholderId); + Topic topic = topicService.findByPath(stakeholder, topicId); + return this.categoryService.save(stakeholder, topic, new Category(categoryFull)); + /*if (stakeholder != null) { if (!rolesUtils.hasUpdateAuthority(stakeholder.getType(), stakeholder.getAlias())) { // EXCEPTION - Access denied throw new ForbiddenException("Save Category: You are not authorized to update stakeholder with id: " + stakeholderId); @@ -128,10 +128,10 @@ public class CategoryController { // EXCEPTION - Stakeholder not found throw new EntityNotFoundException("Save category: Stakeholder with id: " + stakeholderId + " not found"); } - return categoryFull; - }*/ + return categoryFull;*/ + } - /*public void onSaveDefaultCategory(Category category, String topicId) { + /* public void onSaveDefaultCategory(Category category, String topicId) { log.debug("On save default category"); List topics = topicDAO.findByDefaultId(topicId); @@ -151,33 +151,6 @@ public class CategoryController { subCategoryController.onSaveDefaultSubCategory(subCategoryOverview, category.getId()); }*/ - /*public void onUpdateDefaultCategory(Category category, Category oldCategory) { - log.debug("On update default category"); - - List categories = categoryDAO.findByDefaultId(category.getId()); - boolean changed = false; - for (Category categoryBasedOnDefault : categories) { - if (category.getName() != null && !category.getName().equals(categoryBasedOnDefault.getName()) - && (oldCategory.getName() == null || oldCategory.getName().equals(categoryBasedOnDefault.getName()))) { - - categoryBasedOnDefault.setName(category.getName()); - categoryBasedOnDefault.setAlias(category.getAlias()); - changed = true; - } - if (category.getDescription() != null && !category.getDescription().equals(categoryBasedOnDefault.getDescription()) - && (oldCategory.getDescription() == null || oldCategory.getDescription().equals(categoryBasedOnDefault.getDescription()))) { - - categoryBasedOnDefault.setDescription(category.getDescription()); - changed = true; - } - - if (!changed) { - continue; - } - categoryBasedOnDefault.setUpdateDate(category.getUpdateDate()); - categoryDAO.save(categoryBasedOnDefault); - } - }*/ @PreAuthorize("isAuthenticated()") @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/delete", method = RequestMethod.DELETE) diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/SectionController.java b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/SectionController.java index 4531344..f93d6da 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/SectionController.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/SectionController.java @@ -1,5 +1,6 @@ package eu.dnetlib.uoamonitorservice.controllers; +import eu.dnetlib.uoamonitorservice.dto.SectionFull; import eu.dnetlib.uoamonitorservice.entities.*; import eu.dnetlib.uoamonitorservice.service.*; import org.apache.logging.log4j.LogManager; @@ -29,154 +30,23 @@ public class SectionController { this.sectionService = sectionService; } - /*@PreAuthorize("isAuthenticated()") + @PreAuthorize("isAuthenticated()") @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/save/{index}", method = RequestMethod.POST) - public Section saveSection(@PathVariable("stakeholderId") String stakeholderId, + public SectionFull saveSection(@PathVariable("stakeholderId") String stakeholderId, @PathVariable("topicId") String topicId, @PathVariable("categoryId") String categoryId, @PathVariable("subcategoryId") String subcategoryId, @PathVariable("index") String index, - @RequestBody Section sectionFull) { + @RequestBody SectionFull sectionFull) { log.debug("save section"); log.debug("Name: "+sectionFull.getTitle() + " - Id: "+sectionFull.getId() + " - Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId+ " - SubCategory: "+subcategoryId); - - SubCategory subCategory = checkForExceptions(stakeholderId, topicId, categoryId, subcategoryId); - - Section section = new Section<>(sectionFull); - - Date date = new Date(); - section.setUpdateDate(date); - sectionFull.setUpdateDate(date); - - List indicators = new ArrayList<>(); - - Section oldSection = null; - if(sectionFull.getId() != null) { - oldSection = sectionDAO.findById(sectionFull.getId()); - if(oldSection == null) { - // EXCEPTION - Section not found - throw new EntityNotFoundException("save section: Section with id: " + sectionFull.getId() + " not found"); - } - - for(String indicatorId : oldSection.getIndicators()) { - Indicator indicator = indicatorDAO.findById(indicatorId); - if (indicator == null) { - // EXCEPTION - Indicator not found - throw new EntityNotFoundException("Save section: Indicator with id: "+indicatorId+" not found (indicator exists in section: "+section.getId()+")"); - } - indicators.add(indicator.getId()); - } - } else { // section does not exist in DB - section.setCreationDate(date); - sectionFull.setCreationDate(date); - - for(Indicator indicator : sectionFull.getIndicators()) { - indicators.add(indicator.getId()); - } - } - - String sectionId = sectionFull.getId(); - - section.setIndicators(indicators); - - Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId); - // this section belongs in default profile and it is new or it is updated - if(stakeholder.getDefaultId() == null) { - if(sectionId == null) { - sectionDAO.save(section); - onSaveDefaultSection(section, topicId, categoryId, subcategoryId, stakeholder); - } - else { - onUpdateDefaultSection(section, stakeholder, oldSection); - sectionDAO.save(section); - } - } else { - sectionDAO.save(section); - } - - List sections = null; - if(sectionFull.getType().equals("chart")) { - sections = subCategory.getCharts(); - } else if(sectionFull.getType().equals("number")) { - sections = subCategory.getNumbers(); - } - - int existing_index = sections.indexOf(section.getId()); - if (existing_index == -1) { - if(Integer.parseInt(index) != -1) { - sections.add(Integer.parseInt(index), section.getId()); - } else { - sections.add(section.getId()); - } - subCategoryDAO.save(subCategory); - log.debug("Section saved!"); - - sectionFull.setId(section.getId()); - } - - return sectionFull; + Stakeholder stakeholder = this.stakeholderService.findByPath(stakeholderId); + Topic topic = this.topicService.findByPath(stakeholder, topicId); + Category category = this.categoryService.findByPath(topic, categoryId); + SubCategory subCategory = this.subCategoryService.findByPath(category, subcategoryId); + return this.sectionService.save(stakeholder, subCategory, new Section(sectionFull), Integer.parseInt(index)); } - public void onSaveDefaultSection(Section section, - String defaultTopicId, String defaultCategoryId, - String defaultSubcategoryId, Stakeholder defaultStakeholder) { - log.debug("On save default section"); - - - // new section in default profile - add it on profiles of the same type - List subCategories = subCategoryDAO.findByDefaultId(defaultSubcategoryId); - - for (SubCategory subCategory : subCategories) { - Category parentCategory = categoryDAO.findBySubCategoriesContaining(subCategory.getId()); - Topic parentTopic = topicDAO.findByCategoriesContaining(parentCategory.getId()); - Stakeholder parentStakeholder = stakeholderDAO.findByTopicsContaining(parentTopic.getId()); - - Section sectionNew = new Section(); - sectionNew.copyFromDefault(section); - - sectionNew.setStakeholderAlias(parentStakeholder.getAlias()); - - sectionDAO.save(sectionNew); - - List sections = null; - if (section.getType().equals("chart")) { - sections = subCategory.getCharts(); - } else if (section.getType().equals("number")) { - sections = subCategory.getNumbers(); - } - sections.add(sectionNew.getId()); - - subCategoryDAO.save(subCategory); - } - } - - public void onUpdateDefaultSection(Section section, Stakeholder stakeholder, Section oldSection) { - log.debug("On update default section"); - - // section already exists - check if changed and update all sections based on it - - boolean changed = false; - List
sections = sectionDAO.findByDefaultId(section.getId()); - - for(Section sectionBasedOnDefault : sections) { - if(section.getTitle() != null && !section.getTitle().equals(sectionBasedOnDefault.getTitle()) - && (oldSection.getTitle() == null || oldSection.getTitle().equals(sectionBasedOnDefault.getTitle()))) { - - sectionBasedOnDefault.setTitle(section.getTitle()); - changed = true; - } - - if(!changed) { -// break; - continue; - } - -// sectionBasedOnDefault.setTitle(section.getTitle()); - sectionBasedOnDefault.setUpdateDate(section.getUpdateDate()); - sectionDAO.save(sectionBasedOnDefault); - } - }*/ - @PreAuthorize("isAuthenticated()") @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/{sectionId}/delete", method = RequestMethod.DELETE) public boolean deleteSection(@PathVariable("stakeholderId") String stakeholderId, diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/StakeholderController.java b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/StakeholderController.java index cc0e850..7df0dfb 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/StakeholderController.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/StakeholderController.java @@ -77,14 +77,14 @@ public class StakeholderController { } @RequestMapping(value = "/stakeholder", method = RequestMethod.GET) - public List getAllRealStakeholders(@RequestParam(required = false) String type, + public List getVisibleStakeholders(@RequestParam(required = false) String type, @RequestParam(required = false) String defaultId) { return stakeholderService.getStakeholdersByTypeAndRole(type, defaultId, false); } @PreAuthorize("isAuthenticated()") @RequestMapping(value = "/my-stakeholder", method = RequestMethod.GET) - public List getMyRealStakeholders(@RequestParam(required = false) String type) { + public List getManagedStakeholders(@RequestParam(required = false) String type) { return stakeholderService.getStakeholdersByTypeAndRole(type, null, true); } diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/SubCategoryController.java b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/SubCategoryController.java index ef9b4d6..32dee17 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/SubCategoryController.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/SubCategoryController.java @@ -1,6 +1,8 @@ package eu.dnetlib.uoamonitorservice.controllers; +import eu.dnetlib.uoamonitorservice.dto.CategoryFull; import eu.dnetlib.uoamonitorservice.dto.SubCategoryFull; +import eu.dnetlib.uoamonitorservice.dto.TopicFull; import eu.dnetlib.uoamonitorservice.entities.Category; import eu.dnetlib.uoamonitorservice.entities.Stakeholder; import eu.dnetlib.uoamonitorservice.entities.SubCategory; @@ -34,136 +36,20 @@ public class SubCategoryController { this.subCategoryService = subCategoryService; } - /* @PreAuthorize("isAuthenticated()") + @PreAuthorize("isAuthenticated()") @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/save", method = RequestMethod.POST) - public SubCategory> saveSubCategory(@PathVariable("stakeholderId") String stakeholderId, - @PathVariable("topicId") String topicId, - @PathVariable("categoryId") String categoryId, - @RequestBody SubCategory> subcategoryFull) { + public SubCategoryFull saveSubCategory(@PathVariable("stakeholderId") String stakeholderId, + @PathVariable("topicId") String topicId, + @PathVariable("categoryId") String categoryId, + @RequestBody SubCategoryFull subcategoryFull) { log.debug("save subcategory"); - log.debug("Alias: "+subcategoryFull.getAlias() + " - Id: "+subcategoryFull.getId() + " - Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId); - - Category category = checkForExceptions(stakeholderId, topicId, categoryId); - - SubCategory subCategory = new SubCategory<>(subcategoryFull); - - Date date = new Date(); - subCategory.setUpdateDate(date); - subcategoryFull.setUpdateDate(date); - - List chartSections = new ArrayList<>(); - List numberSections = new ArrayList<>(); - - SubCategory oldSubcategory = null; - if(subcategoryFull.getId() != null) { - oldSubcategory = subCategoryDAO.findById(subcategoryFull.getId()); - if(oldSubcategory == null) { - // EXCEPTION - SubCategory not found - throw new EntityNotFoundException("save subcategory: SubCategory with id: " + subcategoryFull.getId() + " not found"); - } - - for(String chartSectionId : oldSubcategory.getCharts()) { - Section section = sectionDAO.findById(chartSectionId); - if (section == null) { - // EXCEPTION - Section not found - throw new EntityNotFoundException("Save subcategory: Chart section with id: "+chartSectionId+" not found (section exists in subcategory: "+subCategory.getId()+")"); - } - chartSections.add(section.getId()); - } - - for(String numberSectionId : oldSubcategory.getNumbers()) { - Section section = sectionDAO.findById(numberSectionId); - if (section == null) { - // EXCEPTION - Section not found - throw new EntityNotFoundException("Save subcategory: Number section with id: "+numberSectionId+" not found (section exists in subcategory: "+subCategory.getId()+")"); - } - numberSections.add(section.getId()); - } - } else { // subcategory does not exist in DB - subCategory.setCreationDate(date); - subcategoryFull.setCreationDate(date); - - for(Section chartSection : subcategoryFull.getCharts()) { - chartSections.add(chartSection.getId()); - } - - for(Section numberSection : subcategoryFull.getNumbers()) { - numberSections.add(numberSection.getId()); - } - } - subCategory.setCharts(chartSections); - subCategory.setNumbers(numberSections); - - Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId); - if(stakeholder.getDefaultId() == null) { - if(subcategoryFull.getId() == null) { - subCategoryDAO.save(subCategory); - onSaveDefaultSubCategory(subCategory, categoryId); - } else { - onUpdateDefaultSubCategory(subCategory, oldSubcategory); - subCategoryDAO.save(subCategory); - } - } else { - subCategoryDAO.save(subCategory); - } - - List subcategories = category.getSubCategories(); - int index = subcategories.indexOf(subCategory.getId()); - if(index == -1) { - subcategories.add(subCategory.getId()); - categoryDAO.save(category); - log.debug("Subcategory saved!"); - - subcategoryFull.setId(subCategory.getId()); - } - return subcategoryFull; + log.debug("Alias: " + subcategoryFull.getAlias() + " - Id: " + subcategoryFull.getId() + " - Stakeholder: " + stakeholderId + " - Topic: " + topicId + " - Category: " + categoryId); + Stakeholder stakeholder = this.stakeholderService.findByPath(stakeholderId); + Topic topic = this.topicService.findByPath(stakeholder, topicId); + Category category = this.categoryService.findByPath(topic, categoryId); + return this.subCategoryService.save(stakeholder, category, new SubCategory(subcategoryFull)); } - public void onSaveDefaultSubCategory(SubCategory subCategory, String categoryId) { - log.debug("On save default subCategory"); - - List categories = categoryDAO.findByDefaultId(categoryId); - for(Category category : categories) { - SubCategory subCategoryNew = new SubCategory(); - subCategoryNew.copyFromDefault(subCategory); - - subCategoryDAO.save(subCategoryNew); - - List subCategories = category.getSubCategories(); - subCategories.add(subCategoryNew.getId()); - - categoryDAO.save(category); - } - } - - public void onUpdateDefaultSubCategory(SubCategory subCategory, SubCategory oldSubcategory) { - log.debug("On update default subCategory"); - - List subCategories = subCategoryDAO.findByDefaultId(subCategory.getId()); - boolean changed = false; - for(SubCategory subCategoryBasedOnDefault : subCategories) { - if(subCategory.getName() != null && !subCategory.getName().equals(subCategoryBasedOnDefault.getName()) - && (oldSubcategory.getName() == null || oldSubcategory.getName().equals(subCategoryBasedOnDefault.getName()))) { - - subCategoryBasedOnDefault.setName(subCategory.getName()); - subCategoryBasedOnDefault.setAlias(subCategory.getAlias()); - changed = true; - } - if(subCategory.getDescription() != null && !subCategory.getDescription().equals(subCategoryBasedOnDefault.getDescription()) - && (oldSubcategory.getDescription() == null || oldSubcategory.getDescription().equals(subCategoryBasedOnDefault.getDescription()))) { - - subCategoryBasedOnDefault.setDescription(subCategory.getDescription()); - changed = true; - } - - if(!changed) { - continue; - } - subCategoryBasedOnDefault.setUpdateDate(subCategory.getUpdateDate()); - subCategoryDAO.save(subCategoryBasedOnDefault); - } - }*/ - @PreAuthorize("isAuthenticated()") @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/delete", method = RequestMethod.DELETE) public boolean deleteSubCategory(@PathVariable("stakeholderId") String stakeholderId, diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/dto/CategoryFull.java b/src/main/java/eu/dnetlib/uoamonitorservice/dto/CategoryFull.java index 3211e1d..72b82d8 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/dto/CategoryFull.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/dto/CategoryFull.java @@ -13,6 +13,6 @@ public class CategoryFull extends CategoryGeneric { public CategoryFull(CategoryGeneric category, List subCategories) { super(category); subCategories.removeIf(Objects::isNull); - this.setSubCategories(subCategories); + this.subCategories = subCategories; } } diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/dto/SectionFull.java b/src/main/java/eu/dnetlib/uoamonitorservice/dto/SectionFull.java index fc7f74b..fd6f7f6 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/dto/SectionFull.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/dto/SectionFull.java @@ -1,19 +1,18 @@ package eu.dnetlib.uoamonitorservice.dto; -import eu.dnetlib.uoamonitorservice.dao.IndicatorDAO; import eu.dnetlib.uoamonitorservice.entities.Indicator; import eu.dnetlib.uoamonitorservice.entities.Section; import eu.dnetlib.uoamonitorservice.generics.SectionGeneric; -import eu.dnetlib.uoamonitorservice.handlers.EntityNotFoundException; import java.util.List; -import java.util.stream.Collectors; +import java.util.Objects; public class SectionFull extends SectionGeneric { public SectionFull() {} public SectionFull(Section section, List indicators) { super(section); - this.setIndicators(indicators); + indicators.removeIf(Objects::isNull); + this.indicators = indicators; } } diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/dto/StakeholderFull.java b/src/main/java/eu/dnetlib/uoamonitorservice/dto/StakeholderFull.java index 10f84b4..3c14598 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/dto/StakeholderFull.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/dto/StakeholderFull.java @@ -14,6 +14,6 @@ public class StakeholderFull extends StakeholderGeneric { public StakeholderFull(StakeholderGeneric stakeholder, List topics) { super(stakeholder); topics.removeIf(Objects::isNull); - this.setTopics(topics); + this.topics = topics; } } diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/dto/SubCategoryFull.java b/src/main/java/eu/dnetlib/uoamonitorservice/dto/SubCategoryFull.java index ca16ffd..b326a2a 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/dto/SubCategoryFull.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/dto/SubCategoryFull.java @@ -12,7 +12,7 @@ public class SubCategoryFull extends SubCategoryGeneric { super(subCategory); numbers.removeIf(Objects::isNull); charts.removeIf(Objects::isNull); - this.setNumbers(numbers); - this.setCharts(charts); + this.numbers = numbers; + this.charts = charts; } } diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/dto/TopicFull.java b/src/main/java/eu/dnetlib/uoamonitorservice/dto/TopicFull.java index 99cf4c5..92a8982 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/dto/TopicFull.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/dto/TopicFull.java @@ -13,6 +13,6 @@ public class TopicFull extends TopicGeneric { public TopicFull(TopicGeneric topic, List categories) { super(topic); categories.removeIf(Objects::isNull); - this.setCategories(categories); + this.categories = categories; } } diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/entities/Category.java b/src/main/java/eu/dnetlib/uoamonitorservice/entities/Category.java index 21a3ec8..60dbc49 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/entities/Category.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/entities/Category.java @@ -16,20 +16,42 @@ public class Category extends CategoryGeneric { super(); } + public Category(Category category) { + super(category); + } + public Category(CategoryFull category) { super(category); this.subCategories = category.getSubCategories().stream().map(Common::getId).collect(Collectors.toList()); this.subCategories.removeIf(Objects::isNull); } - public void copyFromDefault(CategoryGeneric defaultCategory) { - setName(defaultCategory.getName()); - setAlias(defaultCategory.getAlias()); - setDescription(defaultCategory.getDescription()); - setVisibility(defaultCategory.getVisibility()); - setCreationDate(defaultCategory.getCreationDate()); - setUpdateDate(defaultCategory.getUpdateDate()); - setDefaultId(defaultCategory.getId()); - setSubCategories(new ArrayList<>()); + public Category copy() { + Category category = new Category(this); + category.setDefaultId(this.getId()); + category.setId(null); + return category; + } + + public Category override(Category category, Category old) { + return (Category) super.override(category, old); + } + + public SubCategory createOverview() { + SubCategory subCategory = new SubCategory(); + subCategory.setName("Overview"); + subCategory.setAlias("overview"); + subCategory.setVisibility(this.getVisibility()); + subCategory.setCharts(new ArrayList<>()); + subCategory.setNumbers(new ArrayList<>()); + return subCategory; + } + + public void addSubCategory(String id) { + this.subCategories.add(id); + } + + public void removeSubCategory(String id) { + this.subCategories.remove(id); } } diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/entities/Section.java b/src/main/java/eu/dnetlib/uoamonitorservice/entities/Section.java index 5f9e4de..ecd6497 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/entities/Section.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/entities/Section.java @@ -1,6 +1,7 @@ package eu.dnetlib.uoamonitorservice.entities; import eu.dnetlib.uoamonitorservice.dto.SectionFull; +import eu.dnetlib.uoamonitorservice.generics.Common; import eu.dnetlib.uoamonitorservice.generics.SectionGeneric; import org.springframework.data.mongodb.core.mapping.Document; @@ -12,9 +13,29 @@ import java.util.stream.Collectors; public class Section extends SectionGeneric { public Section() {} + public Section(Section section) { + super(section); + } + public Section(SectionFull section) { super(section); this.indicators = section.getIndicators().stream().map(Indicator::getId).collect(Collectors.toList()); this.indicators.removeIf(Objects::isNull); } + + public Section copy() { + Section section = new Section(this); + section.setDefaultId(this.getId()); + section.setId(null); + return section; + } + + public Section override(Section section, Section old) { + section = (Section) super.override(section, old); + if(this.getTitle() != null && !this.getTitle().equals(section.getTitle()) && + (old.getTitle() == null || old.getTitle().equals(section.getTitle()))) { + section.setTitle(this.getTitle()); + } + return section; + } } diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/entities/SubCategory.java b/src/main/java/eu/dnetlib/uoamonitorservice/entities/SubCategory.java index 1791cc2..42b83a7 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/entities/SubCategory.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/entities/SubCategory.java @@ -5,12 +5,19 @@ import eu.dnetlib.uoamonitorservice.generics.Common; import eu.dnetlib.uoamonitorservice.generics.SubCategoryGeneric; import org.springframework.data.mongodb.core.mapping.Document; +import java.util.ArrayList; import java.util.Objects; import java.util.stream.Collectors; @Document public class SubCategory extends SubCategoryGeneric { - public SubCategory() {} + public SubCategory() { + super(); + } + + public SubCategory(SubCategory subCategory) { + super(subCategory); + } public SubCategory(SubCategoryFull subCategory) { super(subCategory); @@ -19,4 +26,39 @@ public class SubCategory extends SubCategoryGeneric { this.numbers.removeIf(Objects::isNull); this.charts.removeIf(Objects::isNull); } + + public SubCategory copy() { + SubCategory subCategory = new SubCategory(this); + subCategory.setDefaultId(this.getId()); + subCategory.setId(null); + return subCategory; + } + + public SubCategory override(SubCategory subCategory, SubCategory old) { + return (SubCategory) super.override(subCategory, old); + } + + public void addNumber(String id, int index) { + if(index == -1) { + this.numbers.add(id); + } else { + this.numbers.add(index, id); + } + } + + public void removeNumber(String id) { + this.numbers.remove(id); + } + + public void addChart(String id, int index) { + if(index == -1) { + this.charts.add(id); + } else { + this.charts.add(index, id); + } + } + + public void removeChart(String id) { + this.charts.remove(id); + } } diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/entities/Topic.java b/src/main/java/eu/dnetlib/uoamonitorservice/entities/Topic.java index 19eef8d..dd15ff0 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/entities/Topic.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/entities/Topic.java @@ -47,4 +47,12 @@ public class Topic extends TopicGeneric { } return topic; } + + public void addCategory(String id) { + this.categories.add(id); + } + + public void removeCategory(String id) { + this.categories.remove(id); + } } diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/generics/CategoryGeneric.java b/src/main/java/eu/dnetlib/uoamonitorservice/generics/CategoryGeneric.java index f0e8f57..30166f7 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/generics/CategoryGeneric.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/generics/CategoryGeneric.java @@ -28,6 +28,8 @@ public class CategoryGeneric extends Common { creationDate = category.getCreationDate(); updateDate = category.getUpdateDate(); defaultId = category.getDefaultId(); + isOverview = category.getIsOverview(); + subCategories = new ArrayList<>(); } public boolean getIsOverview() { diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/generics/SectionGeneric.java b/src/main/java/eu/dnetlib/uoamonitorservice/generics/SectionGeneric.java index 48132b5..0914261 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/generics/SectionGeneric.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/generics/SectionGeneric.java @@ -22,6 +22,7 @@ public class SectionGeneric extends Common { setType(section.getType()); creationDate = section.getCreationDate(); updateDate = section.getUpdateDate(); + indicators = new ArrayList<>(); } public void copyFromDefault(SectionGeneric defaultSection) { @@ -64,6 +65,10 @@ public class SectionGeneric extends Common { } } + public boolean isNumber() { + return this.type == IndicatorType.NUMBER || this.type == IndicatorType.number; + } + public List getIndicators() { return indicators; } diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/generics/SubCategoryGeneric.java b/src/main/java/eu/dnetlib/uoamonitorservice/generics/SubCategoryGeneric.java index 5b45e6f..e1870fa 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/generics/SubCategoryGeneric.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/generics/SubCategoryGeneric.java @@ -1,14 +1,6 @@ package eu.dnetlib.uoamonitorservice.generics; -import com.fasterxml.jackson.annotation.JsonProperty; -import eu.dnetlib.uoamonitorservice.entities.Category; -import eu.dnetlib.uoamonitorservice.primitives.Visibility; -import org.springframework.data.annotation.CreatedDate; -import org.springframework.data.annotation.Id; -import org.springframework.data.annotation.LastModifiedDate; - import java.util.ArrayList; -import java.util.Date; import java.util.List; public class SubCategoryGeneric extends Common { @@ -25,28 +17,8 @@ public class SubCategoryGeneric extends Common { creationDate = subCategory.getCreationDate(); updateDate = subCategory.getUpdateDate(); defaultId = subCategory.getDefaultId(); - } - - public void createOverviewSubCategory(Category category) { - setName("Overview"); - setAlias("overview"); - setVisibility(category.getVisibility()); - setCreationDate(category.getCreationDate()); - setUpdateDate(category.getUpdateDate()); - setCharts(new ArrayList<>()); - setNumbers(new ArrayList<>()); - } - - public void copyFromDefault(SubCategoryGeneric defaultSubCategory) { - setName(defaultSubCategory.getName()); - setAlias(defaultSubCategory.getAlias()); - setDescription(defaultSubCategory.getDescription()); - setVisibility(defaultSubCategory.getVisibility()); - setCreationDate(defaultSubCategory.getCreationDate()); - setUpdateDate(defaultSubCategory.getUpdateDate()); - setDefaultId(defaultSubCategory.getId()); - setCharts(new ArrayList<>()); - setNumbers(new ArrayList<>()); + numbers = new ArrayList<>(); + charts = new ArrayList<>(); } public List getCharts() { diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/service/CategoryService.java b/src/main/java/eu/dnetlib/uoamonitorservice/service/CategoryService.java index 7e97767..4fab426 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/service/CategoryService.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/service/CategoryService.java @@ -2,9 +2,12 @@ package eu.dnetlib.uoamonitorservice.service; import eu.dnetlib.uoaadmintoolslibrary.handlers.ForbiddenException; import eu.dnetlib.uoamonitorservice.dao.CategoryDAO; +import eu.dnetlib.uoamonitorservice.dao.StakeholderDAO; import eu.dnetlib.uoamonitorservice.dao.TopicDAO; import eu.dnetlib.uoamonitorservice.dto.CategoryFull; +import eu.dnetlib.uoamonitorservice.dto.TopicFull; import eu.dnetlib.uoamonitorservice.entities.Category; +import eu.dnetlib.uoamonitorservice.entities.Stakeholder; import eu.dnetlib.uoamonitorservice.entities.Topic; import eu.dnetlib.uoamonitorservice.handlers.EntityNotFoundException; import eu.dnetlib.uoamonitorservice.handlers.PathNotValidException; @@ -17,6 +20,7 @@ import java.util.stream.Collectors; @Service public class CategoryService { + private final StakeholderDAO stakeholderDAO; private final TopicDAO topicDAO; private final CategoryDAO dao; @@ -24,13 +28,18 @@ public class CategoryService { private final CommonService commonService; @Autowired - public CategoryService(TopicDAO topicDAO, CategoryDAO dao, SubCategoryService subCategoryService, CommonService commonService) { + public CategoryService(StakeholderDAO stakeholderDAO, TopicDAO topicDAO, CategoryDAO dao, SubCategoryService subCategoryService, CommonService commonService) { + this.stakeholderDAO = stakeholderDAO; this.topicDAO = topicDAO; this.dao = dao; this.subCategoryService = subCategoryService; this.commonService = commonService; } + public Category find(String id) { + return dao.findById(id).orElseThrow(() -> new EntityNotFoundException("Category with id: " + id + " not found")); + } + public Category findByPath(Topic topic, String categoryId) { if (!topic.getCategories().contains(categoryId)) { throw new PathNotValidException("Category with id: " + categoryId + " not found in Topic: " + topic.getId()); @@ -53,9 +62,6 @@ public class CategoryService { return this.getFullCategory(type, alias, category); } - public Category find(String id) { - return dao.findById(id).orElseThrow(() -> new EntityNotFoundException("Category with id: " + id + " not found")); - } public CategoryFull buildCategory(CategoryFull categoryFull) { categoryFull.setSubCategories(categoryFull.getSubCategories().stream().map(this.subCategoryService::buildSubcategory).collect(Collectors.toList())); @@ -68,6 +74,48 @@ public class CategoryService { return this.dao.save(category); } + public CategoryFull save(Stakeholder stakeholder, Topic topic, Category category) { + return this.save(stakeholder, topic, category, true); + } + + public CategoryFull save(Stakeholder stakeholder, Topic topic, Category category, boolean createOverview) { + if (category.getId() != null) { + if (this.commonService.hasEditAuthority(stakeholder.getType(), stakeholder.getAlias())) { + category.setSubCategories(this.find(topic.getId()).getSubCategories()); + this.updateChildren(category); + category = this.save(category); + } else { + throw new ForbiddenException("You are not authorized to update stakeholder with id: " + stakeholder.getId()); + } + } else { + if (this.commonService.hasCreateAuthority(stakeholder.getType())) { + category = this.save(category); + this.createChildren(topic, category); + if(createOverview) { + this.subCategoryService.save(stakeholder, category, category.createOverview()); + } + this.addCategory(topic, category.getId()); + } else { + throw new ForbiddenException("You are not authorized to create a category in stakeholder with id: " + stakeholder.getId()); + } + } + return this.getFullCategory(stakeholder.getType(), stakeholder.getAlias(), category); + } + + public void createChildren(Topic defaultTopic, Category category) { + this.topicDAO.findByDefaultId(defaultTopic.getId()).forEach(topic -> { + this.stakeholderDAO.findByTopicsContaining(topic.getId()).forEach(stakeholder -> { + this.save(stakeholder, topic, category.copy(), false); + }); + }); + } + + public void updateChildren(Category category) { + this.dao.findByDefaultId(category.getId()).forEach(child -> { + this.save(category.override(child, this.find(category.getId()))); + }); + } + public void delete(String type, Category category, boolean remove) { if (this.commonService.hasDeleteAuthority(type)) { this.dao.findByDefaultId(category.getId()).forEach(child -> { @@ -90,9 +138,14 @@ public class CategoryService { this.delete(type, category, remove); } + public void addCategory(Topic topic, String id) { + topic.addCategory(id); + this.topicDAO.save(topic); + } + public void removeCategory(String id) { this.topicDAO.findByCategoriesContaining(id).forEach(topic -> { - topic.getCategories().remove(id); + topic.removeCategory(id); this.topicDAO.save(topic); }); } diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/service/SectionService.java b/src/main/java/eu/dnetlib/uoamonitorservice/service/SectionService.java index 696c54e..ec7a606 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/service/SectionService.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/service/SectionService.java @@ -1,13 +1,9 @@ package eu.dnetlib.uoamonitorservice.service; import eu.dnetlib.uoaadmintoolslibrary.handlers.ForbiddenException; -import eu.dnetlib.uoamonitorservice.dao.SectionDAO; -import eu.dnetlib.uoamonitorservice.dao.SubCategoryDAO; +import eu.dnetlib.uoamonitorservice.dao.*; import eu.dnetlib.uoamonitorservice.dto.SectionFull; -import eu.dnetlib.uoamonitorservice.entities.Category; -import eu.dnetlib.uoamonitorservice.entities.Indicator; -import eu.dnetlib.uoamonitorservice.entities.Section; -import eu.dnetlib.uoamonitorservice.entities.SubCategory; +import eu.dnetlib.uoamonitorservice.entities.*; import eu.dnetlib.uoamonitorservice.handlers.EntityNotFoundException; import eu.dnetlib.uoamonitorservice.handlers.PathNotValidException; import eu.dnetlib.uoamonitorservice.primitives.Visibility; @@ -20,6 +16,9 @@ import java.util.stream.Collectors; @Service public class SectionService { + private final StakeholderDAO stakeholderDAO; + private final TopicDAO topicDAO; + private final CategoryDAO categoryDAO; private final SubCategoryDAO subCategoryDAO; private final SectionDAO dao; @@ -27,13 +26,20 @@ public class SectionService { private final CommonService commonService; @Autowired - public SectionService(SubCategoryDAO subCategoryDAO, SectionDAO dao, IndicatorService indicatorService, CommonService commonService) { + public SectionService(StakeholderDAO stakeholderDAO, TopicDAO topicDAO, CategoryDAO categoryDAO,SubCategoryDAO subCategoryDAO, SectionDAO dao, IndicatorService indicatorService, CommonService commonService) { + this.stakeholderDAO = stakeholderDAO; + this.topicDAO = topicDAO; + this.categoryDAO = categoryDAO; this.subCategoryDAO = subCategoryDAO; this.dao = dao; this.indicatorService = indicatorService; this.commonService = commonService; } + public Section find(String id) { + return dao.findById(id).orElseThrow(() -> new EntityNotFoundException("Section with id: " + id + " not found")); + } + public Section findByPath(SubCategory subCategory, String sectionId) { if (!subCategory.getNumbers().contains(sectionId) && !subCategory.getCharts().contains(sectionId)) { throw new PathNotValidException("Section with id: " + sectionId + " not found in SubCategory: " + subCategory.getId()); @@ -41,24 +47,15 @@ public class SectionService { return this.dao.findById(sectionId).orElseThrow(() -> new EntityNotFoundException("Section with id: " + sectionId + " not found")); } - public Section find(String id) { - return dao.findById(id).orElseThrow(() -> new EntityNotFoundException("Section with id: " + id + " not found")); - } - - public SectionFull getFullSection(String type, String alias, String id) { - Section section = this.find(id); + public SectionFull getFullSection(String type, String alias, Section section) { return new SectionFull(section, section.getIndicators().stream() .map(indicatorId -> this.indicatorService.getIndicator(type, alias, indicatorId)) .collect(Collectors.toList())); } - public List getIndicators(String id) { + public SectionFull getFullSection(String type, String alias, String id) { Section section = this.find(id); - return section.getIndicators().stream().map(this.indicatorService::find).collect(Collectors.toList()); - } - - public List getIndicatorsId(String id) { - return this.getIndicators(id).stream().map(Indicator::getId).collect(Collectors.toList()); + return this.getFullSection(type, alias, section); } public SectionFull buildSection(SectionFull sectionFull) { @@ -72,6 +69,46 @@ public class SectionService { return this.dao.save(section); } + public SectionFull save(Stakeholder stakeholder, SubCategory subCategory, Section section, int index) { + section.setStakeholderAlias(stakeholder.getAlias()); + if (section.getId() != null) { + if (this.commonService.hasEditAuthority(stakeholder.getType(), stakeholder.getAlias())) { + section.setIndicators(this.find(section.getId()).getIndicators()); + this.updateChildren(section); + section = this.save(section); + } else { + throw new ForbiddenException("You are not authorized to update stakeholder with id: " + stakeholder.getId()); + } + } else { + if (this.commonService.hasCreateAuthority(stakeholder.getType())) { + section = this.save(section); + this.createChildren(subCategory, section, index); + this.addSection(subCategory, section.getId(), index); + } else { + throw new ForbiddenException("You are not authorized to create a section in stakeholder with id: " + stakeholder.getId()); + } + } + return this.getFullSection(stakeholder.getType(), stakeholder.getAlias(), section); + } + + public void createChildren(SubCategory defaultSubCategory, Section section, int index) { + this.subCategoryDAO.findByDefaultId(defaultSubCategory.getId()).forEach(subCategory -> { + this.categoryDAO.findBySubCategoriesContaining(subCategory.getId()).forEach(category -> { + this.topicDAO.findByCategoriesContaining(category.getId()).forEach(topic -> { + this.stakeholderDAO.findByTopicsContaining(topic.getId()).forEach(stakeholder -> { + this.save(stakeholder, subCategory, section.copy(), index); + }); + }); + }); + }); + } + + public void updateChildren(Section section) { + this.dao.findByDefaultId(section.getId()).forEach(child -> { + this.save(section.override(child, this.find(section.getId()))); + }); + } + public void delete(String type, Section section, boolean remove) { if(this.commonService.hasDeleteAuthority(type)) { this.dao.findByDefaultId(section.getId()).forEach(child -> { @@ -94,13 +131,22 @@ public class SectionService { this.delete(type, section, remove); } + public void addSection(SubCategory subCategory, String id, int index) { + if(this.find(id).isNumber()) { + subCategory.addNumber(id, index); + } else { + subCategory.addChart(id, index); + } + this.subCategoryDAO.save(subCategory); + } + public void removeSection(String id) { this.subCategoryDAO.findByNumbersContaining(id).forEach(subCategory -> { - subCategory.getNumbers().remove(id); + subCategory.removeNumber(id); this.subCategoryDAO.save(subCategory); }); this.subCategoryDAO.findByChartsContaining(id).forEach(subCategory -> { - subCategory.getCharts().remove(id); + subCategory.removeChart(id); this.subCategoryDAO.save(subCategory); }); } diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/service/StakeholderService.java b/src/main/java/eu/dnetlib/uoamonitorservice/service/StakeholderService.java index 56259b0..0e57409 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/service/StakeholderService.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/service/StakeholderService.java @@ -78,8 +78,7 @@ public class StakeholderService { } public StakeholderFull getFullStakeholder(Stakeholder stakeholder) { - if(this.commonService.hasAccessAuthority(stakeholder.getType(), stakeholder.getAlias(), stakeholder.getDefaultId() == null) || - (stakeholder.getDefaultId() != null && this.commonService.hasVisibilityAuthority(stakeholder.getType(), stakeholder.getAlias(), stakeholder))) { + if(this.commonService.hasVisibilityAuthority(stakeholder.getType(), stakeholder.getAlias(), stakeholder)) { return new StakeholderFull(stakeholder, stakeholder.getTopics().stream() .map(topicId -> topicService.getFullTopic(stakeholder.getType(), stakeholder.getAlias(), topicId)) diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/service/SubCategoryService.java b/src/main/java/eu/dnetlib/uoamonitorservice/service/SubCategoryService.java index f0da766..597bbae 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/service/SubCategoryService.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/service/SubCategoryService.java @@ -2,11 +2,14 @@ package eu.dnetlib.uoamonitorservice.service; import eu.dnetlib.uoaadmintoolslibrary.handlers.ForbiddenException; import eu.dnetlib.uoamonitorservice.dao.CategoryDAO; +import eu.dnetlib.uoamonitorservice.dao.StakeholderDAO; import eu.dnetlib.uoamonitorservice.dao.SubCategoryDAO; +import eu.dnetlib.uoamonitorservice.dao.TopicDAO; import eu.dnetlib.uoamonitorservice.dto.CategoryFull; import eu.dnetlib.uoamonitorservice.dto.SubCategoryFull; import eu.dnetlib.uoamonitorservice.dto.TopicFull; 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.generics.Common; @@ -24,6 +27,8 @@ import java.util.stream.Collectors; @Service public class SubCategoryService { + private final StakeholderDAO stakeholderDAO; + private final TopicDAO topicDAO; private final CategoryDAO categoryDAO; private final SubCategoryDAO dao; @@ -31,13 +36,19 @@ public class SubCategoryService { private final SectionService sectionService; @Autowired - public SubCategoryService(CategoryDAO categoryDAO, SubCategoryDAO dao,CommonService commonService, SectionService sectionService) { + public SubCategoryService(StakeholderDAO stakeholderDAO, TopicDAO topicDAO, CategoryDAO categoryDAO, SubCategoryDAO dao,CommonService commonService, SectionService sectionService) { + this.stakeholderDAO = stakeholderDAO; + this.topicDAO = topicDAO; this.categoryDAO = categoryDAO; this.dao = dao; this.commonService = commonService; this.sectionService = sectionService; } + public SubCategory find(String id) { + return dao.findById(id).orElseThrow(() -> new EntityNotFoundException("SubCategory with id: " + id + " not found")); + } + public SubCategory findByPath(Category category, String subcategoryId) { if (!category.getSubCategories().contains(subcategoryId)) { throw new PathNotValidException("SubCategory with id: " + subcategoryId + " not found in Category: " + category.getId()); @@ -63,10 +74,6 @@ public class SubCategoryService { return this.getFullSubCategory(type, alias, subCategory); } - public SubCategory find(String id) { - return dao.findById(id).orElseThrow(() -> new EntityNotFoundException("SubCategory with id: " + id + " not found")); - } - public SubCategoryFull buildSubcategory(SubCategoryFull subCategoryFull) { subCategoryFull.setNumbers(subCategoryFull.getNumbers().stream().map(this.sectionService::buildSection).collect(Collectors.toList())); subCategoryFull.setCharts(subCategoryFull.getCharts().stream().map(this.sectionService::buildSection).collect(Collectors.toList())); @@ -80,7 +87,46 @@ public class SubCategoryService { return this.dao.save(subCategory); } - public void delete(String type, SubCategory subCategory, boolean remove ) { + public SubCategoryFull save(Stakeholder stakeholder, Category category, SubCategory subCategory) { + if (subCategory.getId() != null) { + if (this.commonService.hasEditAuthority(stakeholder.getType(), stakeholder.getAlias())) { + SubCategory old = this.find(subCategory.getId()); + subCategory.setNumbers(old.getNumbers()); + subCategory.setCharts(old.getCharts()); + this.updateChildren(subCategory); + subCategory = this.save(subCategory); + } else { + throw new ForbiddenException("You are not authorized to update stakeholder with id: " + stakeholder.getId()); + } + } else { + if (this.commonService.hasCreateAuthority(stakeholder.getType())) { + subCategory = this.save(subCategory); + this.createChildren(category, subCategory); + this.addSubCategory(category, subCategory.getId()); + } else { + throw new ForbiddenException("You are not authorized to create a subCategory in stakeholder with id: " + stakeholder.getId()); + } + } + return this.getFullSubCategory(stakeholder.getType(), stakeholder.getAlias(), subCategory); + } + + public void createChildren(Category defaultCategory, SubCategory subCategory) { + this.categoryDAO.findByDefaultId(defaultCategory.getId()).forEach(category -> { + this.topicDAO.findByCategoriesContaining(category.getId()).forEach(topic -> { + this.stakeholderDAO.findByTopicsContaining(topic.getId()).forEach(stakeholder -> { + this.save(stakeholder, category, subCategory.copy()); + }); + }); + }); + } + + public void updateChildren(SubCategory subCategory) { + this.dao.findByDefaultId(subCategory.getId()).forEach(child -> { + this.save(subCategory.override(child, this.find(subCategory.getId()))); + }); + } + + public void delete(String type, SubCategory subCategory, boolean remove) { if(this.commonService.hasDeleteAuthority(type)) { this.dao.findByDefaultId(subCategory.getId()).forEach(child -> { this.delete(type, child.getId(), remove); @@ -106,9 +152,14 @@ public class SubCategoryService { this.delete(type, subCategory, remove); } + public void addSubCategory(Category category, String id) { + category.addSubCategory(id); + this.categoryDAO.save(category); + } + public void removeSubCategory(String id) { this.categoryDAO.findBySubCategoriesContaining(id).forEach(category -> { - category.getSubCategories().remove(id); + category.removeSubCategory(id); this.categoryDAO.save(category); }); } diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/service/TopicService.java b/src/main/java/eu/dnetlib/uoamonitorservice/service/TopicService.java index 93b46f5..eb46604 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/service/TopicService.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/service/TopicService.java @@ -32,6 +32,10 @@ public class TopicService { this.commonService = commonService; } + public Topic find(String id) { + return dao.findById(id).orElseThrow(() -> new EntityNotFoundException("Topic with id: " + id + " not found")); + } + public Topic findByPath(Stakeholder stakeholder, String topicId) { if (!stakeholder.getTopics().contains(topicId)) { throw new PathNotValidException("Topic with id: " + topicId + " not found in Stakeholder: " + stakeholder.getId()); @@ -54,19 +58,21 @@ public class TopicService { return this.getFullTopic(type, alias, topic); } - public Topic find(String id) { - return dao.findById(id).orElseThrow(() -> new EntityNotFoundException("Topic with id: " + id + " not found")); - } - public TopicFull buildTopic(TopicFull topicFull) { topicFull.setCategories(topicFull.getCategories().stream().map(this.categoryService::buildCategory).collect(Collectors.toList())); topicFull.update(this.save(new Topic(topicFull))); return topicFull; } + public Topic save(Topic topic) { + topic.getCategories().forEach(this.categoryService::find); + return this.dao.save(topic); + } + public TopicFull save(Stakeholder stakeholder, Topic topic) { if(topic.getId() != null) { if(this.commonService.hasEditAuthority(stakeholder.getType(), stakeholder.getAlias())) { + topic.setCategories(this.find(topic.getId()).getCategories()); this.updateChildren(topic); topic = this.save(topic); } else { @@ -74,8 +80,8 @@ public class TopicService { } } else { if(this.commonService.hasCreateAuthority(stakeholder.getType())) { - this.createChildren(stakeholder, topic); topic = this.save(topic); + this.createChildren(stakeholder, topic); this.addTopic(stakeholder, topic.getId()); } else { throw new ForbiddenException("You are not authorized to create a topic in stakeholder with id: " + stakeholder.getId()); @@ -96,11 +102,6 @@ public class TopicService { }); } - public Topic save(Topic topic) { - topic.getCategories().forEach(this.categoryService::find); - return this.dao.save(topic); - } - public void delete(String type, Topic topic, boolean remove) { if (this.commonService.hasDeleteAuthority(type)) { this.dao.findByDefaultId(topic.getId()).forEach(child -> { From c83dc441d443b15a63136572a70e090534ff97b3 Mon Sep 17 00:00:00 2001 From: "k.triantafyllou" Date: Tue, 2 Apr 2024 14:49:02 +0300 Subject: [PATCH 09/11] Refactor reorder methods and save sections in bulk --- .../controllers/CategoryController.java | 149 +----- .../controllers/IndicatorController.java | 466 ++---------------- .../controllers/SectionController.java | 81 ++- .../controllers/StakeholderController.java | 8 +- .../controllers/SubCategoryController.java | 51 +- .../controllers/TopicController.java | 53 +- .../uoamonitorservice/entities/Indicator.java | 54 +- .../uoamonitorservice/entities/Section.java | 9 + .../entities/Stakeholder.java | 2 + .../generics/SectionGeneric.java | 9 - .../service/CategoryService.java | 19 + .../service/IndicatorService.java | 78 ++- .../service/SectionService.java | 47 +- .../service/StakeholderService.java | 22 +- .../service/SubCategoryService.java | 42 +- .../service/TopicService.java | 18 + 16 files changed, 363 insertions(+), 745 deletions(-) diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/CategoryController.java b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/CategoryController.java index ce406b4..8f53d88 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/CategoryController.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/CategoryController.java @@ -14,6 +14,8 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.web.bind.annotation.*; +import java.util.List; + @RestController @CrossOrigin(origins = "*") public class CategoryController { @@ -40,117 +42,8 @@ public class CategoryController { Stakeholder stakeholder = stakeholderService.findByPath(stakeholderId); Topic topic = topicService.findByPath(stakeholder, topicId); return this.categoryService.save(stakeholder, topic, new Category(categoryFull)); - /*if (stakeholder != null) { - if (!rolesUtils.hasUpdateAuthority(stakeholder.getType(), stakeholder.getAlias())) { - // EXCEPTION - Access denied - throw new ForbiddenException("Save Category: You are not authorized to update stakeholder with id: " + stakeholderId); - } - - Category oldCategory = null; - if (categoryFull.getId() != null) { - oldCategory = categoryDAO.findById(categoryFull.getId()); - if (oldCategory == null) { - // EXCEPTION - Category not found - throw new EntityNotFoundException("save category: Category with id: " + categoryFull.getId() + " not found"); - } - } - - Topic topic = topicDAO.findById(topicId); - if (topic != null) { - if (stakeholder.getTopics().contains(topicId)) { - Category category = new Category<>(categoryFull); - - Date date = new Date(); - category.setUpdateDate(date); - categoryFull.setUpdateDate(date); - - List subCategories = new ArrayList<>(); - - // if category not exists (no id), create a new default subcategory, identical to category - if (categoryFull.getId() == null) { - category.setCreationDate(date); - categoryFull.setCreationDate(date); - - SubCategory subCategory = new SubCategory<>(); - subCategory.createOverviewSubCategory(categoryFull); - - subCategoryDAO.save(subCategory); - - List subCategoriesFull = categoryFull.getSubCategories(); - subCategoriesFull.add(subCategory); - - for (SubCategory oldSubCategory : subCategoriesFull) { - subCategories.add(oldSubCategory.getId()); - } - } else { - for (String subCategoryId : oldCategory.getSubCategories()) { - SubCategory subCategory = subCategoryDAO.findById(subCategoryId); - if (subCategory == null) { - // EXCEPTION - SubCategory not found - throw new EntityNotFoundException("Save category: SubCategory with id: " + subCategoryId + " not found (subcategory exists in category: " + category.getId() + ")"); - } - subCategories.add(subCategory.getId()); - } - } - - category.setSubCategories(subCategories); - - if (stakeholder.getDefaultId() == null) { - if (categoryFull.getId() == null) { - categoryDAO.save(category); - onSaveDefaultCategory(category, topicId); - } else { - onUpdateDefaultCategory(category, oldCategory); - categoryDAO.save(category); - } - } else { - categoryDAO.save(category); - } - - List categories = topic.getCategories(); - int index = categories.indexOf(category.getId()); - if (index == -1) { - categories.add(category.getId()); - topicDAO.save(topic); - log.debug("Category saved!"); - - categoryFull.setId(category.getId()); - } - } 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;*/ } - /* public void onSaveDefaultCategory(Category category, String topicId) { - log.debug("On save default category"); - - List topics = topicDAO.findByDefaultId(topicId); - for (Topic topic : topics) { - Category categoryNew = new Category(); - categoryNew.copyFromDefault(category); - - categoryDAO.save(categoryNew); - - List categories = topic.getCategories(); - categories.add(categoryNew.getId()); - - topicDAO.save(topic); - } - String subCategoryOverviewId = category.getSubCategories().get(0); - SubCategory subCategoryOverview = subCategoryDAO.findById(subCategoryOverviewId); - subCategoryController.onSaveDefaultSubCategory(subCategoryOverview, category.getId()); - }*/ - @PreAuthorize("isAuthenticated()") @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/delete", method = RequestMethod.DELETE) @@ -160,46 +53,24 @@ public class CategoryController { @RequestParam(required = false) String children) { log.debug("delete category"); log.debug("Id: " + categoryId + " - Stakeholder: " + stakeholderId + " - Topic: " + topicId); - Stakeholder stakeholder = this.stakeholderService.findById(stakeholderId); + Stakeholder stakeholder = this.stakeholderService.find(stakeholderId); Topic topic = this.topicService.findByPath(stakeholder, topicId); Category category = this.categoryService.findByPath(topic, categoryId); this.categoryService.delete(stakeholder.getType(), category, true); return true; } - /*@PreAuthorize("isAuthenticated()") + @PreAuthorize("isAuthenticated()") @RequestMapping(value = "/{stakeholderId}/{topicId}/reorder", method = RequestMethod.POST) - public List reorderCategories(@PathVariable("stakeholderId") String stakeholderId, + public List reorderCategories(@PathVariable("stakeholderId") String stakeholderId, @PathVariable("topicId") String topicId, @RequestBody List categories) { log.debug("reorder categories"); log.debug("Stakeholder: " + stakeholderId + " - Topic: " + topicId); - - Topic topic = checkForExceptions(stakeholderId, topicId); - - List oldCategories = topic.getCategories(); - for (String categoryId : oldCategories) { - if (!categories.contains(categoryId)) { - categories.add(categoryId); - } - } - topic.setCategories(categories); - - List categoriesFull = new ArrayList<>(); - for (String categoryId : categories) { - Category category = categoryDAO.findById(categoryId); - if (category == null) { - // EXCEPTION - Category not found - throw new EntityNotFoundException("Reorder Categories: Category with id: " + categoryId + " not found"); - } - categoriesFull.add(category); - } - - topicDAO.save(topic); - log.debug("Categories reordered!"); - - return categoriesFull; - }*/ + Stakeholder stakeholder = stakeholderService.findByPath(stakeholderId); + Topic topic = topicService.findByPath(stakeholder, topicId); + return this.topicService.reorderCategories(stakeholder, topic, categories).getCategories(); + } @PreAuthorize("isAuthenticated()") @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/change-visibility", method = RequestMethod.POST) @@ -209,7 +80,7 @@ public class CategoryController { @RequestParam("visibility") Visibility visibility, @RequestParam(defaultValue = "false") Boolean propagate) { log.debug("change category visibility: " + visibility + " - toggle propagate: " + propagate); log.debug("Stakeholder: " + stakeholderId + " - Topic: " + topicId + " - Category: " + categoryId); - Stakeholder stakeholder = this.stakeholderService.findById(stakeholderId); + Stakeholder stakeholder = this.stakeholderService.find(stakeholderId); Topic topic = this.topicService.findByPath(stakeholder, topicId); Category category = this.categoryService.findByPath(topic, categoryId); return this.categoryService.changeVisibility(stakeholder.getType(), stakeholder.getAlias(), category, visibility, propagate); diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/IndicatorController.java b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/IndicatorController.java index 6d513cc..32f79f7 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/IndicatorController.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/IndicatorController.java @@ -1,7 +1,10 @@ package eu.dnetlib.uoamonitorservice.controllers; +import eu.dnetlib.uoamonitorservice.dto.SectionFull; +import eu.dnetlib.uoamonitorservice.dto.StakeholderFull; import eu.dnetlib.uoamonitorservice.entities.*; +import eu.dnetlib.uoamonitorservice.primitives.ReorderEvent; import eu.dnetlib.uoamonitorservice.primitives.Visibility; import eu.dnetlib.uoamonitorservice.service.*; import org.apache.logging.log4j.LogManager; @@ -10,6 +13,10 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.web.bind.annotation.*; +import java.io.UnsupportedEncodingException; +import java.util.ArrayList; +import java.util.List; + @RestController @CrossOrigin(origins = "*") public class IndicatorController { @@ -33,417 +40,41 @@ public class IndicatorController { this.indicatorService = indicatorService; } - /*@PreAuthorize("isAuthenticated()") + @PreAuthorize("isAuthenticated()") @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/save-bulk", method = RequestMethod.POST) - public Stakeholder saveBulkIndicators(@PathVariable("stakeholderId") String stakeholderId, - @PathVariable("topicId") String topicId, - @PathVariable("categoryId") String categoryId, - @PathVariable("subcategoryId") String subcategoryId, - @RequestBody List> sections) throws UnsupportedEncodingException { + public StakeholderFull saveBulkIndicators(@PathVariable("stakeholderId") String stakeholderId, + @PathVariable("topicId") String topicId, + @PathVariable("categoryId") String categoryId, + @PathVariable("subcategoryId") String subcategoryId, + @RequestBody List sections) throws UnsupportedEncodingException { log.debug("save bulk indicators"); log.debug("Stakeholder: " + stakeholderId + " - Topic: " + topicId + " - Category: " + categoryId + " - SubCategory: " + subcategoryId); - - Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId); - - Date date = new Date(); - - createSectionsAndSaveBulk(date, sections, stakeholder, topicId, categoryId, subcategoryId); - return stakeholderController.setFullEntities(stakeholder); + Stakeholder stakeholder = this.stakeholderService.findByPath(stakeholderId); + Topic topic = this.topicService.findByPath(stakeholder, topicId); + Category category = this.categoryService.findByPath(topic, categoryId); + SubCategory subCategory = this.subCategoryService.findByPath(category, subcategoryId); + this.sectionService.saveBulk(stakeholder, subCategory, sections); + return this.stakeholderService.getFullStakeholder(stakeholder); } - private void createSectionsAndSaveBulk(Date date, List> old_sections, - Stakeholder stakeholder, String topicId, String categoryId, String subcategoryId) throws UnsupportedEncodingException { - for (Section section : old_sections) { - if (section == null) { - continue; - } - - Section chart_section = null; - Section number_section = null; - - List chart_indicators = null; - List number_indicators = null; - - if (section.getType().equals("chart")) { - chart_section = createSection(chart_section, "chart", section.getTitle(), date, stakeholder, topicId, categoryId, subcategoryId); - chart_indicators = chart_section.getIndicators(); - } else if (section.getType().equals("number")) { - number_section = createSection(number_section, "number", section.getTitle(), date, stakeholder, topicId, categoryId, subcategoryId); - number_indicators = number_section.getIndicators(); - } - - List indicators = section.getIndicators(); - for (Indicator indicator : indicators) { - if (indicator == null) { - continue; - } - - if (indicator.getType().equals("chart")) { - saveIndicatorAndAddInSection(indicator, date, stakeholder, subcategoryId, chart_section, chart_indicators); - - } else if (indicator.getType().equals("number")) { - saveIndicatorAndAddInSection(indicator, date, stakeholder, subcategoryId, number_section, number_indicators); - } - } - - if (chart_section != null) { - sectionDAO.save(chart_section); - } - if (number_section != null) { - sectionDAO.save(number_section); - } - } - } - - private Section createSection(Section section, String type, String title, Date date, - Stakeholder stakeholder, String topicId, String categoryId, String subcategoryId) { - section = new Section<>(); - section.setType(type); - section.setTitle(title); - section.setStakeholderAlias(stakeholder.getAlias()); - section.setUpdateDate(date); - section.setCreationDate(date); - section.setIndicators(new ArrayList<>()); - section = sectionController.saveSection(stakeholder.getId(), topicId, categoryId, subcategoryId, "-1", section); - - return section; - } - - private void saveIndicatorAndAddInSection(Indicator indicator, Date date, Stakeholder stakeholder, String subcategoryId, Section section, List indicators) throws UnsupportedEncodingException { - // indicator does not exist in DB - indicator.setCreationDate(date); - indicator.setUpdateDate(date); - - if (stakeholder.getDefaultId() == null) { // this indicator belongs in default profile and it is new - indicatorDAO.save(indicator); - onSaveDefaultIndicator(indicator, section, subcategoryId); - } else { // this indicator belongs in a stakeholder's profile and it is new - indicatorDAO.save(indicator); - } - - indicators.add(indicator.getId()); - log.debug("Indicator saved!"); - } -*/ - /* @PreAuthorize("isAuthenticated()") + @PreAuthorize("isAuthenticated()") @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/{sectionId}/save", method = RequestMethod.POST) public Indicator saveIndicator(@PathVariable("stakeholderId") String stakeholderId, @PathVariable("topicId") String topicId, @PathVariable("categoryId") String categoryId, @PathVariable("subcategoryId") String subcategoryId, @PathVariable("sectionId") String sectionId, - @RequestBody Indicator indicator) throws UnsupportedEncodingException { + @RequestBody Indicator indicator) { log.debug("save indicator"); log.debug("Name: " + indicator.getName() + " - Id: " + indicator.getId() + " - Stakeholder: " + stakeholderId + " - Topic: " + topicId + " - Category: " + categoryId + " - SubCategory: " + subcategoryId + " - Section: " + sectionId); - - Section section = checkForExceptions(stakeholderId, topicId, categoryId, subcategoryId, sectionId, indicator.getType()); - - Date date = new Date(); - indicator.setUpdateDate(date); - - Indicator oldIndicator = null; - if (indicator.getId() != null) { - oldIndicator = indicatorDAO.findById(indicator.getId()); - if (oldIndicator == null) { - // EXCEPTION - Indicator not found - throw new EntityNotFoundException("save indicator: Indicator with id: " + indicator.getId() + " not found"); - } - } else { // indicator does not exist in DB - indicator.setCreationDate(date); - } - - String indicatorId = indicator.getId(); - - Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId); - // this indicator belongs in default profile and it is new or it is updated - if (stakeholder.getDefaultId() == null) { - if (indicatorId == null) { - indicatorDAO.save(indicator); - onSaveDefaultIndicator(indicator, section, subcategoryId); - } else { - onUpdateDefaultIndicator(indicator, stakeholder, oldIndicator); - indicatorDAO.save(indicator); - } - } else { - indicatorDAO.save(indicator); - } - - List indicators = section.getIndicators(); - - int index = indicators.indexOf(indicator.getId()); - if (index == -1) { - indicators.add(indicator.getId()); - sectionDAO.save(section); - log.debug("Indicator saved!"); - } - - return indicator; + Stakeholder stakeholder = this.stakeholderService.findByPath(stakeholderId); + Topic topic = this.topicService.findByPath(stakeholder, topicId); + Category category = this.categoryService.findByPath(topic, categoryId); + SubCategory subCategory = this.subCategoryService.findByPath(category, subcategoryId); + Section section = this.sectionService.findByPath(subCategory, sectionId); + return this.indicatorService.save(stakeholder, section, indicator); } - public void onSaveDefaultIndicator(Indicator indicator, Section defaultSection, String defaultSubcategoryId) throws UnsupportedEncodingException { - log.debug("On save default indicator"); - - // new indicator in default profile - add it on profiles of the same type - List subCategories = subCategoryDAO.findByDefaultId(defaultSubcategoryId); - for (SubCategory subCategory : subCategories) { - - List sections = null; - if (defaultSection.getType().equals("chart")) { - sections = subCategory.getCharts(); - } else { - sections = subCategory.getNumbers(); - } - - for (String sectionId : sections) { - Section section = sectionDAO.findById(sectionId); - if (section.getDefaultId() != null && section.getDefaultId().equals(defaultSection.getId())) { - Indicator indicatorNew = new Indicator(); - indicatorNew.copyFromDefault(indicator, subCategory.getVisibility()); - indicatorDAO.save(indicatorNew); - List indicators = section.getIndicators(); - indicators.add(indicatorNew.getId()); - sectionDAO.save(section); - } - } - } - } - - public void onUpdateDefaultIndicator(Indicator indicator, Stakeholder stakeholder, Indicator oldIndicator) throws UnsupportedEncodingException { - log.debug("On update default indicator"); - - // indicator already exists - check if changed and update all indicators based on it - - boolean changed; - List indicators = indicatorDAO.findByDefaultId(indicator.getId()); - - for (Indicator indicatorBasedOnDefault : indicators) { - changed = false; - if (( - (indicator.getName() == null && oldIndicator.getName() != null) - || - (indicator.getName() != null && !indicator.getName().equals(indicatorBasedOnDefault.getName())) - ) && ( - (oldIndicator.getName() == null && indicatorBasedOnDefault.getName() == null) - || - (oldIndicator.getName() != null && oldIndicator.getName().equals(indicatorBasedOnDefault.getName())) - )) { - indicatorBasedOnDefault.setName(indicator.getName()); - changed = true; - } - - if (indicator.getDescription() != null && !indicator.getDescription().equals(indicatorBasedOnDefault.getDescription()) - || indicator.getDescription() == null && indicatorBasedOnDefault.getDescription() != null) { - - indicatorBasedOnDefault.setDescription(indicator.getDescription()); - changed = true; - } - if (( - (indicator.getAdditionalDescription() == null && oldIndicator.getAdditionalDescription() != null) - || - (indicator.getAdditionalDescription() != null && !indicator.getAdditionalDescription().equals(indicatorBasedOnDefault.getAdditionalDescription())) - ) && ( - (oldIndicator.getAdditionalDescription() == null && indicatorBasedOnDefault.getAdditionalDescription() == null) - || - (oldIndicator.getAdditionalDescription() != null && oldIndicator.getAdditionalDescription().equals(indicatorBasedOnDefault.getAdditionalDescription())) - )) { - indicatorBasedOnDefault.setAdditionalDescription(indicator.getAdditionalDescription()); - changed = true; - } - - int i = 0; - List indicatorPaths = indicatorBasedOnDefault.getIndicatorPaths(); - if (indicatorPaths == null && indicator.getIndicatorPaths() != null) { - indicatorPaths = new ArrayList<>(); - } - - for (IndicatorPath indicatorPath : indicator.getIndicatorPaths()) { - IndicatorPath indicatorPathBasedOnDefault = null; - if (i < indicatorPaths.size()) { - indicatorPathBasedOnDefault = indicatorPaths.get(i); - } - - if (indicatorPathBasedOnDefault == null) { - // Add new indicator path in existing indicators - IndicatorPath indicatorPathNew = new IndicatorPath(indicatorPath); - indicatorPaths.add(indicatorPathNew); - changed = true; - } else { - IndicatorPath oldIndicatorPath = oldIndicator.getIndicatorPaths().get(i); - - // Check if there are changes in indicator path and update existing indicators if needed - log.debug("update indicator path: " + i + " (indicator id: " + indicatorBasedOnDefault.getId() + ")"); - if (( - (indicatorPath.getType() == null && oldIndicatorPath.getType() != null) - || - (indicatorPath.getType() != null && !indicatorPath.getType().equals(indicatorPathBasedOnDefault.getType())) - ) && ( - (oldIndicatorPath.getType() == null && indicatorPathBasedOnDefault.getType() == null) - || - (oldIndicatorPath.getType() != null && oldIndicatorPath.getType().equals(indicatorPathBasedOnDefault.getType())) - )) { - indicatorPathBasedOnDefault.setType(indicatorPath.getType()); - changed = true; // parameter "type" needs to be changed as well - } - log.debug("After type check: " + changed); - - if (( - (indicatorPath.getFormat() == null && oldIndicatorPath.getFormat() != null) - || - (indicatorPath.getFormat() != null && !indicatorPath.getFormat().equals(indicatorPathBasedOnDefault.getFormat())) - ) && ( - (oldIndicatorPath.getFormat() == null && indicatorPathBasedOnDefault.getFormat() == null) - || - (oldIndicatorPath.getFormat() != null && oldIndicatorPath.getFormat().equals(indicatorPathBasedOnDefault.getFormat())) - )) { - indicatorPathBasedOnDefault.setFormat(indicatorPath.getFormat()); - changed = true; - } - log.debug("After type check: " + changed); - if (( - (indicatorPath.getSource() == null && oldIndicatorPath.getSource() != null) - || - (indicatorPath.getSource() != null && !indicatorPath.getSource().equals(indicatorPathBasedOnDefault.getSource())) - ) && ( - (oldIndicatorPath.getSource() == null && indicatorPathBasedOnDefault.getSource() == null) - || - (oldIndicatorPath.getSource() != null && oldIndicatorPath.getSource().equals(indicatorPathBasedOnDefault.getSource())) - )) { - indicatorPathBasedOnDefault.setSource(indicatorPath.getSource()); - changed = true; - } - log.debug("After source check: " + changed); - if (( - (indicatorPath.getUrl() == null && oldIndicatorPath.getUrl() != null) - || - (indicatorPath.getUrl() != null && !indicatorPath.getUrl().equals(indicatorPathBasedOnDefault.getUrl())) - ) && ( - (oldIndicatorPath.getUrl() == null && indicatorPathBasedOnDefault.getUrl() == null) - || - (oldIndicatorPath.getUrl() != null && oldIndicatorPath.getUrl().equals(indicatorPathBasedOnDefault.getUrl())) - )) { - indicatorPathBasedOnDefault.setUrl(indicatorPath.getUrl()); - changed = true; - } - log.debug("After url check: " + changed); - - if (( - (indicatorPath.getChartObject() == null && oldIndicatorPath.getChartObject() != null) - || - (indicatorPath.getChartObject() != null && !indicatorPath.getChartObject().equals(indicatorPathBasedOnDefault.getChartObject())) - ) && ( - (oldIndicatorPath.getChartObject() == null && indicatorPathBasedOnDefault.getChartObject() == null) - || - (oldIndicatorPath.getChartObject() != null && oldIndicatorPath.getChartObject().equals(indicatorPathBasedOnDefault.getChartObject())) - )) { - - indicatorPathBasedOnDefault.setChartObject(indicatorPath.getChartObject()); - changed = true; - } - log.debug("After chartObject check: " + changed); - - if (indicatorPath.getParameters() != null) { - if (indicatorPathBasedOnDefault.getParameters() == null) { - indicatorPathBasedOnDefault.setParameters(new HashMap<>()); - } - for (Map.Entry parameter : indicatorPath.getParameters().entrySet()) { - log.debug("\nindicatorPath: parameter.getKey(): " + parameter.getKey() + " - value: " + parameter.getValue() - + "\nindicatorPathBasedOnDefault:parameters:key: " + indicatorPathBasedOnDefault.getParameters().get(parameter.getKey()) - + "\noldIndicatorPath:parameters:key: " + (oldIndicatorPath.getParameters() == null ? "null" : oldIndicatorPath.getParameters().get(parameter.getKey()))); - if (!indicatorPathBasedOnDefault.getParameters().containsKey(parameter.getKey()) - || (oldIndicatorPath.getParameters() == null || oldIndicatorPath.getParameters().get(parameter.getKey()) == null - || (oldIndicatorPath.getParameters().get(parameter.getKey()).equals(indicatorPathBasedOnDefault.getParameters().get(parameter.getKey())) - && !parameter.getValue().equals(indicatorPathBasedOnDefault.getParameters().get(parameter.getKey())))) - ) { - indicatorPathBasedOnDefault.getParameters().put(parameter.getKey(), parameter.getValue()); - changed = true; - } - } - - // When deleting indicator path parameters in a default profile, delete them also from all children profiles - if (oldIndicatorPath.getParameters() != null && indicatorPath.getParameters().size() < oldIndicatorPath.getParameters().size()) { - for (Map.Entry parameter : oldIndicatorPath.getParameters().entrySet()) { - if (!indicatorPath.getParameters().containsKey(parameter.getKey())) { - indicatorPathBasedOnDefault.getParameters().remove(parameter.getKey()); - } - } - } - } - log.debug("After parameters check: " + changed); - - if (indicatorPath.getJsonPath() != null) { - boolean jsonPathChanged = false; - boolean breaked = false; - - int oldJsonPathSize = 0; - if (oldIndicatorPath.getJsonPath() != null) { - oldJsonPathSize = oldIndicatorPath.getJsonPath().size(); - } - int basedOnDefaultJsonPathSize = 0; - if (indicatorPathBasedOnDefault.getJsonPath() != null) { - basedOnDefaultJsonPathSize = indicatorPathBasedOnDefault.getJsonPath().size(); - } - log.debug("old: " + oldJsonPathSize + " - based on default: " + basedOnDefaultJsonPathSize + " - new: " + indicatorPath.getJsonPath().size()); - if (oldJsonPathSize == basedOnDefaultJsonPathSize) { - if (indicatorPathBasedOnDefault.getJsonPath() == null && indicatorPath.getJsonPath().size() > 0) { - indicatorPathBasedOnDefault.setJsonPath(new ArrayList<>()); - } - - int basedOnDefaultIndex = 0; - int oldIndex = 0; - - Iterator jsonStringBasedOnDefaultIterator = indicatorPathBasedOnDefault.getJsonPath().iterator(); - while (jsonStringBasedOnDefaultIterator.hasNext()) { - String jsonStringBasedOnDefault = jsonStringBasedOnDefaultIterator.next(); - if (oldIndicatorPath.getJsonPath().get(oldIndex).equals(jsonStringBasedOnDefault)) { - if (basedOnDefaultIndex >= indicatorPath.getJsonPath().size()) { // string deleted - jsonStringBasedOnDefaultIterator.remove(); - jsonPathChanged = true; - } else { // check if string changed - if (!indicatorPath.getJsonPath().get(basedOnDefaultIndex).equals(jsonStringBasedOnDefault)) { - indicatorPathBasedOnDefault.getJsonPath().set(basedOnDefaultIndex, indicatorPath.getJsonPath().get(basedOnDefaultIndex)); - jsonPathChanged = true; - } - basedOnDefaultIndex++; - } - oldIndex++; - } else { - breaked = true; - jsonPathChanged = false; - log.debug("not the same: " + oldIndex); - break; - } - } - - int index = 0; - if (!breaked && indicatorPath.getJsonPath().size() > indicatorPathBasedOnDefault.getJsonPath().size()) { // strings added - jsonPathChanged = true; - for (index = indicatorPathBasedOnDefault.getJsonPath().size(); index < indicatorPath.getJsonPath().size(); index++) { - indicatorPathBasedOnDefault.getJsonPath().add(indicatorPath.getJsonPath().get(index)); - } - } - - if (jsonPathChanged) { - changed = true; - } - } - // TODO when deleting indicator path json path strings... --> is this done? (line 327) - } - log.debug("After jsonPath check: " + changed); - } - i++; - } - // TODO when deleting indicator paths... - - if (!changed) { -// break; - continue; - } - - indicatorBasedOnDefault.setUpdateDate(indicator.getUpdateDate()); - indicatorDAO.save(indicatorBasedOnDefault); - } - } -*/ @PreAuthorize("isAuthenticated()") @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/{sectionId}/{indicatorId}/delete", method = RequestMethod.DELETE) public boolean deleteIndicator(@PathVariable("stakeholderId") String stakeholderId, @@ -455,7 +86,7 @@ public class IndicatorController { @RequestParam(required = false) String children) { log.debug("delete indicator"); log.debug("Id: " + indicatorId + " - Stakeholder: " + stakeholderId + " - Topic: " + topicId + " - Category: " + categoryId + " - SubCategory: " + subcategoryId + " - Section: " + sectionId); - Stakeholder stakeholder = this.stakeholderService.findById(stakeholderId); + Stakeholder stakeholder = this.stakeholderService.find(stakeholderId); Topic topic = this.topicService.findByPath(stakeholder, topicId); Category category = this.categoryService.findByPath(topic, categoryId); SubCategory subCategory = this.subCategoryService.findByPath(category, subcategoryId); @@ -466,7 +97,7 @@ public class IndicatorController { } - /*@PreAuthorize("isAuthenticated()") + @PreAuthorize("isAuthenticated()") @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/{sectionId}/{type}/reorder", method = RequestMethod.POST) public List reorderIndicators(@PathVariable("stakeholderId") String stakeholderId, @PathVariable("topicId") String topicId, @@ -477,36 +108,13 @@ public class IndicatorController { @RequestBody ReorderEvent reorderEvent) { log.debug("reorder indicators of type: " + type); log.debug("Stakeholder: " + stakeholderId + " - Topic: " + topicId + " - Category: " + categoryId + " - SubCategory: " + subcategoryId + " - Section: " + sectionId); - - List indicators = reorderEvent.getIds(); - String actionType = reorderEvent.getAction(); - String targetId = reorderEvent.getTarget(); - - Section section = checkForExceptions(stakeholderId, topicId, categoryId, subcategoryId, sectionId, type); - - List oldIndicators = section.getIndicators(); - for (String indicatorId : oldIndicators) { - if ((!actionType.equals("removed") || !targetId.equals(indicatorId)) && !indicators.contains(indicatorId)) { - indicators.add(indicatorId); - } - } - section.setIndicators(indicators); - - List indicatorsFull = new ArrayList<>(); - for (String indicatorId : indicators) { - Indicator indicator = indicatorDAO.findById(indicatorId); - if (indicator == null) { - // EXCEPTION - Indicator not found - throw new EntityNotFoundException("Reorder indicators: Indicator with id: " + indicatorId + " not found"); - } - indicatorsFull.add(indicator); - } - - sectionDAO.save(section); - log.debug("Indicators reordered!"); - - return indicatorsFull; - }*/ + Stakeholder stakeholder = this.stakeholderService.find(stakeholderId); + Topic topic = this.topicService.findByPath(stakeholder, topicId); + Category category = this.categoryService.findByPath(topic, categoryId); + SubCategory subCategory = this.subCategoryService.findByPath(category, subcategoryId); + Section section = this.sectionService.findByPath(subCategory, sectionId); + return this.sectionService.reorderIndicators(stakeholder, section, reorderEvent).getIndicators(); + } @PreAuthorize("isAuthenticated()") @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/{sectionId}/{indicatorId}/change-visibility", method = RequestMethod.POST) @@ -519,7 +127,7 @@ public class IndicatorController { @RequestParam("visibility") Visibility visibility) { log.debug("change indicator visibility: " + visibility); log.debug("Stakeholder: " + stakeholderId + " - Topic: " + topicId + " - Category: " + categoryId + " - SubCategory: " + subcategoryId + " - Section: " + sectionId + " - Indicator: " + indicatorId); - Stakeholder stakeholder = this.stakeholderService.findById(stakeholderId); + Stakeholder stakeholder = this.stakeholderService.find(stakeholderId); Topic topic = this.topicService.findByPath(stakeholder, topicId); Category category = this.categoryService.findByPath(topic, categoryId); SubCategory subCategory = this.subCategoryService.findByPath(category, subcategoryId); diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/SectionController.java b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/SectionController.java index f93d6da..f917627 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/SectionController.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/SectionController.java @@ -2,6 +2,7 @@ package eu.dnetlib.uoamonitorservice.controllers; import eu.dnetlib.uoamonitorservice.dto.SectionFull; import eu.dnetlib.uoamonitorservice.entities.*; +import eu.dnetlib.uoamonitorservice.handlers.PathNotValidException; import eu.dnetlib.uoamonitorservice.service.*; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -9,6 +10,8 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.web.bind.annotation.*; +import java.util.List; + @RestController @CrossOrigin(origins = "*") public class SectionController { @@ -33,13 +36,13 @@ public class SectionController { @PreAuthorize("isAuthenticated()") @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/save/{index}", method = RequestMethod.POST) public SectionFull saveSection(@PathVariable("stakeholderId") String stakeholderId, - @PathVariable("topicId") String topicId, - @PathVariable("categoryId") String categoryId, - @PathVariable("subcategoryId") String subcategoryId, - @PathVariable("index") String index, - @RequestBody SectionFull sectionFull) { + @PathVariable("topicId") String topicId, + @PathVariable("categoryId") String categoryId, + @PathVariable("subcategoryId") String subcategoryId, + @PathVariable("index") String index, + @RequestBody SectionFull sectionFull) { log.debug("save section"); - log.debug("Name: "+sectionFull.getTitle() + " - Id: "+sectionFull.getId() + " - Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId+ " - SubCategory: "+subcategoryId); + log.debug("Name: " + sectionFull.getTitle() + " - Id: " + sectionFull.getId() + " - Stakeholder: " + stakeholderId + " - Topic: " + topicId + " - Category: " + categoryId + " - SubCategory: " + subcategoryId); Stakeholder stakeholder = this.stakeholderService.findByPath(stakeholderId); Topic topic = this.topicService.findByPath(stakeholder, topicId); Category category = this.categoryService.findByPath(topic, categoryId); @@ -57,7 +60,7 @@ public class SectionController { @RequestParam(required = false) String children) { log.debug("delete section"); log.debug("Id: " + sectionId + " - Stakeholder: " + stakeholderId + " - Topic: " + topicId + " - Category: " + categoryId + " - SubCategory: " + subcategoryId); - Stakeholder stakeholder = this.stakeholderService.findById(stakeholderId); + Stakeholder stakeholder = this.stakeholderService.find(stakeholderId); Topic topic = this.topicService.findByPath(stakeholder, topicId); Category category = this.categoryService.findByPath(topic, categoryId); SubCategory subCategory = this.subCategoryService.findByPath(category, subcategoryId); @@ -66,50 +69,26 @@ public class SectionController { return true; } - /*@PreAuthorize("isAuthenticated()") + @PreAuthorize("isAuthenticated()") @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/{type}/reorder", method = RequestMethod.POST) - public List
reorderSections(@PathVariable("stakeholderId") String stakeholderId, - @PathVariable("topicId") String topicId, - @PathVariable("categoryId") String categoryId, - @PathVariable("subcategoryId") String subcategoryId, - @PathVariable("type") String type, - @RequestBody List sections) { - log.debug("reorder sections of type: "+type); - log.debug("Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId+ " - SubCategory: "+subcategoryId); - - SubCategory subCategory = checkForExceptions(stakeholderId, topicId, categoryId, subcategoryId); - - if (type.equals("chart")) { - List oldSections = subCategory.getCharts(); - for (String sectionId : oldSections) { - if (!sections.contains(sectionId)) { - sections.add(sectionId); - } - } - subCategory.setCharts(sections); - } else if (type.equals("number")) { - List oldSections = subCategory.getNumbers(); - for (String sectionId : oldSections) { - if (!sections.contains(sectionId)) { - sections.add(sectionId); - } - } - subCategory.setNumbers(sections); + public List reorderSections(@PathVariable("stakeholderId") String stakeholderId, + @PathVariable("topicId") String topicId, + @PathVariable("categoryId") String categoryId, + @PathVariable("subcategoryId") String subcategoryId, + @PathVariable("type") String type, + @RequestBody List sections) { + log.debug("reorder sections of type: " + type); + log.debug("Stakeholder: " + stakeholderId + " - Topic: " + topicId + " - Category: " + categoryId + " - SubCategory: " + subcategoryId); + Stakeholder stakeholder = this.stakeholderService.findByPath(stakeholderId); + Topic topic = this.topicService.findByPath(stakeholder, topicId); + Category category = this.categoryService.findByPath(topic, categoryId); + SubCategory subCategory = this.subCategoryService.findByPath(category, subcategoryId); + if (type.equals("number")) { + return this.subCategoryService.reorderNumbers(stakeholder, subCategory, sections).getNumbers(); + } else if (type.equals("chart")) { + return this.subCategoryService.reorderCharts(stakeholder, subCategory, sections).getCharts(); + } else { + throw new PathNotValidException("Type is not valid"); } - - List
sectionsFull = new ArrayList<>(); - for(String sectionId : sections) { - Section section = sectionDAO.findById(sectionId); - if(section == null) { - // EXCEPTION - Section not found - throw new EntityNotFoundException("Reorder sections: Section with id: " + sectionId + " not found"); - } - sectionsFull.add(section); - } - - subCategoryDAO.save(subCategory); - log.debug("Sections reordered!"); - - return sectionsFull; - }*/ + } } diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/StakeholderController.java b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/StakeholderController.java index 7df0dfb..2b98882 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/StakeholderController.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/StakeholderController.java @@ -47,10 +47,9 @@ public class StakeholderController { "@AuthorizationService.PORTAL_ADMIN, " + "@AuthorizationService.curator(#stakeholder.getType()))") @RequestMapping(value = "/build-stakeholder", method = RequestMethod.POST) - public StakeholderFull buildFullStakeholder(@RequestBody StakeholderFull stakeholder) { + public StakeholderFull buildStakeholder(@RequestBody StakeholderFull stakeholder) { log.debug("build stakeholder"); log.debug("Alias: " + stakeholder.getAlias()); - stakeholder = this.stakeholderService.buildStakeholder(stakeholder); Portal portal = portalService.getPortal(stakeholder.getAlias()); if (portal == null) { portal = new Portal(); @@ -59,7 +58,7 @@ public class StakeholderController { portal.setType(stakeholder.getType()); portalService.insertPortal(portal); } - return stakeholder; + return this.stakeholderService.buildStakeholder(stakeholder); } @PreAuthorize("hasAnyAuthority(" + "@AuthorizationService.PORTAL_ADMIN)") @@ -106,8 +105,7 @@ public class StakeholderController { public StakeholderFull saveStakeholder(@RequestBody StakeholderFull stakeholder) { log.debug("save stakeholder"); log.debug("Alias: " + stakeholder.getAlias() + " - Id: " + stakeholder.getId()); - stakeholder.update(this.stakeholderService.save(new Stakeholder(stakeholder))); - return stakeholder; + return this.stakeholderService.getFullStakeholder(this.stakeholderService.save(new Stakeholder(stakeholder))); } @PreAuthorize("isAuthenticated()") diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/SubCategoryController.java b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/SubCategoryController.java index 32dee17..01aa2fd 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/SubCategoryController.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/SubCategoryController.java @@ -1,8 +1,6 @@ package eu.dnetlib.uoamonitorservice.controllers; -import eu.dnetlib.uoamonitorservice.dto.CategoryFull; import eu.dnetlib.uoamonitorservice.dto.SubCategoryFull; -import eu.dnetlib.uoamonitorservice.dto.TopicFull; import eu.dnetlib.uoamonitorservice.entities.Category; import eu.dnetlib.uoamonitorservice.entities.Stakeholder; import eu.dnetlib.uoamonitorservice.entities.SubCategory; @@ -18,6 +16,8 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.web.bind.annotation.*; +import java.util.List; + @RestController @CrossOrigin(origins = "*") public class SubCategoryController { @@ -59,7 +59,7 @@ public class SubCategoryController { @RequestParam(required = false) String children) { log.debug("delete subcategory"); log.debug("Id: " + subcategoryId + " - Stakeholder: " + stakeholderId + " - Topic: " + topicId + " - Category: " + categoryId); - Stakeholder stakeholder = this.stakeholderService.findById(stakeholderId); + Stakeholder stakeholder = this.stakeholderService.find(stakeholderId); Topic topic = this.topicService.findByPath(stakeholder, topicId); Category category = this.categoryService.findByPath(topic, categoryId); SubCategory subCategory = this.subCategoryService.findByPath(category, subcategoryId); @@ -67,40 +67,19 @@ public class SubCategoryController { return true; } - /*@PreAuthorize("isAuthenticated()") + @PreAuthorize("isAuthenticated()") @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/reorder", method = RequestMethod.POST) - public List reorderSubCategories(@PathVariable("stakeholderId") String stakeholderId, - @PathVariable("topicId") String topicId, - @PathVariable("categoryId") String categoryId, - @RequestBody List subCategories) { + public List reorderSubCategories(@PathVariable("stakeholderId") String stakeholderId, + @PathVariable("topicId") String topicId, + @PathVariable("categoryId") String categoryId, + @RequestBody List subCategories) { log.debug("reorder subCategories"); - log.debug("Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId); - - Category category = checkForExceptions(stakeholderId, topicId, categoryId); - - List oldSubcategories = category.getSubCategories(); - for (String subcategoryId : oldSubcategories) { - if (!subCategories.contains(subcategoryId)) { - subCategories.add(subcategoryId); - } - } - category.setSubCategories(subCategories); - - List subCategoriesFull = new ArrayList<>(); - for(String subCategoryId : subCategories) { - SubCategory subCategory = subCategoryDAO.findById(subCategoryId); - if(subCategory == null) { - // EXCEPTION - SubCategory not found - throw new EntityNotFoundException("Reorder subCategories: subCategory with id: " + subCategoryId + " not found"); - } - subCategoriesFull.add(subCategory); - } - - categoryDAO.save(category); - log.debug("SubCategories reordered!"); - - return subCategoriesFull; - }*/ + log.debug("Stakeholder: " + stakeholderId + " - Topic: " + topicId + " - Category: " + categoryId); + Stakeholder stakeholder = this.stakeholderService.findByPath(stakeholderId); + Topic topic = this.topicService.findByPath(stakeholder, topicId); + Category category = this.categoryService.findByPath(topic, categoryId); + return this.categoryService.reorderSubCategories(stakeholder, category, subCategories).getSubCategories(); + } @PreAuthorize("isAuthenticated()") @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/change-visibility", method = RequestMethod.POST) @@ -112,7 +91,7 @@ public class SubCategoryController { @RequestParam(defaultValue = "false") Boolean propagate) { log.debug("change subCategory visibility: " + visibility + " - toggle propagate: " + propagate); log.debug("Stakeholder: " + stakeholderId + " - Topic: " + topicId + " - Category: " + categoryId + " - SubCategory: " + subcategoryId); - Stakeholder stakeholder = this.stakeholderService.findById(stakeholderId); + Stakeholder stakeholder = this.stakeholderService.find(stakeholderId); Topic topic = this.topicService.findByPath(stakeholder, topicId); Category category = this.categoryService.findByPath(topic, categoryId); SubCategory subCategory = this.subCategoryService.findByPath(category, subcategoryId); diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/TopicController.java b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/TopicController.java index 1c9ff6a..40bac34 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/TopicController.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/TopicController.java @@ -12,6 +12,8 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.web.bind.annotation.*; +import java.util.List; + @RestController @CrossOrigin(origins = "*") public class TopicController { @@ -42,55 +44,22 @@ public class TopicController { @RequestParam(required = false) String children) { log.debug("delete topic"); log.debug("Id: " + topicId + " - Stakeholder: " + stakeholderId); - Stakeholder stakeholder = stakeholderService.findById(stakeholderId); + Stakeholder stakeholder = stakeholderService.find(stakeholderId); Topic topic = this.topicService.findByPath(stakeholder, topicId); this.topicService.delete(stakeholder.getType(), topic, true); return true; } - /*@PreAuthorize("isAuthenticated()") + @PreAuthorize("isAuthenticated()") @RequestMapping(value = "/{stakeholderId}/reorder", method = RequestMethod.POST) - public List reorderTopics(@PathVariable("stakeholderId") String stakeholderId, - @RequestBody List topics) { + public List reorderTopics(@PathVariable("stakeholderId") String stakeholderId, + @RequestBody List topics) { log.debug("reorder topics"); - log.debug("Stakeholder: "+stakeholderId); - - Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId); - - if(stakeholder != null) { - if(!rolesUtils.hasUpdateAuthority(stakeholder.getType(), stakeholder.getAlias())) { - // EXCEPTION - Access denied - throw new ForbiddenException("Reorder topics: You are not authorized to update stakeholder with id: "+stakeholderId); - } - - List oldTopics = stakeholder.getTopics(); - for (String topicId : oldTopics) { - if (!topics.contains(topicId)) { - topics.add(topicId); - } - } - stakeholder.setTopics(topics); - - List topicsFull = new ArrayList<>(); - for (String topicId : topics) { - Topic topic = topicDAO.findById(topicId); - if(topic == null) { - // EXCEPTION - Topic not found - throw new EntityNotFoundException("Reorder Topics: Topic with id: " + topicId + " not found"); - } - topicsFull.add(topic); - } - - stakeholderDAO.save(stakeholder); - log.debug("Topics reordered!"); - - return topicsFull; - } else { - // EXCEPTION - Stakeholder not found - throw new EntityNotFoundException("Reorder topics: Stakeholder with id: "+stakeholderId+" not found"); - } - }*/ + log.debug("Stakeholder: " + stakeholderId); + Stakeholder stakeholder = stakeholderService.findByPath(stakeholderId); + return this.stakeholderService.reorderTopics(stakeholder, topics).getTopics(); + } @PreAuthorize("isAuthenticated()") @RequestMapping(value = "/{stakeholderId}/{topicId}/change-visibility", method = RequestMethod.POST) @@ -99,7 +68,7 @@ public class TopicController { @RequestParam("visibility") Visibility visibility, @RequestParam(defaultValue = "false") Boolean propagate) { log.debug("change topic visibility: " + visibility + " - toggle propagate: " + propagate); log.debug("Stakeholder: " + stakeholderId + " - Topic: " + topicId); - Stakeholder stakeholder = this.stakeholderService.findById(stakeholderId); + Stakeholder stakeholder = this.stakeholderService.find(stakeholderId); Topic topic = this.topicService.findByPath(stakeholder, topicId); return this.topicService.changeVisibility(stakeholder.getType(), stakeholder.getAlias(), topic, visibility, propagate); } diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/entities/Indicator.java b/src/main/java/eu/dnetlib/uoamonitorservice/entities/Indicator.java index a0ae38a..ea231c3 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/entities/Indicator.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/entities/Indicator.java @@ -21,19 +21,47 @@ public class Indicator extends Common { private List tags; // this field is not used anywhere now private List indicatorPaths; - public void copyFromDefault(Indicator defaultIndicator, Visibility visibility) { - setName(defaultIndicator.getName()); - setDescription(defaultIndicator.getDescription()); - setAdditionalDescription(defaultIndicator.getAdditionalDescription()); - setType(defaultIndicator.getType()); - setWidth(defaultIndicator.getWidth()); - setHeight(defaultIndicator.getHeight()); - setTags(defaultIndicator.getTags()); - setVisibility(visibility); - setCreationDate(defaultIndicator.getCreationDate()); - setUpdateDate(defaultIndicator.getUpdateDate()); - setDefaultId(defaultIndicator.getId()); - setIndicatorPaths(defaultIndicator.getIndicatorPaths()); + public Indicator() {} + + public Indicator(Indicator indicator) { + id = indicator.getId(); + defaultId = indicator.getDefaultId(); + name = indicator.getName(); + description = indicator.getDescription(); + additionalDescription = indicator.getAdditionalDescription(); + creationDate = indicator.getCreationDate(); + updateDate = indicator.getUpdateDate(); + visibility = indicator.getVisibility(); + indicatorPaths = indicator.getIndicatorPaths(); + tags = indicator.getTags(); + setType(indicator.getType()); + width = indicator.getWidth(); + height = indicator.getHeight(); + } + + public Indicator copy() { + Indicator indicator = new Indicator(this); + indicator.setDefaultId(this.getId()); + indicator.setId(null); + return indicator; + } + + public Indicator override(Indicator indicator, Indicator old) { + indicator = (Indicator) super.override(indicator, old); + if(this.getAdditionalDescription() != null && !this.getAdditionalDescription().equals(indicator.getAdditionalDescription()) && + (old.getAdditionalDescription() == null || old.getAdditionalDescription().equals(indicator.getAdditionalDescription()))) { + indicator.setAdditionalDescription(this.getAdditionalDescription()); + } + if(this.getWidth() != null && !this.getWidth().equals(indicator.getWidth()) && + (old.getWidth() == null || old.getWidth().equals(indicator.getWidth()))) { + indicator.setWidth(this.getWidth()); + } + if(this.getHeight() != null && !this.getHeight().equals(indicator.getHeight()) && + (old.getHeight() == null || old.getHeight().equals(indicator.getHeight()))) { + indicator.setHeight(this.getHeight()); + } + indicator.setIndicatorPaths(this.getIndicatorPaths()); + return indicator; } public String getAdditionalDescription() { diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/entities/Section.java b/src/main/java/eu/dnetlib/uoamonitorservice/entities/Section.java index ecd6497..494e430 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/entities/Section.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/entities/Section.java @@ -5,6 +5,7 @@ import eu.dnetlib.uoamonitorservice.generics.Common; import eu.dnetlib.uoamonitorservice.generics.SectionGeneric; import org.springframework.data.mongodb.core.mapping.Document; +import java.util.ArrayList; import java.util.List; import java.util.Objects; import java.util.stream.Collectors; @@ -38,4 +39,12 @@ public class Section extends SectionGeneric { } return section; } + + public void addIndicator(String id) { + this.indicators.add(id); + } + + public void removeIndicator(String id) { + this.indicators.remove(id); + } } diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/entities/Stakeholder.java b/src/main/java/eu/dnetlib/uoamonitorservice/entities/Stakeholder.java index 319b1c2..545eac8 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/entities/Stakeholder.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/entities/Stakeholder.java @@ -5,6 +5,8 @@ import eu.dnetlib.uoamonitorservice.generics.Common; import eu.dnetlib.uoamonitorservice.generics.StakeholderGeneric; import org.springframework.data.mongodb.core.mapping.Document; +import java.util.HashSet; +import java.util.List; import java.util.Objects; import java.util.stream.Collectors; diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/generics/SectionGeneric.java b/src/main/java/eu/dnetlib/uoamonitorservice/generics/SectionGeneric.java index 0914261..ce0cd09 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/generics/SectionGeneric.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/generics/SectionGeneric.java @@ -25,15 +25,6 @@ public class SectionGeneric extends Common { indicators = new ArrayList<>(); } - public void copyFromDefault(SectionGeneric defaultSection) { - setTitle(defaultSection.getTitle()); - setType(defaultSection.getType()); - setDefaultId(defaultSection.id); - setCreationDate(defaultSection.getCreationDate()); - setUpdateDate(defaultSection.getUpdateDate()); - setIndicators(new ArrayList<>()); - } - public String getTitle() { return title; } diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/service/CategoryService.java b/src/main/java/eu/dnetlib/uoamonitorservice/service/CategoryService.java index 4fab426..f1e9505 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/service/CategoryService.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/service/CategoryService.java @@ -15,6 +15,8 @@ import eu.dnetlib.uoamonitorservice.primitives.Visibility; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import java.util.HashSet; +import java.util.List; import java.util.stream.Collectors; @Service @@ -70,6 +72,9 @@ public class CategoryService { } public Category save(Category category) { + if(category.getId() != null) { + category.setSubCategories(this.find(category.getId()).getSubCategories()); + } category.getSubCategories().forEach(this.subCategoryService::find); return this.dao.save(category); } @@ -116,6 +121,20 @@ public class CategoryService { }); } + public CategoryFull reorderSubCategories(Stakeholder stakeholder, Category category, List subcategories) { + if(this.commonService.hasEditAuthority(stakeholder.getType(), stakeholder.getAlias())) { + subcategories.forEach(this.subCategoryService::find); + if (category.getSubCategories().size() == subcategories.size() && new HashSet<>(category.getSubCategories()).containsAll(subcategories)) { + category.setSubCategories(subcategories); + return this.getFullCategory(stakeholder.getType(), stakeholder.getAlias(), this.dao.save(category)); + } else { + throw new EntityNotFoundException("Some subCategories dont exist in the category with id " + category.getId()); + } + } else { + throw new ForbiddenException("You are not authorized to reorder subCategories in category with id: " + category.getId()); + } + } + public void delete(String type, Category category, boolean remove) { if (this.commonService.hasDeleteAuthority(type)) { this.dao.findByDefaultId(category.getId()).forEach(child -> { diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/service/IndicatorService.java b/src/main/java/eu/dnetlib/uoamonitorservice/service/IndicatorService.java index 7030aa1..46cc742 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/service/IndicatorService.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/service/IndicatorService.java @@ -1,11 +1,11 @@ package eu.dnetlib.uoamonitorservice.service; import eu.dnetlib.uoaadmintoolslibrary.handlers.ForbiddenException; -import eu.dnetlib.uoamonitorservice.dao.IndicatorDAO; -import eu.dnetlib.uoamonitorservice.dao.SectionDAO; +import eu.dnetlib.uoamonitorservice.dao.*; import eu.dnetlib.uoamonitorservice.dto.SectionFull; import eu.dnetlib.uoamonitorservice.entities.Indicator; import eu.dnetlib.uoamonitorservice.entities.Section; +import eu.dnetlib.uoamonitorservice.entities.Stakeholder; import eu.dnetlib.uoamonitorservice.entities.SubCategory; import eu.dnetlib.uoamonitorservice.handlers.EntityNotFoundException; import eu.dnetlib.uoamonitorservice.handlers.PathNotValidException; @@ -13,25 +13,34 @@ import eu.dnetlib.uoamonitorservice.primitives.Visibility; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import java.util.List; import java.util.stream.Collectors; @Service public class IndicatorService { - + private final StakeholderDAO stakeholderDAO; + private final TopicDAO topicDAO; + private final CategoryDAO categoryDAO; + private final SubCategoryDAO subCategoryDAO; private final SectionDAO sectionDAO; private final IndicatorDAO dao; private final CommonService commonService; @Autowired - public IndicatorService(SectionDAO sectionDAO, IndicatorDAO dao, CommonService commonService) { + public IndicatorService(StakeholderDAO stakeholderDAO, TopicDAO topicDAO, CategoryDAO categoryDAO,SubCategoryDAO subCategoryDAO, + SectionDAO sectionDAO, IndicatorDAO dao, CommonService commonService) { + this.stakeholderDAO = stakeholderDAO; + this.topicDAO = topicDAO; + this.categoryDAO = categoryDAO; + this.subCategoryDAO = subCategoryDAO; this.sectionDAO = sectionDAO; this.dao = dao; this.commonService = commonService; } - public Indicator save(Indicator indicator) { - return this.dao.save(indicator); + public Indicator find(String id) { + return dao.findById(id).orElseThrow(() -> new EntityNotFoundException("Indicator with id: " + id + " not found")); } public Indicator findByPath(Section section, String indicatorId) { @@ -41,10 +50,6 @@ public class IndicatorService { return this.dao.findById(indicatorId).orElseThrow(() -> new EntityNotFoundException("Indicator with id: " + indicatorId + " not found")); } - public Indicator find(String id) { - return dao.findById(id).orElseThrow(() -> new EntityNotFoundException("Indicator with id: " + id + " not found")); - } - public Indicator getIndicator(String type, String alias, String id) { Indicator indicator = this.find(id); if(this.commonService.hasVisibilityAuthority(type, alias, indicator)) { @@ -54,6 +59,52 @@ public class IndicatorService { } } + public Indicator save(Indicator indicator) { + return this.dao.save(indicator); + } + + public Indicator save(Stakeholder stakeholder, Section section, Indicator indicator) { + if(indicator.getId() != null) { + if (this.commonService.hasEditAuthority(stakeholder.getType(), stakeholder.getAlias())) { + this.updateChildren(indicator); + indicator = this.save(indicator); + } else { + throw new ForbiddenException("You are not authorized to update stakeholder with id: " + stakeholder.getId()); + } + } else { + if (this.commonService.hasCreateAuthority(stakeholder.getType())) { + indicator = this.save(indicator); + this.createChildren(section, indicator); + this.addIndicator(section, indicator.getId()); + } else { + throw new ForbiddenException("You are not authorized to create an indicator in stakeholder with id: " + stakeholder.getId()); + } + } + return indicator; + } + + public void createChildren(Section defaultSection, Indicator indicator) { + this.sectionDAO.findByDefaultId(defaultSection.getId()).forEach(section -> { + List subCategories = this.subCategoryDAO.findByNumbersContaining(section.getId()); + subCategories.addAll(this.subCategoryDAO.findByChartsContaining(section.getId())); + subCategories.forEach(subCategory -> { + this.categoryDAO.findBySubCategoriesContaining(subCategory.getId()).forEach(category -> { + this.topicDAO.findByCategoriesContaining(category.getId()).forEach(topic -> { + this.stakeholderDAO.findByTopicsContaining(topic.getId()).forEach(stakeholder -> { + this.save(stakeholder, section, indicator.copy()); + }); + }); + }); + }); + }); + } + + public void updateChildren(Indicator indicator) { + this.dao.findByDefaultId(indicator.getId()).forEach(child -> { + this.save(indicator.override(child, this.find(indicator.getId()))); + }); + } + public void delete(String type, Indicator indicator, boolean remove) { if(this.commonService.hasDeleteAuthority(type)) { this.dao.findByDefaultId(indicator.getId()).forEach(child -> { @@ -74,9 +125,14 @@ public class IndicatorService { this.delete(type, indicator, remove); } + public void addIndicator(Section section, String id) { + section.addIndicator(id); + this.sectionDAO.save(section); + } + public void removeIndicator(String id) { this.sectionDAO.findByIndicatorsContaining(id).forEach(section -> { - section.getIndicators().remove(id); + section.removeIndicator(id); this.sectionDAO.save(section); }); } diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/service/SectionService.java b/src/main/java/eu/dnetlib/uoamonitorservice/service/SectionService.java index ec7a606..61dc622 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/service/SectionService.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/service/SectionService.java @@ -3,13 +3,17 @@ package eu.dnetlib.uoamonitorservice.service; import eu.dnetlib.uoaadmintoolslibrary.handlers.ForbiddenException; import eu.dnetlib.uoamonitorservice.dao.*; import eu.dnetlib.uoamonitorservice.dto.SectionFull; -import eu.dnetlib.uoamonitorservice.entities.*; +import eu.dnetlib.uoamonitorservice.entities.Section; +import eu.dnetlib.uoamonitorservice.entities.Stakeholder; +import eu.dnetlib.uoamonitorservice.entities.SubCategory; import eu.dnetlib.uoamonitorservice.handlers.EntityNotFoundException; import eu.dnetlib.uoamonitorservice.handlers.PathNotValidException; +import eu.dnetlib.uoamonitorservice.primitives.ReorderEvent; import eu.dnetlib.uoamonitorservice.primitives.Visibility; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import java.util.HashSet; import java.util.List; import java.util.stream.Collectors; @@ -65,10 +69,25 @@ public class SectionService { } public Section save(Section section) { + if(section.getId() != null) { + section.setIndicators(this.find(section.getId()).getIndicators()); + } section.getIndicators().forEach(this.indicatorService::find); return this.dao.save(section); } + public void saveBulk(Stakeholder stakeholder, SubCategory subCategory, List sections) { + if (this.commonService.hasCreateAuthority(stakeholder.getType())) { + sections = sections.stream().map(this::buildSection).collect(Collectors.toList()); + sections.forEach(section -> { + this.addSection(subCategory, section.getId()); + this.createChildren(subCategory, new Section(section), -1); + }); + } else { + throw new ForbiddenException("You are not authorized to create sections in stakeholder with id: " + stakeholder.getId()); + } + } + public SectionFull save(Stakeholder stakeholder, SubCategory subCategory, Section section, int index) { section.setStakeholderAlias(stakeholder.getAlias()); if (section.getId() != null) { @@ -97,6 +116,9 @@ public class SectionService { this.topicDAO.findByCategoriesContaining(category.getId()).forEach(topic -> { this.stakeholderDAO.findByTopicsContaining(topic.getId()).forEach(stakeholder -> { this.save(stakeholder, subCategory, section.copy(), index); + section.getIndicators().forEach(indicator -> { + this.indicatorService.createChildren(section, this.indicatorService.find(indicator).copy()); + }); }); }); }); @@ -109,6 +131,25 @@ public class SectionService { }); } + public SectionFull reorderIndicators(Stakeholder stakeholder, Section section, ReorderEvent reorderEvent) { + if (this.commonService.hasEditAuthority(stakeholder.getType(), stakeholder.getAlias())) { + reorderEvent.getIds().forEach(this.indicatorService::find); + if (reorderEvent.getAction().equals("added")) { + section.addIndicator(reorderEvent.getTarget()); + } else if (reorderEvent.getAction().equals("removed")) { + section.removeIndicator(reorderEvent.getTarget()); + } + if (section.getIndicators().size() == reorderEvent.getIds().size() && new HashSet<>(section.getIndicators()).containsAll(reorderEvent.getIds())) { + section.setIndicators(reorderEvent.getIds()); + return this.getFullSection(stakeholder.getType(), stakeholder.getAlias(), this.dao.save(section)); + } else { + throw new EntityNotFoundException("Some indicators dont exist in the section with id " + section.getId()); + } + } else { + throw new ForbiddenException("You are not authorized to reorder indicators in section with id: " + section.getId()); + } + } + public void delete(String type, Section section, boolean remove) { if(this.commonService.hasDeleteAuthority(type)) { this.dao.findByDefaultId(section.getId()).forEach(child -> { @@ -131,6 +172,10 @@ public class SectionService { this.delete(type, section, remove); } + public void addSection(SubCategory subCategory, String id) { + this.addSection(subCategory, id, -1); + } + public void addSection(SubCategory subCategory, String id, int index) { if(this.find(id).isNumber()) { subCategory.addNumber(id, index); diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/service/StakeholderService.java b/src/main/java/eu/dnetlib/uoamonitorservice/service/StakeholderService.java index 0e57409..2335cec 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/service/StakeholderService.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/service/StakeholderService.java @@ -9,6 +9,7 @@ import eu.dnetlib.uoamonitorservice.primitives.Visibility; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import java.util.HashSet; import java.util.List; import java.util.stream.Collectors; @@ -27,7 +28,7 @@ public class StakeholderService { this.topicService = topicService; } - public Stakeholder findById(String id) { + public Stakeholder find(String id) { return this.dao.findById(id).orElseThrow(() -> new EntityNotFoundException("Stakeholder with id: " + id + " not found")); } @@ -95,12 +96,29 @@ public class StakeholderService { } public Stakeholder save(Stakeholder stakeholder) { + if(stakeholder.getId() != null) { + stakeholder.setTopics(this.find(stakeholder.getId()).getTopics()); + } stakeholder.getTopics().forEach(this.topicService::find); return this.dao.save(stakeholder); } + public StakeholderFull reorderTopics(Stakeholder stakeholder, List topics) { + if(this.commonService.hasEditAuthority(stakeholder.getType(), stakeholder.getAlias())) { + topics.forEach(this.topicService::find); + if (stakeholder.getTopics().size() == topics.size() && new HashSet<>(stakeholder.getTopics()).containsAll(topics)) { + stakeholder.setTopics(topics); + return this.getFullStakeholder(this.dao.save(stakeholder)); + } else { + throw new EntityNotFoundException("Some topics dont exist in the stakeholder with id " + stakeholder.getId()); + } + } else { + throw new ForbiddenException("You are not authorized to reorder topics in stakeholder with id: " + stakeholder.getId()); + } + } + public String delete(String id) { - Stakeholder stakeholder = this.findById(id); + Stakeholder stakeholder = this.find(id); if(this.commonService.hasDeleteAuthority(stakeholder.getType())) { this.dao.findByDefaultId(stakeholder.getId()).forEach(child -> { this.delete(child.getId()); diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/service/SubCategoryService.java b/src/main/java/eu/dnetlib/uoamonitorservice/service/SubCategoryService.java index 597bbae..80c5186 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/service/SubCategoryService.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/service/SubCategoryService.java @@ -5,23 +5,18 @@ import eu.dnetlib.uoamonitorservice.dao.CategoryDAO; import eu.dnetlib.uoamonitorservice.dao.StakeholderDAO; import eu.dnetlib.uoamonitorservice.dao.SubCategoryDAO; import eu.dnetlib.uoamonitorservice.dao.TopicDAO; -import eu.dnetlib.uoamonitorservice.dto.CategoryFull; import eu.dnetlib.uoamonitorservice.dto.SubCategoryFull; -import eu.dnetlib.uoamonitorservice.dto.TopicFull; 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.generics.Common; -import eu.dnetlib.uoamonitorservice.generics.SectionGeneric; -import eu.dnetlib.uoamonitorservice.generics.SubCategoryGeneric; import eu.dnetlib.uoamonitorservice.handlers.EntityNotFoundException; import eu.dnetlib.uoamonitorservice.handlers.PathNotValidException; -import eu.dnetlib.uoamonitorservice.primitives.IndicatorType; import eu.dnetlib.uoamonitorservice.primitives.Visibility; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import java.util.HashSet; +import java.util.List; import java.util.stream.Collectors; @Service @@ -82,6 +77,11 @@ public class SubCategoryService { } public SubCategory save(SubCategory subCategory) { + if(subCategory.getId() != null) { + SubCategory old = this.find(subCategory.getId()); + subCategory.setNumbers(old.getNumbers()); + subCategory.setCharts(old.getCharts()); + } subCategory.getNumbers().forEach(this.sectionService::find); subCategory.getCharts().forEach(this.sectionService::find); return this.dao.save(subCategory); @@ -126,6 +126,34 @@ public class SubCategoryService { }); } + public SubCategoryFull reorderNumbers(Stakeholder stakeholder, SubCategory subCategory, List numbers) { + if(this.commonService.hasEditAuthority(stakeholder.getType(), stakeholder.getAlias())) { + numbers.forEach(this.sectionService::find); + if (subCategory.getNumbers().size() == numbers.size() && new HashSet<>(subCategory.getNumbers()).containsAll(numbers)) { + subCategory.setNumbers(numbers); + return this.getFullSubCategory(stakeholder.getType(), stakeholder.getAlias(), this.dao.save(subCategory)); + } else { + throw new EntityNotFoundException("Some sections dont exist in the subCategory with id " + subCategory.getId()); + } + } else { + throw new ForbiddenException("You are not authorized to reorder sections in subCategory with id: " + subCategory.getId()); + } + } + + public SubCategoryFull reorderCharts(Stakeholder stakeholder, SubCategory subCategory, List charts) { + if(this.commonService.hasEditAuthority(stakeholder.getType(), stakeholder.getAlias())) { + charts.forEach(this.sectionService::find); + if (subCategory.getCharts().size() == charts.size() && new HashSet<>(subCategory.getCharts()).containsAll(charts)) { + subCategory.setCharts(charts); + return this.getFullSubCategory(stakeholder.getType(), stakeholder.getAlias(), this.dao.save(subCategory)); + } else { + throw new EntityNotFoundException("Some sections dont exist in the subCategory with id " + subCategory.getId()); + } + } else { + throw new ForbiddenException("You are not authorized to reorder sections in subCategory with id: " + subCategory.getId()); + } + } + public void delete(String type, SubCategory subCategory, boolean remove) { if(this.commonService.hasDeleteAuthority(type)) { this.dao.findByDefaultId(subCategory.getId()).forEach(child -> { diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/service/TopicService.java b/src/main/java/eu/dnetlib/uoamonitorservice/service/TopicService.java index eb46604..60527ae 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/service/TopicService.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/service/TopicService.java @@ -12,6 +12,7 @@ import eu.dnetlib.uoamonitorservice.primitives.Visibility; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import java.util.HashSet; import java.util.List; import java.util.stream.Collectors; @@ -65,6 +66,9 @@ public class TopicService { } public Topic save(Topic topic) { + if(topic.getId() != null) { + topic.setCategories(this.find(topic.getId()).getCategories()); + } topic.getCategories().forEach(this.categoryService::find); return this.dao.save(topic); } @@ -102,6 +106,20 @@ public class TopicService { }); } + public TopicFull reorderCategories(Stakeholder stakeholder, Topic topic, List categories) { + if(this.commonService.hasEditAuthority(stakeholder.getType(), stakeholder.getAlias())) { + categories.forEach(this.categoryService::find); + if (topic.getCategories().size() == categories.size() && new HashSet<>(topic.getCategories()).containsAll(categories)) { + topic.setCategories(categories); + return this.getFullTopic(stakeholder.getType(), stakeholder.getAlias(), this.dao.save(topic)); + } else { + throw new EntityNotFoundException("Some categories dont exist in the topic with id " + topic.getId()); + } + } else { + throw new ForbiddenException("You are not authorized to reorder categories in topic with id: " + topic.getId()); + } + } + public void delete(String type, Topic topic, boolean remove) { if (this.commonService.hasDeleteAuthority(type)) { this.dao.findByDefaultId(topic.getId()).forEach(child -> { From eee0814016bbcce104cb656aa8896396d152c871 Mon Sep 17 00:00:00 2001 From: "k.triantafyllou" Date: Tue, 2 Apr 2024 22:26:57 +0300 Subject: [PATCH 10/11] [new-model]: Reorder for move action for all entities. Doesn't work for added and removed, need a different implementation. --- .../uoamonitorservice/dao/SubCategoryDAO.java | 3 +- .../primitives/ReorderEvent.java | 9 +++ .../service/CategoryService.java | 13 +++- .../service/CommonService.java | 23 +++++++ .../service/SectionService.java | 64 ++++++++++++++----- .../service/StakeholderService.java | 37 +++++++---- .../service/SubCategoryService.java | 22 +++++++ .../service/TopicService.java | 11 ++++ 8 files changed, 148 insertions(+), 34 deletions(-) diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/dao/SubCategoryDAO.java b/src/main/java/eu/dnetlib/uoamonitorservice/dao/SubCategoryDAO.java index 237f294..86bcecd 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/dao/SubCategoryDAO.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/dao/SubCategoryDAO.java @@ -10,8 +10,7 @@ import java.util.Optional; @Repository public interface SubCategoryDAO extends MongoRepository { List findAll(); - List findByDefaultId(String DefaultId); - + List findByDefaultId(String defaultId); List findByNumbersContaining(String id); List findByChartsContaining(String id); diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/primitives/ReorderEvent.java b/src/main/java/eu/dnetlib/uoamonitorservice/primitives/ReorderEvent.java index caba8e8..cb9917c 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/primitives/ReorderEvent.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/primitives/ReorderEvent.java @@ -7,6 +7,15 @@ public class ReorderEvent { private String target; private List ids; + public ReorderEvent() { + } + + public ReorderEvent(String action, String target, List ids) { + this.action = action; + this.target = target; + this.ids = ids; + } + public String getAction() { return action; } diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/service/CategoryService.java b/src/main/java/eu/dnetlib/uoamonitorservice/service/CategoryService.java index f1e9505..6ec151b 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/service/CategoryService.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/service/CategoryService.java @@ -9,6 +9,7 @@ import eu.dnetlib.uoamonitorservice.dto.TopicFull; import eu.dnetlib.uoamonitorservice.entities.Category; import eu.dnetlib.uoamonitorservice.entities.Stakeholder; import eu.dnetlib.uoamonitorservice.entities.Topic; +import eu.dnetlib.uoamonitorservice.generics.Common; import eu.dnetlib.uoamonitorservice.handlers.EntityNotFoundException; import eu.dnetlib.uoamonitorservice.handlers.PathNotValidException; import eu.dnetlib.uoamonitorservice.primitives.Visibility; @@ -86,7 +87,7 @@ public class CategoryService { public CategoryFull save(Stakeholder stakeholder, Topic topic, Category category, boolean createOverview) { if (category.getId() != null) { if (this.commonService.hasEditAuthority(stakeholder.getType(), stakeholder.getAlias())) { - category.setSubCategories(this.find(topic.getId()).getSubCategories()); + category.setSubCategories(this.find(category.getId()).getSubCategories()); this.updateChildren(category); category = this.save(category); } else { @@ -126,6 +127,7 @@ public class CategoryService { subcategories.forEach(this.subCategoryService::find); if (category.getSubCategories().size() == subcategories.size() && new HashSet<>(category.getSubCategories()).containsAll(subcategories)) { category.setSubCategories(subcategories); + this.reorderChildren(stakeholder, category, subcategories); return this.getFullCategory(stakeholder.getType(), stakeholder.getAlias(), this.dao.save(category)); } else { throw new EntityNotFoundException("Some subCategories dont exist in the category with id " + category.getId()); @@ -135,6 +137,15 @@ public class CategoryService { } } + public void reorderChildren(Stakeholder defaultStakeholder, Category defaultCategory, List defaultSubCategories) { + this.stakeholderDAO.findByDefaultId(defaultStakeholder.getId()).forEach(stakeholder -> { + this.dao.findByDefaultId(defaultCategory.getId()).stream().map(category -> this.getFullCategory(stakeholder.getType(), stakeholder.getAlias(), category)).forEach(category -> { + this.reorderSubCategories(stakeholder, new Category(category), + this.commonService.reorder(defaultSubCategories, category.getSubCategories().stream().map(subCategory -> (Common) subCategory).collect(Collectors.toList()))); + }); + }); + } + public void delete(String type, Category category, boolean remove) { if (this.commonService.hasDeleteAuthority(type)) { this.dao.findByDefaultId(category.getId()).forEach(child -> { diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/service/CommonService.java b/src/main/java/eu/dnetlib/uoamonitorservice/service/CommonService.java index 5073704..40bdf3b 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/service/CommonService.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/service/CommonService.java @@ -1,11 +1,18 @@ package eu.dnetlib.uoamonitorservice.service; import eu.dnetlib.uoaauthorizationlibrary.security.AuthorizationService; +import eu.dnetlib.uoamonitorservice.dto.TopicFull; import eu.dnetlib.uoamonitorservice.generics.Common; import eu.dnetlib.uoamonitorservice.primitives.Visibility; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import java.util.Comparator; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + @Service public class CommonService { @@ -46,4 +53,20 @@ public class CommonService { return common.getVisibility() == Visibility.PUBLIC; } } + + public List reorder(List defaultIds, List commons) { + Map map = new HashMap<>(); + for(int i = 0; i < commons.size(); i++) { + if (!defaultIds.contains(commons.get(i).getDefaultId())) { + map.put(commons.get(i).getId(), i); + } + } + commons = commons.stream().filter(common -> defaultIds.contains(common.getDefaultId())).collect(Collectors.toList());; + commons.sort(Comparator.comparingInt(common -> defaultIds.indexOf(common.getDefaultId()))); + List ids = commons.stream().map(Common::getId).collect(Collectors.toList()); + map.keySet().forEach(key -> { + ids.add(map.get(key), key); + }); + return ids; + } } diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/service/SectionService.java b/src/main/java/eu/dnetlib/uoamonitorservice/service/SectionService.java index 61dc622..4ffe51c 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/service/SectionService.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/service/SectionService.java @@ -3,9 +3,8 @@ package eu.dnetlib.uoamonitorservice.service; import eu.dnetlib.uoaadmintoolslibrary.handlers.ForbiddenException; import eu.dnetlib.uoamonitorservice.dao.*; import eu.dnetlib.uoamonitorservice.dto.SectionFull; -import eu.dnetlib.uoamonitorservice.entities.Section; -import eu.dnetlib.uoamonitorservice.entities.Stakeholder; -import eu.dnetlib.uoamonitorservice.entities.SubCategory; +import eu.dnetlib.uoamonitorservice.entities.*; +import eu.dnetlib.uoamonitorservice.generics.Common; import eu.dnetlib.uoamonitorservice.handlers.EntityNotFoundException; import eu.dnetlib.uoamonitorservice.handlers.PathNotValidException; import eu.dnetlib.uoamonitorservice.primitives.ReorderEvent; @@ -30,7 +29,7 @@ public class SectionService { private final CommonService commonService; @Autowired - public SectionService(StakeholderDAO stakeholderDAO, TopicDAO topicDAO, CategoryDAO categoryDAO,SubCategoryDAO subCategoryDAO, SectionDAO dao, IndicatorService indicatorService, CommonService commonService) { + public SectionService(StakeholderDAO stakeholderDAO, TopicDAO topicDAO, CategoryDAO categoryDAO, SubCategoryDAO subCategoryDAO, SectionDAO dao, IndicatorService indicatorService, CommonService commonService) { this.stakeholderDAO = stakeholderDAO; this.topicDAO = topicDAO; this.categoryDAO = categoryDAO; @@ -69,7 +68,7 @@ public class SectionService { } public Section save(Section section) { - if(section.getId() != null) { + if (section.getId() != null) { section.setIndicators(this.find(section.getId()).getIndicators()); } section.getIndicators().forEach(this.indicatorService::find); @@ -117,7 +116,7 @@ public class SectionService { this.stakeholderDAO.findByTopicsContaining(topic.getId()).forEach(stakeholder -> { this.save(stakeholder, subCategory, section.copy(), index); section.getIndicators().forEach(indicator -> { - this.indicatorService.createChildren(section, this.indicatorService.find(indicator).copy()); + this.indicatorService.createChildren(section, this.indicatorService.find(indicator)); }); }); }); @@ -131,16 +130,19 @@ public class SectionService { }); } - public SectionFull reorderIndicators(Stakeholder stakeholder, Section section, ReorderEvent reorderEvent) { + public SectionFull reorderIndicators(Stakeholder stakeholder, Section section, ReorderEvent event) { if (this.commonService.hasEditAuthority(stakeholder.getType(), stakeholder.getAlias())) { - reorderEvent.getIds().forEach(this.indicatorService::find); - if (reorderEvent.getAction().equals("added")) { - section.addIndicator(reorderEvent.getTarget()); - } else if (reorderEvent.getAction().equals("removed")) { - section.removeIndicator(reorderEvent.getTarget()); + event.getIds().forEach(this.indicatorService::find); + if (event.getAction().equals("added")) { + section.addIndicator(event.getTarget()); + } else if (event.getAction().equals("removed")) { + section.removeIndicator(event.getTarget()); } - if (section.getIndicators().size() == reorderEvent.getIds().size() && new HashSet<>(section.getIndicators()).containsAll(reorderEvent.getIds())) { - section.setIndicators(reorderEvent.getIds()); + if (section.getIndicators().size() == event.getIds().size() && new HashSet<>(section.getIndicators()).containsAll(event.getIds())) { + section.setIndicators(event.getIds()); + if(event.getAction().equals("moved")) { + this.reorderChildren(stakeholder, section, event); + } return this.getFullSection(stakeholder.getType(), stakeholder.getAlias(), this.dao.save(section)); } else { throw new EntityNotFoundException("Some indicators dont exist in the section with id " + section.getId()); @@ -150,8 +152,36 @@ public class SectionService { } } + public void reorderChildren(Stakeholder defaultStakeholder, Section defaultSection, ReorderEvent event) { + this.stakeholderDAO.findByDefaultId(defaultStakeholder.getId()).forEach(stakeholder -> { + this.dao.findByDefaultId(defaultSection.getId()).stream().map(section -> this.getFullSection(stakeholder.getType(), stakeholder.getAlias(), section)).forEach(section -> { + Indicator target; + if(section.isNumber()) { + target = this.subCategoryDAO.findByNumbersContaining(section.getId()).stream().flatMap(subCategory -> subCategory.getNumbers().stream()) + .map(id -> this.getFullSection(stakeholder.getType(), stakeholder.getAlias(), id)).flatMap(sectionFull -> section.getIndicators().stream()) + .filter(indicator -> indicator.getDefaultId().equals(event.getTarget())).findFirst().orElse(null); + } else { + target = this.subCategoryDAO.findByChartsContaining(section.getId()).stream().flatMap(subCategory -> subCategory.getCharts().stream()) + .map(id -> this.getFullSection(stakeholder.getType(), stakeholder.getAlias(), id)).flatMap(sectionFull -> section.getIndicators().stream()) + .filter(indicator -> indicator.getDefaultId().equals(event.getTarget())).findFirst().orElse(null); + } + if (target != null) { + List indicators = section.getIndicators().stream().map(indicator -> (Common) indicator).collect(Collectors.toList()); + if(event.getAction().equals("removed")) { + indicators.removeIf(indicator -> indicator.getId().equals(target.getId())); + } else if(event.getAction().equals("added")) { + indicators.add(target); + } + List ids = this.commonService.reorder(event.getIds(), indicators); + ReorderEvent childEvent = new ReorderEvent(event.getAction(), target.getId(), ids); + this.reorderIndicators(stakeholder, new Section(section), childEvent); + } + }); + }); + } + public void delete(String type, Section section, boolean remove) { - if(this.commonService.hasDeleteAuthority(type)) { + if (this.commonService.hasDeleteAuthority(type)) { this.dao.findByDefaultId(section.getId()).forEach(child -> { this.delete(type, child.getId(), remove); }); @@ -177,7 +207,7 @@ public class SectionService { } public void addSection(SubCategory subCategory, String id, int index) { - if(this.find(id).isNumber()) { + if (this.find(id).isNumber()) { subCategory.addNumber(id, index); } else { subCategory.addChart(id, index); @@ -197,7 +227,7 @@ public class SectionService { } public SectionFull changeVisibility(String type, String alias, SectionFull section, Visibility visibility) { - if(this.commonService.hasEditAuthority(type, alias)) { + if (this.commonService.hasEditAuthority(type, alias)) { section.setIndicators(section.getIndicators().stream() .map(indicator -> this.indicatorService.changeVisibility(type, alias, indicator, visibility)) .collect(Collectors.toList())); diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/service/StakeholderService.java b/src/main/java/eu/dnetlib/uoamonitorservice/service/StakeholderService.java index 2335cec..c38c81e 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/service/StakeholderService.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/service/StakeholderService.java @@ -4,6 +4,7 @@ import eu.dnetlib.uoaadmintoolslibrary.handlers.ForbiddenException; import eu.dnetlib.uoamonitorservice.dao.StakeholderDAO; import eu.dnetlib.uoamonitorservice.dto.StakeholderFull; import eu.dnetlib.uoamonitorservice.entities.Stakeholder; +import eu.dnetlib.uoamonitorservice.generics.Common; import eu.dnetlib.uoamonitorservice.handlers.EntityNotFoundException; import eu.dnetlib.uoamonitorservice.primitives.Visibility; import org.springframework.beans.factory.annotation.Autowired; @@ -36,7 +37,7 @@ public class StakeholderService { return this.dao.findByAlias(alias).orElseThrow(() -> new EntityNotFoundException("Stakeholder with alias: " + alias + " not found")); } - public List findByDefaultId(String id ){ + public List findByDefaultId(String id) { return this.dao.findByDefaultId(id); } @@ -49,37 +50,37 @@ public class StakeholderService { } public List getAll(String type) { - if(type != null) { + if (type != null) { return this.dao.findByType(type); } return this.dao.findAll(); } public List getAllDefaultByRole(String type) { - return (type == null?this.dao.findByDefaultId(null):this.dao.findByDefaultIdAndType(null, type)).stream() + return (type == null ? this.dao.findByDefaultId(null) : this.dao.findByDefaultIdAndType(null, type)).stream() .filter(stakeholder -> this.commonService.hasAccessAuthority(stakeholder.getType(), stakeholder.getAlias(), true)) .collect(Collectors.toList()); } public List getStakeholdersByTypeAndRole(String type, String defaultId, boolean manage) { List stakeholders; - if(type != null && defaultId != null) { + if (type != null && defaultId != null) { stakeholders = dao.findByDefaultIdAndType(defaultId, type); - } else if(defaultId != null) { + } else if (defaultId != null) { stakeholders = dao.findByDefaultId(defaultId); - } else if(type != null) { + } else if (type != null) { stakeholders = dao.findByDefaultIdNotAndType(null, type); } else { stakeholders = dao.findByDefaultIdNot(null); } return stakeholders.stream().filter(stakeholder -> - (!manage && (stakeholder.getVisibility() == Visibility.PUBLIC || stakeholder.getVisibility() == Visibility.RESTRICTED)) - || this.commonService.hasAccessAuthority(stakeholder.getType(), stakeholder.getAlias(), false)) + (!manage && (stakeholder.getVisibility() == Visibility.PUBLIC || stakeholder.getVisibility() == Visibility.RESTRICTED)) + || this.commonService.hasAccessAuthority(stakeholder.getType(), stakeholder.getAlias(), false)) .collect(Collectors.toList()); } public StakeholderFull getFullStakeholder(Stakeholder stakeholder) { - if(this.commonService.hasVisibilityAuthority(stakeholder.getType(), stakeholder.getAlias(), stakeholder)) { + if (this.commonService.hasVisibilityAuthority(stakeholder.getType(), stakeholder.getAlias(), stakeholder)) { return new StakeholderFull(stakeholder, stakeholder.getTopics().stream() .map(topicId -> topicService.getFullTopic(stakeholder.getType(), stakeholder.getAlias(), topicId)) @@ -96,7 +97,7 @@ public class StakeholderService { } public Stakeholder save(Stakeholder stakeholder) { - if(stakeholder.getId() != null) { + if (stakeholder.getId() != null) { stakeholder.setTopics(this.find(stakeholder.getId()).getTopics()); } stakeholder.getTopics().forEach(this.topicService::find); @@ -104,10 +105,11 @@ public class StakeholderService { } public StakeholderFull reorderTopics(Stakeholder stakeholder, List topics) { - if(this.commonService.hasEditAuthority(stakeholder.getType(), stakeholder.getAlias())) { + if (this.commonService.hasEditAuthority(stakeholder.getType(), stakeholder.getAlias())) { topics.forEach(this.topicService::find); if (stakeholder.getTopics().size() == topics.size() && new HashSet<>(stakeholder.getTopics()).containsAll(topics)) { stakeholder.setTopics(topics); + this.reorderChildren(stakeholder, topics); return this.getFullStakeholder(this.dao.save(stakeholder)); } else { throw new EntityNotFoundException("Some topics dont exist in the stakeholder with id " + stakeholder.getId()); @@ -117,9 +119,16 @@ public class StakeholderService { } } + public void reorderChildren(Stakeholder defaultStakeholder, List defaultTopics) { + this.dao.findByDefaultId(defaultStakeholder.getId()).stream().map(this::getFullStakeholder).forEach(stakeholder -> { + this.reorderTopics(new Stakeholder(stakeholder), + this.commonService.reorder(defaultTopics, stakeholder.getTopics().stream().map(topic -> (Common) topic).collect(Collectors.toList()))); + }); + } + public String delete(String id) { Stakeholder stakeholder = this.find(id); - if(this.commonService.hasDeleteAuthority(stakeholder.getType())) { + if (this.commonService.hasDeleteAuthority(stakeholder.getType())) { this.dao.findByDefaultId(stakeholder.getId()).forEach(child -> { this.delete(child.getId()); }); @@ -134,8 +143,8 @@ public class StakeholderService { } public StakeholderFull changeVisibility(StakeholderFull stakeholder, Visibility visibility, Boolean propagate) { - if(this.commonService.hasEditAuthority(stakeholder.getType(), stakeholder.getAlias())) { - if(propagate) { + if (this.commonService.hasEditAuthority(stakeholder.getType(), stakeholder.getAlias())) { + if (propagate) { stakeholder.setTopics(stakeholder.getTopics().stream(). map(topic -> this.topicService.changeVisibility(stakeholder.getType(), stakeholder.getAlias(), topic, visibility, true)) .collect(Collectors.toList())); diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/service/SubCategoryService.java b/src/main/java/eu/dnetlib/uoamonitorservice/service/SubCategoryService.java index 80c5186..ff3141e 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/service/SubCategoryService.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/service/SubCategoryService.java @@ -9,8 +9,10 @@ import eu.dnetlib.uoamonitorservice.dto.SubCategoryFull; import eu.dnetlib.uoamonitorservice.entities.Category; import eu.dnetlib.uoamonitorservice.entities.Stakeholder; import eu.dnetlib.uoamonitorservice.entities.SubCategory; +import eu.dnetlib.uoamonitorservice.generics.Common; import eu.dnetlib.uoamonitorservice.handlers.EntityNotFoundException; import eu.dnetlib.uoamonitorservice.handlers.PathNotValidException; +import eu.dnetlib.uoamonitorservice.primitives.IndicatorType; import eu.dnetlib.uoamonitorservice.primitives.Visibility; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -131,6 +133,7 @@ public class SubCategoryService { numbers.forEach(this.sectionService::find); if (subCategory.getNumbers().size() == numbers.size() && new HashSet<>(subCategory.getNumbers()).containsAll(numbers)) { subCategory.setNumbers(numbers); + this.reorderChildrenNumbers(stakeholder, subCategory, numbers); return this.getFullSubCategory(stakeholder.getType(), stakeholder.getAlias(), this.dao.save(subCategory)); } else { throw new EntityNotFoundException("Some sections dont exist in the subCategory with id " + subCategory.getId()); @@ -145,6 +148,7 @@ public class SubCategoryService { charts.forEach(this.sectionService::find); if (subCategory.getCharts().size() == charts.size() && new HashSet<>(subCategory.getCharts()).containsAll(charts)) { subCategory.setCharts(charts); + this.reorderChildrenCharts(stakeholder, subCategory, charts); return this.getFullSubCategory(stakeholder.getType(), stakeholder.getAlias(), this.dao.save(subCategory)); } else { throw new EntityNotFoundException("Some sections dont exist in the subCategory with id " + subCategory.getId()); @@ -154,6 +158,24 @@ public class SubCategoryService { } } + public void reorderChildrenNumbers(Stakeholder defaultStakeholder, SubCategory defaultSubCategory, List defaultSections) { + this.stakeholderDAO.findByDefaultId(defaultStakeholder.getId()).forEach(stakeholder -> { + this.dao.findByDefaultId(defaultSubCategory.getId()).stream().map(subCategory -> this.getFullSubCategory(stakeholder.getType(), stakeholder.getAlias(), subCategory)).forEach(subCategory -> { + this.reorderNumbers(stakeholder, new SubCategory(subCategory), + this.commonService.reorder(defaultSections, subCategory.getNumbers().stream().map(section -> (Common) section).collect(Collectors.toList()))); + }); + }); + } + + public void reorderChildrenCharts(Stakeholder defaultStakeholder, SubCategory defaultSubCategory, List defaultSections) { + this.stakeholderDAO.findByDefaultId(defaultStakeholder.getId()).forEach(stakeholder -> { + this.dao.findByDefaultId(defaultSubCategory.getId()).stream().map(subCategory -> this.getFullSubCategory(stakeholder.getType(), stakeholder.getAlias(), subCategory)).forEach(subCategory -> { + this.reorderCharts(stakeholder, new SubCategory(subCategory), + this.commonService.reorder(defaultSections, subCategory.getCharts().stream().map(section -> (Common) section).collect(Collectors.toList()))); + }); + }); + } + public void delete(String type, SubCategory subCategory, boolean remove) { if(this.commonService.hasDeleteAuthority(type)) { this.dao.findByDefaultId(subCategory.getId()).forEach(child -> { diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/service/TopicService.java b/src/main/java/eu/dnetlib/uoamonitorservice/service/TopicService.java index 60527ae..a5a4c89 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/service/TopicService.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/service/TopicService.java @@ -6,6 +6,7 @@ import eu.dnetlib.uoamonitorservice.dao.TopicDAO; import eu.dnetlib.uoamonitorservice.dto.TopicFull; import eu.dnetlib.uoamonitorservice.entities.Stakeholder; import eu.dnetlib.uoamonitorservice.entities.Topic; +import eu.dnetlib.uoamonitorservice.generics.Common; import eu.dnetlib.uoamonitorservice.handlers.EntityNotFoundException; import eu.dnetlib.uoamonitorservice.handlers.PathNotValidException; import eu.dnetlib.uoamonitorservice.primitives.Visibility; @@ -111,6 +112,7 @@ public class TopicService { categories.forEach(this.categoryService::find); if (topic.getCategories().size() == categories.size() && new HashSet<>(topic.getCategories()).containsAll(categories)) { topic.setCategories(categories); + this.reorderChildren(stakeholder, topic, categories); return this.getFullTopic(stakeholder.getType(), stakeholder.getAlias(), this.dao.save(topic)); } else { throw new EntityNotFoundException("Some categories dont exist in the topic with id " + topic.getId()); @@ -120,6 +122,15 @@ public class TopicService { } } + public void reorderChildren(Stakeholder defaultStakeholder, Topic defaultTopic, List defaultCategories) { + this.stakeholderDAO.findByDefaultId(defaultStakeholder.getId()).forEach(stakeholder -> { + this.dao.findByDefaultId(defaultTopic.getId()).stream().map(topic -> this.getFullTopic(stakeholder.getType(), stakeholder.getAlias(), topic)).forEach(topic -> { + this.reorderCategories(stakeholder, new Topic(topic), + this.commonService.reorder(defaultCategories, topic.getCategories().stream().map(category -> (Common) category).collect(Collectors.toList()))); + }); + }); + } + public void delete(String type, Topic topic, boolean remove) { if (this.commonService.hasDeleteAuthority(type)) { this.dao.findByDefaultId(topic.getId()).forEach(child -> { From dc383dda0d8e365c2bfff1c664bdf8443aa1ffdc Mon Sep 17 00:00:00 2001 From: "k.triantafyllou" Date: Sat, 6 Apr 2024 12:40:52 +0300 Subject: [PATCH 11/11] [new-model]: Add move indicator controller in order to move an indicator from a section to another. --- .../configuration/mongo/MongoConnection.java | 2 +- .../controllers/IndicatorController.java | 10 +-- .../controllers/SectionController.java | 27 ++++---- .../uoamonitorservice/dto/MoveIndicator.java | 40 ++++++++++++ .../uoamonitorservice/dto/SectionFull.java | 14 +++++ .../uoamonitorservice/dto/SectionInfo.java | 32 ++++++++++ .../dto/SubCategoryFull.java | 8 +++ .../uoamonitorservice/generics/Common.java | 1 - .../primitives/ReorderEvent.java | 51 --------------- .../service/CategoryService.java | 9 ++- .../service/IndicatorService.java | 9 ++- .../service/SectionService.java | 62 ++++++++----------- .../service/StakeholderService.java | 5 ++ .../service/SubCategoryService.java | 52 +++++++++++++++- .../service/TopicService.java | 7 +++ 15 files changed, 211 insertions(+), 118 deletions(-) create mode 100644 src/main/java/eu/dnetlib/uoamonitorservice/dto/MoveIndicator.java create mode 100644 src/main/java/eu/dnetlib/uoamonitorservice/dto/SectionInfo.java delete mode 100644 src/main/java/eu/dnetlib/uoamonitorservice/primitives/ReorderEvent.java diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/configuration/mongo/MongoConnection.java b/src/main/java/eu/dnetlib/uoamonitorservice/configuration/mongo/MongoConnection.java index 17e52d1..676f4ea 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/configuration/mongo/MongoConnection.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/configuration/mongo/MongoConnection.java @@ -12,8 +12,8 @@ import org.springframework.data.mongodb.MongoDbFactory; import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.data.mongodb.core.SimpleMongoDbFactory; import org.springframework.data.mongodb.repository.config.EnableMongoRepositories; - import java.util.Collections; + @Configuration @EnableMongoRepositories(basePackages = {"eu.dnetlib.uoamonitorservice.dao", "eu.dnetlib.uoaadmintoolslibrary.dao"}) public class MongoConnection { diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/IndicatorController.java b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/IndicatorController.java index 32f79f7..52f9452 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/IndicatorController.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/IndicatorController.java @@ -4,7 +4,6 @@ package eu.dnetlib.uoamonitorservice.controllers; import eu.dnetlib.uoamonitorservice.dto.SectionFull; import eu.dnetlib.uoamonitorservice.dto.StakeholderFull; import eu.dnetlib.uoamonitorservice.entities.*; -import eu.dnetlib.uoamonitorservice.primitives.ReorderEvent; import eu.dnetlib.uoamonitorservice.primitives.Visibility; import eu.dnetlib.uoamonitorservice.service.*; import org.apache.logging.log4j.LogManager; @@ -14,7 +13,6 @@ import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.web.bind.annotation.*; import java.io.UnsupportedEncodingException; -import java.util.ArrayList; import java.util.List; @RestController @@ -98,22 +96,20 @@ public class IndicatorController { @PreAuthorize("isAuthenticated()") - @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/{sectionId}/{type}/reorder", method = RequestMethod.POST) + @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/{sectionId}/reorder", method = RequestMethod.POST) public List reorderIndicators(@PathVariable("stakeholderId") String stakeholderId, @PathVariable("topicId") String topicId, @PathVariable("categoryId") String categoryId, @PathVariable("subcategoryId") String subcategoryId, @PathVariable("sectionId") String sectionId, - @PathVariable("type") String type, - @RequestBody ReorderEvent reorderEvent) { - log.debug("reorder indicators of type: " + type); + @RequestBody List indicators) { log.debug("Stakeholder: " + stakeholderId + " - Topic: " + topicId + " - Category: " + categoryId + " - SubCategory: " + subcategoryId + " - Section: " + sectionId); Stakeholder stakeholder = this.stakeholderService.find(stakeholderId); Topic topic = this.topicService.findByPath(stakeholder, topicId); Category category = this.categoryService.findByPath(topic, categoryId); SubCategory subCategory = this.subCategoryService.findByPath(category, subcategoryId); Section section = this.sectionService.findByPath(subCategory, sectionId); - return this.sectionService.reorderIndicators(stakeholder, section, reorderEvent).getIndicators(); + return this.sectionService.reorderIndicators(stakeholder, section, indicators).getIndicators(); } @PreAuthorize("isAuthenticated()") diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/SectionController.java b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/SectionController.java index f917627..46115dc 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/SectionController.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/SectionController.java @@ -1,6 +1,8 @@ package eu.dnetlib.uoamonitorservice.controllers; +import eu.dnetlib.uoamonitorservice.dto.MoveIndicator; import eu.dnetlib.uoamonitorservice.dto.SectionFull; +import eu.dnetlib.uoamonitorservice.dto.SubCategoryFull; import eu.dnetlib.uoamonitorservice.entities.*; import eu.dnetlib.uoamonitorservice.handlers.PathNotValidException; import eu.dnetlib.uoamonitorservice.service.*; @@ -70,25 +72,18 @@ public class SectionController { } @PreAuthorize("isAuthenticated()") - @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/{type}/reorder", method = RequestMethod.POST) - public List reorderSections(@PathVariable("stakeholderId") String stakeholderId, - @PathVariable("topicId") String topicId, - @PathVariable("categoryId") String categoryId, - @PathVariable("subcategoryId") String subcategoryId, - @PathVariable("type") String type, - @RequestBody List sections) { - log.debug("reorder sections of type: " + type); + @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/moveIndicator", method = RequestMethod.POST) + public SubCategoryFull moveIndicator(@PathVariable("stakeholderId") String stakeholderId, + @PathVariable("topicId") String topicId, + @PathVariable("categoryId") String categoryId, + @PathVariable("subcategoryId") String subcategoryId, + @RequestBody MoveIndicator moveIndicator) { + log.debug("move indicator"); log.debug("Stakeholder: " + stakeholderId + " - Topic: " + topicId + " - Category: " + categoryId + " - SubCategory: " + subcategoryId); - Stakeholder stakeholder = this.stakeholderService.findByPath(stakeholderId); + Stakeholder stakeholder = this.stakeholderService.find(stakeholderId); Topic topic = this.topicService.findByPath(stakeholder, topicId); Category category = this.categoryService.findByPath(topic, categoryId); SubCategory subCategory = this.subCategoryService.findByPath(category, subcategoryId); - if (type.equals("number")) { - return this.subCategoryService.reorderNumbers(stakeholder, subCategory, sections).getNumbers(); - } else if (type.equals("chart")) { - return this.subCategoryService.reorderCharts(stakeholder, subCategory, sections).getCharts(); - } else { - throw new PathNotValidException("Type is not valid"); - } + return this.subCategoryService.moveIndicator(stakeholder, subCategory, moveIndicator); } } diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/dto/MoveIndicator.java b/src/main/java/eu/dnetlib/uoamonitorservice/dto/MoveIndicator.java new file mode 100644 index 0000000..624f553 --- /dev/null +++ b/src/main/java/eu/dnetlib/uoamonitorservice/dto/MoveIndicator.java @@ -0,0 +1,40 @@ +package eu.dnetlib.uoamonitorservice.dto; + +public class MoveIndicator { + private String target; + private SectionInfo from; + private SectionInfo to; + + public MoveIndicator() { + } + + public MoveIndicator(String target, SectionInfo from, SectionInfo to) { + this.target = target; + this.from = from; + this.to = to; + } + + public String getTarget() { + return target; + } + + public void setTarget(String target) { + this.target = target; + } + + public SectionInfo getFrom() { + return from; + } + + public void setFrom(SectionInfo from) { + this.from = from; + } + + public SectionInfo getTo() { + return to; + } + + public void setTo(SectionInfo to) { + this.to = to; + } +} diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/dto/SectionFull.java b/src/main/java/eu/dnetlib/uoamonitorservice/dto/SectionFull.java index fd6f7f6..06643bd 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/dto/SectionFull.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/dto/SectionFull.java @@ -3,9 +3,11 @@ package eu.dnetlib.uoamonitorservice.dto; import eu.dnetlib.uoamonitorservice.entities.Indicator; import eu.dnetlib.uoamonitorservice.entities.Section; import eu.dnetlib.uoamonitorservice.generics.SectionGeneric; +import io.swagger.models.auth.In; import java.util.List; import java.util.Objects; +import java.util.Optional; public class SectionFull extends SectionGeneric { public SectionFull() {} @@ -15,4 +17,16 @@ public class SectionFull extends SectionGeneric { indicators.removeIf(Objects::isNull); this.indicators = indicators; } + + public Optional getIndicatorByDefaultId(String id) { + return this.indicators.stream().filter(indicator -> indicator.getDefaultId().equals(id)).findFirst(); + } + + public void removeIndicator(String id) { + this.indicators.removeIf(indicator -> indicator.getId().equals(id)); + } + + public void addIndicator(Indicator indicator) { + this.indicators.add(indicator); + } } diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/dto/SectionInfo.java b/src/main/java/eu/dnetlib/uoamonitorservice/dto/SectionInfo.java new file mode 100644 index 0000000..55f6a10 --- /dev/null +++ b/src/main/java/eu/dnetlib/uoamonitorservice/dto/SectionInfo.java @@ -0,0 +1,32 @@ +package eu.dnetlib.uoamonitorservice.dto; + +import java.util.List; + +public class SectionInfo { + private String id; + private List indicators; + + public SectionInfo() { + } + + public SectionInfo(String id, List indicators) { + this.id = id; + this.indicators = indicators; + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public List getIndicators() { + return indicators; + } + + public void setIndicators(List indicators) { + this.indicators = indicators; + } +} diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/dto/SubCategoryFull.java b/src/main/java/eu/dnetlib/uoamonitorservice/dto/SubCategoryFull.java index b326a2a..aeb5374 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/dto/SubCategoryFull.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/dto/SubCategoryFull.java @@ -2,8 +2,10 @@ package eu.dnetlib.uoamonitorservice.dto; import eu.dnetlib.uoamonitorservice.generics.SubCategoryGeneric; +import java.util.ArrayList; import java.util.List; import java.util.Objects; +import java.util.Optional; public class SubCategoryFull extends SubCategoryGeneric { public SubCategoryFull() {} @@ -15,4 +17,10 @@ public class SubCategoryFull extends SubCategoryGeneric { this.numbers = numbers; this.charts = charts; } + + public Optional getSectionByDefaultId(String id) { + List sections = new ArrayList<>(this.numbers); + sections.addAll(this.charts); + return sections.stream().filter(section -> section.getDefaultId().equals(id)).findFirst(); + } } diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/generics/Common.java b/src/main/java/eu/dnetlib/uoamonitorservice/generics/Common.java index 2075c09..0928914 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/generics/Common.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/generics/Common.java @@ -1,7 +1,6 @@ package eu.dnetlib.uoamonitorservice.generics; import com.fasterxml.jackson.annotation.JsonProperty; -import eu.dnetlib.uoamonitorservice.entities.Stakeholder; import eu.dnetlib.uoamonitorservice.primitives.Visibility; import org.springframework.data.annotation.CreatedDate; import org.springframework.data.annotation.Id; diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/primitives/ReorderEvent.java b/src/main/java/eu/dnetlib/uoamonitorservice/primitives/ReorderEvent.java deleted file mode 100644 index cb9917c..0000000 --- a/src/main/java/eu/dnetlib/uoamonitorservice/primitives/ReorderEvent.java +++ /dev/null @@ -1,51 +0,0 @@ -package eu.dnetlib.uoamonitorservice.primitives; - -import java.util.List; - -public class ReorderEvent { - private String action; // "moved", "added", "removed" - private String target; - private List ids; - - public ReorderEvent() { - } - - public ReorderEvent(String action, String target, List ids) { - this.action = action; - this.target = target; - this.ids = ids; - } - - public String getAction() { - return action; - } - - public void setAction(String action) { - this.action = action; - } - - public String getTarget() { - return target; - } - - public void setTarget(String target) { - this.target = target; - } - - public List getIds() { - return ids; - } - - public void setIds(List ids) { - this.ids = ids; - } - - @Override - public String toString() { - return "ReorderEvent{" + - "action='" + action + '\'' + - ", target='" + target + '\'' + - ", ids=" + ids + - '}'; - } -} diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/service/CategoryService.java b/src/main/java/eu/dnetlib/uoamonitorservice/service/CategoryService.java index 6ec151b..5adef1f 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/service/CategoryService.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/service/CategoryService.java @@ -5,7 +5,6 @@ import eu.dnetlib.uoamonitorservice.dao.CategoryDAO; import eu.dnetlib.uoamonitorservice.dao.StakeholderDAO; import eu.dnetlib.uoamonitorservice.dao.TopicDAO; import eu.dnetlib.uoamonitorservice.dto.CategoryFull; -import eu.dnetlib.uoamonitorservice.dto.TopicFull; import eu.dnetlib.uoamonitorservice.entities.Category; import eu.dnetlib.uoamonitorservice.entities.Stakeholder; import eu.dnetlib.uoamonitorservice.entities.Topic; @@ -16,6 +15,7 @@ import eu.dnetlib.uoamonitorservice.primitives.Visibility; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import java.util.Date; import java.util.HashSet; import java.util.List; import java.util.stream.Collectors; @@ -26,7 +26,6 @@ public class CategoryService { private final StakeholderDAO stakeholderDAO; private final TopicDAO topicDAO; private final CategoryDAO dao; - private final SubCategoryService subCategoryService; private final CommonService commonService; @@ -75,7 +74,10 @@ public class CategoryService { public Category save(Category category) { if(category.getId() != null) { category.setSubCategories(this.find(category.getId()).getSubCategories()); + } else { + category.setCreationDate(new Date()); } + category.setUpdateDate(new Date()); category.getSubCategories().forEach(this.subCategoryService::find); return this.dao.save(category); } @@ -127,6 +129,7 @@ public class CategoryService { subcategories.forEach(this.subCategoryService::find); if (category.getSubCategories().size() == subcategories.size() && new HashSet<>(category.getSubCategories()).containsAll(subcategories)) { category.setSubCategories(subcategories); + category.setUpdateDate(new Date()); this.reorderChildren(stakeholder, category, subcategories); return this.getFullCategory(stakeholder.getType(), stakeholder.getAlias(), this.dao.save(category)); } else { @@ -170,12 +173,14 @@ public class CategoryService { public void addCategory(Topic topic, String id) { topic.addCategory(id); + topic.setUpdateDate(new Date()); this.topicDAO.save(topic); } public void removeCategory(String id) { this.topicDAO.findByCategoriesContaining(id).forEach(topic -> { topic.removeCategory(id); + topic.setUpdateDate(new Date()); this.topicDAO.save(topic); }); } diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/service/IndicatorService.java b/src/main/java/eu/dnetlib/uoamonitorservice/service/IndicatorService.java index 46cc742..820b4c6 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/service/IndicatorService.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/service/IndicatorService.java @@ -2,7 +2,6 @@ package eu.dnetlib.uoamonitorservice.service; import eu.dnetlib.uoaadmintoolslibrary.handlers.ForbiddenException; import eu.dnetlib.uoamonitorservice.dao.*; -import eu.dnetlib.uoamonitorservice.dto.SectionFull; import eu.dnetlib.uoamonitorservice.entities.Indicator; import eu.dnetlib.uoamonitorservice.entities.Section; import eu.dnetlib.uoamonitorservice.entities.Stakeholder; @@ -13,8 +12,8 @@ import eu.dnetlib.uoamonitorservice.primitives.Visibility; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import java.util.Date; import java.util.List; -import java.util.stream.Collectors; @Service public class IndicatorService { @@ -60,6 +59,10 @@ public class IndicatorService { } public Indicator save(Indicator indicator) { + if(indicator.getId() == null) { + indicator.setCreationDate(new Date()); + } + indicator.setUpdateDate(new Date()); return this.dao.save(indicator); } @@ -127,12 +130,14 @@ public class IndicatorService { public void addIndicator(Section section, String id) { section.addIndicator(id); + section.setUpdateDate(new Date()); this.sectionDAO.save(section); } public void removeIndicator(String id) { this.sectionDAO.findByIndicatorsContaining(id).forEach(section -> { section.removeIndicator(id); + section.setUpdateDate(new Date()); this.sectionDAO.save(section); }); } diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/service/SectionService.java b/src/main/java/eu/dnetlib/uoamonitorservice/service/SectionService.java index 4ffe51c..3c66a20 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/service/SectionService.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/service/SectionService.java @@ -2,16 +2,21 @@ package eu.dnetlib.uoamonitorservice.service; import eu.dnetlib.uoaadmintoolslibrary.handlers.ForbiddenException; import eu.dnetlib.uoamonitorservice.dao.*; +import eu.dnetlib.uoamonitorservice.dto.MoveIndicator; import eu.dnetlib.uoamonitorservice.dto.SectionFull; -import eu.dnetlib.uoamonitorservice.entities.*; +import eu.dnetlib.uoamonitorservice.dto.SubCategoryFull; +import eu.dnetlib.uoamonitorservice.entities.Indicator; +import eu.dnetlib.uoamonitorservice.entities.Section; +import eu.dnetlib.uoamonitorservice.entities.Stakeholder; +import eu.dnetlib.uoamonitorservice.entities.SubCategory; import eu.dnetlib.uoamonitorservice.generics.Common; import eu.dnetlib.uoamonitorservice.handlers.EntityNotFoundException; import eu.dnetlib.uoamonitorservice.handlers.PathNotValidException; -import eu.dnetlib.uoamonitorservice.primitives.ReorderEvent; import eu.dnetlib.uoamonitorservice.primitives.Visibility; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import java.util.Date; import java.util.HashSet; import java.util.List; import java.util.stream.Collectors; @@ -70,7 +75,10 @@ public class SectionService { public Section save(Section section) { if (section.getId() != null) { section.setIndicators(this.find(section.getId()).getIndicators()); + } else { + section.setCreationDate(new Date()); } + section.setUpdateDate(new Date()); section.getIndicators().forEach(this.indicatorService::find); return this.dao.save(section); } @@ -130,18 +138,18 @@ public class SectionService { }); } - public SectionFull reorderIndicators(Stakeholder stakeholder, Section section, ReorderEvent event) { + public SectionFull reorderIndicators(Stakeholder stakeholder, Section section, List indicators) { + return this.reorderIndicators(stakeholder, section, indicators, true); + } + + public SectionFull reorderIndicators(Stakeholder stakeholder, Section section, List indicators, boolean reorderChildren) { if (this.commonService.hasEditAuthority(stakeholder.getType(), stakeholder.getAlias())) { - event.getIds().forEach(this.indicatorService::find); - if (event.getAction().equals("added")) { - section.addIndicator(event.getTarget()); - } else if (event.getAction().equals("removed")) { - section.removeIndicator(event.getTarget()); - } - if (section.getIndicators().size() == event.getIds().size() && new HashSet<>(section.getIndicators()).containsAll(event.getIds())) { - section.setIndicators(event.getIds()); - if(event.getAction().equals("moved")) { - this.reorderChildren(stakeholder, section, event); + indicators.forEach(this.indicatorService::find); + if (section.getIndicators().size() == indicators.size() && new HashSet<>(section.getIndicators()).containsAll(indicators)) { + section.setIndicators(indicators); + section.setUpdateDate(new Date()); + if(reorderChildren) { + this.reorderChildren(stakeholder, section, indicators); } return this.getFullSection(stakeholder.getType(), stakeholder.getAlias(), this.dao.save(section)); } else { @@ -152,30 +160,11 @@ public class SectionService { } } - public void reorderChildren(Stakeholder defaultStakeholder, Section defaultSection, ReorderEvent event) { + public void reorderChildren(Stakeholder defaultStakeholder, Section defaultSection, List defaultIndicators) { this.stakeholderDAO.findByDefaultId(defaultStakeholder.getId()).forEach(stakeholder -> { this.dao.findByDefaultId(defaultSection.getId()).stream().map(section -> this.getFullSection(stakeholder.getType(), stakeholder.getAlias(), section)).forEach(section -> { - Indicator target; - if(section.isNumber()) { - target = this.subCategoryDAO.findByNumbersContaining(section.getId()).stream().flatMap(subCategory -> subCategory.getNumbers().stream()) - .map(id -> this.getFullSection(stakeholder.getType(), stakeholder.getAlias(), id)).flatMap(sectionFull -> section.getIndicators().stream()) - .filter(indicator -> indicator.getDefaultId().equals(event.getTarget())).findFirst().orElse(null); - } else { - target = this.subCategoryDAO.findByChartsContaining(section.getId()).stream().flatMap(subCategory -> subCategory.getCharts().stream()) - .map(id -> this.getFullSection(stakeholder.getType(), stakeholder.getAlias(), id)).flatMap(sectionFull -> section.getIndicators().stream()) - .filter(indicator -> indicator.getDefaultId().equals(event.getTarget())).findFirst().orElse(null); - } - if (target != null) { - List indicators = section.getIndicators().stream().map(indicator -> (Common) indicator).collect(Collectors.toList()); - if(event.getAction().equals("removed")) { - indicators.removeIf(indicator -> indicator.getId().equals(target.getId())); - } else if(event.getAction().equals("added")) { - indicators.add(target); - } - List ids = this.commonService.reorder(event.getIds(), indicators); - ReorderEvent childEvent = new ReorderEvent(event.getAction(), target.getId(), ids); - this.reorderIndicators(stakeholder, new Section(section), childEvent); - } + this.reorderIndicators(stakeholder, new Section(section), + this.commonService.reorder(defaultIndicators, section.getIndicators().stream().map(indicator -> (Common) indicator).collect(Collectors.toList()))); }); }); } @@ -212,16 +201,19 @@ public class SectionService { } else { subCategory.addChart(id, index); } + subCategory.setUpdateDate(new Date()); this.subCategoryDAO.save(subCategory); } public void removeSection(String id) { this.subCategoryDAO.findByNumbersContaining(id).forEach(subCategory -> { subCategory.removeNumber(id); + subCategory.setUpdateDate(new Date()); this.subCategoryDAO.save(subCategory); }); this.subCategoryDAO.findByChartsContaining(id).forEach(subCategory -> { subCategory.removeChart(id); + subCategory.setUpdateDate(new Date()); this.subCategoryDAO.save(subCategory); }); } diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/service/StakeholderService.java b/src/main/java/eu/dnetlib/uoamonitorservice/service/StakeholderService.java index c38c81e..47b085b 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/service/StakeholderService.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/service/StakeholderService.java @@ -10,6 +10,7 @@ import eu.dnetlib.uoamonitorservice.primitives.Visibility; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import java.util.Date; import java.util.HashSet; import java.util.List; import java.util.stream.Collectors; @@ -99,7 +100,10 @@ public class StakeholderService { public Stakeholder save(Stakeholder stakeholder) { if (stakeholder.getId() != null) { stakeholder.setTopics(this.find(stakeholder.getId()).getTopics()); + } else { + stakeholder.setCreationDate(new Date()); } + stakeholder.setUpdateDate(new Date()); stakeholder.getTopics().forEach(this.topicService::find); return this.dao.save(stakeholder); } @@ -109,6 +113,7 @@ public class StakeholderService { topics.forEach(this.topicService::find); if (stakeholder.getTopics().size() == topics.size() && new HashSet<>(stakeholder.getTopics()).containsAll(topics)) { stakeholder.setTopics(topics); + stakeholder.setUpdateDate(new Date()); this.reorderChildren(stakeholder, topics); return this.getFullStakeholder(this.dao.save(stakeholder)); } else { diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/service/SubCategoryService.java b/src/main/java/eu/dnetlib/uoamonitorservice/service/SubCategoryService.java index ff3141e..a177862 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/service/SubCategoryService.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/service/SubCategoryService.java @@ -5,18 +5,21 @@ import eu.dnetlib.uoamonitorservice.dao.CategoryDAO; import eu.dnetlib.uoamonitorservice.dao.StakeholderDAO; import eu.dnetlib.uoamonitorservice.dao.SubCategoryDAO; import eu.dnetlib.uoamonitorservice.dao.TopicDAO; +import eu.dnetlib.uoamonitorservice.dto.MoveIndicator; +import eu.dnetlib.uoamonitorservice.dto.SectionFull; +import eu.dnetlib.uoamonitorservice.dto.SectionInfo; import eu.dnetlib.uoamonitorservice.dto.SubCategoryFull; -import eu.dnetlib.uoamonitorservice.entities.Category; -import eu.dnetlib.uoamonitorservice.entities.Stakeholder; -import eu.dnetlib.uoamonitorservice.entities.SubCategory; +import eu.dnetlib.uoamonitorservice.entities.*; import eu.dnetlib.uoamonitorservice.generics.Common; import eu.dnetlib.uoamonitorservice.handlers.EntityNotFoundException; import eu.dnetlib.uoamonitorservice.handlers.PathNotValidException; import eu.dnetlib.uoamonitorservice.primitives.IndicatorType; import eu.dnetlib.uoamonitorservice.primitives.Visibility; +import io.swagger.models.auth.In; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import java.util.Date; import java.util.HashSet; import java.util.List; import java.util.stream.Collectors; @@ -83,7 +86,10 @@ public class SubCategoryService { SubCategory old = this.find(subCategory.getId()); subCategory.setNumbers(old.getNumbers()); subCategory.setCharts(old.getCharts()); + } else { + subCategory.setCreationDate(new Date()); } + subCategory.setUpdateDate(new Date()); subCategory.getNumbers().forEach(this.sectionService::find); subCategory.getCharts().forEach(this.sectionService::find); return this.dao.save(subCategory); @@ -128,11 +134,49 @@ public class SubCategoryService { }); } + public SubCategoryFull moveIndicator(Stakeholder stakeholder, SubCategory subCategory, MoveIndicator moveIndicator) { + if (this.commonService.hasEditAuthority(stakeholder.getType(), stakeholder.getAlias())) { + Section from = this.sectionService.findByPath(subCategory, moveIndicator.getFrom().getId()); + Section to = this.sectionService.findByPath(subCategory, moveIndicator.getTo().getId()); + from.removeIndicator(moveIndicator.getTarget()); + to.addIndicator(moveIndicator.getTarget()); + this.sectionService.reorderIndicators(stakeholder, from, moveIndicator.getFrom().getIndicators(), false); + this.sectionService.reorderIndicators(stakeholder, to, moveIndicator.getTo().getIndicators(), false); + this.moveIndicatorChildren(stakeholder, subCategory, moveIndicator); + return this.getFullSubCategory(stakeholder.getType(), stakeholder.getAlias(), subCategory); + } else { + throw new ForbiddenException("You are not authorized to move indicators in subCategory with id: " + subCategory.getId()); + } + } + + public void moveIndicatorChildren(Stakeholder defaultStakeholder, SubCategory defaultSubCategory, MoveIndicator moveIndicator) { + this.stakeholderDAO.findByDefaultId(defaultStakeholder.getId()).forEach(stakeholder -> { + this.dao.findByDefaultId(defaultSubCategory.getId()).stream() + .map(subCategory -> this.getFullSubCategory(stakeholder.getType(), stakeholder. getAlias(), subCategory)) + .collect(Collectors.toList()).forEach(subCategory -> { + SectionFull from = subCategory.getSectionByDefaultId(moveIndicator.getFrom().getId()).orElse(null); + SectionFull to = subCategory.getSectionByDefaultId(moveIndicator.getTo().getId()).orElse(null); + if(from != null && to != null) { + Indicator target = from.getIndicatorByDefaultId(moveIndicator.getTarget()).orElse(null); + if(target != null) { + from.removeIndicator(target.getId()); + to.addIndicator(target); + MoveIndicator moveIndicatorChild = new MoveIndicator(target.getId(), + new SectionInfo(from.getId(), this.commonService.reorder(moveIndicator.getFrom().getIndicators(), from.getIndicators().stream().map(section -> (Common) section).collect(Collectors.toList()))), + new SectionInfo(to.getId(), this.commonService.reorder(moveIndicator.getTo().getIndicators(), to.getIndicators().stream().map(section -> (Common) section).collect(Collectors.toList())))); + this.moveIndicator(stakeholder, new SubCategory(subCategory), moveIndicatorChild); + } + } + }); + }); + } + public SubCategoryFull reorderNumbers(Stakeholder stakeholder, SubCategory subCategory, List numbers) { if(this.commonService.hasEditAuthority(stakeholder.getType(), stakeholder.getAlias())) { numbers.forEach(this.sectionService::find); if (subCategory.getNumbers().size() == numbers.size() && new HashSet<>(subCategory.getNumbers()).containsAll(numbers)) { subCategory.setNumbers(numbers); + subCategory.setUpdateDate(new Date()); this.reorderChildrenNumbers(stakeholder, subCategory, numbers); return this.getFullSubCategory(stakeholder.getType(), stakeholder.getAlias(), this.dao.save(subCategory)); } else { @@ -204,12 +248,14 @@ public class SubCategoryService { public void addSubCategory(Category category, String id) { category.addSubCategory(id); + category.setUpdateDate(new Date()); this.categoryDAO.save(category); } public void removeSubCategory(String id) { this.categoryDAO.findBySubCategoriesContaining(id).forEach(category -> { category.removeSubCategory(id); + category.setUpdateDate(new Date()); this.categoryDAO.save(category); }); } diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/service/TopicService.java b/src/main/java/eu/dnetlib/uoamonitorservice/service/TopicService.java index a5a4c89..4f2e267 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/service/TopicService.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/service/TopicService.java @@ -13,6 +13,7 @@ import eu.dnetlib.uoamonitorservice.primitives.Visibility; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import java.util.Date; import java.util.HashSet; import java.util.List; import java.util.stream.Collectors; @@ -69,7 +70,10 @@ public class TopicService { public Topic save(Topic topic) { if(topic.getId() != null) { topic.setCategories(this.find(topic.getId()).getCategories()); + } else { + topic.setCreationDate(new Date()); } + topic.setUpdateDate(new Date()); topic.getCategories().forEach(this.categoryService::find); return this.dao.save(topic); } @@ -113,6 +117,7 @@ public class TopicService { if (topic.getCategories().size() == categories.size() && new HashSet<>(topic.getCategories()).containsAll(categories)) { topic.setCategories(categories); this.reorderChildren(stakeholder, topic, categories); + topic.setUpdateDate(new Date()); return this.getFullTopic(stakeholder.getType(), stakeholder.getAlias(), this.dao.save(topic)); } else { throw new EntityNotFoundException("Some categories dont exist in the topic with id " + topic.getId()); @@ -155,12 +160,14 @@ public class TopicService { public void addTopic(Stakeholder stakeholder, String id) { stakeholder.addTopic(id); + stakeholder.setUpdateDate(new Date()); this.stakeholderDAO.save(stakeholder); } public void removeTopic(String id) { this.stakeholderDAO.findByTopicsContaining(id).forEach(stakeholder -> { stakeholder.removeTopic(id); + stakeholder.setUpdateDate(new Date()); this.stakeholderDAO.save(stakeholder); }); }