Search in sources :

Example 1 with LastUpdateAtSelector

use of com.marketo.mktows.LastUpdateAtSelector 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 2 with LastUpdateAtSelector

use of com.marketo.mktows.LastUpdateAtSelector in project components by Talend.

the class MarketoSOAPClient method getMultipleLeads.

/**
 * In getMultipleLeadsJSON you have to add includeAttributes base fields like Email. Otherwise, they return null from
 * API. WTF ?!? It's like that...
 */
@Override
public MarketoRecordResult getMultipleLeads(TMarketoInputProperties parameters, String offset) {
    LOG.debug("MarketoSOAPClient.getMultipleLeadsJSON with {}", parameters.leadSelectorSOAP.getValue());
    Schema schema = parameters.schemaInput.schema.getValue();
    Map<String, String> mappings = parameters.mappingInput.getNameMappingsForMarketo();
    int bSize = parameters.batchSize.getValue();
    // 
    // Create Request
    // 
    ParamsGetMultipleLeads request = new ParamsGetMultipleLeads();
    // 
    if (parameters.leadSelectorSOAP.getValue().equals(LeadKeySelector)) {
        LOG.info("LeadKeySelector - Key type:  {} with value : {}.", parameters.leadKeyTypeSOAP.getValue().toString(), parameters.leadKeyValues.getValue());
        LeadKeySelector keySelector = new LeadKeySelector();
        keySelector.setKeyType(valueOf(parameters.leadKeyTypeSOAP.getValue().toString()));
        ArrayOfString aos = new ArrayOfString();
        String[] keys = parameters.leadKeyValues.getValue().split("(,|;|\\s)");
        for (String s : keys) {
            LOG.debug("Adding leadKeyValue : {}.", s);
            aos.getStringItems().add(s);
        }
        keySelector.setKeyValues(aos);
        request.setLeadSelector(keySelector);
    } else if (parameters.leadSelectorSOAP.getValue().equals(LastUpdateAtSelector)) {
        LOG.debug("LastUpdateAtSelector - since {} to  {}.", parameters.oldestUpdateDate.getValue(), parameters.latestUpdateDate.getValue());
        LastUpdateAtSelector leadSelector = new LastUpdateAtSelector();
        try {
            DatatypeFactory factory = newInstance();
            Date oldest = MarketoUtils.parseDateString(parameters.oldestUpdateDate.getValue());
            Date latest = MarketoUtils.parseDateString(parameters.latestUpdateDate.getValue());
            GregorianCalendar gc = new GregorianCalendar();
            gc.setTime(latest);
            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);
        } catch (ParseException | DatatypeConfigurationException e) {
            LOG.error("Error for LastUpdateAtSelector : {}.", e.getMessage());
            throw new ComponentException(e);
        }
    } else // 
    if (parameters.leadSelectorSOAP.getValue().equals(StaticListSelector)) {
        LOG.info("StaticListSelector - List type : {} with value : {}.", parameters.listParam.getValue(), parameters.listParamListName.getValue());
        StaticListSelector staticListSelector = new StaticListSelector();
        if (parameters.listParam.getValue().equals(STATIC_LIST_NAME)) {
            JAXBElement<String> listName = objectFactory.createStaticListSelectorStaticListName(parameters.listParamListName.getValue());
            staticListSelector.setStaticListName(listName);
        } else {
            // you can find listId by examining the URL : https://app-abq.marketo.com/#ST29912B2
            // #ST29912B2 :
            // #ST -> Static list identifier
            // 29912 -> our list FIELD_ID !
            // B2 -> tab in the UI
            JAXBElement<Integer> listId = objectFactory.createStaticListSelectorStaticListId(// 
            parameters.listParamListId.getValue());
            staticListSelector.setStaticListId(listId);
        }
        request.setLeadSelector(staticListSelector);
    } else {
        // Duh !
        LOG.error("Unknown LeadSelector : {}.", parameters.leadSelectorSOAP.getValue());
        throw new ComponentException(new Exception("Incorrect parameter value for LeadSelector : " + parameters.leadSelectorSOAP.getValue()));
    }
    // attributes
    // curiously we have to put some basic fields like Email in attributes if we have them feed...
    ArrayOfString attributes = new ArrayOfString();
    for (String s : mappings.values()) {
        attributes.getStringItems().add(s);
    }
    attributes.getStringItems().add("Company");
    request.setIncludeAttributes(attributes);
    // batchSize : another curious behavior... Don't seem to work properly with leadKeySelector...
    // nevertheless, the server automatically adjust batch size according request.
    JAXBElement<Integer> batchSize = new ObjectFactory().createParamsGetMultipleLeadsBatchSize(bSize);
    request.setBatchSize(batchSize);
    // stream position
    if (offset != null && !offset.isEmpty()) {
        request.setStreamPosition(new ObjectFactory().createParamsGetMultipleLeadsStreamPosition(offset));
    }
    // 
    // 
    // Request execution
    // 
    SuccessGetMultipleLeads result = null;
    MarketoRecordResult mkto = new MarketoRecordResult();
    try {
        result = getPort().getMultipleLeads(request, header);
    } catch (Exception e) {
        LOG.error("Lead not found : {}.", e.getMessage());
        mkto.setSuccess(false);
        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.setSuccess(true);
        mkto.setErrors(Collections.singletonList(new MarketoError(SOAP, MESSAGE_NO_LEADS_FOUND)));
        mkto.setRecordCount(0);
        mkto.setRemainCount(0);
        return mkto;
    } else {
        String streamPos = result.getResult().getNewStreamPosition();
        int recordCount = result.getResult().getReturnCount();
        int remainCount = result.getResult().getRemainingCount();
        // Process results
        List<IndexedRecord> results = convertLeadRecords(result.getResult().getLeadRecordList().getValue().getLeadRecords(), schema, mappings);
        return new MarketoRecordResult(true, streamPos, recordCount, remainCount, results);
    }
}
Also used : IndexedRecord(org.apache.avro.generic.IndexedRecord) Schema(org.apache.avro.Schema) ArrayOfString(com.marketo.mktows.ArrayOfString) StaticListSelector(com.marketo.mktows.StaticListSelector) StaticListSelector(org.talend.components.marketo.tmarketoinput.TMarketoInputProperties.LeadSelector.StaticListSelector) MarketoError(org.talend.components.marketo.runtime.client.type.MarketoError) ObjectFactory(com.marketo.mktows.ObjectFactory) MarketoRecordResult(org.talend.components.marketo.runtime.client.type.MarketoRecordResult) LeadKeySelector(org.talend.components.marketo.tmarketoinput.TMarketoInputProperties.LeadSelector.LeadKeySelector) LeadKeySelector(com.marketo.mktows.LeadKeySelector) LastUpdateAtSelector(com.marketo.mktows.LastUpdateAtSelector) LastUpdateAtSelector(org.talend.components.marketo.tmarketoinput.TMarketoInputProperties.LeadSelector.LastUpdateAtSelector) DatatypeFactory(javax.xml.datatype.DatatypeFactory) GregorianCalendar(java.util.GregorianCalendar) XMLGregorianCalendar(javax.xml.datatype.XMLGregorianCalendar) JAXBElement(javax.xml.bind.JAXBElement) ParamsGetMultipleLeads(com.marketo.mktows.ParamsGetMultipleLeads) 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) ArrayOfString(com.marketo.mktows.ArrayOfString) ComponentException(org.talend.components.api.exception.ComponentException) SuccessGetMultipleLeads(com.marketo.mktows.SuccessGetMultipleLeads)

Aggregations

ArrayOfString (com.marketo.mktows.ArrayOfString)2 LastUpdateAtSelector (com.marketo.mktows.LastUpdateAtSelector)2 MalformedURLException (java.net.MalformedURLException)2 InvalidKeyException (java.security.InvalidKeyException)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 ParseException (java.text.ParseException)2 Date (java.util.Date)2 GregorianCalendar (java.util.GregorianCalendar)2 JAXBException (javax.xml.bind.JAXBException)2 DatatypeConfigurationException (javax.xml.datatype.DatatypeConfigurationException)2 DatatypeFactory (javax.xml.datatype.DatatypeFactory)2 XMLGregorianCalendar (javax.xml.datatype.XMLGregorianCalendar)2 WebServiceException (javax.xml.ws.WebServiceException)2 Schema (org.apache.avro.Schema)2 IndexedRecord (org.apache.avro.generic.IndexedRecord)2 ComponentException (org.talend.components.api.exception.ComponentException)2 MarketoError (org.talend.components.marketo.runtime.client.type.MarketoError)2 MarketoException (org.talend.components.marketo.runtime.client.type.MarketoException)2 MarketoRecordResult (org.talend.components.marketo.runtime.client.type.MarketoRecordResult)2 LastUpdateAtSelector (org.talend.components.marketo.tmarketoinput.TMarketoInputProperties.LeadSelector.LastUpdateAtSelector)2