Przeglądaj źródła

FD3-780 se agrega assignedip:import:data para importar IP fijas desde FD2. minor fixes

Guillermo Espinoza 6 lat temu
rodzic
commit
83607f84fe

+ 5 - 5
app/config/import_data.yml

@@ -14,22 +14,22 @@
 Templates:
     source_table: "Template"
     destination_table: "template"
-    offset: 
+    offset:
       id: 1000
-      name: "Imported %name%"
+      name: "%name%"
     mapping:
       id: id
       name: name
       content: content
-      owner: 
+      owner:
         value: "iksop"
 
 ModelosCablemodem:
     source_table: "Model"
     destination_table: "cablemodem_model"
-    offset: 
+    offset:
       id: 1000
-      name: "Imported %name%"
+      name: "%name%"
       docsis_template_id: 1000
       mta_docsis_template_id: 1000
     mapping:

+ 120 - 0
src/CablemodemBundle/Command/AssignedIPImportDataCommand.php

@@ -0,0 +1,120 @@
+<?php
+
+namespace CablemodemBundle\Command;
+
+use mysqli;
+use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
+use Symfony\Component\Console\Input\InputInterface;
+use Symfony\Component\Console\Input\InputOption;
+use Symfony\Component\Console\Output\OutputInterface;
+use Symfony\Component\Console\Style\SymfonyStyle;
+use Symfony\Component\Console\Helper\ProgressBar;
+use Symfony\Component\Console\Question\ConfirmationQuestion;
+use Doctrine\DBAL\DriverManager;
+use Symfony\Component\Yaml\Yaml;
+
+
+class AssignedIPImportDataCommand extends ContainerAwareCommand
+{
+
+    protected $em;
+
+    protected function configure()
+    {
+        $this
+            ->setName('assignedip:import:data')
+            ->setDescription('Imports AssignedIP data from Flowdat 2')
+            ->setHelp('Imports AssignedIP data from Flowdat 2.')
+            ->addOption('dbname', null, InputOption::VALUE_OPTIONAL, 'Origin Database name. Default: flowdat2')
+            ->addOption('user', null, InputOption::VALUE_OPTIONAL, 'User name for origin database. Default: root')
+            ->addOption('password', null, InputOption::VALUE_OPTIONAL, 'Password for origin database. Default: "empty_value"')
+            ->addOption('host', null, InputOption::VALUE_OPTIONAL, 'Host of origin database. Default: localhost')
+        ;
+    }
+
+    protected function execute(InputInterface $input, OutputInterface $output)
+    {
+
+        $container = $this->getContainer();
+        $this->em = $container->get('doctrine.orm.entity_manager');
+
+        // Get data
+        $host =     $input->getOption('host');
+        $dbname =   $input->getOption('dbname');
+        $user =     $input->getOption('user');
+        $password = $input->getOption('password');
+
+        // Set defaults
+        if (!$dbname)   { $dbname = 'flowdat2'; }
+        if (!$user)     { $user = 'root'; }
+        if (!$password) { $password = ''; }
+        if (!$host)     { $host = 'localhost'; }
+
+        // Presentation
+        $io = new SymfonyStyle($input, $output);
+        $io->title('Flowdat3 AssignedIP Data Importer');
+
+        // Ask for confirmation
+        if (!$input->getOption('no-interaction')) {
+            $helper = $this->getHelper('question');
+            $question = new ConfirmationQuestion("This will IMPORT DATA FROM a Flowdat2 database. The CABLEMODEM TABLE will be ALTERED by OVERWRITING existing data, leading to potential DATA LOSS. Are you really sure you want to proceed? ", false);
+            if (!$helper->ask($input, $output, $question)) {
+                return;
+            }
+        }
+
+        $io->newLine();
+
+        $conn = new mysqli($host, $user, $password, $dbname);
+        $sql =  "SELECT * FROM AssignedIP a JOIN HostType h ON a.host_type_id = h.id;";
+        $rows = $conn->query($sql);
+
+        // Progress bar
+        $progressBar = new ProgressBar($output, $rows->num_rows);
+        $errors = [];
+
+        while ($row = $rows->fetch_assoc()) {
+
+            if ($row['cablemodem_id'] != null) {
+
+                $object = $this->em->getRepository('CablemodemBundle:Cablemodem')->findOneById($row['cablemodem_id']);
+
+                if (!is_null($object)) {
+
+                    if (($row['name'] == 'Cablemodem' || $row['shortname'] == 'cm') &&
+                        ($object->getFixedIP() == '' || is_null($object->getFixedIP()))) {
+                        $object->setFixedIP($row['ip']);
+                    }
+
+                    if (($row['name'] == 'Consumer Provided Endpoint' || $row['shortname'] == 'cpe') &&
+                        ($object->getCpeFixedIP() == '' || is_null($object->getCpeFixedIP()))) {
+                        $object->setCpeFixedIP($row['ip']);
+                    }
+
+                    if (($row['name'] == 'Embedded Multimedia Terminal Adapter' || $row['shortname'] == 'mta') &&
+                        ($object->getMtaFixedIP() == '' || is_null($object->getMtaFixedIP()))) {
+                        $object->setMtaFixedIP($row['ip']);
+                    }
+
+                    if ($container->get('validator')->validate($object)->count() == 0) {
+                        $this->em->persist($object);
+                        $this->em->flush();
+                    } else {
+                        $errors[] = "<error>[{$row['id']}] Cablemodem with ID {$row['cablemodem_id']}. Element has errors.</error>";
+                    }
+
+                    // Detach the object from the entity manager
+                    $this->em->detach($object);
+                }
+            } else {
+                $errors[] = "<error>[{$row['id']}] Unable to find a Cablemodem with ID {$row['cablemodem_id']}. Element has been skipped.</error>";
+            }
+
+            $progressBar->advance();
+        }
+
+        $output->writeln(['']);
+        $output->writeln($errors);
+        $output->writeln(['', 'Done!']);
+    }
+}

+ 17 - 13
src/CablemodemBundle/Command/VoipImportDataCommand.php

@@ -23,8 +23,8 @@ class VoipImportDataCommand extends ContainerAwareCommand
     {
         $this
             ->setName('voip:import:data')
-            ->setDescription('Imports client data from Flowdat 2')
-            ->setHelp('Imports Client data from Flowdat 2.')
+            ->setDescription('Imports voip data from Flowdat 2')
+            ->setHelp('Imports Voip data from Flowdat 2.')
             ->addOption('dbname', null, InputOption::VALUE_OPTIONAL, 'Origin Database name. Default: flowdat2')
             ->addOption('user', null, InputOption::VALUE_OPTIONAL, 'User name for origin database. Default: root')
             ->addOption('password', null, InputOption::VALUE_OPTIONAL, 'Password for origin database. Default: "empty_value"')
@@ -74,26 +74,26 @@ class VoipImportDataCommand extends ContainerAwareCommand
         $errors = [];
 
         while ($row = $rows->fetch_assoc()) {
-            
+
             if ($row['cablemodem_id'] != null) {
-                
+
                 $object = $this->em->getRepository('CablemodemBundle:Cablemodem')->findOneById($row['cablemodem_id']);
 
                 if (!is_null($object)) {
 
                     $voip = $object->getVoip();
-                    
+
                     if (is_null($voip)) { $voip = array(); }
-    
+
                     $found = null;
-    
+
                     // Look for an item containing the number
-                    foreach ($voip as $key=>$item) {
+                    foreach ($voip as $key => $item) {
                         if ($item['number'] == $row['number']) {
                             $found = $key;
                         }
                     }
-    
+
                     if (is_null($found)) {      // If number wasn't found, append.
                         $voip[] = array(
                             'number' => $row['number'],
@@ -102,19 +102,23 @@ class VoipImportDataCommand extends ContainerAwareCommand
                     } else {                    // If number was found, update password.
                         $voip[$found]['password'] = $row['password'];
                     }
-                    
+
                     // Persist
                     $object->setVoip($voip);
+
                     $this->em->persist($object);
                     $this->em->flush();
-                }    
+
+                    // Detach the object from the entity manager
+                    $this->em->detach($object);
+                }
             } else {
-                $errors[] = "<error>[{$row['id']}] Unable to find a Cablemodem witih ID {$row['cablemodem_id']}. Element has been skipped.</error>";
+                $errors[] = "<error>[{$row['id']}] Unable to find a Cablemodem with ID {$row['cablemodem_id']}. Element has been skipped.</error>";
             }
 
             $progressBar->advance();
         }
-        
+
         $output->writeln(['']);
         $output->writeln($errors);
         $output->writeln(['', 'Done!']);