From e140afebe49ad880c1b09d402bcb616f9723f6ea Mon Sep 17 00:00:00 2001 From: Giancarlo Panichi Date: Tue, 21 Jan 2020 11:28:33 +0100 Subject: [PATCH] ref 18291: Modify the Accounting Dashboard portlet to support the Core Services Category per gateway https://support.d4science.org/issues/18291 Fixed the creation of the tree --- .../accounting/PortalContextTreeProvider.java | 81 +++++++++---------- .../server/is/BuildInfraNode.java | 39 ++++----- .../AccountingServiceTest.java | 8 +- .../user/accountingdashboard/ColorTest.java | 4 +- .../user/accountingdashboard/ISTest.java | 34 ++++++-- .../user/accountingdashboard/ServiceTest.java | 4 +- 6 files changed, 92 insertions(+), 78 deletions(-) diff --git a/src/main/java/org/gcube/portlets/user/accountingdashboard/server/accounting/PortalContextTreeProvider.java b/src/main/java/org/gcube/portlets/user/accountingdashboard/server/accounting/PortalContextTreeProvider.java index f892bb5..ddce2aa 100644 --- a/src/main/java/org/gcube/portlets/user/accountingdashboard/server/accounting/PortalContextTreeProvider.java +++ b/src/main/java/org/gcube/portlets/user/accountingdashboard/server/accounting/PortalContextTreeProvider.java @@ -96,9 +96,9 @@ public class PortalContextTreeProvider implements ContextTreeProvider { LinkedList infraChildren = new LinkedList<>(); PortalContext pContext = PortalContext.getConfiguration(); GCubeUser currUser = pContext.getCurrentUser(request); - - InfraNode infraNode=BuildInfraNode.build(infrastructureScope); - + + InfraNode infraNode = BuildInfraNode.build(infrastructureScope); + List theGateways = new LiferayGroupManager().getGateways(); for (GCubeGroup gCubeGroup : theGateways) { logger.debug("Gateway: [id=" + gCubeGroup.getGroupId() + ", name=" + gCubeGroup.getGroupName() + "]"); @@ -108,49 +108,45 @@ public class PortalContextTreeProvider implements ContextTreeProvider { LinkedList gatewayChildren = retrieveGatewayChildren(request, gCubeGroup.getGroupId(), currUser); - - if(infraNode!=null){ - String currScope=portalContext.getCurrentScope(gCubeGroup.getGroupId()+""); - ScopeDescriptor infraNodeScopeDescriptor=createRelativeInfraNode(infraNode, currScope); - if(gatewayChildren!=null){ + + if (infraNode != null) { + String currScope = portalContext.getCurrentScope(gCubeGroup.getGroupId() + ""); + ScopeDescriptor infraNodeScopeDescriptor = createRelativeInfraNode(infraNode, currScope); + if (gatewayChildren != null) { gatewayChildren.addFirst(infraNodeScopeDescriptor); } else { - gatewayChildren=new LinkedList<>(); + gatewayChildren = new LinkedList<>(); gatewayChildren.add(infraNodeScopeDescriptor); } } - + ScopeDescriptor gatewayScopeDescriptor = new ScopeDescriptor(gCubeGroup.getGroupName(), gCubeGroup.getGroupId() + ""); gatewayScopeDescriptor.setChildren(gatewayChildren); infraChildren.add(gatewayScopeDescriptor); } - infra.setChildren(infraChildren); return infra; } - - private ScopeDescriptor createRelativeInfraNode(InfraNode infraNode, String scope){ - ScopeDescriptor scopeDescriptor=null; - if(infraNode!=null){ - StringBuilder absoluteScope=new StringBuilder(); - absoluteScope.append(scope); - absoluteScope.append("/"); - absoluteScope.append(infraNode.getScope()); - scopeDescriptor=new ScopeDescriptor(infraNode.getName(), absoluteScope.toString()); - if(infraNode.getChildren()!=null&&!infraNode.getChildren().isEmpty()){ - LinkedList childsDescriptor=new LinkedList<>(); - for(InfraNode child:infraNode.getChildren()){ - ScopeDescriptor childScopeDescriptor=createRelativeInfraNode(child, absoluteScope.toString()); - childsDescriptor.add(childScopeDescriptor); - } - scopeDescriptor.setChildren(childsDescriptor); + + private ScopeDescriptor createRelativeInfraNode(InfraNode infraNode, String scope) { + StringBuilder absoluteScope = new StringBuilder(); + absoluteScope.append(scope); + absoluteScope.append("/"); + absoluteScope.append(infraNode.getScope()); + ScopeDescriptor scopeDescriptor = new ScopeDescriptor(infraNode.getName(), absoluteScope.toString()); + if (infraNode.getChildren() != null && !infraNode.getChildren().isEmpty()) { + LinkedList childsScopeDescriptor = new LinkedList<>(); + for (InfraNode child : infraNode.getChildren()) { + ScopeDescriptor childScopeDescriptor = createRelativeInfraNode(child, absoluteScope.toString()); + childsScopeDescriptor.add(childScopeDescriptor); } - } + scopeDescriptor.setChildren(childsScopeDescriptor); + } + return scopeDescriptor; } - private ScopeDescriptor recreateTreeForPortalContext(HttpServletRequest request) throws Exception, PortalException, SystemException { @@ -160,23 +156,23 @@ public class PortalContextTreeProvider implements ContextTreeProvider { PortalContext pContext = PortalContext.getConfiguration(); GCubeUser currUser = pContext.getCurrentUser(request); - String currScope=pContext.getCurrentScope(currentSiteGroupId+""); - - InfraNode infraNode=BuildInfraNode.build(currScope); - + String currScope = pContext.getCurrentScope(currentSiteGroupId + ""); + + InfraNode infraNode = BuildInfraNode.build(currScope); + LinkedList rootChildren = null; rootChildren = retrieveGatewayChildren(request, currentSiteGroupId, currUser); - - if(infraNode!=null){ - ScopeDescriptor infraNodeScopeDescriptor=createRelativeInfraNode(infraNode, currScope); - if(rootChildren!=null){ + + if (infraNode != null) { + ScopeDescriptor infraNodeScopeDescriptor = createRelativeInfraNode(infraNode, currScope); + if (rootChildren != null) { rootChildren.addFirst(infraNodeScopeDescriptor); } else { - rootChildren=new LinkedList<>(); + rootChildren = new LinkedList<>(); rootChildren.add(infraNodeScopeDescriptor); } } - + Group rootGroup = getSiteFromServletRequest(request); root = new ScopeDescriptor(rootGroup.getDescriptiveName(), rootGroup.getGroupId() + ""); root.setChildren(rootChildren); @@ -186,17 +182,16 @@ public class PortalContextTreeProvider implements ContextTreeProvider { private LinkedList retrieveGatewayChildren(HttpServletRequest request, long currentSiteGroupId, GCubeUser currUser) throws Exception, PortalException, SystemException { - logger.info("Retrieve Gateway Children: currentSiteGroupId="+currentSiteGroupId); - + logger.info("Retrieve Gateway Children: currentSiteGroupId=" + currentSiteGroupId); + // PARSE TREE LinkedHashMap> gatewayTree = getPortalSitesMappedToVRE(currUser, currentSiteGroupId); logger.debug("Parsing tree from gateway. Size {} ", gatewayTree.size()); - LinkedList rootChildren = new LinkedList<>(); - + for (Entry> entry : gatewayTree.entrySet()) { ScopeDescriptor rootChild = new ScopeDescriptor(entry.getKey().name, entry.getKey().categoryID + ""); for (VRE vre : entry.getValue()) diff --git a/src/main/java/org/gcube/portlets/user/accountingdashboard/server/is/BuildInfraNode.java b/src/main/java/org/gcube/portlets/user/accountingdashboard/server/is/BuildInfraNode.java index 64c02b9..84b1394 100644 --- a/src/main/java/org/gcube/portlets/user/accountingdashboard/server/is/BuildInfraNode.java +++ b/src/main/java/org/gcube/portlets/user/accountingdashboard/server/is/BuildInfraNode.java @@ -19,7 +19,7 @@ public class BuildInfraNode { private static Logger logger = LoggerFactory.getLogger(BuildInfraNode.class); public static InfraNode build(String scope) throws ServiceException { - + logger.info("Build Infra Nodes"); InfraNode infraNode = null; if (Constants.DEBUG_MODE) { @@ -42,8 +42,10 @@ public class BuildInfraNode { if (infraNodeJAXB != null) { infraNode = new InfraNode(infraNodeJAXB.getScope(), infraNodeJAXB.getName(), infraNodeJAXB.getDescription()); - ArrayList children = retrieveChildren(infraNodeJAXB); - infraNode.setChildren(children); + if (infraNodeJAXB.getChildren() != null && !infraNodeJAXB.getChildren().isEmpty()) { + ArrayList children = retrieveChildren(infraNodeJAXB); + infraNode.setChildren(children); + } } else { logger.info("Infra Nodes use default configuration for scope: " + scope); @@ -64,9 +66,9 @@ public class BuildInfraNode { private static InfraNode useDefaultConfiguration() { InfraNode infraNodeCoreServices = new InfraNode("CoreServices", "Core Services"); - InfraNode infraNodeCatalogue = new InfraNode("CoreServices/Catalogue", "Catalogue"); - InfraNode infraNodeWorkspace = new InfraNode("CoreServices/Workspace", "Workspace"); - InfraNode infraNodeMessages = new InfraNode("CoreServices/Messages", "Messages"); + InfraNode infraNodeCatalogue = new InfraNode("Catalogue", "Catalogue"); + InfraNode infraNodeWorkspace = new InfraNode("Workspace", "Workspace"); + InfraNode infraNodeMessages = new InfraNode("Messages", "Messages"); ArrayList children = new ArrayList(); children.add(infraNodeCatalogue); @@ -79,23 +81,22 @@ public class BuildInfraNode { private static ArrayList retrieveChildren(InfraNodeJAXB infraNodeJAXB) throws ServiceException { try { - if (infraNodeJAXB.getChildren() == null || infraNodeJAXB.getChildren().isEmpty()) { - return null; - } else { - ArrayList children = new ArrayList<>(); - for (InfraNodeJAXB childJAXB : infraNodeJAXB.getChildren()) { - InfraNode child = new InfraNode(childJAXB.getScope(), childJAXB.getName(), - childJAXB.getDescription()); - ArrayList childrenOfChild = retrieveChildren(infraNodeJAXB); + ArrayList children = new ArrayList<>(); + for (InfraNodeJAXB childJAXB : infraNodeJAXB.getChildren()) { + InfraNode child = new InfraNode(childJAXB.getScope(), childJAXB.getName(), childJAXB.getDescription()); + if (childJAXB.getChildren() != null && !childJAXB.getChildren().isEmpty()) { + ArrayList childrenOfChild = retrieveChildren(childJAXB); child.setChildren(childrenOfChild); - children.add(child); } - return children; - + children.add(child); } + return children; + } catch (Throwable e) { - logger.error("Ivalid infra nodes configuration: " + e.getLocalizedMessage(), e); - throw new ServiceException("Ivalid infra nodes configuration: " + e.getLocalizedMessage(), e); + logger.error("Ivalid infra nodes configuration. Error retrieving children for infra nodes: " + + e.getLocalizedMessage(), e); + throw new ServiceException("Ivalid infra nodes configuration. Error retrieving children for infra nodes: " + + e.getLocalizedMessage(), e); } } diff --git a/src/test/java/org/gcube/portlets/user/accountingdashboard/AccountingServiceTest.java b/src/test/java/org/gcube/portlets/user/accountingdashboard/AccountingServiceTest.java index a95fe65..693567c 100644 --- a/src/test/java/org/gcube/portlets/user/accountingdashboard/AccountingServiceTest.java +++ b/src/test/java/org/gcube/portlets/user/accountingdashboard/AccountingServiceTest.java @@ -27,7 +27,7 @@ public class AccountingServiceTest extends TestCase { AccountingService accountingService = new AccountingService(AccountingServiceType.CurrentScope); // accountingService.getTree(); - assertTrue("Success", true); + assertTrue(true); } catch (Throwable e) { logger.error(e.getLocalizedMessage(), e); fail("Error:" + e.getLocalizedMessage()); @@ -35,7 +35,7 @@ public class AccountingServiceTest extends TestCase { } } else { - assertTrue("Success", true); + assertTrue(true); } } @@ -44,7 +44,7 @@ public class AccountingServiceTest extends TestCase { if (Constants.TEST_ENABLE) { try { - assertTrue("Success", true); + assertTrue(true); } catch (Throwable e) { logger.error(e.getLocalizedMessage(), e); @@ -53,7 +53,7 @@ public class AccountingServiceTest extends TestCase { } } else { - assertTrue("Success", true); + assertTrue(true); } } diff --git a/src/test/java/org/gcube/portlets/user/accountingdashboard/ColorTest.java b/src/test/java/org/gcube/portlets/user/accountingdashboard/ColorTest.java index de823dd..b166dfc 100644 --- a/src/test/java/org/gcube/portlets/user/accountingdashboard/ColorTest.java +++ b/src/test/java/org/gcube/portlets/user/accountingdashboard/ColorTest.java @@ -123,7 +123,7 @@ public class ColorTest extends TestCase { logger.debug("HTML Color green: " + htmlSingleColorBuilder.toString()); - assertTrue("Success", true); + assertTrue(true); } catch (Throwable e) { logger.error(e.getLocalizedMessage(), e); fail("Error:" + e.getLocalizedMessage()); @@ -131,7 +131,7 @@ public class ColorTest extends TestCase { } } else { - assertTrue("Success", true); + assertTrue(true); } } diff --git a/src/test/java/org/gcube/portlets/user/accountingdashboard/ISTest.java b/src/test/java/org/gcube/portlets/user/accountingdashboard/ISTest.java index 6be825e..00f298f 100644 --- a/src/test/java/org/gcube/portlets/user/accountingdashboard/ISTest.java +++ b/src/test/java/org/gcube/portlets/user/accountingdashboard/ISTest.java @@ -3,11 +3,15 @@ */ package org.gcube.portlets.user.accountingdashboard; +import java.io.File; import java.io.StringWriter; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.ArrayList; import javax.xml.bind.JAXBContext; import javax.xml.bind.Marshaller; +import javax.xml.bind.Unmarshaller; import org.gcube.portlets.user.accountingdashboard.server.is.AccountingDashboardConfigJAXB; import org.gcube.portlets.user.accountingdashboard.server.is.BuildInfraNode; @@ -58,20 +62,22 @@ public class ISTest extends TestCase { logger.debug("Test Enabled"); try { + logger.info("Check Marshalling"); + InfraNodeJAXB infraNodeCoreServices = new InfraNodeJAXB(); infraNodeCoreServices.setScope("CoreServices"); infraNodeCoreServices.setName("Core Services"); - + InfraNodeJAXB infraNodeCatalogue = new InfraNodeJAXB(); - infraNodeCatalogue.setScope("CoreServices/Catalogue"); + infraNodeCatalogue.setScope("Catalogue"); infraNodeCatalogue.setName("Catalogue"); - + InfraNodeJAXB infraNodeWorkspace = new InfraNodeJAXB(); - infraNodeWorkspace.setScope("CoreServices/Workspace"); + infraNodeWorkspace.setScope("Workspace"); infraNodeWorkspace.setName("Workspace"); - + InfraNodeJAXB infraNodeMessages = new InfraNodeJAXB(); - infraNodeMessages.setScope("CoreServices/Messages"); + infraNodeMessages.setScope("Messages"); infraNodeMessages.setName("Messages"); ArrayList children = new ArrayList<>(); @@ -79,8 +85,8 @@ public class ISTest extends TestCase { children.add(infraNodeWorkspace); children.add(infraNodeMessages); infraNodeCoreServices.setChildren(children); - - AccountingDashboardConfigJAXB config=new AccountingDashboardConfigJAXB(); + + AccountingDashboardConfigJAXB config = new AccountingDashboardConfigJAXB(); config.setEnabledInfraNode(true); config.setBaseInfraNode(infraNodeCoreServices); @@ -97,6 +103,18 @@ public class ISTest extends TestCase { // Verify XML Content String xmlContent = sw.toString(); logger.debug(xmlContent); + + logger.info("Check Unmarshalling"); + Path path = Files.createTempFile("AccountingDashboardConfig", ".xml"); + File file = path.toFile(); + jaxbMarshaller.marshal(config, file); + + Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); + AccountingDashboardConfigJAXB configUnmarshalled = (AccountingDashboardConfigJAXB) jaxbUnmarshaller + .unmarshal(file); + logger.debug("Config unmarshallded: " + configUnmarshalled); + file.delete(); + logger.info("Success!"); assertTrue(true); } catch (Throwable e) { diff --git a/src/test/java/org/gcube/portlets/user/accountingdashboard/ServiceTest.java b/src/test/java/org/gcube/portlets/user/accountingdashboard/ServiceTest.java index 2cd7917..7c57f41 100644 --- a/src/test/java/org/gcube/portlets/user/accountingdashboard/ServiceTest.java +++ b/src/test/java/org/gcube/portlets/user/accountingdashboard/ServiceTest.java @@ -22,7 +22,7 @@ public class ServiceTest extends TestCase { try { - assertTrue("Success", true); + assertTrue(true); } catch (Throwable e) { logger.error(e.getLocalizedMessage(), e); fail("Error:" + e.getLocalizedMessage()); @@ -30,7 +30,7 @@ public class ServiceTest extends TestCase { } } else { - assertTrue("Success", true); + assertTrue(true); } }