1 19 20 package org.apache.cayenne.map; 21 22 import java.util.Iterator ; 23 24 import org.apache.cayenne.dba.TypesMapping; 25 import org.apache.cayenne.map.event.AttributeEvent; 26 import org.apache.cayenne.map.event.DbAttributeListener; 27 import org.apache.cayenne.util.Util; 28 import org.apache.cayenne.util.XMLEncoder; 29 30 36 public class DbAttribute extends Attribute { 37 38 41 protected int type = TypesMapping.NOT_DEFINED; 42 43 46 protected boolean mandatory; 47 48 51 protected boolean primaryKey; 52 53 59 protected boolean generated; 60 61 protected int maxLength = -1; 63 64 67 protected int scale = -1; 68 69 72 protected int attributePrecision = -1; 74 75 public DbAttribute() { 76 super(); 77 } 78 79 public DbAttribute(String name) { 80 super(name); 81 } 82 83 public DbAttribute(String name, int type, DbEntity entity) { 84 this.setName(name); 85 this.setType(type); 86 this.setEntity(entity); 87 } 88 89 94 public void encodeAsXML(XMLEncoder encoder) { 95 96 encoder.print("<db-attribute name=\""); 97 encoder.print(Util.encodeXmlAttribute(getName())); 98 encoder.print('\"'); 99 100 String type = TypesMapping.getSqlNameByType(getType()); 101 if (type != null) { 102 encoder.print(" type=\"" + type + '\"'); 103 } 104 105 if (isPrimaryKey()) { 106 encoder.print(" isPrimaryKey=\"true\""); 107 108 if (isGenerated()) { 110 encoder.print(" isGenerated=\"true\""); 111 } 112 } 113 114 if (isMandatory()) { 115 encoder.print(" isMandatory=\"true\""); 116 } 117 118 if (getMaxLength() > 0) { 119 encoder.print(" length=\""); 120 encoder.print(getMaxLength()); 121 encoder.print('\"'); 122 } 123 124 if (getScale() > 0) { 125 encoder.print(" scale=\""); 126 encoder.print(getScale()); 127 encoder.print('\"'); 128 } 129 130 if (getAttributePrecision() > 0) { 131 encoder.print(" attributePrecision=\""); 132 encoder.print(getAttributePrecision()); 133 encoder.print('\"'); 134 } 135 136 encoder.println("/>"); 137 } 138 139 public String getAliasedName(String alias) { 140 return (alias != null) ? alias + '.' + this.getName() : this.getName(); 141 } 142 143 148 public int getType() { 149 return type; 150 } 151 152 157 public void setType(int type) { 158 this.type = type; 159 } 160 161 public boolean isPrimaryKey() { 162 return primaryKey; 163 } 164 165 171 public boolean isForeignKey() { 172 String name = getName(); 173 if (name == null) { 174 return false; 176 } 177 178 Iterator relationships = getEntity().getRelationships().iterator(); 179 while (relationships.hasNext()) { 180 DbRelationship relationship = (DbRelationship) relationships.next(); 181 Iterator joins = relationship.getJoins().iterator(); 182 183 while (joins.hasNext()) { 184 DbJoin join = (DbJoin) joins.next(); 185 if (name.equals(join.getSourceName())) { 186 DbAttribute target = join.getTarget(); 187 if (target != null && target.isPrimaryKey()) { 188 return true; 189 } 190 } 191 } 192 } 193 194 return false; 195 } 196 197 200 public void setPrimaryKey(boolean primaryKey) { 201 if (this.primaryKey != primaryKey) { 202 this.primaryKey = primaryKey; 203 204 Entity e = this.getEntity(); 205 if (e instanceof DbAttributeListener) { 206 ((DbAttributeListener) e).dbAttributeChanged(new AttributeEvent( 207 this, 208 this, 209 e)); 210 } 211 } 212 } 213 214 public boolean isMandatory() { 215 return mandatory; 216 } 217 218 public void setMandatory(boolean mandatory) { 219 this.mandatory = mandatory; 220 } 221 222 225 public int getMaxLength() { 226 return maxLength; 227 } 228 229 232 public void setMaxLength(int maxLength) { 233 this.maxLength = maxLength; 234 } 235 236 242 public int getPrecision() { 243 return getScale(); 244 } 245 246 250 public void setPrecision(int i) { 251 setScale(i); 252 } 253 254 260 public boolean isGenerated() { 261 return generated; 262 } 263 264 269 public void setGenerated(boolean generated) { 270 if (this.generated != generated) { 271 this.generated = generated; 272 273 Entity e = this.getEntity(); 274 if (e instanceof DbAttributeListener) { 275 ((DbAttributeListener) e).dbAttributeChanged(new AttributeEvent( 276 this, 277 this, 278 e)); 279 } 280 } 281 } 282 283 286 public int getAttributePrecision() { 287 return attributePrecision; 288 } 289 290 293 public void setAttributePrecision(int attributePrecision) { 294 this.attributePrecision = attributePrecision; 295 } 296 297 303 public int getScale() { 304 return scale; 305 } 306 307 310 public void setScale(int scale) { 311 this.scale = scale; 312 } 313 } 314 | Popular Tags |