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;
}
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());
}
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;
}
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);
}
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());
}
Aggregations