Search in sources :

Example 6 with FieldDesc

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

the class ValueConverterTest method testJsonConverterError.

@Test
public void testJsonConverterError() throws Exception {
    NsObjectInputTransducer transducer = new NsObjectInputTransducer(clientService, schema, typeDesc.getTypeName());
    FieldDesc fieldDesc = typeDesc.getField("department");
    AvroConverter<RecordRef, String> converter1 = (AvroConverter<RecordRef, String>) transducer.getValueConverter(fieldDesc);
    try {
        converter1.convertToDatum("{name:'R&D',internalId:'12345',externalId:null,type:null}");
        fail("NetSuiteException expected");
    } catch (Exception e) {
        assertThat(e, instanceOf(NetSuiteException.class));
        NetSuiteException nsException = (NetSuiteException) e;
        assertNotNull(nsException.getCode());
        assertNotNull(nsException.getContext());
        assertNotNull(nsException.getContext().get(ExceptionContext.KEY_MESSAGE));
    }
}
Also used : RecordRef(com.netsuite.webservices.test.platform.core.RecordRef) NsObjectInputTransducer(org.talend.components.netsuite.input.NsObjectInputTransducer) NetSuiteException(org.talend.components.netsuite.client.NetSuiteException) AvroConverter(org.talend.daikon.avro.converter.AvroConverter) NetSuiteException(org.talend.components.netsuite.client.NetSuiteException) FieldDesc(org.talend.components.netsuite.client.model.FieldDesc) Test(org.junit.Test)

Example 7 with FieldDesc

use of org.talend.components.netsuite.client.model.FieldDesc 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 FieldDesc

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

the class NsObjectInputTransducer method read.

/**
 * Translate NetSuite data model object to {@code IndexedRecord}.
 *
 * @param data NetSuite data object
 * @return indexed record
 */
public IndexedRecord read(Object data) {
    prepare();
    Map<String, FieldDesc> fieldMap = typeDesc.getFieldMap();
    Map<String, Object> mapView = getMapView(data, runtimeSchema, typeDesc);
    GenericRecord indexedRecord = new GenericData.Record(runtimeSchema);
    for (Schema.Field field : runtimeSchema.getFields()) {
        String nsFieldName = NetSuiteDatasetRuntimeImpl.getNsFieldName(field);
        FieldDesc fieldDesc = fieldMap.get(nsFieldName);
        if (fieldDesc == null) {
            continue;
        }
        Object value = readField(mapView, fieldDesc);
        indexedRecord.put(field.name(), value);
    }
    return indexedRecord;
}
Also used : Schema(org.apache.avro.Schema) GenericRecord(org.apache.avro.generic.GenericRecord) IndexedRecord(org.apache.avro.generic.IndexedRecord) GenericRecord(org.apache.avro.generic.GenericRecord) FieldDesc(org.talend.components.netsuite.client.model.FieldDesc)

Example 9 with FieldDesc

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

the class NetSuiteDatasetRuntimeImpl method getCustomFieldDescMap.

/**
 * Build and return map of custom field descriptors.
 *
 * @param fieldDescList list of custom field descriptors
 * @return map of custom field descriptors by names
 */
public static Map<String, CustomFieldDesc> getCustomFieldDescMap(Collection<FieldDesc> fieldDescList) {
    Map<String, CustomFieldDesc> customFieldDescMap = new HashMap<>();
    for (FieldDesc fieldDesc : fieldDescList) {
        if (fieldDesc instanceof CustomFieldDesc) {
            CustomFieldDesc customFieldDesc = fieldDesc.asCustom();
            customFieldDescMap.put(customFieldDesc.getName(), customFieldDesc);
        }
    }
    return customFieldDescMap;
}
Also used : HashMap(java.util.HashMap) CustomFieldDesc(org.talend.components.netsuite.client.model.CustomFieldDesc) CustomFieldDesc(org.talend.components.netsuite.client.model.CustomFieldDesc) FieldDesc(org.talend.components.netsuite.client.model.FieldDesc)

Example 10 with FieldDesc

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

the class NetSuiteDatasetRuntimeImpl method getSchemaForRecordRef.

public Schema getSchemaForRecordRef(String typeName) {
    try {
        // Get info for target record type
        final RecordTypeInfo referencedRecordTypeInfo = metaDataSource.getRecordType(typeName);
        final RefType refType = referencedRecordTypeInfo.getRefType();
        // Get type info for record ref
        final TypeDesc typeDesc = metaDataSource.getTypeInfo(refType.getTypeName());
        List<FieldDesc> fieldDescList = new ArrayList<>(typeDesc.getFields());
        // Sort in alphabetical order
        Collections.sort(fieldDescList, FieldDescComparator.INSTANCE);
        Schema schema = inferSchemaForType(typeDesc.getTypeName(), fieldDescList);
        augmentSchemaWithCustomMetaData(metaDataSource, schema, referencedRecordTypeInfo, null);
        return schema;
    } catch (NetSuiteException e) {
        throw new ComponentException(e);
    }
}
Also used : CustomFieldRefType(org.talend.components.netsuite.client.model.customfield.CustomFieldRefType) RefType(org.talend.components.netsuite.client.model.RefType) RecordTypeInfo(org.talend.components.netsuite.client.model.RecordTypeInfo) CustomRecordTypeInfo(org.talend.components.netsuite.client.model.CustomRecordTypeInfo) Schema(org.apache.avro.Schema) SearchRecordTypeDesc(org.talend.components.netsuite.client.model.SearchRecordTypeDesc) RecordTypeDesc(org.talend.components.netsuite.client.model.RecordTypeDesc) TypeDesc(org.talend.components.netsuite.client.model.TypeDesc) ArrayList(java.util.ArrayList) ComponentException(org.talend.components.api.exception.ComponentException) NetSuiteException(org.talend.components.netsuite.client.NetSuiteException) CustomFieldDesc(org.talend.components.netsuite.client.model.CustomFieldDesc) FieldDesc(org.talend.components.netsuite.client.model.FieldDesc)

Aggregations

FieldDesc (org.talend.components.netsuite.client.model.FieldDesc)24 Schema (org.apache.avro.Schema)14 CustomFieldDesc (org.talend.components.netsuite.client.model.CustomFieldDesc)14 ArrayList (java.util.ArrayList)10 TypeDesc (org.talend.components.netsuite.client.model.TypeDesc)10 Test (org.junit.Test)9 RecordTypeDesc (org.talend.components.netsuite.client.model.RecordTypeDesc)8 NsObjectInputTransducer (org.talend.components.netsuite.input.NsObjectInputTransducer)8 CustomRecordTypeInfo (org.talend.components.netsuite.client.model.CustomRecordTypeInfo)7 RecordTypeInfo (org.talend.components.netsuite.client.model.RecordTypeInfo)7 SearchRecordTypeDesc (org.talend.components.netsuite.client.model.SearchRecordTypeDesc)7 AvroConverter (org.talend.daikon.avro.converter.AvroConverter)6 NetSuiteException (org.talend.components.netsuite.client.NetSuiteException)5 ComponentException (org.talend.components.api.exception.ComponentException)4 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)3 XMLGregorianCalendar (javax.xml.datatype.XMLGregorianCalendar)3 IndexedRecord (org.apache.avro.generic.IndexedRecord)3 RecordRef (com.netsuite.webservices.test.platform.core.RecordRef)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2