protected function prepareResult(array &$map, DedupeDataSourceResult $result)
 {
     $entityTypeID = $this->getEntityTypeID();
     foreach ($map as $matchHash => &$entry) {
         $primaryQty = isset($entry['PRIMARY']) ? count($entry['PRIMARY']) : 0;
         $secondaryQty = isset($entry['SECONDARY']) ? count($entry['SECONDARY']) : 0;
         if ($primaryQty > 1) {
             $matches = $this->getEntityMatchesByHash($entityTypeID, $entry['PRIMARY'][0], $matchHash);
             if (is_array($matches)) {
                 $criterion = $this->createCriterionFromMatches($matches);
                 $dup = new Duplicate($criterion, array());
                 foreach ($entry['PRIMARY'] as $entityID) {
                     $dup->addEntity(new DuplicateEntity($entityTypeID, $entityID));
                 }
                 $result->addItem($matchHash, $dup);
             }
         }
         if ($primaryQty > 0 && $secondaryQty > 0) {
             $matches = $this->loadEntitesMatches($entityTypeID, $entry['SECONDARY']);
             foreach ($matches as $entityID => $entityMatches) {
                 $criterion = $this->createCriterionFromMatches($entityMatches);
                 $entityMatchHash = $criterion->getMatchHash();
                 if ($entityMatchHash === '') {
                     continue;
                 }
                 $dup = $result->getItem($entityMatchHash);
                 if (!$dup) {
                     $dup = new Duplicate($criterion, array(new DuplicateEntity($entityTypeID, $entityID)));
                     $dup->setOption('enableOverwrite', false);
                     $dup->setRootEntityID($entityID);
                 }
                 $result->addItem($entityMatchHash, $dup);
                 foreach ($entry['PRIMARY'] as $primaryEntityID) {
                     $matches = $this->getEntityMatchesByHash($entityTypeID, $primaryEntityID, $matchHash);
                     if (is_array($matches)) {
                         $entity = new DuplicateEntity($entityTypeID, $primaryEntityID);
                         $entity->setCriterion($this->createCriterionFromMatches($matches));
                         $dup->addEntity($entity);
                     }
                 }
             }
         }
     }
     unset($entry);
 }
 protected function onAfterDuplicateCreated(Duplicate $dup, $entityTypeID, $userID, $enablePermissionCheck, $enableRanking, array &$rankings)
 {
     $name = $this->name;
     $lastName = $this->lastName;
     $secondName = $this->secondName;
     $rootEntityID = $dup->getRootEntityID();
     if ($secondName === '' && $name === '') {
         return;
     }
     $permissionSql = '';
     if ($enablePermissionCheck) {
         $permissions = isset($params['PERMISSIONS']) ? $params['PERMISSIONS'] : null;
         if ($permissions === null) {
             $permissions = \CCrmPerms::GetUserPermissions($userID);
         }
         $permissionSql = \CCrmPerms::BuildSql(\CCrmOwnerType::ResolveName($entityTypeID), '', 'READ', array('RAW_QUERY' => true, 'PERMS' => $permissions));
         if ($permissionSql === false) {
             //Access denied;
             return;
         }
     }
     if ($secondName !== '') {
         $query = new Main\Entity\Query(DuplicatePersonMatchCodeTable::getEntity());
         $query->addSelect('ENTITY_ID');
         $query->addFilter('=ENTITY_TYPE_ID', $entityTypeID);
         $query->addFilter('=LAST_NAME', $lastName);
         $query->addFilter('=NAME', $name);
         $query->addFilter('=SECOND_NAME', '');
         if ($rootEntityID) {
             $query->addFilter('!ENTITY_ID', $rootEntityID);
             $query->addFilter('!@ENTITY_ID', DuplicateIndexMismatch::prepareQueryField(self::createFromMatches(array('LAST_NAME' => $lastName, 'NAME' => $name)), $entityTypeID, $rootEntityID, $userID));
         }
         if ($enablePermissionCheck && $permissionSql !== '') {
             $query->addFilter('@ENTITY_ID', new Main\DB\SqlExpression($permissionSql));
         }
         $dbResult = $query->exec();
         while ($fields = $dbResult->fetch()) {
             $entityID = isset($fields['ENTITY_ID']) ? intval($fields['ENTITY_ID']) : 0;
             if ($entityID <= 0) {
                 continue;
             }
             $entity = new DuplicateEntity($entityTypeID, $entityID);
             $entity->setCriterion(self::createFromMatches(array('LAST_NAME' => $lastName, 'NAME' => $name)));
             if ($enableRanking) {
                 $rankings[] = $entity->getRanking();
             }
             $dup->addEntity($entity);
         }
     }
     if ($name !== '') {
         $query = new Main\Entity\Query(DuplicatePersonMatchCodeTable::getEntity());
         $query->addSelect('ENTITY_ID');
         $query->addFilter('=ENTITY_TYPE_ID', $entityTypeID);
         $query->addFilter('=LAST_NAME', $lastName);
         $query->addFilter('=NAME', '');
         $query->addFilter('=SECOND_NAME', '');
         if ($rootEntityID) {
             $query->addFilter('!ENTITY_ID', $rootEntityID);
             $query->addFilter('!@ENTITY_ID', DuplicateIndexMismatch::prepareQueryField(self::createFromMatches(array('LAST_NAME' => $lastName)), $entityTypeID, $rootEntityID, $userID));
         }
         if ($enablePermissionCheck && $permissionSql !== '') {
             $query->addFilter('@ENTITY_ID', new Main\DB\SqlExpression($permissionSql));
         }
         $dbResult = $query->exec();
         while ($fields = $dbResult->fetch()) {
             $entityID = isset($fields['ENTITY_ID']) ? intval($fields['ENTITY_ID']) : 0;
             if ($entityID <= 0) {
                 continue;
             }
             $entity = new DuplicateEntity($entityTypeID, $entityID);
             $entity->setCriterion(self::createFromMatches(array('LAST_NAME' => $lastName)));
             if ($enableRanking) {
                 $rankings[] = $entity->getRanking();
             }
             $dup->addEntity($entity);
         }
     }
 }