Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 36 |
|
0.00% |
0 / 9 |
CRAP | |
0.00% |
0 / 1 |
GateWay | |
0.00% |
0 / 36 |
|
0.00% |
0 / 9 |
182 | |
0.00% |
0 / 1 |
init | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
connect | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
search | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
searchTriples | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
count | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
fetchTripleList | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
6 | |||
fetchObjectList | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
12 | |||
fetchOne | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
getQuery | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 |
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) 2023 (original work) Open Assessment Technologies SA; |
19 | * |
20 | */ |
21 | |
22 | namespace oat\generis\model\kernel\persistence\starsql\search; |
23 | |
24 | use common_persistence_Manager; |
25 | use Laudis\Neo4j\Databags\Statement; |
26 | use oat\oatbox\service\ServiceManager; |
27 | use oat\search\base\exception\SearchGateWayExeption; |
28 | use oat\search\base\QueryBuilderInterface; |
29 | use oat\search\base\ResultSetInterface; |
30 | use oat\search\ResultSet; |
31 | use oat\search\TaoSearchGateWay; |
32 | |
33 | class GateWay extends TaoSearchGateWay |
34 | { |
35 | /** |
36 | * |
37 | * @var \common_persistence_GraphPersistence |
38 | */ |
39 | protected $connector; |
40 | |
41 | protected $serialyserList = [ |
42 | 'taoRdf' => 'search.neo4j.serialyser' |
43 | ]; |
44 | |
45 | protected $driverList = [ |
46 | 'taoRdf' => 'search.driver.neo4j' |
47 | ]; |
48 | |
49 | /** |
50 | * resultSet service or className |
51 | * @var string |
52 | */ |
53 | protected $resultSetClassName = ResultSet::class; |
54 | |
55 | public function init() |
56 | { |
57 | parent::init(); |
58 | |
59 | $this->connector = ServiceManager::getServiceManager() |
60 | ->get(common_persistence_Manager::SERVICE_ID) |
61 | ->getPersistenceById($this->options['persistence'] ?? 'neo4j'); |
62 | |
63 | return $this; |
64 | } |
65 | |
66 | /** |
67 | * try to connect to database. throw an exception |
68 | * if connection failed. |
69 | * |
70 | * @throws SearchGateWayExeption |
71 | * @return $this |
72 | */ |
73 | public function connect() |
74 | { |
75 | return !is_null($this->connector); |
76 | } |
77 | |
78 | public function search(QueryBuilderInterface $Builder) |
79 | { |
80 | $result = $this->fetchObjectList(parent::search($Builder)); |
81 | $totalCount = $this->count($Builder); |
82 | |
83 | return new $this->resultSetClassName($result, $totalCount); |
84 | } |
85 | |
86 | /** |
87 | * @param QueryBuilderInterface $Builder |
88 | * @param string $propertyUri |
89 | * @param bool $isDistinct |
90 | * |
91 | * @return ResultSetInterface |
92 | */ |
93 | public function searchTriples(QueryBuilderInterface $Builder, string $propertyUri, bool $isDistinct = false) |
94 | { |
95 | $result = $this->fetchTripleList( |
96 | parent::searchTriples($Builder, $propertyUri, $isDistinct) |
97 | ); |
98 | |
99 | return new $this->resultSetClassName($result, count($result)); |
100 | } |
101 | |
102 | /** |
103 | * return total count result |
104 | * |
105 | * @param QueryBuilderInterface $builder |
106 | * |
107 | * @return int |
108 | */ |
109 | public function count(QueryBuilderInterface $builder) |
110 | { |
111 | return (int)($this->fetchOne(parent::count($builder))); |
112 | } |
113 | |
114 | private function fetchTripleList(Statement $query): array |
115 | { |
116 | $returnValue = []; |
117 | $statement = $this->connector->runStatement($query); |
118 | foreach ($statement as $row) { |
119 | $triple = new \core_kernel_classes_Triple(); |
120 | |
121 | $triple->id = $row->get('id', 0); |
122 | $triple->subject = $row->get('uri', ''); |
123 | $triple->object = $row->get('object') ?? ''; |
124 | |
125 | $returnValue[] = $triple; |
126 | } |
127 | return $returnValue; |
128 | } |
129 | |
130 | |
131 | private function fetchObjectList(Statement $query): array |
132 | { |
133 | $returnValue = []; |
134 | $statement = $this->connector->runStatement($query); |
135 | foreach ($statement as $result) { |
136 | $object = $result->current(); |
137 | if (!$object) { |
138 | continue; |
139 | } |
140 | $returnValue[] = \common_Utils::toResource($object->getProperty('uri')); |
141 | } |
142 | return $returnValue; |
143 | } |
144 | |
145 | private function fetchOne(Statement $query) |
146 | { |
147 | $results = $this->connector->runStatement($query); |
148 | return $results->first()->current(); |
149 | } |
150 | |
151 | public function getQuery() |
152 | { |
153 | if ($this->parsedQuery instanceof Statement) { |
154 | return $this->parsedQuery->getText(); |
155 | } else { |
156 | return ''; |
157 | } |
158 | } |
159 | } |