Search in sources :

Example 1 with HpMobCaseWhenElement

use of org.dbflute.cbean.chelper.HpMobCaseWhenElement in project dbflute-core by dbflute.

the class OrderByElement method setupManualOrderClause.

protected void setupManualOrderClause(StringBuilder sb, String columnAlias, Map<String, String> selectClauseRealColumnAliasMap) {
    final String realAlias;
    if (_manualOrderOption.hasOrderByCalculation()) {
        final HpCalcSpecification<ConditionBean> calculationOrder = _manualOrderOption.getOrderByCalculation();
        realAlias = calculationOrder.buildStatementToSpecifidName(columnAlias, selectClauseRealColumnAliasMap);
    } else {
        if (selectClauseRealColumnAliasMap != null) {
            // means union
            realAlias = columnAlias;
        } else {
            realAlias = decryptIfNeeds(_columnInfo, columnAlias);
        }
    }
    final List<HpMobCaseWhenElement> caseWhenList = _manualOrderOption.getCaseWhenBoundList();
    if (!caseWhenList.isEmpty()) {
        sb.append(ln()).append("   case").append(ln());
        int index = 0;
        for (HpMobCaseWhenElement element : caseWhenList) {
            sb.append("     when ");
            doSetupManualOrderClause(sb, realAlias, element);
            final List<HpMobCaseWhenElement> connectedElementList = element.getConnectedElementList();
            for (HpMobCaseWhenElement connectedElement : connectedElementList) {
                doSetupManualOrderClause(sb, realAlias, connectedElement);
            }
            final Object thenValue = element.getThenValue();
            final String thenExp;
            if (thenValue != null) {
                thenExp = thenValue.toString();
            } else {
                thenExp = String.valueOf(index);
            }
            sb.append(" then ").append(thenExp).append(ln());
            ++index;
        }
        final Object elseValue = _manualOrderOption.getElseValue();
        final String elseExp;
        if (elseValue != null) {
            elseExp = elseValue.toString();
        } else {
            elseExp = String.valueOf(index);
        }
        sb.append("     else ").append(elseExp).append(ln());
        sb.append("   end");
    } else {
        sb.append(realAlias);
    }
    sb.append(" ").append(_ascDesc);
}
Also used : HpMobCaseWhenElement(org.dbflute.cbean.chelper.HpMobCaseWhenElement) ConditionBean(org.dbflute.cbean.ConditionBean)

Example 2 with HpMobCaseWhenElement

use of org.dbflute.cbean.chelper.HpMobCaseWhenElement in project dbflute-core by dbflute.

the class ManualOrderOption method xregisterThenValueToLastElement.

// ===================================================================================
// Then/Else
// =========
public void xregisterThenValueToLastElement(Object thenValue) {
    if (thenValue == null) {
        String msg = "The argument 'thenValue' should not be null.";
        throw new IllegalArgumentException(msg);
    }
    if (_caseWhenAcceptedList.isEmpty()) {
        throwManualOrderThenValueCaseWhenElementNotFoundException(thenValue);
    }
    final HpMobCaseWhenElement lastElement = getAcceptedLastElement();
    lastElement.setThenValue(thenValue);
}
Also used : HpMobCaseWhenElement(org.dbflute.cbean.chelper.HpMobCaseWhenElement)

Example 3 with HpMobCaseWhenElement

use of org.dbflute.cbean.chelper.HpMobCaseWhenElement in project dbflute-core by dbflute.

the class ManualOrderOption method bind.

// ===================================================================================
// Binding Process
// ===============
/**
 * Bind parameters for manual order. <br>
 * It is called from DBFlute runtime internally.
 * @param handler The handler for free parameters. (NotNull)
 */
public void bind(HpManualOrderThemeListHandler handler) {
    // called when set to query
    if (!hasManualOrder()) {
        return;
    }
    for (HpMobCaseWhenElement topElement : _caseWhenAcceptedList) {
        final HpMobCaseWhenElement boundTopElement = doBindCaseWhen(handler, topElement);
        final List<HpMobCaseWhenElement> connectedList = topElement.getConnectedElementList();
        for (HpMobCaseWhenElement connectedElement : connectedList) {
            final HpMobCaseWhenElement boundConnectedElement = doBindCaseWhen(handler, connectedElement);
            boundTopElement.addConnectedElement(boundConnectedElement);
        }
        _caseWhenBoundList.add(boundTopElement);
    }
    doBindElseEnd(handler);
}
Also used : HpMobCaseWhenElement(org.dbflute.cbean.chelper.HpMobCaseWhenElement)

Example 4 with HpMobCaseWhenElement

use of org.dbflute.cbean.chelper.HpMobCaseWhenElement in project dbflute-core by dbflute.

the class ManualOrderOption method doWhen.

// -----------------------------------------------------
// Assist Helper
// -------------
protected HpMobConnectedBean doWhen(ConditionKey conditionKey, Object orderValue) {
    if (orderValue == null && !isManualOrderConditionKeyNullHandling(conditionKey)) {
        String msg = "The argument 'orderValue' should not be null: conditionKey=" + conditionKey;
        throw new IllegalArgumentException(msg);
    }
    final HpMobCaseWhenElement addedElement = createElement(conditionKey, orderValue);
    if (_connectionMode != null) {
        if (_caseWhenAcceptedList.isEmpty()) {
            throwManualOrderPreviousConditionNotFoundException(_connectionMode, conditionKey, orderValue);
        }
        addedElement.setConnectionMode(_connectionMode);
        final HpMobCaseWhenElement lastElement = getAcceptedLastElement();
        final List<HpMobCaseWhenElement> connectedElementList = lastElement.getConnectedElementList();
        if (!connectedElementList.isEmpty()) {
            // check same connectors
            final HpMobCaseWhenElement previousConnected = connectedElementList.get(connectedElementList.size() - 1);
            final HpMobConnectionMode previousMode = previousConnected.getConnectionMode();
            if (previousMode != null && !previousMode.equals(addedElement.getConnectionMode())) {
                throwManualOrderTwoConnectorUnsupportedException(conditionKey, orderValue, lastElement);
            }
        }
        lastElement.addConnectedElement(addedElement);
    } else {
        _caseWhenAcceptedList.add(addedElement);
    }
    return createConnectedBean();
}
Also used : HpMobConnectionMode(org.dbflute.cbean.chelper.HpMobConnectionMode) HpMobCaseWhenElement(org.dbflute.cbean.chelper.HpMobCaseWhenElement)

Example 5 with HpMobCaseWhenElement

use of org.dbflute.cbean.chelper.HpMobCaseWhenElement in project dbflute-core by dbflute.

the class ManualOrderOption method doValidateCaseWhenConstraint.

protected void doValidateCaseWhenConstraint() {
    if (_caseWhenAcceptedList.isEmpty()) {
        return;
    }
    final HpMobCaseWhenElement first = _caseWhenAcceptedList.get(0);
    final boolean firstThenExists = first.getThenValue() != null;
    for (HpMobCaseWhenElement current : _caseWhenAcceptedList) {
        final boolean currentThenExists = current.getThenValue() != null;
        if (firstThenExists && !currentThenExists) {
            throwManualOrderRequiredThenNotFoundException(current);
        } else if (!firstThenExists && currentThenExists) {
            throwManualOrderUnnecessaryThenFoundException(current);
        }
    }
    final boolean elseExists = _elseAcceptedValue != null;
    if (firstThenExists && !elseExists) {
        // e.g. SwitchOrder
        throwManualOrderRequiredElseNotFoundException();
    } else if (!firstThenExists && elseExists) {
        // e.g. PriorityOrder
        String msg = "Found unnecessary 'else', it doesn't need it if PriorityOrder: " + toString();
        throw new IllegalConditionBeanOperationException(msg);
    }
}
Also used : IllegalConditionBeanOperationException(org.dbflute.exception.IllegalConditionBeanOperationException) HpMobCaseWhenElement(org.dbflute.cbean.chelper.HpMobCaseWhenElement)

Aggregations

HpMobCaseWhenElement (org.dbflute.cbean.chelper.HpMobCaseWhenElement)9 IllegalConditionBeanOperationException (org.dbflute.exception.IllegalConditionBeanOperationException)3 ExceptionMessageBuilder (org.dbflute.helper.message.ExceptionMessageBuilder)2 Date (java.util.Date)1 ConditionBean (org.dbflute.cbean.ConditionBean)1 HpManualOrderThemeListHandler (org.dbflute.cbean.chelper.HpManualOrderThemeListHandler)1 HpMobConnectionMode (org.dbflute.cbean.chelper.HpMobConnectionMode)1 ConditionKey (org.dbflute.cbean.ckey.ConditionKey)1