Search in sources :

Example 1 with FastPropertyDescriptor

use of org.eclipse.scout.rt.platform.reflect.FastPropertyDescriptor in project scout.rt by eclipse.

the class TableRowDataMapper method exportTableRowData.

@Override
public void exportTableRowData(ITableRow row, AbstractTableRowData rowData) {
    for (IColumn column : m_columnSet.getColumns()) {
        if (m_ignoredColumns.contains(column)) {
            continue;
        }
        Object value = column.getValue(row);
        FastPropertyDescriptor propertyDesc = m_propertyDescriptorByColumn.get(column);
        if (propertyDesc != null) {
            try {
                Method columnWriteMethod = propertyDesc.getWriteMethod();
                Object dto = getDataContainer(rowData, columnWriteMethod.getDeclaringClass());
                columnWriteMethod.invoke(dto, value);
            } catch (Exception t) {
                LOG.warn("Error writing row data property for column [{}]", column.getClass().getName(), t);
            }
        } else {
            rowData.setCustomValue(column.getColumnId(), value);
        }
    }
    rowData.setRowState(row.getStatus());
    exportCustomValues(row, rowData);
}
Also used : IColumn(org.eclipse.scout.rt.client.ui.basic.table.columns.IColumn) Method(java.lang.reflect.Method) FastPropertyDescriptor(org.eclipse.scout.rt.platform.reflect.FastPropertyDescriptor)

Example 2 with FastPropertyDescriptor

use of org.eclipse.scout.rt.platform.reflect.FastPropertyDescriptor in project scout.rt by eclipse.

the class TableRowDataMapper method getValue.

private Object getValue(IColumn<?> column, AbstractTableRowData rowData) {
    Object value = null;
    FastPropertyDescriptor propertyDesc = m_propertyDescriptorByColumn.get(column);
    if (propertyDesc != null) {
        try {
            Method columnReadMethod = propertyDesc.getReadMethod();
            Object dto = getDataContainer(rowData, columnReadMethod.getDeclaringClass());
            value = columnReadMethod.invoke(dto);
        } catch (Exception e) {
            LOG.warn("Error reading row data property for column [{}]", column.getClass().getName(), e);
        }
    } else {
        value = rowData.getCustomValue(column.getColumnId());
    }
    return value;
}
Also used : Method(java.lang.reflect.Method) FastPropertyDescriptor(org.eclipse.scout.rt.platform.reflect.FastPropertyDescriptor)

Example 3 with FastPropertyDescriptor

use of org.eclipse.scout.rt.platform.reflect.FastPropertyDescriptor in project scout.rt by eclipse.

the class TableRowDataPropertyFilterTest method testTableRowDataPropertyFilter.

@Test
public void testTableRowDataPropertyFilter() throws Exception {
    TableRowDataPropertyFilter propertyFilter = new TableRowDataPropertyFilter();
    FastPropertyDescriptor[] props = BeanUtility.getFastPropertyDescriptors(P_TableRowData.class, AbstractTableRowData.class, propertyFilter);
    Map<String, FastPropertyDescriptor> propertyDescriptorsByName = new HashMap<String, FastPropertyDescriptor>();
    for (FastPropertyDescriptor prop : props) {
        propertyDescriptorsByName.put(prop.getName(), prop);
    }
    assertEquals(3, propertyDescriptorsByName.size());
    assertTrue(propertyDescriptorsByName.containsKey("intColumn"));
    assertTrue(propertyDescriptorsByName.containsKey("booleanColumn"));
    assertTrue(propertyDescriptorsByName.containsKey("objectColumn"));
}
Also used : TableRowDataPropertyFilter(org.eclipse.scout.rt.shared.data.form.fields.tablefield.TableRowDataPropertyFilter) HashMap(java.util.HashMap) FastPropertyDescriptor(org.eclipse.scout.rt.platform.reflect.FastPropertyDescriptor) Test(org.junit.Test)

Example 4 with FastPropertyDescriptor

use of org.eclipse.scout.rt.platform.reflect.FastPropertyDescriptor in project scout.rt by eclipse.

the class JsonObjectUtility method jsonValueToJava.

@SuppressWarnings("unchecked")
private static <T> T jsonValueToJava(Object jval, Class<T> type, boolean throwForMissingProperty) {
    if (jval == null || jval == JSONObject.NULL) {
        return null;
    }
    // basic types
    if (type == String.class) {
        return (T) jval;
    }
    if (type == byte[].class) {
        return (T) new JsonByteArray((String) jval).getBytes();
    }
    if (type == Date.class) {
        return (T) new JsonDate((String) jval).asJavaDate();
    }
    if (type == int.class || type == Integer.class) {
        return (T) jval;
    }
    if (type == long.class || type == Long.class) {
        return (T) jval;
    }
    if (type == boolean.class || type == Boolean.class) {
        return (T) jval;
    }
    // array
    if (jval instanceof JSONArray) {
        JSONArray jarray = (JSONArray) jval;
        int n = jarray.length();
        T array = (T) Array.newInstance(type.getComponentType(), n);
        for (int i = 0; i < n; i++) {
            Array.set(array, i, jsonArrayElementToJava(jarray, i, type.getComponentType(), throwForMissingProperty));
        }
        return array;
    }
    // bean
    JSONObject jbean = (JSONObject) jval;
    T o;
    try {
        o = (T) type.newInstance();
    } catch (Exception e) {
        throw new IllegalArgumentException("type " + type + " object " + jval, e);
    }
    try {
        HashSet<String> missingNames = new HashSet<>();
        String[] nameArray = getNames(jbean);
        if (nameArray != null) {
            for (String key : nameArray) {
                missingNames.add(key);
            }
            for (String key : nameArray) {
                try {
                    // NOSONAR
                    Field f = type.getField(key);
                    if (Modifier.isStatic(f.getModifiers())) {
                        continue;
                    }
                    Object val = jsonObjectPropertyToJava(jbean, key, f.getType(), throwForMissingProperty);
                    f.set(o, val);
                    missingNames.remove(key);
                } catch (NoSuchElementException | NoSuchFieldException e) {
                // NOSONAR
                // nop
                }
            }
        }
        if (missingNames.size() > 0) {
            FastBeanInfo beanInfo = new FastBeanInfo(type, Object.class);
            for (FastPropertyDescriptor desc : beanInfo.getPropertyDescriptors()) {
                Method m = desc.getWriteMethod();
                if (m == null) {
                    continue;
                }
                String key = desc.getName();
                Object val = jsonObjectPropertyToJava(jbean, key, m.getParameterTypes()[0], throwForMissingProperty);
                m.invoke(o, val);
                missingNames.remove(key);
            }
        }
        if (throwForMissingProperty && missingNames.size() > 0) {
            throw new IllegalArgumentException("properties " + missingNames + " do not exist in " + type);
        }
        return o;
    } catch (IllegalAccessException | InvocationTargetException | RuntimeException e) {
        throw new IllegalArgumentException(jbean + " to " + type, e);
    }
}
Also used : FastPropertyDescriptor(org.eclipse.scout.rt.platform.reflect.FastPropertyDescriptor) FastBeanInfo(org.eclipse.scout.rt.platform.reflect.FastBeanInfo) Field(java.lang.reflect.Field) HashSet(java.util.HashSet) JSONArray(org.json.JSONArray) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) JSONException(org.json.JSONException) NoSuchElementException(java.util.NoSuchElementException) InvocationTargetException(java.lang.reflect.InvocationTargetException) JSONObject(org.json.JSONObject) JSONObject(org.json.JSONObject) NoSuchElementException(java.util.NoSuchElementException)

Example 5 with FastPropertyDescriptor

use of org.eclipse.scout.rt.platform.reflect.FastPropertyDescriptor in project scout.rt by eclipse.

the class JsonBean method toJson.

@Override
public Object toJson() {
    if (m_bean == null) {
        return null;
    }
    Class<?> type = m_bean.getClass();
    // basic types
    if (type.isPrimitive() || type == String.class || type == Boolean.class || Number.class.isAssignableFrom(type)) {
        return m_bean;
    }
    // binary resource
    if (BinaryResource.class.isAssignableFrom(type)) {
        BinaryResource binaryResource = (BinaryResource) m_bean;
        m_binaryResourceMediator.addBinaryResource(binaryResource);
        return m_binaryResourceMediator.createUrl(binaryResource);
    }
    // array
    if (type.isArray()) {
        JSONArray jsonArray = new JSONArray();
        int n = Array.getLength(m_bean);
        for (int i = 0; i < n; i++) {
            IJsonObject jsonObject = createJsonObject(Array.get(m_bean, i));
            jsonArray.put(jsonObject.toJson());
        }
        return jsonArray;
    }
    // collection
    if (Collection.class.isAssignableFrom(type)) {
        JSONArray jsonArray = new JSONArray();
        Collection collection = (Collection) m_bean;
        for (Object object : collection) {
            IJsonObject jsonObject = createJsonObject(object);
            jsonArray.put(jsonObject.toJson());
        }
        return jsonArray;
    }
    // Map
    if (Map.class.isAssignableFrom(type)) {
        JSONObject jsonMap = new JSONObject();
        Map map = (Map) m_bean;
        @SuppressWarnings("unchecked") Set<Entry> entries = (Set<Entry>) map.entrySet();
        for (Entry entry : entries) {
            if (!(entry.getKey() instanceof String)) {
                throw new IllegalArgumentException("Cannot convert " + type + " to json object");
            }
            IJsonObject jsonObject = createJsonObject(entry.getValue());
            jsonMap.put((String) entry.getKey(), jsonObject.toJson());
        }
        return jsonMap;
    }
    // bean
    if (type.getName().startsWith("java.")) {
        throw new IllegalArgumentException("Cannot convert " + type + " to json object");
    }
    try {
        TreeMap<String, Object> properties = new TreeMap<>();
        for (Field f : type.getFields()) {
            if (Modifier.isStatic(f.getModifiers())) {
                continue;
            }
            String key = f.getName();
            Object val = f.get(m_bean);
            IJsonObject jsonObject = createJsonObject(val);
            properties.put(key, jsonObject.toJson());
        }
        FastBeanInfo beanInfo = new FastBeanInfo(type, Object.class);
        for (FastPropertyDescriptor desc : beanInfo.getPropertyDescriptors()) {
            Method m = desc.getReadMethod();
            if (m == null) {
                continue;
            }
            // skip ignored annotated getters with context GUI
            IgnoreProperty ignoredPropertyAnnotation = m.getAnnotation(IgnoreProperty.class);
            if (ignoredPropertyAnnotation != null && Context.GUI.equals(ignoredPropertyAnnotation.value())) {
                continue;
            }
            String key = desc.getName();
            Object val = m.invoke(m_bean);
            IJsonObject jsonObject = createJsonObject(val);
            properties.put(key, jsonObject.toJson());
        }
        JSONObject jbean = new JSONObject();
        for (Map.Entry<String, Object> e : properties.entrySet()) {
            jbean.put(e.getKey(), e.getValue());
        }
        return jbean;
    } catch (Exception e) {
        throw new IllegalArgumentException(type + " to json", e);
    }
}
Also used : Set(java.util.Set) FastPropertyDescriptor(org.eclipse.scout.rt.platform.reflect.FastPropertyDescriptor) FastBeanInfo(org.eclipse.scout.rt.platform.reflect.FastBeanInfo) Field(java.lang.reflect.Field) Entry(java.util.Map.Entry) JSONArray(org.json.JSONArray) Method(java.lang.reflect.Method) TreeMap(java.util.TreeMap) BinaryResource(org.eclipse.scout.rt.platform.resource.BinaryResource) JSONObject(org.json.JSONObject) IgnoreProperty(org.eclipse.scout.rt.platform.annotations.IgnoreProperty) Collection(java.util.Collection) JSONObject(org.json.JSONObject) TreeMap(java.util.TreeMap) Map(java.util.Map)

Aggregations

FastPropertyDescriptor (org.eclipse.scout.rt.platform.reflect.FastPropertyDescriptor)13 Method (java.lang.reflect.Method)8 ProcessingException (org.eclipse.scout.rt.platform.exception.ProcessingException)5 FastBeanInfo (org.eclipse.scout.rt.platform.reflect.FastBeanInfo)5 Map (java.util.Map)4 TreeMap (java.util.TreeMap)4 IHolder (org.eclipse.scout.rt.platform.holders.IHolder)4 HashMap (java.util.HashMap)3 Field (java.lang.reflect.Field)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 Collection (java.util.Collection)2 IBeanArrayHolder (org.eclipse.scout.rt.platform.holders.IBeanArrayHolder)2 ITableBeanHolder (org.eclipse.scout.rt.platform.holders.ITableBeanHolder)2 NVPair (org.eclipse.scout.rt.platform.holders.NVPair)2 JSONArray (org.json.JSONArray)2 JSONObject (org.json.JSONObject)2 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 HashSet (java.util.HashSet)1 Entry (java.util.Map.Entry)1