3 |
## $Id$ |
## $Id$ |
4 |
## ------------------------------------------------------------------------------------- |
## ------------------------------------------------------------------------------------- |
5 |
## $Log$ |
## $Log$ |
6 |
|
## Revision 1.6 2002/12/22 13:26:20 jonen |
7 |
|
## + added support of tangram independent id (e.g. Data::UUID) toggled by option at conrtuctor |
8 |
|
## |
9 |
|
## Revision 1.5 2002/12/18 22:36:49 jonen |
10 |
|
## + added support to get remote objects via backend via 'guid' |
11 |
|
## + renamed '_loadBackend' to '_loadRemote' |
12 |
|
## + constructor now accepts additional options: |
13 |
|
## + remote: try to get object via '_loadRemote' ... |
14 |
|
## + oid: use the given identifier as an 'oid' when doing '_loadRemote' |
15 |
|
## + guid: use the given identifier as a 'guid' when doing '_loadRemote' |
16 |
|
## |
17 |
|
## Revision 1.4 2002/12/12 02:46:31 joko |
18 |
|
## + state (session) gets flushed when data was successfully loaded from remote side |
19 |
|
## + fixed _loadBackend |
20 |
|
## + fixed flushState |
21 |
|
## |
22 |
|
## Revision 1.3 2002/12/06 04:12:54 joko |
23 |
|
## + replaced 'xyzCache' through 'xyzProxy' |
24 |
|
## + function store(...) |
25 |
|
## |
26 |
## Revision 1.2 2002/12/05 21:44:09 joko |
## Revision 1.2 2002/12/05 21:44:09 joko |
27 |
## + debugging |
## + debugging |
28 |
## |
## |
82 |
var $attributes; |
var $attributes; |
83 |
var $backend; |
var $backend; |
84 |
|
|
85 |
function ProxyObject($objectId = "") { |
function ProxyObject($objectId = "", $options = array() ) { |
86 |
session_register_safe("proxy"); |
session_register_safe("proxy"); |
87 |
$this->backend = new Remote; |
$this->backend = new Remote; |
88 |
$this->_init($objectId); |
$this->_init($objectId, $options); |
89 |
} |
} |
90 |
|
|
91 |
function _init($objectId="") { |
function _init($objectId="", $options = array() ) { |
92 |
if ($objectId) { |
if ($objectId) { |
93 |
$this->objectId = $objectId; |
$this->objectId = $objectId; |
94 |
|
$this->meta = $options; |
95 |
|
|
96 |
|
// set default load mechanismn |
97 |
|
if( $this->meta[remote] && ((!$this->meta[oid] && !$this->meta[guid]) || ($this->meta[oid] && $this->meta[guid])) ) { |
98 |
|
$this->meta[oid] = 1; |
99 |
|
} |
100 |
|
|
101 |
$this->load(); |
$this->load(); |
102 |
//$this->_saveCache(); |
//$this->_saveProxy(); |
103 |
} |
} |
104 |
} |
} |
105 |
|
|
106 |
function load() { |
function load() { |
107 |
if (!$this->_loadState()) { |
if (!$this->_loadState()) { |
108 |
if (!$this->_loadCache()) { |
if (!$this->_loadProxy()) { |
109 |
$this->_loadBackend(); |
|
110 |
|
// just load object from remote side if its flagged to be a remote one ... |
111 |
|
if ($this->meta[remote]) { |
112 |
|
$this->_loadRemote(); |
113 |
|
} |
114 |
|
|
115 |
} |
} |
116 |
} |
} |
117 |
} |
} |
129 |
|
|
130 |
function flush() { |
function flush() { |
131 |
$this->flushState(); |
$this->flushState(); |
132 |
$this->flushCache(); |
$this->flushProxy(); |
133 |
} |
} |
134 |
|
|
135 |
function _commit() { |
function _commit() { |
136 |
$this->_saveBackend($this->attributes); |
$this->_saveBackend($this->attributes); |
137 |
$this->flushState(); |
$this->flushState(); |
138 |
$this->flushCache(); |
$this->flushProxy(); |
139 |
} |
} |
140 |
|
|
141 |
|
|
144 |
function _create() { |
function _create() { |
145 |
$this->_createBackend($this->attributes); |
$this->_createBackend($this->attributes); |
146 |
$this->flushState(); |
$this->flushState(); |
147 |
$this->flushCache(); |
$this->flushProxy(); |
148 |
} |
} |
149 |
*/ |
*/ |
150 |
|
|
160 |
$this->attributes = $data; |
$this->attributes = $data; |
161 |
} |
} |
162 |
|
|
163 |
function flushCache() { |
function flushProxy() { |
164 |
connectdb(); |
connectdb(); |
165 |
$sql = "DELETE FROM f_proxy WHERE oid='$this->objectId'"; |
$sql = "DELETE FROM f_proxy WHERE oid='$this->objectId'"; |
166 |
send_sql($sql); |
send_sql($sql); |
169 |
function flushState() { |
function flushState() { |
170 |
global $proxy; |
global $proxy; |
171 |
unset($proxy[$this->objectId]); |
unset($proxy[$this->objectId]); |
172 |
|
$this->meta[decoded] = 0; |
173 |
} |
} |
174 |
|
|
175 |
function _loadState() { |
function _loadState() { |
191 |
// TODO: throw exception-message back to user if operation fails |
// TODO: throw exception-message back to user if operation fails |
192 |
} |
} |
193 |
|
|
194 |
function _loadCache() { |
function _loadProxy() { |
195 |
logp(get_class($this) . "->_loadCache()", LOG_DEBUG); |
logp(get_class($this) . "->_loadProxy()", LOG_DEBUG); |
196 |
connectdb(); |
connectdb(); |
197 |
$sql = "SELECT payload FROM f_proxy WHERE oid='$this->objectId'"; |
$sql = "SELECT payload FROM f_proxy WHERE oid='$this->objectId'"; |
198 |
if ($res = send_sql($sql)) { |
if ($res = send_sql($sql)) { |
205 |
} |
} |
206 |
} |
} |
207 |
|
|
208 |
function _saveCache() { |
// TODO: use PEAR here |
209 |
logp(get_class($this) . "->_saveCache()", LOG_DEBUG); |
function _saveProxy() { |
210 |
|
logp(get_class($this) . "->_saveProxy()", LOG_DEBUG); |
211 |
connectdb(); |
connectdb(); |
212 |
if ($this->payload) { |
if ($this->payload) { |
213 |
//$sql = "INSERT INTO f_proxy SET payload='$this->payload' WHERE oid='$this->objectId'"; |
//$sql = "INSERT INTO f_proxy SET payload='$this->payload' WHERE oid='$this->objectId'"; |
219 |
} |
} |
220 |
} |
} |
221 |
|
|
222 |
function _loadBackend() { |
function _loadRemote() { |
223 |
logp(get_class($this) . "->_loadBackend()", LOG_DEBUG); |
logp(get_class($this) . "->_loadRemote()", LOG_DEBUG); |
224 |
|
|
225 |
// TODO: test backend for reachability first (eventually cache this information and "reset" it by another party) |
// TODO: test backend for reachability first (eventually cache this information and "reset" it by another party) |
226 |
if ($result = $this->backend->call('getObject', $this->objectId)) { |
|
227 |
|
// check for guid or oid |
228 |
|
if($this->meta[guid]) { |
229 |
|
$args = array( guid => $this->objectId, classname => $this->meta[classname] ); |
230 |
|
$result = $this->backend->send('getObjectByGuid', $args ); |
231 |
|
} |
232 |
|
if($this->meta[oid]) { |
233 |
|
$result = $this->backend->send('getObject', $this->objectId); |
234 |
|
} |
235 |
|
|
236 |
|
if ($result) { |
237 |
//print "result: " . dumpVar($result) . "<br>"; |
//print "result: " . dumpVar($result) . "<br>"; |
238 |
if (count($result) == 0) { return; } |
if (count($result) == 0) { return; } |
239 |
|
|
240 |
|
// ----- move this to _encode some times: $this->_encode() |
241 |
$encoder = new TextEncode($result); |
$encoder = new TextEncode($result); |
242 |
$encoder->toISO(); |
$encoder->toISO(); |
243 |
if ($_GET[debug]) { |
if ($_GET[debug]) { |
244 |
print dumpVar($result); |
print dumpVar($result); |
245 |
} |
} |
246 |
$this->payload = serialize($result); |
$this->payload = serialize($result); |
247 |
$this->_saveCache(); |
// ----- move this to _encode some times |
248 |
|
|
249 |
|
$this->_saveProxy(); |
250 |
|
//print "oid: $this->objectId<br>"; |
251 |
|
$this->flushState(); |
252 |
|
} else { |
253 |
|
print "Error in _loadRemote!!!<br>"; |
254 |
} |
} |
255 |
|
|
256 |
} |
} |
257 |
|
|
258 |
function _saveBackend($result) { |
function _saveBackend($result) { |
259 |
logp(get_class($this) . "->_saveBackend()", LOG_DEBUG); |
logp(get_class($this) . "->_saveBackend()", LOG_DEBUG); |
260 |
$encoder = new TextEncode($result); |
|
261 |
$encoder->toUTF8(); |
//$encoder = new TextEncode($result); |
262 |
$response = $this->backend->send('saveObject', array('oid' => $this->objectId, 'data' => $result) ); |
//$encoder->toUTF8(); |
263 |
|
|
264 |
|
// check for guid or oid |
265 |
|
if($this->meta[guid]) { |
266 |
|
$args = array( 'guid' => $this->objectId, 'classname' => $this->meta[classname], 'data' => $result ); |
267 |
|
$response = $this->backend->send('saveObjectByGuid', $args, array( utf8 => 1) ); |
268 |
|
} |
269 |
|
if($this->meta[oid]) { |
270 |
|
$response = $this->backend->send('saveObject', array('oid' => $this->objectId, 'data' => $result), array( utf8 => 1) ); |
271 |
|
} |
272 |
} |
} |
273 |
|
|
274 |
function _decode() { |
function _decode() { |
279 |
$this->meta[decoded] = 1; |
$this->meta[decoded] = 1; |
280 |
} |
} |
281 |
} |
} |
282 |
|
|
283 |
|
function store($struct) { |
284 |
|
$this->payload = serialize($struct); |
285 |
|
$this->_saveProxy(); |
286 |
|
} |
287 |
|
|
288 |
} |
} |
289 |
|
|