1 18 19 package org.jahia.data.containers; 20 21 import java.io.Serializable ; 22 23 24 25 31 class FilterClause implements Serializable { 32 33 private static final String CLASS_NAME = FilterClause.class.getName(); 34 35 36 private String lowerComp; 37 38 private String upperComp; 39 40 private String [] values; 41 42 private boolean isValid = false; 43 44 private boolean isRangeClause = false; 45 46 53 public FilterClause(String comp, String value){ 54 if ( value != null && comp != null ) 55 { 56 String [] values = {value}; 57 this.values = values; 58 this.lowerComp = comp; 59 this.isValid = true; 60 } 61 } 62 63 70 public FilterClause(String comp, String [] values){ 71 if ( (values != null) && (values.length>0)&& (comp != null) ) 72 { 73 this.values = values; 74 this.lowerComp = comp; 75 this.isValid = true; 76 } 77 } 78 79 88 public FilterClause( String lowerComp, 89 String upperComp, 90 String lowerVal, 91 String upperVal ){ 92 93 if ( (lowerVal != null) 94 && (upperVal != null) 95 && (lowerComp != null) 96 && (upperComp != null) ) 97 { 98 String [] values = {lowerVal,upperVal}; 99 this.values = values; 100 this.lowerComp = lowerComp; 101 this.upperComp = upperComp; 102 this.isValid = true; 103 this.isRangeClause = true; 104 } 105 } 106 107 110 public boolean isValid(){ 111 return this.isValid; 112 } 113 114 117 public boolean isRangeClause(){ 118 return this.isRangeClause; 119 } 120 121 124 public String [] getValues(){ 125 return this.values; 126 } 127 128 131 public String getValue(){ 132 return this.values[0]; 133 } 134 135 138 public String getLowerValue(){ 139 return this.values[0]; 140 } 141 142 145 public String getUpperValue(){ 146 return this.values[1]; 147 } 148 149 152 public String getComp(){ 153 return this.lowerComp; 154 } 155 156 159 public String getLowerComp(){ 160 return this.lowerComp; 161 } 162 163 166 public String getUpperComp(){ 167 return this.upperComp; 168 } 169 170 179 public boolean compareNumber(String value, String numberFormat){ 180 if ( !isValid ){ 181 return false; 182 } 183 184 boolean result = false; 185 186 try { 187 String comp = getComp(); 188 String v = null; 189 190 if ( isNullOrNotNullClause() ){ 192 result = compareNullOrNotNull(value); 193 } else if ( isRangeClause() ) { 194 v = getValue(); 195 result = compare(value,v,comp,numberFormat); 196 if ( result ) 197 { 198 v = getUpperValue(); 199 result = compare(value,v,getUpperComp(),numberFormat); 200 } 201 } else { 202 String [] vals = this.getValues(); 203 int size = vals.length; 204 for ( int i=0; i<size; i++ ){ 205 v = vals[i]; 206 result = compare(value,v,comp,numberFormat); 207 if ( result ){ 208 return true; 209 } 210 } 211 } 212 } catch ( Throwable t ){ 213 t.printStackTrace(); 214 } 215 return result; 216 } 217 218 224 public boolean compare(String value){ 225 if ( !isValid || value == null ){ 226 return false; 227 } 228 229 boolean result = false; 230 231 String [] vals = this.getValues(); 232 int size = vals.length; 233 for ( int i=0; i<size; i++ ){ 234 String val = vals[i]; 235 if ( ContainerFilterBean.COMP_EQUAL.equals(this.getComp()) ){ 236 if ( value.equals(val) ){ 237 return true; 238 } 239 } else if ( ContainerFilterBean.COMP_NOT_EQUAL.equals(this.getComp())) { 240 if ( !value.equals(val) ){ 241 return true; 242 } 243 } else if ( ContainerFilterBean.COMP_ISNULL.equals(this.getComp())) { 244 if ( value == null || "".equals(value.trim()) ){ 245 return true; 246 } 247 } else if ( ContainerFilterBean.COMP_NOTNULL.equals(this.getComp())) { 248 if ( value != null && !"".equals(value.trim()) ){ 249 return true; 250 } 251 } else if ( ContainerFilterBean.COMP_STARTS_WITH.equals(this.getComp())) { 252 if ( val.toLowerCase().startsWith(value.toLowerCase())){ 253 return true; 254 } 255 } 256 } 257 return result; 258 } 259 260 private boolean compareNullOrNotNull(String value) 261 { 262 if (!isValid) 263 { 264 return false; 265 } 266 267 if (ContainerFilterBean.COMP_ISNULL.equals(this.getComp())) 268 { 269 return value == null || value.trim().length() == 0; 270 } 271 else if (ContainerFilterBean.COMP_NOTNULL.equals(this.getComp())) 272 { 273 return value != null && value.trim().length() > 0; 274 } 275 276 return false; 277 } 278 279 288 private boolean compare(String valueA, String valueB, String comp, String format){ 289 boolean result = false; 290 int compResult = 0; 291 if ( comp.equals(ContainerFilterBean.COMP_EQUAL) ){ 292 result = ( NumberFormats.compareNumber(valueA,valueB, format) == 0 ); 293 } else if ( comp.equals(ContainerFilterBean.COMP_SMALLER) ){ 294 result = ( NumberFormats.compareNumber(valueA,valueB, format) == -1 ); 295 } else if ( comp.equals(ContainerFilterBean.COMP_SMALLER_OR_EQUAL) ){ 296 compResult = NumberFormats.compareNumber(valueA,valueB, format); 297 result = ( compResult == 0 || compResult == -1 ); 298 } else if ( comp.equals(ContainerFilterBean.COMP_BIGGER_OR_EQUAL) ){ 299 compResult = NumberFormats.compareNumber(valueA,valueB, format); 300 result = ( compResult == 0 || compResult == 1 ); 301 } else if ( comp.equals(ContainerFilterBean.COMP_BIGGER) ){ 302 compResult = NumberFormats.compareNumber(valueA,valueB, format); 303 result = ( compResult == 1 ); 304 } else if ( comp.equals(ContainerFilterBean.COMP_NOT_EQUAL) ){ 305 if ( valueA == null && valueB == null ){ 306 return true; 307 } else { 308 try { 309 result = valueA.equals(valueB) ; 310 } catch ( Throwable t ){ 311 } 312 } 313 } 314 316 return result; 317 } 318 319 323 public boolean isNullOrNotNullClause() 324 { 325 return ContainerFilterBean.COMP_ISNULL.equals(getComp()) 326 || ContainerFilterBean.COMP_NOTNULL.equals(getComp()); 327 } 328 329 } 330 | Popular Tags |