add plugin controller and service and related changes

This commit is contained in:
argirok 2024-04-03 11:20:09 +03:00
parent 0c75baa89b
commit 4eaac0ee19
11 changed files with 650 additions and 9 deletions

View File

@ -2,19 +2,19 @@ package eu.dnetlib.uoaadmintoolslibrary.controllers;
import eu.dnetlib.uoaadmintoolslibrary.entities.*; import eu.dnetlib.uoaadmintoolslibrary.entities.*;
import eu.dnetlib.uoaadmintoolslibrary.entities.fullEntities.*; import eu.dnetlib.uoaadmintoolslibrary.entities.fullEntities.*;
import eu.dnetlib.uoaadmintoolslibrary.entities.plugin.Plugin;
import eu.dnetlib.uoaadmintoolslibrary.entities.plugin.PluginTemplate;
import eu.dnetlib.uoaadmintoolslibrary.handlers.ContentNotFoundException; import eu.dnetlib.uoaadmintoolslibrary.handlers.ContentNotFoundException;
import eu.dnetlib.uoaadmintoolslibrary.handlers.MismatchingContentException; import eu.dnetlib.uoaadmintoolslibrary.handlers.MismatchingContentException;
import eu.dnetlib.uoaadmintoolslibrary.services.*; import eu.dnetlib.uoaadmintoolslibrary.services.*;
import org.apache.http.protocol.ResponseContent;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.security.access.prepost.PreAuthorize;
import java.util.HashMap; import java.util.*;
import java.util.List;
import java.util.Map;
import java.util.Set;
@RestController @RestController
@RequestMapping(value = "{portalType}") @RequestMapping(value = "{portalType}")
@ -28,6 +28,9 @@ public class AdminPortalRelationsController {
@Autowired @Autowired
private DivHelpContentService divHelpContentService; private DivHelpContentService divHelpContentService;
@Autowired
private PluginService pluginService;
@Autowired @Autowired
private DivIdService divIdService; private DivIdService divIdService;
@ -127,6 +130,51 @@ public class AdminPortalRelationsController {
return mapCount; return mapCount;
} }
@RequestMapping(value = "/{pid}/pluginTemplate/page/count", method = RequestMethod.GET)
public Map<String, Integer> countPluginTemplatesForPages(@PathVariable PortalType portalType, @PathVariable(value = "pid") String pid) {
Map<String, Integer> mapCount = new HashMap<String, Integer>();
List<Page> pages = pageService.getAllPages(pid, null, null);
for(Page page : pages){
List<PluginTemplate> plugin = pluginService.getPluginTemplatesByPage( page.getId());
if (plugin == null) {
mapCount.put(page.getId(), 0);
} else {
mapCount.put(page.getId(), plugin.size());
}
}
return mapCount;
}
@RequestMapping(value = "/{pid}/plugins/page/{pageId}", method = RequestMethod.GET)
public List<Plugin> getPluginsForPage(@PathVariable PortalType portalType,@PathVariable(value = "pid") String pid, @PathVariable(value = "pageId") String pageId) {
return pluginService.getPluginsByPage(pid, pageId);
}
@RequestMapping(value = "/{pid}/pluginTemplates/page/{pageId}", method = RequestMethod.GET)
public List<PluginTemplate> getPluginTemplatesForPages(@PathVariable PortalType portalType,@PathVariable(value = "pid") String pid, @PathVariable(value = "pageId") String pageId) {
return pluginService.getPluginTemplatesByPage(pageId);
}
@RequestMapping(value = "/{pid}/plugins/page/route", method = RequestMethod.GET)
public List<Plugin> getPluginsForPageRoute(@PathVariable PortalType portalType,@PathVariable(value = "pid") String pid, @RequestParam(value = "route") String route) {
Page p = pageService.getPageByPortalTypeAndRoute(portalType + "",route,pid);
if(p != null) {
return pluginService.getPluginsByPage(pid,p.getId());
}
return new ArrayList<>();
}
@RequestMapping(value = "/{pid}/pluginTemplates/page/route", method = RequestMethod.GET)
public List<PluginTemplate> getPluginTemplatesForPageRoute(@PathVariable PortalType portalType,@PathVariable(value = "pid") String pid,
@RequestParam(value = "route") String route) {
Page p = pageService.getPageByPortalTypeAndRoute(portalType + "",route ,pid);
if(p != null) {
return pluginService.getPluginTemplatesByPage(p.getId());
}
return new ArrayList<>();
}
@PreAuthorize("hasAnyAuthority(" + @PreAuthorize("hasAnyAuthority(" +
"@AuthorizationService.PORTAL_ADMIN, " + "@AuthorizationService.PORTAL_ADMIN, " +
"@AuthorizationService.curator(#portalType), @AuthorizationService.manager(#portalType, #pid))") "@AuthorizationService.curator(#portalType), @AuthorizationService.manager(#portalType, #pid))")

View File

@ -2,6 +2,7 @@ package eu.dnetlib.uoaadmintoolslibrary.controllers;
import eu.dnetlib.uoaadmintoolslibrary.entities.Page; import eu.dnetlib.uoaadmintoolslibrary.entities.Page;
import eu.dnetlib.uoaadmintoolslibrary.entities.fullEntities.PortalPage; import eu.dnetlib.uoaadmintoolslibrary.entities.fullEntities.PortalPage;
import eu.dnetlib.uoaadmintoolslibrary.entities.plugin.PluginTemplate;
import eu.dnetlib.uoaadmintoolslibrary.handlers.ForbiddenException; import eu.dnetlib.uoaadmintoolslibrary.handlers.ForbiddenException;
import eu.dnetlib.uoaadmintoolslibrary.handlers.MismatchingContentException; import eu.dnetlib.uoaadmintoolslibrary.handlers.MismatchingContentException;
import eu.dnetlib.uoaadmintoolslibrary.handlers.utils.RolesUtils; import eu.dnetlib.uoaadmintoolslibrary.handlers.utils.RolesUtils;
@ -107,11 +108,11 @@ public class PageController {
return pageService.deletePages(pages); return pageService.deletePages(pages);
} }
// // not used by portals //used from admin - manage templates per page
// @RequestMapping(value = "/page/{id}", method = RequestMethod.GET) @RequestMapping(value = "/page/{id}", method = RequestMethod.GET)
// public Page getPage(@PathVariable(value = "id") String id) { public Page getPage(@PathVariable(value = "id") String id) {
// return pageService.getPage(id); return pageService.getPage(id);
// } }
// //
// // not used by portals // // not used by portals
// @RequestMapping(value = "/page/{id}", method = RequestMethod.DELETE) // @RequestMapping(value = "/page/{id}", method = RequestMethod.DELETE)

View File

@ -0,0 +1,137 @@
package eu.dnetlib.uoaadmintoolslibrary.controllers;
import eu.dnetlib.uoaadmintoolslibrary.entities.Page;
import eu.dnetlib.uoaadmintoolslibrary.entities.plugin.Plugin;
import eu.dnetlib.uoaadmintoolslibrary.entities.plugin.PluginTemplate;
import eu.dnetlib.uoaadmintoolslibrary.services.PageService;
import eu.dnetlib.uoaadmintoolslibrary.services.PluginService;
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.HashMap;
import java.util.List;
import java.util.Map;
@RestController
@CrossOrigin(origins = "*")
public class PluginController {
private final Logger log = LogManager.getLogger(this.getClass());
@Autowired
private PluginService pluginService;
@Autowired
private PageService pageService;
// used WITHOUT ANY PARAMS
@RequestMapping(value = "/plugins", method = RequestMethod.GET)
public List<Plugin> getPlugins() {
return pluginService.getPlugins(null, null, null);
}
@RequestMapping(value = "/plugins/template/{id}", method = RequestMethod.GET)
public List<Plugin> getPluginsByTemplate(@PathVariable(value = "id") String id) {
return pluginService.getPluginsByTemplateId(id);
}
@RequestMapping(value = "/pluginTemplates", method = RequestMethod.GET)
public List<PluginTemplate> getPluginTemplates(/*@RequestParam(required = false) String page,
@RequestParam(required = false) String name,
@RequestParam(value="portal", required = false) String pid*/) {
return pluginService.getPluginTemplates(null, null, null);
}
@RequestMapping(value = "/pluginTemplates/page/{pageId}", method = RequestMethod.GET)
public List<PluginTemplate> getPluginTemplatesByPage(@PathVariable(value = "pageId") String pageId) {
return pluginService.getPluginTemplates(pageId, null, null);
}
@RequestMapping(value = "/pluginTemplates/{id}", method = RequestMethod.GET)
public PluginTemplate getPluginTemplateById(@PathVariable(value = "id") String id) {
return pluginService.getPluginTemplateById(id);
}
// used
@PreAuthorize("hasAnyAuthority(" +
"@AuthorizationService.PORTAL_ADMIN, " +
"@AuthorizationService.curator('community'))")
@RequestMapping(value = "/pluginTemplate/save", method = RequestMethod.POST)
public PluginTemplate insertTemplate(@RequestBody PluginTemplate pluginTemplate ) {
return pluginService.savePluginTemplate(pluginTemplate);
}
@PreAuthorize("hasAnyAuthority(" +
"@AuthorizationService.PORTAL_ADMIN, " +
"@AuthorizationService.curator('community'))")
@RequestMapping(value = "/pluginTemplate/save/order/{position}", method = RequestMethod.POST)
public PluginTemplate updatePluginTemplateOrder(@RequestBody PluginTemplate pluginTemplate, @PathVariable(value = "position") Integer position ) {
PluginTemplate pt = pluginService.getPluginTemplateById(pluginTemplate.getId());
pt.setOrder(pt.getOrder() != null ? pt.getOrder() + position: 0 );
return pluginService.savePluginTemplate(pt);
}
@PreAuthorize("hasAnyAuthority(" +
"@AuthorizationService.PORTAL_ADMIN, " +
"@AuthorizationService.curator('community')," +
"@AuthorizationService.manager('community', #pid))")
@RequestMapping(value = "/community/{pid}/plugin/save/order/{position}", method = RequestMethod.POST)
public Plugin updatePluginOrder(@PathVariable String pid,@RequestBody Plugin plugin, @PathVariable(value = "position") Integer position ) {
Plugin pl = pluginService.getPluginById(plugin.getId());
pl.setOrder(pl.getOrder() != null ? pl.getOrder() + position: 0 );
return pluginService.savePlugin(pl);
}
@PreAuthorize("hasAnyAuthority(" +
"@AuthorizationService.PORTAL_ADMIN, " +
"@AuthorizationService.curator('community')," +
"@AuthorizationService.manager('community', #pid))")
@RequestMapping(value = "/community/{pid}/plugin/save", method = RequestMethod.POST)
public Plugin savePlugin(@PathVariable String pid, @RequestBody Plugin plugin ) {
return pluginService.savePlugin(plugin);
}
@PreAuthorize("hasAnyAuthority(" +
"@AuthorizationService.PORTAL_ADMIN, " +
"@AuthorizationService.curator('community'))")
@RequestMapping(value = "/community/{pid}/plugin/status/{id}", method = RequestMethod.POST)
public Plugin updatePluginStatus(@PathVariable String pid, @PathVariable(value = "id") String id, @RequestBody String status ) {
return pluginService.updatePluginStatus(id,status);
}
@RequestMapping(value = "/pluginTemplate/page/count", method = RequestMethod.GET)
public Map<String, Integer> countPluginTemplatesForPages() {
Map<String, Integer> mapCount = new HashMap<String, Integer>();
List<Page> pages = pageService.getAllPages(null, null, null);
for(Page page : pages){
List<PluginTemplate> plugin = pluginService.getPluginTemplatesByPage( page.getId());
if (plugin == null) {
mapCount.put(page.getId(), 0);
} else {
mapCount.put(page.getId(), plugin.size());
}
}
return mapCount;
}
// not used by portals
@PreAuthorize("hasAnyAuthority(" +
"@AuthorizationService.PORTAL_ADMIN, " +
"@AuthorizationService.curator('community'))")
@RequestMapping(value = "/pluginTemplate/{id}", method = RequestMethod.DELETE)
public void deletePluginTemplate(@PathVariable(value = "id") String id) {
pluginService.deletePluginTemplate(id);
}
@RequestMapping(value = "/plugin/template/count", method = RequestMethod.GET)
public Map<String, Integer> countPluginsByTemplate() {
Map<String, Integer> mapCount = new HashMap<String, Integer>();
List<PluginTemplate> templates = pluginService.getPluginTemplates(null,null,null);
for(PluginTemplate template: templates){
List<Plugin> plugins = pluginService.getPluginsByTemplateId(template.getId());
if (plugins == null) {
mapCount.put(template.getId(), 0);
} else {
mapCount.put(template.getId(), plugins.size());
}
}
return mapCount;
}
}

View File

@ -0,0 +1,21 @@
package eu.dnetlib.uoaadmintoolslibrary.dao.MongoDBDAOs;
import eu.dnetlib.uoaadmintoolslibrary.dao.PluginDAO;
import eu.dnetlib.uoaadmintoolslibrary.entities.plugin.Plugin;
import eu.dnetlib.uoaadmintoolslibrary.entities.plugin.PluginTemplate;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.mongodb.repository.Query;
import java.util.List;
public interface MongoDBPluginDAO extends PluginDAO, MongoRepository<Plugin, String> {
Plugin findById(String Id);
List<Plugin> findAll();
List<Plugin> findByPage(String page);
List<Plugin> findByPageAndPid(String page, String pid);
List<Plugin> findByTemplateId(String templateId);
Plugin save(Plugin plugin);
void deleteAll();
void delete(String id);
}

View File

@ -0,0 +1,23 @@
package eu.dnetlib.uoaadmintoolslibrary.dao.MongoDBDAOs;
import eu.dnetlib.uoaadmintoolslibrary.dao.DivIdDAO;
import eu.dnetlib.uoaadmintoolslibrary.dao.PluginTemplateDAO;
import eu.dnetlib.uoaadmintoolslibrary.entities.DivId;
import eu.dnetlib.uoaadmintoolslibrary.entities.plugin.PluginTemplate;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.mongodb.repository.Query;
import java.util.List;
public interface MongoDBPluginTemplateDAO extends PluginTemplateDAO, MongoRepository<PluginTemplate, String> {
PluginTemplate findById(String Id);
PluginTemplate findByCode(String code);
List<PluginTemplate> findAll();
@Query("{ pages: ?0 }")
List<PluginTemplate> findByPages(String page);
PluginTemplate save(PluginTemplate pluginTemplate);
void deleteAll();
void delete(String id);
}

View File

@ -0,0 +1,18 @@
package eu.dnetlib.uoaadmintoolslibrary.dao;
import eu.dnetlib.uoaadmintoolslibrary.entities.plugin.Plugin;
import java.util.List;
public interface PluginDAO {
Plugin findById(String Id);
List<Plugin> findAll();
List<Plugin> findByPage(String page);
List<Plugin> findByPageAndPid(String page, String pid);
List<Plugin> findByTemplateId(String templateId);
Plugin save(Plugin plugin);
void deleteAll();
void delete(String id);
}

View File

@ -0,0 +1,16 @@
package eu.dnetlib.uoaadmintoolslibrary.dao;
import eu.dnetlib.uoaadmintoolslibrary.entities.plugin.PluginTemplate;
import java.util.List;
public interface PluginTemplateDAO {
PluginTemplate findById(String Id);
List<PluginTemplate> findAll();
List<PluginTemplate> findByPage(String page);
PluginTemplate save(PluginTemplate pluginTemplate);
void deleteAll();
void delete(String id);
}

View File

@ -0,0 +1,106 @@
package eu.dnetlib.uoaadmintoolslibrary.entities.plugin;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.springframework.data.annotation.Id;
import java.util.HashMap;
import java.util.Map;
public class Plugin {
@Id
@JsonProperty("_id")
private String id;
private String pid;
private String templateCode;
private String templateId;
private String page;
private String placement;
private Integer order;
private Boolean isActive = true;
private Map<String,String> settingsValues = new HashMap<>();
private Object object;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getPid() {
return pid;
}
public void setPid(String pid) {
this.pid = pid;
}
public String getTemplateCode() {
return templateCode;
}
public void setTemplateCode(String templateCode) {
this.templateCode = templateCode;
}
public String getTemplateId() {
return templateId;
}
public void setTemplateId(String templateId) {
this.templateId = templateId;
}
public String getPage() {
return page;
}
public void setPage(String page) {
this.page = page;
}
public String getPlacement() {
return placement;
}
public void setPlacement(String placement) {
this.placement = placement;
}
public Integer getOrder() {
return order;
}
public void setOrder(Integer order) {
this.order = order;
}
public Boolean getActive() {
return isActive;
}
public void setActive(Boolean active) {
isActive = active;
}
public Map<String, String> getSettingsValues() {
return settingsValues;
}
public void setSettingsValues(Map<String, String> settingsValues) {
this.settingsValues = settingsValues;
}
public Object getObject() {
return object;
}
public void setObject(Object object) {
this.object = object;
}
}

View File

@ -0,0 +1,31 @@
package eu.dnetlib.uoaadmintoolslibrary.entities.plugin;
public class PluginAttributes {
String name;
String type;
String value;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}

View File

@ -0,0 +1,144 @@
package eu.dnetlib.uoaadmintoolslibrary.entities.plugin;
import com.fasterxml.jackson.annotation.JsonProperty;
import eu.dnetlib.uoaadmintoolslibrary.entities.PortalType;
import org.springframework.data.annotation.Id;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class PluginTemplate {
@Id
@JsonProperty("_id")
private String id;
private String name;
private String description;
private String code;
private String image;
private String page;
private String placement;
private Integer order;
private PortalType portalType; // explore, connect, community, monitor
private String plan; // e.g basic, ultimate, etc for paid communities
private Boolean defaultIsActive;
private ArrayList<String> portalSpecific = new ArrayList<>();
private Map<String,PluginAttributes> settings = new HashMap<>();
private Object object;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public PortalType getPortalType() {
return portalType;
}
public void setPortalType(PortalType portalType) {
this.portalType = portalType;
}
public String getPlan() {
return plan;
}
public void setPlan(String plan) {
this.plan = plan;
}
public Object getObject() {
return object;
}
public void setObject(Object object) {
this.object = object;
}
public Map<String, PluginAttributes> getSettings() {
return settings;
}
public void setSettings(Map<String, PluginAttributes> settings) {
this.settings = settings;
}
public Integer getOrder() {
return order;
}
public void setOrder(Integer order) {
this.order = order;
}
public String getPage() {
return page;
}
public void setPage(String page) {
this.page = page;
}
public String getPlacement() {
return placement;
}
public void setPlacement(String placement) {
this.placement = placement;
}
public ArrayList<String> getPortalSpecific() {
return portalSpecific;
}
public void setPortalSpecific(ArrayList<String> portalSpecific) {
this.portalSpecific = portalSpecific;
}
public Boolean getDefaultIsActive() {
return defaultIsActive;
}
public void setDefaultIsActive(Boolean defaultIsActive) {
this.defaultIsActive = defaultIsActive;
}
}

View File

@ -0,0 +1,96 @@
package eu.dnetlib.uoaadmintoolslibrary.services;
import eu.dnetlib.uoaadmintoolslibrary.dao.PluginDAO;
import eu.dnetlib.uoaadmintoolslibrary.dao.PluginTemplateDAO;
import eu.dnetlib.uoaadmintoolslibrary.entities.plugin.Plugin;
import eu.dnetlib.uoaadmintoolslibrary.entities.plugin.PluginTemplate;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
@Service
public class PluginService {
private final Logger log = LogManager.getLogger(this.getClass());
@Autowired
private PluginTemplateDAO pluginTemplateDAO;
@Autowired
private PluginDAO pluginDAO;
public List<PluginTemplate> getPluginTemplates(String page, String name, String pid) {
List<PluginTemplate> templates;
if(page != null){
templates = this.pluginTemplateDAO.findByPage(page);
}else {
templates = this.pluginTemplateDAO.findAll();
}
templates.sort(Comparator.comparingInt(PluginTemplate::getOrder));
return templates;
}
public List<Plugin> getPlugins(String page, String name, String pid) {
return this.pluginDAO.findAll();
}
public Plugin getPluginById(String id) {
return this.pluginDAO.findById(id);
}
public PluginTemplate getPluginTemplateById(String id) {
return this.pluginTemplateDAO.findById(id);
}
public List<Plugin> getPluginsByTemplateId(String id) {
return this.pluginDAO.findByTemplateId(id);
}
public List<PluginTemplate> getPluginTemplatesByPage(String pageId) {
ArrayList<String> pages = new ArrayList<String>();
pages.add(pageId);
List<PluginTemplate> templates = this.pluginTemplateDAO.findByPage(pageId);
templates.sort(Comparator.comparingInt(PluginTemplate::getOrder));
return templates;
}
public List<Plugin> getPluginsByPage(String pid, String pageId) {
ArrayList<String> pages = new ArrayList<String>();
pages.add(pageId);
return this.pluginDAO.findByPageAndPid(pageId,pid);
}
public PluginTemplate savePluginTemplate(PluginTemplate pluginTemplate) {
return this.pluginTemplateDAO.save(pluginTemplate);
}
public Plugin savePlugin(Plugin plugin) {
return this.pluginDAO.save(plugin);
}
public Plugin updatePluginStatus(String id, String status) {
Plugin plugin = this.pluginDAO.findById(id);
if(plugin != null) {
plugin.setActive(Boolean.parseBoolean(status));
}
return this.pluginDAO.save(plugin);
}
public void deletePluginTemplate(String id) {
pluginTemplateDAO.delete(id);
List <Plugin> plugins = pluginDAO.findByTemplateId(id);
for(Plugin p: plugins){
pluginDAO.delete(p.getId());
}
}
public void deletePlugin(String id) {
pluginDAO.delete(id);
}
}