1 19 package org.apache.cayenne.jpa.map; 20 21 import java.util.ArrayList ; 22 import java.util.Collection ; 23 24 import org.apache.cayenne.util.TreeNodeChild; 25 26 31 public class JpaAttributes { 32 33 protected Collection <JpaId> ids; 34 protected JpaEmbeddedId embeddedId; 35 protected Collection <JpaBasic> basicAttributes; 36 protected Collection <JpaVersion> versionAttributes; 37 protected Collection <JpaManyToOne> manyToOneRelationships; 38 protected Collection <JpaOneToMany> oneToManyRelationships; 39 protected Collection <JpaOneToOne> oneToOneRelationships; 40 protected Collection <JpaManyToMany> manyToManyRelationships; 41 protected Collection <JpaEmbedded> embeddedAttributes; 42 protected Collection <JpaTransient> transientAttributes; 43 44 public JpaAttribute getAttribute(String name) { 45 if (name == null) { 46 return null; 47 } 48 49 if (embeddedId != null && name.equals(embeddedId.getName())) { 50 return embeddedId; 51 } 52 53 JpaAttribute attribute; 54 55 attribute = getId(name); 56 if (attribute != null) { 57 return attribute; 58 } 59 60 attribute = getBasicAttribute(name); 61 if (attribute != null) { 62 return attribute; 63 } 64 65 attribute = getVersionAttribute(name); 66 if (attribute != null) { 67 return attribute; 68 } 69 70 attribute = getManyToOneRelationship(name); 71 if (attribute != null) { 72 return attribute; 73 } 74 75 attribute = getOneToManyRelationship(name); 76 if (attribute != null) { 77 return attribute; 78 } 79 80 attribute = getOneToOneRelationship(name); 81 if (attribute != null) { 82 return attribute; 83 } 84 85 attribute = getManyToManyRelationship(name); 86 if (attribute != null) { 87 return attribute; 88 } 89 90 attribute = getTransientAttribute(name); 91 if (attribute != null) { 92 return attribute; 93 } 94 95 attribute = getEmbeddedAttribute(name); 96 if (attribute != null) { 97 return attribute; 98 } 99 100 return null; 101 } 102 103 106 public int size() { 107 int size = 0; 108 109 if (embeddedId != null) { 110 size++; 111 } 112 113 if (ids != null) { 114 size += ids.size(); 115 } 116 if (basicAttributes != null) { 117 size += basicAttributes.size(); 118 } 119 if (versionAttributes != null) { 120 size += versionAttributes.size(); 121 } 122 if (manyToOneRelationships != null) { 123 size += manyToOneRelationships.size(); 124 } 125 if (oneToManyRelationships != null) { 126 size += oneToManyRelationships.size(); 127 } 128 if (oneToOneRelationships != null) { 129 size += oneToOneRelationships.size(); 130 } 131 if (manyToManyRelationships != null) { 132 size += manyToManyRelationships.size(); 133 } 134 if (embeddedAttributes != null) { 135 size += embeddedAttributes.size(); 136 } 137 if (transientAttributes != null) { 138 size += transientAttributes.size(); 139 } 140 return size; 141 } 142 143 public JpaId getId(String idName) { 144 if (idName == null) { 145 throw new IllegalArgumentException ("Null id name"); 146 } 147 148 if (ids != null) { 149 for (JpaId id : ids) { 150 if (idName.equals(id.getName())) { 151 return id; 152 } 153 } 154 } 155 156 return null; 157 } 158 159 162 public JpaBasic getBasicAttribute(String attributeName) { 163 if (attributeName == null) { 164 throw new IllegalArgumentException ("Null attribute name"); 165 } 166 167 if (basicAttributes != null) { 168 for (JpaBasic attribute : basicAttributes) { 169 if (attributeName.equals(attribute.getName())) { 170 return attribute; 171 } 172 } 173 } 174 175 return null; 176 } 177 178 public JpaManyToOne getManyToOneRelationship(String attributeName) { 179 if (attributeName == null) { 180 throw new IllegalArgumentException ("Null attribute name"); 181 } 182 183 if (manyToOneRelationships != null) { 184 for (JpaManyToOne attribute : manyToOneRelationships) { 185 if (attributeName.equals(attribute.getName())) { 186 return attribute; 187 } 188 } 189 } 190 191 return null; 192 } 193 194 public JpaOneToMany getOneToManyRelationship(String attributeName) { 195 if (attributeName == null) { 196 throw new IllegalArgumentException ("Null attribute name"); 197 } 198 199 if (oneToManyRelationships != null) { 200 for (JpaOneToMany attribute : oneToManyRelationships) { 201 if (attributeName.equals(attribute.getName())) { 202 return attribute; 203 } 204 } 205 } 206 207 return null; 208 } 209 210 @TreeNodeChild(type = JpaId.class) 211 public Collection <JpaId> getIds() { 212 if (ids == null) { 213 ids = new ArrayList <JpaId>(); 214 } 215 216 return ids; 217 } 218 219 @TreeNodeChild 220 public JpaEmbeddedId getEmbeddedId() { 221 return embeddedId; 222 } 223 224 public void setEmbeddedId(JpaEmbeddedId embeddedId) { 225 this.embeddedId = embeddedId; 226 } 227 228 @TreeNodeChild(type = JpaBasic.class) 229 public Collection <JpaBasic> getBasicAttributes() { 230 if (basicAttributes == null) { 231 basicAttributes = new ArrayList <JpaBasic>(); 232 } 233 return basicAttributes; 234 } 235 236 @TreeNodeChild(type = JpaEmbedded.class) 237 public Collection <JpaEmbedded> getEmbeddedAttributes() { 238 if (embeddedAttributes == null) { 239 embeddedAttributes = new ArrayList <JpaEmbedded>(); 240 } 241 return embeddedAttributes; 242 } 243 244 public JpaEmbedded getEmbeddedAttribute(String attributeName) { 245 if (attributeName == null) { 246 throw new IllegalArgumentException ("Null attribute name"); 247 } 248 249 if (embeddedAttributes != null) { 250 for (JpaEmbedded attribute : embeddedAttributes) { 251 if (attributeName.equals(attribute.getName())) { 252 return attribute; 253 } 254 } 255 } 256 257 return null; 258 } 259 260 @TreeNodeChild(type = JpaManyToMany.class) 261 public Collection <JpaManyToMany> getManyToManyRelationships() { 262 if (manyToManyRelationships == null) { 263 manyToManyRelationships = new ArrayList <JpaManyToMany>(); 264 } 265 return manyToManyRelationships; 266 } 267 268 public JpaManyToMany getManyToManyRelationship(String attributeName) { 269 if (attributeName == null) { 270 throw new IllegalArgumentException ("Null attribute name"); 271 } 272 273 if (manyToManyRelationships != null) { 274 for (JpaManyToMany attribute : manyToManyRelationships) { 275 if (attributeName.equals(attribute.getName())) { 276 return attribute; 277 } 278 } 279 } 280 281 return null; 282 } 283 284 @TreeNodeChild(type = JpaManyToOne.class) 285 public Collection <JpaManyToOne> getManyToOneRelationships() { 286 if (manyToOneRelationships == null) { 287 manyToOneRelationships = new ArrayList <JpaManyToOne>(); 288 } 289 return manyToOneRelationships; 290 } 291 292 @TreeNodeChild(type = JpaOneToMany.class) 293 public Collection <JpaOneToMany> getOneToManyRelationships() { 294 if (oneToManyRelationships == null) { 295 oneToManyRelationships = new ArrayList <JpaOneToMany>(); 296 } 297 return oneToManyRelationships; 298 } 299 300 @TreeNodeChild(type = JpaOneToOne.class) 301 public Collection <JpaOneToOne> getOneToOneRelationships() { 302 if (oneToOneRelationships == null) { 303 oneToOneRelationships = new ArrayList <JpaOneToOne>(); 304 } 305 return oneToOneRelationships; 306 } 307 308 public JpaOneToOne getOneToOneRelationship(String attributeName) { 309 if (attributeName == null) { 310 throw new IllegalArgumentException ("Null attribute name"); 311 } 312 313 if (oneToOneRelationships != null) { 314 for (JpaOneToOne attribute : oneToOneRelationships) { 315 if (attributeName.equals(attribute.getName())) { 316 return attribute; 317 } 318 } 319 } 320 321 return null; 322 } 323 324 @TreeNodeChild(type = JpaTransient.class) 325 public Collection <JpaTransient> getTransientAttributes() { 326 if (transientAttributes == null) { 327 transientAttributes = new ArrayList <JpaTransient>(); 328 } 329 return transientAttributes; 330 } 331 332 335 public JpaTransient getTransientAttribute(String attributeName) { 336 if (attributeName == null) { 337 throw new IllegalArgumentException ("Null attribute name"); 338 } 339 340 if (transientAttributes != null) { 341 for (JpaTransient attribute : transientAttributes) { 342 if (attributeName.equals(attribute.getName())) { 343 return attribute; 344 } 345 } 346 } 347 348 return null; 349 } 350 351 @TreeNodeChild(type = JpaVersion.class) 352 public Collection <JpaVersion> getVersionAttributes() { 353 if (versionAttributes == null) { 354 versionAttributes = new ArrayList <JpaVersion>(); 355 } 356 return versionAttributes; 357 } 358 359 362 public JpaVersion getVersionAttribute(String attributeName) { 363 if (attributeName == null) { 364 throw new IllegalArgumentException ("Null attribute name"); 365 } 366 367 if (versionAttributes != null) { 368 for (JpaVersion attribute : versionAttributes) { 369 if (attributeName.equals(attribute.getName())) { 370 return attribute; 371 } 372 } 373 } 374 375 return null; 376 } 377 } 378 | Popular Tags |