litesoft
@ 939
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 |
// This Source Code is in the Public Domain per: http://litesoft.org/License.txt package org.litesoft.orsup.base.filters; import java.util.*; import org.litesoft.commonfoundation.stringmatching.*; import org.litesoft.core.simpletypes.nonpublic.*; import org.litesoft.db.*; import org.litesoft.orsup.base.*; import org.litesoft.orsup.selection.*; import org.litesoft.orsup.selection.nonpublic.*; public class POFWCfactory { public static POFilteringWhereClause create( MetaDataForPO pMD, WhereClause pWhereClause ) { switch ( pWhereClause.getType() ) { case TRUE: return POFWC_TRUE.INSTANCE; case FALSE: return POFWC_FALSE.INSTANCE; case NOT: return create_NOT( pMD, (AbstractWhereClauseWrapper) pWhereClause ); case OR: return create_OR( pMD, (AbstractWhereClauseAssociativeList) pWhereClause ); case AND: return create_AND( pMD, (AbstractWhereClauseAssociativeList) pWhereClause ); case IS_NULL: return create_IS_NULL( pMD, (AbstractWhereClauseColumnAndTypeTo) pWhereClause ); case EQUALS: return create_EQUALS( pMD, (AbstractWhereClauseColumnAndValue) pWhereClause ); case LESSTHAN: return create_LESSTHAN( pMD, (AbstractWhereClauseColumnAndValue) pWhereClause ); case GREATERTHAN: return create_GREATERTHAN( pMD, (AbstractWhereClauseColumnAndValue) pWhereClause ); case BETWEEN: return create_BETWEEN( pMD, (AbstractWhereClauseColumnAndTwoValues) pWhereClause ); case IS_ANY_OF: return create_IS_ANY_OF( pMD, (AbstractWhereClauseColumnAnd_N_Values) pWhereClause ); case CONTAINS: return create_CONTAINS( pMD, (AbstractWhereClauseColumnAndLikeValue) pWhereClause ); case STARTS_WITH: return create_STARTS_WITH( pMD, (AbstractWhereClauseColumnAndLikeValue) pWhereClause ); case ENDS_WITH: return create_ENDS_WITH( pMD, (AbstractWhereClauseColumnAndLikeValue) pWhereClause ); case LIKE: return create_LIKE( pMD, (AbstractWhereClauseColumnAndLikeValues) pWhereClause ); case IS_IN: // Not Currently Supported default: // Extensions? return null; } } private static POFilteringWhereClause[] createChildren( MetaDataForPO pMD, AbstractWhereClauseAssociativeList pWhereClause ) { List<WhereClause> zClauses = pWhereClause.getWhereClauseList(); POFilteringWhereClause[] zFilters = new POFilteringWhereClause[zClauses.size()]; for ( int i = 0; i < zClauses.size(); i++ ) { if ( null == (zFilters[i] = create( pMD, zClauses.get( i ) )) ) { return null; } } return zFilters; } private static Object convertToColumnType( AttributeAccessorSCD pSCD, Object pValue ) { try { return (pSCD != null) ? pSCD.convertToAppropriateColumnType( pValue ) : null; } catch ( IllegalArgumentException verypossible ) { return null; } } private static Comparable convertToComparableColumnType( AttributeAccessorSCD pSCD, Object pValue ) { try { return (Comparable) convertToColumnType( pSCD, pValue ); } catch ( ClassCastException possible ) { return null; } } private static String convertToStringColumnType( AttributeAccessorSCD pSCD, Object pValue ) { try { return (String) convertToColumnType( pSCD, pValue ); } catch ( ClassCastException possible ) { return null; } } private static AttributeAccessorSCD getAASCD( MetaDataForPO pMD, AbstractWhereClauseColumnReference pWhereClause ) { AttributeAccessorSCD zSCD = pMD.getAccessorSCDoptional( pWhereClause.getColumnDefinition().getName() ); return ((zSCD != null) && zSCD.isPersisted()) ? zSCD : null; } private static AttributeAccessorSCD getComparableAASCD( MetaDataForPO pMD, AbstractWhereClauseColumnReference pWhereClause ) { AttributeAccessorSCD zSCD = getAASCD( pMD, pWhereClause ); return ((zSCD != null) && zSCD.isColumnTypeComparable()) ? zSCD : null; } private static AttributeAccessorSCD getStringAASCD( MetaDataForPO pMD, AbstractWhereClauseColumnReference pWhereClause ) { AttributeAccessorSCD zSCD = getAASCD( pMD, pWhereClause ); return ((zSCD != null) && zSCD.isColumnTypeComparable()) ? zSCD : null; } private static POFilteringWhereClause not( MetaDataForPO pMD, POFilteringWhereClause pFilter ) { return (pFilter == null) ? pFilter : new Filter_NOT( pMD, pFilter ); } private static POFilteringWhereClause notable( MetaDataForPO pMD, WhereClause pWhereClause, POFilteringWhereClause pFilter ) { if ( pWhereClause instanceof WhereClauseNotable ) { if ( ((WhereClauseNotable) pWhereClause).isNot() ) { return not( pMD, pFilter ); } } return pFilter; } private static POFilteringWhereClause create_NOT( MetaDataForPO pMD, AbstractWhereClauseWrapper pWhereClause ) { return not( pMD, create( pMD, pWhereClause.getWrappedWhereClause() ) ); } private static POFilteringWhereClause create_OR( MetaDataForPO pMD, AbstractWhereClauseAssociativeList pWhereClause ) { return new Filter_OR( pMD, createChildren( pMD, pWhereClause ) ); } private static POFilteringWhereClause create_AND( MetaDataForPO pMD, AbstractWhereClauseAssociativeList pWhereClause ) { return new Filter_AND( pMD, createChildren( pMD, pWhereClause ) ); } private static POFilteringWhereClause create_IS_NULL( MetaDataForPO pMD, AbstractWhereClauseColumnAndTypeTo pWhereClause ) { AttributeAccessorSCD zSCD = getAASCD( pMD, pWhereClause ); return notable( pMD, pWhereClause, (zSCD != null) ? new Filter_IS_NULL( pMD, zSCD ) : null ); } private static POFilteringWhereClause create_EQUALS( MetaDataForPO pMD, AbstractWhereClauseColumnAndValue pWhereClause ) { AttributeAccessorSCD zSCD = getAASCD( pMD, pWhereClause ); Object zValue = convertToColumnType( zSCD, pWhereClause.getValue() ); return notable( pMD, pWhereClause, (zValue != null) ? // new Filter_EQUALS( pMD, zSCD, zValue ) : // null ); } private static POFilteringWhereClause create_LESSTHAN( MetaDataForPO pMD, AbstractWhereClauseColumnAndValue pWhereClause ) { AttributeAccessorSCD zSCD = getComparableAASCD( pMD, pWhereClause ); Comparable zValue = convertToComparableColumnType( zSCD, pWhereClause.getValue() ); return notable( pMD, pWhereClause, (zValue != null) ? // new Filter_LESSTHAN( pMD, zSCD, zValue ) : // null ); } private static POFilteringWhereClause create_GREATERTHAN( MetaDataForPO pMD, AbstractWhereClauseColumnAndValue pWhereClause ) { AttributeAccessorSCD zSCD = getComparableAASCD( pMD, pWhereClause ); Comparable zValue = convertToComparableColumnType( zSCD, pWhereClause.getValue() ); return notable( pMD, pWhereClause, (zValue != null) ? // new Filter_GREATERTHAN( pMD, zSCD, zValue ) : // null ); } private static POFilteringWhereClause create_BETWEEN( MetaDataForPO pMD, AbstractWhereClauseColumnAndTwoValues pWhereClause ) { AttributeAccessorSCD zSCD = getComparableAASCD( pMD, pWhereClause ); Comparable zLeftValue = convertToComparableColumnType( zSCD, pWhereClause.getLeftValue() ); Comparable zRightValue = convertToComparableColumnType( zSCD, pWhereClause.getRightValue() ); return notable( pMD, pWhereClause, ((zLeftValue != null) && (zRightValue != null)) ? // new Filter_BETWEEN( pMD, zSCD, zLeftValue, zRightValue ) : // null ); } private static POFilteringWhereClause create_IS_ANY_OF( MetaDataForPO pMD, AbstractWhereClauseColumnAnd_N_Values pWhereClause ) { AttributeAccessorSCD zSCD = getAASCD( pMD, pWhereClause ); if ( zSCD != null ) { Object[] zWCValues = pWhereClause.getValues(); Object[] zValues = new Object[zWCValues.length]; for ( int i = 0; i < zWCValues.length; i++ ) { if ( null == (zValues[i] = convertToColumnType( zSCD, zWCValues[i] )) ) { return null; } } return notable( pMD, pWhereClause, new Filter_IS_ANY_OF( pMD, zSCD, zValues ) ); } return null; } private static POFilteringWhereClause create_CONTAINS( MetaDataForPO pMD, AbstractWhereClauseColumnAndLikeValue pWhereClause ) { AttributeAccessorSCD zSCD = getStringAASCD( pMD, pWhereClause ); String zValue = convertToStringColumnType( zSCD, pWhereClause.getValue() ); return notable( pMD, pWhereClause, (zValue != null) ? // new Filter_CONTAINS( pMD, zSCD, zValue ) : // null ); } private static POFilteringWhereClause create_STARTS_WITH( MetaDataForPO pMD, AbstractWhereClauseColumnAndLikeValue pWhereClause ) { AttributeAccessorSCD zSCD = getStringAASCD( pMD, pWhereClause ); String zValue = convertToStringColumnType( zSCD, pWhereClause.getValue() ); return notable( pMD, pWhereClause, (zValue != null) ? // new Filter_STARTS_WITH( pMD, zSCD, zValue ) : // null ); } private static POFilteringWhereClause create_ENDS_WITH( MetaDataForPO pMD, AbstractWhereClauseColumnAndLikeValue pWhereClause ) { AttributeAccessorSCD zSCD = getStringAASCD( pMD, pWhereClause ); String zValue = convertToStringColumnType( zSCD, pWhereClause.getValue() ); return notable( pMD, pWhereClause, (zValue != null) ? // new Filter_ENDS_WITH( pMD, zSCD, zValue ) : // null ); } private static POFilteringWhereClause create_LIKE( MetaDataForPO pMD, AbstractWhereClauseColumnAndLikeValues pWhereClause ) { AttributeAccessorSCD zSCD = getStringAASCD( pMD, pWhereClause ); if ( zSCD != null ) { String[] zValues = pWhereClause.getValues(); StringMatcher zStringMatcher = StringMatcherFactory.createEquals( zValues ); if ( zStringMatcher != null ) { return notable( pMD, pWhereClause, new Filter_LIKE( pMD, zSCD, zStringMatcher, zValues ) ); } } return null; } private static class Filter_NOT extends AbstractPOFWC_Wrapper { public Filter_NOT( MetaDataForPO pMD, POFilteringWhereClause pFilter ) { super( WhereClauseType.NOT, pMD, pFilter ); } @Override public boolean selects( PersistentObject pPO ) throws DBException { return !mFilter.selects( pPO ); } } private static class Filter_OR extends AbstractPOFWC_AssociativeList { public Filter_OR( MetaDataForPO pMD, POFilteringWhereClause[] pFilters ) { super( WhereClauseType.OR, pMD, pFilters ); } @Override public boolean selects( PersistentObject pPO ) throws DBException { for ( POFilteringWhereClause filter : mFilters ) { if ( filter.selects( pPO ) ) { return true; } } return false; } } private static class Filter_AND extends AbstractPOFWC_AssociativeList { public Filter_AND( MetaDataForPO pMD, POFilteringWhereClause[] pFilters ) { super( WhereClauseType.AND, pMD, pFilters ); } @Override public boolean selects( PersistentObject pPO ) throws DBException { for ( POFilteringWhereClause filter : mFilters ) { if ( !filter.selects( pPO ) ) { return false; } } return true; } } private static class Filter_IS_NULL extends AbstractPOFWC_ColumnAndTypeTo { public Filter_IS_NULL( MetaDataForPO pMD, AttributeAccessorSCD pColumnDefinition ) { super( WhereClauseType.IS_NULL, pMD, pColumnDefinition ); } @Override public boolean selects( PersistentObject pPO ) throws DBException { return getValueOnPO( pPO ) == null; } } private static class Filter_EQUALS extends AbstractPOFWC_ColumnAndValue { public Filter_EQUALS( MetaDataForPO pMD, AttributeAccessorSCD pColumnDefinition, Object pValue ) { super( WhereClauseType.EQUALS, pMD, pColumnDefinition, pValue ); } @Override public boolean selects( PersistentObject pPO ) throws DBException { return mValue.equals( getValueOnPO( pPO ) ); } } private static class Filter_LESSTHAN extends AbstractPOFWC_ColumnAndValue { public Filter_LESSTHAN( MetaDataForPO pMD, AttributeAccessorSCD pColumnDefinition, Comparable pValue ) { super( WhereClauseType.LESSTHAN, pMD, pColumnDefinition, pValue ); } @Override public boolean selects( PersistentObject pPO ) throws DBException { Comparable zPOValue = getComparableValueOnPO( pPO ); return (zPOValue != null) && (CompareSupport.compare( zPOValue, (Comparable) mValue ) < 0); } } private static class Filter_GREATERTHAN extends AbstractPOFWC_ColumnAndValue { public Filter_GREATERTHAN( MetaDataForPO pMD, AttributeAccessorSCD pColumnDefinition, Comparable pValue ) { super( WhereClauseType.GREATERTHAN, pMD, pColumnDefinition, pValue ); } @Override public boolean selects( PersistentObject pPO ) throws DBException { Comparable zPOValue = getComparableValueOnPO( pPO ); return (zPOValue != null) && (CompareSupport.compare( zPOValue, (Comparable) mValue ) > 0); } } private static class Filter_BETWEEN extends AbstractPOFWC_ColumnAndTwoValue { public Filter_BETWEEN( MetaDataForPO pMD, AttributeAccessorSCD pColumnDefinition, Comparable pLeftValue, Comparable pRightValue ) { super( WhereClauseType.BETWEEN, pMD, pColumnDefinition, pLeftValue, pRightValue ); } @Override public boolean selects( PersistentObject pPO ) throws DBException { Comparable zPOValue = getComparableValueOnPO( pPO ); return (zPOValue != null) && // (CompareSupport.compare( mLeftValue, zPOValue ) <= 0) && // (CompareSupport.compare( zPOValue, mRightValue ) <= 0); } @Override protected void toStringHelper( StringBuilder pSB ) { WhereClauseColumnSupport.makeStringValue( pSB, mColumnDefinition, mLeftValue ); pSB.append( " <= " ); toStringColumnReference( pSB ); pSB.append( " <= " ); WhereClauseColumnSupport.makeStringValue( pSB, mColumnDefinition, mRightValue ); } } private static class Filter_IS_ANY_OF extends AbstractPOFWC_ColumnAnd_N_Value { public Filter_IS_ANY_OF( MetaDataForPO pMD, AttributeAccessorSCD pColumnDefinition, Object[] pValues ) { super( WhereClauseType.IS_ANY_OF, pMD, pColumnDefinition, pValues ); } @Override public boolean selects( PersistentObject pPO ) throws DBException { Object zPOValue = getValueOnPO( pPO ); if ( zPOValue != null ) { for ( Object zValue : mValues ) { if ( zPOValue.equals( zValue ) ) { return true; } } } return false; } } private static class Filter_CONTAINS extends AbstractPOFWC_ColumnAndValue { public Filter_CONTAINS( MetaDataForPO pMD, AttributeAccessorSCD pColumnDefinition, String pValue ) { super( WhereClauseType.CONTAINS, pMD, pColumnDefinition, pValue ); } @Override public boolean selects( PersistentObject pPO ) throws DBException { Object zPOValue = getValueOnPO( pPO ); return (zPOValue != null) && zPOValue.toString().contains( mValue.toString() ); } } private static class Filter_STARTS_WITH extends AbstractPOFWC_ColumnAndValue { public Filter_STARTS_WITH( MetaDataForPO pMD, AttributeAccessorSCD pColumnDefinition, String pValue ) { super( WhereClauseType.STARTS_WITH, pMD, pColumnDefinition, pValue ); } @Override public boolean selects( PersistentObject pPO ) throws DBException { Object zPOValue = getValueOnPO( pPO ); return (zPOValue != null) && zPOValue.toString().startsWith( mValue.toString() ); } } private static class Filter_ENDS_WITH extends AbstractPOFWC_ColumnAndValue { public Filter_ENDS_WITH( MetaDataForPO pMD, AttributeAccessorSCD pColumnDefinition, String pValue ) { super( WhereClauseType.ENDS_WITH, pMD, pColumnDefinition, pValue ); } @Override public boolean selects( PersistentObject pPO ) throws DBException { Object zPOValue = getValueOnPO( pPO ); return (zPOValue != null) && zPOValue.toString().endsWith( mValue.toString() ); } } private static class Filter_LIKE extends AbstractPOFWC_ColumnAndLikeValues { public Filter_LIKE( MetaDataForPO pMD, AttributeAccessorSCD pColumnDefinition, StringMatcher pStringMatcher, String[] pValues ) { super( WhereClauseType.LIKE, pMD, pColumnDefinition, pStringMatcher, pValues ); } } } |
Commits for litesoft/trunk/Java/core/Server/src/org/litesoft/orsup/base/filters/POFWCfactory.java
Revision | Author | Commited | Message |
---|---|---|---|
939 Diff | GeorgeS | Mon 02 Jun, 2014 21:30:31 +0000 | Extracting commonfoundation |
151 Diff | GeorgeS | Thu 17 Mar, 2011 04:16:22 +0000 | |
49 Diff | GeorgeS | Mon 12 Apr, 2010 02:59:10 +0000 | License Text |
24 Diff | GeorgeS | Wed 24 Feb, 2010 01:51:38 +0000 | |
2 | GeorgeS | Sun 07 Feb, 2010 12:50:58 +0000 |