Search in sources :

Example 6 with IHolder

use of org.eclipse.scout.rt.platform.holders.IHolder 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 7 with IHolder

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

the class AbstractSqlStyle method toPlainText.

@Override
public String toPlainText(Object value) {
    if (value instanceof IHolder) {
        value = ((IHolder) value).getValue();
    }
    // 
    if (value == null) {
        return "null";
    } else if (value instanceof Boolean) {
        Boolean b = (Boolean) value;
        return b ? "1" : "0";
    } else if (value instanceof TriState) {
        TriState t = (TriState) value;
        return "" + t.toString();
    } else if (value instanceof String) {
        String s = (String) value;
        if (s.length() > MAX_SQL_STRING_LENGTH) {
            s = s.substring(0, MAX_SQL_STRING_LENGTH);
            LOG.warn("toPlainText of a String with more than {} characters failed; truncated to '{}'", MAX_SQL_STRING_LENGTH, s);
            return "'" + s.replaceAll("'", "''") + "'";
        }
        return "'" + s.replaceAll("'", "''") + "'";
    } else if (value instanceof char[]) {
        if (((char[]) value).length > MAX_SQL_STRING_LENGTH) {
            String s = new String((char[]) value, 0, MAX_SQL_STRING_LENGTH);
            LOG.warn("toPlainText of a CLOB with more than {} characters failed; truncated to '{}'", MAX_SQL_STRING_LENGTH, s);
            return "'" + s.replaceAll("'", "''") + "'";
        }
        String s = new String((char[]) value);
        return "'" + s.replaceAll("'", "''") + "'";
    } else if (value instanceof byte[]) {
        LOG.warn("toPlainText of a BLOB failed; using NULL");
        return "NULL";
    } else if (value instanceof Date) {
        Date d = (Date) value;
        SimpleDateFormat fmt = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss");
        return "to_date('" + fmt.format(d) + "','dd.mm.yyyy hh24:mi:ss')";
    } else if (value instanceof Collection || value.getClass().isArray()) {
        Object array;
        if (value instanceof Collection) {
            array = ((Collection) value).toArray();
        } else {
            array = value;
        }
        int n = Array.getLength(array);
        StringBuilder buf = new StringBuilder();
        buf.append("(");
        if (n > 0) {
            for (int i = 0; i < n; i++) {
                if (i > 0) {
                    buf.append(",");
                }
                buf.append(toPlainText(Array.get(array, i)));
            }
        } else {
            buf.append("-1");
        }
        buf.append(")");
        return buf.toString();
    } else {
        return value.toString();
    }
}
Also used : IHolder(org.eclipse.scout.rt.platform.holders.IHolder) Collection(java.util.Collection) TriState(org.eclipse.scout.rt.platform.util.TriState) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date)

Example 8 with IHolder

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

the class AbstractSqlStyle method buildBindFor.

@Override
public SqlBind buildBindFor(Object o, Class nullType) {
    if (o instanceof IHolder) {
        IHolder h = (IHolder) o;
        o = h.getValue();
        nullType = h.getHolderType();
    }
    // 
    Class c;
    if (o != null) {
        c = o.getClass();
    } else {
        if (nullType != null && IHolder.class.isAssignableFrom(nullType)) {
            try {
                nullType = TypeCastUtility.getGenericsParameterClass(nullType, IHolder.class);
            } catch (RuntimeException t) {
                LOG.debug("Could not determine type parameter class", t);
                nullType = null;
            }
        }
        c = nullType;
    }
    // 
    if (o == null && c == null) {
        return new SqlBind(Types.NULL, o);
    }
    return createBindFor(o, c);
}
Also used : SqlBind(org.eclipse.scout.rt.server.jdbc.SqlBind) IHolder(org.eclipse.scout.rt.platform.holders.IHolder)

Example 9 with IHolder

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

the class ServiceUtility method extractHolderArguments.

/**
 * Extract holders and nvpairs in callerArgs (and eventually in sub-arrays)
 */
public Object[] extractHolderArguments(Object[] callerArgs) {
    Object[] holderArgs = new Object[callerArgs.length];
    new AbstractHolderArgumentVisitor() {

        @Override
        public void visitHolder(IHolder input, IHolder output) {
        // do nothing
        }

        @Override
        public void visitOther(Object[] input, Object[] output, int index) {
        // do nothing
        }
    }.startVisiting(callerArgs, holderArgs, 1, true);
    return holderArgs;
}
Also used : AbstractHolderArgumentVisitor(org.eclipse.scout.rt.shared.servicetunnel.internal.AbstractHolderArgumentVisitor) IHolder(org.eclipse.scout.rt.platform.holders.IHolder)

Aggregations

IHolder (org.eclipse.scout.rt.platform.holders.IHolder)9 ProcessingException (org.eclipse.scout.rt.platform.exception.ProcessingException)4 FastPropertyDescriptor (org.eclipse.scout.rt.platform.reflect.FastPropertyDescriptor)3 Method (java.lang.reflect.Method)2 Collection (java.util.Collection)2 Map (java.util.Map)2 TreeMap (java.util.TreeMap)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 AbstractHolderArgumentVisitor (org.eclipse.scout.rt.shared.servicetunnel.internal.AbstractHolderArgumentVisitor)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Date (java.util.Date)1 IFormField (org.eclipse.scout.rt.client.ui.form.fields.IFormField)1 BeanArrayHolderFilter (org.eclipse.scout.rt.platform.holders.BeanArrayHolderFilter)1 Holder (org.eclipse.scout.rt.platform.holders.Holder)1 TableBeanHolderFilter (org.eclipse.scout.rt.platform.holders.TableBeanHolderFilter)1 TriState (org.eclipse.scout.rt.platform.util.TriState)1 AbstractSqlService (org.eclipse.scout.rt.server.jdbc.AbstractSqlService)1