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.EjbRelation;
00031 import org.objectweb.jonas_ejb.deployment.xml.EjbRelationshipRole;
00032 import org.objectweb.jonas_ejb.deployment.xml.JonasEjbRelation;
00033 import org.objectweb.jonas_ejb.deployment.xml.JonasEjbRelationshipRole;
00034 import org.objectweb.jonas_lib.deployment.api.DeploymentDescException;
00035 import org.objectweb.util.monolog.api.Logger;
00036
00045 public class EjbRelationDesc {
00046
00047 private Logger logger = null;
00048 private String name;
00049 private String name1;
00050 private String name2;
00051 private EjbRelationshipRoleDesc relationshipRoleDesc1;
00052 private EjbRelationshipRoleDesc relationshipRoleDesc2;
00053 private String jdbcTableName = null;
00054
00055
00056
00057 private EjbRelationshipRole role1;
00058 private EjbRelationshipRole role2;
00059 JonasEjbRelation jRel = null;
00060 JonasEjbRelationshipRole jRsRole1 = null;
00061 JonasEjbRelationshipRole jRsRole2 = null;
00062
00063
00070 public EjbRelationDesc(EjbRelation er, Logger logger) throws DeploymentDescException {
00071
00072 this.logger = logger;
00073
00074 role1 = er.getEjbRelationshipRole();
00075 role2 = er.getEjbRelationshipRole2();
00076
00077 String cmr1 = "";
00078 if (role1.getCmrField() != null) {
00079 cmr1 = role1.getCmrField().getCmrFieldName();
00080 }
00081 String cmr2 = "";
00082 if (role2.getCmrField() != null) {
00083 cmr2 = role2.getCmrField().getCmrFieldName();
00084 }
00085
00086
00087
00088 name1 = role1.getEjbRelationshipRoleName();
00089 if (name1 == null || name1.length() == 0) {
00090 if (role2.getCmrField() != null) {
00091 name1 = cmr2;
00092 } else {
00093 name1 = "role1";
00094 }
00095
00096 role1.setEjbRelationshipRoleName(name1);
00097 }
00098
00099
00100
00101 name2 = role2.getEjbRelationshipRoleName();
00102 if (name2 == null || name2.length() == 0) {
00103 if (role1.getCmrField() != null) {
00104 name2 = cmr1;
00105 } else {
00106 name2 = "role2";
00107 }
00108
00109 role2.setEjbRelationshipRoleName(name2);
00110 }
00111
00112
00113 if (name1.equals(name2)) {
00114 throw new DeploymentDescException("Relation " + name + " have 2 roles with same name: " + name1);
00115 }
00116
00117
00118 String ern = er.getEjbRelationName();
00119 if (ern == null || ern.length() == 0) {
00120 name = cmr2 + "-" + cmr1;
00121 } else {
00122 name = ern;
00123 }
00124 }
00125
00131 public void setJonasInfo(JonasEjbRelation jer) throws DeploymentDescException {
00132
00133
00134 jRel = jer;
00135 HashMap table = new HashMap();
00136 if (jRel != null) {
00137 for (Iterator i = jRel.getJonasEjbRelationshipRoleList().iterator(); i.hasNext();) {
00138 JonasEjbRelationshipRole jersr = (JonasEjbRelationshipRole) i.next();
00139 String rname = jersr.getEjbRelationshipRoleName();
00140 if (!rname.equals(name1) && !rname.equals(name2)) {
00141 throw new DeploymentDescException("Invalid relationship-role-name \"" + rname + "\" for relation \"" + name + "\" in jonas-ejb-jar.xml");
00142 }
00143 table.put(rname, jersr);
00144 }
00145 }
00146 jRsRole1 = (JonasEjbRelationshipRole) table.get(name1);
00147 jRsRole2 = (JonasEjbRelationshipRole) table.get(name2);
00148
00149 relationshipRoleDesc1 = new EjbRelationshipRoleDesc(this, name1, role1, jRsRole1, role2, true, logger);
00150 relationshipRoleDesc2 = new EjbRelationshipRoleDesc(this, name2, role2, jRsRole2, role1, false, logger);
00151
00152
00153
00154 boolean r1hf = relationshipRoleDesc1.hasCmrField();
00155 boolean r2hf = relationshipRoleDesc2.hasCmrField();
00156 EjbRelationshipRoleDesc nocmr = null;
00157 EjbRelationshipRoleDesc cmr = null;
00158 if (r1hf && !r2hf) {
00159 nocmr = relationshipRoleDesc2;
00160 cmr = relationshipRoleDesc1;
00161 } else if (!r1hf && r2hf) {
00162 nocmr = relationshipRoleDesc1;
00163 cmr = relationshipRoleDesc2;
00164 }
00165 if (nocmr != null) {
00166
00167 String cmrName = name;
00168
00169
00170 for (int i = 0; i < cmrName.length(); i++) {
00171 char c = cmrName.charAt(i);
00172 if (!Character.isJavaIdentifierPart(c)) {
00173 cmrName = cmrName.replace(c, '_');
00174 }
00175 }
00176 cmrName = "jonasCMR" + cmrName;
00177
00178
00179 nocmr.setCmrFieldName(cmrName);
00180 nocmr.setIsJOnASCmrField();
00181 if (nocmr.isTargetMultiple()) {
00182 if (cmr.isTargetMultiple()) {
00183 nocmr.setCmrFieldType(cmr.cmrFieldType.getName());
00184 } else {
00185 nocmr.setCmrFieldType("java.util.Collection");
00186 }
00187 }
00188 }
00189 }
00190
00195 protected void fillMappingInfo() throws DeploymentDescException {
00196 if (jRel != null) {
00197 if (jRel.getJdbcTableName() != null) {
00198 if (jRel.getJdbcTableName().length() != 0) {
00199 jdbcTableName = jRel.getJdbcTableName();
00200 }
00201 }
00202 relationshipRoleDesc1.fillMappingInfo();
00203 relationshipRoleDesc2.fillMappingInfo();
00204 }
00205 }
00206
00211 protected void fillMappingInfoWithDefault() {
00212 if (!hasJdbcTable()) {
00213 if (getRelationshipRole1().isTargetMultiple()
00214 && getRelationshipRole2().isTargetMultiple()) {
00215
00216 jdbcTableName = getRelationshipRole1().getSourceBean().getAbstractSchemaName().toUpperCase()
00217 + "_" + getRelationshipRole2().getSourceBean().getAbstractSchemaName().toUpperCase();
00218 }
00219 }
00220 if (!getRelationshipRole1().isSourceMultiple()
00221 && !getRelationshipRole2().isSourceMultiple()) {
00222
00223 if (!getRelationshipRole1().hasJdbcMapping()
00224 && !getRelationshipRole2().hasJdbcMapping()) {
00225 if (!getRelationshipRole1().isJOnASCmrField()
00226 && !getRelationshipRole2().isJOnASCmrField()) {
00227
00228 getRelationshipRole1().fillMappingInfoWithDefault();
00229 } else {
00230 if (!getRelationshipRole1().isJOnASCmrField()) {
00231
00232 getRelationshipRole1().fillMappingInfoWithDefault();
00233 } else {
00234
00235 getRelationshipRole2().fillMappingInfoWithDefault();
00236 }
00237 }
00238 }
00239 } else if (getRelationshipRole1().isSourceMultiple()
00240 && getRelationshipRole2().isSourceMultiple()) {
00241
00242 getRelationshipRole1().fillMappingInfoWithDefault();
00243 getRelationshipRole2().fillMappingInfoWithDefault();
00244 } else {
00245
00246 if (getRelationshipRole1().isSourceMultiple()) {
00247
00248 getRelationshipRole1().fillMappingInfoWithDefault();
00249 } else {
00250
00251 getRelationshipRole2().fillMappingInfoWithDefault();
00252 }
00253 }
00254 }
00255
00260 public String getName() {
00261 return name;
00262 }
00263
00268 public EjbRelationshipRoleDesc getRelationshipRole1() {
00269 return relationshipRoleDesc1;
00270 }
00271
00276 public EjbRelationshipRoleDesc getRelationshipRole2() {
00277 return relationshipRoleDesc2;
00278 }
00279
00284 public boolean hasJdbcTable() {
00285 return (jdbcTableName != null);
00286 }
00287
00292 public String getJdbcTableName() {
00293 return jdbcTableName;
00294 }
00295
00300 public String toString() {
00301 StringBuffer ret = new StringBuffer();
00302 ret.append("\ngetName()=" + getName());
00303 if (hasJdbcTable()) {
00304 ret.append("\ngetJdbcTableName() = " + getJdbcTableName());
00305 }
00306 ret.append("\ngetRelationshipRole1() = " + getRelationshipRole1());
00307 ret.append("\ngetRelationshipRole2() = " + getRelationshipRole2());
00308 return ret.toString();
00309 }
00310 }