5 Replies Latest reply: Jul 29, 2014 8:02 AM by EricSteele RSS

    Reusable Functions?

    EricSteele

      Is there a way for me to write some JavaScript functions to which all of my rules and transformations have access?  Currently, I'm cutting and pasting them into each script.  I'm hoping there's a better way.

       

      -Eric


        • 1. Re: Reusable Functions?
          Dimitar Slavov-Oracle

          Hi Eric,

           

          Use libraries in Tools->Design Rules->Libraries. Create Libraries and method(s) and use them from transformation scripts and rules.

          There is sample transformation script called "Tables to upper case - JRuby - library usage" which you can use as example.

          • 2. Re: Reusable Functions?
            EricSteele

            Thank you, Dimitar.

             

            My install doesn't have that example, but will give it a try. 

             

            -Eric


            • 3. Re: Re: Reusable Functions?
              EricSteele

              I'm not having much luck getting this to behave the way I want.  Let me be more specific about what I'm trying to do and maybe you can tell me if this is possible.

               

              I have written a function that adds a note to an object.  I call this function anytime a transaction makes a change to an object so that these automatic changes can be tracked.

               

              function addNote(obj, txt) {
                   dt = new Date();
                   // Format a timestamp.
                   tmstmp = (dt.getMonth() + 1).toString() + "/" + dt.getDate() + "/" + dt.getFullYear() + " " +
                             dt.getHours() + ":" + (dt.getMinutes() < 10 ? "0" : "") + dt.getMinutes() + ":" +
                            (dt.getSeconds() < 10 ? "0" : "") + dt.getSeconds();
                   try {
                        // Append to any existing notes.
                        obj.setNotes(obj.getNotes() + (obj.getNotes().length() == 0 ? "" : "\n") + tmstmp + ' - "Sample Transformation" - ' + txt);
                   }
                   catch(e) {
                        // If this errors, we just don't get a note written.
                   }
              }
              

              So, I use this function later in a case such as...

               

              // ctid is the ID of the new Classification Type
              // ctName is the name of the new ClassificationType
              if (table.typeID != ctid) {
                   // Set the ID to the expected CT's ID.
                   table.typeID = ctid;
                   // Add a note to the table that the CT has been changed.
                   addNote(table, "Set classification type to '" + ctName + "'.");
                   // Let the UI know that the table now needs saved.
                   table.setDirty(true);
              }
              

               

              I'd like to move this function to a library and not need it written into every transformation.  I tried adding it to a library, but I only seem to be able to call it as a stand alone script and not from another transformation.  I need to pass parameters.  How do I go about that?

               

              Thanks!

              -Eric

              • 4. Re: Re: Re: Reusable Functions?
                Dimitar Slavov-Oracle

                Hi Eric,

                 

                In the "Functions/Methods" list of "Custom libraries" dialog you have to create publicly exposed methods/functions, that can be selected in "Custom transformations scripts" dialog. Of course these function names must correspond to real function names in the code. These will be separate parts of your tasks. Other functions(non exposed in this example) in code will be common code, used on many places i.e. called/calling from/to other functions.

                I'm attaching screenshot and the code below



                This is very simple example of library with the functions:

                 

                Lib_example.png

                 

                This is how you use it in the transformation script dialog

                 

                Lib_example_2.png

                 

                Here is the code

                 

                // this is publicly exposed
                function doSomeImportantThing() {
                // your logic goes here
                tables = model.getTableSet().toArray();
                for (var t = 0; t<tables.length;t++){
                  table = tables[t];
                  addNote(table, "Add note ...");
                  // call other functions here if needed
                }
                }
                
                
                // this is publicly exposed
                function checkThings() {
                // your logic goes here
                tables = model.getTableSet().toArray();
                for (var t = 0; t<tables.length;t++){
                  table = tables[t];
                  addComments(table, "Add comment ...");
                  // call other functions here if needed
                }
                }
                
                
                // internally accessible, common functions
                function addNote(obj, txt) {
                  obj.setNotes(txt);
                }
                
                
                function addComments(obj, txt) {
                  obj.setComment(txt);
                }
                

                 

                I guess that is what you are looking for

                • 5. Re: Reusable Functions?
                  EricSteele

                  Thank you, Dimitar.  I can make that work.  The screenshots were a little confusing because it looked like the function code was duplicated.  Until I got in and created a couple of function definitions, it wasn't clear that all of the code for an entire library is in the same code block and the "Functions / Methods" list only defines which are public.

                   

                  I also have an enhancement request, but I'll make that a separate post so it can be tracked outside of this question.

                   

                  Thanks again!

                  -Eric