/* ${__APPLICATION_VERSION__} */

/* All messages must clone this object.
 *
 * Message are very small objects that look like the following.
 * =================
 * eds.webapp.your.namespace.MessageName = function() {
 *     var fields = ['field1', 'field2' ];
 *     var topic = "some unique string like eds.webapp.your.namespace.MessageName";
 *     eds.webapp.message.Message.call(this, topic, fields);
 * }
 * eds.webapp.your.namespace.MessageName.prototype =
 *     new eds.webapp.message.Message('tmp', null);
 * =================
 *
 * Setting the topic to the string object of the message object name
 * is a good idea.
 *
 * This base object will create getters and setters for all fields.
 * The getters and setters will be Java style. For example,
 *     field1      => getField1(), setField1()
 *     otherField1 => getOtherField1(), setOtherField1()
 *     onetwo      => getOnetwo(), setOnetwo()
 *
 * We capitalize the first char.
 *
 * You should make every effort to keep these message objects small.
 *
 */

eds.webapp.message.Message = function(topic, fields) {

	if ( arguments.length < 2 ) {
		throw "not enough arguments";
	}

	if ( !topic ) {
		throw "we must have a topic";
	}

	/* create getters and setters for all our fields */
	for ( var index in fields ) {
		var field = fields[index];
		var methodField = field;
		var getter;
		var setter;

		methodField = field.charAt(0).toUpperCase() +
			field.substring(1, field.length);

		getter = 'get' + methodField;
		setter = 'set' + methodField;

		eval("this['" + field + "'] = null;");

		eval("this['" + getter + "'] = function() { return this['" + field + "']; }");

		eval("this['" + setter + "'] = function(value) { this['" + field + "'] = value; }");
	}

	this.getTopic = function() {
		return '[ ' + topic + ' ]';
	}

	this.toString = function() {
		var str  = '[ ' + topic + ' { ';
		for ( var index in fields ) {
			var field = fields[index];
			str += field + ': ';
			str += this[field] + ', ';
		}
		str += ' } ]';

		return str;
	}

}

