|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Use doctrine odm as backend |
|
4
|
|
|
*/ |
|
5
|
|
|
|
|
6
|
|
|
namespace Graviton\RestBundle\Model; |
|
7
|
|
|
|
|
8
|
|
|
use Doctrine\ODM\MongoDB\DocumentRepository; |
|
9
|
|
|
use Graviton\RestBundle\Service\RqlTranslator; |
|
10
|
|
|
use Graviton\Rql\Node\SearchNode; |
|
11
|
|
|
use Graviton\SchemaBundle\Model\SchemaModel; |
|
12
|
|
|
use Graviton\SecurityBundle\Entities\SecurityUser; |
|
13
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
14
|
|
|
use Doctrine\ODM\MongoDB\Query\Builder; |
|
15
|
|
|
use Graviton\Rql\Visitor\MongoOdm as Visitor; |
|
16
|
|
|
use Xiag\Rql\Parser\Node\Query\LogicOperator\AndNode; |
|
17
|
|
|
use Xiag\Rql\Parser\Node\Query\LogicOperator\OrNode; |
|
18
|
|
|
use Xiag\Rql\Parser\Node\Query\ScalarOperator\LikeNode; |
|
19
|
|
|
use Xiag\Rql\Parser\Query; |
|
20
|
|
|
use Graviton\ExceptionBundle\Exception\RecordOriginModifiedException; |
|
21
|
|
|
use Xiag\Rql\Parser\Exception\SyntaxErrorException as RqlSyntaxErrorException; |
|
22
|
|
|
use Graviton\SchemaBundle\Document\Schema as SchemaDocument; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Use doctrine odm as backend |
|
26
|
|
|
* |
|
27
|
|
|
* @author List of contributors <https://github.com/libgraviton/graviton/graphs/contributors> |
|
28
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GNU Public License |
|
29
|
|
|
* @link http://swisscom.ch |
|
30
|
|
|
*/ |
|
31
|
|
|
class DocumentModel extends SchemaModel implements ModelInterface |
|
32
|
|
|
{ |
|
33
|
|
|
/** |
|
34
|
|
|
* @var string |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $description; |
|
37
|
|
|
/** |
|
38
|
|
|
* @var string[] |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $fieldTitles; |
|
41
|
|
|
/** |
|
42
|
|
|
* @var string[] |
|
43
|
|
|
*/ |
|
44
|
|
|
protected $fieldDescriptions; |
|
45
|
|
|
/** |
|
46
|
|
|
* @var string[] |
|
47
|
|
|
*/ |
|
48
|
|
|
protected $requiredFields = array(); |
|
49
|
|
|
/** |
|
50
|
|
|
* @var string[] |
|
51
|
|
|
*/ |
|
52
|
|
|
protected $searchableFields = array(); |
|
53
|
|
|
/** |
|
54
|
|
|
* @var DocumentRepository |
|
55
|
|
|
*/ |
|
56
|
|
|
private $repository; |
|
57
|
|
|
/** |
|
58
|
|
|
* @var Visitor |
|
59
|
|
|
*/ |
|
60
|
|
|
private $visitor; |
|
61
|
|
|
/** |
|
62
|
|
|
* @var array |
|
63
|
|
|
*/ |
|
64
|
|
|
protected $notModifiableOriginRecords; |
|
65
|
|
|
/** |
|
66
|
|
|
* @var integer |
|
67
|
|
|
*/ |
|
68
|
|
|
private $paginationDefaultLimit; |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @var boolean |
|
72
|
|
|
*/ |
|
73
|
|
|
protected $filterByAuthUser; |
|
74
|
|
|
|
|
75
|
2 |
|
/** |
|
76
|
|
|
* @var string |
|
77
|
2 |
|
*/ |
|
78
|
2 |
|
protected $filterByAuthField; |
|
79
|
2 |
|
|
|
80
|
2 |
|
/** |
|
81
|
2 |
|
* @var RqlTranslator |
|
82
|
|
|
*/ |
|
83
|
|
|
protected $translator; |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @param Visitor $visitor rql query visitor |
|
87
|
|
|
* @param RqlTranslator $translator Translator for query modification |
|
88
|
|
|
* @param array $notModifiableOriginRecords strings with not modifiable recordOrigin values |
|
89
|
|
|
* @param integer $paginationDefaultLimit amount of data records to be returned when in pagination context |
|
90
|
|
|
*/ |
|
91
|
|
|
public function __construct( |
|
92
|
|
|
Visitor $visitor, |
|
93
|
|
|
RqlTranslator $translator, |
|
94
|
|
|
$notModifiableOriginRecords, |
|
95
|
|
|
$paginationDefaultLimit |
|
96
|
|
|
) { |
|
97
|
|
|
parent::__construct(); |
|
98
|
|
|
$this->visitor = $visitor; |
|
99
|
|
|
$this->translator = $translator; |
|
100
|
2 |
|
$this->notModifiableOriginRecords = $notModifiableOriginRecords; |
|
101
|
|
|
$this->paginationDefaultLimit = (int) $paginationDefaultLimit; |
|
102
|
2 |
|
} |
|
103
|
|
|
|
|
104
|
2 |
|
/** |
|
105
|
|
|
* get repository instance |
|
106
|
|
|
* |
|
107
|
|
|
* @return DocumentRepository |
|
108
|
|
|
*/ |
|
109
|
|
|
public function getRepository() |
|
110
|
|
|
{ |
|
111
|
|
|
return $this->repository; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
1 |
|
* create new app model |
|
116
|
|
|
* |
|
117
|
1 |
|
* @param DocumentRepository $repository Repository of countries |
|
118
|
1 |
|
* |
|
119
|
1 |
|
* @return \Graviton\RestBundle\Model\DocumentModel |
|
120
|
|
|
*/ |
|
121
|
|
|
public function setRepository(DocumentRepository $repository) |
|
122
|
1 |
|
{ |
|
123
|
1 |
|
$this->repository = $repository; |
|
124
|
|
|
|
|
125
|
1 |
|
return $this; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* {@inheritDoc} |
|
130
|
1 |
|
* |
|
131
|
|
|
* @param Request $request The request object |
|
132
|
|
|
* @param SecurityUser $user SecurityUser Object |
|
|
|
|
|
|
133
|
|
|
* @param SchemaDocument $schema Schema model used for search fields extraction |
|
|
|
|
|
|
134
|
|
|
* |
|
135
|
|
|
* @return array |
|
136
|
|
|
*/ |
|
137
|
|
|
public function findAll(Request $request, SecurityUser $user = null, SchemaDocument $schema = null) |
|
138
|
1 |
|
{ |
|
139
|
|
|
$pageNumber = $request->query->get('page', 1); |
|
140
|
|
|
$numberPerPage = (int) $request->query->get('perPage', $this->getDefaultLimit()); |
|
141
|
|
|
$startAt = ($pageNumber - 1) * $numberPerPage; |
|
142
|
1 |
|
|
|
143
|
1 |
|
/** @var \Doctrine\ODM\MongoDB\Query\Builder $queryBuilder */ |
|
144
|
1 |
|
$queryBuilder = $this->repository |
|
145
|
|
|
->createQueryBuilder(); |
|
146
|
|
|
|
|
147
|
|
|
if ($this->filterByAuthUser && $user && $user->hasRole(SecurityUser::ROLE_USER)) { |
|
148
|
1 |
|
$queryBuilder->field($this->filterByAuthField)->equals($user->getUser()->getId()); |
|
149
|
1 |
|
} |
|
150
|
1 |
|
|
|
151
|
|
|
|
|
152
|
|
|
$searchableFields = $this->getSearchableFields(); |
|
153
|
|
|
if (!is_null($schema)) { |
|
154
|
|
|
$searchableFields = $schema->getSearchable(); |
|
155
|
1 |
|
} |
|
156
|
|
|
|
|
157
|
|
|
// *** do we have an RQL expression, do we need to filter data? |
|
158
|
|
|
if ($request->attributes->get('hasRql', false)) { |
|
159
|
|
|
$queryBuilder = $this->doRqlQuery( |
|
160
|
|
|
$queryBuilder, |
|
161
|
|
|
$this->translator->translateSearchQuery( |
|
162
|
|
|
$request->attributes->get('rqlQuery'), |
|
163
|
|
|
$searchableFields |
|
|
|
|
|
|
164
|
1 |
|
) |
|
165
|
1 |
|
); |
|
166
|
1 |
|
} else { |
|
167
|
|
|
// @todo [lapistano]: seems the offset is missing for this query. |
|
168
|
|
|
/** @var \Doctrine\ODM\MongoDB\Query\Builder $qb */ |
|
169
|
1 |
|
$queryBuilder->find($this->repository->getDocumentName()); |
|
170
|
1 |
|
} |
|
171
|
|
|
|
|
172
|
1 |
|
// define offset and limit |
|
173
|
1 |
|
if (!array_key_exists('skip', $queryBuilder->getQuery()->getQuery())) { |
|
174
|
1 |
|
$queryBuilder->skip($startAt); |
|
|
|
|
|
|
175
|
1 |
|
} else { |
|
176
|
|
|
$startAt = (int) $queryBuilder->getQuery()->getQuery()['skip']; |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
if (!array_key_exists('limit', $queryBuilder->getQuery()->getQuery())) { |
|
180
|
|
|
$queryBuilder->limit($numberPerPage); |
|
|
|
|
|
|
181
|
|
|
} else { |
|
182
|
|
|
$numberPerPage = (int) $queryBuilder->getQuery()->getQuery()['limit']; |
|
183
|
|
|
} |
|
184
|
1 |
|
|
|
185
|
|
|
// Limit can not be negative nor null. |
|
186
|
|
|
if ($numberPerPage < 1) { |
|
187
|
|
|
throw new RqlSyntaxErrorException('negative or null limit in rql'); |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
/** |
|
191
|
|
|
* add a default sort on id if none was specified earlier |
|
192
|
|
|
* |
|
193
|
|
|
* not specifying something to sort on leads to very weird cases when fetching references |
|
194
|
|
|
*/ |
|
195
|
|
|
if (!array_key_exists('sort', $queryBuilder->getQuery()->getQuery())) { |
|
196
|
|
|
$queryBuilder->sort('_id'); |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
// run query |
|
200
|
|
|
$query = $queryBuilder->getQuery(); |
|
201
|
|
|
$records = array_values($query->execute()->toArray()); |
|
202
|
|
|
|
|
203
|
|
|
$totalCount = $query->count(); |
|
204
|
|
|
$numPages = (int) ceil($totalCount / $numberPerPage); |
|
205
|
|
|
$page = (int) ceil($startAt / $numberPerPage) + 1; |
|
206
|
|
|
if ($numPages > 1) { |
|
207
|
1 |
|
$request->attributes->set('paging', true); |
|
208
|
|
|
$request->attributes->set('page', $page); |
|
209
|
1 |
|
$request->attributes->set('numPages', $numPages); |
|
210
|
|
|
$request->attributes->set('startAt', $startAt); |
|
211
|
|
|
$request->attributes->set('perPage', $numberPerPage); |
|
212
|
|
|
$request->attributes->set('totalCount', $totalCount); |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
return $records; |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
/** |
|
219
|
|
|
* @param \Graviton\I18nBundle\Document\Translatable $entity entity to insert |
|
220
|
1 |
|
* |
|
221
|
|
|
* @return Object |
|
222
|
1 |
|
*/ |
|
223
|
|
View Code Duplication |
public function insertRecord($entity) |
|
|
|
|
|
|
224
|
1 |
|
{ |
|
225
|
1 |
|
$this->checkIfOriginRecord($entity); |
|
226
|
1 |
|
$manager = $this->repository->getDocumentManager(); |
|
227
|
1 |
|
$manager->persist($entity); |
|
228
|
|
|
$manager->flush($entity); |
|
229
|
1 |
|
|
|
230
|
|
|
return $this->find($entity->getId()); |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
/** |
|
234
|
|
|
* @param string $documentId id of entity to find |
|
235
|
|
|
* |
|
236
|
|
|
* @return Object |
|
237
|
|
|
*/ |
|
238
|
|
|
public function find($documentId) |
|
239
|
1 |
|
{ |
|
240
|
|
|
return $this->repository->find($documentId); |
|
241
|
1 |
|
} |
|
242
|
1 |
|
|
|
243
|
|
|
/** |
|
244
|
1 |
|
* {@inheritDoc} |
|
245
|
1 |
|
* |
|
246
|
1 |
|
* @param string $documentId id of entity to update |
|
247
|
1 |
|
* @param Object $entity new entity |
|
248
|
1 |
|
* |
|
249
|
1 |
|
* @return Object |
|
250
|
1 |
|
*/ |
|
251
|
|
View Code Duplication |
public function updateRecord($documentId, $entity) |
|
|
|
|
|
|
252
|
1 |
|
{ |
|
253
|
|
|
$manager = $this->repository->getDocumentManager(); |
|
254
|
|
|
// In both cases the document attribute named originRecord must not be 'core' |
|
255
|
|
|
$this->checkIfOriginRecord($entity); |
|
256
|
|
|
$this->checkIfOriginRecord($this->find($documentId)); |
|
257
|
|
|
$entity = $manager->merge($entity); |
|
258
|
|
|
$manager->flush(); |
|
259
|
|
|
|
|
260
|
1 |
|
return $entity; |
|
261
|
|
|
} |
|
262
|
1 |
|
|
|
263
|
|
|
/** |
|
264
|
|
|
* {@inheritDoc} |
|
265
|
|
|
* |
|
266
|
|
|
* @param string $documentId id of entity to delete |
|
267
|
|
|
* |
|
268
|
|
|
* @return null|Object |
|
269
|
|
|
*/ |
|
270
|
|
|
public function deleteRecord($documentId) |
|
271
|
|
|
{ |
|
272
|
|
|
$manager = $this->repository->getDocumentManager(); |
|
273
|
|
|
$entity = $this->find($documentId); |
|
274
|
|
|
|
|
275
|
|
|
$return = $entity; |
|
276
|
|
|
if ($entity) { |
|
277
|
|
|
$this->checkIfOriginRecord($entity); |
|
278
|
|
|
$manager->remove($entity); |
|
279
|
|
|
$manager->flush(); |
|
280
|
|
|
$return = null; |
|
281
|
|
|
} |
|
282
|
|
|
|
|
283
|
|
|
return $return; |
|
284
|
|
|
} |
|
285
|
|
|
|
|
286
|
|
|
/** |
|
287
|
|
|
* get classname of entity |
|
288
|
|
|
* |
|
289
|
|
|
* @return string |
|
290
|
|
|
*/ |
|
291
|
|
|
public function getEntityClass() |
|
292
|
|
|
{ |
|
293
|
|
|
return $this->repository->getDocumentName(); |
|
294
|
|
|
} |
|
295
|
|
|
|
|
296
|
|
|
/** |
|
297
|
|
|
* {@inheritDoc} |
|
298
|
|
|
* |
|
299
|
|
|
* Currently this is being used to build the route id used for redirecting |
|
300
|
|
|
* to newly made documents. It might benefit from having a different name |
|
301
|
|
|
* for those purposes. |
|
302
|
|
|
* |
|
303
|
|
|
* We might use a convention based mapping here: |
|
304
|
|
|
* Graviton\CoreBundle\Document\App -> mongodb://graviton_core |
|
305
|
|
|
* Graviton\CoreBundle\Entity\Table -> mysql://graviton_core |
|
306
|
|
|
* |
|
307
|
|
|
* @todo implement this in a more convention based manner |
|
308
|
|
|
* |
|
309
|
8 |
|
* @return string |
|
310
|
|
|
*/ |
|
311
|
|
|
public function getConnectionName() |
|
312
|
8 |
|
{ |
|
313
|
7 |
|
$bundle = strtolower(substr(explode('\\', get_class($this))[1], 0, -6)); |
|
314
|
3 |
|
|
|
315
|
3 |
|
return 'graviton.' . $bundle; |
|
316
|
|
|
} |
|
317
|
3 |
|
|
|
318
|
1 |
|
/** |
|
319
|
|
|
* Does the actual query using the RQL Bundle. |
|
320
|
1 |
|
* |
|
321
|
|
|
* @param Builder $queryBuilder Doctrine ODM QueryBuilder |
|
322
|
2 |
|
* @param Query $query query from parser |
|
323
|
6 |
|
* |
|
324
|
|
|
* @return array |
|
|
|
|
|
|
325
|
|
|
*/ |
|
326
|
|
|
protected function doRqlQuery($queryBuilder, Query $query) |
|
327
|
|
|
{ |
|
328
|
|
|
$this->visitor->setBuilder($queryBuilder); |
|
329
|
|
|
|
|
330
|
1 |
|
return $this->visitor->visit($query); |
|
331
|
|
|
} |
|
332
|
1 |
|
|
|
333
|
1 |
|
/** |
|
334
|
|
|
* Checks the recordOrigin attribute of a record and will throw an exception if value is not allowed |
|
335
|
|
|
* |
|
336
|
|
|
* @param Object $record record |
|
337
|
|
|
* |
|
338
|
|
|
* @return void |
|
339
|
|
|
*/ |
|
340
|
|
|
protected function checkIfOriginRecord($record) |
|
341
|
|
|
{ |
|
342
|
|
|
if ($record instanceof RecordOriginInterface |
|
343
|
|
|
&& !$record->isRecordOriginModifiable() |
|
344
|
2 |
|
) { |
|
345
|
|
|
$values = $this->notModifiableOriginRecords; |
|
346
|
2 |
|
$originValue = strtolower(trim($record->getRecordOrigin())); |
|
347
|
2 |
|
|
|
348
|
2 |
|
if (in_array($originValue, $values)) { |
|
349
|
|
|
$msg = sprintf("Must not be one of the following keywords: %s", implode(', ', $values)); |
|
350
|
|
|
|
|
351
|
|
|
throw new RecordOriginModifiedException($msg); |
|
352
|
|
|
} |
|
353
|
|
|
} |
|
354
|
|
|
} |
|
355
|
|
|
|
|
356
|
|
|
/** |
|
357
|
|
|
* Determines the configured amount fo data records to be returned in pagination context. |
|
358
|
|
|
* |
|
359
|
|
|
* @return int |
|
360
|
|
|
*/ |
|
361
|
|
|
private function getDefaultLimit() |
|
362
|
|
|
{ |
|
363
|
|
|
if (0 < $this->paginationDefaultLimit) { |
|
364
|
|
|
return $this->paginationDefaultLimit; |
|
365
|
|
|
} |
|
366
|
|
|
|
|
367
|
|
|
return 10; |
|
368
|
|
|
} |
|
369
|
|
|
|
|
370
|
|
|
/** |
|
371
|
|
|
* @param Boolean $active active |
|
372
|
|
|
* @param String $field field |
|
373
|
|
|
* @return void |
|
374
|
|
|
*/ |
|
375
|
|
|
public function setFilterByAuthUser($active, $field) |
|
376
|
|
|
{ |
|
377
|
|
|
$this->filterByAuthUser = is_bool($active) ? $active : false; |
|
378
|
|
|
$this->filterByAuthField = $field; |
|
379
|
|
|
} |
|
380
|
|
|
} |
|
381
|
|
|
|
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.