Search in sources :

Example 1 with XmlObject

use of com.sforce.ws.bind.XmlObject in project pentaho-kettle by pentaho.

the class SalesforceConnectionTest method testMessageElements.

@Test
public void testMessageElements() throws Exception {
    XmlObject me = SalesforceConnection.fromTemplateElement("myName", 123, false);
    assertNotNull(me);
    assertEquals("myName", me.getName().getLocalPart());
    assertNull(me.getValue());
    me = null;
    me = SalesforceConnection.fromTemplateElement("myName", 123, true);
    assertNotNull(me);
    assertEquals("myName", me.getName().getLocalPart());
    assertEquals(123, me.getValue());
    me = null;
    me = SalesforceConnection.createMessageElement("myName", 123, false);
    assertNotNull(me);
    assertEquals("myName", me.getName().getLocalPart());
    assertEquals(123, me.getValue());
    me = null;
    try {
        me = SalesforceConnection.createMessageElement("myName", 123, true);
        fail();
    } catch (Exception expected) {
    // Ignore, name was expected to have a colon ':'
    }
    me = null;
    try {
        me = SalesforceConnection.createMessageElement("myType:Name", "123", true);
        assertNotNull(me);
        assertEquals("Name", me.getName().getLocalPart());
        assertEquals("myType", me.getField("type"));
        assertEquals("123", me.getField("Name"));
    } catch (Exception expected) {
        fail();
    }
    me = null;
    try {
        me = SalesforceConnection.createMessageElement("myType:Name/MyLookupField", 123, true);
        assertNotNull(me);
        assertEquals("MyLookupField", me.getName().getLocalPart());
        // assertEquals( "myType", me.getField( "type" ) );
        assertEquals(123, me.getField("Name"));
    } catch (Exception expected) {
        fail();
    }
}
Also used : XmlObject(com.sforce.ws.bind.XmlObject) KettleException(org.pentaho.di.core.exception.KettleException) ConnectionException(com.sforce.ws.ConnectionException) Test(org.junit.Test)

Example 2 with XmlObject

use of com.sforce.ws.bind.XmlObject in project pentaho-kettle by pentaho.

the class SalesforceConnection method getMessageElementForHierarchy.

/**
 * Drill down the SObject hierarchy based on the given field hierarchy until either null or the correct MessageElement
 * is found
 */
private XmlObject getMessageElementForHierarchy(SObject con, String[] fieldHierarchy) {
    final int lastIndex = fieldHierarchy.length - 1;
    SObject currentSObject = con;
    for (int index = 0; index <= lastIndex; index++) {
        for (XmlObject element : getChildren(currentSObject)) {
            if (element.getName().getLocalPart().equals(fieldHierarchy[index])) {
                if (index == lastIndex) {
                    return element;
                } else {
                    if (element instanceof SObject) {
                        // Found the next level, keep going
                        currentSObject = (SObject) element;
                    }
                    break;
                }
            }
        }
    }
    return null;
}
Also used : SObject(com.sforce.soap.partner.sobject.SObject) XmlObject(com.sforce.ws.bind.XmlObject)

Example 3 with XmlObject

use of com.sforce.ws.bind.XmlObject in project pentaho-kettle by pentaho.

the class SalesforceConnection method createForeignKeyElement.

private static XmlObject createForeignKeyElement(String type, String lookupField, String extIdName, Object extIdValue) throws Exception {
    // Foreign key relationship to the object
    XmlObject me = fromTemplateElement(lookupField, null, false);
    me.addField("type", type);
    me.addField(extIdName, extIdValue);
    return me;
}
Also used : XmlObject(com.sforce.ws.bind.XmlObject)

Example 4 with XmlObject

use of com.sforce.ws.bind.XmlObject in project pentaho-kettle by pentaho.

the class SalesforceConnection method createMessageElement.

public static XmlObject createMessageElement(String name, Object value, boolean useExternalKey) throws Exception {
    XmlObject me = null;
    if (useExternalKey) {
        // We use an external key
        // the structure should be like this :
        // object:externalId/lookupField
        // where
        // object is the type of the object
        // externalId is the name of the field in the object to resolve the value
        // lookupField is the name of the field in the current object to update (is the "__r" version)
        int indexOfType = name.indexOf(":");
        if (indexOfType > 0) {
            String type = name.substring(0, indexOfType);
            String extIdName = null;
            String lookupField = null;
            String rest = name.substring(indexOfType + 1, name.length());
            int indexOfExtId = rest.indexOf("/");
            if (indexOfExtId > 0) {
                extIdName = rest.substring(0, indexOfExtId);
                lookupField = rest.substring(indexOfExtId + 1, rest.length());
            } else {
                extIdName = rest;
                lookupField = extIdName;
            }
            me = createForeignKeyElement(type, lookupField, extIdName, value);
        } else {
            throw new KettleException(BaseMessages.getString(PKG, "SalesforceConnection.UnableToFindObjectType"));
        }
    } else {
        me = fromTemplateElement(name, value, true);
    }
    return me;
}
Also used : KettleException(org.pentaho.di.core.exception.KettleException) XmlObject(com.sforce.ws.bind.XmlObject)

Example 5 with XmlObject

use of com.sforce.ws.bind.XmlObject in project pentaho-kettle by pentaho.

the class SalesforceConnection method getChildren.

public static XmlObject[] getChildren(SObject object) {
    List<String> reservedFieldNames = Arrays.asList("type", "fieldsToNull");
    if (object == null) {
        return null;
    }
    List<XmlObject> children = new ArrayList<>();
    Iterator<XmlObject> iterator = object.getChildren();
    while (iterator.hasNext()) {
        XmlObject child = iterator.next();
        if (child.getName().getNamespaceURI().equals(Constants.PARTNER_SOBJECT_NS) && reservedFieldNames.contains(child.getName().getLocalPart())) {
            continue;
        }
        children.add(child);
    }
    if (children.size() == 0) {
        return null;
    }
    return children.toArray(new XmlObject[children.size()]);
}
Also used : ArrayList(java.util.ArrayList) XmlObject(com.sforce.ws.bind.XmlObject)

Aggregations

XmlObject (com.sforce.ws.bind.XmlObject)30 SObject (com.sforce.soap.partner.sobject.SObject)15 Test (org.junit.Test)11 ArrayList (java.util.ArrayList)6 KettleException (org.pentaho.di.core.exception.KettleException)6 LinkedHashSet (java.util.LinkedHashSet)5 QName (javax.xml.namespace.QName)5 ValueMetaInterface (org.pentaho.di.core.row.ValueMetaInterface)4 VisibleForTesting (com.google.common.annotations.VisibleForTesting)3 KettleStepException (org.pentaho.di.core.exception.KettleStepException)3 RowMeta (org.pentaho.di.core.row.RowMeta)3 Schema (org.apache.avro.Schema)2 ValueMetaBase (org.pentaho.di.core.row.value.ValueMetaBase)2 ValueMetaInteger (org.pentaho.di.core.row.value.ValueMetaInteger)2 Column (org.teiid.metadata.Column)2 Field (com.sforce.soap.partner.Field)1 ConnectionException (com.sforce.ws.ConnectionException)1 Time (com.sforce.ws.types.Time)1 Method (java.lang.reflect.Method)1 DateFormat (java.text.DateFormat)1