View Javadoc

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.control.EOClassDescription;
21  import net.wotonomy.control.EOEditingContext;
22  import net.wotonomy.control.EOFetchSpecification;
23  import net.wotonomy.control.EOGlobalID;
24  import net.wotonomy.control.EOKeyValueCodingAdditions;
25  import net.wotonomy.foundation.NSArray;
26  import net.wotonomy.foundation.NSDictionary;
27  import net.wotonomy.foundation.NSMutableArray;
28  
29  /***
30  *
31  * @author ezamudio@nasoft.com
32  * @author $Author: cgruber $
33  * @version $Revision: 894 $
34  */
35  public class EODatabaseChannel {
36  
37  	protected EODatabaseContext _context;
38  	protected EOAdaptorChannel _channel;
39  	protected EOEntity _currEntity;
40  	protected EOEditingContext _currEC;
41  	protected NSArray _attributes;
42  	protected boolean _locking;
43  	protected boolean _refreshing;
44  
45  	public EODatabaseChannel(EODatabaseContext context) {
46  		super();
47  		_context = context;
48  		_channel = _context.adaptorContext().createAdaptorChannel();
49  	}
50  
51  	public EOAdaptorChannel adaptorChannel() {
52  		return _channel;
53  	}
54  
55  	public EODatabaseContext databaseContext() {
56  		return _context;
57  	}
58  
59  	public void cancelFetch() {
60  		_channel.cancelFetch();
61  	}
62  
63  	public Object fetchObject() {
64  		NSDictionary r = _channel.fetchRow();
65  		EOGlobalID gid = _currEntity.globalIDForRow(r);
66  		Object eo = _currEC.objectForGlobalID(gid);
67  		if (eo == null) {
68  			eo = EOClassDescription.classDescriptionForEntityName(_currEntity.name()).createInstanceWithEditingContext(_currEC, gid);
69  			if (eo instanceof EOKeyValueCodingAdditions)
70  				((EOKeyValueCodingAdditions)eo).takeValuesFromDictionary(r);
71  			else
72  				EOKeyValueCodingAdditions.DefaultImplementation.takeValuesFromDictionary(eo, r);
73  		} else {
74  			if (isRefreshingObjects()) {
75  				//TODO: refresh object (how?)
76  			}
77  		}
78  		return eo;
79  	}
80  
81  	public boolean isFetchInProgress() {
82  		return _channel.isFetchInProgress();
83  	}
84  
85  	public void setIsLocking(boolean flag) {
86  		_locking = flag;
87  	}
88  
89  	public boolean isLocking() {
90  		return _locking;
91  	}
92  
93  	public void setIsRefreshingObjects(boolean flag) {
94  		_refreshing = flag;
95  	}
96  
97  	public boolean isRefreshingObjects() {
98  		return _refreshing;
99  	}
100 
101 	public void selectObjectsWithFetchSpecification(EOFetchSpecification fspec, EOEditingContext ec) {
102 		setIsLocking(fspec.locksObjects());
103 		setIsRefreshingObjects(fspec.refreshesRefetchedObjects());
104 		setCurrentEditingContext(ec);
105 		setCurrentEntity(databaseContext().database().entityNamed(fspec.entityName()));
106 		NSMutableArray atts = new NSMutableArray();
107 		atts.addObjectsFromArray(_currEntity.attributes());
108 		adaptorChannel().selectAttributes(atts, fspec, isLocking(), _currEntity);
109 		adaptorChannel().setAttributesToFetch(atts);
110 		_attributes = atts;
111 	}
112 
113 	public void setCurrentEditingContext(EOEditingContext ec) {
114 		_currEC = ec;
115 	}
116 
117 	public void setCurrentEntity(EOEntity entity) {
118 		_currEntity = entity;
119 	}
120 
121 }
122 /*
123  * $Log$
124  * Revision 1.2  2006/02/16 16:47:14  cgruber
125  * Move some classes in to "internal" packages and re-work imports, etc.
126  *
127  * Also use UnsupportedOperationExceptions where appropriate, instead of WotonomyExceptions.
128  *
129  * Revision 1.1  2006/02/16 13:19:57  cgruber
130  * Check in all sources in eclipse-friendly maven-enabled packages.
131  *
132  * Revision 1.2  2003/08/29 21:15:46  chochos
133  * use EOEntity's globalIDForRow method.
134  *
135  * Revision 1.1  2003/08/19 01:54:43  chochos
136  * The EODatabase layer still needs a lot of work, but it's on its way...
137  *
138  */