<commit_before><?php

namespace Oro\Bundle\CalendarBundle\Entity\Repository;

use Doctrine\Common\Collections\Criteria;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\Query\Expr;
use Doctrine\ORM\QueryBuilder;

use Oro\Bundle\CalendarBundle\Entity\Attendee;
use Oro\Bundle\CalendarBundle\Entity\CalendarEvent;

class CalendarEventRepository extends EntityRepository
{
    const RECURRENCE_FIELD_PREFIX = 'recurrence';

    /**
     * Returns a query builder which can be used to get a list of user calendar events filtered by start and end dates
     *
     * @param \DateTime      $startDate
     * @param \DateTime      $endDate
     * @param array|Criteria $filters   Additional filtering criteria, e.g. ['allDay' => true, ...]
     *                                  or \Doctrine\Common\Collections\Criteria
     * @param array          $extraFields
     *
     * @return QueryBuilder
     */
    public function getUserEventListByTimeIntervalQueryBuilder($startDate, $endDate, $filters = [], $extraFields = [])
    {
        $qb = $this->getUserEventListQueryBuilder($filters, $extraFields);
        $this->addTimeIntervalFilter($qb, $startDate, $endDate);

        return $qb;
    }

    /**
     * Returns a query builder which can be used to get a list of user calendar events
     *
     * @param array|Criteria $filters Additional filtering criteria, e.g. ['allDay' => true, ...]
     *                                or \Doctrine\Common\Collections\Criteria
     * @param array          $extraFields
     *
     * @return QueryBuilder
     */
    public function getUserEventListQueryBuilder($filters = [], $extraFields = [])
    {
        $qb = $this->getEventListQueryBuilder($filters, $extraFields)
            ->addSelect(
                sprintf(
                    'COALESCE(status.id, \'%s\') as invitationStatus',
                    Attendee::STATUS_NONE
                )
            )
            ->addSelect('IDENTITY(e.parent) AS parentEventId')
            ->addSelect('c.id as calendar')
            ->addSelect('IDENTITY(relatedAttendee.user) AS relatedAttendeeUserId')
            ->leftJoin('e.relatedAttendee', 'relatedAttendee')
            ->leftJoin('e.parent', 'parent')
            ->leftJoin('relatedAttendee.status', 'status')
            ->innerJoin('e.calendar', 'c');

        $this->addRecurrenceData($qb);

        return $qb;
    }

    /**
     * @param CalendarEvent $event
     * @param int $calendarId
     * @return CalendarEvent[]
     */
    public function findDuplicatedEvent(CalendarEvent $event, int $calendarId)
    {
        $qb = $this->createQueryBuilder('ce');
        $qb
            ->where('ce.uid = :uid')
            ->andWhere('ce.calendar = :calendarId')
            ->andWhere($qb->expr()->isNull('ce.recurringEvent'))
            ->andWhere($qb->expr()->isNull('ce.parent'))
            ->setParameter('uid', $event->getUid())
            ->setParameter('calendarId', $calendarId);

        if ($event->getId()) {
            $qb
                ->andWhere('ce.id != :id')
                ->setParameter('id', $event->getId());
        }

        return $qb->getQuery()->execute();
    }

    /**
     * Returns a base query builder which can be used to get a list of calendar events
     *
     * @param array|Criteria $filters Additional filtering criteria, e.g. ['allDay' => true, ...]
     *                                or \Doctrine\Common\Collections\Criteria
     * @param array          $extraFields
     *
     * @return QueryBuilder
     */
    protected function getEventListQueryBuilder($filters = [], $extraFields = [])<commit_msg>BAP-15807: UID for the Calendar Events - fix broken test<commit_after><?php

namespace Oro\Bundle\CalendarBundle\Entity\Repository;

use Doctrine\Common\Collections\Criteria;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\Query\Expr;
use Doctrine\ORM\QueryBuilder;

use Oro\Bundle\CalendarBundle\Entity\Attendee;
use Oro\Bundle\CalendarBundle\Entity\CalendarEvent;

class CalendarEventRepository extends EntityRepository
{
    const RECURRENCE_FIELD_PREFIX = 'recurrence';

    /**
     * Returns a query builder which can be used to get a list of user calendar events filtered by start and end dates
     *
     * @param \DateTime      $startDate
     * @param \DateTime      $endDate
     * @param array|Criteria $filters   Additional filtering criteria, e.g. ['allDay' => true, ...]
     *                                  or \Doctrine\Common\Collections\Criteria
     * @param array          $extraFields
     *
     * @return QueryBuilder
     */
    public function getUserEventListByTimeIntervalQueryBuilder($startDate, $endDate, $filters = [], $extraFields = [])
    {
        $qb = $this->getUserEventListQueryBuilder($filters, $extraFields);
        $this->addTimeIntervalFilter($qb, $startDate, $endDate);

        return $qb;
    }

    /**
     * Returns a query builder which can be used to get a list of user calendar events
     *
     * @param array|Criteria $filters Additional filtering criteria, e.g. ['allDay' => true, ...]
     *                                or \Doctrine\Common\Collections\Criteria
     * @param array          $extraFields
     *
     * @return QueryBuilder
     */
    public function getUserEventListQueryBuilder($filters = [], $extraFields = [])
    {
        $qb = $this->getEventListQueryBuilder($filters, $extraFields)
            ->addSelect(
                sprintf(
                    'COALESCE(status.id, \'%s\') as invitationStatus',
                    Attendee::STATUS_NONE
                )
            )
            ->addSelect('IDENTITY(e.parent) AS parentEventId')
            ->addSelect('c.id as calendar')
            ->addSelect('IDENTITY(relatedAttendee.user) AS relatedAttendeeUserId')
            ->leftJoin('e.relatedAttendee', 'relatedAttendee')
            ->leftJoin('e.parent', 'parent')
            ->leftJoin('relatedAttendee.status', 'status')
            ->innerJoin('e.calendar', 'c');

        $this->addRecurrenceData($qb);

        return $qb;
    }

    /**
     * @param CalendarEvent $event
     * @param Calendar|int $calendarId
     * @return CalendarEvent[]
     */
    public function findDuplicatedEvent(CalendarEvent $event, $calendarId)
    {
        $qb = $this->createQueryBuilder('ce');
        $qb
            ->where('ce.uid = :uid')
            ->andWhere('ce.calendar = :calendarId')
            ->andWhere($qb->expr()->isNull('ce.recurringEvent'))
            ->andWhere($qb->expr()->isNull('ce.parent'))
            ->setParameter('uid', $event->getUid())
            ->setParameter('calendarId', $calendarId);

        if ($event->getId()) {
            $qb
                ->andWhere('ce.id != :id')
                ->setParameter('id', $event->getId());
        }

        return $qb->getQuery()->execute();
    }

    /**
     * Returns a base query builder which can be used to get a list of calendar events
     *
     * @param array|Criteria $filters Additional filtering criteria, e.g. ['allDay' => true, ...]
     *                                or \Doctrine\Common\Collections\Criteria
     * @param array          $extraFields
     *
     * @return QueryBuilder
     */
    protected function getEventListQueryBuilder($filters = [], $extraFields = [])
{'fraction_non_alphanumeric': 0.12925356750823272, 'fraction_numerical': 0.0006860592755214051, 'mean_word_length': 2.203956043956044, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 69, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6604300', 'n_tokens_mistral': 2044, 'n_tokens_neox': 1996, 'n_words': 443}