1 30 package com.genimen.djeneric.repository; 31 32 import java.math.BigDecimal ; 33 import java.util.ArrayList ; 34 import java.util.HashMap ; 35 import java.util.Iterator ; 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.oql.DjOqlWhereTranslator; 41 import com.genimen.djeneric.repository.oql.core.MatchException; 42 import com.genimen.djeneric.repository.oql.core.ParseException; 43 import com.genimen.djeneric.repository.oql.core.SimpleNode; 44 import com.genimen.djeneric.repository.oql.core.nodes.MatchingContext; 45 46 public class DjOql implements DjObjectMatcher 47 { 48 DjExtent _extent; 49 String _alias; 50 ArrayList _parameterNames = null; 51 HashMap _parameters = new HashMap (); 52 String _sqlFromWhere; 53 SimpleNode _matchTree = null; 54 DjOqlWhereTranslator _xltr = null; 55 ArrayList _baseRestrictions = new ArrayList (); 56 String _expression = null; 57 boolean _dirty = false; 58 boolean _resultIsUnique = true; 59 60 public DjOql(DjExtent extent) 61 { 62 _extent = extent; 63 64 String alias = _extent.getAlias().toLowerCase(); 65 if (alias.length() > 4) alias = alias.substring(0, 4); 66 alias += "0"; 67 68 setAlias(alias); 69 } 70 71 public DjOql(DjExtent extent, String whereExpression) 72 { 73 this(extent); 74 setWhereExpression(whereExpression); 75 } 76 77 public void clearBaseRestrictions() 78 { 79 _baseRestrictions = new ArrayList (); 80 _dirty = true; 81 } 82 83 public void addBaseRestriction(String restriction) 84 { 85 if (restriction != null && restriction.trim().length() > 0) 86 { 87 _baseRestrictions.add(restriction); 88 _dirty = true; 89 } 90 } 91 92 public void setWhereExpression(String expression) 93 { 94 _expression = expression; 95 _dirty = true; 96 } 97 98 public String getWhereExpression() 99 { 100 return _expression; 101 } 102 103 private void rebuildExpressionTree() throws ParseException 104 { 105 String completeExpr = ""; 106 107 Iterator it = _baseRestrictions.iterator(); 108 while (it.hasNext()) 109 { 110 completeExpr = addAnd(completeExpr); 111 completeExpr += "(" + it.next().toString() + ")"; 112 } 113 114 if (_expression != null && _expression.trim().length() > 0) 115 { 116 completeExpr = addAnd(completeExpr); 117 completeExpr += "(" + _expression + ")"; 118 } 119 120 if (completeExpr.trim().length() == 0) 121 { 122 _xltr = null; 123 _sqlFromWhere = getFromOnly(_extent); 124 } 125 else 126 { 127 _xltr = new DjOqlWhereTranslator(); 128 try 129 { 130 _sqlFromWhere = _xltr.translateWhere(_extent, _alias, completeExpr); 131 _resultIsUnique = _xltr.isResultUnique(); 132 } 133 catch (ParseException pe) 134 { 135 pe.setAdditionalInfo(completeExpr); 136 throw pe; 137 } 138 _parameterNames = _xltr.getParameters(); 139 _matchTree = _xltr.getTree(); 140 } 141 142 removeUnusedParameters(); 143 144 _dirty = false; 145 146 } 147 148 protected void removeUnusedParameters() 149 { 150 ArrayList removeThese = new ArrayList (); 151 Iterator it = _parameters.keySet().iterator(); 152 while (it.hasNext()) 153 { 154 String name = it.next().toString(); 155 if (!_parameterNames.contains(name)) removeThese.add(name); 156 } 157 158 it = removeThese.iterator(); 159 while (it.hasNext()) 160 _parameters.remove(it.next()); 161 } 162 163 private String addAnd(String completeExpr) 164 { 165 if (completeExpr.trim().length() > 0) completeExpr += " && "; 166 return completeExpr; 167 } 168 169 private String getFromOnly(DjExtent extent) 170 { 171 return "from " + getExtent().getName() + " " + getAlias(); 172 } 173 174 public void setBaseClassExtent(DjExtent extent) 175 { 176 _extent = extent; 177 if (_xltr == null) _dirty = true; 178 } 179 180 public String getFromWhereClause() throws ParseException 181 { 182 if (!_dirty && _xltr != null) 183 { 184 _sqlFromWhere = _xltr.translateAgain(_extent); 187 } 188 else if (_dirty) rebuildExpressionTree(); 189 190 return _sqlFromWhere; 191 } 192 193 public boolean match(DjObject po) throws DjenericException 194 { 195 try 196 { 197 if (_dirty) rebuildExpressionTree(); 198 } 199 catch (ParseException e1) 200 { 201 throw new DjenericException(e1); 202 } 203 204 if (_matchTree == null) return true; 205 206 MatchingContext ctxt = new MatchingContext(); 207 ctxt.setObject(po); 208 ctxt.setParameters(_parameters); 209 210 try 211 { 212 return _matchTree.match(ctxt); 213 } 214 catch (MatchException e) 215 { 216 throw new DjenericException(e); 217 } 218 } 219 220 public String getAlias() 221 { 222 return _alias; 223 } 224 225 protected void setAlias(String string) 226 { 227 _alias = string.toLowerCase(); 228 } 229 230 public void setParameter(String name, Object value) 231 { 232 if (value instanceof Boolean ) 233 { 234 boolean b = ((Boolean ) value).booleanValue(); 235 value = new Integer (b ? 1 : 0); 236 } 237 238 _parameters.put(name, value); 239 } 240 241 public void setParameter(String name, int value) 242 { 243 setParameter(name, new Integer (value)); 244 } 245 246 public void setParameter(String name, long value) 247 { 248 setParameter(name, new Long (value)); 249 } 250 251 public void setParameter(String name, double value) 252 { 253 setParameter(name, new BigDecimal (value)); 254 } 255 256 public void setParameter(String name, boolean value) 257 { 258 setParameter(name, new Boolean (value)); 259 } 260 261 public HashMap getParameters() 262 { 263 return _parameters; 264 } 265 266 public void setParameters(HashMap parameters) 267 { 268 _parameters = parameters; 269 } 270 271 public DjExtent getExtent() 272 { 273 return _extent; 274 } 275 276 public void validateParameters() throws ObjectNotDefinedException, ParseException 277 { 278 if (_dirty) rebuildExpressionTree(); 279 Iterator it = _parameters.keySet().iterator(); 280 281 while (it.hasNext()) 282 { 283 String name = it.next().toString(); 284 if (!_parameterNames.contains(name)) throw new ObjectNotDefinedException(Messages 285 .getString("DjOql.ParameterUndefined", name)); 286 } 287 } 288 289 public String [] getParameterNames() throws ParseException 290 { 291 if (_parameterNames == null) getFromWhereClause(); 292 if (_parameterNames == null) return new String [0]; 293 return (String []) _parameterNames.toArray(new String [_parameterNames.size()]); 294 } 295 296 public boolean isResultUnique() 297 { 298 return _resultIsUnique; 299 } 300 } | Popular Tags |