Grey Line

It started as a simple task – I needed to store user preferences.  No problem SharedObject.getLocal() will do the job. Oh, by the way, we do not want to store the preferences on the local disk because users want to be able to switch workstations. SharedObject.getRemote()? No good. Who’s going to let you store the data on the server’s machine? And even if they will, how are you going to organize multi-user complex preferences?

Using DBMS is a no-brainer.  I’ll create a two-column table – userID and something with properties. What something? An object as a blob. What type of object? Hmm. How about ByteArray?  Rolling up the sleeves.

Before we proceed, let me remind you I spend many years doing  pure object-oriented programming in Java. You’ll see why it’s important in a little bit.

There is a lots of various user preferences to remember. For example, which is a default tab in TabNavigator, which columns should be visible in a datagrid and more.  Here’s what goues through my mind, “For simple preferences I’ll just use Strings and Numbers, and more complex ones I’ll put in a collection with key-value pairs. Then, I’ll create the object UserPrefs, add all the preferences to it, convert it into a ByteArray and will store it either in the SharedObject.data or send it over to a POJO for storage in the DBMS”.  Sounds good? Boy, I was wrong…

It started with the fact that I decided to use ActionScript 3 Dictionary class.  When I opened the language reference, I was surprised by several things:

a) The class Dictionary is inherited right from the Object and has only a couple of extra methods there. To iterate the dictionary, I was looking for a property length or something to know how many elements are there in my collection. Nope, there is not such thing there. Now it’s getting clearer how Flash Player’s VM is so light comparing to JVM.  No big deal. Will loop through the collection while it has some elements.

b) The fun began when I started adding my collections to the UserPrefs object trying  serialize them into the ByteArray for storing in the SharedObject.  When the user will start my application, I’ll de-serialize it using ByteArray.readObject(). Oopsie…Class deserialization is not supported in ActionScript 3. Need to recreate collections manually. But what if I’ll have collections inside collections?

Anyway, finally I got it to work…while debugging in Flash Player. The regular run-time kept giving me some errors…

My colleague  Anatole suggested, “Forget about Dictionary. In this case you can live with a simple Object, but use dynamic typing, and Flash will do deep copying”.  I followed his advice, the program became simpler, I got rid of my UserPref class and it works like a charm.  The following sample creates some hardcoded preferences for illustration purposes, converts them to byte array, saves them, and the on a button click retrieves and deserializes them. Conversion to byte array and back is done just to simplify communication with the Java back end, if any. Here’s the code:

<?xml version=”1.0″ encoding=”utf-8″?>
<mx:Application xmlns:mx=”http://www.adobe.com/2006/mxml” layout=”absolute”>
 <mx:Button x=”173″ y=”16″ label=”Save Hardcoded Preferences” width=”185″ click=”saveData()”/>
 <mx:Button x=”203″ y=”46″ label=”Read Preferences”  click=”getSavedData()”/>
 <mx:TextArea x=”86″ y=”76″ width=”356″ id=”txtOutput” height=”156″/>
 <mx:Script>
  <![CDATA[
        private var prefs:SharedObject;
        private const COOKIE_SIZE:int=10000;
        private function getSavedData():void{

          prefs = SharedObject.getLocal("UserPrefs5");
                var myBA:ByteArray = prefs.data.justTheBytes;
             var recoveredUPrefs:Object=myBA.readObject();

                var defaultTab:String=recoveredUPrefs["defaultTab"];
             txtOutput.text+= “default tab=”+defaultTab + “\n”;

                var visibleColumns:Object=recoveredUPrefs["visibleColumns"];
                 for (var elem:String in visibleColumns){
                   txtOutput.text+= elem+”=”+visibleColumns[elem]+ “\n”;
                 }
           }
               private function saveData():void{
                prefs = SharedObject.getLocal(“UserPrefs5″);

                var uPrefs:Object= new Object();
                var visibleCol:Object = new Object();
    //which col’s to make visible
    visibleCol["custName"]=”true”;
    visibleCol["custAddress"]=”false”;
    visibleCol["customerAge"]=”true”;
                uPrefs["visibleColumns"]=visibleCol;
                // which tab in TabNavigator has to be opened first
                uPrefs["defaultTab"]=”CanceledOrders”;
                // turning the object with prefs into a set of bytes
                var baPrefs:ByteArray = new ByteArray();
                baPrefs.writeObject(uPrefs);
                baPrefs.position = 0;

                // Saving the byte array on disk in a SharedObject
                prefs.data.justTheBytes=baPrefs;
                var flushStatus:String = null;   

                try {
   prefs.flush(COOKIE_SIZE);
                } catch (error:Error) {
                    trace(“Error…Could not write SharedObject to disk”+ error.message);
                }
           }     ]]>
 </mx:Script>
</mx:Application>

Press the button Save, and it’ll save the hardcoded preferences in a local shared object – a String called defaultTab and a collection of flags for visible columns.  Before saving, it serializes the Object (yes a simple Object with dynamically added properties) into an array of bytes and stores it in a local shared object.   If you want, you can pass this array to the backend for storage in the database.

Press the Read button, and the properties will be resurrected from the disk and added to the text area on the screen. Simple, elegant, and flexible.  Not exactly object-oriented. Sorry.

Yakov

4 Comments

  1. Valery Silaev said,

    December 22, 2006 @ 7:00 am

    You say “not exactly object-oriented”?

    Hehe…

    The first suggestion from Java camp will be not about OOP, but rather about using XML as preferences format :)
    Sure, shortly they suggest you to roll your own ActionScript JAXB atop of this ;)

    VS

  2. Anatole Tartakovsky said,

    December 23, 2006 @ 11:09 am

    Valera,
    Interesting comment. I think Yakov’s point was to show the difference in thinking in scripting languages: (1-liners approach) versus strong-typing-compiler-knows-everything one. Syntax “compatibility” of ActionScript and Java cause people to use the same code and techniques. Two points, really:
    1 not to use classes as Object is quite flexible and should not be treated as sunctional compatibility.
    2. serialization / deserialization in Flex are way different from Java.

    XML deep copy serializer would make sense for some applications requiring both sides persistance and accessibility. However, I think for the most cases we would advocate rpc calls for simplicity

    Thank you
    Anatole

  3. Valery Silaev said,

    December 24, 2006 @ 8:20 am

    Anatole,

    I believe that I understand Yakov’s point correctly. The only thing I’d like to emphasize is that Yakov grants too much to “open-mind thinking” of Java crowd (and I’m part of this crowd). EJB + XML descriptors, Spring + XML configurations, Hibernate + XML mappings… JavaScript developers at least know about JSON alternative, Ruby developers consider YAML, Java developers stick with XML for all cases (configuration, preferences, transport formats).

    So 90% of Java Joe-s never get to the static vs dynamic aproaches comparision while they take different direction on previous step — XML as the only possible serialization format. They will advocate that this is a “standard” solution and, ironically, they will be right — they refer to standard set by Sun+IBM+BEA+…numerous OSS projects.

    VS

  4. Yakov said,

    December 24, 2006 @ 9:03 am

    While Java developers do not consider XML as the only serialization format, XML is being abused in J2EE world as the only way of configuring multiple frameworks to make them work together. I’ve been writing on the subj on multiple ocasions, and this is the most recent article: http://java.sys-con.com/read/299903.htm

RSS feed for comments on this post

canada online pharmacy propecia
free cialis
buy cialis without rx
cheap cialis
viagra quick delivery
levitra overnight shipping
best price cialis without perscription
hydrochlorothiazide cialis
50mg viagra
cialis discount prices
best price cialis
canada levitra
online viagra levitra cialis
cialis pills for sale
indian cialis
cialis free samples
when will viagra be generic?
buy cheap viagra online uk
order generic viagra canada
where can i get cialis
generic viagra online pharmacy
cheap online propecia
propecia from canada
best price for generic viagra
where buy viagra
cialis 5 mg
viagra off internet
alaska viagra doctors
buy viagra online without a prescription
viagra onlines
propecia sales canadian
cialis ottawa pharmacy
cheapest viagra online
online ordering propecia
buy cheap uk viagra
overnight viagra
viagra online buy
buy propecia 5mg
online order viagra overnight delivery
how to buy viagra in canada
propecia discount
cialis daily canada
canada propecia prescription
vardenafil:
cialis next day delivery
order viagra in canada
cialis free delivery
uk cialis sales
buying generic propecia
canada viagra
levitra without prescription
generic viagra australia
cheap cialis from india
buy generic cialis online
online presription for viagra
best viagra and popular in uk

real viagra without prescription
levitra online without prescription
liquid cialis for sale
buy generic cialis online from canada
viagra express delivery
soft gel viagra
buy cialis pill
canadian generic cialis
tablet viagra
100 mg cialis
buy cheapest cialis
propecia for sale online
overnight propecia
viagra price
get cialis online
buy cheap generic cialis
viagra replacement
viagra online in canada
order cialis from canada
prescription needed for cialis
menu:
50mg viagra
viagra for sale online in the uk
cialis canada on line
viagra mail order usa
cialis viagra
viagra 100 mg
cialis for less 20 mg
levitra viagra online
find cheap viagra online
online viagra au
online generic cialis 100 mg
online pharmacy propecia
pfizer viagra no prescription
buy propecia online cheap pharmacy
to buy viagra online
propecia
viagra doses
i want free viagra
buy real viagra online no prescription
viagra for woman
brand viagra professional
sildenafil viagra
cialis now
viagra for less
lowest price for viagra from canada
where to buy cialis cheap
viagra online in canada
canadian healthcare cialis
cialis kanada
cheapest levitra uk
cheapest viagra in uk
no prescription viagra canada
canadian pharmacy cialis no rx
cheapest prices on propecia
viagra samples
cheap discount cialis
viagra tablet weight
low price viagra
viagra canadian pharmacy support
cialis us drug stores
generic cialis next day delivery
viagra canada cheapest
buy propecia online
generic propecia canada
find cialis no prescription required
propecia cialis viagra
cialis strenght mg
lowest-price propecia costs us
discount cialis levitra viagra
buy viagra for women
real cialis online
drugstore best buy generic cialisbuy generic cialis
generic viagra canadian pharmacy
cialis testimonial
propecia generic
viagra how much
buy propecia no prescription
find viagra without prescription
buy generic cialis online
no rx viagra
cialis free delivery
viagra from uk
cheap canadian viagra
buy generic cialis
cheap cialis pills
levitra 10 mg without prescriptions
viagra pfizer canada
cheap viagra for sale
cheap cialis online canada
viagra echeck
cheap cialis online no prescription
propecia orders
cialis without prescription in canada
low cost canadian viagra
viagra/cialis sales
buy cialis without prescription
generic propecia for sale
cheap viagra canada
can i buy viagra in canada
nizagara viagra online
viagra mail order
www.cialis.com
viagra sales online
viagra in china
canadian pharmacy viagra cheap
ordering viagra online
cialis canadian cost
buy viagra online without prescription
viagra with no prescription in britain
find viagra no prescription required
cialis daily canada
cheap brand name cialis
vardenafil generic
buy propecia without a prescription
the best price of viagra
try cialis for free
order cialis in canada
usa cialis women
generic cialis sales
viagra sale
sale of viagra tablets
womens viagra no prescription
di scount 50 mg viagra
buy cialis online australia
cialis online australia
real viagra without a prescription
buy cheapest viagra online
cialis canadian online pharmacy
sale viagra
alternative for viagra
prescription for propecia
buying cheap cialis
viagra order
viagra com
purchase cialis next day delivery
online viagra levitra cialis
professional cialis online
combine cialis and levitra
prices for propecia
real viagra to buy
sildenafil
levitra for sale
cheap order prescription propecia