Search in sources :

Example 21 with FieldDesc

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

the class NsObjectTransducer method getDynamicSchema.

/**
 * Get dynamic schema using given type descriptor and design schema.
 *
 * @param typeDesc NetSuite data model object type descriptor
 * @param designSchema design schema
 * @param targetSchemaName name of target schema
 * @return schema with all fields
 */
protected Schema getDynamicSchema(TypeDesc typeDesc, Schema designSchema, String targetSchemaName) {
    RecordTypeInfo recordTypeInfo = metaDataSource.getRecordType(typeDesc.getTypeName());
    Map<String, FieldDesc> fieldMap = typeDesc.getFieldMap();
    String dynamicPosProp = designSchema.getProp(NetSuiteSchemaConstants.TALEND6_DYNAMIC_COLUMN_POSITION);
    List<Schema.Field> fields = new ArrayList<>();
    if (dynamicPosProp != null) {
        Set<String> designFieldNames = new HashSet<>(designSchema.getFields().size());
        for (Schema.Field field : designSchema.getFields()) {
            String fieldName = NetSuiteDatasetRuntimeImpl.getNsFieldName(field);
            designFieldNames.add(fieldName);
        }
        int dynPos = Integer.parseInt(dynamicPosProp);
        int dynamicColumnSize = fieldMap.size() - designSchema.getFields().size();
        List<FieldDesc> dynaFieldDescList = new ArrayList<>(dynamicColumnSize);
        for (FieldDesc fieldDesc : fieldMap.values()) {
            String fieldName = fieldDesc.getName();
            if (!designFieldNames.contains(fieldName)) {
                dynaFieldDescList.add(fieldDesc);
            }
        }
        if (designSchema.getFields().size() > 0) {
            for (Schema.Field field : designSchema.getFields()) {
                // Dynamic column is first or middle column in design schema
                if (dynPos == field.pos()) {
                    for (int i = 0; i < dynamicColumnSize; i++) {
                        // Add dynamic schema fields
                        FieldDesc fieldDesc = dynaFieldDescList.get(i);
                        fields.add(createSchemaField(fieldDesc));
                    }
                }
                // Add fields of design schema
                Schema.Field avroField = new Schema.Field(field.name(), field.schema(), null, field.defaultVal());
                Map<String, Object> fieldProps = field.getObjectProps();
                for (String propName : fieldProps.keySet()) {
                    Object propValue = fieldProps.get(propName);
                    if (propValue != null) {
                        avroField.addProp(propName, propValue);
                    }
                }
                fields.add(avroField);
                // Dynamic column is last column in design schema
                if (field.pos() == (designSchema.getFields().size() - 1) && dynPos == (field.pos() + 1)) {
                    for (int i = 0; i < dynamicColumnSize; i++) {
                        // Add dynamic schema fields
                        FieldDesc fieldDesc = dynaFieldDescList.get(i);
                        fields.add(createSchemaField(fieldDesc));
                    }
                }
            }
        } else {
            // All fields are included in dynamic schema
            for (String fieldName : fieldMap.keySet()) {
                FieldDesc fieldDesc = fieldMap.get(fieldName);
                fields.add(createSchemaField(fieldDesc));
            }
        }
    } else {
        // All fields are included in dynamic schema
        for (String fieldName : fieldMap.keySet()) {
            FieldDesc fieldDesc = fieldMap.get(fieldName);
            fields.add(createSchemaField(fieldDesc));
        }
    }
    Schema schema = Schema.createRecord(targetSchemaName, null, null, false, fields);
    NetSuiteDatasetRuntimeImpl.augmentSchemaWithCustomMetaData(metaDataSource, schema, recordTypeInfo, typeDesc.getFields());
    return schema;
}
Also used : Schema(org.apache.avro.Schema) ArrayList(java.util.ArrayList) RecordTypeInfo(org.talend.components.netsuite.client.model.RecordTypeInfo) SimpleFieldDesc(org.talend.components.netsuite.client.model.SimpleFieldDesc) CustomFieldDesc(org.talend.components.netsuite.client.model.CustomFieldDesc) FieldDesc(org.talend.components.netsuite.client.model.FieldDesc) HashSet(java.util.HashSet)

Example 22 with FieldDesc

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

the class NsObjectTransducer method getMapView.

/**
 * Build and get map of field values by names, including custom fields.
 *
 * <p>Custom fields in data model object are stored in separate {@code customFieldList} field
 * as list of {@code CustomFieldRef} objects.
 *
 * @param nsObject NetSuite data model object which to extract field values from
 * @param schema target schema
 * @param typeDesc type descriptor
 * @return table of fields' values by field names
 */
protected Map<String, Object> getMapView(Object nsObject, Schema schema, TypeDesc typeDesc) {
    Map<String, Object> valueMap = new HashMap<>();
    BeanInfo beanInfo = Beans.getBeanInfo(typeDesc.getTypeClass());
    Map<String, FieldDesc> fieldMap = typeDesc.getFieldMap();
    Map<String, CustomFieldDesc> customFieldMap = new HashMap<>();
    for (Schema.Field field : schema.getFields()) {
        // Get actual name of the field
        String nsFieldName = NetSuiteDatasetRuntimeImpl.getNsFieldName(field);
        FieldDesc fieldDesc = fieldMap.get(nsFieldName);
        if (fieldDesc == null) {
            continue;
        }
        if (fieldDesc instanceof CustomFieldDesc) {
            // It's custom field, we will extract it in next stage.
            customFieldMap.put(nsFieldName, (CustomFieldDesc) fieldDesc);
        } else {
            Object value = getSimpleProperty(nsObject, fieldDesc.getName());
            valueMap.put(nsFieldName, value);
        }
    }
    if (!customFieldMap.isEmpty() && beanInfo.getProperty("customFieldList") != null) {
        List<?> customFieldList = (List<?>) getProperty(nsObject, "customFieldList.customField");
        if (customFieldList != null && !customFieldList.isEmpty()) {
            // Traverse all received custom fields and extract fields specified in schema
            for (Object customField : customFieldList) {
                String scriptId = (String) getSimpleProperty(customField, "scriptId");
                CustomFieldDesc customFieldInfo = customFieldMap.get(scriptId);
                if (customFieldInfo != null) {
                    String fieldName = customFieldInfo.getName();
                    valueMap.put(fieldName, customField);
                }
            }
        }
    }
    return valueMap;
}
Also used : HashMap(java.util.HashMap) BeanInfo(org.talend.components.netsuite.client.model.beans.BeanInfo) Schema(org.apache.avro.Schema) ArrayList(java.util.ArrayList) List(java.util.List) CustomFieldDesc(org.talend.components.netsuite.client.model.CustomFieldDesc) SimpleFieldDesc(org.talend.components.netsuite.client.model.SimpleFieldDesc) CustomFieldDesc(org.talend.components.netsuite.client.model.CustomFieldDesc) FieldDesc(org.talend.components.netsuite.client.model.FieldDesc)

Example 23 with FieldDesc

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

the class DefaultMetaDataSource method getTypeInfo.

/**
 * {@inheritDoc}
 */
@Override
public TypeDesc getTypeInfo(final String typeName) {
    TypeDesc baseTypeDesc;
    String targetTypeName = null;
    Class<?> targetTypeClass;
    List<FieldDesc> baseFieldDescList;
    RecordTypeInfo recordTypeInfo = getRecordType(typeName);
    if (recordTypeInfo != null) {
        if (recordTypeInfo instanceof CustomRecordTypeInfo) {
            CustomRecordTypeInfo customRecordTypeInfo = (CustomRecordTypeInfo) recordTypeInfo;
            baseTypeDesc = clientService.getBasicMetaData().getTypeInfo(customRecordTypeInfo.getRecordType().getTypeName());
            targetTypeName = customRecordTypeInfo.getName();
        } else {
            baseTypeDesc = clientService.getBasicMetaData().getTypeInfo(typeName);
        }
    } else {
        baseTypeDesc = clientService.getBasicMetaData().getTypeInfo(typeName);
    }
    if (targetTypeName == null) {
        targetTypeName = baseTypeDesc.getTypeName();
    }
    targetTypeClass = baseTypeDesc.getTypeClass();
    baseFieldDescList = baseTypeDesc.getFields();
    List<FieldDesc> resultFieldDescList = new ArrayList<>(baseFieldDescList.size() + 10);
    // Add basic fields except field list containers (custom field list, null field list)
    for (FieldDesc fieldDesc : baseFieldDescList) {
        String fieldName = fieldDesc.getName();
        if (fieldName.equals("customFieldList") || fieldName.equals("nullFieldList")) {
            continue;
        }
        resultFieldDescList.add(fieldDesc);
    }
    if (recordTypeInfo != null) {
        if (customizationEnabled) {
            // Add custom fields
            Map<String, CustomFieldDesc> customFieldMap = customMetaDataSource.getCustomFields(recordTypeInfo);
            for (CustomFieldDesc fieldInfo : customFieldMap.values()) {
                resultFieldDescList.add(fieldInfo);
            }
        }
    }
    return new TypeDesc(targetTypeName, targetTypeClass, resultFieldDescList);
}
Also used : CustomRecordTypeInfo(org.talend.components.netsuite.client.model.CustomRecordTypeInfo) RecordTypeInfo(org.talend.components.netsuite.client.model.RecordTypeInfo) 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) CustomFieldDesc(org.talend.components.netsuite.client.model.CustomFieldDesc) CustomRecordTypeInfo(org.talend.components.netsuite.client.model.CustomRecordTypeInfo) CustomFieldDesc(org.talend.components.netsuite.client.model.CustomFieldDesc) FieldDesc(org.talend.components.netsuite.client.model.FieldDesc)

Example 24 with FieldDesc

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

the class NsObjectInputTransducerIT method testIncludeAllFields.

@Test
public void testIncludeAllFields() throws Exception {
    NetSuiteClientService<?> connection = webServiceTestFixture.getClientService();
    connection.login();
    TypeDesc basicTypeDesc = connection.getBasicMetaData().getTypeInfo("Opportunity");
    Schema schema = getDynamicSchema();
    NsObjectInputTransducer transducer = new NsObjectInputTransducer(connection, schema, basicTypeDesc.getTypeName());
    SearchResultSet<Record> rs = connection.newSearch().target(basicTypeDesc.getTypeName()).search();
    TypeDesc typeDesc = connection.getMetaDataSource().getTypeInfo(basicTypeDesc.getTypeName());
    int count = 0;
    while (count++ < connection.getSearchPageSize() && rs.next()) {
        Record record = rs.get();
        IndexedRecord indexedRecord = transducer.read(record);
        logger.debug("Indexed record: {}", indexedRecord);
        Schema recordSchema = indexedRecord.getSchema();
        assertEquals(typeDesc.getFields().size(), recordSchema.getFields().size());
        for (FieldDesc fieldDesc : typeDesc.getFields()) {
            String fieldName = fieldDesc.getName();
            Schema.Field field = recordSchema.getField(fieldName);
            assertNotNull(field);
            Object value = indexedRecord.get(field.pos());
        }
    }
    if (count == 0) {
        throw new IllegalStateException("No records");
    }
}
Also used : IndexedRecord(org.apache.avro.generic.IndexedRecord) Schema(org.apache.avro.Schema) TypeDesc(org.talend.components.netsuite.client.model.TypeDesc) NsObjectInputTransducer(org.talend.components.netsuite.input.NsObjectInputTransducer) Record(com.netsuite.webservices.v2016_2.platform.core.Record) IndexedRecord(org.apache.avro.generic.IndexedRecord) FieldDesc(org.talend.components.netsuite.client.model.FieldDesc) Test(org.junit.Test)

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