|  1 |     | 
     | 
  |  2 |     | 
     | 
  |  3 |     | 
     | 
  |  4 |     | 
     | 
  |  5 |     | 
     | 
  |  6 |     | 
     | 
  |  7 |     | 
     | 
  |  8 |     | 
     | 
  |  9 |     | 
     | 
  |  10 |     | 
     | 
  |  11 |     | 
     | 
  |  12 |     | 
     | 
  |  13 |     | 
     | 
  |  14 |     | 
     | 
  |  15 |     | 
     | 
  |  16 |     | 
   package org.kuali.rice.kns.dao.impl;  | 
  |  17 |     | 
     | 
  |  18 |     | 
   import java.math.BigDecimal;  | 
  |  19 |     | 
   import java.sql.Timestamp;  | 
  |  20 |     | 
   import java.text.ParseException;  | 
  |  21 |     | 
   import java.util.ArrayList;  | 
  |  22 |     | 
   import java.util.Collection;  | 
  |  23 |     | 
   import java.util.Iterator;  | 
  |  24 |     | 
   import java.util.List;  | 
  |  25 |     | 
   import java.util.Map;  | 
  |  26 |     | 
     | 
  |  27 |     | 
   import org.apache.commons.beanutils.PropertyUtils;  | 
  |  28 |     | 
   import org.apache.commons.lang.StringUtils;  | 
  |  29 |     | 
   import org.apache.ojb.broker.query.Criteria;  | 
  |  30 |     | 
   import org.apache.ojb.broker.query.Query;  | 
  |  31 |     | 
   import org.apache.ojb.broker.query.QueryByCriteria;  | 
  |  32 |     | 
   import org.apache.ojb.broker.query.QueryFactory;  | 
  |  33 |     | 
   import org.kuali.rice.core.api.datetime.DateTimeService;  | 
  |  34 |     | 
   import org.kuali.rice.core.util.RiceKeyConstants;  | 
  |  35 |     | 
   import org.kuali.rice.core.util.type.TypeUtils;  | 
  |  36 |     | 
   import org.kuali.rice.kns.bo.BusinessObject;  | 
  |  37 |     | 
   import org.kuali.rice.kns.bo.InactivateableFromTo;  | 
  |  38 |     | 
   import org.kuali.rice.kns.dao.LookupDao;  | 
  |  39 |     | 
   import org.kuali.rice.kns.lookup.CollectionIncomplete;  | 
  |  40 |     | 
   import org.kuali.rice.kns.lookup.LookupUtils;  | 
  |  41 |     | 
   import org.kuali.rice.kns.service.BusinessObjectDictionaryService;  | 
  |  42 |     | 
   import org.kuali.rice.kns.service.KNSServiceLocatorWeb;  | 
  |  43 |     | 
   import org.kuali.rice.kns.service.PersistenceStructureService;  | 
  |  44 |     | 
   import org.kuali.rice.kns.util.GlobalVariables;  | 
  |  45 |     | 
   import org.kuali.rice.kns.util.KNSConstants;  | 
  |  46 |     | 
   import org.kuali.rice.kns.util.KNSPropertyConstants;  | 
  |  47 |     | 
   import org.kuali.rice.kns.util.ObjectUtils;  | 
  |  48 |     | 
   import org.kuali.rice.core.framework.persistence.ojb.conversion.OjbCharBooleanConversion;  | 
  |  49 |     | 
   import org.springframework.dao.DataIntegrityViolationException;  | 
  |  50 |     | 
   import org.springmodules.orm.ojb.OjbOperationException;  | 
  |  51 |     | 
     | 
  |  52 |     | 
     | 
  |  53 |     | 
     | 
  |  54 |     | 
     | 
  |  55 |    0 |    public class LookupDaoOjb extends PlatformAwareDaoBaseOjb implements LookupDao { | 
  |  56 |    0 |        private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(LookupDaoOjb.class);  | 
  |  57 |     | 
       private DateTimeService dateTimeService;  | 
  |  58 |     | 
       private PersistenceStructureService persistenceStructureService;  | 
  |  59 |     | 
       private BusinessObjectDictionaryService businessObjectDictionaryService;  | 
  |  60 |     | 
     | 
  |  61 |     | 
       public Collection findCollectionBySearchHelper(Class businessObjectClass, Map formProps, boolean unbounded, boolean usePrimaryKeyValuesOnly) { | 
  |  62 |    0 |            return findCollectionBySearchHelper( businessObjectClass, formProps, unbounded, usePrimaryKeyValuesOnly, null );  | 
  |  63 |     | 
       }  | 
  |  64 |     | 
     | 
  |  65 |     | 
       public Collection findCollectionBySearchHelper(Class businessObjectClass, Map formProps, boolean unbounded, boolean usePrimaryKeyValuesOnly, Object additionalCriteria ) { | 
  |  66 |    0 |            BusinessObject businessObject = checkBusinessObjectClass(businessObjectClass);  | 
  |  67 |    0 |            if (usePrimaryKeyValuesOnly) { | 
  |  68 |    0 |                    return executeSearch(businessObjectClass, getCollectionCriteriaFromMapUsingPrimaryKeysOnly(businessObjectClass, formProps), unbounded);  | 
  |  69 |     | 
           }  | 
  |  70 |     | 
             | 
  |  71 |    0 |                    Criteria crit = getCollectionCriteriaFromMap(businessObject, formProps);  | 
  |  72 |    0 |                    if (additionalCriteria != null && additionalCriteria instanceof Criteria) { | 
  |  73 |    0 |                            crit.addAndCriteria((Criteria) additionalCriteria);  | 
  |  74 |     | 
                   }  | 
  |  75 |     | 
     | 
  |  76 |    0 |                    return executeSearch(businessObjectClass, crit, unbounded);  | 
  |  77 |     | 
           }  | 
  |  78 |     | 
     | 
  |  79 |     | 
         | 
  |  80 |     | 
     | 
  |  81 |     | 
     | 
  |  82 |     | 
       public Criteria getCollectionCriteriaFromMap(BusinessObject example, Map formProps) { | 
  |  83 |    0 |            Criteria criteria = new Criteria();  | 
  |  84 |    0 |            Iterator propsIter = formProps.keySet().iterator();  | 
  |  85 |    0 |            while (propsIter.hasNext()) { | 
  |  86 |    0 |                String propertyName = (String) propsIter.next();  | 
  |  87 |    0 |                Boolean caseInsensitive = Boolean.TRUE;  | 
  |  88 |    0 |                    if ( KNSServiceLocatorWeb.getDataDictionaryService().isAttributeDefined( example.getClass(), propertyName )) { | 
  |  89 |    0 |                            caseInsensitive = !KNSServiceLocatorWeb.getDataDictionaryService().getAttributeForceUppercase( example.getClass(), propertyName );  | 
  |  90 |     | 
                   }  | 
  |  91 |    0 |                    if ( caseInsensitive == null ) { caseInsensitive = Boolean.TRUE; } | 
  |  92 |    0 |                    boolean treatWildcardsAndOperatorsAsLiteral = KNSServiceLocatorWeb  | 
  |  93 |     | 
                                   .getBusinessObjectDictionaryService().isLookupFieldTreatWildcardsAndOperatorsAsLiteral(example.getClass(), propertyName);  | 
  |  94 |     | 
                     | 
  |  95 |    0 |                if (formProps.get(propertyName) instanceof Collection) { | 
  |  96 |    0 |                    Iterator iter = ((Collection) formProps.get(propertyName)).iterator();  | 
  |  97 |    0 |                    while (iter.hasNext()) { | 
  |  98 |    0 |                        if (!createCriteria(example, (String) iter.next(), propertyName, caseInsensitive, treatWildcardsAndOperatorsAsLiteral, criteria, formProps )) { | 
  |  99 |    0 |                            throw new RuntimeException("Invalid value in Collection"); | 
  |  100 |     | 
                       }  | 
  |  101 |     | 
                   }  | 
  |  102 |    0 |                }  | 
  |  103 |     | 
               else { | 
  |  104 |    0 |                    if (!createCriteria(example, (String) formProps.get(propertyName), propertyName, caseInsensitive, treatWildcardsAndOperatorsAsLiteral, criteria, formProps)) { | 
  |  105 |    0 |                        continue;  | 
  |  106 |     | 
                   }  | 
  |  107 |     | 
               }  | 
  |  108 |    0 |            }  | 
  |  109 |    0 |            return criteria;  | 
  |  110 |     | 
       }  | 
  |  111 |     | 
         | 
  |  112 |     | 
       public Criteria getCollectionCriteriaFromMapUsingPrimaryKeysOnly(Class businessObjectClass, Map formProps) { | 
  |  113 |    0 |            BusinessObject businessObject = checkBusinessObjectClass(businessObjectClass);  | 
  |  114 |    0 |            Criteria criteria = new Criteria();  | 
  |  115 |    0 |            List pkFields = KNSServiceLocatorWeb.getBusinessObjectMetaDataService().listPrimaryKeyFieldNames(businessObjectClass);  | 
  |  116 |    0 |            Iterator pkIter = pkFields.iterator();  | 
  |  117 |    0 |            while (pkIter.hasNext()) { | 
  |  118 |    0 |                String pkFieldName = (String) pkIter.next();  | 
  |  119 |    0 |                String pkValue = (String) formProps.get(pkFieldName);  | 
  |  120 |     | 
     | 
  |  121 |    0 |                if (StringUtils.isBlank(pkValue)) { | 
  |  122 |    0 |                    throw new RuntimeException("Missing pk value for field " + pkFieldName + " when a search based on PK values only is performed."); | 
  |  123 |     | 
               }  | 
  |  124 |    0 |                else if (StringUtils.indexOfAny(pkValue, KNSConstants.QUERY_CHARACTERS) != -1) { | 
  |  125 |    0 |                    throw new RuntimeException("Value \"" + pkValue + "\" for PK field " + pkFieldName + " contains wildcard/operator characters."); | 
  |  126 |     | 
               }  | 
  |  127 |    0 |                boolean treatWildcardsAndOperatorsAsLiteral = KNSServiceLocatorWeb.  | 
  |  128 |     | 
                               getBusinessObjectDictionaryService().isLookupFieldTreatWildcardsAndOperatorsAsLiteral(businessObjectClass, pkFieldName);  | 
  |  129 |    0 |                createCriteria(businessObject, pkValue, pkFieldName, false, treatWildcardsAndOperatorsAsLiteral, criteria);  | 
  |  130 |    0 |            }  | 
  |  131 |    0 |            return criteria;  | 
  |  132 |     | 
       }  | 
  |  133 |     | 
         | 
  |  134 |     | 
       private BusinessObject checkBusinessObjectClass(Class businessObjectClass) { | 
  |  135 |    0 |            if (businessObjectClass == null) { | 
  |  136 |    0 |                throw new IllegalArgumentException("BusinessObject class passed to LookupDaoOjb findCollectionBySearchHelper... method was null"); | 
  |  137 |     | 
           }  | 
  |  138 |    0 |            BusinessObject businessObject = null;  | 
  |  139 |     | 
           try { | 
  |  140 |    0 |                businessObject = (BusinessObject) businessObjectClass.newInstance();  | 
  |  141 |     | 
           }  | 
  |  142 |    0 |            catch (IllegalAccessException e) { | 
  |  143 |    0 |                throw new RuntimeException("LookupDaoOjb could not get instance of " + businessObjectClass.getName(), e); | 
  |  144 |     | 
           }  | 
  |  145 |    0 |            catch (InstantiationException e) { | 
  |  146 |    0 |                throw new RuntimeException("LookupDaoOjb could not get instance of " + businessObjectClass.getName(), e); | 
  |  147 |    0 |            }  | 
  |  148 |    0 |            return businessObject;  | 
  |  149 |     | 
       }  | 
  |  150 |     | 
     | 
  |  151 |     | 
       private Collection executeSearch(Class businessObjectClass, Criteria criteria, boolean unbounded) { | 
  |  152 |    0 |                Collection searchResults = new ArrayList();  | 
  |  153 |    0 |                Long matchingResultsCount = null;  | 
  |  154 |     | 
               try { | 
  |  155 |    0 |                        Integer searchResultsLimit = LookupUtils.getSearchResultsLimit(businessObjectClass);  | 
  |  156 |    0 |                        if (!unbounded && (searchResultsLimit != null)) { | 
  |  157 |    0 |                                matchingResultsCount = new Long(getPersistenceBrokerTemplate().getCount(QueryFactory.newQuery(businessObjectClass, criteria)));  | 
  |  158 |    0 |                                LookupUtils.applySearchResultsLimit(businessObjectClass, criteria, getDbPlatform());  | 
  |  159 |     | 
                       }  | 
  |  160 |    0 |                        if ((matchingResultsCount == null) || (matchingResultsCount.intValue() <= searchResultsLimit.intValue())) { | 
  |  161 |    0 |                                matchingResultsCount = new Long(0);  | 
  |  162 |     | 
                       }  | 
  |  163 |    0 |                        searchResults = getPersistenceBrokerTemplate().getCollectionByQuery(QueryFactory.newQuery(businessObjectClass, criteria));  | 
  |  164 |     | 
                         | 
  |  165 |    0 |                        List bos = new ArrayList();  | 
  |  166 |    0 |                        bos.addAll(searchResults);  | 
  |  167 |    0 |                        searchResults = bos;  | 
  |  168 |     | 
               }  | 
  |  169 |    0 |                catch (OjbOperationException e) { | 
  |  170 |    0 |                        throw new RuntimeException("LookupDaoOjb encountered exception during executeSearch", e); | 
  |  171 |     | 
               }  | 
  |  172 |    0 |                catch (DataIntegrityViolationException e) { | 
  |  173 |    0 |                        throw new RuntimeException("LookupDaoOjb encountered exception during executeSearch", e); | 
  |  174 |    0 |                }  | 
  |  175 |    0 |                return new CollectionIncomplete(searchResults, matchingResultsCount);  | 
  |  176 |     | 
       }  | 
  |  177 |     | 
     | 
  |  178 |     | 
       public boolean createCriteria(Object example, String searchValue, String propertyName, Object criteria) { | 
  |  179 |    0 |                return createCriteria( example, searchValue, propertyName, false, false, criteria );  | 
  |  180 |     | 
       }  | 
  |  181 |     | 
         | 
  |  182 |     | 
       public boolean createCriteria(Object example, String searchValue, String propertyName, boolean caseInsensitive, boolean treatWildcardsAndOperatorsAsLiteral, Object criteria) { | 
  |  183 |    0 |                return createCriteria( example, searchValue, propertyName, false, false, criteria, null );  | 
  |  184 |     | 
       }  | 
  |  185 |     | 
     | 
  |  186 |     | 
       public boolean createCriteria(Object example, String searchValue, String propertyName, boolean caseInsensitive, boolean treatWildcardsAndOperatorsAsLiteral, Object criteria, Map searchValues) { | 
  |  187 |     | 
             | 
  |  188 |    0 |            if (!(criteria instanceof Criteria) || StringUtils.isBlank(searchValue) || !ObjectUtils.isWriteable(example, propertyName, persistenceStructureService)) { | 
  |  189 |    0 |                return false;  | 
  |  190 |     | 
           }  | 
  |  191 |     | 
     | 
  |  192 |     | 
             | 
  |  193 |    0 |            Class propertyType = ObjectUtils.getPropertyType(example, propertyName, persistenceStructureService);  | 
  |  194 |    0 |            if (propertyType == null) { | 
  |  195 |    0 |                return false;  | 
  |  196 |     | 
           }  | 
  |  197 |     | 
     | 
  |  198 |     | 
                     | 
  |  199 |    0 |                    if (example instanceof InactivateableFromTo) { | 
  |  200 |    0 |                            if (KNSPropertyConstants.ACTIVE.equals(propertyName)) { | 
  |  201 |    0 |                                    addInactivateableFromToActiveCriteria(example, searchValue, (Criteria) criteria, searchValues);  | 
  |  202 |    0 |                            } else if (KNSPropertyConstants.CURRENT.equals(propertyName)) { | 
  |  203 |    0 |                                    addInactivateableFromToCurrentCriteria(example, searchValue, (Criteria) criteria, searchValues);  | 
  |  204 |    0 |                            } else if (!KNSPropertyConstants.ACTIVE_AS_OF_DATE.equals(propertyName)) { | 
  |  205 |    0 |                                    addCriteria(propertyName, searchValue, propertyType, caseInsensitive,  | 
  |  206 |     | 
                                                   treatWildcardsAndOperatorsAsLiteral, (Criteria) criteria);  | 
  |  207 |     | 
                           }  | 
  |  208 |     | 
                   } else { | 
  |  209 |    0 |                            addCriteria(propertyName, searchValue, propertyType, caseInsensitive, treatWildcardsAndOperatorsAsLiteral,  | 
  |  210 |     | 
                                           (Criteria) criteria);  | 
  |  211 |     | 
                   }  | 
  |  212 |     | 
             | 
  |  213 |    0 |            return true;  | 
  |  214 |     | 
       }  | 
  |  215 |     | 
     | 
  |  216 |     | 
         | 
  |  217 |     | 
     | 
  |  218 |     | 
     | 
  |  219 |     | 
       public Long findCountByMap(Object example, Map formProps) { | 
  |  220 |    0 |            Criteria criteria = new Criteria();  | 
  |  221 |     | 
             | 
  |  222 |    0 |            Iterator propsIter = formProps.keySet().iterator();  | 
  |  223 |    0 |            while (propsIter.hasNext()) { | 
  |  224 |    0 |                String propertyName = (String) propsIter.next();  | 
  |  225 |    0 |                String searchValue = (String) formProps.get(propertyName);  | 
  |  226 |     | 
     | 
  |  227 |     | 
                 | 
  |  228 |    0 |                if (StringUtils.isBlank(searchValue) || !(PropertyUtils.isWriteable(example, propertyName))) { | 
  |  229 |    0 |                    continue;  | 
  |  230 |     | 
               }  | 
  |  231 |     | 
     | 
  |  232 |     | 
                 | 
  |  233 |    0 |                Class propertyType = ObjectUtils.getPropertyType(example, propertyName, persistenceStructureService);  | 
  |  234 |    0 |                if (propertyType == null) { | 
  |  235 |    0 |                    continue;  | 
  |  236 |     | 
               }  | 
  |  237 |    0 |                    Boolean caseInsensitive = Boolean.TRUE;  | 
  |  238 |    0 |                    if ( KNSServiceLocatorWeb.getDataDictionaryService().isAttributeDefined( example.getClass(), propertyName )) { | 
  |  239 |    0 |                            caseInsensitive = !KNSServiceLocatorWeb.getDataDictionaryService().getAttributeForceUppercase( example.getClass(), propertyName );  | 
  |  240 |     | 
                   }  | 
  |  241 |    0 |                    if ( caseInsensitive == null ) { caseInsensitive = Boolean.TRUE; } | 
  |  242 |     | 
     | 
  |  243 |    0 |                    boolean treatWildcardsAndOperatorsAsLiteral = KNSServiceLocatorWeb  | 
  |  244 |     | 
                                           .getBusinessObjectDictionaryService().isLookupFieldTreatWildcardsAndOperatorsAsLiteral(example.getClass(), propertyName);  | 
  |  245 |     | 
                     | 
  |  246 |     | 
                 | 
  |  247 |    0 |                addCriteria(propertyName, searchValue, propertyType, caseInsensitive, treatWildcardsAndOperatorsAsLiteral, criteria);  | 
  |  248 |    0 |            }  | 
  |  249 |     | 
     | 
  |  250 |     | 
             | 
  |  251 |    0 |            Query query = QueryFactory.newQuery(example.getClass(), criteria);  | 
  |  252 |     | 
     | 
  |  253 |    0 |            return new Long(getPersistenceBrokerTemplate().getCount(query));  | 
  |  254 |     | 
       }  | 
  |  255 |     | 
     | 
  |  256 |     | 
         | 
  |  257 |     | 
     | 
  |  258 |     | 
     | 
  |  259 |     | 
       public Object findObjectByMap(Object example, Map formProps) { | 
  |  260 |    0 |                if ( persistenceStructureService.isPersistable(example.getClass())) { | 
  |  261 |    0 |                        Criteria criteria = new Criteria();  | 
  |  262 |     | 
             | 
  |  263 |     | 
                         | 
  |  264 |    0 |                        Iterator propsIter = formProps.keySet().iterator();  | 
  |  265 |    0 |                        while (propsIter.hasNext()) { | 
  |  266 |    0 |                                String propertyName = (String) propsIter.next();  | 
  |  267 |    0 |                                String searchValue = "";  | 
  |  268 |    0 |                                if (formProps.get(propertyName) != null) { | 
  |  269 |    0 |                                        searchValue = (formProps.get(propertyName)).toString();  | 
  |  270 |     | 
                               }  | 
  |  271 |     | 
             | 
  |  272 |    0 |                                if (StringUtils.isNotBlank(searchValue) & PropertyUtils.isWriteable(example, propertyName)) { | 
  |  273 |    0 |                                        Class propertyType = ObjectUtils.getPropertyType(example, propertyName, persistenceStructureService);  | 
  |  274 |    0 |                                        if (TypeUtils.isIntegralClass(propertyType) || TypeUtils.isDecimalClass(propertyType) ) { | 
  |  275 |    0 |                                                criteria.addEqualTo(propertyName, cleanNumeric(searchValue));  | 
  |  276 |    0 |                                        } else if (TypeUtils.isTemporalClass(propertyType)) { | 
  |  277 |    0 |                                                criteria.addEqualTo(propertyName, parseDate( ObjectUtils.clean(searchValue) ) );  | 
  |  278 |     | 
                                       } else { | 
  |  279 |    0 |                                                criteria.addEqualTo(propertyName, searchValue);  | 
  |  280 |     | 
                                       }  | 
  |  281 |     | 
                               }  | 
  |  282 |    0 |                        }  | 
  |  283 |     | 
             | 
  |  284 |     | 
                         | 
  |  285 |    0 |                        Query query = QueryFactory.newQuery(example.getClass(), criteria);  | 
  |  286 |    0 |                        return getPersistenceBrokerTemplate().getObjectByQuery(query);  | 
  |  287 |     | 
               }  | 
  |  288 |    0 |                return null;  | 
  |  289 |     | 
       }  | 
  |  290 |     | 
     | 
  |  291 |     | 
     | 
  |  292 |     | 
         | 
  |  293 |     | 
     | 
  |  294 |     | 
     | 
  |  295 |     | 
       private void addCriteria(String propertyName, String propertyValue, Class propertyType, boolean caseInsensitive, boolean treatWildcardsAndOperatorsAsLiteral, Criteria criteria) { | 
  |  296 |    0 |            if (!treatWildcardsAndOperatorsAsLiteral && StringUtils.contains(propertyValue, KNSConstants.OR_LOGICAL_OPERATOR)) { | 
  |  297 |    0 |                addOrCriteria(propertyName, propertyValue, propertyType, caseInsensitive, criteria);  | 
  |  298 |    0 |                return;  | 
  |  299 |     | 
           }  | 
  |  300 |     | 
     | 
  |  301 |    0 |            if (!treatWildcardsAndOperatorsAsLiteral && StringUtils.contains(propertyValue, KNSConstants.AND_LOGICAL_OPERATOR)) { | 
  |  302 |    0 |                addAndCriteria(propertyName, propertyValue, propertyType, caseInsensitive, criteria);  | 
  |  303 |    0 |                return;  | 
  |  304 |     | 
           }  | 
  |  305 |     | 
     | 
  |  306 |    0 |            if (StringUtils.containsIgnoreCase(propertyValue, KNSConstants.NULL_OPERATOR)) { | 
  |  307 |    0 |                    if (StringUtils.contains(propertyValue, KNSConstants.NOT_LOGICAL_OPERATOR)) { | 
  |  308 |    0 |                            criteria.addColumnNotNull(propertyName);  | 
  |  309 |     | 
                   }  | 
  |  310 |     | 
                   else { | 
  |  311 |    0 |                            criteria.addColumnIsNull(propertyName);  | 
  |  312 |     | 
                   }  | 
  |  313 |     | 
           }  | 
  |  314 |    0 |            else if (TypeUtils.isStringClass(propertyType)) { | 
  |  315 |     | 
                     | 
  |  316 |    0 |                    if ( caseInsensitive ) { | 
  |  317 |    0 |                            propertyName = getDbPlatform().getUpperCaseFunction() + "(" + propertyName + ")"; | 
  |  318 |    0 |                            propertyValue = propertyValue.toUpperCase();  | 
  |  319 |     | 
                   }  | 
  |  320 |    0 |                if (!treatWildcardsAndOperatorsAsLiteral && StringUtils.contains(propertyValue, KNSConstants.NOT_LOGICAL_OPERATOR)) { | 
  |  321 |    0 |                    addNotCriteria(propertyName, propertyValue, propertyType, caseInsensitive, criteria);  | 
  |  322 |    0 |                } else if (  | 
  |  323 |     | 
                               !treatWildcardsAndOperatorsAsLiteral && propertyValue != null && (  | 
  |  324 |     | 
                                               StringUtils.contains(propertyValue, KNSConstants.BETWEEN_OPERATOR)   | 
  |  325 |     | 
                                               || propertyValue.startsWith(">") | 
  |  326 |     | 
                                               || propertyValue.startsWith("<") ) ) { | 
  |  327 |    0 |                    addStringRangeCriteria(propertyName, propertyValue, criteria);  | 
  |  328 |     | 
               } else { | 
  |  329 |    0 |                        if (treatWildcardsAndOperatorsAsLiteral) { | 
  |  330 |    0 |                                propertyValue = StringUtils.replace(propertyValue, "*", "\\*");  | 
  |  331 |     | 
                       }  | 
  |  332 |    0 |                        criteria.addLike(propertyName, propertyValue);  | 
  |  333 |     | 
               }  | 
  |  334 |    0 |            } else if (TypeUtils.isIntegralClass(propertyType) || TypeUtils.isDecimalClass(propertyType) ) { | 
  |  335 |    0 |                addNumericRangeCriteria(propertyName, propertyValue, treatWildcardsAndOperatorsAsLiteral, criteria);  | 
  |  336 |    0 |            } else if (TypeUtils.isTemporalClass(propertyType)) { | 
  |  337 |    0 |                addDateRangeCriteria(propertyName, propertyValue, treatWildcardsAndOperatorsAsLiteral, criteria);  | 
  |  338 |    0 |            } else if (TypeUtils.isBooleanClass(propertyType)) { | 
  |  339 |    0 |                criteria.addEqualTo(propertyName, ObjectUtils.clean(propertyValue));  | 
  |  340 |     | 
           } else { | 
  |  341 |    0 |                LOG.error("not adding criterion for: " + propertyName + "," + propertyType + "," + propertyValue); | 
  |  342 |     | 
           }  | 
  |  343 |    0 |        }  | 
  |  344 |     | 
         | 
  |  345 |     | 
         | 
  |  346 |     | 
     | 
  |  347 |     | 
     | 
  |  348 |     | 
     | 
  |  349 |     | 
     | 
  |  350 |     | 
     | 
  |  351 |     | 
     | 
  |  352 |     | 
     | 
  |  353 |     | 
       protected void addInactivateableFromToActiveCriteria(Object example, String activeSearchValue, Criteria criteria, Map searchValues) { | 
  |  354 |    0 |                    Timestamp activeTimestamp = LookupUtils.getActiveDateTimestampForCriteria(searchValues);  | 
  |  355 |     | 
                     | 
  |  356 |    0 |                String activeBooleanStr = (String) (new OjbCharBooleanConversion()).javaToSql(activeSearchValue);  | 
  |  357 |    0 |                if (OjbCharBooleanConversion.DATABASE_BOOLEAN_TRUE_STRING_REPRESENTATION.equals(activeBooleanStr)) { | 
  |  358 |     | 
                         | 
  |  359 |    0 |                        Criteria criteriaBeginDate = new Criteria();  | 
  |  360 |    0 |                        criteriaBeginDate.addLessOrEqualThan(KNSPropertyConstants.ACTIVE_FROM_DATE, activeTimestamp);  | 
  |  361 |     | 
                         | 
  |  362 |    0 |                        Criteria criteriaBeginDateNull = new Criteria();  | 
  |  363 |    0 |                        criteriaBeginDateNull.addIsNull(KNSPropertyConstants.ACTIVE_FROM_DATE);  | 
  |  364 |    0 |                        criteriaBeginDate.addOrCriteria(criteriaBeginDateNull);  | 
  |  365 |     | 
                         | 
  |  366 |    0 |                        criteria.addAndCriteria(criteriaBeginDate);  | 
  |  367 |     | 
                         | 
  |  368 |    0 |                        Criteria criteriaEndDate = new Criteria();  | 
  |  369 |    0 |                        criteriaEndDate.addGreaterThan(KNSPropertyConstants.ACTIVE_TO_DATE, activeTimestamp);  | 
  |  370 |     | 
                 | 
  |  371 |    0 |                        Criteria criteriaEndDateNull = new Criteria();  | 
  |  372 |    0 |                        criteriaEndDateNull.addIsNull(KNSPropertyConstants.ACTIVE_TO_DATE);  | 
  |  373 |    0 |                        criteriaEndDate.addOrCriteria(criteriaEndDateNull);  | 
  |  374 |     | 
                         | 
  |  375 |    0 |                        criteria.addAndCriteria(criteriaEndDate);  | 
  |  376 |    0 |                }  | 
  |  377 |    0 |                else if (OjbCharBooleanConversion.DATABASE_BOOLEAN_FALSE_STRING_REPRESENTATION.equals(activeBooleanStr)) { | 
  |  378 |     | 
                         | 
  |  379 |    0 |                        Criteria criteriaNonActive = new Criteria();  | 
  |  380 |    0 |                        criteriaNonActive.addGreaterThan(KNSPropertyConstants.ACTIVE_FROM_DATE, activeTimestamp);  | 
  |  381 |     | 
                         | 
  |  382 |    0 |                        Criteria criteriaEndDate = new Criteria();  | 
  |  383 |    0 |                        criteriaEndDate.addLessOrEqualThan(KNSPropertyConstants.ACTIVE_TO_DATE, activeTimestamp);  | 
  |  384 |    0 |                        criteriaNonActive.addOrCriteria(criteriaEndDate);  | 
  |  385 |     | 
                         | 
  |  386 |    0 |                        criteria.addAndCriteria(criteriaNonActive);  | 
  |  387 |     | 
               }  | 
  |  388 |    0 |        }  | 
  |  389 |     | 
         | 
  |  390 |     | 
         | 
  |  391 |     | 
     | 
  |  392 |     | 
     | 
  |  393 |     | 
     | 
  |  394 |     | 
     | 
  |  395 |     | 
     | 
  |  396 |     | 
     | 
  |  397 |     | 
           protected void addInactivateableFromToCurrentCriteria(Object example, String currentSearchValue, Criteria criteria, Map searchValues) { | 
  |  398 |    0 |                    Criteria maxBeginDateCriteria = new Criteria();  | 
  |  399 |     | 
                     | 
  |  400 |    0 |                    Timestamp activeTimestamp = LookupUtils.getActiveDateTimestampForCriteria(searchValues);  | 
  |  401 |     | 
                     | 
  |  402 |    0 |                    maxBeginDateCriteria.addLessOrEqualThan(KNSPropertyConstants.ACTIVE_FROM_DATE, activeTimestamp);  | 
  |  403 |     | 
     | 
  |  404 |    0 |                    List<String> groupByFieldList = businessObjectDictionaryService.getGroupByAttributesForEffectiveDating(example  | 
  |  405 |     | 
                                   .getClass());  | 
  |  406 |    0 |                    if (groupByFieldList == null) { | 
  |  407 |    0 |                            return;  | 
  |  408 |     | 
                   }  | 
  |  409 |     | 
     | 
  |  410 |     | 
                     | 
  |  411 |    0 |                    String[] groupBy = new String[groupByFieldList.size()];  | 
  |  412 |    0 |                    for (int i = 0; i < groupByFieldList.size(); i++) { | 
  |  413 |    0 |                            String groupByField = groupByFieldList.get(i);  | 
  |  414 |    0 |                            groupBy[i] = groupByField;  | 
  |  415 |     | 
     | 
  |  416 |    0 |                            maxBeginDateCriteria.addEqualToField(groupByField, Criteria.PARENT_QUERY_PREFIX + groupByField);  | 
  |  417 |     | 
                   }  | 
  |  418 |     | 
     | 
  |  419 |    0 |                    String[] columns = new String[1];  | 
  |  420 |    0 |                    columns[0] = "max(" + KNSPropertyConstants.ACTIVE_FROM_DATE + ")"; | 
  |  421 |     | 
     | 
  |  422 |    0 |                    QueryByCriteria query = QueryFactory.newReportQuery(example.getClass(), columns, maxBeginDateCriteria, true);  | 
  |  423 |    0 |                    query.addGroupBy(groupBy);  | 
  |  424 |     | 
     | 
  |  425 |    0 |                    String currentBooleanStr = (String) (new OjbCharBooleanConversion()).javaToSql(currentSearchValue);  | 
  |  426 |    0 |                    if (OjbCharBooleanConversion.DATABASE_BOOLEAN_TRUE_STRING_REPRESENTATION.equals(currentBooleanStr)) { | 
  |  427 |    0 |                            criteria.addIn(KNSPropertyConstants.ACTIVE_FROM_DATE, query);  | 
  |  428 |    0 |                    } else if (OjbCharBooleanConversion.DATABASE_BOOLEAN_FALSE_STRING_REPRESENTATION.equals(currentBooleanStr)) { | 
  |  429 |    0 |                            criteria.addNotIn(KNSPropertyConstants.ACTIVE_FROM_DATE, query);  | 
  |  430 |     | 
                   }  | 
  |  431 |    0 |            }  | 
  |  432 |     | 
     | 
  |  433 |     | 
         | 
  |  434 |     | 
     | 
  |  435 |     | 
     | 
  |  436 |     | 
     | 
  |  437 |     | 
     | 
  |  438 |     | 
     | 
  |  439 |     | 
       private void addOrCriteria(String propertyName, String propertyValue, Class propertyType, boolean caseInsensitive, Criteria criteria) { | 
  |  440 |    0 |            addLogicalOperatorCriteria(propertyName, propertyValue, propertyType, caseInsensitive, criteria, KNSConstants.OR_LOGICAL_OPERATOR);  | 
  |  441 |    0 |        }  | 
  |  442 |     | 
            | 
  |  443 |     | 
         | 
  |  444 |     | 
     | 
  |  445 |     | 
     | 
  |  446 |     | 
     | 
  |  447 |     | 
     | 
  |  448 |     | 
     | 
  |  449 |     | 
       private void addAndCriteria(String propertyName, String propertyValue, Class propertyType, boolean caseInsensitive, Criteria criteria) { | 
  |  450 |    0 |            addLogicalOperatorCriteria(propertyName, propertyValue, propertyType, caseInsensitive, criteria, KNSConstants.AND_LOGICAL_OPERATOR);  | 
  |  451 |    0 |        }  | 
  |  452 |     | 
     | 
  |  453 |     | 
       private void addNotCriteria(String propertyName, String propertyValue, Class propertyType, boolean caseInsensitive, Criteria criteria) { | 
  |  454 |     | 
     | 
  |  455 |    0 |            String[] splitPropVal = StringUtils.split(propertyValue, KNSConstants.NOT_LOGICAL_OPERATOR);  | 
  |  456 |     | 
     | 
  |  457 |    0 |            int strLength = splitPropVal.length;  | 
  |  458 |     | 
             | 
  |  459 |    0 |            if (strLength > 1) { | 
  |  460 |    0 |                String expandedNot = "!" + StringUtils.join(splitPropVal, KNSConstants.AND_LOGICAL_OPERATOR + KNSConstants.NOT_LOGICAL_OPERATOR);  | 
  |  461 |     | 
                 | 
  |  462 |    0 |                addCriteria(propertyName, expandedNot, propertyType, caseInsensitive, false, criteria);  | 
  |  463 |    0 |            }  | 
  |  464 |     | 
           else { | 
  |  465 |     | 
                 | 
  |  466 |    0 |                criteria.addNotLike(propertyName, splitPropVal[0]);  | 
  |  467 |     | 
           }  | 
  |  468 |    0 |        }  | 
  |  469 |     | 
     | 
  |  470 |     | 
         | 
  |  471 |     | 
     | 
  |  472 |     | 
     | 
  |  473 |     | 
     | 
  |  474 |     | 
       private void addLogicalOperatorCriteria(String propertyName, String propertyValue, Class propertyType, boolean caseInsensitive, Criteria criteria, String splitValue) { | 
  |  475 |    0 |            String[] splitPropVal = StringUtils.split(propertyValue, splitValue);  | 
  |  476 |     | 
     | 
  |  477 |    0 |            Criteria subCriteria = new Criteria();  | 
  |  478 |    0 |            for (int i = 0; i < splitPropVal.length; i++) { | 
  |  479 |    0 |                    Criteria predicate = new Criteria();  | 
  |  480 |     | 
     | 
  |  481 |    0 |                addCriteria(propertyName, splitPropVal[i], propertyType, caseInsensitive, false, predicate);  | 
  |  482 |    0 |                if (splitValue == KNSConstants.OR_LOGICAL_OPERATOR) { | 
  |  483 |    0 |                        subCriteria.addOrCriteria(predicate);  | 
  |  484 |     | 
               }  | 
  |  485 |    0 |                if (splitValue == KNSConstants.AND_LOGICAL_OPERATOR) { | 
  |  486 |    0 |                        subCriteria.addAndCriteria(predicate);  | 
  |  487 |     | 
               }  | 
  |  488 |     | 
           }  | 
  |  489 |     | 
     | 
  |  490 |    0 |            criteria.addAndCriteria(subCriteria);  | 
  |  491 |    0 |        }  | 
  |  492 |     | 
         | 
  |  493 |     | 
       private java.sql.Date parseDate(String dateString) { | 
  |  494 |    0 |                    dateString = dateString.trim();  | 
  |  495 |     | 
                   try { | 
  |  496 |    0 |                            return dateTimeService.convertToSqlDate(dateString);  | 
  |  497 |    0 |                    } catch (ParseException ex) { | 
  |  498 |    0 |                            return null;  | 
  |  499 |     | 
                   }  | 
  |  500 |     | 
           }  | 
  |  501 |     | 
     | 
  |  502 |     | 
         | 
  |  503 |     | 
     | 
  |  504 |     | 
     | 
  |  505 |     | 
       private void addDateRangeCriteria(String propertyName, String propertyValue, boolean treatWildcardsAndOperatorsAsLiteral, Criteria criteria) { | 
  |  506 |     | 
     | 
  |  507 |    0 |            if (StringUtils.contains(propertyValue, KNSConstants.BETWEEN_OPERATOR)) { | 
  |  508 |    0 |                    if (treatWildcardsAndOperatorsAsLiteral)  | 
  |  509 |    0 |                            throw new RuntimeException("Wildcards and operators are not allowed on this date field: " + propertyName); | 
  |  510 |    0 |                String[] rangeValues = StringUtils.split(propertyValue, KNSConstants.BETWEEN_OPERATOR);  | 
  |  511 |    0 |                criteria.addBetween(propertyName, parseDate( ObjectUtils.clean(rangeValues[0] ) ), parseDate( ObjectUtils.clean(rangeValues[1] ) ) );  | 
  |  512 |    0 |            } else if (propertyValue.startsWith(">=")) { | 
  |  513 |    0 |                    if (treatWildcardsAndOperatorsAsLiteral)  | 
  |  514 |    0 |                            throw new RuntimeException("Wildcards and operators are not allowed on this date field: " + propertyName); | 
  |  515 |    0 |                criteria.addGreaterOrEqualThan(propertyName, parseDate( ObjectUtils.clean(propertyValue) ) );  | 
  |  516 |    0 |            } else if (propertyValue.startsWith("<=")) { | 
  |  517 |    0 |                    if (treatWildcardsAndOperatorsAsLiteral)  | 
  |  518 |    0 |                            throw new RuntimeException("Wildcards and operators are not allowed on this date field: " + propertyName); | 
  |  519 |    0 |                criteria.addLessOrEqualThan(propertyName, parseDate( ObjectUtils.clean(propertyValue) ) );  | 
  |  520 |    0 |            } else if (propertyValue.startsWith(">")) { | 
  |  521 |    0 |                    if (treatWildcardsAndOperatorsAsLiteral)  | 
  |  522 |    0 |                            throw new RuntimeException("Wildcards and operators are not allowed on this date field: " + propertyName); | 
  |  523 |    0 |                criteria.addGreaterThan(propertyName, parseDate( ObjectUtils.clean(propertyValue) ) );  | 
  |  524 |    0 |            } else if (propertyValue.startsWith("<")) { | 
  |  525 |    0 |                    if (treatWildcardsAndOperatorsAsLiteral)  | 
  |  526 |    0 |                            throw new RuntimeException("Wildcards and operators are not allowed on this date field: " + propertyName); | 
  |  527 |    0 |                criteria.addLessThan(propertyName, parseDate( ObjectUtils.clean(propertyValue) ) );  | 
  |  528 |     | 
           } else { | 
  |  529 |    0 |                criteria.addEqualTo(propertyName, parseDate( ObjectUtils.clean(propertyValue) ) );  | 
  |  530 |     | 
           }  | 
  |  531 |    0 |        }  | 
  |  532 |     | 
     | 
  |  533 |     | 
       private BigDecimal cleanNumeric( String value ) { | 
  |  534 |    0 |            String cleanedValue = value.replaceAll( "[^-0-9.]", "" );  | 
  |  535 |     | 
             | 
  |  536 |    0 |            if ( cleanedValue.lastIndexOf( '-' ) > 0 ) { | 
  |  537 |    0 |                if ( cleanedValue.charAt( 0 ) == '-' ) { | 
  |  538 |    0 |                    cleanedValue = "-" + cleanedValue.replaceAll( "-", "" );  | 
  |  539 |     | 
               } else { | 
  |  540 |    0 |                    cleanedValue = cleanedValue.replaceAll( "-", "" );  | 
  |  541 |     | 
               }  | 
  |  542 |     | 
           }  | 
  |  543 |     | 
             | 
  |  544 |    0 |            int decimalLoc = cleanedValue.lastIndexOf( '.' );  | 
  |  545 |    0 |            if ( cleanedValue.indexOf( '.' ) != decimalLoc ) { | 
  |  546 |    0 |                cleanedValue = cleanedValue.substring( 0, decimalLoc ).replaceAll( "\\.", "" ) + cleanedValue.substring( decimalLoc );  | 
  |  547 |     | 
           }  | 
  |  548 |     | 
           try { | 
  |  549 |    0 |                return new BigDecimal( cleanedValue );  | 
  |  550 |    0 |            } catch ( NumberFormatException ex ) { | 
  |  551 |    0 |                GlobalVariables.getMessageMap().putError(KNSConstants.DOCUMENT_ERRORS, RiceKeyConstants.ERROR_CUSTOM, new String[] { "Invalid Numeric Input: " + value }); | 
  |  552 |    0 |                return null;  | 
  |  553 |     | 
           }  | 
  |  554 |     | 
       }  | 
  |  555 |     | 
     | 
  |  556 |     | 
         | 
  |  557 |     | 
     | 
  |  558 |     | 
     | 
  |  559 |     | 
       private void addNumericRangeCriteria(String propertyName, String propertyValue, boolean treatWildcardsAndOperatorsAsLiteral, Criteria criteria) { | 
  |  560 |     | 
     | 
  |  561 |    0 |            if (StringUtils.contains(propertyValue, KNSConstants.BETWEEN_OPERATOR)) { | 
  |  562 |    0 |                    if (treatWildcardsAndOperatorsAsLiteral)  | 
  |  563 |    0 |                            throw new RuntimeException("Cannot use wildcards and operators on numeric field " + propertyName); | 
  |  564 |    0 |                String[] rangeValues = StringUtils.split(propertyValue, KNSConstants.BETWEEN_OPERATOR);  | 
  |  565 |    0 |                criteria.addBetween(propertyName, cleanNumeric( rangeValues[0] ), cleanNumeric( rangeValues[1] ));  | 
  |  566 |    0 |            } else if (propertyValue.startsWith(">=")) { | 
  |  567 |    0 |                    if (treatWildcardsAndOperatorsAsLiteral)  | 
  |  568 |    0 |                            throw new RuntimeException("Cannot use wildcards and operators on numeric field " + propertyName); | 
  |  569 |    0 |                criteria.addGreaterOrEqualThan(propertyName, cleanNumeric(propertyValue));  | 
  |  570 |    0 |            } else if (propertyValue.startsWith("<=")) { | 
  |  571 |    0 |                    if (treatWildcardsAndOperatorsAsLiteral)  | 
  |  572 |    0 |                            throw new RuntimeException("Cannot use wildcards and operators on numeric field " + propertyName); | 
  |  573 |    0 |                criteria.addLessOrEqualThan(propertyName, cleanNumeric(propertyValue));  | 
  |  574 |    0 |            } else if (propertyValue.startsWith(">")) { | 
  |  575 |    0 |                    if (treatWildcardsAndOperatorsAsLiteral)  | 
  |  576 |    0 |                            throw new RuntimeException("Cannot use wildcards and operators on numeric field " + propertyName); | 
  |  577 |    0 |                criteria.addGreaterThan(propertyName, cleanNumeric( propertyValue ) );  | 
  |  578 |    0 |            } else if (propertyValue.startsWith("<")) { | 
  |  579 |    0 |                    if (treatWildcardsAndOperatorsAsLiteral)  | 
  |  580 |    0 |                            throw new RuntimeException("Cannot use wildcards and operators on numeric field " + propertyName); | 
  |  581 |    0 |                criteria.addLessThan(propertyName, cleanNumeric(propertyValue));  | 
  |  582 |     | 
           } else { | 
  |  583 |    0 |                criteria.addEqualTo(propertyName, cleanNumeric(propertyValue));  | 
  |  584 |     | 
           }  | 
  |  585 |    0 |        }  | 
  |  586 |     | 
     | 
  |  587 |     | 
         | 
  |  588 |     | 
     | 
  |  589 |     | 
     | 
  |  590 |     | 
       private void addStringRangeCriteria(String propertyName, String propertyValue, Criteria criteria) { | 
  |  591 |     | 
     | 
  |  592 |    0 |            if (StringUtils.contains(propertyValue, KNSConstants.BETWEEN_OPERATOR)) { | 
  |  593 |    0 |                String[] rangeValues = StringUtils.split(propertyValue, KNSConstants.BETWEEN_OPERATOR);  | 
  |  594 |    0 |                criteria.addBetween(propertyName, rangeValues[0], rangeValues[1]);  | 
  |  595 |    0 |            } else if (propertyValue.startsWith(">=")) { | 
  |  596 |    0 |                criteria.addGreaterOrEqualThan(propertyName, ObjectUtils.clean(propertyValue));  | 
  |  597 |    0 |            } else if (propertyValue.startsWith("<=")) { | 
  |  598 |    0 |                criteria.addLessOrEqualThan(propertyName, ObjectUtils.clean(propertyValue));  | 
  |  599 |    0 |            } else if (propertyValue.startsWith(">")) { | 
  |  600 |    0 |                criteria.addGreaterThan(propertyName, ObjectUtils.clean(propertyValue));  | 
  |  601 |    0 |            } else if (propertyValue.startsWith("<")) { | 
  |  602 |    0 |                criteria.addLessThan(propertyName, ObjectUtils.clean(propertyValue));  | 
  |  603 |     | 
           } else { | 
  |  604 |    0 |                    criteria.addEqualTo(propertyName, ObjectUtils.clean(propertyValue));  | 
  |  605 |     | 
           }  | 
  |  606 |    0 |        }  | 
  |  607 |     | 
     | 
  |  608 |     | 
           public void setDateTimeService(DateTimeService dateTimeService) { | 
  |  609 |    0 |                    this.dateTimeService = dateTimeService;  | 
  |  610 |    0 |            }  | 
  |  611 |     | 
     | 
  |  612 |     | 
           public void setBusinessObjectDictionaryService(BusinessObjectDictionaryService businessObjectDictionaryService) { | 
  |  613 |    0 |                    this.businessObjectDictionaryService = businessObjectDictionaryService;  | 
  |  614 |    0 |            }  | 
  |  615 |     | 
             | 
  |  616 |     | 
       public void setPersistenceStructureService(PersistenceStructureService persistenceStructureService) { | 
  |  617 |    0 |            this.persistenceStructureService = persistenceStructureService;  | 
  |  618 |    0 |        }  | 
  |  619 |     | 
     | 
  |  620 |     | 
   }  |