use of org.talend.components.netsuite.client.model.FieldDesc in project components by Talend.
the class ValueConverterTest method testJsonConverterError.
@Test
public void testJsonConverterError() throws Exception {
NsObjectInputTransducer transducer = new NsObjectInputTransducer(clientService, schema, typeDesc.getTypeName());
FieldDesc fieldDesc = typeDesc.getField("department");
AvroConverter<RecordRef, String> converter1 = (AvroConverter<RecordRef, String>) transducer.getValueConverter(fieldDesc);
try {
converter1.convertToDatum("{name:'R&D',internalId:'12345',externalId:null,type:null}");
fail("NetSuiteException expected");
} catch (Exception e) {
assertThat(e, instanceOf(NetSuiteException.class));
NetSuiteException nsException = (NetSuiteException) e;
assertNotNull(nsException.getCode());
assertNotNull(nsException.getContext());
assertNotNull(nsException.getContext().get(ExceptionContext.KEY_MESSAGE));
}
}
use of org.talend.components.netsuite.client.model.FieldDesc in project components by Talend.
the class NsObjectOutputTransducer method write.
/**
* Translate input {@code IndexedRecord} to output NetSuite data object.
*
* @param indexedRecord indexed record to be processed
* @return NetSuite data object
*/
public Object write(IndexedRecord indexedRecord) {
prepare();
Map<String, FieldDesc> fieldMap = typeDesc.getFieldMap();
BeanInfo beanInfo = Beans.getBeanInfo(typeDesc.getTypeClass());
Schema schema = indexedRecord.getSchema();
String targetTypeName;
if (recordTypeInfo != null && !reference) {
RecordTypeDesc recordTypeDesc = recordTypeInfo.getRecordType();
targetTypeName = recordTypeDesc.getTypeName();
} else {
targetTypeName = typeDesc.getTypeName();
}
Object nsObject = clientService.getBasicMetaData().createInstance(targetTypeName);
// Names of fields to be null'ed.
Set<String> nullFieldNames = new HashSet<>();
// Custom fields by names.
Map<String, Object> customFieldMap = Collections.emptyMap();
if (!reference && beanInfo.getProperty("customFieldList") != null) {
customFieldMap = new HashMap<>();
Object customFieldListWrapper = getSimpleProperty(nsObject, "customFieldList");
if (customFieldListWrapper != null) {
List<Object> customFieldList = (List<Object>) getSimpleProperty(customFieldListWrapper, "customField");
for (Object customField : customFieldList) {
String scriptId = (String) getSimpleProperty(customField, "scriptId");
customFieldMap.put(scriptId, customField);
}
}
}
for (Schema.Field field : schema.getFields()) {
String nsFieldName = NetSuiteDatasetRuntimeImpl.getNsFieldName(field);
FieldDesc fieldDesc = fieldMap.get(nsFieldName);
if (fieldDesc == null) {
continue;
}
Object value = indexedRecord.get(field.pos());
writeField(nsObject, fieldDesc, customFieldMap, nullFieldNames, value);
}
if (reference) {
if (recordTypeInfo.getRefType() == RefType.RECORD_REF) {
FieldDesc recTypeFieldDesc = typeDesc.getField("type");
RecordTypeDesc recordTypeDesc = recordTypeInfo.getRecordType();
nullFieldNames.remove("type");
writeSimpleField(nsObject, recTypeFieldDesc.asSimple(), false, nullFieldNames, recordTypeDesc.getType());
} else if (recordTypeInfo.getRefType() == RefType.CUSTOM_RECORD_REF) {
CustomRecordTypeInfo customRecordTypeInfo = (CustomRecordTypeInfo) recordTypeInfo;
NsRef customizationRef = customRecordTypeInfo.getCustomizationRef();
FieldDesc typeIdFieldDesc = typeDesc.getField("typeId");
nullFieldNames.remove("typeId");
writeSimpleField(nsObject, typeIdFieldDesc.asSimple(), false, nullFieldNames, customizationRef.getInternalId());
}
} else if (recordTypeInfo != null) {
RecordTypeDesc recordTypeDesc = recordTypeInfo.getRecordType();
if (recordTypeDesc.getType().equals(BasicRecordType.CUSTOM_RECORD.getType())) {
CustomRecordTypeInfo customRecordTypeInfo = (CustomRecordTypeInfo) recordTypeInfo;
FieldDesc recTypeFieldDesc = typeDesc.getField("recType");
NsRef recordTypeRef = customRecordTypeInfo.getCustomizationRef();
// Create custom record type ref as JSON to create native RecordRef
ObjectNode recordRefNode = JsonNodeFactory.instance.objectNode();
recordRefNode.set("internalId", JsonNodeFactory.instance.textNode(recordTypeRef.getInternalId()));
recordRefNode.set("type", JsonNodeFactory.instance.textNode(recordTypeDesc.getType()));
nullFieldNames.remove("recType");
writeSimpleField(nsObject, recTypeFieldDesc.asSimple(), false, nullFieldNames, recordRefNode.toString());
}
}
if (!nullFieldNames.isEmpty() && beanInfo.getProperty("nullFieldList") != null) {
Object nullFieldListWrapper = clientService.getBasicMetaData().createInstance("NullField");
setSimpleProperty(nsObject, "nullFieldList", nullFieldListWrapper);
List<String> nullFields = (List<String>) getSimpleProperty(nullFieldListWrapper, "name");
nullFields.addAll(nullFieldNames);
}
return nsObject;
}
use of org.talend.components.netsuite.client.model.FieldDesc in project components by Talend.
the class NsObjectInputTransducer method read.
/**
* Translate NetSuite data model object to {@code IndexedRecord}.
*
* @param data NetSuite data object
* @return indexed record
*/
public IndexedRecord read(Object data) {
prepare();
Map<String, FieldDesc> fieldMap = typeDesc.getFieldMap();
Map<String, Object> mapView = getMapView(data, runtimeSchema, typeDesc);
GenericRecord indexedRecord = new GenericData.Record(runtimeSchema);
for (Schema.Field field : runtimeSchema.getFields()) {
String nsFieldName = NetSuiteDatasetRuntimeImpl.getNsFieldName(field);
FieldDesc fieldDesc = fieldMap.get(nsFieldName);
if (fieldDesc == null) {
continue;
}
Object value = readField(mapView, fieldDesc);
indexedRecord.put(field.name(), value);
}
return indexedRecord;
}
use of org.talend.components.netsuite.client.model.FieldDesc in project components by Talend.
the class NetSuiteDatasetRuntimeImpl method getCustomFieldDescMap.
/**
* Build and return map of custom field descriptors.
*
* @param fieldDescList list of custom field descriptors
* @return map of custom field descriptors by names
*/
public static Map<String, CustomFieldDesc> getCustomFieldDescMap(Collection<FieldDesc> fieldDescList) {
Map<String, CustomFieldDesc> customFieldDescMap = new HashMap<>();
for (FieldDesc fieldDesc : fieldDescList) {
if (fieldDesc instanceof CustomFieldDesc) {
CustomFieldDesc customFieldDesc = fieldDesc.asCustom();
customFieldDescMap.put(customFieldDesc.getName(), customFieldDesc);
}
}
return customFieldDescMap;
}
use of org.talend.components.netsuite.client.model.FieldDesc in project components by Talend.
the class NetSuiteDatasetRuntimeImpl method getSchemaForRecordRef.
public Schema getSchemaForRecordRef(String typeName) {
try {
// Get info for target record type
final RecordTypeInfo referencedRecordTypeInfo = metaDataSource.getRecordType(typeName);
final RefType refType = referencedRecordTypeInfo.getRefType();
// Get type info for record ref
final TypeDesc typeDesc = metaDataSource.getTypeInfo(refType.getTypeName());
List<FieldDesc> fieldDescList = new ArrayList<>(typeDesc.getFields());
// Sort in alphabetical order
Collections.sort(fieldDescList, FieldDescComparator.INSTANCE);
Schema schema = inferSchemaForType(typeDesc.getTypeName(), fieldDescList);
augmentSchemaWithCustomMetaData(metaDataSource, schema, referencedRecordTypeInfo, null);
return schema;
} catch (NetSuiteException e) {
throw new ComponentException(e);
}
}
Aggregations