diff --git a/pom.xml b/pom.xml index bf847c0..f0ff326 100644 --- a/pom.xml +++ b/pom.xml @@ -18,6 +18,12 @@ + + + + + + UTF-8 UTF-8 @@ -56,6 +62,11 @@ gson 2.8.2 + + eu.dnetlib + uoa-help-texts-library + 2.0.0-SNAPSHOT + diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/UoaMonitorServiceApplication.java b/src/main/java/eu/dnetlib/uoamonitorservice/UoaMonitorServiceApplication.java index 35eb3f3..1b0b5ce 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/UoaMonitorServiceApplication.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/UoaMonitorServiceApplication.java @@ -1,7 +1,5 @@ package eu.dnetlib.uoamonitorservice; -import eu.dnetlib.uoamonitorservice.configuration.properties.GoogleConfig; -import eu.dnetlib.uoamonitorservice.configuration.properties.MailConfig; import eu.dnetlib.uoamonitorservice.configuration.properties.MongoConfig; import eu.dnetlib.uoamonitorservice.configuration.properties.SecurityConfig; import org.springframework.boot.SpringApplication; @@ -10,15 +8,16 @@ import org.springframework.boot.context.properties.EnableConfigurationProperties import org.springframework.context.annotation.PropertySource; import org.springframework.context.annotation.PropertySources; -@SpringBootApplication +@SpringBootApplication(scanBasePackages = {"eu.dnetlib.uoamonitorservice", "eu.dnetlib.uoahelptexts"}) @PropertySources({ @PropertySource("classpath:monitorservice.properties"), @PropertySource(value = "file:/usr/share/tomcat7/lib/dnet-override.properties", ignoreResourceNotFound = true), - @PropertySource(value = "file:/var/lib/tomcat_dnet/8380/lib/dnet-override.properties", ignoreResourceNotFound = true) + @PropertySource(value = "file:/var/lib/tomcat_dnet/8380/lib/dnet-override.properties", ignoreResourceNotFound = true), + @PropertySource(value = "file:/var/lib/tomcat8/lib/dnet-override.properties", ignoreResourceNotFound = true) }) -@EnableConfigurationProperties({SecurityConfig.class, MailConfig.class, GoogleConfig.class, MongoConfig.class}) +@EnableConfigurationProperties({SecurityConfig.class, MongoConfig.class}) public class UoaMonitorServiceApplication { public static void main(String[] args) { diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/configuration/mongo/MongoConnection.java b/src/main/java/eu/dnetlib/uoamonitorservice/configuration/mongo/MongoConnection.java index 3a75740..f208afd 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/configuration/mongo/MongoConnection.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/configuration/mongo/MongoConnection.java @@ -16,7 +16,7 @@ import org.springframework.data.mongodb.repository.config.EnableMongoRepositorie import java.util.Collections; @Configuration -@EnableMongoRepositories(basePackages = {"eu.dnetlib.uoamonitorservice.dao"}) +@EnableMongoRepositories(basePackages = {"eu.dnetlib.uoamonitorservice.dao", "eu.dnetlib.uoahelptexts.dao"}) public class MongoConnection { @Autowired diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/configuration/properties/GoogleConfig.java b/src/main/java/eu/dnetlib/uoamonitorservice/configuration/properties/GoogleConfig.java deleted file mode 100644 index fb3d613..0000000 --- a/src/main/java/eu/dnetlib/uoamonitorservice/configuration/properties/GoogleConfig.java +++ /dev/null @@ -1,17 +0,0 @@ -package eu.dnetlib.uoamonitorservice.configuration.properties; - -import org.springframework.boot.context.properties.ConfigurationProperties; - -@ConfigurationProperties("monitorservice.google") -public class GoogleConfig { - - private String secret; - - public String getSecret() { - return secret; - } - - public void setSecret(String secret) { - this.secret = secret; - } -} diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/configuration/properties/MailConfig.java b/src/main/java/eu/dnetlib/uoamonitorservice/configuration/properties/MailConfig.java deleted file mode 100644 index 6405828..0000000 --- a/src/main/java/eu/dnetlib/uoamonitorservice/configuration/properties/MailConfig.java +++ /dev/null @@ -1,65 +0,0 @@ -package eu.dnetlib.uoamonitorservice.configuration.properties; - -import org.springframework.boot.context.properties.ConfigurationProperties; - -@ConfigurationProperties("monitorservice.mail") -public class MailConfig { - - private String host; - private String port; - private String auth; - private String from; - private String username; - private String password; - - - public void setHost(String host) { - this.host = host; - } - - public void setPort(String port) { - this.port = port; - } - - public void setAuth(String auth) { - this.auth = auth; - } - - public void setFrom(String from) { - this.from = from; - } - - public void setUsername(String username) { - this.username = username; - } - - public void setPassword(String password) { - this.password = password; - } - - public String getHost() { - return host; - } - - public String getPort() { - return port; - } - - public String getAuth() { - return auth; - } - - public String getFrom() { - return from; - } - - public String getUsername() { - return username; - } - - public String getPassword() { - return password; - } - - -} diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/CategoryController.java b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/CategoryController.java index 4522e58..286e236 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/CategoryController.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/CategoryController.java @@ -47,9 +47,9 @@ public class CategoryController { categoryFull.setSubCategories(subCategoriesFull); category.setSubCategories(subCategories); - Category categorySaved = categoryDAO.save(category); + categoryDAO.save(category); - categoryFull.setId(categorySaved.getId()); + categoryFull.setId(category.getId()); return categoryFull; } @@ -58,6 +58,7 @@ public class CategoryController { @PathVariable("topicId") String topicId, @RequestBody Category categoryFull) { log.debug("save category"); + log.debug("Alias: "+categoryFull.getAlias() + " - Id: "+categoryFull.getId() + " - Stakeholder: "+stakeholderId + " - Topic: "+topicId); Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId); @@ -69,17 +70,11 @@ public class CategoryController { // if category not exists (no id), create a new default subcategory, identical to category if(categoryFull.getId() == null) { SubCategory subCategory = new SubCategory<>(); - subCategory.setName("Overview"); - subCategory.setAlias("overview"); - subCategory.setIsActive(categoryFull.getIsActive()); - subCategory.setIsPublic(categoryFull.getIsPublic()); - subCategory.setIsDefault(true); - subCategory.setCharts(new ArrayList()); - subCategory.setNumbers(new ArrayList()); + subCategory.createOverviewSubCategory(categoryFull); - SubCategory subCategorySaved = subCategoryDAO.save(subCategory); + subCategoryDAO.save(subCategory); List subCategories = categoryFull.getSubCategories(); - subCategories.add(subCategorySaved); + subCategories.add(subCategory); } @@ -91,16 +86,24 @@ public class CategoryController { } category.setSubCategories(subCategories); - Category categorySaved = categoryDAO.save(category); + categoryDAO.save(category); + + if(stakeholder.getDefaultId() == null) { + if(categoryFull.getId() == null) { + onSaveDefaultCategory(category, topicId); + } else { + onUpdateDefaultCategory(category); + } + } List categories = topic.getCategories(); - int index = categories.indexOf(categorySaved.getId()); + int index = categories.indexOf(category.getId()); if(index == -1) { - categories.add(categorySaved.getId()); + categories.add(category.getId()); topicDAO.save(topic); log.debug("Category saved!"); - categoryFull.setId(categorySaved.getId()); + categoryFull.setId(category.getId()); } subCategories = null; @@ -120,11 +123,54 @@ public class CategoryController { return categoryFull; } + public void onSaveDefaultCategory(Category category, String topicId) { + log.debug("On save default category"); + + List topics = topicDAO.findByDefaultId(topicId); + for(Topic topic : topics) { + Category categoryNew = new Category(); + categoryNew.copyFromDefault(category); + + categoryDAO.save(categoryNew); + + List categories = topic.getCategories(); + categories.add(categoryNew.getId()); + + topicDAO.save(topic); + } + String subCategoryOverviewId = category.getSubCategories().get(0); + SubCategory subCategoryOverview = subCategoryDAO.findById(subCategoryOverviewId); + subCategoryController.onSaveDefaultSubCategory(subCategoryOverview, category.getId()); + } + + public void onUpdateDefaultCategory(Category category) { + log.debug("On update default category"); + + List categories = categoryDAO.findByDefaultId(category.getId()); + boolean changed = false; + for(Category categoryBasedOnDefault : categories) { + if(category.getName() != null && !category.getName().equals(categoryBasedOnDefault.getName())) { + changed = true; + } + if(category.getDescription() != null && !category.getDescription().equals(categoryBasedOnDefault.getDescription())) { + changed = true; + } + + if(!changed) { + break; + } + categoryBasedOnDefault.setName(category.getName()); + categoryBasedOnDefault.setDescription(category.getDescription()); + categoryDAO.save(categoryBasedOnDefault); + } + } + @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/delete", method = RequestMethod.DELETE) public boolean deleteCategory(@PathVariable("stakeholderId") String stakeholderId, @PathVariable("topicId") String topicId, @PathVariable("categoryId") String categoryId) { log.debug("delete category"); + log.debug("Id: "+categoryId + " - Stakeholder: "+stakeholderId + " - Topic: "+topicId); Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId); diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/EmailController.java b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/EmailController.java new file mode 100644 index 0000000..53c54ce --- /dev/null +++ b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/EmailController.java @@ -0,0 +1,63 @@ +package eu.dnetlib.uoamonitorservice.controllers; + +import eu.dnetlib.uoahelptexts.emailSender.EmailSender; +import eu.dnetlib.uoahelptexts.entities.EmailRecaptcha; +import eu.dnetlib.uoahelptexts.entities.Email; +import eu.dnetlib.uoahelptexts.handlers.InvalidReCaptchaException; +import eu.dnetlib.uoahelptexts.recaptcha.VerifyRecaptcha; +import org.apache.log4j.Logger; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.*; + +import java.util.*; + +@RestController +@CrossOrigin(origins = "*") +public class EmailController { + private final Logger log = Logger.getLogger(this.getClass()); + + @Autowired + private EmailSender emailSender; + + @Autowired + private VerifyRecaptcha verifyRecaptcha; + + @RequestMapping(value = "/contact", method = RequestMethod.POST) + public Boolean contact(@RequestBody EmailRecaptcha form) throws InvalidReCaptchaException { + verifyRecaptcha.processResponse(form.getRecaptcha()); + Email email = form.getEmail(); + ArrayList sendTo = new ArrayList<>(); + for(String userMail: email.getRecipients()){ + sendTo.add(userMail); + } + return emailSender.send(sendTo, email.getSubject(), email.getBody(), false); + } + + @RequestMapping(value = "/sendMail", method = RequestMethod.POST) + public Map> sendEmail(@RequestBody Email email, + @RequestParam(required = false) Optional optional) { + String successString = "success"; + String failureString = "failure"; + Map> mailResults = new HashMap<>(); + boolean bcc = (optional.isPresent())?optional.get():true; + for(String userMail:email.getRecipients()){ + ArrayList sendTo = new ArrayList<>(); + sendTo.add(userMail); + boolean success =emailSender.send(sendTo,email.getSubject(),email.getBody(), bcc); + if(success){ + if(!mailResults.containsKey(successString)) { + mailResults.put(successString, new ArrayList<>()); + } + mailResults.get(successString).add(userMail); + } else { + if(!mailResults.containsKey(failureString)) { + mailResults.put(failureString, new ArrayList<>()); + } + mailResults.get(failureString).add(userMail); + } + } + return mailResults; + + } + +} \ No newline at end of file diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/IndicatorController.java b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/IndicatorController.java index aacffa6..6d31133 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/IndicatorController.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/IndicatorController.java @@ -12,6 +12,7 @@ import org.springframework.web.bind.annotation.*; import java.util.ArrayList; import java.util.Iterator; import java.util.List; +import java.util.Map; @RestController @CrossOrigin(origins = "*") @@ -41,6 +42,7 @@ public class IndicatorController { @PathVariable("subcategoryId") String subcategoryId, @RequestBody Indicator indicator) { log.debug("save indicator"); + log.debug("Name: "+indicator.getName() + " - Id: "+indicator.getId() + " - Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId+ " - SubCategory: "+subcategoryId); // if(stakeholderId == null) { // // EXCEPTION - Parameter for Stakeholder is not accepted @@ -73,7 +75,18 @@ public class IndicatorController { SubCategory subcategory = subCategoryDAO.findById(subcategoryId); if(subcategory != null) { if (category.getSubCategories().contains(subcategoryId)) { - Indicator indicatorSaved = indicatorDAO.save(indicator); + String indicatorId = indicator.getId(); + indicatorDAO.save(indicator); + + // this indicator belongs in default profile and it is new or it is updated + if(stakeholder.getDefaultId() == null) { + if(indicatorId == null) { + onSaveDefaultIndicator(indicator, topicId, categoryId, subcategoryId, stakeholder); + } + else { + onUpdateDefaultIndicator(indicator, stakeholder); + } + } List indicators = null; //if(indicator.hasType("chart")) { @@ -84,13 +97,13 @@ public class IndicatorController { indicators = subcategory.getNumbers(); } - int index = indicators.indexOf(indicatorSaved.getId()); + int index = indicators.indexOf(indicator.getId()); if (index == -1) { - indicators.add(indicatorSaved.getId()); + indicators.add(indicator.getId()); subCategoryDAO.save(subcategory); log.debug("Indicator saved!"); - indicator.setId(indicatorSaved.getId()); + indicator.setId(indicator.getId()); } } else { // EXCEPTION - SubCategory not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); -> Category: category.getAlias(); @@ -123,6 +136,162 @@ public class IndicatorController { return indicator; } + public void onSaveDefaultIndicator(Indicator indicator, + String defaultTopicId, String defaultCategoryId, + String defaultSubcategoryId, Stakeholder defaultStakeholder) { + log.debug("On save default indicator"); + +// List stakeholders = stakeholderDAO.findByDefaultId(defaultStakeholder.getId()); +// for(Stakeholder stakeholder : stakeholders) { +// List topicIds = stakeholder.getTopics(); +// Topic topic = null; +// for(String topicId : topicIds) { +// topic = topicDAO.findById(topicId); +// if(topic.getDefaultId().equals(defaultTopicId)) { +// break; +// } +// } +// +// List categoryIds = topic.getCategories(); +// Category category = null; +// for(String categoryId : categoryIds) { +// category = categoryDAO.findById(categoryId); +// if(category.getDefaultId().equals(defaultCategoryId)) { +// break; +// } +// } +// +// List subCategoryIds = category.getSubCategories(); +// SubCategory subCategory = null; +// for(String subCategoryId : subCategoryIds) { +// subCategory = subCategoryDAO.findById(subCategoryId); +// if(subCategory.getDefaultId().equals(defaultSubcategoryId)) { +// break; +// } +// } +// +// Indicator indicatorNew = new Indicator(indicator); +// //indicatorNew.setStakeholderId(defaultStakeholder.getId()); +// for (IndicatorPath indicatorPath : indicatorNew.getIndicatorPaths()) { +// parameterMapping(indicatorPath, stakeholder); +// } +// +// indicatorDAO.save(indicatorNew); +// +// List indicators = null; +// if (indicator.getType().equals("chart")) { +// indicators = subCategory.getCharts(); +// } else if (indicator.getType().equals("number")) { +// indicators = subCategory.getNumbers(); +// } +// indicators.add(indicatorNew.getId()); +// +// subCategoryDAO.save(subCategory); +// } + + + // new indicator in default profile - add it on profiles of the same type + List subCategories = subCategoryDAO.findByDefaultId(defaultSubcategoryId); + + for (SubCategory subCategory : subCategories) { + Indicator indicatorNew = new Indicator(); + indicatorNew.copyFromDefault(indicator); + for (IndicatorPath indicatorPath : indicatorNew.getIndicatorPaths()) { + Stakeholder stakeholder = stakeholderDAO.findById(subCategory.getStakeholderId()); + parameterMapping(indicatorPath, stakeholder); + } + + indicatorDAO.save(indicatorNew); + + List indicators = null; + if (indicator.getType().equals("chart")) { + indicators = subCategory.getCharts(); + } else if (indicator.getType().equals("number")) { + indicators = subCategory.getNumbers(); + } + indicators.add(indicatorNew.getId()); + + subCategoryDAO.save(subCategory); + } + } + + public void onUpdateDefaultIndicator(Indicator indicator, Stakeholder stakeholder) { + log.debug("On update default indicator"); + + // indicator already exists - check if changed and update all indicators based on it + + boolean changed = false; + List indicators = indicatorDAO.findByDefaultId(indicator.getId()); + + for(Indicator indicatorBasedOnDefault : indicators) { + int i = 0; + List indicatorPaths = indicatorBasedOnDefault.getIndicatorPaths(); + + for (IndicatorPath indicatorPath : indicator.getIndicatorPaths()) { + IndicatorPath indicatorPathBasedOnDefault = indicatorBasedOnDefault.getIndicatorPaths().get(i); + + if(indicatorPathBasedOnDefault == null) { + // Add new indicator path in existing indicators + IndicatorPath indicatorPathNew = new IndicatorPath(indicatorPath); + parameterMapping(indicatorPathNew, stakeholder); + indicatorPaths.add(indicatorPathNew); + changed = true; + } else { + // Check if there are changes in indicator path and update existing indicators if needed + if(!indicatorPath.getType().equals(indicatorPathBasedOnDefault.getType())) { + indicatorPathBasedOnDefault.setType(indicatorPath.getType()); + changed = true; + } + if(!indicatorPath.getSource().equals(indicatorPathBasedOnDefault.getSource())) { + indicatorPathBasedOnDefault.setSource(indicatorPath.getSource()); + changed = true; + } + if(!indicatorPath.getUrl().equals(indicatorPathBasedOnDefault.getUrl())) { + indicatorPathBasedOnDefault.setUrl(indicatorPath.getUrl()); + changed = true; + } + if(!indicatorPath.getChartObject().equals(indicatorPathBasedOnDefault.getChartObject())) { + indicatorPathBasedOnDefault.setChartObject(indicatorPath.getChartObject()); + changed = true; + } + if(indicatorPath.getParameters().size() != indicatorPathBasedOnDefault.getParameters().size()) { + for (Map.Entry parameter : indicatorPath.getParameters().entrySet()) { + if(!indicatorPathBasedOnDefault.getParameters().containsKey(parameter.getKey())) { + indicatorPathBasedOnDefault.getParameters().put(parameter.getKey(), parameter.getValue()); + } + } + parameterMapping(indicatorPathBasedOnDefault, stakeholder); + changed = true; + } + int j=0; + for(String jsonString : indicatorPath.getJsonPath()) { + String jsonStringBasedOnDefault = indicatorPathBasedOnDefault.getJsonPath().get(j); + if(!jsonString.equals(jsonStringBasedOnDefault)) { + indicatorPathBasedOnDefault.getJsonPath().set(j, jsonString); + changed = true; + } + j++; + } + } + i++; + } + if(!changed) { + break; + } + indicatorDAO.save(indicatorBasedOnDefault); + } + } + + public void parameterMapping(IndicatorPath indicatorPath, Stakeholder stakeholder) { + if (indicatorPath.getParameters().containsKey("funder_name")) { + indicatorPath.getParameters().put("funder_name", stakeholder.getIndex_name()); + } else if (indicatorPath.getParameters().containsKey("fsn")) { + indicatorPath.getParameters().put("fsn", stakeholder.getIndex_name().toLowerCase()); + } else if (indicatorPath.getParameters().containsKey("funder_id")) { + indicatorPath.getParameters().put("funder_id", stakeholder.getIndex_id()); + } + } + @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/{indicatorId}/delete", method = RequestMethod.DELETE) public boolean deleteIndicator(@PathVariable("stakeholderId") String stakeholderId, @PathVariable("topicId") String topicId, @@ -130,6 +299,7 @@ public class IndicatorController { @PathVariable("subcategoryId") String subcategoryId, @PathVariable("indicatorId") String indicatorId) { log.debug("delete indicator"); + log.debug("Id: "+indicatorId + " - Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId+ " - SubCategory: "+subcategoryId); Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId); @@ -204,7 +374,8 @@ public class IndicatorController { @RequestMapping(value = "/{stakeholderId}/charts/delete", method = RequestMethod.DELETE) public boolean deleteAllChartIndicators(@PathVariable("stakeholderId") String stakeholderId) { - log.debug("delete indicator"); + log.debug("delete all chart indicators of stakeholder"); + log.debug("Stakeholder: "+stakeholderId); Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId); if(stakeholder != null) { @@ -265,76 +436,6 @@ public class IndicatorController { return true; } -// @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/{type}/reorder", method = RequestMethod.POST) -// public boolean reorderIndicators(@PathVariable("stakeholderId") String stakeholderId, -// @PathVariable("topicId") String topicId, -// @PathVariable("categoryId") String categoryId, -// @PathVariable("subcategoryId") String subcategoryId, -// @PathVariable("type") String type, -// @RequestBody List indicatorsFull) { -// log.debug("reorder indicators"); -// -// Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId); -// -// if (stakeholder != null) { -// -// Topic topic = topicDAO.findById(topicId); -// if (topic != null) { -// if (stakeholder.getTopics().contains(topicId)) { -// -// Category category = categoryDAO.findById(categoryId); -// if (category != null) { -// if (topic.getCategories().contains(categoryId)) { -// -// SubCategory subcategory = subCategoryDAO.findById(subcategoryId); -// if (subcategory != null) { -// if (category.getSubCategories().contains(subcategoryId)) { -// -// List indicators = new ArrayList<>(); -// for(Indicator indicator : indicatorsFull) { -// indicators.add(indicator.getId()); -// } -// -// if(type.equals("chart")) { -// subcategory.setCharts(indicators); -// } else if(type.equals("number")) { -// subcategory.setNumbers(indicators); -// } -// -// subCategoryDAO.save(subcategory); -// indicators = null; -// log.debug("Indicators reordered!"); -// } else { -// // EXCEPTION - SubCategory not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); -> Category: category.getAlias(); -// throw new PathNotValidException("Reorder indicators: SubCategory with id: "+subcategoryId+" not found in Category: "+categoryId); -// } -// } else { -// // EXCEPTION - SubCategory not found -// throw new EntityNotFoundException("Reorder indicators: SubCategory with id: "+subcategoryId+" not found"); -// } -// } else { -// // EXCEPTION - Category not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); -// throw new PathNotValidException("Reorder indicators: Category with id: "+categoryId+" not found in Topic: "+topicId); -// } -// } else { -// // EXCEPTION - Category not found -// throw new EntityNotFoundException("Reorder indicators: Category with id: "+categoryId+" not found"); -// } -// } else { -// // EXCEPTION - Topic not found in Stakeholder: stakeholder.getAlias(); -// throw new PathNotValidException("Reorder indicators: Topic with id: "+topicId+" not found in Stakeholder: "+stakeholderId); -// } -// } else { -// // EXCEPTION - Topic not found -// throw new EntityNotFoundException("Reorder indicators: Topic with id: "+topicId+" not found"); -// } -// } else { -// // EXCEPTION - Stakeholder not found -// throw new EntityNotFoundException("Reorder indicators: Stakeholder with id: "+stakeholderId+" not found"); -// } -// return true; -// } - @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/{type}/reorder", method = RequestMethod.POST) public List reorderIndicators(@PathVariable("stakeholderId") String stakeholderId, @PathVariable("topicId") String topicId, @@ -342,7 +443,8 @@ public class IndicatorController { @PathVariable("subcategoryId") String subcategoryId, @PathVariable("type") String type, @RequestBody List indicators) { - log.debug("reorder indicators"); + log.debug("reorder indicators of type: "+type); + log.debug("Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId+ " - SubCategory: "+subcategoryId); Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId); @@ -402,4 +504,94 @@ public class IndicatorController { } return indicatorsFull; } + + @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/{indicatorId}/toggle-status", method = RequestMethod.POST) + public Boolean toggleIndicatorStatus(@PathVariable("stakeholderId") String stakeholderId, + @PathVariable("topicId") String topicId, + @PathVariable("categoryId") String categoryId, + @PathVariable("subcategoryId") String subcategoryId, + @PathVariable("indicatorId") String indicatorId) { + log.debug("toggle indicator status (isActive)"); + log.debug("Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId+ " - SubCategory: "+subcategoryId+ " - Indicator: "+indicatorId); + + Indicator indicator = indicatorDAO.findById(indicatorId); + if (indicator == null) { + // EXCEPTION - Indicator not found + throw new EntityNotFoundException("Toggle indicator status: Indicator with id: "+indicatorId+" not found"); + } + indicator.setIsActive(!indicator.getIsActive()); + + this.toggleIndicator(stakeholderId, topicId, categoryId, subcategoryId, indicator); + + return indicator.getIsActive(); + } + + @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/{indicatorId}/toggle-access", method = RequestMethod.POST) + public Boolean toggleIndicatorAccess(@PathVariable("stakeholderId") String stakeholderId, + @PathVariable("topicId") String topicId, + @PathVariable("categoryId") String categoryId, + @PathVariable("subcategoryId") String subcategoryId, + @PathVariable("indicatorId") String indicatorId) { + log.debug("toggle indicator access (isPublic)"); + log.debug("Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId+ " - SubCategory: "+subcategoryId+ " - Indicator: "+indicatorId); + + Indicator indicator = indicatorDAO.findById(indicatorId); + if (indicator == null) { + // EXCEPTION - Indicator not found + throw new EntityNotFoundException("Toggle indicator access: Indicator with id: "+indicatorId+" not found"); + } + indicator.setIsPublic(!indicator.getIsPublic()); + + this.toggleIndicator(stakeholderId, topicId, categoryId, subcategoryId, indicator); + + return indicator.getIsPublic(); + } + + public void toggleIndicator(String stakeholderId, String topicId, String categoryId, String subcategoryId, Indicator indicator) { + Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId); + + if (stakeholder != null) { + + Topic topic = topicDAO.findById(topicId); + if (topic != null) { + if (stakeholder.getTopics().contains(topicId)) { + + Category category = categoryDAO.findById(categoryId); + if (category != null) { + if (topic.getCategories().contains(categoryId)) { + + SubCategory subcategory = subCategoryDAO.findById(subcategoryId); + if (subcategory != null) { + if (category.getSubCategories().contains(subcategoryId)) { + indicatorDAO.save(indicator); + log.debug("Indicator toggled!"); + } else { + // EXCEPTION - SubCategory not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); -> Category: category.getAlias(); + throw new PathNotValidException("Reorder indicators: SubCategory with id: "+subcategoryId+" not found in Category: "+categoryId); + } + } else { + // EXCEPTION - SubCategory not found + throw new EntityNotFoundException("Reorder indicators: SubCategory with id: "+subcategoryId+" not found"); + } + } else { + // EXCEPTION - Category not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); + throw new PathNotValidException("Reorder indicators: Category with id: "+categoryId+" not found in Topic: "+topicId); + } + } else { + // EXCEPTION - Category not found + throw new EntityNotFoundException("Reorder indicators: Category with id: "+categoryId+" not found"); + } + } else { + // EXCEPTION - Topic not found in Stakeholder: stakeholder.getAlias(); + throw new PathNotValidException("Reorder indicators: Topic with id: "+topicId+" not found in Stakeholder: "+stakeholderId); + } + } else { + // EXCEPTION - Topic not found + throw new EntityNotFoundException("Reorder indicators: Topic with id: "+topicId+" not found"); + } + } else { + // EXCEPTION - Stakeholder not found + throw new EntityNotFoundException("Reorder indicators: Stakeholder with id: "+stakeholderId+" not found"); + } + } } diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/StakeholderController.java b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/StakeholderController.java index ca4e23e..78af19f 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/StakeholderController.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/StakeholderController.java @@ -39,6 +39,7 @@ public class StakeholderController { @RequestMapping(value = "/build-stakeholder", method = RequestMethod.POST) public Stakeholder>>> buildFullStakeholder(@RequestBody Stakeholder>>> stakeholderFull) { log.debug("build stakeholder"); + log.debug("Alias: "+stakeholderFull.getAlias()); Stakeholder stakeholder = new Stakeholder<>(stakeholderFull); @@ -52,6 +53,13 @@ public class StakeholderController { stakeholderFull.setTopics(topicsFull); stakeholder.setTopics(topics); + Date date = new Date(); + stakeholder.setCreationDate(date); + stakeholder.setUpdateDate(date); + + stakeholderFull.setCreationDate(date); + stakeholderFull.setUpdateDate(date); + Stakeholder stakeholderSaved = stakeholderDAO.save(stakeholder); stakeholderFull.setId(stakeholderSaved.getId()); return stakeholderFull; @@ -65,29 +73,51 @@ public class StakeholderController { for (String topicId: (List)stakeholder.getTopics()) { Topic topic = topicDAO.findById(topicId); + if(topic == null) { + // EXCEPTION - Topic not found + throw new EntityNotFoundException("Get stakeholder: Topic with id: "+topicId+" not found (topic exists in stakeholder: "+stakeholder.getId()+")"); + } Topic topicFull = new Topic(topic); List categories = new ArrayList<>(); for(String categoryId : topic.getCategories()) { Category category = categoryDAO.findById(categoryId); + if(category == null) { + // EXCEPTION - Category not found + throw new EntityNotFoundException("Get stakeholder: Category with id: "+categoryId+" not found (category exists in topic: "+topicId+")"); + } Category categoryFull = new Category(category); List subCategories = new ArrayList<>(); for(String subCategoryId : category.getSubCategories()) { SubCategory subCategory = subCategoryDAO.findById(subCategoryId); + if(subCategory == null) { + // EXCEPTION - SubCategory not found + throw new EntityNotFoundException("Get stakeholder: SubCategory with id: "+subCategoryId+" not found (subCategory exists in category: "+categoryId+")"); + } SubCategory subCategoryFull = new SubCategory(subCategory); List charts = new ArrayList<>(); for(String indicatorId : subCategory.getCharts()) { - charts.add(indicatorDAO.findById(indicatorId)); + Indicator indicator = indicatorDAO.findById(indicatorId); + if(indicator == null) { + // EXCEPTION - Indicator not found + throw new EntityNotFoundException("Get stakeholder: Indicator with id: "+indicatorId+" not found (indicator exists in subCategory: "+subCategoryId+")"); + } + charts.add(indicator); } subCategoryFull.setCharts(charts); List numbers = new ArrayList<>(); for(String indicatorId : subCategory.getNumbers()) { - numbers.add(indicatorDAO.findById(indicatorId)); + 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); @@ -108,6 +138,8 @@ public class StakeholderController { @RequestMapping(value = "/stakeholder/all", method = RequestMethod.GET) public List getAllStakeholders(@RequestParam(required = false) String type) { + log.debug("get all stakeholders" + (type != null ? " with type: "+type : "")); + List stakeholders; if(type == null) { stakeholders = stakeholderDAO.findAll(); @@ -125,11 +157,13 @@ public class StakeholderController { @RequestMapping(value = "/stakeholder/default", method = RequestMethod.GET) public List getAllDefaultStakeholders(@RequestParam(required = false) String type) { + log.debug("get all default stakeholders" + (type != null ? " with type: "+type : "")); + List stakeholders; if(type == null) { - stakeholders = stakeholderDAO.findByIsDefaultProfile(true); + stakeholders = stakeholderDAO.findByDefaultId(null); } else { - stakeholders = stakeholderDAO.findByIsDefaultProfileAndType(true, type); + stakeholders = stakeholderDAO.findByDefaultIdAndType(null, type); } List stakeholdersFull = new ArrayList<>(); @@ -141,11 +175,13 @@ public class StakeholderController { @RequestMapping(value = "/stakeholder", method = RequestMethod.GET) public List getAllRealStakeholders(@RequestParam(required = false) String type) { + log.debug("get all NOT default stakeholders" + (type != null ? " with type: "+type : "")); + List stakeholders; if(type == null) { - stakeholders = stakeholderDAO.findByIsDefaultProfile(false); + stakeholders = stakeholderDAO.findByDefaultIdNot(null); } else { - stakeholders = stakeholderDAO.findByIsDefaultProfileAndType(false, type); + stakeholders = stakeholderDAO.findByDefaultIdNotAndType(null, type); } List stakeholdersFull = new ArrayList<>(); @@ -159,6 +195,8 @@ public class StakeholderController { @RequestMapping(value = "/stakeholder/{alias}", method = RequestMethod.GET) public Stakeholder getStakeholder(@PathVariable("alias") String alias) { + log.debug("get stakeholder: "+alias); + Stakeholder stakeholder = stakeholderDAO.findByAlias(alias); if(stakeholder == null) { // EXCEPTION - Stakeholder not found @@ -170,6 +208,7 @@ public class StakeholderController { @RequestMapping(value = "/save", method = RequestMethod.POST) public Stakeholder saveStakeholder(@RequestBody Stakeholder stakeholderFull) { log.debug("save stakeholder"); + log.debug("Alias: "+stakeholderFull.getAlias() + " - Id: "+stakeholderFull.getId()); // if(stakeholderFull == null) { // log.debug("stakeholder null"); @@ -178,6 +217,14 @@ public class StakeholderController { Stakeholder stakeholder = new Stakeholder<>(stakeholderFull); + Date date = new Date(); + stakeholder.setUpdateDate(date); + + // stakeholder does not exist in DB + if(stakeholderFull.getId() == null) { + stakeholder.setCreationDate(date); + } + List topics = new ArrayList<>(); for(Topic topic : stakeholderFull.getTopics()) { topics.add(topic.getId()); @@ -186,6 +233,8 @@ public class StakeholderController { Stakeholder stakeholderSaved = stakeholderDAO.save(stakeholder); stakeholderFull.setId(stakeholderSaved.getId()); + stakeholderFull.setCreationDate(stakeholderSaved.getCreationDate()); + stakeholderFull.setUpdateDate(stakeholderSaved.getUpdateDate()); topics = null; stakeholder = null; @@ -198,6 +247,7 @@ public class StakeholderController { @RequestMapping(value = "/{stakeholderId}/delete", method = RequestMethod.DELETE) public boolean deleteStakeholder(@PathVariable("stakeholderId") String stakeholderId) { log.debug("delete stakeholder"); + log.debug("Id: "+stakeholderId); Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId); diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/SubCategoryController.java b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/SubCategoryController.java index 4c99507..796b468 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/SubCategoryController.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/SubCategoryController.java @@ -56,9 +56,9 @@ public class SubCategoryController { subcategoryFull.setNumbers(numbersFull); subCategory.setNumbers(numbers); - SubCategory subCategorySaved = subCategoryDAO.save(subCategory); + subCategoryDAO.save(subCategory); - subcategoryFull.setId(subCategorySaved.getId()); + subcategoryFull.setId(subCategory.getId()); return subcategoryFull; } @@ -68,6 +68,7 @@ public class SubCategoryController { @PathVariable("categoryId") String categoryId, @RequestBody SubCategory subcategoryFull) { log.debug("save subcategory"); + log.debug("Alias: "+subcategoryFull.getAlias() + " - Id: "+subcategoryFull.getId() + " - Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId); Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId); @@ -94,16 +95,24 @@ public class SubCategoryController { } subCategory.setNumbers(numbers); - SubCategory subCategorySaved = subCategoryDAO.save(subCategory); + subCategoryDAO.save(subCategory); + + if(stakeholder.getDefaultId() == null) { + if(topic.getId() == null) { + onSaveDefaultSubCategory(subCategory, categoryId); + } else { + onUpdateDefaultSubCategory(subCategory); + } + } List subcategories = category.getSubCategories(); - int index = subcategories.indexOf(subCategorySaved.getId()); + int index = subcategories.indexOf(subCategory.getId()); if(index == -1) { - subcategories.add(subCategorySaved.getId()); + subcategories.add(subCategory.getId()); categoryDAO.save(category); log.debug("Subcategory saved!"); - subcategoryFull.setId(subCategorySaved.getId()); + subcategoryFull.setId(subCategory.getId()); } charts = null; @@ -132,12 +141,52 @@ public class SubCategoryController { return subcategoryFull; } + public void onSaveDefaultSubCategory(SubCategory subCategory, String categoryId) { + log.debug("On save default subCategory"); + + List categories = categoryDAO.findByDefaultId(categoryId); + for(Category category : categories) { + SubCategory subCategoryNew = new SubCategory(); + subCategoryNew.copyFromDefault(subCategory); + + subCategoryDAO.save(subCategoryNew); + + List subCategories = category.getSubCategories(); + subCategories.add(subCategoryNew.getId()); + + categoryDAO.save(category); + } + } + + public void onUpdateDefaultSubCategory(SubCategory subCategory) { + log.debug("On update default subCategory"); + + List subCategories = subCategoryDAO.findByDefaultId(subCategory.getId()); + boolean changed = false; + for(SubCategory subCategoryBasedOnDefault : subCategories) { + if(subCategory.getName() != null && !subCategory.getName().equals(subCategoryBasedOnDefault.getName())) { + changed = true; + } + if(subCategory.getDescription() != null && !subCategory.getDescription().equals(subCategoryBasedOnDefault.getDescription())) { + changed = true; + } + + if(!changed) { + break; + } + subCategoryBasedOnDefault.setName(subCategory.getName()); + subCategoryBasedOnDefault.setDescription(subCategory.getDescription()); + subCategoryDAO.save(subCategoryBasedOnDefault); + } + } + @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/delete", method = RequestMethod.DELETE) public boolean deleteSubCategory(@PathVariable("stakeholderId") String stakeholderId, @PathVariable("topicId") String topicId, @PathVariable("categoryId") String categoryId, @PathVariable("subcategoryId") String subcategoryId) { log.debug("delete subcategory"); + log.debug("Id: "+subcategoryId + " - Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId); Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId); diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/TopicController.java b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/TopicController.java index 927ea83..4e1e8c2 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/TopicController.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/TopicController.java @@ -50,9 +50,9 @@ public class TopicController { topicFull.setCategories(categoriesFull); topic.setCategories(categories); - Topic topicSaved = topicDAO.save(topic); + topicDAO.save(topic); - topicFull.setId(topicSaved.getId()); + topicFull.setId(topic.getId()); return topicFull; } @@ -60,6 +60,7 @@ public class TopicController { public Topic saveTopic(@PathVariable("stakeholderId") String stakeholderId, @RequestBody Topic topicFull) { log.debug("save topic"); + log.debug("Alias: "+topicFull.getAlias() + " - Id: "+topicFull.getId()+ " - Stakeholder: "+stakeholderId); Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId); @@ -73,16 +74,24 @@ public class TopicController { } topic.setCategories(categories); - Topic topicSaved = topicDAO.save(topic); + topicDAO.save(topic); + + if(stakeholder.getDefaultId() == null) { + if(topicFull.getId() == null) { + onSaveDefaultTopic(topic, stakeholderId); + } else { + onUpdateDefaultTopic(topic); + } + } List topics = stakeholder.getTopics(); - int index = topics.indexOf(topicSaved.getId()); + int index = topics.indexOf(topic.getId()); if(index == -1) { - topics.add(topicSaved.getId()); + topics.add(topic.getId()); stakeholderDAO.save(stakeholder); log.debug("Topic saved!"); - topicFull.setId(topicSaved.getId()); + topicFull.setId(topic.getId()); } categories = null; @@ -94,10 +103,50 @@ public class TopicController { return topicFull; } + public void onSaveDefaultTopic(Topic topic, String stakeholderId) { + log.debug("On save default topic"); + + List stakeholders = stakeholderDAO.findByDefaultId(stakeholderId); + for(Stakeholder _stakeholder : stakeholders) { + Topic topicNew = new Topic(); + topicNew.copyFromDefault(topic); + + topicDAO.save(topicNew); + + List topics = _stakeholder.getTopics(); + topics.add(topicNew.getId()); + + stakeholderDAO.save(_stakeholder); + } + } + + public void onUpdateDefaultTopic(Topic topic) { + log.debug("On update default topic"); + + List topics = topicDAO.findByDefaultId(topic.getId()); + boolean changed = false; + for(Topic topicBasedOnDefault : topics) { + if(topic.getName() != null && !topic.getName().equals(topicBasedOnDefault.getName())) { + changed = true; + } + if(topic.getDescription() != null && !topic.getDescription().equals(topicBasedOnDefault.getDescription())) { + changed = true; + } + + if(!changed) { + break; + } + topicBasedOnDefault.setName(topic.getName()); + topicBasedOnDefault.setDescription(topic.getDescription()); + topicDAO.save(topicBasedOnDefault); + } + } + @RequestMapping(value = "/{stakeholderId}/{topicId}/delete", method = RequestMethod.DELETE) public boolean deleteTopic(@PathVariable("stakeholderId") String stakeholderId, @PathVariable("topicId") String topicId) { log.debug("delete topic"); + log.debug("Id: "+topicId + " - Stakeholder: "+stakeholderId); Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId); diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/dao/CategoryDAO.java b/src/main/java/eu/dnetlib/uoamonitorservice/dao/CategoryDAO.java index 7e370ea..6ee4424 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/dao/CategoryDAO.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/dao/CategoryDAO.java @@ -6,6 +6,8 @@ import java.util.List; public interface CategoryDAO { List findAll(); + List findByDefaultId(String DefaultId); + Category findById(String Id); void delete(String Id); diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/dao/IndicatorDAO.java b/src/main/java/eu/dnetlib/uoamonitorservice/dao/IndicatorDAO.java index f7b4059..0ac32bd 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/dao/IndicatorDAO.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/dao/IndicatorDAO.java @@ -6,6 +6,8 @@ import java.util.List; public interface IndicatorDAO { List findAll(); + List findByDefaultId(String DefaultId); + Indicator findById(String Id); void delete(String Id); diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/dao/MongoDBCategoryDAO.java b/src/main/java/eu/dnetlib/uoamonitorservice/dao/MongoDBCategoryDAO.java index 1a34048..22d8a02 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/dao/MongoDBCategoryDAO.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/dao/MongoDBCategoryDAO.java @@ -7,6 +7,8 @@ import java.util.List; public interface MongoDBCategoryDAO extends CategoryDAO, MongoRepository { List findAll(); + List findByDefaultId(String DefaultId); + Category findById(String Id); void delete(String Id); diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/dao/MongoDBIndicatorDAO.java b/src/main/java/eu/dnetlib/uoamonitorservice/dao/MongoDBIndicatorDAO.java index 54cc391..f35ee01 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/dao/MongoDBIndicatorDAO.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/dao/MongoDBIndicatorDAO.java @@ -7,6 +7,8 @@ import java.util.List; public interface MongoDBIndicatorDAO extends IndicatorDAO, MongoRepository { List findAll(); + List findByDefaultId(String DefaultId); + Indicator findById(String Id); void delete(String Id); diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/dao/MongoDBStakeholderDAO.java b/src/main/java/eu/dnetlib/uoamonitorservice/dao/MongoDBStakeholderDAO.java index 02a5285..6db54e9 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/dao/MongoDBStakeholderDAO.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/dao/MongoDBStakeholderDAO.java @@ -9,8 +9,11 @@ public interface MongoDBStakeholderDAO extends StakeholderDAO, MongoRepository findAll(); List findByType(String Type); - List findByIsDefaultProfile(boolean IsDefaultProfile); - List findByIsDefaultProfileAndType(boolean IsDefaultProfile, String Type); + List findByDefaultId(String DefaultId); + List findByDefaultIdAndType(String DefaultId, String Type); + + List findByDefaultIdNot(String DefaultId); + List findByDefaultIdNotAndType(String DefaultId, String Type); Stakeholder findById(String Id); Stakeholder findByAlias(String Alias); diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/dao/MongoDBSubCategoryDAO.java b/src/main/java/eu/dnetlib/uoamonitorservice/dao/MongoDBSubCategoryDAO.java index 25e165b..375085e 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/dao/MongoDBSubCategoryDAO.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/dao/MongoDBSubCategoryDAO.java @@ -7,6 +7,8 @@ import java.util.List; public interface MongoDBSubCategoryDAO extends SubCategoryDAO, MongoRepository { List findAll(); + List findByDefaultId(String DefaultId); + SubCategory findById(String Id); void delete(String Id); diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/dao/MongoDBTopicDAO.java b/src/main/java/eu/dnetlib/uoamonitorservice/dao/MongoDBTopicDAO.java index 3e5b26e..ca4964f 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/dao/MongoDBTopicDAO.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/dao/MongoDBTopicDAO.java @@ -7,6 +7,8 @@ import java.util.List; public interface MongoDBTopicDAO extends TopicDAO, MongoRepository { List findAll(); + List findByDefaultId(String DefaultId); + Topic findById(String Id); void delete(String Id); diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/dao/StakeholderDAO.java b/src/main/java/eu/dnetlib/uoamonitorservice/dao/StakeholderDAO.java index 7fb1b57..f79ef5a 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/dao/StakeholderDAO.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/dao/StakeholderDAO.java @@ -8,8 +8,11 @@ public interface StakeholderDAO { List findAll(); List findByType(String Type); - List findByIsDefaultProfile(boolean IsDefaultProfile); - List findByIsDefaultProfileAndType(boolean IsDefaultProfile, String Type); + List findByDefaultId(String DefaultId); + List findByDefaultIdAndType(String DefaultId, String Type); + + List findByDefaultIdNot(String DefaultId); + List findByDefaultIdNotAndType(String DefaultId, String Type); Stakeholder findById(String Id); Stakeholder findByAlias(String Alias); diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/dao/SubCategoryDAO.java b/src/main/java/eu/dnetlib/uoamonitorservice/dao/SubCategoryDAO.java index 50fdf0a..8a9f406 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/dao/SubCategoryDAO.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/dao/SubCategoryDAO.java @@ -6,6 +6,8 @@ import java.util.List; public interface SubCategoryDAO { List findAll(); + List findByDefaultId(String DefaultId); + SubCategory findById(String Id); void delete(String Id); diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/dao/TopicDAO.java b/src/main/java/eu/dnetlib/uoamonitorservice/dao/TopicDAO.java index d1f2810..0bfc1dd 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/dao/TopicDAO.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/dao/TopicDAO.java @@ -6,6 +6,8 @@ import java.util.List; public interface TopicDAO { List findAll(); + List findByDefaultId(String DefaultId); + Topic findById(String Id); void delete(String Id); diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/entities/Category.java b/src/main/java/eu/dnetlib/uoamonitorservice/entities/Category.java index 2a74f9a..47e62c7 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/entities/Category.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/entities/Category.java @@ -3,6 +3,7 @@ package eu.dnetlib.uoamonitorservice.entities; import com.fasterxml.jackson.annotation.JsonProperty; import org.springframework.data.annotation.Id; +import java.util.ArrayList; import java.util.List; public class Category { @@ -12,10 +13,11 @@ public class Category { private String name; private String alias; + private String description; private boolean isActive; private boolean isPublic; private boolean isOverview; - private boolean isDefault; + private String defaultId; private List subCategories; public Category() {} @@ -23,10 +25,22 @@ public class Category { id = category.getId(); name = category.getName(); alias = category.getAlias(); + description = category.getDescription(); isActive = category.getIsActive(); isPublic = category.getIsPublic(); isOverview = category.getIsOverview(); - isDefault = category.getIsDefault(); + defaultId = category.getDefaultId(); + } + + public void copyFromDefault(Category defaultCategory) { + setName(defaultCategory.getName()); + setAlias(defaultCategory.getAlias()); + setDescription(defaultCategory.getDescription()); + setIsActive(false); + setIsPublic(false); + setIsOverview(defaultCategory.getIsOverview()); + setDefaultId(defaultCategory.getId()); + setSubCategories(new ArrayList()); } public String getId() { @@ -53,6 +67,14 @@ public class Category { this.alias = alias; } + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + public boolean getIsActive() { return isActive; } @@ -77,12 +99,12 @@ public class Category { this.isOverview = isOverview; } - public boolean getIsDefault() { - return isDefault; + public String getDefaultId() { + return defaultId; } - public void setIsDefault(boolean isDefault) { - this.isDefault = isDefault; + public void setDefaultId(String defaultId) { + this.defaultId = defaultId; } public List getSubCategories() { diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/entities/Indicator.java b/src/main/java/eu/dnetlib/uoamonitorservice/entities/Indicator.java index 6d38184..230ab7c 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/entities/Indicator.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/entities/Indicator.java @@ -1,9 +1,9 @@ package eu.dnetlib.uoamonitorservice.entities; import com.fasterxml.jackson.annotation.JsonProperty; -import org.apache.log4j.Logger; import org.springframework.data.annotation.Id; +import java.util.ArrayList; import java.util.List; enum IndicatorType { @@ -25,12 +25,24 @@ public class Indicator { private String description; private IndicatorType type; //number,chart private IndicatorWidth width; //small,medium,large - private List tags; + private List tags; // this field is not used anywhere now private boolean isActive; private boolean isPublic; - private boolean isDefault; + private String defaultId; private List indicatorPaths; + public void copyFromDefault(Indicator defaultIndicator) { + setName(defaultIndicator.getName()); + setDescription(defaultIndicator.getDescription()); + setType(defaultIndicator.getType())); + setWidth(defaultIndicator.getWidth()); + setTags(defaultIndicator.getTags()); + setIsActive(false); + setIsPublic(false); + setDefaultId(defaultIndicator.id); + setIndicatorPaths(defaultIndicator.getIndicatorPaths()); + } + public String getId() { return id; } @@ -55,15 +67,27 @@ public class Indicator { this.description = description; } -// public IndicatorType getType() { -// return type; -// } public String getType() { + if(type == null) { + return null; + } return type.name(); } - public void setType(IndicatorType type) { - this.type = type; +// 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; + } } public IndicatorWidth getWidth() { @@ -98,12 +122,12 @@ public class Indicator { this.isPublic = isPublic; } - public boolean getIsDefault() { - return isDefault; + public String getDefaultId() { + return defaultId; } - public void setIsDefault(boolean isDefault) { - this.isDefault = isDefault; + public void setDefaultId(String defaultId) { + this.defaultId = defaultId; } public List getIndicatorPaths() { diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/entities/IndicatorPath.java b/src/main/java/eu/dnetlib/uoamonitorservice/entities/IndicatorPath.java index 9227ba0..33cd7a2 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/entities/IndicatorPath.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/entities/IndicatorPath.java @@ -18,12 +18,36 @@ public class IndicatorPath { private Map filters; //private Map> filters; - public IndicatorPathType getType() { - return type; + public IndicatorPath() {} + + public IndicatorPath(IndicatorPath indicatorPath) { + setType(indicatorPath.getType()); + source = indicatorPath.getSource(); + url = indicatorPath.getUrl(); + jsonPath = indicatorPath.getJsonPath(); + chartObject = indicatorPath.getChartObject(); + parameters = indicatorPath.getParameters(); + filters = indicatorPath.getFilters(); } - public void setType(IndicatorPathType type) { - this.type = type; + public String getType() { + if(type == null) { + return null; + } + return type.name(); + } + +// public IndicatorPathType getType() { +// return type; +// } + + public void setType(String type) { + if(type == null) { + this.type = null; + } else { + IndicatorPathType indicatorPathType = IndicatorPathType.valueOf(type); + this.type = indicatorPathType; + } } public String getSource() { diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/entities/Stakeholder.java b/src/main/java/eu/dnetlib/uoamonitorservice/entities/Stakeholder.java index 826f547..32f6e44 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/entities/Stakeholder.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/entities/Stakeholder.java @@ -23,8 +23,9 @@ public class Stakeholder { private String index_id; private String index_name; private String index_shortName; + private String logoUrl; private String alias; - private boolean isDefaultProfile; + private String defaultId = null; private boolean isActive; private boolean isPublic; private Date creationDate; @@ -33,15 +34,18 @@ public class Stakeholder { private List topics; - public Stakeholder() {} + public Stakeholder() { + + } public Stakeholder(Stakeholder stakeholder) { id = stakeholder.getId(); - type = stakeholder.getType(); + setType(stakeholder.getType()); index_id = stakeholder.getIndex_id(); index_name = stakeholder.getIndex_name(); index_shortName = stakeholder.getIndex_shortName(); + logoUrl = stakeholder.getLogoUrl(); alias = stakeholder.getAlias(); - isDefaultProfile = stakeholder.getIsDefaultProfile(); + defaultId = stakeholder.getDefaultId(); isActive = stakeholder.getIsActive(); isPublic = stakeholder.getIsPublic(); creationDate = stakeholder.getCreationDate(); @@ -57,12 +61,28 @@ public class Stakeholder { this.id = id; } - public StakeholderType getType() { - return type; +// 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(StakeholderType type) { - this.type = type; + public void setType(String type) { + if(type == null) { + this.type = null; + } else { + StakeholderType stakeholderType = StakeholderType.valueOf(type); + this.type = stakeholderType; + } } public String getIndex_id() { @@ -89,6 +109,14 @@ public class Stakeholder { this.index_shortName = index_shortName; } + public String getLogoUrl() { + return logoUrl; + } + + public void setLogoUrl(String logoUrl) { + this.logoUrl = logoUrl; + } + public String getAlias() { return alias; } @@ -97,12 +125,12 @@ public class Stakeholder { this.alias = alias; } - public boolean getIsDefaultProfile() { - return isDefaultProfile; + public String getDefaultId() { + return defaultId; } - public void setIsDefaultProfile(boolean isDefaultProfile) { - this.isDefaultProfile = isDefaultProfile; + public void setDefaultId(String defaultId) { + this.defaultId = defaultId; } public boolean getIsActive() { diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/entities/SubCategory.java b/src/main/java/eu/dnetlib/uoamonitorservice/entities/SubCategory.java index 2e2ede0..017936c 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/entities/SubCategory.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/entities/SubCategory.java @@ -3,6 +3,7 @@ package eu.dnetlib.uoamonitorservice.entities; import com.fasterxml.jackson.annotation.JsonProperty; import org.springframework.data.annotation.Id; +import java.util.ArrayList; import java.util.List; public class SubCategory { @@ -12,9 +13,11 @@ public class SubCategory { private String name; private String alias; + private String description; private boolean isActive; private boolean isPublic; - private boolean isDefault; + private String defaultId; + private String stakeholderId; private List charts; private List numbers; @@ -23,9 +26,30 @@ public class SubCategory { id = subCategory.getId(); name = subCategory.getName(); alias = subCategory.getAlias(); + description = subCategory.getDescription(); isActive = subCategory.getIsActive(); isPublic = subCategory.getIsPublic(); - isDefault = subCategory.getIsDefault(); + defaultId = subCategory.getDefaultId(); + } + + public void createOverviewSubCategory(Category category) { + setName("Overview"); + setAlias("overview"); + setIsActive(category.getIsActive()); + setIsPublic(category.getIsPublic()); + setCharts(new ArrayList<>()); + setNumbers(new ArrayList<>()); + } + + public void copyFromDefault(SubCategory defaultSubCategory) { + setName(defaultSubCategory.getName()); + setAlias(defaultSubCategory.getAlias()); + setDescription(defaultSubCategory.getDescription()); + setIsActive(false); + setIsPublic(false); + setDefaultId(defaultSubCategory.getId()); + setCharts(new ArrayList()); + setNumbers(new ArrayList()); } public String getId() { @@ -52,6 +76,14 @@ public class SubCategory { this.alias = alias; } + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + public boolean getIsActive() { return isActive; } @@ -68,12 +100,20 @@ public class SubCategory { this.isPublic = isPublic; } - public boolean getIsDefault() { - return isDefault; + public String getDefaultId() { + return defaultId; } - public void setIsDefault(boolean isDefault) { - this.isDefault = isDefault; + public void setDefaultId(String defaultId) { + this.defaultId = defaultId; + } + + public String getStakeholderId() { + return stakeholderId; + } + + public void setStakeholderId(String stakeholderId) { + this.stakeholderId = stakeholderId; } public List getCharts() { diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/entities/Topic.java b/src/main/java/eu/dnetlib/uoamonitorservice/entities/Topic.java index ebd102e..d2c2f44 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/entities/Topic.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/entities/Topic.java @@ -3,6 +3,7 @@ package eu.dnetlib.uoamonitorservice.entities; import com.fasterxml.jackson.annotation.JsonProperty; import org.springframework.data.annotation.Id; +import java.util.ArrayList; import java.util.List; public class Topic { @@ -15,7 +16,7 @@ public class Topic { private String description; private boolean isActive; private boolean isPublic; - private boolean isDefault; + private String defaultId; private List categories; public Topic() {} @@ -26,7 +27,17 @@ public class Topic { description = topic.getDescription(); isActive = topic.getIsActive(); isPublic = topic.getIsPublic(); - isDefault = topic.getIsDefault(); + defaultId = topic.getDefaultId(); + } + + public void copyFromDefault(Topic defaultTopic) { + setName(defaultTopic.getName()); + setAlias(defaultTopic.getAlias()); + setDescription(defaultTopic.getDescription()); + setIsActive(false); + setIsPublic(false); + setDefaultId(defaultTopic.getId()); + setCategories(new ArrayList()); } public String getId() { @@ -77,12 +88,12 @@ public class Topic { this.isPublic = isPublic; } - public boolean getIsDefault() { - return isDefault; + public String getDefaultId() { + return defaultId; } - public void setIsDefault(boolean isDefault) { - this.isDefault = isDefault; + public void setDefaultId(String defaultId) { + this.defaultId = defaultId; } public List getCategories() { diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/handlers/ExceptionsHandler.java b/src/main/java/eu/dnetlib/uoamonitorservice/handlers/ExceptionsHandler.java index 2be3212..c6feb10 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/handlers/ExceptionsHandler.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/handlers/ExceptionsHandler.java @@ -22,7 +22,7 @@ public class ExceptionsHandler { response.setErrorMessage("Invalid inputs"); response.setErrors(ex.getMessage()); response.setStatus(HttpStatus.BAD_REQUEST); - log.debug("invalidInput exception"); + log.debug("invalidInput exception : "+ ex.getMessage()); return new ResponseEntity(response, HttpStatus.BAD_REQUEST); } @@ -33,7 +33,7 @@ public class ExceptionsHandler { response.setErrorMessage("Null pointer Exception"); response.setErrors(ex.getMessage()); response.setStatus(HttpStatus.BAD_REQUEST); - log.debug("nullPointerException exception"); + log.debug("nullPointerException exception : "+ ex.getMessage()); return new ResponseEntity(response, HttpStatus.BAD_REQUEST); } @@ -44,7 +44,7 @@ public class ExceptionsHandler { response.setErrorMessage("Not found Exception"); response.setErrors(ex.getMessage()); response.setStatus(HttpStatus.NOT_FOUND); - log.debug("notFoundException exception"); + log.debug("notFoundException exception : "+ ex.getMessage()); return new ResponseEntity(response, HttpStatus.NOT_FOUND); } @@ -55,7 +55,7 @@ public class ExceptionsHandler { response.setErrorMessage("Entity not found Exception"); response.setErrors(ex.getMessage()); response.setStatus(HttpStatus.NOT_FOUND); - log.debug("entityNotFoundException exception"); + log.debug("entityNotFoundException exception : "+ ex.getMessage()); return new ResponseEntity(response, HttpStatus.NOT_FOUND); } @@ -66,7 +66,7 @@ public class ExceptionsHandler { response.setErrorMessage("Path not valid Exception"); response.setErrors(ex.getMessage()); response.setStatus(HttpStatus.NOT_FOUND); - log.debug("pathNotValidException exception"); + log.debug("pathNotValidException exception : "+ ex.getMessage()); return new ResponseEntity(response, HttpStatus.NOT_FOUND); } }