1 15 package org.josql.expressions; 16 17 import com.gentlyweb.utils.Getter; 18 19 import org.josql.Query; 20 import org.josql.QueryExecutionException; 21 import org.josql.QueryParseException; 22 23 import org.josql.internal.Utilities; 24 25 37 public class Accessor extends ValueExpression 38 { 39 40 private String acc = null; 41 private Getter get = null; 42 43 public Class getExpectedReturnType (Query q) 44 throws QueryParseException 45 { 46 47 return this.get.getType (); 48 49 } 50 51 public void init (Query q) 52 throws QueryParseException 53 { 54 55 try 57 { 58 59 this.get = new Getter (this.acc, 60 q.getFromObjectClass ()); 61 62 } catch (Exception e) { 63 64 throw new QueryParseException ("Unable to create getter: " + 65 this.acc, 66 e); 67 68 } 69 70 } 71 72 public String getAccessor () 73 { 74 75 return this.acc; 76 77 } 78 79 public void setAccessor (String a) 80 { 81 82 this.acc = a; 83 84 } 85 86 public Getter getGetter () 87 { 88 89 return this.get; 90 91 } 92 93 public void setName (String name) 94 { 95 96 this.acc = name; 97 98 } 99 100 public boolean isTrue (Object o, 101 Query q) 102 throws QueryExecutionException 103 { 104 105 o = this.evaluate (o, 106 q); 107 108 if (o == null) 109 { 110 111 return false; 112 113 } 114 115 if (Utilities.isNumber (o)) 116 { 117 118 return Utilities.getDouble (o) > 0; 119 120 } 121 122 if (o instanceof Boolean ) 123 { 124 125 return ((Boolean ) o).booleanValue (); 126 127 } 128 129 return true; 131 132 } 133 134 public boolean hasFixedResult (Query q) 135 { 136 137 return false; 139 140 } 141 142 public Object evaluate (Object o, 143 Query q) 144 throws QueryExecutionException 145 { 146 147 try 148 { 149 150 return this.get.getValue (o); 151 152 } catch (Exception e) { 153 154 throw new QueryExecutionException ("Unable to get value from: " + 155 this + 156 " passed in object type: " + 157 o.getClass ().getName () + 158 " expecting: " + 159 this.get.getType ().getName (), 160 e); 161 162 } 163 164 } 165 166 public boolean equals (Object o) 167 { 168 169 if (o == null) 170 { 171 172 return false; 173 174 } 175 176 if (!(o instanceof Accessor)) 177 { 178 179 return false; 180 181 } 182 183 Accessor a = (Accessor) o; 184 185 return this.acc.equals (a.getAccessor ()); 186 187 } 188 189 public String toString () 190 { 191 192 if (this.isBracketed ()) 193 { 194 195 return "(" + this.acc + ")"; 196 197 } 198 199 return this.acc + "[detail: " + this.get + "]"; 200 201 } 202 203 } 204
| Popular Tags
|