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 eb85a09..7b8666e 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 @@ -18,6 +18,8 @@ import org.gcube.infrastructure.detachedres.detachedreslibrary.shared.re.Detache import org.gcube.infrastructure.detachedres.detachedreslibrary.shared.re.Gateway; import org.gcube.infrastructure.detachedres.detachedreslibrary.shared.re.VO; import org.gcube.portlets.user.accountingdashboard.server.is.BuildInfraNode; +import org.gcube.portlets.user.accountingdashboard.server.util.CocktailSort; +import org.gcube.portlets.user.accountingdashboard.server.util.CocktailSort.Order; import org.gcube.portlets.user.accountingdashboard.shared.is.InfraNode; import org.gcube.portlets.user.accountingdashboard.shared.options.TreeOptions; import org.gcube.vomanagement.usermanagement.GroupManager; @@ -53,9 +55,9 @@ public class PortalContextTreeProvider implements ContextTreeProvider { this.accountingServiceType = accountingServiceType; this.treeOptions = null; } - - public void setTreeOptions(TreeOptions treeOptions){ - this.treeOptions=treeOptions; + + public void setTreeOptions(TreeOptions treeOptions) { + this.treeOptions = treeOptions; } static { @@ -86,7 +88,7 @@ public class PortalContextTreeProvider implements ContextTreeProvider { break; case Infrastructure: logger.debug("AccountingService: Infrastructure"); - root = recreateTreeForInfrastructure(request, treeOptions); + root = recreateTreeForInfrastructure(request); break; default: logger.debug("AccountingService: CurrentScope"); @@ -97,7 +99,7 @@ public class PortalContextTreeProvider implements ContextTreeProvider { return root; } - private ScopeDescriptor recreateTreeForInfrastructure(HttpServletRequest request, TreeOptions treeOptions2) + private ScopeDescriptor recreateTreeForInfrastructure(HttpServletRequest request) throws Exception, PortalException, SystemException { ScopeDescriptor infra = null; PortalContext portalContext = PortalContext.getConfiguration(); @@ -156,51 +158,60 @@ public class PortalContextTreeProvider implements ContextTreeProvider { if (detachedREs != null && detachedREs.isEnabled()) { logger.debug("DetachedREs is enabled"); if (detachedREs.getGateways() != null && !detachedREs.getGateways().isEmpty()) { - for (String key : detachedREs.getGateways().keySet()) { - Gateway gateway = detachedREs.getGateways().get(key); - ScopeDescriptor getewaySD = new ScopeDescriptor(gateway.getName(), key); - LinkedList voChildren = retrieveVOChildren(gateway); + ArrayList gatewaysList = new ArrayList(detachedREs.getGateways().values()); + Collections.sort(gatewaysList); + for (Gateway gateway : gatewaysList) { + ScopeDescriptor getewaySD = new ScopeDescriptor(gateway.getName(), gateway.getScope()); + LinkedList voChildren = retrieveVOChildren(infraNode, gateway); getewaySD.setChildren(voChildren); infraChildren.add(getewaySD); } + CocktailSort.sort(infraChildren,Order.DESC); } } else { logger.debug("DetachedREs is disabled"); } } - + infra.setChildren(infraChildren); return infra; } - private LinkedList retrieveVOChildren(Gateway gateway) { + private LinkedList retrieveVOChildren(InfraNode infraNode, Gateway gateway) { + LinkedList vos = new LinkedList<>(); + + if (infraNode != null) { + ScopeDescriptor infraNodeScopeDescriptor = createRelativeInfraNode(infraNode, gateway.getScope()); + vos.add(infraNodeScopeDescriptor); + } if (gateway.getVos() != null && !gateway.getVos().isEmpty()) { - LinkedList vos = new LinkedList<>(); - for (String key : gateway.getVos().keySet()) { - VO vo = gateway.getVos().get(key); - ScopeDescriptor voSD = new ScopeDescriptor(vo.getName(), key); + ArrayList vosList = new ArrayList(gateway.getVos().values()); + Collections.sort(vosList); + for (VO vo : vosList) { + ScopeDescriptor voSD = new ScopeDescriptor(vo.getName(), vo.getScope()); LinkedList voChildren = retrieveVREChildren(vo); voSD.setChildren(voChildren); vos.add(voSD); } - return vos; - } else { - return null; } + + return vos; } private LinkedList retrieveVREChildren(VO vo) { + LinkedList vres = new LinkedList<>(); + if (vo.getVres() != null && !vo.getVres().isEmpty()) { - LinkedList vres = new LinkedList<>(); - for (String key : vo.getVres().keySet()) { - org.gcube.infrastructure.detachedres.detachedreslibrary.shared.re.VRE vre = vo.getVres().get(key); - ScopeDescriptor vreSD = new ScopeDescriptor(vre.getName(), key); + ArrayList vresList = new ArrayList( + vo.getVres().values()); + Collections.sort(vresList); + for (org.gcube.infrastructure.detachedres.detachedreslibrary.shared.re.VRE vre : vresList) { + ScopeDescriptor vreSD = new ScopeDescriptor(vre.getName(), vre.getScope()); vres.add(vreSD); } - return vres; - } else { - return null; + } + return vres; } private ScopeDescriptor createRelativeInfraNode(InfraNode infraNode, String scope) { diff --git a/src/main/java/org/gcube/portlets/user/accountingdashboard/server/util/CocktailSort.java b/src/main/java/org/gcube/portlets/user/accountingdashboard/server/util/CocktailSort.java new file mode 100644 index 0000000..b8435e2 --- /dev/null +++ b/src/main/java/org/gcube/portlets/user/accountingdashboard/server/util/CocktailSort.java @@ -0,0 +1,71 @@ +package org.gcube.portlets.user.accountingdashboard.server.util; + +import java.util.List; + +import org.gcube.accounting.accounting.summary.access.model.ScopeDescriptor; + +public class CocktailSort { + + public enum Order { + ASC, DESC + } + + public static void sort(List list, Order order) { + if (list != null && list.size() > 1) { + + // `begin` ed `end` first and last index to check + int begin = -1; + int end = list.size() - 1; + boolean swapped; + do { + swapped = false; + // increases `begin` because the elements before `begin` are + // sorted correctly + begin = begin + 1; + for (int i = begin; i < end; i++) { + if (order == Order.ASC) { + if (list.get(i).getName().compareTo(list.get(i + 1).getName()) < 0) { + ScopeDescriptor sd = list.get(i); + list.set(i, list.get(i + 1)); + list.set(i + 1, sd); + swapped = true; + } + } else { + if (list.get(i).getName().compareTo(list.get(i + 1).getName()) > 0) { + ScopeDescriptor sd = list.get(i); + list.set(i, list.get(i + 1)); + list.set(i + 1, sd); + swapped = true; + } + } + + } + if (swapped == false) { + break; + } + swapped = false; + // decreases `end` because the elements after `end` are sorted + // correctly + end = end - 1; + for (int i = end; i > begin; i--) { + if (order == Order.ASC) { + if (list.get(i).getName().compareTo(list.get(i - 1).getName()) > 0) { + ScopeDescriptor sd = list.get(i); + list.set(i, list.get(i - 1)); + list.set(i - 1, sd); + swapped = true; + } + } else { + if (list.get(i).getName().compareTo(list.get(i - 1).getName()) < 0) { + ScopeDescriptor sd = list.get(i); + list.set(i, list.get(i - 1)); + list.set(i - 1, sd); + swapped = true; + } + } + } + } while (swapped); + } + } + +}