DatasetListAction.php 8.17 KB
Newer Older
1
<?php declare(strict_types=1);
François Agneray's avatar
François Agneray committed
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/*
 * This file is part of ANIS SERVER API.
 *
 * (c) François Agneray <francois.agneray@lam.fr>
 * (c) Chrystel Moreau <chrystel.moreau@lam.fr>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
namespace App\Action\Meta;

use Psr\Log\LoggerInterface;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;

17
use App\Utils\ActionTrait;
François Agneray's avatar
François Agneray committed
18
use App\Utils\DBALConnectionFactory;
19
20
21
22
23
use App\Utils\MetaEntityManagerFactory;
use App\Entity\Metamodel\Dataset;
use App\Entity\Metamodel\Attribute;
use App\Entity\Metamodel\Project;
use App\Entity\Metamodel\DatasetFamily;
François Agneray's avatar
François Agneray committed
24

François Agneray's avatar
François Agneray committed
25
26
27
28
29
30
31
32
33
34
/**
 * Route: /metadata/dataset
 *
 * This action is used to:
 *  - Get all datasets
 *  - Add a new dataset
 *
 * @author François Agneray <francois.agneray@lam.fr>
 * @package App\Action\Meta
 */
35
final class DatasetListAction
François Agneray's avatar
François Agneray committed
36
{
37
38
    use ActionTrait;

François Agneray's avatar
François Agneray committed
39
40
41
42
43
    /**
     * The logger interface is the central access point to log anis information
     *
     * @var LoggerInterface
     */
44
    private $logger;
François Agneray's avatar
François Agneray committed
45
46

    /**
47
48
     * The MetaEntityManagerFactory create the doctrine entity manager for the metamodel database.
     * The entity manager is the central access point to Doctrine ORM functionality (metadata database)
François Agneray's avatar
François Agneray committed
49
     *
50
     * @var MetaEntityManagerFactory
François Agneray's avatar
François Agneray committed
51
     */
52
    private $memf;
François Agneray's avatar
François Agneray committed
53
54
55

    /**
     * Factory that provides a connection to the business database
56
     *
François Agneray's avatar
François Agneray committed
57
58
     * @var DBALConnectionFactory
     */
François Agneray's avatar
François Agneray committed
59
    private $dcf;
François Agneray's avatar
François Agneray committed
60
61

    /**
François Agneray's avatar
François Agneray committed
62
     * The key needed to decrypt the password of the database
François Agneray's avatar
François Agneray committed
63
64
65
66
     *
     * @var string
     */
    private $encryptionKey;
François Agneray's avatar
François Agneray committed
67
    
François Agneray's avatar
François Agneray committed
68
69
70
    /**
     * Create the classe before call __invoke to execute the action
     *
71
72
73
74
     * @param LoggerInterface          $logger
     * @param MetaEntityManagerFactory $memf
     * @param DBALConnectionFactory    $dcf
     * @param string                   $encryptionKey
François Agneray's avatar
François Agneray committed
75
     */
François Agneray's avatar
François Agneray committed
76
77
78
79
80
81
    public function __construct(
        LoggerInterface $logger,
        MetaEntityManagerFactory $memf,
        DBALConnectionFactory $dcf,
        string $encryptionKey
    ) {
82
        $this->logger = $logger;
83
        $this->memf = $memf;
François Agneray's avatar
François Agneray committed
84
        $this->dcf = $dcf;
François Agneray's avatar
François Agneray committed
85
        $this->encryptionKey = $encryptionKey;
François Agneray's avatar
François Agneray committed
86
87
    }
    
François Agneray's avatar
François Agneray committed
88
89
90
91
92
93
94
95
96
97
98
    /**
     * `GET` Returns the list of datasets
     * `POST` Add a new dataset. The program will search the business database for
     *        the columns of the table (table_ref) to transform them into attributes
     *
     * @param ServerRequestInterface $request   This object contains the HTTP request
     * @param ResponseInterface      $response  This object represents the HTTP response
     * @param string[]               $args      This table contains information transmitted in the URL (see routes.php)
     *
     * @return ResponseInterface
     */
François Agneray's avatar
François Agneray committed
99
100
101
102
103
104
105
106
    public function __invoke(Request $request, Response $response, array $args): Response
    {
        $this->logger->info('Dataset list action dispatched');

        if ($request->isOptions()) {
            return $response->withHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
        }

107
108
109
        // Create the doctrine Entity Manager object for the instance metamodel database
        $this->memf->createMetaEntityManager($args['instance']);

François Agneray's avatar
François Agneray committed
110
        if ($request->isGet()) {
111
112
113
114
            $datasets = $this->memf->getMetaEntityManager()->getRepository('App\Entity\Metamodel\Dataset')->findBy(
                array(),
                array('display' => 'ASC')
            );
François Agneray's avatar
François Agneray committed
115
            $newResponse = $response->withJson($datasets);
François Agneray's avatar
François Agneray committed
116
        }
François Agneray's avatar
François Agneray committed
117
118
119
120
        
        if ($request->isPost()) {
            $parsedBody = $request->getParsedBody();

François Agneray's avatar
François Agneray committed
121
            // If mandatories empty fields 400
122
123
124
125
126
127
128
129
130
            $fields = array(
                'name',
                'table_ref',
                'label',
                'description',
                'display',
                'count',
                'vo',
                'data_path',
François Agneray's avatar
François Agneray committed
131
                'selectable_raw',
132
133
134
135
                'project_name',
                'id_dataset_family'
            );
            foreach ($fields as $a) {
François Agneray's avatar
François Agneray committed
136
137
138
139
                if ($this->isEmptyField($a, $parsedBody)) {
                    return $this->dispatchHttpError(
                        $response,
                        'Invalid request',
140
                        'Param ' . $a . ' needed to add a new dataset'
François Agneray's avatar
François Agneray committed
141
142
143
144
                    );
                }
            }

145
146
147
148
            $project = $this->memf->getMetaEntityManager()->find(
                'App\Entity\Metamodel\Project',
                $parsedBody['project_name']
            );
François Agneray's avatar
François Agneray committed
149
150
151
152
            if (is_null($project)) {
                return $this->dispatchHttpError(
                    $response,
                    'Invalid request',
153
                    'Project with name ' . $parsedBody['project_name'] . ' is not found'
François Agneray's avatar
François Agneray committed
154
155
156
                )->withStatus(404);
            }

157
158
159
160
            $family = $this->memf->getMetaEntityManager()->find(
                'App\Entity\Metamodel\DatasetFamily',
                $parsedBody['id_dataset_family']
            );
François Agneray's avatar
François Agneray committed
161
162
163
164
            if (is_null($family)) {
                return $this->dispatchHttpError(
                    $response,
                    'Invalid request',
165
                    'Dataset family with id ' . $parsedBody['id_dataset_family'] . ' is not found'
François Agneray's avatar
François Agneray committed
166
167
168
169
                )->withStatus(404);
            }

            $dataset = $this->postDataset($parsedBody, $project, $family);
François Agneray's avatar
François Agneray committed
170
            $newResponse = $response->withJson($dataset);
François Agneray's avatar
François Agneray committed
171
172
173
174
175
        }

        return $newResponse;
    }

François Agneray's avatar
François Agneray committed
176
177
178
179
180
181
182
183
184
    /**
     * Create a new dataset doctrine object and save it
     *
     * @param string[]      $parsedBody    Contains the values ​​of the new dataset sent by the user
     * @param Project       $project       Contains the project doctrine object
     * @param DatasetFamily $datasetFamily Contains the dataset family doctrine object
     *
     * @return Dataset
     */
François Agneray's avatar
François Agneray committed
185
186
187
188
189
190
191
192
193
194
    private function postDataset(array $parsedBody, Project $project, DatasetFamily $family): Dataset
    {
        $dataset = new Dataset($parsedBody['name']);
        $dataset->setTableRef($parsedBody['table_ref']);
        $dataset->setLabel($parsedBody['label']);
        $dataset->setDescription($parsedBody['description']);
        $dataset->setDisplay($parsedBody['display']);
        $dataset->setCount($parsedBody['count']);
        $dataset->setVo($parsedBody['vo']);
        $dataset->setDataPath($parsedBody['data_path']);
François Agneray's avatar
François Agneray committed
195
        $dataset->setSelectableRaw($parsedBody['selectable_raw']);
François Agneray's avatar
François Agneray committed
196
197
198
        $dataset->setProject($project);
        $dataset->setDatasetFamily($family);

199
        $this->memf->getMetaEntityManager()->persist($dataset);
François Agneray's avatar
François Agneray committed
200
        $this->postAttributes($dataset);
201
        $this->memf->getMetaEntityManager()->flush();
François Agneray's avatar
François Agneray committed
202
203
204
205

        return $dataset;
    }

François Agneray's avatar
François Agneray committed
206
207
208
    /**
     * Access to the business database dans get all the columns of the table (table_ref)
     * and makes the corresponding doctrine attribute objects
François Agneray's avatar
François Agneray committed
209
     *
François Agneray's avatar
François Agneray committed
210
211
     * @param Dataset $dataset Contains the new dataset doctrine object
     */
François Agneray's avatar
François Agneray committed
212
213
214
    private function postAttributes(Dataset $dataset): void
    {
        $database = $dataset->getProject()->getDatabase();
François Agneray's avatar
François Agneray committed
215
216
        $decryptedPassword = $this->decryptData($database->getPassword());
        $connection = $this->dcf->create($database, $decryptedPassword);
François Agneray's avatar
François Agneray committed
217
218
219
        $sm = $connection->getSchemaManager();
        $columns = $sm->listTableColumns($dataset->getTableRef());
        $i = 10;
François Agneray's avatar
#9 done    
François Agneray committed
220
        $id = 1;
François Agneray's avatar
François Agneray committed
221
222
223
        foreach ($columns as $column) {
            $columnName = $column->getName();
            $columnType = $column->getType()->getName();
224
            $attribute = new Attribute($id, $dataset);
François Agneray's avatar
#9 done    
François Agneray committed
225
            $attribute->setName($columnName);
François Agneray's avatar
François Agneray committed
226
227
228
229
230
231
232
233
234
            $attribute->setTableName($dataset->getTableRef());
            $attribute->setLabel($columnName);
            $attribute->setFormLabel($columnName);
            $attribute->setType($columnType);
            $attribute->setCriteriaDisplay($i);
            $attribute->setOutputDisplay($i);
            $attribute->setDisplayDetail($i);
            $attribute->setOrderDisplay($i);
            $attribute->setSelected(true);
235
            $this->memf->getMetaEntityManager()->persist($attribute);
François Agneray's avatar
François Agneray committed
236
            $i = $i + 10;
François Agneray's avatar
#9 done    
François Agneray committed
237
            $id++;
François Agneray's avatar
François Agneray committed
238
239
240
        }
    }
}