com.aoindustries.aoserv.client.account.AccountTable 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) 2001-2013, 2016, 2017, 2018, 2019, 2020 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.account;
import com.aoindustries.aoserv.client.AOServConnector;
import com.aoindustries.aoserv.client.aosh.AOSH;
import com.aoindustries.aoserv.client.aosh.Command;
import com.aoindustries.aoserv.client.net.Host;
import com.aoindustries.aoserv.client.schema.AoservProtocol;
import com.aoindustries.aoserv.client.schema.Table;
import com.aoindustries.collections.IntList;
import com.aoindustries.io.TerminalWriter;
import com.aoindustries.io.stream.StreamableInput;
import com.aoindustries.io.stream.StreamableOutput;
import com.aoindustries.util.tree.Node;
import com.aoindustries.util.tree.Tree;
import com.aoindustries.util.tree.TreeCopy;
import com.aoindustries.validation.ValidationException;
import com.aoindustries.validation.ValidationResult;
import java.io.IOException;
import java.io.Reader;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* @see Account
*
* @author AO Industries, Inc.
*/
final public class AccountTable extends CachedTableAccountNameKey {
private Account.Name rootAccounting;
AccountTable(AOServConnector connector) {
super(connector, Account.class);
}
private static final OrderBy[] defaultOrderBy = {
new OrderBy(Account.COLUMN_ACCOUNTING_name, ASCENDING)
};
@Override
protected OrderBy[] getDefaultOrderBy() {
return defaultOrderBy;
}
public void addAccount(
final Account.Name accounting,
final String contractNumber,
final Host defaultServer,
final Account.Name parent,
final boolean canAddBackupServers,
final boolean canAddAccounts,
final boolean canSeePrices,
final boolean billParent
) throws IOException, SQLException {
connector.requestUpdate(
true,
AoservProtocol.CommandID.ADD,
new AOServConnector.UpdateRequest() {
IntList invalidateList;
@Override
public void writeRequest(StreamableOutput out) throws IOException {
out.writeCompressedInt(Table.TableID.BUSINESSES.ordinal());
out.writeUTF(accounting.toString());
out.writeBoolean(contractNumber!=null);
if(contractNumber!=null) out.writeUTF(contractNumber);
out.writeCompressedInt(defaultServer.getPkey());
out.writeUTF(parent.toString());
out.writeBoolean(canAddBackupServers);
out.writeBoolean(canAddAccounts);
out.writeBoolean(canSeePrices);
out.writeBoolean(billParent);
}
@Override
public void readResponse(StreamableInput in) throws IOException, SQLException {
int code=in.readByte();
if(code==AoservProtocol.DONE) invalidateList=AOServConnector.readInvalidateList(in);
else {
AoservProtocol.checkResult(code, in);
throw new IOException("Unexpected response code: "+code);
}
}
@Override
public void afterRelease() {
connector.tablesUpdated(invalidateList);
}
}
);
}
@Override
public void clearCache() {
super.clearCache();
synchronized(this) {
rootAccounting=null;
}
}
public Account.Name generateAccountingCode(Account.Name template) throws IOException, SQLException {
try {
return Account.Name.valueOf(
connector.requestStringQuery(
true,
AoservProtocol.CommandID.GENERATE_ACCOUNTING_CODE,
template.toString()
)
);
} catch(ValidationException e) {
throw new IOException(e);
}
}
/**
* Gets one {@link Account} from the database.
*/
@Override
public Account get(Account.Name accounting) throws IOException, SQLException {
return getUniqueRow(Account.COLUMN_ACCOUNTING, accounting);
}
List getChildAccounts(Account business) throws IOException, SQLException {
Account.Name accounting=business.getName();
List cached=getRows();
List matches=new ArrayList<>();
int size=cached.size();
for(int c=0;c getTopLevelAccounts() throws IOException, SQLException {
List cached=getRows();
List matches=new ArrayList<>();
int size=cached.size();
for(int c=0;c
private final Tree tree = () -> {
List topLevelAccounts = getTopLevelAccounts();
int size = topLevelAccounts.size();
if(size==0) {
return Collections.emptyList();
} else if(size==1) {
Node singleNode = new AccountTreeNode(topLevelAccounts.get(0));
return Collections.singletonList(singleNode);
} else {
List> rootNodes = new ArrayList<>(size);
for(Account topLevelAccount : topLevelAccounts) rootNodes.add(new AccountTreeNode(topLevelAccount));
return Collections.unmodifiableList(rootNodes);
}
};
static class AccountTreeNode implements Node {
private final Account business;
AccountTreeNode(Account business) {
this.business = business;
}
@Override
public List> getChildren() throws IOException, SQLException {
// Look for any existing children
List children = business.getChildAccounts();
int size = children.size();
if(size==0) {
if(business.canAddAccounts()) {
// Can have children but empty
return Collections.emptyList();
} else {
// Not allowed to have children
return null;
}
} else if(size==1) {
Node singleNode = new AccountTreeNode(children.get(0));
return Collections.singletonList(singleNode);
} else {
List> childNodes = new ArrayList<>(size);
for(Account child : children) childNodes.add(new AccountTreeNode(child));
return Collections.unmodifiableList(childNodes);
}
}
@Override
public Account getValue() {
return business;
}
}
/**
* Gets a Tree view of all the accessible businesses.
* 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 - 2025 Weber Informatics LLC | Privacy Policy