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)))); }
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); }
public function Add(DateRange $blackoutDate, $resourceIds, $title, IReservationConflictResolution $reservationConflictResolution, IRepeatOptions $repeatOptions) { if (!$blackoutDate->GetEnd()->GreaterThan($blackoutDate->GetBegin())) { return new BlackoutDateTimeValidationResult(); } $userId = ServiceLocator::GetServer()->GetUserSession()->UserId; $blackoutSeries = BlackoutSeries::Create($userId, $title, $blackoutDate); $blackoutSeries->Repeats($repeatOptions); foreach ($resourceIds as $resourceId) { $blackoutSeries->AddResourceId($resourceId); } $conflictingBlackouts = $this->GetConflictingBlackouts($blackoutSeries); $conflictingReservations = array(); if (empty($conflictingBlackouts)) { $conflictingReservations = $this->GetConflictingReservations($blackoutSeries, $reservationConflictResolution); } $blackoutValidationResult = new BlackoutValidationResult($conflictingBlackouts, $conflictingReservations); if ($blackoutValidationResult->CanBeSaved()) { $this->blackoutRepository->Add($blackoutSeries); } return $blackoutValidationResult; }
public function testConflictHandlerReportsConflictingReservationAndDoesNotUpdateBlackout() { $userId = $this->fakeUser->UserId; $start = Date::Parse('2011-01-01 01:01:01'); $end = Date::Parse('2011-02-02 02:02:02'); $date = new DateRange($start, $end); $resourceId = 2; $resourceIds = array($resourceId); $title = 'title'; $seriesId = 111; $blackoutInstanceId = 10; $series = BlackoutSeries::Create(1, 'old title', new TestDateRange()); $series->WithId($seriesId); $user = $this->getMock('User'); $user->expects($this->any())->method('IsResourceAdminFor')->with($this->anything())->will($this->returnValue(true)); $this->userRepository->expects($this->once())->method('LoadById')->with($this->equalTo($userId))->will($this->returnValue($user)); $this->reservationViewRepository->expects($this->once())->method('GetBlackoutsWithin')->with($this->equalTo($date))->will($this->returnValue(array())); $reservation1 = new TestReservationItemView(1, $start, $end, 2); $reservation2 = new TestReservationItemView(2, $start, $end, 2); $this->reservationViewRepository->expects($this->once())->method('GetReservationList')->with($this->equalTo($start), $this->equalTo($end))->will($this->returnValue(array($reservation1, $reservation2))); $this->conflictHandler->expects($this->at(0))->method('Handle')->with($this->equalTo($reservation1))->will($this->returnValue(false)); $this->conflictHandler->expects($this->at(1))->method('Handle')->with($this->equalTo($reservation2))->will($this->returnValue(false)); $this->blackoutRepository->expects($this->never())->method('Update'); $this->blackoutRepository->expects($this->once())->method('LoadByBlackoutId')->with($this->equalTo($blackoutInstanceId))->will($this->returnValue($series)); $result = $this->service->Update($blackoutInstanceId, $date, $resourceIds, $title, $this->conflictHandler, new RepeatNone(), SeriesUpdateScope::FullSeries); $this->assertFalse($result->WasSuccessful()); }
public function testLoadsBlackoutSeriesByBlackoutId() { $series = BlackoutSeries::Create(1, 'title', new TestDateRange()); $repeatOptions = new RepeatDaily(1, Date::Now()); $series->WithRepeatOptions($repeatOptions); $series->AddResource(new BlackoutResource(1, 'r1', 1)); $series->AddResource(new BlackoutResource(2, 'r2', 1)); $config = $series->RepeatConfiguration(); $userTz = $this->fakeUser->Timezone; $id = 123; $this->page->expects($this->once())->method('GetBlackoutId')->will($this->returnValue($id)); $this->page->expects($this->once())->method('SetBlackoutResources')->with($this->equalTo($series->ResourceIds())); $this->page->expects($this->once())->method('SetTitle')->with($this->equalTo('title')); $this->page->expects($this->once())->method('SetRepeatType')->with($this->equalTo($config->Type)); $this->page->expects($this->once())->method('SetRepeatInterval')->with($this->equalTo($config->Interval)); $this->page->expects($this->once())->method('SetRepeatMonthlyType')->with($this->equalTo($config->MonthlyType)); $this->page->expects($this->once())->method('SetRepeatWeekdays')->with($this->equalTo($config->Weekdays)); $this->page->expects($this->once())->method('SetRepeatTerminationDate')->with($this->equalTo($config->TerminationDate->ToTimezone($userTz))); $this->page->expects($this->once())->method('SetBlackoutId')->with($this->equalTo($id)); $this->page->expects($this->once())->method('SetIsRecurring')->with($this->equalTo(true)); $this->page->expects($this->once())->method('SetBlackoutStartDate')->with($this->equalTo($series->CurrentBlackout()->StartDate()->ToTimezone($userTz))); $this->page->expects($this->once())->method('SetBlackoutEndDate')->with($this->equalTo($series->CurrentBlackout()->EndDate()->ToTimezone($userTz))); $this->page->expects($this->once())->method('SetWasBlackoutFound')->with($this->equalTo(true)); $this->page->expects($this->once())->method('ShowBlackout'); $this->blackoutsService->expects($this->once())->method('LoadBlackout')->with($this->equalTo($id), $this->equalTo($this->fakeUser->UserId))->will($this->returnValue($series)); $this->presenter->LoadBlackout(); }