EjbRelationshipRoleDesc.java

00001 
00025 package org.objectweb.jonas_ejb.deployment.api;
00026 
00027 import java.util.HashMap;
00028 import java.util.Iterator;
00029 
00030 import org.objectweb.jonas_ejb.deployment.xml.EjbRelationshipRole;
00031 import org.objectweb.jonas_ejb.deployment.xml.ForeignKeyJdbcMapping;
00032 import org.objectweb.jonas_ejb.deployment.xml.JonasEjbRelationshipRole;
00033 import org.objectweb.jonas_lib.deployment.api.DeploymentDescException;
00034 import org.objectweb.util.monolog.api.BasicLevel;
00035 import org.objectweb.util.monolog.api.Logger;
00036 
00044 public class EjbRelationshipRoleDesc {
00045 
00046     private Logger logger = null;
00047 
00051     public static final byte INDEX = 1;
00052 
00057     public static final byte SOURCE = 2;
00058 
00063     public static final byte TARGET = 4;
00064 
00068     public static final byte OOU = 0;
00069 
00070     public static final byte OOB = 1;
00071 
00072     public static final byte OMU = 2;
00073 
00074     public static final byte OMB = OMU + OOB;
00075 
00076     public static final byte MOU = 4;
00077 
00078     public static final byte MOB = MOU + OOB;
00079 
00080     public static final byte MMU = OMU + MOU;
00081 
00082     public static final byte MMB = MMU + OOB;
00083 
00084     private String rsrName;
00085 
00086     private String ejbSourceName;
00087 
00088     private EjbRelationDesc ejbRelationDesc;
00089 
00090     private EntityCmp2Desc sourceEntityCmp2Desc;
00091 
00092     private boolean isSourceMultiple;
00093 
00094     private EntityCmp2Desc targetEntityCmp2Desc;
00095 
00096     private boolean isTargetMultiple;
00097 
00098     private boolean isSlave;
00099 
00100     protected String cmrFieldName = null;
00101 
00102     protected Class cmrFieldType = null;
00103 
00104     protected boolean isJOnASCMR = false;
00105 
00106     private byte relationType = -1;
00107 
00108     private boolean mustCascade;
00109 
00110     // Zeus objects for the associated mapping information (needed in
00111     // fillMappingInfo);
00112     // They may be null.
00113     private JonasEjbRelationshipRole jSourceRsRole = null;
00114 
00115     // mapping information build by fillMappingInfo and
00116     // fillMappingInfoWithDefault
00117     private HashMap foreignKeyMap = new HashMap();
00118 
00119     private boolean hasJdbcMapping = false;
00120 
00131     public EjbRelationshipRoleDesc(EjbRelationDesc rd, String name, EjbRelationshipRole role,
00132             JonasEjbRelationshipRole jrole, EjbRelationshipRole opposite, boolean isSlave, Logger logger)
00133             throws DeploymentDescException {
00134 
00135         this.logger = logger;
00136         ejbRelationDesc = rd;
00137         this.isSlave = isSlave;
00138         rsrName = name;
00139         mustCascade = opposite.isCascadeDelete();
00140         ejbSourceName = role.getRelationshipRoleSource().getEjbName();
00141 
00142         // mutiplicity is One or Many
00143         if (opposite.getMultiplicity().equalsIgnoreCase("Many")) {
00144             isTargetMultiple = true;
00145             if (role.isCascadeDelete()) {
00146                 throw new DeploymentDescException("Cascade delete not allowed for relationshipRole for relationship '"
00147                         + rd.getName() + "(because opposite role has a multiplicity of Many)");
00148             }
00149         } else if (opposite.getMultiplicity().equalsIgnoreCase("One")) {
00150             isTargetMultiple = false;
00151         } else {
00152             throw new DeploymentDescException("Invalid multiplicity value for relationshipRole for relationship '"
00153                     + rd.getName() + "'(must be One or Many)");
00154         }
00155         if (role.getMultiplicity().equalsIgnoreCase("Many")) {
00156             isSourceMultiple = true;
00157         } else if (role.getMultiplicity().equalsIgnoreCase("One")) {
00158             isSourceMultiple = false;
00159         } else {
00160             throw new DeploymentDescException("Invalid multiplicity value for relationshipRole for relationship '"
00161                     + rd.getName() + "'(must be One or Many)");
00162         }
00163 
00164         // store cmr field if any
00165         if (role.getCmrField() != null) {
00166             setCmrFieldName(role.getCmrField().getCmrFieldName());
00167             if (isTargetMultiple) {
00168                 String type = role.getCmrField().getCmrFieldType();
00169                 if (type == null) {
00170                     throw new DeploymentDescException(
00171                             "You must specify a cmr-field-type in case where the relation is 'Many' in the cmr-field '"
00172                                     + cmrFieldName + "' of bean " + ejbSourceName);
00173                 }
00174                 setCmrFieldType(type);
00175             }
00176         }
00177 
00178         // store the JonasEjbRelationshipRole
00179         jSourceRsRole = jrole;
00180     }
00181 
00187     protected void fillMappingInfo() throws DeploymentDescException {
00188         logger.log(BasicLevel.DEBUG, "" + (jSourceRsRole != null) + " for " + rsrName);
00189         if (jSourceRsRole != null) {
00190             for (Iterator i = jSourceRsRole.getForeignKeyJdbcMappingList().iterator(); i.hasNext();) {
00191                 ForeignKeyJdbcMapping fkMapping = (ForeignKeyJdbcMapping) i.next();
00192                 String fkc = fkMapping.getForeignKeyJdbcName();
00193                 String kc = null;
00194                 if (fkMapping.getKeyJdbcName() != null) {
00195                     kc = fkMapping.getKeyJdbcName();
00196                 }
00197                 if (kc == null) {
00198                     // if the target bean has a primary-key-field, this value
00199                     // may not be defined
00200                     // in this case, this is the column name of the
00201                     // primary-key-field
00202                     if (targetEntityCmp2Desc.hasSimplePkField()) {
00203                         kc = ((FieldJdbcDesc) targetEntityCmp2Desc.getSimplePkField()).getJdbcFieldName();
00204                     } else {
00205                         // error
00206                         throw new DeploymentDescException(
00207                                 "key-jdbc-name must be provided for foreign-key-jdbc-mapping " + fkc
00208                                         + " of relation-ship role " + rsrName + "of relation "
00209                                         + ejbRelationDesc.getName());
00210                     }
00211                 }
00212                 logger.log(BasicLevel.DEBUG, "explicit fk mapping = " + fkc + " for " + kc);
00213                 foreignKeyMap.put(kc, fkc);
00214             }
00215             hasJdbcMapping = true;
00216         }
00217     }
00218 
00223     protected void fillMappingInfoWithDefault() {
00224         logger.log(BasicLevel.DEBUG, "" + hasJdbcMapping);
00225         if (!hasJdbcMapping) {
00226             if (targetEntityCmp2Desc.hasSimplePkField()) {
00227                 // target entity has a simple pk (primary-key-field)
00228                 String fn = targetEntityCmp2Desc.getSimplePkFieldName();
00229                 FieldJdbcDesc fd = (FieldJdbcDesc) targetEntityCmp2Desc.getCmpFieldDesc(fn);
00230                 String kc = fd.getJdbcFieldName();
00231                 String fkc = targetEntityCmp2Desc.getAbstractSchemaName() + "_" + kc;
00232                 logger.log(BasicLevel.DEBUG, "default fk mapping = " + fkc + " for " + kc);
00233                 foreignKeyMap.put(kc, fkc);
00234             } else {
00235                 // target entity has a composite pk
00236                 for (Iterator i = targetEntityCmp2Desc.getCmpFieldDescIterator(); i.hasNext();) {
00237                     FieldJdbcDesc fd = (FieldJdbcDesc) i.next();
00238                     String kc = fd.getJdbcFieldName();
00239                     String fkc = targetEntityCmp2Desc.getAbstractSchemaName() + "_" + kc;
00240                     logger.log(BasicLevel.DEBUG, "default fk mapping = " + fkc + " for " + kc);
00241                     foreignKeyMap.put(kc, fkc);
00242                 }
00243             }
00244             hasJdbcMapping = true;
00245         }
00246     }
00247 
00252     public String getName() {
00253         return rsrName;
00254     }
00255 
00256     protected void setCmrFieldName(String name) throws DeploymentDescException {
00257         cmrFieldName = name;
00258     }
00259 
00260     protected void setCmrFieldType(String type) throws DeploymentDescException {
00261         try {
00262             cmrFieldType = Class.forName(type);
00263         } catch (ClassNotFoundException e) {
00264             throw new DeploymentDescException("class name not found for cmr-field " + cmrFieldName + " of bean "
00265                     + ejbSourceName, e);
00266         }
00267         if (!(cmrFieldType.getName().equals("java.util.Collection") || cmrFieldType.getName().equals("java.util.Set"))) {
00268             throw new DeploymentDescException("value of cmr-field-type " + cmrFieldName + " of bean " + ejbSourceName
00269                     + " should be java.util.Set or java.util.Collection if set");
00270         }
00271     }
00272 
00276     protected void setIsJOnASCmrField() {
00277         isJOnASCMR = true;
00278     }
00279 
00284     protected void setSourceBean(EntityCmp2Desc led) {
00285         sourceEntityCmp2Desc = led;
00286     }
00287 
00292     protected void setTargetBean(EntityCmp2Desc led) {
00293         targetEntityCmp2Desc = led;
00294         if (cmrFieldType == null) {
00295             cmrFieldType = led.getLocalClass();
00296         }
00297     }
00298 
00303     public EjbRelationDesc getRelation() {
00304         return ejbRelationDesc;
00305     }
00306 
00311     public EjbRelationshipRoleDesc getOppositeRelationshipRole() {
00312         EjbRelationshipRoleDesc res = ejbRelationDesc.getRelationshipRole1();
00313         if (res == this) {
00314             return ejbRelationDesc.getRelationshipRole2();
00315         } else {
00316             return res;
00317         }
00318     }
00319 
00325     public String getSourceBeanName() {
00326         return ejbSourceName;
00327     }
00328 
00334     public EntityCmp2Desc getSourceBean() {
00335         return sourceEntityCmp2Desc;
00336     }
00337 
00344     public EntityCmp2Desc getTargetBean() {
00345         return targetEntityCmp2Desc;
00346     }
00347 
00352     public boolean isSourceMultiple() {
00353         return isSourceMultiple;
00354     }
00355 
00361     public boolean isTargetMultiple() {
00362         return isTargetMultiple;
00363     }
00364 
00369     public boolean mustCascade() {
00370         return mustCascade;
00371     }
00372 
00379     public boolean isJOnASCmrField() {
00380         return isJOnASCMR;
00381     }
00382 
00388     // TODO: is this method really needed (return always true??)
00389     public boolean hasCmrField() {
00390         return cmrFieldName != null;
00391     }
00392 
00397     public String getCmrFieldName() {
00398         return cmrFieldName;
00399     }
00400 
00406     public Class getCmrFieldType() {
00407         return cmrFieldType;
00408     }
00409 
00414     public byte getRelationType() {
00415         if (relationType == -1) {
00416             relationType = OOU;
00417             EjbRelationshipRoleDesc rsr2 = getOppositeRelationshipRole();
00418             if (rsr2.hasCmrField() && hasCmrField()) {
00419                 relationType += OOB;
00420             }
00421             if (isTargetMultiple()) {
00422                 relationType += OMU;
00423             }
00424             if (rsr2.isTargetMultiple()) {
00425                 relationType += MOU;
00426             }
00427         }
00428         return relationType;
00429     }
00430 
00435     public boolean hasJdbcMapping() {
00436         return hasJdbcMapping;
00437     }
00438 
00443     public boolean isSlave() {
00444         return isSlave;
00445     }
00446 
00453     public String getForeignKeyJdbcName(String jdbcFieldName) {
00454         return (String) foreignKeyMap.get(jdbcFieldName);
00455     }
00456 
00461     public String toString() {
00462         StringBuffer ret = new StringBuffer();
00463         ret.append("\ngetName() = " + getName());
00464         ret.append("\ngetRelation().getName() = " + getRelation().getName());
00465         ret.append("\ngetOppositeRelationshipRole().getName() = " + getOppositeRelationshipRole().getName());
00466         ret.append("\ngetSourceBeanName() = " + getSourceBeanName());
00467         ret.append("\ngetTargetBean().getName() = " + getTargetBean().getEjbName());
00468         ret.append("\nisSourceMultiple() = " + isSourceMultiple());
00469         ret.append("\nisTargetMultiple() = " + isTargetMultiple());
00470         ret.append("\nmustCascade() = " + mustCascade());
00471         ret.append("\nisJOnASCmrField() = " + isJOnASCmrField());
00472         ret.append("\ngetCmrFieldName() = " + getCmrFieldName());
00473         ret.append("\ngetCmrFieldType() = " + getCmrFieldType());
00474         ret.append("\ngetRelationType() = " + getRelationType());
00475         if (hasJdbcMapping()) {
00476             for (Iterator i = foreignKeyMap.keySet().iterator(); i.hasNext();) {
00477                 String key = (String) i.next();
00478                 String fkey = (String) foreignKeyMap.get(key);
00479                 ret.append("\ngetForeignKeyJdbcName(" + key + ")=" + fkey);
00480             }
00481         }
00482         return ret.toString();
00483     }
00484 
00485 }

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