Coverage Report - org.kuali.rice.core.jdbc.SqlBuilder
 
Classes in this File Line Coverage Branch Coverage Complexity
SqlBuilder
0%
0/224
0%
0/136
3.625
 
 1  
 /*
 2  
  * Copyright 2007-2009 The Kuali Foundation
 3  
  *
 4  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  * http://www.opensource.org/licenses/ecl2.php
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 package org.kuali.rice.core.jdbc;
 17  
 
 18  
 import org.apache.commons.lang.StringUtils;
 19  
 import org.kuali.rice.core.database.platform.DatabasePlatform;
 20  
 import org.kuali.rice.core.jdbc.criteria.Criteria;
 21  
 import org.kuali.rice.core.resourceloader.GlobalResourceLoader;
 22  
 import org.kuali.rice.core.util.RiceConstants;
 23  
 import org.kuali.rice.kew.docsearch.SearchableAttribute;
 24  
 import org.kuali.rice.kns.service.DateTimeService;
 25  
 import org.kuali.rice.kns.service.KNSServiceLocator;
 26  
 import org.kuali.rice.kns.util.*;
 27  
 import org.kuali.rice.kns.web.format.BooleanFormatter;
 28  
 
 29  
 import java.math.BigDecimal;
 30  
 import java.sql.Date;
 31  
 import java.sql.Timestamp;
 32  
 import java.text.ParseException;
 33  
 import java.text.SimpleDateFormat;
 34  
 import java.util.ArrayList;
 35  
 import java.util.Arrays;
 36  
 import java.util.List;
 37  
 
 38  
 /**
 39  
  * This is a description of what this class does - Garey don't forget to fill this in.
 40  
  *
 41  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 42  
  *
 43  
  */
 44  0
 public class SqlBuilder {
 45  0
         private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(SqlBuilder.class);
 46  
 
 47  
         private DateTimeService dateTimeService;
 48  
         private DatabasePlatform dbPlatform;
 49  
 
 50  
         public Criteria createCriteria(String columnName, String searchValue, String tableName, String tableAlias, Class propertyType) {
 51  0
                 return createCriteria(columnName, searchValue, tableName, tableAlias, propertyType, false, true);
 52  
         }
 53  
 
 54  
         public Criteria createCriteria(String columnName, String searchValue, String tableName, String tableAlias, Class propertyType, boolean caseInsensitive, boolean allowWildcards) {
 55  
 
 56  0
                 if (propertyType == null) {
 57  0
                         return null;
 58  
                 }
 59  
 
 60  0
                 Criteria criteria = new Criteria(tableName, tableAlias);
 61  0
                 criteria.setDbPlatform(this.getDbPlatform());
 62  
 
 63  
                 // build criteria
 64  0
                 addCriteria(columnName, searchValue, propertyType, caseInsensitive, allowWildcards, criteria);
 65  0
                 return criteria;
 66  
         }
 67  
 
 68  
         public void andCriteria(String columnName, String searchValue, String tableName, String tableAlias, Class propertyType, boolean caseInsensitive, boolean allowWildcards, Criteria addToThisCriteria) {
 69  0
                 Criteria crit = createCriteria(columnName,searchValue, tableName, tableAlias, propertyType, caseInsensitive, allowWildcards);
 70  
 
 71  0
                 addToThisCriteria.and(crit);
 72  0
         }
 73  
         public void andCriteria(Criteria addToThisCriteria, Criteria newCriteria) {
 74  0
                 addToThisCriteria.and(newCriteria);
 75  0
         }
 76  
         public void orCriteria(String columnName, String searchValue, String tableName, String tableAlias, Class propertyType, boolean caseInsensitive, boolean allowWildcards, Criteria addToThisCriteria) {
 77  0
                 Criteria crit = createCriteria(columnName, searchValue,tableName, tableAlias, propertyType, caseInsensitive, allowWildcards);
 78  
 
 79  0
                 addToThisCriteria.or(crit);
 80  0
         }
 81  
 
 82  
         /**
 83  
          * @see org.kuali.rice.kns.dao.LookupDao#findObjectByMap(java.lang.Object,
 84  
          *      java.util.Map)
 85  
          *
 86  
         public Object findObjectByMap(Object example, Map formProps) {
 87  
                 Criteria jpaCriteria = new Criteria(example.getClass().getName());
 88  
 
 89  
                 Iterator propsIter = formProps.keySet().iterator();
 90  
                 while (propsIter.hasNext()) {
 91  
                         String propertyName = (String) propsIter.next();
 92  
                         String searchValue = "";
 93  
                         if (formProps.get(propertyName) != null) {
 94  
                                 searchValue = (formProps.get(propertyName)).toString();
 95  
                         }
 96  
 
 97  
                         if (StringUtils.isNotBlank(searchValue) & PropertyUtils.isWriteable(example, propertyName)) {
 98  
                                 Class propertyType = ObjectUtils.getPropertyType(example, propertyName, persistenceStructureService);
 99  
                                 if (TypeUtils.isIntegralClass(propertyType) || TypeUtils.isDecimalClass(propertyType)) {
 100  
                                         if (propertyType.equals(Long.class)) {
 101  
                                                 jpaCriteria.eq(propertyName, new Long(searchValue));
 102  
                                         } else {
 103  
                                                 jpaCriteria.eq(propertyName, new Integer(searchValue));
 104  
                                         }
 105  
                                 } else if (TypeUtils.isTemporalClass(propertyType)) {
 106  
                                         jpaCriteria.eq(propertyName, parseDate(ObjectUtils.clean(searchValue)));
 107  
                                 } else {
 108  
                                         jpaCriteria.eq(propertyName, searchValue);
 109  
                                 }
 110  
                         }
 111  
                 }
 112  
 
 113  
                 return new QueryByCriteria(entityManager, jpaCriteria).toQuery().getSingleResult();
 114  
         }
 115  
 */
 116  
         /*
 117  
         public void addCriteria(String propertyName, String propertyValue, Class propertyType, boolean caseInsensitive, boolean treatWildcardsAndOperatorsAsLiteral, Criteria criteria) {
 118  
 
 119  
                 String tableAlias = "__JPA_ALIAS__";
 120  
 
 121  
                 if(criteria != null && criteria.getAlias() != null && !"".equals(criteria.getAlias())){
 122  
                         tableAlias = criteria.getAlias();
 123  
                 }
 124  
                 addCriteria(propertyName, propertyValue, tableAlias,propertyType, caseInsensitive, treatWildcardsAndOperatorsAsLiteral, criteria);
 125  
         }
 126  
 */
 127  
 
 128  
         public void addCriteria(String propertyName, String propertyValue, Class propertyType, boolean caseInsensitive, boolean allowWildcards, Criteria criteria) {
 129  
 
 130  0
                 if(TypeUtils.isJoinClass(propertyType)){ // treat this as a join table.
 131  0
                         String temp = ObjectUtils.clean(propertyValue);
 132  0
                         criteria.eq(propertyName, temp, propertyType);
 133  0
                         return;
 134  
                 }
 135  
 
 136  0
                 if (StringUtils.contains(propertyValue, KNSConstants.OR_LOGICAL_OPERATOR)) {
 137  0
                         addOrCriteria(propertyName, propertyValue, propertyType, caseInsensitive, criteria, allowWildcards);
 138  0
                         return;
 139  
                 }
 140  
 
 141  0
                 if ( StringUtils.contains(propertyValue, KNSConstants.AND_LOGICAL_OPERATOR)) {
 142  0
                         addAndCriteria(propertyName, propertyValue, propertyType, caseInsensitive, criteria, allowWildcards);
 143  0
                         return;
 144  
                 }
 145  
 
 146  0
                 if (TypeUtils.isStringClass(propertyType)) {
 147  0
                         if (StringUtils.contains(propertyValue,
 148  
                                         KNSConstants.NOT_LOGICAL_OPERATOR)) {
 149  0
                                 addNotCriteria(propertyName, propertyValue, propertyType,
 150  
                                                 caseInsensitive, criteria, allowWildcards);
 151  0
             } else if (propertyValue != null && (
 152  
                                             StringUtils.contains(propertyValue, KNSConstants.BETWEEN_OPERATOR)
 153  
                                             || propertyValue.startsWith(">")
 154  
                                             || propertyValue.startsWith("<") ) ) {
 155  0
                                 addStringRangeCriteria(propertyName, propertyValue, criteria, propertyType, caseInsensitive, allowWildcards);
 156  
                         } else {
 157  
                                 //if (!allowWildcards) {
 158  
                                 //        propertyValue = StringUtils.replace(propertyValue, "*", "\\*");
 159  
                                 //}
 160  
                                 // KULRICE-85 : made string searches case insensitive - used new
 161  
                                 // DBPlatform function to force strings to upper case
 162  0
                                 if (caseInsensitive) {
 163  
                                         // TODO: What to do here now that the JPA version does not extend platform aware?
 164  0
                                         propertyName = getDbPlatform().getUpperCaseFunction() + "(__JPA_ALIAS__." + propertyName + ")";
 165  
                                         //propertyName = "UPPER("+ tableAlias + "." + propertyName + ")";
 166  0
                                         propertyValue = propertyValue.toUpperCase();
 167  
                                 }
 168  0
                                 criteria.like(propertyName, propertyValue,propertyType, allowWildcards);
 169  
                         }
 170  0
                 } else if (TypeUtils.isIntegralClass(propertyType) || TypeUtils.isDecimalClass(propertyType)) {
 171  0
                         addNumericRangeCriteria(propertyName, propertyValue, criteria, propertyType);
 172  0
                 } else if (TypeUtils.isTemporalClass(propertyType)) {
 173  0
                         addDateRangeCriteria(propertyName, propertyValue, criteria, propertyType);
 174  0
                 } else if (TypeUtils.isBooleanClass(propertyType)) {
 175  0
                         String temp = ObjectUtils.clean(propertyValue);
 176  0
                         criteria.eq(propertyName, new BooleanFormatter().convertFromPresentationFormat(temp), propertyType);
 177  0
                 } else {
 178  0
                         LOG.error("not adding criterion for: " + propertyName + "," + propertyType + "," + propertyValue);
 179  
                 }
 180  0
         }
 181  
 
 182  
         private void addOrCriteria(String propertyName, String propertyValue, Class propertyType, boolean caseInsensitive, Criteria criteria, boolean allowWildcards) {
 183  0
                 addLogicalOperatorCriteria(propertyName, propertyValue, propertyType, caseInsensitive, criteria, KNSConstants.OR_LOGICAL_OPERATOR, allowWildcards);
 184  0
         }
 185  
 
 186  
         private void addAndCriteria(String propertyName, String propertyValue, Class propertyType, boolean caseInsensitive, Criteria criteria, boolean allowWildcards) {
 187  0
                 addLogicalOperatorCriteria(propertyName, propertyValue, propertyType, caseInsensitive, criteria, KNSConstants.AND_LOGICAL_OPERATOR, allowWildcards);
 188  0
         }
 189  
 
 190  
         private void addNotCriteria(String propertyName, String propertyValue, Class propertyType, boolean caseInsensitive, Criteria criteria, boolean allowWildcards) {
 191  0
                 String[] splitPropVal = StringUtils.split(propertyValue, KNSConstants.NOT_LOGICAL_OPERATOR);
 192  
 
 193  0
                 int strLength = splitPropVal.length;
 194  
                 // if more than one NOT operator assume an implicit and (i.e. !a!b = !a&!b)
 195  0
                 if (strLength > 1) {
 196  0
                         String expandedNot = "!" + StringUtils.join(splitPropVal, KNSConstants.AND_LOGICAL_OPERATOR + KNSConstants.NOT_LOGICAL_OPERATOR);
 197  
                         // we know that since this method is called, treatWildcardsAndOperatorsAsLiteral is false
 198  0
                         addCriteria(propertyName, expandedNot, propertyType, caseInsensitive, allowWildcards, criteria);
 199  0
                 } else {
 200  
                         // only one so add a not like
 201  0
                         criteria.notLike(propertyName, splitPropVal[0], propertyType, allowWildcards);
 202  
                 }
 203  0
         }
 204  
 
 205  
         private void addLogicalOperatorCriteria(String propertyName, String propertyValue, Class propertyType, boolean caseInsensitive, Criteria criteria, String splitValue, boolean allowWildcards) {
 206  0
                 String[] splitPropVal = StringUtils.split(propertyValue, splitValue);
 207  
 
 208  0
                 Criteria subCriteria = new Criteria("N/A");
 209  0
                 for (int i = 0; i < splitPropVal.length; i++) {
 210  0
                         Criteria predicate = new Criteria("N/A", criteria.getAlias());
 211  
                         // we know that since this method is called, treatWildcardsAndOperatorsAsLiteral is false
 212  0
                         addCriteria(propertyName, splitPropVal[i], propertyType, caseInsensitive, allowWildcards, predicate);
 213  0
                         if (splitValue == KNSConstants.OR_LOGICAL_OPERATOR) {
 214  0
                                 subCriteria.or(predicate);
 215  
                         }
 216  0
                         if (splitValue == KNSConstants.AND_LOGICAL_OPERATOR) {
 217  0
                                 subCriteria.and(predicate);
 218  
                         }
 219  
                 }
 220  
 
 221  0
                 criteria.and(subCriteria);
 222  0
         }
 223  
 
 224  
         private Timestamp parseDate(String dateString) {
 225  0
                 dateString = dateString.trim();
 226  
                 try {
 227  0
                         Timestamp dt =  this.getDateTimeService().convertToSqlTimestamp(dateString);
 228  0
                         return dt;
 229  0
                 } catch (ParseException ex) {
 230  0
                         return null;
 231  
                 }
 232  
         }
 233  
         public boolean isValidDate(String dateString){
 234  0
                 dateString = dateString.trim();
 235  
                 try {
 236  0
                         int oldErrorCount = GlobalVariables.getMessageMap().getErrorCount();
 237  0
                         this.createCriteria("date", dateString, "validation", "test", Date.class);
 238  
                         //Timestamp dt =  this.getDateTimeService().convertToSqlTimestamp(cleanDate(dateString));
 239  0
                         return (GlobalVariables.getMessageMap().getErrorCount() <= oldErrorCount);
 240  0
                 } catch (Exception ex) {
 241  0
                         return false;
 242  
                 }
 243  
         }
 244  
 
 245  
         public static String cleanDate(String string) {
 246  0
         for (int i = 0; i < KNSConstants.RANGE_CHARACTERS.length; i++) {
 247  0
             string = StringUtils.replace(string, KNSConstants.RANGE_CHARACTERS[i], KNSConstants.EMPTY_STRING);
 248  
         }
 249  0
         return string;
 250  
     }
 251  
 
 252  
         public static boolean containsRangeCharacters(String string){
 253  0
                 boolean bRet = false;
 254  0
                 for (int i = 0; i < KNSConstants.RANGE_CHARACTERS.length; i++) {
 255  0
             if(StringUtils.contains(string, KNSConstants.RANGE_CHARACTERS[i])){
 256  0
                     bRet = true;
 257  
             }
 258  
         }
 259  0
                 return bRet;
 260  
         }
 261  
 
 262  
         private void addDateRangeCriteria(String propertyName, String propertyValue, Criteria criteria, Class propertyType) {
 263  
 
 264  0
                 if (StringUtils.contains(propertyValue, KNSConstants.BETWEEN_OPERATOR)) {
 265  0
                         String[] rangeValues = propertyValue.split("\\.\\."); // this translate to the .. operator
 266  0
                         criteria.between(propertyName, parseDate(cleanDate(rangeValues[0])), parseDate(cleanUpperBound(cleanDate(rangeValues[1]))), propertyType);
 267  0
                 } else if (propertyValue.startsWith(">=")) {
 268  0
                         criteria.gte(propertyName, parseDate(cleanDate(propertyValue)), propertyType);
 269  0
                 } else if (propertyValue.startsWith("<=")) {
 270  0
                         criteria.lte(propertyName, parseDate(cleanUpperBound(cleanDate(propertyValue))),propertyType);
 271  0
                 } else if (propertyValue.startsWith(">")) {
 272  
                         // we clean the upper bound here because if you say >12/22/09, it translates greater than
 273  
                         // the date... as in whole date. ie. the next day on.
 274  0
                         criteria.gt(propertyName, parseDate(cleanUpperBound(cleanDate(propertyValue))), propertyType);
 275  0
                 } else if (propertyValue.startsWith("<")) {
 276  0
                         criteria.lt(propertyName, parseDate(cleanDate(propertyValue)), propertyType);
 277  
                 } else {
 278  0
                         String sDate = convertSimpleDateToDateRange(cleanDate(propertyValue));
 279  0
                         if(sDate.contains(KNSConstants.BETWEEN_OPERATOR)){
 280  0
                                 addDateRangeCriteria(propertyName, sDate, criteria, propertyType);
 281  
                         }else{
 282  0
                                 criteria.eq(propertyName, parseDate(sDate), propertyType);
 283  
                         }
 284  
                 }
 285  0
         }
 286  
 
 287  
         public static boolean isValidNumber(String value){
 288  
                 try{
 289  0
                 BigDecimal bd = stringToBigDecimal(value);
 290  0
                         return true;
 291  0
                 }catch(Exception ex){
 292  0
                         return false;
 293  
                 }
 294  
         }
 295  
 
 296  
         public static String cleanNumericOfValidOperators(String string){
 297  0
                 for (int i = 0; i < KNSConstants.RANGE_CHARACTERS.length; i++) {
 298  0
             string = StringUtils.replace(string, KNSConstants.RANGE_CHARACTERS[i], KNSConstants.EMPTY_STRING);
 299  
         }
 300  0
                 string = StringUtils.replace(string, KNSConstants.OR_LOGICAL_OPERATOR, KNSConstants.EMPTY_STRING);
 301  0
                 string = StringUtils.replace(string, KNSConstants.AND_LOGICAL_OPERATOR, KNSConstants.EMPTY_STRING);
 302  0
                 string = StringUtils.replace(string, KNSConstants.NOT_LOGICAL_OPERATOR, KNSConstants.EMPTY_STRING);
 303  
 
 304  0
                 return string;
 305  
         }
 306  
 
 307  
         public static String cleanNumeric(String value){
 308  0
                 String cleanedValue = value.replaceAll("[^-0-9.]", "");
 309  
                 // ensure only one "minus" at the beginning, if any
 310  0
                 if (cleanedValue.lastIndexOf('-') > 0) {
 311  0
                         if (cleanedValue.charAt(0) == '-') {
 312  0
                                 cleanedValue = "-" + cleanedValue.replaceAll("-", "");
 313  
                         } else {
 314  0
                                 cleanedValue = cleanedValue.replaceAll("-", "");
 315  
                         }
 316  
                 }
 317  
                 // ensure only one decimal in the string
 318  0
                 int decimalLoc = cleanedValue.lastIndexOf('.');
 319  0
                 if (cleanedValue.indexOf('.') != decimalLoc) {
 320  0
                         cleanedValue = cleanedValue.substring(0, decimalLoc).replaceAll("\\.", "") + cleanedValue.substring(decimalLoc);
 321  
                 }
 322  0
                 return cleanedValue;
 323  
         }
 324  
 
 325  
         public static BigDecimal stringToBigDecimal(String value) {
 326  
 
 327  
                 //try {
 328  0
                         return new BigDecimal(cleanNumeric(value));
 329  
                 /*
 330  
                 } catch (NumberFormatException ex) {
 331  
                         GlobalVariables.getMessageMap().putError(KNSConstants.DOCUMENT_ERRORS, RiceKeyConstants.ERROR_CUSTOM, new String[] { "Invalid Numeric Input: " + value });
 332  
                         return null;
 333  
                 }*/
 334  
         }
 335  
 
 336  
         private void addNumericRangeCriteria(String propertyName, String propertyValue, Criteria criteria, Class propertyType) {
 337  
 
 338  0
                 if (StringUtils.contains(propertyValue, KNSConstants.BETWEEN_OPERATOR)) {
 339  0
                         String[] rangeValues = propertyValue.split("\\.\\."); // this translate to the .. operator
 340  0
                         criteria.between(propertyName, stringToBigDecimal(rangeValues[0]), stringToBigDecimal(rangeValues[1]), propertyType);
 341  0
                 } else if (propertyValue.startsWith(">=")) {
 342  0
                         criteria.gte(propertyName, stringToBigDecimal(propertyValue), propertyType);
 343  0
                 } else if (propertyValue.startsWith("<=")) {
 344  0
                         criteria.lte(propertyName, stringToBigDecimal(propertyValue), propertyType);
 345  0
                 } else if (propertyValue.startsWith(">")) {
 346  0
                         criteria.gt(propertyName, stringToBigDecimal(propertyValue), propertyType);
 347  0
                 } else if (propertyValue.startsWith("<")) {
 348  0
                         criteria.lt(propertyName, stringToBigDecimal(propertyValue), propertyType);
 349  
                 } else {
 350  0
                         criteria.eq(propertyName, stringToBigDecimal(propertyValue), propertyType);
 351  
                 }
 352  0
         }
 353  
 
 354  
         private void addStringRangeCriteria(String propertyName, String propertyValue, Criteria criteria, Class propertyType, boolean caseInsensitive, boolean allowWildcards) {
 355  
 
 356  0
                 if (StringUtils.contains(propertyValue, KNSConstants.BETWEEN_OPERATOR)) {
 357  0
                         String[] rangeValues = propertyValue.split("\\.\\."); // this translate to the .. operator
 358  0
                         propertyName = this.getCaseAndLiteralPropertyName(propertyName, caseInsensitive);
 359  0
                         String val1 = this.getCaseAndLiteralPropertyValue(rangeValues[0], caseInsensitive, allowWildcards);
 360  0
                         String val2 = this.getCaseAndLiteralPropertyValue(rangeValues[1], caseInsensitive, allowWildcards);
 361  0
                         criteria.between(propertyName, val1, val2, propertyType);
 362  0
                 } else{
 363  0
                         propertyName = this.getCaseAndLiteralPropertyName(propertyName, caseInsensitive);
 364  0
                         String value = this.getCaseAndLiteralPropertyValue(ObjectUtils.clean(propertyValue), caseInsensitive, allowWildcards);
 365  
 
 366  0
                         if (propertyValue.startsWith(">=")) {
 367  0
                                 criteria.gte(propertyName, value, propertyType);
 368  0
                         } else if (propertyValue.startsWith("<=")) {
 369  0
                                 criteria.lte(propertyName, value, propertyType);
 370  0
                         } else if (propertyValue.startsWith(">")) {
 371  0
                                 criteria.gt(propertyName, value, propertyType);
 372  0
                         } else if (propertyValue.startsWith("<")) {
 373  0
                                 criteria.lt(propertyName, value, propertyType);
 374  
                         }
 375  
                 }
 376  0
         }
 377  
 
 378  
         private String getCaseAndLiteralPropertyName(String propertyName, boolean caseInsensitive){
 379  
                 // KULRICE-85 : made string searches case insensitive - used new
 380  
                 // DBPlatform function to force strings to upper case
 381  0
                 if (caseInsensitive) {
 382  
                         // TODO: What to do here now that the JPA version does not extend platform aware?
 383  0
                         propertyName = getDbPlatform().getUpperCaseFunction() + "(__JPA_ALIAS__." + propertyName + ")";
 384  
 
 385  
                 }
 386  0
                 return propertyName;
 387  
         }
 388  
         private String getCaseAndLiteralPropertyValue(String propertyValue, boolean caseInsensitive, boolean allowWildcards){
 389  
                 //if (!allowWildcards) {
 390  
                 //        propertyValue = StringUtils.replace(propertyValue, "*", "\\*");
 391  
                 //}
 392  
                 // KULRICE-85 : made string searches case insensitive - used new
 393  
                 // DBPlatform function to force strings to upper case
 394  0
                 if (caseInsensitive) {
 395  
                         //propertyName = "UPPER("+ tableAlias + "." + propertyName + ")";
 396  0
                         propertyValue = propertyValue.toUpperCase();
 397  
                 }
 398  0
                 return propertyValue;
 399  
         }
 400  
 
 401  
 
 402  
         protected DateTimeService getDateTimeService(){
 403  0
                 if (dateTimeService == null) {
 404  0
                         dateTimeService = KNSServiceLocator.getDateTimeService();
 405  
             }
 406  0
             return dateTimeService;
 407  
         }
 408  
 
 409  
         /**
 410  
          * @param dateTimeService
 411  
          *            the dateTimeService to set
 412  
          */
 413  
         public void setDateTimeService(DateTimeService dateTimeService) {
 414  0
                 this.dateTimeService = dateTimeService;
 415  0
         }
 416  
 
 417  
         public DatabasePlatform getDbPlatform() {
 418  0
             if (dbPlatform == null) {
 419  0
                     dbPlatform = (DatabasePlatform) GlobalResourceLoader.getService(RiceConstants.DB_PLATFORM);
 420  
             }
 421  0
             return dbPlatform;
 422  
     }
 423  
 
 424  
         public void setDbPlatform(DatabasePlatform dbPlatform){
 425  0
                 this.dbPlatform = dbPlatform;
 426  0
         }
 427  
 
 428  
          /**
 429  
      * When dealing with upperbound dates, it is a business requirement that if a timestamp isn't already
 430  
      * stated append 23:59:59 to the end of the date.  This ensures that you are searching for the entire
 431  
      * day.
 432  
      */
 433  
     private static String cleanUpperBound(String stringDate){
 434  
             try{
 435  0
                     java.sql.Timestamp dt = KNSServiceLocator.getDateTimeService().convertToSqlTimestamp(stringDate);
 436  0
                     SimpleDateFormat sdfTime = new SimpleDateFormat("HH:mm:ss");
 437  
 
 438  0
                         if("00:00:00".equals(sdfTime.format(dt)) && !StringUtils.contains(stringDate, "00:00:00") && !StringUtils.contains(stringDate, "12:00 AM")){
 439  0
                                 stringDate = stringDate + " 23:59:59";
 440  
                         }
 441  0
                 } catch (Exception ex){
 442  0
                         GlobalVariables.getMessageMap().putError(KNSConstants.DOCUMENT_ERRORS, RiceKeyConstants.ERROR_CUSTOM, new String[] { "Invalid Date Input: " + stringDate });
 443  0
                 }
 444  0
                 return stringDate;
 445  
     }
 446  
 
 447  
     /**
 448  
     *
 449  
     * This method will take a whole date like 03/02/2009 and convert it into
 450  
     * 03/02/2009 .. 03/02/20009 00:00:00
 451  
     *
 452  
     * This is used for non-range searchable attributes
 453  
     *
 454  
     * @param stringDate
 455  
     * @return
 456  
     */
 457  
    private static String convertSimpleDateToDateRange(String stringDate){
 458  
            try{
 459  0
                    java.sql.Timestamp dt = KNSServiceLocator.getDateTimeService().convertToSqlTimestamp(stringDate);
 460  0
                    SimpleDateFormat sdfTime = new SimpleDateFormat("HH:mm:ss");
 461  
 
 462  0
                         if("00:00:00".equals(sdfTime.format(dt)) && !StringUtils.contains(stringDate, "00:00:00") && !StringUtils.contains(stringDate, "12:00 AM")){
 463  0
                                 stringDate = stringDate + " .. " + stringDate + " 23:59:59";
 464  
                         }
 465  0
                 } catch (Exception ex){
 466  0
                         GlobalVariables.getMessageMap().putError(KNSConstants.DOCUMENT_ERRORS, RiceKeyConstants.ERROR_CUSTOM, new String[] { "Invalid Date Input: " + stringDate });
 467  0
                 }
 468  0
                 return stringDate;
 469  
    }
 470  
 
 471  
    /**
 472  
    *
 473  
    * This method splits the values then cleans them of any other query characters like *?!><...
 474  
    *
 475  
    * @param valueEntered
 476  
    * @param propertyDataType
 477  
    * @return
 478  
    */
 479  
   public static List<String> getCleanedSearchableValues(String valueEntered, String propertyDataType) {
 480  0
            List<String> lRet = null;
 481  0
            List<String> lTemp = getSearchableValues(valueEntered);
 482  0
            if(lTemp != null && !lTemp.isEmpty()){
 483  0
                    lRet = new ArrayList<String>();
 484  0
                    for(String val: lTemp){
 485  
                            // Clean the wildcards appropriately, depending on the field's data type.
 486  0
                            if (SearchableAttribute.DATA_TYPE_STRING.equals(propertyDataType)) {
 487  0
                                    lRet.add(ObjectUtils.clean(val));
 488  0
                            } else if (SearchableAttribute.DATA_TYPE_FLOAT.equals(propertyDataType) || SearchableAttribute.DATA_TYPE_LONG.equals(propertyDataType)) {
 489  0
                                    lRet.add(SqlBuilder.cleanNumericOfValidOperators(val));
 490  0
                            } else if (SearchableAttribute.DATA_TYPE_DATE.equals(propertyDataType)) {
 491  0
                                    lRet.add(SqlBuilder.cleanDate(val));
 492  
                            } else {
 493  0
                                    lRet.add(ObjectUtils.clean(val));
 494  
                            }
 495  
                    }
 496  
            }
 497  0
            return lRet;
 498  
   }
 499  
 
 500  
    /**
 501  
     *
 502  
     * This method splits the valueEntered on locical operators and, or, and between
 503  
     *
 504  
     * @param valueEntered
 505  
     * @return
 506  
     */
 507  
    public static List<String> getSearchableValues(String valueEntered) {
 508  0
                 List<String> lRet = new ArrayList<String>();
 509  
 
 510  0
                 getSearchableValueRecursive(valueEntered, lRet);
 511  
 
 512  0
                 return lRet;
 513  
         }
 514  
 
 515  
         protected static void getSearchableValueRecursive(String valueEntered, List lRet) {
 516  0
                 if(valueEntered == null)return;
 517  
 
 518  0
                 valueEntered = valueEntered.trim();
 519  
 
 520  0
                 if(lRet == null){
 521  0
                         throw new NullPointerException("The list passed in is by reference and should never be null.");
 522  
                 }
 523  
 
 524  0
                 if (StringUtils.contains(valueEntered, KNSConstants.BETWEEN_OPERATOR)) {
 525  0
                         List<String> l = Arrays.asList(valueEntered.split("\\.\\."));
 526  0
                         for(String value : l){
 527  0
                                 getSearchableValueRecursive(value,lRet);
 528  
                         }
 529  0
                         return;
 530  
                 }
 531  0
                 if (StringUtils.contains(valueEntered, KNSConstants.OR_LOGICAL_OPERATOR)) {
 532  0
                         List<String> l = Arrays.asList(StringUtils.split(valueEntered, KNSConstants.OR_LOGICAL_OPERATOR));
 533  0
                         for(String value : l){
 534  0
                                 getSearchableValueRecursive(value,lRet);
 535  
                         }
 536  0
                         return;
 537  
                 }
 538  0
                 if (StringUtils.contains(valueEntered, KNSConstants.AND_LOGICAL_OPERATOR)) {
 539  
                         //splitValueList.addAll(Arrays.asList(StringUtils.split(valueEntered, KNSConstants.AND_LOGICAL_OPERATOR)));
 540  0
                         List<String> l = Arrays.asList(StringUtils.split(valueEntered, KNSConstants.AND_LOGICAL_OPERATOR));
 541  0
                         for(String value : l){
 542  0
                                 getSearchableValueRecursive(value,lRet);
 543  
                         }
 544  0
                         return;
 545  
                 }
 546  
 
 547  
                 // lRet is pass by ref and should NEVER be null
 548  0
                 lRet.add(valueEntered);
 549  0
    }
 550  
 
 551  
 
 552  
 }