1 29 30 package com.caucho.amber.type; 31 32 import com.caucho.amber.field.AmberField; 33 import com.caucho.amber.field.AmberFieldCompare; 34 import com.caucho.amber.manager.AmberPersistenceUnit; 35 import com.caucho.amber.table.Column; 36 import com.caucho.amber.table.Table; 37 import com.caucho.bytecode.JClass; 38 import com.caucho.bytecode.JClassDependency; 39 import com.caucho.bytecode.JField; 40 import com.caucho.bytecode.JMethod; 41 import com.caucho.config.ConfigException; 42 import com.caucho.java.JavaWriter; 43 import com.caucho.make.ClassDependency; 44 import com.caucho.util.CharBuffer; 45 import com.caucho.util.L10N; 46 import com.caucho.vfs.PersistentDependency; 47 48 import java.io.IOException ; 49 import java.util.ArrayList ; 50 import java.util.Collections ; 51 import java.util.HashMap ; 52 import java.util.logging.Logger ; 53 54 58 abstract public class AbstractStatefulType extends AbstractEnhancedType { 59 private static final Logger log = Logger.getLogger(AbstractStatefulType.class.getName()); 60 private static final L10N L = new L10N(AbstractStatefulType.class); 61 62 private boolean _isFieldAccess; 63 64 private ArrayList <AmberField> _fields = new ArrayList <AmberField>(); 65 66 private volatile boolean _isConfigured; 67 68 private ArrayList <PersistentDependency> _dependencies 69 = new ArrayList <PersistentDependency>(); 70 71 private HashMap <String ,String > _completionFields 72 = new HashMap <String ,String >(); 73 74 private Column _discriminator; 75 76 public AbstractStatefulType(AmberPersistenceUnit amberPersistenceUnit) 77 { 78 super(amberPersistenceUnit); 79 } 80 81 84 public void setFieldAccess(boolean isFieldAccess) 85 { 86 _isFieldAccess = isFieldAccess; 87 } 88 89 92 public boolean isFieldAccess() 93 { 94 return _isFieldAccess; 95 } 96 97 100 public Column getDiscriminator() 101 { 102 return _discriminator; 103 } 104 105 108 public void setDiscriminator(Column discriminator) 109 { 110 _discriminator = discriminator; 111 } 112 113 116 public String getJavaTypeName() 117 { 118 return getInstanceClassName(); 119 } 120 121 124 public void addField(AmberField field) 125 { 126 _fields.add(field); 127 Collections.sort(_fields, new AmberFieldCompare()); 128 } 129 130 133 public ArrayList <AmberField> getFields() 134 { 135 return _fields; 136 } 137 138 141 public AmberField getField(String name) 142 { 143 for (int i = 0; i < _fields.size(); i++) { 144 AmberField field = _fields.get(i); 145 146 if (field.getName().equals(name)) 147 return field; 148 } 149 150 return null; 151 } 152 153 156 public void setBeanClass(JClass beanClass) 157 { 158 super.setBeanClass(beanClass); 159 160 addDependency(_beanClass); 161 } 162 163 166 public void addDependency(Class cl) 167 { 168 addDependency(new ClassDependency(cl)); 169 } 170 171 174 public void addDependency(JClass cl) 175 { 176 addDependency(new JClassDependency(cl)); 177 } 178 179 182 public void addDependency(PersistentDependency depend) 183 { 184 if (! _dependencies.contains(depend)) 185 _dependencies.add(depend); 186 } 187 188 191 public ArrayList <PersistentDependency> getDependencies() 192 { 193 return _dependencies; 194 } 195 196 199 public void addCompletionField(String name) 200 { 201 _completionFields.put(name, name); 202 } 203 204 207 public boolean containsCompletionField(String completionField) 208 { 209 return _completionFields.containsKey(completionField); 210 } 211 212 215 public void removeAllCompletionFields() 216 { 217 _completionFields.clear(); 218 } 219 220 223 public boolean startConfigure() 224 { 225 synchronized (this) { 226 if (_isConfigured) 227 return false; 228 229 _isConfigured = true; 230 231 return true; 232 } 233 } 234 235 238 public void init() 239 throws ConfigException 240 { 241 } 242 243 246 public String generateCastFromObject(String value) 247 { 248 return "((" + getInstanceClassName() + ") " + value + ")"; 249 } 250 251 254 public int generateLoad(JavaWriter out, String rs, 255 String indexVar, int index, int loadGroupIndex) 256 throws IOException 257 { 258 if (loadGroupIndex == 0 && _discriminator != null) 259 index++; 260 261 ArrayList <AmberField> fields = getFields(); 262 for (int i = 0; i < fields.size(); i++) { 263 AmberField field = fields.get(i); 264 265 if (field.getLoadGroupIndex() == loadGroupIndex) 266 index = field.generateLoad(out, rs, indexVar, index); 267 } 268 269 return index; 270 } 271 272 275 abstract public String generateLoadSelect(Table table, String id); 276 277 280 public String generateLoadSelect(Table table, 281 String id, 282 int loadGroup) 283 { 284 return generateLoadSelect(table, id, loadGroup, false); 285 } 286 287 290 public String generateLoadSelect(Table table, 291 String id, 292 int loadGroup, 293 boolean hasSelect) 294 { 295 CharBuffer cb = CharBuffer.allocate(); 296 297 ArrayList <AmberField> fields = getFields(); 298 299 for (int i = 0; i < fields.size(); i++) { 300 AmberField field = fields.get(i); 301 302 if (field.getLoadGroupIndex() != loadGroup) 303 continue; 304 305 String propSelect = field.generateLoadSelect(table, id); 306 307 if (propSelect == null) 308 continue; 309 310 if (hasSelect) 311 cb.append(", "); 312 hasSelect = true; 313 314 cb.append(propSelect); 315 } 316 317 if (cb.length() == 0) 318 return null; 319 else 320 return cb.close(); 321 } 322 323 326 public void generateInvalidateForeign(JavaWriter out) 327 throws IOException 328 { 329 for (AmberField field : getFields()) { 330 field.generateInvalidateForeign(out); 331 } 332 } 333 334 337 public void generateExpire(JavaWriter out) 338 throws IOException 339 { 340 for (AmberField field : getFields()) { 341 field.generateExpire(out); 342 } 343 } 344 345 348 public JMethod getGetter(String name) 349 { 350 return getGetter(_beanClass, name); 351 } 352 353 356 public static JMethod getGetter(JClass cl, String name) 357 { 358 JMethod []methods = cl.getMethods(); 359 360 for (int i = 0; i < methods.length; i++) { 361 JClass []param = methods[i].getParameterTypes(); 362 String methodName = methods[i].getName(); 363 364 if (name.equals(methodName) && param.length == 0) 365 return methods[i]; 366 } 367 368 cl = cl.getSuperClass(); 369 370 if (cl != null) 371 return getGetter(cl, name); 372 else 373 return null; 374 } 375 376 379 public static JField getField(JClass cl, String name) 380 { 381 JField []fields = cl.getDeclaredFields(); 382 383 for (int i = 0; i < fields.length; i++) { 384 if (name.equals(fields[i].getName())) 385 return fields[i]; 386 } 387 388 cl = cl.getSuperClass(); 389 390 if (cl != null) 391 return getField(cl, name); 392 else 393 return null; 394 } 395 396 399 public static JMethod getSetter(JClass cl, String name) 400 { 401 JMethod []methods = cl.getMethods(); 402 403 for (int i = 0; i < methods.length; i++) { 404 JClass []param = methods[i].getParameterTypes(); 405 String methodName = methods[i].getName(); 406 407 if (name.equals(methodName) && param.length == 1) 408 return methods[i]; 409 } 410 411 cl = cl.getSuperClass(); 412 413 if (cl != null) 414 return getSetter(cl, name); 415 else 416 return null; 417 } 418 419 422 public long getCreateLoadMask(int group) 423 { 424 long mask = 0; 425 426 for (int i = 0; i < _fields.size(); i++) { 427 mask |= _fields.get(i).getCreateLoadMask(group); 428 } 429 430 return mask; 431 } 432 433 436 public String toString() 437 { 438 return "AbstractStatefulType[" + _beanClass.getName() + "]"; 439 } 440 } 441 | Popular Tags |