use of org.talend.components.netsuite.client.model.FieldDesc in project components by Talend.
the class NsObjectTransducer method getDynamicSchema.
/**
* Get dynamic schema using given type descriptor and design schema.
*
* @param typeDesc NetSuite data model object type descriptor
* @param designSchema design schema
* @param targetSchemaName name of target schema
* @return schema with all fields
*/
protected Schema getDynamicSchema(TypeDesc typeDesc, Schema designSchema, String targetSchemaName) {
RecordTypeInfo recordTypeInfo = metaDataSource.getRecordType(typeDesc.getTypeName());
Map<String, FieldDesc> fieldMap = typeDesc.getFieldMap();
String dynamicPosProp = designSchema.getProp(NetSuiteSchemaConstants.TALEND6_DYNAMIC_COLUMN_POSITION);
List<Schema.Field> fields = new ArrayList<>();
if (dynamicPosProp != null) {
Set<String> designFieldNames = new HashSet<>(designSchema.getFields().size());
for (Schema.Field field : designSchema.getFields()) {
String fieldName = NetSuiteDatasetRuntimeImpl.getNsFieldName(field);
designFieldNames.add(fieldName);
}
int dynPos = Integer.parseInt(dynamicPosProp);
int dynamicColumnSize = fieldMap.size() - designSchema.getFields().size();
List<FieldDesc> dynaFieldDescList = new ArrayList<>(dynamicColumnSize);
for (FieldDesc fieldDesc : fieldMap.values()) {
String fieldName = fieldDesc.getName();
if (!designFieldNames.contains(fieldName)) {
dynaFieldDescList.add(fieldDesc);
}
}
if (designSchema.getFields().size() > 0) {
for (Schema.Field field : designSchema.getFields()) {
// Dynamic column is first or middle column in design schema
if (dynPos == field.pos()) {
for (int i = 0; i < dynamicColumnSize; i++) {
// Add dynamic schema fields
FieldDesc fieldDesc = dynaFieldDescList.get(i);
fields.add(createSchemaField(fieldDesc));
}
}
// Add fields of design schema
Schema.Field avroField = new Schema.Field(field.name(), field.schema(), null, field.defaultVal());
Map<String, Object> fieldProps = field.getObjectProps();
for (String propName : fieldProps.keySet()) {
Object propValue = fieldProps.get(propName);
if (propValue != null) {
avroField.addProp(propName, propValue);
}
}
fields.add(avroField);
// Dynamic column is last column in design schema
if (field.pos() == (designSchema.getFields().size() - 1) && dynPos == (field.pos() + 1)) {
for (int i = 0; i < dynamicColumnSize; i++) {
// Add dynamic schema fields
FieldDesc fieldDesc = dynaFieldDescList.get(i);
fields.add(createSchemaField(fieldDesc));
}
}
}
} else {
// All fields are included in dynamic schema
for (String fieldName : fieldMap.keySet()) {
FieldDesc fieldDesc = fieldMap.get(fieldName);
fields.add(createSchemaField(fieldDesc));
}
}
} else {
// All fields are included in dynamic schema
for (String fieldName : fieldMap.keySet()) {
FieldDesc fieldDesc = fieldMap.get(fieldName);
fields.add(createSchemaField(fieldDesc));
}
}
Schema schema = Schema.createRecord(targetSchemaName, null, null, false, fields);
NetSuiteDatasetRuntimeImpl.augmentSchemaWithCustomMetaData(metaDataSource, schema, recordTypeInfo, typeDesc.getFields());
return schema;
}
use of org.talend.components.netsuite.client.model.FieldDesc in project components by Talend.
the class NsObjectTransducer method getMapView.
/**
* Build and get map of field values by names, including custom fields.
*
* <p>Custom fields in data model object are stored in separate {@code customFieldList} field
* as list of {@code CustomFieldRef} objects.
*
* @param nsObject NetSuite data model object which to extract field values from
* @param schema target schema
* @param typeDesc type descriptor
* @return table of fields' values by field names
*/
protected Map<String, Object> getMapView(Object nsObject, Schema schema, TypeDesc typeDesc) {
Map<String, Object> valueMap = new HashMap<>();
BeanInfo beanInfo = Beans.getBeanInfo(typeDesc.getTypeClass());
Map<String, FieldDesc> fieldMap = typeDesc.getFieldMap();
Map<String, CustomFieldDesc> customFieldMap = new HashMap<>();
for (Schema.Field field : schema.getFields()) {
// Get actual name of the field
String nsFieldName = NetSuiteDatasetRuntimeImpl.getNsFieldName(field);
FieldDesc fieldDesc = fieldMap.get(nsFieldName);
if (fieldDesc == null) {
continue;
}
if (fieldDesc instanceof CustomFieldDesc) {
// It's custom field, we will extract it in next stage.
customFieldMap.put(nsFieldName, (CustomFieldDesc) fieldDesc);
} else {
Object value = getSimpleProperty(nsObject, fieldDesc.getName());
valueMap.put(nsFieldName, value);
}
}
if (!customFieldMap.isEmpty() && beanInfo.getProperty("customFieldList") != null) {
List<?> customFieldList = (List<?>) getProperty(nsObject, "customFieldList.customField");
if (customFieldList != null && !customFieldList.isEmpty()) {
// Traverse all received custom fields and extract fields specified in schema
for (Object customField : customFieldList) {
String scriptId = (String) getSimpleProperty(customField, "scriptId");
CustomFieldDesc customFieldInfo = customFieldMap.get(scriptId);
if (customFieldInfo != null) {
String fieldName = customFieldInfo.getName();
valueMap.put(fieldName, customField);
}
}
}
}
return valueMap;
}
use of org.talend.components.netsuite.client.model.FieldDesc in project components by Talend.
the class DefaultMetaDataSource method getTypeInfo.
/**
* {@inheritDoc}
*/
@Override
public TypeDesc getTypeInfo(final String typeName) {
TypeDesc baseTypeDesc;
String targetTypeName = null;
Class<?> targetTypeClass;
List<FieldDesc> baseFieldDescList;
RecordTypeInfo recordTypeInfo = getRecordType(typeName);
if (recordTypeInfo != null) {
if (recordTypeInfo instanceof CustomRecordTypeInfo) {
CustomRecordTypeInfo customRecordTypeInfo = (CustomRecordTypeInfo) recordTypeInfo;
baseTypeDesc = clientService.getBasicMetaData().getTypeInfo(customRecordTypeInfo.getRecordType().getTypeName());
targetTypeName = customRecordTypeInfo.getName();
} else {
baseTypeDesc = clientService.getBasicMetaData().getTypeInfo(typeName);
}
} else {
baseTypeDesc = clientService.getBasicMetaData().getTypeInfo(typeName);
}
if (targetTypeName == null) {
targetTypeName = baseTypeDesc.getTypeName();
}
targetTypeClass = baseTypeDesc.getTypeClass();
baseFieldDescList = baseTypeDesc.getFields();
List<FieldDesc> resultFieldDescList = new ArrayList<>(baseFieldDescList.size() + 10);
// Add basic fields except field list containers (custom field list, null field list)
for (FieldDesc fieldDesc : baseFieldDescList) {
String fieldName = fieldDesc.getName();
if (fieldName.equals("customFieldList") || fieldName.equals("nullFieldList")) {
continue;
}
resultFieldDescList.add(fieldDesc);
}
if (recordTypeInfo != null) {
if (customizationEnabled) {
// Add custom fields
Map<String, CustomFieldDesc> customFieldMap = customMetaDataSource.getCustomFields(recordTypeInfo);
for (CustomFieldDesc fieldInfo : customFieldMap.values()) {
resultFieldDescList.add(fieldInfo);
}
}
}
return new TypeDesc(targetTypeName, targetTypeClass, resultFieldDescList);
}
use of org.talend.components.netsuite.client.model.FieldDesc in project components by Talend.
the class NsObjectInputTransducerIT method testIncludeAllFields.
@Test
public void testIncludeAllFields() throws Exception {
NetSuiteClientService<?> connection = webServiceTestFixture.getClientService();
connection.login();
TypeDesc basicTypeDesc = connection.getBasicMetaData().getTypeInfo("Opportunity");
Schema schema = getDynamicSchema();
NsObjectInputTransducer transducer = new NsObjectInputTransducer(connection, schema, basicTypeDesc.getTypeName());
SearchResultSet<Record> rs = connection.newSearch().target(basicTypeDesc.getTypeName()).search();
TypeDesc typeDesc = connection.getMetaDataSource().getTypeInfo(basicTypeDesc.getTypeName());
int count = 0;
while (count++ < connection.getSearchPageSize() && rs.next()) {
Record record = rs.get();
IndexedRecord indexedRecord = transducer.read(record);
logger.debug("Indexed record: {}", indexedRecord);
Schema recordSchema = indexedRecord.getSchema();
assertEquals(typeDesc.getFields().size(), recordSchema.getFields().size());
for (FieldDesc fieldDesc : typeDesc.getFields()) {
String fieldName = fieldDesc.getName();
Schema.Field field = recordSchema.getField(fieldName);
assertNotNull(field);
Object value = indexedRecord.get(field.pos());
}
}
if (count == 0) {
throw new IllegalStateException("No records");
}
}
Aggregations