Archives for September 2005

Switch it up

Anyone who has done application development for very long has likely run across the problem of having to work with settings (ie what options are turned on, what options are turned off) and how to store them.

Let's use the following case:

Your application currently has four modules, but may grow to include more. Each module can be independently turned off or on on a user by user basis. User profiles are stored in a database and when a user logs into the system the application creates an instance of a 'user' class to store and access information about the user. Two problems arise. First, if the application module count will increase how can additional switches be added to the user profile without having to extend the user table. Second, how can you easily work with the switch values returned from the database without having to add extra code to the user class.

Enter the binary string .... 11110110101, behold the beauty of 0's and 1's. A string of 0's and 1's, or true's and false's if you may, little switches lined up and just waiting to be used. So how can you store and access this powerful little creature. A number can easily be converted between bases (ie base 2, base 10, base 16). Base 2 is binary (01010101). Base 10 is decimal, how you normally see numbers (0123456789). Base 16 is hexadecimal (ff9900) this will look familiar to anyone who has used html colour values.

The ability to easily change the base of a number means we can retrieve a single number, convert it to base 2 (binary) to get the switch values we need, adjust any of the switch values, and then convert the binary string back to base 10 and store it again as a single numeric value.

Here are some examples of values in decimal and the corresponding binary value (note binary strings always read from right to left, the rightmost value is the least significant digit, decimal 0 or 1.

  • 1 = 1
  • 2 = 10
  • 3 = 11
  • 4 = 100
  • 100 = 1100100
  • 1000 = 1111101000

 

We now have a way to store one value in the database and the ability to turn that value into a set of switches to determine if something is on or off. In the example mentioned above we could represent any combination of the four modules being on or off with an integer value between 0 and 15.

The sample above uses my 'BinarySwitch' class to manage a set switches. By defining a set of switches the class can set/get the value for any switch or set/get the total switch value. Download the source here.

In our sample case adding an extra module would not require any extensions to the database or the user class. In the database could have one column called 'module_settings' in the 'users' table that would store the numeric value of the switches. The user class could contain a property called 'module_settings' that would store the numeric value returned from the database when the user profile is retrieved. In our application code we could use the following snippet to either set or get the user's module settings.

import com.dgrigg.data.BinarySwitch;



var moduleSwitch = new BinarySwitch();

var mods = ['module1', 'module2', 'module3', 'module4'];

moduleSwitch.setSwitches(mods);

moduleSwitch.setValue(userProfile.module_settings);



//check if the user has access to module 2

if (moduleSwitch.getSwitch('module2') == 1){

//user has access

} else {

//alert the user that they do not have access to this module

}

If a new module gets added to the application all we need to do is add the module name in the array that sets up the switches

var mods = ['module1', 'module2', 'module3', 'module4', 'module5'];

moduleSwitch.setSwitches(mods);

To set a switch value in the user's profile and get the total switch value for storage in the database is equally easy.

import com.dgrigg.data.BinarySwitch;



var moduleSwitch = new BinarySwitch();

var mods = ['module1', 'module2', 'module3', 'module4'];

moduleSwitch.setSwitches(mods)

moduleSwitch.setValue(userProfile.module_settings);



//update the users access to module 3

//based on the value of module3 checkbox value

moduleSwitch.setValue('module3', module3_chbx.selected ? 1 : 0);



userProfile.module_settings = moduleSwitch.getValue();

Who knew 0's and 1's could be so useful.

Xml, Scope and the Delegate

One of the problems/complaints I often here about loading XML data into Flash is how to keep the scope straight without having to use a hack. Thankfully Macromedia has provided a solution, the 'mx.utils.Delegate' class.

The Delegate gets mentioned alot when using controls and event listeners, and rightly so, but it also provides a very nice solution to xml scope problem. The great thing about the Delegate is that you can easily control the scope within the called function, which is exactly what you want once the xml has loaded and you begin trying to use the data.

Without the Delegate you can either rely on scope chain the find what you are after (in the example below the 'myText_txt' text field) or you have to hack something in (a property to use for scope, in the example I used '_parent').

Rely on scope chain
function xmlLoaded(success){
	if (success){
		myText_txt.text += myXml.toString();	
	} else {
		myText_text.text = 'error loading xml';
	}
}

myXml = new XML();
myXml.ignoreWhite = true;
myXml.onLoad = this.xmlLoaded;

myXml.load('test.xml');
Rely on hack
function xmlLoaded(success){
	if (success){
		this._parent.myText_txt.text += myXml.toString();	
	} else {
		this._parent.myText_text.text = 'error parsing xml';
	}
}

myXml = new XML();
myXml.ignoreWhite = true;
myXml._parent = this;
myXml.onLoad = xmlLoaded;

myXml.load('menu.xml');
In Delegate we trust
import mx.utils.Delegate; 

function xmlLoaded(success){
	if (success){
		myText_txt.text += myXml.toString();	
	} else {
		myText_text.text = 'error loading xml';
	}
}

myXml = new XML();
myXml.ignoreWhite = true;
myXml.onLoad = Delegate.create(this, xmlLoaded);

myXml.load('test.xml');

The examples look quite similiar, but if you have run across this problem before you will know how much of a difference using the Delegate will make. Hope you find this useful.

Welcome to Derrick's Blog

Who am I?I am the Senior Solution Architect for Innovasium. We specialize in web application development and web site design and hosting. I am responsible for our core application and product development.

What is this site for?I hope to accomplish two things with the site. First and foremost share my thoughts and ideas on:

  • Web Application Development
  • Database Development
  • Tips and tutorials on Flash, Flex, Coldfusion, Flash Remoting

Secondly, use this as a testing ground for new ideas and concepts as I push the development of Innovasium's Content Management System foreward.