/*
** SellaNMS
** $Id: format_statement_from.cxx,v 1.8 2005/05/21 23:10:28 sella Exp $
** Copyright (c) 2001-2005 Digital Genesis Software, LLC. All Rights Reserved.
** Released under the GPL Version 2 License.
** http://www.digitalgenesis.com
*/

static char rcsid[] = "$Id: format_statement_from.cxx,v 1.8 2005/05/21 23:10:28 sella Exp $";

#include "format_statement_from.h"
#include "common/snmp_util.h"
#include <stdio.h>
#include <stl/vector>
#include <stl/string>

FormatStatementFrom::FormatStatementFrom() {

}

FormatStatementFrom::FormatStatementFrom(From const &src) {

}

FormatStatementFrom::FormatStatementFrom(FormatStatementFrom const &src) {

}

FormatStatementFrom::~FormatStatementFrom() {

}

From const& FormatStatementFrom::operator=(From const &src) {
	/* Typecast src to its real type. */
	FormatStatementFrom *real_src = (FormatStatementFrom*) &src;

	return operator=(*real_src);
}

FormatStatementFrom const& FormatStatementFrom::operator=(FormatStatementFrom const &src) {
	/* Wipe contents. */
	this->clear();

	/* Do object specifics. */
	this->groups = src.groups;
	this->variables = src.variables;

	return *this;
}

/*
	Compile all information in this from clause.

	Parameters
		config:	the section of the configuration from the level "policy-options format-statement <statement-name> term <term-name> from"

	Return Values
		SUCCESS:	if the format-statement from clause was successfully compiled.
		FAILURE: if the format-statement from clause failed to compile.
*/
RESULT FormatStatementFrom::compile(ConfigNode *config) {
	RESULT result = SUCCESS;

	ConfigNode							group_config;
	ConfigNode							variable_config;

	vector<string>						group_names;
	vector<string>						variable_names;

	FormatStatementFromVariable	variable;
	FormatStatementFromGroup		group;

	string								oid;

	if (config->assignValues("group", group_names)) {
		for (vector<string>::iterator name = group_names.begin(); name != group_names.end(); ++name) {
			group.clear();
			group.setName(*name);

			if (config->assignNode("group " + *name, group_config)) {
				if (group.compile(&group_config) == SUCCESS) {
					this->groups.insert(this->groups.end(), group);
				} else {
					result = FAILURE;
				}
			} else {
				result = FAILURE;
			}
		}
	}

	if (config->assignValues("variable", variable_names)) {
		for (vector<string>::iterator name = variable_names.begin(); name != variable_names.end(); ++name) {
			if (config->assignNode("variable " + *name, variable_config)) {
				variable.clear();

				oid = *name;
				resolveMIBObject(oid);
				variable.setName(oid);

				if (variable.compile(&variable_config) == SUCCESS) {
					this->variables.insert(this->variables.end(), variable);
				} else {
					result = FAILURE;
				}
			} else {
				result = FAILURE;
			}
		}
	}
	return result;
}

RESULT FormatStatementFrom::getAppliedMapStatements(slist<string> &map_names) {
	RESULT result = SUCCESS;

	for (slist<FormatStatementFromGroup>::iterator g = this->groups.begin(); g != this->groups.end(); ++g) {
		if (g->getAppliedMapStatements(map_names) != SUCCESS) {
			LMSG("ERROR: failed to get applied map-statements.");

			result = FAILURE;
		}
	}
	for (slist<FormatStatementFromVariable>::iterator v = this->variables.begin(); v != this->variables.end(); ++v) {
		if (v->getAppliedMapStatements(map_names) != SUCCESS) {
			LMSG("ERROR: failed to get applied map-statements.");

			result = FAILURE;
		}
	}
	return result;
}

RESULT FormatStatementFrom::setVariableType(unsigned long &type, string &variable) {
	RESULT result = FAILURE;

	for (slist<FormatStatementFromGroup>::iterator g = this->groups.begin(); g != this->groups.end(); ++g) {
		if (g->setVariableType(type, variable) == SUCCESS) {
			result = SUCCESS;
		}
	}
	for (slist<FormatStatementFromVariable>::iterator v = this->variables.begin(); v != this->variables.end(); ++v) {
		if (v->setVariableType(type, variable) == SUCCESS) {
			result = SUCCESS;
		}
	}
	return result;
}

RESULT FormatStatementFrom::setVariableFunction(unsigned long &function, string &variable) {
	RESULT result = FAILURE;

	for (slist<FormatStatementFromGroup>::iterator g = this->groups.begin(); g != this->groups.end(); ++g) {
		if (g->setVariableFunction(function, variable) == SUCCESS) {
			result = SUCCESS;
		}
	}
	for (slist<FormatStatementFromVariable>::iterator v = this->variables.begin(); v != this->variables.end(); ++v) {
		if (v->setVariableFunction(function, variable) == SUCCESS) {
			result = SUCCESS;
		}
	}
	return result;

}

RESULT FormatStatementFrom::setVariableMap(slist<string> &mapStatements, string &variable) {
	RESULT result = FAILURE;

	for (slist<FormatStatementFromGroup>::iterator g = this->groups.begin(); g != this->groups.end(); ++g) {
		if (g->setVariableMap(mapStatements, variable) == SUCCESS) {
			result = SUCCESS;
		}
	}
	for (slist<FormatStatementFromVariable>::iterator v = this->variables.begin(); v != this->variables.end(); ++v) {
		if (v->setVariableMap(mapStatements, variable) == SUCCESS) {
			result = SUCCESS;
		}
	}
	return result;

}

/*
	Clear all information from this from clause.
	
	Return Values
		SUCCESS:	if the from clause was successfully cleared.
		FAILURE: if the from clause failed to clear.
*/
RESULT FormatStatementFrom::clear() {
	this->groups.clear();
	this->variables.clear();

	return SUCCESS;
}

/*
	Print all information from this from clause.
	
	Return Values
		SUCCESS:	if the from clause was successfully print.
		FAILURE: if the from clause failed to print.
*/
RESULT FormatStatementFrom::print(unsigned long depth) {
	string	tab = "";
	for (unsigned long i = 0; i < depth; i++) {
		tab.append("   ");
	}

	LMSG("%sfrom {", tab.c_str());
	for (slist<FormatStatementFromGroup>::iterator group = this->groups.begin(); group != this->groups.end(); ++group) {
		group->print(depth + 1);
	}
	for (slist<FormatStatementFromVariable>::iterator variable = this->variables.begin(); variable != this->variables.end(); ++variable) {
		variable->print(depth + 1);
	}
	LMSG("%s}", tab.c_str());

	return SUCCESS;
}

/*
** Local Variables:
** c-basic-offset: 3
** tab-width: 3
** End:
** vim600: fdm=marker
** vim: noet ts=3 sw=3 sts=3
*/
