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;
}
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;
}
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);
}
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);
}
}
Aggregations