-
Notifications
You must be signed in to change notification settings - Fork 0
/
MongOMDatastore.php
610 lines (477 loc) · 20.9 KB
/
MongOMDatastore.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
<?php
namespace com\bazaar\core\mongom;
use com\bazaar\core as core;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
* Description of MongOMDatastore
*
* @author gourav sarkar
*/
class MongOMDatastore {
//put your code here
private $connection;
public function __construct(\MongoClient $mongoCon) {
$this->connection = $mongoCon->{PROJECT_NAME};
}
public function getConnection() {
return $this->connection;
}
/**
*
* @param \com\bazaar\core\Model $model
* @param type $options
* @throws \RuntimeException
*
* @todo Handle property with different files
*
* This method takes one or more model
* Access its mapper
* get the models value mapped by the appropiate mapper
* handle scalar and embeded document
* prepare the data
* insert to database
* check for valid transaction
* update the id of corresponding models
*
*/
private function prepareSave(core\Model $model, $options = []) {
$data = []; //converted data for DB transaction
$propertyData = ''; //property data accesed using getter
$getter = ''; //getter name
//Get mapper for each model
$mapper = new MongOM($model);
//xdebug_var_dump("Property map of " . $mapper->getEntity()['name'], $mapper->getProperties());
//Recover property value
//loop through each property and populate the array with data
foreach ($mapper->getProperties() as $property => $propertyMeta) {
$propertyName = (isset($propertyMeta['name'])) ? $propertyMeta['name'] : $property;
//xdebug_var_dump("PROPPP",$property);
//getter method must have been set otherwise use default getter
$getter = (isset($propertyMeta['getter'])) ? $propertyMeta['getter'] : $getter = "get{$property}";
//xdebug_var_dump($getter);
//If getter is not there throw exception
//@todo change to specific exception
if (!method_exists($model, $getter)) {
throw new \RuntimeException("MongoM: Getter method is not defined for model {$model} and property {$property}");
}
//Access property using getter
$propertyData = $model->$getter();
/*
* Handle embeded document
*/
if (isset($propertyMeta['isEmbeded']) && $propertyMeta['isEmbeded'] == true) {
/*
* Handle many documents
*/
if ($propertyMeta['hasMany'] == false) {
$data[$propertyName] = $this->prepareSave($propertyData, $options);
} else {
//Initialize for blank properties
$data[$propertyName] = [];
assert('$propertyData instanceof Traversable /* Probable data mis match of property and propertylist */');
//assert(empty($propertyData));
/*
* Handle non empty list of Embeded document
* Handle Countable interface and array
*/
if (!empty($propertyData) || ($propertyData instanceof \Traversable && $propertyData instanceof \Countable && $propertyData->count() !== 0)) {
//trigger_error(\count($propertyData));
foreach ($propertyData as $modelItem) {
//Embeded documents should be flatened and push to array
$flatenModel = $this->prepareSave($modelItem, $options);
//Do not add blank models
if (!empty($flatenModel)) {
$data[$propertyName][] = $flatenModel;
}
}
}
}
}
/*
* Handle referenced document
*/ elseif (isset($propertyMeta['isReference']) && $propertyMeta['isReference'] == true) {
/*
* Handle single reference document
*/
if ($propertyMeta['hasMany'] == false) {
/*
* Handle FK and id fetching depending on key FK and index
* index must be unique
* @todo assert if method exist or not
* @todo assert for null value in id (FK)
*/
if (isset($propertyMeta['fk']) && isset($propertyMeta['index']) && $propertyMeta['index'] == 'unique') {
$data[$propertyName] = $propertyData->{"get{$propertyMeta['fk']}"}();
//xdebug_var_dump($data[$propertyName]);
} else {
$data[$propertyName] = $propertyData->getID();
}
}
/*
* Handle many reference document
*/ else {
$data[$propertyName] = []; //initalize property
/*
* Handle non empty list of reference
* Handle Countable interface and array
*/
if (!empty($propertyData) || ($propertyData instanceof \Traversable && $propertyData instanceof \Countable && $propertyData->count() != 0)) {
//xdebug_var_dump($propertyData);
//assert(false);
foreach ($propertyData as $modelItem) {
//xdebug_var_dump($modelItem);
assert('$modelItem instanceof \com\bazaar\core\Model');
$refID = $modelItem->getID();
//$refID= (empty($refID))? new \MongoId() : $refID;
//xdebug_var_dump("REFID",$modelItem);
$data[$propertyName][] = $refID;
//
}
}
}
}
/*
* Handle scalar fields and PHP array fields null fields
*/ else if (is_scalar($propertyData) || is_array($propertyData)) {
//xdebug_var_dump("scalar " . $property);
/*
* Handle id
* id is always scalar dont execute later part if id is found
*/
$data[$propertyName] = $propertyData;
} elseif (is_null($propertyData)) {
//Do not add null value for id
if (!isset($propertyMeta['id']) || $propertyMeta['id'] == false) {
$data[$propertyName] = $propertyData;
}
}
/*
* Handle other data types
*/ else {
//xdebug_var_dump($propertyData, $property);
//throw new \RuntimeException("Attempt to Unknown data type conversion of " . $propertyData);
}
}
return $data;
}
private function save($models, $options = []) {
//xdebug_var_dump("Save");
$batchData = [];
$data = [];
$collection = [];
//Loop through all object
//get map
//get data
//append
$models->rewind();
while ($models->valid()) {
$model = $models->current();
//xdebug_var_dump("Log object========");
//xdebug_var_dump("Model", $model);
//xdebug_var_dump("Log object========");
/*
* This part are used to get only collection name
*
*/
$mapper = new MongOM($model);
$entity = $mapper->getEntity();
$collection = $entity[MongOM::IDF_COL_NAME];
//==================================
$data = $this->prepareSave($model);
//@todo unset if it is empty other wise retain its value
unset($data['_id']);
// xdebug_var_dump("###################################################", "COLLECTION_NAME $collection", $data);
$this->connection->{$collection}->insert($data, ["w" => true]);
$model->setID($data["_id"]);
$models->next();
UnitOfWork::getInstance()->unregisterNew($model);
UnitOfWork::getInstance()->registerClean($model);
$batchData[] = $data;
// xdebug_var_dump($data);
//xdebug_var_dump("#################################################");
}
//xdebug_var_dump("Final data", $data);
//@throws Throw exception if $entity['name'] is no there
//do not try to insert if there is no data
if (!empty($batchData)) {
//xdebug_var_dump("Data for insert processing BATCHDATA", count($batchData), $batchData);
//$this->connection->{$collection}->batchInsert($batchData,$options);
}
return $batchData;
}
/**
*
* @param core\Model $updateModels
* @param QueryObject $queryObject
*
* Only valid for cleaned object laoded from DB
*
* @todo Update now onl;y handles list collection using $pushAll. Update will be made
* to mae support set collection using $addToSet
*
*/
public function update(core\Model $updateModel, QueryObject $queryObject = NULL, $options = []) {
// xdebug_var_dump("Update new objects " . $updateModels->count());
$updateModelSerialized=[];
$modelMap = new MongOM($updateModel);
$data = $this->prepareSave($updateModel, $options);
//xdebug_var_dump('Data' ,$data);
//xdebug_var_dump($modelMap->getProperties());
/*
* @todo Replace flag
* If REPLACE flag is off
* Remove mongo id
*/
unset($data['_id']);
$collectionFields = array_filter($data, function ($value) {
//xdebug_var_dump($value,is_array($value));
return is_array($value);
}
);
$singularFields = array_diff_key($data, $collectionFields);
//xdebug_var_dump($collectionFields,$singularFields);
$updateModelSerialized=['$set' => $singularFields, '$pushAll' => $collectionFields];
//xdebug_var_dump($updateModelSerialized,$queryObject->getQuery());
/*
* @todo will be removed when untiofworker will be chnaged.
* Commits will be made from unitof work
*/
$collection=$modelMap->getEntity();
//xdebug_var_dump($collection);
$updatestatus=$this->connection->$collection['name']->update($queryObject->getQuery(),$updateModelSerialized,$options);
// xdebug_var_dump($queryObject->getQuery(),$updatestatus);
}
/**
*
* @param \com\bazaar\core\mongom\MongOM $map
* @param array $data
* @param bool $fromNonDB generate object from Model directly or Database using map
* @return \com\bazaar\core\mongom\className
* @throws \RuntimeException
*/
public static function loadObject(MongOM $map, $data, $fromNonDB = true) {
$classMeta = $map->getEntity();
$className = $classMeta[MongOM::IDF_DOCUMENT_REF];
$setterName = $getterName = '';
/*
* @todo will be removed when unitofwork workflow will be changed
*
* _id is mandatory but some documents like embeded document may not have
* _id
*/
$id = ($fromNonDB) ? 'id' : '_id';
$modelID = (!empty($data[$id])) ? $data[$id] : null;
$model = new $className($modelID);
//xdebug_var_dump($data);
/*
* Loop through all fields that does not belongs to database or persitence storage
* these fields are usually generated from calculated field or mostly
* Aggregated data.
*
* Currently it handles only scalar values
*/
foreach ($map->getPropertiesNonDBMapped($data) as $nondbfield) {
$setterName = "set{$nondbfield}";
if (!method_exists($model, $setterName)) {
throw new \RuntimeException("Setter method is not available for {$nondbfield}");
}
/*
* @todo id mapping
* Skip ID field
*/
//xdebug_var_dump($nondbfield);
$model->$setterName($data[$nondbfield]);
}
/*
* Loop through all property
*/
//xdebug_var_dump($map->getPropertiesDBMapped());
foreach ($map->getPropertiesDBMapped() as $propertyNameModel => $propertyNameDB) {
if ($fromNonDB) {
$propertyNameDB = $propertyNameModel;
}
//xdebug_var_dump($data);
$propertyMap = $map->getProperties()[$propertyNameModel];
/*
* @important
* use this setter getter name later in code for setter and getter
* $propertyName matches with model property name so it matches with DEFAYLT setter name
*/
$setterName = (isset($propertyMap['setter'])) ? $propertyMap['setter'] : "set{$propertyNameModel}";
$getterName = (isset($propertyMap['getter'])) ? $propertyMap['getter'] : "get{$propertyNameModel}";
//xdebug_var_dump($setterName);
if (!method_exists($model, $setterName)) {
throw new \RuntimeException("Setter method is not available for {$propertyNameModel} in {$className}");
}
//$propertyName = (isset($propertyMap['name'])) ? $propertyMap['name'] : $propertyName;
// xdebug_var_dump($propertyNameDB);
/*
* 'reference' means it can be embeded or it can be referenced
*/
if (isset($propertyMap['reference'])) {
//Get map for object
$className = $propertyMap['reference'];
$refPropMap = new MongOM(new $className());
/*
* Handle many documents
*/
if (isset($propertyMap['hasMany']) && $propertyMap['hasMany']) {
/**
* @internal Get the list and update it instead of creating new
* that way it dont need to look for list type and use
* ArrayAccess interface to manipulate its data
* Also it will by pass need of setter method for list
*
* create new map for heterogenous collecion otherwise use same
* map
*/
$list = $model->{$getterName}();
assert('$list instanceof ArrayAccess');
foreach ($data[$propertyNameDB] as $listItemModelData) {
//xdebug_var_dump("listdata",$listItemModelData);
if (isset($propertyMap['isEmbeded']) && $propertyMap['isEmbeded']) {
$list[] = MongOMDatastore::loadObject($refPropMap, $listItemModelData, false);
} else {
/*
* only id reference is stored in list. so just get the id
* Create object with only id
*/
$className = $propertyMap['reference'];
$list[] = new $className($listItemModelData);
}
}
} else {
//xdebug_var_dump($fkFlag);
/*
* Handle embeded data
*/
if (isset($propertyMap['isEmbeded']) && $propertyMap['isEmbeded']) {
$model->{$setterName}(MongOMDatastore::loadObject($refPropMap, $data[$propertyNameDB], false));
}
/*
* Handel refrence data which could have default FK or explicit FK
*
* @todo index and fk comaptibility could be tested here also
*/ else if (isset($propertyMap['isReference']) && $propertyMap['isReference']) {
//xdebug_var_dump("Reference");
/*
* explicit FK
* Dont iterate. initialize the model and set its properties
*/
if (isset($propertyMap['fk'])) {
//xdebug_var_dump($model);
$fkModel = new $propertyMap['reference']();
/*
* @todo setter existance valdiation
* @todo mark incomplete or ghost object marking for lazy loading
*/
$fkModel->{"set{$propertyMap['fk']}"}($data[$propertyNameDB]);
$model->$setterName($fkModel);
}
/*
* Default _id FK
*/ else {
$model->{$setterName}(MongOMDatastore::loadObject($refPropMap, $data[$propertyNameDB], false));
}
}
}
}
/*
* Handle scalar data
*/ else {
/*
$propertyData = (isset($data[$propertyName])) ? $data[$propertyName] : '';
$model->{$setterName}($propertyData);
*
*/
//xdebug_var_dump($propertyMap);
/*
* If data is not available use default data stored in property init
* In models
*/
if (isset($data[$propertyNameDB])) {
//xdebug_var_dump("$setterName setting data ");
/*
* @todo will be removed when unitofwork workflow will be changed
*/
if (isset($propertyMap['id']) && $propertyMap['id']) {
break;
}
$model->{$setterName}($data[$propertyNameDB]);
}
}
}
/**
* @internal If object is loaded from DB it is clean and complete. So it should be marked as clean
* @todo Will be removed in future
*/
if (!$fromNonDB) {
UnitOfWork::getInstance()->registerclean($model);
}
return $model;
}
/*
* Public interface
*/
public function insert(core\Model $model) {
//xdebug_var_dump('insert');
UnitOfWork::getInstance()->registerNew($model);
}
/*
public function remove(core\Model $model) {
UnitOfWork::getInstance()->registerRemoved($model);
}
*
*/
public function commit($options = []) {
//xdebug_var_dump("Unit of work commiting..");
$this->save(UnitOfWork::getInstance()->getNewObjects());
// UnitOfWork::getInstance()->doRemove();
// UnitOfWork::getInstance()->doUpdate();
}
public function query($model, QueryInterface $queryObject) {
$modelList = [];
$query = $queryObject->getQuery();
//xdebug_var_dump($model);
$mapper = new MongOM($model);
$queryObject->setMapper($mapper);
$collection = $mapper->getEntity();
//xdebug_var_dump($collection[MongOM::IDF_COL_NAME], $query);
if ($queryObject instanceof QueryObject) {
/* @var $cursor MongoCursor */
$cursor = $this->connection->{$collection[MongOM::IDF_COL_NAME]}->find($query);
/*
* Handle pagination
*/
//xdebug_var_dump($queryObject->getLimit(),$queryObject->getSkip());
/*
* Only for query object . not for aggregate
*/
$cursor->limit($queryObject->getLimit());
$cursor->skip($queryObject->getSkip());
} else if ($queryObject instanceof aggregationframework\AggregateInterface) {
$cursor = $this->connection->{$collection[MongOM::IDF_COL_NAME]}->aggregateCursor($query);
}
//xdebug_var_dump($cursor->count(true));
/*
* Could be change to return only one cursor
* Detecting NO RESULT is more generic and suitable
* Further limit can be made later
*/
if ($cursor->count(true) == 0) {
throw new core\rest\ResourceNotFoundException("Specified Resource could not be found");
}
foreach ($cursor as $document) {
//Same mapper will be used
//xdebug_var_dump($document);
$model = MongOMDatastore::loadObject($mapper, $document, false);
$modelList[] = $model;
}
//xdebug_var_dump(count($modelList));
return $modelList;
}
}
?>