Hitachi Vantara Pentaho Community Forums
Results 1 to 11 of 11

Thread: Modified Javascript: good script doesn't pass syntax check, str2date

  1. #1
    Join Date
    Jul 2007
    Posts
    18

    Question Modified Javascript: good script doesn't pass syntax check, str2date

    I have the following situation: one of my input fields contains a date in Julian format (e.g.: "2007/300") OR a string constant ("PERMANENT" or "WHILECATLG"). The Julian dates parse easily using the format: "y/D", but the string constants cause parsing errors, so i'm reading this field as a String and parsing it later in a modified Javascript step.

    The input field mentioned above is RETSTR. I used "add constants" to add a RETDATE field, with value of Date. Then, in Modified Javascript, i have:

    rstr = RETSTR.getString()

    if ((rstr == "PERMANENT") || (rstr == "WHILECATLG"))
    {
    // nothing
    }
    else
    {
    RETDATE.setValue(str2date(rstr, "y/D"))
    }

    This looks right to me, and it works fine if i run a "preview" or "debug" of the transformation. But the "Test Script" button returns "Could not apply the given format on the string : Format.parseObject(String) failed (script#40)". The same thing happens when i try to run the transformation.

    I used Javascript Alert to display the value of rstr before the str2date above (Alert(rstr)), and i get "test value test value test value test value".

    So it's looking like to me that the Modified Javascript step uses that string ("test value test value test value") as a placeholder for the value of the string variable, and of course the compile fails.

    Is this a known problem? Is there any way around this? Javascript is not my forte, but i've checked the available documentation for things like error handling and exception handling, but i'm not finding anything.

    Note: I've tried this with Kettle 2.5.1 and 3.0, with the same results.

    Thank you,


    glauber

  2. #2
    Join Date
    May 2006
    Posts
    4,882

    Default

    It's a known 'problem'... http://jira.pentaho.org/browse/PDI-160

    Regards,
    Sven

  3. #3
    Join Date
    Jul 2007
    Posts
    18

    Default

    Thanks. It would be nice if we could still run the transformation, but Kettle seems to run a syntax check when it gets to the Modified Javascript step, and stops the transformation there.

  4. #4
    Join Date
    Nov 1999
    Posts
    9,729

    Default

    That script just hurts my eyes...
    This would seem better form:

    Code:
    var returnDate = RETDATE.getDate();
    
    if ( !rstr.equals("PERMANENT") && !rstr.equals("WHILECATLG") )
    {
      returnDate = str2date(rstr, "y/D");
    }

  5. #5
    Join Date
    Jul 2007
    Posts
    18

    Default

    You probably meant

    Code:
    var retentionDate;
    var rstr = RETSTR.getString();
    
    
    if ( !rstr.equals("PERMANENT") && !rstr.equals("WHILECATLG") )
    {
        retentionDate = str2date(rstr, "y/D");
        RETDATE.setValue(retentionDate);
    }
    ... which of course still doesn't work.

    I didin't write the if statement like this becasue i don't like double negatives (if not this and not that).

    But thanks for the Javascript tips.

  6. #6
    Join Date
    Nov 1999
    Posts
    9,729

    Default

    I couldn't care less about the "if" statement.
    Using the setValue() method is actually bad form if you ask me.
    It was a good idea originally, but JavaScript is just not the right language for it.

    For the reasons behind it:

    http://wiki.pentaho.org/display/EAI/...2.5.x+to+3.0.0

    Matt

  7. #7
    Join Date
    Jul 2007
    Posts
    18

    Default

    Cool, this brings up something else i meant to post about. I stumbled upon the URL you mention earlier today, when i was looking for ways around the script validation problem.

    How do we modify a field's value without using setValue()? Assigning to the field directly doesn't seem to work. (If we can't change the value of a field, is there still value in using Javascript?) Creating a new field with the new value would be ok too, as long as the result of the Javascript can be passed to the next step in the transformation.
    Last edited by glauber; 11-16-2007 at 05:55 PM.

  8. #8
    Join Date
    May 2006
    Posts
    4,882

    Default

    In 3.0 without compatibility on, you don't have Values but your row consists of actual Long, Strings, ... Because conversion in JavaScript is sometimes lose, changing a value in place is not the best in some cases.

    You can add new fields in the bottom table to be passed to the next steps, and directly assign values to that field to pass it to the next step.

    Regards,
    Sven

  9. #9
    Join Date
    Jul 2007
    Posts
    18

    Default

    Allright, Sven, this works! Thanks!!!

    (Now if i can figure out a way to parse those dates without triggering the script validation bug, i'll be set!)
    Last edited by glauber; 11-16-2007 at 06:29 PM.

  10. #10
    Join Date
    May 2006
    Posts
    4,882

    Default

    It's not a compilation/validation bug, the problem is just that the input row when using the test button is filled with "dummy data" and if you assume in your javascript code some field needs to be of a certain format you may get an exception thrown at you.

    The step will run and preview ok (with correct input data), the test button in such a case will be useless. The problem is logged in JIRA... vote for it

    Regards,
    Sven

  11. #11
    Join Date
    Jul 2007
    Posts
    18

    Default

    so it does. Thanks again. I don't know why my script was failing; i got it to work now.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Privacy Policy | Legal Notices | Safe Harbor Privacy Policy

Copyright © 2005 - 2019 Hitachi Vantara Corporation. All Rights Reserved.