All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.aoindustries.aoserv.client.BusinessTable Maven / Gradle / Ivy

There is a newer version: 1.92.0
Show newest version
/*
 * aoserv-client - Java client for the AOServ platform.
 * Copyright (C) 2001-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.aoserv.client.validator.ValidationException;
import com.aoindustries.aoserv.client.validator.ValidationResult;
import com.aoindustries.io.CompressedDataInputStream;
import com.aoindustries.io.CompressedDataOutputStream;
import com.aoindustries.io.TerminalWriter;
import com.aoindustries.util.IntList;
import com.aoindustries.util.tree.Node;
import com.aoindustries.util.tree.Tree;
import com.aoindustries.util.tree.TreeCopy;
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  Business
 *
 * @author  AO Industries, Inc.
 */
final public class BusinessTable extends CachedTableAccountingCodeKey {

	private AccountingCode rootAccounting;

	BusinessTable(AOServConnector connector) {
		super(connector, Business.class);
	}

	private static final OrderBy[] defaultOrderBy = {
		new OrderBy(Business.COLUMN_ACCOUNTING_name, ASCENDING)
	};
	@Override
	OrderBy[] getDefaultOrderBy() {
		return defaultOrderBy;
	}

	void addBusiness(
		final AccountingCode accounting,
		final String contractNumber,
		final Server defaultServer,
		final AccountingCode parent,
		final boolean canAddBackupServers,
		final boolean canAddBusinesses,
		final boolean canSeePrices,
		final boolean billParent
	) throws IOException, SQLException {
		connector.requestUpdate(
			true,
			new AOServConnector.UpdateRequest() {
				IntList invalidateList;

				@Override
				public void writeRequest(CompressedDataOutputStream out) throws IOException {
					out.writeCompressedInt(AOServProtocol.CommandID.ADD.ordinal());
					out.writeCompressedInt(SchemaTable.TableID.BUSINESSES.ordinal());
					out.writeUTF(accounting.toString());
					out.writeBoolean(contractNumber!=null);
					if(contractNumber!=null) out.writeUTF(contractNumber);
					out.writeCompressedInt(defaultServer.pkey);
					out.writeUTF(parent.toString());
					out.writeBoolean(canAddBackupServers);
					out.writeBoolean(canAddBusinesses);
					out.writeBoolean(canSeePrices);
					out.writeBoolean(billParent);
				}

				@Override
				public void readResponse(CompressedDataInputStream 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 AccountingCode generateAccountingCode(AccountingCode template) throws IOException, SQLException {
		try {
			return AccountingCode.valueOf(
				connector.requestStringQuery(
					true,
					AOServProtocol.CommandID.GENERATE_ACCOUNTING_CODE,
					template.toString()
				)
			);
		} catch(ValidationException e) {
			throw new IOException(e);
		}
	}

	/**
	 * Gets one Business from the database.
	 */
	@Override
	public Business get(AccountingCode accounting) throws IOException, SQLException {
		return getUniqueRow(Business.COLUMN_ACCOUNTING, accounting);
	}

	List getChildBusinesses(Business business) throws IOException, SQLException {
		AccountingCode accounting=business.pkey;

		List cached=getRows();
		List matches=new ArrayList<>();
		int size=cached.size();
		for(int c=0;c getTopLevelBusinesses() throws IOException, SQLException {
		List cached=getRows();
		List matches=new ArrayList<>();
		int size=cached.size();
		for(int c=0;c
	private final Tree tree = new Tree() {
		@Override
		public List> getRootNodes() throws IOException, SQLException {
			List topLevelBusinesses = getTopLevelBusinesses();
			int size = topLevelBusinesses.size();
			if(size==0) {
				return Collections.emptyList();
			} else if(size==1) {
				Node singleNode = new BusinessTreeNode(topLevelBusinesses.get(0));
				return Collections.singletonList(singleNode);
			} else {
				List> rootNodes = new ArrayList<>(size);
				for(Business topLevelBusiness : topLevelBusinesses) rootNodes.add(new BusinessTreeNode(topLevelBusiness));
				return Collections.unmodifiableList(rootNodes);
			}
		}
	};

	static class BusinessTreeNode implements Node {

		private final Business business;

		BusinessTreeNode(Business business) {
			this.business = business;
		}

		@Override
		public List> getChildren() throws IOException, SQLException {
			// Look for any existing children
			List children = business.getChildBusinesses();
			int size = children.size();
			if(size==0) {
				if(business.canAddBusinesses()) {
					// Can have children but empty
					return Collections.emptyList();
				} else {
					// Not allowed to have children
					return null;
				}
			} else if(size==1) {
				Node singleNode = new BusinessTreeNode(children.get(0));
				return Collections.singletonList(singleNode);
			} else {
				List> childNodes = new ArrayList<>(size);
				for(Business child : children) childNodes.add(new BusinessTreeNode(child));
				return Collections.unmodifiableList(childNodes);
			}
		}

		@Override
		public Business 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 - 2024 Weber Informatics LLC | Privacy Policy