Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
83.00% |
83 / 100 |
|
64.71% |
11 / 17 |
CRAP | |
0.00% |
0 / 1 |
RdfValueCollectionRepository | |
83.00% |
83 / 100 |
|
64.71% |
11 / 17 |
46.47 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
isApplicable | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
findAll | |
100.00% |
21 / 21 |
|
100.00% |
1 / 1 |
3 | |||
persist | |
72.22% |
13 / 18 |
|
0.00% |
0 / 1 |
8.05 | |||
delete | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
verifyUriUniqueness | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
20 | |||
insert | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
update | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
3 | |||
enrichQueryWithOrderBy | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
enrichWithLimit | |
75.00% |
3 / 4 |
|
0.00% |
0 / 1 |
3.14 | |||
enrichQueryWithValueCollectionSearchCondition | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
4 | |||
enrichQueryWithSubject | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
enrichQueryWithExcludedValueUris | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
enrichQueryWithObjects | |
50.00% |
2 / 4 |
|
0.00% |
0 / 1 |
2.50 | |||
getPersistence | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
count | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
getQuery | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | /** |
4 | * This program is free software; you can redistribute it and/or |
5 | * modify it under the terms of the GNU General Public License |
6 | * as published by the Free Software Foundation; under version 2 |
7 | * of the License (non-upgradable). |
8 | * |
9 | * This program is distributed in the hope that it will be useful, |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | * GNU General Public License for more details. |
13 | * |
14 | * You should have received a copy of the GNU General Public License |
15 | * along with this program; if not, write to the Free Software |
16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
17 | * |
18 | * Copyright (c) 2020-2022 (original work) Open Assessment Technologies SA; |
19 | * |
20 | * @author Sergei Mikhailov <sergei.mikhailov@taotesting.com> |
21 | */ |
22 | |
23 | declare(strict_types=1); |
24 | |
25 | namespace oat\tao\model\Lists\DataAccess\Repository; |
26 | |
27 | use common_exception_Error; |
28 | use core_kernel_classes_Class as KernelClass; |
29 | use core_kernel_classes_Property as KernelProperty; |
30 | use core_kernel_classes_Resource as KernelResource; |
31 | use oat\generis\model\kernel\persistence\smoothsql\search\ComplexSearchService; |
32 | use oat\generis\model\OntologyAwareTrait; |
33 | use oat\generis\model\OntologyRdf; |
34 | use oat\generis\model\OntologyRdfs; |
35 | use oat\generis\persistence\PersistenceManager; |
36 | use oat\search\base\QueryBuilderInterface; |
37 | use oat\search\base\QueryCriterionInterface; |
38 | use oat\search\base\QueryInterface; |
39 | use oat\tao\model\Lists\Business\Contract\ValueCollectionRepositoryInterface; |
40 | use oat\tao\model\Lists\Business\Domain\CollectionType; |
41 | use oat\tao\model\Lists\Business\Domain\Value; |
42 | use oat\tao\model\Lists\Business\Domain\ValueCollection; |
43 | use oat\tao\model\Lists\Business\Domain\ValueCollectionSearchRequest; |
44 | use oat\tao\model\service\InjectionAwareService; |
45 | |
46 | class RdfValueCollectionRepository extends InjectionAwareService implements ValueCollectionRepositoryInterface |
47 | { |
48 | use OntologyAwareTrait; |
49 | |
50 | public const SERVICE_ID = 'tao/ValueCollectionRepository'; |
51 | |
52 | /** @var PersistenceManager */ |
53 | private $persistenceManager; |
54 | |
55 | /** @var string */ |
56 | private $persistenceId; |
57 | |
58 | public function __construct(PersistenceManager $persistenceManager, string $persistenceId) |
59 | { |
60 | parent::__construct(); |
61 | |
62 | $this->persistenceManager = $persistenceManager; |
63 | $this->persistenceId = $persistenceId; |
64 | } |
65 | |
66 | public function isApplicable(string $collectionUri): bool |
67 | { |
68 | return CollectionType::fromCollectionUri($collectionUri)->equals(CollectionType::default()); |
69 | } |
70 | |
71 | public function findAll(ValueCollectionSearchRequest $searchRequest): ValueCollection |
72 | { |
73 | /** @var ComplexSearchService $search */ |
74 | $search = $this->getModel()->getSearchInterface(); |
75 | $queryBuilder = $search->query(); |
76 | |
77 | $query = $this->getQuery($search, $queryBuilder, $searchRequest); |
78 | $this->enrichWithLimit($searchRequest, $queryBuilder); |
79 | $this->enrichQueryWithValueCollectionSearchCondition($searchRequest, $query); |
80 | $this->enrichQueryWithSubject($searchRequest, $query); |
81 | $this->enrichQueryWithExcludedValueUris($searchRequest, $query); |
82 | $this->enrichQueryWithObjects($searchRequest, $query); |
83 | $this->enrichQueryWithOrderBy($queryBuilder); |
84 | |
85 | $values = []; |
86 | $data = $search->getGateway()->searchTriples($queryBuilder, OntologyRdfs::RDFS_LABEL); |
87 | foreach ($data as $triple) { |
88 | $values[] = new Value( |
89 | $triple->id, |
90 | $triple->subject, |
91 | $triple->object |
92 | ); |
93 | } |
94 | |
95 | if ($searchRequest->hasValueCollectionUri()) { |
96 | $valueCollectionUri = $searchRequest->getValueCollectionUri(); |
97 | } else { |
98 | $valueCollectionUri = null; |
99 | } |
100 | |
101 | return new ValueCollection($valueCollectionUri, ...$values); |
102 | } |
103 | |
104 | public function persist(ValueCollection $valueCollection): bool |
105 | { |
106 | if ($valueCollection->hasDuplicates()) { |
107 | throw new ValueConflictException("Value Collection {$valueCollection->getUri()} has duplicate values."); |
108 | } |
109 | |
110 | $persistValueCollectionAction = function () use ($valueCollection): void { |
111 | foreach ($valueCollection as $value) { |
112 | $this->verifyUriUniqueness($value); |
113 | |
114 | if (null === $value->getId()) { |
115 | $this->insert($valueCollection, $value); |
116 | } else { |
117 | $this->update($value); |
118 | } |
119 | } |
120 | }; |
121 | |
122 | try { |
123 | $platform = $this->getPersistence(); |
124 | if ($platform instanceof \common_persistence_Transactional) { |
125 | $platform->transactional($persistValueCollectionAction); |
126 | } else { |
127 | $persistValueCollectionAction(); |
128 | } |
129 | return true; |
130 | } catch (ValueConflictException $exception) { |
131 | throw $exception; |
132 | } catch (\Throwable $exception) { |
133 | return false; |
134 | } |
135 | } |
136 | |
137 | /** |
138 | * @param string $valueCollectionUri |
139 | * |
140 | * @throws common_exception_Error |
141 | */ |
142 | public function delete(string $valueCollectionUri): void |
143 | { |
144 | $listClass = new KernelClass($valueCollectionUri); |
145 | |
146 | $listItems = $listClass->getInstances(false); |
147 | |
148 | foreach ($listItems as $listItem) { |
149 | $listItem->delete(); |
150 | } |
151 | } |
152 | |
153 | /** |
154 | * @noinspection PhpDocMissingThrowsInspection |
155 | * |
156 | * @param Value $value |
157 | * |
158 | * @throws ValueConflictException |
159 | */ |
160 | protected function verifyUriUniqueness(Value $value): void |
161 | { |
162 | if (!$value->hasModifiedUri()) { |
163 | return; |
164 | } |
165 | |
166 | /** @noinspection PhpUnhandledExceptionInspection */ |
167 | if ((new KernelResource($value->getUri()))->exists() || (new KernelClass($value->getUri()))->exists()) { |
168 | throw new ValueConflictException("Value with {$value->getUri()} is already defined"); |
169 | } |
170 | } |
171 | |
172 | protected function insert(ValueCollection $valueCollection, Value $value): void |
173 | { |
174 | /** @noinspection PhpUnhandledExceptionInspection */ |
175 | $valueCollectionResource = new KernelClass($valueCollection->getUri()); |
176 | |
177 | $valueCollectionResource->createInstance($value->getLabel(), '', $value->getUri()); |
178 | } |
179 | |
180 | private function update(Value $value): void |
181 | { |
182 | if (!$value->hasChanges()) { |
183 | return; |
184 | } |
185 | |
186 | $listValue = new KernelClass($value->getOriginalUri()); |
187 | |
188 | $listValue->setLabel($value->getLabel()); |
189 | if ($value->hasModifiedUri()) { |
190 | $listValue->updateUri($value->getUri()); |
191 | } |
192 | } |
193 | |
194 | private function enrichQueryWithOrderBy(QueryBuilderInterface $query): void |
195 | { |
196 | $query->sort([OntologyRdfs::RDFS_LABEL => 'asc']); |
197 | } |
198 | |
199 | private function enrichWithLimit(ValueCollectionSearchRequest $searchRequest, QueryBuilderInterface $query): void |
200 | { |
201 | if ($searchRequest->hasOffset()) { |
202 | $query->setOffset($searchRequest->getOffset()); |
203 | } |
204 | |
205 | if ($searchRequest->hasLimit()) { |
206 | $query->setLimit($searchRequest->getLimit()); |
207 | } |
208 | } |
209 | |
210 | private function enrichQueryWithValueCollectionSearchCondition( |
211 | ValueCollectionSearchRequest $searchRequest, |
212 | QueryInterface $query |
213 | ): void { |
214 | $typeList = []; |
215 | |
216 | if ($searchRequest->hasPropertyUri()) { |
217 | $rangeProperty = new KernelProperty(OntologyRdfs::RDFS_RANGE); |
218 | $searchProperty = new KernelProperty($searchRequest->getPropertyUri()); |
219 | $typeList = $searchProperty->getPropertyValues($rangeProperty); |
220 | } |
221 | |
222 | if ($searchRequest->hasValueCollectionUri()) { |
223 | $typeList[] = $searchRequest->getValueCollectionUri(); |
224 | } |
225 | |
226 | if (!empty($typeList)) { |
227 | $query->add(OntologyRdf::RDF_TYPE)->in(array_unique($typeList)); |
228 | } |
229 | } |
230 | |
231 | private function enrichQueryWithSubject(ValueCollectionSearchRequest $searchRequest, QueryInterface $query): void |
232 | { |
233 | if (!$searchRequest->hasSubject()) { |
234 | return; |
235 | } |
236 | |
237 | $query->add(OntologyRdfs::RDFS_LABEL) |
238 | ->contains($searchRequest->getSubject()); |
239 | } |
240 | |
241 | private function enrichQueryWithExcludedValueUris( |
242 | ValueCollectionSearchRequest $searchRequest, |
243 | QueryInterface $query |
244 | ): void { |
245 | if (!$searchRequest->hasExcluded()) { |
246 | return; |
247 | } |
248 | |
249 | $query->add(QueryCriterionInterface::VIRTUAL_URI_FIELD) |
250 | ->notIn($searchRequest->getExcluded()); |
251 | } |
252 | |
253 | private function enrichQueryWithObjects( |
254 | ValueCollectionSearchRequest $searchRequest, |
255 | QueryInterface $query |
256 | ): void { |
257 | if (!$searchRequest->hasUris()) { |
258 | return; |
259 | } |
260 | |
261 | $query->add(QueryCriterionInterface::VIRTUAL_URI_FIELD) |
262 | ->in($searchRequest->getUris()); |
263 | } |
264 | |
265 | private function getPersistence(): \common_persistence_Persistence |
266 | { |
267 | $ontologyOptions = $this->getModel()->getOptions(); |
268 | return $this->persistenceManager->getPersistenceById($ontologyOptions['persistence']); |
269 | } |
270 | |
271 | public function count(ValueCollectionSearchRequest $searchRequest): int |
272 | { |
273 | /** @var ComplexSearchService $search */ |
274 | $search = $this->getModel()->getSearchInterface(); |
275 | $queryBuilder = $search->query(); |
276 | |
277 | $query = $this->getQuery($search, $queryBuilder, $searchRequest); |
278 | $this->enrichQueryWithValueCollectionSearchCondition($searchRequest, $query); |
279 | |
280 | return $search->getGateway()->count($queryBuilder); |
281 | } |
282 | |
283 | private function getQuery( |
284 | ComplexSearchService $search, |
285 | QueryBuilderInterface $queryBuilder, |
286 | ValueCollectionSearchRequest $searchRequest |
287 | ): QueryInterface { |
288 | $search->setLanguage( |
289 | $queryBuilder, |
290 | $searchRequest->getDataLanguage(), |
291 | $searchRequest->getDefaultLanguage() |
292 | ); |
293 | |
294 | $query = $queryBuilder->newQuery(); |
295 | $queryBuilder->setCriteria($query); |
296 | |
297 | return $query; |
298 | } |
299 | } |