Search in sources :

Example 1 with FieldType

use of com.sforce.soap.partner.FieldType in project pentaho-kettle by pentaho.

the class SalesforceInsertDialog method generateMappings.

/**
 * Reads in the fields from the previous steps and from the ONE next step and opens an EnterMappingDialog with this
 * information. After the user did the mapping, those information is put into the Select/Rename table.
 */
private void generateMappings() {
    if (!checkInput()) {
        return;
    }
    // Determine the source and target fields...
    // 
    RowMetaInterface sourceFields;
    RowMetaInterface targetFields = new RowMeta();
    try {
        sourceFields = transMeta.getPrevStepFields(stepMeta);
    } catch (KettleException e) {
        new ErrorDialog(shell, BaseMessages.getString(PKG, "SalesforceInsertDialog.DoMapping.UnableToFindSourceFields.Title"), BaseMessages.getString(PKG, "SalesforceInsertDialog.DoMapping.UnableToFindSourceFields.Message"), e);
        return;
    }
    try {
        SalesforceConnection connection = getConnection();
        Field[] fields = connection.getObjectFields(transMeta.environmentSubstitute(wModule.getText()));
        String[] fieldNames = connection.getFields(fields);
        FieldType dateType = FieldType.date;
        for (int i = 0; i < fields.length; i++) {
            if (dateType.equals(fields[i].getType())) {
                // Mark date columns as TYPE_DATE to strip time part later
                targetFields.addValueMeta(ValueMetaFactory.createValueMeta(fieldNames[i], ValueMetaInterface.TYPE_DATE));
            } else {
                targetFields.addValueMeta(new ValueMetaNone(fieldNames[i]));
            }
        }
    } catch (Exception e) {
        new ErrorDialog(shell, BaseMessages.getString(PKG, "SalesforceInsertDialog.DoMapping.UnableToFindTargetFields.Title"), BaseMessages.getString(PKG, "SalesforceInsertDialog.DoMapping.UnableToFindTargetFields.Message"), e);
        return;
    }
    String[] inputNames = new String[sourceFields.size()];
    for (int i = 0; i < sourceFields.size(); i++) {
        ValueMetaInterface value = sourceFields.getValueMeta(i);
        inputNames[i] = value.getName() + EnterMappingDialog.STRING_ORIGIN_SEPARATOR + value.getOrigin() + ")";
    }
    // Create the existing mapping list...
    // 
    List<SourceToTargetMapping> mappings = new ArrayList<SourceToTargetMapping>();
    StringBuffer missingSourceFields = new StringBuffer();
    StringBuffer missingTargetFields = new StringBuffer();
    int nrFields = wReturn.nrNonEmpty();
    for (int i = 0; i < nrFields; i++) {
        TableItem item = wReturn.getNonEmpty(i);
        String source = item.getText(2);
        String target = item.getText(1);
        int sourceIndex = sourceFields.indexOfValue(source);
        if (sourceIndex < 0) {
            missingSourceFields.append(Const.CR + "   " + source + " --> " + target);
        }
        int targetIndex = targetFields.indexOfValue(target);
        if (targetIndex < 0) {
            missingTargetFields.append(Const.CR + "   " + source + " --> " + target);
        }
        if (sourceIndex < 0 || targetIndex < 0) {
            continue;
        }
        SourceToTargetMapping mapping = new SourceToTargetMapping(sourceIndex, targetIndex);
        mappings.add(mapping);
    }
    // 
    if (missingSourceFields.length() > 0 || missingTargetFields.length() > 0) {
        String message = "";
        if (missingSourceFields.length() > 0) {
            message += BaseMessages.getString(PKG, "SalesforceInsertDialog.DoMapping.SomeSourceFieldsNotFound", missingSourceFields.toString()) + Const.CR;
        }
        if (missingTargetFields.length() > 0) {
            message += BaseMessages.getString(PKG, "SalesforceInsertDialog.DoMapping.SomeTargetFieldsNotFound", missingSourceFields.toString()) + Const.CR;
        }
        message += Const.CR;
        message += BaseMessages.getString(PKG, "SalesforceInsertDialog.DoMapping.SomeFieldsNotFoundContinue") + Const.CR;
        MessageDialog.setDefaultImage(GUIResource.getInstance().getImageSpoon());
        boolean goOn = MessageDialog.openConfirm(shell, BaseMessages.getString(PKG, "SalesforceInsertDialog.DoMapping.SomeFieldsNotFoundTitle"), message);
        if (!goOn) {
            return;
        }
    }
    EnterMappingDialog d = new EnterMappingDialog(SalesforceInsertDialog.this.shell, sourceFields.getFieldNames(), targetFields.getFieldNames(), mappings);
    mappings = d.open();
    // 
    if (mappings != null) {
        // Clear and re-populate!
        // 
        wReturn.table.removeAll();
        wReturn.table.setItemCount(mappings.size());
        for (int i = 0; i < mappings.size(); i++) {
            SourceToTargetMapping mapping = mappings.get(i);
            TableItem item = wReturn.table.getItem(i);
            item.setText(2, sourceFields.getValueMeta(mapping.getSourcePosition()).getName());
            item.setText(1, targetFields.getValueMeta(mapping.getTargetPosition()).getName());
        }
        wReturn.setRowNums();
        wReturn.optWidth(true);
    }
}
Also used : KettleException(org.pentaho.di.core.exception.KettleException) RowMeta(org.pentaho.di.core.row.RowMeta) EnterMappingDialog(org.pentaho.di.ui.core.dialog.EnterMappingDialog) TableItem(org.eclipse.swt.widgets.TableItem) ArrayList(java.util.ArrayList) ErrorDialog(org.pentaho.di.ui.core.dialog.ErrorDialog) RowMetaInterface(org.pentaho.di.core.row.RowMetaInterface) SalesforceConnection(org.pentaho.di.trans.steps.salesforce.SalesforceConnection) KettleException(org.pentaho.di.core.exception.KettleException) FieldType(com.sforce.soap.partner.FieldType) ValueMetaInterface(org.pentaho.di.core.row.ValueMetaInterface) Field(com.sforce.soap.partner.Field) ValueMetaNone(org.pentaho.di.core.row.value.ValueMetaNone) SourceToTargetMapping(org.pentaho.di.core.SourceToTargetMapping)

Example 2 with FieldType

use of com.sforce.soap.partner.FieldType in project teiid by teiid.

the class SalesForceMetadataProcessor method addColumns.

private boolean addColumns(DescribeSObjectResult objectMetadata, Table table) {
    boolean hasUpdateableColumn = false;
    Field[] fields = objectMetadata.getFields();
    for (Field field : fields) {
        String normalizedName = field.getName();
        if (normalizeNames) {
            normalizedName = NameUtil.normalizeName(normalizedName);
        }
        FieldType fieldType = field.getType();
        if (!isModelAuditFields() && isAuditField(field.getName())) {
            continue;
        }
        String sfTypeName = fieldType.name();
        Column column = null;
        switch(fieldType) {
            case string:
            case combobox:
            case reference:
            case phone:
            case id:
            case url:
            case email:
            case encryptedstring:
            case anyType:
                column = metadataFactory.addColumn(normalizedName, DataTypeManager.DefaultDataTypes.STRING, table);
                column.setNativeType(sfTypeName);
                if (sfTypeName.equals(FieldType.id.name())) {
                    column.setNullType(NullType.No_Nulls);
                    ArrayList<String> columnNames = new ArrayList<String>();
                    columnNames.add(field.getName());
                    // $NON-NLS-1$
                    metadataFactory.addPrimaryKey(field.getName() + "_PK", columnNames, table);
                }
                break;
            case picklist:
                column = metadataFactory.addColumn(normalizedName, DataTypeManager.DefaultDataTypes.STRING, table);
                if (field.isRestrictedPicklist()) {
                    // $NON-NLS-1$
                    column.setNativeType("restrictedpicklist");
                } else {
                    column.setNativeType(sfTypeName);
                }
                column.setProperty(COLUMN_PICKLIST_VALUES, getPicklistValues(field));
                break;
            case multipicklist:
                column = metadataFactory.addColumn(normalizedName, DataTypeManager.DefaultDataTypes.STRING, table);
                if (field.isRestrictedPicklist()) {
                    // $NON-NLS-1$
                    column.setNativeType("restrictedmultiselectpicklist");
                } else {
                    column.setNativeType(sfTypeName);
                }
                column.setProperty(COLUMN_PICKLIST_VALUES, getPicklistValues(field));
                break;
            case base64:
                column = metadataFactory.addColumn(normalizedName, DataTypeManager.DefaultDataTypes.BLOB, table);
                column.setNativeType(sfTypeName);
                break;
            case _boolean:
                column = metadataFactory.addColumn(normalizedName, DataTypeManager.DefaultDataTypes.BOOLEAN, table);
                column.setNativeType(sfTypeName);
                break;
            case currency:
                column = metadataFactory.addColumn(normalizedName, DataTypeManager.DefaultDataTypes.DOUBLE, table);
                column.setNativeType(sfTypeName);
                column.setCurrency(true);
                column.setScale(field.getScale());
                column.setPrecision(field.getPrecision());
                break;
            case textarea:
                column = metadataFactory.addColumn(normalizedName, DataTypeManager.DefaultDataTypes.STRING, table);
                column.setNativeType(sfTypeName);
                column.setSearchType(SearchType.Unsearchable);
                break;
            case _int:
                column = metadataFactory.addColumn(normalizedName, DataTypeManager.DefaultDataTypes.INTEGER, table);
                column.setNativeType(sfTypeName);
                column.setPrecision(field.getPrecision());
                break;
            case _double:
            case percent:
                column = metadataFactory.addColumn(normalizedName, DataTypeManager.DefaultDataTypes.DOUBLE, table);
                column.setNativeType(sfTypeName);
                column.setScale(field.getScale());
                column.setPrecision(field.getPrecision());
                break;
            case date:
                column = metadataFactory.addColumn(normalizedName, DataTypeManager.DefaultDataTypes.DATE, table);
                column.setNativeType(sfTypeName);
                break;
            case datetime:
                column = metadataFactory.addColumn(normalizedName, DataTypeManager.DefaultDataTypes.TIMESTAMP, table);
                column.setNativeType(sfTypeName);
                break;
            case time:
                column = metadataFactory.addColumn(normalizedName, DataTypeManager.DefaultDataTypes.TIME, table);
                column.setNativeType(sfTypeName);
                break;
            default:
                if (sfTypeName.equals("address")) {
                    // $NON-NLS-1$
                    // $NON-NLS-1$
                    LogManager.logDetail(LogConstants.CTX_CONNECTOR, "Ignoring composite address field", normalizedName);
                } else {
                    LogManager.logWarning(LogConstants.CTX_CONNECTOR, SalesForcePlugin.Util.gs(SalesForcePlugin.Event.TEIID13001, sfTypeName));
                }
                continue;
        }
        column.setNameInSource(field.getName());
        column.setLength(field.getLength());
        if (field.isUpdateable() || field.isCreateable()) {
            column.setUpdatable(true);
            hasUpdateableColumn = true;
        }
        column.setProperty(COLUMN_CALCULATED, String.valueOf(field.isCalculated()));
        column.setProperty(COLUMN_CUSTOM, String.valueOf(field.isCustom()));
        column.setProperty(COLUMN_DEFAULTED, String.valueOf(field.isDefaultedOnCreate()));
        if (field.isDefaultedOnCreate()) {
            // $NON-NLS-1$
            column.setDefaultValue("sf default");
        }
        column.setNullType(field.isNillable() ? NullType.Nullable : NullType.No_Nulls);
    }
    return hasUpdateableColumn;
}
Also used : Field(com.sforce.soap.partner.Field) Column(org.teiid.metadata.Column) ArrayList(java.util.ArrayList) FieldType(com.sforce.soap.partner.FieldType)

Aggregations

Field (com.sforce.soap.partner.Field)2 FieldType (com.sforce.soap.partner.FieldType)2 ArrayList (java.util.ArrayList)2 TableItem (org.eclipse.swt.widgets.TableItem)1 SourceToTargetMapping (org.pentaho.di.core.SourceToTargetMapping)1 KettleException (org.pentaho.di.core.exception.KettleException)1 RowMeta (org.pentaho.di.core.row.RowMeta)1 RowMetaInterface (org.pentaho.di.core.row.RowMetaInterface)1 ValueMetaInterface (org.pentaho.di.core.row.ValueMetaInterface)1 ValueMetaNone (org.pentaho.di.core.row.value.ValueMetaNone)1 SalesforceConnection (org.pentaho.di.trans.steps.salesforce.SalesforceConnection)1 EnterMappingDialog (org.pentaho.di.ui.core.dialog.EnterMappingDialog)1 ErrorDialog (org.pentaho.di.ui.core.dialog.ErrorDialog)1 Column (org.teiid.metadata.Column)1