1 23 24 package org.objectweb.jorm.mapper.rdb.generator; 25 26 import org.objectweb.jorm.metainfo.api.NameDef; 27 import org.objectweb.jorm.metainfo.api.PrimitiveElement; 28 import org.objectweb.jorm.api.PException; 29 30 import java.util.HashMap ; 31 import java.util.Iterator ; 32 import java.util.Map ; 33 import java.util.Set ; 34 35 43 public class RdbGenRef { 44 47 public String cnName = null; 48 51 public int cnId = -1; 52 55 public String cnPackage = null; 56 60 public Map cnFieldColumns = null; 61 66 public RdbGenColumn refColumn = null; 67 68 public NameDef nd = null; 69 70 74 public String fieldName = null; 75 76 public RdbGenJoin join = null; 77 78 public String getFieldName() { 79 return fieldName; 80 } 81 82 public NameDef getNd() { 83 return nd; 84 } 85 public String getCnName() { 86 return cnName; 87 } 88 89 public int getCnId() { 90 return cnId; 91 } 92 93 public String getCnPackage() { 94 return cnPackage; 95 } 96 97 public String getCnFQName() { 98 return (cnPackage == null ? cnName : cnPackage + "." + cnName); 99 } 100 101 public Map getCnFieldColumns() { 102 return cnFieldColumns; 103 } 104 105 public RdbGenColumn getRefColumn() { 106 return refColumn; 107 } 108 109 115 public String getFieldName(RdbGenColumn rgc) { 116 if (rgc.joinCol != null) { 117 return getFieldName(rgc.joinCol); 118 } 119 if (refColumn != null) { 120 return null; 121 } 122 Iterator it = cnFieldColumns.keySet().iterator(); 123 while (it.hasNext()) { 124 String res = (String ) it.next(); 125 if (cnFieldColumns.get(res) == rgc) { 126 return res; 127 } 128 } 129 return null; 130 } 131 132 public String getClassFieldName(String compositeFieldName) { 133 return (String ) nd.getNameRef().getProjection().get(compositeFieldName); 134 } 135 136 public String getAccessorValue(boolean isSpecific) { 137 if (isSpecific) { 138 return "paccessor.paGet" 139 + RdbGenInfos.commonHelper.upperFL(fieldName) + "(conn)"; 140 } else { 141 return "paccessorGen.paGetRefField(" 142 + fieldName + "_NAME, conn)"; 143 } 144 145 } 146 147 public RdbGenJoin getJoin(RdbGenColumn rgc) throws PException { 148 return getJoin(rgc, nd.getFieldName()); 149 } 150 151 public RdbGenJoin getJoin(RdbGenColumn rgc, 152 String fn) throws PException { 153 try { 154 if (rgc.pes == null) { 155 throw new PException("No join needed to reach the field " + fn 156 + " over the " + rgc.columnName + " column."); 157 } 158 int i = 0; 159 while (i < rgc.pes.size() 160 && !fn.equals(((PrimitiveElement) rgc.pes.get(i)).getName())) { 161 i++; 162 } 163 if (i < rgc.pes.size()) { 164 RdbGenJoin rgj = (RdbGenJoin) rgc.joins.get(i); 165 if (rgj == null) { 166 new PException("No RdbGenJoin found for the field '" 167 + fn + "' and the column '" 168 + rgc.columnName + "'.").printStackTrace(); 169 } 170 return rgj; 171 } else { 172 throw new PException("Field " + fn + " is not mapped over the " 173 + rgc.columnName + " column."); 174 } 175 } catch (RuntimeException e) { 176 e.printStackTrace(); 177 throw e; 178 } 179 } 180 public Set getcnField2ColumnName() { 181 Map p = new HashMap (); 182 try { 183 if (nd.isFieldName()) { 184 p.put("", refColumn.table.tableName + "." + refColumn.columnName); 187 } else if (nd.isNameRef()) { 188 189 for (Iterator iter = cnFieldColumns.entrySet().iterator(); iter.hasNext();) { 190 Map.Entry me = (Map.Entry ) iter.next(); 191 RdbGenColumn rgc = (RdbGenColumn) me.getValue(); 192 p.put(me.getKey(), rgc.table.tableName + "." + rgc.columnName); 195 } 196 } 197 }catch(RuntimeException e){ 198 System.out.println(this); 199 e.printStackTrace();throw e;}; 200 return p.entrySet(); 201 202 } 203 204 public String toString() { 205 StringBuffer sb = new StringBuffer (super.toString()); 206 sb.append(" / cnId=").append(cnId); 207 sb.append(" / cnPackage=").append(cnPackage); 208 sb.append(" / cnFieldColumns=").append(cnFieldColumns); 209 sb.append(" / refColumn=").append(refColumn); 210 sb.append(" / nd=").append(nd); 211 if (nd != null) { 212 if (nd.isNameRef()) { 213 sb.append(" / nd.nr.cn=").append(nd.getNameRef().getCompositeName()); 214 sb.append(" / nd.nr.cn=").append(nd.getNameRef().getProjection()); 215 } 216 } 217 sb.append(" / fieldName=").append(fieldName); 218 sb.append(" / join=").append(join); 219 return sb.toString(); 220 } 221 } 222 | Popular Tags |