// Create selector.
Selector selector = new Selector();
selector.setFields(new String[] {"Login", "CustomerId"});
// Get results.
ManagedCustomerPage page = managedCustomerService.get(selector);
if (page.getEntries() != null) {
// Create map from customerId to customer node.
Map<Long, ManagedCustomerTreeNode> customerIdToCustomerNode =
new HashMap<Long, ManagedCustomerTreeNode>();
// Create account tree nodes for each customer.
for (ManagedCustomer customer : page.getEntries()) {
ManagedCustomerTreeNode node = new ManagedCustomerTreeNode();
node.managedCustomer = customer;
customerIdToCustomerNode.put(customer.getCustomerId(), node);
}
// For each link, connect nodes in tree.
if (page.getLinks() != null) {
for (ManagedCustomerLink link : page.getLinks()) {
ManagedCustomerTreeNode managerNode = customerIdToCustomerNode.get(
link.getManagerCustomerId());
ManagedCustomerTreeNode childNode = customerIdToCustomerNode.get(
link.getClientCustomerId());
childNode.parentNode = managerNode;
if (managerNode != null) {
managerNode.childAccounts.add(childNode);
}
}
}
// Find the root account node in the tree.
ManagedCustomerTreeNode rootNode = null;
for (ManagedCustomer account : page.getEntries()) {
if (customerIdToCustomerNode.get(account.getCustomerId()).parentNode == null) {
rootNode = customerIdToCustomerNode.get(account.getCustomerId());
break;
}
}