示例#1
0
文件: NGI.php 项目: Tom-Byrne/gocdb
 /**
  * Delete the given NGI and cascade delete all of the NGI's child entities. 
  * These include Sites, Services, EndpointLocations, Downtimes that will be 
  * orphaned, CertificationStatusLogs and Roles that previously linked to the 
  * deleted owned entities. 
  *  
  * @param \NGI $ngi
  * @param \User $user - must be an admin user 
  * @param boolean $logNgiSiteServiceInArchives Record the deletion of the ngi, 
  * its child sites and services in the archive tables. 
  * @throws \org\gocdb\services\Exception
  */
 public function deleteNgi(\NGI $ngi, \User $user = null, $logNgiSiteServiceInArchives = true)
 {
     require_once __DIR__ . '/../DAOs/SiteDAO.php';
     require_once __DIR__ . '/../DAOs/ServiceDAO.php';
     require_once __DIR__ . '/../DAOs/NGIDAO.php';
     require_once __DIR__ . '/ServiceService.php';
     //Check the portal is not in read only mode, throws exception if it is
     $this->checkPortalIsNotReadOnlyOrUserIsAdmin($user);
     //Throws exception if user is not an administrator
     $this->checkUserIsAdmin($user);
     $this->em->getConnection()->beginTransaction();
     try {
         $ngiDAO = new \NGIDAO();
         $ngiDAO->setEntityManager($this->em);
         $siteDAO = new \SiteDAO();
         $siteDAO->setEntityManager($this->em);
         $serviceDAO = new \ServiceDAO();
         $serviceDAO->setEntityManager($this->em);
         //Archive ngi
         if ($logNgiSiteServiceInArchives) {
             $ngiDAO->addNGIToArchive($ngi, $user);
         }
         //delete each child site
         foreach ($ngi->getSites() as $site) {
             //Archive site
             if ($logNgiSiteServiceInArchives) {
                 $siteDAO->addSiteToArchive($site, $user);
             }
             //delete each child service
             foreach ($site->getServices() as $service) {
                 //archive the srvice
                 if ($logNgiSiteServiceInArchives) {
                     $serviceDAO->addServiceToArchive($service, $user);
                 }
                 //remove the service (and any downtimes only associated with it)
                 $serviceDAO->removeService($service);
             }
             //remove the site
             $siteDAO->removeSite($site);
         }
         //remove the NGI
         $ngiDAO->removeNGI($ngi);
         $this->em->flush();
         $this->em->getConnection()->commit();
     } catch (\Exception $e) {
         $this->em->getConnection()->rollback();
         $this->em->close();
         throw $e;
     }
 }
 /**
  * Delete the ngi but not the sites 
  * @see testRolesCascadeDelete_OnOwnedEntityDeletion1
  */
 public function testRolesCascadeDelete_OnOwnedEntityDeletion3()
 {
     print __METHOD__ . "\n";
     include __DIR__ . '/resources/sampleFixtureData1.php';
     // Queue Deletion of ngi and assert that the relevant
     // ngi user roles were also deleted.
     $ngiDAO = new NGIDAO();
     $ngiDAO->setEntityManager($this->em);
     // need to delete the relationships between ngi and sites before deleting
     // the ngi - remember to unlink from both sides of the relationship
     $ngi->getSites()->removeElement($site1);
     $site1->setNgiDoJoin(null);
     $ngi->getSites()->removeElement($site2);
     $site2->setNgiDoJoin(null);
     // Now delete the NGI
     $ngiDAO->removeNGI($ngi);
     $this->em->flush();
     // Need to clear the identity map (all objects become detached) so that
     // when we re-fetch the user, it will be looked from db not served by entity map
     $this->em->clear();
     // Need to re-fetch the user from the DB again, if don't, then user already
     // has his eargerly fetched roles present in UserProxy object
     $userWithRoles = $this->em->find("User", $userId);
     // user should have 4 remaining roles still from sites that exist in DB
     $this->assertEquals(4, count($userWithRoles->getRoles()));
     $testConn = $this->getConnection();
     $result = $testConn->createQueryTable('results_table', "SELECT * FROM Roles");
     $this->assertTrue($result->getRowCount() == 4);
     $result = $testConn->createQueryTable('results_table', "SELECT * FROM Sites");
     $this->assertTrue($result->getRowCount() == 2);
     $result = $testConn->createQueryTable('results_table', "SELECT * FROM NGIs");
     $this->assertTrue($result->getRowCount() == 0);
 }