Topic
IC4NOTICE: developerWorks Community will be offline May 29-30, 2015 while we upgrade to the latest version of IBM Connections. For more information, read our upgrade FAQ.
14 replies Latest Post - ‏2014-05-20T03:30:51Z by jane313
F7QD_Nicolas_Echavarria
59 Posts
ACCEPTED ANSWER

Pinned topic How to write from dojo to a service variable

‏2012-12-28T04:19:45Z |
Hello forum and Neil!!

We have been working in the implementation of a mobile BPM appplication using IBM Worklight with Dojo.
At this point we are capturing the patient's barcode after implementing the barcodereader plugin from the Apache Cordova project in the Worklight "shell".
We are in fact even able to write the result in the asynchronous message from Worklight back to BPM.

This is the function we are using in the Worklight App:

[code]
function captureBarCode(source) {
console.log("Reading barcode on app");

window.plugins.barcodeScanner.scan(function(result) {

console.log("We got a barcode\n" + "Result: " + result.text
+ "\n" + "Format: " + result.format + "\n"
+ "Cancelled: " + result.cancelled);

var status = {
"message" : "window.plugins.barcodeScanner.scan",
"status" : "success",
"result" : result
};

console.log(status);
source.postMessage(status, "*");
}, function(error) {
console.log("Scanning failed: " + error);

var status = {
"message" : "navigator.geolocation.getCurrentPosition",
"status" : "fail",
"result" : {text: "Undefined", type: "Undefined", canceled: 0}
};

source.postMessage(status, "*");
});
}
[/code]

So we send the response back to BPM with source.postMessage(status, "*");

Then, BPM receives the message from Worklight in a dojo load event handler as:
[code]
var receiveMessage = function(event) {
var reply = event.data;
var node = dom.byId("result");
node.innerHTML = reply.result.text + " / " + reply.result.format ;
[/code]

At this point while the string is in BPM we have not been able to write it to a coach view variable that gets pushed to the Human service input variable that in turn should be mapped to a BPD input variable in order to move forward and use that variable to query a webservice for the patient's info.

So the question is:
How could we write that result back to a BPD variable?
We've read all the portions of Neil's your latest issue (Pages 244,245) and we are unable to really grasp the complete picture.
Thanks so much for any light that you could shed on this issue!!

And... Happy, prosperous and healthy 2013!!

Nicolas E.
Updated on 2013-01-12T00:42:01Z at 2013-01-12T00:42:01Z by F7QD_Nicolas_Echavarria
  • kolban
    kolban
    3315 Posts
    ACCEPTED ANSWER

    Re: How to write from dojo to a service variable

    ‏2012-12-28T15:14:05Z  in response to F7QD_Nicolas_Echavarria
    Hi Nicolas,
    Pages 244,245 seem to be about environment variables???

    Unfortunately I have no skills in worklight. Can you elaborate on the interaction you are trying to achieve?

    One can use REST APIs to access and change the variables in a running Human Service ...

    If you can clarify the REST API you are attempting to use (I think that is what you might be doing) then I'll certainly try and assist.

    Neil
    • F7QD_Nicolas_Echavarria
      59 Posts
      ACCEPTED ANSWER

      Re: How to write from dojo to a service variable

      ‏2012-12-28T16:02:33Z  in response to kolban
      Hi Neil!

      Sorry for my lack of clarity.
      The REST interactions, we've managed to do them using the Worklight adapters, and we're ok now. We have the barcode variable in javascript in the coach view.

      Basically, if we've declared a variable (that includes a barcode numer) in Javascript in a coach view:
      1.) Where should we declare it? (Inline Javascript, event handlers, etc), we currently have it in the load handler.
      2.) How are we able to pass that variable to a view variable?
      3.) Once we have that variable as a coach variable, how do we assign it to a human service variable?

      I think that more clear... let me know if anything needs further clarification,

      Thanks so much!

      Nicolas E.
      • kolban
        kolban
        3315 Posts
        ACCEPTED ANSWER

        Re: How to write from dojo to a service variable

        ‏2012-12-28T16:57:34Z  in response to F7QD_Nicolas_Echavarria
        Hi Nicolas,
        Don't be sorry about the previous posts. When I am deep in debugging mode I am so steeped in the issue at hand that I make assumptions on notions when discussing with others. When I look for help on the forums, I usually write a detailed description of what I am doing, why I am doing it and what I have tried. Many, many times the simple act of doing that has solved my own problem as I am now imagining describing the issue to someone else. When I am "conceptually challenged" I write my specific questions and then write my reasoning for asking those questions.... eg:
        How do I use "XYZ" feature?
        Background: I want to achieve "ABC" and it seem that using "XYZ" is the right way to go.

        Think of a Coach View as being a "stateful" building block. An instance of a Coach View has a lifecycle that is called at various points such as when "loaded" and when "viewed". The Coach View runs in the browser and has no (immediate) access to the server runtime resources. In order for it to do something useful, we define "variables" on the Coach View which are then mapped to runtime variables. When the Coach in the Human Service is reached, Coach Views contained in the Coach are "instantiated" in the browser. Copies of the variables mapped in the runtime are then "passed" as to the Coach View.

        Pause here ... so now we understand that the Coach View has a copy of the variable!!

        When the Coach "synchronizes" again with the runtime via a boundary trigger, any variables sent to the Coach Views are read back from the Coach Views and overwrite the variable values in the runtime.

        A Coach View provides two separate places where variables can be associated. One is the "primary" binding and the other is the configuration options. Both appear to be functionally identical and the only difference I can see is how they are "intended" to be used and how they appear in the Process Designer environment.

        A Coach View can be used to present data to the user, solicit data from the user or the combination of the two (show data and then have the user change it).

        In your story, what is the "functional goal"? What is it you want your new Coach View to "do"? Is the Coach View display only? Input only? Or both? What are the "data types" of the data you want to pass between the Coach and the Coach View?

        Neil
        • F7QD_Nicolas_Echavarria
          59 Posts
          ACCEPTED ANSWER

          Re: How to write from dojo to a service variable

          ‏2012-12-28T18:11:40Z  in response to kolban
          Hello Neil!

          Thanks so much for your help!
          The following steps are shown graphically in the attached screenshots ZIP.
          Goal: We are trying to obtain the demographic details of a patient using the barcode printed in the wristband of a patient.

          Steps of the story:
          1.) The user logs in the mobile app. DONE.
          2.) The user fires up the required process. DONE.
          3.) The user chooses to "pasively identify the patient" (scans the barcode in the patients wristband). DONE. (This step invokes the Cordova plugin).
          4.) The user scans the barcode. DONE.
          5.) The user observes the number scanned from the barcode in the coach view. DONE.
          6.) The system looks up the patients demographic data using the scanned id via a SOAP or REST webservice integration service. PENDING
          7.) The user "actively verifies the patient ID" using the information on the screen. PENDING

          In order to fire up the webservice call we need to have the barcode ID in a BPD variable, and we have not been able to "write" the barcode ID to a BPD variable. So far via dojo we have the variable available in Javascript but we need that value in a variable of the BPD instance.

          Once we have that variable available to the BPD we could be able to structure the webservice call.

          Let me know any other questions that could help you guide us...
          Thanks so much!!

          Nicolas E.
          • kolban
            kolban
            3315 Posts
            ACCEPTED ANSWER

            Re: How to write from dojo to a service variable

            ‏2012-12-28T18:31:41Z  in response to F7QD_Nicolas_Echavarria
            I am looking at image "step05" in your ZIP. I see what appears to be a Coach running in a phone (nice) and I also see a code number to the right of the button. This means that the code number is already in the browser. How is this number being displayed? Is this a Coach View and if so ... is it a custom coach view that you have written? If not a Coach View, how are you display that number to the user?

            It feels like we have 3 parts to this story and not 2. It seems that we have

            o BPM runtime
            o Coach technology running in the browser
            o 3rd party technology to produce data from a barcode scan

            If the story were just the BPM runtime and the Coach, I think I would be understanding better, but the 3rd party technology is leaving me cold.

            At a logical level, I could see a "barcode" Coach View owning the "value" of the barcode. This would then be mapped to a bar code value in the runtime BPM in Process Designer when the "barcode" Coach View were added to the Coach. I would then imagine that the "barcode" Coach View would provide:

            o A button (or other mechanism) to initiate a bar code capture
            o A visualization of the captured bar code value
            o A variable (its state) representing the captured bar code (this is what is mapped as a variable)

            I would then imagine that when the "barcode" Coach View button is pressed then it "does what it needs to do" to scan the bar code and get the data. I am currently thinking of this as black box stuff. Once the value has been obtained from the scan, then the value is set to the Coach View variable (I am assuming you know how to do this at Coach View programming level ....). When the Coach triggers a boundary event, then the value of the "barcode" variable is then copies and saved in the mapped BPM runtime variable.

            Neil
            • F7QD_Nicolas_Echavarria
              59 Posts
              ACCEPTED ANSWER

              Re: How to write from dojo to a service variable

              ‏2012-12-28T21:05:26Z  in response to kolban
              Hi Neil,

              You got everything right!
              Except you went too quick in this part ;-) "When the Coach triggers a boundary event, then the value of the "barcode" variable is then copied and saved in the mapped BPM runtime variable"

              That is exactly what we have not been able to do.
              We have the barcode value already in the javascript of the view and, as you can see in the code we've attached its written into a DOM object dom.byId("result").
              But then, we don't know how to do the last part you describe.... trigger the boundary event, and writing to the mapped BPM variable...

              Check the attached code, I hope it makes sense to you, I tried to comment it as much as possible.

              Thanks a lot!!

              Nicolas E.
              • kolban
                kolban
                3315 Posts
                ACCEPTED ANSWER

                Re: How to write from dojo to a service variable

                ‏2012-12-28T21:30:47Z  in response to F7QD_Nicolas_Echavarria
                Aha ... we may be getting closer :-)

                With reference to my December PDF ... page 282 onwards might be useful.

                At a high level, the Coach View is an object. Let us assume its scope is "this" (the JavaScript context 'this')

                Then

                
                this.context.binding
                


                is the reference to the binding between the variable in the run time and the data in the Coach View. I am going to assume that the bar code value is a simple "String".

                In your Coach View definition, you would define the binding type as "String".

                In your Coach you would add your "barcode" Coach View and bind that to a variable (eg. tw.local.myBarCodeValue).

                When the Coach loads and the "barcode" Coach View is instantiated, then using:

                
                this.context.binding.get(
                "value")
                


                will return the value of the "tw.local.myBarCodeValue" (run time variable) as it was when the Coach was loaded. You may use this or ignore it.

                I am now assuming that "something happens" which gets a value (i.e. a barcode scan). You can then call:

                
                this.context.binding.set(
                "value", myBarCodeValueBrowserVariable);
                


                which will set the "binding value" of the "barcode" Coach View to be the content of the browser side JavaScript variable called "myBarCodeValueBrowserVariable".

                When the Coach completes (i.e. a button press) or a boundary trigger occurs (eg. this.context.trigger()) then the value that you stored as the value of Coach View is copied back to the runtime and the mapped variable ("tw.local.myBarCodeValue") now contains the value that was set in the Coach View code.

                Digest this for a little bit and come back and let's see if we are getting closer.

                Neil
                • F7QD_Nicolas_Echavarria
                  59 Posts
                  ACCEPTED ANSWER

                  Re: How to write from dojo to a service variable

                  ‏2013-01-02T18:29:50Z  in response to kolban
                  Hello Neil!

                  I hope you had a great new year celebration!

                  Retaking our issue we've read your post, IBM docs and your Dec. PDF.
                  I "think" we get it, so here's what we've done:

                  1.) In the BPD we created and input string variable and named it "barcode_bpd".
                  2.) We created a human service (which was already created to capture de barcode) with a coach view.
                  3.) We added an output string variable to the Human service named "barcode_hs"
                  4.) We added the output data mapping of barcode_hs -> tw.local.barcode_bpd
                  5.) Then we created the Coach view inside the human service and added a Business Data string variable named "barcode_cv"
                  6.) Then, in the layout of our coach view we dragged the variable "barcode_cv" inside a vertical section. This way we make sure that the binding is created adequately.
                  7.) At his point we go to behavior and in the AMD dependencies register de dojo modules. We have on, dom-construct, _base/lang, dom-class, dom, form/TextBox
                  8.) Now, in the event handlers load section we try the following code:
                  
                  
                  //Testing Javascript console.info(
                  "This is a test"); var a = 1; var b = 2; console.info(a+b);
                  

                  We check in firebug and our info messages correctly display "3". So javascript is working fine.
                  9.) Then, we try to output the current "empty" value of a newly created variable as such:
                  
                  var mybarcode = this.context.binding.get(
                  "value"); console.info(barcode);
                  


                  And we get the following error:
                  "TypeError: this.context.binding is undefined"

                  So... a couple of questions:
                  • Are we getting the process right?
                  • Do we have to somehow, define or literally declare the context in some other way we are not doing?
                  • What happens if inside a view there are three different variables "binded" to three diferent controls? How will the this.context.binding behave?

                  Thanks a lot Neil for all you help!

                  Best,

                  Nicolas E.
                  • F7QD_Nicolas_Echavarria
                    59 Posts
                    ACCEPTED ANSWER

                    Re: How to write from dojo to a service variable

                    ‏2013-01-02T18:45:22Z  in response to F7QD_Nicolas_Echavarria
                    Mistake on the previous post.
                    The correct piece of code used is:

                    
                    var mybarcode = this.context.binding.get(
                    "value"); console.info(mybarcode);
                    
                  • kolban
                    kolban
                    3315 Posts
                    ACCEPTED ANSWER

                    Re: How to write from dojo to a service variable

                    ‏2013-01-02T19:44:23Z  in response to F7QD_Nicolas_Echavarria
                    When you create a new Coach View, the Coach View itself can have a variable bound to it. It is this variable that is available through this.context.binding. If no variable is bound or the variable has no value then this.context.binding may be null.

                    If you have sub views in your new Coach View,they are NOT bound to the top level Coach View unless you explicitly either code the binding or else map at the wiring level.

                    Ive got this feeling that there will suddenly be a light-bulb going off in your brain and you will suddenly "get it" but the problem is as much at my end with my inability to describe it properly.

                    Try a trivial and simple test and work your way up from there.

                    Create a Coach View that binds to a string and in its "view" and "change" callbacks, have it log to the console.

                    Do nothing more than that ... and get a feel of how the data bindings works. Once you have that firmly in your grasp, then we can go deeper.

                    I'm still 110% available to you via the forum ... so don't hesitate to come back and post more.

                    If you post any sample TWX, I'll most certainly look and see. Also consider looking at the existing Coaches in the Wiki and open them up to see how they operate.

                    Neil
                    • F7QD_Nicolas_Echavarria
                      59 Posts
                      ACCEPTED ANSWER

                      Re: How to write from dojo to a service variable

                      ‏2013-01-03T23:46:15Z  in response to kolban
                      Hi Neil!!

                      I'm happy to report we just made it!
                      We are moving the barcode data into a BPD variable to query the webservice.

                      Your advice about creating a simple example made us understand how coach view binding works and after browsing some other examples we think we now get how to bind to multiple variables in a single coach using the .notation

                      Again, as always, thanks so much for all your patience and help!
                      We'll probably be back with more goodies when we hit the next rock. So far we're good!!

                      Thanks again,

                      Nicolas E.
                      • SystemAdmin
                        SystemAdmin
                        7615 Posts
                        ACCEPTED ANSWER

                        Re: How to write from dojo to a service variable

                        ‏2013-01-04T02:07:21Z  in response to F7QD_Nicolas_Echavarria
                        Hi Nicolas,

                        Congrats! I have been following this very interesting post of yours & Neil's solutions. Interesting, because you have introduced Worklight to the variable mix and also how you achieved the integration.

                        In our company our group have been trying to feel the water regarding use of Mobile with IBM BPM. Was just wondering if you can please share, if at all possible, some high level technical architecture for this solution. Basically a solution architecture / design showing integration points, components etc. Also any sample TWX that you have created will also be very helpful.

                        Thank you!
                        Ashish Aggarwal
                        BPM Architect
                        • F7QD_Nicolas_Echavarria
                          59 Posts
                          ACCEPTED ANSWER

                          Re: How to write from dojo to a service variable

                          ‏2013-01-12T00:42:01Z  in response to SystemAdmin
                          Hello Ashish!

                          Well, the BPM current application interacts with 3 different systems.
                          • An Informix DB with Hospital patient information.
                          • A current running Medical Laboratory information system running in a DB server called 4D.
                          • A new Medical Laboratory information system running in DB2.
                          The system also interacts with the iOS devices via Worklight/Cordova to implement the barcode scanning ability and also prints to Zebra printers for POS and Barcode stickers via integrations.

                          As for the TWX we don't have a version we can share (will require a fair bit of work to remove all the proprietary info), but will gladly help you with any doubts or questions that we can answer.

                          As of today, we are hard at work trying to grasp as much as possible on how to integrate an advanced Dojo UI with all the modern Dojo mobile components that are not directly accessible via the off-the-shelf BPM 8.0.1 toolkit. This work is being done in RAD using Dojo and Worklight and we are doing some trials of custom coach creation to build our own toolkit to reuse both functionality and GUI components throughout the project.

                          Best of luck in your project!

                          Nicolas E.