Search in sources :

Example 6 with NsRef

use of org.talend.components.netsuite.client.NsRef in project components by Talend.

the class NetSuiteOutputWriter method processReturnVariables.

/**
 * Update return variables from given {@code IndexedRecord} after write.
 *
 * @param response write response
 */
private void processReturnVariables(final NsWriteResponse<RefT> response) {
    if (container != null) {
        String internalId = null;
        if (response.getRef() != null) {
            NsRef ref = NsRef.fromNativeRef(response.getRef());
            internalId = ref.getInternalId();
        }
        try {
            // For compatibility with old component, current internal ID is stored as Integer.
            container.setComponentData(container.getCurrentComponentId(), NetSuiteOutputDefinition.RETURN_LEGACY_CURRENT_INTERNAL_ID, (internalId != null ? Integer.parseInt(internalId) : null));
        } catch (NumberFormatException e) {
            // Normally this should not happen but we need to avoid failure of writer.
            logger.error("Couldn't parse internalId as Integer: {}", internalId);
        }
    }
}
Also used : NsRef(org.talend.components.netsuite.client.NsRef)

Example 7 with NsRef

use of org.talend.components.netsuite.client.NsRef in project components by Talend.

the class NsObjectOutputTransducer method write.

/**
 * Translate input {@code IndexedRecord} to output NetSuite data object.
 *
 * @param indexedRecord indexed record to be processed
 * @return NetSuite data object
 */
public Object write(IndexedRecord indexedRecord) {
    prepare();
    Map<String, FieldDesc> fieldMap = typeDesc.getFieldMap();
    BeanInfo beanInfo = Beans.getBeanInfo(typeDesc.getTypeClass());
    Schema schema = indexedRecord.getSchema();
    String targetTypeName;
    if (recordTypeInfo != null && !reference) {
        RecordTypeDesc recordTypeDesc = recordTypeInfo.getRecordType();
        targetTypeName = recordTypeDesc.getTypeName();
    } else {
        targetTypeName = typeDesc.getTypeName();
    }
    Object nsObject = clientService.getBasicMetaData().createInstance(targetTypeName);
    // Names of fields to be null'ed.
    Set<String> nullFieldNames = new HashSet<>();
    // Custom fields by names.
    Map<String, Object> customFieldMap = Collections.emptyMap();
    if (!reference && beanInfo.getProperty("customFieldList") != null) {
        customFieldMap = new HashMap<>();
        Object customFieldListWrapper = getSimpleProperty(nsObject, "customFieldList");
        if (customFieldListWrapper != null) {
            List<Object> customFieldList = (List<Object>) getSimpleProperty(customFieldListWrapper, "customField");
            for (Object customField : customFieldList) {
                String scriptId = (String) getSimpleProperty(customField, "scriptId");
                customFieldMap.put(scriptId, customField);
            }
        }
    }
    for (Schema.Field field : schema.getFields()) {
        String nsFieldName = NetSuiteDatasetRuntimeImpl.getNsFieldName(field);
        FieldDesc fieldDesc = fieldMap.get(nsFieldName);
        if (fieldDesc == null) {
            continue;
        }
        Object value = indexedRecord.get(field.pos());
        writeField(nsObject, fieldDesc, customFieldMap, nullFieldNames, value);
    }
    if (reference) {
        if (recordTypeInfo.getRefType() == RefType.RECORD_REF) {
            FieldDesc recTypeFieldDesc = typeDesc.getField("type");
            RecordTypeDesc recordTypeDesc = recordTypeInfo.getRecordType();
            nullFieldNames.remove("type");
            writeSimpleField(nsObject, recTypeFieldDesc.asSimple(), false, nullFieldNames, recordTypeDesc.getType());
        } else if (recordTypeInfo.getRefType() == RefType.CUSTOM_RECORD_REF) {
            CustomRecordTypeInfo customRecordTypeInfo = (CustomRecordTypeInfo) recordTypeInfo;
            NsRef customizationRef = customRecordTypeInfo.getCustomizationRef();
            FieldDesc typeIdFieldDesc = typeDesc.getField("typeId");
            nullFieldNames.remove("typeId");
            writeSimpleField(nsObject, typeIdFieldDesc.asSimple(), false, nullFieldNames, customizationRef.getInternalId());
        }
    } else if (recordTypeInfo != null) {
        RecordTypeDesc recordTypeDesc = recordTypeInfo.getRecordType();
        if (recordTypeDesc.getType().equals(BasicRecordType.CUSTOM_RECORD.getType())) {
            CustomRecordTypeInfo customRecordTypeInfo = (CustomRecordTypeInfo) recordTypeInfo;
            FieldDesc recTypeFieldDesc = typeDesc.getField("recType");
            NsRef recordTypeRef = customRecordTypeInfo.getCustomizationRef();
            // Create custom record type ref as JSON to create native RecordRef
            ObjectNode recordRefNode = JsonNodeFactory.instance.objectNode();
            recordRefNode.set("internalId", JsonNodeFactory.instance.textNode(recordTypeRef.getInternalId()));
            recordRefNode.set("type", JsonNodeFactory.instance.textNode(recordTypeDesc.getType()));
            nullFieldNames.remove("recType");
            writeSimpleField(nsObject, recTypeFieldDesc.asSimple(), false, nullFieldNames, recordRefNode.toString());
        }
    }
    if (!nullFieldNames.isEmpty() && beanInfo.getProperty("nullFieldList") != null) {
        Object nullFieldListWrapper = clientService.getBasicMetaData().createInstance("NullField");
        setSimpleProperty(nsObject, "nullFieldList", nullFieldListWrapper);
        List<String> nullFields = (List<String>) getSimpleProperty(nullFieldListWrapper, "name");
        nullFields.addAll(nullFieldNames);
    }
    return nsObject;
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) BeanInfo(org.talend.components.netsuite.client.model.beans.BeanInfo) Schema(org.apache.avro.Schema) NsRef(org.talend.components.netsuite.client.NsRef) RecordTypeDesc(org.talend.components.netsuite.client.model.RecordTypeDesc) List(java.util.List) CustomRecordTypeInfo(org.talend.components.netsuite.client.model.CustomRecordTypeInfo) FieldDesc(org.talend.components.netsuite.client.model.FieldDesc) HashSet(java.util.HashSet)

Example 8 with NsRef

use of org.talend.components.netsuite.client.NsRef in project components by Talend.

the class NetSuiteDatasetRuntimeImpl method writeCustomField.

/**
 * Write custom field meta data to a given <code>JsonProperties</code>.
 *
 * @see NetSuiteSchemaConstants
 *
 * @param properties properties object which to write meta data to
 * @param fieldDesc information about custom field to be used
 */
public static void writeCustomField(JsonProperties properties, CustomFieldDesc fieldDesc) {
    NsRef ref = fieldDesc.getCustomizationRef();
    CustomFieldRefType customFieldRefType = fieldDesc.getCustomFieldType();
    properties.addProp(NetSuiteSchemaConstants.NS_CUSTOM_FIELD, "true");
    properties.addProp(NetSuiteSchemaConstants.NS_CUSTOM_FIELD_SCRIPT_ID, ref.getScriptId());
    properties.addProp(NetSuiteSchemaConstants.NS_CUSTOM_FIELD_INTERNAL_ID, ref.getInternalId());
    properties.addProp(NetSuiteSchemaConstants.NS_CUSTOM_FIELD_CUSTOMIZATION_TYPE, ref.getType());
    properties.addProp(NetSuiteSchemaConstants.NS_CUSTOM_FIELD_TYPE, customFieldRefType.name());
}
Also used : CustomFieldRefType(org.talend.components.netsuite.client.model.customfield.CustomFieldRefType) NsRef(org.talend.components.netsuite.client.NsRef)

Example 9 with NsRef

use of org.talend.components.netsuite.client.NsRef in project components by Talend.

the class NetSuiteDatasetRuntimeImpl method writeCustomRecord.

/**
 * Write custom record meta data to a given <code>JsonProperties</code>.
 *
 * @see NetSuiteSchemaConstants
 *
 * @param basicMetaData basic meta data
 * @param properties properties object which to write meta data to
 * @param recordTypeInfo information about record type to be used
 */
public static void writeCustomRecord(BasicMetaData basicMetaData, JsonProperties properties, CustomRecordTypeInfo recordTypeInfo) {
    NsRef ref = recordTypeInfo.getCustomizationRef();
    RecordTypeDesc recordTypeDesc = recordTypeInfo.getRecordType();
    properties.addProp(NetSuiteSchemaConstants.NS_CUSTOM_RECORD, "true");
    properties.addProp(NetSuiteSchemaConstants.NS_CUSTOM_RECORD_SCRIPT_ID, ref.getScriptId());
    properties.addProp(NetSuiteSchemaConstants.NS_CUSTOM_RECORD_INTERNAL_ID, ref.getInternalId());
    properties.addProp(NetSuiteSchemaConstants.NS_CUSTOM_RECORD_CUSTOMIZATION_TYPE, ref.getType());
    properties.addProp(NetSuiteSchemaConstants.NS_CUSTOM_RECORD_TYPE, recordTypeDesc.getType());
}
Also used : SearchRecordTypeDesc(org.talend.components.netsuite.client.model.SearchRecordTypeDesc) RecordTypeDesc(org.talend.components.netsuite.client.model.RecordTypeDesc) NsRef(org.talend.components.netsuite.client.NsRef)

Example 10 with NsRef

use of org.talend.components.netsuite.client.NsRef in project components by Talend.

the class NetSuiteDatasetRuntimeImpl method readCustomField.

/**
 * Read custom field meta data from a given <code>JsonProperties</code>.
 *
 * @see NetSuiteSchemaConstants
 *
 * @param properties properties object which to read meta data from
 * @return custom field info or <code>null</code> if meta data was not found
 */
public static CustomFieldDesc readCustomField(JsonProperties properties) {
    String scriptId = properties.getProp(NetSuiteSchemaConstants.NS_CUSTOM_FIELD_SCRIPT_ID);
    if (StringUtils.isEmpty(scriptId)) {
        return null;
    }
    String internalId = properties.getProp(NetSuiteSchemaConstants.NS_CUSTOM_FIELD_INTERNAL_ID);
    String customizationType = properties.getProp(NetSuiteSchemaConstants.NS_CUSTOM_FIELD_CUSTOMIZATION_TYPE);
    String type = properties.getProp(NetSuiteSchemaConstants.NS_CUSTOM_FIELD_TYPE);
    NsRef ref = new NsRef();
    ref.setRefType(RefType.CUSTOMIZATION_REF);
    ref.setScriptId(scriptId);
    ref.setInternalId(internalId);
    ref.setType(customizationType);
    CustomFieldRefType customFieldRefType = CustomFieldRefType.valueOf(type);
    CustomFieldDesc fieldDesc = new CustomFieldDesc();
    fieldDesc.setCustomFieldType(customFieldRefType);
    fieldDesc.setCustomizationRef(ref);
    fieldDesc.setName(scriptId);
    fieldDesc.setValueType(getCustomFieldValueClass(customFieldRefType));
    fieldDesc.setNullable(true);
    return fieldDesc;
}
Also used : CustomFieldRefType(org.talend.components.netsuite.client.model.customfield.CustomFieldRefType) CustomFieldDesc(org.talend.components.netsuite.client.model.CustomFieldDesc) NsRef(org.talend.components.netsuite.client.NsRef)

Aggregations

NsRef (org.talend.components.netsuite.client.NsRef)23 Test (org.junit.Test)7 CustomFieldRefType (org.talend.components.netsuite.client.model.customfield.CustomFieldRefType)5 ArrayList (java.util.ArrayList)4 List (java.util.List)4 CustomRecordTypeInfo (org.talend.components.netsuite.client.model.CustomRecordTypeInfo)4 RecordTypeDesc (org.talend.components.netsuite.client.model.RecordTypeDesc)4 Schema (org.apache.avro.Schema)3 CustomFieldDesc (org.talend.components.netsuite.client.model.CustomFieldDesc)3 CustomRecordRef (com.netsuite.webservices.test.platform.core.CustomRecordRef)2 NetSuitePortType (com.netsuite.webservices.v2016_2.platform.NetSuitePortType)2 CustomizationRef (com.netsuite.webservices.v2016_2.platform.core.CustomizationRef)2 StopWatch (org.apache.commons.lang3.time.StopWatch)2 NetSuiteClientService (org.talend.components.netsuite.client.NetSuiteClientService)2 NetSuiteException (org.talend.components.netsuite.client.NetSuiteException)2 FieldDesc (org.talend.components.netsuite.client.model.FieldDesc)2 SearchRecordTypeDesc (org.talend.components.netsuite.client.model.SearchRecordTypeDesc)2 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 CustomizationRef (com.netsuite.webservices.test.platform.core.CustomizationRef)1 RecordRef (com.netsuite.webservices.test.platform.core.RecordRef)1