1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Cdf\BiCoreBundle\Command; |
4
|
|
|
|
5
|
|
|
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; |
6
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
7
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
8
|
|
|
use Symfony\Component\Console\Input\InputOption; |
9
|
|
|
use Symfony\Component\Yaml\Yaml; |
10
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
11
|
|
|
use Cdf\BiCoreBundle\Utils\FieldType\FieldTypeUtils; |
12
|
|
|
|
13
|
|
|
class BiConfiguratorimportCommand extends ContainerAwareCommand |
14
|
|
|
{ |
15
|
|
|
|
16
|
|
|
private $forceupdate = false; |
17
|
|
|
private $verboso = false; |
|
|
|
|
18
|
|
|
private $dbutility; |
19
|
|
|
private $systementity; |
20
|
|
|
private $entityutility; |
21
|
|
|
private $em; |
22
|
|
|
private $truncatetables; |
23
|
|
|
private $output; |
24
|
|
|
|
25
|
3 |
|
protected function configure() |
26
|
|
|
{ |
27
|
|
|
$this |
28
|
3 |
|
->setName('bi:configuratorimport') |
29
|
3 |
|
->setDescription('Configuratore per Fifree') |
30
|
3 |
|
->setHelp('Importa la configurazione di bi da file fixtures.yml') |
31
|
3 |
|
->addOption('forceupdate', null, InputOption::VALUE_NONE, 'Forza update di record con id già presente') |
32
|
3 |
|
->addOption('truncatetables', null, InputOption::VALUE_NONE, 'Esegue una truncate della tabelle') |
33
|
3 |
|
->addOption('verboso', null, InputOption::VALUE_NONE, 'Visualizza tutti i messaggi di importazione'); |
34
|
3 |
|
} |
35
|
|
|
|
36
|
2 |
|
protected function execute(InputInterface $input, OutputInterface $output) |
37
|
|
|
{ |
38
|
2 |
|
$this->output = $output; |
|
|
|
|
39
|
2 |
|
$this->forceupdate = $input->getOption('forceupdate'); |
|
|
|
|
40
|
2 |
|
$this->verboso = $input->getOption('verboso'); |
|
|
|
|
41
|
2 |
|
$this->truncatetables = $input->getOption('truncatetables'); |
42
|
2 |
|
$this->dbutility = $this->getContainer()->get("fi.bicorebundle.utility.database"); |
|
|
|
|
43
|
2 |
|
$this->entityutility = $this->getContainer()->get("fi.bicorebundle.utility.entity"); |
|
|
|
|
44
|
2 |
|
$this->systementity = $this->getContainer()->get("fi.bicorebundle.utility.entity.system"); |
|
|
|
|
45
|
2 |
|
$this->em = $this->getContainer()->get("doctrine")->getManager(); |
|
|
|
|
46
|
|
|
|
47
|
2 |
|
$this->checkSchemaStatus(); |
48
|
|
|
|
49
|
2 |
|
$fixturefile = sys_get_temp_dir() . DIRECTORY_SEPARATOR . "fixtures.yml"; |
|
|
|
|
50
|
2 |
|
$ret = $this->import($fixturefile); |
|
|
|
|
51
|
2 |
|
return $ret; |
52
|
|
|
} |
53
|
|
|
|
54
|
2 |
|
protected function import($fixturefile) |
55
|
|
|
{ |
56
|
2 |
|
$fs = new Filesystem; |
57
|
2 |
|
if ($fs->exists($fixturefile)) { |
58
|
2 |
|
$fixtures = Yaml::parse(file_get_contents($fixturefile)); |
59
|
2 |
|
$msg = "<info>Trovate " . count($fixtures) . " entities nel file " . $fixturefile . "</info>"; |
|
|
|
|
60
|
2 |
|
$this->output->writeln($msg); |
61
|
|
|
|
62
|
2 |
|
if ($this->truncatetables) { |
63
|
1 |
|
foreach ($fixtures as $entityclass => $fixture) { |
64
|
1 |
|
$this->truncateTable($entityclass); |
65
|
|
|
} |
66
|
|
|
} |
67
|
2 |
|
$sortedEntities = $this->getSortedEntities($fixtures); |
68
|
2 |
|
foreach ($sortedEntities as $entityclass => $fixture) { |
69
|
2 |
|
$ret = $this->executeImport($entityclass, $fixture); |
70
|
2 |
|
if ($ret == 1) { |
71
|
2 |
|
return 1; |
72
|
|
|
} |
73
|
|
|
} |
74
|
2 |
|
return 0; |
75
|
|
|
} else { |
76
|
1 |
|
$msgerr = "<error>Non trovato file " . $fixturefile . "</error>"; |
|
|
|
|
77
|
1 |
|
$this->output->writeln($msgerr); |
78
|
1 |
|
return 1; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
2 |
|
private function checkSchemaStatus() |
83
|
|
|
{ |
84
|
2 |
|
$schemachanged = $this->dbutility->isSchemaChanged(); |
85
|
|
|
|
86
|
2 |
|
if ($schemachanged) { |
87
|
2 |
|
$msgerr = "<error>Attenzione, lo schema database non è aggiornato, verrà comunque tentata l'importazione</error>"; |
88
|
2 |
|
$this->output->writeln($msgerr); |
89
|
2 |
|
sleep(3); |
90
|
|
|
} |
91
|
2 |
|
} |
92
|
|
|
|
93
|
1 |
|
private function truncateTable($entityclass) |
94
|
|
|
{ |
95
|
1 |
|
$tablename = $this->entityutility->getTableFromEntity($entityclass); |
96
|
1 |
|
$msg = "<info>TRUNCATE della tabella " . $tablename . " (" . $entityclass . ")</info>"; |
|
|
|
|
97
|
1 |
|
$this->output->writeln($msg); |
98
|
1 |
|
$this->dbutility->truncatetable($entityclass, true); |
99
|
1 |
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @SuppressWarnings(PHPMD.UnusedLocalVariable) |
103
|
|
|
*/ |
104
|
2 |
|
private function getSortedEntities($fixtures) |
105
|
|
|
{ |
106
|
2 |
|
$entities = array(); |
|
|
|
|
107
|
2 |
|
$sortedEntities = $this->systementity->getSystemEntities(); |
108
|
2 |
|
foreach ($sortedEntities as $fixture => $details) { |
109
|
2 |
|
if (isset($fixtures[$fixture])) { |
110
|
2 |
|
$entities[$fixture] = $fixtures[$fixture]; |
111
|
|
|
} |
112
|
|
|
} |
113
|
2 |
|
return $entities; |
114
|
|
|
} |
115
|
|
|
|
116
|
2 |
|
private function executeImport($entityclass, $fixture) |
117
|
|
|
{ |
118
|
2 |
|
$msg = "<info>Trovati " . count($fixture) . " record per l'entity " . $entityclass . "</info>"; |
|
|
|
|
119
|
2 |
|
$this->output->writeln($msg); |
120
|
2 |
|
foreach ($fixture as $record) { |
121
|
2 |
|
$objrecord = $this->em->getRepository($entityclass)->find($record["id"]); |
|
|
|
|
122
|
2 |
|
$ret = $this->switchInsertUpdate($entityclass, $record, $objrecord); |
|
|
|
|
123
|
2 |
|
if ($ret !== 0) { |
124
|
2 |
|
return 1; |
125
|
|
|
} |
126
|
|
|
} |
127
|
2 |
|
return 0; |
128
|
|
|
} |
129
|
|
|
|
130
|
2 |
|
private function switchInsertUpdate($entityclass, $record, $objrecord) |
131
|
|
|
{ |
132
|
2 |
|
if (!$objrecord) { |
133
|
2 |
|
return $this->executeInsert($entityclass, $record); |
134
|
|
|
} |
135
|
|
|
|
136
|
1 |
|
if ($this->forceupdate) { |
137
|
1 |
|
return $this->executeUpdate($entityclass, $record, $objrecord); |
138
|
|
|
} else { |
139
|
|
|
$msgerr = "<error>" . $entityclass . " con id " . $record["id"] |
|
|
|
|
140
|
|
|
. " non modificata, specificare l'opzione --forceupdate " |
141
|
|
|
. "per sovrascrivere record presenti</error>"; |
|
|
|
|
142
|
|
|
$this->output->writeln($msgerr); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
146
|
2 |
|
private function executeInsert($entityclass, $record) |
147
|
|
|
{ |
148
|
2 |
|
$objrecord = new $entityclass(); |
149
|
2 |
|
foreach ($record as $key => $value) { |
150
|
2 |
|
if ($key !== 'id') { |
151
|
2 |
|
$propertyEntity = $this->entityutility->getEntityProperties($key, $objrecord); |
152
|
2 |
|
$getfieldname = $propertyEntity["get"]; |
|
|
|
|
153
|
2 |
|
$setfieldname = $propertyEntity["set"]; |
|
|
|
|
154
|
2 |
|
if ($key=="discr") { |
|
|
|
|
155
|
1 |
|
continue; |
156
|
|
|
} |
157
|
2 |
|
$fieldtype = $this->dbutility->getFieldType($objrecord, $key); |
158
|
2 |
|
if ($fieldtype === 'boolean') { |
159
|
2 |
|
$newval = FieldTypeUtils::getBooleanValue($value); |
160
|
2 |
|
$msgok = "<info>Inserimento " . $entityclass . " con id " . $record["id"] |
|
|
|
|
161
|
2 |
|
. " per campo " . $key . " con valore " |
|
|
|
|
162
|
2 |
|
. var_export($newval, true) . " in formato Boolean</info>"; |
|
|
|
|
163
|
2 |
|
$this->output->writeln($msgok); |
164
|
2 |
|
$objrecord->$setfieldname($newval); |
165
|
2 |
|
continue; |
166
|
|
|
} |
167
|
|
|
//Si prende in considerazione solo il null del boolean, gli altri non si toccano |
168
|
2 |
|
if (!$value) { |
169
|
2 |
|
continue; |
170
|
|
|
} |
171
|
2 |
|
if ($fieldtype === 'datetime' || $fieldtype === 'date') { |
172
|
1 |
|
$date = FieldTypeUtils::getDateTimeValueFromTimestamp($value); |
|
|
|
|
173
|
1 |
|
$msgok = "<info>Inserimento " . $entityclass . " con id " . $record["id"] |
|
|
|
|
174
|
1 |
|
. " per campo " . $key . " cambio valore da " |
|
|
|
|
175
|
1 |
|
. ($objrecord->$getfieldname() ? $objrecord->$getfieldname()->format("Y-m-d H:i:s") : "NULL") |
|
|
|
|
176
|
1 |
|
. " a " . $date->format("Y-m-d H:i:s") . " in formato DateTime</info>"; |
|
|
|
|
177
|
1 |
|
$this->output->writeln($msgok); |
178
|
1 |
|
$objrecord->$setfieldname($date); |
179
|
1 |
|
continue; |
180
|
|
|
} |
181
|
2 |
|
if (is_array($value)) { |
182
|
2 |
|
$msgarray = "<info>Inserimento " . $entityclass . " con id " . $record["id"] |
|
|
|
|
183
|
2 |
|
. " per campo " . $key . " cambio valore da " |
|
|
|
|
184
|
2 |
|
. json_encode($objrecord->$getfieldname()) . " a " |
|
|
|
|
185
|
2 |
|
. json_encode($value) . " in formato array" . "</info>"; |
|
|
|
|
186
|
2 |
|
$this->output->writeln($msgarray); |
187
|
2 |
|
$objrecord->$setfieldname($value); |
188
|
2 |
|
continue; |
189
|
|
|
} |
190
|
|
|
|
191
|
2 |
|
$joincolumn = $this->entityutility->getJoinTableField($entityclass, $key); |
|
|
|
|
192
|
2 |
|
$joincolumnproperty = $this->entityutility->getJoinTableFieldProperty($entityclass, $key); |
193
|
2 |
|
if ($joincolumn && $joincolumnproperty) { |
194
|
2 |
|
$joincolumnobj = $this->em->getRepository($joincolumn)->find($value); |
195
|
2 |
|
$msgok = "<info>Inserimento " . $entityclass . " con id " . $record["id"] |
|
|
|
|
196
|
2 |
|
. " per campo " . $key |
|
|
|
|
197
|
2 |
|
. " con valore " . print_r($value, true) . " tramite entity find</info>"; |
|
|
|
|
198
|
2 |
|
$this->output->writeln($msgok); |
199
|
2 |
|
$joinobj = $this->entityutility->getEntityProperties($joincolumnproperty, new $entityclass()); |
|
|
|
|
200
|
2 |
|
$setfieldname = $joinobj["set"]; |
|
|
|
|
201
|
2 |
|
$objrecord->$setfieldname($joincolumnobj); |
202
|
2 |
|
continue; |
203
|
|
|
} |
204
|
2 |
|
$objrecord->$setfieldname($value); |
205
|
|
|
} |
206
|
|
|
} |
207
|
2 |
|
$this->em->persist($objrecord); |
208
|
2 |
|
$this->em->flush(); |
209
|
|
|
|
210
|
2 |
|
$infomsg = "<info>" . $entityclass . " con id " . $objrecord->getId() . " aggiunta</info>"; |
|
|
|
|
211
|
2 |
|
$this->output->writeln($infomsg); |
212
|
2 |
|
$checkid = $this->changeRecordId($entityclass, $record, $objrecord); |
213
|
2 |
|
return $checkid; |
214
|
|
|
} |
215
|
|
|
|
216
|
2 |
|
private function changeRecordId($entityclass, $record, $objrecord) |
217
|
|
|
{ |
218
|
2 |
|
if ($record["id"] !== $objrecord->getId()) { |
|
|
|
|
219
|
|
|
try { |
220
|
1 |
|
$qb = $this->em->createQueryBuilder(); |
221
|
1 |
|
$q = $qb->update($entityclass, 'u') |
|
|
|
|
222
|
1 |
|
->set('u.id', ":newid") |
|
|
|
|
223
|
1 |
|
->where('u.id = :oldid') |
224
|
1 |
|
->setParameter("newid", $record["id"]) |
|
|
|
|
225
|
1 |
|
->setParameter("oldid", $objrecord->getId()) |
|
|
|
|
226
|
1 |
|
->getQuery(); |
227
|
1 |
|
$q->execute(); |
228
|
1 |
|
$msgok = "<info>" . $entityclass . " con id " . $objrecord->getId() . " sistemata</info>"; |
|
|
|
|
229
|
1 |
|
$this->output->writeln($msgok); |
230
|
|
|
} catch (\Exception $exc) { |
231
|
|
|
echo $exc->getMessage(); |
232
|
|
|
return 1; |
233
|
|
|
} |
234
|
1 |
|
$this->em->flush(); |
235
|
|
|
} |
236
|
2 |
|
return 0; |
237
|
|
|
} |
238
|
|
|
|
239
|
1 |
|
private function executeUpdate($entityclass, $record, $objrecord) |
240
|
|
|
{ |
241
|
1 |
|
unset($record["id"]); |
|
|
|
|
242
|
1 |
|
foreach ($record as $key => $value) { |
243
|
1 |
|
if ($key=="discr") { |
|
|
|
|
244
|
1 |
|
continue; |
245
|
|
|
} |
246
|
1 |
|
$propertyEntity = $this->entityutility->getEntityProperties($key, $objrecord); |
247
|
1 |
|
$getfieldname = $propertyEntity["get"]; |
|
|
|
|
248
|
1 |
|
$setfieldname = $propertyEntity["set"]; |
|
|
|
|
249
|
1 |
|
$cambiato = $this->dbutility->isRecordChanged($entityclass, $key, $objrecord->$getfieldname(), $value); |
|
|
|
|
250
|
1 |
|
if (!$cambiato) { |
251
|
1 |
|
if ($this->verboso) { |
252
|
1 |
|
$msginfo = "<info>" . $entityclass . " con id " . $objrecord->getId() |
|
|
|
|
253
|
1 |
|
. " per campo " . $key . " non modificato perchè già " |
|
|
|
|
254
|
1 |
|
. $value . "</info>"; |
|
|
|
|
255
|
1 |
|
$this->output->writeln($msginfo); |
256
|
|
|
} |
257
|
|
|
} else { |
258
|
|
|
try { |
259
|
1 |
|
$fieldtype = $this->dbutility->getFieldType($objrecord, $key); |
260
|
1 |
|
if ($fieldtype === 'boolean') { |
261
|
|
|
$newval = FieldTypeUtils::getBooleanValue($value); |
262
|
|
|
|
263
|
|
|
$msgok = "<info>Modifica " . $entityclass . " con id " . $objrecord->getId() |
|
|
|
|
264
|
|
|
. " per campo " . $key . " cambio valore da " |
|
|
|
|
265
|
|
|
. var_export($objrecord->$getfieldname(), true) |
266
|
|
|
. " a " . var_export($newval, true) . " in formato Boolean</info>"; |
|
|
|
|
267
|
|
|
$this->output->writeln($msgok); |
268
|
|
|
$objrecord->$setfieldname($newval); |
269
|
|
|
continue; |
270
|
|
|
} |
271
|
|
|
//Si prende in considerazione solo il null del boolean, gli altri non si toccano |
272
|
1 |
|
if (!$value) { |
273
|
1 |
|
continue; |
274
|
|
|
} |
275
|
|
|
|
276
|
1 |
|
if ($fieldtype === 'datetime' || $fieldtype === 'date') { |
277
|
1 |
|
$date = FieldTypeUtils::getDateTimeValueFromTimestamp($value); |
|
|
|
|
278
|
1 |
|
$msgok = "<info>Modifica " . $entityclass . " con id " . $objrecord->getId() |
|
|
|
|
279
|
1 |
|
. " per campo " . $key . " cambio valore da " |
|
|
|
|
280
|
|
|
//. (!is_null($objrecord->$getfieldname())) ? $objrecord->$getfieldname()->format("Y-m-d H:i:s") : "(null)" |
|
|
|
|
281
|
1 |
|
. ($objrecord->$getfieldname() ? $objrecord->$getfieldname()->format("Y-m-d H:i:s") : "NULL") |
|
|
|
|
282
|
1 |
|
. " a " . $date->format("Y-m-d H:i:s") . " in formato DateTime</info>"; |
|
|
|
|
283
|
1 |
|
$this->output->writeln($msgok); |
284
|
1 |
|
$objrecord->$setfieldname($date); |
285
|
1 |
|
continue; |
286
|
|
|
} |
287
|
1 |
|
if (is_array($value)) { |
288
|
1 |
|
$msgarray = "<info>Modifica " . $entityclass . " con id " . $objrecord->getId() |
|
|
|
|
289
|
1 |
|
. " per campo " . $key . " cambio valore da " |
|
|
|
|
290
|
1 |
|
. json_encode($objrecord->$getfieldname()) . " a " |
|
|
|
|
291
|
1 |
|
. json_encode($value) . " in formato array" . "</info>"; |
|
|
|
|
292
|
1 |
|
$this->output->writeln($msgarray); |
293
|
1 |
|
$objrecord->$setfieldname($value); |
294
|
1 |
|
continue; |
295
|
|
|
} |
296
|
|
|
|
297
|
1 |
|
$joincolumn = $this->entityutility->getJoinTableField($entityclass, $key); |
|
|
|
|
298
|
1 |
|
$joincolumnproperty = $this->entityutility->getJoinTableFieldProperty($entityclass, $key); |
299
|
1 |
|
if ($joincolumn && $joincolumnproperty) { |
300
|
1 |
|
$joincolumnobj = $this->em->getRepository($joincolumn)->find($value); |
301
|
1 |
|
$msgok = "<info>Modifica " . $entityclass . " con id " . $objrecord->getId() |
|
|
|
|
302
|
1 |
|
. " per campo " . $key . " cambio valore da " . print_r($objrecord->$getfieldname(), true) |
|
|
|
|
303
|
1 |
|
. " a " . print_r($value, true) . " tramite entity find</info>"; |
|
|
|
|
304
|
1 |
|
$this->output->writeln($msgok); |
305
|
1 |
|
$joinobj = $this->entityutility->getEntityProperties($joincolumnproperty, new $entityclass()); |
|
|
|
|
306
|
1 |
|
$setfieldname = $joinobj["set"]; |
|
|
|
|
307
|
1 |
|
$objrecord->$setfieldname($joincolumnobj); |
308
|
1 |
|
continue; |
309
|
|
|
} |
310
|
|
|
|
311
|
1 |
|
$msgok = "<info>Modifica " . $entityclass . " con id " . $objrecord->getId() |
|
|
|
|
312
|
1 |
|
. " per campo " . $key . " cambio valore da " . print_r($objrecord->$getfieldname(), true) |
|
|
|
|
313
|
1 |
|
. " a " . print_r($value, true) . "</info>"; |
|
|
|
|
314
|
1 |
|
$this->output->writeln($msgok); |
315
|
1 |
|
$objrecord->$setfieldname($value); |
316
|
|
|
} catch (\Exception $exc) { |
317
|
|
|
$msgerr = "<error>Modifica " . $entityclass . " con id " . $objrecord->getId() |
|
|
|
|
318
|
|
|
. " per campo " . $key . ", ERRORE: " . $exc->getMessage() |
|
|
|
|
319
|
|
|
. " alla riga " . $exc->getLine() . "</error>"; |
|
|
|
|
320
|
|
|
$this->output->writeln($msgerr); |
321
|
|
|
//dump($exc); |
322
|
1 |
|
return 1; |
323
|
|
|
} |
324
|
|
|
} |
325
|
|
|
} |
326
|
1 |
|
$this->em->persist($objrecord); |
327
|
1 |
|
$this->em->flush(); |
328
|
|
|
|
329
|
1 |
|
return 0; |
330
|
|
|
} |
331
|
|
|
} |
332
|
|
|
|
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.