rbrindley: branch rbrindley/astman_revamp r4544 - /team/rbrindley/astman_reva...

SVN commits to the Asterisk-GUI project asterisk-gui-commits at lists.digium.com
Mon Mar 2 18:22:17 CST 2009


Author: rbrindley
Date: Mon Mar  2 18:22:14 2009
New Revision: 4544

URL: http://svn.digium.com/svn-view/asterisk-gui?view=rev&rev=4544
Log:

- copied and enhanced ASTGUI.ASTDB.* funcs to astman.astdb.*
- enhancements include:
	- try/catch wrappers for parseCLIResponse call
	- throwing Errors unsuccessful CLI output
	- if statements verifying that variables used in parseCLIResponse are in fact not null
	- more intutive function names (deleteKey is now just delete)
	- ASTGUI.globals.GUI_DB is now astman.astdb.defaults.dbname


Modified:
    team/rbrindley/astman_revamp/config/js/astman2.js

Modified: team/rbrindley/astman_revamp/config/js/astman2.js
URL: http://svn.digium.com/svn-view/asterisk-gui/team/rbrindley/astman_revamp/config/js/astman2.js?view=diff&rev=4544&r1=4543&r2=4544
==============================================================================
--- team/rbrindley/astman_revamp/config/js/astman2.js (original)
+++ team/rbrindley/astman_revamp/config/js/astman2.js Mon Mar  2 18:22:14 2009
@@ -32,34 +32,132 @@
  */
 astman.astdb = {
 	/**
+	 * Object contain default values.
+	 */
+	defaults: {
+		dbname: "astgui"
+	},
+
+	/**
 	 * Delete by key.
-	 * @param k a db object containing: dbname, keyname, and keyvalue
+	 * @param k a db object containing: key[, dbname]
+	 * @return true or false
 	 */
 	delete: function(k) {
+		if (!k.hasOwnProperty('dbname')) {
+			k.dbname = this.defaults.dbname;
+		}
 
+		try {
+			var s = astman.cliCommand('database del' + k.dbname + ' ' + k.key);
+			if (!s.contains('entry removed')) {
+				throw new Error(s);
+			}
+		} catch (err) {
+			log.error(err.message);
+			return false;
+		}
+		return true;
 	},
 
 	/**
 	 * Get by key.
-	 * @param k a db object containing: dbname, keyname, and keyvalue
+	 * @param k a db object containing: key[, dbname]
+	 * @return key value
 	 */
 	get: function(k) {
+		if (!k.hasOwnProperty('dbname')) {
+			k.dbname = this.defaults.dbname;
+		}
 
+		try {
+			if (!k.hasOwnProperty('key')) {
+				log.error('k.key is null, cannot complete astdb.update request');
+				return false;	
+			}
+
+			var s = astman.cliCommand('database get ' + k.dbname + ' ' + k.key);
+			if (!s.contains('Value: ')) {
+				throw new Error(astman.parseCLIResponse(s));
+			}
+		} catch (err) {
+			log.error(err.message);
+			return null;
+		}
+
+		var val = astman.parseCLIResponse(s);
+		val.trim().withOut('Value: ');
+		return val.trim();
 	},
 
 	/**
 	 * Get all.
-	 * @param k a db object containing: dbname, keyname, and keyvalue
+	 * @param k a db object containing: [dbname]
+	 * @return object with all the key-values
 	 */
 	getAll: function(k) {
+		if (!k.hasOwnProperty('dbname')) {
+			k.dbname = this.defaults.dbname;
+		}
 
+		var db_keys = {};
+		var dbpath = '/' + k.dbname + '/';
+		
+		try {
+			var s = astman.cliCommand('database show ' + k.dbname);
+			if (!s.contains(dbpath)) {
+				throw new Error(s);
+			}
+		
+			var op = astman.parseCLIResponse(s);
+			if (op.trim() === '') {
+				return null;
+			}
+
+			var lines = op.split('\n');
+			lines.each( function(line) {
+				line = line.withOut(dbpath);
+				var key = line.beforeChar(':').trim();
+				var val = line.afterChar(':').trim();
+				if (key !== '') {
+					db_keys[key] = val;
+				}
+			});
+		} catch (err) {
+			log.error(err.message);
+			return null;
+		}
+
+		return db_keys;
 	},
 
 	/**
 	 * Update by key.
-	 * @param k a db object containing: dbname, keyname, and keyvalue
+	 * @param k a db object containing: key, keyvalue[, dbname]
+	 * @return true or false
 	 */
 	update: function (k) {
+		if (!k.hasOwnProperty('dbname')) {
+			k.dbname = this.defaults.dbname;
+		}
 
+		try {
+			if (!k.hasOwnProperty('key')) {
+				log.error('k.key is null, cannot complete astdb.update request');
+				return false;	
+			} else if (!k.hasOwnProperty('keyvalue')) {
+				log.error('k.keyvalue is null, cannot complete astdb.update request');
+				return false;
+			}
+		
+			var s = astman.cliCommand('database put ' + k.dbname + ' ' + k.key + ' ' + k.keyvalue);
+			if(!s.contains('successfully')) {
+				throw new Error(s);
+			}
+		} catch (err) {
+			log.error(s);
+			return false;
+		}
+		return true;
 	}
 };




More information about the asterisk-gui-commits mailing list