Search in sources :

Example 11 with NsRef

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);
}
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 12 with NsRef

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;
}
Also used : SearchRecordTypeDesc(org.talend.components.netsuite.client.model.SearchRecordTypeDesc) RecordTypeDesc(org.talend.components.netsuite.client.model.RecordTypeDesc) NsRef(org.talend.components.netsuite.client.NsRef) CustomRecordTypeInfo(org.talend.components.netsuite.client.model.CustomRecordTypeInfo)

Example 13 with NsRef

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);
}
Also used : NsRef(org.talend.components.netsuite.client.NsRef) Test(org.junit.Test)

Example 14 with NsRef

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());
}
Also used : CustomizationRef(com.netsuite.webservices.test.platform.core.CustomizationRef) NsRef(org.talend.components.netsuite.client.NsRef) Test(org.junit.Test)

Example 15 with NsRef

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());
}
Also used : CustomRecordRef(com.netsuite.webservices.test.platform.core.CustomRecordRef) NsRef(org.talend.components.netsuite.client.NsRef) Test(org.junit.Test)

Aggregations

NsRef (org.talend.components.netsuite.client.NsRef)23 Test (org.junit.Test)7 CustomFieldRefType (org.talend.components.netsuite.client.model.customfield.CustomFieldRefType)5 ArrayList (java.util.ArrayList)4 List (java.util.List)4 CustomRecordTypeInfo (org.talend.components.netsuite.client.model.CustomRecordTypeInfo)4 RecordTypeDesc (org.talend.components.netsuite.client.model.RecordTypeDesc)4 Schema (org.apache.avro.Schema)3 CustomFieldDesc (org.talend.components.netsuite.client.model.CustomFieldDesc)3 CustomRecordRef (com.netsuite.webservices.test.platform.core.CustomRecordRef)2 NetSuitePortType (com.netsuite.webservices.v2016_2.platform.NetSuitePortType)2 CustomizationRef (com.netsuite.webservices.v2016_2.platform.core.CustomizationRef)2 StopWatch (org.apache.commons.lang3.time.StopWatch)2 NetSuiteClientService (org.talend.components.netsuite.client.NetSuiteClientService)2 NetSuiteException (org.talend.components.netsuite.client.NetSuiteException)2 FieldDesc (org.talend.components.netsuite.client.model.FieldDesc)2 SearchRecordTypeDesc (org.talend.components.netsuite.client.model.SearchRecordTypeDesc)2 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 CustomizationRef (com.netsuite.webservices.test.platform.core.CustomizationRef)1 RecordRef (com.netsuite.webservices.test.platform.core.RecordRef)1