Coverage Report - net.wotonomy.access.EOEntity
 
Classes in this File Line Coverage Branch Coverage Complexity
EOEntity
0% 
0% 
2.481
 
 1  
 /*
 2  
  Wotonomy: OpenStep design patterns for pure Java applications.
 3  
  Copyright (C) 2001 Michael Powers
 4  
 
 5  
  This library is free software; you can redistribute it and/or
 6  
  modify it under the terms of the GNU Lesser General Public
 7  
  License as published by the Free Software Foundation; either
 8  
  version 2.1 of the License, or (at your option) any later version.
 9  
 
 10  
  This library is distributed in the hope that it will be useful,
 11  
  but WITHOUT ANY WARRANTY; without even the implied warranty of
 12  
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 13  
  Lesser General Public License for more details.
 14  
 
 15  
  You should have received a copy of the GNU Lesser General Public
 16  
  License along with this library; if not, see http://www.gnu.org
 17  
  */
 18  
 package net.wotonomy.access;
 19  
 
 20  
 import java.io.File;
 21  
 import java.io.FileInputStream;
 22  
 import java.io.IOException;
 23  
 import java.util.Map;
 24  
 
 25  
 import net.wotonomy.control.EOAndQualifier;
 26  
 import net.wotonomy.control.EOClassDescription;
 27  
 import net.wotonomy.control.EOFetchSpecification;
 28  
 import net.wotonomy.control.EOGenericRecord;
 29  
 import net.wotonomy.control.EOGlobalID;
 30  
 import net.wotonomy.control.EOIntegralKeyGlobalID;
 31  
 import net.wotonomy.control.EOKeyGlobalID;
 32  
 import net.wotonomy.control.EOKeyValueArchiver;
 33  
 import net.wotonomy.control.EOKeyValueQualifier;
 34  
 import net.wotonomy.control.EOKeyValueUnarchiver;
 35  
 import net.wotonomy.control.EOQualifier;
 36  
 import net.wotonomy.control.EOVectorKeyGlobalID;
 37  
 import net.wotonomy.foundation.NSArray;
 38  
 import net.wotonomy.foundation.NSDictionary;
 39  
 import net.wotonomy.foundation.NSKeyValueCoding;
 40  
 import net.wotonomy.foundation.NSMutableArray;
 41  
 import net.wotonomy.foundation.NSMutableDictionary;
 42  
 import net.wotonomy.foundation.NSPropertyListSerialization;
 43  
 
 44  
 /**
 45  
 * An EOEntity is a mapping between a Java class and a database table or view.
 46  
 * It indicates which attributes should be fetched from the table/view, what
 47  
 * attributes are part of the primary key, what class the entity should map to.
 48  
 *
 49  
 * @author ezamudio@nasoft.com
 50  
 * @author $Author: cgruber $
 51  
 * @version $Revision: 894 $
 52  
 */
 53  
 public class EOEntity implements EOPropertyListEncoding {
 54  
 
 55  0
         protected NSMutableDictionary _attributes = new NSMutableDictionary();
 56  0
         protected NSMutableDictionary _relations = new NSMutableDictionary();
 57  0
         private NSMutableArray _classPropertyNames = new NSMutableArray();
 58  0
         private NSMutableArray _classProperties = new NSMutableArray();
 59  0
         private NSMutableArray _classPropertyAttributes = new NSMutableArray();
 60  0
         private NSMutableArray _classPropertyManyRelationships = new NSMutableArray();
 61  0
         private NSMutableArray _classPropertyOneRelationships = new NSMutableArray();
 62  0
         protected NSArray _pkAttributes = NSArray.EmptyArray;
 63  0
         protected NSArray _pkAttributeNames = NSArray.EmptyArray;
 64  0
         protected NSMutableDictionary _fetchSpecs = new NSMutableDictionary();
 65  0
         protected NSMutableArray _lockingAttributes = new NSMutableArray();
 66  
 
 67  
         protected String _className;
 68  
         protected String _name;
 69  
         protected String _externalName;
 70  
         protected boolean _isAbstract;
 71  
         protected boolean _isReadOnly;
 72  
         protected EOModel _model;
 73  
         protected NSDictionary _userInfo;
 74  
         protected NSDictionary _internalInfo;
 75  
         private boolean _loadedFetchSpecs;
 76  
 
 77  
         public EOEntity() {
 78  0
                 super();
 79  0
         }
 80  
 
 81  
         public EOEntity(NSDictionary dict, Object obj) {
 82  0
                 super();
 83  0
                 _model = (EOModel)obj;
 84  0
                 setName((String)dict.objectForKey("name"));
 85  0
                 setExternalName((String)dict.objectForKey("externalName"));
 86  0
                 setClassName((String)dict.objectForKey("className"));
 87  0
                 if (dict.objectForKey("internalInfo") != null)
 88  0
                         _internalInfo = (NSDictionary)dict.objectForKey("internalInfo");
 89  0
                 if (dict.objectForKey("userInfo") != null)
 90  0
                         _userInfo = (NSDictionary)dict.objectForKey("userInfo");
 91  
 
 92  
                 //Read the attributes
 93  0
                 NSArray atr = (NSArray)dict.objectForKey("attributes");
 94  0
                 for (int i = 0; i < atr.count(); i++) {
 95  0
                         NSDictionary d = (NSDictionary)atr.objectAtIndex(i);
 96  0
                         EOAttribute atrib = new EOAttribute(d, this);
 97  0
                         addAttribute(atrib);
 98  
                 }
 99  
 
 100  
                 //Set the primary key
 101  0
                 atr = (NSArray)dict.objectForKey("primaryKeyAttributes");
 102  0
                 NSMutableArray pka = new NSMutableArray();
 103  0
                 for (int i = 0; i < atr.count(); i++) {
 104  0
                         EOAttribute a = attributeNamed((String)atr.objectAtIndex(i));
 105  0
                         pka.addObject(a);
 106  
                 }
 107  0
                 _pkAttributes = new NSArray(pka);
 108  0
                 _pkAttributeNames = atr;
 109  
 
 110  
                 //attributes used for locking
 111  0
                 _lockingAttributes.removeAllObjects();
 112  0
                 atr = (NSArray)dict.objectForKey("attributesUsedForLocking");
 113  0
                 for (int i = 0; i < atr.count(); i++) {
 114  0
                         String x = (String)atr.objectAtIndex(i);
 115  0
                         EOAttribute a = attributeNamed(x);
 116  0
                         _lockingAttributes.addObject(a);
 117  
                 }
 118  
 
 119  
                 //class properties
 120  0
                 atr = (NSArray)dict.objectForKey("classProperties");
 121  0
                 if (atr != null) {
 122  0
                         for (int i = 0; i < atr.count(); i++)
 123  0
                                 if (!_classPropertyNames.containsObject((atr.objectAtIndex(i))))
 124  0
                                         _classPropertyNames.addObject(atr.objectAtIndex(i));
 125  
                 }
 126  
 
 127  
                 //Read the relationships
 128  0
                 atr = (NSArray)dict.objectForKey("relationships");
 129  0
                 if (atr != null) {
 130  0
                         for (int i = 0; i < atr.count(); i++) {
 131  0
                                 NSDictionary d = (NSDictionary)atr.objectAtIndex(i);
 132  0
                                 EORelationship rel = new EORelationship(d, this);
 133  0
                                 addRelationship(rel);
 134  
                         }
 135  
                 }
 136  0
         }
 137  
 
 138  
         public void addAttribute(EOAttribute atr) {
 139  0
                 if (atr.name() == null)
 140  0
                         throw new IllegalArgumentException("Cannot add an unnamed attribute to an entity.");
 141  0
                 if (_attributes.objectForKey(atr.name()) != null)
 142  0
                         throw new IllegalArgumentException("Entity " + name() + " already has an attribute named " + atr.name());
 143  0
                 _attributes.setObjectForKey(atr, atr.name());
 144  0
                 atr.setEntity(this);
 145  0
                 _lockingAttributes.addObject(atr);
 146  0
                 _classProperties.addObject(atr);
 147  0
                 _classPropertyNames.addObject(atr.name());
 148  0
                 _classPropertyAttributes.addObject(atr);
 149  0
         }
 150  
 
 151  
         public void removeAttribute(EOAttribute atr) {
 152  0
                 _attributes.removeObjectForKey(atr.name());
 153  0
                 atr.setEntity(null);
 154  0
                 _classProperties.removeObject(atr);
 155  0
                 _classPropertyNames.removeObject(atr.name());
 156  0
                 _classPropertyAttributes.removeObject(atr);
 157  0
         }
 158  
 
 159  
         public void addFetchSpecification(EOFetchSpecification fspec, String name) {
 160  0
                 loadFetchSpecifications();
 161  0
                 if (_fetchSpecs.objectForKey(name) != null)
 162  0
                         throw new IllegalArgumentException("Entity " + name() + " already has a fetch specification named " + name);
 163  0
                 _fetchSpecs.setObjectForKey(fspec, name);
 164  0
         }
 165  
 
 166  
         public void removeFetchSpecificationNamed(String name) {
 167  0
                 _fetchSpecs.removeObjectForKey(name);
 168  0
         }
 169  
 
 170  
         public EOFetchSpecification fetchSpecificationNamed(String name) {
 171  0
                 loadFetchSpecifications();
 172  0
                 return (EOFetchSpecification)_fetchSpecs.objectForKey(name);
 173  
         }
 174  
 
 175  
         public NSArray fetchSpecificationNames() {
 176  0
                 loadFetchSpecifications();
 177  0
                 return _fetchSpecs.allKeys();
 178  
         }
 179  
 
 180  
         /** Loads fetch specifications from the .fspec file,
 181  
          * if one exists.
 182  
          */
 183  
         private void loadFetchSpecifications() {
 184  0
                 if (_loadedFetchSpecs)
 185  0
                         return;
 186  0
                 _loadedFetchSpecs = true;
 187  0
                 if (model().path() == null)
 188  0
                         return;
 189  0
                 File f = new File(model().path());
 190  
                 //Read the fetch specification file, if it exists
 191  0
                 f = new File(f, name() + ".fspec");
 192  0
                 if (!f.exists())
 193  0
                         return;
 194  0
                 NSDictionary fdict = null;
 195  0
                 String x = null;
 196  
                 try {
 197  0
                         FileInputStream fin = new FileInputStream(f);
 198  0
                         byte[] b = new byte[fin.available()];
 199  0
                         fin.read(b);
 200  0
                         fin.close();
 201  0
                         x = new String(b);
 202  0
                 } catch (IOException ex) {
 203  0
                         throw new IllegalArgumentException("Cannot read file " + f);
 204  0
                 }
 205  0
                 fdict = NSPropertyListSerialization.dictionaryForString(x);
 206  0
                 if (fdict == null)
 207  0
                         throw new IllegalArgumentException("Cannot read dictionary from " + f);
 208  0
                 NSArray keys = fdict.allKeys();
 209  
 
 210  
                 //Unarchive the fetch specification
 211  0
                 EOKeyValueUnarchiver unarch = new EOKeyValueUnarchiver(fdict);
 212  0
                 for (int i = 0; i < keys.count(); i++) {
 213  0
                         String k = (String)keys.objectAtIndex(i);
 214  0
                         EOFetchSpecification fs = (EOFetchSpecification)unarch.decodeObjectForKey(k);
 215  0
                         if (fs != null)
 216  0
                                 _fetchSpecs.setObjectForKey(fs, k);
 217  
                 }
 218  0
         }
 219  
 
 220  
         public NSArray attributes() {
 221  0
                 return _attributes.allValues();
 222  
         }
 223  
 
 224  
         public EOAttribute attributeNamed(String name) {
 225  0
                 return (EOAttribute)_attributes.objectForKey(name);
 226  
         }
 227  
 
 228  
         public NSArray flattenedAttributes() {
 229  0
                 return null;
 230  
         }
 231  
 
 232  
         public void setClassName(String name) {
 233  0
                 _className = name;
 234  0
         }
 235  
         public String className() {
 236  0
                 return _className;
 237  
         }
 238  
 
 239  
         public void setName(String name) {
 240  0
                 _name = name;
 241  0
         }
 242  
         public String name() {
 243  0
                 return _name;
 244  
         }
 245  
 
 246  
         public void setExternalName(String name) {
 247  0
                 _externalName = name;
 248  0
         }
 249  
         public String externalName() {
 250  0
                 return _externalName;
 251  
         }
 252  
 
 253  
         public void addRelationship(EORelationship rel) {
 254  0
                 if (rel.name() == null)
 255  0
                         throw new IllegalArgumentException("Cannot add an unnamed relationship to an entity.");
 256  0
                 if (_relations.objectForKey(rel.name()) != null)
 257  0
                         throw new IllegalArgumentException("Entity " + name() + " already has a relationship named " + rel.name());
 258  0
                 if (_attributes.objectForKey(rel.name()) != null)
 259  0
                         throw new IllegalArgumentException("Entity " + name() + " has an attribute named " + rel.name());
 260  0
                 _relations.setObjectForKey(rel, rel.name());
 261  0
                 _classProperties.addObject(rel);
 262  0
                 _classPropertyNames.addObject(rel.name());
 263  0
                 if (rel.isToMany())
 264  0
                         _classPropertyManyRelationships.addObject(rel);
 265  
                 else
 266  0
                         _classPropertyOneRelationships.addObject(rel);
 267  0
         }
 268  
 
 269  
         public void removeRelationship(EORelationship rel) {
 270  0
                 _relations.removeObjectForKey(rel.name());
 271  0
                 _classProperties.removeObject(rel);
 272  0
                 _classPropertyNames.removeObject(rel.name());
 273  0
                 _classPropertyManyRelationships.removeObject(rel);
 274  0
                 _classPropertyOneRelationships.removeObject(rel);
 275  0
         }
 276  
 
 277  
         /** Returns the relationships from this entity to other entities.
 278  
          * @return An array of the relationships of this entity.
 279  
          */
 280  
         public NSArray relationships() {
 281  0
                 return _relations.allValues();
 282  
         }
 283  
 
 284  
         public EORelationship relationshipNamed(String name) {
 285  0
                 return (EORelationship)_relations.objectForKey(name);
 286  
         }
 287  
         public EOModel model() {
 288  0
                 return _model;
 289  
         }
 290  
 
 291  
         public void setPrimaryKeyAtributes(NSArray pk) {
 292  0
                 _pkAttributes = pk;
 293  0
         }
 294  
         public NSArray primaryKeyAttributes() {
 295  0
                 return _pkAttributes;
 296  
         }
 297  
 
 298  
         public NSArray primaryKeyAttributeNames() {
 299  0
                 if (_pkAttributeNames.count() != _pkAttributes.count()) {
 300  0
                         NSMutableArray arr = new NSMutableArray();
 301  0
                         for (int i = 0; i < _pkAttributes.count(); i++) {
 302  0
                                 EOAttribute a = (EOAttribute)_pkAttributes.objectAtIndex(i);
 303  0
                                 arr.addObject(a.name());
 304  
                         }
 305  0
                         _pkAttributeNames = new NSArray(arr);
 306  
                 }
 307  0
                 return _pkAttributeNames;
 308  
         }
 309  
 
 310  
         public boolean hasSimplePrimaryKey() {
 311  0
                 return _pkAttributes.count() == 1;
 312  
         }
 313  
 
 314  
         public boolean isValidPrimaryKeyAttribute(EOAttribute attr) {
 315  0
                 return !attr.allowsNull();
 316  
         }
 317  
 
 318  
         public void setAttributesUsedForLocking(NSArray value) {
 319  0
                 _lockingAttributes.removeAllObjects();
 320  0
                 _lockingAttributes.addObjectsFromArray(value);
 321  0
         }
 322  
         public NSArray attributesUsedForLocking() {
 323  0
                 return new NSArray(_lockingAttributes);
 324  
         }
 325  
 
 326  
         public void setClassProperties(NSArray value) {
 327  0
                 _classProperties.removeAllObjects();
 328  0
                 _classProperties.addObjectsFromArray(value);
 329  0
                 _classPropertyNames.removeAllObjects();
 330  0
                 _classPropertyAttributes.removeAllObjects();
 331  0
                 _classPropertyOneRelationships.removeAllObjects();
 332  0
                 _classPropertyManyRelationships.removeAllObjects();
 333  0
                 for (int i = 0; i < value.count(); i++) {
 334  0
                         EOProperty o = (EOProperty)value.objectAtIndex(i);
 335  0
                         _classPropertyNames.addObject(o.name());
 336  0
                         if (o instanceof EOAttribute) {
 337  0
                                 _classPropertyAttributes.addObject(o);
 338  0
                         } else if (o instanceof EORelationship) {
 339  0
                                 if (((EORelationship)o).isToMany())
 340  0
                                         _classPropertyManyRelationships.addObject(o);
 341  
                                 else
 342  0
                                         _classPropertyOneRelationships.addObject(o);
 343  
                         }
 344  
                 }
 345  0
         }
 346  
         public NSArray classProperties() {
 347  0
                 if (_classProperties == null) {
 348  0
                         if (_classPropertyNames == null)
 349  0
                                 return NSArray.EmptyArray;
 350  
                         else {
 351  0
                                 NSMutableArray props = new NSMutableArray();
 352  0
                                 NSMutableArray atribs = new NSMutableArray();
 353  0
                                 NSMutableArray ones = new NSMutableArray();
 354  0
                                 NSMutableArray manies = new NSMutableArray();
 355  0
                                 for (int i = 0; i < _classPropertyNames.count(); i++) {
 356  0
                                         String name = (String)_classPropertyNames.objectAtIndex(i);
 357  0
                                         EOAttribute a = attributeNamed(name);
 358  0
                                         EORelationship r = relationshipNamed(name);
 359  0
                                         if (a != null) {
 360  0
                                                 props.addObject(a);
 361  0
                                                 atribs.addObject(a);
 362  0
                                         } else if (r != null) {
 363  0
                                                 props.addObject(r);
 364  0
                                                 if (r.isToMany())
 365  0
                                                         manies.addObject(r);
 366  
                                                 else
 367  0
                                                         ones.addObject(r);
 368  0
                                         } else
 369  0
                                                 throw new IllegalArgumentException("Cannot find attribute or relationship named " + name);
 370  
                                 }
 371  0
                                 _classProperties = props;
 372  0
                                 _classPropertyAttributes = atribs;
 373  0
                                 _classPropertyOneRelationships = ones;
 374  0
                                 _classPropertyManyRelationships = manies;
 375  
                         }
 376  
                 }
 377  0
                 return _classProperties;
 378  
         }
 379  
 
 380  
         public NSArray classPropertyNames() {
 381  0
                 return _classPropertyNames;
 382  
         }
 383  
 
 384  
         public NSArray classPropertyAttributeNames() {
 385  0
                 if (_classPropertyAttributes == null)
 386  0
                         return NSArray.EmptyArray;
 387  0
                 NSMutableArray arr = new NSMutableArray(_classPropertyAttributes.count());
 388  0
                 for (int i = 0 ; i < _classPropertyAttributes.count(); i++) {
 389  0
                         EOAttribute a = (EOAttribute)_classPropertyAttributes.objectAtIndex(i);
 390  0
                         arr.addObject(a.name());
 391  
                 }
 392  0
                 return arr;
 393  
         }
 394  
 
 395  
         public NSArray classPropertyToManyRelationshipNames() {
 396  0
                 if (_classPropertyManyRelationships == null)
 397  0
                         return NSArray.EmptyArray;
 398  0
                 NSMutableArray arr = new NSMutableArray(_classPropertyManyRelationships.count());
 399  0
                 for (int i = 0 ; i < _classPropertyManyRelationships.count(); i++) {
 400  0
                         EOAttribute a = (EOAttribute)_classPropertyManyRelationships.objectAtIndex(i);
 401  0
                         arr.addObject(a.name());
 402  
                 }
 403  0
                 return arr;
 404  
         }
 405  
 
 406  
         public NSArray classPropertyToOneRelationshipNames() {
 407  0
                 if (_classPropertyOneRelationships == null)
 408  0
                         return NSArray.EmptyArray;
 409  0
                 NSMutableArray arr = new NSMutableArray(_classPropertyOneRelationships.count());
 410  0
                 for (int i = 0 ; i < _classPropertyOneRelationships.count(); i++) {
 411  0
                         EOAttribute a = (EOAttribute)_classPropertyOneRelationships.objectAtIndex(i);
 412  0
                         arr.addObject(a.name());
 413  
                 }
 414  0
                 return arr;
 415  
         }
 416  
 
 417  
         public void setIsAbstractEntity(boolean flag) {
 418  0
                 _isAbstract = flag;
 419  0
         }
 420  
         public boolean isAbstractEntity() {
 421  0
                 return _isAbstract;
 422  
         }
 423  
 
 424  
         public void setReadOnly(boolean flag) {
 425  0
                 _isReadOnly = flag;
 426  0
         }
 427  
         public boolean isReadOnly() {
 428  0
                 return _isReadOnly;
 429  
         }
 430  
 
 431  
         public void setStoredProcedure(EOStoredProcedure proc, String operation) {
 432  0
         }
 433  
         public EOStoredProcedure storedProcedureForOperation(String operation) {
 434  0
                 return null;
 435  
         }
 436  
 
 437  
         public NSArray subEntities() {
 438  0
                 return null;
 439  
         }
 440  
 
 441  
         public NSArray attributesToFetch() {
 442  0
                 return attributes();
 443  
         }
 444  
 
 445  
         public NSArray externalModelsReferenced() {
 446  0
                 return null;
 447  
         }
 448  
 
 449  
         public EOClassDescription classDescriptionForInstances() {
 450  0
                 EOClassDescription cd = EOClassDescription.classDescriptionForEntityName(name());
 451  0
                 if (cd == null) {
 452  0
                         cd = new EOEntityClassDescription(this);
 453  0
                         Class cl = null;
 454  
                         try {
 455  0
                                 cl = Class.forName(className());
 456  0
                         } catch (ClassNotFoundException ex) {
 457  0
                                 cl = EOGenericRecord.class;
 458  0
                         }
 459  0
                         EOClassDescription.registerClassDescription(cd, cl);
 460  
                 }
 461  0
                 return cd;
 462  
         }
 463  
 
 464  
         /**
 465  
          * Creates a global ID for a row. The row must have values
 466  
          * for all the primary key attributes.
 467  
          * @param row A raw row for this entity.
 468  
          * @return A key global ID to identify this row.
 469  
          */
 470  
         public EOGlobalID globalIDForRow(Map row) {
 471  0
                 NSArray pknames = primaryKeyAttributeNames();
 472  0
                 EOKeyGlobalID gid = null;
 473  0
                 if (pknames.count() == 1 && row.get(pknames.objectAtIndex(0)) instanceof Number) {
 474  0
                         Number n = (Number)row.get(pknames.objectAtIndex(0));
 475  0
                         gid = new EOIntegralKeyGlobalID(name(), n);
 476  0
                 } else {
 477  0
                         Object[] vals = new Object[pknames.count()];
 478  0
                         for (int i = 0; i < pknames.count(); i++) {
 479  0
                                 Object v = row.get(pknames.objectAtIndex(i));
 480  0
                                 vals[i] = v;
 481  
                         }
 482  0
                         gid = new EOVectorKeyGlobalID(name(), vals);
 483  
                 }
 484  0
                 return gid;
 485  
         }
 486  
 
 487  
         /**
 488  
          * Returns a dictionary with the primary key values contained in 
 489  
          * the global id.
 490  
          * @param gid A Key global ID.
 491  
          * @return A dictionary with the primary key values for gid.
 492  
          */
 493  
         public NSDictionary primaryKeyForGlobalID(EOGlobalID gid) {
 494  0
                 if (!(gid instanceof EOKeyGlobalID))
 495  0
                         return null;
 496  0
                 Object[] vals = ((EOKeyGlobalID)gid).keyValues();
 497  0
                 NSArray pknames = primaryKeyAttributeNames();
 498  0
                 return new NSDictionary(vals, pknames.toArray());
 499  
         }
 500  
 
 501  
         public EOQualifier qualifierForPrimaryKey(Map pkey) {
 502  0
                 NSArray pknames = primaryKeyAttributeNames();
 503  0
                 EOQualifier q = null;
 504  0
                 NSMutableArray subq = new NSMutableArray(pknames.count());
 505  0
                 for (int i = 0; i < pknames.count(); i++) {
 506  0
                         String key = (String)pknames.objectAtIndex(i);
 507  0
                         Object v = pkey.get(key);
 508  0
                         if (v == null || v == NSKeyValueCoding.NullValue)
 509  0
                                 throw new IllegalArgumentException("Primary key with null values.");
 510  0
                         subq.addObject(new EOKeyValueQualifier(key, EOQualifier.QualifierOperatorEqual, v));
 511  
                 }
 512  0
                 if (subq.count() == 1)
 513  0
                         q = (EOQualifier)subq.objectAtIndex(0);
 514  
                 else
 515  0
                         q = new EOAndQualifier(subq);
 516  0
                 return q;
 517  
         }
 518  
 
 519  
         public void setUserInfo(NSDictionary value) {
 520  0
                 _userInfo = value;
 521  0
         }
 522  
         public NSDictionary userInfo() {
 523  0
                 return _userInfo;
 524  
         }
 525  
 
 526  
         public void awakeWithPropertyList(NSDictionary plist) {
 527  0
         }
 528  
 
 529  
         public void encodeIntoPropertyList(NSMutableDictionary dict) {
 530  0
                 dict.setObjectForKey(name(), "name");
 531  0
                 dict.setObjectForKey(externalName(), "externalName");
 532  0
                 dict.setObjectForKey(className(), "className");
 533  
 
 534  
                 //Encode attributes
 535  0
                 NSMutableArray arr = new NSMutableArray(_attributes.allValues());
 536  0
                 for (int i = 0; i < _attributes.count(); i++) {
 537  0
                         EOAttribute a = (EOAttribute)arr.objectAtIndex(i);
 538  0
                         NSMutableDictionary d = new NSMutableDictionary();
 539  0
                         a.encodeIntoPropertyList(d);
 540  0
                         arr.replaceObjectAtIndex(i, d);
 541  
                 }
 542  0
                 dict.setObjectForKey(arr, "attributes");
 543  
                 //Encode relationships
 544  0
                 if (_relations.count() > 0) {
 545  0
                         arr = new NSMutableArray(_relations.allValues());
 546  0
                         for (int i = 0; i < _relations.count(); i++) {
 547  0
                                 EORelationship r = (EORelationship)arr.objectAtIndex(i);
 548  0
                                 NSMutableDictionary d = new NSMutableDictionary();
 549  0
                                 r.encodeIntoPropertyList(d);
 550  0
                                 arr.replaceObjectAtIndex(i, d);
 551  
                         }
 552  0
                         dict.setObjectForKey(arr, "relationships");
 553  
                 }
 554  
 
 555  
                 //Fetch specifications
 556  0
                 NSMutableDictionary d = new NSMutableDictionary();
 557  0
                 loadFetchSpecifications();
 558  0
                 java.util.Enumeration enumeration = _fetchSpecs.keyEnumerator();
 559  0
                 while (enumeration.hasMoreElements()) {
 560  0
                         String k = (String)enumeration.nextElement();
 561  0
                         EOFetchSpecification f = (EOFetchSpecification)_fetchSpecs.objectForKey(k);
 562  0
                         EOKeyValueArchiver arch = new EOKeyValueArchiver();
 563  0
                         f.encodeWithKeyValueArchiver(arch);
 564  0
                         d.setObjectForKey(arch.dictionary(), k);
 565  0
                 }
 566  0
                 dict.setObjectForKey(d, "fetchSpecificationDictionary");
 567  
 
 568  
                 //Other information
 569  0
                 dict.setObjectForKey(_classPropertyNames, "classProperties");
 570  0
                 dict.setObjectForKey(_pkAttributeNames, "primaryKeyAttributes");
 571  0
                 arr = new NSMutableArray(_lockingAttributes);
 572  0
                 for (int i = 0; i < arr.count(); i++)
 573  0
                         arr.replaceObjectAtIndex(i, ((EOAttribute)arr.objectAtIndex(i)).name());
 574  0
                 dict.setObjectForKey(arr, "attributesUsedForLocking");
 575  0
                 if (_userInfo != null && _userInfo.count() > 0 )
 576  0
                         dict.setObjectForKey(_userInfo, "userInfo");
 577  0
                 if (_internalInfo != null && _internalInfo.count() > 0 )
 578  0
                         dict.setObjectForKey(_internalInfo, "internalInfo");
 579  0
         }
 580  
 
 581  
         public EOAttribute _attributeForPath(String path) {
 582  0
                 NSArray comps = NSArray.componentsSeparatedByString(path, ".");
 583  0
                 if (comps.count() < 2)
 584  0
                         return null;
 585  0
                 EORelationship r = null;
 586  0
                 EOEntity e = this;
 587  0
                 for (int i = 0; i < comps.count()-1; i++) {
 588  0
                         String name = (String)comps.objectAtIndex(i);
 589  0
                         r = e.relationshipNamed(name);
 590  0
                         if (r == null)
 591  0
                                 return null;
 592  0
                         e = r.destinationEntity();
 593  
                 }
 594  0
                 return e.attributeNamed((String)comps.lastObject());
 595  
         }
 596  
 
 597  
 }
 598  
 /*
 599  
  * $Log$
 600  
  * Revision 1.2  2006/02/16 16:47:14  cgruber
 601  
  * Move some classes in to "internal" packages and re-work imports, etc.
 602  
  *
 603  
  * Also use UnsupportedOperationExceptions where appropriate, instead of WotonomyExceptions.
 604  
  *
 605  
  * Revision 1.1  2006/02/16 13:19:57  cgruber
 606  
  * Check in all sources in eclipse-friendly maven-enabled packages.
 607  
  *
 608  
  * Revision 1.9  2005/05/11 15:21:53  cgruber
 609  
  * Change enum to enumeration, since enum is now a keyword as of Java 5.0
 610  
  *
 611  
  * A few other comments in the code.
 612  
  *
 613  
  * Revision 1.8  2003/08/19 19:47:58  chochos
 614  
  * added some methods to handle EOGlobalIDs and primary keys.
 615  
  *
 616  
  * Revision 1.7  2003/08/14 02:13:56  chochos
 617  
  * implement and use _attributeForPath()
 618  
  *
 619  
  * Revision 1.6  2003/08/11 19:38:27  chochos
 620  
  * Can now read from a file and re-write to another file.
 621  
  *
 622  
  * Revision 1.5  2003/08/11 18:19:33  chochos
 623  
  * encoding into property list seems to work fine now.
 624  
  *
 625  
  * Revision 1.4  2003/08/09 01:39:04  chochos
 626  
  * better handling of class properties; use EOKeyValueArchiving to encode/decode fetch specifications; implement EOPropertyListEncoding
 627  
  *
 628  
  * Revision 1.3  2003/08/08 05:52:21  chochos
 629  
  * gets the class description for the entity.
 630  
  *
 631  
  * Revision 1.2  2003/08/08 02:14:20  chochos
 632  
  * now it can read stored procedures.
 633  
  *
 634  
  * Revision 1.1  2003/08/07 02:38:33  chochos
 635  
  * implementation of EOEntity. What works for now is reading an entity from file.
 636  
  *
 637  
  */