Commit fbb600b29476abe9155ef0332fca0cc086ac05a0

Authored by Bruno Menezes
2 parents a974521e 3a24f9f7
Exists in master and in 1 other branch 3.1

Merge branch 'master' of https://github.com/lightbase/cacic

app/config/config.yml
... ... @@ -169,3 +169,7 @@ services:
169 169 class: Cacic\CommonBundle\Command\DemoCommand
170 170 tags:
171 171 - { name: console.command }
  172 + cacic_demo.command.upgrade_command:
  173 + class: Cacic\CommonBundle\Command\UpgradeCommand
  174 + tags:
  175 + - { name: console.command }
172 176 \ No newline at end of file
... ...
src/Cacic/CommonBundle/Command/UpgradeCommand.php 0 → 100644
... ... @@ -0,0 +1,150 @@
  1 +<?php
  2 +/**
  3 + * Created by PhpStorm.
  4 + * User: eduardo
  5 + * Date: 14/04/14
  6 + * Time: 12:47
  7 + */
  8 +
  9 +namespace Cacic\CommonBundle\Command;
  10 +
  11 +use Cacic\CommonBundle\Entity\Rede;
  12 +use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
  13 +use Symfony\Component\Console\Input\InputArgument;
  14 +use Symfony\Component\Console\Input\InputInterface;
  15 +use Symfony\Component\Console\Input\InputOption;
  16 +use Symfony\Component\Console\Output\OutputInterface;
  17 +
  18 +
  19 +class UpgradeCommand extends ContainerAwareCommand {
  20 + protected function configure()
  21 + {
  22 + $this
  23 + ->setName('cacic:upgrade')
  24 + ->setDescription('Atualiza Cacic')
  25 + ->addArgument('component', InputArgument::OPTIONAL, 'Nome de quem está executando o comando, só para não perder o atributo')
  26 + ->addOption('force', null, InputOption::VALUE_NONE, 'Prossegue com a carga mesmo se acontecer algum erro')
  27 + ;
  28 + }
  29 +
  30 + protected function execute(InputInterface $input, OutputInterface $output)
  31 + {
  32 + $component = $input->getArgument('component');
  33 + $text = "Executando operação de atualização para o componente $component";
  34 +
  35 + $force = ($input->getOption('force'));
  36 +
  37 + // Atualiza Software
  38 + if ($component =='software') {
  39 + $this->upgradeSoftware();
  40 + }
  41 +
  42 + $output->writeln($text);
  43 + }
  44 +
  45 + /**
  46 + * Atualiza software impedindo que haja entradas repetidas na tabela
  47 + */
  48 + private function upgradeSoftware() {
  49 + $logger = $this->getContainer()->get('logger');
  50 + $em = $this->getContainer()->get('doctrine')->getManager();
  51 +
  52 + $softwaresRepetidos = $em->getRepository('CacicCommonBundle:Software')->getNomesRepetidos();
  53 + $arrSoftware = array();
  54 + foreach ($softwaresRepetidos as $softwareElement) {
  55 + // Pega o primeiro elemento como resultado
  56 + $nmSoftware = $softwareElement['nmSoftware'];
  57 + $logger->debug("O seguinte software possui entradas repetidas: $nmSoftware");
  58 +
  59 + $this->processaRepetido($nmSoftware);
  60 +
  61 + }
  62 +
  63 + // Limpa memória
  64 + $propriedadeSoftware = null;
  65 +
  66 + // Finalmente limpa todas as entradas de software que não possuem informação de coleta
  67 + $softwareList = $em->getRepository('CacicCommonBundle:Software')->semColeta();
  68 + foreach($softwareList as $software) {
  69 + try {
  70 + $em->remove($software);
  71 + $em->flush();
  72 + } catch(\Exception $e) {
  73 + $message = $e->getMessage();
  74 + $idSoftware = $software->getIdSoftware();
  75 +
  76 + $logger->error("Falha ao remover o software $idSoftware\n$message");
  77 + }
  78 + }
  79 +
  80 + // Limpa memória
  81 + $em->clear();
  82 +
  83 + $logger->debug("Adiciona rede padrão");
  84 + $this->redePadrao();
  85 +
  86 + }
  87 +
  88 + /**
  89 + * Processa um software repetido
  90 + *
  91 + * @param $nmSoftware
  92 + */
  93 +
  94 + private function processaRepetido($nmSoftware) {
  95 + $logger = $this->getContainer()->get('logger');
  96 + $em = $this->getContainer()->get('doctrine')->getManager();
  97 + $software = $em->getRepository('CacicCommonBundle:Software')->porNome($nmSoftware);
  98 +
  99 + $propriedade = $em->getRepository('CacicCommonBundle:PropriedadeSoftware')->propPorNome( $nmSoftware);
  100 +
  101 + foreach ($propriedade as $propriedadeSoftware) {
  102 + $this->processaPropriedade($software, $propriedadeSoftware);
  103 + }
  104 +
  105 + // Limpa para economizar memória
  106 + $propriedade = null;
  107 + }
  108 +
  109 + /**
  110 + * Processa software e propriedade
  111 + *
  112 + * @param $software
  113 + * @param $propriedadeSoftware
  114 + */
  115 +
  116 + private function processaPropriedade($software, $propriedadeSoftware) {
  117 + $logger = $this->getContainer()->get('logger');
  118 + $em = $this->getContainer()->get('doctrine')->getManager();
  119 +
  120 + $propriedadeSoftware->setSoftware($software);
  121 + $em->persist($propriedadeSoftware);
  122 + $em->flush();
  123 + }
  124 +
  125 + private function redePadrao() {
  126 + $em = $this->getContainer()->get('doctrine')->getManager();
  127 + $logger = $this->getContainer()->get('logger');
  128 +
  129 + $rede = $em->getRepository('CacicCommonBundle:Rede')->findOneBy( array('teIpRede' => '0.0.0.0') );
  130 + if (empty($rede)) {
  131 + $rede = new Rede();
  132 +
  133 + $rede->setTeIpRede('0.0.0.0');
  134 + $rede->setTeMascaraRede('255.255.255.255');
  135 + $rede->setNmRede('Rede não encontrada');
  136 + $rede->setTeServCacic('http://localhost');
  137 + $rede->setTeServUpdates('http://localhost');
  138 + $rede->setNuLimiteFtp(100);
  139 + $rede->setCsPermitirDesativarSrcacic('S');
  140 +
  141 + // Armazena no primeiro local encontrado
  142 + $local = $em->getRepository('CacicCommonBundle:Local')->findAll();
  143 + $rede->setIdLocal($local[0]);
  144 +
  145 + $em->persist($rede);
  146 + $em->flush();
  147 +
  148 + }
  149 + }
  150 +}
0 151 \ No newline at end of file
... ...
src/Cacic/CommonBundle/DataFixtures/ORM/LoadRedeData.php 0 → 100644
... ... @@ -0,0 +1,49 @@
  1 +<?php
  2 +/**
  3 + * Created by PhpStorm.
  4 + * User: eduardo
  5 + * Date: 14/04/14
  6 + * Time: 19:33
  7 + */
  8 +
  9 +namespace Cacic\CommonBundle\DataFixtures\ORM;
  10 +
  11 +use Doctrine\Common\DataFixtures\AbstractFixture;
  12 +use Doctrine\Common\DataFixtures\FixtureInterface;
  13 +use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
  14 +use Doctrine\Common\Persistence\ObjectManager;
  15 +use Symfony\Component\DependencyInjection\ContainerAwareInterface;
  16 +use Symfony\Component\DependencyInjection\ContainerInterface;
  17 +
  18 +use Cacic\CommonBundle\Entity\Rede;
  19 +
  20 +
  21 +class LoadRedeData extends AbstractFixture implements FixtureInterface, ContainerAwareInterface, OrderedFixtureInterface
  22 +{
  23 + private $container;
  24 +
  25 + public function setContainer(ContainerInterface $container = null)
  26 + {
  27 + $this->container = $container;
  28 + }
  29 +
  30 + public function load(ObjectManager $manager)
  31 + {
  32 + $rede = new Rede();
  33 + $rede->setTeIpRede('0.0.0.0');
  34 + $rede->setTeMascaraRede('255.255.255.255');
  35 + $rede->setTeServCacic('http://localhost');
  36 + $rede->setTeServUpdates('http://localhost');
  37 + $rede->setNuLimiteFtp(100);
  38 + $rede->setCsPermitirDesativarSrcacic('S');
  39 + $rede->setIdLocal($this->getReference('local'));
  40 +
  41 + $manager->persist($rede);
  42 + $manager->flush();
  43 + }
  44 +
  45 + public function getOrder()
  46 + {
  47 + return 5;
  48 + }
  49 +}
0 50 \ No newline at end of file
... ...
src/Cacic/CommonBundle/Entity/PropriedadeSoftwareRepository.php
... ... @@ -12,4 +12,21 @@ use Doctrine\ORM\EntityRepository;
12 12 */
13 13 class PropriedadeSoftwareRepository extends EntityRepository
14 14 {
  15 +
  16 + /**
  17 + * Retorna lista de todas as propriedades coletadas
  18 + *
  19 + * @return mixed
  20 + */
  21 +
  22 + public function propPorNome( $nmSoftware ) {
  23 + $qb = $this->createQueryBuilder('prop')
  24 + ->select('prop')
  25 + ->innerJoin('CacicCommonBundle:Software', 'sw', 'WITH', 'sw.idSoftware = prop.software')
  26 + ->andWhere('sw.nmSoftware = :nmSoftware')
  27 + ->setParameter('nmSoftware', $nmSoftware);
  28 +
  29 + return $qb->getQuery()->execute();
  30 + }
  31 +
15 32 }
... ...
src/Cacic/CommonBundle/Entity/SoftwareRepository.php
... ... @@ -258,4 +258,48 @@ class SoftwareRepository extends EntityRepository
258 258 return $qb->getQuery()->execute();
259 259 }
260 260  
  261 + /**
  262 + * Lista softwares que possuem o nome repetido no sistema
  263 + */
  264 + public function getNomesRepetidos() {
  265 +
  266 + $qb = $this->createQueryBuilder('sw')
  267 + ->select('sw.nmSoftware, COUNT(prop) as n_repeticoes')
  268 + ->innerJoin('CacicCommonBundle:PropriedadeSoftware','prop', 'WITH', 'sw.idSoftware = prop.software')
  269 + ->having('COUNT(prop) > 1')
  270 + ->groupBy('sw.nmSoftware')
  271 + ->orderBy('sw.nmSoftware');
  272 +
  273 + return $qb->getQuery()->execute();
  274 +
  275 + }
  276 +
  277 + /**
  278 + * Pega primeiro resultado de uma consulta por nome
  279 + *
  280 + * @param $nmSoftware
  281 + */
  282 + public function porNome( $nmSoftware ) {
  283 +
  284 + $qb = $this->createQueryBuilder('sw')
  285 + ->select('sw')
  286 + ->andWhere('sw.nmSoftware = :nmSoftware')
  287 + ->setMaxResults(1)
  288 + ->setParameter('nmSoftware', $nmSoftware);
  289 +
  290 + return $qb->getQuery()->getSingleResult();
  291 +
  292 + }
  293 +
  294 + public function semColeta() {
  295 + $qb = $this->createQueryBuilder('sw')
  296 + ->select('sw')
  297 + ->leftJoin('CacicCommonBundle:PropriedadeSoftware', 'prop', 'WITH', 'sw.idSoftware = prop.software')
  298 + ->leftJoin('CacicCommonBundle:AquisicaoItem', 'aq', 'WITH', 'sw.idSoftware = aq.idSoftware')
  299 + ->andWhere('prop IS NULL')
  300 + ->andWhere('aq IS NULL');
  301 +
  302 + return $qb->getQuery()->execute();
  303 + }
  304 +
261 305 }
262 306 \ No newline at end of file
... ...
src/Cacic/WSBundle/Controller/ColetaController.php
... ... @@ -177,19 +177,22 @@ class ColetaController extends Controller
177 177 $classPropertyObject = $this->getDoctrine()->getRepository('CacicCommonBundle:ClassProperty')->findOneBy( array( 'idClassProperty'=> $idClassProperty ) );
178 178  
179 179 if (empty($propriedadeSoftware)) {
180   - // Primeiro crio o software
181   - $softwareObject = new Software();
182 180  
183 181 // Se não tiver nome coloco o ID Software no nome
184 182 if (empty($arrTagsNames['DisplayName'])) {
185   - $softwareObject->setNmSoftware($softwareName);
  183 + $nmSoftware = $softwareName;
186 184 } else {
187   - $softwareObject->setNmSoftware($arrTagsNames['DisplayName']);
  185 + $nmSoftware = $arrTagsNames['DisplayName'];
188 186 }
189 187  
190   - // Grava software recém inserido
191   - $this->getDoctrine()->getManager()->persist($softwareObject);
192   - $this->getDoctrine()->getManager()->flush();
  188 +
  189 + $softwareObject = $this->getDoctrine()->getRepository('CacicCommonBundle:Software')->findOneBy( array( 'nmSoftware' => $nmSoftware ) );
  190 + if (empty($softwareObject)) {
  191 + $softwareObject = new Software();
  192 + // Grava software recém inserido
  193 + $this->getDoctrine()->getManager()->persist($softwareObject);
  194 + $this->getDoctrine()->getManager()->flush();
  195 + }
193 196  
194 197 // Depois adiciono as propriedades
195 198 $propriedadeSoftware = new PropriedadeSoftware();
... ... @@ -219,7 +222,7 @@ class ColetaController extends Controller
219 222  
220 223 // Grava software recém inserido
221 224 $this->getDoctrine()->getManager()->persist($softwareObject);
222   - $this->getDoctrine()->getManager()->flush();
  225 + //$this->getDoctrine()->getManager()->flush();
223 226  
224 227 // Ajusta valores coletados
225 228 $propriedadeSoftware->setDisplayName($arrTagsNames['DisplayName']);
... ...