Search in sources :

Example 16 with HandyDate

use of org.dbflute.helper.HandyDate in project dbflute-core by dbflute.

the class DfRelativeDateResolver method invokeMethod.

protected HandyDate invokeMethod(String tableName, String columnName, String relativeDate, HandyDate handyDate, String methodCall) {
    if (!methodCall.contains("(") || !methodCall.endsWith(")")) {
        throwLoadDataRelativeDateMethodArgPartNotFoundException(tableName, columnName, relativeDate);
    }
    final String methodName = Srl.substringFirstFront(methodCall, "(");
    final String methodArgsPart = Srl.substringFirstFront(Srl.substringFirstRear(methodCall, "("), ")");
    final List<String> argElementList;
    if (Srl.is_NotNull_and_NotTrimmedEmpty(methodArgsPart)) {
        argElementList = Srl.splitListTrimmed(methodArgsPart, ",");
    } else {
        argElementList = DfCollectionUtil.emptyList();
    }
    final List<Object> argValueList = DfCollectionUtil.newArrayList();
    for (String arg : argElementList) {
        if (isNumber(arg)) {
            // int only supported (cannot use long)
            argValueList.add(DfTypeUtil.toInteger(arg));
        } else {
            argValueList.add(arg);
        }
    }
    final List<Class<?>> argTypeList = DfCollectionUtil.newArrayList();
    for (Object argValue : argValueList) {
        final Class<? extends Object> argType = argValue.getClass();
        if (Integer.class.equals(argType)) {
            // even if the argument value is int type, getClass() returns Integer type
            argTypeList.add(int.class);
        } else {
            argTypeList.add(argType);
        }
    }
    final Class<?>[] argTypes = argTypeList.toArray(new Class<?>[argTypeList.size()]);
    final Class<HandyDate> handyDateType = HandyDate.class;
    final Method method = DfReflectionUtil.getPublicMethod(handyDateType, methodName, argTypes);
    if (method == null) {
        throwLoadDataRelativeDateMethodNotFoundException(tableName, columnName, relativeDate, handyDateType, methodName, argTypes);
    }
    try {
        handyDate = (HandyDate) DfReflectionUtil.invoke(method, handyDate, argValueList.toArray());
    } catch (ReflectionFailureException e) {
        throwLoadDataRelativeDateInvokeFailureException(tableName, columnName, relativeDate, handyDateType, methodName, e);
    }
    return handyDate;
}
Also used : HandyDate(org.dbflute.helper.HandyDate) Method(java.lang.reflect.Method) ReflectionFailureException(org.dbflute.util.DfReflectionUtil.ReflectionFailureException)

Example 17 with HandyDate

use of org.dbflute.helper.HandyDate in project dbflute-core by dbflute.

the class DfRelativeDateResolver method doResolveRelativeDate.

protected String doResolveRelativeDate(String tableName, String columnName, String relativeDate, Date date) {
    final String calcPart = relativeDate.trim();
    if (calcPart.trim().length() == 0 || date.after(LIMIT_DATE)) {
        return DfTypeUtil.toString(date, RESOLVED_PATTERN);
    }
    final List<String> methodList = Srl.splitListTrimmed(Srl.trim(calcPart, "."), ".");
    HandyDate handyDate = new HandyDate(date);
    for (String methodCall : methodList) {
        handyDate = invokeMethod(tableName, columnName, relativeDate, handyDate, methodCall);
    }
    return DfTypeUtil.toString(handyDate.getDate(), RESOLVED_PATTERN);
}
Also used : HandyDate(org.dbflute.helper.HandyDate)

Aggregations

HandyDate (org.dbflute.helper.HandyDate)17 Date (java.util.Date)5 Method (java.lang.reflect.Method)3 LocalDateTime (java.time.LocalDateTime)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 ParseDateExpressionFailureException (org.dbflute.exception.ParseDateExpressionFailureException)3 ReflectionFailureException (org.dbflute.util.DfReflectionUtil.ReflectionFailureException)3 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 StringKeyMap (org.dbflute.helper.StringKeyMap)2 DfTableMeta (org.dbflute.logic.jdbc.metadata.info.DfTableMeta)1 ScopeInfo (org.dbflute.util.Srl.ScopeInfo)1