use of org.pentaho.di.trans.steps.selectvalues.SelectMetadataChange in project pentaho-kettle by pentaho.
the class SpoonDBDelegate method copyTable.
public boolean copyTable(DatabaseMeta sourceDBInfo, DatabaseMeta targetDBInfo, String tablename) {
try {
//
// Create a new transformation...
//
TransMeta meta = new TransMeta();
meta.addDatabase(sourceDBInfo);
meta.addDatabase(targetDBInfo);
//
// Add a note
//
String note = BaseMessages.getString(PKG, "Spoon.Message.Note.ReadInformationFromTableOnDB", tablename, sourceDBInfo.getDatabaseName()) + // "Reads information from table ["+tablename+"]
Const.CR;
// on database ["+sourceDBInfo+"]"
note += BaseMessages.getString(PKG, "Spoon.Message.Note.WriteInformationToTableOnDB", tablename, targetDBInfo.getDatabaseName());
// the information to table
// ["+tablename+"] on
// database
// ["+targetDBInfo+"]"
NotePadMeta ni = new NotePadMeta(note, 150, 10, -1, -1);
meta.addNote(ni);
//
// create the source step...
//
// "read
String fromstepname = BaseMessages.getString(PKG, "Spoon.Message.Note.ReadFromTable", tablename);
// from
// ["+tablename+"]";
TableInputMeta tii = new TableInputMeta();
tii.setDatabaseMeta(sourceDBInfo);
tii.setSQL("SELECT * FROM " + tablename);
PluginRegistry registry = PluginRegistry.getInstance();
String fromstepid = registry.getPluginId(StepPluginType.class, tii);
StepMeta fromstep = new StepMeta(fromstepid, fromstepname, tii);
fromstep.setLocation(150, 100);
fromstep.setDraw(true);
fromstep.setDescription(BaseMessages.getString(PKG, "Spoon.Message.Note.ReadInformationFromTableOnDB", tablename, sourceDBInfo.getDatabaseName()));
meta.addStep(fromstep);
//
// add logic to rename fields in case any of the field names contain
// reserved words...
// Use metadata logic in SelectValues, use SelectValueInfo...
//
Database sourceDB = new Database(loggingObject, sourceDBInfo);
sourceDB.shareVariablesWith(meta);
sourceDB.connect();
try {
// Get the fields for the input table...
RowMetaInterface fields = sourceDB.getTableFields(tablename);
// See if we need to deal with reserved words...
int nrReserved = targetDBInfo.getNrReservedWords(fields);
if (nrReserved > 0) {
SelectValuesMeta svi = new SelectValuesMeta();
svi.allocate(0, 0, nrReserved);
int nr = 0;
// CHECKSTYLE:Indentation:OFF
for (int i = 0; i < fields.size(); i++) {
ValueMetaInterface v = fields.getValueMeta(i);
if (targetDBInfo.isReservedWord(v.getName())) {
if (svi.getMeta()[nr] == null) {
svi.getMeta()[nr] = new SelectMetadataChange(svi);
}
svi.getMeta()[nr].setName(v.getName());
svi.getMeta()[nr].setRename(targetDBInfo.quoteField(v.getName()));
nr++;
}
}
String selstepname = BaseMessages.getString(PKG, "Spoon.Message.Note.HandleReservedWords");
String selstepid = registry.getPluginId(StepPluginType.class, svi);
StepMeta selstep = new StepMeta(selstepid, selstepname, svi);
selstep.setLocation(350, 100);
selstep.setDraw(true);
selstep.setDescription(BaseMessages.getString(PKG, "Spoon.Message.Note.RenamesReservedWords", //
targetDBInfo.getPluginId()));
meta.addStep(selstep);
TransHopMeta shi = new TransHopMeta(fromstep, selstep);
meta.addTransHop(shi);
fromstep = selstep;
}
//
// Create the target step...
//
//
// Add the TableOutputMeta step...
//
String tostepname = BaseMessages.getString(PKG, "Spoon.Message.Note.WriteToTable", tablename);
TableOutputMeta toi = new TableOutputMeta();
toi.setDatabaseMeta(targetDBInfo);
toi.setTableName(tablename);
toi.setCommitSize(200);
toi.setTruncateTable(true);
String tostepid = registry.getPluginId(StepPluginType.class, toi);
StepMeta tostep = new StepMeta(tostepid, tostepname, toi);
tostep.setLocation(550, 100);
tostep.setDraw(true);
tostep.setDescription(BaseMessages.getString(PKG, "Spoon.Message.Note.WriteInformationToTableOnDB2", tablename, targetDBInfo.getDatabaseName()));
meta.addStep(tostep);
//
// Add a hop between the two steps...
//
TransHopMeta hi = new TransHopMeta(fromstep, tostep);
meta.addTransHop(hi);
// OK, if we're still here: overwrite the current transformation...
// Set a name on this generated transformation
//
String name = "Copy table from [" + sourceDBInfo.getName() + "] to [" + targetDBInfo.getName() + "]";
String transName = name;
int nr = 1;
if (spoon.delegates.trans.getTransformation(transName) != null) {
nr++;
transName = name + " " + nr;
}
meta.setName(transName);
spoon.delegates.trans.addTransGraph(meta);
spoon.refreshGraph();
spoon.refreshTree();
} finally {
sourceDB.disconnect();
}
} catch (Exception e) {
new ErrorDialog(spoon.getShell(), BaseMessages.getString(PKG, "Spoon.Dialog.UnexpectedError.Title"), BaseMessages.getString(PKG, "Spoon.Dialog.UnexpectedError.Message"), new KettleException(e.getMessage(), e));
return false;
}
return true;
}
use of org.pentaho.di.trans.steps.selectvalues.SelectMetadataChange in project pentaho-metaverse by pentaho.
the class SelectValuesStepAnalyzer method getChangeRecords.
@Override
public Set<ComponentDerivationRecord> getChangeRecords(SelectValuesMeta selectValuesMeta) throws MetaverseAnalyzerException {
validateState(null, selectValuesMeta);
Set<ComponentDerivationRecord> changeRecords = new HashSet<ComponentDerivationRecord>();
String inputFieldName;
String outputFieldName;
ComponentDerivationRecord changeRecord;
// Process the fields/tabs in the same order as the real step does
if (!Const.isEmpty(selectValuesMeta.getSelectName())) {
String[] fieldNames = selectValuesMeta.getSelectName();
String[] fieldRenames = selectValuesMeta.getSelectRename();
int[] fieldLength = selectValuesMeta.getSelectLength();
int[] fieldPrecision = selectValuesMeta.getSelectPrecision();
for (int i = 0; i < fieldNames.length; i++) {
inputFieldName = fieldNames[i];
outputFieldName = fieldRenames[i];
changeRecord = new ComponentDerivationRecord(inputFieldName, outputFieldName == null ? inputFieldName : outputFieldName, ChangeType.METADATA);
Set<String> metadataChangedFields = new HashSet<String>();
if (inputFieldName != null && outputFieldName != null && !inputFieldName.equals(outputFieldName)) {
metadataChangedFields.add("name");
}
// Check for changes in field length
if (fieldLength != null && fieldLength[i] != NOT_CHANGED) {
metadataChangedFields.add("length");
}
// Check for changes in field precision
if (fieldPrecision != null && fieldPrecision[i] != NOT_CHANGED) {
metadataChangedFields.add("precision");
}
if (!metadataChangedFields.isEmpty()) {
// Add all the changed metadata fields as a single operation
changeRecord.addOperation(new Operation(DictionaryConst.PROPERTY_MODIFIED, StringUtils.join(metadataChangedFields, ",")));
}
changeRecords.add(changeRecord);
}
}
if (!Const.isEmpty(selectValuesMeta.getMeta())) {
String[] prevStepNames = parentTransMeta.getPrevStepNames(getStepName());
SelectMetadataChange[] metadataChanges = selectValuesMeta.getMeta();
if (metadataChanges != null) {
for (SelectMetadataChange metadataChange : metadataChanges) {
inputFieldName = metadataChange.getName();
outputFieldName = metadataChange.getRename();
changeRecord = new ComponentDerivationRecord(inputFieldName, outputFieldName == null ? inputFieldName : outputFieldName, ChangeType.METADATA);
Set<String> metadataChangedFields = new HashSet<String>();
// NOTE: We use equalsIgnoreCase instead of equals because that's how Select Values currently works
if (inputFieldName != null && outputFieldName != null && !inputFieldName.equalsIgnoreCase(outputFieldName)) {
metadataChangedFields.add("name");
}
// Get the ValueMetaInterface for the input field, to determine if any of its metadata has changed
if (prevFields == null) {
prevFields = getInputFields(selectValuesMeta);
if (prevFields == null) {
log.warn(Messages.getString("WARNING.CannotDetermineFieldType", inputFieldName));
continue;
}
}
RowMetaInterface rowMetaInterface = prevFields.get(prevStepNames[0]);
ValueMetaInterface inputFieldValueMeta = null;
if (rowMetaInterface == null) {
log.warn(Messages.getString("WARNING.CannotDetermineFieldType", inputFieldName));
continue;
}
inputFieldValueMeta = rowMetaInterface.searchValueMeta(inputFieldName);
if (inputFieldValueMeta == null) {
log.warn(Messages.getString("WARNING.CannotDetermineFieldType", inputFieldName));
continue;
}
// Check for changes in field type
if (inputFieldValueMeta.getType() != metadataChange.getType()) {
metadataChangedFields.add("type");
}
// Check for changes in field length
if (metadataChange.getLength() != NOT_CHANGED) {
metadataChangedFields.add("length");
}
// Check for changes in field precision
if (metadataChange.getPrecision() != NOT_CHANGED) {
metadataChangedFields.add("precision");
}
// Check for changes in storage type (binary to string, e.g.)
if ((metadataChange.getStorageType() != -1) && (inputFieldValueMeta.getStorageType() != metadataChange.getStorageType())) {
metadataChangedFields.add("storagetype");
}
// Check for changes in conversion mask
if ((metadataChange.getConversionMask() != null) && (inputFieldValueMeta.getConversionMask() == null || !inputFieldValueMeta.getConversionMask().equals(metadataChange.getConversionMask()))) {
metadataChangedFields.add("conversionmask");
}
// Check for changes in date format leniency
if (inputFieldValueMeta.isDateFormatLenient() != metadataChange.isDateFormatLenient()) {
metadataChangedFields.add("dateformatlenient");
}
// Check for changes in date format locale
if ((metadataChange.getDateFormatLocale() != null) && (inputFieldValueMeta.getDateFormatLocale() == null || !inputFieldValueMeta.getDateFormatLocale().toString().equals(metadataChange.getDateFormatLocale()))) {
metadataChangedFields.add("datelocale");
}
// Check for changes in date format locale
if ((metadataChange.getDateFormatTimeZone() != null) && (inputFieldValueMeta.getDateFormatTimeZone() == null || !inputFieldValueMeta.getDateFormatTimeZone().toString().equals(metadataChange.getDateFormatTimeZone()))) {
metadataChangedFields.add("datetimezone");
}
// Check for changes in date format locale
if (inputFieldValueMeta.isLenientStringToNumber() != metadataChange.isLenientStringToNumber()) {
metadataChangedFields.add("lenientnumberconversion");
}
// Check for changes in encoding
if ((metadataChange.getDateFormatTimeZone() != null) && (inputFieldValueMeta.getStringEncoding() == null || !inputFieldValueMeta.getStringEncoding().equals(metadataChange.getDateFormatTimeZone()))) {
metadataChangedFields.add("encoding");
}
// Check for changes in encoding
if ((metadataChange.getDecimalSymbol() != null) && (inputFieldValueMeta.getDecimalSymbol() == null || !inputFieldValueMeta.getDecimalSymbol().equals(metadataChange.getDecimalSymbol()))) {
metadataChangedFields.add("decimalsymbol");
}
// Check for changes in encoding
if ((metadataChange.getGroupingSymbol() != null) && (inputFieldValueMeta.getGroupingSymbol() == null || !inputFieldValueMeta.getGroupingSymbol().equals(metadataChange.getGroupingSymbol()))) {
metadataChangedFields.add("groupsymbol");
}
// Check for changes in encoding
if ((metadataChange.getCurrencySymbol() != null) && (inputFieldValueMeta.getCurrencySymbol() == null || !inputFieldValueMeta.getCurrencySymbol().equals(metadataChange.getCurrencySymbol()))) {
metadataChangedFields.add("currencysymbol");
}
if (!metadataChangedFields.isEmpty()) {
// Add all the changed metadata fields as a single operation
changeRecord.addOperation(new Operation(DictionaryConst.PROPERTY_MODIFIED, StringUtils.join(metadataChangedFields, ",")));
}
changeRecords.add(changeRecord);
}
}
}
return changeRecords;
}
use of org.pentaho.di.trans.steps.selectvalues.SelectMetadataChange in project pdi-dataservice-server-plugin by pentaho.
the class SqlTransGenerator method generateConversionStep.
private StepMeta generateConversionStep() {
// Set conversion masks for each column, modify type where required.
SelectValuesMeta meta = new SelectValuesMeta();
meta.allocate(0, 0, serviceFields.size());
meta.setMeta(serviceFields.getValueMetaList().stream().map(valueMeta -> getSelectMetadataChange(meta, valueMeta)).collect(Collectors.toList()).toArray(new SelectMetadataChange[serviceFields.size()]));
StepMeta stepMeta = new StepMeta("Set Conversion", meta);
stepMeta.setLocation(xLocation, 50);
xLocation += 100;
stepMeta.setDraw(true);
return stepMeta;
}
use of org.pentaho.di.trans.steps.selectvalues.SelectMetadataChange in project pentaho-metaverse by pentaho.
the class SelectValuesStepAnalyzer method getUsedFields.
@Override
protected Set<StepField> getUsedFields(SelectValuesMeta meta) {
Set<StepField> usedFields = new HashSet<>();
String[] fieldNames = meta.getSelectName();
for (String fieldName : fieldNames) {
usedFields.addAll(createStepFields(fieldName, getInputs()));
}
SelectMetadataChange[] selectMetadataChanges = meta.getMeta();
for (SelectMetadataChange selectMetadataChange : selectMetadataChanges) {
usedFields.addAll(createStepFields(selectMetadataChange.getName(), getInputs()));
}
return usedFields;
}
use of org.pentaho.di.trans.steps.selectvalues.SelectMetadataChange in project pentaho-metaverse by pentaho.
the class SelectValuesStepAnalyzer method isPassthrough.
@Override
protected boolean isPassthrough(StepField originalFieldName) {
// there are no entries on the select tab AND the field is NOT on the deleted tab OR meta tab
if (ArrayUtils.isEmpty(baseStepMeta.getSelectName())) {
SelectMetadataChange[] changes = baseStepMeta.getMeta();
boolean isOnMetaTab = false;
for (int i = 0; i < changes.length; i++) {
SelectMetadataChange change = changes[i];
if (originalFieldName.getFieldName().equals(change.getName())) {
isOnMetaTab = true;
break;
}
}
boolean isDeleted = ArrayUtils.contains(baseStepMeta.getDeleteName(), originalFieldName.getFieldName());
return (!(isOnMetaTab || isDeleted));
}
return false;
}
Aggregations