|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
/** |
|
3
|
|
|
* ownCloud |
|
4
|
|
|
* |
|
5
|
|
|
* This file is licensed under the Affero General Public License version 3 or |
|
6
|
|
|
* later. See the COPYING file. |
|
7
|
|
|
* |
|
8
|
|
|
* @author Alessandro Cosentino <[email protected]> |
|
9
|
|
|
* @author Bernhard Posselt <[email protected]> |
|
10
|
|
|
* @author Pauli Järvinen <[email protected]> |
|
11
|
|
|
* @copyright Alessandro Cosentino 2012 |
|
12
|
|
|
* @copyright Bernhard Posselt 2012, 2014 |
|
13
|
|
|
* @copyright Pauli Järvinen 2017 - 2021 |
|
14
|
|
|
*/ |
|
15
|
|
|
|
|
16
|
|
|
namespace OCA\Music\AppFramework\BusinessLayer; |
|
17
|
|
|
|
|
18
|
|
|
use \OCA\Music\Db\BaseMapper; |
|
19
|
|
|
use \OCA\Music\Db\SortBy; |
|
20
|
|
|
|
|
21
|
|
|
use \OCP\AppFramework\Db\DoesNotExistException; |
|
22
|
|
|
use \OCP\AppFramework\Db\Entity; |
|
23
|
|
|
use \OCP\AppFramework\Db\MultipleObjectsReturnedException; |
|
24
|
|
|
|
|
25
|
|
|
abstract class BusinessLayer { |
|
26
|
|
|
protected $mapper; |
|
27
|
|
|
|
|
28
|
|
|
public function __construct(BaseMapper $mapper) { |
|
29
|
|
|
$this->mapper = $mapper; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Update an entity in the database |
|
34
|
|
|
* @param Entity $entity |
|
35
|
|
|
*/ |
|
36
|
|
|
public function update(Entity $entity) : void { |
|
37
|
|
|
$this->mapper->update($entity); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Delete an entity |
|
42
|
|
|
* @param int $id the id of the entity |
|
43
|
|
|
* @param string $userId the name of the user for security reasons |
|
44
|
|
|
* @throws BusinessLayerException if the entity does not exist or more than one entity exists |
|
45
|
|
|
*/ |
|
46
|
|
|
public function delete(int $id, string $userId) : void { |
|
47
|
|
|
$entity = $this->find($id, $userId); |
|
48
|
|
|
$this->mapper->delete($entity); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Deletes entities without specifying the owning user. |
|
53
|
|
|
* This should never be called directly from the HTML API, but only in case |
|
54
|
|
|
* we can actually trust the passed IDs (e.g. file deleted hook). |
|
55
|
|
|
* @param array $ids the ids of the entities which should be deleted |
|
56
|
|
|
*/ |
|
57
|
|
|
public function deleteById(array $ids) : void { |
|
58
|
|
|
if (\count($ids) > 0) { |
|
59
|
|
|
$this->mapper->deleteById($ids); |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Delete all entities of the given user |
|
65
|
|
|
*/ |
|
66
|
|
|
public function deleteAll(string $userId) : void { |
|
67
|
|
|
$this->mapper->deleteAll($userId); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Finds an entity by id |
|
72
|
|
|
* @param int $id the id of the entity |
|
73
|
|
|
* @param string $userId the name of the user for security reasons |
|
74
|
|
|
* @throws BusinessLayerException if the entity does not exist or more than one entity exists |
|
75
|
|
|
* @return Entity the entity |
|
76
|
|
|
*/ |
|
77
|
|
|
public function find(int $id, string $userId) : Entity { |
|
78
|
|
|
try { |
|
79
|
|
|
return $this->mapper->find($id, $userId); |
|
80
|
|
|
} catch (DoesNotExistException $ex) { |
|
81
|
|
|
throw new BusinessLayerException($ex->getMessage()); |
|
82
|
|
|
} catch (MultipleObjectsReturnedException $ex) { |
|
83
|
|
|
throw new BusinessLayerException($ex->getMessage()); |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Finds an entity by id, or returns an empty entity instance if the requested one is not found |
|
89
|
|
|
* @param int $id the id of the entity |
|
90
|
|
|
* @param string $userId the name of the user for security reasons |
|
91
|
|
|
* @return Entity the entity |
|
92
|
|
|
*/ |
|
93
|
|
|
public function findOrDefault(int $id, string $userId) : Entity { |
|
94
|
|
|
try { |
|
95
|
|
|
return $this->find($id, $userId); |
|
96
|
|
|
} catch (BusinessLayerException $ex) { |
|
97
|
|
|
return $this->mapper->createEntity(); |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Find all entities matching the given IDs. |
|
103
|
|
|
* Specifying the user is optional; if omitted, the caller should make sure that |
|
104
|
|
|
* user's data is not leaked to unauthorized users. |
|
105
|
|
|
* @param integer[] $ids IDs of the entities to be found |
|
106
|
|
|
* @param string|null $userId |
|
107
|
|
|
* @return Entity[] |
|
108
|
|
|
*/ |
|
109
|
|
|
public function findById(array $ids, string $userId=null) : array { |
|
110
|
|
|
if (\count($ids) > 0) { |
|
111
|
|
|
return $this->mapper->findById($ids, $userId); |
|
112
|
|
|
} else { |
|
113
|
|
|
return []; |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Finds all entities |
|
119
|
|
|
* @param string $userId the name of the user |
|
120
|
|
|
* @param integer $sortBy sort order of the result set |
|
121
|
|
|
* @param integer|null $limit |
|
122
|
|
|
* @param integer|null $offset |
|
123
|
|
|
* @param string|null $createdMin Optional minimum `created` timestamp. |
|
124
|
|
|
* @param string|null $createdMax Optional maximum `created` timestamp. |
|
125
|
|
|
* @param string|null $updatedMin Optional minimum `updated` timestamp. |
|
126
|
|
|
* @param string|null $updatedMax Optional maximum `updated` timestamp. |
|
127
|
|
|
* @return Entity[] |
|
128
|
|
|
*/ |
|
129
|
|
|
public function findAll( |
|
130
|
|
|
string $userId, int $sortBy=SortBy::None, int $limit=null, int $offset=null, |
|
131
|
|
|
?string $createdMin=null, ?string $createdMax=null, ?string $updatedMin=null, ?string $updatedMax=null) : array { |
|
132
|
|
|
return $this->mapper->findAll($userId, $sortBy, $limit, $offset, $createdMin, $createdMax, $updatedMin, $updatedMax); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Return all entities with name matching the search criteria |
|
137
|
|
|
* @param string $name |
|
138
|
|
|
* @param string $userId |
|
139
|
|
|
* @param bool $fuzzy |
|
140
|
|
|
* @param integer|null $limit |
|
141
|
|
|
* @param integer|null $offset |
|
142
|
|
|
* @param string|null $createdMin Optional minimum `created` timestamp. |
|
143
|
|
|
* @param string|null $createdMax Optional maximum `created` timestamp. |
|
144
|
|
|
* @param string|null $updatedMin Optional minimum `updated` timestamp. |
|
145
|
|
|
* @param string|null $updatedMax Optional maximum `updated` timestamp. |
|
146
|
|
|
* @return Entity[] |
|
147
|
|
|
*/ |
|
148
|
|
|
public function findAllByName( |
|
149
|
|
|
string $name, string $userId, bool $fuzzy=false, int $limit=null, int $offset=null, |
|
150
|
|
|
?string $createdMin=null, ?string $createdMax=null, ?string $updatedMin=null, ?string $updatedMax=null) : array { |
|
151
|
|
|
$name = \trim($name); |
|
152
|
|
|
return $this->mapper->findAllByName($name, $userId, $fuzzy, $limit, $offset, $createdMin, $createdMax, $updatedMin, $updatedMax); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* Find all starred entities |
|
157
|
|
|
* @param string $userId |
|
158
|
|
|
* @param integer|null $limit |
|
159
|
|
|
* @param integer|null $offset |
|
160
|
|
|
* @return Entity[] |
|
161
|
|
|
*/ |
|
162
|
|
|
public function findAllStarred(string $userId, int $limit=null, int $offset=null) : array { |
|
163
|
|
|
return $this->mapper->findAllStarred($userId, $limit, $offset); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* Find IDs of all user's entities of this kind |
|
168
|
|
|
* @return int[] |
|
169
|
|
|
*/ |
|
170
|
|
|
public function findAllIds(string $userId) : array { |
|
171
|
|
|
return $this->mapper->findAllIds($userId); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* Find IDs of all users owning any entities of this business layer |
|
176
|
|
|
* @return string[] |
|
177
|
|
|
*/ |
|
178
|
|
|
public function findAllUsers() : array { |
|
179
|
|
|
return $this->mapper->findAllUsers(); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* Set the given entities as "starred" on this date |
|
184
|
|
|
* @param int[] $ids |
|
185
|
|
|
* @param string $userId |
|
186
|
|
|
* @return int number of modified entities |
|
187
|
|
|
*/ |
|
188
|
|
|
public function setStarred(array $ids, string $userId) : int { |
|
189
|
|
|
if (\count($ids) > 0) { |
|
190
|
|
|
return $this->mapper->setStarredDate(new \DateTime(), $ids, $userId); |
|
191
|
|
|
} else { |
|
192
|
|
|
return 0; |
|
193
|
|
|
} |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
/** |
|
197
|
|
|
* Remove the "starred" status of the given entities |
|
198
|
|
|
* @param integer[] $ids |
|
199
|
|
|
* @param string $userId |
|
200
|
|
|
* @return int number of modified entities |
|
201
|
|
|
*/ |
|
202
|
|
|
public function unsetStarred(array $ids, string $userId) : int { |
|
203
|
|
|
if (\count($ids) > 0) { |
|
204
|
|
|
return $this->mapper->setStarredDate(null, $ids, $userId); |
|
205
|
|
|
} else { |
|
206
|
|
|
return 0; |
|
207
|
|
|
} |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
/** |
|
211
|
|
|
* Tests if entity with given ID and user ID exists in the database |
|
212
|
|
|
* @param int $id |
|
213
|
|
|
* @param string $userId |
|
214
|
|
|
* @return bool |
|
215
|
|
|
*/ |
|
216
|
|
|
public function exists(int $id, string $userId) : bool { |
|
217
|
|
|
return $this->mapper->exists($id, $userId); |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
/** |
|
221
|
|
|
* Get the number of entities |
|
222
|
|
|
* @param string $userId |
|
223
|
|
|
*/ |
|
224
|
|
|
public function count(string $userId) : int { |
|
225
|
|
|
return $this->mapper->count($userId); |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
/** |
|
229
|
|
|
* Get the timestamp of the latest insert operation on the entity type in question |
|
230
|
|
|
*/ |
|
231
|
|
|
public function latestInsertTime(string $userId) : \DateTime { |
|
232
|
|
|
return $this->mapper->latestInsertTime($userId) ?? new \DateTime('1970-01-01'); |
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
|
|
/** |
|
236
|
|
|
* Get the timestamp of the latest update operation on the entity type in question |
|
237
|
|
|
*/ |
|
238
|
|
|
public function latestUpdateTime(string $userId) : \DateTime { |
|
239
|
|
|
return $this->mapper->latestUpdateTime($userId) ?? new \DateTime('1970-01-01'); |
|
240
|
|
|
} |
|
241
|
|
|
} |
|
242
|
|
|
|