1 25 26 package org.objectweb.jonas_ejb.deployment.api; 27 28 import java.lang.reflect.Field ; 29 import java.lang.reflect.Modifier ; 30 import java.util.HashMap ; 31 import java.util.Iterator ; 32 33 import org.objectweb.jonas_ejb.deployment.xml.AssemblyDescriptor; 34 import org.objectweb.jonas_ejb.deployment.xml.Entity; 35 import org.objectweb.jonas_ejb.deployment.xml.CmpField; 36 import org.objectweb.jonas_ejb.deployment.xml.JonasEntity; 37 import org.objectweb.jonas_ejb.deployment.xml.JdbcMapping; 38 import org.objectweb.jonas_ejb.lib.BeanNaming; 39 import org.objectweb.jonas_lib.deployment.api.DeploymentDescException; 40 import org.objectweb.jonas_lib.deployment.xml.JLinkedList; 41 42 48 public abstract class EntityCmpDesc extends EntityDesc { 49 50 protected HashMap fieldDesc = new HashMap (); 51 protected String primaryKeyField = null; 52 protected String jdbcAutomaticPkFieldName = null; 53 54 63 public EntityCmpDesc(ClassLoader classLoader, Entity ent, 64 AssemblyDescriptor asd, JonasEntity jEnt, 65 JLinkedList jMDRList, String fileName) 66 throws DeploymentDescException { 67 68 super(classLoader, ent, asd, jEnt, jMDRList, fileName); 69 70 for (Iterator i = ent.getCmpFieldList().iterator(); i.hasNext();) { 72 String fn = ((CmpField) i.next()).getFieldName(); 73 FieldDesc fd = this.newFieldDescInstance(); 74 fd.setName(fn); 75 fd.setPrimaryKey(false); 76 fieldDesc.put(fn, fd); 77 78 } 79 80 JdbcMapping jm = jEnt.getJdbcMapping(); 82 if ((jm != null) && (jm.getJdbcAutomaticPkFieldName() != null)) { 83 if (jm.getJdbcAutomaticPkFieldName().length() != 0) { 84 jdbcAutomaticPkFieldName = jm.getJdbcAutomaticPkFieldName(); 85 } 86 } 87 if (jdbcAutomaticPkFieldName == null) { 88 jdbcAutomaticPkFieldName = "JPK_"; 90 } 91 92 if (isUndefinedPK()) { primaryKeyField = "JONASAUTOPKFIELD"; 95 } else if (ent.getPrimkeyField() != null) { 96 primaryKeyField = ent.getPrimkeyField(); 98 FieldDesc fd = (FieldDesc) fieldDesc.get(primaryKeyField); 99 if (fd == null) { 100 throw new DeploymentDescException("primkey-field " + primaryKeyField + " is not listed as cmp-field in bean " + this.ejbName); 101 } 102 fd.setPrimaryKey(true); 103 } else { 104 Field [] pcf = primaryKeyClass.getFields(); 106 for (int i = 0; i < pcf.length; i++) { 107 if (Modifier.isPublic(pcf[i].getModifiers())) { 108 String pn = pcf[i].getName(); 109 if (!pn.equals("serialVersionUID") && !pn.startsWith("JProbe$")) { 112 FieldDesc fd = (FieldDesc) fieldDesc.get(pn); 113 if (fd == null) { 114 throw new DeploymentDescException("public field " + pn + " of primkey-class is not listed as cmp-field in bean " + this.ejbName); 115 } 116 fd.setPrimaryKey(true); 117 } 118 } 119 } 120 } 121 String packageName = BeanDesc.GENERATED_PREFIX + BeanNaming.getPackageName(getEjbClass().getName()); 122 derivedBeanName = new String ("JOnAS" + getIdentifier() + "Bean"); 123 fullDerivedBeanName = BeanNaming.getClassName(packageName, derivedBeanName); 124 125 String primkeytype = ent.getPrimKeyClass(); 128 if ((jm != null) && (jm.getAutomaticPk() != null)) { 129 jdbcAutomaticPk = jm.getAutomaticPk().equalsIgnoreCase("true"); 131 if (jdbcAutomaticPk && pkObjectType) { 132 throw new DeploymentDescException("Don't use specific tag 'automatic-pk' with prim-key-type=java.lang.Object in bean " + ent.getEjbName()); 134 } 135 } 136 if (pkObjectType && ent.getPrimkeyField() != null) { 137 throw new DeploymentDescException("'prim-key-field' must not be set if your prim-key-type was java.lang.Object in bean " + ent.getEjbName()); 138 } 139 if (this.isAutomaticPk()) { if (!(primkeytype.equalsIgnoreCase("java.lang.Integer") 141 || primkeytype.equalsIgnoreCase("Integer"))) { 142 throw new DeploymentDescException("You must used java.lang.Integer type for your auto-generate primary key field in bean " + ent.getEjbName()); 143 } 144 } 145 } 146 147 153 public FieldDesc getCmpFieldDesc(String fieldName) { 154 return (FieldDesc) fieldDesc.get(fieldName); 155 } 156 157 163 public boolean hasSimplePkField() { 164 return primaryKeyField != null; 165 } 166 167 public boolean hasPrimaryKeyField() { 169 return hasSimplePkField(); 170 } 171 172 176 public FieldDesc getSimplePkField() { 177 FieldDesc fd = (FieldDesc) fieldDesc.get(getSimplePkFieldName()); 178 return fd; 179 } 180 181 185 public String getSimplePkFieldName() { 186 if (primaryKeyField == null) { 187 throw new Error ("No primary key field defined for bean " + this.ejbName); 188 } 189 return primaryKeyField; 190 } 191 192 public String getPrimaryKeyFieldName() { 194 return getSimplePkFieldName(); 195 } 196 197 201 public String getJdbcAutomaticPkFieldName() { 202 return jdbcAutomaticPkFieldName; 203 } 204 205 public Iterator getCmpFieldDescIterator() { 206 return fieldDesc.values().iterator(); 207 } 208 212 public String toString() { 213 StringBuffer ret = new StringBuffer (); 214 ret.append(super.toString()); 215 for (Iterator i = fieldDesc.keySet().iterator(); i.hasNext();) { 216 String f = (String ) i.next(); 217 FieldDesc fd = (FieldDesc) fieldDesc.get(f); 218 ret.append("\ngetCmpFieldDesc(" + f + ")=" + fd.getClass().getName()); 219 ret.append(fd.toString()); 220 } 221 if (hasPrimaryKeyField()) { 222 ret.append("\ngetPrimaryKeyField()=" + getPrimaryKeyFieldName()); 223 } 224 return ret.toString(); 225 } 226 227 231 protected FieldDesc newFieldDescInstance() { 232 return new FieldJdbcDesc(); 233 } 234 } 235 236 | Popular Tags |