Search in sources :

Example 11 with MarketoError

use of org.talend.components.marketo.runtime.client.type.MarketoError in project components by Talend.

the class MarketoSOAPClient method getLeadChanges.

@Override
public MarketoRecordResult getLeadChanges(TMarketoInputProperties parameters, String offset) {
    Schema schema = parameters.schemaInput.schema.getValue();
    Map<String, String> mappings = parameters.mappingInput.getNameMappingsForMarketo();
    int bSize = parameters.batchSize.getValue() > 100 ? 100 : parameters.batchSize.getValue();
    String sOldest = parameters.oldestCreateDate.getValue();
    String sLatest = parameters.latestCreateDate.getValue();
    LOG.debug("LeadChanges - from {} to {}.", sOldest, sLatest);
    // 
    // Create Request
    // 
    ParamsGetLeadChanges request = new ParamsGetLeadChanges();
    LastUpdateAtSelector leadSelector = new LastUpdateAtSelector();
    try {
        Date oldest = MarketoUtils.parseDateString(sOldest);
        Date latest = MarketoUtils.parseDateString(sLatest);
        GregorianCalendar gc = new GregorianCalendar();
        gc.setTime(latest);
        DatatypeFactory factory = newInstance();
        JAXBElement<XMLGregorianCalendar> until = objectFactory.createLastUpdateAtSelectorLatestUpdatedAt(factory.newXMLGregorianCalendar(gc));
        GregorianCalendar since = new GregorianCalendar();
        since.setTime(oldest);
        leadSelector.setOldestUpdatedAt(factory.newXMLGregorianCalendar(since));
        leadSelector.setLatestUpdatedAt(until);
        request.setLeadSelector(leadSelector);
        JAXBElement<XMLGregorianCalendar> oldestCreateAtValue = objectFactory.createStreamPositionOldestCreatedAt(factory.newXMLGregorianCalendar(since));
        StreamPosition sp = new StreamPosition();
        sp.setOldestCreatedAt(oldestCreateAtValue);
        if (offset != null && !offset.isEmpty()) {
            sp.setOffset(objectFactory.createStreamPositionOffset(offset));
        }
        request.setStartPosition(sp);
    } catch (ParseException | DatatypeConfigurationException e) {
        LOG.error("Error for LastUpdateAtSelector : {}.", e.getMessage());
        throw new ComponentException(e);
    }
    // attributes
    ArrayOfString attributes = new ArrayOfString();
    for (String s : mappings.values()) {
        attributes.getStringItems().add(s);
    }
    // Activity filter
    ActivityTypeFilter filter = new ActivityTypeFilter();
    if (parameters.setIncludeTypes.getValue()) {
        ArrayOfActivityType includes = new ArrayOfActivityType();
        for (String a : parameters.includeTypes.type.getValue()) {
            includes.getActivityTypes().add(fromValue(a));
        }
        filter.setIncludeTypes(includes);
    }
    if (parameters.setExcludeTypes.getValue()) {
        ArrayOfActivityType excludes = new ArrayOfActivityType();
        for (String a : parameters.excludeTypes.type.getValue()) {
            excludes.getActivityTypes().add(fromValue(a));
        }
        filter.setExcludeTypes(excludes);
    }
    JAXBElement<ActivityTypeFilter> typeFilter = objectFactory.createParamsGetLeadActivityActivityFilter(filter);
    request.setActivityFilter(typeFilter);
    // batch size
    JAXBElement<Integer> batchSize = objectFactory.createParamsGetMultipleLeadsBatchSize(bSize);
    request.setBatchSize(batchSize);
    // 
    // 
    // Request execution
    // 
    SuccessGetLeadChanges result = null;
    MarketoRecordResult mkto = new MarketoRecordResult();
    try {
        result = getPort().getLeadChanges(request, header);
        mkto.setSuccess(true);
    } catch (Exception e) {
        LOG.error("getLeadChanges error: {}.", e.getMessage());
        mkto.setSuccess(false);
        mkto.setRecordCount(0);
        mkto.setRemainCount(0);
        mkto.setErrors(Collections.singletonList(new MarketoError(SOAP, e.getMessage())));
        return mkto;
    }
    if (result == null || result.getResult().getReturnCount() == 0) {
        LOG.debug(MESSAGE_REQUEST_RETURNED_0_MATCHING_LEADS);
        mkto.setErrors(Collections.singletonList(new MarketoError(SOAP, MESSAGE_NO_LEADS_FOUND)));
        return mkto;
    }
    String streamPos = result.getResult().getNewStartPosition().getOffset().getValue();
    int recordCount = result.getResult().getReturnCount();
    int remainCount = result.getResult().getRemainingCount();
    // Process results
    List<IndexedRecord> results = convertLeadChangeRecords(result.getResult().getLeadChangeRecordList().getValue().getLeadChangeRecords(), schema, mappings);
    mkto.setRecordCount(recordCount);
    mkto.setRemainCount(remainCount);
    mkto.setStreamPosition(streamPos);
    mkto.setRecords(results);
    return mkto;
}
Also used : IndexedRecord(org.apache.avro.generic.IndexedRecord) Schema(org.apache.avro.Schema) ArrayOfString(com.marketo.mktows.ArrayOfString) ParamsGetLeadChanges(com.marketo.mktows.ParamsGetLeadChanges) MarketoError(org.talend.components.marketo.runtime.client.type.MarketoError) MarketoRecordResult(org.talend.components.marketo.runtime.client.type.MarketoRecordResult) LastUpdateAtSelector(com.marketo.mktows.LastUpdateAtSelector) LastUpdateAtSelector(org.talend.components.marketo.tmarketoinput.TMarketoInputProperties.LeadSelector.LastUpdateAtSelector) DatatypeFactory(javax.xml.datatype.DatatypeFactory) ArrayOfActivityType(com.marketo.mktows.ArrayOfActivityType) SuccessGetLeadChanges(com.marketo.mktows.SuccessGetLeadChanges) GregorianCalendar(java.util.GregorianCalendar) XMLGregorianCalendar(javax.xml.datatype.XMLGregorianCalendar) StreamPosition(com.marketo.mktows.StreamPosition) Date(java.util.Date) DatatypeConfigurationException(javax.xml.datatype.DatatypeConfigurationException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) WebServiceException(javax.xml.ws.WebServiceException) MarketoException(org.talend.components.marketo.runtime.client.type.MarketoException) ParseException(java.text.ParseException) JAXBException(javax.xml.bind.JAXBException) ComponentException(org.talend.components.api.exception.ComponentException) MalformedURLException(java.net.MalformedURLException) XMLGregorianCalendar(javax.xml.datatype.XMLGregorianCalendar) DatatypeConfigurationException(javax.xml.datatype.DatatypeConfigurationException) ArrayOfString(com.marketo.mktows.ArrayOfString) ComponentException(org.talend.components.api.exception.ComponentException) ParseException(java.text.ParseException) ActivityTypeFilter(com.marketo.mktows.ActivityTypeFilter)

Example 12 with MarketoError

use of org.talend.components.marketo.runtime.client.type.MarketoError in project components by Talend.

the class MarketoClientCustomObjectsTestIT method testGetCustomObjectsAllRecords.

@Test
public void testGetCustomObjectsAllRecords() throws Exception {
    MarketoSource source = new MarketoSource();
    source.initialize(null, irProps);
    MarketoRESTClient client = (MarketoRESTClient) source.getClientService(null);
    irProps.customObjectName.setValue(TEST_CO_NAME_SMARTPHONE);
    irProps.batchSize.setValue(500);
    irProps.schemaInput.schema.setValue(MarketoConstants.getCustomObjectRecordSchema());
    MarketoRecordResult result = client.getCustomObjects(irProps, null);
    assertFalse(result.isSuccess());
    assertEquals(0, result.getRecordCount());
    assertNotNull(result.getErrors());
    Object err = result.getErrors().get(0);
    assertTrue(err instanceof MarketoError);
    assertEquals("REST", ((MarketoError) err).getApiMode());
    assertEquals("1003", ((MarketoError) err).getCode());
    assertEquals("filterType not specified", ((MarketoError) err).getMessage());
}
Also used : MarketoSource(org.talend.components.marketo.runtime.MarketoSource) MarketoRecordResult(org.talend.components.marketo.runtime.client.type.MarketoRecordResult) MarketoError(org.talend.components.marketo.runtime.client.type.MarketoError) Test(org.junit.Test)

Example 13 with MarketoError

use of org.talend.components.marketo.runtime.client.type.MarketoError in project components by Talend.

the class RequestResult method getErrorsString.

public String getErrorsString() {
    StringBuilder errs = new StringBuilder("");
    for (MarketoError err : getErrors()) {
        errs.append("{");
        if (!StringUtils.isEmpty(err.getCode())) {
            errs.append("[").append(err.getCode()).append("] ");
        }
        errs.append(err.getMessage());
        errs.append("}");
    }
    return errs.toString();
}
Also used : MarketoError(org.talend.components.marketo.runtime.client.type.MarketoError)

Example 14 with MarketoError

use of org.talend.components.marketo.runtime.client.type.MarketoError in project components by Talend.

the class MarketoSOAPClient method syncLead.

/**
 * Request<br/>
 *
 * Field Name <br/>
 * <code>leadRecord->Id</code> Required – Only when Email or foreignSysPersonId is not present The Marketo Id of the
 * lead record<br/>
 * <code>leadRecord->Email</code> Required – Only when Id or foreignSysPersonId is not present The email address
 * associated with the lead record<br/>
 * <code>leadRecord->foreignSysPersonId</code> Required – Only when Id or Email is not present The foreign system id
 * associated with the lead record<br/>
 * <code>leadRecord->foreignSysType</code> Optional – Only required when foreignSysPersonId is present The type of
 * foreign system. Possible values: CUSTOM, SFDC, NETSUITE<br/>
 * <code>leadRecord->leadAttributeList->attribute->attrName</code> Required The name of the lead attribute you want to
 * update the value of.<br/>
 * <code>leadRecord->leadAttributeList->attribute->attrValue</code> Required The value you want to set to the lead
 * attribute specificed in attrName. returnLead Required When true will return the complete updated lead record upon
 * update.<br/>
 * <code>marketoCookie</code> Optional The Munchkin javascript cookie<br/>
 */
@Override
public MarketoSyncResult syncLead(TMarketoOutputProperties parameters, IndexedRecord lead) {
    MarketoSyncResult mkto = new MarketoSyncResult();
    try {
        ParamsSyncLead request = new ParamsSyncLead();
        request.setReturnLead(false);
        // 
        request.setLeadRecord(convertToLeadRecord(lead, parameters.mappingInput.getNameMappingsForMarketo()));
        MktowsContextHeader headerContext = new MktowsContextHeader();
        headerContext.setTargetWorkspace("default");
        SuccessSyncLead result = getPort().syncLead(request, header, headerContext);
        // 
        if (LOG.isDebugEnabled()) {
            try {
                JAXBContext context = JAXBContext.newInstance(SuccessSyncLead.class);
                Marshaller m = context.createMarshaller();
                m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
                m.marshal(result, System.out);
            } catch (JAXBException e) {
                LOG.error(e.getMessage());
            }
        }
        // 
        com.marketo.mktows.SyncStatus status = result.getResult().getSyncStatus();
        mkto.setSuccess(status.getError().isNil());
        if (mkto.isSuccess()) {
            mkto.setRecordCount(1);
            SyncStatus resultStatus = new SyncStatus(status.getLeadId(), status.getStatus().value());
            mkto.setRecords(Collections.singletonList(resultStatus));
        } else {
            mkto.setErrors(Collections.singletonList(new MarketoError(SOAP, status.getError().getValue())));
        }
    } catch (Exception e) {
        LOG.error(e.toString());
        mkto.setSuccess(false);
        mkto.setErrors(Collections.singletonList(new MarketoError(SOAP, e.getMessage())));
    }
    return mkto;
}
Also used : MktowsContextHeader(com.marketo.mktows.MktowsContextHeader) Marshaller(javax.xml.bind.Marshaller) ParamsSyncLead(com.marketo.mktows.ParamsSyncLead) JAXBException(javax.xml.bind.JAXBException) SyncStatus(org.talend.components.marketo.runtime.client.rest.type.SyncStatus) JAXBContext(javax.xml.bind.JAXBContext) DatatypeConfigurationException(javax.xml.datatype.DatatypeConfigurationException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) WebServiceException(javax.xml.ws.WebServiceException) MarketoException(org.talend.components.marketo.runtime.client.type.MarketoException) ParseException(java.text.ParseException) JAXBException(javax.xml.bind.JAXBException) ComponentException(org.talend.components.api.exception.ComponentException) MalformedURLException(java.net.MalformedURLException) MarketoSyncResult(org.talend.components.marketo.runtime.client.type.MarketoSyncResult) SuccessSyncLead(com.marketo.mktows.SuccessSyncLead) MarketoError(org.talend.components.marketo.runtime.client.type.MarketoError)

Example 15 with MarketoError

use of org.talend.components.marketo.runtime.client.type.MarketoError in project components by Talend.

the class MarketoSOAPClient method getLeadActivity.

@Override
public MarketoRecordResult getLeadActivity(TMarketoInputProperties parameters, String offset) {
    LOG.debug("MarketoSOAPClient.getLeadActivity with {}", parameters.leadKeyTypeSOAP.getValue());
    Schema schema = parameters.schemaInput.schema.getValue();
    Map<String, String> mappings = parameters.mappingInput.getNameMappingsForMarketo();
    int bSize = parameters.batchSize.getValue();
    String lkt = parameters.leadKeyTypeSOAP.getValue().toString();
    String lkv = parameters.leadKeyValue.getValue();
    // 
    // Create Request
    // 
    LOG.info("LeadKeySelector - Key type:  {} with value : {}.", lkt, lkv);
    ParamsGetLeadActivity request = new ParamsGetLeadActivity();
    LeadKey key = new LeadKey();
    key.setKeyType(valueOf(lkt));
    key.setKeyValue(lkv);
    request.setLeadKey(key);
    // attributes
    ArrayOfString attributes = new ArrayOfString();
    for (String s : mappings.values()) {
        attributes.getStringItems().add(s);
    }
    // Activity filter
    ActivityTypeFilter filter = new ActivityTypeFilter();
    if (parameters.setIncludeTypes.getValue()) {
        ArrayOfActivityType includes = new ArrayOfActivityType();
        for (String a : parameters.includeTypes.type.getValue()) {
            includes.getActivityTypes().add(fromValue(a));
        }
        filter.setIncludeTypes(includes);
    }
    if (parameters.setExcludeTypes.getValue()) {
        ArrayOfActivityType excludes = new ArrayOfActivityType();
        for (String a : parameters.excludeTypes.type.getValue()) {
            excludes.getActivityTypes().add(fromValue(a));
        }
        filter.setExcludeTypes(excludes);
    }
    JAXBElement<ActivityTypeFilter> typeFilter = objectFactory.createParamsGetLeadActivityActivityFilter(filter);
    request.setActivityFilter(typeFilter);
    // batch size
    JAXBElement<Integer> batchSize = objectFactory.createParamsGetMultipleLeadsBatchSize(bSize);
    request.setBatchSize(batchSize);
    // stream position
    if (offset != null && !offset.isEmpty()) {
        StreamPosition sposition = new StreamPosition();
        sposition.setOffset(objectFactory.createStreamPositionOffset(offset));
        JAXBElement<StreamPosition> position = objectFactory.createParamsGetLeadActivityStartPosition(sposition);
        request.setStartPosition(position);
    }
    // 
    // 
    // Request execution
    // 
    SuccessGetLeadActivity result = null;
    MarketoRecordResult mkto = new MarketoRecordResult();
    try {
        result = getPort().getLeadActivity(request, header);
        mkto.setSuccess(true);
    } catch (Exception e) {
        LOG.error("getLeadActivity error : {}.", e.getMessage());
        mkto.setSuccess(false);
        mkto.setRecordCount(0);
        mkto.setRemainCount(0);
        mkto.setErrors(Collections.singletonList(new MarketoError(SOAP, e.getMessage())));
        return mkto;
    }
    if (result == null || result.getLeadActivityList().getReturnCount() == 0) {
        LOG.debug(MESSAGE_REQUEST_RETURNED_0_MATCHING_LEADS);
        mkto.setErrors(Collections.singletonList(new MarketoError(SOAP, MESSAGE_NO_LEADS_FOUND)));
        return mkto;
    }
    String streamPos = result.getLeadActivityList().getNewStartPosition().getOffset().getValue();
    int recordCount = result.getLeadActivityList().getReturnCount();
    int remainCount = result.getLeadActivityList().getRemainingCount();
    // Process results
    List<IndexedRecord> results = convertLeadActivityRecords(result.getLeadActivityList().getActivityRecordList().getValue().getActivityRecords(), schema, mappings);
    mkto.setRecordCount(recordCount);
    mkto.setRemainCount(remainCount);
    mkto.setStreamPosition(streamPos);
    mkto.setRecords(results);
    return mkto;
}
Also used : IndexedRecord(org.apache.avro.generic.IndexedRecord) ArrayOfActivityType(com.marketo.mktows.ArrayOfActivityType) Schema(org.apache.avro.Schema) StreamPosition(com.marketo.mktows.StreamPosition) ArrayOfString(com.marketo.mktows.ArrayOfString) DatatypeConfigurationException(javax.xml.datatype.DatatypeConfigurationException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) WebServiceException(javax.xml.ws.WebServiceException) MarketoException(org.talend.components.marketo.runtime.client.type.MarketoException) ParseException(java.text.ParseException) JAXBException(javax.xml.bind.JAXBException) ComponentException(org.talend.components.api.exception.ComponentException) MalformedURLException(java.net.MalformedURLException) MarketoError(org.talend.components.marketo.runtime.client.type.MarketoError) SuccessGetLeadActivity(com.marketo.mktows.SuccessGetLeadActivity) ArrayOfString(com.marketo.mktows.ArrayOfString) LeadKey(com.marketo.mktows.LeadKey) ArrayOfLeadKey(com.marketo.mktows.ArrayOfLeadKey) ActivityTypeFilter(com.marketo.mktows.ActivityTypeFilter) ParamsGetLeadActivity(com.marketo.mktows.ParamsGetLeadActivity) MarketoRecordResult(org.talend.components.marketo.runtime.client.type.MarketoRecordResult)

Aggregations

MarketoError (org.talend.components.marketo.runtime.client.type.MarketoError)27 MarketoRecordResult (org.talend.components.marketo.runtime.client.type.MarketoRecordResult)16 Test (org.junit.Test)12 MarketoException (org.talend.components.marketo.runtime.client.type.MarketoException)12 IndexedRecord (org.apache.avro.generic.IndexedRecord)8 MalformedURLException (java.net.MalformedURLException)7 InvalidKeyException (java.security.InvalidKeyException)7 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)7 ParseException (java.text.ParseException)7 ArrayList (java.util.ArrayList)7 JAXBException (javax.xml.bind.JAXBException)7 DatatypeConfigurationException (javax.xml.datatype.DatatypeConfigurationException)7 WebServiceException (javax.xml.ws.WebServiceException)7 ComponentException (org.talend.components.api.exception.ComponentException)7 MarketoSyncResult (org.talend.components.marketo.runtime.client.type.MarketoSyncResult)6 ArrayOfString (com.marketo.mktows.ArrayOfString)5 List (java.util.List)4 Schema (org.apache.avro.Schema)4 SyncStatus (org.talend.components.marketo.runtime.client.rest.type.SyncStatus)4 TMarketoBulkExecProperties (org.talend.components.marketo.tmarketobulkexec.TMarketoBulkExecProperties)4