Search in sources :

Example 1 with SalesforceConnection

use of org.apache.hop.pipeline.transforms.salesforce.SalesforceConnection in project hop by apache.

the class SalesforceInputDialog method get.

private void get() {
    SalesforceConnection connection = null;
    try {
        SalesforceInputMeta meta = new SalesforceInputMeta();
        getInfo(meta);
        // Clear Fields Grid
        wFields.removeAll();
        // get real values
        String realModule = variables.resolve(meta.getModule());
        String realURL = variables.resolve(meta.getTargetUrl());
        String realUsername = variables.resolve(meta.getUsername());
        String realPassword = Utils.resolvePassword(variables, meta.getPassword());
        int realTimeOut = Const.toInt(variables.resolve(meta.getTimeout()), 0);
        connection = new SalesforceConnection(log, realURL, realUsername, realPassword);
        connection.setTimeOut(realTimeOut);
        String[] fieldsName = null;
        if (meta.isSpecifyQuery()) {
            // Free hand SOQL
            String realQuery = variables.resolve(meta.getQuery());
            connection.setSQL(realQuery);
            connection.connect();
            // We are connected, so let's query
            XmlObject[] fields = connection.getElements();
            int nrFields = fields.length;
            Set<String> fieldNames = new HashSet<>();
            for (int i = 0; i < nrFields; i++) {
                addFields("", fieldNames, fields[i]);
            }
            fieldsName = fieldNames.toArray(new String[fieldNames.size()]);
        } else {
            connection.connect();
            Field[] fields = connection.getObjectFields(realModule);
            fieldsName = new String[fields.length];
            for (int i = 0; i < fields.length; i++) {
                Field field = fields[i];
                fieldsName[i] = field.getName();
                addField(field);
            }
        }
        if (fieldsName != null) {
            columns[1].setComboValues(fieldsName);
        }
        wFields.removeEmptyRows();
        wFields.setRowNums();
        wFields.optWidth(true);
    } catch (HopException e) {
        new ErrorDialog(shell, BaseMessages.getString(PKG, "SalesforceInputMeta.ErrorRetrieveData.DialogTitle"), BaseMessages.getString(PKG, "SalesforceInputMeta.ErrorRetrieveData.DialogMessage"), e);
    } catch (Exception e) {
        new ErrorDialog(shell, BaseMessages.getString(PKG, "SalesforceInputMeta.ErrorRetrieveData.DialogTitle"), BaseMessages.getString(PKG, "SalesforceInputMeta.ErrorRetrieveData.DialogMessage"), e);
    } finally {
        if (connection != null) {
            try {
                connection.close();
            } catch (Exception e) {
            // Ignore errors
            }
        }
    }
}
Also used : HopException(org.apache.hop.core.exception.HopException) SalesforceConnection(org.apache.hop.pipeline.transforms.salesforce.SalesforceConnection) HopException(org.apache.hop.core.exception.HopException) Field(com.sforce.soap.partner.Field) XmlObject(com.sforce.ws.bind.XmlObject) HashSet(java.util.HashSet)

Example 2 with SalesforceConnection

use of org.apache.hop.pipeline.transforms.salesforce.SalesforceConnection in project hop by apache.

the class SalesforceInputDialog method getModulesList.

private void getModulesList() {
    if (!gotModule) {
        SalesforceConnection connection = null;
        String selectedField = wModule.getText();
        wModule.removeAll();
        try {
            SalesforceInputMeta meta = new SalesforceInputMeta();
            getInfo(meta);
            String url = variables.resolve(meta.getTargetUrl());
            // Define a new Salesforce connection
            connection = new SalesforceConnection(log, url, variables.resolve(meta.getUsername()), Utils.resolvePassword(variables, meta.getPassword()));
            // connect to Salesforce
            connection.connect();
            // retrieve modules list
            String[] modules = connection.getAllAvailableObjects(true);
            if (modules != null && modules.length > 0) {
                // populate Combo
                wModule.setItems(modules);
            }
            gotModule = true;
            getModulesListError = false;
        } catch (Exception e) {
            new ErrorDialog(shell, BaseMessages.getString(PKG, "SalesforceInputDialog.ErrorRetrieveModules.DialogTitle"), BaseMessages.getString(PKG, "SalesforceInputDialog.ErrorRetrieveData.ErrorRetrieveModules"), e);
            getModulesListError = true;
        } finally {
            if (!Utils.isEmpty(selectedField)) {
                wModule.setText(selectedField);
            }
            if (connection != null) {
                try {
                    connection.close();
                } catch (Exception e) {
                /* Ignore */
                }
            }
        }
    }
}
Also used : SalesforceConnection(org.apache.hop.pipeline.transforms.salesforce.SalesforceConnection) HopException(org.apache.hop.core.exception.HopException)

Example 3 with SalesforceConnection

use of org.apache.hop.pipeline.transforms.salesforce.SalesforceConnection in project hop by apache.

the class SalesforceInsertDialog method generateMappings.

/**
 * Reads in the fields from the previous transforms and from the ONE next transform 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...
    // 
    IRowMeta sourceFields;
    IRowMeta targetFields = new RowMeta();
    try {
        sourceFields = pipelineMeta.getPrevTransformFields(variables, transformMeta);
    } catch (HopException 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(variables.resolve(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], IValueMeta.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++) {
        IValueMeta value = sourceFields.getValueMeta(i);
        inputNames[i] = value.getName();
    }
    // Create the existing mapping list...
    // 
    List<SourceToTargetMapping> mappings = new ArrayList<>();
    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;
        int answer = BaseDialog.openMessageBox(shell, BaseMessages.getString(PKG, "SalesforceInsertDialog.DoMapping.SomeFieldsNotFoundTitle"), message, SWT.ICON_QUESTION | SWT.OK | SWT.CANCEL);
        boolean goOn = (answer & SWT.OK) != 0;
        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 : RowMeta(org.apache.hop.core.row.RowMeta) IRowMeta(org.apache.hop.core.row.IRowMeta) HopException(org.apache.hop.core.exception.HopException) IRowMeta(org.apache.hop.core.row.IRowMeta) EnterMappingDialog(org.apache.hop.ui.core.dialog.EnterMappingDialog) ErrorDialog(org.apache.hop.ui.core.dialog.ErrorDialog) SalesforceConnection(org.apache.hop.pipeline.transforms.salesforce.SalesforceConnection) HopException(org.apache.hop.core.exception.HopException) FieldType(com.sforce.soap.partner.FieldType) Field(com.sforce.soap.partner.Field) IValueMeta(org.apache.hop.core.row.IValueMeta) ValueMetaNone(org.apache.hop.core.row.value.ValueMetaNone) SourceToTargetMapping(org.apache.hop.core.SourceToTargetMapping)

Example 4 with SalesforceConnection

use of org.apache.hop.pipeline.transforms.salesforce.SalesforceConnection in project hop by apache.

the class SalesforceInsertDialog method getFieldNames.

private String[] getFieldNames() throws HopException {
    SalesforceConnection connection = null;
    String selectedModule = variables.resolve(wModule.getText());
    try {
        // Define a new Salesforce connection
        connection = getConnection();
        // return fieldsname for the module
        return connection.getFields(selectedModule);
    } catch (Exception e) {
        throw new HopException("Error getting fields from module [" + selectedModule + "]!", e);
    } finally {
        if (connection != null) {
            try {
                connection.close();
            } catch (Exception e) {
            // Ignore close errors
            }
        }
    }
}
Also used : HopException(org.apache.hop.core.exception.HopException) SalesforceConnection(org.apache.hop.pipeline.transforms.salesforce.SalesforceConnection) HopException(org.apache.hop.core.exception.HopException)

Example 5 with SalesforceConnection

use of org.apache.hop.pipeline.transforms.salesforce.SalesforceConnection in project hop by apache.

the class SalesforceInsertDialog method getModulesList.

private void getModulesList() {
    if (!gotModule) {
        SalesforceConnection connection = null;
        try {
            String selectedField = wModule.getText();
            wModule.removeAll();
            connection = getConnection();
            // return
            wModule.setItems(connection.getAllAvailableObjects(false));
            if (!Utils.isEmpty(selectedField)) {
                wModule.setText(selectedField);
            }
            gotModule = true;
            getModulesListError = false;
        } catch (Exception e) {
            new ErrorDialog(shell, BaseMessages.getString(PKG, "SalesforceInsertDialog.ErrorRetrieveModules.DialogTitle"), BaseMessages.getString(PKG, "SalesforceInsertDialog.ErrorRetrieveData.ErrorRetrieveModules"), e);
            getModulesListError = true;
        } finally {
            if (connection != null) {
                try {
                    connection.close();
                } catch (Exception e) {
                /* Ignore */
                }
            }
        }
    }
}
Also used : ErrorDialog(org.apache.hop.ui.core.dialog.ErrorDialog) SalesforceConnection(org.apache.hop.pipeline.transforms.salesforce.SalesforceConnection) HopException(org.apache.hop.core.exception.HopException)

Aggregations

SalesforceConnection (org.apache.hop.pipeline.transforms.salesforce.SalesforceConnection)11 HopException (org.apache.hop.core.exception.HopException)10 ErrorDialog (org.apache.hop.ui.core.dialog.ErrorDialog)5 Field (com.sforce.soap.partner.Field)2 FieldType (com.sforce.soap.partner.FieldType)1 XmlObject (com.sforce.ws.bind.XmlObject)1 HashSet (java.util.HashSet)1 SourceToTargetMapping (org.apache.hop.core.SourceToTargetMapping)1 IRowMeta (org.apache.hop.core.row.IRowMeta)1 IValueMeta (org.apache.hop.core.row.IValueMeta)1 RowMeta (org.apache.hop.core.row.RowMeta)1 ValueMetaNone (org.apache.hop.core.row.value.ValueMetaNone)1 EnterMappingDialog (org.apache.hop.ui.core.dialog.EnterMappingDialog)1 Cursor (org.eclipse.swt.graphics.Cursor)1