[Trunk | Admin tools]: MenuItemController.java & MenuItemDAO.java & MongoDBMenuItemDAO.java & MenuItem.java & MenuItemFull.java & MenuItemService.java: Added dynamically configurable Menu Items for specific portal.
This commit is contained in:
parent
38833860e3
commit
ad80551db7
|
@ -0,0 +1,213 @@
|
||||||
|
package eu.dnetlib.uoaadmintools.controllers;
|
||||||
|
|
||||||
|
import eu.dnetlib.uoaadmintools.dao.NotificationsDAO;
|
||||||
|
import eu.dnetlib.uoaadmintools.entities.Notifications;
|
||||||
|
import eu.dnetlib.uoaadmintools.entities.menu.MenuItem;
|
||||||
|
import eu.dnetlib.uoaadmintools.entities.menu.MenuItemFull;
|
||||||
|
import eu.dnetlib.uoaadmintools.services.MenuItemService;
|
||||||
|
import eu.dnetlib.uoaadmintoolslibrary.dao.PortalDAO;
|
||||||
|
import eu.dnetlib.uoaadmintoolslibrary.entities.Portal;
|
||||||
|
import eu.dnetlib.uoaadmintoolslibrary.entities.fullEntities.PortalPage;
|
||||||
|
import eu.dnetlib.uoaadmintoolslibrary.handlers.ContentNotFoundException;
|
||||||
|
import eu.dnetlib.uoaadmintoolslibrary.handlers.MismatchingContentException;
|
||||||
|
import eu.dnetlib.uoaadmintoolslibrary.handlers.utils.RolesUtils;
|
||||||
|
import org.apache.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.List;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@CrossOrigin(origins = "*")
|
||||||
|
public class MenuItemController {
|
||||||
|
private final Logger log = Logger.getLogger(this.getClass());
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private MenuItemService menuItemService;
|
||||||
|
@Autowired
|
||||||
|
private PortalDAO portalDAO;
|
||||||
|
@Autowired
|
||||||
|
private RolesUtils rolesUtils;
|
||||||
|
|
||||||
|
// @PreAuthorize("hasAnyAuthority(" +
|
||||||
|
// "@AuthorizationService.PORTAL_ADMIN, " +
|
||||||
|
// "@AuthorizationService.curator('community'), @AuthorizationService.manager('community', #pid))")
|
||||||
|
@RequestMapping(value = "/community/{pid}/menu", method = RequestMethod.GET)
|
||||||
|
public List<MenuItem> getMenuItems(@PathVariable(value = "pid") String pid ) throws ContentNotFoundException {
|
||||||
|
Portal portal = portalDAO.findByPid(pid);
|
||||||
|
if(portal == null){
|
||||||
|
throw new ContentNotFoundException("Portal with pid: "+pid+" not found");
|
||||||
|
}
|
||||||
|
if(!portal.getType().equals("community")) {
|
||||||
|
// EXCEPTION - MismatchingContent
|
||||||
|
throw new MismatchingContentException("Get Notifications: Portal with id: "+portal.getId()+" has type: "+portal.getType()+" instead of community");
|
||||||
|
}
|
||||||
|
|
||||||
|
List<MenuItem> menuItems = menuItemService.getMenuItems(pid);
|
||||||
|
if(menuItems == null || menuItems.size() == 0){
|
||||||
|
throw new ContentNotFoundException("Menu for community with pid: "+pid+" not found");
|
||||||
|
}
|
||||||
|
return menuItems;
|
||||||
|
}
|
||||||
|
|
||||||
|
@RequestMapping(value = "/community/{pid}/menu/{itemId}/full", method = RequestMethod.GET)
|
||||||
|
public MenuItemFull getMenuItemFull(@PathVariable(value = "pid") String pid, @PathVariable(value = "itemId") String itemId ) throws ContentNotFoundException {
|
||||||
|
Portal portal = portalDAO.findByPid(pid);
|
||||||
|
if(portal == null){
|
||||||
|
throw new ContentNotFoundException("Portal with pid: "+pid+" not found");
|
||||||
|
}
|
||||||
|
if(!portal.getType().equals("community")) {
|
||||||
|
// EXCEPTION - MismatchingContent
|
||||||
|
throw new MismatchingContentException("Get Menu Item Full: Portal with id: "+portal.getId()+" has type: "+portal.getType()+" instead of community");
|
||||||
|
}
|
||||||
|
|
||||||
|
MenuItemFull menuItem = menuItemService.getMenuItemFull(itemId);
|
||||||
|
if(menuItem == null){
|
||||||
|
throw new ContentNotFoundException("MenuItem with id: "+itemId+" for community with pid: "+pid+" not found");
|
||||||
|
}
|
||||||
|
return menuItem;
|
||||||
|
}
|
||||||
|
|
||||||
|
@RequestMapping(value = "/community/{pid}/menu/root/full", method = RequestMethod.GET)
|
||||||
|
public List<MenuItemFull> getRootMenuItemsFull(@PathVariable(value = "pid") String pid ) throws ContentNotFoundException {
|
||||||
|
Portal portal = portalDAO.findByPid(pid);
|
||||||
|
if(portal == null){
|
||||||
|
throw new ContentNotFoundException("Portal with pid: "+pid+" not found");
|
||||||
|
}
|
||||||
|
if(!portal.getType().equals("community")) {
|
||||||
|
// EXCEPTION - MismatchingContent
|
||||||
|
throw new MismatchingContentException("getMenuItemsFull: Portal with id: "+portal.getId()+" has type: "+portal.getType()+" instead of community");
|
||||||
|
}
|
||||||
|
|
||||||
|
List<MenuItemFull> menuItems = menuItemService.getRootMenuItemsFull(pid);
|
||||||
|
// if(menuItems == null || menuItems.size() == 0){
|
||||||
|
// throw new ContentNotFoundException("Root menu for community with pid: "+pid+" not found");
|
||||||
|
// }
|
||||||
|
return menuItems;
|
||||||
|
}
|
||||||
|
|
||||||
|
@RequestMapping(value = "/community/{pid}/menu/root", method = RequestMethod.GET)
|
||||||
|
public List<MenuItem> getRootMenuItems(@PathVariable(value = "pid") String pid ) throws ContentNotFoundException {
|
||||||
|
Portal portal = portalDAO.findByPid(pid);
|
||||||
|
if(portal == null){
|
||||||
|
throw new ContentNotFoundException("Portal with pid: "+pid+" not found");
|
||||||
|
}
|
||||||
|
if(!portal.getType().equals("community")) {
|
||||||
|
// EXCEPTION - MismatchingContent
|
||||||
|
throw new MismatchingContentException("getMenuItemsFull: Portal with id: "+portal.getId()+" has type: "+portal.getType()+" instead of community");
|
||||||
|
}
|
||||||
|
|
||||||
|
List<MenuItem> menuItems = menuItemService.getMenuItemsByParent(null, pid);
|
||||||
|
if(menuItems == null || menuItems.size() == 0){
|
||||||
|
throw new ContentNotFoundException("Root menu for community with pid: "+pid+" not found");
|
||||||
|
}
|
||||||
|
return menuItems;
|
||||||
|
}
|
||||||
|
|
||||||
|
@RequestMapping(value = "/community/{pid}/menu/{parentId}/children", method = RequestMethod.GET)
|
||||||
|
public List<MenuItem> getChildrenMenuItemsOfParent(@PathVariable(value = "pid") String pid, @PathVariable(value="parentId") String parentId) throws ContentNotFoundException {
|
||||||
|
Portal portal = portalDAO.findByPid(pid);
|
||||||
|
if(portal == null){
|
||||||
|
throw new ContentNotFoundException("Portal with pid: "+pid+" not found");
|
||||||
|
}
|
||||||
|
if(!portal.getType().equals("community")) {
|
||||||
|
// EXCEPTION - MismatchingContent
|
||||||
|
throw new MismatchingContentException("getMenuItemsFull: Portal with id: "+portal.getId()+" has type: "+portal.getType()+" instead of community");
|
||||||
|
}
|
||||||
|
|
||||||
|
List<MenuItem> menuItems = menuItemService.getMenuItemsByParent(parentId, pid);
|
||||||
|
if(menuItems == null || menuItems.size() == 0){
|
||||||
|
throw new ContentNotFoundException("Root menu for community with pid: "+pid+" not found");
|
||||||
|
}
|
||||||
|
return menuItems;
|
||||||
|
}
|
||||||
|
|
||||||
|
@PreAuthorize("hasAnyAuthority(" +
|
||||||
|
"@AuthorizationService.PORTAL_ADMIN, " +
|
||||||
|
"@AuthorizationService.curator('community')," +
|
||||||
|
"@AuthorizationService.manager('community', #pid))")
|
||||||
|
@RequestMapping(value = "/community/{pid}/menu/update", method = RequestMethod.POST)
|
||||||
|
public MenuItemFull updateMenuItem(@PathVariable String pid, @RequestBody MenuItemFull menuItemFull) {
|
||||||
|
Portal portal = portalDAO.findByPid(pid);
|
||||||
|
if(portal == null){
|
||||||
|
throw new ContentNotFoundException("Portal with pid: "+pid+" not found");
|
||||||
|
}
|
||||||
|
if(!portal.getType().equals("community")) {
|
||||||
|
// EXCEPTION - MismatchingContent
|
||||||
|
throw new MismatchingContentException("updateMenuItem: Portal with id: "+portal.getId()+" has type: "+portal.getType()+" instead of community");
|
||||||
|
}
|
||||||
|
if(menuItemFull.getType() == null ||
|
||||||
|
(menuItemFull.getType().equals("internal") && menuItemFull.getRoute() == null) ||
|
||||||
|
(menuItemFull.getType().equals("external") && menuItemFull.getUrl() == null)) {
|
||||||
|
// EXCEPTION - MismatchingContent
|
||||||
|
throw new MismatchingContentException("insertMenuItem: A required field is missing in menu item: type="+menuItemFull.getType()
|
||||||
|
+ " - url="+menuItemFull.getUrl() + " - route="+menuItemFull.getRoute());
|
||||||
|
}
|
||||||
|
if(menuItemFull.getPortalPid() == null) {
|
||||||
|
menuItemFull.setPortalPid(pid);
|
||||||
|
} else if(!menuItemFull.getPortalPid().equals(pid)) {
|
||||||
|
// EXCEPTION - MismatchingContent
|
||||||
|
throw new MismatchingContentException("updateMenuItem: MenuItem has portalPid: "+menuItemFull.getPortalPid()+" instead of "+pid);
|
||||||
|
}
|
||||||
|
if(menuItemFull.getId() == null) {
|
||||||
|
// EXCEPTION - MismatchingContent
|
||||||
|
throw new MismatchingContentException("updateMenuItem: This MenuItem has no id.");
|
||||||
|
}
|
||||||
|
|
||||||
|
return menuItemService.updateMenuItem(menuItemFull);
|
||||||
|
}
|
||||||
|
|
||||||
|
@PreAuthorize("hasAnyAuthority(" +
|
||||||
|
"@AuthorizationService.PORTAL_ADMIN, " +
|
||||||
|
"@AuthorizationService.curator('community')," +
|
||||||
|
"@AuthorizationService.manager('community', #pid))")
|
||||||
|
@RequestMapping(value = "/community/{pid}/menu/save", method = RequestMethod.POST)
|
||||||
|
public MenuItemFull insertMenuItem(@PathVariable String pid, @RequestBody MenuItem menuItem) {
|
||||||
|
Portal portal = portalDAO.findByPid(pid);
|
||||||
|
if(portal == null){
|
||||||
|
throw new ContentNotFoundException("Portal with pid: "+pid+" not found");
|
||||||
|
}
|
||||||
|
if(portal.getType() == null || !portal.getType().equals("community")) {
|
||||||
|
// EXCEPTION - MismatchingContent
|
||||||
|
throw new MismatchingContentException("insertMenuItem: Portal with id: "+portal.getId()+" has type: "+portal.getType()+" instead of community");
|
||||||
|
}
|
||||||
|
if(menuItem.getType() == null ||
|
||||||
|
(menuItem.getType().equals("internal") && menuItem.getRoute() == null) ||
|
||||||
|
(menuItem.getType().equals("external") && menuItem.getUrl() == null)) {
|
||||||
|
// EXCEPTION - MismatchingContent
|
||||||
|
throw new MismatchingContentException("insertMenuItem: A required field is missing in menu item: type="+menuItem.getType()
|
||||||
|
+ " - url="+menuItem.getUrl() + " - route="+menuItem.getRoute());
|
||||||
|
}
|
||||||
|
if(menuItem.getPortalPid() == null) {
|
||||||
|
menuItem.setPortalPid(pid);
|
||||||
|
} else if(!menuItem.getPortalPid().equals(pid)) {
|
||||||
|
// EXCEPTION - MismatchingContent
|
||||||
|
throw new MismatchingContentException("insertMenuItem: MenuItem has portalPid: "+menuItem.getPortalPid()+" instead of "+pid);
|
||||||
|
}
|
||||||
|
if(menuItem.getId() != null) {
|
||||||
|
// EXCEPTION - MismatchingContent
|
||||||
|
throw new MismatchingContentException("insertMenuItem: MenuItem has already an id: "+menuItem.getId());
|
||||||
|
}
|
||||||
|
|
||||||
|
return menuItemService.insertMenuItem(menuItem, pid);
|
||||||
|
}
|
||||||
|
|
||||||
|
@PreAuthorize("hasAnyAuthority(" +
|
||||||
|
"@AuthorizationService.PORTAL_ADMIN, " +
|
||||||
|
"@AuthorizationService.curator('community')," +
|
||||||
|
"@AuthorizationService.manager('community', #pid))")
|
||||||
|
@RequestMapping(value = "/community/{pid}/menu/delete", method = RequestMethod.POST)
|
||||||
|
public Boolean deleteMenuItem(@PathVariable String pid, @RequestBody String menuItemId) throws Exception {
|
||||||
|
Portal portal = portalDAO.findByPid(pid);
|
||||||
|
if(portal == null){
|
||||||
|
throw new ContentNotFoundException("Portal with pid: "+pid+" not found");
|
||||||
|
}
|
||||||
|
if(!portal.getType().equals("community")) {
|
||||||
|
// EXCEPTION - MismatchingContent
|
||||||
|
throw new MismatchingContentException("Get Notifications: Portal with id: "+portal.getId()+" has type: "+portal.getType()+" instead of community");
|
||||||
|
}
|
||||||
|
|
||||||
|
return menuItemService.deleteMenuItem(menuItemId, pid);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,21 @@
|
||||||
|
package eu.dnetlib.uoaadmintools.dao;
|
||||||
|
|
||||||
|
import eu.dnetlib.uoaadmintools.entities.menu.MenuItem;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public interface MenuItemDAO {
|
||||||
|
List<MenuItem> findAll();
|
||||||
|
List<MenuItem> findByParentItemId(String parentId);
|
||||||
|
|
||||||
|
MenuItem findById(String Id);
|
||||||
|
|
||||||
|
List<MenuItem> findByPortalPid(String portalPid);
|
||||||
|
List<MenuItem> findByParentItemIdAndPortalPid(String parentId, String portalPid);
|
||||||
|
|
||||||
|
MenuItem save(MenuItem menuItem);
|
||||||
|
|
||||||
|
void deleteAll();
|
||||||
|
|
||||||
|
void delete(String id);
|
||||||
|
}
|
|
@ -0,0 +1,23 @@
|
||||||
|
package eu.dnetlib.uoaadmintools.dao.MongoDBDAOs;
|
||||||
|
|
||||||
|
import eu.dnetlib.uoaadmintools.dao.MenuItemDAO;
|
||||||
|
import eu.dnetlib.uoaadmintools.entities.menu.MenuItem;
|
||||||
|
import org.springframework.data.mongodb.repository.MongoRepository;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public interface MongoDBMenuItemDAO extends MenuItemDAO, MongoRepository<MenuItem, String> {
|
||||||
|
List<MenuItem> findAll();
|
||||||
|
List<MenuItem> findByParentItemId(String parentId);
|
||||||
|
|
||||||
|
MenuItem findById(String Id);
|
||||||
|
|
||||||
|
List<MenuItem> findByPortalPid(String portalPid);
|
||||||
|
List<MenuItem> findByParentItemIdAndPortalPid(String parentId, String portalPid);
|
||||||
|
|
||||||
|
MenuItem save(MenuItem menuItem);
|
||||||
|
|
||||||
|
void deleteAll();
|
||||||
|
|
||||||
|
void delete(String id);
|
||||||
|
}
|
|
@ -0,0 +1,96 @@
|
||||||
|
package eu.dnetlib.uoaadmintools.entities.menu;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||||
|
import org.springframework.data.annotation.Id;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class MenuItem {
|
||||||
|
@Id
|
||||||
|
@JsonProperty("_id")
|
||||||
|
private String id; // for root menu in order to close the dropdown when clicked
|
||||||
|
|
||||||
|
String title;
|
||||||
|
String url; // external url
|
||||||
|
String route; // internal url - using angular routing and components
|
||||||
|
String type; // internal or external
|
||||||
|
List<String> items;
|
||||||
|
String parentItemId;
|
||||||
|
String portalPid;
|
||||||
|
|
||||||
|
public MenuItem(){
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(String id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getTitle() {
|
||||||
|
return title;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setTitle(String title) {
|
||||||
|
this.title = title;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getUrl() {
|
||||||
|
return url;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setUrl(String url) {
|
||||||
|
this.url = url;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getRoute() {
|
||||||
|
return route;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRoute(String route) {
|
||||||
|
this.route = route;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getType() {
|
||||||
|
return type;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setType(String type) {
|
||||||
|
this.type = type;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<String> getItems() {
|
||||||
|
return items;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setItems(List<String> items) {
|
||||||
|
this.items = items;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getParentItemId() {
|
||||||
|
return parentItemId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setParentItemId(String parentItemId) {
|
||||||
|
this.parentItemId = parentItemId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getPortalPid() {
|
||||||
|
return portalPid;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPortalPid(String portalPid) {
|
||||||
|
this.portalPid = portalPid;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "MenuItem{" +
|
||||||
|
"id='" + id + '\'' +
|
||||||
|
// ", notifyForNewManagers=" + notifyForNewManagers +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,104 @@
|
||||||
|
package eu.dnetlib.uoaadmintools.entities.menu;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||||
|
import org.springframework.data.annotation.Id;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class MenuItemFull {
|
||||||
|
@Id
|
||||||
|
@JsonProperty("_id")
|
||||||
|
private String id; // for root menu in order to close the dropdown when clicked
|
||||||
|
|
||||||
|
String title;
|
||||||
|
String url; // external url
|
||||||
|
String route; // internal url - using angular routing and components
|
||||||
|
String type; // internal or external
|
||||||
|
List<MenuItemFull> items;
|
||||||
|
String parentItemId;
|
||||||
|
String portalPid;
|
||||||
|
|
||||||
|
public MenuItemFull(){}
|
||||||
|
public MenuItemFull(MenuItem menuItem){
|
||||||
|
setId(menuItem.getId());
|
||||||
|
setTitle(menuItem.getTitle());
|
||||||
|
setUrl(menuItem.getUrl());
|
||||||
|
setType(menuItem.getType());
|
||||||
|
setRoute(menuItem.getRoute());
|
||||||
|
setParentItemId(menuItem.getParentItemId());
|
||||||
|
setPortalPid(menuItem.getPortalPid());
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(String id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getTitle() {
|
||||||
|
return title;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setTitle(String title) {
|
||||||
|
this.title = title;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getUrl() {
|
||||||
|
return url;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setUrl(String url) {
|
||||||
|
this.url = url;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getRoute() {
|
||||||
|
return route;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRoute(String route) {
|
||||||
|
this.route = route;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public String getType() {
|
||||||
|
return type;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setType(String type) {
|
||||||
|
this.type = type;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<MenuItemFull> getItems() {
|
||||||
|
return items;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setItems(List<MenuItemFull> items) {
|
||||||
|
this.items = items;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getParentItemId() {
|
||||||
|
return parentItemId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setParentItemId(String parentItemId) {
|
||||||
|
this.parentItemId = parentItemId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getPortalPid() {
|
||||||
|
return portalPid;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPortalPid(String portalPid) {
|
||||||
|
this.portalPid = portalPid;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "MenuItemFull{" +
|
||||||
|
"id='" + id + '\'' +
|
||||||
|
// ", notifyForNewManagers=" + notifyForNewManagers +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,197 @@
|
||||||
|
package eu.dnetlib.uoaadmintools.services;
|
||||||
|
|
||||||
|
import eu.dnetlib.uoaadmintools.dao.MenuItemDAO;
|
||||||
|
import eu.dnetlib.uoaadmintools.entities.menu.MenuItem;
|
||||||
|
import eu.dnetlib.uoaadmintools.entities.menu.MenuItemFull;
|
||||||
|
import eu.dnetlib.uoaadmintoolslibrary.handlers.MismatchingContentException;
|
||||||
|
import org.apache.log4j.Logger;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class MenuItemService {
|
||||||
|
private final Logger log = Logger.getLogger(this.getClass());
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private MenuItemDAO menuItemDAO;
|
||||||
|
|
||||||
|
public MenuItem getMenuItem(String id) {
|
||||||
|
return menuItemDAO.findById(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
public MenuItemFull getMenuItemFull(String id) {
|
||||||
|
return this.getMenuItemFull(id, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
public MenuItemFull getMenuItemFull(String id, int maxDepth) {
|
||||||
|
MenuItem menuItem = menuItemDAO.findById(id);
|
||||||
|
MenuItemFull menuItemFull = new MenuItemFull(menuItem);
|
||||||
|
if(maxDepth == 0) {
|
||||||
|
menuItemFull.setItems(new ArrayList<>());
|
||||||
|
return menuItemFull;
|
||||||
|
}
|
||||||
|
|
||||||
|
List<MenuItemFull> menuItemsFull = new ArrayList<>();
|
||||||
|
for(String menuItemString : menuItem.getItems()) {
|
||||||
|
menuItemsFull.add(this.getMenuItemFull(menuItemString, maxDepth-1));
|
||||||
|
}
|
||||||
|
menuItemFull.setItems(menuItemsFull);
|
||||||
|
|
||||||
|
return menuItemFull;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<MenuItem> getMenuItems(String portalPid) {
|
||||||
|
if(portalPid != null) {
|
||||||
|
return menuItemDAO.findByPortalPid(portalPid);
|
||||||
|
} else {
|
||||||
|
return menuItemDAO.findAll();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<MenuItem> getMenuItemsByParent(String parentId, String portalPid) {
|
||||||
|
if(portalPid != null) {
|
||||||
|
return menuItemDAO.findByParentItemIdAndPortalPid(parentId, portalPid);
|
||||||
|
} else {
|
||||||
|
return menuItemDAO.findByParentItemId(parentId);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<MenuItemFull> getRootMenuItemsFull(String portalPid) {
|
||||||
|
List<MenuItem> rootMenuItems = this.getMenuItemsByParent(null, portalPid);
|
||||||
|
List<MenuItemFull> rootMenuItemsFull = new ArrayList<>();
|
||||||
|
for(MenuItem rootMenuItem : rootMenuItems) {
|
||||||
|
MenuItemFull rootMenuItemFull = new MenuItemFull(rootMenuItem);
|
||||||
|
|
||||||
|
List<MenuItemFull> childrenMenuItemsFull = new ArrayList<>();
|
||||||
|
for(String childMenuItemString : rootMenuItem.getItems()) {
|
||||||
|
childrenMenuItemsFull.add(this.getMenuItemFull(childMenuItemString));
|
||||||
|
}
|
||||||
|
rootMenuItemFull.setItems(childrenMenuItemsFull);
|
||||||
|
rootMenuItemsFull.add(rootMenuItemFull);
|
||||||
|
}
|
||||||
|
|
||||||
|
return rootMenuItemsFull;
|
||||||
|
}
|
||||||
|
|
||||||
|
// public MenuItemFull saveAndReturn(MenuItemFull menuItemFull) {
|
||||||
|
// menuItemFull.setId(save(menuItemFull));
|
||||||
|
// return menuItemFull;
|
||||||
|
// }
|
||||||
|
|
||||||
|
public MenuItemFull insertMenuItem(MenuItem menuItem, String portalPid) {
|
||||||
|
// for(MenuItemFull childMenuItemFull : menuItemFull.getItems()) {
|
||||||
|
// childMenuItemFull.setId(this.save(childMenuItemFull));
|
||||||
|
// }
|
||||||
|
|
||||||
|
// MenuItem menuItem = getMenuItemByMenuItemFull(menuItemFull);
|
||||||
|
// menuItemDAO.save(menuItem);
|
||||||
|
//
|
||||||
|
// return menuItem.getId();
|
||||||
|
|
||||||
|
MenuItem parent = null;
|
||||||
|
if(menuItem.getParentItemId() != null && !menuItem.getParentItemId().equals("")) {
|
||||||
|
parent = getMenuItem(menuItem.getParentItemId());
|
||||||
|
if (!parent.getPortalPid().equals(portalPid)) {
|
||||||
|
// EXCEPTION - MismatchingContent
|
||||||
|
throw new MismatchingContentException("insertMenuItem: parent ("+parent.getParentItemId()+") of MenuItem has portalPid: " + parent.getPortalPid() + " instead of " + portalPid);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
menuItem.setParentItemId(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
// MenuItem menuItem = getMenuItemByMenuItemFull(menuItemFull);
|
||||||
|
if(menuItem.getItems() == null) {
|
||||||
|
List<String> menuItems = new ArrayList<String>();
|
||||||
|
menuItem.setItems(menuItems);
|
||||||
|
}
|
||||||
|
menuItemDAO.save(menuItem);
|
||||||
|
|
||||||
|
if(parent != null) {
|
||||||
|
List<String> siblingsOfNew = parent.getItems();
|
||||||
|
siblingsOfNew.add(menuItem.getId());
|
||||||
|
parent.setItems(siblingsOfNew);
|
||||||
|
menuItemDAO.save(parent);
|
||||||
|
}
|
||||||
|
|
||||||
|
MenuItemFull menuItemFull = new MenuItemFull(menuItem);
|
||||||
|
menuItemFull.setItems(new ArrayList<>());
|
||||||
|
|
||||||
|
return menuItemFull;
|
||||||
|
}
|
||||||
|
|
||||||
|
public MenuItemFull updateMenuItem(MenuItemFull menuItemFull) {
|
||||||
|
MenuItem menuItem = getMenuItemByMenuItemFull(menuItemFull);
|
||||||
|
|
||||||
|
// Update should not affect parent or children - only basic info can be updated
|
||||||
|
MenuItem oldMenuItem = getMenuItem(menuItemFull.getId());
|
||||||
|
menuItem.setItems(oldMenuItem.getItems());
|
||||||
|
menuItem.setParentItemId(oldMenuItem.getParentItemId());
|
||||||
|
|
||||||
|
menuItemDAO.save(menuItem);
|
||||||
|
menuItemFull = getMenuItemFull(menuItem.getId());
|
||||||
|
|
||||||
|
return menuItemFull;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Boolean deleteMenuItem(String id, String portalPid) throws Exception {
|
||||||
|
// menuItemDAO.delete(id);
|
||||||
|
log.debug("delete menu item; "+id);
|
||||||
|
List<String> menuItems = new ArrayList<>();
|
||||||
|
menuItems.add(id);
|
||||||
|
return deleteMenuItems(menuItems, portalPid);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Boolean deleteMenuItems(List<String> menuItems, String portalPid) throws Exception {
|
||||||
|
if(menuItems == null) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
for (String id: menuItems) {
|
||||||
|
MenuItem menuItem = menuItemDAO.findById(id);
|
||||||
|
|
||||||
|
if(!portalPid.equals(menuItem.getPortalPid())) {
|
||||||
|
// EXCEPTION - MismatchingContent
|
||||||
|
throw new MismatchingContentException("Delete Menu Items: MenuItem with id: "+id+" has portalPid: "+menuItem.getPortalPid()+" instead of "+portalPid);
|
||||||
|
}
|
||||||
|
|
||||||
|
deleteMenuItems(menuItem.getItems(), portalPid);
|
||||||
|
|
||||||
|
if(menuItem.getParentItemId() != null && !menuItem.getParentItemId().equals("")) {
|
||||||
|
MenuItem parent = menuItemDAO.findById(menuItem.getParentItemId());
|
||||||
|
List<String> siblingsOfDeleted = parent.getItems();
|
||||||
|
siblingsOfDeleted.remove(id);
|
||||||
|
parent.setItems(siblingsOfDeleted);
|
||||||
|
menuItemDAO.save(parent);
|
||||||
|
}
|
||||||
|
|
||||||
|
menuItemDAO.delete(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private MenuItem getMenuItemByMenuItemFull(MenuItemFull menuItemFull) {
|
||||||
|
MenuItem menuItem = new MenuItem();
|
||||||
|
menuItem.setId(menuItemFull.getId());
|
||||||
|
menuItem.setTitle(menuItemFull.getTitle());
|
||||||
|
menuItem.setUrl(menuItemFull.getUrl());
|
||||||
|
menuItem.setType(menuItemFull.getType());
|
||||||
|
menuItem.setRoute(menuItemFull.getRoute());
|
||||||
|
menuItem.setPortalPid(menuItemFull.getPortalPid());
|
||||||
|
menuItem.setParentItemId(menuItemFull.getParentItemId());
|
||||||
|
|
||||||
|
List<MenuItemFull> menuItemsFull = menuItemFull.getItems();
|
||||||
|
List<String> menuItems = new ArrayList<String>();
|
||||||
|
if(menuItemsFull != null) {
|
||||||
|
for (MenuItemFull childMenuItemFull : menuItemsFull) {
|
||||||
|
menuItems.add(childMenuItemFull.getId());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
menuItem.setItems(menuItems);
|
||||||
|
|
||||||
|
return menuItem;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue