Persisting Preferences on the Server

clock May 31, 2010 11:48 by author Flexicious

Flexicious has a robust persistence preference mechanism that "just works" out of the box, but the preferences are stored on the client machine in the form of Shared Objects by default. This enables the preference persistence mechanism to work without any additional coding on the part of the developers utilizing the library. While this may be sufficient for most people, this will not work in scenarios when the same user uses multiple machines to access your application, or if multiple users access the application using the same machine. For this, you will need to store preferences in the backend, and this post aims to show you how this is done. Attached are 2 files that will help you understand both the server and client side of this equation.

This entry will discuss at a high level the code in these two files:

1) preferencesFlex.mxml:  A sample file, that shows the Flex code require to enable server based persistence of preferences:

Please note the declaration of the grid - we wire up 3 events, loadPreferences, persistPreferences, and clearPreferences. Each of these events call a service method, which we will talk about in a bit. But the idea is simple, on persistPreferences, we send up the preferences xml, the name of the grid, and in your service method, you figure out who the logged in user is (on basis of your security implementation) and store a record in the database with UserID, gridName, and preferenceXML (Make this a large text field (nvarchar(max) in SQL Server Lingo), it could get pretty verbose). the loadPreferences will call your service method to load the preferences you persisted, and set the grids preferences property. Once you set the property, the grid will parse everything and get back to the state it was when the preferences were stored. And then the final method is the clearPreferences, which will basically just wipe out the preferences that were previously stored. Sound like a lot, but really pretty straight forward.

    <grids:ExtendedDataGrid id="dgEmployees" enablePreferencePersistence="true"
        preferencePersistenceMode="server"...... loadPreferences="onLoadPreferences()" persistPreferences="onPersistPreferences()"
        clearPreferences="onClearPreferences()">

           //Called when the grid needs preferences (on creation complete)
            private function onLoadPreferences():void{
                callMethod("GetSavedPreferences",{gridName:dgEmployees.preferencePersistenceKey},onLoadPreferenceResult);
            }
            //Callback when the preference data is loaded from the server.
            private function onLoadPreferenceResult(event:ResultEvent):void{
                if(event.result.toString().length>0)
                dgEmployees.preferences=event.result.toString();
            }
            //Called when the user clicks on "save preferences" in the preferences dialog box
            private function onPersistPreferences():void{
                callMethod("PersistPreferences",{gridName:dgEmployees.preferencePersistenceKey,prefXml:dgEmployees.preferences},onPersistPreferencesResult);
            }
            //Callback when the preference data is persisted on the server
            private function onPersistPreferencesResult(response:ResultEvent):void{
                processResult(response);
            }
            //Called when the user clicks on "clear preferences" in the preferences dialog box
            private function onClearPreferences():void{
                callMethod("ClearPreferences",{gridName:dgEmployees.preferencePersistenceKey},onClearPreferencesResult);
            }
            //Callback when the preference data is cleared on the server
            private function onClearPreferencesResult(response:ResultEvent):void{
                processResult(response);
            }
            //here we just call server methods using http services, but you could
            //use remoting or web services, or whatever
            private function callMethod(methodName:String, parameters:Object, callBack:Function):void{...}


2) The Backend Service Code (This is C#, but Java/PHP/Anything would perform similar actions):

We are basically implementing the code to tie the passed in preferences to the IP address of the end user, but in your case, you could tie it to a session specific user ID.


        /// <summary>
        /// Returns previously saved preferences for the provided
        /// Grid Name. This name corresponds to the preferencePersistenceKey
        /// from Flex
        /// </summary>
        /// <param name="gridName">The Grid To get preferences for</param>
        /// <returns>The persisted preference values</returns>
        public ActionResult GetSavedPreferences(string gridName){...}

        /// <summary>
        /// Persists the preferences for the provided grid name
        /// </summary>
        /// <param name="gridName">Grid to persist the preferences for</param>
        /// <param name="prefXml">The preferences to persist</param>
        /// <returns>Success code.</returns>
        [ValidateInput(false)]
        public ActionResult PersistPreferences(string gridName, string prefXml)
       
        /// <summary>
        /// Clears previously saved preferences for the provided
        /// Grid Name. This name corresponds to the preferencePersistenceKey
        /// from Flex
        /// </summary>
        /// <param name="gridName">The Grid To clear preferences for</param>
        /// <returns>The persisted preference values</returns>
        public ActionResult ClearPreferences(string gridName)
           
We hope this explanation and the attached files should get you started in the right direction, but please feel free to reach out to us at the info address if you need any additional help!

preferencesFlex.txt (5.79 kb)

preferencesBackend.txt (3.20 kb)



Converting The Flexicious Filter to SQL

clock May 18, 2010 04:48 by author Flexicious

Question:

Have you got a working example where you connect to coldfusion?

Or where we can use plain SQL?

We currently dont have any examples of RAW sql creation. But we do have examples of generation of Hibernate HQL and .NET Entity SQL. Creation of raw SQL should be very similar. Please get in touch with us and we will be able to send you samples of the Java and .NET backend!