Correct event mamangement in hdabo_sf V00.02
authorymh <ymh.work@gmail.com>
Mon, 19 Dec 2011 17:50:04 +0100
changeset 65 ba6b8e38d90e
parent 64 caeb4c8b5487
child 66 868d0fcdbb99
Correct event mamangement in hdabo_sf
Controller/WikiTagController.php
Entity/DocumentRepository.php
Entity/DocumentTagRepository.php
Entity/TagRepository.php
Listener/DocumentListener.php
Model/Document.php
Model/DocumentInterface.php
Services/DocumentService.php
Tests/Services/DocumentServiceTest.php
--- a/Controller/WikiTagController.php	Wed Dec 14 23:53:37 2011 +0100
+++ b/Controller/WikiTagController.php	Mon Dec 19 17:50:04 2011 +0100
@@ -272,13 +272,15 @@
         
         try
         {
-            $this->get('wiki_tag.document')->addTag($id_doc, $tag_label);
+            $this->get('wiki_tag.document')->addTags($id_doc, $tag_label);
         }
         catch (WikiTagServiceException $e)
         {
             return new Response(json_encode(array('error' => $e->getErrorCode(), 'message' => $e->getMessage())),$e->getCode());
         }
+        $this->getDoctrine()->getEntityManager()->flush();
 
+        
         return $this->renderDocTags($id_doc, $this->getRequest()->request->get('wikitag_document_profile'));
     }
 
--- a/Entity/DocumentRepository.php	Wed Dec 14 23:53:37 2011 +0100
+++ b/Entity/DocumentRepository.php	Mon Dec 19 17:50:04 2011 +0100
@@ -214,7 +214,6 @@
         }
         
         $this->getEntityManager()->persist($baseDocument);
-        $this->getEntityManager()->flush();
         return $baseDocument;
     
     }
--- a/Entity/DocumentTagRepository.php	Wed Dec 14 23:53:37 2011 +0100
+++ b/Entity/DocumentTagRepository.php	Mon Dec 19 17:50:04 2011 +0100
@@ -37,6 +37,8 @@
         ->createQuery("SELECT MAX(doctag.tagOrder) FROM WikiTagBundle:DocumentTag doctag JOIN doctag.document doc WHERE doc.externalId= :doc_id")
         ->setParameter("doc_id", strval($doc_id))
         ->getResult();
+        
+        //todo: look in work unit
     }
     
     
--- a/Entity/TagRepository.php	Wed Dec 14 23:53:37 2011 +0100
+++ b/Entity/TagRepository.php	Mon Dec 19 17:50:04 2011 +0100
@@ -31,7 +31,7 @@
     public function getCompletion($seed)
     {
         $qb = $this->getEntityManager()->createQueryBuilder();
-        $qb->select('t.label');
+        $qb->select('DISTINCT t.label');
         $qb->from('WikiTagBundle:Tag','t');
         $qb->where($qb->expr()->orx(
             $qb->expr()->like('t.label',$qb->expr()->literal(addcslashes(mysql_real_escape_string($seed),"%_")."%")),
@@ -56,6 +56,7 @@
         $tag_label_normalized = WikiTagUtils::normalizeTag($tag_label);
         // We get the wikipedia references for the tag_label
         // We get or create the tag object
+        // TODO: search also in work unit ?
         $tags = $this->findBy(array('normalizedLabel' => $tag_label_normalized));
         $tag = null;
         foreach ($tags as $t) {
@@ -92,7 +93,6 @@
             // Save datas.
             $em = $this->getEntityManager();
             $em->persist($tag);
-            $em->flush();
                 
             $wikipedia_revision_id = $wp_response['revision_id'];
     
--- a/Listener/DocumentListener.php	Wed Dec 14 23:53:37 2011 +0100
+++ b/Listener/DocumentListener.php	Mon Dec 19 17:50:04 2011 +0100
@@ -74,6 +74,33 @@
     
     public function onFlush(OnFlushEventArgs $eventArgs)
     {
+        $logger = $this->container->get('logger');
+        $em = $eventArgs->getEntityManager();
+        $uow = $em->getUnitOfWork();
+        $class = $this->getContainer()->getParameter("wiki_tag.document_class");
+        
+        foreach ($uow->getScheduledEntityInsertions() as $entity) {
+            if (is_a($entity, $class)) {
+                $this->writeDoc($entity, $em);
+            }
+        }
+        
+        foreach ($uow->getScheduledEntityUpdates() as $entity) {
+            if (is_a($entity, $class)) {
+                $this->writeDoc($entity, $em);
+            }
+        }
+    }
+    
+    private function writeDoc($entity, $em)
+    {
+        $logger = $this->container->get('logger');
+        $classmd = $em->getClassMetadata("WikiTagBundle:Document");
+        $uow = $em->getUnitOfWork();
+        
+        $logger->debug("treating document : " . $entity->getId());
+        $doc = $this->container->get('doctrine')->getRepository("WikiTagBundle:Document")->writeDocument($entity, $this->getContainer()->getParameter("wiki_tag.document_id_column"), $this->getContainer()->getParameter("wiki_tag.fields"));
+        $uow->computeChangeSet($classmd, $doc);
         
     }
     
@@ -121,13 +148,7 @@
         $logger = $this->container->get('logger');
         $entity = $args->getEntity();
         
-        $class = $this->getContainer()->getParameter("wiki_tag.document_class");
-        if (is_a($entity, $class))
-        {
-            $logger->debug("treating document : " . $entity->getId());
-            $this->container->get('doctrine')->getRepository("WikiTagBundle:Document")->writeDocument($entity, $this->getContainer()->getParameter("wiki_tag.document_id_column"), $this->getContainer()->getParameter("wiki_tag.fields"));
-        }
-        elseif (is_a($entity, "IRI\Bundle\WikiTagBundle\Model\DocumentTagInterface"))
+        if (is_a($entity, "IRI\Bundle\WikiTagBundle\Model\DocumentTagInterface"))
         {
             $doc = $entity->getDocument();
             $this->updateTagsStr($doc);
--- a/Model/Document.php	Wed Dec 14 23:53:37 2011 +0100
+++ b/Model/Document.php	Mon Dec 19 17:50:04 2011 +0100
@@ -140,7 +140,10 @@
         $this->createdAt = new \DateTime("now", new \DateTimeZone('UTC'));
     }
     
-    
+    /**
+     * (non-PHPdoc)
+     * @see IRI\Bundle\WikiTagBundle\Model.DocumentInterface::__toString()
+     */
     public function __toString()
     {
         return print_r(Debug::export($this, 3),true);
--- a/Model/DocumentInterface.php	Wed Dec 14 23:53:37 2011 +0100
+++ b/Model/DocumentInterface.php	Mon Dec 19 17:50:04 2011 +0100
@@ -76,5 +76,9 @@
     */
     function getCreatedAt();
     
+    /**
+     * __toString magic method
+     */
+    function __toString();
             
 }
--- a/Services/DocumentService.php	Wed Dec 14 23:53:37 2011 +0100
+++ b/Services/DocumentService.php	Mon Dec 19 17:50:04 2011 +0100
@@ -85,15 +85,25 @@
      * Add a new tag to a "host" document.
      * If the label already exists, an exception is raised.
      *
-     * @param mixed $id_doc
+     * @param mixed $doc
      * @param string|array $tag_label : the label of the new tag
      */
-    public function addTags($id_doc, $tag_labels)
+    public function addTags($doc, $tag_labels)
     {
         // We get the DocumentTags
         $em = $this->getDoctrine()->getEntityManager();
+
+        $class = $this->getContainer()->getParameter("wiki_tag.document_class");
         
-        $need_flush = false;
+        if(! is_a($doc,"\IRI\Bundle\WikiTagBundle\Model\DocumentInterface")) {
+            if(is_a($doc, $class)) {
+                $doc_id = $doc->getId();
+            }
+            else {
+                $doc_id = $doc;
+            }
+            $doc = $this->getDoctrine()->getRepository('WikiTagBundle:Document')->findOneByExternalId($doc_id);
+        }
         
         
         if(!is_array($tag_labels)) {
@@ -105,7 +115,7 @@
             $normalized_tag_label = WikiTagUtils::normalizeTag($tag_label);
             
             $query = $em->createQuery("SELECT COUNT(dt.id) FROM WikiTagBundle:DocumentTag dt JOIN dt.tag t WHERE dt.document = :id_doc AND t.normalizedLabel = :label");
-            $query->setParameters(array("id_doc"=>$id_doc, "label"=>$normalized_tag_label));
+            $query->setParameters(array("id_doc"=>$doc, "label"=>$normalized_tag_label));
             
             $nb_tags = $query->getSingleScalarResult();
             
@@ -125,31 +135,27 @@
             $revision_id = $ar[1];
             $created = $ar[2];
             
-            $tags = $this->getDoctrine()->getRepository('WikiTagBundle:DocumentTag')->findByDocumentExternalId($id_doc, array('tag'=>$tag->getId()));
-            $nb_tags = count($tags);
-            
-            if($created==true || $nb_tags==0){
-                $new_order_ar = $this->getDoctrine()->getRepository('WikiTagBundle:DocumentTag')->getMaxOrder($id_doc);
+            if(!$created) {
+                $query = $em->createQuery("SELECT COUNT(dt.id) FROM WikiTagBundle:DocumentTag dt WHERE dt.document = :id_doc AND dt.tag = :tag");
+                $query->setParameters(array("id_doc"=>$doc, "tag"=>$tag));
+                $nb_tags = $query->getSingleScalarResult();
+            }
+                        
+            if($created || $nb_tags==0){
+                $new_order_ar = $this->getDoctrine()->getRepository('WikiTagBundle:DocumentTag')->getMaxOrder($doc);
                 // The result is a double array. And reset(reset($newOrderAr)) is not allowed. And a string is returned.
                 $a1 = reset($new_order_ar);
                 $new_order = intval(reset($a1)) + 1;
                 $new_DT = new DocumentTag();
-                $new_DT->setDocument($this->getDoctrine()->getRepository('WikiTagBundle:Document')->findOneByExternalId($id_doc));
+                $new_DT->setDocument($doc);
                 $new_DT->setTag($tag);
                 $new_DT->setOriginalOrder($new_order);
                 $new_DT->setTagOrder($new_order);
                 $new_DT->setWikipediaRevisionId($revision_id);
                 $em->persist($new_DT);
-                $need_flush = true;
-                
             }
         }
-        
-        if($need_flush) {
-            $em->flush();
-        }
-        
-        
+                
     }
     
     public function getTagLabels($id_doc)
--- a/Tests/Services/DocumentServiceTest.php	Wed Dec 14 23:53:37 2011 +0100
+++ b/Tests/Services/DocumentServiceTest.php	Mon Dec 19 17:50:04 2011 +0100
@@ -41,6 +41,87 @@
         }
         return $this->_doctrine;
     }
+        
+    
+    public function testInsert()
+    {
+        $newdoc = new \Company\BaseBundle\Entity\Document();
+        $newdoc->setTitle("Title 5");
+        $newdoc->setDescription("Description 5");
+        
+        $this->getDoctrine()->getEntityManager()->persist($newdoc);
+        
+        $this->getDoctrine()->getEntityManager()->flush();
+        
+        
+        $doc = $this->getDoctrine()->getRepository("WikiTagBundle:Document")->findOneByExternalId($newdoc->getId());
+        
+        $this->assertEquals("Title 5", $doc->getTitle());
+        $this->assertEquals("Description 5", $doc->getDescription());
+        
+        
+    }
+
+    public function testUpdate()
+    {
+        $hostdoc = $this->getDoctrine()->getRepository("CompanyBaseBundle:Document")->findOneByTitle("Title 3");
+        
+        $this->assertEquals("Title 3", $hostdoc->getTitle());
+        $this->assertEquals("Description 3", $hostdoc->getDescription());
+        
+        $hostdoc->setTitle("Title 3 modified");
+        $hostdoc->setDescription("Description 3 modified");
+        
+        $this->getDoctrine()->getEntityManager()->persist($hostdoc);
+        $this->getDoctrine()->getEntityManager()->flush();
+        
+        $doc = $this->getDoctrine()->getRepository("WikiTagBundle:Document")->findOneByExternalId($hostdoc->getId());
+        
+        $this->assertEquals("Title 3 modified", $doc->getTitle());
+        $this->assertEquals("Description 3 modified", $doc->getDescription());
+
+    }
+    
+    public function testUpdateCategory()
+    {
+        $hostdoc = $this->getDoctrine()->getRepository("CompanyBaseBundle:Document")->findOneByTitle("Title 5");
+        $cat = $this->getDoctrine()->getRepository("CompanyBaseBundle:category")->findOneByName("cat3");
+    
+        $this->assertEquals("Title 5", $hostdoc->getTitle());
+        $this->assertEquals("Description 5", $hostdoc->getDescription());
+        $this->assertEquals("cat1,cat2", $hostdoc->getCategoriesStr());
+        
+        
+        $hostdoc->setTitle("Title 5 modified");
+        $hostdoc->setDescription("Description 5 modified");
+        $hostdoc->getCategories()->add($cat);
+    
+        $this->getDoctrine()->getEntityManager()->persist($hostdoc);
+        $this->getDoctrine()->getEntityManager()->flush();
+    
+        $doc = $this->getDoctrine()->getRepository("WikiTagBundle:Document")->findOneByExternalId($hostdoc->getId());
+    
+        $this->assertEquals("Title 5 modified", $doc->getTitle());
+        $this->assertEquals("Description 5 modified", $doc->getDescription());
+        
+        
+        $this->assertEquals("cat1,cat2,cat3", $doc->getCategories());
+        
+    }
+    
+    
+    public function testDelete()
+    {
+        $hostdoc = $this->getDoctrine()->getRepository("CompanyBaseBundle:Document")->findOneByTitle("Title 10");
+        $hostdocid = $hostdoc->getId();
+        $this->getDoctrine()->getEntityManager()->remove($hostdoc);
+        $this->getDoctrine()->getEntityManager()->flush();
+        
+        $doc = $this->getDoctrine()->getRepository("WikiTagBundle:Document")->findOneByExternalId($hostdocid);
+        
+        //$this->assertNull($doc);
+        
+    }
     
     
     public function testGetTagLabels()