1 56 57 package org.objectstyle.cayenne.map; 58 59 import java.util.Iterator ; 60 61 import org.objectstyle.cayenne.dba.TypesMapping; 62 import org.objectstyle.cayenne.map.event.AttributeEvent; 63 import org.objectstyle.cayenne.map.event.DbAttributeListener; 64 import org.objectstyle.cayenne.util.Util; 65 import org.objectstyle.cayenne.util.XMLEncoder; 66 67 73 public class DbAttribute extends Attribute { 74 77 protected int type = TypesMapping.NOT_DEFINED; 78 79 82 protected boolean mandatory; 83 84 87 protected boolean primaryKey; 88 89 95 protected boolean generated; 96 97 protected int maxLength = -1; 99 100 protected int precision = -1; 102 103 public DbAttribute() { 104 super(); 105 } 106 107 public DbAttribute(String name) { 108 super(name); 109 } 110 111 public DbAttribute(String name, int type, DbEntity entity) { 112 this.setName(name); 113 this.setType(type); 114 this.setEntity(entity); 115 } 116 117 122 public void encodeAsXML(XMLEncoder encoder) { 123 124 encoder.print("<db-attribute name=\""); 125 encoder.print(Util.encodeXmlAttribute(getName())); 126 encoder.print('\"'); 127 128 String type = TypesMapping.getSqlNameByType(getType()); 129 if (type != null) { 130 encoder.print(" type=\"" + type + '\"'); 131 } 132 133 if (isPrimaryKey()) { 135 encoder.print(" isPrimaryKey=\"true\""); 136 } 137 138 if (isMandatory()) { 139 encoder.print(" isMandatory=\"true\""); 140 } 141 142 if (isGenerated()) { 143 encoder.print(" isGenerated=\"true\""); 144 } 145 146 if (getMaxLength() > 0) { 147 encoder.print(" length=\""); 148 encoder.print(getMaxLength()); 149 encoder.print('\"'); 150 } 151 152 if (getPrecision() > 0) { 153 encoder.print(" precision=\""); 154 encoder.print(getPrecision()); 155 encoder.print('\"'); 156 } 157 158 encoder.println("/>"); 159 } 160 161 public String getAliasedName(String alias) { 162 return (alias != null) ? alias + '.' + this.getName() : this.getName(); 163 } 164 165 170 public int getType() { 171 return type; 172 } 173 174 179 public void setType(int type) { 180 this.type = type; 181 } 182 183 public boolean isPrimaryKey() { 184 return primaryKey; 185 } 186 187 193 public boolean isForeignKey() { 194 String name = getName(); 195 if (name == null) { 196 return false; 198 } 199 200 Iterator relationships = getEntity().getRelationships().iterator(); 201 while (relationships.hasNext()) { 202 DbRelationship relationship = (DbRelationship) relationships.next(); 203 Iterator joins = relationship.getJoins().iterator(); 204 205 while (joins.hasNext()) { 206 DbJoin join = (DbJoin) joins.next(); 207 if (name.equals(join.getSourceName())) { 208 DbAttribute target = join.getTarget(); 209 if (target != null && target.isPrimaryKey()) { 210 return true; 211 } 212 } 213 } 214 } 215 216 return false; 217 } 218 219 222 public void setPrimaryKey(boolean primaryKey) { 223 if (this.primaryKey != primaryKey) { 224 this.primaryKey = primaryKey; 225 226 Entity e = this.getEntity(); 227 if (e instanceof DbAttributeListener) { 228 ((DbAttributeListener) e).dbAttributeChanged(new AttributeEvent( 229 this, 230 this, 231 e)); 232 } 233 } 234 } 235 236 public boolean isMandatory() { 237 return mandatory; 238 } 239 240 public void setMandatory(boolean mandatory) { 241 this.mandatory = mandatory; 242 } 243 244 247 public int getMaxLength() { 248 return maxLength; 249 } 250 251 254 public void setMaxLength(int maxLength) { 255 this.maxLength = maxLength; 256 } 257 258 261 public int getPrecision() { 262 return precision; 263 } 264 265 266 public void setPrecision(int precision) { 267 this.precision = precision; 268 } 269 270 276 public boolean isGenerated() { 277 return generated; 278 } 279 280 285 public void setGenerated(boolean generated) { 286 if (this.generated != generated) { 287 this.generated = generated; 288 289 Entity e = this.getEntity(); 290 if (e instanceof DbAttributeListener) { 291 ((DbAttributeListener) e).dbAttributeChanged(new AttributeEvent( 292 this, 293 this, 294 e)); 295 } 296 } 297 } 298 } 299 | Popular Tags |