1 30 package com.genimen.djeneric.repository; 31 32 import java.math.BigDecimal ; 33 import java.util.ArrayList ; 34 import java.util.Date ; 35 import java.util.HashMap ; 36 37 import com.genimen.djeneric.language.Messages; 38 import com.genimen.djeneric.repository.exceptions.DjenericException; 39 import com.genimen.djeneric.repository.exceptions.ObjectNotDefinedException; 40 import com.genimen.djeneric.repository.exceptions.PropertyFormatException; 41 import com.genimen.djeneric.repository.exceptions.QbeFilterException; 42 import com.genimen.djeneric.util.DjLikeComparator; 43 44 50 public class DjQueryByExample extends DjObject implements DjObjectMatcher 51 { 52 HashMap _operators = new HashMap (); 53 final static String [] _supportedOps = {"==", "!=", "<", ">", "<=", ">=", "like"}; 54 55 62 protected DjQueryByExample(DjSession session, DjExtent extent) throws DjenericException 63 { 64 super(session, extent); 65 setTemporary(true); 67 setTransient(true); 68 } 69 70 77 protected DjQueryByExample(DjSession session, String objectType) throws DjenericException 78 { 79 super(session, objectType); 80 setTransient(true); 81 } 82 83 public static String [] getSupportedOperators() 84 { 85 return _supportedOps; 86 } 87 88 protected void initializeDefaultValues() 89 { } 91 92 100 public void setOperator(int propertyIndex, String operator) throws ObjectNotDefinedException, QbeFilterException 101 { 102 setOperator(getPropertyName(propertyIndex), operator); 103 } 104 105 113 public void setOperator(String propertyName, String operator) throws ObjectNotDefinedException, QbeFilterException 114 { 115 if (operator.equals("<>")) operator = "!="; 116 117 getProperty(propertyName); 119 isValidOperator(operator); 121 122 _operators.put(propertyName, operator); 123 } 124 125 public static void isValidOperator(String operator) throws QbeFilterException 126 { 127 boolean valid = false; 128 for (int i = 0; !valid && i < _supportedOps.length; i++) 129 valid |= operator.equals(_supportedOps[i]); 130 131 if (!valid) throw new QbeFilterException(Messages.getString("DjQueryByExample.UnsupportedOperator", operator)); 132 } 133 134 141 public String getOperator(String propertyName) throws DjenericException 142 { 143 getProperty(propertyName); 145 146 String result = (String ) _operators.get(propertyName); 147 if (result == null) result = "=="; 148 149 return result; 150 } 151 152 public boolean hasOperatorFor(String propertyName) 153 { 154 String result = (String ) _operators.get(propertyName); 155 return result != null; 156 } 157 158 public boolean hasOperatorFor(int propertyIndex) 159 { 160 return hasOperatorFor(getPropertyName(propertyIndex)); 161 } 162 163 public void removeOperatorFor(String propertyName) 164 { 165 _operators.remove(propertyName); 166 } 167 168 public void removeOperatorFor(int propertyIndex) 169 { 170 _operators.remove(getPropertyName(propertyIndex)); 171 } 172 173 180 public String getOperator(int propertyIndex) throws DjenericException 181 { 182 return getOperator(getPropertyName(propertyIndex)); 183 } 184 185 190 public DjExtent getExtent() 191 { 192 return super.getExtent(); 193 } 194 195 200 protected void update() 201 { 202 } 204 205 210 protected void insert() 211 { 212 } 214 215 220 protected void delete() 221 { 222 } 224 225 230 public void reload() 231 { 232 } 234 235 public boolean match(DjObject po) throws DjenericException 236 { 237 if (po.isMarkedForDelete()) return false; 239 240 for (int j = 0; j < getPropertyCount(); j++) 241 { 242 if (!isNull(j)) 243 { 244 DjProperty prop = getProperty(j); 245 String oper = getOperator(j); 246 247 if (oper.equals("==")) 248 { 249 if (!get(j).equals(po.get(prop.getName()))) 250 { 251 return false; 252 } 253 } 254 else if (oper.equals("!=")) 255 { 256 if (get(j).equals(po.get(prop.getName()))) 257 { 258 return false; 259 } 260 } 261 else 262 { 263 Object o1 = get(j); 264 Object o2 = po.get(prop.getName()); 265 266 if (oper.equals("<")) 267 { 268 if (o1 instanceof String ) return o1.toString().compareTo(o2.toString()) < 0; 269 if (o1 instanceof BigDecimal ) return ((BigDecimal ) o1).compareTo((BigDecimal ) o1) < 0; 270 if (o1 instanceof Date ) return ((Date ) o1).compareTo((Date ) o1) < 0; 271 if (o1 instanceof Integer ) return ((Integer ) o1).compareTo((Integer ) o1) < 0; 272 if (o1 instanceof Long ) return ((Long ) o1).compareTo((Long ) o1) < 0; 273 throw new DjenericException(Messages.getString("DjQueryByExample.CanNotMatch", o1.getClass().getName())); 274 } 275 276 if (oper.equals("<=")) 277 { 278 if (o1 instanceof String ) return o1.toString().compareTo(o2.toString()) <= 0; 279 if (o1 instanceof BigDecimal ) return ((BigDecimal ) o1).compareTo((BigDecimal ) o1) <= 0; 280 if (o1 instanceof Date ) return ((Date ) o1).compareTo((Date ) o1) <= 0; 281 if (o1 instanceof Integer ) return ((Integer ) o1).compareTo((Integer ) o1) <= 0; 282 if (o1 instanceof Long ) return ((Long ) o1).compareTo((Long ) o1) <= 0; 283 throw new DjenericException(Messages.getString("DjQueryByExample.CanNotMatch", o1.getClass().getName())); 284 } 285 286 if (oper.equals(">")) 287 { 288 if (o1 instanceof String ) return o1.toString().compareTo(o2.toString()) > 0; 289 if (o1 instanceof BigDecimal ) return ((BigDecimal ) o1).compareTo((BigDecimal ) o1) > 0; 290 if (o1 instanceof Date ) return ((Date ) o1).compareTo((Date ) o1) > 0; 291 if (o1 instanceof Integer ) return ((Integer ) o1).compareTo((Integer ) o1) > 0; 292 if (o1 instanceof Long ) return ((Long ) o1).compareTo((Long ) o1) > 0; 293 throw new DjenericException(Messages.getString("DjQueryByExample.CanNotMatch", o1.getClass().getName())); 294 } 295 296 if (oper.equals(">=")) 297 { 298 if (o1 instanceof String ) return o1.toString().compareTo(o2.toString()) >= 0; 299 if (o1 instanceof BigDecimal ) return ((BigDecimal ) o1).compareTo((BigDecimal ) o1) >= 0; 300 if (o1 instanceof Date ) return ((Date ) o1).compareTo((Date ) o1) >= 0; 301 if (o1 instanceof Integer ) return ((Integer ) o1).compareTo((Integer ) o1) >= 0; 302 if (o1 instanceof Long ) return ((Long ) o1).compareTo((Long ) o1) >= 0; 303 throw new DjenericException(Messages.getString("DjQueryByExample.CanNotMatch", o1.getClass().getName())); 304 } 305 306 if (oper.equals("like")) 307 { 308 String s1 = getString(j); 309 String s2 = po.getString(prop.getName()); 310 311 if (s1 == null || s2 == null) return false; 312 313 DjLikeComparator like = new DjLikeComparator(s2, '\\', false); 314 return like.compare(s1); 315 } 316 } 317 } 318 } 319 return true; 320 } 321 322 public void setExpressionValue(String propertyName, Object value) throws PropertyFormatException, 323 ObjectNotDefinedException, DjenericException 324 { 325 set(propertyName, translateConstant(value)); 326 } 327 328 public void setExpressionValue(int propertyIdx, Object value) throws PropertyFormatException, 329 ObjectNotDefinedException, DjenericException 330 { 331 set(propertyIdx, translateConstant(value)); 332 } 333 334 public static Object translateConstant(Object someValue) throws QbeFilterException 335 { 336 if (someValue instanceof String ) 337 { 338 String value = (String ) someValue; 339 if ((value.startsWith("\"") && value.endsWith("\"")) || (value.startsWith("'") && value.endsWith("'"))) 340 { 341 value = value.substring(1); 342 if (value.length() > 0) value = value.substring(0, value.length() - 1); 343 else throw new QbeFilterException(Messages.getString("DjQueryByExample.UnterminatedString", someValue)); 344 someValue = value; 345 } 346 else 347 { 348 try 349 { someValue = new BigDecimal (value); 351 } 352 catch (Exception x) 353 { 354 if (value.equalsIgnoreCase("null")) someValue = null; 356 else if (value.equalsIgnoreCase("timestamp")) someValue = new Date (); 357 else throw new QbeFilterException(Messages.getString("DjQueryByExample.InvalidQBEConstant", value)); 358 } 359 } 360 } 361 return someValue; 362 } 363 364 public String [] getPropertiesWithFilter() throws DjenericException 365 { 366 String propertyNames[] = getPropertyNames(); 367 ArrayList result = new ArrayList (); 368 369 for (int i = 0; i < propertyNames.length; i++) 370 { 371 if (hasOperatorFor(propertyNames[i]) || !isNull(propertyNames[i])) 372 { 373 result.add(propertyNames[i]); 374 } 375 } 376 return (String []) result.toArray(new String [0]); 377 } 378 379 public boolean isResultUnique() 380 { 381 return true; 383 } 384 385 394 public void acceptSimpleProperties(DjObject obj, boolean includeNulls) throws DjenericException 395 { 396 for (int i = 0; i < getPropertyCount(); i++) 397 { 398 DjProperty prop = getProperty(i); 399 if (prop.getType() instanceof DjDomain && obj.hasProperty(prop.getName())) 400 { 401 if (obj.getExtent().getIdProperty().getName().equals(prop.getName())) continue; 402 403 if (!obj.isNull(prop.getName())) set(prop.getName(), obj.get(prop.getName())); 404 else if (includeNulls) setNull(prop.getName()); 405 } 406 } 407 } 408 409 public String toString() 410 { 411 StringBuffer sb = new StringBuffer (100); 412 DjExtent extent = getExtent(); 413 sb.append("Qbe(" + extent.getName() + ")\n{\n"); 414 for (int i = 0; i < extent.getPropertyCount(); i++) 415 { 416 if (!isNull(i)) 417 { 418 sb.append(" " + extent.getProperty(i).getName() + "=\"" + getString(i) + "\"\n"); 419 } 420 } 421 sb.append("}"); 422 return sb.toString(); 423 } 424 } | Popular Tags |