Search in sources :

Example 6 with CustomFieldRefType

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

use of org.talend.components.netsuite.client.model.customfield.CustomFieldRefType 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 8 with CustomFieldRefType

use of org.talend.components.netsuite.client.model.customfield.CustomFieldRefType 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 9 with CustomFieldRefType

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

the class NsObjectTransducer method writeCustomField.

/**
 * Write a custom field which is not defined by NetSuite standard data model.
 *
 * @param nsObject target NetSuite data model object which to write field value to
 * @param fieldDesc field descriptor
 * @param customFieldMap map of native custom field objects by names
 * @param replace specifies whether to forcibly replace a field's value
 * @param nullFieldNames collection to register null'ed fields
 * @param value value to be written, can be <code>null</code>
 */
protected void writeCustomField(Object nsObject, CustomFieldDesc fieldDesc, Map<String, Object> customFieldMap, boolean replace, Collection<String> nullFieldNames, Object value) {
    NsRef ref = fieldDesc.getCustomizationRef();
    CustomFieldRefType customFieldRefType = fieldDesc.getCustomFieldType();
    // Create custom field list wrapper if required
    Object customFieldListWrapper = getSimpleProperty(nsObject, "customFieldList");
    if (customFieldListWrapper == null) {
        customFieldListWrapper = clientService.getBasicMetaData().createInstance("CustomFieldList");
        setSimpleProperty(nsObject, "customFieldList", customFieldListWrapper);
    }
    List<Object> customFieldList = (List<Object>) getSimpleProperty(customFieldListWrapper, "customField");
    Object customField = customFieldMap.get(ref.getScriptId());
    AvroConverter valueConverter = getValueConverter(fieldDesc);
    Object targetValue = valueConverter.convertToDatum(value);
    if (targetValue == null) {
        if (replace && customField != null && customFieldList != null) {
            customFieldList.remove(customField);
            nullFieldNames.add(fieldDesc.getName());
        }
    } else {
        if (customField == null) {
            // Custom field instance doesn't exist,
            // create new instance and set identifiers
            customField = clientService.getBasicMetaData().createInstance(customFieldRefType.getTypeName());
            setSimpleProperty(customField, "scriptId", ref.getScriptId());
            setSimpleProperty(customField, "internalId", ref.getInternalId());
            customFieldList.add(customField);
            customFieldMap.put(ref.getScriptId(), customField);
        }
        setSimpleProperty(customField, "value", targetValue);
    }
}
Also used : CustomFieldRefType(org.talend.components.netsuite.client.model.customfield.CustomFieldRefType) ArrayList(java.util.ArrayList) List(java.util.List) NsRef(org.talend.components.netsuite.client.NsRef) AvroConverter(org.talend.daikon.avro.converter.AvroConverter)

Aggregations

CustomFieldRefType (org.talend.components.netsuite.client.model.customfield.CustomFieldRefType)9 CustomFieldDesc (org.talend.components.netsuite.client.model.CustomFieldDesc)6 NsRef (org.talend.components.netsuite.client.NsRef)5 ArrayList (java.util.ArrayList)2 Schema (org.apache.avro.Schema)2 EntityCustomField (com.netsuite.webservices.test.setup.customization.EntityCustomField)1 HashMap (java.util.HashMap)1 List (java.util.List)1 XMLGregorianCalendar (javax.xml.datatype.XMLGregorianCalendar)1 Test (org.junit.Test)1 NullConverter (org.talend.components.netsuite.avro.converter.NullConverter)1 FieldDesc (org.talend.components.netsuite.client.model.FieldDesc)1 AvroConverter (org.talend.daikon.avro.converter.AvroConverter)1