Passed
Push — master ( fc42a6...b0746a )
by Marcel
02:44 queued 11s
created

DatasetService::deleteByUser()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 1
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Analytics
4
 *
5
 * This file is licensed under the Affero General Public License version 3 or
6
 * later. See the LICENSE.md file.
7
 *
8
 * @author Marcel Scherello <[email protected]>
9
 * @copyright 2021 Marcel Scherello
10
 */
11
12
namespace OCA\Analytics\Service;
13
14
use OCA\Analytics\Activity\ActivityManager;
15
use OCA\Analytics\Controller\DatasourceController;
16
use OCA\Analytics\Db\DataloadMapper;
17
use OCA\Analytics\Db\DatasetMapper;
18
use OCA\Analytics\Db\ReportMapper;
19
use OCA\Analytics\Db\StorageMapper;
20
use OCP\AppFramework\Http\DataDownloadResponse;
21
use OCP\AppFramework\Http\DataResponse;
22
use OCP\DB\Exception;
23
use OCP\Files\IRootFolder;
24
use OCP\Files\NotFoundException;
25
use OCP\ITagManager;
26
use Psr\Log\LoggerInterface;
27
28
class DatasetService
29
{
30
    private $userId;
31
    private $logger;
32
    private $tagManager;
33
    private $ShareService;
34
    private $StorageMapper;
35
    private $DatasetMapper;
36
    private $ThresholdService;
37
    private $DataloadMapper;
38
    private $ActivityManager;
39
    private $rootFolder;
40
    private $VariableService;
41
    private $ReportMapper;
42
43
    public function __construct(
44
        $userId,
45
        LoggerInterface $logger,
46
        ITagManager $tagManager,
47
        ShareService $ShareService,
48
        StorageMapper $StorageMapper,
49
        DatasetMapper $DatasetMapper,
50
        ThresholdService $ThresholdService,
51
        DataloadMapper $DataloadMapper,
52
        ActivityManager $ActivityManager,
53
        IRootFolder $rootFolder,
54
        VariableService $VariableService,
55
        ReportMapper $ReportMapper
56
    )
57
    {
58
        $this->userId = $userId;
59
        $this->logger = $logger;
60
        $this->tagManager = $tagManager;
61
        $this->ShareService = $ShareService;
62
        $this->ThresholdService = $ThresholdService;
63
        $this->StorageMapper = $StorageMapper;
64
        $this->DatasetMapper = $DatasetMapper;
65
        $this->DataloadMapper = $DataloadMapper;
66
        $this->ActivityManager = $ActivityManager;
67
        $this->rootFolder = $rootFolder;
68
        $this->VariableService = $VariableService;
69
        $this->ReportMapper = $ReportMapper;
70
    }
71
72
    /**
73
     * get all datasets
74
     *
75
     * @return array
76
     */
77
    public function index()
78
    {
79
        $ownDatasets = $this->DatasetMapper->index();
80
81
        // get data load indicators for icons shown in the advanced screen
82
        $dataloads = $this->DataloadMapper->getAllDataloadMetadata();
83
        foreach ($dataloads as $dataload) {
84
            $key = array_search($dataload['dataset'], array_column($ownDatasets, 'id'));
85
            if ($key !== '') {
86
                if ($dataload['schedules'] !== '' and $dataload['schedules'] !== null) {
87
                    $dataload['schedules'] = 1;
88
                } else {
89
                    $dataload['schedules'] = 0;
90
                }
91
                $ownDatasets[$key]['dataloads'] = $dataload['dataloads'];
92
                $ownDatasets[$key]['schedules'] = $dataload['schedules'];
93
            }
94
        }
95
96
        foreach ($ownDatasets as &$ownDataset) {
97
            $ownDataset['type'] = DatasourceController::DATASET_TYPE_INTERNAL_DB;
98
            $ownDataset = $this->VariableService->replaceTextVariables($ownDataset);
99
        }
100
101
        return $ownDatasets;
102
    }
103
104
    /**
105
     * get own dataset details
106
     *
107
     * @param int $datasetId
108
     * @return array|bool
109
     * @throws Exception
110
     */
111
    public function readOwn(int $datasetId)
112
    {
113
        $ownDataset = $this->DatasetMapper->readOwn($datasetId);
114
        if (! empty($ownDataset)) {
115
            $ownDataset['permissions'] = \OCP\Constants::PERMISSION_UPDATE;
116
        }
117
        return $ownDataset;
118
    }
119
120
    /**
121
     * get  dataset details
122
     *
123
     * @param int $datasetId
124
     * @return array|bool
125
     * @throws Exception
126
     */
127
    public function read(int $datasetId)
128
    {
129
        $ownDataset = $this->DatasetMapper->read($datasetId);
130
        return $ownDataset;
131
    }
132
133
    /**
134
     * check if own report
135
     *
136
     * @param int $reportId
137
     * @return bool
138
     */
139
    public function isOwn(int $datasetId)
140
    {
141
        $ownDataset = $this->DatasetMapper->readOwn($datasetId);
142
        if (!empty($ownDataset)) {
143
            return true;
144
        } else {
145
            return false;
146
        }
147
    }
148
149
    /**
150
     * get dataset status
151
     *
152
     * @param int $datasetId
153
     * @return array|bool
154
     * @throws Exception
155
     */
156
    public function status(int $datasetId): array
157
    {
158
        $status = array();
159
        $status['reports'] = $this->ReportMapper->reportsForDataset($datasetId);
160
        $status['data'] = $this->StorageMapper->getRecordCount($datasetId);
161
        return $status;
162
    }
163
164
    /**
165
     * create new dataset
166
     *
167
     * @param $name
168
     * @param $dimension1
169
     * @param $dimension2
170
     * @param $value
171
     * @return int
172
     * @throws Exception
173
     */
174
    public function create($name, $dimension1, $dimension2, $value)
175
    {
176
        //$this->ActivityManager->triggerEvent(0, ActivityManager::OBJECT_DATASET, ActivityManager::SUBJECT_DATASET_ADD);
177
        return $this->DatasetMapper->create($name, $dimension1, $dimension2, $value);
178
    }
179
180
    /**
181
     * get dataset details
182
     *
183
     * @param int $datasetId
184
     * @param $name
185
     * @param $dimension1
186
     * @param $dimension2
187
     * @param $value
188
     * @return bool
189
     * @throws Exception
190
     */
191
    public function update(int $datasetId, $name,$dimension1 = null, $dimension2 = null, $value = null)
192
    {
193
        return $this->DatasetMapper->update($datasetId, $name, $dimension1, $dimension2, $value);
194
    }
195
196
    /**
197
     * Export Dataset
198
     *
199
     * @param int $datasetId
200
     * @return DataDownloadResponse
201
     * @throws Exception
202
     */
203
    public function export(int $datasetId)
204
    {
205
        $result = array();
206
        $result['dataset'] = $this->DatasetMapper->read($datasetId);
207
        $result['dataload'] = $this->DataloadMapper->read($datasetId);
208
        $result['threshold'] = $this->ThresholdService->read($datasetId);
209
        $result['favorite'] = '';
210
211
        if ($result['dataset']['type'] === DatasourceController::DATASET_TYPE_INTERNAL_DB) {
212
            $result['data'] = $this->StorageMapper->read($datasetId);
213
        }
214
215
        unset($result['dataset']['id'], $result['dataset']['user_id'], $result['dataset']['user_id'], $result['dataset']['parent']);
216
        $data = json_encode($result);
217
        return new DataDownloadResponse($data, $result['dataset']['name'] . '.export.txt', 'text/plain; charset=utf-8');
218
    }
219
220
    /**
221
     * Delete Dataset and all depending objects
222
     *
223
     * @param int $datasetId
224
     * @return bool
225
     * @throws Exception
226
     */
227
    public function delete(int $datasetId)
228
    {
229
        $this->DatasetMapper->delete($datasetId);
230
        $this->DataloadMapper->deleteByDataset($datasetId);
231
        $this->StorageMapper->deleteByDataset($datasetId);
232
        return true;
233
    }
234
235
    /**
236
     * get dataset by user
237
     *
238
     * @param string $userId
239
     * @return bool
240
     * @throws Exception
241
     */
242
    public function deleteByUser(string $userId)
243
    {
244
        $datasets = $this->DatasetMapper->indexByUser($userId);
245
        foreach ($datasets as $dataset) {
246
            $this->DatasetMapper->delete($dataset['id']);
247
            $this->DataloadMapper->deleteByDataset($dataset['id']);
248
            $this->StorageMapper->deleteByDataset($dataset['id']);
249
        }
250
        return true;
251
    }
252
253
}