Search in sources :

Example 11 with FieldDesc

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

the class NetSuiteDatasetRuntimeImpl method getSchema.

@Override
public Schema getSchema(String typeName) {
    try {
        final RecordTypeInfo recordTypeInfo = metaDataSource.getRecordType(typeName);
        final TypeDesc typeDesc = metaDataSource.getTypeInfo(typeName);
        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, recordTypeInfo, fieldDescList);
        return schema;
    } catch (NetSuiteException e) {
        throw new ComponentException(e);
    }
}
Also used : RecordTypeInfo(org.talend.components.netsuite.client.model.RecordTypeInfo) CustomRecordTypeInfo(org.talend.components.netsuite.client.model.CustomRecordTypeInfo) Schema(org.apache.avro.Schema) SearchRecordTypeDesc(org.talend.components.netsuite.client.model.SearchRecordTypeDesc) RecordTypeDesc(org.talend.components.netsuite.client.model.RecordTypeDesc) TypeDesc(org.talend.components.netsuite.client.model.TypeDesc) ArrayList(java.util.ArrayList) ComponentException(org.talend.components.api.exception.ComponentException) NetSuiteException(org.talend.components.netsuite.client.NetSuiteException) CustomFieldDesc(org.talend.components.netsuite.client.model.CustomFieldDesc) FieldDesc(org.talend.components.netsuite.client.model.FieldDesc)

Example 12 with FieldDesc

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

the class NetSuiteDatasetRuntimeImpl method getSchemaForUpdateFlow.

@Override
public Schema getSchemaForUpdateFlow(String typeName, Schema schema) {
    RecordTypeInfo recordTypeInfo = metaDataSource.getRecordType(typeName);
    TypeDesc typeDesc = metaDataSource.getTypeInfo(typeName);
    // We should check and add key fields:
    // internalId, externalId and scriptId (for custom record type)
    List<FieldDesc> fieldDescList = new ArrayList<>();
    Schema.Field internalIdField = getNsFieldByName(schema, "internalId");
    if (internalIdField == null) {
        FieldDesc fieldDesc = typeDesc.getField("internalId");
        fieldDescList.add(fieldDesc);
    }
    Schema.Field externalIdField = getNsFieldByName(schema, "externalId");
    if (externalIdField == null) {
        FieldDesc fieldDesc = typeDesc.getField("externalId");
        fieldDescList.add(fieldDesc);
    }
    if (recordTypeInfo instanceof CustomRecordTypeInfo) {
        Schema.Field scriptIdField = getNsFieldByName(schema, "scriptId");
        if (scriptIdField == null) {
            FieldDesc fieldDesc = typeDesc.getField("scriptId");
            if (fieldDesc != null) {
                fieldDescList.add(fieldDesc);
            }
        }
    }
    // Create schema fields for mandatory fields.
    List<Schema.Field> fields = new ArrayList<>();
    Schema.Field f;
    if (!fieldDescList.isEmpty()) {
        Schema schemaToAdd = inferSchemaForType(typeName, fieldDescList);
        for (Schema.Field sourceField : schemaToAdd.getFields()) {
            f = copyField(sourceField);
            f.addProp(SchemaConstants.TALEND_FIELD_GENERATED, "true");
            f.addProp(SchemaConstants.TALEND_IS_LOCKED, "true");
            fields.add(f);
        }
    }
    return extendSchema(schema, typeName + "_FLOW", fields);
}
Also used : RecordTypeInfo(org.talend.components.netsuite.client.model.RecordTypeInfo) CustomRecordTypeInfo(org.talend.components.netsuite.client.model.CustomRecordTypeInfo) Schema(org.apache.avro.Schema) SearchRecordTypeDesc(org.talend.components.netsuite.client.model.SearchRecordTypeDesc) RecordTypeDesc(org.talend.components.netsuite.client.model.RecordTypeDesc) TypeDesc(org.talend.components.netsuite.client.model.TypeDesc) ArrayList(java.util.ArrayList) CustomRecordTypeInfo(org.talend.components.netsuite.client.model.CustomRecordTypeInfo) CustomFieldDesc(org.talend.components.netsuite.client.model.CustomFieldDesc) FieldDesc(org.talend.components.netsuite.client.model.FieldDesc)

Example 13 with FieldDesc

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

the class NetSuiteDatasetRuntimeImpl method getSchemaForDeleteFlow.

@Override
public Schema getSchemaForDeleteFlow(String typeName, Schema schema) {
    RecordTypeInfo recordTypeInfo = metaDataSource.getRecordType(typeName);
    TypeDesc typeDesc = metaDataSource.getTypeInfo(typeName);
    // We should check and add key fields:
    // internalId, externalId and scriptId (for custom record type)
    List<FieldDesc> fieldDescList = new ArrayList<>();
    Schema.Field internalIdField = getNsFieldByName(schema, "internalId");
    if (internalIdField == null) {
        FieldDesc fieldDesc = typeDesc.getField("internalId");
        fieldDescList.add(fieldDesc);
    }
    Schema.Field externalIdField = getNsFieldByName(schema, "externalId");
    if (externalIdField == null) {
        FieldDesc fieldDesc = typeDesc.getField("externalId");
        fieldDescList.add(fieldDesc);
    }
    if (recordTypeInfo instanceof CustomRecordTypeInfo) {
        Schema.Field scriptIdField = getNsFieldByName(schema, "scriptId");
        if (scriptIdField == null) {
            FieldDesc fieldDesc = typeDesc.getField("scriptId");
            if (fieldDesc != null) {
                fieldDescList.add(fieldDesc);
            }
        }
    }
    // Create schema fields for mandatory fields.
    List<Schema.Field> fields = new ArrayList<>();
    Schema.Field f;
    if (!fieldDescList.isEmpty()) {
        Schema schemaToAdd = inferSchemaForType(typeName, fieldDescList);
        for (Schema.Field sourceField : schemaToAdd.getFields()) {
            f = copyField(sourceField);
            f.addProp(SchemaConstants.TALEND_FIELD_GENERATED, "true");
            f.addProp(SchemaConstants.TALEND_IS_LOCKED, "true");
            fields.add(f);
        }
    }
    return extendSchema(schema, typeName + "_FLOW", fields);
}
Also used : RecordTypeInfo(org.talend.components.netsuite.client.model.RecordTypeInfo) CustomRecordTypeInfo(org.talend.components.netsuite.client.model.CustomRecordTypeInfo) Schema(org.apache.avro.Schema) SearchRecordTypeDesc(org.talend.components.netsuite.client.model.SearchRecordTypeDesc) RecordTypeDesc(org.talend.components.netsuite.client.model.RecordTypeDesc) TypeDesc(org.talend.components.netsuite.client.model.TypeDesc) ArrayList(java.util.ArrayList) CustomRecordTypeInfo(org.talend.components.netsuite.client.model.CustomRecordTypeInfo) CustomFieldDesc(org.talend.components.netsuite.client.model.CustomFieldDesc) FieldDesc(org.talend.components.netsuite.client.model.FieldDesc)

Example 14 with FieldDesc

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

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

the class NetSuiteDatasetRuntimeImpl method getSearchInfo.

@Override
public SearchInfo getSearchInfo(String typeName) {
    try {
        final SearchRecordTypeDesc searchInfo = metaDataSource.getSearchRecordType(typeName);
        final TypeDesc searchRecordInfo = metaDataSource.getBasicMetaData().getTypeInfo(searchInfo.getSearchBasicClass());
        List<FieldDesc> fieldDescList = searchRecordInfo.getFields();
        List<SearchFieldInfo> fields = new ArrayList<>(fieldDescList.size());
        for (FieldDesc fieldDesc : fieldDescList) {
            SearchFieldInfo field = new SearchFieldInfo(fieldDesc.getName(), fieldDesc.getValueType());
            fields.add(field);
        }
        // Sort by display name in alphabetical order
        Collections.sort(fields, new Comparator<SearchFieldInfo>() {

            @Override
            public int compare(SearchFieldInfo o1, SearchFieldInfo o2) {
                return o1.getName().compareTo(o2.getName());
            }
        });
        return new SearchInfo(searchRecordInfo.getTypeName(), fields);
    } catch (NetSuiteException e) {
        throw new ComponentException(e);
    }
}
Also used : SearchRecordTypeDesc(org.talend.components.netsuite.client.model.SearchRecordTypeDesc) SearchFieldInfo(org.talend.components.netsuite.schema.SearchFieldInfo) SearchInfo(org.talend.components.netsuite.schema.SearchInfo) SearchRecordTypeDesc(org.talend.components.netsuite.client.model.SearchRecordTypeDesc) RecordTypeDesc(org.talend.components.netsuite.client.model.RecordTypeDesc) TypeDesc(org.talend.components.netsuite.client.model.TypeDesc) ArrayList(java.util.ArrayList) ComponentException(org.talend.components.api.exception.ComponentException) NetSuiteException(org.talend.components.netsuite.client.NetSuiteException) CustomFieldDesc(org.talend.components.netsuite.client.model.CustomFieldDesc) FieldDesc(org.talend.components.netsuite.client.model.FieldDesc)

Aggregations

FieldDesc (org.talend.components.netsuite.client.model.FieldDesc)24 Schema (org.apache.avro.Schema)14 CustomFieldDesc (org.talend.components.netsuite.client.model.CustomFieldDesc)14 ArrayList (java.util.ArrayList)10 TypeDesc (org.talend.components.netsuite.client.model.TypeDesc)10 Test (org.junit.Test)9 RecordTypeDesc (org.talend.components.netsuite.client.model.RecordTypeDesc)8 NsObjectInputTransducer (org.talend.components.netsuite.input.NsObjectInputTransducer)8 CustomRecordTypeInfo (org.talend.components.netsuite.client.model.CustomRecordTypeInfo)7 RecordTypeInfo (org.talend.components.netsuite.client.model.RecordTypeInfo)7 SearchRecordTypeDesc (org.talend.components.netsuite.client.model.SearchRecordTypeDesc)7 AvroConverter (org.talend.daikon.avro.converter.AvroConverter)6 NetSuiteException (org.talend.components.netsuite.client.NetSuiteException)5 ComponentException (org.talend.components.api.exception.ComponentException)4 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)3 XMLGregorianCalendar (javax.xml.datatype.XMLGregorianCalendar)3 IndexedRecord (org.apache.avro.generic.IndexedRecord)3 RecordRef (com.netsuite.webservices.test.platform.core.RecordRef)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2