Search in sources :

Example 1 with TableBeanHolderFilter

use of org.eclipse.scout.rt.platform.holders.TableBeanHolderFilter in project scout.rt by eclipse.

the class SelectInputBindTest method testBatchUpdateFromTableBeanHolderFilter.

/**
 * {@link TableFieldBeanData} in combination with {@link TableBeanHolderFilter} (introduced with Luna). Direct batch
 * update.
 */
@Test
public void testBatchUpdateFromTableBeanHolderFilter() throws Exception {
    SqlServiceMock sql = createSqlServiceMock();
    TableFieldBeanData tableData = createTableFieldBeanData(true);
    TableBeanHolderFilter filter = new TableBeanHolderFilter(tableData, ITableBeanRowHolder.STATUS_UPDATED);
    sql.update("UDPATE my_table SET a=:{active}, s=:{state} where n=:{name} ", filter);
    assertExpectedProtocol(sql);
}
Also used : TableBeanHolderFilter(org.eclipse.scout.rt.platform.holders.TableBeanHolderFilter) SqlServiceMock(org.eclipse.scout.rt.server.jdbc.fixture.SqlServiceMock) TableFieldBeanData(org.eclipse.scout.rt.server.jdbc.fixture.TableFieldBeanData) Test(org.junit.Test)

Example 2 with TableBeanHolderFilter

use of org.eclipse.scout.rt.platform.holders.TableBeanHolderFilter in project scout.rt by eclipse.

the class StatementProcessor method createInputRec.

@SuppressWarnings({ "squid:S2583", "squid:S138" })
private IBindInput createInputRec(ValueInputToken bindToken, String[] path, Object bindBase, Class nullType) {
    boolean terminal = (path.length == 1);
    Object o = null;
    boolean found = false;
    if (bindBase instanceof Map) {
        // handle all terminal cases for map
        o = ((Map) bindBase).get(path[0]);
        if (o != null) {
            found = true;
        } else if (((Map) bindBase).containsKey(path[0])) {
            found = true;
        }
        if (found) {
            if (o instanceof ITableBeanHolder) {
                return new TableBeanHolderInput((ITableBeanHolder) o, null, path[1], bindToken);
            } else if (o instanceof TableBeanHolderFilter) {
                return new TableBeanHolderInput(((TableBeanHolderFilter) o).getTableBeanHolder(), ((TableBeanHolderFilter) o).getFilteredRows(), path[1], bindToken);
            } else if (o instanceof IBeanArrayHolder) {
                return new BeanArrayHolderInput((IBeanArrayHolder) o, null, path[1], bindToken);
            } else if (o instanceof BeanArrayHolderFilter) {
                return new BeanArrayHolderInput(((BeanArrayHolderFilter) o).getBeanArrayHolder(), ((BeanArrayHolderFilter) o).getFilteredBeans(), path[1], bindToken);
            } else {
                if (terminal) {
                    return createInputTerminal(o, nullType, bindToken);
                } else {
                    if (o == null) {
                        throw new ProcessingException("input bind {} resolves to null on path element: {}", bindToken, path[0]);
                    }
                }
            }
        }
    } else if (bindBase instanceof NVPair) {
        // handle all terminal cases for nvpair
        if (((NVPair) bindBase).getName().equals(path[0])) {
            o = ((NVPair) bindBase).getValue();
            found = true;
            if (o instanceof ITableBeanHolder) {
                return new TableBeanHolderInput((ITableBeanHolder) o, null, path[1], bindToken);
            } else if (o instanceof TableBeanHolderFilter) {
                return new TableBeanHolderInput(((TableBeanHolderFilter) o).getTableBeanHolder(), ((TableBeanHolderFilter) o).getFilteredRows(), path[1], bindToken);
            } else if (o instanceof IBeanArrayHolder) {
                return new BeanArrayHolderInput((IBeanArrayHolder) o, null, path[1], bindToken);
            } else if (o instanceof BeanArrayHolderFilter) {
                return new BeanArrayHolderInput(((BeanArrayHolderFilter) o).getBeanArrayHolder(), ((BeanArrayHolderFilter) o).getFilteredBeans(), path[1], bindToken);
            } else {
                if (terminal) {
                    return createInputTerminal(o, nullType, bindToken);
                } else {
                    if (o == null) {
                        throw new ProcessingException("input bind {} resolves to null on path element: {}", bindToken, path[0]);
                    }
                }
            }
        }
    } else if (bindBase instanceof ITableBeanHolder) {
        // handle all terminal cases for table holder
        ITableBeanHolder table = (ITableBeanHolder) bindBase;
        try {
            Method m = table.getRowType().getMethod("get" + Character.toUpperCase(path[0].charAt(0)) + path[0].substring(1));
            if (m != null) {
                found = true;
                return new TableBeanHolderInput(table, null, path[0], bindToken);
            }
        } catch (NoSuchMethodException | SecurityException t) {
            found = false;
        // nop
        }
    } else if (bindBase instanceof TableBeanHolderFilter) {
        // handle all terminal cases for table holder filter
        TableBeanHolderFilter filter = (TableBeanHolderFilter) bindBase;
        ITableBeanHolder table = filter.getTableBeanHolder();
        try {
            Method m = table.getRowType().getMethod("get" + Character.toUpperCase(path[0].charAt(0)) + path[0].substring(1));
            if (m != null) {
                found = true;
                return new TableBeanHolderInput(table, filter.getFilteredRows(), path[0], bindToken);
            }
        } catch (NoSuchMethodException | SecurityException t) {
            // nop
            found = false;
        }
    } else if (bindBase instanceof IBeanArrayHolder) {
        // handle all terminal cases for BeanArrayHolder
        IBeanArrayHolder<?> holder = (IBeanArrayHolder) bindBase;
        try {
            Method m = holder.getHolderType().getMethod("get" + Character.toUpperCase(path[0].charAt(0)) + path[0].substring(1));
            if (m != null) {
                found = true;
                return new BeanArrayHolderInput(holder, null, path[0], bindToken);
            }
        } catch (NoSuchMethodException | SecurityException t1) {
            try {
                Method m = holder.getHolderType().getMethod("is" + Character.toUpperCase(path[0].charAt(0)) + path[0].substring(1));
                if (m != null) {
                    found = true;
                    return new BeanArrayHolderInput(holder, null, path[0], bindToken);
                }
            } catch (NoSuchMethodException | SecurityException t2) {
                found = false;
            // nop
            }
        }
    } else if (bindBase instanceof BeanArrayHolderFilter) {
        // handle all terminal cases for table holder filter
        BeanArrayHolderFilter filter = (BeanArrayHolderFilter) bindBase;
        IBeanArrayHolder<?> holder = filter.getBeanArrayHolder();
        try {
            Method m = holder.getHolderType().getMethod("get" + Character.toUpperCase(path[0].charAt(0)) + path[0].substring(1));
            if (m != null) {
                found = true;
                return new BeanArrayHolderInput(holder, filter.getFilteredBeans(), path[0], bindToken);
            }
        } catch (NoSuchMethodException | SecurityException t1) {
            try {
                Method m = holder.getHolderType().getMethod("is" + Character.toUpperCase(path[0].charAt(0)) + path[0].substring(1));
                if (m != null) {
                    found = true;
                    return new BeanArrayHolderInput(holder, null, path[0], bindToken);
                }
            } catch (NoSuchMethodException | SecurityException t2) {
                found = false;
            // nop
            }
        }
    } else if (bindBase != null) {
        if (bindBase.getClass().isArray() && terminal) {
            return new BeanPropertyInput(path[0], (Object[]) bindBase, bindToken);
        }
        if (bindBase instanceof Collection && terminal) {
            return new BeanPropertyInput(path[0], ((Collection) bindBase).toArray(), bindToken);
        }
        /* bean property */
        try {
            Object propertyBean = bindBase;
            FastPropertyDescriptor pd = BeanUtility.getFastBeanInfo(propertyBean.getClass(), null).getPropertyDescriptor(path[0]);
            Method getter = pd != null ? pd.getReadMethod() : null;
            if (getter != null) {
                // getter exists
                o = getter.invoke(propertyBean);
                found = true;
                if (terminal) {
                    return createInputTerminal(o, getter.getReturnType(), bindToken);
                } else {
                    if (o == null) {
                        throw new ProcessingException("input bind {} resolves to null on path element: {}", bindToken, path[0]);
                    }
                }
            }
        } catch (IllegalAccessException | InvocationTargetException e) {
            LOG.warn("Exception while invoking bean getter", e);
            LOG.debug("Cannot access property.", e);
        }
    }
    // 
    if (found) {
        if (terminal) {
            throw new ProcessingException("input bind '{}' was not recognized as a terminal", bindToken.getName());
        }
        // continue
        String[] newPath = new String[path.length - 1];
        System.arraycopy(path, 1, newPath, 0, newPath.length);
        IBindInput input = null;
        if (o instanceof IHolder<?>) {
            /* dereference value if current object is an IHolder. If input cannot be resolved (i.e. ProcessingException occurs),
         * search given property names on holder. Hence both of the following forms are supported on holder types:
         * :holder.property
         * :holder.value.property
         */
            try {
                input = createInputRec(bindToken, newPath, ((IHolder) o).getValue(), nullType);
            } catch (RuntimeException pe) {
            // nop, see below
            }
        }
        if (input == null) {
            // strict search without dereferncing holder objects
            input = createInputRec(bindToken, newPath, o, nullType);
        }
        return input;
    } else {
        return null;
    }
}
Also used : IHolder(org.eclipse.scout.rt.platform.holders.IHolder) FastPropertyDescriptor(org.eclipse.scout.rt.platform.reflect.FastPropertyDescriptor) TableBeanHolderFilter(org.eclipse.scout.rt.platform.holders.TableBeanHolderFilter) ProcessingException(org.eclipse.scout.rt.platform.exception.ProcessingException) BeanArrayHolderFilter(org.eclipse.scout.rt.platform.holders.BeanArrayHolderFilter) Method(java.lang.reflect.Method) ITableBeanHolder(org.eclipse.scout.rt.platform.holders.ITableBeanHolder) InvocationTargetException(java.lang.reflect.InvocationTargetException) NVPair(org.eclipse.scout.rt.platform.holders.NVPair) Collection(java.util.Collection) Map(java.util.Map) TreeMap(java.util.TreeMap) IBeanArrayHolder(org.eclipse.scout.rt.platform.holders.IBeanArrayHolder)

Example 3 with TableBeanHolderFilter

use of org.eclipse.scout.rt.platform.holders.TableBeanHolderFilter in project scout.rt by eclipse.

the class SelectInputBindTest method testBatchUpdateFromTableBeanHolderFilterInBean.

/**
 * {@link TableFieldBeanData} in combination with {@link TableBeanHolderFilter} (introduced with Luna). TableData for
 * batch update is in a bean (ContainerBean).
 */
@Test
public void testBatchUpdateFromTableBeanHolderFilterInBean() throws Exception {
    SqlServiceMock sql = createSqlServiceMock();
    TableFieldBeanData tableData = createTableFieldBeanData(true);
    TableBeanHolderFilter filter = new TableBeanHolderFilter(tableData, ITableBeanRowHolder.STATUS_UPDATED);
    ContainerBean bean = new ContainerBean();
    bean.setTableBeanHolderFilter(filter);
    sql.update("UDPATE my_table SET a=:{TableBeanHolderFilter.active}, s=:{TableBeanHolderFilter.state} where n=:{TableBeanHolderFilter.name} ", bean);
    assertExpectedProtocol(sql);
}
Also used : TableBeanHolderFilter(org.eclipse.scout.rt.platform.holders.TableBeanHolderFilter) ContainerBean(org.eclipse.scout.rt.server.jdbc.fixture.ContainerBean) SqlServiceMock(org.eclipse.scout.rt.server.jdbc.fixture.SqlServiceMock) TableFieldBeanData(org.eclipse.scout.rt.server.jdbc.fixture.TableFieldBeanData) Test(org.junit.Test)

Example 4 with TableBeanHolderFilter

use of org.eclipse.scout.rt.platform.holders.TableBeanHolderFilter in project scout.rt by eclipse.

the class SelectInputBindTest method testBatchUpdateFromTableBeanHolderFilterInNVPair.

/**
 * {@link TableFieldBeanData} in combination with {@link TableBeanHolderFilter} (introduced with Luna). TableData for
 * batch update is in NVPair bind.
 */
@Test
public void testBatchUpdateFromTableBeanHolderFilterInNVPair() throws Exception {
    SqlServiceMock sql = createSqlServiceMock();
    TableFieldBeanData tableData = createTableFieldBeanData(true);
    TableBeanHolderFilter filter = new TableBeanHolderFilter(tableData, ITableBeanRowHolder.STATUS_UPDATED);
    sql.update("UDPATE my_table SET a=:{filter.active}, s=:{filter.state} where n=:{filter.name} ", new NVPair("filter", filter));
    assertExpectedProtocol(sql);
}
Also used : TableBeanHolderFilter(org.eclipse.scout.rt.platform.holders.TableBeanHolderFilter) SqlServiceMock(org.eclipse.scout.rt.server.jdbc.fixture.SqlServiceMock) NVPair(org.eclipse.scout.rt.platform.holders.NVPair) TableFieldBeanData(org.eclipse.scout.rt.server.jdbc.fixture.TableFieldBeanData) Test(org.junit.Test)

Example 5 with TableBeanHolderFilter

use of org.eclipse.scout.rt.platform.holders.TableBeanHolderFilter in project scout.rt by eclipse.

the class SelectInputBindTest method testBatchUpdateFromTableBeanHolderFilterInMap.

/**
 * {@link TableFieldBeanData} in combination with {@link TableBeanHolderFilter} (introduced with Luna). TableData for
 * batch update is in Map bind.
 */
@Test
public void testBatchUpdateFromTableBeanHolderFilterInMap() throws Exception {
    SqlServiceMock sql = createSqlServiceMock();
    TableFieldBeanData tableData = createTableFieldBeanData(true);
    TableBeanHolderFilter filter = new TableBeanHolderFilter(tableData, ITableBeanRowHolder.STATUS_UPDATED);
    Map<String, ?> map = Collections.singletonMap("filter", filter);
    sql.update("UDPATE my_table SET a=:{filter.active}, s=:{filter.state} where n=:{filter.name} ", map);
    assertExpectedProtocol(sql);
}
Also used : TableBeanHolderFilter(org.eclipse.scout.rt.platform.holders.TableBeanHolderFilter) SqlServiceMock(org.eclipse.scout.rt.server.jdbc.fixture.SqlServiceMock) TableFieldBeanData(org.eclipse.scout.rt.server.jdbc.fixture.TableFieldBeanData) Test(org.junit.Test)

Aggregations

TableBeanHolderFilter (org.eclipse.scout.rt.platform.holders.TableBeanHolderFilter)5 SqlServiceMock (org.eclipse.scout.rt.server.jdbc.fixture.SqlServiceMock)4 TableFieldBeanData (org.eclipse.scout.rt.server.jdbc.fixture.TableFieldBeanData)4 Test (org.junit.Test)4 NVPair (org.eclipse.scout.rt.platform.holders.NVPair)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 Collection (java.util.Collection)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1 ProcessingException (org.eclipse.scout.rt.platform.exception.ProcessingException)1 BeanArrayHolderFilter (org.eclipse.scout.rt.platform.holders.BeanArrayHolderFilter)1 IBeanArrayHolder (org.eclipse.scout.rt.platform.holders.IBeanArrayHolder)1 IHolder (org.eclipse.scout.rt.platform.holders.IHolder)1 ITableBeanHolder (org.eclipse.scout.rt.platform.holders.ITableBeanHolder)1 FastPropertyDescriptor (org.eclipse.scout.rt.platform.reflect.FastPropertyDescriptor)1 ContainerBean (org.eclipse.scout.rt.server.jdbc.fixture.ContainerBean)1