Package org.pentaho.actionsequence.dom.actions

Examples of org.pentaho.actionsequence.dom.actions.JavascriptAction


  }

  @Override
  protected boolean validateAction() {
    boolean actionValidated = true;
    JavascriptAction jscriptAction = null;

    if ( getActionDefinition() instanceof JavascriptAction ) {
      jscriptAction = (JavascriptAction) getActionDefinition();

      // get report connection setting
      if ( jscriptAction.getScript() == ActionInputConstant.NULL_INPUT ) {
        error( Messages.getInstance().getErrorString( "JSRULE.ERROR_0001_SCRIPT_NOT_DEFINED", getActionName() ) ); //$NON-NLS-1$
        actionValidated = false;
      }

      if ( actionValidated ) {
        if ( jscriptAction.getOutputs().length <= 0 ) {
          error( Messages.getInstance().getString( "Template.ERROR_0002_OUTPUT_COUNT_WRONG" ) ); //$NON-NLS-1$
          actionValidated = false;
        }
      }

      if ( actionValidated ) {
        IActionOutput[] outputs = jscriptAction.getOutputs(); // getOutputNames();
        /*
         * if the number of action def outputs is more than 1 and if the fist output var defined in the input section is
         * named output1 then the xaction is defined oldstyle and we should check if there is corresponding o/p variable
         * defined in the input section for each output var defined in the output section. NOTE: With the old style you
         * can only have output defined as "output#" where # is a number.
         */
        if ( ( outputs.length > 1 ) && ( jscriptAction.getInput( "output1" ) != ActionInputConstant.NULL_INPUT ) ) { //$NON-NLS-1$
          oldStyleOutputs = true;
          for ( int i = 1; i <= outputs.length; ++i ) {
            if ( jscriptAction.getInput( "output" + i ) == ActionInputConstant.NULL_INPUT ) { //$NON-NLS-1$
              error( Messages.getInstance().getErrorString(
                  "JavascriptRule.ERROR_0006_NO_MAPPED_OUTPUTS", String.valueOf( outputs.length ), String.valueOf( i ) ) ); //$NON-NLS-1$
              actionValidated = false;
              break;
            }
View Full Code Here


        buffer.append( getResourceAsString( resource ) );
      }
    }

    List<String> outputNames = new ArrayList<String>();
    JavascriptAction jscriptAction = (JavascriptAction) getActionDefinition();

    IActionOutput[] actionOutputs = jscriptAction.getOutputs();
    if ( actionOutputs.length == 1 ) {
      String outputName = actionOutputs[0].getName();
      outputNames.add( outputName );
    } else {
      if ( oldStyleOutputs ) {
        int i = 1;
        while ( true ) {
          if ( jscriptAction.getInput( "output" + i ) != ActionInputConstant.NULL_INPUT ) { //$NON-NLS-1$
            outputNames.add( jscriptAction.getInput( "output" + i ).getStringValue() ); //$NON-NLS-1$
          } else {
            break;
          }
          i++;
        }
      } else {
        for ( IActionOutput element : actionOutputs ) {
          outputNames.add( element.getName() );
        }
      }
    }

    boolean success = false;
    try {
      String script = jscriptAction.getScript().getStringValue();
      if ( script == null ) {
        error( Messages.getInstance().getErrorString( "JSRULE.ERROR_0001_SCRIPT_NOT_DEFINED", getActionName() ) ); //$NON-NLS-1$
      } else {
        buffer.append( script );
        script = buffer.toString();
        if ( ComponentBase.debug ) {
          debug( "script=" + script ); //$NON-NLS-1$
        }
        try {
          ScriptableObject scriptable = new RhinoScriptable();
          // initialize the standard javascript objects
          Scriptable scope = cx.initStandardObjects( scriptable );

          Object resultObject = executeScript( scriptable, scope, script, cx );
          if ( oldStyleOutputs ) {
            if ( resultObject instanceof org.mozilla.javascript.NativeArray ) {
              // we need to convert this to an ArrayList
              NativeArray jsArray = (NativeArray) resultObject;
              int length = (int) jsArray.getLength();
              for ( int i = 0; i < length; i++ ) {
                Object value = jsArray.get( i, scriptable );
                if ( i < outputNames.size() ) {
                  jscriptAction.getOutput( outputNames.get( i ).toString() )
                      .setValue( convertWrappedJavaObject( value ) );
                } else {
                  break;
                }
              }
            } else {
              jscriptAction.getOutput( outputNames.get( 0 ).toString() ).setValue(
                convertWrappedJavaObject( resultObject ) );
            }
          } else {
            if ( ( outputNames.size() == 1 ) && ( resultObject != null ) ) {
              jscriptAction.getOutput( outputNames.get( 0 ).toString() ).setValue(
                convertWrappedJavaObject( resultObject ) );
            } else {
              List<String> setOutputs = new ArrayList<String>( outputNames.size() );
              Object[] ids = ScriptableObject.getPropertyIds( scope );
              for ( Object element : ids ) {
                int idx = outputNames.indexOf( element.toString() );
                if ( idx >= 0 ) {
                  jscriptAction.getOutput( outputNames.get( idx ).toString() ).setValue(
                      convertWrappedJavaObject( ScriptableObject.getProperty( scope, (String) element ) ) );
                  setOutputs.add( outputNames.get( idx ) );
                }
              }
              // Javascript Component defined an output, but
              // didn't set it to anything.
              // So, set it to null.
              if ( setOutputs.size() != outputNames.size() ) {
                for ( int i = 0; i < outputNames.size(); i++ ) {
                  if ( setOutputs.indexOf( outputNames.get( i ) ) < 0 ) {
                    // An output that wasn't set in the
                    // javascript component
                    jscriptAction.getOutput( outputNames.get( i ).toString() ).setValue( null );
                  }
                }
              }
            }
View Full Code Here

TOP

Related Classes of org.pentaho.actionsequence.dom.actions.JavascriptAction

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.