EntityCmpDesc.java

00001 
00026 package org.objectweb.jonas_ejb.deployment.api;
00027 
00028 import java.lang.reflect.Field;
00029 import java.lang.reflect.Modifier;
00030 import java.util.HashMap;
00031 import java.util.Iterator;
00032 
00033 import org.objectweb.jonas_ejb.deployment.xml.AssemblyDescriptor;
00034 import org.objectweb.jonas_ejb.deployment.xml.Entity;
00035 import org.objectweb.jonas_ejb.deployment.xml.CmpField;
00036 import org.objectweb.jonas_ejb.deployment.xml.JonasEntity;
00037 import org.objectweb.jonas_ejb.deployment.xml.JdbcMapping;
00038 import org.objectweb.jonas_ejb.lib.BeanNaming;
00039 import org.objectweb.jonas_lib.deployment.api.DeploymentDescException;
00040 import org.objectweb.jonas_lib.deployment.xml.JLinkedList;
00041 
00048 public abstract class EntityCmpDesc extends EntityDesc {
00049 
00050     protected HashMap fieldDesc = new HashMap();
00051     protected String primaryKeyField = null;
00052     protected String jdbcAutomaticPkFieldName = null;
00053 
00063     public EntityCmpDesc(ClassLoader classLoader, Entity ent,
00064              AssemblyDescriptor asd, JonasEntity jEnt,
00065              JLinkedList jMDRList, String fileName)
00066         throws DeploymentDescException {
00067 
00068         super(classLoader, ent, asd, jEnt, jMDRList, fileName);
00069 
00070         // field descriptors for persistent fields
00071         for (Iterator i = ent.getCmpFieldList().iterator(); i.hasNext();) {
00072             String fn = ((CmpField) i.next()).getFieldName();
00073             FieldDesc fd = this.newFieldDescInstance();
00074             fd.setName(fn);
00075             fd.setPrimaryKey(false);
00076             fieldDesc.put(fn, fd);
00077 
00078         }
00079 
00080         // jdbc pk auto field name
00081         JdbcMapping jm = jEnt.getJdbcMapping();
00082         if ((jm != null) && (jm.getJdbcAutomaticPkFieldName() != null)) {
00083             if (jm.getJdbcAutomaticPkFieldName().length() != 0) {
00084                 jdbcAutomaticPkFieldName = jm.getJdbcAutomaticPkFieldName();
00085             }
00086         }
00087         if (jdbcAutomaticPkFieldName == null) {
00088             // Default value
00089             jdbcAutomaticPkFieldName =  "JPK_";
00090         }
00091 
00092         if (isUndefinedPK()) { // Automatic PK
00093            // If Primary Field is not declared (auto generated key field)
00094             primaryKeyField = "JONASAUTOPKFIELD";
00095         } else if (ent.getPrimkeyField() != null) {
00096             // primary key field
00097             primaryKeyField = ent.getPrimkeyField();
00098             FieldDesc fd = (FieldDesc) fieldDesc.get(primaryKeyField);
00099             if (fd == null) {
00100                 throw new DeploymentDescException("primkey-field " + primaryKeyField + " is not listed as cmp-field in bean " + this.ejbName);
00101             }
00102             fd.setPrimaryKey(true);
00103         } else {
00104             // public fields of primary key class
00105             Field[] pcf = primaryKeyClass.getFields();
00106             for (int i = 0; i < pcf.length; i++) {
00107                 if (Modifier.isPublic(pcf[i].getModifiers())) {
00108                     String pn = pcf[i].getName();
00109                     // exclude serialVersionUID field for jdk1.2.1 on solaris and
00110                     // exclude JProbe$ for JProbe using
00111                     if (!pn.equals("serialVersionUID") && !pn.startsWith("JProbe$")) {
00112                         FieldDesc fd = (FieldDesc) fieldDesc.get(pn);
00113                         if (fd == null) {
00114                             throw new DeploymentDescException("public field " + pn + " of primkey-class is not listed as cmp-field in bean " + this.ejbName);
00115                         }
00116                         fd.setPrimaryKey(true);
00117                     }
00118                 }
00119             }
00120         }
00121         String packageName = BeanNaming.getPackageName(getEjbClass().getName());
00122         derivedBeanName = new String("JOnAS" + getIdentifier() + "Bean");
00123         fullDerivedBeanName = BeanNaming.getClassName(packageName, derivedBeanName);
00124 
00125         // automatic-pk
00126         // used of specific tag automatic-pk is deprecated so nothing in this code was about jdbc-mapping
00127         String primkeytype = ent.getPrimKeyClass();
00128         if ((jm != null) && (jm.getAutomaticPk() != null)) {
00129             // optional parameter automatic-pk
00130             jdbcAutomaticPk = jm.getAutomaticPk().equalsIgnoreCase("true");
00131             if (jdbcAutomaticPk && pkObjectType) {
00132                 // You can't use specific tag 'automatic-pk' with prim-key-type=java.lang.Object
00133                 throw new DeploymentDescException("Don't use specific tag 'automatic-pk' with prim-key-type=java.lang.Object in bean " + ent.getEjbName());
00134             }
00135         }
00136         if (pkObjectType && ent.getPrimkeyField() != null) {
00137              throw new DeploymentDescException("'prim-key-field' must not be set if your prim-key-type was java.lang.Object in bean " + ent.getEjbName());
00138         }
00139         if (this.isAutomaticPk()) { // Check if prim-key-class type is Integer or java.lang.Object
00140             if (!(primkeytype.equalsIgnoreCase("java.lang.Integer")
00141                     || primkeytype.equalsIgnoreCase("Integer"))) {
00142                 throw new DeploymentDescException("You must used java.lang.Integer type for your auto-generate primary key field in bean " + ent.getEjbName());
00143             }
00144         }
00145     }
00146 
00153     public FieldDesc getCmpFieldDesc(String fieldName) {
00154         return (FieldDesc) fieldDesc.get(fieldName);
00155     }
00156 
00163     public boolean hasSimplePkField() {
00164         return primaryKeyField != null;
00165     }
00166 
00167     // TODO remove this method and keep only the hasSimplePkField method
00168     public boolean hasPrimaryKeyField() {
00169         return hasSimplePkField();
00170     }
00171 
00176     public FieldDesc getSimplePkField() {
00177         FieldDesc fd = (FieldDesc) fieldDesc.get(getSimplePkFieldName());
00178         return fd;
00179     }
00180 
00185     public String getSimplePkFieldName() {
00186         if (primaryKeyField == null) {
00187             throw new Error("No primary key field defined for bean " + this.ejbName);
00188         }
00189         return primaryKeyField;
00190     }
00191 
00192     // TODO remove this method and keep only the getSimplePkFieldName() method
00193     public String getPrimaryKeyFieldName() {
00194         return getSimplePkFieldName();
00195     }
00196 
00201     public String getJdbcAutomaticPkFieldName() {
00202         return jdbcAutomaticPkFieldName;
00203     }
00204 
00205     public Iterator getCmpFieldDescIterator() {
00206         return fieldDesc.values().iterator();
00207     }
00212     public String toString() {
00213         StringBuffer ret = new StringBuffer();
00214         ret.append(super.toString());
00215         for (Iterator i = fieldDesc.keySet().iterator(); i.hasNext();) {
00216             String f = (String) i.next();
00217             FieldDesc fd = (FieldDesc) fieldDesc.get(f);
00218             ret.append("\ngetCmpFieldDesc(" + f + ")=" + fd.getClass().getName());
00219             ret.append(fd.toString());
00220         }
00221         if (hasPrimaryKeyField()) {
00222             ret.append("\ngetPrimaryKeyField()=" + getPrimaryKeyFieldName());
00223         }
00224         return ret.toString();
00225     }
00226 
00231     protected FieldDesc newFieldDescInstance() {
00232         return new FieldJdbcDesc();
00233     }
00234 }
00235 

Generated on Tue Feb 15 15:05:44 2005 for JOnAS by  doxygen 1.3.9.1