Search in sources :

Example 11 with CustomFieldDesc

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

the class NetSuiteDatasetRuntimeImpl method inferSchemaForField.

/**
 * Infers an Avro schema for the given FieldDesc. This can be an expensive operation so the schema should be
 * cached where possible. The return type will be the Avro Schema that can contain the fieldDesc data without loss of
 * precision.
 *
 * @param fieldDesc the <code>FieldDesc</code> to analyse.
 * @return the schema for data that the fieldDesc describes.
 */
public static Schema inferSchemaForField(FieldDesc fieldDesc) {
    Schema base;
    if (fieldDesc instanceof CustomFieldDesc) {
        CustomFieldDesc customFieldInfo = (CustomFieldDesc) fieldDesc;
        CustomFieldRefType customFieldRefType = customFieldInfo.getCustomFieldType();
        if (customFieldRefType == CustomFieldRefType.BOOLEAN) {
            base = AvroUtils._boolean();
        } else if (customFieldRefType == CustomFieldRefType.LONG) {
            base = AvroUtils._long();
        } else if (customFieldRefType == CustomFieldRefType.DOUBLE) {
            base = AvroUtils._double();
        } else if (customFieldRefType == CustomFieldRefType.DATE) {
            base = AvroUtils._logicalTimestamp();
        } else if (customFieldRefType == CustomFieldRefType.STRING) {
            base = AvroUtils._string();
        } else {
            base = AvroUtils._string();
        }
    } else {
        Class<?> fieldType = fieldDesc.getValueType();
        if (fieldType == Boolean.TYPE || fieldType == Boolean.class) {
            base = AvroUtils._boolean();
        } else if (fieldType == Integer.TYPE || fieldType == Integer.class) {
            base = AvroUtils._int();
        } else if (fieldType == Long.TYPE || fieldType == Long.class) {
            base = AvroUtils._long();
        } else if (fieldType == Float.TYPE || fieldType == Float.class) {
            base = AvroUtils._float();
        } else if (fieldType == Double.TYPE || fieldType == Double.class) {
            base = AvroUtils._double();
        } else if (fieldType == XMLGregorianCalendar.class) {
            base = AvroUtils._logicalTimestamp();
        } else if (fieldType == String.class) {
            base = AvroUtils._string();
        } else if (fieldType.isEnum()) {
            base = AvroUtils._string();
        } else {
            base = AvroUtils._string();
        }
    }
    base = fieldDesc.isNullable() ? AvroUtils.wrapAsNullable(base) : base;
    return base;
}
Also used : XMLGregorianCalendar(javax.xml.datatype.XMLGregorianCalendar) Schema(org.apache.avro.Schema) CustomFieldRefType(org.talend.components.netsuite.client.model.customfield.CustomFieldRefType) CustomFieldDesc(org.talend.components.netsuite.client.model.CustomFieldDesc)

Example 12 with CustomFieldDesc

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

Example 13 with CustomFieldDesc

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

the class NetSuiteDatasetRuntimeImpl method inferSchemaForType.

/**
 * Infers an Avro schema for the given type. This can be an expensive operation so the schema
 * should be cached where possible. This is always an {@link Schema.Type#RECORD}.
 *
 * @param name name of a record.
 * @return the schema for data given from the object.
 */
public static Schema inferSchemaForType(String name, List<FieldDesc> fieldDescList) {
    List<Schema.Field> fields = new ArrayList<>();
    for (FieldDesc fieldDesc : fieldDescList) {
        final String fieldName = fieldDesc.getName();
        final String avroFieldName = toInitialUpper(fieldName);
        Schema.Field avroField = new Schema.Field(avroFieldName, inferSchemaForField(fieldDesc), null, (Object) null);
        // Add some Talend6 custom properties to the schema.
        Schema avroFieldSchema = AvroUtils.unwrapIfNullable(avroField.schema());
        avroField.addProp(SchemaConstants.TALEND_COLUMN_DB_COLUMN_NAME, fieldDesc.getName());
        if (AvroUtils.isSameType(avroFieldSchema, AvroUtils._string())) {
            if (fieldDesc.getLength() != 0) {
                avroField.addProp(SchemaConstants.TALEND_COLUMN_DB_LENGTH, String.valueOf(fieldDesc.getLength()));
            }
        }
        if (fieldDesc instanceof CustomFieldDesc) {
            CustomFieldDesc customFieldInfo = (CustomFieldDesc) fieldDesc;
            CustomFieldRefType customFieldRefType = customFieldInfo.getCustomFieldType();
            avroField.addProp(SchemaConstants.TALEND_COLUMN_DB_TYPE, customFieldRefType.getTypeName());
            if (customFieldRefType == CustomFieldRefType.DATE) {
                avroField.addProp(SchemaConstants.TALEND_COLUMN_PATTERN, "yyyy-MM-dd'T'HH:mm:ss'.000Z'");
            }
            NsRef ref = customFieldInfo.getCustomizationRef();
            if (StringUtils.isNotEmpty(ref.getName())) {
                avroField.addProp(NetSuiteSchemaConstants.TALEND6_COMMENT, ref.getName());
            }
        } else {
            Class<?> fieldType = fieldDesc.getValueType();
            avroField.addProp(SchemaConstants.TALEND_COLUMN_DB_TYPE, fieldType.getSimpleName());
            if (fieldType == XMLGregorianCalendar.class) {
                avroField.addProp(SchemaConstants.TALEND_COLUMN_PATTERN, "yyyy-MM-dd'T'HH:mm:ss'.000Z'");
            }
        }
        if (avroField.defaultVal() != null) {
            avroField.addProp(SchemaConstants.TALEND_COLUMN_DEFAULT, String.valueOf(avroField.defaultVal()));
        }
        if (fieldDesc.isKey()) {
            avroField.addProp(SchemaConstants.TALEND_COLUMN_IS_KEY, Boolean.TRUE.toString());
        }
        fields.add(avroField);
    }
    return Schema.createRecord(name, null, null, false, fields);
}
Also used : Schema(org.apache.avro.Schema) CustomFieldRefType(org.talend.components.netsuite.client.model.customfield.CustomFieldRefType) ArrayList(java.util.ArrayList) CustomFieldDesc(org.talend.components.netsuite.client.model.CustomFieldDesc) NsRef(org.talend.components.netsuite.client.NsRef) CustomFieldDesc(org.talend.components.netsuite.client.model.CustomFieldDesc) FieldDesc(org.talend.components.netsuite.client.model.FieldDesc)

Example 14 with CustomFieldDesc

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

the class NetSuiteDatasetRuntimeImpl method augmentSchemaWithCustomMetaData.

/**
 * Augment a given <code>Schema</code> with customization related meta data.
 *
 * @param metaDataSource source of meta data
 * @param schema schema to be augmented
 * @param recordTypeInfo information about record type to be used for augmentation
 * @param fieldDescList list of field descriptors to be used for augmentation
 */
public static void augmentSchemaWithCustomMetaData(final MetaDataSource metaDataSource, final Schema schema, final RecordTypeInfo recordTypeInfo, final Collection<FieldDesc> fieldDescList) {
    if (recordTypeInfo == null) {
        // Not a record, do nothing
        return;
    }
    // Add custom record type meta data to a key field
    if (recordTypeInfo instanceof CustomRecordTypeInfo) {
        CustomRecordTypeInfo customRecordTypeInfo = (CustomRecordTypeInfo) recordTypeInfo;
        for (Schema.Field field : schema.getFields()) {
            writeCustomRecord(metaDataSource.getBasicMetaData(), field, customRecordTypeInfo);
        }
    }
    // Add custom field meta data to fields
    if (fieldDescList != null && !fieldDescList.isEmpty()) {
        Map<String, CustomFieldDesc> customFieldDescMap = getCustomFieldDescMap(fieldDescList);
        if (!customFieldDescMap.isEmpty()) {
            for (Schema.Field field : schema.getFields()) {
                String nsFieldName = getNsFieldName(field);
                CustomFieldDesc customFieldDesc = customFieldDescMap.get(nsFieldName);
                if (customFieldDesc != null) {
                    writeCustomField(field, customFieldDesc);
                }
            }
        }
    }
}
Also used : Schema(org.apache.avro.Schema) CustomFieldDesc(org.talend.components.netsuite.client.model.CustomFieldDesc) CustomRecordTypeInfo(org.talend.components.netsuite.client.model.CustomRecordTypeInfo)

Example 15 with CustomFieldDesc

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

the class NetSuiteWebServiceMockTestFixture method assertNsObject.

public static void assertNsObject(TypeDesc typeDesc, Object nsObject) throws Exception {
    assertNotNull(nsObject);
    assertEquals(typeDesc.getTypeClass(), nsObject.getClass());
    for (FieldDesc fieldDesc : typeDesc.getFields()) {
        String fieldName = fieldDesc.getName();
        Object value = getProperty(nsObject, fieldName);
        if (fieldDesc instanceof CustomFieldDesc) {
            CustomFieldDesc customFieldDesc = fieldDesc.asCustom();
            switch(customFieldDesc.getCustomFieldType()) {
                case BOOLEAN:
                case LONG:
                case DOUBLE:
                case STRING:
                case DATE:
                    assertNotNull(value);
                    break;
            }
        } else {
            Class<?> datumClass = fieldDesc.getValueType();
            if (datumClass == Boolean.class || datumClass == Long.class || datumClass == Double.class || datumClass == String.class || datumClass == XMLGregorianCalendar.class) {
                assertNotNull(value);
            } else if (datumClass.isEnum()) {
                assertNotNull(value);
            }
        }
    }
}
Also used : XMLGregorianCalendar(javax.xml.datatype.XMLGregorianCalendar) CustomFieldDesc(org.talend.components.netsuite.client.model.CustomFieldDesc) CustomFieldDesc(org.talend.components.netsuite.client.model.CustomFieldDesc) FieldDesc(org.talend.components.netsuite.client.model.FieldDesc)

Aggregations

CustomFieldDesc (org.talend.components.netsuite.client.model.CustomFieldDesc)21 Schema (org.apache.avro.Schema)6 FieldDesc (org.talend.components.netsuite.client.model.FieldDesc)6 CustomFieldRefType (org.talend.components.netsuite.client.model.customfield.CustomFieldRefType)6 HashMap (java.util.HashMap)5 CustomRecordTypeInfo (org.talend.components.netsuite.client.model.CustomRecordTypeInfo)5 ArrayList (java.util.ArrayList)4 XMLGregorianCalendar (javax.xml.datatype.XMLGregorianCalendar)3 Test (org.junit.Test)3 NsRef (org.talend.components.netsuite.client.NsRef)3 RecordTypeInfo (org.talend.components.netsuite.client.model.RecordTypeInfo)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 CustomFieldSpec (org.talend.components.netsuite.CustomFieldSpec)2 RecordTypeDesc (org.talend.components.netsuite.client.model.RecordTypeDesc)2 TypeDesc (org.talend.components.netsuite.client.model.TypeDesc)2 CustomRecordType (com.netsuite.webservices.test.setup.customization.CustomRecordType)1 Contact (com.netsuite.webservices.v2016_2.lists.relationships.Contact)1 BooleanCustomFieldRef (com.netsuite.webservices.v2016_2.platform.core.BooleanCustomFieldRef)1 CustomFieldList (com.netsuite.webservices.v2016_2.platform.core.CustomFieldList)1 CustomRecordType (com.netsuite.webservices.v2016_2.setup.customization.CustomRecordType)1