Search in sources :

Example 6 with VariableSpace

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

the class JobExecutorMeta method loadJobMeta.

public static final synchronized JobMeta loadJobMeta(JobExecutorMeta executorMeta, Repository rep, IMetaStore metaStore, VariableSpace space) throws KettleException {
    JobMeta mappingJobMeta = null;
    CurrentDirectoryResolver r = new CurrentDirectoryResolver();
    VariableSpace tmpSpace = r.resolveCurrentDirectory(executorMeta.getSpecificationMethod(), space, rep, executorMeta.getParentStepMeta(), executorMeta.getFileName());
    switch(executorMeta.getSpecificationMethod()) {
        case FILENAME:
            String realFilename = tmpSpace.environmentSubstitute(executorMeta.getFileName());
            try {
                // 
                if (rep != null) {
                    realFilename = r.normalizeSlashes(realFilename);
                    // need to try to load from the repository
                    try {
                        String dirStr = realFilename.substring(0, realFilename.lastIndexOf("/"));
                        String tmpFilename = realFilename.substring(realFilename.lastIndexOf("/") + 1);
                        RepositoryDirectoryInterface dir = rep.findDirectory(dirStr);
                        mappingJobMeta = rep.loadJob(tmpFilename, dir, null, null);
                    } catch (KettleException ke) {
                        // try without extension
                        if (realFilename.endsWith(Const.STRING_JOB_DEFAULT_EXT)) {
                            try {
                                String tmpFilename = realFilename.substring(realFilename.lastIndexOf("/") + 1, realFilename.indexOf("." + Const.STRING_JOB_DEFAULT_EXT));
                                String dirStr = realFilename.substring(0, realFilename.lastIndexOf("/"));
                                RepositoryDirectoryInterface dir = rep.findDirectory(dirStr);
                                mappingJobMeta = rep.loadJob(tmpFilename, dir, null, null);
                            } catch (KettleException ke2) {
                            // fall back to try loading from file system (mappingJobMeta is going to be null)
                            }
                        }
                    }
                }
                if (mappingJobMeta == null) {
                    mappingJobMeta = new JobMeta(null, realFilename, rep, metaStore, null);
                    LogChannel.GENERAL.logDetailed("Loading job from repository", "Job was loaded from XML file [" + realFilename + "]");
                }
            } catch (Exception e) {
                throw new KettleException(BaseMessages.getString(PKG, "JobExecutorMeta.Exception.UnableToLoadJob"), e);
            }
            break;
        case REPOSITORY_BY_NAME:
            String realJobname = tmpSpace.environmentSubstitute(executorMeta.getJobName());
            String realDirectory = tmpSpace.environmentSubstitute(executorMeta.getDirectoryPath());
            if (rep != null) {
                if (!Utils.isEmpty(realJobname) && !Utils.isEmpty(realDirectory)) {
                    realDirectory = r.normalizeSlashes(realDirectory);
                    RepositoryDirectoryInterface repdir = rep.findDirectory(realDirectory);
                    if (repdir != null) {
                        try {
                            // reads the last revision in the repository...
                            // 
                            // TODO: FIXME: should we also pass an
                            mappingJobMeta = rep.loadJob(realJobname, repdir, null, null);
                            // external MetaStore into the
                            // repository?
                            LogChannel.GENERAL.logDetailed("Loading job from repository", "Executor job [" + realJobname + "] was loaded from the repository");
                        } catch (Exception e) {
                            throw new KettleException("Unable to load job [" + realJobname + "]", e);
                        }
                    }
                }
            } else {
                // rep is null, let's try loading by filename
                try {
                    mappingJobMeta = new JobMeta(null, realDirectory + "/" + realJobname, rep, metaStore, null);
                } catch (KettleException ke) {
                    try {
                        // add .kjb extension and try again
                        mappingJobMeta = new JobMeta(null, realDirectory + "/" + realJobname + "." + Const.STRING_JOB_DEFAULT_EXT, rep, metaStore, null);
                    } catch (KettleException ke2) {
                        throw new KettleException(BaseMessages.getString(PKG, "JobExecutorMeta.Exception.UnableToLoadJob", realJobname) + realDirectory);
                    }
                }
            }
            break;
        case REPOSITORY_BY_REFERENCE:
            // Read the last revision by reference...
            mappingJobMeta = rep.loadJob(executorMeta.getJobObjectId(), null);
            break;
        default:
            break;
    }
    // Pass some important information to the mapping transformation metadata:
    // When the child parameter does exist in the parent parameters, overwrite the child parameter by the
    // parent parameter.
    StepWithMappingMeta.replaceVariableValues(mappingJobMeta, space);
    if (executorMeta.getParameters().isInheritingAllVariables()) {
        // All other parent parameters need to get copied into the child parameters  (when the 'Inherit all
        // variables from the transformation?' option is checked)
        StepWithMappingMeta.addMissingVariables(mappingJobMeta, space);
    }
    mappingJobMeta.setRepository(rep);
    mappingJobMeta.setMetaStore(metaStore);
    mappingJobMeta.setFilename(mappingJobMeta.getFilename());
    return mappingJobMeta;
}
Also used : RepositoryDirectoryInterface(org.pentaho.di.repository.RepositoryDirectoryInterface) KettleException(org.pentaho.di.core.exception.KettleException) JobMeta(org.pentaho.di.job.JobMeta) VariableSpace(org.pentaho.di.core.variables.VariableSpace) CurrentDirectoryResolver(org.pentaho.di.core.util.CurrentDirectoryResolver) ValueMetaString(org.pentaho.di.core.row.value.ValueMetaString) KettleXMLException(org.pentaho.di.core.exception.KettleXMLException) KettleStepException(org.pentaho.di.core.exception.KettleStepException) KettleException(org.pentaho.di.core.exception.KettleException) KettlePluginException(org.pentaho.di.core.exception.KettlePluginException)

Example 7 with VariableSpace

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

the class JsonInputTest method testJsonInputMetaInputFieldsNotOverwritten.

@Test
public void testJsonInputMetaInputFieldsNotOverwritten() throws Exception {
    JsonInputField inputField = new JsonInputField();
    final String PATH = "$..book[?(@.category=='${category}')].price";
    inputField.setPath(PATH);
    inputField.setType(ValueMetaInterface.TYPE_STRING);
    final JsonInputMeta inputMeta = createSimpleMeta("json", inputField);
    VariableSpace variables = new Variables();
    variables.setVariable("category", "fiction");
    JsonInput jsonInput = createJsonInput("json", inputMeta, variables, new Object[] { getBasicTestJson() });
    processRows(jsonInput, 2);
    assertEquals("Meta input fields paths should be the same after processRows", PATH, inputMeta.getInputFields()[0].getPath());
}
Also used : Variables(org.pentaho.di.core.variables.Variables) VariableSpace(org.pentaho.di.core.variables.VariableSpace) ValueMetaString(org.pentaho.di.core.row.value.ValueMetaString) Test(org.junit.Test)

Example 8 with VariableSpace

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

the class MetaInjectMeta method loadTransformationMeta.

public static final synchronized TransMeta loadTransformationMeta(MetaInjectMeta injectMeta, Repository rep, IMetaStore metaStore, VariableSpace space) throws KettleException {
    TransMeta mappingTransMeta = null;
    CurrentDirectoryResolver resolver = new CurrentDirectoryResolver();
    VariableSpace tmpSpace = resolver.resolveCurrentDirectory(injectMeta.getSpecificationMethod(), space, rep, injectMeta.getParentStepMeta(), injectMeta.getFileName());
    switch(injectMeta.getSpecificationMethod()) {
        case FILENAME:
            String realFilename = tmpSpace.environmentSubstitute(injectMeta.getFileName());
            try {
                // 
                if (rep != null) {
                    // need to try to load from the repository
                    realFilename = resolver.normalizeSlashes(realFilename);
                    try {
                        String dirStr = realFilename.substring(0, realFilename.lastIndexOf("/"));
                        String tmpFilename = realFilename.substring(realFilename.lastIndexOf("/") + 1);
                        RepositoryDirectoryInterface dir = rep.findDirectory(dirStr);
                        mappingTransMeta = rep.loadTransformation(tmpFilename, dir, null, true, null);
                    } catch (KettleException ke) {
                        // try without extension
                        if (realFilename.endsWith(Const.STRING_TRANS_DEFAULT_EXT)) {
                            try {
                                String tmpFilename = realFilename.substring(realFilename.lastIndexOf("/") + 1, realFilename.indexOf("." + Const.STRING_TRANS_DEFAULT_EXT));
                                String dirStr = realFilename.substring(0, realFilename.lastIndexOf("/"));
                                RepositoryDirectoryInterface dir = rep.findDirectory(dirStr);
                                mappingTransMeta = rep.loadTransformation(tmpFilename, dir, null, true, null);
                            } catch (KettleException ke2) {
                            // fall back to try loading from file system (transMeta is going to be null)
                            }
                        }
                    }
                }
                if (mappingTransMeta == null) {
                    mappingTransMeta = new TransMeta(realFilename, metaStore, rep, false, tmpSpace, null);
                    mappingTransMeta.getLogChannel().logDetailed("Loading Mapping from repository", "Mapping transformation was loaded from XML file [" + realFilename + "]");
                }
            } catch (Exception e) {
                throw new KettleException(BaseMessages.getString(PKG, "MetaInjectMeta.Exception.UnableToLoadTransformationFromFile", realFilename), e);
            }
            break;
        case REPOSITORY_BY_NAME:
            String realTransname = tmpSpace.environmentSubstitute(injectMeta.getTransName());
            String realDirectory = tmpSpace.environmentSubstitute(injectMeta.getDirectoryPath());
            if (rep != null) {
                if (!Utils.isEmpty(realTransname) && !Utils.isEmpty(realDirectory) && rep != null) {
                    RepositoryDirectoryInterface repdir = rep.findDirectory(realDirectory);
                    if (repdir != null) {
                        try {
                            // reads the last revision in the repository...
                            // 
                            // TODO: FIXME: see if we need to pass external MetaStore references to the repository?
                            // 
                            mappingTransMeta = rep.loadTransformation(realTransname, repdir, null, true, null);
                            mappingTransMeta.getLogChannel().logDetailed("Loading Mapping from repository", "Mapping transformation [" + realTransname + "] was loaded from the repository");
                        } catch (Exception e) {
                            throw new KettleException("Unable to load transformation [" + realTransname + "]", e);
                        }
                    } else {
                        throw new KettleException(BaseMessages.getString(PKG, "MetaInjectMeta.Exception.UnableToLoadTransformationFromRepository", realTransname, realDirectory));
                    }
                }
            } else {
                try {
                    mappingTransMeta = new TransMeta(realDirectory + "/" + realTransname, metaStore, rep, true, tmpSpace, null);
                } catch (KettleException ke) {
                    try {
                        // add .ktr extension and try again
                        mappingTransMeta = new TransMeta(realDirectory + "/" + realTransname + "." + Const.STRING_TRANS_DEFAULT_EXT, metaStore, rep, true, tmpSpace, null);
                    } catch (KettleException ke2) {
                        throw new KettleException(BaseMessages.getString(PKG, "StepWithMappingMeta.Exception.UnableToLoadTrans", realTransname) + realDirectory);
                    }
                }
            }
            break;
        case REPOSITORY_BY_REFERENCE:
            // Read the last revision by reference...
            mappingTransMeta = rep.loadTransformation(injectMeta.getTransObjectId(), null);
            break;
        default:
            break;
    }
    // Pass some important information to the mapping transformation metadata:
    // 
    mappingTransMeta.copyVariablesFrom(space);
    mappingTransMeta.setRepository(rep);
    mappingTransMeta.setFilename(mappingTransMeta.getFilename());
    return mappingTransMeta;
}
Also used : RepositoryDirectoryInterface(org.pentaho.di.repository.RepositoryDirectoryInterface) KettleException(org.pentaho.di.core.exception.KettleException) VariableSpace(org.pentaho.di.core.variables.VariableSpace) TransMeta(org.pentaho.di.trans.TransMeta) CurrentDirectoryResolver(org.pentaho.di.core.util.CurrentDirectoryResolver) KettleException(org.pentaho.di.core.exception.KettleException) KettleXMLException(org.pentaho.di.core.exception.KettleXMLException) KettlePluginException(org.pentaho.di.core.exception.KettlePluginException) KettleStepException(org.pentaho.di.core.exception.KettleStepException)

Example 9 with VariableSpace

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

the class MetaInjectMetaTest method exportResources.

@Test
public void exportResources() throws KettleException {
    VariableSpace variableSpace = mock(VariableSpace.class);
    ResourceNamingInterface resourceNamingInterface = mock(ResourceNamingInterface.class);
    Repository repository = mock(Repository.class);
    IMetaStore metaStore = mock(IMetaStore.class);
    MetaInjectMeta injectMetaSpy = spy(metaInjectMeta);
    TransMeta transMeta = mock(TransMeta.class);
    Map<String, ResourceDefinition> definitions = Collections.<String, ResourceDefinition>emptyMap();
    doReturn(TEST_FILE_NAME).when(transMeta).exportResources(transMeta, definitions, resourceNamingInterface, repository, metaStore);
    doReturn(transMeta).when(injectMetaSpy).loadTransformationMeta(repository, variableSpace);
    String actualExportedFileName = injectMetaSpy.exportResources(variableSpace, definitions, resourceNamingInterface, repository, metaStore);
    assertEquals(TEST_FILE_NAME, actualExportedFileName);
    assertEquals(EXPORTED_FILE_NAME, injectMetaSpy.getFileName());
    verify(transMeta).exportResources(transMeta, definitions, resourceNamingInterface, repository, metaStore);
}
Also used : Repository(org.pentaho.di.repository.Repository) VariableSpace(org.pentaho.di.core.variables.VariableSpace) ResourceDefinition(org.pentaho.di.resource.ResourceDefinition) TransMeta(org.pentaho.di.trans.TransMeta) Matchers.anyString(org.mockito.Matchers.anyString) IMetaStore(org.pentaho.metastore.api.IMetaStore) ResourceNamingInterface(org.pentaho.di.resource.ResourceNamingInterface) Test(org.junit.Test)

Example 10 with VariableSpace

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

the class TableInputMetaTest method testGetFields.

@Test
public void testGetFields() throws Exception {
    TableInputMetaHandler meta = new TableInputMetaHandler();
    meta.setLazyConversionActive(true);
    DatabaseMeta dbMeta = mock(DatabaseMeta.class);
    meta.setDatabaseMeta(dbMeta);
    Database mockDB = meta.getDatabase();
    when(mockDB.getQueryFields(anyString(), anyBoolean())).thenReturn(createMockFields());
    RowMetaInterface expectedRowMeta = new RowMeta();
    ValueMetaInterface valueMeta = new ValueMetaString("field1");
    valueMeta.setStorageMetadata(new ValueMetaString("field1"));
    valueMeta.setStorageType(ValueMetaInterface.STORAGE_TYPE_BINARY_STRING);
    expectedRowMeta.addValueMeta(valueMeta);
    VariableSpace space = mock(VariableSpace.class);
    RowMetaInterface rowMetaInterface = new RowMeta();
    meta.getFields(rowMetaInterface, "TABLE_INPUT_META", null, null, space, null, null);
    assertEquals(expectedRowMeta.toString(), rowMetaInterface.toString());
}
Also used : ValueMetaString(org.pentaho.di.core.row.value.ValueMetaString) RowMeta(org.pentaho.di.core.row.RowMeta) VariableSpace(org.pentaho.di.core.variables.VariableSpace) Database(org.pentaho.di.core.database.Database) RowMetaInterface(org.pentaho.di.core.row.RowMetaInterface) DatabaseMeta(org.pentaho.di.core.database.DatabaseMeta) ValueMetaInterface(org.pentaho.di.core.row.ValueMetaInterface) Test(org.junit.Test)

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