Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2014-2015 the original author or authors.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
  * either express or implied. See the License for the specific language
  * governing permissions and limitations under the License.
  */
 package org.dbflute.twowaysql.node;
 
 import java.util.List;
 import java.util.Map;
 

Author(s):
jflute
 
 public class BoundValueTracer {
 
     // ===================================================================================
     //                                                                          Definition
     //                                                                          ==========
     protected static final String LIKE_SEARCH_OPTION_SUFFIX = "InternalLikeSearchOption";
 
     // ===================================================================================
     //                                                                           Attribute
     //                                                                           =========
     protected final List<String_nameList;
     protected final String _expression// for logging only
     protected final String _specifiedSql// for logging only
     protected final ParameterCommentType _commentType// for logging only
 
     // ===================================================================================
     //                                                                         Constructor
     //                                                                         ===========
     
Constructor.

Parameters:
nameList The list of property names. (NotNull)
expression The expression of the comment for logging only. (NotNull)
specifiedSql The specified SQL for logging only. (NotNull)
commentType The type of comment for logging only. (NotNull)
 
     public BoundValueTracer(List<StringnameListString expressionString specifiedSqlParameterCommentType commentType) {
          = nameList;
          = expression;
          = specifiedSql;
          = commentType;
     }
 
     // ===================================================================================
     //                                                                              Set up
     //                                                                              ======
     public void trace(BoundValue boundValue) {
         Object value = boundValue.getFirstValue();
         if (value == null) { // if null, do nothing
             return;
         }
         Class<?> clazz = boundValue.getFirstType(); // if value is not null, required 
 
         // LikeSearchOption handling here is for OutsideSql.
         FilteringBindOption filteringBindOption = null;
 
         for (int pos = 1; pos < .size(); pos++) {
             if (value == null) {
                 break;
             }
             final String currentName = .get(pos);
             final DfBeanDesc beanDesc = getBeanDesc(clazz);
             if (hasLikeSearchProperty(beanDesccurrentNamevalue)) {
                 final FilteringBindOption currentOption = getFilteringBindOption(beanDesccurrentNamevalue);
                if (currentOption != null) { // if exists, override option
                    filteringBindOption = currentOption;
                }
            }
            if (beanDesc.hasPropertyDesc(currentName)) { // main case
                final DfPropertyDesc pd = beanDesc.getPropertyDesc(currentName);
                value = getPropertyValue(clazzvaluecurrentNamepd);
                clazz = (value != null ? value.getClass() : pd.getPropertyType());
                continue;
            }
            if (MapParameterBean.class.isInstance(value)) { // used by union-query internally
                final Map<?, ?> map = ((MapParameterBean<?>) value).getParameterMap();
                // if the key does not exist, it does not process
                // (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(expe);
                    } catch (IndexOutOfBoundsException e) {
                        throwListIndexOutOfBoundsException(expe);
                    }
                    clazz = (value != null ? value.getClass() : null);
                    continue;
                }
            }
            throwNotFoundPropertyException(clazzcurrentName);
        }
        adjustLikeSearchDBWay(filteringBindOption);
        boundValue.setTargetValue(value);
        boundValue.setTargetType(clazz);
        boundValue.setFilteringBindOption(filteringBindOption);
    }
    protected DfBeanDesc getBeanDesc(Class<?> clazz) {
        return DfBeanDescFactory.getBeanDesc(clazz);
    }
    // -----------------------------------------------------
    //                             LikeSearch for OutsideSql
    //                             -------------------------
    protected boolean hasLikeSearchProperty(DfBeanDesc beanDescString currentNameObject pmb) {
        final String propertyName = buildLikeSearchPropertyName(currentName);
        boolean result = false;
        if (beanDesc.hasPropertyDesc(propertyName)) { // main case
            result = true;
        } else if (MapParameterBean.class.isInstance(pmb)) {
            final Map<?, ?> map = ((MapParameterBean<?>) pmb).getParameterMap();
            result = map.containsKey(propertyName);
        } else if (Map.class.isInstance(pmb)) {
            result = ((Map<?, ?>) pmb).containsKey(propertyName);
        }
        return result;
    }
    protected FilteringBindOption getFilteringBindOption(DfBeanDesc beanDescString currentNameObject pmb) {
        final String propertyName = buildLikeSearchPropertyName(currentName);
        final FilteringBindOption option;
        if (beanDesc.hasPropertyDesc(propertyName)) { // main case
            final DfPropertyDesc pb = beanDesc.getPropertyDesc(propertyName);
            option = (FilteringBindOptionpb.getValue(pmb);
        } else if (MapParameterBean.class.isInstance(pmb)) {
            final Map<?, ?> map = ((MapParameterBean<?>) pmb).getParameterMap();
            option = (FilteringBindOptionmap.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);
        }
        // no check here for various situation
        // (parameter-bean checks option's existence and wrong operation)
        return option;
    }
    protected String buildLikeSearchPropertyName(String resourceName) {
        return resourceName + ;
    }
    protected void adjustLikeSearchDBWay(FilteringBindOption option) {
        // for extension
    }
    // -----------------------------------------------------
    //                                         Assist Helper
    //                                         -------------
    protected boolean isLastLoop(int pos) {
        return .size() == (pos + 1);
    }
    protected Object getPropertyValue(Class<?> beanTypeObject beanValueString currentNameDfPropertyDesc pd) {
        try {
            return pd.getValue(beanValue);
        } catch (DfBeanIllegalPropertyException e) {
            throwPropertyReadFailureException(beanTypecurrentNamee);
            return null// unreachable
        }
    }
    protected Object invokeGetter(Method methodObject target) {
        return DfReflectionUtil.invoke(methodtargetnull);
    }
    protected void throwPropertyReadFailureException(Class<?> targetTypeString propertyNameDfBeanIllegalPropertyException e) {
        br.addNotice("Failed to read the property on the " + .textName() + ".");
        br.addItem("Advice");
        br.addElement("Please confirm your comment properties.");
        br.addElement("(readable? accessbile? and so on...)");
        br.addItem(.titleName());
        br.addElement();
        br.addItem("Illegal Property");
        br.addElement(DfTypeUtil.toClassTitle(targetType) + "." + propertyName);
        br.addItem("Exception Message");
        br.addElement(e.getClass());
        br.addElement(e.getMessage());
        final Throwable cause = e.getCause();
        if (cause != null) { // basically DfBeanIllegalPropertyException has its cause
            br.addElement(cause.getClass());
            br.addElement(cause.getMessage());
            final Throwable nextCause = cause.getCause();
            if (nextCause != null) {
                br.addElement(nextCause.getClass());
                br.addElement(nextCause.getMessage());
            }
        }
        br.addItem("Specified SQL");
        br.addElement();
        final String msg = br.buildExceptionMessage();
            throw new BindVariableCommentPropertyReadFailureException(msge);
        } else if (..equals()) {
            throw new EmbeddedVariableCommentPropertyReadFailureException(msge);
        } else if (..equals()) {
            throw new ForCommentPropertyReadFailureException(msge);
        } else { // no way
            throw new BindVariableCommentPropertyReadFailureException(msge);
        }
    }
    protected void throwNotFoundPropertyException(Class<?> targetTypeString notFoundProperty) {
        br.addNotice("The property on the " + .textName() + " was not found.");
        br.addItem("Advice");
        br.addElement("Please confirm the existence of your property on your arguments.");
        br.addElement("And has the property had misspelling?");
        br.addItem(.titleName());
        br.addElement();
        br.addItem("NotFound Property");
        br.addElement((targetType != null ? targetType.getName() + "#" : "") + notFoundProperty);
        br.addItem("Specified SQL");
        br.addElement();
        final String msg = br.buildExceptionMessage();
            throw new BindVariableCommentNotFoundPropertyException(msg);
        } else if (..equals()) {
            throw new EmbeddedVariableCommentNotFoundPropertyException(msg);
        } else if (..equals()) {
            throw new ForCommentNotFoundPropertyException(msg);
        } else { // no way
            throw new BindVariableCommentNotFoundPropertyException(msg);
        }
    }
    protected void throwListIndexNotNumberException(String notNumberIndexNumberFormatException e) {
        br.addNotice("The list index on the " + .textName() + " was not number.");
        br.addItem("Advice");
        br.addElement("Please confirm the index on your comment.");
        br.addItem(.titleName());
        br.addElement();
        br.addItem("NotNumber Index");
        br.addElement(notNumberIndex);
        br.addItem("NumberFormatException");
        br.addElement(e.getMessage());
        br.addItem("Specified SQL");
        br.addElement();
        final String msg = br.buildExceptionMessage();
            throw new BindVariableCommentListIndexNotNumberException(msge);
        } else if (..equals()) {
            throw new EmbeddedVariableCommentListIndexNotNumberException(msge);
        } else if (..equals()) {
            throw new ForCommentListIndexNotNumberException(msge);
        } else { // no way
            throw new BindVariableCommentListIndexNotNumberException(msge);
        }
    }
    protected void throwListIndexOutOfBoundsException(String numberIndexIndexOutOfBoundsException e) {
        br.addNotice("The list index on the " + .textName() + " was out of bounds.");
        br.addItem("Advice");
        br.addElement("Please confirm the index on your comment.");
        br.addItem(.titleName());
        br.addElement();
        br.addItem("OutOfBounds Index");
        br.addElement(numberIndex);
        br.addItem("IndexOutOfBoundsException");
        br.addElement(e.getMessage());
        br.addItem("Specified SQL");
        br.addElement();
        final String msg = br.buildExceptionMessage();
            throw new BindVariableCommentListIndexOutOfBoundsException(msge);
        } else if (..equals()) {
            throw new EmbeddedVariableCommentListIndexOutOfBoundsException(msge);
        } else if (..equals()) {
            throw new ForCommentListIndexOutOfBoundsException(msge);
        } else { // no way
            throw new BindVariableCommentListIndexOutOfBoundsException(msge);
        }
    }
    // ===================================================================================
    //                                                                    Exception Helper
    //                                                                    ================
        return new ExceptionMessageBuilder();
    }
    // ===================================================================================
    //                                                                      General Helper
    //                                                                      ==============
    protected String initCap(String name) {
        return Srl.initCap(name);
    }
New to GrepCode? Check out our FAQ X