Search in sources :

Example 21 with VariableSpace

use of org.pentaho.di.core.variables.VariableSpace in project pentaho-kettle by pentaho.

the class AbstractMetaTest method testGetSetParentVariableSpace.

@Test
public void testGetSetParentVariableSpace() throws Exception {
    assertNull(meta.getParentVariableSpace());
    VariableSpace variableSpace = mock(VariableSpace.class);
    meta.setParentVariableSpace(variableSpace);
    assertEquals(variableSpace, meta.getParentVariableSpace());
}
Also used : VariableSpace(org.pentaho.di.core.variables.VariableSpace) Test(org.junit.Test)

Example 22 with VariableSpace

use of org.pentaho.di.core.variables.VariableSpace in project pentaho-kettle by pentaho.

the class SetVariable method setValue.

private void setValue(Object[] rowData, int i, boolean usedefault) throws KettleException {
    // Set the appropriate environment variable
    // 
    String value = null;
    if (usedefault) {
        value = environmentSubstitute(meta.getDefaultValue()[i]);
    } else {
        int index = data.outputMeta.indexOfValue(meta.getFieldName()[i]);
        if (index < 0) {
            throw new KettleException("Unable to find field [" + meta.getFieldName()[i] + "] in input row");
        }
        ValueMetaInterface valueMeta = data.outputMeta.getValueMeta(index);
        Object valueData = rowData[index];
        // 
        if (meta.isUsingFormatting()) {
            value = valueMeta.getString(valueData);
        } else {
            value = valueMeta.getCompatibleString(valueData);
        }
    }
    if (value == null) {
        value = "";
    }
    // Get variable name
    String varname = meta.getVariableName()[i];
    if (Utils.isEmpty(varname)) {
        if (Utils.isEmpty(value)) {
            throw new KettleException("Variable name nor value was specified on line #" + (i + 1));
        } else {
            throw new KettleException("There was no variable name specified for value [" + value + "]");
        }
    }
    Job parentJob = null;
    // We always set the variable in this step and in the parent transformation...
    // 
    setVariable(varname, value);
    // Set variable in the transformation
    // 
    Trans trans = getTrans();
    trans.setVariable(varname, value);
    // 
    while (trans.getParentTrans() != null) {
        trans = trans.getParentTrans();
        trans.setVariable(varname, value);
    }
    // 
    switch(meta.getVariableType()[i]) {
        case SetVariableMeta.VARIABLE_TYPE_JVM:
            System.setProperty(varname, value);
            parentJob = trans.getParentJob();
            while (parentJob != null) {
                parentJob.setVariable(varname, value);
                parentJob = parentJob.getParentJob();
            }
            break;
        case SetVariableMeta.VARIABLE_TYPE_ROOT_JOB:
            // Comments by SB
            // VariableSpace rootJob = null;
            parentJob = trans.getParentJob();
            while (parentJob != null) {
                parentJob.setVariable(varname, value);
                // rootJob = parentJob;
                parentJob = parentJob.getParentJob();
            }
            break;
        case SetVariableMeta.VARIABLE_TYPE_GRAND_PARENT_JOB:
            // Set the variable in the parent job
            // 
            parentJob = trans.getParentJob();
            if (parentJob != null) {
                parentJob.setVariable(varname, value);
            } else {
                throw new KettleStepException("Can't set variable [" + varname + "] on parent job: the parent job is not available");
            }
            // Set the variable on the grand-parent job
            // 
            VariableSpace gpJob = trans.getParentJob().getParentJob();
            if (gpJob != null) {
                gpJob.setVariable(varname, value);
            } else {
                throw new KettleStepException("Can't set variable [" + varname + "] on grand parent job: the grand parent job is not available");
            }
            break;
        case SetVariableMeta.VARIABLE_TYPE_PARENT_JOB:
            // Set the variable in the parent job
            // 
            parentJob = trans.getParentJob();
            if (parentJob != null) {
                parentJob.setVariable(varname, value);
            } else {
                throw new KettleStepException("Can't set variable [" + varname + "] on parent job: the parent job is not available");
            }
            break;
        default:
            break;
    }
    logBasic(BaseMessages.getString(PKG, "SetVariable.Log.SetVariableToValue", meta.getVariableName()[i], value));
}
Also used : KettleException(org.pentaho.di.core.exception.KettleException) KettleStepException(org.pentaho.di.core.exception.KettleStepException) VariableSpace(org.pentaho.di.core.variables.VariableSpace) Job(org.pentaho.di.job.Job) Trans(org.pentaho.di.trans.Trans) ValueMetaInterface(org.pentaho.di.core.row.ValueMetaInterface)

Example 23 with VariableSpace

use of org.pentaho.di.core.variables.VariableSpace in project pentaho-kettle by pentaho.

the class ScriptValuesAddedFunctions method setParentScopeVariable.

static void setParentScopeVariable(Trans trans, String variableName, String variableValue) {
    trans.setVariable(variableName, variableValue);
    VariableSpace parentSpace = trans.getParentVariableSpace();
    if (parentSpace != null) {
        parentSpace.setVariable(variableName, variableValue);
    }
}
Also used : VariableSpace(org.pentaho.di.core.variables.VariableSpace)

Example 24 with VariableSpace

use of org.pentaho.di.core.variables.VariableSpace in project pentaho-kettle by pentaho.

the class ScriptValuesAddedFunctions method setRootScopeVariable.

static void setRootScopeVariable(Trans trans, String variableName, String variableValue) {
    trans.setVariable(variableName, variableValue);
    VariableSpace parentSpace = trans.getParentVariableSpace();
    while (parentSpace != null) {
        parentSpace.setVariable(variableName, variableValue);
        parentSpace = parentSpace.getParentVariableSpace();
    }
}
Also used : VariableSpace(org.pentaho.di.core.variables.VariableSpace)

Example 25 with VariableSpace

use of org.pentaho.di.core.variables.VariableSpace in project pentaho-kettle by pentaho.

the class ScriptValuesAddedFunctions method setGrandParentScopeVariable.

static void setGrandParentScopeVariable(Trans trans, String variableName, String variableValue) {
    trans.setVariable(variableName, variableValue);
    VariableSpace parentSpace = trans.getParentVariableSpace();
    if (parentSpace != null) {
        parentSpace.setVariable(variableName, variableValue);
        VariableSpace grandParentSpace = parentSpace.getParentVariableSpace();
        if (grandParentSpace != null) {
            grandParentSpace.setVariable(variableName, variableValue);
        }
    }
}
Also used : VariableSpace(org.pentaho.di.core.variables.VariableSpace)

Aggregations

VariableSpace (org.pentaho.di.core.variables.VariableSpace)49 Test (org.junit.Test)21 Variables (org.pentaho.di.core.variables.Variables)14 KettleException (org.pentaho.di.core.exception.KettleException)12 Repository (org.pentaho.di.repository.Repository)10 TransMeta (org.pentaho.di.trans.TransMeta)10 ValueMetaString (org.pentaho.di.core.row.value.ValueMetaString)9 RepositoryDirectoryInterface (org.pentaho.di.repository.RepositoryDirectoryInterface)8 StepMeta (org.pentaho.di.trans.step.StepMeta)8 RowMetaInterface (org.pentaho.di.core.row.RowMetaInterface)7 KettleXMLException (org.pentaho.di.core.exception.KettleXMLException)6 IOException (java.io.IOException)5 FileObject (org.apache.commons.vfs2.FileObject)5 CurrentDirectoryResolver (org.pentaho.di.core.util.CurrentDirectoryResolver)5 HashMap (java.util.HashMap)4 Properties (java.util.Properties)4 Matchers.anyString (org.mockito.Matchers.anyString)4 DatabaseMeta (org.pentaho.di.core.database.DatabaseMeta)4 KettleDatabaseException (org.pentaho.di.core.exception.KettleDatabaseException)4 ValueMetaInterface (org.pentaho.di.core.row.ValueMetaInterface)4