/**
  * Update criteriagroups for a condition
  *
  * @param StepCondition $conditionDB
  * @param int $level
  * @param array $criteriagroupsJS
  * @return array
  */
 protected function publishCriteriagroups(StepCondition $conditionDB, $level = 0, Criteriagroup $parentCG = null, array $criteriagroupsJS = array())
 {
     $processedCriteriagroups = array();
     $currentOrder = 0;
     // Retrieve existing criteriagroups for this condition
     $existingCriteriagroups = $conditionDB->getCriteriagroups();
     foreach ($criteriagroupsJS as $criteriagroupJS) {
         //echo "criteriagroupid ".$criteriagroupJS->id."<br>\n";
         // Current criteriagroup has never been published or criteriagroup entity has been deleted => create it
         if (empty($criteriagroupJS->cgid) || !$existingCriteriagroups->containsKey($criteriagroupJS->cgid)) {
             //echo "create group <br>\n";
             $criteriagroupDB = $this->stepConditionsManager->createCriteriagroup($level, $currentOrder, $parentCG, $conditionDB);
             $uniqId = "_CG" . uniqid();
             $this->uniqId2cg[$uniqId] = $criteriagroupDB;
             // Update json structure with new resource ID
             $criteriagroupJS->cgid = $uniqId;
         } else {
             //echo "edit group <br>\n";
             //retrieve CG
             $criteriagroupDB = $existingCriteriagroups->get($criteriagroupJS->cgid);
             //edit CG in DB
             $criteriagroupDB = $this->stepConditionsManager->editCriteriagroup($level, $currentOrder, $parentCG, $conditionDB, $criteriagroupDB);
         }
         //echo "Manage criteria <br>\n";
         // Manage criteria
         $existingCriteria = $criteriagroupDB->getCriteria();
         $publishedCriteria = $this->publishCriteria($criteriagroupJS, $criteriagroupDB);
         //echo "Clean criteria to remove <br>\n";
         // Clean criteria to remove
         $this->cleanCriteria($publishedCriteria, $existingCriteria->toArray(), $criteriagroupDB);
         // Store criteriagroup to know it doesn't have to be deleted when we will clean the condition
         $processedCriteriagroups[] = $criteriagroupDB;
         //Check children criteriagroup
         if (!empty($criteriagroupJS->criteriagroup)) {
             $childrenLevel = $level + 1;
             $childrenCriteriagroups = $this->publishCriteriagroups($conditionDB, $childrenLevel, $criteriagroupDB, $criteriagroupJS->criteriagroup);
             // Store children criteriagroup
             $processedCriteriagroups = array_merge($processedCriteriagroups, $childrenCriteriagroups);
         }
         $currentOrder++;
     }
     return $processedCriteriagroups;
 }