Compare commits

...

31 Commits

Author SHA1 Message Date
Konstantinos Triantafyllou aaf3338e11 Json Ignore for umbrella optional 2024-06-12 12:36:17 +03:00
Konstantinos Triantafyllou 8f793d31ad Update admin tools library 2024-06-12 12:24:41 +03:00
Konstantinos Triantafyllou 00b17fad1c [umbrella]: Add reorder methods in updateUmbrella. 2024-06-12 01:42:49 +03:00
Konstantinos Triantafyllou 1bc576ea69 [umbrella]: Fix delete stakeholder in order to remove from umbrella dashboards when it is deleted. 2024-06-11 20:17:39 +03:00
Konstantinos Triantafyllou ab33c2ec45 [umbrella]: Add method to update umbrella of a stakeholder. 2024-06-11 17:38:39 +03:00
Konstantinos Triantafyllou 615124b6b2 Remove some unused imports. 2024-06-05 12:42:26 +03:00
Konstantinos Triantafyllou 0d50ce9dd2 [umbrella]: Add script to add standalone field with default value true. 2024-06-03 11:47:25 +03:00
Konstantinos Triantafyllou 4f1d1b89f4 merge changes from master 2024-05-30 22:59:55 +03:00
Konstantinos Triantafyllou 4325e1fdb0 Fix getFullStakeholder by removing visibility authority. 2024-05-30 22:54:50 +03:00
Konstantinos Triantafyllou aa1cab1ccb Merge remote-tracking branch 'origin/master' into umbrella 2024-05-27 13:49:37 +03:00
Konstantinos Triantafyllou a73db0e081 Add script to initialize copy to default value. 2024-05-27 13:48:41 +03:00
Konstantinos Triantafyllou c15b63d3da Add method to get child stakeholder and change manageStakeholder to return object of different types of stakeholders. 2024-05-24 13:40:14 +03:00
Konstantinos Triantafyllou 0fdb13ae56 Add unauthorized method in common service and use it instead of ForbiddenException 2024-05-23 10:50:09 +03:00
Konstantinos Triantafyllou 014ca643d5 Fix save stakeholder bug, where topics will be deleted on save. 2024-04-24 23:23:27 +03:00
Konstantinos Triantafyllou 6b5ae3512f Add copy true in dao when find profiles with specific defaultId. 2024-04-15 10:42:39 +03:00
Konstantinos Triantafyllou c4d33a2486 Comment use database in scripts 2024-04-12 11:47:43 +03:00
Konstantinos Triantafyllou 86f4990d70 Introduce copy field for stakeholder and align all methods to appy the right structure. 2024-04-12 11:37:28 +03:00
Konstantinos Triantafyllou 54f9ebb9ce Add copy constructor in stakeholder class. 2024-04-10 15:54:03 +03:00
Konstantinos Triantafyllou 8eb99c8ae1 Merge pull request 'Merge refactor code and change the implementation of reordering in order to transmit changes also in the children of a profile.' (#2) from new-model into master
Reviewed-on: #2
2024-04-10 14:42:38 +02:00
Konstantinos Triantafyllou dc383dda0d [new-model]: Add move indicator controller in order to move an indicator from a section to another. 2024-04-06 12:40:52 +03:00
Konstantinos Triantafyllou eee0814016 [new-model]: Reorder for move action for all entities. Doesn't work for added and removed, need a different implementation. 2024-04-02 22:26:57 +03:00
Konstantinos Triantafyllou c83dc441d4 Refactor reorder methods and save sections in bulk 2024-04-02 14:49:02 +03:00
Konstantinos Triantafyllou 8a56c3ad8b Save category, subcategory, section refactor. 2024-03-27 14:07:41 +02:00
Konstantinos Triantafyllou c3cc942332 Refactor save topic endpoint and fix change visibility. 2024-03-21 12:42:34 +02:00
Konstantinos Triantafyllou cf505bd214 Fix delete and change visibility methods for all entities. 2024-03-14 19:01:28 +02:00
Konstantinos Triantafyllou cb7780b84e [new-model]: Resolve merge from master 2024-03-13 00:23:44 +02:00
Konstantinos Triantafyllou 959d553ca4 [new-model]: Refactor all delete and change visibility methods. 2024-03-13 00:20:55 +02:00
Konstantinos Triantafyllou e345f5b689 [new-model | WIP]: Refactoring the code in order to improve performance and code. 2024-03-08 20:55:51 +02:00
Konstantinos Triantafyllou 1158b676e4 Revert properties. Remove some comments. Remove mongodb DAOs. Move primitives classes and enums to new package primitives. 2024-03-06 01:48:31 +02:00
Konstantinos Triantafyllou 76c39d6eb3 Add monitor deploy service 2024-03-06 01:27:38 +02:00
Konstantinos Triantafyllou 8918826766 Remove comments. Add getAliases method in stakeholderService. Remove parametersMapping 2024-03-05 18:51:14 +02:00
79 changed files with 3969 additions and 5301 deletions

12
pom.xml
View File

@ -30,7 +30,7 @@
<dependency> <!-- this dependency includes dependency to uoa-authorization-library -->
<groupId>eu.dnetlib</groupId>
<artifactId>uoa-admin-tools-library</artifactId>
<version>1.0.10</version>
<version>1.0.13</version>
</dependency>
<dependency>
<groupId>eu.dnetlib</groupId>
@ -61,10 +61,6 @@
<configuration>
<mainClass>eu.dnetlib.uoamonitorservice.application.UoaMonitorServiceApplication</mainClass>
<executable>true</executable>
<excludes>
<!-- <packagingExcludes>**/eu/dnetlib/uoamonitorservice/controllers/MonitorLibraryCheckDeployController.class</packagingExcludes>-->
<!-- <exclude>**/eu/dnetlib/uoamonitorservice/controllers/MonitorLibraryCheckDeployController.class</exclude>-->
</excludes>
</configuration>
<executions>
<execution>
@ -73,12 +69,6 @@
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<excludes>
<!-- <packagingExcludes>**/eu/dnetlib/uoamonitorservice/controllers/MonitorLibraryCheckDeployController.class</packagingExcludes>-->
<!-- <exclude>**/eu/dnetlib/uoamonitorservice/controllers/MonitorLibraryCheckDeployController.class</exclude>-->
</excludes>
</configuration>
</execution>
</executions>
</plugin>

243
scripts/2020-10-22.js Normal file
View File

@ -0,0 +1,243 @@
function upperCaseEnumValues() {
stakeholders = db.stakeholder.find().map(function (stakeholders) {
return stakeholders;
});
for(var i=0; i<stakeholders.length; i++) {
stakeholder = stakeholders[i];
stakeholderType = stakeholder.type;
stakeholder.type = stakeholderType.toUpperCase();
db.stakeholder.save(stakeholder);
}
print("Uppercase enum values for Stakeholder types");
sections = db.section.find().map(function (sections) {
return sections;
});
for(var i=0; i<sections.length; i++) {
section = sections[i];
sectionType = section.type;
section.type = sectionType.toUpperCase();
db.section.save(section);
}
print("Uppercase enum values for Section types");
indicators = db.indicator.find().map(function (indicators) {
return indicators;
});
for(var i=0; i<indicators.length; i++) {
indicator = indicators[i];
indicatorType = indicator.type;
indicator.type = indicatorType.toUpperCase();
indicatorWidth = indicator.width;
indicator.width = indicatorWidth.toUpperCase();
indicatorPaths = indicator.indicatorPaths;
for(var j=0; j<indicatorPaths.lenght; j++) {
indicatorPath = indicatorPaths[j];
indicatorPathType = indicatorPath.type;
indicatorPath.type = indicatorPathType.toUpperCase();
indicatorPathSource = indicatorPath.source;
indicatorPath.source = indicatorPathSource.toUpperCase();
}
db.indicator.save(indicator);
}
print("Uppercase enum values for Indicator types and width");
print("Uppercase enum values for Indicator path types and source");
}
function addHeightInIndicators() {
indicators = db.indicator.find().map(function (indicators) {
return indicators;
});
for(var i=0; i<indicators.length; i++) {
indicator = indicators[i];
if(indicator.type == "chart" || indicator.type == "CHART") {
indicator['height'] = "MEDIUM";
} else {
indicator['height'] = "SMALL";
}
db.indicator.save(indicator);
}
print("Height field added in Indicators");
}
function addVisibility() {
stakeholders = db.stakeholder.find().map(function (stakeholders) {
return stakeholders;
});
for (var i = 0; i < stakeholders.length; i++) {
stakeholder = stakeholders[i];
if (stakeholder.isActive) {
if (stakeholder.isPublic) {
stakeholder['visibility'] = "PUBLIC";
} else {
stakeholder['visibility'] = "RESTRICTED";
}
} else {
stakeholder['visibility'] = "PRIVATE";
}
db.stakeholder.save(stakeholder);
}
print("Add visibility field for Stakeholders");
topics = db.topic.find().map(function (topics) {
return topics;
});
for (var i = 0; i < topics.length; i++) {
topic = topics[i];
topicVisibility = "PRIVATE";
if (topic.isActive) {
if (topic.isPublic) {
topicVisibility = "PUBLIC";
} else {
topicVisibility = "RESTRICTED";
}
}
topic['visibility'] = topicVisibility;
db.topic.save(topic);
}
print("Add visibility field for Topics");
categories = db.category.find().map(function (categories) {
return categories;
});
for (var i = 0; i < categories.length; i++) {
category = categories[i];
if (category.isActive) {
if (category.isPublic) {
category['visibility'] = "PUBLIC";
} else {
category['visibility'] = "RESTRICTED";
}
} else {
category['visibility'] = "PRIVATE";
}
db.category.save(category);
}
print("Add visibility field for Categories");
subCategories = db.subCategory.find().map(function (subCategories) {
return subCategories;
});
for (var i = 0; i < subCategories.length; i++) {
subCategory = subCategories[i];
if (subCategory.isActive) {
if (subCategory.isPublic) {
subCategory['visibility'] = "PUBLIC";
} else {
subCategory['visibility'] = "RESTRICTED";
}
} else {
subCategory['visibility'] = "PRIVATE";
}
db.subCategory.save(subCategory);
}
print("Add visibility field for SubCategories");
indicators = db.indicator.find().map(function (indicators) {
return indicators;
});
for (var i = 0; i < indicators.length; i++) {
indicator = indicators[i];
if (indicator.isActive) {
if (indicator.isPublic) {
indicator['visibility'] = "PUBLIC";
} else {
indicator['visibility'] = "RESTRICTED";
}
} else {
indicator['visibility'] = "PRIVATE";
}
db.indicator.save(indicator);
}
print("Add visibility field for Indicators");
}
function removeIsActiveAndIsPublic() {
stakeholders = db.stakeholder.find().map(function (stakeholders) {
return stakeholders;
});
for(var i=0; i<stakeholders.length; i++) {
stakeholder = stakeholders[i];
db.stakeholder.update({"_id": stakeholder._id}, {$unset: {isPublic: "", isActive: ""}});
}
topics = db.topic.find().map(function (topics) {
return topics;
});
for(var i=0; i<topics.length; i++) {
topic = topics[i];
db.topic.update({"_id": topic._id}, {$unset: {isPublic: "", isActive: ""}});
}
categories = db.category.find().map(function (categories) {
return categories;
});
for(var i=0; i<categories.length; i++) {
category = categories[i];
db.category.update({"_id": category._id}, {$unset: {isPublic: "", isActive: ""}});
}
subCategories = db.subCategory.find().map(function (subCategories) {
return subCategories;
});
for(var i=0; i<subCategories.length; i++) {
subCategory = subCategories[i];
db.subCategory.update({"_id": subCategory._id}, {$unset: {isPublic: "", isActive: ""}});
}
indicators = db.indicator.find().map(function (indicators) {
return indicators;
});
for(var i=0; i<indicators.length; i++) {
indicator = indicators[i];
db.indicator.update({"_id": indicator._id}, {$unset: {isPublic: "", isActive: ""}});
}
}
function addAdminToolsCollections () {
db.createCollection("portal");
db.createCollection("entity");
db.createCollection("page");
db.createCollection("pageHelpContent");
db.createCollection("divId");
db.createCollection("divHelpContent");
}
function addPortals() {
db.portal.save({"pid": "monitor", "name": "Monitor", "type": "monitor", "piwik": null, "pages": {}, "entities": {}});
}
function uniqueIndexes() {
db.portal.createIndex( { "pid": 1 }, { unique: true } );
db.stakeholder.createIndex( { "alias": 1 }, { unique: true } );
}
upperCaseEnumValues();
addHeightInIndicators();
addVisibility();
removeIsActiveAndIsPublic();
addAdminToolsCollections();
addPortals();
uniqueIndexes();

63
scripts/2021-06-24.js Normal file
View File

@ -0,0 +1,63 @@
function addHomePageInPortalType(portalType) {
homePageID = db.page.insertOne({ "route" : "/", "name" : "Home", "type" : "other", "entities" : [ ], "portalType" : portalType, "top" : false, "bottom" : false, "left" : false, "right" : false }).insertedId.str;
print("Creating Home page with id " + homePageID);
portals = db.portal.find({"type": portalType}).map( function(portal) { return portal; } );
for (var i = 0; i < portals.length; i++) {
portal_pages = portals[i].pages;
portal_pages[homePageID] = true;
portal_pid = portals[i].pid;
db.portal.update({ "pid" : portal_pid },{$set: { "pages": portal_pages}});
print("Add home page with id " + homePageID + " on " + portalType + " " + portal_pid);
}
}
function addFooterDivIdForPortalType(portalType) {
homePageID = db.page.find({"portalType": portalType, "route": "/"}).map( function(portal) { return portal._id.str; } ).toString();
db.divId.save({ "name" : "footer", "pages" : [ homePageID ], "portalType" : portalType });
print("divId 'footer' added for "+portalType+" (home page id: "+homePageID + ")");
}
function addFooterHelpTextForPortalType(portalType) {
footerDivIdID = db.divId.find({"portalType": portalType, "name": "footer"}).map( function(divId) { return divId._id.str; } ).toString();
portals = db.portal.find({"type": portalType}).map( function(portal) { return portal; } );
for (var j = 0; j < portals.length; j++) {
portal = portals[j];
var content = "This OpenAIRE MONITOR dashboard is part of a project that has received funding from the European Union's Horizon 2020 research and innovation programme under grant agreements No. 777541 and 101017452";
if(portal.pid == "egi") {
content = "This OpenAIRE MONITOR dashboard is part of a project that has received funding from the European Union's Horizon 2020 research and innovation programme under grant agreements No. 777541 and 101017452";
} else if(portal.pid == "risis") {
content = "This OpenAIRE MONITOR dashboard is part of a project that has received funding from the European Union's Horizon 2020 research and innovation programme under grant agreements No. 777541 and 101017452 and 824091";
} else if(portal.pid == "sobigdata") {
content = "This OpenAIRE MONITOR dashboard is part of a project that has received funding from the European Union's Horizon 2020 research and innovation programme under grant agreements No. 777541 and 101017452 and 871042";
}
db.divHelpContent.save(
{ "divId" : footerDivIdID,
"portal" : portal._id.str,
"content" : "<p class=\"uk-margin-remove-bottom\"><span style=\"font-size:8pt\">"+content+"</span></p>",
"isActive" : true
}
);
print("div help text for divId 'footer' added for "+portalType +" (id: "+ portal._id + " - pid: " + portal.pid + " - footer divId id: "+footerDivIdID + ")");
}
}
addHomePageInPortalType("funder");
addFooterDivIdForPortalType("funder");
addFooterHelpTextForPortalType("funder");
addHomePageInPortalType("ri");
addFooterDivIdForPortalType("ri");
addFooterHelpTextForPortalType("ri");
addHomePageInPortalType("organization");
addFooterDivIdForPortalType("organization");
addFooterHelpTextForPortalType("organization");

180
scripts/2023-05-30.js Normal file
View File

@ -0,0 +1,180 @@
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")
}
}
statsProfileOfIndicatorsAsVariable();
addFundingStreamInDefaultMSCA();

23
scripts/2024-02-27.js Normal file
View File

@ -0,0 +1,23 @@
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);
});
})
}
function addCopyWithDefaultValue() {
/* Set default profiles with copy: false */
db.stakeholder.updateMany({defaultId: null, copy: {$exists: false}}, {$set: {copy: false}});
/* Set not default profiles with copy: true */
db.stakeholder.updateMany({defaultId: {$ne: null}, copy: {$exists: false}}, {$set: {copy: true}});
}
deleteIndexParameters();
addCopyWithDefaultValue();

6
scripts/2024-05-27.js Normal file
View File

@ -0,0 +1,6 @@
function addStandaloneWithDefaultValue() {
/* Set standalone by default true */
db.stakeholder.updateMany({standalone: {$exists: false}}, {$set: {standalone: true}});
}
addStandaloneWithDefaultValue();

View File

@ -1,6 +1,5 @@
package eu.dnetlib.uoamonitorservice.application;
import eu.dnetlib.uoaadmintoolslibrary.UoaAdminToolsLibraryConfiguration;
import eu.dnetlib.uoaauthorizationlibrary.configuration.AuthorizationConfiguration;
import eu.dnetlib.uoanotificationservice.configuration.NotificationConfiguration;
import org.springframework.boot.SpringApplication;
@ -8,7 +7,8 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.PropertySources;
//uoahelptexts
@SpringBootApplication(scanBasePackages = {"eu.dnetlib.uoamonitorservice"})
@PropertySources({
@PropertySource("classpath:authorization.properties"),

View File

@ -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"))

View File

@ -12,9 +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;
//"uoahelptexts"
@Configuration
@EnableMongoRepositories(basePackages = {"eu.dnetlib.uoamonitorservice.dao", "eu.dnetlib.uoaadmintoolslibrary.dao"})
public class MongoConnection {

View File

@ -1,20 +1,19 @@
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.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
@ -22,205 +21,32 @@ import java.util.List;
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<SubCategory> buildCategory(Category<SubCategory> categoryFull) {
Category<String> category = new Category<>(categoryFull);
List<String> subCategories = new ArrayList<>();
List<SubCategory> subCategoriesFull = new ArrayList<>();
for(SubCategory<Section<Indicator>> subCategory : categoryFull.getSubCategories()) {
SubCategory<Section<Indicator>> 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()")
@RequestMapping(value = "/{stakeholderId}/{topicId}/save", method = RequestMethod.POST)
public Category<SubCategory> saveCategory(@PathVariable("stakeholderId") String stakeholderId,
public CategoryFull saveCategory(@PathVariable("stakeholderId") String stakeholderId,
@PathVariable("topicId") String topicId,
@RequestBody Category<SubCategory> categoryFull) {
@RequestBody CategoryFull categoryFull) {
log.debug("save category");
log.debug("Alias: "+categoryFull.getAlias() + " - Id: "+categoryFull.getId() + " - Stakeholder: "+stakeholderId + " - Topic: "+topicId);
Stakeholder<String> stakeholder = stakeholderDAO.findById(stakeholderId);
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<String> 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<String> topic = topicDAO.findById(topicId);
if(topic != null) {
if(stakeholder.getTopics().contains(topicId)) {
Category<String> category = new Category<>(categoryFull);
Date date = new Date();
category.setUpdateDate(date);
categoryFull.setUpdateDate(date);
List<String> 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<String> subCategory = new SubCategory<>();
subCategory.createOverviewSubCategory(categoryFull);
subCategoryDAO.save(subCategory);
List<SubCategory> 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<String> 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());
}
subCategories = null;
category = null;
} else {
// EXCEPTION - Topic not found in Stakeholder: stakeholder.getAlias();
throw new PathNotValidException("Save category: Topic with id: "+topicId+" not found in Stakeholder: "+stakeholderId);
}
} else {
// EXCEPTION - Topic not found
throw new EntityNotFoundException("Save category: Topic with id: "+topicId+" not found");
}
} else {
// EXCEPTION - Stakeholder not found
throw new EntityNotFoundException("Save category: Stakeholder with id: "+stakeholderId+" not found");
log.debug("Alias: " + categoryFull.getAlias() + " - Id: " + categoryFull.getId() + " - Stakeholder: " + stakeholderId + " - Topic: " + topicId);
Stakeholder stakeholder = stakeholderService.findByPath(stakeholderId);
Topic topic = topicService.findByPath(stakeholder, topicId);
if(categoryFull.getId() != null) {
this.categoryService.findByPath(topic, categoryFull.getId());
}
return categoryFull;
return this.categoryService.save(stakeholder, topic, new Category(categoryFull));
}
public void onSaveDefaultCategory(Category<String> category, String topicId) {
log.debug("On save default category");
List<Topic> topics = topicDAO.findByDefaultId(topicId);
for(Topic topic : topics) {
Category categoryNew = new Category();
categoryNew.copyFromDefault(category);
categoryDAO.save(categoryNew);
List<String> 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());
}
public void onUpdateDefaultCategory(Category category, Category oldCategory) {
log.debug("On update default category");
List<Category> 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) {
// break;
continue;
}
// categoryBasedOnDefault.setName(category.getName());
// categoryBasedOnDefault.setDescription(category.getDescription());
categoryBasedOnDefault.setUpdateDate(category.getUpdateDate());
categoryDAO.save(categoryBasedOnDefault);
}
}
@PreAuthorize("isAuthenticated()")
@RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/delete", method = RequestMethod.DELETE)
@ -229,343 +55,37 @@ public class CategoryController {
@PathVariable("categoryId") String categoryId,
@RequestParam(required = false) String children) {
log.debug("delete category");
log.debug("Id: "+categoryId + " - Stakeholder: "+stakeholderId + " - Topic: "+topicId);
Stakeholder<String> 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<String> topic = topicDAO.findById(topicId);
if(topic != null) {
if(stakeholder.getTopics().contains(topicId)) {
Category<String> 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<String> 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);
}
// for(String subCategoryId : category.getSubCategories()) {
// SubCategory<String> subcategory = subCategoryDAO.findById(subCategoryId);
// if(subcategory == null) {
// // EXCEPTION - SubCategory not found
// throw new EntityNotFoundException("Delete category: SubCategory with id: "+subCategoryId+" not found (subcategory exists in category: "+categoryId+")");
// }
//
// for(String chartSectionId : subcategory.getCharts()) {
// Section<String> chartSection = sectionDAO.findById(chartSectionId);
// if (chartSection == null) {
// // EXCEPTION - Section not found
// throw new EntityNotFoundException("Delete topic: Section with id: "+chartSectionId+" not found (section exists in subcategory: "+subCategoryId+")");
// }
//
// for (String chartId : chartSection.getIndicators()) {
// indicatorDAO.delete(chartId);
// }
// subcategory.setCharts(null);
// sectionDAO.delete(chartSectionId);
// }
//
// for(String numberSectionId : subcategory.getNumbers()) {
// Section<String> numberSection = sectionDAO.findById(numberSectionId);
// if (numberSection == null) {
// // EXCEPTION - Section not found
// throw new EntityNotFoundException("Delete topic: Section with id: "+numberSectionId+" not found (section exists in subcategory: "+subCategoryId+")");
// }
//
// for (String numberId : numberSection.getIndicators()) {
// indicatorDAO.delete(numberId);
// }
// subcategory.setNumbers(null);
// sectionDAO.delete(numberSectionId);
// }
//
// subCategoryDAO.delete(subCategoryId);
// }
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");
}
return true;
}
public boolean onDeleteDefaultCategory(String defaultCategoryId, String defaultTopicId, String children) {
if(children.equals("delete")) {
List<Topic> topics = topicDAO.findByDefaultId(defaultTopicId);
List<Category> categories = categoryDAO.findByDefaultId(defaultCategoryId);
for(Topic topic : topics) {
Iterator<Category> 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<Category> 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!");
}
}
log.debug("Id: " + categoryId + " - Stakeholder: " + stakeholderId + " - Topic: " + topicId);
Stakeholder stakeholder = this.stakeholderService.findByPath(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()")
@RequestMapping(value = "/{stakeholderId}/{topicId}/reorder", method = RequestMethod.POST)
public List<Category> reorderCategories(@PathVariable("stakeholderId") String stakeholderId,
@PathVariable("topicId") String topicId,
@RequestBody List<String> categories) {
public List<CategoryFull> reorderCategories(@PathVariable("stakeholderId") String stakeholderId,
@PathVariable("topicId") String topicId,
@RequestBody List<String> categories) {
log.debug("reorder categories");
log.debug("Stakeholder: "+stakeholderId + " - Topic: "+topicId);
Topic<String> topic = checkForExceptions(stakeholderId, topicId);
List<String> oldCategories = topic.getCategories();
for (String categoryId : oldCategories) {
if (!categories.contains(categoryId)) {
categories.add(categoryId);
}
}
topic.setCategories(categories);
List<Category> 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;
log.debug("Stakeholder: " + stakeholderId + " - Topic: " + topicId);
Stakeholder stakeholder = stakeholderService.findByPath(stakeholderId);
Topic topic = topicService.findByPath(stakeholder, topicId);
return this.topicService.reorderCategories(stakeholder, topic, categories).getCategories();
}
// @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,
@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<String> 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<String> 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<String> category = categoryDAO.findById(categoryId);
if (category == null) {
// EXCEPTION - Category not found
throw new EntityNotFoundException("Change category visibility: Category with id: "+categoryId+" not found");
}
Category<SubCategory> categoryFull = new Category(category);
List<SubCategory> 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<String> 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<String> 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<String> 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<String> 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);
}
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 = this.stakeholderService.findByPath(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);
}
}

View File

@ -10,28 +10,30 @@ 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 = "*")
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 {
public Boolean contact(@RequestBody EmailRecaptcha form) throws InvalidReCaptchaException {
verifyRecaptcha.processResponse(form.getRecaptcha());
Email email = form.getEmail();
ArrayList<String> 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,18 +42,18 @@ public class EmailController {
String successString = "success";
String failureString = "failure";
Map<String, ArrayList<String>> mailResults = new HashMap<>();
boolean bcc = (optional.isPresent())?optional.get():true;
for(String userMail:email.getRecipients()){
boolean bcc = optional.orElse(true);
for (String userMail : email.getRecipients()) {
ArrayList<String> 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);

View File

@ -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;
}
@ -53,23 +45,8 @@ public class MonitorController {
@RequestMapping(value = "/delete", method = RequestMethod.POST)
public Boolean deletePortals(@RequestBody List<String> portals) {
for (String id: portals) {
String pid = portalService.deletePortal(id);
// layoutService.deleteByPid(pid);
}
portals.forEach(id -> portalService.deletePortal(id));
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);
// }
}

View File

@ -1,42 +1,27 @@
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
@CrossOrigin(origins = "*")
@RequestMapping("/monitor-library")
//@ConditionalOnBean(UoaMonitorServiceApplication.class)
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() {
@ -47,34 +32,6 @@ public class MonitorLibraryCheckDeployController {
@PreAuthorize("hasAnyAuthority(@AuthorizationService.PORTAL_ADMIN)")
@RequestMapping(value = "/health_check/advanced", method = RequestMethod.GET)
public Map<String, String> checkEverything() {
Map<String, String> 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]");
// response.put("Define also", "monitorservice.mongodb.username, monitorservice.mongodb.password");
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();
}
}

View File

@ -1,22 +1,14 @@
package eu.dnetlib.uoamonitorservice.controllers;
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 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.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
@ -24,14 +16,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,34 +31,6 @@ public class MonitorServiceCheckDeployController {
@PreAuthorize("hasAnyAuthority(@AuthorizationService.PORTAL_ADMIN)")
@RequestMapping(value = "/health_check/advanced", method = RequestMethod.GET)
public Map<String, String> checkEverything() {
Map<String, String> 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]");
// response.put("Define also", "monitorservice.mongodb.username, monitorservice.mongodb.password");
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();
}
}

View File

@ -1,20 +1,17 @@
package eu.dnetlib.uoamonitorservice.controllers;
import eu.dnetlib.uoaadmintoolslibrary.handlers.utils.RolesUtils;
import eu.dnetlib.uoamonitorservice.dao.*;
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.EntityNotFoundException;
import eu.dnetlib.uoaadmintoolslibrary.handlers.ForbiddenException;
import eu.dnetlib.uoamonitorservice.handlers.PathNotValidException;
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.Iterator;
import java.util.List;
@RestController
@ -22,203 +19,40 @@ import java.util.List;
public class SectionController {
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;
@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<Indicator> buildSection(Section<Indicator> sectionFull) {
Section<String> section = new Section<>(sectionFull);
List<String> indicators = new ArrayList<>();
List<Indicator> indicatorsFull = new ArrayList<>();
for(Indicator chart : sectionFull.getIndicators()) {
Indicator chartSaved = indicatorDAO.save(chart);
chart.setId(chartSaved.getId());
indicatorsFull.add(chart);
indicators.add(chartSaved.getId());
}
sectionFull.setIndicators(indicatorsFull);
section.setIndicators(indicators);
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;
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 Section saveSection(@PathVariable("stakeholderId") String stakeholderId,
@PathVariable("topicId") String topicId,
@PathVariable("categoryId") String categoryId,
@PathVariable("subcategoryId") String subcategoryId,
@PathVariable("index") String index,
@RequestBody Section<Indicator> sectionFull) {
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<String> subCategory = checkForExceptions(stakeholderId, topicId, categoryId, subcategoryId);
Section<String> section = new Section<>(sectionFull);
Date date = new Date();
section.setUpdateDate(date);
sectionFull.setUpdateDate(date);
List<String> indicators = new ArrayList<>();
Section<String> oldSection = null;
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);
SubCategory subCategory = this.subCategoryService.findByPath(category, subcategoryId);
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<String> stakeholder = stakeholderDAO.findById(stakeholderId);
// this section belongs in default profile and it is new or it is updated
if(stakeholder.getDefaultId() == null) {
if(sectionId == null) {
sectionDAO.save(section);
onSaveDefaultSection(section, topicId, categoryId, subcategoryId, stakeholder);
}
else {
onUpdateDefaultSection(section, stakeholder, oldSection);
sectionDAO.save(section);
}
} else {
sectionDAO.save(section);
}
List<String> sections = null;
if(sectionFull.getType().equals("chart")) {
sections = subCategory.getCharts();
} else if(sectionFull.getType().equals("number")) {
sections = subCategory.getNumbers();
}
int existing_index = sections.indexOf(section.getId());
if (existing_index == -1) {
if(Integer.parseInt(index) != -1) {
sections.add(Integer.parseInt(index), section.getId());
} else {
sections.add(section.getId());
}
subCategoryDAO.save(subCategory);
log.debug("Section saved!");
sectionFull.setId(section.getId());
}
return sectionFull;
}
public void onSaveDefaultSection(Section section,
String defaultTopicId, String defaultCategoryId,
String defaultSubcategoryId, Stakeholder defaultStakeholder) {
log.debug("On save default section");
// new section in default profile - add it on profiles of the same type
List<SubCategory> subCategories = subCategoryDAO.findByDefaultId(defaultSubcategoryId);
for (SubCategory subCategory : subCategories) {
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<String> sections = null;
if (section.getType().equals("chart")) {
sections = subCategory.getCharts();
} else if (section.getType().equals("number")) {
sections = subCategory.getNumbers();
}
sections.add(sectionNew.getId());
subCategoryDAO.save(subCategory);
}
}
public void onUpdateDefaultSection(Section section, Stakeholder stakeholder, 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<Section> 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);
this.sectionService.findByPath(subCategory, sectionFull.getId());
}
return this.sectionService.save(stakeholder, subCategory, new Section(sectionFull), Integer.parseInt(index));
}
@PreAuthorize("isAuthenticated()")
@ -230,331 +64,29 @@ public class SectionController {
@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<String> subCategory = checkForExceptions(stakeholderId, topicId, categoryId, subcategoryId);
Stakeholder<String> 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<String> 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<SubCategory> subCategories = subCategoryDAO.findByDefaultId(defaultSubCategoryId);
List<Section> sections = sectionDAO.findByDefaultId(defaultSectionId);
for(SubCategory subCategory : subCategories) {
Iterator<Section> sectionsIterator = sections.iterator();
while(sectionsIterator.hasNext()) {
Section section = sectionsIterator.next();
String sectionId = section.getId();
List<String> 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<Section> 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!");
}
}
log.debug("Id: " + sectionId + " - 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);
Section section = this.sectionService.findByPath(subCategory, sectionId);
this.sectionService.delete(stakeholder.getType(), section, true);
return true;
}
@PreAuthorize("isAuthenticated()")
@RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/{type}/reorder", method = RequestMethod.POST)
public List<Section> reorderSections(@PathVariable("stakeholderId") String stakeholderId,
@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,
@PathVariable("type") String type,
@RequestBody List<String> sections) {
log.debug("reorder sections of type: "+type);
log.debug("Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId+ " - SubCategory: "+subcategoryId);
SubCategory<String> subCategory = checkForExceptions(stakeholderId, topicId, categoryId, subcategoryId);
if (type.equals("chart")) {
List<String> oldSections = subCategory.getCharts();
for (String sectionId : oldSections) {
if (!sections.contains(sectionId)) {
sections.add(sectionId);
}
}
subCategory.setCharts(sections);
} else if (type.equals("number")) {
List<String> oldSections = subCategory.getNumbers();
for (String sectionId : oldSections) {
if (!sections.contains(sectionId)) {
sections.add(sectionId);
}
}
subCategory.setNumbers(sections);
}
List<Section> 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<String> subCategory = checkForExceptions(stakeholderId, topicId, categoryId, subcategoryId);
List<String> sections = null;
if (section.getType().equals("chart")) {
sections = subCategory.getCharts();
} else if (section.getType().equals("number")) {
sections = subCategory.getNumbers();
}
if(sections.contains(section.getId())) {
sectionDAO.save(section);
log.debug("Section toggled!");
} else {
// EXCEPTION - Section not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); -> Category: category.getAlias(); -> SubCategory: subCategory.getAlias();
throw new PathNotValidException("Toggle section: Section with id: "+section.getId()+" not found in SubCategory: "+subcategoryId);
}
}
private SubCategory checkForExceptions(String stakeholderId, String topicId, String categoryId, String subcategoryId) {
Stakeholder<String> stakeholder = stakeholderDAO.findById(stakeholderId);
if(stakeholder == null) {
// EXCEPTION - Stakeholder not found
throw new EntityNotFoundException("Save indicator: Stakeholder with id: " + stakeholderId + " not found");
}
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<String> topic = topicDAO.findById(topicId);
if(topic == null) {
// EXCEPTION - Topic not found
throw new EntityNotFoundException("Save indicator: Topic with id: "+topicId+" not found");
}
if(!stakeholder.getTopics().contains(topicId)) {
// EXCEPTION - Topic not found in Stakeholder: stakeholder.getAlias();
throw new PathNotValidException("Save indicator: Topic with id: " + topicId + " not found in Stakeholder: " + stakeholderId);
}
Category<String> category = categoryDAO.findById(categoryId);
if(category == null) {
// EXCEPTION - Category not found
throw new EntityNotFoundException("Save indicator: Category with id: "+categoryId+" not found");
}
if(!topic.getCategories().contains(categoryId)) {
// EXCEPTION - Category not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias();
throw new PathNotValidException("Save indicator: Category with id: "+categoryId+" not found in Topic: "+topicId);
}
SubCategory<String> subcategory = subCategoryDAO.findById(subcategoryId);
if(subcategory == null) {
// EXCEPTION - SubCategory not found
throw new EntityNotFoundException("Save indicator: SubCategory with id: "+subcategoryId+" not found");
}
if (!category.getSubCategories().contains(subcategoryId)) {
// EXCEPTION - SubCategory not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); -> Category: category.getAlias();
throw new PathNotValidException("Save indicator: SubCategory with id: "+subcategoryId+" not found in Category: "+categoryId);
}
return subcategory;
}
public void deleteTree(SubCategory subCategory) {
List<String> 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()+")");
}
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()+")");
}
indicatorController.deleteTree(section);
sectionDAO.delete(sectionId);
}
}
public void disConnectTree(SubCategory subCategory) {
List<String> sections = subCategory.getCharts();
for(String sectionId : sections) {
Section section = sectionDAO.findById(sectionId);
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()+")");
}
indicatorController.disConnectTree(section);
section.setDefaultId(null);
sectionDAO.save(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()+")");
}
indicatorController.disConnectTree(section);
section.setDefaultId(null);
sectionDAO.save(section);
}
}
public Section changeVisibilityTree(String sectionId, Visibility visibility) {
Section<String> section = sectionDAO.findById(sectionId);
if (section == null) {
// EXCEPTION - Section not found
throw new EntityNotFoundException("Change section visibility: Section with id: " + sectionId + " not found");
}
Section<Indicator> sectionFull = new Section(section);
List<Indicator> indicatorsFull = new ArrayList();
for (String indicatorId : section.getIndicators()) {
indicatorsFull.add(indicatorController.changeVisibilityTree(indicatorId, null, visibility));
}
sectionFull.setIndicators(indicatorsFull);
return sectionFull;
@RequestBody MoveIndicator moveIndicator) {
log.debug("move indicator");
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);
return this.subCategoryService.moveIndicator(stakeholder, subCategory, moveIndicator);
}
}

View File

@ -1,680 +1,164 @@
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.uoamonitorservice.dto.BuildStakeholder;
import eu.dnetlib.uoamonitorservice.dto.ManageStakeholders;
import eu.dnetlib.uoamonitorservice.dto.StakeholderFull;
import eu.dnetlib.uoamonitorservice.dto.UpdateUmbrella;
import eu.dnetlib.uoamonitorservice.entities.Stakeholder;
import eu.dnetlib.uoamonitorservice.primitives.Umbrella;
import eu.dnetlib.uoamonitorservice.primitives.Visibility;
import eu.dnetlib.uoamonitorservice.service.CommonService;
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;
@RestController
@CrossOrigin(origins = "*")
public class StakeholderController {
private final Logger log = LogManager.getLogger(this.getClass());
@Autowired
private RolesUtils rolesUtils;
private final PortalService portalService;
private final StakeholderService stakeholderService;
private final CommonService commonService;
@Autowired
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, CommonService commonService) {
this.portalService = portalService;
this.stakeholderService = stakeholderService;
this.commonService = commonService;
}
@PreAuthorize("isAuthenticated()")
@RequestMapping(value = "/stakeholder/alias", method = RequestMethod.GET)
public List<String> getAllReservedStakeholderAlias() {
// log.debug("get all stakeholder reserved alias-es");
List<String> stakeholderAlias = new ArrayList<>();
List<Stakeholder> stakeholders = stakeholderDAO.findAll();
if(stakeholders != null) {
stakeholders.forEach(stakeholder -> {
stakeholderAlias.add(stakeholder.getAlias());
});
public List<String> getAllReservedStakeholderAlias(@RequestParam(required = false) String type) {
List<String> stakeholderAlias = this.stakeholderService.getAllAliases(type);
if(type == null ) {
stakeholderAlias.add("all");
stakeholderAlias.add("default");
stakeholderAlias.add("alias");
}
stakeholderAlias.add( "all");
stakeholderAlias.add("default");
stakeholderAlias.add("alias");
return stakeholderAlias;
}
// @PreAuthorize("isAuthenticated()")
@PreAuthorize("hasAnyAuthority(" +
"@AuthorizationService.PORTAL_ADMIN, " +
"@AuthorizationService.curator(#stakeholderFull.getType()))")
"@AuthorizationService.PORTAL_ADMIN, " +
"@AuthorizationService.curator(#buildStakeholder.stakeholder.getType()))")
@RequestMapping(value = "/build-stakeholder", method = RequestMethod.POST)
public Stakeholder<Topic<Category<SubCategory<Section<Indicator>>>>> buildFullStakeholder(@RequestBody Stakeholder<Topic<Category<SubCategory<Section<Indicator>>>>> stakeholderFull) {
public Stakeholder buildStakeholder(@RequestBody BuildStakeholder buildStakeholder) {
Stakeholder stakeholder = buildStakeholder.getStakeholder();
log.debug("build stakeholder");
log.debug("Alias: "+stakeholderFull.getAlias());
Stakeholder<String> stakeholder = new Stakeholder<>(stakeholderFull);
List<String> topics = new ArrayList<>();
List<Topic<Category<SubCategory<Section<Indicator>>>>> topicsFull = new ArrayList<>();
for(Topic topic : stakeholderFull.getTopics()) {
Topic<Category<SubCategory<Section<Indicator>>>> topicFull = topicController.buildTopic(topic);
topicsFull.add(topicFull);
topics.add(topicFull.getId());
}
stakeholderFull.setTopics(topicsFull);
stakeholder.setTopics(topics);
Date date = new Date();
stakeholder.setCreationDate(date);
stakeholder.setUpdateDate(date);
stakeholderFull.setCreationDate(date);
stakeholderFull.setUpdateDate(date);
Stakeholder<String> stakeholderSaved = stakeholderDAO.save(stakeholder);
stakeholderFull.setId(stakeholderSaved.getId());
Portal portal = portalService.getPortal(stakeholderFull.getAlias());
if(portal == null) {
log.debug("Alias: " + stakeholder.getAlias());
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 null;
stakeholder.setStandalone(buildStakeholder.isStandalone());
if (buildStakeholder.isUmbrella()) {
stakeholder.setUmbrella(new Umbrella<>());
}
return this.stakeholderService.buildStakeholder(stakeholder, buildStakeholder.getCopyId());
}
public Stakeholder setFullEntities(Stakeholder<String> stakeholder) {
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<Topic> stakeholderFull = new Stakeholder<>(stakeholder);
List<Topic> topics = new ArrayList<>();
for (String topicId: (List<String>)stakeholder.getTopics()) {
Topic<String> topic = topicDAO.findById(topicId);
if(topic == null) {
// EXCEPTION - Topic not found
throw new EntityNotFoundException("Get stakeholder: Topic with id: "+topicId+" not found (topic exists in stakeholder: "+stakeholder.getId()+")");
}
if((!addAll && topic.getVisibility() == Visibility.PRIVATE)
|| (!addPublicAndRestricted && topic.getVisibility() == Visibility.RESTRICTED)) {
continue;
}
Topic<Category> topicFull = new Topic<Category>(topic);
List<Category> categories = new ArrayList<>();
for(String categoryId : topic.getCategories()) {
Category<String> category = categoryDAO.findById(categoryId);
if(category == null) {
// EXCEPTION - Category not found
throw new EntityNotFoundException("Get stakeholder: Category with id: "+categoryId+" not found (category exists in topic: "+topicId+")");
}
if((!addAll && category.getVisibility() == Visibility.PRIVATE)
|| (!addPublicAndRestricted && category.getVisibility() == Visibility.RESTRICTED)) {
continue;
}
Category<SubCategory> categoryFull = new Category<SubCategory>(category);
List<SubCategory> subCategories = new ArrayList<>();
for(String subCategoryId : category.getSubCategories()) {
SubCategory<String> subCategory = subCategoryDAO.findById(subCategoryId);
if(subCategory == null) {
// EXCEPTION - SubCategory not found
throw new EntityNotFoundException("Get stakeholder: SubCategory with id: "+subCategoryId+" not found (subCategory exists in category: "+categoryId+")");
}
if((!addAll && subCategory.getVisibility() == Visibility.PRIVATE)
|| (!addPublicAndRestricted && subCategory.getVisibility() == Visibility.RESTRICTED)) {
continue;
}
SubCategory subCategoryFull = new SubCategory<Section<Indicator>>(subCategory);
List<Section> sectionsCharts = new ArrayList<>();
for(String sectionId : subCategory.getCharts()) {
sectionsCharts.add(getSectionFull(sectionId, subCategoryId, addAll, addPublicAndRestricted));
}
subCategoryFull.setCharts(sectionsCharts);
List<Section> sectionsNumbers = new ArrayList<>();
for(String sectionId : subCategory.getNumbers()) {
sectionsNumbers.add(getSectionFull(sectionId, subCategoryId, addAll, addPublicAndRestricted));
}
subCategoryFull.setNumbers(sectionsNumbers);
// List<Indicator> charts = new ArrayList<>();
// for(String indicatorId : subCategory.getCharts()) {
// Indicator indicator = indicatorDAO.findById(indicatorId);
// if(indicator == null) {
// // EXCEPTION - Indicator not found
// throw new EntityNotFoundException("Get stakeholder: Indicator with id: "+indicatorId+" not found (indicator exists in subCategory: "+subCategoryId+")");
// }
// charts.add(indicator);
// }
// subCategoryFull.setCharts(charts);
//
// List<Indicator> numbers = new ArrayList<>();
// for (String indicatorId : subCategory.getNumbers()) {
// Indicator indicator = indicatorDAO.findById(indicatorId);
// if (indicator == null) {
// // EXCEPTION - Indicator not found
// throw new EntityNotFoundException("Get stakeholder: Indicator with id: " + indicatorId + " not found (indicator exists in subCategory: " + subCategoryId + ")");
// }
// numbers.add(indicator);
// }
// subCategoryFull.setNumbers(numbers);
subCategories.add(subCategoryFull);
}
categoryFull.setSubCategories(subCategories);
categories.add(categoryFull);
}
topicFull.setCategories(categories);
topics.add(topicFull);
}
stakeholderFull.setTopics(topics);
return stakeholderFull;
}
// private SubCategory setFullSubcategory(SubCategory subCategory) {
// SubCategory subCategoryFull = new SubCategory<Section<Indicator>>(subCategory);
//
// List<Section> sectionsCharts = new ArrayList<>();
//
// for(String sectionId : subCategory.getCharts()) {
// sectionsCharts.add(getSectionFull(sectionId, subCategoryId, addAll, addPublicAndRestricted));
// }
// subCategoryFull.setCharts(sectionsCharts);
//
// List<Section> 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<String> section = sectionDAO.findById(sectionId);
if (section == null) {
// EXCEPTION - Section not found
throw new EntityNotFoundException("Get stakeholder: Section with id: " + sectionId + " not found (section exists in subCategory: " + subCategoryId + ")");
}
Section sectionFull = new Section<Indicator>(section);
List<Indicator> indicators = new ArrayList<>();
for (String indicatorId : section.getIndicators()) {
Indicator indicator = indicatorDAO.findById(indicatorId);
if (indicator == null) {
// EXCEPTION - Indicator not found
throw new EntityNotFoundException("Get stakeholder: Indicator with id: " + indicatorId + " not found (indicator exists in section: " + sectionId + ")");
}
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<Stakeholder> getAllStakeholders(@RequestParam(required = false) String type) {
// log.debug("get all stakeholders" + (type != null ? " with type: "+type : ""));
List<Stakeholder> stakeholders;
if(type == null) {
stakeholders = stakeholderDAO.findAll();
} else {
stakeholders = stakeholderDAO.findByType(type);
}
List<Stakeholder> stakeholdersFull = new ArrayList<>();
for(Stakeholder stakeholder : stakeholders) {
stakeholdersFull.add(this.setFullEntities(stakeholder));
}
return stakeholdersFull;
}
@PreAuthorize("isAuthenticated()")
@RequestMapping(value = "/stakeholder/default", method = RequestMethod.GET)
public List<Stakeholder> getAllDefaultStakeholders(@RequestParam(required = false) String type) {
// log.debug("get all default stakeholders" + (type != null ? " with type: "+type : ""));
List<Stakeholder> stakeholders;
if(type == null) {
stakeholders = stakeholderDAO.findByDefaultId(null);
} else {
stakeholders = stakeholderDAO.findByDefaultIdAndType(null, type);
}
List<Stakeholder> stakeholdersFull = new ArrayList<>();
// 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));
}
return stakeholdersFull;
}
Iterator<Stakeholder> 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;
}
stakeholderIterator.remove();
}
}
return stakeholdersFull;
return this.stakeholderService.getAll(type);
}
@RequestMapping(value = "/stakeholder", method = RequestMethod.GET)
public List<Stakeholder> getAllRealStakeholders(@RequestParam(required = false) String type,
public List<Stakeholder> getVisibleStakeholders(@RequestParam(required = false) String type,
@RequestParam(required = false) String defaultId) {
return stakeholderService.getStakeholdersByTypeAndRole(type, defaultId, false);
return stakeholderService.getVisibleStakeholders(type, defaultId);
}
@PreAuthorize("isAuthenticated()")
@RequestMapping(value = "/my-stakeholder", method = RequestMethod.GET)
public List<Stakeholder> getMyRealStakeholders(@RequestParam(required = false) String type) {
// log.debug("get my NOT default stakeholders" + (type != null ? " with type: "+type : ""));
return stakeholderService.getStakeholdersByTypeAndRole(type, null, true);
public ManageStakeholders getManagedStakeholders(@RequestParam(required = false) String type) {
return stakeholderService.getManageStakeholders(type);
}
@RequestMapping(value = "/stakeholder/{alias:.+}", method = RequestMethod.GET)
public Stakeholder getStakeholder(@PathVariable("alias") String alias) {
// log.debug("get stakeholder: "+alias);
Stakeholder<String> stakeholder = stakeholderDAO.findByAlias(alias);
if(stakeholder == null) {
// EXCEPTION - Stakeholder not found
throw new EntityNotFoundException("Get stakeholder: Stakeholder with alias: "+alias+" not found");
public StakeholderFull getStakeholder(@PathVariable("alias") String alias) {
StakeholderFull stakeholder = this.stakeholderService.getFullStakeholder(this.stakeholderService.findByAlias(alias));
if (stakeholder == null) {
this.commonService.unauthorized("Get stakeholder: You are not authorized to access stakeholder with alias: " + alias);
}
// List<String> 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);
}
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
// throw new ForbiddenException("Get stakeholder: You are not authorized to get stakeholder with alias: "+alias);
List<String> topicsEmpty = stakeholder.getTopics();
topicsEmpty.clear();
stakeholder.setTopics(topicsEmpty);
stakeholder.setVisibility(Visibility.PRIVATE);
return stakeholder;
}
return this.setFullEntities(stakeholder);
return stakeholder;
}
// @PreAuthorize("isAuthenticated()")
@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) {
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<String> stakeholder = new Stakeholder<>(_stakeholder);
Date date = new Date();
stakeholder.setUpdateDate(date);
List<String> 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<String> 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);
// _stakeholder = this.setFullEntities(stakeholder, rolesUtils.getRoles());
@RequestMapping(value = "/stakeholder/{parent:.+}/{type}/{child:.+}", method = RequestMethod.GET)
public StakeholderFull getStakeholderWithParent(@PathVariable("parent") String parent, @PathVariable("type") String type, @PathVariable("child") String child) {
StakeholderFull stakeholder = this.stakeholderService.getFullStakeholderWithParents(this.stakeholderService.findByAlias(child), type, parent);
if (stakeholder == null) {
this.commonService.unauthorized("Get stakeholder: You are not authorized to access stakeholder with alias: " + child);
}
return stakeholder;
stakeholder.setTopics(topics);
}
Stakeholder<String> stakeholderSaved = stakeholderDAO.save(stakeholder);
_stakeholder.setId(stakeholderSaved.getId());
_stakeholder.setCreationDate(stakeholderSaved.getCreationDate());
_stakeholder.setUpdateDate(stakeholderSaved.getUpdateDate());
@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) {
log.debug("save stakeholder");
log.debug("Alias: " + stakeholder.getAlias() + " - Id: " + stakeholder.getId());
return this.stakeholderService.save(stakeholder);
}
topics = null;
stakeholder = null;
stakeholderSaved = null;
return _stakeholder;
@PreAuthorize("hasAnyAuthority("
+ "@AuthorizationService.PORTAL_ADMIN, "
+ "@AuthorizationService.curator(#stakeholder.getType()), "
+ "@AuthorizationService.manager(#stakeholder.getType(), #stakeholder.getAlias()) "
+ ")")
@RequestMapping(value = "/save/full", method = RequestMethod.POST)
public StakeholderFull saveStakeholderFull(@RequestBody StakeholderFull stakeholder) {
log.debug("save stakeholder");
log.debug("Alias: " + stakeholder.getAlias() + " - Id: " + stakeholder.getId());
return this.stakeholderService.getFullStakeholder(this.stakeholderService.save(new Stakeholder(stakeholder, this.stakeholderService)));
}
@PreAuthorize("isAuthenticated()")
@RequestMapping(value = "/{stakeholderId}/delete", method = RequestMethod.DELETE)
public boolean deleteStakeholder(@PathVariable("stakeholderId") String stakeholderId) {
public boolean deleteStakeholder(@PathVariable("stakeholderId") String id) {
log.debug("delete stakeholder");
log.debug("Id: "+stakeholderId);
Stakeholder<String> stakeholder = stakeholderDAO.findById(stakeholderId);
String pid = null;
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<String> topic = topicDAO.findById(topicId);
// if (topic == null) {
// // EXCEPTION - Topic not found
// throw new EntityNotFoundException("Delete stakeholder: Topic with id: "+topicId+" not found (topic exists in stakeholder: "+stakeholderId+")");
// }
//
// for (String categoryId : topic.getCategories()) {
// Category<String> category = categoryDAO.findById(categoryId);
// if (category == null) {
// // EXCEPTION - Category not found
// throw new EntityNotFoundException("Delete stakeholder: Category with id: "+categoryId+" not found (category exists in topic: "+topicId+")");
// }
//
// for (String subCategoryId : category.getSubCategories()) {
// SubCategory<String> subcategory = subCategoryDAO.findById(subCategoryId);
// if (subcategory == null) {
// // EXCEPTION - SubCategory not found
// throw new EntityNotFoundException("Delete stakeholder: SubCategory with id: "+subCategoryId+" not found (subcategory exists in category: "+categoryId+")");
// }
//
// for(String chartSectionId : subcategory.getCharts()) {
// Section<String> chartSection = sectionDAO.findById(chartSectionId);
// if (chartSection == null) {
// // EXCEPTION - Section not found
// throw new EntityNotFoundException("Delete topic: Section with id: "+chartSectionId+" not found (section exists in subcategory: "+subCategoryId+")");
// }
//
// for (String chartId : chartSection.getIndicators()) {
// indicatorDAO.delete(chartId);
// }
// subcategory.setCharts(null);
// sectionDAO.delete(chartSectionId);
// }
//
// for(String numberSectionId : subcategory.getNumbers()) {
// Section<String> numberSection = sectionDAO.findById(numberSectionId);
// if (numberSection == null) {
// // EXCEPTION - Section not found
// throw new EntityNotFoundException("Delete topic: Section with id: "+numberSectionId+" not found (section exists in subcategory: "+subCategoryId+")");
// }
//
// for (String numberId : numberSection.getIndicators()) {
// indicatorDAO.delete(numberId);
// }
// subcategory.setNumbers(null);
// sectionDAO.delete(numberSectionId);
// }
//
// subCategoryDAO.delete(subCategoryId);
// }
// category.setSubCategories(null);
// categoryDAO.delete(categoryId);
// }
// topic.setCategories(null);
// topicDAO.delete(topicId);
// }
topicController.deleteTree(stakeholder);
stakeholder.setTopics(null);
stakeholderDAO.delete(stakeholderId);
log.debug("Stakeholder deleted!");
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");
log.debug("Id: " + id);
Portal portal = portalService.getPortal(this.stakeholderService.delete(id));
if (portal != null) {
portalService.deletePortal(portal.getId());
}
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,
@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");
}
// List<String> 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);
}
return changeStakeholderVisibilityTree(stakeholder, visibility, propagate);
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);
log.debug("Stakeholder: " + stakeholderId);
Stakeholder stakeholder = this.stakeholderService.findByPath(stakeholderId);
return this.stakeholderService.changeVisibility(stakeholder, visibility, propagate);
}
private Stakeholder changeStakeholderVisibilityTree(Stakeholder<String> stakeholder, Visibility visibility, Boolean propagate) {
Stakeholder<Topic> stakeholderFull = new Stakeholder<>(stakeholder);
List<Topic> topicsFull = new ArrayList<>();
if(propagate != null && propagate) {
for (String topicId : stakeholder.getTopics()) {
topicsFull.add(topicController.changeVisibilityTree(topicId, visibility, propagate));
}
}
stakeholder.setVisibility(visibility);
stakeholderDAO.save(stakeholder);
log.debug("Stakeholder toggled!");
stakeholderFull.setVisibility(visibility);
stakeholderFull.setTopics(topicsFull);
return stakeholder;
@PreAuthorize("isAuthenticated()")
@RequestMapping(value = "/{stakeholderId}/umbrella", method = RequestMethod.POST)
public Umbrella<Stakeholder> updateUmbrella(@PathVariable("stakeholderId") String stakeholderId, @RequestBody UpdateUmbrella update) {
log.debug("update stakeholder umbrella");
log.debug("Stakeholder: " + stakeholderId);
Stakeholder stakeholder = this.stakeholderService.findByPath(stakeholderId);
return this.stakeholderService.updateUmbrella(stakeholder, update);
}
// The following are not supposed to be used
// @RequestMapping(value = "/stakeholder/dates", method = RequestMethod.GET)
// public List<Date> getAllStakeholderDates() {
// List<Stakeholder> profiles = stakeholderDAO.findAll();
// List<Date> profileDates = new ArrayList<>();
//
// int i=0;
// for(Stakeholder profile : profiles) {
// log.debug(profile.getCreationDate());
// profileDates.add(profile.getCreationDate());
// log.debug(profileDates.get(i));
// i++;
// }
// return profileDates;
// }
//
// @RequestMapping(value = "/stakeholder/dates1", method = RequestMethod.GET)
// public List<String> getAllStakeholderDates1() {
// List<Stakeholder> profiles = stakeholderDAO.findAll();
// List<String> profileDates = new ArrayList<>();
//
// for(Stakeholder profile : profiles) {
// log.debug(profile.getCreationDate().toString());
// profileDates.add(profile.getCreationDate().toString());
// }
// return profileDates;
// }
//
// @RequestMapping(value = "/stakeholder/dates2", method = RequestMethod.GET)
// public List<String> getAllStakeholderDates2() {
// List<Stakeholder> profiles = stakeholderDAO.findAll();
// List<String> profileDates = new ArrayList<>();
// SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//
// for(Stakeholder profile : profiles) {
// log.debug(format.format(profile.getCreationDate()));
// profileDates.add(format.format(profile.getCreationDate()));
// }
// return profileDates;
// }
}

View File

@ -1,20 +1,21 @@
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
@ -22,220 +23,34 @@ import java.util.List;
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<Section<Indicator>> buildSubCategory(SubCategory<Section<Indicator>> subcategoryFull) {
SubCategory<String> subCategory = new SubCategory<>(subcategoryFull);
List<String> sectionCharts = new ArrayList<>();
List<Section<Indicator>> sectionChartsFull = new ArrayList<>();
for(Section section : subcategoryFull.getCharts()) {
Section<Indicator> sectionFull = sectionController.buildSection(section);
sectionChartsFull.add(sectionFull);
sectionCharts.add(sectionFull.getId());
}
subcategoryFull.setCharts(sectionChartsFull);
subCategory.setCharts(sectionCharts);
List<String> sectionNumbers = new ArrayList<>();
List<Section<Indicator>> sectionNumbersFull = new ArrayList<>();
for(Section section : subcategoryFull.getNumbers()) {
Section<Indicator> 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()")
@RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/save", method = RequestMethod.POST)
public SubCategory<Section<Indicator>> saveSubCategory(@PathVariable("stakeholderId") String stakeholderId,
@PathVariable("topicId") String topicId,
@PathVariable("categoryId") String categoryId,
@RequestBody SubCategory<Section<Indicator>> 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<String> subCategory = new SubCategory<>(subcategoryFull);
Date date = new Date();
subCategory.setUpdateDate(date);
subcategoryFull.setUpdateDate(date);
List<String> chartSections = new ArrayList<>();
List<String> numberSections = new ArrayList<>();
SubCategory<String> oldSubcategory = null;
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);
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());
}
}
// List<String> charts = new ArrayList<>();
// for(Indicator chart : subcategoryFull.getCharts()) {
// charts.add(chart.getId());
// }
// subCategory.setCharts(charts);
//
// List<String> numbers = new ArrayList<>();
// for(Indicator numbr : subcategoryFull.getNumbers()) {
// numbers.add(numbr.getId());
// }
// subCategory.setNumbers(numbers);
subCategory.setCharts(chartSections);
subCategory.setNumbers(numberSections);
Stakeholder<String> 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<String> 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());
}
chartSections = null;
numberSections = null;
subCategory = null;
return subcategoryFull;
}
public void onSaveDefaultSubCategory(SubCategory subCategory, String categoryId) {
log.debug("On save default subCategory");
List<Category> categories = categoryDAO.findByDefaultId(categoryId);
for(Category category : categories) {
SubCategory subCategoryNew = new SubCategory();
subCategoryNew.copyFromDefault(subCategory);
subCategoryDAO.save(subCategoryNew);
List<String> subCategories = category.getSubCategories();
subCategories.add(subCategoryNew.getId());
categoryDAO.save(category);
}
}
public void onUpdateDefaultSubCategory(SubCategory subCategory, SubCategory oldSubcategory) {
log.debug("On update default subCategory");
List<SubCategory> 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) {
// break;
continue;
}
// subCategoryBasedOnDefault.setName(subCategory.getName());
// subCategoryBasedOnDefault.setDescription(subCategory.getDescription());
subCategoryBasedOnDefault.setUpdateDate(subCategory.getUpdateDate());
subCategoryDAO.save(subCategoryBasedOnDefault);
this.subCategoryService.findByPath(category, subcategoryFull.getId());
}
return this.subCategoryService.save(stakeholder, category, new SubCategory(subcategoryFull));
}
@PreAuthorize("isAuthenticated()")
@ -246,325 +61,43 @@ 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<String> subcategory = subCategoryDAO.findById(subcategoryId);
if(subcategory != null) {
Stakeholder<String> 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<String> 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);
}
// for(String chartSectionId : subcategory.getCharts()) {
// Section<String> 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<String> 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);
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");
}
return true;
}
public boolean onDeleteDefaultSubCategory(String defaultSubCategoryId, String defaultCategoryId, String children) {
if(children.equals("delete")) {
List<Category> categories = categoryDAO.findByDefaultId(defaultCategoryId);
List<SubCategory> subCategories = subCategoryDAO.findByDefaultId(defaultSubCategoryId);
for(Category category : categories) {
Iterator<SubCategory> 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<SubCategory> 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!");
}
}
log.debug("Id: " + subcategoryId + " - 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);
SubCategory subCategory = this.subCategoryService.findByPath(category, subcategoryId);
this.subCategoryService.delete(stakeholder.getType(), subCategory, true);
return true;
}
@PreAuthorize("isAuthenticated()")
@RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/reorder", method = RequestMethod.POST)
public List<SubCategory> reorderSubCategories(@PathVariable("stakeholderId") String stakeholderId,
@PathVariable("topicId") String topicId,
@PathVariable("categoryId") String categoryId,
@RequestBody List<String> subCategories) {
public List<SubCategoryFull> reorderSubCategories(@PathVariable("stakeholderId") String stakeholderId,
@PathVariable("topicId") String topicId,
@PathVariable("categoryId") String categoryId,
@RequestBody List<String> subCategories) {
log.debug("reorder subCategories");
log.debug("Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId);
Category<String> category = checkForExceptions(stakeholderId, topicId, categoryId);
List<String> oldSubcategories = category.getSubCategories();
for (String subcategoryId : oldSubcategories) {
if (!subCategories.contains(subcategoryId)) {
subCategories.add(subcategoryId);
}
}
category.setSubCategories(subCategories);
List<SubCategory> 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();
}
// @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,
@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<String> subCategory = subCategoryDAO.findById(subCategoryId);
if (subCategory == null) {
// EXCEPTION - SubCategory not found
throw new EntityNotFoundException("Change subCategory visibility: SubCategory with id: " + subCategoryId + " not found");
}
SubCategory<Section> subCategoryFull = new SubCategory(subCategory);
List<Section> chartsFull = new ArrayList();
List<Section> 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<String> 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<String> topic = topicDAO.findById(topicId);
if(topic == null) {
// EXCEPTION - Topic not found
throw new EntityNotFoundException("Save indicator: Topic with id: "+topicId+" not found");
}
if(!stakeholder.getTopics().contains(topicId)) {
// EXCEPTION - Topic not found in Stakeholder: stakeholder.getAlias();
throw new PathNotValidException("Save indicator: Topic with id: " + topicId + " not found in Stakeholder: " + stakeholderId);
}
Category<String> category = categoryDAO.findById(categoryId);
if(category == null) {
// EXCEPTION - Category not found
throw new EntityNotFoundException("Save indicator: Category with id: "+categoryId+" not found");
}
if(!topic.getCategories().contains(categoryId)) {
// EXCEPTION - Category not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias();
throw new PathNotValidException("Save indicator: Category with id: "+categoryId+" not found in Topic: "+topicId);
}
// SubCategory<String> subcategory = subCategoryDAO.findById(subcategoryId);
// if(subcategory == null) {
// // EXCEPTION - SubCategory not found
// throw new EntityNotFoundException("Save indicator: SubCategory with id: "+subcategoryId+" not found");
// }
//
// if (!category.getSubCategories().contains(subcategoryId)) {
// // EXCEPTION - SubCategory not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); -> Category: category.getAlias();
// throw new PathNotValidException("Save indicator: SubCategory with id: "+subcategoryId+" not found in Category: "+categoryId);
// }
return category;
}
public void deleteTree(Category category) {
List<String> 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<String> 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.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.subCategoryService.changeVisibility(stakeholder.getType(), stakeholder.getAlias(), subCategory, visibility, propagate);
}
}

View File

@ -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<Stakeholder> 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);
}
}

View File

@ -1,20 +1,17 @@
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.TopicFull;
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
@ -22,179 +19,25 @@ import java.util.List;
public class TopicController {
private final Logger log = LogManager.getLogger(this.getClass());
@Autowired
private RolesUtils rolesUtils;
private final TopicService topicService;
private final StakeholderService stakeholderService;
@Autowired
private StakeholderDAO stakeholderDAO;
@Autowired
private TopicDAO topicDAO;
@Autowired
private CategoryController categoryController;
@Autowired
private CategoryDAO categoryDAO;
public Topic<Category> buildTopic(Topic<Category> topicFull) {
Topic<String> topic = new Topic<>(topicFull);
List<String> categories = new ArrayList<>();
List<Category> categoriesFull = new ArrayList<>();
for(Category<SubCategory> category : topicFull.getCategories()) {
Category<SubCategory> categoryFull = categoryController.buildCategory(category);
categoriesFull.add(categoryFull);
categories.add(categoryFull.getId());
}
topicFull.setCategories(categoriesFull);
topic.setCategories(categories);
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()")
@RequestMapping(value = "/{stakeholderId}/save", method = RequestMethod.POST)
public Topic<Category> saveTopic(@PathVariable("stakeholderId") String stakeholderId,
@RequestBody Topic<Category> 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<String> 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<String> topic = new Topic<>(topicFull);
Date date = new Date();
topic.setUpdateDate(date);
topicFull.setUpdateDate(date);
List<String> categories = new ArrayList<>();
Topic<String> 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<String> 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());
}
categories = null;
topic = null;
} else {
// EXCEPTION - Stakeholder not found
throw new EntityNotFoundException("Save topic: Stakeholder with id: "+stakeholderId+" not found");
}
return topicFull;
}
public void onSaveDefaultTopic(Topic topic, String stakeholderId) {
log.debug("On save default topic");
List<Stakeholder> stakeholders = stakeholderDAO.findByDefaultId(stakeholderId);
for(Stakeholder _stakeholder : stakeholders) {
Topic topicNew = new Topic();
topicNew.copyFromDefault(topic);
topicDAO.save(topicNew);
List<String> topics = _stakeholder.getTopics();
topics.add(topicNew.getId());
stakeholderDAO.save(_stakeholder);
}
}
public void onUpdateDefaultTopic(Topic topic, Topic oldTopic) {
log.debug("On update default topic");
List<Topic> 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) {
// break;
continue;
}
// topicBasedOnDefault.setName(topic.getName());
// topicBasedOnDefault.setDescription(topic.getDescription());
topicBasedOnDefault.setUpdateDate(topic.getUpdateDate());
topicDAO.save(topicBasedOnDefault);
log.debug("Alias: " + topicFull.getAlias() + " - Id: " + topicFull.getId() + " - Stakeholder: " + stakeholderId);
Stakeholder stakeholder = this.stakeholderService.findByPath(stakeholderId);
if(topicFull.getId() != null) {
this.topicService.findByPath(stakeholder, topicFull.getId());
}
return this.topicService.save(stakeholder, new Topic(topicFull));
}
@PreAuthorize("isAuthenticated()")
@ -203,296 +46,33 @@ public class TopicController {
@PathVariable("topicId") String topicId,
@RequestParam(required = false) String children) {
log.debug("delete topic");
log.debug("Id: "+topicId + " - Stakeholder: "+stakeholderId);
Stakeholder<String> 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<String> 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<String> 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);
}
// for(String categoryId : topic.getCategories()) {
// Category<String> category = categoryDAO.findById(categoryId);
// if(category == null) {
// // EXCEPTION - Category not found
// throw new EntityNotFoundException("Delete topic: Category with id: "+categoryId+" not found (category exists in topic: "+topicId+")");
// }
//
// for(String subCategoryId : category.getSubCategories()) {
// SubCategory<String> subcategory = subCategoryDAO.findById(subCategoryId);
// if (subcategory == null) {
// // EXCEPTION - SubCategory not found
// throw new EntityNotFoundException("Delete topic: SubCategory with id: "+subCategoryId+" not found (subcategory exists in category: "+categoryId+")");
// }
//
// for(String chartSectionId : subcategory.getCharts()) {
// Section<String> chartSection = sectionDAO.findById(chartSectionId);
// if (chartSection == null) {
// // EXCEPTION - Section not found
// throw new EntityNotFoundException("Delete topic: Section with id: "+chartSectionId+" not found (section exists in subcategory: "+subCategoryId+")");
// }
//
// for (String chartId : chartSection.getIndicators()) {
// indicatorDAO.delete(chartId);
// }
// subcategory.setCharts(null);
// sectionDAO.delete(chartSectionId);
// }
//
// for(String numberSectionId : subcategory.getNumbers()) {
// Section<String> numberSection = sectionDAO.findById(numberSectionId);
// if (numberSection == null) {
// // EXCEPTION - Section not found
// throw new EntityNotFoundException("Delete topic: Section with id: "+numberSectionId+" not found (section exists in subcategory: "+subCategoryId+")");
// }
//
// for (String numberId : numberSection.getIndicators()) {
// indicatorDAO.delete(numberId);
// }
// subcategory.setNumbers(null);
// sectionDAO.delete(numberSectionId);
// }
//
// subCategoryDAO.delete(subCategoryId);
// }
// category.setSubCategories(null);
// categoryDAO.delete(categoryId);
// }
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.findByPath(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<Stakeholder> stakeholders = stakeholderDAO.findByDefaultId(defaultStakeholderId);
List<Topic> topics = topicDAO.findByDefaultId(defaultTopicId);
for(Stakeholder stakeholder : stakeholders) {
Iterator<Topic> 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<Topic> 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()")
@RequestMapping(value = "/{stakeholderId}/reorder", method = RequestMethod.POST)
public List<Topic> reorderTopics(@PathVariable("stakeholderId") String stakeholderId,
@RequestBody List<String> topics) {
public List<TopicFull> reorderTopics(@PathVariable("stakeholderId") String stakeholderId,
@RequestBody List<String> topics) {
log.debug("reorder topics");
log.debug("Stakeholder: "+stakeholderId);
Stakeholder<String> 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<String> oldTopics = stakeholder.getTopics();
for (String topicId : oldTopics) {
if (!topics.contains(topicId)) {
topics.add(topicId);
}
}
stakeholder.setTopics(topics);
List<Topic> 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();
}
// @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,
@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<String> 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");
}
// this.toggleTopic(stakeholderId, topic);
}
public Topic changeVisibilityTree(String topicId, Visibility visibility, Boolean propagate) {
Topic<String> topic = topicDAO.findById(topicId);
if (topic == null) {
// EXCEPTION - Topic not found
throw new EntityNotFoundException("Change topic visibility: Topic with id: "+topicId+" not found");
}
Topic<Category> topicFull = new Topic(topic);
List<Category> 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<String> 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(defaultValue = "false") Boolean propagate) {
log.debug("change topic visibility: " + visibility + " - toggle propagate: " + propagate);
log.debug("Stakeholder: " + stakeholderId + " - Topic: " + topicId);
Stakeholder stakeholder = this.stakeholderService.findByPath(stakeholderId);
Topic topic = this.topicService.findByPath(stakeholder, topicId);
return this.topicService.changeVisibility(stakeholder.getType(), stakeholder.getAlias(), topic, visibility, propagate);
}
}

View File

@ -1,16 +1,20 @@
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;
import java.util.Optional;
public interface CategoryDAO {
@Repository
public interface CategoryDAO extends MongoRepository<Category, String> {
List<Category> findAll();
List<Category> findByDefaultId(String DefaultId);
Category findBySubCategoriesContaining(String subCategory);
List<Category> findBySubCategoriesContaining(String subCategory);
Category findById(String Id);
Optional<Category> findById(String Id);
void delete(String Id);

View File

@ -1,14 +1,18 @@
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;
import java.util.Optional;
public interface IndicatorDAO {
@Repository
public interface IndicatorDAO extends MongoRepository<Indicator, String> {
List<Indicator> findAll();
List<Indicator> findByDefaultId(String DefaultId);
Indicator findById(String Id);
Optional<Indicator> findById(String Id);
void delete(String Id);

View File

@ -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<Category, String> {
List<Category> findAll();
List<Category> findByDefaultId(String DefaultId);
Category findBySubCategoriesContaining(String subCategory);
Category findById(String Id);
void delete(String Id);
Category save(Category category);
}

View File

@ -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<Indicator, String> {
List<Indicator> findAll();
List<Indicator> findByDefaultId(String DefaultId);
Indicator findById(String Id);
void delete(String Id);
Indicator save(Indicator indicator);
//List<Indicator> saveAll(List<Indicator> indicators);
}

View File

@ -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<Section, String> {
List<Section> findAll();
List<Section> findByDefaultId(String DefaultId);
Section findById(String Id);
void delete(String Id);
Section save(Section indicator);
}

View File

@ -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<Stakeholder, String> {
List<Stakeholder> findAll();
List<Stakeholder> findByType(String Type);
List<Stakeholder> findByDefaultId(String DefaultId);
List<Stakeholder> findByDefaultIdAndType(String DefaultId, String Type);
List<Stakeholder> findByDefaultIdNot(String DefaultId);
List<Stakeholder> 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);
}

View File

@ -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<SubCategory, String> {
List<SubCategory> findAll();
List<SubCategory> findByDefaultId(String DefaultId);
SubCategory findById(String Id);
void delete(String Id);
SubCategory save(SubCategory subCategory);
}

View File

@ -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<Topic, String> {
List<Topic> findAll();
List<Topic> findByDefaultId(String DefaultId);
Topic findByCategoriesContaining(String category);
Topic findById(String Id);
void delete(String Id);
Topic save(Topic topic);
}

View File

@ -1,14 +1,19 @@
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;
import java.util.Optional;
public interface SectionDAO {
@Repository
public interface SectionDAO extends MongoRepository<Section, String> {
List<Section> findAll();
List<Section> findByDefaultId(String DefaultId);
List<Section> findByIndicatorsContaining(String id);
Section findById(String Id);
Optional<Section> findById(String Id);
void delete(String Id);

View File

@ -1,23 +1,196 @@
package eu.dnetlib.uoamonitorservice.dao;
import eu.dnetlib.uoamonitorservice.entities.Stakeholder;
import eu.dnetlib.uoamonitorservice.primitives.StakeholderType;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.stereotype.Repository;
import java.util.List;
import java.util.Optional;
public interface StakeholderDAO {
@Repository
public interface StakeholderDAO extends MongoRepository<Stakeholder, String> {
/**
* All Stakeholders
*/
List<Stakeholder> findAll();
List<Stakeholder> findByType(String Type);
List<Stakeholder> findByDefaultId(String DefaultId);
List<Stakeholder> findByDefaultIdAndType(String DefaultId, String Type);
List<Stakeholder> findByDefaultId(String id);
List<Stakeholder> findByDefaultIdNot(String DefaultId);
List<Stakeholder> findByDefaultIdNotAndType(String DefaultId, String Type);
List<Stakeholder> findByType(StakeholderType Type);
Stakeholder findByTopicsContaining(String topic);
List<Stakeholder> findByTypeAndDefaultId(StakeholderType Type, String id);
default List<Stakeholder> allStakeholders() {
return allStakeholders(null);
}
default List<Stakeholder> allStakeholders(StakeholderType type) {
return allStakeholders(type, null);
}
default List<Stakeholder> allStakeholders(StakeholderType type, String id) {
if (type != null && id != null) {
return findByTypeAndDefaultId(type, id);
} else if (id != null) {
return findByDefaultId(id);
} else if (type != null) {
return findByType(type);
} else {
return findAll();
}
}
/**
* Default Stakeholders
*/
List<Stakeholder> findByDefaultIdIsNull();
List<Stakeholder> findByDefaultIdIsNullAndType(StakeholderType Type);
default List<Stakeholder> defaultStakeholders(StakeholderType type) {
if (type == null) {
return findByDefaultIdIsNull();
}
return findByDefaultIdIsNullAndType(type);
}
/**
* Standalone Stakeholders (not umbrella)
*/
List<Stakeholder> findByDefaultIdIsNotNullAndStandaloneIsTrueAndUmbrellaIsNull();
List<Stakeholder> findByDefaultIdIsNotNullAndStandaloneIsTrueAndUmbrellaIsNullAndDefaultId(String id);
List<Stakeholder> findByDefaultIdIsNotNullAndStandaloneIsTrueAndUmbrellaIsNullAndType(StakeholderType Type);
List<Stakeholder> findByDefaultIdIsNotNullAndStandaloneIsTrueAndUmbrellaIsNullAndTypeAndDefaultId(StakeholderType Type, String id);
default List<Stakeholder> standaloneStakeholders() {
return standaloneStakeholders(null);
}
default List<Stakeholder> standaloneStakeholders(StakeholderType type) {
return standaloneStakeholders(type, null);
}
default List<Stakeholder> standaloneStakeholders(StakeholderType type, String id) {
if (type != null && id != null) {
return findByDefaultIdIsNotNullAndStandaloneIsTrueAndUmbrellaIsNullAndTypeAndDefaultId(type, id);
} else if (id != null) {
return findByDefaultIdIsNotNullAndStandaloneIsTrueAndUmbrellaIsNullAndDefaultId(id);
} else if (type != null) {
return findByDefaultIdIsNotNullAndStandaloneIsTrueAndUmbrellaIsNullAndType(type);
} else {
return findByDefaultIdIsNotNullAndStandaloneIsTrueAndUmbrellaIsNull();
}
}
/**
* Dependent Stakeholders
*/
List<Stakeholder> findByDefaultIdIsNotNullAndStandaloneIsFalse();
List<Stakeholder> findByDefaultIdIsNotNullAndStandaloneIsFalseAndDefaultId(String id);
List<Stakeholder> findByDefaultIdIsNotNullAndStandaloneIsFalseAndType(StakeholderType Type);
List<Stakeholder> findByDefaultIdIsNotNullAndStandaloneIsFalseAndTypeAndDefaultId(StakeholderType Type, String id);
default List<Stakeholder> dependentStakeholders() {
return dependentStakeholders(null);
}
default List<Stakeholder> dependentStakeholders(StakeholderType type) {
return dependentStakeholders(type, null);
}
default List<Stakeholder> dependentStakeholders(StakeholderType type, String id) {
if (type != null && id != null) {
return findByDefaultIdIsNotNullAndStandaloneIsFalseAndTypeAndDefaultId(type, id);
} else if (id != null) {
return findByDefaultIdIsNotNullAndStandaloneIsFalseAndDefaultId(id);
} else if (type != null) {
return findByDefaultIdIsNotNullAndStandaloneIsFalseAndType(type);
} else {
return findByDefaultIdIsNotNullAndStandaloneIsFalse();
}
}
/**
* Umbrella Stakeholders
*/
List<Stakeholder> findByUmbrellaNotNull();
List<Stakeholder> findByUmbrellaNotNullAndDefaultId(String id);
List<Stakeholder> findByUmbrellaNotNullAndType(StakeholderType Type);
List<Stakeholder> findByUmbrellaNotNullAndTypeAndDefaultId(StakeholderType Type, String id);
default List<Stakeholder> umbrellaStakeholders() {
return umbrellaStakeholders(null);
}
default List<Stakeholder> umbrellaStakeholders(StakeholderType type) {
return umbrellaStakeholders(type, null);
}
default List<Stakeholder> umbrellaStakeholders(StakeholderType type, String id) {
if (type != null && id != null) {
return findByUmbrellaNotNullAndTypeAndDefaultId(type, id);
} else if (id != null) {
return findByUmbrellaNotNullAndDefaultId(id);
} else if (type != null) {
return findByUmbrellaNotNullAndType(type);
} else {
return findByUmbrellaNotNull();
}
}
/**
* Browse Stakeholders
* */
List<Stakeholder> findByDefaultIdNotNullAndStandaloneIsTrue();
List<Stakeholder> findByDefaultIdNotNullAndStandaloneIsTrueAndDefaultId(String id);
List<Stakeholder> findByDefaultIdNotNullAndStandaloneIsTrueAndType(StakeholderType Type);
List<Stakeholder> findByDefaultIdNotNullAndStandaloneIsTrueAndTypeAndDefaultId(StakeholderType Type, String id);
default List<Stakeholder> browseStakeholders() {
return browseStakeholders(null);
}
default List<Stakeholder> browseStakeholders(StakeholderType type) {
return browseStakeholders(type, null);
}
default List<Stakeholder> browseStakeholders(StakeholderType type, String id) {
if (type != null && id != null) {
return findByDefaultIdNotNullAndStandaloneIsTrueAndTypeAndDefaultId(type, id);
} else if (id != null) {
return findByDefaultIdNotNullAndStandaloneIsTrueAndDefaultId(id);
} else if (type != null) {
return findByDefaultIdNotNullAndStandaloneIsTrueAndType(type);
} else {
return findByDefaultIdNotNullAndStandaloneIsTrue();
}
}
/**
* Other method
*/
List<Stakeholder> findByDefaultIdAndCopyIsTrue(String defaultId);
List<Stakeholder> findByTopicsContaining(String topic);
Optional<Stakeholder> findById(String id);
Optional<Stakeholder> findByAlias(String alias);
Stakeholder findById(String Id);
Stakeholder findByAlias(String Alias);
void delete(String Id);

View File

@ -1,14 +1,20 @@
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;
import java.util.Optional;
public interface SubCategoryDAO {
@Repository
public interface SubCategoryDAO extends MongoRepository<SubCategory, String> {
List<SubCategory> findAll();
List<SubCategory> findByDefaultId(String DefaultId);
List<SubCategory> findByDefaultId(String defaultId);
List<SubCategory> findByNumbersContaining(String id);
List<SubCategory> findByChartsContaining(String id);
SubCategory findById(String Id);
Optional<SubCategory> findById(String Id);
void delete(String Id);

View File

@ -1,16 +1,20 @@
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;
import java.util.Optional;
public interface TopicDAO {
@Repository
public interface TopicDAO extends MongoRepository<Topic, String> {
List<Topic> findAll();
List<Topic> findByDefaultId(String DefaultId);
Topic findByCategoriesContaining(String category);
List<Topic> findByCategoriesContaining(String category);
Topic findById(String Id);
Optional<Topic> findById(String Id);
void delete(String Id);

View File

@ -0,0 +1,45 @@
package eu.dnetlib.uoamonitorservice.dto;
import eu.dnetlib.uoamonitorservice.entities.Stakeholder;
public class BuildStakeholder {
private Stakeholder stakeholder;
private String copyId;
private boolean umbrella = false;
private boolean standalone = true;
public BuildStakeholder() {
}
public Stakeholder getStakeholder() {
return stakeholder;
}
public void setStakeholder(Stakeholder stakeholder) {
this.stakeholder = stakeholder;
}
public String getCopyId() {
return copyId;
}
public void setCopyId(String copyId) {
this.copyId = copyId;
}
public boolean isUmbrella() {
return umbrella;
}
public void setUmbrella(boolean umbrella) {
this.umbrella = umbrella;
}
public boolean isStandalone() {
return standalone;
}
public void setStandalone(boolean standalone) {
this.standalone = standalone;
}
}

View File

@ -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<SubCategoryFull> {
public CategoryFull() {
super();
}
public CategoryFull(CategoryGeneric category, List<SubCategoryFull> subCategories) {
super(category);
subCategories.removeIf(Objects::isNull);
this.subCategories = subCategories;
}
}

View File

@ -0,0 +1,47 @@
package eu.dnetlib.uoamonitorservice.dto;
import eu.dnetlib.uoamonitorservice.entities.Stakeholder;
import java.util.List;
public class ManageStakeholders {
List<Stakeholder> templates;
List<Stakeholder> standalone;
List<Stakeholder> dependent;
List<Stakeholder> umbrella;
public ManageStakeholders() {
}
public List<Stakeholder> getTemplates() {
return templates;
}
public void setTemplates(List<Stakeholder> templates) {
this.templates = templates;
}
public List<Stakeholder> getStandalone() {
return standalone;
}
public void setStandalone(List<Stakeholder> standalone) {
this.standalone = standalone;
}
public List<Stakeholder> getDependent() {
return dependent;
}
public void setDependent(List<Stakeholder> dependent) {
this.dependent = dependent;
}
public List<Stakeholder> getUmbrella() {
return umbrella;
}
public void setUmbrella(List<Stakeholder> umbrella) {
this.umbrella = umbrella;
}
}

View File

@ -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;
}
}

View File

@ -0,0 +1,32 @@
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<Indicator> {
public SectionFull() {}
public SectionFull(Section section, List<Indicator> indicators) {
super(section);
indicators.removeIf(Objects::isNull);
this.indicators = indicators;
}
public Optional<Indicator> 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);
}
}

View File

@ -0,0 +1,32 @@
package eu.dnetlib.uoamonitorservice.dto;
import java.util.List;
public class SectionInfo {
private String id;
private List<String> indicators;
public SectionInfo() {
}
public SectionInfo(String id, List<String> indicators) {
this.id = id;
this.indicators = indicators;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public List<String> getIndicators() {
return indicators;
}
public void setIndicators(List<String> indicators) {
this.indicators = indicators;
}
}

View File

@ -0,0 +1,40 @@
package eu.dnetlib.uoamonitorservice.dto;
import eu.dnetlib.uoamonitorservice.entities.Stakeholder;
import eu.dnetlib.uoamonitorservice.generics.StakeholderGeneric;
import eu.dnetlib.uoamonitorservice.primitives.Umbrella;
import java.util.List;
import java.util.Objects;
public class StakeholderFull extends StakeholderGeneric<TopicFull, Stakeholder> {
public List<Stakeholder> otherParents;
public Stakeholder parent;
public StakeholderFull() {
super();
}
public StakeholderFull(StakeholderGeneric stakeholder, List<TopicFull> topics, Umbrella<Stakeholder> umbrella) {
super(stakeholder);
topics.removeIf(Objects::isNull);
this.topics = topics;
this.setUmbrella(umbrella);
}
public List<Stakeholder> getOtherParents() {
return otherParents;
}
public void setOtherParents(List<Stakeholder> otherParents) {
this.otherParents = otherParents;
}
public Stakeholder getParent() {
return parent;
}
public void setParent(Stakeholder parent) {
this.parent = parent;
}
}

View File

@ -0,0 +1,26 @@
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<SectionFull> {
public SubCategoryFull() {}
public SubCategoryFull(SubCategoryGeneric subCategory, List<SectionFull> numbers, List<SectionFull> charts) {
super(subCategory);
numbers.removeIf(Objects::isNull);
charts.removeIf(Objects::isNull);
this.numbers = numbers;
this.charts = charts;
}
public Optional<SectionFull> getSectionByDefaultId(String id) {
List<SectionFull> sections = new ArrayList<>(this.numbers);
sections.addAll(this.charts);
return sections.stream().filter(section -> section.getDefaultId().equals(id)).findFirst();
}
}

View File

@ -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<CategoryFull> {
public TopicFull() {
super();
}
public TopicFull(TopicGeneric topic, List<CategoryFull> categories) {
super(topic);
categories.removeIf(Objects::isNull);
this.categories = categories;
}
}

View File

@ -0,0 +1,57 @@
package eu.dnetlib.uoamonitorservice.dto;
import eu.dnetlib.uoamonitorservice.primitives.Action;
import eu.dnetlib.uoamonitorservice.primitives.StakeholderType;
import java.util.List;
public class UpdateUmbrella {
StakeholderType type;
Action action;
String child;
List<StakeholderType> types;
List<String> children;
public UpdateUmbrella() {
}
public StakeholderType getType() {
return type;
}
public void setType(StakeholderType type) {
this.type = type;
}
public Action getAction() {
return action;
}
public void setAction(Action action) {
this.action = action;
}
public String getChild() {
return child;
}
public void setChild(String child) {
this.child = child;
}
public List<StakeholderType> getTypes() {
return types;
}
public void setTypes(List<StakeholderType> types) {
this.types = types;
}
public List<String> getChildren() {
return children;
}
public void setChildren(List<String> children) {
this.children = children;
}
}

View File

@ -1,129 +1,57 @@
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.Common;
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;
import java.util.Objects;
import java.util.stream.Collectors;
public class Category<StringOrSubcategory> {
@Id
@JsonProperty("_id")
private String id;
@Document
public class Category extends CategoryGeneric<String> {
public Category() {
super();
}
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<StringOrSubcategory> 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();
super(category);
}
public void copyFromDefault(Category 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 Category(CategoryFull category) {
super(category);
this.subCategories = category.getSubCategories().stream().map(Common::getId).collect(Collectors.toList());
this.subCategories.removeIf(Objects::isNull);
}
public String getId() {
return id;
public Category copy() {
Category category = new Category(this);
category.setDefaultId(this.getId());
category.setId(null);
return category;
}
public void setId(String id) {
this.id = id;
public Category override(Category category, Category old) {
return (Category) super.override(category, old);
}
public String getName() {
return name;
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 setName(String name) {
this.name = name;
public void addSubCategory(String id) {
this.subCategories.add(id);
}
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<StringOrSubcategory> getSubCategories() {
return subCategories;
}
public void setSubCategories(List<StringOrSubcategory> subCategories) {
this.subCategories = subCategories;
public void removeSubCategory(String id) {
this.subCategories.remove(id);
}
}

View File

@ -1,78 +1,61 @@
package eu.dnetlib.uoamonitorservice.entities;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.springframework.data.annotation.Id;
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 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")
private String id;
private String name;
private String description;
public class Indicator extends Common {
private String additionalDescription;
private IndicatorType type; //number,chart
private IndicatorSize width; //small,medium,large
private IndicatorSize height = IndicatorSize.MEDIUM; //small,medium,large
private List<String> tags; // this field is not used anywhere now
private Visibility visibility = Visibility.PRIVATE;
private Date creationDate;
private Date updateDate;
private String defaultId;
private List<IndicatorPath> 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 String getId() {
return id;
public Indicator copy() {
Indicator indicator = new Indicator(this);
indicator.setDefaultId(this.getId());
indicator.setId(null);
return indicator;
}
public void setId(String id) {
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 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() {
@ -90,19 +73,11 @@ 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;
} else {
IndicatorType indicatorType = IndicatorType.valueOf(type);
this.type = indicatorType;
this.type = IndicatorType.valueOf(type);
}
}
@ -130,38 +105,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;
}
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<IndicatorPath> getIndicatorPaths() {
return indicatorPaths;
}
@ -169,8 +112,4 @@ public class Indicator {
public void setIndicatorPaths(List<IndicatorPath> indicatorPaths) {
this.indicatorPaths = indicatorPaths;
}
// public String hasType() {
// return this.type.name();
// }
}

View File

@ -1,42 +0,0 @@
package eu.dnetlib.uoamonitorservice.entities;
import java.util.List;
public class ReorderEvent {
private String action; // "moved", "added", "removed"
private String target;
private List<String> 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<String> getIds() {
return ids;
}
public void setIds(List<String> ids) {
this.ids = ids;
}
@Override
public String toString() {
return "ReorderEvent{" +
"action='" + action + '\'' +
", target='" + target + '\'' +
", ids=" + ids +
'}';
}
}

View File

@ -1,121 +1,50 @@
package eu.dnetlib.uoamonitorservice.entities;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.springframework.data.annotation.Id;
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;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
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<StringOrIndicator> {
@Id
@JsonProperty("_id")
private String id;
private String title;
private String defaultId;
private String stakeholderAlias;
private SectionType type;
private Date creationDate;
private Date updateDate;
private List<StringOrIndicator> indicators;
@Document
public class Section extends SectionGeneric<String> {
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();
super(section);
}
public void copyFromDefault(Section defaultSection) {
setTitle(defaultSection.getTitle());
setType(defaultSection.getType());
setDefaultId(defaultSection.id);
setCreationDate(defaultSection.getCreationDate());
setUpdateDate(defaultSection.getUpdateDate());
setIndicators(new ArrayList<>());
public Section(SectionFull section) {
super(section);
this.indicators = section.getIndicators().stream().map(Indicator::getId).collect(Collectors.toList());
this.indicators.removeIf(Objects::isNull);
}
public String getId() {
return id;
public Section copy() {
Section section = new Section(this);
section.setDefaultId(this.getId());
section.setId(null);
return section;
}
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;
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 type.name();
return section;
}
public void setType(String type) {
if(type == null) {
this.type = null;
} else {
SectionType sectionType = SectionType.valueOf(type);
this.type = sectionType;
}
public void addIndicator(String id) {
this.indicators.add(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 List<StringOrIndicator> getIndicators() {
return indicators;
}
public void setIndicators(List<StringOrIndicator> indicators) {
this.indicators = indicators;
public void removeIndicator(String id) {
this.indicators.remove(id);
}
}

View File

@ -1,250 +1,40 @@
package eu.dnetlib.uoamonitorservice.entities;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.springframework.data.annotation.Id;
import eu.dnetlib.uoamonitorservice.dto.StakeholderFull;
import eu.dnetlib.uoamonitorservice.generics.Common;
import eu.dnetlib.uoamonitorservice.generics.StakeholderGeneric;
import eu.dnetlib.uoamonitorservice.primitives.Umbrella;
import eu.dnetlib.uoamonitorservice.service.StakeholderService;
import org.springframework.data.mongodb.core.mapping.Document;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
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<StringOrTopic> {
@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<StringOrTopic> topics;
@Document
public class Stakeholder extends StakeholderGeneric<String, String> {
public Stakeholder() {
super();
}
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(stakeholder);
}
public String getId() {
return id;
public Stakeholder(StakeholderFull stakeholder, StakeholderService service) {
super(stakeholder);
Stakeholder old = service.findByPath(stakeholder.getId());
this.defaultId = old.getDefaultId();
this.standalone = old.isStandalone();
this.topics = old.getTopics();
this.umbrella = old.getUmbrella();
}
public void setId(String id) {
this.id = id;
public void addTopic(String id) {
this.topics.add(id);
}
// public StakeholderType getType() {
// return type;
// }
//
// public void setType(StakeholderType type) {
// this.type = type;
// }
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<StringOrTopic> getTopics() {
return topics;
}
public void setTopics(List<StringOrTopic> topics) {
this.topics = topics;
public void removeTopic(String id) {
this.topics.remove(id);
}
}

View File

@ -1,138 +1,64 @@
package eu.dnetlib.uoamonitorservice.entities;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.springframework.data.annotation.Id;
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.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
public class SubCategory<StringOrSection> {
@Id
@JsonProperty("_id")
private String id;
@Document
public class SubCategory extends SubCategoryGeneric<String> {
public SubCategory() {
super();
}
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<StringOrSection> charts;
private List<StringOrSection> numbers;
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();
super(subCategory);
}
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 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);
}
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 SubCategory copy() {
SubCategory subCategory = new SubCategory(this);
subCategory.setDefaultId(this.getId());
subCategory.setId(null);
return subCategory;
}
public String getId() {
return id;
public SubCategory override(SubCategory subCategory, SubCategory old) {
return (SubCategory) super.override(subCategory, old);
}
public void setId(String id) {
this.id = id;
public void addNumber(String id, int index) {
if(index == -1) {
this.numbers.add(id);
} else {
this.numbers.add(index, id);
}
}
public String getName() {
return name;
public void removeNumber(String id) {
this.numbers.remove(id);
}
public void setName(String name) {
this.name = name;
public void addChart(String id, int index) {
if(index == -1) {
this.charts.add(id);
} else {
this.charts.add(index, id);
}
}
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<StringOrSection> getCharts() {
return charts;
}
public void setCharts(List<StringOrSection> charts) {
this.charts = charts;
}
public List<StringOrSection> getNumbers() {
return numbers;
}
public void setNumbers(List<StringOrSection> numbers) {
this.numbers = numbers;
public void removeChart(String id) {
this.charts.remove(id);
}
}

View File

@ -1,129 +1,58 @@
package eu.dnetlib.uoamonitorservice.entities;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.springframework.data.annotation.Id;
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.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
public class Topic<StringOrCategory> {
@Id
@JsonProperty("_id")
private String id;
@Document
public class Topic extends TopicGeneric<String> {
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<StringOrCategory> categories;
public Topic() {
super();
}
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();
super(topic);
}
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 Topic(TopicFull topic) {
super(topic);
this.categories = topic.getCategories().stream().map(Common::getId).collect(Collectors.toList());
this.categories.removeIf(Objects::isNull);
}
public String getId() {
return id;
public Topic(TopicFull topic, List<String> categories) {
super(topic);
this.categories = new ArrayList<>(categories);
}
public void setId(String id) {
this.id = id;
public Topic copy() {
Topic topic = new Topic(this);
topic.setDefaultId(this.getId());
topic.setId(null);
return topic;
}
public String getName() {
return name;
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;
}
public void setName(String name) {
this.name = name;
public void addCategory(String id) {
this.categories.add(id);
}
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<StringOrCategory> getCategories() {
return categories;
}
public void setCategories(List<StringOrCategory> categories) {
this.categories = categories;
public void removeCategory(String id) {
this.categories.remove(id);
}
}

View File

@ -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<String, Visibility> 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);
// }
}

View File

@ -0,0 +1,43 @@
package eu.dnetlib.uoamonitorservice.generics;
import org.springframework.data.mongodb.core.mapping.Document;
import java.util.ArrayList;
import java.util.List;
@Document
public class CategoryGeneric<T> extends Common {
protected boolean isOverview;
protected List<T> 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();
isOverview = category.getIsOverview();
subCategories = new ArrayList<>();
}
public boolean getIsOverview() {
return isOverview;
}
public void setIsOverview(boolean isOverview) {
this.isOverview = isOverview;
}
public List<T> getSubCategories() {
return subCategories;
}
public void setSubCategories(List<T> subCategories) {
this.subCategories = subCategories;
}
}

View File

@ -0,0 +1,107 @@
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.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 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;
}
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;
}
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;
}
}

View File

@ -0,0 +1,70 @@
package eu.dnetlib.uoamonitorservice.generics;
import eu.dnetlib.uoamonitorservice.primitives.IndicatorType;
import java.util.ArrayList;
import java.util.List;
public class SectionGeneric<T> extends Common {
protected String title;
protected String stakeholderAlias;
protected IndicatorType type;
protected List<T> 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();
indicators = new ArrayList<>();
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
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 boolean isNumber() {
return this.type == IndicatorType.NUMBER || this.type == IndicatorType.number;
}
public List<T> getIndicators() {
return indicators;
}
public void setIndicators(List<T> indicators) {
this.indicators = indicators;
}
}

View File

@ -0,0 +1,186 @@
package eu.dnetlib.uoamonitorservice.generics;
import com.fasterxml.jackson.annotation.JsonIgnore;
import eu.dnetlib.uoamonitorservice.primitives.Locale;
import eu.dnetlib.uoamonitorservice.primitives.StakeholderType;
import eu.dnetlib.uoamonitorservice.primitives.Umbrella;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;
public class StakeholderGeneric<T, S> extends Common {
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 Locale locale = Locale.EU;
protected String funderType;
protected Boolean standalone = true;
protected Boolean copy;
protected List<T> topics = new ArrayList<>();
protected Umbrella<S> umbrella;
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();
this.copy = stakeholder.isCopy();
this.standalone = stakeholder.isStandalone();
creationDate = stakeholder.getCreationDate();
updateDate = stakeholder.getUpdateDate();
}
public String getType() {
if (type == null) {
return null;
}
return type.name();
}
public void setType(String type) {
this.type = StakeholderType.convert(type);
}
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 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 boolean isStandalone() {
if(standalone == null) {
standalone = true;
}
return standalone;
}
public void setStandalone(boolean standalone) {
this.standalone = standalone;
}
public boolean isCopy() {
if(copy == null) {
copy = this.defaultId != null;
}
return copy;
}
public void setCopy(boolean copy) {
this.copy = copy;
}
public List<T> getTopics() {
return topics;
}
public void setTopics(List<T> topics) {
this.topics = topics;
}
public Umbrella<S> getUmbrella() {
return umbrella;
}
@JsonIgnore
public Optional<Umbrella<S>> getUmbrellaOptional() {
return Optional.ofNullable(umbrella);
}
public void setUmbrella(Umbrella<S> umbrella) {
this.umbrella =umbrella;
}
}

View File

@ -0,0 +1,39 @@
package eu.dnetlib.uoamonitorservice.generics;
import java.util.ArrayList;
import java.util.List;
public class SubCategoryGeneric<T> extends Common {
protected List<T> charts;
protected List<T> 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();
numbers = new ArrayList<>();
charts = new ArrayList<>();
}
public List<T> getCharts() {
return charts;
}
public void setCharts(List<T> charts) {
this.charts = charts;
}
public List<T> getNumbers() {
return numbers;
}
public void setNumbers(List<T> numbers) {
this.numbers = numbers;
}
}

View File

@ -0,0 +1,42 @@
package eu.dnetlib.uoamonitorservice.generics;
import java.util.ArrayList;
import java.util.List;
public class TopicGeneric<T> extends Common {
protected String icon;
protected List<T> 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();
categories = new ArrayList<>();
}
public String getIcon() {
return icon;
}
public void setIcon(String icon) {
this.icon = icon;
}
public List<T> getCategories() {
return categories;
}
public void setCategories(List<T> categories) {
this.categories = categories;
}
}

View File

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

View File

@ -25,6 +25,17 @@ public class ExceptionsHandler {
return new ResponseEntity<ExceptionResponse>(response, HttpStatus.NOT_FOUND);
}
@ExceptionHandler(BadRequestException.class)
public ResponseEntity<ExceptionResponse> conflictException(Exception ex) {
ExceptionResponse response = new ExceptionResponse();
response.setErrorCode("Not found Exception");
response.setErrorMessage("Conflict Exception");
response.setErrors(ex.getMessage());
response.setStatus(HttpStatus.BAD_REQUEST);
log.error("conflictException exception : "+ ex.getMessage());
return new ResponseEntity<ExceptionResponse>(response, HttpStatus.BAD_REQUEST);
}
@ExceptionHandler(PathNotValidException.class)
public ResponseEntity<ExceptionResponse> pathNotValidException(Exception ex) {
ExceptionResponse response = new ExceptionResponse();

View File

@ -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<String> edu_person_entitlements = new ArrayList<String>();
@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<String> getEdu_person_entitlements() {
return edu_person_entitlements;
}
public void setEdu_person_entitlements(List<String> edu_person_entitlements) {
this.edu_person_entitlements = edu_person_entitlements;
}
}

View File

@ -0,0 +1,5 @@
package eu.dnetlib.uoamonitorservice.primitives;
public enum Action {
ADD, REMOVE, UPDATE
}

View File

@ -0,0 +1,5 @@
package eu.dnetlib.uoamonitorservice.primitives;
public enum Format {
NUMBER, PERCENTAGE
}

View File

@ -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
@ -56,8 +42,7 @@ public class IndicatorPath {
if(type == null) {
this.type = null;
} else {
IndicatorPathType indicatorPathType = IndicatorPathType.valueOf(type);
this.type = indicatorPathType;
this.type = IndicatorPathType.valueOf(type);
}
}
@ -72,8 +57,7 @@ public class IndicatorPath {
if(format == null) {
this.format = null;
} else {
Format _format = Format.valueOf(format);
this.format = _format;
this.format = Format.valueOf(format);
}
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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;
}
}

View File

@ -0,0 +1,19 @@
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,
publisher, journal,
FUNDER, RI, PROJECT, ORGANIZATION,
COUNTRY, RESEARCHER, DATASOURCE,
PUBLISHER, JOURNAL;
public static StakeholderType convert(String type) {
if(type == null) {
return null;
} else {
return StakeholderType.valueOf(type);
}
}
}

View File

@ -0,0 +1,125 @@
package eu.dnetlib.uoamonitorservice.primitives;
import eu.dnetlib.uoamonitorservice.entities.Stakeholder;
import eu.dnetlib.uoamonitorservice.generics.Common;
import eu.dnetlib.uoamonitorservice.service.StakeholderService;
import java.util.*;
import java.util.stream.Collectors;
public class Umbrella<T> {
List<StakeholderType> types;
Map<StakeholderType, List<T>> children;
public Umbrella() {
this.types = new ArrayList<>();
this.children = new HashMap<>();
}
public Umbrella(List<StakeholderType> types) {
this.types = types;
this.children = new HashMap<>();
}
public static Umbrella<String> convert(Umbrella<Stakeholder> umbrellaFull) {
if(umbrellaFull == null) {
return null;
}
Umbrella<String> umbrella = new Umbrella<>(umbrellaFull.getTypes());
umbrella.types.forEach(type -> umbrella.children.put(type, umbrellaFull.getChildren().get(type).stream().map(Common::getId).collect(Collectors.toList())));
return umbrella;
}
public static Umbrella<Stakeholder> convert(Umbrella<String> umbrella, StakeholderService service) {
if(umbrella == null) {
return null;
}
Umbrella<Stakeholder> umbrellaFull = new Umbrella<>(umbrella.getTypes());
umbrella.types.forEach(type -> umbrellaFull.children.put(type, umbrella.getChildren().get(type).stream().map(service::findByPath).collect(Collectors.toList())));
return umbrellaFull;
}
public Map<StakeholderType, List<T>> getChildren() {
return children;
}
public void setChildren(Map<StakeholderType, List<T>> children) {
this.children = children;
}
public List<StakeholderType> getTypes() {
return types;
}
public void setTypes(List<StakeholderType> types) {
this.types = types;
}
public boolean addType(StakeholderType type) {
if(!this.types.contains(type)) {
this.types.add(type);
this.children.put(type, new ArrayList<>());
return true;
}
return false;
}
public boolean removeType(StakeholderType type) {
if(this.types.contains(type)) {
this.types.remove(type);
this.children.remove(type);
return true;
}
return false;
}
public boolean addChild(StakeholderType type, T child) {
if(this.types.contains(type)) {
if(!this.children.containsKey(type)) {
this.children.put(type, new ArrayList<>());
}
if(!this.children.get(type).contains(child)) {
return this.children.get(type).add(child);
}
}
return false;
}
public boolean removeChild(StakeholderType type, T child) {
if(this.types.contains(type)) {
if(!this.children.containsKey(type)) {
return false;
}
if(this.children.get(type).contains(child)) {
return this.children.get(type).remove(child);
}
}
return false;
}
public boolean update(List<StakeholderType> types) {
if(types == null) {
return false;
} else if(types.size() != this.types.size()) {
return false;
} else {
if(new HashSet<>(this.types).containsAll(types)) {
this.types = types;
return true;
}
return false;
}
}
public boolean update(StakeholderType type, List<T> children) {
if(children == null) {
return false;
} else if(this.children.get(type) == null) {
return false;
} else if(children.size() != this.children.get(type).size()) {
return false;
} else {
return new HashSet<>(this.children.get(type)).containsAll(children);
}
}
}

View File

@ -0,0 +1,5 @@
package eu.dnetlib.uoamonitorservice.primitives;
public enum Visibility {
PUBLIC, RESTRICTED, PRIVATE
}

View File

@ -0,0 +1,217 @@
package eu.dnetlib.uoamonitorservice.service;
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.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;
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;
@Service
public class CategoryService {
private final StakeholderDAO stakeholderDAO;
private final TopicDAO topicDAO;
private final CategoryDAO dao;
private final SubCategoryService subCategoryService;
private final CommonService commonService;
@Autowired
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());
}
return this.dao.findById(categoryId).orElseThrow(() -> new EntityNotFoundException("Category with id: " + categoryId + " not found"));
}
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 {
return null;
}
}
public CategoryFull getFullCategory(String type, String alias, String categoryId) {
Category category = this.find(categoryId);
return this.getFullCategory(type, alias, category);
}
public String build(String id) {
Category category = this.find(id);
Category copy = category.copy();
copy.setSubCategories(category.getSubCategories().stream().map(this.subCategoryService::build).collect(Collectors.toList()));
return this.save(copy).getId();
}
public String copy(String id) {
Category category = this.find(id);
Category copy = new Category(category);
copy.setId(null);
copy.setSubCategories(category.getSubCategories().stream().map(this.subCategoryService::copy).collect(Collectors.toList()));
return this.save(copy).getId();
}
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);
}
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(category.getId()).getSubCategories());
this.updateChildren(category);
category = this.save(category);
} else {
this.commonService.unauthorized("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 {
this.commonService.unauthorized("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 CategoryFull reorderSubCategories(Stakeholder stakeholder, Category category, List<String> 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);
category.setUpdateDate(new Date());
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());
}
} else {
this.commonService.unauthorized("You are not authorized to reorder subCategories in category with id: " + category.getId());
}
return null;
}
public void reorderChildren(Stakeholder defaultStakeholder, Category defaultCategory, List<String> defaultSubCategories) {
this.stakeholderDAO.findByDefaultIdAndCopyIsTrue(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 -> {
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 {
this.commonService.unauthorized("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 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);
});
}
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.update(this.save(new Category(category)));
return category;
} else {
this.commonService.unauthorized("Change category visibility: You are not authorized to update category with id: " + category.getId());
}
return null;
}
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);
}
}

View File

@ -0,0 +1,81 @@
package eu.dnetlib.uoamonitorservice.service;
import eu.dnetlib.uoaadmintoolslibrary.handlers.ForbiddenException;
import eu.dnetlib.uoaadmintoolslibrary.handlers.UnauthorizedException;
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;
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 {
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 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;
} else if(authorizationService.isMember(type, alias) && common.getVisibility() != Visibility.PRIVATE) {
return true;
} else {
return common.getVisibility() == Visibility.PUBLIC;
}
}
public List<String> reorder(List<String> defaultIds, List<Common> commons) {
Map<String, Integer> 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<String> ids = commons.stream().map(Common::getId).collect(Collectors.toList());
map.keySet().forEach(key -> {
ids.add(map.get(key), key);
});
return ids;
}
public void unauthorized(String message) {
if(authorizationService.getAaiId() != null) {
throw new ForbiddenException(message);
} else {
throw new UnauthorizedException(message);
}
}
}

View File

@ -0,0 +1,163 @@
package eu.dnetlib.uoamonitorservice.service;
import eu.dnetlib.uoamonitorservice.dao.*;
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;
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;
@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(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 find(String id) {
return dao.findById(id).orElseThrow(() -> new EntityNotFoundException("Indicator with id: " + id + " not found"));
}
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 getIndicator(String type, String alias, String id) {
Indicator indicator = this.find(id);
if(this.commonService.hasVisibilityAuthority(type, alias, indicator)) {
return indicator;
} else {
return null;
}
}
public String build(String id) {
return this.save(this.find(id).copy()).getId();
}
public String copy(String id) {
Indicator copy = new Indicator(this.find(id));
copy.setId(null);
return this.save(copy).getId();
}
public Indicator save(Indicator indicator) {
if(indicator.getId() == null) {
indicator.setCreationDate(new Date());
}
indicator.setUpdateDate(new Date());
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 {
this.commonService.unauthorized("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 {
this.commonService.unauthorized("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<SubCategory> 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 -> {
this.delete(type, child.getId(), remove);
});
if(remove) {
this.removeIndicator(indicator.getId());
}
this.dao.delete(indicator);
} else {
this.commonService.unauthorized("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 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);
});
}
public Indicator changeVisibility(String type, String alias, Indicator indicator, Visibility visibility) {
if(this.commonService.hasEditAuthority(type, alias)) {
indicator.setVisibility(visibility);
return this.save(indicator);
} else {
this.commonService.unauthorized("Change section visibility: You are not authorized to update section with id: " + indicator.getId());
}
return null;
}
}

View File

@ -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<String, String> checkEverything() {
Map<String, String> 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;
}
}

View File

@ -0,0 +1,245 @@
package eu.dnetlib.uoamonitorservice.service;
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.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.stereotype.Service;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
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;
private final IndicatorService indicatorService;
private final CommonService commonService;
@Autowired
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());
}
return this.dao.findById(sectionId).orElseThrow(() -> new EntityNotFoundException("Section with id: " + sectionId + " not found"));
}
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 SectionFull getFullSection(String type, String alias, String id) {
Section section = this.find(id);
return this.getFullSection(type, alias, section);
}
public SectionFull saveFull(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 String build(String id) {
Section section = this.find(id);
Section copy = section.copy();
copy.setIndicators(section.getIndicators().stream().map(this.indicatorService::build).collect(Collectors.toList()));
return this.save(copy).getId();
}
public String copy(String id) {
Section section = this.find(id);
Section copy = new Section(section);
copy.setId(null);
copy.setIndicators(section.getIndicators().stream().map(this.indicatorService::copy).collect(Collectors.toList()));
return this.save(copy).getId();
}
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);
}
public void saveBulk(Stakeholder stakeholder, SubCategory subCategory, List<SectionFull> sections) {
if (this.commonService.hasCreateAuthority(stakeholder.getType())) {
sections = sections.stream().map(this::saveFull).collect(Collectors.toList());
sections.forEach(section -> {
this.addSection(subCategory, section.getId());
this.createChildren(subCategory, new Section(section), -1);
});
} else {
this.commonService.unauthorized("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) {
if (this.commonService.hasEditAuthority(stakeholder.getType(), stakeholder.getAlias())) {
section.setIndicators(this.find(section.getId()).getIndicators());
this.updateChildren(section);
section = this.save(section);
} else {
this.commonService.unauthorized("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 {
this.commonService.unauthorized("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);
section.getIndicators().forEach(indicator -> {
this.indicatorService.createChildren(section, this.indicatorService.find(indicator));
});
});
});
});
});
}
public void updateChildren(Section section) {
this.dao.findByDefaultId(section.getId()).forEach(child -> {
this.save(section.override(child, this.find(section.getId())));
});
}
public SectionFull reorderIndicators(Stakeholder stakeholder, Section section, List<String> indicators) {
return this.reorderIndicators(stakeholder, section, indicators, true);
}
public SectionFull reorderIndicators(Stakeholder stakeholder, Section section, List<String> indicators, boolean reorderChildren) {
if (this.commonService.hasEditAuthority(stakeholder.getType(), stakeholder.getAlias())) {
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 {
throw new EntityNotFoundException("Some indicators dont exist in the section with id " + section.getId());
}
} else {
this.commonService.unauthorized("You are not authorized to reorder indicators in section with id: " + section.getId());
}
return null;
}
public void reorderChildren(Stakeholder defaultStakeholder, Section defaultSection, List<String> defaultIndicators) {
this.stakeholderDAO.findByDefaultIdAndCopyIsTrue(defaultStakeholder.getId()).forEach(stakeholder -> {
this.dao.findByDefaultId(defaultSection.getId()).stream().map(section -> this.getFullSection(stakeholder.getType(), stakeholder.getAlias(), section)).forEach(section -> {
this.reorderIndicators(stakeholder, new Section(section),
this.commonService.reorder(defaultIndicators, section.getIndicators().stream().map(indicator -> (Common) indicator).collect(Collectors.toList())));
});
});
}
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 {
this.commonService.unauthorized("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 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);
} 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);
});
}
public SectionFull changeVisibility(String type, String alias, SectionFull section, Visibility visibility) {
if (this.commonService.hasEditAuthority(type, alias)) {
section.setIndicators(section.getIndicators().stream()
.map(indicator -> this.indicatorService.changeVisibility(type, alias, indicator, visibility))
.collect(Collectors.toList()));
section.update(this.save(new Section(section)));
return section;
} else {
this.commonService.unauthorized("Change section visibility: You are not authorized to update section with id: " + section.getId());
}
return null;
}
}

View File

@ -1,42 +1,339 @@
package eu.dnetlib.uoamonitorservice.service;
import eu.dnetlib.uoaadmintoolslibrary.handlers.utils.RolesUtils;
import eu.dnetlib.uoamonitorservice.dao.StakeholderDAO;
import eu.dnetlib.uoamonitorservice.dto.ManageStakeholders;
import eu.dnetlib.uoamonitorservice.dto.StakeholderFull;
import eu.dnetlib.uoamonitorservice.dto.TopicFull;
import eu.dnetlib.uoamonitorservice.dto.UpdateUmbrella;
import eu.dnetlib.uoamonitorservice.entities.Stakeholder;
import eu.dnetlib.uoamonitorservice.entities.Visibility;
import eu.dnetlib.uoamonitorservice.generics.Common;
import eu.dnetlib.uoamonitorservice.handlers.BadRequestException;
import eu.dnetlib.uoamonitorservice.handlers.EntityNotFoundException;
import eu.dnetlib.uoamonitorservice.handlers.PathNotValidException;
import eu.dnetlib.uoamonitorservice.primitives.Action;
import eu.dnetlib.uoamonitorservice.primitives.StakeholderType;
import eu.dnetlib.uoamonitorservice.primitives.Umbrella;
import eu.dnetlib.uoamonitorservice.primitives.Visibility;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.*;
import java.util.stream.Collectors;
@Service
public class StakeholderService {
@Autowired
StakeholderDAO dao;
private final StakeholderDAO dao;
private final CommonService commonService;
private final TopicService topicService;
private final MongoTemplate mongoTemplate;
@Autowired
RolesUtils rolesUtils;
public StakeholderService(StakeholderDAO dao, CommonService commonService, TopicService topicService, MongoTemplate mongoTemplate) {
this.dao = dao;
this.commonService = commonService;
this.topicService = topicService;
this.mongoTemplate = mongoTemplate;
}
public List<Stakeholder> getStakeholdersByTypeAndRole(String type, String defaultId, boolean manage) {
List<Stakeholder> stakeholders;
if(type != null && defaultId != null) {
stakeholders = dao.findByDefaultIdAndType(defaultId, type);
} else if(defaultId != null) {
stakeholders = dao.findByDefaultId(defaultId);
} else if(type != null) {
stakeholders = dao.findByDefaultIdNotAndType(null, type);
} else {
stakeholders = dao.findByDefaultIdNot(null);
public Stakeholder findByAlias(String alias) {
return this.dao.findByAlias(alias).orElseThrow(() -> new EntityNotFoundException("Stakeholder with alias: " + alias + " not found"));
}
public List<Stakeholder> findByDefaultId(String id) {
return this.dao.findByDefaultIdAndCopyIsTrue(id);
}
public Stakeholder findByPath(String stakeholderId) {
if (stakeholderId.equals("-1")) {
return 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.getAlias()))
return dao.findById(stakeholderId).orElseThrow(() -> new EntityNotFoundException("Stakeholder with id: " + stakeholderId + " not found"));
}
public List<String> getAllAliases(String type) {
return this.getAll(type).stream().map(Stakeholder::getAlias).collect(Collectors.toList());
}
public List<Stakeholder> getAll(String type) {
return this.dao.allStakeholders(StakeholderType.convert(type));
}
public List<Stakeholder> getAccessedStakeholders(List<Stakeholder> stakeholders, boolean isDefault) {
return stakeholders.stream()
.filter(stakeholder -> this.commonService.hasAccessAuthority(stakeholder.getType(), stakeholder.getAlias(), isDefault))
.collect(Collectors.toList());
}
public ManageStakeholders getManageStakeholders(String type) {
ManageStakeholders manageStakeholders = new ManageStakeholders();
StakeholderType stakeholderType = StakeholderType.convert(type);
manageStakeholders.setTemplates(this.getAccessedStakeholders(this.dao.defaultStakeholders(stakeholderType), true));
manageStakeholders.setStandalone(this.getAccessedStakeholders(this.dao.standaloneStakeholders(stakeholderType), false));
manageStakeholders.setDependent(this.getAccessedStakeholders(this.dao.dependentStakeholders(stakeholderType), false));
manageStakeholders.setUmbrella(this.getAccessedStakeholders(this.dao.umbrellaStakeholders(stakeholderType), false));
return manageStakeholders;
}
public List<Stakeholder> getVisibleStakeholders(String type, String defaultId) {
return this.dao.browseStakeholders(StakeholderType.convert(type), defaultId).stream().filter(stakeholder ->
stakeholder.getVisibility() == Visibility.PUBLIC ||
stakeholder.getVisibility() == Visibility.RESTRICTED ||
this.commonService.hasAccessAuthority(stakeholder.getType(), stakeholder.getAlias(), false))
.collect(Collectors.toList());
}
public StakeholderFull getFullStakeholder(Stakeholder stakeholder) {
List<TopicFull> topics = stakeholder.getTopics().stream()
.map(topicId -> topicService.getFullTopic(stakeholder.getType(), stakeholder.getAlias(), topicId))
.collect(Collectors.toList());
if (!stakeholder.isCopy() && stakeholder.getDefaultId() != null) {
Stakeholder defaultStakeholder = this.findByPath(stakeholder.getDefaultId());
if (defaultStakeholder != null) {
topics = defaultStakeholder.getTopics().stream()
.map(topicId -> topicService.getFullTopic(stakeholder.getType(), stakeholder.getAlias(), topicId))
.collect(Collectors.toList());
}
}
Umbrella<Stakeholder> umbrella = Umbrella.convert(stakeholder.getUmbrella(), this);
return new StakeholderFull(stakeholder, topics, umbrella);
}
private List<Stakeholder> getOtherParents(String parent, String type, String id) {
Query query = new Query();
query.addCriteria(Criteria.where("alias").ne(parent).and("umbrella.children." + type).regex(id, "i"));
return mongoTemplate.find(query, Stakeholder.class);
}
private Optional<Stakeholder> getActiveParent(String parent, String type, String id) {
Query query = new Query();
query.addCriteria(Criteria.where("alias").is(parent).and("umbrella.children." + type).regex(id, "i"));
return Optional.ofNullable(mongoTemplate.findOne(query, Stakeholder.class));
}
public StakeholderFull getFullStakeholderWithParents(Stakeholder stakeholder, String type, String parent) {
StakeholderFull stakeholderFull = this.getFullStakeholder(stakeholder);
if (stakeholderFull != null) {
stakeholderFull.setParent(this.getActiveParent(parent, type, stakeholder.getId())
.orElseThrow(() -> new EntityNotFoundException("Stakeholder with alias: " + stakeholder.getAlias() + " not found in stakeholder " + parent)));
stakeholderFull.setOtherParents(this.getOtherParents(parent, type, stakeholder.getId()));
}
return stakeholderFull;
}
public Stakeholder buildStakeholder(Stakeholder stakeholder, String copyId) {
if (stakeholder.getDefaultId() == null) {
stakeholder.setCopy(false);
if (copyId == null) {
stakeholder.setTopics(new ArrayList<>());
} else {
Stakeholder copyFrom = this.findByPath(copyId);
stakeholder.setTopics(copyFrom.getTopics().stream().map(this.topicService::copy).collect(Collectors.toList()));
}
} else {
stakeholder.setTopics(new ArrayList<>());
if (stakeholder.isCopy()) {
Stakeholder defaultStakeholder = this.findByPath(stakeholder.getDefaultId());
if (defaultStakeholder != null) {
stakeholder.setTopics(defaultStakeholder.getTopics().stream().map(this.topicService::build).collect(Collectors.toList()));
}
}
}
return this.save(stakeholder);
}
public Stakeholder save(Stakeholder stakeholder) {
if (stakeholder.getId() != null) {
Stakeholder old = this.findByPath(stakeholder.getId());
stakeholder.setUmbrella(old.getUmbrella());
stakeholder.setStandalone(old.isStandalone());
stakeholder.setDefaultId(old.getDefaultId());
if (!stakeholder.isCopy() && stakeholder.getDefaultId() != null) {
stakeholder.getTopics().forEach(topic -> {
this.topicService.delete(stakeholder.getType(), topic, false);
});
stakeholder.setTopics(new ArrayList<>());
} else {
if (old.getTopics().isEmpty() && old.getDefaultId() != null) {
Stakeholder defaultStakeholder = this.findByPath(stakeholder.getDefaultId());
if (defaultStakeholder != null) {
stakeholder.setTopics(defaultStakeholder.getTopics().stream().map(this.topicService::build).collect(Collectors.toList()));
}
}
stakeholder.getTopics().forEach(this.topicService::find);
}
} else {
stakeholder.setCreationDate(new Date());
}
stakeholder.setUpdateDate(new Date());
return this.dao.save(stakeholder);
}
public StakeholderFull reorderTopics(Stakeholder stakeholder, List<String> 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);
stakeholder.setUpdateDate(new Date());
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());
}
} else {
this.commonService.unauthorized("You are not authorized to reorder topics in stakeholder with id: " + stakeholder.getId());
}
return null;
}
public void reorderChildren(Stakeholder defaultStakeholder, List<String> defaultTopics) {
this.dao.findByDefaultIdAndCopyIsTrue(defaultStakeholder.getId()).stream().map(this::getFullStakeholder).forEach(stakeholder -> {
this.reorderTopics(new Stakeholder(stakeholder, this),
this.commonService.reorder(defaultTopics, stakeholder.getTopics().stream().map(topic -> (Common) topic).collect(Collectors.toList())));
});
}
public String delete(String id) {
Stakeholder stakeholder = this.findByPath(id);
if (this.commonService.hasDeleteAuthority(stakeholder.getType())) {
this.dao.umbrellaStakeholders().forEach(umbrellaStakeholder -> {
StakeholderType type = StakeholderType.valueOf(stakeholder.getType());
List<String> ids = umbrellaStakeholder.getUmbrella().getChildren().get(type);
if(ids != null && ids.contains(stakeholder.getId())) {
this.removeChild(umbrellaStakeholder.getId(), StakeholderType.valueOf(stakeholder.getType()), stakeholder.getId());
}
});
this.dao.findByDefaultIdAndCopyIsTrue(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 {
this.commonService.unauthorized("Delete stakeholder: You are not authorized to delete stakeholder with id: " + id);
}
return null;
}
public StakeholderFull changeVisibility(StakeholderFull stakeholder, Visibility visibility, Boolean 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()));
}
stakeholder.setVisibility(visibility);
stakeholder.update(this.save(new Stakeholder(stakeholder, this)));
return stakeholder;
} else {
this.commonService.unauthorized("Change stakeholder visibility: You are not authorized to update stakeholder with id: " + stakeholder.getId());
}
return null;
}
public StakeholderFull changeVisibility(Stakeholder stakeholder, Visibility visibility, Boolean propagate) {
StakeholderFull stakeholderFull = this.getFullStakeholder(stakeholder);
return this.changeVisibility(stakeholderFull, visibility, propagate);
}
public Umbrella<Stakeholder> updateUmbrella(Stakeholder stakeholder, UpdateUmbrella update) {
if (this.commonService.hasEditAuthority(stakeholder.getType(), stakeholder.getAlias())) {
if(update.getAction().equals(Action.ADD)) {
if(update.getChild() != null) {
return this.addChild(stakeholder.getId(), update.getType(), update.getChild());
} else {
return this.addType(stakeholder.getId(), update.getType());
}
} else if(update.getAction().equals(Action.REMOVE)) {
if(update.getChild() != null) {
return this.removeChild(stakeholder.getId(), update.getType(), update.getChild());
} else {
return this.removeType(stakeholder.getId(), update.getType());
}
} else if(update.getAction().equals(Action.UPDATE)) {
if(update.getType() != null) {
return this.updateChildren(stakeholder.getId(), update.getType(), update.getChildren());
} else {
return this.updateTypes(stakeholder.getId(), update.getTypes());
}
}
} else {
this.commonService.unauthorized("You are not authorized to update umbrella in stakeholder with id: " + stakeholder.getId());
}
return null;
}
public Umbrella<Stakeholder> addType(String id, StakeholderType type) {
Stakeholder stakeholder = this.findByPath(id);
Umbrella<String> umbrella = stakeholder.getUmbrellaOptional().orElseThrow(() -> new EntityNotFoundException("Umbrella not found in the stakeholder with id " + id));
if (umbrella.addType(type)) {
stakeholder.setUmbrella(umbrella);
stakeholder.setUpdateDate(new Date());
return this.getFullStakeholder(this.dao.save(stakeholder)).getUmbrella();
}
throw new BadRequestException("Cannot add type: " + type + " to stakeholder with id " + id);
}
public Umbrella<Stakeholder> removeType(String id, StakeholderType type) {
Stakeholder stakeholder = this.findByPath(id);
Umbrella<String> umbrella = stakeholder.getUmbrellaOptional().orElseThrow(() -> new EntityNotFoundException("Umbrella not found in the stakeholder with id " + id));
if (umbrella.removeType(type)) {
stakeholder.setUmbrella(umbrella);
stakeholder.setUpdateDate(new Date());
return this.getFullStakeholder(this.dao.save(stakeholder)).getUmbrella();
}
throw new BadRequestException("Cannot add type: " + type + " to stakeholder with id " + id);
}
public Umbrella<Stakeholder> addChild(String id, StakeholderType type, String childId) {
Stakeholder stakeholder = this.findByPath(id);
Umbrella<String> umbrella = stakeholder.getUmbrellaOptional().orElseThrow(() -> new EntityNotFoundException("Umbrella not found in the stakeholder with id " + id));
Stakeholder child = this.findByPath(childId);
if (child.getType().equals(type.name()) && umbrella.addChild(type, childId)) {
stakeholder.setUmbrella(umbrella);
stakeholder.setUpdateDate(new Date());
return this.getFullStakeholder(this.dao.save(stakeholder)).getUmbrella();
}
throw new BadRequestException("Cannot add child: " + childId + " to stakeholder with id " + id);
}
public Umbrella<Stakeholder> removeChild(String id, StakeholderType type, String childId) {
Stakeholder stakeholder = this.findByPath(id);
Umbrella<String> umbrella = stakeholder.getUmbrellaOptional().orElseThrow(() -> new EntityNotFoundException("Umbrella not found in the stakeholder with id " + id));
Stakeholder child = this.findByPath(childId);
if (child.getType().equals(type.name()) && umbrella.removeChild(type, childId)) {
stakeholder.setUmbrella(umbrella);
stakeholder.setUpdateDate(new Date());
return this.getFullStakeholder(this.dao.save(stakeholder)).getUmbrella();
}
throw new BadRequestException("Cannot remove child: " + childId + " to stakeholder with id " + id);
}
public Umbrella<Stakeholder> updateTypes(String id, List<StakeholderType> types) {
Stakeholder stakeholder = this.findByPath(id);
Umbrella<String> umbrella = stakeholder.getUmbrellaOptional().orElseThrow(() -> new EntityNotFoundException("Umbrella not found in the stakeholder with id " + id));
if (stakeholder.getUmbrella().update(types)) {
stakeholder.setUmbrella(umbrella);
stakeholder.setUpdateDate(new Date());
return this.getFullStakeholder(this.dao.save(stakeholder)).getUmbrella();
}
throw new BadRequestException("Cannot update types in umbrella of stakeholder with id " + id);
}
public Umbrella<Stakeholder> updateChildren(String id, StakeholderType type, List<String> children) {
Stakeholder stakeholder = this.findByPath(id);
Umbrella<String> umbrella = stakeholder.getUmbrellaOptional().orElseThrow(() -> new EntityNotFoundException("Umbrella not found in the stakeholder with id " + id));
if (stakeholder.getUmbrella().update(type, children)) {
stakeholder.setUmbrella(umbrella);
stakeholder.setUpdateDate(new Date());
return this.getFullStakeholder(this.dao.save(stakeholder)).getUmbrella();
}
throw new BadRequestException("Cannot update children of " + type + " in umbrella of stakeholder with id " + id);
}
}

View File

@ -0,0 +1,296 @@
package eu.dnetlib.uoamonitorservice.service;
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.*;
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.stereotype.Service;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
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;
private final CommonService commonService;
private final SectionService sectionService;
@Autowired
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());
}
return this.dao.findById(subcategoryId).orElseThrow(() -> new EntityNotFoundException("SubCategory with id: " + subcategoryId + " not found"));
}
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))
.collect(Collectors.toList()),
subCategory.getCharts().stream()
.map(sectionId -> this.sectionService.getFullSection(type, alias, sectionId))
.collect(Collectors.toList()));
} else {
return null;
}
}
public SubCategoryFull getFullSubCategory(String type, String alias, String subCategoryId) {
SubCategory subCategory = this.find(subCategoryId);
return this.getFullSubCategory(type, alias, subCategory);
}
public String build(String id) {
SubCategory subCategory = this.find(id);
SubCategory copy = subCategory.copy();
copy.setNumbers(subCategory.getNumbers().stream().map(this.sectionService::build).collect(Collectors.toList()));
copy.setCharts(subCategory.getCharts().stream().map(this.sectionService::build).collect(Collectors.toList()));
return this.save(copy).getId();
}
public String copy(String id) {
SubCategory subCategory = this.find(id);
SubCategory copy = new SubCategory(subCategory);
copy.setId(null);
copy.setNumbers(subCategory.getNumbers().stream().map(this.sectionService::copy).collect(Collectors.toList()));
copy.setCharts(subCategory.getCharts().stream().map(this.sectionService::copy).collect(Collectors.toList()));
return this.save(copy).getId();
}
public SubCategory save(SubCategory subCategory) {
if(subCategory.getId() != null) {
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);
}
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 {
this.commonService.unauthorized("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 {
this.commonService.unauthorized("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 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 {
this.commonService.unauthorized("You are not authorized to move indicators in subCategory with id: " + subCategory.getId());
}
return null;
}
public void moveIndicatorChildren(Stakeholder defaultStakeholder, SubCategory defaultSubCategory, MoveIndicator moveIndicator) {
this.stakeholderDAO.findByDefaultIdAndCopyIsTrue(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<String> 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 {
throw new EntityNotFoundException("Some sections dont exist in the subCategory with id " + subCategory.getId());
}
} else {
this.commonService.unauthorized("You are not authorized to reorder sections in subCategory with id: " + subCategory.getId());
}
return null;
}
public SubCategoryFull reorderCharts(Stakeholder stakeholder, SubCategory subCategory, List<String> 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);
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());
}
} else {
this.commonService.unauthorized("You are not authorized to reorder sections in subCategory with id: " + subCategory.getId());
}
return null;
}
public void reorderChildrenNumbers(Stakeholder defaultStakeholder, SubCategory defaultSubCategory, List<String> defaultSections) {
this.stakeholderDAO.findByDefaultIdAndCopyIsTrue(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<String> defaultSections) {
this.stakeholderDAO.findByDefaultIdAndCopyIsTrue(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 -> {
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 {
this.commonService.unauthorized("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 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);
});
}
public SubCategoryFull changeVisibility(String type, String alias, SubCategoryFull subCategory, Visibility visibility, Boolean propagate) {
if(this.commonService.hasEditAuthority(type, alias)) {
subCategory.setVisibility(visibility);
if(propagate) {
subCategory.setNumbers(subCategory.getNumbers().stream()
.map(section -> this.sectionService.changeVisibility(type, alias, section, visibility))
.collect(Collectors.toList()));
subCategory.setCharts(subCategory.getCharts().stream()
.map(section -> this.sectionService.changeVisibility(type, alias, section, visibility))
.collect(Collectors.toList()));
}
subCategory.update(this.save(new SubCategory(subCategory)));
return subCategory;
} else {
this.commonService.unauthorized("Change subCategory visibility: You are not authorized to update subCategory with id: " + subCategory.getId());
}
return null;
}
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);
}
}

View File

@ -0,0 +1,204 @@
package eu.dnetlib.uoamonitorservice.service;
import eu.dnetlib.uoamonitorservice.dao.StakeholderDAO;
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;
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;
@Service
public class TopicService {
private final StakeholderDAO stakeholderDAO;
private final TopicDAO dao;
private final CategoryService categoryService;
private final CommonService commonService;
@Autowired
public TopicService(StakeholderDAO stakeholderDAO, TopicDAO dao, CategoryService categoryService, CommonService commonService) {
this.stakeholderDAO = stakeholderDAO;
this.dao = dao;
this.categoryService = categoryService;
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());
}
return this.dao.findById(topicId).orElseThrow(() -> new EntityNotFoundException("Topic with id: " + topicId + " not found"));
}
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))
.collect(Collectors.toList()));
} else {
return null;
}
}
public TopicFull getFullTopic(String type, String alias, String id) {
Topic topic = this.find(id);
return this.getFullTopic(type, alias, topic);
}
public String build(String id) {
Topic topic = this.find(id);
Topic copy = topic.copy();
copy.setCategories(topic.getCategories().stream().map(this.categoryService::build).collect(Collectors.toList()));
return this.save(copy).getId();
}
public String copy(String id) {
Topic topic = this.find(id);
Topic copy = new Topic(topic);
copy.setId(null);
copy.setCategories(topic.getCategories().stream().map(this.categoryService::copy).collect(Collectors.toList()));
return this.save(copy).getId();
}
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);
}
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 {
this.commonService.unauthorized("You are not authorized to update stakeholder with id: " + stakeholder.getId());
}
} else {
if(this.commonService.hasCreateAuthority(stakeholder.getType())) {
topic = this.save(topic);
this.createChildren(stakeholder, topic);
this.addTopic(stakeholder, topic.getId());
} else {
this.commonService.unauthorized("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.findByDefaultIdAndCopyIsTrue(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 TopicFull reorderCategories(Stakeholder stakeholder, Topic topic, List<String> 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);
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());
}
} else {
this.commonService.unauthorized("You are not authorized to reorder categories in topic with id: " + topic.getId());
}
return null;
}
public void reorderChildren(Stakeholder defaultStakeholder, Topic defaultTopic, List<String> defaultCategories) {
this.stakeholderDAO.findByDefaultIdAndCopyIsTrue(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 -> {
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 {
this.commonService.unauthorized("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 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);
});
}
public TopicFull changeVisibility(String type, String alias, TopicFull topic, Visibility visibility, Boolean propagate) {
if (this.commonService.hasEditAuthority(type, alias)) {
topic.setVisibility(visibility);
if (propagate) {
topic.setCategories(topic.getCategories().stream()
.map(category -> this.categoryService.changeVisibility(type, alias, category, visibility, true))
.collect(Collectors.toList()));
}
topic.update(this.save(new Topic(topic)));
return topic;
} else {
this.commonService.unauthorized("Change topic visibility: You are not authorized to update topic with id: " + topic.getId());
}
return null;
}
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);
}
}

View File

@ -25,4 +25,3 @@ monitorservice.globalVars.version=@version@
#production
#monitorservice.userInfoUrl = https://services.openaire.eu/uoa-user-management/api/users/getUserInfo?accessToken=
#monitorservice.originServer = .openaire.eu

View File

@ -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<stakeholders.length; i++) {
stakeholder = stakeholders[i];
stakeholderType = stakeholder.type;
stakeholder.type = stakeholderType.toUpperCase();
db.stakeholder.save(stakeholder);
}
print("Uppercase enum values for Stakeholder types");
sections = db.section.find().map(function (sections) {
return sections;
});
for(var i=0; i<sections.length; i++) {
section = sections[i];
sectionType = section.type;
section.type = sectionType.toUpperCase();
db.section.save(section);
}
print("Uppercase enum values for Section types");
indicators = db.indicator.find().map(function (indicators) {
return indicators;
});
for(var i=0; i<indicators.length; i++) {
indicator = indicators[i];
indicatorType = indicator.type;
indicator.type = indicatorType.toUpperCase();
indicatorWidth = indicator.width;
indicator.width = indicatorWidth.toUpperCase();
indicatorPaths = indicator.indicatorPaths;
for(var j=0; j<indicatorPaths.lenght; j++) {
indicatorPath = indicatorPaths[j];
indicatorPathType = indicatorPath.type;
indicatorPath.type = indicatorPathType.toUpperCase();
indicatorPathSource = indicatorPath.source;
indicatorPath.source = indicatorPathSource.toUpperCase();
}
db.indicator.save(indicator);
}
print("Uppercase enum values for Indicator types and width");
print("Uppercase enum values for Indicator path types and source");
}
function addHeightInIndicators() {
indicators = db.indicator.find().map(function (indicators) {
return indicators;
});
for(var i=0; i<indicators.length; i++) {
indicator = indicators[i];
if(indicator.type == "chart" || indicator.type == "CHART") {
indicator['height'] = "MEDIUM";
} else {
indicator['height'] = "SMALL";
}
db.indicator.save(indicator);
}
print("Height field added in Indicators");
}
function addVisibility() {
stakeholders = db.stakeholder.find().map(function (stakeholders) {
return stakeholders;
});
for (var i = 0; i < stakeholders.length; i++) {
stakeholder = stakeholders[i];
if (stakeholder.isActive) {
if (stakeholder.isPublic) {
stakeholder['visibility'] = "PUBLIC";
} else {
stakeholder['visibility'] = "RESTRICTED";
}
} else {
stakeholder['visibility'] = "PRIVATE";
}
db.stakeholder.save(stakeholder);
}
print("Add visibility field for Stakeholders");
topics = db.topic.find().map(function (topics) {
return topics;
});
for (var i = 0; i < topics.length; i++) {
topic = topics[i];
topicVisibility = "PRIVATE";
if (topic.isActive) {
if (topic.isPublic) {
topicVisibility = "PUBLIC";
} else {
topicVisibility = "RESTRICTED";
}
}
topic['visibility'] = topicVisibility;
db.topic.save(topic);
}
print("Add visibility field for Topics");
categories = db.category.find().map(function (categories) {
return categories;
});
for (var i = 0; i < categories.length; i++) {
category = categories[i];
if (category.isActive) {
if (category.isPublic) {
category['visibility'] = "PUBLIC";
} else {
category['visibility'] = "RESTRICTED";
}
} else {
category['visibility'] = "PRIVATE";
}
db.category.save(category);
}
print("Add visibility field for Categories");
subCategories = db.subCategory.find().map(function (subCategories) {
return subCategories;
});
for (var i = 0; i < subCategories.length; i++) {
subCategory = subCategories[i];
if (subCategory.isActive) {
if (subCategory.isPublic) {
subCategory['visibility'] = "PUBLIC";
} else {
subCategory['visibility'] = "RESTRICTED";
}
} else {
subCategory['visibility'] = "PRIVATE";
}
db.subCategory.save(subCategory);
}
print("Add visibility field for SubCategories");
indicators = db.indicator.find().map(function (indicators) {
return indicators;
});
for (var i = 0; i < indicators.length; i++) {
indicator = indicators[i];
if (indicator.isActive) {
if (indicator.isPublic) {
indicator['visibility'] = "PUBLIC";
} else {
indicator['visibility'] = "RESTRICTED";
}
} else {
indicator['visibility'] = "PRIVATE";
}
db.indicator.save(indicator);
}
print("Add visibility field for Indicators");
}
function removeIsActiveAndIsPublic() {
stakeholders = db.stakeholder.find().map(function (stakeholders) {
return stakeholders;
});
for(var i=0; i<stakeholders.length; i++) {
stakeholder = stakeholders[i];
db.stakeholder.update({"_id": stakeholder._id}, {$unset: {isPublic: "", isActive: ""}});
}
topics = db.topic.find().map(function (topics) {
return topics;
});
for(var i=0; i<topics.length; i++) {
topic = topics[i];
db.topic.update({"_id": topic._id}, {$unset: {isPublic: "", isActive: ""}});
}
categories = db.category.find().map(function (categories) {
return categories;
});
for(var i=0; i<categories.length; i++) {
category = categories[i];
db.category.update({"_id": category._id}, {$unset: {isPublic: "", isActive: ""}});
}
subCategories = db.subCategory.find().map(function (subCategories) {
return subCategories;
});
for(var i=0; i<subCategories.length; i++) {
subCategory = subCategories[i];
db.subCategory.update({"_id": subCategory._id}, {$unset: {isPublic: "", isActive: ""}});
}
indicators = db.indicator.find().map(function (indicators) {
return indicators;
});
for(var i=0; i<indicators.length; i++) {
indicator = indicators[i];
db.indicator.update({"_id": indicator._id}, {$unset: {isPublic: "", isActive: ""}});
}
}
function addAdminToolsCollections () {
db.createCollection("portal");
db.createCollection("entity");
db.createCollection("page");
db.createCollection("pageHelpContent");
db.createCollection("divId");
db.createCollection("divHelpContent");
}
function addPortals() {
db.portal.save({"pid": "monitor", "name": "Monitor", "type": "monitor", "piwik": null, "pages": {}, "entities": {}});
}
function uniqueIndexes() {
db.portal.createIndex( { "pid": 1 }, { unique: true } );
db.stakeholder.createIndex( { "alias": 1 }, { unique: true } );
}
function addFooterDivIdForPortalType(portalType) {
homePageID = db.page.find({"portalType": portalType, "route": "/"}).map( function(portal) { return portal._id.str; } ).toString();
db.divId.save({ "name" : "footer", "pages" : [ homePageID ], "portalType" : portalType });
print("divId 'footer' added for "+portalType+" (home page id: "+homePageID + ")");
}
function addHomePageInPortalType(portalType) {
homePageID = db.page.insertOne({ "route" : "/", "name" : "Home", "type" : "other", "entities" : [ ], "portalType" : portalType, "top" : false, "bottom" : false, "left" : false, "right" : false }).insertedId.str;
print("Creating Home page with id " + homePageID);
portals = db.portal.find({"type": portalType}).map( function(portal) { return portal; } );
for (var i = 0; i < portals.length; i++) {
portal_pages = portals[i].pages;
portal_pages[homePageID] = true;
portal_pid = portals[i].pid;
db.portal.update({ "pid" : portal_pid },{$set: { "pages": portal_pages}});
print("Add home page with id " + homePageID + " on " + portalType + " " + portal_pid);
}
}
function addFooterHelpTextForPortalType(portalType) {
footerDivIdID = db.divId.find({"portalType": portalType, "name": "footer"}).map( function(divId) { return divId._id.str; } ).toString();
portals = db.portal.find({"type": portalType}).map( function(portal) { return portal; } );
for (var j = 0; j < portals.length; j++) {
portal = portals[j];
var content = "This OpenAIRE MONITOR dashboard is part of a project that has received funding from the European Union's Horizon 2020 research and innovation programme under grant agreements No. 777541 and 101017452";
if(portal.pid == "egi") {
content = "This OpenAIRE MONITOR dashboard is part of a project that has received funding from the European Union's Horizon 2020 research and innovation programme under grant agreements No. 777541 and 101017452";
} else if(portal.pid == "risis") {
content = "This OpenAIRE MONITOR dashboard is part of a project that has received funding from the European Union's Horizon 2020 research and innovation programme under grant agreements No. 777541 and 101017452 and 824091";
} else if(portal.pid == "sobigdata") {
content = "This OpenAIRE MONITOR dashboard is part of a project that has received funding from the European Union's Horizon 2020 research and innovation programme under grant agreements No. 777541 and 101017452 and 871042";
}
db.divHelpContent.save(
{ "divId" : footerDivIdID,
"portal" : portal._id.str,
"content" : "<p class=\"uk-margin-remove-bottom\"><span style=\"font-size:8pt\">"+content+"</span></p>",
"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();