Search in sources :

Example 6 with BeanInfo

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

the class SearchFieldAdapter method createField.

/**
 * Create instance of NetSuite's search field.
 *
 * @param internalId internal identifier to be applied to a search field
 * @return search field object
 * @throws NetSuiteException if an error occurs during creation of a search field
 */
protected T createField(String internalId) throws NetSuiteException {
    try {
        BeanInfo fieldTypeMetaData = Beans.getBeanInfo(fieldClass);
        T searchField = fieldClass.newInstance();
        if (fieldTypeMetaData.getProperty("internalId") != null && internalId != null) {
            setProperty(searchField, "internalId", internalId);
        }
        return searchField;
    } catch (IllegalAccessException | IllegalArgumentException | InstantiationException e) {
        throw new NetSuiteException(e.getMessage(), e);
    }
}
Also used : BeanInfo(org.talend.components.netsuite.client.model.beans.BeanInfo) NetSuiteException(org.talend.components.netsuite.client.NetSuiteException)

Example 7 with BeanInfo

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

the class NsRef method fromNativeRef.

/**
 * Create ref object from NetSuite's native ref data object.
 *
 * @param ref native ref data object
 * @return ref object
 */
public static NsRef fromNativeRef(Object ref) {
    String typeName = ref.getClass().getSimpleName();
    RefType refType = RefType.getByTypeName(typeName);
    NsRef nsRef = new NsRef();
    nsRef.setRefType(refType);
    BeanInfo beanInfo = Beans.getBeanInfo(ref.getClass());
    nsRef.setInternalId((String) getSimpleProperty(ref, "internalId"));
    nsRef.setExternalId((String) getSimpleProperty(ref, "externalId"));
    if (refType == RefType.RECORD_REF) {
        nsRef.setType(Beans.getEnumAccessor((Class<Enum>) beanInfo.getProperty("type").getReadType()).getStringValue((Enum) getSimpleProperty(ref, "type")));
    } else if (refType == RefType.CUSTOM_RECORD_REF) {
        nsRef.setTypeId((String) getSimpleProperty(ref, "typeId"));
    } else if (refType == RefType.CUSTOMIZATION_REF) {
        nsRef.setScriptId((String) getSimpleProperty(ref, "scriptId"));
    }
    return nsRef;
}
Also used : RefType(org.talend.components.netsuite.client.model.RefType) BeanInfo(org.talend.components.netsuite.client.model.beans.BeanInfo)

Example 8 with BeanInfo

use of org.talend.components.netsuite.client.model.beans.BeanInfo 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;
}
Also used : HashMap(java.util.HashMap) BeanInfo(org.talend.components.netsuite.client.model.beans.BeanInfo) Schema(org.apache.avro.Schema) ArrayList(java.util.ArrayList) List(java.util.List) CustomFieldDesc(org.talend.components.netsuite.client.model.CustomFieldDesc) SimpleFieldDesc(org.talend.components.netsuite.client.model.SimpleFieldDesc) CustomFieldDesc(org.talend.components.netsuite.client.model.CustomFieldDesc) FieldDesc(org.talend.components.netsuite.client.model.FieldDesc)

Example 9 with BeanInfo

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

the class AbstractNetSuiteTestBase method composeObject.

protected static <T> T composeObject(Class<T> clazz) throws Exception {
    BeanInfo beanInfo = Beans.getBeanInfo(clazz);
    List<PropertyInfo> propertyInfoList = beanInfo.getProperties();
    T obj = clazz.newInstance();
    for (PropertyInfo propertyInfo : propertyInfoList) {
        if (propertyInfo.getWriteType() != null) {
            Object value;
            if (propertyInfo.getName().equals("internalId")) {
                value = composeValue(Integer.class).toString();
            } else {
                value = composeValue(propertyInfo.getWriteType());
            }
            setProperty(obj, propertyInfo.getName(), value);
        }
    }
    return obj;
}
Also used : BeanInfo(org.talend.components.netsuite.client.model.beans.BeanInfo) PropertyInfo(org.talend.components.netsuite.client.model.beans.PropertyInfo)

Example 10 with BeanInfo

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

the class NsRef method toNativeRef.

/**
 * Create NetSuite's native ref data object from this ref object.
 *
 * @param basicMetaData basic meta data to be used
 * @return ref data object
 */
public Object toNativeRef(BasicMetaData basicMetaData) {
    Object ref = basicMetaData.createInstance(refType.getTypeName());
    BeanInfo beanInfo = Beans.getBeanInfo(ref.getClass());
    setSimpleProperty(ref, "internalId", internalId);
    setSimpleProperty(ref, "externalId", externalId);
    if (refType == RefType.CUSTOMIZATION_REF || refType == RefType.CUSTOM_RECORD_REF) {
        setSimpleProperty(ref, "scriptId", scriptId);
    }
    if (refType == RefType.CUSTOM_RECORD_REF) {
        setSimpleProperty(ref, "typeId", typeId);
    } else {
        setSimpleProperty(ref, "type", Beans.getEnumAccessor((Class<Enum>) beanInfo.getProperty("type").getWriteType()).getEnumValue(type));
    }
    return ref;
}
Also used : BeanInfo(org.talend.components.netsuite.client.model.beans.BeanInfo)

Aggregations

BeanInfo (org.talend.components.netsuite.client.model.beans.BeanInfo)12 PropertyInfo (org.talend.components.netsuite.client.model.beans.PropertyInfo)6 HashMap (java.util.HashMap)4 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Schema (org.apache.avro.Schema)2 NetSuiteException (org.talend.components.netsuite.client.NetSuiteException)2 FieldDesc (org.talend.components.netsuite.client.model.FieldDesc)2 SearchFieldType (org.talend.components.netsuite.client.model.search.SearchFieldType)2 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 CustomFieldRef (com.netsuite.webservices.test.platform.core.CustomFieldRef)1 CustomFieldRef (com.netsuite.webservices.v2014_2.platform.core.CustomFieldRef)1 RecordType (com.netsuite.webservices.v2014_2.platform.core.types.RecordType)1 CustomizationFieldType (com.netsuite.webservices.v2014_2.setup.customization.types.CustomizationFieldType)1 CustomFieldRef (com.netsuite.webservices.v2016_2.platform.core.CustomFieldRef)1 HashSet (java.util.HashSet)1 CustomFieldSpec (org.talend.components.netsuite.CustomFieldSpec)1 NsRef (org.talend.components.netsuite.client.NsRef)1 CustomFieldDesc (org.talend.components.netsuite.client.model.CustomFieldDesc)1 CustomRecordTypeInfo (org.talend.components.netsuite.client.model.CustomRecordTypeInfo)1