com.aoindustries.aoserv.client.ResellerTable Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of aoserv-client Show documentation
Show all versions of aoserv-client Show documentation
Java client for the AOServ Platform.
/*
* aoserv-client - Java client for the AOServ platform.
* Copyright (C) 2009-2013, 2016 AO Industries, Inc.
* [email protected]
* 7262 Bull Pen Cir
* Mobile, AL 36695
*
* This file is part of aoserv-client.
*
* aoserv-client is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* aoserv-client is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with aoserv-client. If not, see .
*/
package com.aoindustries.aoserv.client;
import com.aoindustries.aoserv.client.validator.AccountingCode;
import com.aoindustries.util.tree.Node;
import com.aoindustries.util.tree.Tree;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* @see Reseller
*
* @author AO Industries, Inc.
*/
final public class ResellerTable extends CachedTableAccountingCodeKey {
ResellerTable(AOServConnector connector) {
super(connector, Reseller.class);
}
private static final OrderBy[] defaultOrderBy = {
new OrderBy(Reseller.COLUMN_ACCOUNTING_name, ASCENDING)
};
@Override
OrderBy[] getDefaultOrderBy() {
return defaultOrderBy;
}
/**
* Gets a Reseller
from the database.
*/
@Override
public Reseller get(AccountingCode accounting) throws IOException, SQLException {
return getUniqueRow(Reseller.COLUMN_ACCOUNTING, accounting);
}
/**
* Gets a Reseller
given its brand.
*/
Reseller getReseller(Brand brand) throws IOException, SQLException {
return getUniqueRow(Reseller.COLUMN_ACCOUNTING, brand.pkey);
}
@Override
public SchemaTable.TableID getTableID() {
return SchemaTable.TableID.RESELLERS;
}
/**
* Gets the list of all resellers that either have a null parent (the
* actual root of the business tree) or where the parent is inaccessible.
*/
public List getTopLevelResellers() throws IOException, SQLException {
List matches=new ArrayList<>();
for(Reseller reseller : getRows()) {
if(reseller.getParentReseller()==null) matches.add(reseller);
}
return matches;
}
//
private final Tree tree = new Tree() {
@Override
public List> getRootNodes() throws IOException, SQLException {
List topLevelResellers = getTopLevelResellers();
int size = topLevelResellers.size();
if(size==0) {
return Collections.emptyList();
} else if(size==1) {
Node singleNode = new ResellerTreeNode(topLevelResellers.get(0));
return Collections.singletonList(singleNode);
} else {
List> rootNodes = new ArrayList<>(size);
for(Reseller topLevelReseller : topLevelResellers) rootNodes.add(new ResellerTreeNode(topLevelReseller));
return Collections.unmodifiableList(rootNodes);
}
}
};
static class ResellerTreeNode implements Node {
private final Reseller reseller;
ResellerTreeNode(Reseller reseller) {
this.reseller = reseller;
}
/**
* The children of the reseller are any resellers that have their closest parent
* business (that is a reseller) equal to this one.
*/
@Override
public List> getChildren() throws IOException, SQLException {
// Look for any existing children
List children = reseller.getChildResellers();
int size = children.size();
if(size==0) {
// Any reseller without children is rendered as not being able to have children.
return null;
} else if(size==1) {
Node singleNode = new ResellerTreeNode(children.get(0));
return Collections.singletonList(singleNode);
} else {
List> childNodes = new ArrayList<>(size);
for(Reseller child : children) childNodes.add(new ResellerTreeNode(child));
return Collections.unmodifiableList(childNodes);
}
}
@Override
public Reseller getValue() {
return reseller;
}
}
/**
* Gets a Tree view of all the accessible resellers.
* All access to the tree read-through to the underlying storage
* and are thus subject to change at any time. If you need a consistent
* snapshot of the tree, consider using TreeCopy.
*
* @see TreeCopy
*/
public Tree getTree() {
return tree;
}
//
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy