Date: 2004-10-27T05:33:17
Editor: HansH�lf <[EMAIL PROTECTED]>
Wiki: DB Torque Wiki
Page: HarderTorqueFaq
URL: http://wiki.apache.org/db-torque/HarderTorqueFaq
This is my first entry in a Wiki of this kind. Sorry if I did something 'wrong'.
Change Log:
------------------------------------------------------------------------------
@@ -168,3 +168,68 @@
RaphaelMankin
+There is a simple solution for creating all the BeanInfo classes for all Tables
without too much overhead.
+
+First, create an abstract base class. This base class constructs the property
descriptor array from the getFields() method. The getTheClass() method provides the
class of your concrete Table class derived from BaseObject.
+
+{{{
+
+package com.gsk.pmf;
+
+import java.beans.*;
+import java.util.*;
+
+/**
+ * @author Hans H�lf
+ */
+public abstract class TorqueBeanInfo extends SimpleBeanInfo {
+
+ abstract protected Class getTheClass();
+ abstract protected Collection getFields();
+
+ public PropertyDescriptor[] getPropertyDescriptors()
+ {
+ try {
+ PropertyDescriptor[] pdar = new
PropertyDescriptor[getFields().size()];
+ Iterator it = getFields().iterator();
+ for (int i = 0; it.hasNext(); i++) {
+ String field = (String) it.next();
+ PropertyDescriptor pd = new PropertyDescriptor(field,
getTheClass());
+ pdar[i]=pd;
+ }
+
+ return pdar;
+
+ } catch (IntrospectionException e) {
+ System.err.println("IntrospectionException"+e.getMessage());
+ e.printStackTrace();
+ }
+ return null;
+ }
+
+}
+}}}
+
+Then you only have to subclass once for every BaseObject class you have. In this
example the above XML created a class called Modelevent (extends BaseModelevent
(extends BaseObject)).
+
+{{{
+
+package com.gsk.pmf;
+
+import java.util.*;
+/**
+ * @author Hans H�lf
+ */
+public class ModeleventBeanInfo extends TorqueBeanInfo {
+ protected Class getTheClass() {
+ return Modelevent.class;
+ }
+ protected Collection getFields(){
+ return Modelevent.getFieldNames();
+ }
+}
+}}}
+
+The advantage of this method is that you don't have to change the BeanInfo classes
after the Table classes have changed. Only if you add new Tables you have to provide a
new BeanInfo class.
+
+HansH�lf
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]