Coverage Report - net.wotonomy.access.EORelationship
 
Classes in this File Line Coverage Branch Coverage Complexity
EORelationship
0% 
0% 
2.219
 
 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 net.wotonomy.foundation.NSArray;
 21  
 import net.wotonomy.foundation.NSDictionary;
 22  
 import net.wotonomy.foundation.NSMutableArray;
 23  
 import net.wotonomy.foundation.NSMutableDictionary;
 24  
 /**
 25  
 * Represents a relationship from one entity to another. Relationships are unidirectional.
 26  
 *
 27  
 * @author ezamudio@nasoft.com
 28  
 * @author $Author: cgruber $
 29  
 * @version $Revision: 894 $
 30  
 */
 31  
 public class EORelationship extends EOProperty implements EOPropertyListEncoding {
 32  
 
 33  
         public static final int InnerJoin = 0;
 34  
         public static final int FullOuterJoin = 1;
 35  
         public static final int LeftOuterJoin = 2;
 36  
         public static final int RightOuterJoin = 3;
 37  
 
 38  
         protected String _name;
 39  
         protected int _batchCount;
 40  
         protected int _deleteRule;
 41  
         protected int _joinSemantic;
 42  
         protected EOEntity _destination;
 43  
         protected EOEntity _entity;
 44  0
         protected NSMutableArray _joins = new NSMutableArray();
 45  
         protected boolean _isMandatory;
 46  
         protected boolean _isToMany;
 47  
         protected boolean _isFlattened;
 48  
         protected boolean _knowsIfFlattened;
 49  
         protected boolean _ownsDestination;
 50  
         protected boolean _propagatesPrimaryKey;
 51  
         protected boolean _useBatchFaulting;
 52  0
         protected NSDictionary _userInfo = NSDictionary.EmptyDictionary;
 53  0
         protected NSDictionary _internalInfo = NSDictionary.EmptyDictionary;
 54  
         protected NSDictionary plist;
 55  
         protected String _definition;
 56  
 
 57  
         public EORelationship() {
 58  0
                 super();
 59  0
         }
 60  
 
 61  
         public EORelationship(NSDictionary dict, Object obj) {
 62  0
                 super();
 63  0
                 _entity = (EOEntity)obj;
 64  0
                 setName((String)dict.objectForKey("name"));
 65  0
                 setToMany("Y".equals(dict.objectForKey("isToMany")));
 66  0
                 setPropagatesPrimaryKey("Y".equals(dict.objectForKey("propagatesPrimaryKey")));
 67  0
                 setIsMandatory("Y".equals(dict.objectForKey("isMandatory")));
 68  0
                 setOwnsDestination("Y".equals(dict.objectForKey("ownsDestination")));
 69  0
                 setDefinition((String)dict.objectForKey("definition"));
 70  0
                 String delrule = (String)dict.objectForKey("deleteRule");
 71  0
                 if (delrule != null) {
 72  0
                         if (delrule.equals("EODeleteRuleCascade"))
 73  0
                                 setDeleteRule(0);
 74  0
                         else if (delrule.equals("EODeleteRuleDeny"))
 75  0
                                 setDeleteRule(0);
 76  0
                         else if (delrule.equals("EODeleteRuleNoAction"))
 77  0
                                 setDeleteRule(0);
 78  0
                         else if (delrule.equals("EODeleteRuleNullify"))
 79  0
                                 setDeleteRule(0);
 80  
                 }
 81  0
                 delrule = (String)dict.objectForKey("joinSemantic");
 82  0
                 if (delrule != null) {
 83  0
                         if (delrule.equals("EOInnerJoin"))
 84  0
                                 setJoinSemantic(InnerJoin);
 85  0
                         else if (delrule.equals("EOFullOuterJoin"))
 86  0
                                 setJoinSemantic(FullOuterJoin);
 87  0
                         else if (delrule.equals("EOLeftOuterJoin"))
 88  0
                                 setJoinSemantic(LeftOuterJoin);
 89  0
                         else if (delrule.equals("EORightOuterJoin"))
 90  0
                                 setJoinSemantic(RightOuterJoin);
 91  
                 }
 92  0
                 delrule = (String)dict.objectForKey("batchCount");
 93  0
                 if (delrule != null)
 94  0
                         setNumberOfToManyFaultsToBatchFetch(Integer.parseInt(delrule));
 95  0
                 NSDictionary d = (NSDictionary)dict.objectForKey("userInfo");
 96  0
                 if (d != null)
 97  0
                         _userInfo = d;
 98  0
                 d = (NSDictionary)dict.objectForKey("internalInfo");
 99  0
                 if (d != null)
 100  0
                         _internalInfo = d;
 101  0
                 plist = dict;
 102  0
         }
 103  
 
 104  
         public void setName(String name) {
 105  0
                 _name = name;
 106  0
         }
 107  
         public String name() {
 108  0
                 return _name;
 109  
         }
 110  
 
 111  
         public void addJoin(EOJoin join) {
 112  0
                 _joins.addObject(join);
 113  0
         }
 114  
 
 115  
         public void removeJoin(EOJoin join) {
 116  0
                 _joins.removeObject(join);
 117  0
         }
 118  
 
 119  
         public EOEntity entity() {
 120  0
                 return _entity;
 121  
         }
 122  
 
 123  
         public EOEntity destinationEntity() {
 124  0
                 isFlattened();
 125  0
                 if (_destination == null && plist != null) {
 126  0
                         EOModel model = _entity.model();
 127  0
                         EOModelGroup group = model.modelGroup();
 128  0
                         String destEntity = (String)plist.objectForKey("destination");
 129  0
                         if (group != null)
 130  0
                                 _destination = group.entityNamed(destEntity);
 131  
                         else
 132  0
                                 _destination = model.entityNamed(destEntity);
 133  
                 }
 134  0
                 return _destination;
 135  
         }
 136  
 
 137  
         public void setOwnsDestination(boolean flag) {
 138  0
                 _ownsDestination = flag;
 139  0
         }
 140  
         public boolean ownsDestination() {
 141  0
                 return _ownsDestination;
 142  
         }
 143  
 
 144  
         public void setToMany(boolean flag) {
 145  0
                 _isToMany = flag;
 146  0
         }
 147  
         public boolean isToMany() {
 148  0
                 return _isToMany;
 149  
         }
 150  
 
 151  
         public void setIsMandatory(boolean flag) {
 152  0
                 _isMandatory = flag;
 153  0
         }
 154  
         public boolean isMandatory() {
 155  0
                 return _isMandatory;
 156  
         }
 157  
 
 158  
         public void setPropagatesPrimaryKey(boolean flag) {
 159  0
                 _propagatesPrimaryKey = flag;
 160  0
         }
 161  
         public boolean propagatesPrimaryKey() {
 162  0
                 return _propagatesPrimaryKey;
 163  
         }
 164  
 
 165  
         public void setDeleteRule(int value) {
 166  0
                 _deleteRule = value;
 167  0
         }
 168  
         public int deleteRule() {
 169  0
                 return _deleteRule;
 170  
         }
 171  
 
 172  
         public void setJoinSemantic(int value) {
 173  0
                 _joinSemantic = value;
 174  0
         }
 175  
         public int joinSemantic() {
 176  0
                 return _joinSemantic;
 177  
         }
 178  
 
 179  
         public void setNumberOfToManyFaultsToBatchFetch(int count) {
 180  0
                 _batchCount = count;
 181  0
         }
 182  
         public int numberOfToManyFaultsToBatchFetch() {
 183  0
                 return _batchCount;
 184  
         }
 185  
 
 186  
         public NSArray joins() {
 187  0
                 if (_joins.count() == 0 && plist != null) {
 188  0
                         NSArray joins = (NSArray)plist.objectForKey("joins");
 189  0
                         for (int i = 0; i < joins.count(); i++) {
 190  0
                                 NSDictionary d = (NSDictionary)joins.objectAtIndex(i);
 191  0
                                 String srcName = (String)d.objectForKey("sourceAttribute");
 192  0
                                 String dstName = (String)d.objectForKey("destinationAttribute");
 193  0
                                 EOAttribute a1 = _entity.attributeNamed(srcName);
 194  0
                                 EOAttribute a2 = destinationEntity().attributeNamed(dstName);
 195  0
                                 EOJoin j = new EOJoin(a1, a2);
 196  0
                                 addJoin(j);
 197  
                         }
 198  
                 }
 199  0
                 return new NSArray(_joins);
 200  
         }
 201  
 
 202  
         public void setDefinition(String def) {
 203  0
                 _definition = def;
 204  0
         }
 205  
         public String definition() {
 206  0
                 return _definition;
 207  
         }
 208  
 
 209  
         public boolean isFlattened() {
 210  0
                 if (_knowsIfFlattened)
 211  0
                         return _isFlattened;
 212  0
                 _knowsIfFlattened = true;
 213  0
                 if (definition() == null)
 214  0
                         return false;
 215  0
                 NSArray comps = NSArray.componentsSeparatedByString(definition(), ".");
 216  0
                 if (comps.count() < 2)
 217  0
                         return false;
 218  0
                 EORelationship r = null;
 219  0
                 EOEntity e = entity();
 220  0
                 for (int i = 0; i < comps.count(); i++) {
 221  0
                         String name = (String)comps.objectAtIndex(i);
 222  0
                         r = e.relationshipNamed(name);
 223  0
                         if (r == null)
 224  0
                                 return false;
 225  0
                         e = r.destinationEntity();
 226  
                 }
 227  0
                 _destination = e;
 228  0
                 _isFlattened = true;
 229  0
                 return _isFlattened;
 230  
         }
 231  
 
 232  
         public boolean isMultiHop() {
 233  0
                 return false;
 234  
         }
 235  
 
 236  
         public String relationshipPath() {
 237  0
                 if (isFlattened())
 238  0
                         return _definition;
 239  0
                 return null;
 240  
         }
 241  
 
 242  
         public void setUserInfo(NSDictionary value) {
 243  0
                 _userInfo = value;
 244  0
         }
 245  
         public NSDictionary userInfo() {
 246  0
                 return _userInfo;
 247  
         }
 248  
 
 249  
         public void awakeWithPropertyList(NSDictionary plist) {
 250  0
         }
 251  
 
 252  
         public void encodeIntoPropertyList(NSMutableDictionary dict) {
 253  0
                 dict.setObjectForKey(name(), "name");
 254  0
                 if (destinationEntity() != null && definition() == null)
 255  0
                         dict.setObjectForKey(_destination.name(), "destination");
 256  0
                 if (_internalInfo != null && _internalInfo.count() > 0)
 257  0
                         dict.setObjectForKey(_internalInfo, "internalInfo");
 258  0
                 if (_userInfo != null && _userInfo.count() > 0)
 259  0
                         dict.setObjectForKey(_userInfo, "userInfo");
 260  0
                 if (isToMany())
 261  0
                         dict.setObjectForKey("Y", "isToMany");
 262  0
                 switch (_joinSemantic) {
 263  
                         case InnerJoin:
 264  0
                                 dict.setObjectForKey("EOInnerJoin", "joinSemantic");
 265  0
                                 break;
 266  
                         case FullOuterJoin:
 267  0
                                 dict.setObjectForKey("EOFullOuterJoin", "joinSemantic");
 268  0
                                 break;
 269  
                         case LeftOuterJoin:
 270  0
                                 dict.setObjectForKey("EOLefOuterJoin", "joinSemantic");
 271  0
                                 break;
 272  
                         case RightOuterJoin:
 273  0
                                 dict.setObjectForKey("EORightOuterJoin", "joinSemantic");
 274  
                                 break;
 275  
                 }
 276  0
                 if (_batchCount > 0)
 277  0
                         dict.setObjectForKey(new Integer(_batchCount), "batchCount");
 278  0
                 if (definition() != null)
 279  0
                         dict.setObjectForKey(definition(), "definition");
 280  
                 else {
 281  0
                         NSMutableArray jarr = new NSMutableArray(joins().count());
 282  0
                         for (int i = 0; i < _joins.count(); i++) {
 283  0
                                 EOJoin j = (EOJoin)_joins.objectAtIndex(i);
 284  0
                                 NSDictionary d = new NSDictionary(
 285  0
                                         new Object[]{ j.sourceAttribute().name(), j.destinationAttribute().name() },
 286  0
                                         new Object[]{ "sourceAttribute", "destinationAttribute" });
 287  0
                                 jarr.addObject(d);
 288  
                         }
 289  0
                         dict.setObjectForKey(jarr, "joins");
 290  
                 }
 291  0
         }
 292  
 
 293  
 }
 294  
 /*
 295  
  * $Log$
 296  
  * Revision 1.2  2006/02/16 16:47:13  cgruber
 297  
  * Move some classes in to "internal" packages and re-work imports, etc.
 298  
  *
 299  
  * Also use UnsupportedOperationExceptions where appropriate, instead of WotonomyExceptions.
 300  
  *
 301  
  * Revision 1.1  2006/02/16 13:19:57  cgruber
 302  
  * Check in all sources in eclipse-friendly maven-enabled packages.
 303  
  *
 304  
  * Revision 1.5  2003/08/11 19:38:27  chochos
 305  
  * Can now read from a file and re-write to another file.
 306  
  *
 307  
  * Revision 1.4  2003/08/09 01:35:35  chochos
 308  
  * implement EOPropertyListEncoding
 309  
  *
 310  
  * Revision 1.3  2003/08/08 06:51:53  chochos
 311  
  * isFlattened() works
 312  
  *
 313  
  * Revision 1.2  2003/08/08 02:17:43  chochos
 314  
  * main accessors are in place.
 315  
  *
 316  
  * Revision 1.1  2003/08/07 02:41:30  chochos
 317  
  * a relationship that for the moment can be created from a property list.
 318  
  *
 319  
 */