浏览代码

Refactory Action

Guillermo Espinoza 8 年之前
父节点
当前提交
03fdfe1a60

+ 55 - 27
Admin/ActionAdmin.php

@@ -7,7 +7,11 @@ use Sonata\AdminBundle\Datagrid\DatagridMapper;
 use Sonata\AdminBundle\Datagrid\ListMapper;
 use Sonata\AdminBundle\Form\FormMapper;
 use Sonata\AdminBundle\Show\ShowMapper;
-use Symfony\Component\Yaml\Yaml;
+use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
+use Symfony\Component\Form\Extension\Core\Type\TextType;
+use WorkflowBundle\Utils\WorkFlowEntityClasses;
+use WorkflowBundle\Utils\WorkFlowEvents;
+use WorkflowBundle\Utils\DoctrineEvents;
 
 class ActionAdmin extends BaseAdmin
 {
@@ -32,11 +36,9 @@ class ActionAdmin extends BaseAdmin
     {
         $datagridMapper
             ->add('name')
-            ->add('workflowType')
             ->add('workflowName')
             ->add('objectClass')
             ->add('event')
-            ->add('template')
         ;
     }
 
@@ -47,16 +49,17 @@ class ActionAdmin extends BaseAdmin
     {
         $listMapper
             ->add('name')
-            ->add('workflowType')
             ->add('workflowName')
             ->add('objectClass')
-            ->add('event')
+            ->add('event', 'html', array(
+                'template' => 'BaseAdminBundle:Utils:badge_type.html.twig',
+            ))
             ->add('eventReference')
             ->add('_action', null, array(
                 'actions' => array(
                     'show' => array(),
                     'edit' => array(),
-                    'delete' => array()
+                    'delete' => array(),
                 )
             ))
         ;
@@ -69,24 +72,43 @@ class ActionAdmin extends BaseAdmin
     {
 
         $formMapper
-            ->add('name')
-            ->add('workflowType', 'choice', array('choices' => array('state_machine' => 'state_machine', 'workflow' => 'workflow')))
-            ->add('workflowName')
-            ->add('objectClass', 'choice', array('choices' => array('FTTHBundle\Entity\ONU' => 'FTTHBundle\Entity\ONU', 'FTTHBundle\Entity\OLT' => 'FTTHBundle\Entity\OLT')))
-            ->add('event', 'choice', array('choices' => array('transition' => 'transition', 'leave' => 'leave', 'enter' => 'enter', 'guard' => 'guard')))
-            ->add('eventReference')
-            ->add('template', null, array('attr' => array('style' => 'height:500px;')))
-
-            ->setHelps(array(
-               'workflowType' => $this->trans("helps.action_label_workflow_type"),
-               'workflowName' => $this->trans("helps.action_label_workflow_name"),
-               'name' => $this->trans("helps.action_label_name"),
-               'objectClass' => $this->trans("helps.action_label_object_class"),
-               'event' => $this->trans("helps.action_label_event"),
-               'eventReference' => $this->trans("helps.action_label_event_reference"),
-               'template' => $this->trans("helps.action_label_template")
-            ))
-            ;
+                ->add('name')
+                ->add('event', ChoiceType::class, array(
+                    'choices' => array(
+                        'WorkFlow' => WorkFlowEvents::getChoices(), 
+                        'Doctrine' => DoctrineEvents::getDoctrineEventChoices(),
+                    ),
+                    'required' => false,
+                    'multiple' => true,
+                    )
+                )
+                ->add('workflowName', TextType::class, array(
+                    'required' => false,
+                ))
+                ->add('eventReference', TextType::class, array(
+                    'required' => false,
+                ))
+                ->add('objectClass', ChoiceType::class, array(
+                    'choices' => array_filter(WorkFlowEntityClasses::getChoices(), 'class_exists'),
+                    'required' => false,
+                    )
+                )
+                ->add('template', null, array(
+                    'attr' => array(
+                        'style' => 'height:500px;',
+                        ),
+                    )
+                )
+                ->setHelps(array(
+                    'workflowName' => $this->trans("helps.action_label_workflow_name"),
+                    'name' => $this->trans("helps.action_label_name"),
+                    'objectClass' => $this->trans("helps.action_label_object_class"),
+                    'event' => $this->trans("helps.action_label_event"),
+                    'eventReference' => $this->trans("helps.action_label_event_reference"),
+                    'template' => $this->trans("helps.action_label_template"),
+                    )
+                )
+        ;
     }
 
     /**
@@ -97,13 +119,19 @@ class ActionAdmin extends BaseAdmin
         $showMapper
             ->add('id')
             ->add('name')
-            ->add('workflowType')
             ->add('workflowName')
             ->add('objectClass')
-            ->add('event')
+            ->add('event', 'string', array(
+                'template' => 'BaseAdminBundle:Utils:badge_field.html.twig',
+            ))
             ->add('eventReference')
-            ->add('template','string', array('template' => 'WorkflowBundle:Action:show_template.html.twig'))
+            ->add('template','string', array(
+                'template' => 'WorkflowBundle:Action:show_template.html.twig',
+            ))
         ;
+        $this->parameters = array(
+            'template_type' => 'show',
+        );
     }
 
 }

+ 23 - 78
Entity/Action.php

@@ -3,6 +3,7 @@
 namespace WorkflowBundle\Entity;
 
 use Doctrine\ORM\Mapping as ORM;
+use Symfony\Component\Validator\Constraints as Assert;
 
 /**
  * @ORM\Table
@@ -24,58 +25,58 @@ class Action
      * @var string
      *
      * @ORM\Column(type="string", length=255, nullable=false)
+     * 
+     * @Assert\NotNull
+     * @Assert\NotBlank
      */
     protected $name;
-
-    /**
-     * @var string
-     *
-     * @ORM\Column(type="string", length=255, nullable=false)
-     */
-    protected $workflowType;
     
     /**
      * @var string
      *
-     * @ORM\Column(type="string", length=255, nullable=false)
+     * @ORM\Column(type="string", length=255, nullable=true)
+     *
+     * @Assert\Callback({"WorkflowBundle\Validator\EventFieldsValidator", "validate"})
      */
     protected $workflowName;
 
     /**
      * @var string
      *
-     * @ORM\Column(type="string", length=255, nullable=false)
+     * @ORM\Column(type="string", length=255, nullable=true)
+     * 
+     * @Assert\Callback(
+     *  callback={"WorkflowBundle\Validator\EventFieldsValidator", "validate"},
+     *  payload={"doctrine"="true"}
+     * )
      */
     protected $objectClass;
 
     /**
-     * @var string
+     * @var array
      *
-     * @ORM\Column(type="string", length=255, nullable=false)
+     * @ORM\Column(type="array", length=255, nullable=true)
      */
     protected $event;
 
     /**
      * @var string
      *
-     * @ORM\Column(type="string", length=255, nullable=false)
+     * @ORM\Column(type="string", length=255, nullable=true)
+     * @Assert\Callback({"WorkflowBundle\Validator\EventFieldsValidator", "validate"})
      */
     protected $eventReference;
 
     /**
-     * @var text
+     * @var string
      *
      * @ORM\Column(type="text", nullable=false)
+     *
+     * @Assert\NotNull
+     * @Assert\NotBlank
      */
     protected $template;
 
-    /**
-     * @var Doctrine2WorkFlowAction
-     * 
-     * @ORM\ManyToMany(targetEntity="Doctrine2WorkFlowAction", inversedBy="actions", fetch="EXTRA_LAZY")
-     */
-    private $doctrine2WorkFlowActions;
-
     /**
      * @var int
      *
@@ -137,7 +138,7 @@ class Action
      *
      * @return Action
      */
-    public function setEvent($event)
+    public function setEvent($event = null)
     {
         $this->event = $event;
 
@@ -172,30 +173,6 @@ class Action
         return $this->template;
     }
 
-    /**
-     * Set workflowType
-     *
-     * @param string $workflowType
-     *
-     * @return Action
-     */
-    public function setWorkflowType($workflowType)
-    {
-        $this->workflowType = $workflowType;
-
-        return $this;
-    }
-
-    /**
-     * Get workflowType
-     *
-     * @return string
-     */
-    public function getWorkflowType()
-    {
-        return $this->workflowType;
-    }
-
     /**
      * Set workflowName
      *
@@ -203,7 +180,7 @@ class Action
      *
      * @return Action
      */
-    public function setWorkflowName($workflowName)
+    public function setWorkflowName($workflowName = null)
     {
         $this->workflowName = $workflowName;
 
@@ -268,38 +245,6 @@ class Action
         return $this->eventReference;
     }
 
-    /**
-     * @return Doctrine2WorkFlowAction
-     */
-    public function getDoctrine2WorkFlowActions()
-    {
-        return $this->doctrine2WorkFlowActions;
-    }
-
-    /**
-     * @param Doctrine2WorkFlowAction $doctrine2WorkFlowAction
-     * 
-     * @return Workflow
-     */
-    public function addDoctrine2WorkFlowAction($doctrine2WorkFlowAction)
-    {
-        $this->doctrine2WorkFlowActions[] = $doctrine2WorkFlowAction;
-
-        return $this;
-    }
-
-    /**
-     * @param Doctrine2WorkFlowAction $doctrine2WorkFlowAction
-     * 
-     * @return Workflow
-     */
-    public function removeDoctrine2WorkFlowAction($doctrine2WorkFlowAction)
-    {
-        $this->doctrine2WorkFlowActions->removeElement($doctrine2WorkFlowAction);
-
-        return $this;
-    }
-
     /**
      * Set tenancyId
      *

+ 0 - 170
Entity/Doctrine2WorkFlowAction.php

@@ -1,170 +0,0 @@
-<?php
-
-namespace WorkflowBundle\Entity;
-
-use Doctrine\Common\Collections\ArrayCollection;
-use Doctrine\ORM\Mapping as ORM;
-
-/**
- * @ORM\Table
- * @ORM\Entity(repositoryClass="WorkflowBundle\Repository\Doctrine2WorkFlowActionRepository")
- */
-class Doctrine2WorkFlowAction
-{
-   
-    /**
-     * @var int
-     *
-     * @ORM\Column(name="id", type="integer")
-     * @ORM\Id
-     * @ORM\GeneratedValue(strategy="AUTO")
-     */
-    private $id;
-
-    /**
-     * @var string
-     * 
-     * @ORM\Column(type="string", length=255) 
-     */
-    private $name;
-    
-    /**
-     * @var string
-     * 
-     * @ORM\Column(type="string", length=255) 
-     */
-    protected $eventName;
-
-    /**
-     * @var string
-     * 
-     * @ORM\Column(type="string", length=255) 
-     */
-    protected $entityClass;
-    
-    /**
-     * @var ArrayCollection
-     * 
-     * @ORM\ManyToMany(targetEntity="Action", mappedBy="doctrine2WorkFlowActions", cascade={"persist"})
-     */
-    protected $actions;
-
-    
-    /**
-     * @return string
-     */
-    public function __toString()
-    {
-        return (string) $this->name;
-    }
-
-    public function __construct()
-    {
-        $this->actions = new ArrayCollection();
-    }
-
-    /**
-     * Get id
-     *
-     * @return int
-     */
-    public function getId()
-    {
-        return $this->id;
-    }
-    
-    /**
-     * @return string
-     */
-    public function getName()
-    {
-        return $this->name;
-    }
-
-    /**
-     * @param string $name
-     * 
-     * @return $this
-     */
-    public function setName($name)
-    {
-        $this->name = $name;
-        
-        return $this;
-    }
-    
-    /**
-     * @return string
-     */
-    public function getEventName()
-    {
-        return $this->eventName;
-    }
-
-    /**
-     * @param string $eventName
-     * 
-     * @return $this
-     */
-    public function setEventName($eventName)
-    {
-        $this->eventName = $eventName;
-
-        return $this;
-    }
-    
-    /**
-     * @return string
-     */
-    public function getEntityClass()
-    {
-        return $this->entityClass;
-    }
-
-    /**
-     * @param string $entityClass
-     * 
-     * @return $this
-     */
-    public function setEntityClass($entityClass)
-    {
-        $this->entityClass = $entityClass;
-        
-        return $this;
-    }
-    
-    /**
-     * @param Action $action
-     * 
-     * @return Workflow
-     */
-    public function addAction($action)
-    {
-        $action->addDoctrine2WorkFlowAction($this);
-        $this->actions[] = $action;
-
-        return $this;
-    }
-
-    /**
-     * @param Action $action
-     * 
-     * @return Workflow
-     */
-    public function removeAction($action)
-    {
-        $action->removeDoctrine2WorkFlowAction($this);
-        $this->actions->removeElement($action);
-
-        return $this;
-    }
-
-    /**
-     * @return ArrayCollection 
-     */
-    public function getActions()
-    {
-        return $this->actions;
-    }
-
-}

+ 0 - 37
Repository/Doctrine2WorkFlowActionRepository.php

@@ -1,37 +0,0 @@
-<?php
-
-namespace WorkflowBundle\Repository;
-
-use \Doctrine\ORM\EntityRepository;
-
-class Doctrine2WorkFlowActionRepository extends EntityRepository
-{
-
-    /**
-     * @param array $criteria
-     * 
-     * @return array
-     */
-    public function findAllBy($criteria = array())
-    {
-        $qb = $this->createQueryBuilder('Doctrine2WorkFlowAction');
-
-        foreach ($criteria as $field => $value) {
-            $qb->andWhere("Doctrine2WorkFlowAction.{$field} = :{$field}")->setParameter($field, $value);
-        }
-
-        return $qb->getQuery()->getResult();
-    }
-
-    /**
-     * @param string $eventName
-     * @param string $entityClass
-     * 
-     * @return array
-     */
-    public function findAllByEventAndEntityClass($eventName, $entityClass)
-    {
-        return $this->findAllBy(compact('eventName', 'entityClass'));
-    }
-
-}

+ 4 - 4
Resources/translations/WorkflowBundle.es.yml

@@ -97,10 +97,10 @@ helps:
     workflow_label_template: "Sólo requiere definir 'places', 'transitions' y opcionalmente 'initial_place', el resto se completa desde los campos solicitados arriba"
     action_label_name: Nombre con el que se identificará a una Acción
     action_label_workflow_type: Tipo de Workflow.
-    action_label_workflow_name: Nombre del Workflow al cual hace referencia.
-    action_label_object_class: Clase a la que se vinculará la Acción
-    action_label_event: Tipo de evento[leave|transition|enter|guard] al cual responderá la Acción
-    action_label_event_reference: Nombre del estado o transición que realiza el Evento
+    action_label_workflow_name: Nombre del Workflow al cual hace referencia. Requerido si selecciona un evento WorkFlow
+    action_label_object_class: Clase a la que se vinculará la Acción. Requerido si selecciona un evento Doctrine
+    action_label_event: Tipo de evento[leave|transition|enter|guard] al cual responderá la Acción. Puede ser también un evento Doctrine
+    action_label_event_reference: Nombre del estado o transición que realiza el Evento. Requerido si selecciona un evento WorkFlow
     action_label_template: Template en twig con las acciones a realizar en dicha Acción
 Workflow: Workflow
 Action: Action

+ 2 - 0
Resources/translations/validators.es.yml

@@ -0,0 +1,2 @@
+error:
+    event_field_required: El campo es requerido

+ 8 - 7
Services/ProducerService.php

@@ -50,13 +50,14 @@ class ProducerService
 
         // la $entity esta dentro de las entidades con workflow
         if (in_array($entityClass, WorkFlowEntityClasses::getConstants())) {
-            $doctrine2WorkFlowActionRepository = $this->getEntityManager()->getRepository('WorkflowBundle:Doctrine2WorkFlowAction');
-            $doctrine2WorkFlowActions = $doctrine2WorkFlowActionRepository->findAllByEventAndEntityClass($eventName, $entityClass);
-            foreach ($doctrine2WorkFlowActions as $doctrine2WorkFlowAction) {
-                $actions = $doctrine2WorkFlowAction->getActions();
-                foreach ($actions as $action) {
-                    $this->publishMessage($action, $entity);
-                }
+            $actionRepository = $this->getEntityManager()->getRepository('WorkflowBundle:Action');
+            
+            /* @todo Refactorizar */
+            $actions = $actionRepository->findBy(array(
+                'objectClass' => $entityClass,
+            ));
+            foreach ($actions as $action) {
+                $this->publishMessage($action, $entity);
             }
         }
     }

+ 18 - 0
Utils/WorkFlowEvents.php

@@ -0,0 +1,18 @@
+<?php
+
+namespace WorkflowBundle\Utils;
+
+class WorkFlowEvents
+{
+
+    use ChoiceTrait;
+
+    const TRANSITION = 'transition';
+
+    const LEAVE = 'leave';
+
+    const ENTER = 'enter';
+
+    const GUARD = 'guard';
+
+}

+ 30 - 0
Validator/EventFieldsValidator.php

@@ -0,0 +1,30 @@
+<?php
+
+namespace WorkflowBundle\Validator;
+
+use Symfony\Component\Validator\Context\ExecutionContextInterface;
+use WorkflowBundle\Utils\DoctrineEvents;
+use WorkflowBundle\Utils\WorkFlowEvents;
+use WorkflowBundle\Entity\Action;
+
+class EventFieldsValidator
+{
+
+    /**
+     * @param string $value
+     * @param ExecutionContextInterface $context
+     * @param array $payload
+     */
+    public static function validate($value, ExecutionContextInterface $context, $payload)
+    {
+        /* @var $entity Action */
+        $entity = $context->getObject();
+        $class = isset($payload['doctrine']) ? DoctrineEvents::class : WorkFlowEvents::class;
+        if (!empty(array_intersect($entity->getEvent(), $class::getChoices())) && is_null($value)) {
+            $context->buildViolation('error.event_field_required')
+                    ->addViolation()
+                    ;
+        }
+    }
+
+}