public function delete(TemplateDeleteEvent $event, $eventName, EventDispatcherInterface $dispatcher)
    {
        if (null !== ($template = TemplateQuery::create()->findPk($event->getTemplateId()))) {
            // Check if template is used by a product
            $productCount = ProductQuery::create()->findByTemplateId($template->getId())->count();
            
            if ($productCount <= 0) {
                $con = Propel::getWriteConnection(TemplateTableMap::DATABASE_NAME);
                $con->beginTransaction();
                
                try {
                    $template
                        ->setDispatcher($dispatcher)
                        ->delete($con);
                    
                    // We have to also delete any reference of this template in category tables
                    // We can't use a FK here, as the DefaultTemplateId column may be NULL
                    // so let's take care of this.
                    CategoryQuery::create()
                        ->filterByDefaultTemplateId($event->getTemplateId())
                        ->update([ 'DefaultTemplateId' => null], $con);
                    
                    $con->commit();
                } catch (\Exception $ex) {
                    $con->rollback();
                    
                    throw $ex;
                }
            }
            
            $event->setTemplate($template);
            
            $event->setProductCount($productCount);
        }
    }