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.cbean.ckey;
 
 import java.util.List;
 
The abstract class of condition-key.

Author(s):
jflute
 
 public abstract class ConditionKey implements Serializable {
 
     // ===================================================================================
     //                                                                          Definition
     //                                                                          ==========
     
The serial version UID for object serialization. (Default)
 
     private static final long serialVersionUID = 1L;
 
     // ===================================================================================
     //                                                                          Definition
     //                                                                          ==========
     // -----------------------------------------------------
     //                                         Condition Key
     //                                         -------------
     
The condition key of equal.
 
     public static final ConditionKey CK_EQUAL = new ConditionKeyEqual();

    
The condition key of notEqual as standard.
 
     public static final ConditionKey CK_NOT_EQUAL_STANDARD = new ConditionKeyNotEqualStandard();

    
The condition key of notEqual as tradition.
 
     public static final ConditionKey CK_NOT_EQUAL_TRADITION = new ConditionKeyNotEqualTradition();

    
The condition key of greaterThan.
 
     public static final ConditionKey CK_GREATER_THAN = new ConditionKeyGreaterThan();

    
The condition key of greaterThan with orIsNull.
 
     public static final ConditionKey CK_GREATER_THAN_OR_IS_NULL = new ConditionKeyGreaterThanOrIsNull();

    
The condition key of lessThan.
 
     public static final ConditionKey CK_LESS_THAN = new ConditionKeyLessThan();

    
The condition key of lessThan with orIsNull.
 
     public static final ConditionKey CK_LESS_THAN_OR_IS_NULL = new ConditionKeyLessThanOrIsNull();

    
The condition key of greaterEqual.
 
     public static final ConditionKey CK_GREATER_EQUAL = new ConditionKeyGreaterEqual();

    
The condition key of greaterEqual with orIsNull.
 
     public static final ConditionKey CK_GREATER_EQUAL_OR_IS_NULL = new ConditionKeyGreaterEqualOrIsNull();

    
The condition key of lessEqual.
 
     public static final ConditionKey CK_LESS_EQUAL = new ConditionKeyLessEqual();

    
The condition key of lessEqual with orIsNull.
 
     public static final ConditionKey CK_LESS_EQUAL_OR_IS_NULL = new ConditionKeyLessEqualOrIsNull();

    
The condition key of inScope.
 
     public static final ConditionKey CK_IN_SCOPE = new ConditionKeyInScope();

    
The condition key of notInScope.
 
     public static final ConditionKey CK_NOT_IN_SCOPE = new ConditionKeyNotInScope();

    
The condition key of likeSearch.
 
    public static final ConditionKey CK_LIKE_SEARCH = new ConditionKeyLikeSearch();

    
The condition key of notLikeSearch.
    public static final ConditionKey CK_NOT_LIKE_SEARCH = new ConditionKeyNotLikeSearch();

    
The condition key of isNull.
    public static final ConditionKey CK_IS_NULL = new ConditionKeyIsNull();

    
The condition key of isNullOrEmpty.
    public static final ConditionKey CK_IS_NULL_OR_EMPTY = new ConditionKeyIsNullOrEmpty();

    
The condition key of isNotNull.
    public static final ConditionKey CK_IS_NOT_NULL = new ConditionKeyIsNotNull();

    
Dummy-object for IsNull and IsNotNull and so on...
    protected static final Object DUMMY_OBJECT = new Object();
    // -----------------------------------------------------
    //                                        Prepare Result
    //                                        --------------
    // ===================================================================================
    //                                                                           Attribute
    //                                                                           =========
    // not final because of no time of refactoring...
    
The key name of the condition. (NotNull: initialized in constructor of sub-class)
    protected String _conditionKey;

    
The string of operand, used in SQL. (NotNull: initialized in constructor of sub-class)
    protected String _operand;
    // ===================================================================================
    //                                                                       Prepare Query
    //                                                                       =============
    
Prepare the query of the condition value to register the condition.

Parameters:
provider The provider of query mode. (NotNull)
cvalue The object of condition value. (NotNull)
value The value of the condition. (NotNull)
Returns:
The result of the preparation for the condition key. (NotNull)
    public ConditionKeyPrepareResult prepareQuery(final QueryModeProvider providerfinal ConditionValue cvaluefinal Object value) {
        return cvalue.process(new CallbackProcessor<ConditionKeyPrepareResult>() {
            public ConditionKeyPrepareResult process() {
                return doPrepareQuery(cvaluevalue);
            }
            public QueryModeProvider getProvider() {
                return provider;
            }
        });
    }
    protected abstract ConditionKeyPrepareResult doPrepareQuery(ConditionValue cvalueObject value);
    // -----------------------------------------------------
    //                                         Choose Result
    //                                         -------------
    protected ConditionKeyPrepareResult chooseResultAlreadyExists(boolean equalValue) {
        return equalValue ?  : ;
    }
        return needsOverrideValue(cvalue) ?  : ;
    }
        return isInvalidNonFixedQuery(value) ?  : ;
    }
    protected boolean isInvalidNonFixedQuery(Object value) {
        return value == null;
    }
        return isInvalidListQuery(value) ?  : ;
    }
    protected boolean isInvalidListQuery(Object value) {
        return value == null || !(value instanceof List<?>) || ((List<?>) value).isEmpty();
    }
    // ===================================================================================
    //                                                                      Override Check
    //                                                                      ==============
    
Does it need to override the existing value to register the value?
This should be called in CallbackProcessor for e.g. in-line query

Parameters:
cvalue The object of condition value. (NotNull)
Returns:
The determination, true or false.
    protected abstract boolean needsOverrideValue(ConditionValue cvalue);
    // ===================================================================================
    //                                                                        Where Clause
    //                                                                        ============
    
Add where clause.

Parameters:
provider The provider of query mode. (NotNull)
conditionList The list of condition. (NotNull)
columnRealName The real name of column. (NotNull)
cvalue The object of condition value. (NotNull)
cipher The cipher of column by function. (NullAllowed)
option The option of condition. (NullAllowed)
    public void addWhereClause(final QueryModeProvider providerfinal List<QueryClauseconditionList,
            final ColumnRealName columnRealNamefinal ConditionValue cvalue// basic resources
            final ColumnFunctionCipher cipherfinal ConditionOption option) { // optional resources
        cvalue.process(new CallbackProcessor<Void>() {
            public Void process() {
                doAddWhereClause(conditionListcolumnRealNamecvaluecipheroption);
                return null;
            }
            public QueryModeProvider getProvider() {
                return provider;
            }
        });
    }

    
Do adding where clause.

Parameters:
conditionList The list of condition. (NotNull)
columnRealName The real name of column. (NotNull)
cvalue The object of condition value. (NotNull)
cipher The cipher of column by function. (NullAllowed)
option The option of condition. (NullAllowed)
    protected abstract void doAddWhereClause(List<QueryClauseconditionListColumnRealName columnRealNameConditionValue cvalue,
            ColumnFunctionCipher cipherConditionOption option);
    // ===================================================================================
    //                                                                     Condition Value
    //                                                                     ===============
    
Setup condition value.

Parameters:
provider The provider of query mode. (NotNull)
cvalue The object of condition value. (NotNull)
value The native value of condition. (NullAllowed)
location The location on parameter comment. (NotNull)
option Condition option. (NullAllowed)
    public void setupConditionValue(final QueryModeProvider providerfinal ConditionValue cvaluefinal Object value,
            final String locationfinal ConditionOption option) {
        cvalue.process(new CallbackProcessor<Void>() {
            public Void process() {
                doSetupConditionValue(cvaluevaluelocationoption);
                return null;
            }
            public QueryModeProvider getProvider() {
                return provider;
            }
        });
    }

    
Do setting up condition value.

Parameters:
cvalue The object of condition value. (NotNull)
value The native value of condition. (NullAllowed)
location The location on parameter comment. (NotNull)
option The option of condition. (NullAllowed)
    protected abstract void doSetupConditionValue(ConditionValue cvalueObject valueString locationConditionOption option);
    // ===================================================================================
    //                                                                         Bind Clause
    //                                                                         ===========
    // -----------------------------------------------------
    //                                           Entry Point
    //                                           -----------
    
Build bind clause.

Parameters:
columnRealName The real name of column. (NotNull)
location The location on parameter comment. (NotNull)
cipher The cipher of column by function. (NullAllowed)
option The option of condition. (NullAllowed)
Returns:
The query clause as bind clause. (NotNull)
    protected QueryClause buildBindClause(ColumnRealName columnRealNameString locationColumnFunctionCipher cipher,
            ConditionOption option) {
        return new StringQueryClause(doBuildBindClause(columnRealNamelocationcipheroption));
    }

    
Build bind clause with orIsNull condition.

Parameters:
columnRealName The real name of column. (NotNull)
location The location on parameter comment. (NotNull)
cipher The cipher of column by function. (NullAllowed)
option The option of condition. (NullAllowed)
Returns:
The query clause as bind clause. (NotNull)
    protected QueryClause buildBindClauseOrIsNull(ColumnRealName columnRealNameString locationColumnFunctionCipher cipher,
            ConditionOption option) {
        final String mainQuery = doBuildBindClause(columnRealNamelocationcipheroption);
        final String clause = "(" + mainQuery + " or " + columnRealName + " is null)";
        return new StringQueryClause(clause);
    }
    protected String doBuildBindClause(ColumnRealName columnRealNameString locationColumnFunctionCipher cipherConditionOption option) {
        final BindClauseResult result = resolveBindClause(columnRealNamelocationcipheroption);
        return result.toBindClause();
    }

    
Build clause without value. (basically for isNull, isNotNull)

Parameters:
columnRealName The real name of column. (NotNull)
Returns:
The query clause as bind clause. (NotNull)
    protected QueryClause buildClauseWithoutValue(ColumnRealName columnRealName) {
        final String clause = columnRealName + " " + getOperand(); // no need to resolve cipher
        return new StringQueryClause(clause);
    }
    // -----------------------------------------------------
    //                                       Clause Resolver
    //                                       ---------------
    protected BindClauseResult resolveBindClause(ColumnRealName columnRealNameString locationColumnFunctionCipher cipher,
            ConditionOption option) {
        final String basicBindExp = buildBindVariableExp(locationoption);
        final String bindExp;
        final ColumnRealName resolvedColumn;
        {
            final ColumnRealName columnExp;
            if (cipher != null) {
                final String plainColumnExp = columnRealName.toString();
                final String decryptExp = cipher.decrypt(plainColumnExp);
                final boolean nonInvertible = plainColumnExp.equals(decryptExp);
                if (isBindEncryptAllowed(columnRealNameoptionnonInvertible)) {
                    bindExp = cipher.encrypt(basicBindExp);
                    columnExp = columnRealName;
                } else { // needs to decrypt (invertible)
                    bindExp = basicBindExp;
                    columnExp = toColumnRealName(decryptExp);
                }
            } else { // mainly here
                bindExp = basicBindExp;
                columnExp = columnRealName;
            }
            resolvedColumn = resolveOptionalColumn(columnExpoption);
        }
        return createBindClauseResult(resolvedColumnbindExpoption);
    }
    protected boolean isBindEncryptAllowed(ColumnRealName columnRealNameConditionOption optionboolean nonInvertible) {
        if (isOutOfBindEncryptConditionKey()) { // e.g. LikeSearch
            return false// regardless of invertible
        }
        if (nonInvertible) { // means it cannot decrypt
            return true;
        }
        // invertible here
        // basically decrypt but encrypt if it can be, for better performance
        final boolean possible = isPossibleBindEncryptConditionKey(); // e.g. Equal, NotEqual
        return possible && !hasColumnCollaboration(columnRealNameoption); // means simple condition
    }
    protected String buildBindVariableExp(String locationConditionOption option) {
        return "/*pmb." + location + "*/" + getBindVariableDummyValue();
    }
    protected String getBindVariableDummyValue() { // to override
        return null// as default
    }
    protected boolean isOutOfBindEncryptConditionKey() { // to override
        return false// as default
    }
    protected boolean isPossibleBindEncryptConditionKey() { // to override
        return false// as default
    }
    protected ColumnRealName toColumnRealName(String columnSqlName) {
        return ColumnRealName.create(nullnew ColumnSqlName(columnSqlName));
    }
    protected BindClauseResult createBindClauseResult(ColumnRealName columnExpString bindExpConditionOption option) {
        final String operand = resolveOperand(option);
        final String rearOption = resolveRearOption(option);
        final BindClauseResult result = new BindClauseResult(columnExpoperandbindExprearOption);
        result.setArranger(resolveWhereClauseArranger(option));
        return result;
    }
    protected String resolveOperand(ConditionOption option) {
        final String operand = extractExtOperand(option);
        return operand != null ? operand : getOperand();
    }
    protected String extractExtOperand(ConditionOption option) {
        final ExtensionOperand extOperand = option != null ? option.getExtensionOperand() : null;
        return extOperand != null ? extOperand.operand() : null;
    }
    protected String resolveRearOption(ConditionOption option) {
        return option != null ? option.getRearOption() : "";
    }
        return option != null ? option.getWhereClauseArranger() : null;
    }
    // -----------------------------------------------------
    //                                       Optional Column
    //                                       ---------------
    protected ColumnRealName resolveOptionalColumn(ColumnRealName columnExpConditionOption option) {
        return resolveCalculationColumn(resolveCompoundColumn(columnExpoption), option);
    }
    protected boolean hasColumnCollaboration(ColumnRealName columnRealNameConditionOption option) {
        return hasCalculationColumn(columnRealNameoption) || hasCompoundColumn(columnRealNameoption);
    }
    protected boolean hasCalculationColumn(ColumnRealName columnRealNameConditionOption option) {
        return option != null && option instanceof RangeOfOption && ((RangeOfOptionoption).hasCalculationRange();
    }
    protected ColumnRealName resolveCalculationColumn(ColumnRealName columnRealNameConditionOption option) {
        if (option == null) {
            return columnRealName;
        }
        if (option instanceof RangeOfOption) {
            final RangeOfOption rangeOfOption = (RangeOfOptionoption;
            if (rangeOfOption.hasCalculationRange()) {
                final HpCalcSpecification<ConditionBeancalculationRange = rangeOfOption.getCalculationRange();
                final String calculated = calculationRange.buildStatementToSpecifidName(columnRealName.toString());
                return toColumnRealName(calculated);
            }
        }
        return columnRealName;
    }
    protected boolean hasCompoundColumn(ColumnRealName columnRealNameConditionOption option) {
        return option != null && !option.hasCompoundColumn();
    }
    protected ColumnRealName resolveCompoundColumn(ColumnRealName baseRealNameConditionOption option) {
        if (option == null || !option.hasCompoundColumn()) {
            return baseRealName;
        }
        if (!option.hasStringConnector()) { // basically no way
            String msg = "The option should have string connector when compound column is specified: " + option;
            throw new IllegalConditionBeanOperationException(msg);
        }
        final List<SpecifiedColumncompoundColumnList = option.getCompoundColumnList();
        final List<ColumnRealNamerealNameList = new ArrayList<ColumnRealName>();
        realNameList.add(baseRealName); // already cipher
        for (SpecifiedColumn specifiedColumn : compoundColumnList) {
            realNameList.add(doResolveCompoundColumn(optionspecifiedColumn));
        }
        final OnQueryStringConnector stringConnector = option.getStringConnector();
        final String connected = stringConnector.connect(realNameList.toArray());
        return ColumnRealName.create(nullnew ColumnSqlName(connected));
    }
    protected ColumnRealName doResolveCompoundColumn(ConditionOption optionSpecifiedColumn specifiedColumn) {
        final GearedCipherManager cipherManager = option.getGearedCipherManager();
        final ColumnRealName specifiedName = specifiedColumn.toColumnRealName();
        if (cipherManager != null && !specifiedColumn.isDerived()) {
            final ColumnInfo columnInfo = specifiedColumn.getColumnInfo();
            final ColumnFunctionCipher cipher = cipherManager.findColumnFunctionCipher(columnInfo);
            if (cipher != null) {
                return toColumnRealName(cipher.decrypt(specifiedName.toString()));
            } else {
                return specifiedName;
            }
        } else {
            return specifiedName;
        }
    }
    // -----------------------------------------------------
    //                                         Result Object
    //                                         -------------
    public static class BindClauseResult {
        protected final ColumnRealName _columnExp;
        protected final String _operand;
        protected final String _bindExp;
        protected final String _rearOption;
        protected QueryClauseArranger _arranger;
        public BindClauseResult(ColumnRealName columnExpString operandString bindExpString rearOption) {
             = columnExp;
             = operand;
             = bindExp;
             = rearOption;
        }
        public String toBindClause() {
            final String clause;
            if ( != null) {
                clause = .arrange();
            } else {
                clause =  + " " +  + " " +  + ;
            }
            return clause;
        }
        public ColumnRealName getColumnExp() {
            return ;
        }
        public String getOperand() {
            return ;
        }
        public String getBindExp() {
            return ;
        }
        public String getRearOption() {
            return ;
        }
        public QueryClauseArranger getArranger() {
            return ;
        }
        public void setArranger(QueryClauseArranger arranger) {
            this. = arranger;
        }
    }
    // ===================================================================================
    //                                                                       Null-able Key
    //                                                                       =============
    
Is the condition key null-able? (basically for join determination)

Returns:
The determination, true or false.
    public abstract boolean isNullaleKey();
    // ===================================================================================
    //                                                                      Basic Override
    //                                                                      ==============
    

Returns:
View-string of condition key information.
    @Override
    public String toString() {
        return "ConditionKey:{" + getConditionKey() + " " + getOperand() + "}";
    }
    // ===================================================================================
    //                                                                            Accessor
    //                                                                            ========
    
Get condition-key.

Returns:
Condition-key.
    public String getConditionKey() {
        return ;
    }

    
Get operand.

Returns:
Operand.
    public String getOperand() {
        return ;
    }
New to GrepCode? Check out our FAQ X