public function guessBestPatternIdForEntity($refClass, $entityId, $em, $originalRefClassName = null)
    {
        $templateId = null;
        $refClassName = $em->getClassMetadata($refClass->name)->name;

        $viewReference = null;
        if (!$originalRefClassName) {
            $originalRefClassName = $refClassName;
        }

        $businessEntity = $this->businessEntityHelper->findByEntityClassname($refClassName);

        if ($businessEntity) {
            $parameters = [
                'entityId'        => $entityId,
                'entityNamespace' => $originalRefClassName,
            ];
            $viewReference = $this->viewReferenceRepository->getOneReferenceByParameters($parameters);
        }

        if (!$viewReference) {
            $parentRefClass = $refClass->getParentClass();
            if ($parentRefClass) {
                $templateId = $this->guessBestPatternIdForEntity($parentRefClass, $entityId, $em, $originalRefClassName);
            } else {
                throw new \Exception(sprintf('Cannot find a BusinessTemplate that can display the requested BusinessEntity ("%s", "%s".)', $refClassName, $entityId));
            }
        } elseif ($viewReference instanceof BusinessPageReference) {
            $templateId = $viewReference->getTemplateId();
        }

        return $templateId;
    }