public function calculateEntityCount(DuplicateCriterion $criterion, array $options = null)
 {
     $entityTypeID = $this->getEntityTypeID();
     $enablePermissionCheck = $this->isPermissionCheckEnabled();
     $userID = $this->getUserID();
     $query = new Main\Entity\Query(DuplicateOrganizationMatchCodeTable::getEntity());
     $query->addSelect('QTY');
     $query->registerRuntimeField('', new Main\Entity\ExpressionField('QTY', 'COUNT(*)'));
     $query->addFilter('=ENTITY_TYPE_ID', $entityTypeID);
     if ($enablePermissionCheck) {
         $permissionSql = $this->preparePermissionSql();
         if ($permissionSql === false) {
             //Access denied;
             return 0;
         }
         if (is_string($permissionSql) && $permissionSql !== '') {
             $query->addFilter('@ENTITY_ID', new Main\DB\SqlExpression($permissionSql));
         }
     }
     $matches = $criterion->getMatches();
     $title = isset($matches['TITLE']) ? $matches['TITLE'] : '';
     if ($title === '') {
         throw new Main\ArgumentException("Parameter 'TITLE' is required.", 'matches');
     }
     $query->addFilter('=TITLE', $title);
     $rootEntityID = 0;
     if (is_array($options) && isset($options['ROOT_ENTITY_ID'])) {
         $rootEntityID = (int) $options['ROOT_ENTITY_ID'];
     }
     if ($rootEntityID > 0) {
         $query->addFilter('!ENTITY_ID', $rootEntityID);
         $query->addFilter('!@ENTITY_ID', DuplicateIndexMismatch::prepareQueryField($criterion, $entityTypeID, $rootEntityID, $userID));
     }
     $limit = 0;
     if (is_array($options) && isset($options['LIMIT'])) {
         $limit = (int) $options['LIMIT'];
     }
     if ($limit > 0) {
         $query->setLimit($limit);
     }
     $dbResult = $query->exec();
     $fields = $dbResult->fetch();
     return is_array($fields) && isset($fields['QTY']) ? intval($fields['QTY']) : 0;
 }
 public static function loadEntitiesMatches($entityTypeID, array $entityIDs)
 {
     $query = new Main\Entity\Query(DuplicateOrganizationMatchCodeTable::getEntity());
     $query->addFilter('=ENTITY_TYPE_ID', $entityTypeID);
     $query->addFilter('@ENTITY_ID', $entityIDs);
     $query->addSelect('ENTITY_ID');
     $query->addSelect('TITLE');
     $dbResult = $query->exec();
     $results = array();
     while ($fields = $dbResult->fetch()) {
         $entityID = isset($fields['ENTITY_ID']) ? (int) $fields['ENTITY_ID'] : 0;
         if ($entityID <= 0) {
             continue;
         }
         $results[$entityID] = array('TITLE' => isset($fields['TITLE']) ? $fields['TITLE'] : '');
     }
     return $results;
 }