Search in sources :

Example 1 with MapParameterBean

use of org.dbflute.twowaysql.pmbean.MapParameterBean in project dbflute-core by dbflute.

the class BoundValueTracer method getFilteringBindOption.

protected FilteringBindOption getFilteringBindOption(DfBeanDesc beanDesc, String currentName, Object pmb) {
    final String propertyName = buildLikeSearchPropertyName(currentName);
    final FilteringBindOption option;
    if (beanDesc.hasPropertyDesc(propertyName)) {
        // main case
        final DfPropertyDesc pb = beanDesc.getPropertyDesc(propertyName);
        option = (FilteringBindOption) pb.getValue(pmb);
    } else if (MapParameterBean.class.isInstance(pmb)) {
        final Map<?, ?> map = ((MapParameterBean<?>) pmb).getParameterMap();
        option = (FilteringBindOption) map.get(propertyName);
    } else if (Map.class.isInstance(pmb)) {
        option = (FilteringBindOption) ((Map<?, ?>) pmb).get(propertyName);
    } else {
        // no way
        String msg = "Not found the like-search property: name=" + propertyName;
        throw new IllegalStateException(msg);
    }
    // (parameter-bean checks option's existence and wrong operation)
    return option;
}
Also used : DfPropertyDesc(org.dbflute.helper.beans.DfPropertyDesc) MapParameterBean(org.dbflute.twowaysql.pmbean.MapParameterBean) Map(java.util.Map)

Example 2 with MapParameterBean

use of org.dbflute.twowaysql.pmbean.MapParameterBean in project dbflute-core by dbflute.

the class IfCommentEvaluator method processOneProperty.

protected Object processOneProperty(Object baseObject, String firstProperty, String property) {
    if (baseObject == null) {
        throwIfCommentNullPointerException(firstProperty);
    }
    final DfBeanDesc beanDesc = DfBeanDescFactory.getBeanDesc(baseObject.getClass());
    if (beanDesc.hasPropertyDesc(property)) {
        // main case
        final DfPropertyDesc propertyDesc = beanDesc.getPropertyDesc(property);
        try {
            return propertyDesc.getValue(baseObject);
        } catch (DfBeanIllegalPropertyException e) {
            throwIfCommentPropertyReadFailureException(baseObject, propertyDesc.getPropertyName(), e);
            // unreachable
            return null;
        }
    }
    if (property.endsWith(METHOD_SUFFIX)) {
        // sub-main case
        final String methodName = property.substring(0, property.length() - METHOD_SUFFIX.length());
        try {
            final Method method = beanDesc.getMethod(methodName);
            return DfReflectionUtil.invoke(method, baseObject, (Object[]) null);
        } catch (DfBeanMethodNotFoundException e) {
            throwIfCommentNotFoundMethodException(baseObject, methodName);
            // unreachable
            return null;
        } catch (ReflectionFailureException e) {
            throwIfCommentMethodInvocationFailureException(baseObject, methodName, e);
            // unreachable
            return null;
        }
    }
    if (MapParameterBean.class.isInstance(baseObject)) {
        // used by union-query internally
        // if the key does not exist, it does not process
        // (different specification with Map)
        final Map<?, ?> map = ((MapParameterBean<?>) baseObject).getParameterMap();
        if (map.containsKey(property)) {
            return map.get(property);
        }
    }
    if (Map.class.isInstance(baseObject)) {
        // if the key does not exist, treated same as a null value
        final Map<?, ?> map = (Map<?, ?>) baseObject;
        return map.get(property);
    }
    if (List.class.isInstance(baseObject)) {
        if (property.startsWith("get(") && property.endsWith(")")) {
            final List<?> list = (List<?>) baseObject;
            final String exp = Srl.extractScopeFirst(property, "get(", ")").getContent();
            try {
                final Integer index = DfTypeUtil.toInteger(exp);
                return list.get(index);
            } catch (NumberFormatException e) {
                throwIfCommentListIndexNotNumberException(list, exp, e);
                // unreachable
                return null;
            } catch (IndexOutOfBoundsException e) {
                throwIfCommentListIndexOutOfBoundsException(list, exp, e);
                // unreachable
                return null;
            }
        }
    }
    throwIfCommentNotFoundPropertyException(baseObject, property);
    // unreachable
    return null;
}
Also used : IfCommentListIndexOutOfBoundsException(org.dbflute.twowaysql.exception.IfCommentListIndexOutOfBoundsException) Method(java.lang.reflect.Method) DfBeanMethodNotFoundException(org.dbflute.helper.beans.exception.DfBeanMethodNotFoundException) ReflectionFailureException(org.dbflute.util.DfReflectionUtil.ReflectionFailureException) DfPropertyDesc(org.dbflute.helper.beans.DfPropertyDesc) DfBeanIllegalPropertyException(org.dbflute.helper.beans.exception.DfBeanIllegalPropertyException) MapParameterBean(org.dbflute.twowaysql.pmbean.MapParameterBean) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map) DfBeanDesc(org.dbflute.helper.beans.DfBeanDesc)

Example 3 with MapParameterBean

use of org.dbflute.twowaysql.pmbean.MapParameterBean in project dbflute-core by dbflute.

the class BoundValueTracer method trace.

// ===================================================================================
// Set up
// ======
public void trace(BoundValue boundValue) {
    Object value = boundValue.getFirstValue();
    if (value == null) {
        // if null, do nothing
        return;
    }
    // if value is not null, required
    Class<?> clazz = boundValue.getFirstType();
    // LikeSearchOption handling here is for OutsideSql.
    FilteringBindOption filteringBindOption = null;
    for (int pos = 1; pos < _nameList.size(); pos++) {
        if (value == null) {
            break;
        }
        final String currentName = _nameList.get(pos);
        final DfBeanDesc beanDesc = getBeanDesc(clazz);
        if (hasLikeSearchProperty(beanDesc, currentName, value)) {
            final FilteringBindOption currentOption = getFilteringBindOption(beanDesc, currentName, value);
            if (currentOption != null) {
                // if exists, override option
                filteringBindOption = currentOption;
            }
        }
        if (beanDesc.hasPropertyDesc(currentName)) {
            // main case
            final DfPropertyDesc pd = beanDesc.getPropertyDesc(currentName);
            value = getPropertyValue(clazz, value, currentName, pd);
            clazz = (value != null ? value.getClass() : pd.getPropertyType());
            continue;
        }
        if (MapParameterBean.class.isInstance(value)) {
            // used by union-query internally
            final Map<?, ?> map = ((MapParameterBean<?>) value).getParameterMap();
            // (different specification with Map)
            if (map.containsKey(currentName)) {
                value = map.get(currentName);
                clazz = (value != null ? value.getClass() : null);
                continue;
            }
        }
        if (Map.class.isInstance(value)) {
            final Map<?, ?> map = (Map<?, ?>) value;
            // if the key does not exist, treated same as a null value
            value = map.get(currentName);
            clazz = (value != null ? value.getClass() : null);
            continue;
        }
        if (List.class.isInstance(value)) {
            if (currentName.startsWith("get(") && currentName.endsWith(")")) {
                final List<?> list = (List<?>) value;
                final String exp = Srl.extractScopeFirst(currentName, "get(", ")").getContent();
                try {
                    final Integer index = DfTypeUtil.toInteger(exp);
                    value = list.get(index);
                } catch (NumberFormatException e) {
                    throwListIndexNotNumberException(exp, e);
                } catch (IndexOutOfBoundsException e) {
                    throwListIndexOutOfBoundsException(exp, e);
                }
                clazz = (value != null ? value.getClass() : null);
                continue;
            }
        }
        throwNotFoundPropertyException(clazz, currentName);
    }
    adjustLikeSearchDBWay(filteringBindOption);
    boundValue.setTargetValue(value);
    boundValue.setTargetType(clazz);
    boundValue.setFilteringBindOption(filteringBindOption);
}
Also used : ForCommentListIndexOutOfBoundsException(org.dbflute.twowaysql.exception.ForCommentListIndexOutOfBoundsException) BindVariableCommentListIndexOutOfBoundsException(org.dbflute.twowaysql.exception.BindVariableCommentListIndexOutOfBoundsException) EmbeddedVariableCommentListIndexOutOfBoundsException(org.dbflute.twowaysql.exception.EmbeddedVariableCommentListIndexOutOfBoundsException) DfPropertyDesc(org.dbflute.helper.beans.DfPropertyDesc) MapParameterBean(org.dbflute.twowaysql.pmbean.MapParameterBean) List(java.util.List) Map(java.util.Map) DfBeanDesc(org.dbflute.helper.beans.DfBeanDesc)

Aggregations

Map (java.util.Map)3 DfPropertyDesc (org.dbflute.helper.beans.DfPropertyDesc)3 MapParameterBean (org.dbflute.twowaysql.pmbean.MapParameterBean)3 List (java.util.List)2 DfBeanDesc (org.dbflute.helper.beans.DfBeanDesc)2 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 DfBeanIllegalPropertyException (org.dbflute.helper.beans.exception.DfBeanIllegalPropertyException)1 DfBeanMethodNotFoundException (org.dbflute.helper.beans.exception.DfBeanMethodNotFoundException)1 BindVariableCommentListIndexOutOfBoundsException (org.dbflute.twowaysql.exception.BindVariableCommentListIndexOutOfBoundsException)1 EmbeddedVariableCommentListIndexOutOfBoundsException (org.dbflute.twowaysql.exception.EmbeddedVariableCommentListIndexOutOfBoundsException)1 ForCommentListIndexOutOfBoundsException (org.dbflute.twowaysql.exception.ForCommentListIndexOutOfBoundsException)1 IfCommentListIndexOutOfBoundsException (org.dbflute.twowaysql.exception.IfCommentListIndexOutOfBoundsException)1 ReflectionFailureException (org.dbflute.util.DfReflectionUtil.ReflectionFailureException)1