Skip to content

Commit

Permalink
Merge pull request #16 from monarc-project/bugfix/216
Browse files Browse the repository at this point in the history
Fixed the Anr creation
  • Loading branch information
ruslanbaydan authored Dec 18, 2019
2 parents 3597c76 + 1075f25 commit 2560489
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 56 deletions.
37 changes: 0 additions & 37 deletions src/Model/Entity/ObjectCategorySuperClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,30 +68,6 @@ class ObjectCategorySuperClass extends AbstractEntity
*/
protected $parent;

/**
* @var ObjectCategorySuperClass[]|ArrayCollection
*
* @ORM\OneToMany(targetEntity="ObjectCategory", mappedBy="parent")
*/
protected $children;

/**
* @var ArrayCollection|ObjectSuperClass[]
*
* @ORM\OneToMany(targetEntity="MonarcObject", orphanRemoval=true, mappedBy="category",
* cascade={"persist", "remove"}
* )
*/
protected $objects;

/**
* @return ArrayCollection|ObjectCategorySuperClass[]
*/
public function getChildren()
{
return $this->children;
}

/**
* @var string
*
Expand Down Expand Up @@ -215,19 +191,6 @@ public function setRoot($root): self
return $this;
}

/**
* @return ArrayCollection|ObjectSuperClass[]
*/
public function getObjects()
{
return $this->objects;
}

public function hasObjects(): bool
{
return (bool)\count($this->objects);
}

public function getInputFilter($partial = false)
{
if (!$this->inputFilter) {
Expand Down
9 changes: 8 additions & 1 deletion src/Model/Table/AnrObjectCategoryTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,14 @@ public function __construct(Db $dbService, ConnectedUserService $connectedUserSe
parent::__construct($dbService, AnrObjectCategory::class, $connectedUserService);
}

// TODO: in order to specify return object type we need to create a superclass for AnrObjectCategory.
/**
* TODO: in order to specify return object type we need to create a superclass for AnrObjectCategory.
*
* @param AnrSuperClass $anr
* @param ObjectCategorySuperClass $objectCategory
*
* @return AnrObjectCategory|null
*/
public function findOneByAnrAndObjectCategory(AnrSuperClass $anr, ObjectCategorySuperClass $objectCategory)
{
$result = $this->getRepository()
Expand Down
14 changes: 14 additions & 0 deletions src/Model/Table/MonarcObjectTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@

namespace Monarc\Core\Model\Table;

use Doctrine\ORM\Query\Expr;
use Monarc\Core\Model\Db;
use Monarc\Core\Model\Entity\MonarcObject;
use Monarc\Core\Model\Entity\ObjectCategorySuperClass;
use Monarc\Core\Service\ConnectedUserService;

/**
Expand Down Expand Up @@ -42,6 +44,18 @@ public function getGenericByAssetId($assetId)
return $objects;
}

public function hasObjectsUnderRootCategory(ObjectCategorySuperClass $rootCategory): bool
{
return (bool)$this->getRepository()
->createQueryBuilder('o')
->select('COUNT(o.uuid)')
->join('o.category', 'oc', Expr\Join::WITH, 'o.category = oc')
->where('oc.root = :rootCategory OR oc = :rootCategory')
->setParameter('rootCategory', $rootCategory)
->getQuery()
->getSingleScalarResult();
}

/**
* Check In Anr
*
Expand Down
25 changes: 7 additions & 18 deletions src/Service/ObjectService.php
Original file line number Diff line number Diff line change
Expand Up @@ -1465,14 +1465,18 @@ private function unlinkCategoryFromAnrIfNoObjectsOrChildrenLeft(
ObjectCategorySuperClass $objectCategory,
AnrSuperClass $anr
): void {
// Check if there are no more objects left under the category or its children ones.
if ($this->hasObjectsInsideOrUnderChildren($objectCategory)) {
/** @var MonarcObjectTable $monarcObjectTable */
$monarcObjectTable = $this->get('table');
// Check if there are no more objects left under the root category or its children ones.
$rootCategory = $objectCategory->getRoot() ?: $objectCategory;
if ($monarcObjectTable->hasObjectsUnderRootCategory($rootCategory)) {
return;
}

// Remove the relation with Anr (AnrObjectCategory) if exists.
/** @var AnrObjectCategoryTable $anrObjectCategoryTable */
$anrObjectCategoryTable = $this->get('anrObjectCategoryTable');

// Remove the relation with Anr (AnrObjectCategory) if exists.
$anrObjectCategory = $anrObjectCategoryTable->findOneByAnrAndObjectCategory($anr, $objectCategory);
if ($anrObjectCategory !== null) {
$anrObjectCategoryTable->delete($anrObjectCategory->getId());
Expand Down Expand Up @@ -1503,19 +1507,4 @@ private function linkCategoryWithAnrIfNotLinked(
$anrObjectCategoryTable->save($anrObjectCategory);
}

private function hasObjectsInsideOrUnderChildren(ObjectCategorySuperClass $objectCategory): bool
{
if ($objectCategory->hasObjects()) {
return true;
}

foreach ($objectCategory->getChildren() as $childCategory) {
$result = $this->hasObjectsInsideOrUnderChildren($childCategory);
if ($result) {
return true;
}
}

return false;
}
}

0 comments on commit 2560489

Please sign in to comment.