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

com.aoindustries.aoserv.client.EmailPipeAddressTable 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.io.TerminalWriter;
import java.io.IOException;
import java.io.Reader;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @see  EmailPipeAddress
 *
 * @author  AO Industries, Inc.
 */
final public class EmailPipeAddressTable extends CachedTableIntegerKey {

	EmailPipeAddressTable(AOServConnector connector) {
		super(connector, EmailPipeAddress.class);
	}

	private static final OrderBy[] defaultOrderBy = {
		new OrderBy(EmailPipeAddress.COLUMN_EMAIL_ADDRESS_name+'.'+EmailAddress.COLUMN_DOMAIN_name+'.'+EmailDomain.COLUMN_DOMAIN_name, ASCENDING),
		new OrderBy(EmailPipeAddress.COLUMN_EMAIL_ADDRESS_name+'.'+EmailAddress.COLUMN_DOMAIN_name+'.'+EmailDomain.COLUMN_AO_SERVER_name+'.'+AOServer.COLUMN_HOSTNAME_name, ASCENDING),
		new OrderBy(EmailPipeAddress.COLUMN_EMAIL_ADDRESS_name+'.'+EmailAddress.COLUMN_ADDRESS_name, ASCENDING),
		new OrderBy(EmailPipeAddress.COLUMN_EMAIL_PIPE_name+'.'+EmailPipe.COLUMN_PATH_name, ASCENDING)
	};
	@Override
	OrderBy[] getDefaultOrderBy() {
		return defaultOrderBy;
	}

	int addEmailPipeAddress(EmailAddress emailAddressObject, EmailPipe emailPipeObject) throws IOException, SQLException {
		return connector.requestIntQueryIL(
			true,
			AOServProtocol.CommandID.ADD,
			SchemaTable.TableID.EMAIL_PIPE_ADDRESSES,
			emailAddressObject.pkey,
			emailPipeObject.pkey
		);
	}

	@Override
	public EmailPipeAddress get(int pkey) throws IOException, SQLException {
		return getUniqueRow(EmailPipeAddress.COLUMN_PKEY, pkey);
	}

	List getEmailPipes(EmailAddress address) throws IOException, SQLException {
		int pkey=address.pkey;
		List cached=getRows();
		int len = cached.size();
		List matches=new ArrayList<>(len);
		for (int c = 0; c < len; c++) {
			EmailPipeAddress pipe=cached.get(c);
			if (pipe.email_address==pkey) {
				// The pipe might be filtered
				EmailPipe ep=pipe.getEmailPipe();
				if(ep!=null) matches.add(pipe.getEmailPipe());
			}
		}
		return matches;
	}

	List getEmailPipeAddresses(EmailAddress address) throws IOException, SQLException {
		return getIndexedRows(EmailPipeAddress.COLUMN_EMAIL_ADDRESS, address.pkey);
	}

	List getEnabledEmailPipeAddresses(EmailAddress address) throws IOException, SQLException {
		// Use the index first
		List cached = getEmailPipeAddresses(address);
		int len = cached.size();
		List matches=new ArrayList<>(len);
		for (int c = 0; c < len; c++) {
			EmailPipeAddress pipe=cached.get(c);
			if(pipe.getEmailPipe().disable_log==-1) matches.add(pipe);
		}
		return matches;
	}

	EmailPipeAddress getEmailPipeAddress(EmailAddress address, EmailPipe pipe) throws IOException, SQLException {
		int pkey=address.pkey;
		int pipePKey=pipe.pkey;
		List cached = getRows();
		int len = cached.size();
		for (int c = 0; c < len; c++) {
			EmailPipeAddress epa = cached.get(c);
			if (epa.email_address==pkey && epa.email_pipe==pipePKey) return epa;
		}
		return null;
	}

	List getEmailPipeAddresses(AOServer ao) throws IOException, SQLException {
		int aoPKey=ao.pkey;
		List cached = getRows();
		int len = cached.size();
		List matches=new ArrayList<>(len);
		for (int c = 0; c < len; c++) {
			EmailPipeAddress pipe = cached.get(c);
			if(pipe.getEmailAddress().getDomain().ao_server==aoPKey) matches.add(pipe);
		}
		return matches;
	}

	@Override
	public SchemaTable.TableID getTableID() {
		return SchemaTable.TableID.EMAIL_PIPE_ADDRESSES;
	}

	@Override
	boolean handleCommand(String[] args, Reader in, TerminalWriter out, TerminalWriter err, boolean isInteractive) throws IllegalArgumentException, IOException, SQLException {
		String command=args[0];
		if(command.equalsIgnoreCase(AOSHCommand.ADD_EMAIL_PIPE_ADDRESS)) {
			if(AOSH.checkMinParamCount(AOSHCommand.ADD_EMAIL_PIPE_ADDRESS, args, 2, err)) {
				if((args.length&1)==0) {
					err.println("aosh: "+AOSHCommand.ADD_EMAIL_PIPE_ADDRESS+": must have even number of parameters");
					err.flush();
				} else {
					for(int c=1;c




© 2015 - 2025 Weber Informatics LLC | Privacy Policy