18 Replies Latest reply on Jun 3, 2009 6:05 AM by Richard_Abbott

    4D for Flex

    chandler124

      Hello,

       

      I develop a Flex application which interacts with a 4D database.

       

      Idevelop a 4D method which returns an integer.

       

      I have to recover this integer in a Flex TextInput. How should I proceed ?

       

      After researching for a long time , I've never found a clear and good example.

       

      Thank you

        • 1. Re: 4D for Flex
          chandler124 Level 1

          Can someone help me please ?

           

          I know there's a module in 4D which allows you to do that.

           

          Someone knows this module ?

          • 2. Re: 4D for Flex
            Richard_Abbott Level 3

            Chandler124,

            I have never used 4D, but the basic principles are common across all database types:

            1) use a server-side page written in ASP, PHP, JSP etc to access the database, get back the results in whatever format the database likes, and populate the page with the results in some convenient format (normally XML, but if you only have a single number that seems overkill)

            2) from your Flex app, use HTTPRequest or URLRequest to call the server side page

            3) in the load success handler of the request, parse the received data and populate your interface control.

            Richard

            • 3. Re: 4D for Flex
              chandler124 Level 1

              Hello can i have an Flex example ?

               

              How configure the HTTPService and put the result of the 4D method in an TextInput ?

              • 4. Re: 4D for Flex
                Michael Borbor Level 4

                I'd recommend you to look in the 4D documentation.

                • 5. Re: 4D for Flex
                  Richard_Abbott Level 3

                  Chandler124,

                  as Michael has said, the format of the actual database call will vary depending on the specifics of 4D, but more particularly on the specific server-side language you are using, and the database drivers. So for example if you are using ASP.NET, there are some specific connectors that you would use and configure, same with PHP etc. That's not something that's really relevant for a Flex list.

                  As for the URLRequest part, the regular Flex documentation should be fine here. Create the correct objects and event listeners as per the example at the end of the URLRequest documentation page, and in the handler unravel whatever format you have sent the data across in, and then just do

                  my_text_input.text = string_derived_from_event_result.

                  Richard

                  • 6. Re: 4D for Flex
                    Bart Alcorn

                    Chandler,

                     

                         A lot depends on your set up, so I'm being as generic as possible.

                     

                         I'm going to assume you have a fourDSQLService object already set up and connecting to your 4D database. IF this is not the case, you need to follow the examples that come with the 4D for Flex package to at least get that far.

                     

                         Your call is going to look something like:

                             var sql:String = "SELECT {FN methodname(?, ?) as int} as id FROM tablename";

                         var params:Array = [ <parameter as string>, <second parameter as string> ];

                     

                         var token:AsyncToken = fourDSQLService.execute(sql, params);

                     

                         Where:

                              fourDSQLService is the name of your FourDSQLService object

                              methodname is the name of the method in the 4D database

                              <parameter as string> is the required 4D method parameter, as a string (the number of ?s must match the number of values in the array and be in the same order)

                              id is the name of the returning variable

                              tablename is a valid table name from the 4D database

                     

                         Your fourDSQLService Result Handler will be something along the lines of

                              private function _onResultHandler(event:ResultEvent):void

                         {

                              var _result: SQLResultSet = event.result as SQLResultSet

                           if(_result.length >= 0 )

                                myTextInput.text = String (_result[0].id);

                         }

                     

                    This crude example has no error checking, etc. But it should at least get you started. Use the debugger to inspect the actual resultset and massage your result handler code as appropriate.

                     

                    ~ Bart

                    1 person found this helpful
                    • 7. Re: 4D for Flex
                      chandler124 Level 1

                      Thank you Bart,

                       

                      But my 4D method returns an int. I want to use this int and take it in a TextInput or TextArea.

                       

                      Can you explain me ? please

                       

                      Thank you

                      • 8. Re: 4D for Flex
                        Bart Alcorn Level 1

                        Chandler 124 said:

                         

                             But my 4D method returns an int. I want to use this int and take it in a TextInput or TextArea.

                         

                             Can you explain me ? please

                         

                        The result set in the example will be an int (integer).

                         

                        Simply cast that result as a string when you set the text property of your Text Input/Text Area.

                         

                             myTextInput.text = String(_result[0].id);

                         

                        The result set is an array, therefore the first (and in this case, the only) element ( index 0 (zero) ) contains a property which we earlier named "id".

                         

                        Hope this helps,

                         

                        ~ Bart

                        • 9. Re: 4D for Flex
                          chandler124 Level 1

                          Erf, this is difficult for me. That makes me crazy :s

                           

                          Here is my 4D method :

                           

                           

                          ' Find_Nr_Of_Movies

                          C_ENTIER($ALLMovies)

                          $ALLMovies:=0


                          Debut SQL

                          SELECT COUNT(*)

                          FROM MOVIES

                          INTO<<$ALLMovies>>

                          Fin SQL


                          $0:=$ALLMovies

                           

                           

                          So, I want to take $ALLMOVIES and put it in my Flex application

                           

                          And Here's my AS :

                           

                           

                          // ActionScript File


                          /* ===============================

                          Partie dÈclarative

                          =============================== */


                          import flash.events.Event;


                          import fourD.sql.*;

                          import fourD.sql.events.SQLEvent;

                          import fourD.sql.events.connection.*;

                          import fourD.sql.events.resultSet.*;

                          import fourD.utils.Tracer;


                          import mx.controls.Alert;

                          import mx.rpc.AsyncToken;

                          import mx.rpc.events.FaultEvent;

                          import mx.rpc.events.ResultEvent;


                          [Bindable]

                          private var _resultSet:SQLResultSet; // RÈcupÈration des donnÈes 4D dans un SQLResultSet


                          [Bindable]

                          private var _selectedSocieteIndex:int; // Index d'une ligne de la grille

                          private var _Movies:Object; // Objet Movies


                          [Bindable]

                          private var _ID:uint; // identifiant Movies

                          [Bindable]

                          private var _Title:String;



                          /* ===============================

                          Partie procÈdurale

                          =============================== */


                          // MÈthodes de gestion d'ÈvÈnements


                          /* -------------------------------

                          Nom : connectHandler

                          But : ---

                          ------------------------------- */

                          private function connectHandler(event:Event):void

                          {

                          Tracer.traceInformation("connectHandler");

                          fourDSQLService.execute("SELECT ID, Title, Director, Category FROM MOVIES");

                          }


                          /* -------------------------------

                          Nom : resultHandler

                          But : appel au service SQL retournant un rÈsultat

                          ------------------------------- */

                          private function resultHandler(event:ResultEvent):void

                          {

                          Tracer.traceInformation("resultHandler");

                          if(event.result != null)

                          {

                             

                          _resultSet = event.result as SQLResultSet

                          // //////////////////////////// recordCountLabel.text = String(_resultSet.nbRecords);


                          if (_resultSet.length >= 0)

                          recordCountLabel.text = String(_resultSet[0].id);


                           

                           

                           

                          /* var Recno:int = event.result as int

                          recordCountLabel.text = String(Recno);

                          */  

                          }

                          }


                          /* -------------------------------

                          Nom : faultHandler

                          But : appel au service SQL retournant une erreur

                          ------------------------------- */

                          private function faultHandler(event:FaultEvent):void

                          {

                          Tracer.traceInformation("function faultHandler" );

                           

                          var zeError:String = "Error #" + event.fault.faultCode;

                          Tracer.traceInformation("faultHandler : " + zeError + event.fault.faultString);

                          Alert.show(event.fault.faultString, zeError);

                          }


                          // Autres mÈthodes


                          /* -------------------------------

                          Nom : connect

                          But : connexion 4D

                          ------------------------------- */

                          private function connect():void

                          {

                          fourDSQLService.connect();

                          }


                          /* -------------------------------

                          Nom : disconnect

                          But : dÈconnexion 4D

                          ------------------------------- */

                          private function disconnect():void

                          {

                          fourDSQLService.addEventListener(SQLEvent.DISCONNECT, initApp);

                          fourDSQLService.disconnect();

                          clearMovies()

                          }

                           


                          /* -------------------------------

                          Nom : selectCount

                          But : ordre SQL pour trouver le nombre de records de la table MOVIES

                          ------------------------------- */

                          private function selectCount():void

                          {


                          var sql:String = "SELECT {FN Find_Nr_Of_Movies(?,?) as int} as id FROM MOVIES";

                          var params:Array = [<param1 as String>,<param2 as String>];

                          var token:AsyncToken = fourDSQLService.execute(sql,params);

                          }


                          /* -------------------------------

                          Nom : onResultHandler

                          But : xxx

                          ------------------------------- */

                          /* private function onResultHandler(event:ResultEvent):void

                          {

                          var _result:SQLResultSet = event.result as SQLResultSet

                           

                          if (_result.length >= 0)

                          recordCountLabel.text = String(_result[0].id);

                          } */


                          // fin du script

                           

                          Hope you'll help me by giving me the correct way ...

                           

                          And I don't know how to manage the parameters ....

                           

                          Thank you

                          • 10. Re: 4D for Flex
                            Bart Alcorn Level 1

                            You don't HAVE to call a 4D method, you can use SQL directly, and in this case that might be easier.

                             

                            Change:

                             

                             

                            /* -------------------------------

                            Nom : selectCount

                            But : ordre SQL pour trouver le nombre de records de la table MOVIES

                            ------------------------------- */

                            private function selectCount():void

                            {

                                 // the count of the number of movies will be returned as the variable 'count'

                            var sql:String = "SELECT COUNT(*) as count FROM MOVIES";

                            var token:AsyncToken = fourDSQLService.execute(sql);

                            }

                             

                            and

                             

                             

                            /* -------------------------------

                            Nom : resultHandler

                            But : appel au service SQL retournant un rÈsultat

                            ------------------------------- */

                            private function resultHandler(event:ResultEvent):void

                            {

                            Tracer.traceInformation("resultHandler");

                            if(event.result != null)

                            {

                                 _resultSet = event.result as SQLResultSet

                                  if (_resultSet.length >= 0)

                                 {

                                        recordCountLabel.text = String(_resultSet[0].count);

                                  }

                                  else

                                  {

                                        recordCountLabel.text = 'None found!';

                                  }

                               }

                            }

                             

                            This should work based on the rest of your posted code.

                             

                            ~ Bart

                             

                            Message was edited by: Bart Alcorn, added else statement to result handler.

                            • 11. Re: 4D for Flex
                              chandler124 Level 1

                              Yes, I know I can call by SQL but I'm on a training period and I have to call a 4D method ...

                               

                              How can I proceed ?

                               

                              Thank you

                              • 12. Re: 4D for Flex
                                Richard_Abbott Level 3

                                Bart, Chandler124,

                                Bart wrote"Simply cast that (int) result as a string when you set the text property" - if you're feeling really lazy you can just do

                                myTextInput.text = "" + _result[0].id

                                as Flex then does the cast anyway.

                                Richard

                                • 13. Re: 4D for Flex
                                  Bart Alcorn Level 1

                                  If you must call a 4D method, change the call to:

                                   

                                  /* -------------------------------

                                  Nom : selectCount

                                  But : ordre SQL pour trouver le nombre de records de la table MOVIES

                                  ------------------------------- */

                                  private function selectCount():void

                                  {

                                       // the result of the 4D method Find_Nr_Of_Movies will be returned as the variable 'count'

                                       var sql:String = "SELECT {FN Find_Nr_Of_Movies as int} as count FROM MOVIES";

                                       var token:AsyncToken = fourDSQLService.execute(sql);

                                  }

                                   

                                   

                                  ~ Bart

                                  • 14. Re: 4D for Flex
                                    chandler124 Level 1

                                    Thank you but I still have the same interrogations.

                                     

                                    What to do in the parameters in my cases ?

                                     

                                    And how put the variable $ALLMOVIES in a Flex application ? What Have I to do in a TextArea to have the integer ?

                                     

                                    And What is this token variable, you don't call it ... I don't understand...

                                     

                                    Thank you for helping me in my quest of 4D and Flex

                                    • 15. Re: 4D for Flex
                                      chandler124 Level 1

                                      Someone can help me please ?

                                      • 16. Re: 4D for Flex
                                        Richard_Abbott Level 3

                                        Chandler,

                                         

                                        I think people have given you the information you need. token is the return value of the 4D call, and if you wanted you could inspect it for values, but the answer to the actual remote query comes back in a separate method called resultHandler (check in one of the code examples earlier in the thread). Remember that the call is handled asynchronously - in other words the immediate return value of the call is not in the least related to the answer to the DB query. Your Flex program execution is not held up waiting for the call to finish (which might in some cases take hours) - execution continues, and when at some stage the call returns your handler will run. As for putting the result into the text input, you have had two suggestions how to do that. What exactly is the problem you still feel you need help with?

                                         

                                        Richard

                                        1 person found this helpful
                                        • 17. Re: 4D for Flex
                                          chandler124 Level 1

                                          Thank you both for these explanations

                                           

                                          Actually, I don't understand where do I have to call Token. Because Token is just a variable which is not called in Bart's functions. I just need this information... I suppose that it is called in ResuldHandler but where and when ?

                                           

                                          Sorry, you should have mentionned it but I'm french and I don't understand the "token part" ...

                                           

                                          Thank you

                                          • 18. Re: 4D for Flex
                                            Richard_Abbott Level 3

                                            Hi Chandler,

                                            you wrote "Sorry... I'm french and I don't understand the "token part"" - no problem, nice to be conversing with a fellow European!

                                            In his code snippet, Bart included the line

                                            var token:AsyncToken = fourDSQLService.execute(sql);

                                             

                                            Now, the really important part of that is the right-hand side, plus of course the fact that you have attached the event handler to this. Looking back through the code snippets in previous posts, I am not sure this has been explicitly said, but at some opint you need to have a line like

                                            fourDSQLService.addEventListener(event_name, result_handler);

                                            where event_name is whatever 4DS calls the event - for URLRequest it would be Event.COMPLETE and at a guess it will be the same, but it may not be and you should check the documentation for this.

                                             

                                            Now, back to token. As I said, the important thing is that you are calling the asynchronous execute method - in the code, the method returns token so that in principle you could track progress by, say, occasionally polling a request. But, in this case token is defined only locally and so is not accessible inside other methods. To use it properly token would have to be declared as a class variable, initialised by the above call and then used somewhere else. But that is not crucial to the flow of your example... the crucial bit is the event listener link between fourDSQLService and result_handler.

                                             

                                            Hope that helps,

                                            Richard