use of org.talend.components.netsuite.client.NsRef 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.NsRef in project components by Talend.
the class NetSuiteDatasetRuntimeImpl method readCustomRecord.
/**
* Read custom record meta data from a given <code>JsonProperties</code>.
*
* @see NetSuiteSchemaConstants
*
* @param basicMetaData basic meta data
* @param properties properties object which to read meta data from
* @return custom record type info or <code>null</code> if meta data was not found
*/
public static CustomRecordTypeInfo readCustomRecord(BasicMetaData basicMetaData, JsonProperties properties) {
String scriptId = properties.getProp(NetSuiteSchemaConstants.NS_CUSTOM_RECORD_SCRIPT_ID);
if (StringUtils.isEmpty(scriptId)) {
return null;
}
String internalId = properties.getProp(NetSuiteSchemaConstants.NS_CUSTOM_RECORD_INTERNAL_ID);
String customizationType = properties.getProp(NetSuiteSchemaConstants.NS_CUSTOM_RECORD_CUSTOMIZATION_TYPE);
String recordType = properties.getProp(NetSuiteSchemaConstants.NS_CUSTOM_RECORD_TYPE);
NsRef ref = new NsRef();
ref.setRefType(RefType.CUSTOMIZATION_REF);
ref.setScriptId(scriptId);
ref.setInternalId(internalId);
ref.setType(customizationType);
RecordTypeDesc recordTypeDesc = basicMetaData.getRecordType(recordType);
CustomRecordTypeInfo recordTypeInfo = new CustomRecordTypeInfo(scriptId, recordTypeDesc, ref);
return recordTypeInfo;
}
use of org.talend.components.netsuite.client.NsRef in project components by Talend.
the class NsRefTest method testEqualsAndHashCode.
@Test
public void testEqualsAndHashCode() {
NsRef ref1 = new NsRef(RefType.RECORD_REF);
ref1.setType(RecordType.ACCOUNT.value());
ref1.setInternalId("10001");
NsRef ref2 = new NsRef(RefType.RECORD_REF);
ref2.setType(RecordType.ACCOUNT.value());
ref2.setInternalId("10001");
assertEquals(ref1, ref2);
assertEquals(ref1.hashCode(), ref2.hashCode());
NsRef ref3 = new NsRef(RefType.RECORD_REF);
ref3.setType(RecordType.ACCOUNT.value());
ref3.setInternalId("10002");
assertNotEquals(ref1, ref3);
}
use of org.talend.components.netsuite.client.NsRef in project components by Talend.
the class NsRefTest method testCustomizationRef.
@Test
public void testCustomizationRef() {
NsRef ref = new NsRef(RefType.CUSTOMIZATION_REF);
ref.setType(RecordType.ENTITY_CUSTOM_FIELD.value());
ref.setInternalId("10001");
ref.setExternalId(UUID.randomUUID().toString());
ref.setScriptId("custentity102");
ref.setName("Custom Entity Field 1");
CustomizationRef customizationRef = (CustomizationRef) ref.toNativeRef(basicMetaData);
assertNotNull(customizationRef);
assertNull(ref.getTypeId());
assertEquals(RecordType.ENTITY_CUSTOM_FIELD, customizationRef.getType());
assertEquals(ref.getInternalId(), customizationRef.getInternalId());
assertEquals(ref.getExternalId(), customizationRef.getExternalId());
assertEquals(ref.getScriptId(), customizationRef.getScriptId());
NsRef ref2 = NsRef.fromNativeRef(customizationRef);
assertNotNull(ref2);
assertNull(ref2.getType());
assertEquals(customizationRef.getInternalId(), ref2.getInternalId());
assertEquals(customizationRef.getExternalId(), ref2.getExternalId());
assertEquals(customizationRef.getScriptId(), ref2.getScriptId());
}
use of org.talend.components.netsuite.client.NsRef in project components by Talend.
the class NsRefTest method testCustomRecordRef.
@Test
public void testCustomRecordRef() {
NsRef ref = new NsRef(RefType.CUSTOM_RECORD_REF);
ref.setTypeId("1001");
ref.setInternalId("10001");
ref.setExternalId(UUID.randomUUID().toString());
ref.setScriptId("custrecord25");
CustomRecordRef customRecordRef = (CustomRecordRef) ref.toNativeRef(basicMetaData);
assertNotNull(customRecordRef);
assertNull(ref.getType());
assertEquals(ref.getTypeId(), customRecordRef.getTypeId());
assertEquals(ref.getInternalId(), customRecordRef.getInternalId());
assertEquals(ref.getExternalId(), customRecordRef.getExternalId());
assertEquals(ref.getScriptId(), customRecordRef.getScriptId());
NsRef ref2 = NsRef.fromNativeRef(customRecordRef);
assertNotNull(ref2);
assertEquals(customRecordRef.getTypeId(), ref2.getTypeId());
assertEquals(customRecordRef.getInternalId(), ref2.getInternalId());
assertEquals(customRecordRef.getExternalId(), ref2.getExternalId());
assertNull(ref2.getScriptId());
}
Aggregations