diff --git a/app/config/config.yml b/app/config/config.yml index d23d4b3..46ea0f1 100644 --- a/app/config/config.yml +++ b/app/config/config.yml @@ -169,3 +169,7 @@ services: class: Cacic\CommonBundle\Command\DemoCommand tags: - { name: console.command } + cacic_demo.command.upgrade_command: + class: Cacic\CommonBundle\Command\UpgradeCommand + tags: + - { name: console.command } \ No newline at end of file diff --git a/src/Cacic/CommonBundle/Command/UpgradeCommand.php b/src/Cacic/CommonBundle/Command/UpgradeCommand.php new file mode 100644 index 0000000..e1c4ebd --- /dev/null +++ b/src/Cacic/CommonBundle/Command/UpgradeCommand.php @@ -0,0 +1,150 @@ +setName('cacic:upgrade') + ->setDescription('Atualiza Cacic') + ->addArgument('component', InputArgument::OPTIONAL, 'Nome de quem está executando o comando, só para não perder o atributo') + ->addOption('force', null, InputOption::VALUE_NONE, 'Prossegue com a carga mesmo se acontecer algum erro') + ; + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + $component = $input->getArgument('component'); + $text = "Executando operação de atualização para o componente $component"; + + $force = ($input->getOption('force')); + + // Atualiza Software + if ($component =='software') { + $this->upgradeSoftware(); + } + + $output->writeln($text); + } + + /** + * Atualiza software impedindo que haja entradas repetidas na tabela + */ + private function upgradeSoftware() { + $logger = $this->getContainer()->get('logger'); + $em = $this->getContainer()->get('doctrine')->getManager(); + + $softwaresRepetidos = $em->getRepository('CacicCommonBundle:Software')->getNomesRepetidos(); + $arrSoftware = array(); + foreach ($softwaresRepetidos as $softwareElement) { + // Pega o primeiro elemento como resultado + $nmSoftware = $softwareElement['nmSoftware']; + $logger->debug("O seguinte software possui entradas repetidas: $nmSoftware"); + + $this->processaRepetido($nmSoftware); + + } + + // Limpa memória + $propriedadeSoftware = null; + + // Finalmente limpa todas as entradas de software que não possuem informação de coleta + $softwareList = $em->getRepository('CacicCommonBundle:Software')->semColeta(); + foreach($softwareList as $software) { + try { + $em->remove($software); + $em->flush(); + } catch(\Exception $e) { + $message = $e->getMessage(); + $idSoftware = $software->getIdSoftware(); + + $logger->error("Falha ao remover o software $idSoftware\n$message"); + } + } + + // Limpa memória + $em->clear(); + + $logger->debug("Adiciona rede padrão"); + $this->redePadrao(); + + } + + /** + * Processa um software repetido + * + * @param $nmSoftware + */ + + private function processaRepetido($nmSoftware) { + $logger = $this->getContainer()->get('logger'); + $em = $this->getContainer()->get('doctrine')->getManager(); + $software = $em->getRepository('CacicCommonBundle:Software')->porNome($nmSoftware); + + $propriedade = $em->getRepository('CacicCommonBundle:PropriedadeSoftware')->propPorNome( $nmSoftware); + + foreach ($propriedade as $propriedadeSoftware) { + $this->processaPropriedade($software, $propriedadeSoftware); + } + + // Limpa para economizar memória + $propriedade = null; + } + + /** + * Processa software e propriedade + * + * @param $software + * @param $propriedadeSoftware + */ + + private function processaPropriedade($software, $propriedadeSoftware) { + $logger = $this->getContainer()->get('logger'); + $em = $this->getContainer()->get('doctrine')->getManager(); + + $propriedadeSoftware->setSoftware($software); + $em->persist($propriedadeSoftware); + $em->flush(); + } + + private function redePadrao() { + $em = $this->getContainer()->get('doctrine')->getManager(); + $logger = $this->getContainer()->get('logger'); + + $rede = $em->getRepository('CacicCommonBundle:Rede')->findOneBy( array('teIpRede' => '0.0.0.0') ); + if (empty($rede)) { + $rede = new Rede(); + + $rede->setTeIpRede('0.0.0.0'); + $rede->setTeMascaraRede('255.255.255.255'); + $rede->setNmRede('Rede não encontrada'); + $rede->setTeServCacic('http://localhost'); + $rede->setTeServUpdates('http://localhost'); + $rede->setNuLimiteFtp(100); + $rede->setCsPermitirDesativarSrcacic('S'); + + // Armazena no primeiro local encontrado + $local = $em->getRepository('CacicCommonBundle:Local')->findAll(); + $rede->setIdLocal($local[0]); + + $em->persist($rede); + $em->flush(); + + } + } +} \ No newline at end of file diff --git a/src/Cacic/CommonBundle/DataFixtures/ORM/LoadRedeData.php b/src/Cacic/CommonBundle/DataFixtures/ORM/LoadRedeData.php new file mode 100644 index 0000000..bc43ed3 --- /dev/null +++ b/src/Cacic/CommonBundle/DataFixtures/ORM/LoadRedeData.php @@ -0,0 +1,49 @@ +container = $container; + } + + public function load(ObjectManager $manager) + { + $rede = new Rede(); + $rede->setTeIpRede('0.0.0.0'); + $rede->setTeMascaraRede('255.255.255.255'); + $rede->setTeServCacic('http://localhost'); + $rede->setTeServUpdates('http://localhost'); + $rede->setNuLimiteFtp(100); + $rede->setCsPermitirDesativarSrcacic('S'); + $rede->setIdLocal($this->getReference('local')); + + $manager->persist($rede); + $manager->flush(); + } + + public function getOrder() + { + return 5; + } +} \ No newline at end of file diff --git a/src/Cacic/CommonBundle/Entity/PropriedadeSoftwareRepository.php b/src/Cacic/CommonBundle/Entity/PropriedadeSoftwareRepository.php index 02e298b..0a9fbd3 100644 --- a/src/Cacic/CommonBundle/Entity/PropriedadeSoftwareRepository.php +++ b/src/Cacic/CommonBundle/Entity/PropriedadeSoftwareRepository.php @@ -12,4 +12,21 @@ use Doctrine\ORM\EntityRepository; */ class PropriedadeSoftwareRepository extends EntityRepository { + + /** + * Retorna lista de todas as propriedades coletadas + * + * @return mixed + */ + + public function propPorNome( $nmSoftware ) { + $qb = $this->createQueryBuilder('prop') + ->select('prop') + ->innerJoin('CacicCommonBundle:Software', 'sw', 'WITH', 'sw.idSoftware = prop.software') + ->andWhere('sw.nmSoftware = :nmSoftware') + ->setParameter('nmSoftware', $nmSoftware); + + return $qb->getQuery()->execute(); + } + } diff --git a/src/Cacic/CommonBundle/Entity/SoftwareRepository.php b/src/Cacic/CommonBundle/Entity/SoftwareRepository.php index a4fc50c..c5d9c45 100644 --- a/src/Cacic/CommonBundle/Entity/SoftwareRepository.php +++ b/src/Cacic/CommonBundle/Entity/SoftwareRepository.php @@ -256,4 +256,48 @@ class SoftwareRepository extends EntityRepository return $qb->getQuery()->execute(); } + /** + * Lista softwares que possuem o nome repetido no sistema + */ + public function getNomesRepetidos() { + + $qb = $this->createQueryBuilder('sw') + ->select('sw.nmSoftware, COUNT(prop) as n_repeticoes') + ->innerJoin('CacicCommonBundle:PropriedadeSoftware','prop', 'WITH', 'sw.idSoftware = prop.software') + ->having('COUNT(prop) > 1') + ->groupBy('sw.nmSoftware') + ->orderBy('sw.nmSoftware'); + + return $qb->getQuery()->execute(); + + } + + /** + * Pega primeiro resultado de uma consulta por nome + * + * @param $nmSoftware + */ + public function porNome( $nmSoftware ) { + + $qb = $this->createQueryBuilder('sw') + ->select('sw') + ->andWhere('sw.nmSoftware = :nmSoftware') + ->setMaxResults(1) + ->setParameter('nmSoftware', $nmSoftware); + + return $qb->getQuery()->getSingleResult(); + + } + + public function semColeta() { + $qb = $this->createQueryBuilder('sw') + ->select('sw') + ->leftJoin('CacicCommonBundle:PropriedadeSoftware', 'prop', 'WITH', 'sw.idSoftware = prop.software') + ->leftJoin('CacicCommonBundle:AquisicaoItem', 'aq', 'WITH', 'sw.idSoftware = aq.idSoftware') + ->andWhere('prop IS NULL') + ->andWhere('aq IS NULL'); + + return $qb->getQuery()->execute(); + } + } \ No newline at end of file diff --git a/src/Cacic/WSBundle/Controller/ColetaController.php b/src/Cacic/WSBundle/Controller/ColetaController.php index 0a5fe3d..cb1d2fd 100644 --- a/src/Cacic/WSBundle/Controller/ColetaController.php +++ b/src/Cacic/WSBundle/Controller/ColetaController.php @@ -177,19 +177,22 @@ class ColetaController extends Controller $classPropertyObject = $this->getDoctrine()->getRepository('CacicCommonBundle:ClassProperty')->findOneBy( array( 'idClassProperty'=> $idClassProperty ) ); if (empty($propriedadeSoftware)) { - // Primeiro crio o software - $softwareObject = new Software(); // Se não tiver nome coloco o ID Software no nome if (empty($arrTagsNames['DisplayName'])) { - $softwareObject->setNmSoftware($softwareName); + $nmSoftware = $softwareName; } else { - $softwareObject->setNmSoftware($arrTagsNames['DisplayName']); + $nmSoftware = $arrTagsNames['DisplayName']; } - // Grava software recém inserido - $this->getDoctrine()->getManager()->persist($softwareObject); - $this->getDoctrine()->getManager()->flush(); + + $softwareObject = $this->getDoctrine()->getRepository('CacicCommonBundle:Software')->findOneBy( array( 'nmSoftware' => $nmSoftware ) ); + if (empty($softwareObject)) { + $softwareObject = new Software(); + // Grava software recém inserido + $this->getDoctrine()->getManager()->persist($softwareObject); + $this->getDoctrine()->getManager()->flush(); + } // Depois adiciono as propriedades $propriedadeSoftware = new PropriedadeSoftware(); @@ -219,7 +222,7 @@ class ColetaController extends Controller // Grava software recém inserido $this->getDoctrine()->getManager()->persist($softwareObject); - $this->getDoctrine()->getManager()->flush(); + //$this->getDoctrine()->getManager()->flush(); // Ajusta valores coletados $propriedadeSoftware->setDisplayName($arrTagsNames['DisplayName']); -- libgit2 0.21.2