protected void buildExpression(
     StringBuffer sb,
     String entityKey,
     String field,
     Object[] value,
     SupportedFieldType type,
     SupportedFieldType secondaryType,
     String operator,
     boolean includeParenthesis,
     boolean isFieldComparison,
     boolean ignoreCase,
     boolean isNegation,
     boolean ignoreQuotes)
     throws IncompatibleMVELTranslationException {
   if (operator.equals("==") && !isFieldComparison && value.length > 1) {
     sb.append("(");
     sb.append("[");
     sb.append(
         formatValue(
             field,
             entityKey,
             type,
             secondaryType,
             value,
             isFieldComparison,
             ignoreCase,
             ignoreQuotes));
     sb.append("] contains ");
     sb.append(formatField(entityKey, type, field, ignoreCase, isNegation));
     if ((type.equals(SupportedFieldType.ID)
             && secondaryType != null
             && secondaryType.equals(SupportedFieldType.INTEGER))
         || type.equals(SupportedFieldType.INTEGER)) {
       sb.append(".intValue()");
     }
     sb.append(")");
   } else {
     sb.append(formatField(entityKey, type, field, ignoreCase, isNegation));
     sb.append(operator);
     if (includeParenthesis) {
       sb.append("(");
     }
     sb.append(
         formatValue(
             field,
             entityKey,
             type,
             secondaryType,
             value,
             isFieldComparison,
             ignoreCase,
             ignoreQuotes));
     if (includeParenthesis) {
       sb.append(")");
     }
   }
 }
 protected String formatValue(
     String fieldName,
     String entityKey,
     SupportedFieldType type,
     SupportedFieldType secondaryType,
     Object[] value,
     boolean isFieldComparison,
     boolean ignoreCase,
     boolean ignoreQuotes)
     throws IncompatibleMVELTranslationException {
   StringBuffer response = new StringBuffer();
   if (isFieldComparison) {
     switch (type) {
       case MONEY:
         response.append(entityKey);
         response.append(".");
         response.append(value[0]);
         response.append(".getAmount()");
         break;
       case STRING:
         if (ignoreCase) {
           response.append("MVEL.eval(\"toUpperCase()\",");
         }
         response.append(entityKey);
         response.append(".");
         response.append(value[0]);
         if (ignoreCase) {
           response.append(")");
         }
         break;
       default:
         response.append(entityKey);
         response.append(".");
         response.append(value[0]);
         break;
     }
   } else {
     for (int j = 0; j < value.length; j++) {
       switch (type) {
         case BOOLEAN:
           response.append(value[j]);
           break;
         case DECIMAL:
           try {
             Double.parseDouble(value[j].toString());
           } catch (Exception e) {
             throw new IncompatibleMVELTranslationException(
                 "Cannot format value for the field ("
                     + fieldName
                     + ") based on field type. The type of field is Decimal, and you entered: ("
                     + value[j]
                     + ")");
           }
           response.append(value[j]);
           break;
         case ID:
           if (secondaryType != null
               && secondaryType.toString().equals(SupportedFieldType.STRING.toString())) {
             if (ignoreCase) {
               response.append("MVEL.eval(\"toUpperCase()\",");
             }
             if (!ignoreQuotes) {
               response.append("\"");
             }
             response.append(value[j]);
             if (!ignoreQuotes) {
               response.append("\"");
             }
             if (ignoreCase) {
               response.append(")");
             }
           } else {
             try {
               Integer.parseInt(value[j].toString());
             } catch (Exception e) {
               throw new IncompatibleMVELTranslationException(
                   "Cannot format value for the field ("
                       + fieldName
                       + ") based on field type. The type of field is Integer, and you entered: ("
                       + value[j]
                       + ")");
             }
             response.append(value[j]);
           }
           break;
         case INTEGER:
           try {
             Integer.parseInt(value[j].toString());
           } catch (Exception e) {
             throw new IncompatibleMVELTranslationException(
                 "Cannot format value for the field ("
                     + fieldName
                     + ") based on field type. The type of field is Integer, and you entered: ("
                     + value[j]
                     + ")");
           }
           response.append(value[j]);
           break;
         case MONEY:
           try {
             Double.parseDouble(value[j].toString());
           } catch (Exception e) {
             throw new IncompatibleMVELTranslationException(
                 "Cannot format value for the field ("
                     + fieldName
                     + ") based on field type. The type of field is Money, and you entered: ("
                     + value[j]
                     + ")");
           }
           response.append(value[j]);
           break;
         case DATE:
           DateTimeFormat formatter = DateTimeFormat.getFormat("MM/dd/yy H:mm a");
           String formattedDate = formatter.format((Date) value[0]);
           response.append("java.text.DateFormat.getDateTimeInstance(3,3).parse(\"");
           response.append(formattedDate);
           response.append("\")");
           break;
         default:
           if (ignoreCase) {
             response.append("MVEL.eval(\"toUpperCase()\",");
           }
           if (!ignoreQuotes) {
             response.append("\"");
           }
           response.append(value[j]);
           if (!ignoreQuotes) {
             response.append("\"");
           }
           if (ignoreCase) {
             response.append(")");
           }
           break;
       }
       if (j < value.length - 1) {
         response.append(",");
       }
     }
   }
   return response.toString();
 }
 @SuppressWarnings("rawtypes")
 protected void buildExpression(
     Criteria criteria,
     StringBuffer sb,
     String entityKey,
     OperatorId operator,
     DataSource dataSource)
     throws IncompatibleMVELTranslationException {
   Map values = criteria.getValues();
   String field = (String) values.get("fieldName");
   SupportedFieldType type =
       SupportedFieldType.valueOf(dataSource.getField(field).getAttribute("fieldType"));
   SupportedFieldType secondaryType = null;
   String secondaryTypeVal = dataSource.getField(field).getAttribute("secondaryFieldType");
   Object[] value;
   if (secondaryTypeVal != null) {
     secondaryType = SupportedFieldType.valueOf(secondaryTypeVal);
   }
   if (SupportedFieldType.DATE.toString().equals(type.toString())
       && !OperatorId.CONTAINS_FIELD.getValue().equals(operator.getValue())
       && !OperatorId.ENDS_WITH_FIELD.getValue().equals(operator.getValue())
       && !OperatorId.EQUALS_FIELD.getValue().equals(operator.getValue())
       && !OperatorId.GREATER_OR_EQUAL_FIELD.getValue().equals(operator.getValue())
       && !OperatorId.GREATER_THAN_FIELD.getValue().equals(operator.getValue())
       && !OperatorId.LESS_OR_EQUAL_FIELD.getValue().equals(operator.getValue())
       && !OperatorId.LESS_THAN_FIELD.getValue().equals(operator.getValue())
       && !OperatorId.NOT_EQUAL_FIELD.getValue().equals(operator.getValue())
       && !OperatorId.STARTS_WITH_FIELD.getValue().equals(operator.getValue())
       && !OperatorId.BETWEEN.getValue().equals(operator.getValue())
       && !OperatorId.BETWEEN_INCLUSIVE.getValue().equals(operator.getValue())) {
     value = extractDate(criteria, operator, values, "value");
   } else {
     value = extractBasicValues(values.get("value"));
   }
   switch (operator) {
     case CONTAINS:
       {
         buildExpression(
             sb,
             entityKey,
             field,
             value,
             type,
             secondaryType,
             ".contains",
             true,
             false,
             false,
             false,
             false);
         break;
       }
     case CONTAINS_FIELD:
       {
         buildExpression(
             sb,
             entityKey,
             field,
             value,
             type,
             secondaryType,
             ".contains",
             true,
             true,
             false,
             false,
             false);
         break;
       }
     case ENDS_WITH:
       {
         buildExpression(
             sb,
             entityKey,
             field,
             value,
             type,
             secondaryType,
             ".endsWith",
             true,
             false,
             false,
             false,
             false);
         break;
       }
     case ENDS_WITH_FIELD:
       {
         buildExpression(
             sb,
             entityKey,
             field,
             value,
             type,
             secondaryType,
             ".endsWith",
             true,
             true,
             false,
             false,
             false);
         break;
       }
     case EQUALS:
       {
         buildExpression(
             sb,
             entityKey,
             field,
             value,
             type,
             secondaryType,
             "==",
             false,
             false,
             false,
             false,
             false);
         break;
       }
     case EQUALS_FIELD:
       {
         buildExpression(
             sb,
             entityKey,
             field,
             value,
             type,
             secondaryType,
             "==",
             false,
             true,
             false,
             false,
             false);
         break;
       }
     case GREATER_OR_EQUAL:
       {
         buildExpression(
             sb,
             entityKey,
             field,
             value,
             type,
             secondaryType,
             ">=",
             false,
             false,
             false,
             false,
             false);
         break;
       }
     case GREATER_OR_EQUAL_FIELD:
       {
         buildExpression(
             sb,
             entityKey,
             field,
             value,
             type,
             secondaryType,
             ">=",
             false,
             true,
             false,
             false,
             false);
         break;
       }
     case GREATER_THAN:
       {
         buildExpression(
             sb,
             entityKey,
             field,
             value,
             type,
             secondaryType,
             ">",
             false,
             false,
             false,
             false,
             false);
         break;
       }
     case GREATER_THAN_FIELD:
       {
         buildExpression(
             sb,
             entityKey,
             field,
             value,
             type,
             secondaryType,
             ">",
             false,
             true,
             false,
             false,
             false);
         break;
       }
     case ICONTAINS:
       {
         buildExpression(
             sb,
             entityKey,
             field,
             value,
             type,
             secondaryType,
             ".contains",
             true,
             false,
             true,
             false,
             false);
         break;
       }
     case IENDS_WITH:
       {
         buildExpression(
             sb,
             entityKey,
             field,
             value,
             type,
             secondaryType,
             ".endsWith",
             true,
             false,
             true,
             false,
             false);
         break;
       }
     case IEQUALS:
       {
         buildExpression(
             sb,
             entityKey,
             field,
             value,
             type,
             secondaryType,
             "==",
             false,
             false,
             true,
             false,
             false);
         break;
       }
     case INOT_CONTAINS:
       {
         buildExpression(
             sb,
             entityKey,
             field,
             value,
             type,
             secondaryType,
             ".contains",
             true,
             false,
             true,
             true,
             false);
         break;
       }
     case INOT_ENDS_WITH:
       {
         buildExpression(
             sb,
             entityKey,
             field,
             value,
             type,
             secondaryType,
             ".endsWith",
             true,
             false,
             true,
             true,
             false);
         break;
       }
     case INOT_EQUAL:
       {
         buildExpression(
             sb,
             entityKey,
             field,
             value,
             type,
             secondaryType,
             "!=",
             false,
             false,
             true,
             false,
             false);
         break;
       }
     case INOT_STARTS_WITH:
       {
         buildExpression(
             sb,
             entityKey,
             field,
             value,
             type,
             secondaryType,
             ".startsWith",
             true,
             false,
             true,
             true,
             false);
         break;
       }
     case IS_NULL:
       {
         buildExpression(
             sb,
             entityKey,
             field,
             new Object[] {"null"},
             type,
             secondaryType,
             "==",
             false,
             false,
             false,
             false,
             true);
         break;
       }
     case ISTARTS_WITH:
       {
         buildExpression(
             sb,
             entityKey,
             field,
             value,
             type,
             secondaryType,
             ".startsWith",
             true,
             false,
             true,
             false,
             false);
         break;
       }
     case LESS_OR_EQUAL:
       {
         buildExpression(
             sb,
             entityKey,
             field,
             value,
             type,
             secondaryType,
             "<=",
             false,
             false,
             false,
             false,
             false);
         break;
       }
     case LESS_OR_EQUAL_FIELD:
       {
         buildExpression(
             sb,
             entityKey,
             field,
             value,
             type,
             secondaryType,
             "<=",
             false,
             true,
             false,
             false,
             false);
         break;
       }
     case LESS_THAN:
       {
         buildExpression(
             sb,
             entityKey,
             field,
             value,
             type,
             secondaryType,
             "<",
             false,
             false,
             false,
             false,
             false);
         break;
       }
     case LESS_THAN_FIELD:
       {
         buildExpression(
             sb,
             entityKey,
             field,
             value,
             type,
             secondaryType,
             "<",
             false,
             true,
             false,
             false,
             false);
         break;
       }
     case NOT_CONTAINS:
       {
         buildExpression(
             sb,
             entityKey,
             field,
             value,
             type,
             secondaryType,
             ".contains",
             true,
             false,
             false,
             true,
             false);
         break;
       }
     case NOT_ENDS_WITH:
       {
         buildExpression(
             sb,
             entityKey,
             field,
             value,
             type,
             secondaryType,
             ".endsWith",
             true,
             false,
             false,
             true,
             false);
         break;
       }
     case NOT_EQUAL:
       {
         buildExpression(
             sb,
             entityKey,
             field,
             value,
             type,
             secondaryType,
             "!=",
             false,
             false,
             false,
             false,
             false);
         break;
       }
     case NOT_EQUAL_FIELD:
       {
         buildExpression(
             sb,
             entityKey,
             field,
             value,
             type,
             secondaryType,
             "!=",
             false,
             true,
             false,
             false,
             false);
         break;
       }
     case NOT_NULL:
       {
         buildExpression(
             sb,
             entityKey,
             field,
             new Object[] {"null"},
             type,
             secondaryType,
             "!=",
             false,
             false,
             false,
             false,
             true);
         break;
       }
     case NOT_STARTS_WITH:
       {
         buildExpression(
             sb,
             entityKey,
             field,
             value,
             type,
             secondaryType,
             ".startsWith",
             true,
             false,
             false,
             true,
             false);
         break;
       }
     case STARTS_WITH:
       {
         buildExpression(
             sb,
             entityKey,
             field,
             value,
             type,
             secondaryType,
             ".startsWith",
             true,
             false,
             false,
             false,
             false);
         break;
       }
     case STARTS_WITH_FIELD:
       {
         buildExpression(
             sb,
             entityKey,
             field,
             value,
             type,
             secondaryType,
             ".startsWith",
             true,
             true,
             false,
             false,
             false);
         break;
       }
     case BETWEEN:
       {
         if (SupportedFieldType.DATE.toString().equals(type.toString())) {
           sb.append("(");
           buildExpression(
               sb,
               entityKey,
               field,
               extractDate(criteria, OperatorId.GREATER_THAN, values, "start"),
               type,
               secondaryType,
               ">",
               false,
               false,
               false,
               false,
               false);
           sb.append("&&");
           buildExpression(
               sb,
               entityKey,
               field,
               extractDate(criteria, OperatorId.LESS_THAN, values, "end"),
               type,
               secondaryType,
               "<",
               false,
               false,
               false,
               false,
               false);
           sb.append(")");
         } else {
           sb.append("(");
           buildExpression(
               sb,
               entityKey,
               field,
               new Object[] {values.get("start")},
               type,
               secondaryType,
               ">",
               false,
               false,
               false,
               false,
               false);
           sb.append("&&");
           buildExpression(
               sb,
               entityKey,
               field,
               new Object[] {values.get("end")},
               type,
               secondaryType,
               "<",
               false,
               false,
               false,
               false,
               false);
           sb.append(")");
         }
         break;
       }
     case BETWEEN_INCLUSIVE:
       {
         if (SupportedFieldType.DATE.toString().equals(type.toString())) {
           sb.append("(");
           buildExpression(
               sb,
               entityKey,
               field,
               extractDate(
                   criteria, OperatorId.GREATER_OR_EQUAL, (Map) values.get("start"), "start"),
               type,
               secondaryType,
               ">=",
               false,
               false,
               false,
               false,
               false);
           sb.append("&&");
           buildExpression(
               sb,
               entityKey,
               field,
               extractDate(criteria, OperatorId.LESS_OR_EQUAL, (Map) values.get("end"), "end"),
               type,
               secondaryType,
               "<=",
               false,
               false,
               false,
               false,
               false);
           sb.append(")");
         } else {
           sb.append("(");
           buildExpression(
               sb,
               entityKey,
               field,
               new Object[] {values.get("start")},
               type,
               secondaryType,
               ">=",
               false,
               false,
               false,
               false,
               false);
           sb.append("&&");
           buildExpression(
               sb,
               entityKey,
               field,
               new Object[] {values.get("end")},
               type,
               secondaryType,
               "<=",
               false,
               false,
               false,
               false,
               false);
           sb.append(")");
         }
         break;
       }
   }
 }