Пример #1
0
 public function testCanAddNewBlackoutSeries()
 {
     $seriesId = 9909870;
     $userId = 123;
     $resourceId = 555;
     $resourceId2 = 5552;
     $title = 'title';
     $start = Date::Parse('2000-01-01 4:44:44');
     $end = Date::Parse('2000-02-01 13:22:33');
     $date = new DateRange($start, $end);
     $series = BlackoutSeries::Create($userId, $title, $date);
     $series->AddResourceId($resourceId);
     $series->AddResourceId($resourceId2);
     $repeatOptions = new RepeatDaily(1, $start->AddDays(2));
     $series->Repeats($repeatOptions);
     $this->db->_ExpectedInsertId = $seriesId;
     $this->repository->Add($series);
     $addBlackoutCommand = new AddBlackoutCommand($userId, $title, $repeatOptions->RepeatType(), $repeatOptions->ConfigurationString());
     $addBlackoutResourceCommand1 = new AddBlackoutResourceCommand($seriesId, $resourceId);
     $addBlackoutResourceCommand2 = new AddBlackoutResourceCommand($seriesId, $resourceId2);
     $addBlackoutInstanceCommand = new AddBlackoutInstanceCommand($seriesId, $start, $end);
     $this->assertEquals($addBlackoutCommand, $this->db->_Commands[0]);
     $this->assertTrue($this->db->ContainsCommand($addBlackoutResourceCommand1));
     $this->assertTrue($this->db->ContainsCommand($addBlackoutResourceCommand2));
     $this->assertTrue($this->db->ContainsCommand($addBlackoutInstanceCommand));
     $this->assertTrue($this->db->ContainsCommand(new AddBlackoutInstanceCommand($seriesId, $start->AddDays(1), $end->AddDays(1))));
 }
Пример #2
0
    private function MigrateReservations(Database $legacyDatabase, Database $currentDatabase)
    {
        $reservationsMigrated = MigrationSession::GetLastReservationRow();
        $getMigratedCount = new AdHocCommand('SELECT
				(select count(*) from reservation_series where legacyid is not null) +
				(select count(*) from blackout_series where legacyid is not null )
				as count');
        $reader = ServiceLocator::GetDatabase()->Query($getMigratedCount);
        if ($row = $reader->GetRow()) {
            $reservationsMigrated = $row['count'];
        }
        Log::Debug('Start migrating reservations. Starting at row %s', $reservationsMigrated);
        $reservationRepository = new ReservationRepository();
        $blackoutRepository = new BlackoutRepository();
        $getLegacyReservations = new AdHocCommand("select r.resid, machid, scheduleid, start_date, end_date,\n            starttime, endtime, created, modified, parentid, is_blackout, is_pending, summary, allow_participation, allow_anon_participation,\n            ru.memberid\n            FROM reservations r INNER JOIN reservation_users ru ON r.resid = ru.resid AND owner = 1\n            ORDER BY r.resid LIMIT {$reservationsMigrated}, 100");
        $getExisting = new AdHocCommand('select legacyid from reservation_series');
        $reader = $currentDatabase->Query($getExisting);
        $knownIds = array();
        while ($row = $reader->GetRow()) {
            $knownIds[] = $row['legacyid'];
        }
        $getLegacyReservationAccessories = new AdHocCommand('SELECT resid, resourceid from reservation_resources');
        $getLegacyReservationParticipants = new AdHocCommand('SELECT resid, memberid, owner, invited  FROM reservation_users WHERE (owner is null or owner = 0)');
        $getAccessoryMapping = new AdHocCommand('select accessory_id, legacyid from accessories');
        $getUserMapping = new AdHocCommand('select user_id, legacyid from users');
        $getResourceMapping = new AdHocCommand('select resource_id, legacyid from resources');
        $accessoryMapping = array();
        $accessoryMappingReader = $currentDatabase->Query($getAccessoryMapping);
        while ($row = $accessoryMappingReader->GetRow()) {
            $legacyId = $row['legacyid'];
            $accessoryMapping[$legacyId] = $row['accessory_id'];
        }
        $userMapping = array();
        $userMappingReader = $currentDatabase->Query($getUserMapping);
        while ($row = $userMappingReader->GetRow()) {
            $legacyId = $row['legacyid'];
            $userMapping[$legacyId] = $row['user_id'];
        }
        $resourceMapping = array();
        $resourceMappingReader = $currentDatabase->Query($getResourceMapping);
        while ($row = $resourceMappingReader->GetRow()) {
            $legacyId = $row['legacyid'];
            $resourceMapping[$legacyId] = $row['resource_id'];
        }
        $reservationAccessories = array();
        $legacyAccessoryReader = $legacyDatabase->Query($getLegacyReservationAccessories);
        while ($row = $legacyAccessoryReader->GetRow()) {
            $resId = $row['resid'];
            if (!array_key_exists($resId, $reservationAccessories)) {
                $reservationAccessories[$resId] = array();
            }
            $reservationAccessories[$resId][] = $row['resourceid'];
        }
        $reservationParticipants = array();
        $legacyParticipantReader = $legacyDatabase->Query($getLegacyReservationParticipants);
        while ($row = $legacyParticipantReader->GetRow()) {
            $resId = $row['resid'];
            if (!array_key_exists($resId, $reservationParticipants)) {
                $reservationParticipants[$resId] = array();
            }
            $reservationParticipants[$resId][] = array('id' => $row['memberid'], 'invited' => $row['invited']);
        }
        $legacyReservationReader = $legacyDatabase->Query($getLegacyReservations);
        while ($row = $legacyReservationReader->GetRow()) {
            $legacyId = $row['resid'];
            if (in_array($legacyId, $knownIds)) {
                continue;
            }
            $date = $this->BuildDateRange($row['start_date'], $row['starttime'], $row['end_date'], $row['endtime']);
            $mappedUserId = $userMapping[$row['memberid']];
            $mappedResourceId = $resourceMapping[$row['machid']];
            if ($row['is_blackout'] == 1) {
                // handle blackout
                $blackout = BlackoutSeries::Create($mappedUserId, '', $date);
                $blackout->AddResourceId($mappedResourceId);
                $newId = $blackoutRepository->Add($blackout);
                $currentDatabase->Execute(new AdHocCommand("update blackout_series set legacyid = \"{$legacyId}\" where blackout_series_id = {$newId}"));
            } else {
                // handle reservation
                $mappedParticipantIds = array();
                $mappedInviteeIds = array();
                if (array_key_exists($legacyId, $reservationParticipants)) {
                    $legacyParticipants = $reservationParticipants[$legacyId];
                    foreach ($legacyParticipants as $legacyParticipantId) {
                        $userId = $userMapping[$legacyParticipantId['id']];
                        if (empty($legacyParticipantId['invited'])) {
                            $mappedParticipantIds[] = $userId;
                        } else {
                            $mappedInviteeIds[] = $userId;
                        }
                    }
                }
                $mappedAccessoryList = array();
                if (array_key_exists($legacyId, $reservationAccessories)) {
                    $legacyAccessories = $reservationAccessories[$legacyId];
                    foreach ($legacyAccessories as $legacyAccessoryId) {
                        if (array_key_exists($legacyAccessoryId, $accessoryMapping)) {
                            $mappedAccessoryId = $accessoryMapping[$legacyAccessoryId];
                            $mappedAccessoryList[] = new ReservationAccessory($mappedAccessoryId, 1);
                        }
                    }
                }
                $currentUser = new UserSession($row['memberid']);
                $currentUser->Timezone = Configuration::Instance()->GetDefaultTimezone();
                $mappedResource = new MigrateBookableResource($mappedResourceId);
                $reservation = ReservationSeries::Create($mappedUserId, $mappedResource, '', $row['summary'], $date, new RepeatNone(), $currentUser);
                foreach ($mappedAccessoryList as $accessory) {
                    $reservation->AddAccessory($accessory);
                }
                $reservation->ChangeParticipants($mappedParticipantIds);
                $reservation->ChangeInvitees($mappedInviteeIds);
                try {
                    $reservationRepository->Add($reservation);
                    $newId = $reservation->SeriesId();
                    $currentDatabase->Execute(new AdHocCommand("update reservation_series set legacyid = \"{$legacyId}\" where series_id = {$newId}"));
                } catch (Exception $ex) {
                    Log::Error('Error migrating reservation %s. Exception: %s', $legacyId, $ex);
                }
            }
            $reservationsMigrated++;
            MigrationSession::SetLastReservationRow($reservationsMigrated);
        }
        Log::Debug('Done migrating reservations (%s reservations)', $reservationsMigrated);
        $getLegacyCount = new AdHocCommand('select count(*) as count from reservations');
        //		$getMigratedCount = new AdHocCommand('SELECT
        //		(select count(*) from reservation_series where legacyid is not null) +
        //		(select count(*) from blackout_series where legacyid is not null )
        //		as count');
        $progressCounts = $this->GetProgressCounts($getLegacyCount, $getMigratedCount);
        $this->page->SetProgress($progressCounts);
        Log::Debug('There are %s total legacy reservations and %s already migrated. Progress is %s', $progressCounts->LegacyCount, $progressCounts->MigratedCount, $progressCounts->PercentComplete);
        $this->page->SetReservationsMigrated($progressCounts->MigratedCount);
        MigrationSession::SetLastReservationRow($progressCounts->MigratedCount);
    }